To Be Continued......
[Concept]
Sampler.Objects..OpenGL
[URL]
...
http://www.sinanc.org/blog/?p=215
[Contents]
...
[Questions Original]
...Sampler objects first introduced with OpenGL 3.3. This OpenGL object separates sampler state from texture data.
Textures are represented by texture objects in OpenGL and programmers use actual texture images via texture objects. Texture objects are not C++ classes or such. They are just names. Before OpenGL 3.3 you have to do following things to use a texture in your program:
At This stage you have texture object. Perhaps now you want to load an image from disk and use it as a texture:
- // In your Init() or wherever you initialize
- // named object that represents a texture
- GLuint texture = 0;
- //Create texture object
- glGenTextures(1 , &texture);
We are done with our texture object (we changed its state) and return to default texture object. Lets decide how our texture data is sampled:
- // In your Init() or where ever you initalize
- // Select texture object to associate with image data
- glBindTexture(GL_TEXTURE_2D , texture);
- //Upload data to GPU
- glTexImage2D(.... , data);
- // Switch to default texture object
- glBindTexture(GL_TEXTURE_2D , 0);
So as you see texture and operations performed on it are coupled. What if you want to use same texture with a different sampling state? Options are:
- // In your Init() or wherever you initialize
- // Select texture object
- glBindTexture(GL_TEXTURE_2D , texture);
- // Set sampler state
- glTexParameteri(GL_TEXTURE_2D , GL_TEXTURE_MAG_FILTER , GL_LINEAR);
- glTexParameteri(GL_TEXTURE_2D , GL_TEXTURE_MIN_FILTER , GL_LINEAR);
- glTexParameteri(GL_TEXTURE_2D , GL_TEXTURE_WRAP_S , GL_CLAMP_TO_EDGE);
- glTexParameteri(GL_TEXTURE_2D , GL_TEXTURE_WRAP_T , GL_CLAMP_TO_EDGE);
- // Switch to default texture object
- glBindTexture(GL_TEXTURE_2D , 0);
- // -----------------------------------------------------------
- // then in your render loop
- glActiveTexture(GL_TEXTURE0);
- glBindTexture(GL_TEXTURE_2D , texture);
- glUniform1i(samplerId , 0);
- DrawSomething( );
...
- Load same texture image from disk to GPU again. Associate image with different texture object and set new sampling state. This method duplicates data and wastes precious video memory.
These problems can be solved by using sampler objects.
- Do bind – edit state – bind in your render loop. This may hurt performance.
[Sampler.Objects]
...
Lets look at sampler objects usage.
First you define and create a sampler object:
// In your Init() or where ever you initalize
GLuint sampler = 0;
glGenSamplers(1 , &sampler);
Then set sampling parameters:
// In your Init() or where ever you initalize
glSamplerParameteri(sampler , GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glSamplerParameteri(sampler , GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glSamplerParameteri(sampler , GL_TEXTURE_MIN_FILTER , GL_LINEAR);
glSamplerParameteri(sampler , GL_TEXTURE_MAG_FILTER , GL_LINEAR);
As you see there is no binding.
This is an example of direct state access in OpenGL.
Although there is no binding mechanism for changing sampler state, sampler objects must be bound to a texture unit.
[]
[]
...
...
[]
[]
...
...
[]
[]
...
...
[]
No comments:
Post a Comment