[Mesa-dev] [PATCH 2/5] mesa: don't set _ClampFragmentColor to TRUE if it has no effect

Marek Olšák maraeo at gmail.com
Thu Mar 28 14:24:36 PDT 2013


This should reduce shader recompilations with drivers that emulate fragment
color clamping, because we want the clamping to be enabled only if there is
a signed normalized or floating-point colorbuffer.
---
 src/mesa/main/blend.c                       |    2 +-
 src/mesa/main/fbobject.c                    |    7 ++++++-
 src/mesa/main/framebuffer.c                 |    1 +
 src/mesa/main/get.c                         |    9 +++++----
 src/mesa/main/mtypes.h                      |    1 +
 src/mesa/main/state.c                       |   12 +++++++++++-
 src/mesa/main/texenv.c                      |    3 ++-
 src/mesa/main/texparam.c                    |    3 ++-
 src/mesa/program/prog_statevars.c           |    5 +++--
 src/mesa/state_tracker/st_atom_rasterizer.c |    3 +--
 src/mesa/state_tracker/st_atom_shader.c     |    3 +--
 src/mesa/state_tracker/st_cb_bitmap.c       |    3 +--
 src/mesa/state_tracker/st_cb_drawpixels.c   |    6 ++----
 13 files changed, 37 insertions(+), 21 deletions(-)

diff --git a/src/mesa/main/blend.c b/src/mesa/main/blend.c
index 876cbf2..c1b49b1 100644
--- a/src/mesa/main/blend.c
+++ b/src/mesa/main/blend.c
@@ -860,7 +860,7 @@ void _mesa_init_color( struct gl_context * ctx )
    }
 
    ctx->Color.ClampFragmentColor = GL_FIXED_ONLY_ARB;
-   ctx->Color._ClampFragmentColor = GL_TRUE;
+   ctx->Color._ClampFragmentColor = GL_FALSE;
    ctx->Color.ClampReadColor = GL_FIXED_ONLY_ARB;
 
    if (ctx->API == API_OPENGLES2) {
diff --git a/src/mesa/main/fbobject.c b/src/mesa/main/fbobject.c
index 67c1161..fc2b262 100644
--- a/src/mesa/main/fbobject.c
+++ b/src/mesa/main/fbobject.c
@@ -785,6 +785,7 @@ _mesa_test_framebuffer_completeness(struct gl_context *ctx,
    fb->Width = 0;
    fb->Height = 0;
    fb->_AllColorBuffersFixedPoint = GL_TRUE;
+   fb->_HasSNormOrFloatColorBuffer = GL_FALSE;
 
    /* Start at -2 to more easily loop over all attachment points.
     *  -2: depth buffer
@@ -901,13 +902,17 @@ _mesa_test_framebuffer_completeness(struct gl_context *ctx,
       /* check if integer color */
       fb->_IntegerColor = _mesa_is_format_integer_color(attFormat);
 
-      /* Update _AllColorBuffersFixedPoint. */
+      /* Update _AllColorBuffersFixedPoint and _HasSNormOrFloatColorBuffer. */
       if (i >= 0) {
          GLenum type = _mesa_get_format_datatype(attFormat);
 
          fb->_AllColorBuffersFixedPoint =
             fb->_AllColorBuffersFixedPoint &&
             (type == GL_UNSIGNED_NORMALIZED || type == GL_SIGNED_NORMALIZED);
+
+         fb->_HasSNormOrFloatColorBuffer =
+            fb->_HasSNormOrFloatColorBuffer ||
+            type == GL_SIGNED_NORMALIZED || type == GL_FLOAT;
       }
 
       /* Error-check width, height, format */
diff --git a/src/mesa/main/framebuffer.c b/src/mesa/main/framebuffer.c
index 5fb3677..757c4b0 100644
--- a/src/mesa/main/framebuffer.c
+++ b/src/mesa/main/framebuffer.c
@@ -155,6 +155,7 @@ _mesa_initialize_window_framebuffer(struct gl_framebuffer *fb,
    fb->Delete = _mesa_destroy_framebuffer;
    fb->_Status = GL_FRAMEBUFFER_COMPLETE_EXT;
    fb->_AllColorBuffersFixedPoint = !visual->floatMode;
+   fb->_HasSNormOrFloatColorBuffer = visual->floatMode;
 
    compute_depth_max(fb);
 }
diff --git a/src/mesa/main/get.c b/src/mesa/main/get.c
index 582ef31..244e3ea 100644
--- a/src/mesa/main/get.c
+++ b/src/mesa/main/get.c
@@ -24,6 +24,7 @@
 
 #include "glheader.h"
 #include "context.h"
+#include "blend.h"
 #include "enable.h"
 #include "enums.h"
 #include "extensions.h"
@@ -767,13 +768,13 @@ find_custom_value(struct gl_context *ctx, const struct value_desc *d, union valu
       break;
 
    case GL_FOG_COLOR:
-      if(ctx->Color._ClampFragmentColor)
+      if (_mesa_get_clamp_fragment_color(ctx))
          COPY_4FV(v->value_float_4, ctx->Fog.Color);
       else
          COPY_4FV(v->value_float_4, ctx->Fog.ColorUnclamped);
       break;
    case GL_COLOR_CLEAR_VALUE:
-      if(ctx->Color._ClampFragmentColor) {
+      if (_mesa_get_clamp_fragment_color(ctx)) {
          v->value_float_4[0] = CLAMP(ctx->Color.ClearColor.f[0], 0.0F, 1.0F);
          v->value_float_4[1] = CLAMP(ctx->Color.ClearColor.f[1], 0.0F, 1.0F);
          v->value_float_4[2] = CLAMP(ctx->Color.ClearColor.f[2], 0.0F, 1.0F);
@@ -782,13 +783,13 @@ find_custom_value(struct gl_context *ctx, const struct value_desc *d, union valu
          COPY_4FV(v->value_float_4, ctx->Color.ClearColor.f);
       break;
    case GL_BLEND_COLOR_EXT:
-      if(ctx->Color._ClampFragmentColor)
+      if (_mesa_get_clamp_fragment_color(ctx))
          COPY_4FV(v->value_float_4, ctx->Color.BlendColor);
       else
          COPY_4FV(v->value_float_4, ctx->Color.BlendColorUnclamped);
       break;
    case GL_ALPHA_TEST_REF:
-      if(ctx->Color._ClampFragmentColor)
+      if (_mesa_get_clamp_fragment_color(ctx))
          v->value_float = ctx->Color.AlphaRef;
       else
          v->value_float = ctx->Color.AlphaRefUnclamped;
diff --git a/src/mesa/main/mtypes.h b/src/mesa/main/mtypes.h
index 8c64a5b..575db90 100644
--- a/src/mesa/main/mtypes.h
+++ b/src/mesa/main/mtypes.h
@@ -2671,6 +2671,7 @@ struct gl_framebuffer
 
    /* ARB_color_buffer_float */
    GLboolean _AllColorBuffersFixedPoint; /* no integer, no float */
+   GLboolean _HasSNormOrFloatColorBuffer;
 
    /** Array of all renderbuffer attachments, indexed by BUFFER_* tokens. */
    struct gl_renderbuffer_attachment Attachment[BUFFER_COUNT];
diff --git a/src/mesa/main/state.c b/src/mesa/main/state.c
index c94a244..73c5a1c 100644
--- a/src/mesa/main/state.c
+++ b/src/mesa/main/state.c
@@ -314,7 +314,17 @@ update_multisample(struct gl_context *ctx)
 static void
 update_clamp_fragment_color(struct gl_context *ctx)
 {
-   ctx->Color._ClampFragmentColor = _mesa_get_clamp_fragment_color(ctx);
+   struct gl_framebuffer *fb = ctx->DrawBuffer;
+
+   /* Don't clamp if:
+    * - there is no colorbuffer
+    * - all colorbuffers are unsigned normalized, so clamping has no effect
+    * - there is an integer colorbuffer
+    */
+   if (!fb || !fb->_HasSNormOrFloatColorBuffer || fb->_IntegerColor)
+      ctx->Color._ClampFragmentColor = GL_FALSE;
+   else
+      ctx->Color._ClampFragmentColor = _mesa_get_clamp_fragment_color(ctx);
 }
 
 
diff --git a/src/mesa/main/texenv.c b/src/mesa/main/texenv.c
index 0fe5fbd..2979e67 100644
--- a/src/mesa/main/texenv.c
+++ b/src/mesa/main/texenv.c
@@ -32,6 +32,7 @@
 
 #include "main/glheader.h"
 #include "main/context.h"
+#include "main/blend.h"
 #include "main/enums.h"
 #include "main/macros.h"
 #include "main/mtypes.h"
@@ -680,7 +681,7 @@ _mesa_GetTexEnvfv( GLenum target, GLenum pname, GLfloat *params )
       if (pname == GL_TEXTURE_ENV_COLOR) {
          if(ctx->NewState & (_NEW_BUFFERS | _NEW_FRAG_CLAMP))
             _mesa_update_state(ctx);
-         if(ctx->Color._ClampFragmentColor)
+         if (_mesa_get_clamp_fragment_color(ctx))
             COPY_4FV( params, texUnit->EnvColor );
          else
             COPY_4FV( params, texUnit->EnvColorUnclamped );
diff --git a/src/mesa/main/texparam.c b/src/mesa/main/texparam.c
index bd2f751..224aaee 100644
--- a/src/mesa/main/texparam.c
+++ b/src/mesa/main/texparam.c
@@ -31,6 +31,7 @@
 
 #include <stdbool.h>
 #include "main/glheader.h"
+#include "main/blend.h"
 #include "main/colormac.h"
 #include "main/context.h"
 #include "main/enums.h"
@@ -1329,7 +1330,7 @@ _mesa_GetTexParameterfv( GLenum target, GLenum pname, GLfloat *params )
 
          if (ctx->NewState & (_NEW_BUFFERS | _NEW_FRAG_CLAMP))
             _mesa_update_state_locked(ctx);
-         if (ctx->Color._ClampFragmentColor) {
+         if (_mesa_get_clamp_fragment_color(ctx)) {
             params[0] = CLAMP(obj->Sampler.BorderColor.f[0], 0.0F, 1.0F);
             params[1] = CLAMP(obj->Sampler.BorderColor.f[1], 0.0F, 1.0F);
             params[2] = CLAMP(obj->Sampler.BorderColor.f[2], 0.0F, 1.0F);
diff --git a/src/mesa/program/prog_statevars.c b/src/mesa/program/prog_statevars.c
index ccc590d..09d2a56 100644
--- a/src/mesa/program/prog_statevars.c
+++ b/src/mesa/program/prog_statevars.c
@@ -31,6 +31,7 @@
 
 #include "main/glheader.h"
 #include "main/context.h"
+#include "main/blend.h"
 #include "main/imports.h"
 #include "main/macros.h"
 #include "main/mtypes.h"
@@ -239,14 +240,14 @@ _mesa_fetch_state(struct gl_context *ctx, const gl_state_index state[],
       {
          /* state[1] is the texture unit */
          const GLuint unit = (GLuint) state[1];
-         if(ctx->Color._ClampFragmentColor)
+         if (_mesa_get_clamp_fragment_color(ctx))
             COPY_4V(value, ctx->Texture.Unit[unit].EnvColor);
          else
             COPY_4V(value, ctx->Texture.Unit[unit].EnvColorUnclamped);
       }
       return;
    case STATE_FOG_COLOR:
-      if(ctx->Color._ClampFragmentColor)
+      if (_mesa_get_clamp_fragment_color(ctx))
          COPY_4V(value, ctx->Fog.Color);
       else
          COPY_4V(value, ctx->Fog.ColorUnclamped);
diff --git a/src/mesa/state_tracker/st_atom_rasterizer.c b/src/mesa/state_tracker/st_atom_rasterizer.c
index 866e562..62464b4 100644
--- a/src/mesa/state_tracker/st_atom_rasterizer.c
+++ b/src/mesa/state_tracker/st_atom_rasterizer.c
@@ -227,8 +227,7 @@ static void update_raster_state( struct st_context *st )
 
    /* _NEW_FRAG_CLAMP */
    raster->clamp_fragment_color = !st->clamp_frag_color_in_shader &&
-                                  ctx->Color._ClampFragmentColor &&
-                                  !ctx->DrawBuffer->_IntegerColor;
+                                  ctx->Color._ClampFragmentColor;
    raster->gl_rasterization_rules = 1;
 
    /* _NEW_RASTERIZER_DISCARD */
diff --git a/src/mesa/state_tracker/st_atom_shader.c b/src/mesa/state_tracker/st_atom_shader.c
index c1d7c80..c0239e9 100644
--- a/src/mesa/state_tracker/st_atom_shader.c
+++ b/src/mesa/state_tracker/st_atom_shader.c
@@ -86,8 +86,7 @@ update_fp( struct st_context *st )
 
    /* _NEW_FRAG_CLAMP */
    key.clamp_color = st->clamp_frag_color_in_shader &&
-                     st->ctx->Color._ClampFragmentColor &&
-                     !st->ctx->DrawBuffer->_IntegerColor;
+                     st->ctx->Color._ClampFragmentColor;
 
    st->fp_variant = st_get_fp_variant(st, stfp, &key);
 
diff --git a/src/mesa/state_tracker/st_cb_bitmap.c b/src/mesa/state_tracker/st_cb_bitmap.c
index bae9ff8..a838412 100644
--- a/src/mesa/state_tracker/st_cb_bitmap.c
+++ b/src/mesa/state_tracker/st_cb_bitmap.c
@@ -417,8 +417,7 @@ draw_bitmap_quad(struct gl_context *ctx, GLint x, GLint y, GLfloat z,
    key.st = st;
    key.bitmap = GL_TRUE;
    key.clamp_color = st->clamp_frag_color_in_shader &&
-                     st->ctx->Color._ClampFragmentColor &&
-                     !st->ctx->DrawBuffer->_IntegerColor;
+                     st->ctx->Color._ClampFragmentColor;
 
    fpv = st_get_fp_variant(st, st->fp, &key);
 
diff --git a/src/mesa/state_tracker/st_cb_drawpixels.c b/src/mesa/state_tracker/st_cb_drawpixels.c
index f0baa34..d5b6b50 100644
--- a/src/mesa/state_tracker/st_cb_drawpixels.c
+++ b/src/mesa/state_tracker/st_cb_drawpixels.c
@@ -707,8 +707,7 @@ draw_textured_quad(struct gl_context *ctx, GLint x, GLint y, GLfloat z,
       struct pipe_rasterizer_state rasterizer;
       memset(&rasterizer, 0, sizeof(rasterizer));
       rasterizer.clamp_fragment_color = !st->clamp_frag_color_in_shader &&
-                                        ctx->Color._ClampFragmentColor &&
-                                        !ctx->DrawBuffer->_IntegerColor;
+                                        ctx->Color._ClampFragmentColor;
       rasterizer.gl_rasterization_rules = 1;
       rasterizer.depth_clip = !ctx->Transform.DepthClamp;
       rasterizer.scissor = ctx->Scissor.Enabled;
@@ -1034,8 +1033,7 @@ get_color_fp_variant(struct st_context *st)
                        ctx->Pixel.AlphaScale != 1.0);
    key.pixelMaps = ctx->Pixel.MapColorFlag;
    key.clamp_color = st->clamp_frag_color_in_shader &&
-                     st->ctx->Color._ClampFragmentColor &&
-                     !st->ctx->DrawBuffer->_IntegerColor;
+                     st->ctx->Color._ClampFragmentColor;
 
    fpv = st_get_fp_variant(st, st->fp, &key);
 
-- 
1.7.10.4



More information about the mesa-dev mailing list