From dce4f90345e8e57dfcef74d273e122063dd0a98d Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Kristian=20H=C3=B8gsberg?= <krh@bitplanet.net>
Date: Wed, 7 Jan 2015 17:20:02 -0800
Subject: [PATCH] i965: Use attributes for color and layer
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Signed-off-by: Kristian Høgsberg <krh@bitplanet.net>
---
 src/mesa/drivers/dri/i965/brw_fs.cpp            | 19 ++++--
 src/mesa/drivers/dri/i965/brw_meta_fast_clear.c | 85 ++++++++++++++-----------
 2 files changed, 60 insertions(+), 44 deletions(-)

diff --git a/src/mesa/drivers/dri/i965/brw_fs.cpp b/src/mesa/drivers/dri/i965/brw_fs.cpp
index 6cac6d7..a32d177 100644
--- a/src/mesa/drivers/dri/i965/brw_fs.cpp
+++ b/src/mesa/drivers/dri/i965/brw_fs.cpp
@@ -2633,8 +2633,19 @@ fs_visitor::emit_repclear_shader()
    int base_mrf = 1;
    int color_mrf = base_mrf + 2;
 
-   fs_inst *mov = emit(MOV(vec4(brw_message_reg(color_mrf)),
-                           fs_reg(UNIFORM, 0, BRW_REGISTER_TYPE_F)));
+   fs_inst *mov;
+
+   mov = emit(MOV(brw_vec4_reg(BRW_MESSAGE_REGISTER_FILE, color_mrf, 0),
+                  brw_reg(BRW_GENERAL_REGISTER_FILE,
+                          6,
+                          3,
+                          BRW_REGISTER_TYPE_F,
+                          BRW_VERTICAL_STRIDE_8,
+                          BRW_WIDTH_2,
+                          BRW_HORIZONTAL_STRIDE_4,
+                          BRW_SWIZZLE_XXXX,
+                          WRITEMASK_X)));
+
    mov->force_writemask_all = true;
 
    fs_inst *write;
@@ -2662,10 +2673,6 @@ fs_visitor::emit_repclear_shader()
 
    assign_constant_locations();
    assign_curb_setup();
-
-   /* Now that we have the uniform assigned, go ahead and force it to a vec4. */
-   assert(mov->src[0].file == HW_REG);
-   mov->src[0] = brw_vec4_grf(mov->src[0].fixed_hw_reg.nr, 0);
 }
 
 /**
diff --git a/src/mesa/drivers/dri/i965/brw_meta_fast_clear.c b/src/mesa/drivers/dri/i965/brw_meta_fast_clear.c
index 98d149c..4d74aa3 100644
--- a/src/mesa/drivers/dri/i965/brw_meta_fast_clear.c
+++ b/src/mesa/drivers/dri/i965/brw_meta_fast_clear.c
@@ -57,8 +57,6 @@ struct brw_fast_clear_state {
    GLuint vao;
    GLuint vbo;
    GLuint shader_prog;
-   GLint color_location;
-   GLint layer_location;
 };
 
 static bool
@@ -82,7 +80,7 @@ brw_fast_clear_init(struct brw_context *brw)
    _mesa_BindVertexArray(clear->vao);
    _mesa_GenBuffers(1, &clear->vbo);
    _mesa_BindBuffer(GL_ARRAY_BUFFER, clear->vbo);
-   _mesa_VertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, sizeof(float) * 2, 0);
+   _mesa_VertexAttribPointer(0, 4, GL_FLOAT, GL_FALSE, sizeof(float) * 8, 0);
    _mesa_EnableVertexAttribArray(0);
 
    return true;
@@ -94,19 +92,19 @@ brw_bind_rep_write_shader(struct brw_context *brw, float *color)
    const char *vs_source =
       "#extension GL_AMD_vertex_shader_layer : enable\n"
       "attribute vec4 position;\n"
-      "uniform int layer;\n"
+      "attribute vec4 color;\n"
+      "varying vec4 v_color;\n"
       "void main()\n"
       "{\n"
-      "#ifdef GL_AMD_vertex_shader_layer\n"
-      "   gl_Layer = layer;\n"
-      "#endif\n"
-      "   gl_Position = position;\n"
+      "   gl_Layer = int(position.z);\n"
+      "   gl_Position = vec4(position.xy, 0.0, 1.0);\n"
+      "   v_color = color;\n"
       "}\n";
    const char *fs_source =
-      "uniform vec4 color;\n"
+      "varying vec4 v_color;\n"
       "void main()\n"
       "{\n"
-      "   gl_FragColor = color;\n"
+      "   gl_FragColor = v_color;\n"
       "}\n";
 
    GLuint vs, fs;
@@ -115,7 +113,6 @@ brw_bind_rep_write_shader(struct brw_context *brw, float *color)
 
    if (clear->shader_prog) {
       _mesa_UseProgram(clear->shader_prog);
-      _mesa_Uniform4fv(clear->color_location, 1, color);
       return;
    }
 
@@ -128,16 +125,11 @@ brw_bind_rep_write_shader(struct brw_context *brw, float *color)
    _mesa_AttachShader(clear->shader_prog, vs);
    _mesa_DeleteShader(vs);
    _mesa_BindAttribLocation(clear->shader_prog, 0, "position");
+   _mesa_BindAttribLocation(clear->shader_prog, 1, "color");
    _mesa_ObjectLabel(GL_PROGRAM, clear->shader_prog, -1, "meta clear");
    _mesa_LinkProgram(clear->shader_prog);
 
-   clear->color_location =
-      _mesa_GetUniformLocation(clear->shader_prog, "color");
-   clear->layer_location =
-      _mesa_GetUniformLocation(clear->shader_prog, "layer");
-
    _mesa_UseProgram(clear->shader_prog);
-   _mesa_Uniform4fv(clear->color_location, 1, color);
 }
 
 void
@@ -167,21 +159,44 @@ struct rect {
 };
 
 static void
-brw_draw_rectlist(struct gl_context *ctx, struct rect *rect, int num_instances)
+brw_draw_rectlist(struct gl_context *ctx, struct rect *rect, int num_instances,
+                  float *color)
 {
    int start = 0, count = 3;
    struct _mesa_prim prim;
-   float verts[6];
-
-   verts[0] = rect->x1;
-   verts[1] = rect->y1;
-   verts[2] = rect->x0;
-   verts[3] = rect->y1;
-   verts[4] = rect->x0;
-   verts[5] = rect->y0;
+   float verts[24 * 16], *v;
+   int i;
+
+   v = verts;
+   for (i = 0; i < num_instances; i++) {
+      verts[ 0] = rect->x1;
+      verts[ 1] = rect->y1;
+      verts[ 2] = i;
+      verts[ 4] = color[0];
+      verts[ 5] = color[1];
+      verts[ 6] = color[2];
+      verts[ 7] = color[3];
+
+      verts[ 8] = rect->x0;
+      verts[ 9] = rect->y1;
+      verts[10] = i;
+      verts[12] = color[0];
+      verts[13] = color[1];
+      verts[14] = color[2];
+      verts[15] = color[3];
+
+      verts[16] = rect->x0;
+      verts[17] = rect->y0;
+      verts[18] = i;
+      verts[20] = color[0];
+      verts[21] = color[1];
+      verts[23] = color[2];
+      verts[24] = color[3];
+      v += 24;
+   }
 
    /* upload new vertex data */
-   _mesa_BufferData(GL_ARRAY_BUFFER_ARB, sizeof(verts), verts,
+   _mesa_BufferData(GL_ARRAY_BUFFER_ARB, (v - verts) * sizeof(float), verts,
                     GL_DYNAMIC_DRAW_ARB);
 
    if (ctx->NewState)
@@ -415,7 +430,7 @@ brw_meta_fast_clear(struct brw_context *brw, struct gl_framebuffer *fb,
    enum { FAST_CLEAR, REP_CLEAR, PLAIN_CLEAR } clear_type;
    GLbitfield plain_clear_buffers, meta_save, rep_clear_buffers, fast_clear_buffers;
    struct rect fast_clear_rect, clear_rect;
-   int i, layers;
+   int layers;
 
    fast_clear_buffers = rep_clear_buffers = plain_clear_buffers = 0;
 
@@ -577,21 +592,15 @@ brw_meta_fast_clear(struct brw_context *brw, struct gl_framebuffer *fb,
       _mesa_meta_drawbuffers_from_bitfield(fast_clear_buffers);
       brw_bind_rep_write_shader(brw, (float *) fast_clear_color);
       set_fast_clear_op(brw, GEN7_PS_RENDER_TARGET_FAST_CLEAR_ENABLE);
-      for (i = 0; i < layers; i++) {
-
-         _mesa_Uniform1i(brw->fast_clear_state->layer_location, i);
-         brw_draw_rectlist(ctx, &fast_clear_rect, 1);
-      }
+      brw_draw_rectlist(ctx, &fast_clear_rect, layers,
+                        (float *) fast_clear_color);
       set_fast_clear_op(brw, 0);
    }
 
    if (rep_clear_buffers) {
       _mesa_meta_drawbuffers_from_bitfield(rep_clear_buffers);
       brw_bind_rep_write_shader(brw, ctx->Color.ClearColor.f);
-      for (i = 0; i < layers; i++) {
-         _mesa_Uniform1i(brw->fast_clear_state->layer_location, i);
-         brw_draw_rectlist(ctx, &clear_rect, layers);
-      }
+      brw_draw_rectlist(ctx, &clear_rect, layers, (float *) fast_clear_color);
    }
 
    /* Now set the mts we cleared to INTEL_FAST_CLEAR_STATE_CLEAR so we'll
@@ -703,7 +712,7 @@ brw_meta_resolve_color(struct brw_context *brw,
    mt->fast_clear_state = INTEL_FAST_CLEAR_STATE_RESOLVED;
    get_resolve_rect(brw, mt, &rect);
 
-   brw_draw_rectlist(ctx, &rect, 1);
+   brw_draw_rectlist(ctx, &rect, 1, (float *) fast_clear_color);
 
    set_fast_clear_op(brw, 0);
    use_rectlist(brw, false);
-- 
2.1.0

