On 7 December 2011 00:41, Kenneth Graunke <span dir="ltr"><<a href="mailto:kenneth@whitecape.org">kenneth@whitecape.org</a>></span> wrote:<br><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">
<div class="HOEnZb"><div class="h5">On 12/05/2011 09:40 AM, Paul Berry wrote:<br>
> Prior to this patch, in the Gen4 and Gen5 GS, we used GRF 0 (called<br>
> "R0" in the code) as a staging area to prepare the message header for<br>
> the FF_SYNC and URB_WRITE messages. This cleverly avoided an<br>
> unnecessary MOV operation (since the initial value of GRF 0 contains<br>
> data that needs to be included in the message header), but it made the<br>
> code confusing, since GRF 0 could no longer be relied upon to contain<br>
> its initial value once the GS started preparing its first message.<br>
> This patch avoids confusion by using a separate register ("header") as<br>
> the staging area, at the cost of one MOV instruction.<br>
><br>
> Worse yet, prior to this patch, the GS would completely overwrite the<br>
> contents of GRF 0 with the writeback data it received from a completed<br>
> FF_SYNC or URB_WRITE message. It did this because DWORD 0 of the<br>
> writeback data contains the new URB handle, and that neds to be<br>
> included in DWORD 0 of the next URB_WRITE message header. However,<br>
> that caused the rest of the message header to be corrupted either with<br>
> undefined data or zeros. Astonishingly, this did not produce any<br>
> known failures (probably by dumb luck). However, it seems really<br>
> dodgy--corrupting FFTID in particular seems likely to cause GPU hangs.<br>
> This patch avoids the corruption by storing the writeback data in a<br>
> temporary register and then copying just DWORD 0 to the header for the<br>
> next message. This costs one extra MOV instruction per message sent,<br>
> except for the final message.<br>
><br>
> Also, this patch moves the logic for overriding DWORD 2 of the header<br>
> (which contains PrimType, PrimStart, PrimEnd, and some other data that<br>
> we don't care about yet). This logic is now in the function<br>
> brw_gs_overwrite_header_dw2() rather than in brw_gs_emit_vue(). This<br>
> saves one MOV instruction in brw_gs_quads() and brw_gs_quad_strip(),<br>
> and paves the way for the Gen6 GS, which will need more complex logic<br>
> to override DWORD 2 of the header.<br>
><br>
> Finally, the function brw_gs_alloc_regs() contained a benign bug: it<br>
> neglected to increment the register counter when allocating space for<br>
> the "temp" register. This turned out not to have any effect because<br>
> the temp register wasn't used on Gen4 and Gen5, the only hardware<br>
> models (so far) to require a GS program. Now, all the registers<br>
> allocated by brw_gs_alloc_regs() are actually used, and properly<br>
> accounted for.<br>
> ---<br>
> src/mesa/drivers/dri/i965/brw_gs.h | 1 +<br>
> src/mesa/drivers/dri/i965/brw_gs_emit.c | 175 +++++++++++++++++++------------<br>
> 2 files changed, 111 insertions(+), 65 deletions(-)<br>
<br>
</div></div>I'm really glad to see the overwriting of g0 die in a fire. That was<br>
just maddening. Also, your comments are fantastic.<br>
<div class="im"><br>
> diff --git a/src/mesa/drivers/dri/i965/brw_gs.h b/src/mesa/drivers/dri/i965/brw_gs.h<br>
> index 157afa3..d71609f 100644<br>
> --- a/src/mesa/drivers/dri/i965/brw_gs.h<br>
> +++ b/src/mesa/drivers/dri/i965/brw_gs.h<br>
> @@ -58,6 +58,7 @@ struct brw_gs_compile {<br>
> struct {<br>
> struct brw_reg R0;<br>
> struct brw_reg vertex[MAX_GS_VERTS];<br>
> + struct brw_reg header;<br>
> struct brw_reg temp;<br>
> } reg;<br>
> };<br>
> diff --git a/src/mesa/drivers/dri/i965/brw_gs_emit.c b/src/mesa/drivers/dri/i965/brw_gs_emit.c<br>
> index 8097fad..935e663 100644<br>
> --- a/src/mesa/drivers/dri/i965/brw_gs_emit.c<br>
> +++ b/src/mesa/drivers/dri/i965/brw_gs_emit.c<br>
> @@ -58,35 +58,66 @@ static void brw_gs_alloc_regs( struct brw_gs_compile *c,<br>
> i += c->key.nr_regs;<br>
> }<br>
><br>
> - c->reg.temp = brw_vec8_grf(i, 0);<br>
> + c->reg.header = retype(brw_vec8_grf(i++, 0), BRW_REGISTER_TYPE_UD);<br>
> + c->reg.temp = retype(brw_vec8_grf(i++, 0), BRW_REGISTER_TYPE_UD);<br>
<br>
</div>Hmm. I'd originally been thinking of an MRF for the header directly,<br>
but I suppose in many places the code it's useful to be able to read<br>
from it, and on Gen4-5 we always have the implied MOV to the leading MRF<br>
anyway. So this is probably the right thing to do; I'm fine with it.<br>
<div><div class="h5"><br>
> c->prog_data.urb_read_length = c->key.nr_regs;<br>
> c->prog_data.total_grf = i;<br>
> }<br>
><br>
><br>
> +/**<br>
> + * Set up the initial value of c->reg.header register based on c->reg.R0.<br>
> + *<br>
> + * The following information is passed to the GS thread in R0, and needs to be<br>
> + * included in the first URB_WRITE or FF_SYNC message sent by the GS:<br>
> + *<br>
> + * - DWORD 0 [31:0] handle info (Gen4 only)<br>
> + * - DWORD 5 [7:0] FFTID<br>
> + * - DWORD 6 [31:0] Debug info<br>
> + * - DWORD 7 [31:0] Debug info<br>
> + *<br>
> + * This function sets up the above data by copying by copying the contents of<br>
> + * R0 to the header register.<br>
> + */<br>
> +static void brw_gs_initialize_header(struct brw_gs_compile *c)<br>
> +{<br>
> + struct brw_compile *p = &c->func;<br>
> + brw_MOV(p, c->reg.header, c->reg.R0);<br>
> +}<br>
> +<br>
> +/**<br>
> + * Overwrite DWORD 2 of c->reg.header with the given immediate unsigned value.<br>
> + *<br>
> + * In URB_WRITE messages, DWORD 2 contains the fields PrimType, PrimStart,<br>
> + * PrimEnd, Increment CL_INVOCATIONS, and SONumPrimsWritten, many of which we<br>
> + * need to be able to update on a per-vertex basis.<br>
> + */<br>
> +static void brw_gs_overwrite_header_dw2(struct brw_gs_compile *c,<br>
> + unsigned dw2)<br>
> +{<br>
> + struct brw_compile *p = &c->func;<br>
> + brw_MOV(p, get_element_ud(c->reg.header, 2), brw_imm_ud(dw2));<br>
> +}<br>
<br>
</div></div>I must admit I was skeptical about these 1 line functions, though<br>
brw_gs_overwrite_header_dw2 does seem lot easier to use than a raw<br>
brw_MOV, and gets a lot of use. The comments are a real plus.<br>
<div><div class="h5"><br>
> +/**<br>
> + * Emit a vertex using the URB_WRITE message. Use the contents of<br>
> + * c->reg.header for the message header, and the registers starting at \c vert<br>
> + * for the vertex data.<br>
> + *<br>
> + * If \c last is true, then this is the last vertex, so no further URB space<br>
> + * should be allocated, and this message should end the thread.<br>
> + *<br>
> + * If \c last is false, then a new URB entry will be allocated, and its handle<br>
> + * will be stored in DWORD 0 of c->reg.header for use in the next URB_WRITE<br>
> + * message.<br>
> + */<br>
> static void brw_gs_emit_vue(struct brw_gs_compile *c,<br>
> struct brw_reg vert,<br>
> - bool last,<br>
> - GLuint header)<br>
> + bool last)<br>
> {<br>
> struct brw_compile *p = &c->func;<br>
> - struct intel_context *intel = &c->func.brw->intel;<br>
> bool allocate = !last;<br>
> - struct brw_reg temp;<br>
> -<br>
> - if (intel->gen < 6)<br>
> - temp = c->reg.R0;<br>
> - else {<br>
> - temp = c->reg.temp;<br>
> - brw_MOV(p, retype(temp, BRW_REGISTER_TYPE_UD),<br>
> - retype(c->reg.R0, BRW_REGISTER_TYPE_UD));<br>
> - }<br>
> -<br>
> - /* Overwrite PrimType and PrimStart in the message header, for<br>
> - * each vertex in turn:<br>
> - */<br>
> - brw_MOV(p, get_element_ud(temp, 2), brw_imm_ud(header));<br>
><br>
> /* Copy the vertex from vertn into m1..mN+1:<br>
> */<br>
> @@ -99,9 +130,10 @@ static void brw_gs_emit_vue(struct brw_gs_compile *c,<br>
> * allocated each time.<br>
> */<br>
> brw_urb_WRITE(p,<br>
> - allocate ? temp : retype(brw_null_reg(), BRW_REGISTER_TYPE_UD),<br>
> + allocate ? c->reg.temp<br>
> + : retype(brw_null_reg(), BRW_REGISTER_TYPE_UD),<br>
> 0,<br>
> - temp,<br>
> + c->reg.header,<br>
> allocate,<br>
> 1, /* used */<br>
> c->key.nr_regs + 1, /* msg length */<br>
> @@ -111,38 +143,37 @@ static void brw_gs_emit_vue(struct brw_gs_compile *c,<br>
> 0, /* urb offset */<br>
> BRW_URB_SWIZZLE_NONE);<br>
><br>
> - if (intel->gen >= 6 && allocate)<br>
> - brw_MOV(p, get_element_ud(c->reg.R0, 0), get_element_ud(temp, 0));<br>
> + if (allocate) {<br>
> + brw_MOV(p, get_element_ud(c->reg.header, 0),<br>
> + get_element_ud(c->reg.temp, 0));<br>
> + }<br>
<br>
</div></div>Hmm. I was wondering if you could just use c->reg.header as the SEND<br>
destination. I suppose you can't since you want to preserve everything<br>
except DW0. So nevermind.<br>
<div><div class="h5"><br>
> }<br>
><br>
> +/**<br>
> + * Send an FF_SYNC message to ensure that all previously spawned GS threads<br>
> + * have finished sending primitives down the pipeline, and to allocate a URB<br>
> + * entry for the first output vertex. Only needed when intel->needs_ff_sync<br>
> + * is true.<br>
> + *<br>
> + * This function modifies c->reg.header: in DWORD 1, it stores num_prim (which<br>
> + * is needed by the FF_SYNC message), and in DWORD 0, it stores the handle to<br>
> + * the allocated URB entry (which will be needed by the URB_WRITE meesage that<br>
> + * follows).<br>
> + */<br>
> static void brw_gs_ff_sync(struct brw_gs_compile *c, int num_prim)<br>
> {<br>
> struct brw_compile *p = &c->func;<br>
> - struct intel_context *intel = &c->func.brw->intel;<br>
><br>
> - if (intel->gen < 6) {<br>
> - brw_MOV(p, get_element_ud(c->reg.R0, 1), brw_imm_ud(num_prim));<br>
> - brw_ff_sync(p,<br>
> - c->reg.R0,<br>
> - 0,<br>
> - c->reg.R0,<br>
> - 1, /* allocate */<br>
> - 1, /* response length */<br>
> - 0 /* eot */);<br>
> - } else {<br>
> - brw_MOV(p, retype(c->reg.temp, BRW_REGISTER_TYPE_UD),<br>
> - retype(c->reg.R0, BRW_REGISTER_TYPE_UD));<br>
> - brw_MOV(p, get_element_ud(c->reg.temp, 1), brw_imm_ud(num_prim));<br>
> - brw_ff_sync(p,<br>
> - c->reg.temp,<br>
> - 0,<br>
> - c->reg.temp,<br>
> - 1, /* allocate */<br>
> - 1, /* response length */<br>
> - 0 /* eot */);<br>
> - brw_MOV(p, get_element_ud(c->reg.R0, 0),<br>
> - get_element_ud(c->reg.temp, 0));<br>
> - }<br>
> + brw_MOV(p, get_element_ud(c->reg.header, 1), brw_imm_ud(num_prim));<br>
> + brw_ff_sync(p,<br>
> + c->reg.temp,<br>
> + 0,<br>
> + c->reg.header,<br>
> + 1, /* allocate */<br>
> + 1, /* response length */<br>
> + 0 /* eot */);<br>
> + brw_MOV(p, get_element_ud(c->reg.header, 0),<br>
> + get_element_ud(c->reg.temp, 0));<br>
> }<br>
><br>
><br>
> @@ -151,23 +182,28 @@ void brw_gs_quads( struct brw_gs_compile *c, struct brw_gs_prog_key *key )<br>
> struct intel_context *intel = &c->func.brw->intel;<br>
><br>
> brw_gs_alloc_regs(c, 4);<br>
> -<br>
> + brw_gs_initialize_header(c);<br>
> /* Use polygons for correct edgeflag behaviour. Note that vertex 3<br>
> * is the PV for quads, but vertex 0 for polygons:<br>
> */<br>
> if (intel->needs_ff_sync)<br>
> brw_gs_ff_sync(c, 1);<br>
> + brw_gs_overwrite_header_dw2(c, (_3DPRIM_POLYGON << 2) | R02_PRIM_START);<br>
> if (key->pv_first) {<br>
> - brw_gs_emit_vue(c, c->reg.vertex[0], 0, ((_3DPRIM_POLYGON << 2) | R02_PRIM_START));<br>
> - brw_gs_emit_vue(c, c->reg.vertex[1], 0, (_3DPRIM_POLYGON << 2));<br>
> - brw_gs_emit_vue(c, c->reg.vertex[2], 0, (_3DPRIM_POLYGON << 2));<br>
> - brw_gs_emit_vue(c, c->reg.vertex[3], 1, ((_3DPRIM_POLYGON << 2) | R02_PRIM_END));<br>
> + brw_gs_emit_vue(c, c->reg.vertex[0], 0);<br>
> + brw_gs_overwrite_header_dw2(c, _3DPRIM_POLYGON << 2);<br>
<br>
</div></div>This is correct, but really puzzled me for a while. In the old code,<br>
you had to specify the header on every brw_gs_emit_vue, since it<br>
overwrote it completely. In your new code, you've updated that function<br>
so it only overwrites DW0, so you can set some stuff in the header and<br>
it'll remain in place.<br>
<br>
So now, you have to overwrite DW2 of the header on the first, second,<br>
and last vertex:<br>
- First because you need to set PRIM_START<br>
- Second because you need to *unset* PRIM_START<br>
- Last because you need to set PRIM_END<br>
<br>
It took quite a bit of staring to understand why you needed it on the<br>
second vertex (after all, the 3DPRIM_POLYGON << 2 is already there...)<br>
<br>
In the end, this API change lets you omit setting _3DPRIM_POLYGON on the<br>
third vertex. That...doesn't really seem worth it. I suppose it saves<br>
you a MOV. Mostly I suppose it's useful for your later work, which is a<br>
much more compelling argument. :)<br>
<br>
I wonder if this complexity could be encapsulated in a function which<br>
emits a list of VUEs. Something like:<br>
<br>
void<br>
brw_gs_emit_vertices(brw_gs_compile *c, int prim_type, int count,<br>
int *indices)<br>
{<br>
/* Set primitive type and PRIM_START flag */<br>
brw_gs_overwrite_header_dw2(c, (prim_type << 2) | PRIM_START);<br>
brw_gs_emit_vue(c, c->reg.vertex[indices[0]]);<br>
<br>
/* Unset PRIM_START flag and emit middle vertices */<br>
brw_gs_overwrite_header_dw2(c, (prim_type << 2));<br>
for (int i = 1; i < count - 1; i++)<br>
brw_gs_emit_vue(c, c->reg.vertex[indices[i]]);<br>
<br>
/* Emit last vertex with PRIM_END */<br>
brw_gs_overwrite_header_dw2(c, (prim_type << 2) | PRIM_END);<br>
brw_gs_emit_vue(c, c->reg.vertex[indices[count - 1]]);<br>
}<br>
<br>
Then you could just do:<br>
brw_gs_emit_vertices(c, _3DPRIM_POLYGON, 4, ((int[]){0,1,2,3});<br>
brw_gs_emit_vertices(c, _3DPRIM_POLYGON, 4, ((int[]){3,0,1,2});<br>
or whatever you need.<br>
( If you haven't seen this C99 syntax, check out this awesome article:<br>
<a href="http://www.run.montefiore.ulg.ac.be/%7Emartin/resources/kung-f00.html" target="_blank">http://www.run.montefiore.ulg.ac.be/~martin/resources/kung-f00.html</a> )<br>
<br>
But I suppose this discussion is rather moot, since you need to do more<br>
complex stuff later anyway (like checking edges and conditionally<br>
skipping vertices.)<br><br></blockquote><div> </div></div>Yeah, I pretty much agree with everything you've said here. The code is definitely not ideal, but in light of what's coming later in the series, it's hard to see how to improve it in a way that won't have to be dismantled two patches later.<br>
<br>The idea that I'm most inclined to act on is the brw_gs_emit_vertices() idea, but that would only help gen5 code, and since my focus at this point is to implement transform feedback on gen6, I'm trying to keep gen5 changes as minimal as possible to reduce the risk of regressions.<br>
<br>So I think I'll leave it as is for now. If anyone comes up with a better idea, or really can't stand the what the gen5 code looks like after the dust settles, I'm open to following up with some clean-up patches.<br>