<div dir="ltr">On 13 September 2013 23:10, Kenneth Graunke <span dir="ltr"><<a href="mailto:kenneth@whitecape.org" target="_blank">kenneth@whitecape.org</a>></span> wrote:<br><div class="gmail_extra"><div class="gmail_quote">
<blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">This was an embarassingly large amount of copy and pasted code,<br>
and it wasn't particularly simple code either.  By factoring it out<br>
into a helper function, we consolidate the complexity.<br>
<br>
Signed-off-by: Kenneth Graunke <<a href="mailto:kenneth@whitecape.org">kenneth@whitecape.org</a>><br></blockquote><div><br></div><div>I really like what you're doing here.  A few minor comments:<br></div><div> </div>
<blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
---<br>
 src/mesa/drivers/dri/i965/gen7_wm_surface_state.c | 144 +++++++++-------------<br>
 1 file changed, 58 insertions(+), 86 deletions(-)<br>
<br>
diff --git a/src/mesa/drivers/dri/i965/gen7_wm_surface_state.c b/src/mesa/drivers/dri/i965/gen7_wm_surface_state.c<br>
index 37e3174..8413308 100644<br>
--- a/src/mesa/drivers/dri/i965/gen7_wm_surface_state.c<br>
+++ b/src/mesa/drivers/dri/i965/gen7_wm_surface_state.c<br>
@@ -224,6 +224,37 @@ gen7_check_surface_setup(uint32_t *surf, bool is_render_target)<br>
    }<br>
 }<br>
<br>
+static void<br>
+gen7_emit_buffer_surface_state(struct brw_context *brw,<br>
+                               uint32_t *out_offset,<br>
+                               drm_intel_bo *bo,<br>
+                               unsigned buffer_offset,<br>
+                               unsigned surface_format,<br>
+                               unsigned buffer_size,<br>
+                               unsigned pitch,<br>
+                               unsigned mocs)<br>
+{<br>
+   uint32_t *surf = brw_state_batch(brw, AUB_TRACE_SURFACE_STATE,<br>
+                                    8 * 4, 32, out_offset);<br>
+   memset(surf, 0, 8 * 4);<br>
+<br>
+   surf[0] = BRW_SURFACE_BUFFER << BRW_SURFACE_TYPE_SHIFT |<br>
+             surface_format << BRW_SURFACE_FORMAT_SHIFT |<br>
+             BRW_SURFACE_RC_READ_WRITE;<br>
+   surf[1] = bo->offset + buffer_offset; /* reloc */<br>
+   surf[2] = SET_FIELD(buffer_size & 0x7f, GEN7_SURFACE_WIDTH) |<br>
+             SET_FIELD((buffer_size >> 7) & 0x3fff, GEN7_SURFACE_HEIGHT);<br>
+   surf[3] = SET_FIELD((buffer_size >> 21) & 0x3f, BRW_SURFACE_DEPTH) |<br></blockquote><div><br></div><div>These three instances of buffer_size should be (buffer_size - 1).  I think that there was a pre-existing bug in gen7_update_buffer_texture_surface() where it wasn't subtracting 1 where it should.  Probably we should fix the bug as a pre-patch.<br>
</div><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
+             (pitch - 1);<br>
+   surf[4] = 0;<br></blockquote><div><br></div><div>Technically this line is unnecessary given the memset() above.  I'm quibbling, though--it's hard to imagine this making a significant performance difference :)<br>
</div><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
+   surf[5] = SET_FIELD(mocs, GEN7_SURFACE_MOCS);<br>
+<br>
+   /* Emit relocation to surface contents */<br>
+   drm_intel_bo_emit_reloc(brw-><a href="http://batch.bo" target="_blank">batch.bo</a>, *out_offset + 4,<br>
+                           bo, buffer_offset, I915_GEM_DOMAIN_SAMPLER, 0);<br>
+<br>
+   gen7_check_surface_setup(surf, false /* is_render_target */);<br>
+}<br>
<br>
 static void<br>
 gen7_update_buffer_texture_surface(struct gl_context *ctx,<br>
@@ -237,39 +268,23 @@ gen7_update_buffer_texture_surface(struct gl_context *ctx,<br>
    drm_intel_bo *bo = intel_obj ? intel_obj->buffer : NULL;<br>
    gl_format format = tObj->_BufferObjectFormat;<br>
<br>
-   uint32_t *surf = brw_state_batch(brw, AUB_TRACE_SURFACE_STATE,<br>
-                                    8 * 4, 32, surf_offset);<br>
-   memset(surf, 0, 8 * 4);<br>
-<br>
    uint32_t surface_format = brw_format_for_mesa_format(format);<br>
    if (surface_format == 0 && format != MESA_FORMAT_RGBA_FLOAT32) {<br>
       _mesa_problem(NULL, "bad format %s for texture buffer\n",<br>
                     _mesa_get_format_name(format));<br>
    }<br>
<br>
-   surf[0] = BRW_SURFACE_BUFFER << BRW_SURFACE_TYPE_SHIFT |<br>
-             surface_format << BRW_SURFACE_FORMAT_SHIFT |<br>
-             BRW_SURFACE_RC_READ_WRITE;<br>
-<br>
-   if (bo) {<br>
-      surf[1] = bo->offset; /* reloc */<br>
-<br>
-      drm_intel_bo_emit_reloc(brw-><a href="http://batch.bo" target="_blank">batch.bo</a>,<br>
-                             *surf_offset + 4,<br>
-                             bo, 0,<br>
-                             I915_GEM_DOMAIN_SAMPLER, 0);<br>
-<br>
-      int texel_size = _mesa_get_format_bytes(format);<br>
-      int w = intel_obj->Base.Size / texel_size;<br>
-<br>
-      /* note that these differ from GEN6 */<br>
-      surf[2] = SET_FIELD(w & 0x7f, GEN7_SURFACE_WIDTH) | /* bits 6:0 of size */<br>
-                SET_FIELD((w >> 7) & 0x3fff, GEN7_SURFACE_HEIGHT); /* 20:7 */<br>
-      surf[3] = SET_FIELD((w >> 21) & 0x3f, BRW_SURFACE_DEPTH) | /* bits 26:21 */<br>
-                (texel_size - 1);<br>
-   }<br>
-<br>
-   gen7_check_surface_setup(surf, false /* is_render_target */);<br>
+   int texel_size = _mesa_get_format_bytes(format);<br>
+   int w = intel_obj ? intel_obj->Base.Size / texel_size : 0;<br>
+<br>
+   gen7_emit_buffer_surface_state(brw,<br>
+                                  surf_offset,<br>
+                                  bo,<br>
+                                  0,<br>
+                                  surface_format,<br>
+                                  w,<br>
+                                  texel_size,<br>
+                                  0 /* mocs */);<br>
 }<br>
<br>
 static void<br>
@@ -371,38 +386,15 @@ gen7_create_constant_surface(struct brw_context *brw,<br>
 {<br>
    uint32_t stride = dword_pitch ? 4 : 16;<br>
    uint32_t elements = ALIGN(size, stride) / stride;<br>
-   const GLint w = elements - 1;<br>
<br>
-   uint32_t *surf = brw_state_batch(brw, AUB_TRACE_SURFACE_STATE,<br>
-                                    8 * 4, 32, out_offset);<br>
-   memset(surf, 0, 8 * 4);<br>
-<br>
-   surf[0] = BRW_SURFACE_BUFFER << BRW_SURFACE_TYPE_SHIFT |<br>
-             BRW_SURFACEFORMAT_R32G32B32A32_FLOAT << BRW_SURFACE_FORMAT_SHIFT |<br>
-             BRW_SURFACE_RC_READ_WRITE;<br>
-<br>
-   assert(bo);<br>
-   surf[1] = bo->offset + offset; /* reloc */<br>
-<br>
-   /* note that these differ from GEN6 */<br>
-   surf[2] = SET_FIELD(w & 0x7f, GEN7_SURFACE_WIDTH) |<br>
-             SET_FIELD((w >> 7) & 0x3fff, GEN7_SURFACE_HEIGHT);<br>
-   surf[3] = SET_FIELD((w >> 21) & 0x3f, BRW_SURFACE_DEPTH) |<br>
-             (stride - 1);<br>
-<br>
-   if (brw->is_haswell) {<br>
-      surf[7] = SET_FIELD(HSW_SCS_RED,   GEN7_SURFACE_SCS_R) |<br>
-                SET_FIELD(HSW_SCS_GREEN, GEN7_SURFACE_SCS_G) |<br>
-                SET_FIELD(HSW_SCS_BLUE,  GEN7_SURFACE_SCS_B) |<br>
-                SET_FIELD(HSW_SCS_ALPHA, GEN7_SURFACE_SCS_A);<br>
-   }<br></blockquote><div><br></div><div>I don't see this Haswell-specific code in gen7_emit_buffer_surface_state().  Is that a problem? <br></div><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">

-<br>
-   drm_intel_bo_emit_reloc(brw-><a href="http://batch.bo" target="_blank">batch.bo</a>,<br>
-                          *out_offset + 4,<br>
-                          bo, offset,<br>
-                          I915_GEM_DOMAIN_SAMPLER, 0);<br>
-<br>
-   gen7_check_surface_setup(surf, false /* is_render_target */);<br>
+   gen7_emit_buffer_surface_state(brw,<br>
+                                  out_offset,<br>
+                                  bo,<br>
+                                  offset,<br>
+                                  BRW_SURFACEFORMAT_R32G32B32A32_FLOAT,<br>
+                                  elements - 1,<br>
+                                  stride,<br>
+                                  0 /* mocs */);<br>
 }<br>
<br>
 /**<br>
@@ -411,34 +403,14 @@ gen7_create_constant_surface(struct brw_context *brw,<br>
 void<br>
 gen7_create_shader_time_surface(struct brw_context *brw, uint32_t *out_offset)<br>
 {<br>
-   const int w = brw->shader_time.bo->size - 1;<br>
-<br>
-   uint32_t *surf = brw_state_batch(brw, AUB_TRACE_SURFACE_STATE,<br>
-                                    8 * 4, 32, out_offset);<br>
-   memset(surf, 0, 8 * 4);<br>
-<br>
-   surf[0] = BRW_SURFACE_BUFFER << BRW_SURFACE_TYPE_SHIFT |<br>
-             BRW_SURFACEFORMAT_RAW << BRW_SURFACE_FORMAT_SHIFT |<br>
-             BRW_SURFACE_RC_READ_WRITE;<br>
-<br>
-   surf[1] = brw->shader_time.bo->offset; /* reloc */<br>
-<br>
-   /* note that these differ from GEN6 */<br>
-   surf[2] = SET_FIELD(w & 0x7f, GEN7_SURFACE_WIDTH) |<br>
-             SET_FIELD((w >> 7) & 0x3fff, GEN7_SURFACE_HEIGHT);<br>
-   surf[3] = SET_FIELD((w >> 21) & 0x3f, BRW_SURFACE_DEPTH);<br>
-<br>
-   /* Unlike texture or renderbuffer surfaces, we only do untyped operations<br>
-    * on the shader_time surface, so there's no need to set HSW channel<br>
-    * overrides.<br>
-    */<br>
-<br>
-   drm_intel_bo_emit_reloc(brw-><a href="http://batch.bo" target="_blank">batch.bo</a>,<br>
-                           *out_offset + 4,<br>
-                           brw-><a href="http://shader_time.bo" target="_blank">shader_time.bo</a>, 0,<br>
-                           I915_GEM_DOMAIN_SAMPLER, 0);<br>
-<br>
-   gen7_check_surface_setup(surf, false /* is_render_target */);<br>
+   gen7_emit_buffer_surface_state(brw,<br>
+                                  out_offset,<br>
+                                  brw-><a href="http://shader_time.bo" target="_blank">shader_time.bo</a>,<br>
+                                  0,<br>
+                                  BRW_SURFACEFORMAT_RAW,<br>
+                                  brw->shader_time.bo->size - 1,<br>
+                                  1,<br>
+                                  0 /* mocs */);<br>
 }<br>
<br>
 static void<br>
<span class=""><font color="#888888">--<br>
1.8.3.4<br>
<br>
_______________________________________________<br>
mesa-dev mailing list<br>
<a href="mailto:mesa-dev@lists.freedesktop.org">mesa-dev@lists.freedesktop.org</a><br>
<a href="http://lists.freedesktop.org/mailman/listinfo/mesa-dev" target="_blank">http://lists.freedesktop.org/mailman/listinfo/mesa-dev</a><br>
</font></span></blockquote></div><br></div></div>