[Mesa-dev] [PATCH 2/2] meta: Write color values in to 'out' variables for all the draw buffers

Anuj Phogat anuj.phogat at gmail.com
Mon May 19 13:20:07 PDT 2014


_mesa_meta_setup_blit_shader() currently generates a fragment shader
which, irrespective of the number of draw buffers, writes the color
to only one output variable. Current shader rely on an undefined
behavior and possibly works by chance.

>From OpenGL 4.0  spec, page 256:
  "If a fragment shader writes to gl_FragColor, DrawBuffers specifies a
   set of draw buffers into which the single fragment color defined by
   gl_FragColor is written. If a fragment shader writes to gl_FragData,
   or a user-defined varying out variable, DrawBuffers specifies a set
   of draw buffers into which each of the multiple output colors defined
   by these variables are separately written. If a fragment shader writes
   to none of gl_FragColor, gl_FragData, nor any user defined varying out
   variables, the values of the fragment colors following shader execution
   are undefined, and may differ for each fragment color."

OpenGL 4.4 spec, page 463, added an additional line in this section:
  "If some, but not all user-defined output variables are written, the
   values of fragment colors corresponding to unwritten variables are
   similarly undefined."

Cc: <mesa-stable at lists.freedesktop.org>
Signed-off-by: Anuj Phogat <anuj.phogat at gmail.com>
---
 src/mesa/drivers/common/meta.c | 23 ++++++++++++++++++-----
 1 file changed, 18 insertions(+), 5 deletions(-)

diff --git a/src/mesa/drivers/common/meta.c b/src/mesa/drivers/common/meta.c
index 87609b4..4897cd9 100644
--- a/src/mesa/drivers/common/meta.c
+++ b/src/mesa/drivers/common/meta.c
@@ -247,7 +247,8 @@ _mesa_meta_setup_blit_shader(struct gl_context *ctx,
    struct blit_shader *shader = choose_blit_shader(target, table);
    const char *vs_input, *vs_output, *fs_input, *fs_output;
    const char *vs_preprocess = "", *fs_preprocess = "";
-   const char *fs_output_decl = "";
+   const char *fs_output_decl = "", *for_loop = "";
+   const int draw_buf_count = ctx->DrawBuffer->_NumColorDrawBuffers;
 
    if (ctx->Const.GLSLVersion < 130) {
       vs_input = "attribute";
@@ -255,12 +256,23 @@ _mesa_meta_setup_blit_shader(struct gl_context *ctx,
       fs_preprocess = "#extension GL_EXT_texture_array : enable";
       fs_output = "gl_FragColor";
    } else {
-      vs_preprocess = fs_preprocess = "#version 130";
+      vs_preprocess = "#version 130";
       vs_input = fs_input = "in";
       vs_output = "out";
-      fs_output = "out_color";
-      fs_output_decl = "out vec4 out_color;";
       shader->func = "texture";
+      if (draw_buf_count > 1) {
+         fs_preprocess = ralloc_asprintf(mem_ctx,
+                                         "#version 130\n"
+                                         "#define NUM_DRAW_BUFS %d",
+                                         draw_buf_count);
+         fs_output = "out_color[i]";
+         fs_output_decl = "out vec4 out_color[NUM_DRAW_BUFS];";
+         for_loop = "   for (int i = 0; i < NUM_DRAW_BUFS; i++)\n   ";
+      } else {
+         fs_preprocess = "#version 130";
+         fs_output = "out_color";
+         fs_output_decl = "out vec4 out_color;";
+      }
    }
 
    assert(shader != NULL);
@@ -291,11 +303,12 @@ _mesa_meta_setup_blit_shader(struct gl_context *ctx,
                 "void main()\n"
                 "{\n"
                 "   vec4 color = %s(texSampler, %s);\n"
+                "%s"
                 "   %s = color;\n"
                 "   gl_FragDepth = color.x;\n"
                 "}\n",
                 fs_preprocess, shader->type, fs_input, fs_output_decl,
-                shader->func, shader->texcoords, fs_output);
+                shader->func, shader->texcoords, for_loop, fs_output);
 
    _mesa_meta_compile_and_link_program(ctx, vs_source, fs_source,
                                        ralloc_asprintf(mem_ctx, "%s blit",
-- 
1.8.3.1



More information about the mesa-dev mailing list