36d7f4fba07d557be6c895469ba47d5a0665989a
[mesa.git] / src / mesa / drivers / dri / i965 / gen6_gs_visitor.cpp
1 /*
2 * Copyright © 2014 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
22 *
23 * This code is based on original work by Ilia Mirkin.
24 */
25
26 /**
27 * \file gen6_gs_visitor.cpp
28 *
29 * Gen6 geometry shader implementation
30 */
31
32 #include "gen6_gs_visitor.h"
33
34 namespace brw {
35
36 void
37 gen6_gs_visitor::emit_prolog()
38 {
39 vec4_gs_visitor::emit_prolog();
40
41 /* Gen6 geometry shaders require to allocate an initial VUE handle via
42 * FF_SYNC message, however the documentation remarks that only one thread
43 * can write to the URB simultaneously and the FF_SYNC message provides the
44 * synchronization mechanism for this, so using this message effectively
45 * stalls the thread until it is its turn to write to the URB. Because of
46 * this, the best way to implement geometry shader algorithms in gen6 is to
47 * execute the algorithm before the FF_SYNC message to maximize parallelism.
48 *
49 * To achieve this we buffer the geometry shader outputs for each emitted
50 * vertex in vertex_output during operation. Then, when we have processed
51 * the last vertex (that is, at thread end time), we send the FF_SYNC
52 * message to allocate the initial VUE handle and write all buffered vertex
53 * data to the URB in one go.
54 *
55 * For each emitted vertex, vertex_output will hold vue_map.num_slots
56 * data items plus one additional item to hold required flags
57 * (PrimType, PrimStart, PrimEnd, as expected by the URB_WRITE message)
58 * which come right after the data items for that vertex. Vertex data and
59 * flags for the next vertex come right after the data items and flags for
60 * the previous vertex.
61 */
62 this->current_annotation = "gen6 prolog";
63 this->vertex_output = src_reg(this,
64 glsl_type::uint_type,
65 (prog_data->vue_map.num_slots + 1) *
66 c->gp->program.VerticesOut);
67 this->vertex_output_offset = src_reg(this, glsl_type::uint_type);
68 emit(MOV(dst_reg(this->vertex_output_offset), src_reg(0u)));
69
70 /* MRF 1 will be the header for all messages (FF_SYNC and URB_WRITES),
71 * so initialize it once to R0.
72 */
73 vec4_instruction *inst = emit(MOV(dst_reg(MRF, 1),
74 retype(brw_vec8_grf(0, 0),
75 BRW_REGISTER_TYPE_UD)));
76 inst->force_writemask_all = true;
77
78 /* This will be used as a temporary to store writeback data of FF_SYNC
79 * and URB_WRITE messages.
80 */
81 this->temp = src_reg(this, glsl_type::uint_type);
82 }
83
84 void
85 gen6_gs_visitor::visit(ir_emit_vertex *)
86 {
87 this->current_annotation = "gen6 emit vertex";
88 /* Honor max_vertex layout indication in geometry shader by ignoring any
89 * vertices coming after c->gp->program.VerticesOut.
90 */
91 unsigned num_output_vertices = c->gp->program.VerticesOut;
92 emit(CMP(dst_null_d(), this->vertex_count, src_reg(num_output_vertices),
93 BRW_CONDITIONAL_L));
94 emit(IF(BRW_PREDICATE_NORMAL));
95 {
96 /* Buffer all output slots for this vertex in vertex_output */
97 for (int slot = 0; slot < prog_data->vue_map.num_slots; ++slot) {
98 /* We will handle PSIZ for each vertex at thread end time since it
99 * is not computed by the GS algorithm and requires specific handling.
100 */
101 int varying = prog_data->vue_map.slot_to_varying[slot];
102 if (varying != VARYING_SLOT_PSIZ) {
103 dst_reg dst(this->vertex_output);
104 dst.reladdr = ralloc(mem_ctx, src_reg);
105 memcpy(dst.reladdr, &this->vertex_output_offset, sizeof(src_reg));
106 emit_urb_slot(dst, varying);
107 }
108 emit(ADD(dst_reg(this->vertex_output_offset),
109 this->vertex_output_offset, 1u));
110 }
111
112 /* Now buffer flags for this vertex (we only support point output
113 * for now).
114 */
115 dst_reg dst(this->vertex_output);
116 dst.reladdr = ralloc(mem_ctx, src_reg);
117 memcpy(dst.reladdr, &this->vertex_output_offset, sizeof(src_reg));
118 /* If we are outputting points, then every vertex has PrimStart and
119 * PrimEnd set.
120 */
121 if (c->gp->program.OutputType == GL_POINTS) {
122 emit(MOV(dst, (_3DPRIM_POINTLIST << URB_WRITE_PRIM_TYPE_SHIFT) |
123 URB_WRITE_PRIM_START | URB_WRITE_PRIM_END));
124 }
125 emit(ADD(dst_reg(this->vertex_output_offset),
126 this->vertex_output_offset, 1u));
127
128 /* Update vertex count */
129 emit(ADD(dst_reg(this->vertex_count), this->vertex_count, 1u));
130 }
131 emit(BRW_OPCODE_ENDIF);
132 }
133
134 void
135 gen6_gs_visitor::visit(ir_end_primitive *)
136 {
137 this->current_annotation = "gen6 end primitive";
138 /* Calling EndPrimitive() is optional for point output. In this case we set
139 * the PrimEnd flag when we process EmitVertex().
140 */
141 if (c->gp->program.OutputType == GL_POINTS)
142 return;
143 }
144
145 void
146 gen6_gs_visitor::emit_urb_write_header(int mrf)
147 {
148 this->current_annotation = "gen6 urb header";
149 /* Compute offset of the flags for the current vertex in vertex_output and
150 * write them in dw2 of the message header.
151 *
152 * Notice that by the time that emit_thread_end() calls here
153 * vertex_output_offset should point to the first data item of the current
154 * vertex in vertex_output, thus we only need to add the number of output
155 * slots per vertex to that offset to obtain the flags data offset.
156 */
157 src_reg flags_offset(this, glsl_type::uint_type);
158 emit(ADD(dst_reg(flags_offset),
159 this->vertex_output_offset, src_reg(prog_data->vue_map.num_slots)));
160
161 src_reg flags_data(this->vertex_output);
162 flags_data.reladdr = ralloc(mem_ctx, src_reg);
163 memcpy(flags_data.reladdr, &flags_offset, sizeof(src_reg));
164
165 emit(GS_OPCODE_SET_DWORD_2, dst_reg(MRF, mrf), flags_data);
166 }
167
168 void
169 gen6_gs_visitor::emit_urb_write_opcode(bool complete, src_reg vertex,
170 int base_mrf, int mlen, int urb_offset)
171 {
172 vec4_instruction *inst = NULL;
173
174 /* If the vertex is not complete we don't have to do anything special */
175 if (!complete) {
176 inst = emit(GS_OPCODE_URB_WRITE);
177 inst->urb_write_flags = BRW_URB_WRITE_NO_FLAGS;
178 inst->base_mrf = base_mrf;
179 inst->mlen = mlen;
180 inst->offset = urb_offset;
181 return;
182 }
183
184 /* Otherwise, if this is not the last vertex we are going to write,
185 * we have to request a new VUE handle for the next vertex.
186 *
187 * Notice that the vertex parameter has been pre-incremented in
188 * emit_thread_end() to make this comparison easier.
189 */
190 emit(CMP(dst_null_d(), vertex, this->vertex_count, BRW_CONDITIONAL_L));
191 emit(IF(BRW_PREDICATE_NORMAL));
192 {
193 inst = emit(GS_OPCODE_URB_WRITE_ALLOCATE);
194 inst->urb_write_flags = BRW_URB_WRITE_COMPLETE;
195 inst->base_mrf = base_mrf;
196 inst->mlen = mlen;
197 inst->offset = urb_offset;
198 inst->dst = dst_reg(MRF, base_mrf);
199 inst->src[0] = this->temp;
200 }
201 emit(BRW_OPCODE_ELSE);
202 {
203 inst = emit(GS_OPCODE_URB_WRITE);
204 inst->urb_write_flags = BRW_URB_WRITE_COMPLETE;
205 inst->base_mrf = base_mrf;
206 inst->mlen = mlen;
207 inst->offset = urb_offset;
208 }
209 emit(BRW_OPCODE_ENDIF);
210 }
211
212 void
213 gen6_gs_visitor::emit_thread_end()
214 {
215 /* Here we have to:
216 * 1) Emit an FF_SYNC messsage to obtain an initial VUE handle.
217 * 2) Loop over all buffered vertex data and write it to corresponding
218 * URB entries.
219 * 3) Allocate new VUE handles for all vertices other than the first.
220 * 4) Send a final EOT message.
221 */
222
223 /* MRF 0 is reserved for the debugger, so start with message header
224 * in MRF 1.
225 */
226 int base_mrf = 1;
227
228 /* In the process of generating our URB write message contents, we
229 * may need to unspill a register or load from an array. Those
230 * reads would use MRFs 14-15.
231 */
232 int max_usable_mrf = 13;
233
234 /* Issue the FF_SYNC message and obtain the initial VUE handle. */
235 this->current_annotation = "gen6 thread end: ff_sync";
236 vec4_instruction *inst =
237 emit(GS_OPCODE_FF_SYNC, dst_reg(this->temp), this->vertex_count);
238 inst->base_mrf = base_mrf;
239
240 /* Loop over all buffered vertices and emit URB write messages */
241 this->current_annotation = "gen6 thread end: urb writes init";
242 src_reg vertex(this, glsl_type::uint_type);
243 emit(MOV(dst_reg(vertex), 0u));
244 emit(MOV(dst_reg(this->vertex_output_offset), 0u));
245
246 this->current_annotation = "gen6 thread end: urb writes";
247 emit(BRW_OPCODE_DO);
248 {
249 emit(CMP(dst_null_d(), vertex, this->vertex_count, BRW_CONDITIONAL_GE));
250 inst = emit(BRW_OPCODE_BREAK);
251 inst->predicate = BRW_PREDICATE_NORMAL;
252
253 /* First we prepare the message header */
254 emit_urb_write_header(base_mrf);
255
256 /* Then add vertex data to the message in interleaved fashion */
257 int slot = 0;
258 bool complete = false;
259 do {
260 int mrf = base_mrf + 1;
261
262 /* URB offset is in URB row increments, and each of our MRFs is half
263 * of one of those, since we're doing interleaved writes.
264 */
265 int urb_offset = slot / 2;
266
267 for (; slot < prog_data->vue_map.num_slots; ++slot) {
268 int varying = prog_data->vue_map.slot_to_varying[slot];
269 current_annotation = output_reg_annotation[varying];
270
271 /* Compute offset of this slot for the current vertex
272 * in vertex_output
273 */
274 src_reg data(this->vertex_output);
275 data.reladdr = ralloc(mem_ctx, src_reg);
276 memcpy(data.reladdr, &this->vertex_output_offset, sizeof(src_reg));
277
278 if (varying == VARYING_SLOT_PSIZ) {
279 /* We did not buffer PSIZ, emit it directly here */
280 emit_urb_slot(dst_reg(MRF, mrf), varying);
281 } else {
282 /* Copy this slot to the appropriate message register */
283 dst_reg reg = dst_reg(MRF, mrf);
284 reg.type = output_reg[varying].type;
285 data.type = reg.type;
286 vec4_instruction *inst = emit(MOV(reg, data));
287 inst->force_writemask_all = true;
288 }
289
290 mrf++;
291 emit(ADD(dst_reg(this->vertex_output_offset),
292 this->vertex_output_offset, 1u));
293
294 /* If this was max_usable_mrf, we can't fit anything more into this
295 * URB WRITE.
296 */
297 if (mrf > max_usable_mrf) {
298 slot++;
299 break;
300 }
301 }
302
303 complete = slot >= prog_data->vue_map.num_slots;
304
305 /* When we emit the URB_WRITE below we need to do different things
306 * depending on whether this is the last vertex we are going to
307 * write. That means that we will need to check if
308 * vertex >= vertex_count - 1. However, by increasing vertex early
309 * we transform that comparison into vertex >= vertex_count, which
310 * is more convenient.
311 */
312 if (complete)
313 emit(ADD(dst_reg(vertex), vertex, 1u));
314
315 /* URB data written (does not include the message header reg) must
316 * be a multiple of 256 bits, or 2 VS registers. See vol5c.5,
317 * section 5.4.3.2.2: URB_INTERLEAVED.
318 */
319 int mlen = mrf - base_mrf;
320 if ((mlen % 2) != 1)
321 mlen++;
322 emit_urb_write_opcode(complete, vertex, base_mrf, mlen, urb_offset);
323 } while (!complete);
324
325 /* Skip over the flags data item so that vertex_output_offset points to
326 * the first data item of the next vertex, so that we can start writing
327 * the next vertex.
328 */
329 emit(ADD(dst_reg(this->vertex_output_offset),
330 this->vertex_output_offset, 1u));
331 }
332 emit(BRW_OPCODE_WHILE);
333
334 /* Finally, emit EOT message.
335 *
336 * In gen6 it looks like we have to set the complete flag too, otherwise
337 * the GPU hangs.
338 */
339 this->current_annotation = "gen6 thread end: EOT";
340 inst = emit(GS_OPCODE_THREAD_END);
341 inst->urb_write_flags = BRW_URB_WRITE_COMPLETE;
342 inst->base_mrf = base_mrf;
343 inst->mlen = 1;
344 }
345
346 } /* namespace brw */