CgFX samplers problem

category: general [glöplog]
I've got this particularly annoying problem with CgFX effects in OpenGL. I would appreciate any help with resolving this.

I have a simple effect file which textures an object without any lighting involved. The vertex shader just does the usual modelviewprojection transform and passes texcoords to the fragment shader. The fragment shader assings each fragment the color from a single sampler2D uniform parameter. In my engine I'm doing something like this:
Code:start first pass do { set sampler to texture 1 render object 1 set sampler to texture 2 render object 2 } while(next pass);

This results in every object to have the texture of the last set sampler (in this case object #1 would have the texture #2 instead of #1). I have cgGLSetManageTextureParameters set to CG_TRUE and I'm using cgGLSetupSampler to assign sampler variables appropriate OpenGL textures. What am I doing wrong?
added on the 2010-06-05 02:16:22 by masterm masterm
I don't know a whole lot about CG myself unfortunately, but assigning the sampler id aside, do you actually bind the texture? Because:
Code: glUniform1i(samplerLocationInProgram0, 0); glUniform1i(samplerLocationInProgram1, 0);

Isn't exactly the same thing as:
Code: glUniform1i(samplerLocationInProgram0, 0); glUniform1i(samplerLocationInProgram1, 0); glActiveTexture(GL_TEXTURE0); glBindTexture(GL_TEXTURE_2D, actualOpenGLNameForTheTexture0); // Render object 1 glBindTexture(GL_TEXTURE_2D, actualOpenGLNameForTheTexture1); // Render object 2

Other than these, make sure both of the GPU programs have 0 for their samplers. Also it might be good to remember that a GLSL Sampler ID is not the same thing as an OpenGL Texture Name.

So, I am unsure of your problem as you didn't provide actual code and I am not well informed on CG but I hope I could be of some help.
added on the 2010-06-05 02:24:33 by decipher decipher
wild guess!

start first pass
do {
render object 1
set sampler to texture 1
render object 2
set sampler to texture 2
} while(next pass);
Decipher: I am well aware of the fact that in GLSL you assign not the texture object itself but a unit number to the sampler, but it's not the case in Cg; at least when using cgGLSetupSampler. From Cg reference manual:
cgGLSetupSampler initializes a sampler like cgGLSetTextureParameter, it informs the OpenGL Cg runtime which OpenGL texture object to associate with the sampler.
So passing an actual texture object seems ok. Furthermore if no texture was bound by call to cgGLSetupSampler nothing would be visible on geometry, but all the objects on the scene are drawn using the last one set (which is even more weird because the last call to cgGLSetupSampler occurs after all but one object was drawn by calls to glDrawArrays).

whynot: That would be quite illogical but since I'm out of ideas (at least besides doing the pass loop separately for every object) I might actually try this just to see what happens.
added on the 2010-06-05 10:21:56 by masterm masterm
Is there any special reason to use CG instead of GLSL or HLSL?
added on the 2010-06-05 15:08:35 by xernobyl xernobyl
Cg is HLSL, just with a different name for branding purposes.
added on the 2010-06-06 12:55:21 by masterm masterm
Ok, for anyone interested it turns out that cgGLSetupSampler does not bind textures. It just schedules them for binding by cgSetPassState. So I rearranged some lines of my code and now everything works! :)
added on the 2010-06-07 01:19:34 by masterm masterm
do you actually bind the texture?

I knew it! :D
Anyway, congrats for tackling a deal-breaker. Now let's see the output! :)
added on the 2010-06-07 06:39:08 by decipher decipher
Haha. Thanks. :) Unfortunately it's not a demo but I'll try to make something for Assembly this year. ;)
added on the 2010-06-07 23:35:10 by masterm masterm