st/mesa: consolidate view format setup code
authorBrian Paul <brianp@vmware.com>
Fri, 23 Sep 2016 19:36:22 +0000 (13:36 -0600)
committerBrian Paul <brianp@vmware.com>
Thu, 6 Oct 2016 17:29:32 +0000 (11:29 -0600)
Before, we had code to compute the sampler view's format spread across two
different functions: in update_single_texture() and
st_get_texture_sampler_view_from_stobj().  Now it's all in one new function.

Also, use _mesa_texture_base_format() to simplify the code.

Reviewed-by: Edward O'Callaghan <funfunctor@folklore1984.net>
Reviewed-by: Nicolai Hähnle <nicolai.haehnle@amd.com>
Reviewed-by: Marek Olšák <marek.olsak@amd.com>
src/mesa/state_tracker/st_atom_texture.c

index 86f1c62e752245bae401100066a9c24a089e50e4..bfa16dc5ac707a18217e9ff59375ec28c6b00780 100644 (file)
@@ -271,6 +271,58 @@ last_layer(const struct st_texture_object *stObj)
    return stObj->pt->array_size - 1;
 }
 
+
+/**
+ * Determine the format for the texture sampler view.
+ */
+static enum pipe_format
+get_sampler_view_format(struct st_context *st,
+                        const struct st_texture_object *stObj,
+                        const struct gl_sampler_object *samp)
+{
+   enum pipe_format format;
+
+   if (stObj->base.Target == GL_TEXTURE_BUFFER) {
+      format =
+         st_mesa_format_to_pipe_format(st, stObj->base._BufferObjectFormat);
+   }
+   else {
+      format =
+         stObj->surface_based ? stObj->surface_format : stObj->pt->format;
+
+      if (util_format_is_depth_and_stencil(format)) {
+         if (stObj->base.StencilSampling) {
+            format = util_format_stencil_only(format);
+         }
+         else {
+            GLenum baseFormat = _mesa_texture_base_format(&stObj->base);
+            if (baseFormat == GL_STENCIL_INDEX) {
+               format = util_format_stencil_only(format);
+            }
+         }
+      }
+      else {
+         /* If sRGB decoding is off, use the linear format */
+         if (samp->sRGBDecode == GL_SKIP_DECODE_EXT) {
+            format = util_format_linear(format);
+         }
+
+         /* Use R8_UNORM for video formats */
+         switch (format) {
+         case PIPE_FORMAT_NV12:
+         case PIPE_FORMAT_IYUV:
+            format = PIPE_FORMAT_R8_UNORM;
+            break;
+         default:
+            break;
+         }
+      }
+   }
+
+   return format;
+}
+
+
 static struct pipe_sampler_view *
 st_create_texture_sampler_view_from_stobj(struct st_context *st,
                                          struct st_texture_object *stObj,
@@ -322,23 +374,13 @@ st_get_texture_sampler_view_from_stobj(struct st_context *st,
                                        unsigned glsl_version)
 {
    struct pipe_sampler_view **sv;
-   const struct st_texture_image *firstImage;
+
    if (!stObj || !stObj->pt) {
       return NULL;
    }
 
    sv = st_texture_get_sampler_view(st, stObj);
 
-   if (util_format_is_depth_and_stencil(format)) {
-      if (stObj->base.StencilSampling)
-         format = util_format_stencil_only(format);
-      else {
-         firstImage = st_texture_image_const(_mesa_base_tex_image(&stObj->base));
-         if (firstImage->base._BaseFormat == GL_STENCIL_INDEX)
-            format = util_format_stencil_only(format);
-      }
-   }
-
    /* if sampler view has changed dereference it */
    if (*sv) {
       if (check_sampler_swizzle(st, stObj, *sv, glsl_version) ||
@@ -395,29 +437,7 @@ update_single_texture(struct st_context *st,
       return GL_FALSE;
    }
 
-   /* Determine the format of the texture sampler view */
-   if (texObj->Target == GL_TEXTURE_BUFFER) {
-      view_format =
-         st_mesa_format_to_pipe_format(st, stObj->base._BufferObjectFormat);
-   }
-   else {
-      view_format =
-         stObj->surface_based ? stObj->surface_format : stObj->pt->format;
-
-      /* If sRGB decoding is off, use the linear format */
-      if (samp->sRGBDecode == GL_SKIP_DECODE_EXT) {
-         view_format = util_format_linear(view_format);
-      }
-   }
-
-   switch (view_format) {
-   case PIPE_FORMAT_NV12:
-   case PIPE_FORMAT_IYUV:
-      view_format = PIPE_FORMAT_R8_UNORM;
-      break;
-   default:
-      break;
-   }
+   view_format = get_sampler_view_format(st, stObj, samp);
 
    *sampler_view =
       st_get_texture_sampler_view_from_stobj(st, stObj, view_format,