A question about the sampler data type in shading language

Hi,

I am a newbie in OpenGL Shading language. May I ask a question about the sampler data type in shading language here?

The GLESSL spec. says sampler types can only be defined as uniform or function parameters especially in the texture functions.

For example, in the following fragment shader code snippet.
uniform sampler2D sampler2d;

gl_FragColor = texture2D(sampler2d, varCoord);

And in the c code, the Uniform varible sampler2d is being set like

glUniform1i(glGetUniformLocation(m_uiProgramObject, “sampler2d”), 0); // Sets the sampler2D variable to the first texture unit

What does a sampler really mean? Why setting the uniform varible sampler2d to 0 can “Sets the sampler2D variable to the first texture unit”?

br,

Mike

A sampler is the connection between the shader and a texture unit. The value you set using glUniform is the number of the texture unit you want to associate with that sampler variable.

So if you want a sampler to read from a certain texture, you bind that texture to a texture unit and set the sampler to the number of that texture unit, e.g.:

glActiveTexture(GL_TEXTURE3);
glBindTexture(GL_TEXTURE_2D, uiTex);
glUniform1i(glGetUniformLocation(uiProgram, "someSampler"), 3);

Thank you but what’s the difference between a sampler and a normal uniform? Why GLSL texture functions need such kind of sampler type to access texels?
:frowning:

The GLSL texture functions need to know which texture to read. That information is given using a sampler uniform.

Samplers are opaque handles to textures. You can’t modify them nor get their “value” in some way. The internal details of how a sampler refers to a texture are hidden which allows different ways to implement it in hardware.

When you set a sampler to 0 using glUniform, it doesn’t necessarily follow that the shader hardware actually uses the value 0 anywhere to look up which texture to use.

"When you set a sampler to 0 using glUniform, it doesn’t necessarily follow that the shader hardware actually uses the value 0 anywhere to look up which texture to use.[/quote]

oh, that’s interesting.

Thank you for your replies.

This topic was automatically closed 183 days after the last reply. New replies are no longer allowed.