Thursday, May 5, 2016

Sampler.Objects

[State]
    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: 
  1. // In your Init() or wherever you initialize  
  2. // named object that represents a texture  
  3. GLuint texture = 0;  
  4. //Create  texture object  
  5. glGenTextures(1 , &texture);  
  At This stage you have texture object.  Perhaps now you want to load an image from disk and use it as a texture: 


  1. // In your Init() or where ever you initalize  
  2. // Select texture object to associate with image data  
  3.  glBindTexture(GL_TEXTURE_2D , texture);  
  4. //Upload data to GPU  
  5.  glTexImage2D(.... , data);   
  6. // Switch to default texture object  
  7. glBindTexture(GL_TEXTURE_2D , 0);  
  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: 


  1. // In your Init() or wherever you initialize  
  2. // Select texture object  
  3. glBindTexture(GL_TEXTURE_2D , texture);  
  4. // Set sampler state  
  5. glTexParameteri(GL_TEXTURE_2D , GL_TEXTURE_MAG_FILTER , GL_LINEAR);  
  6. glTexParameteri(GL_TEXTURE_2D , GL_TEXTURE_MIN_FILTER , GL_LINEAR);  
  7. glTexParameteri(GL_TEXTURE_2D , GL_TEXTURE_WRAP_S , GL_CLAMP_TO_EDGE);  
  8. glTexParameteri(GL_TEXTURE_2D , GL_TEXTURE_WRAP_T , GL_CLAMP_TO_EDGE);  
  9. // Switch to default texture object  
  10. glBindTexture(GL_TEXTURE_2D , 0);  
  11. // -----------------------------------------------------------  
  12. // then in your render loop  
  13. glActiveTexture(GL_TEXTURE0);  
  14. glBindTexture(GL_TEXTURE_2D , texture);  
  15. glUniform1i(samplerId , 0);  
  16. DrawSomething( );  
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: 

  • 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.

  • Do bind – edit state – bind in your render loop. This may hurt performance.

These problems can be solved by using sampler objects.
...
[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