i'm using OpenTK warpper for C#, shaders weren't running correctly (i want to generate vertex displacement shader using textures), so i pretend to use a gpu debugger to see what was happening.
The application is quite simple. It just creates a game window, load shaders, textures and render textured cubes (so it's working fine, i discovered the problem trying to use vertex displacement).
I used gDebugger and AMD CodeXL with same results. Debugger detects shaders, vbos, etc but never see allocated textures. This have non-sense because when i'm running application i see a textured cube spinning around the screen and debugger render the object on back/front buffer.
For reference, here is the texture-loading function:
int loadImage(Bitmap image,int tex)
{
int texID = GL.GenTexture();
GL.BindTexture(TextureTarget.Texture2D, texID);
System.Drawing.Imaging.BitmapData data = image.LockBits(new System.Drawing.Rectangle(0, 0, image.Width, image.Height),
System.Drawing.Imaging.ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
GL.TexImage2D(TextureTarget.Texture2D, 0, PixelInternalFormat.Rgba, data.Width, data.Height, 0,
OpenTK.Graphics.OpenGL.PixelFormat.Bgra, PixelType.UnsignedByte, data.Scan0);
image.UnlockBits(data);
return texID;
}
I was searching more information, but i couldn't find anything about this error, i'm not sure if the problem is in the wrapper, in the function or i must have something else considered
EDIT:
It seems that the problem is in the wrapper, OpenTK.BindTexture is different to native glBindTexture, so the profiler can't catch the call and that's why the textures are not shown. So the next step is look for the way to do native gl calls using OpenTK.
PROPOSED SOLUTION:
As i said, some functions of OpenTK wrapper are different to native glCalls, when you use functions like GL.BindTexture, GL.GenTexture (i suppose there are more functions, but i don't know yet), OpenTK uses overloaded calls that not match with the original calls and profilers can't catch it.
It's easy to check, just use OpenTK.GenTextures or GL.BindTextures adding breakpoints into profiler with those functions and they will never break.
Now, the solution, i was thinking about it and the conclussion was to replace some OpenTK calls with native gl.dll calls using GetProcAddress function.
This gives me some ideas: http://blogs.msdn.com/b/jonathanswift/archive/2006/10/03/dynamically-calling-an-unmanaged-dll-from-.net-_2800_c_23002900_.aspx
Using opengl32.dll included in the profiler, i use the same struct as in the previous link
static class NativeMethods
{
[DllImport("kernel32.dll")]
public static extern IntPtr LoadLibrary(string dllToLoad);
[DllImport("kernel32.dll")]
public static extern IntPtr GetProcAddress(IntPtr hModule, string procedureName);
[DllImport("kernel32.dll")]
public static extern bool FreeLibrary(IntPtr hModule);
}
This is added in the GameWindow class:
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
private delegate void BindTexture(OpenTK.Graphics.OpenGL.TextureTarget target, int texID);
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
private delegate void GenTexture(int n, int[] arr_text);
And here the new bindTexture function:
IntPtr pDll = NativeMethods.LoadLibrary(@"....\opengl32.dll");
IntPtr pAddressOfFunctionToCall = NativeMethods.GetProcAddress(pDll, "glBindTexture");
BindTexture bindTexture = (BindTexture)Marshal.GetDelegateForFunctionPointer(pAddressOfFunctionToCall, typeof(BindTexture));
bindTexture(OpenTK.Graphics.OpenGL.TextureTarget.Texture2D, arr_texture[0]);
Now if you try again with breakpoints in the profiler, it will break with glGenTextures and glBindTextures, recognising allocated textures.
I hope it helps.