mesa: Add core API support for GL_ARB_stencil_texturing (from 4.3).
authorKenneth Graunke <kenneth@whitecape.org>
Mon, 24 Feb 2014 05:59:24 +0000 (21:59 -0800)
committerKenneth Graunke <kenneth@whitecape.org>
Wed, 5 Mar 2014 01:21:06 +0000 (17:21 -0800)
While the GL_ARB_stencil_texturing extension does not allow the creation
of stencil textures, it does allow shaders to sample stencil values
stored in packed depth/stencil textures.

Specifically, applications can call glTexParameter* with a pname of
GL_DEPTH_STENCIL_TEXTURE_MODE and value of either GL_DEPTH_COMPONENT or
GL_STENCIL_INDEX to select which component they wish to sample.  The
default value is GL_DEPTH_COMPONENT (for traditional depth sampling).

Shaders should use an unsigned integer sampler (presumably usampler2D)
to access stencil data.  Otherwise, results are undefined.  Using shadow
samplers with GL_STENCIL_INDEX selected also is undefined behavior.

This patch creates a new gl_texture_object field, StencilSampling, to
indicate that stencil should be sampled rather than depth.  (I chose to
use a boolean since I figured it would be more convenient for drivers.)
It also introduces the [Get]TexParameter code to get and set the value,
and of course the extension plumbing.

v2: Also consider textures incomplete when sampling stencil with
    non-NEAREST min/mag filters (caught by Eric Anholt).

Signed-off-by: Kenneth Graunke <kenneth@whitecape.org>
Reviewed-by: Matt Turner <mattst88@gmail.com>
Reviewed-by: Eric Anholt <eric@anholt.net>
docs/GL3.txt
src/mapi/glapi/gen/GL4x.xml
src/mesa/main/extensions.c
src/mesa/main/mtypes.h
src/mesa/main/texobj.c
src/mesa/main/texobj.h
src/mesa/main/texparam.c

index 755a89c0ff9819447ec6b253e5602e7d40675913..f0e95c563e5c00eec271b361151aae48cd9dfdb2 100644 (file)
@@ -158,7 +158,7 @@ GL 4.3:
   GL_ARB_robust_buffer_access_behavior                 not started
   GL_ARB_shader_image_size                             not started
   GL_ARB_shader_storage_buffer_object                  not started
-  GL_ARB_stencil_texturing                             not started
+  GL_ARB_stencil_texturing                             API exists, no drivers
   GL_ARB_texture_buffer_range                          DONE (nv50, nvc0, i965, r600, radeonsi)
   GL_ARB_texture_query_levels                          DONE (i965)
   GL_ARB_texture_storage_multisample                   DONE (all drivers that support GL_ARB_texture_multisample)
index 6706278cef841107c732d6617e9e2cac98a98005..8efef0b9159957940ae038e72156da25c7849370 100644 (file)
@@ -15,7 +15,7 @@
 </category>
 
 <category name="4.3">
-
+  <enum name="DEPTH_STENCIL_TEXTURE_MODE"              value="0x90EA"/>
 </category>
 
 </OpenGLAPI>
index c46d70b205506d40e3a5b482b064becd5f18bfe2..a72284c9a30cfddaf4f200d913d567456e76ec44 100644 (file)
@@ -138,6 +138,7 @@ static const struct extension extension_table[] = {
    { "GL_ARB_shading_language_packing",            o(ARB_shading_language_packing),            GL,             2011 },
    { "GL_ARB_shading_language_420pack",            o(ARB_shading_language_420pack),            GL,             2011 },
    { "GL_ARB_shadow",                              o(ARB_shadow),                              GLL,            2001 },
+   { "GL_ARB_stencil_texturing",                   o(ARB_stencil_texturing),                   GL,             2012 },
    { "GL_ARB_sync",                                o(ARB_sync),                                GL,             2003 },
    { "GL_ARB_texture_border_clamp",                o(ARB_texture_border_clamp),                GLL,            2000 },
    { "GL_ARB_texture_buffer_object",               o(ARB_texture_buffer_object),               GLC,            2008 },
index bbc377280b9af4f132082de673e97c9318d548ef..d05649ccc74dd141e594c3175df8f369bac8beb7 100644 (file)
@@ -1199,6 +1199,7 @@ struct gl_texture_object
    struct gl_sampler_object Sampler;
 
    GLenum DepthMode;           /**< GL_ARB_depth_texture */
+   bool StencilSampling;       /**< Should we sample stencil instead of depth? */
 
    GLfloat Priority;           /**< in [0,1] */
    GLint BaseLevel;            /**< min mipmap level, OpenGL 1.2 */
@@ -3526,6 +3527,7 @@ struct gl_extensions
    GLboolean ARB_shading_language_packing;
    GLboolean ARB_shading_language_420pack;
    GLboolean ARB_shadow;
+   GLboolean ARB_stencil_texturing;
    GLboolean ARB_sync;
    GLboolean ARB_texture_border_clamp;
    GLboolean ARB_texture_buffer_object;
index 3375fe36bc53b57ead7809bf7e63492f9aea65f2..8bdbb08c8ea36ded2f5988f00ca0287fcd21abc0 100644 (file)
@@ -148,6 +148,7 @@ _mesa_initialize_texture_object( struct gl_context *ctx,
    obj->Sampler.CompareMode = GL_NONE;         /* ARB_shadow */
    obj->Sampler.CompareFunc = GL_LEQUAL;       /* ARB_shadow */
    obj->DepthMode = ctx->API == API_OPENGL_CORE ? GL_RED : GL_LUMINANCE;
+   obj->StencilSampling = false;
    obj->Sampler.CubeMapSeamless = GL_FALSE;
    obj->Swizzle[0] = GL_RED;
    obj->Swizzle[1] = GL_GREEN;
@@ -280,6 +281,7 @@ _mesa_copy_texture_object( struct gl_texture_object *dest,
    dest->Sampler.CompareFunc = src->Sampler.CompareFunc;
    dest->Sampler.CubeMapSeamless = src->Sampler.CubeMapSeamless;
    dest->DepthMode = src->DepthMode;
+   dest->StencilSampling = src->StencilSampling;
    dest->Sampler.sRGBDecode = src->Sampler.sRGBDecode;
    dest->_MaxLevel = src->_MaxLevel;
    dest->_MaxLambda = src->_MaxLambda;
index a4573b399e3744207fc1e0aebb715f6d63c835c8..a9de73fff83327ed9c47a553af1cd044eee75b35 100644 (file)
@@ -114,6 +114,20 @@ _mesa_is_texture_complete(const struct gl_texture_object *texObj,
       return GL_FALSE;
    }
 
+   /* From the ARB_stencil_texturing specification:
+    * "Add a new bullet point for the conditions that cause the texture
+    *  to not be complete:
+    *
+    *  * The internal format of the texture is DEPTH_STENCIL, the
+    *    DEPTH_STENCIL_TEXTURE_MODE for the texture is STENCIL_INDEX and either
+    *    the magnification filter or the minification filter is not NEAREST."
+    */
+   if (texObj->StencilSampling &&
+       texObj->Image[0][texObj->BaseLevel]->_BaseFormat == GL_DEPTH_STENCIL &&
+       (sampler->MagFilter != GL_NEAREST || sampler->MinFilter != GL_NEAREST)) {
+      return GL_FALSE;
+   }
+
    if (_mesa_is_mipmap_filter(sampler))
       return texObj->_MipmapComplete;
    else
index e867dbf740c238cd5f5f8864e4733c84fd09af8c..bfb2e1b9f0cf5908c4c00edb0b512285dcd421ea 100644 (file)
@@ -451,6 +451,20 @@ set_tex_parameteri(struct gl_context *ctx,
       }
       goto invalid_pname;
 
+   case GL_DEPTH_STENCIL_TEXTURE_MODE:
+      if (_mesa_is_desktop_gl(ctx) && ctx->Extensions.ARB_stencil_texturing) {
+         bool stencil = params[0] == GL_STENCIL_INDEX;
+         if (!stencil && params[0] != GL_DEPTH_COMPONENT)
+            goto invalid_param;
+
+         if (texObj->StencilSampling == stencil)
+            return GL_FALSE;
+
+         texObj->StencilSampling = stencil;
+         return GL_TRUE;
+      }
+      goto invalid_pname;
+
    case GL_TEXTURE_CROP_RECT_OES:
       if (ctx->API != API_OPENGLES || !ctx->Extensions.OES_draw_texture)
          goto invalid_pname;
@@ -707,6 +721,7 @@ _mesa_TexParameterf(GLenum target, GLenum pname, GLfloat param)
    case GL_TEXTURE_COMPARE_MODE_ARB:
    case GL_TEXTURE_COMPARE_FUNC_ARB:
    case GL_DEPTH_TEXTURE_MODE_ARB:
+   case GL_DEPTH_STENCIL_TEXTURE_MODE:
    case GL_TEXTURE_SRGB_DECODE_EXT:
    case GL_TEXTURE_CUBE_MAP_SEAMLESS:
    case GL_TEXTURE_SWIZZLE_R_EXT:
@@ -762,6 +777,7 @@ _mesa_TexParameterfv(GLenum target, GLenum pname, const GLfloat *params)
    case GL_TEXTURE_COMPARE_MODE_ARB:
    case GL_TEXTURE_COMPARE_FUNC_ARB:
    case GL_DEPTH_TEXTURE_MODE_ARB:
+   case GL_DEPTH_STENCIL_TEXTURE_MODE:
    case GL_TEXTURE_SRGB_DECODE_EXT:
    case GL_TEXTURE_CUBE_MAP_SEAMLESS:
       {
@@ -1459,6 +1475,12 @@ _mesa_GetTexParameterfv( GLenum target, GLenum pname, GLfloat *params )
             goto invalid_pname;
          *params = (GLfloat) obj->DepthMode;
          break;
+      case GL_DEPTH_STENCIL_TEXTURE_MODE:
+         if (!_mesa_is_desktop_gl(ctx) || !ctx->Extensions.ARB_stencil_texturing)
+            goto invalid_pname;
+         *params = (GLfloat)
+            (obj->StencilSampling ? GL_STENCIL_INDEX : GL_DEPTH_COMPONENT);
+         break;
       case GL_TEXTURE_LOD_BIAS:
          if (_mesa_is_gles(ctx))
             goto invalid_pname;
@@ -1673,6 +1695,12 @@ _mesa_GetTexParameteriv( GLenum target, GLenum pname, GLint *params )
             goto invalid_pname;
          *params = (GLint) obj->DepthMode;
          break;
+      case GL_DEPTH_STENCIL_TEXTURE_MODE:
+         if (!_mesa_is_desktop_gl(ctx) || !ctx->Extensions.ARB_stencil_texturing)
+            goto invalid_pname;
+         *params = (GLint)
+            (obj->StencilSampling ? GL_STENCIL_INDEX : GL_DEPTH_COMPONENT);
+         break;
       case GL_TEXTURE_LOD_BIAS:
          if (_mesa_is_gles(ctx))
             goto invalid_pname;