Difference between revisions of "GL EXT texture integer"

From OpenGL Wiki
Jump to: navigation, search
m (Nearest filter mode)
(Sampling The Texture From GLSL: correcting mistakes)
Line 21: Line 21:
  
 
=== Sampling The Texture From GLSL ===
 
=== Sampling The Texture From GLSL ===
You can use the texture with fixed pipeline if you want but that doesn't really make it useful.<br>
+
GL_EXT_gpu_shader4 defines new samplers such as usampler2D, usampler3D, usamplerCube, usampler2DRect, isampler2D, isampler3D, isamplerCube, isampler2DRect, etc.
These formats are more useful if you need absolute integer values returned from the sampler for your GLSL code.<br>
+
 
GL_EXT_gpu_shader4 defines new samplers such as usampler2D, usampler3D, usamplerCube, usampler2DRect, etc.<br>
+
For example, usampler2D would be for sampling an unsigned integer texture such as a GL_LUMINANCE16UI_EXT or GL_LUMINANCE32UI_EXT.
Sampling from a fragment shader should work. Not sure if sampling from VS or GS is supported with these formats. Consult the IHVs manuals.<br>
+
 
 +
For example, isampler2D would be for sampling an signed integer texture such as a GL_LUMINANCE16I_EXT or GL_LUMINANCE32I_EXT.
 +
 
 +
Sampling from a fragment shader should work. Not sure if sampling from VS or GS is supported with these formats. Consult the IHVs manuals.
 +
 
 +
We will need GLSL 1.30.
 +
 
 
Example:
 
Example:
   //This is a FS
+
<pre>
 +
   #version 130
 +
 
 +
  in vec3 InVertex;
 +
  in vec2 InTexCoord0;
 +
 
 +
  uniform mat4 ProjectionModelviewMatrix;
 +
 
 +
  smooth out vec2 TexCoord0;
 +
 
 +
  void main()
 +
  {
 +
    gl_Position = ProjectionModelviewMatrix * vec4(InVertex, 1.0);
 +
    TexCoord0 = InTexCoord0;
 +
  }
 +
</pre>
 +
 
 +
Notice that the above is a GLSL 1.30 vertex shader, therefore we don't use varying. We use out.
 +
 
 +
Also, we are using the default interpolater mode : smooth.
 +
 
 +
<pre>
 +
  //FRAGMENT SHADER
 +
  #version 130
 +
 
 +
  #extension GL_EXT_gpu_shader4 : enable    //Include support for this extension, which defines usampler2D
 +
 
 
   uniform usampler2D Texture0;
 
   uniform usampler2D Texture0;
  //------------
+
 
   varying vec2 TexCoord0;
+
   smooth in vec2 TexCoord0;
  //------------
+
 
 
   void main()
 
   void main()
 
   {
 
   {
     unsigned int texel = texture2D(Texture0, TexCoord0).x;
+
     uint texel = texture2D(Texture0, TexCoord0).x;
     if(texel == 1000)
+
     if(texel == 1000U)
       gl_FragColor = vec4(0.0, 0.0, 1.0, 1.0);
+
       FragColor = vec4(0.0, 0.0, 1.0, 1.0);
 
     else
 
     else
       gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0);
+
       FragColor = vec4(1.0, 0.0, 0.0, 1.0);
 
   }
 
   }
 +
</pre>
 +
 +
The fragment shader defines its own output variable. Here we named it FragColor. This is required for GLSL 1.30.

Revision as of 02:06, 24 March 2011

GL_EXT_texture_integer adds some additional internal formats. It is core in GL 3.0, so you can drop the postfix if GL 3.0 is supported.
http://opengl.org/registry/specs/EXT/texture_integer.txt
This extension requires http://opengl.org/registry/specs/EXT/gpu_shader4.txt when you want to sample the texture from GLSL.
This extension is also core in GL 3.0.

These extensions are present on Gf 8 and up and possibly other GPUs.

Create The Texture

Here we are creating a 2D texture with GL_LUMINANCE16UI_EXT internal format. We won't be using mipmapping so that the values will be perfect. We will use NEAREST as the texture filtering mode.

 glGenTextures(1, &textureID);
 glBindTexture(GL_TEXTURE_2D, textureID);
 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
 glTexImage2D(GL_TEXTURE_2D, 0, GL_LUMINANCE16UI_EXT, width, height, 0, GL_LUMINANCE_INTEGER_EXT, GL_UNSIGNED_SHORT, pixels);

For cleanup

 glDeleteTextures(1, &textureID);

Sampling The Texture From GLSL

GL_EXT_gpu_shader4 defines new samplers such as usampler2D, usampler3D, usamplerCube, usampler2DRect, isampler2D, isampler3D, isamplerCube, isampler2DRect, etc.

For example, usampler2D would be for sampling an unsigned integer texture such as a GL_LUMINANCE16UI_EXT or GL_LUMINANCE32UI_EXT.

For example, isampler2D would be for sampling an signed integer texture such as a GL_LUMINANCE16I_EXT or GL_LUMINANCE32I_EXT.

Sampling from a fragment shader should work. Not sure if sampling from VS or GS is supported with these formats. Consult the IHVs manuals.

We will need GLSL 1.30.

Example:

  #version 130

  in vec3 InVertex;
  in vec2 InTexCoord0;

  uniform mat4 ProjectionModelviewMatrix;

  smooth out vec2 TexCoord0;

  void main()
  {
    gl_Position = ProjectionModelviewMatrix * vec4(InVertex, 1.0);
    TexCoord0 = InTexCoord0;
  }

Notice that the above is a GLSL 1.30 vertex shader, therefore we don't use varying. We use out.

Also, we are using the default interpolater mode : smooth.

  //FRAGMENT SHADER
  #version 130

  #extension GL_EXT_gpu_shader4 : enable    //Include support for this extension, which defines usampler2D

  uniform usampler2D Texture0;

  smooth in vec2 TexCoord0;

  void main()
  {
    uint texel = texture2D(Texture0, TexCoord0).x;
    if(texel == 1000U)
       FragColor = vec4(0.0, 0.0, 1.0, 1.0);
    else
       FragColor = vec4(1.0, 0.0, 0.0, 1.0);
  }

The fragment shader defines its own output variable. Here we named it FragColor. This is required for GLSL 1.30.