PDA

View Full Version : One texture, one shader, two samplers



maxest
05-21-2011, 12:15 PM
I'm having a weird problem. What I want: use one texture in one fragment shader (GLSL) using two different sampling modes, this case let it be nearest and linear on min/mag/mip. So the code:


glActiveTexture(GL_TEXTURE0 + 0);
glBindTexture(GL_TEXTURE_2D, texture1.id);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST_MIPMAP_NEAREST);

glActiveTexture(GL_TEXTURE0 + 1);
glBindTexture(GL_TEXTURE_2D, texture1.id);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);

int location = glGetUniformLocation(4, "diffuseMapSampler1");
glUniform1i(location, 0);
location = glGetUniformLocation(4, "diffuseMapSampler2");
glUniform1i(location, 1);

Draw();

And the shader:


uniform sampler2D diffuseMapSampler1;
uniform sampler2D diffuseMapSampler2;



varying vec3 varNormal;
varying vec2 varTexCoord;



void main(void)
{
gl_FragColor = vec4(1.0f);
#ifdef TEX
gl_FragColor *= texture2D(diffuseMapSampler1, varTexCoord);
#endif
}

As we can see, the shader should use texture1 with nearest sampling, but it uses linear sampling. What is interesting, when I change this piece of code:


glActiveTexture(GL_TEXTURE0 + 0);
glBindTexture(GL_TEXTURE_2D, texture1.id);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST_MIPMAP_NEAREST);

to this:


glActiveTexture(GL_TEXTURE0 + 0);
glBindTexture(GL_TEXTURE_2D, texture2.id);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST_MIPMAP_NEAREST);

i.e. I use a different texture, then I indeed see as a result this different texture with nearest sampling.

All this leads me to a conclusion that glTexParameter doesn't attach to a texture unit (glActiveTexture) but rather to a texture object (glBindTexture), what is incorrect.

Is it me who has forgotten something important here?

Alfonse Reinheart
05-21-2011, 12:29 PM
All this leads me to a conclusion that glTexParameter doesn't attach to a texture unit (glActiveTexture) but rather to a texture object (glBindTexture), what is incorrect.

What is "incorrect" about that; that's how it works. Texture parameter values (http://www.opengl.org/wiki/Texture#Texture_Parameters) are part of the texture object.

What you need is a Sampler Object (http://www.opengl.org/registry/specs/ARB/sampler_objects.txt).

maxest
05-21-2011, 12:40 PM
Yup, you're right. But here: http://www.opengl.org/sdk/docs/man/xhtml/glTexParameter.xml we read:
"For OpenGL versions 1.3 and greater, or when the ARB_multitexture extension is supported, glTexParameter specifies the texture parameters for the active *texture unit*, specified by calling glActiveTexture."
By *texture unit* I understand what is set via glActiveTexture. So I thought that glTexParameter, from GL 1.3 onwards, sets its parameters to tex units, not objects.
Do I really need OpenGL 3.2 to use different sampling modes for the same texture in OpenGL?

Alfonse Reinheart
05-21-2011, 12:55 PM
But here: http://www.opengl.org/sdk/docs/man/xhtml/glTexParameter.xml we read:

Then this text is misleading and should be changed. It should say, "the texture bound to the active texture unit, specified by calling glActiveTexture."


Do I really need OpenGL 3.2 to use different sampling modes for the same texture in OpenGL?

No, you just need to use ARB_sampler_object. Which is generally supported on all hardware still being supported by IHVs. For NVIDIA, that does include some pre-GL 3.x hardware (back to GeForce 6xxx's). But not for ATI; they only support their HD series, which is all 3.x capable hardware.

Note that ATI fixed a bug in sampler objects in 11.5a, where before this sampler objects just didn't work despite claiming to support them. So you'll need the 11.5a drivers to get access to them.

maxest
05-22-2011, 04:24 AM
Ok. Thanks for the clarification. Whom should I turn to with information about misleading GL doc statement?

V-man
05-22-2011, 06:32 AM
Well, in principle you should be able to change the properties of a texture at anytime, even the filter parameters.

Alfonse Reinheart
05-22-2011, 01:05 PM
Whom should I turn to with information about misleading GL doc statement?

Well, the technically correct answer is the Khronos Bugzilla (http://www.khronos.org/bugzilla/). However, I've filed several bugs there, and many of them don't ever get changed from the "NEW" state. This strongly suggests that they ignore them rather than assigning them.

You can try to file a bug report and see what happens. But I wouldn't get my hopes up.