i965: Move brw_get_shader_time_index() call out of emit functions
[mesa.git] / src / mesa / drivers / dri / i965 / brw_vec4_gs_visitor.cpp
1 /*
2 * Copyright © 2013 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
21 * DEALINGS IN THE SOFTWARE.
22 */
23
24 /**
25 * \file brw_vec4_gs_visitor.cpp
26 *
27 * Geometry-shader-specific code derived from the vec4_visitor class.
28 */
29
30 #include "brw_vec4_gs_visitor.h"
31 #include "gen6_gs_visitor.h"
32
33 const unsigned MAX_GS_INPUT_VERTICES = 6;
34
35 namespace brw {
36
37 vec4_gs_visitor::vec4_gs_visitor(const struct brw_compiler *compiler,
38 void *log_data,
39 struct brw_gs_compile *c,
40 nir_shader *shader,
41 void *mem_ctx,
42 bool no_spills,
43 int shader_time_index)
44 : vec4_visitor(compiler, log_data, &c->key.tex,
45 &c->prog_data.base, shader, mem_ctx,
46 no_spills, shader_time_index),
47 c(c)
48 {
49 }
50
51
52 dst_reg *
53 vec4_gs_visitor::make_reg_for_system_value(int location,
54 const glsl_type *type)
55 {
56 dst_reg *reg = new(mem_ctx) dst_reg(this, type);
57
58 switch (location) {
59 case SYSTEM_VALUE_INVOCATION_ID:
60 this->current_annotation = "initialize gl_InvocationID";
61 emit(GS_OPCODE_GET_INSTANCE_ID, *reg);
62 break;
63 default:
64 unreachable("not reached");
65 }
66
67 return reg;
68 }
69
70
71 int
72 vec4_gs_visitor::setup_varying_inputs(int payload_reg, int *attribute_map,
73 int attributes_per_reg)
74 {
75 /* For geometry shaders there are N copies of the input attributes, where N
76 * is the number of input vertices. attribute_map[BRW_VARYING_SLOT_COUNT *
77 * i + j] represents attribute j for vertex i.
78 *
79 * Note that GS inputs are read from the VUE 256 bits (2 vec4's) at a time,
80 * so the total number of input slots that will be delivered to the GS (and
81 * thus the stride of the input arrays) is urb_read_length * 2.
82 */
83 const unsigned num_input_vertices = c->gp->program.VerticesIn;
84 assert(num_input_vertices <= MAX_GS_INPUT_VERTICES);
85 unsigned input_array_stride = c->prog_data.base.urb_read_length * 2;
86
87 for (int slot = 0; slot < c->input_vue_map.num_slots; slot++) {
88 int varying = c->input_vue_map.slot_to_varying[slot];
89 for (unsigned vertex = 0; vertex < num_input_vertices; vertex++) {
90 attribute_map[BRW_VARYING_SLOT_COUNT * vertex + varying] =
91 attributes_per_reg * payload_reg + input_array_stride * vertex +
92 slot;
93 }
94 }
95
96 int regs_used = ALIGN(input_array_stride * num_input_vertices,
97 attributes_per_reg) / attributes_per_reg;
98 return payload_reg + regs_used;
99 }
100
101
102 void
103 vec4_gs_visitor::setup_payload()
104 {
105 int attribute_map[BRW_VARYING_SLOT_COUNT * MAX_GS_INPUT_VERTICES];
106
107 /* If we are in dual instanced or single mode, then attributes are going
108 * to be interleaved, so one register contains two attribute slots.
109 */
110 int attributes_per_reg =
111 c->prog_data.base.dispatch_mode == DISPATCH_MODE_4X2_DUAL_OBJECT ? 1 : 2;
112
113 /* If a geometry shader tries to read from an input that wasn't written by
114 * the vertex shader, that produces undefined results, but it shouldn't
115 * crash anything. So initialize attribute_map to zeros--that ensures that
116 * these undefined results are read from r0.
117 */
118 memset(attribute_map, 0, sizeof(attribute_map));
119
120 int reg = 0;
121
122 /* The payload always contains important data in r0, which contains
123 * the URB handles that are passed on to the URB write at the end
124 * of the thread.
125 */
126 reg++;
127
128 /* If the shader uses gl_PrimitiveIDIn, that goes in r1. */
129 if (c->prog_data.include_primitive_id)
130 attribute_map[VARYING_SLOT_PRIMITIVE_ID] = attributes_per_reg * reg++;
131
132 reg = setup_uniforms(reg);
133
134 reg = setup_varying_inputs(reg, attribute_map, attributes_per_reg);
135
136 lower_attributes_to_hw_regs(attribute_map, attributes_per_reg > 1);
137
138 this->first_non_payload_grf = reg;
139 }
140
141
142 void
143 vec4_gs_visitor::emit_prolog()
144 {
145 /* In vertex shaders, r0.2 is guaranteed to be initialized to zero. In
146 * geometry shaders, it isn't (it contains a bunch of information we don't
147 * need, like the input primitive type). We need r0.2 to be zero in order
148 * to build scratch read/write messages correctly (otherwise this value
149 * will be interpreted as a global offset, causing us to do our scratch
150 * reads/writes to garbage memory). So just set it to zero at the top of
151 * the shader.
152 */
153 this->current_annotation = "clear r0.2";
154 dst_reg r0(retype(brw_vec4_grf(0, 0), BRW_REGISTER_TYPE_UD));
155 vec4_instruction *inst = emit(GS_OPCODE_SET_DWORD_2, r0, 0u);
156 inst->force_writemask_all = true;
157
158 /* Create a virtual register to hold the vertex count */
159 this->vertex_count = src_reg(this, glsl_type::uint_type);
160
161 /* Initialize the vertex_count register to 0 */
162 this->current_annotation = "initialize vertex_count";
163 inst = emit(MOV(dst_reg(this->vertex_count), 0u));
164 inst->force_writemask_all = true;
165
166 if (c->control_data_header_size_bits > 0) {
167 /* Create a virtual register to hold the current set of control data
168 * bits.
169 */
170 this->control_data_bits = src_reg(this, glsl_type::uint_type);
171
172 /* If we're outputting more than 32 control data bits, then EmitVertex()
173 * will set control_data_bits to 0 after emitting the first vertex.
174 * Otherwise, we need to initialize it to 0 here.
175 */
176 if (c->control_data_header_size_bits <= 32) {
177 this->current_annotation = "initialize control data bits";
178 inst = emit(MOV(dst_reg(this->control_data_bits), 0u));
179 inst->force_writemask_all = true;
180 }
181 }
182
183 /* If the geometry shader uses the gl_PointSize input, we need to fix it up
184 * to account for the fact that the vertex shader stored it in the w
185 * component of VARYING_SLOT_PSIZ.
186 */
187 if (c->gp->program.Base.InputsRead & VARYING_BIT_PSIZ) {
188 this->current_annotation = "swizzle gl_PointSize input";
189 for (int vertex = 0; vertex < c->gp->program.VerticesIn; vertex++) {
190 dst_reg dst(ATTR,
191 BRW_VARYING_SLOT_COUNT * vertex + VARYING_SLOT_PSIZ);
192 dst.type = BRW_REGISTER_TYPE_F;
193 src_reg src(dst);
194 dst.writemask = WRITEMASK_X;
195 src.swizzle = BRW_SWIZZLE_WWWW;
196 inst = emit(MOV(dst, src));
197
198 /* In dual instanced dispatch mode, dst has a width of 4, so we need
199 * to make sure the MOV happens regardless of which channels are
200 * enabled.
201 */
202 inst->force_writemask_all = true;
203 }
204 }
205
206 this->current_annotation = NULL;
207 }
208
209 void
210 vec4_gs_visitor::emit_thread_end()
211 {
212 if (c->control_data_header_size_bits > 0) {
213 /* During shader execution, we only ever call emit_control_data_bits()
214 * just prior to outputting a vertex. Therefore, the control data bits
215 * corresponding to the most recently output vertex still need to be
216 * emitted.
217 */
218 current_annotation = "thread end: emit control data bits";
219 emit_control_data_bits();
220 }
221
222 /* MRF 0 is reserved for the debugger, so start with message header
223 * in MRF 1.
224 */
225 int base_mrf = 1;
226
227 bool static_vertex_count = c->prog_data.static_vertex_count != -1;
228
229 /* If the previous instruction was a URB write, we don't need to issue
230 * a second one - we can just set the EOT bit on the previous write.
231 *
232 * Skip this on Gen8+ unless there's a static vertex count, as we also
233 * need to write the vertex count out, and combining the two may not be
234 * possible (or at least not straightforward).
235 */
236 vec4_instruction *last = (vec4_instruction *) instructions.get_tail();
237 if (last && last->opcode == GS_OPCODE_URB_WRITE &&
238 !(INTEL_DEBUG & DEBUG_SHADER_TIME) &&
239 devinfo->gen >= 8 && static_vertex_count) {
240 last->urb_write_flags = BRW_URB_WRITE_EOT | last->urb_write_flags;
241 return;
242 }
243
244 current_annotation = "thread end";
245 dst_reg mrf_reg(MRF, base_mrf);
246 src_reg r0(retype(brw_vec8_grf(0, 0), BRW_REGISTER_TYPE_UD));
247 vec4_instruction *inst = emit(MOV(mrf_reg, r0));
248 inst->force_writemask_all = true;
249 if (devinfo->gen < 8 || !static_vertex_count)
250 emit(GS_OPCODE_SET_VERTEX_COUNT, mrf_reg, this->vertex_count);
251 if (INTEL_DEBUG & DEBUG_SHADER_TIME)
252 emit_shader_time_end();
253 inst = emit(GS_OPCODE_THREAD_END);
254 inst->base_mrf = base_mrf;
255 inst->mlen = devinfo->gen >= 8 && !static_vertex_count ? 2 : 1;
256 }
257
258
259 void
260 vec4_gs_visitor::emit_urb_write_header(int mrf)
261 {
262 /* The SEND instruction that writes the vertex data to the VUE will use
263 * per_slot_offset=true, which means that DWORDs 3 and 4 of the message
264 * header specify an offset (in multiples of 256 bits) into the URB entry
265 * at which the write should take place.
266 *
267 * So we have to prepare a message header with the appropriate offset
268 * values.
269 */
270 dst_reg mrf_reg(MRF, mrf);
271 src_reg r0(retype(brw_vec8_grf(0, 0), BRW_REGISTER_TYPE_UD));
272 this->current_annotation = "URB write header";
273 vec4_instruction *inst = emit(MOV(mrf_reg, r0));
274 inst->force_writemask_all = true;
275 emit(GS_OPCODE_SET_WRITE_OFFSET, mrf_reg, this->vertex_count,
276 (uint32_t) c->prog_data.output_vertex_size_hwords);
277 }
278
279
280 vec4_instruction *
281 vec4_gs_visitor::emit_urb_write_opcode(bool complete)
282 {
283 /* We don't care whether the vertex is complete, because in general
284 * geometry shaders output multiple vertices, and we don't terminate the
285 * thread until all vertices are complete.
286 */
287 (void) complete;
288
289 vec4_instruction *inst = emit(GS_OPCODE_URB_WRITE);
290 inst->offset = c->prog_data.control_data_header_size_hwords;
291
292 /* We need to increment Global Offset by 1 to make room for Broadwell's
293 * extra "Vertex Count" payload at the beginning of the URB entry.
294 */
295 if (devinfo->gen >= 8 && c->prog_data.static_vertex_count == -1)
296 inst->offset++;
297
298 inst->urb_write_flags = BRW_URB_WRITE_PER_SLOT_OFFSET;
299 return inst;
300 }
301
302
303 /**
304 * Write out a batch of 32 control data bits from the control_data_bits
305 * register to the URB.
306 *
307 * The current value of the vertex_count register determines which DWORD in
308 * the URB receives the control data bits. The control_data_bits register is
309 * assumed to contain the correct data for the vertex that was most recently
310 * output, and all previous vertices that share the same DWORD.
311 *
312 * This function takes care of ensuring that if no vertices have been output
313 * yet, no control bits are emitted.
314 */
315 void
316 vec4_gs_visitor::emit_control_data_bits()
317 {
318 assert(c->control_data_bits_per_vertex != 0);
319
320 /* Since the URB_WRITE_OWORD message operates with 128-bit (vec4 sized)
321 * granularity, we need to use two tricks to ensure that the batch of 32
322 * control data bits is written to the appropriate DWORD in the URB. To
323 * select which vec4 we are writing to, we use the "slot {0,1} offset"
324 * fields of the message header. To select which DWORD in the vec4 we are
325 * writing to, we use the channel mask fields of the message header. To
326 * avoid penalizing geometry shaders that emit a small number of vertices
327 * with extra bookkeeping, we only do each of these tricks when
328 * c->prog_data.control_data_header_size_bits is large enough to make it
329 * necessary.
330 *
331 * Note: this means that if we're outputting just a single DWORD of control
332 * data bits, we'll actually replicate it four times since we won't do any
333 * channel masking. But that's not a problem since in this case the
334 * hardware only pays attention to the first DWORD.
335 */
336 enum brw_urb_write_flags urb_write_flags = BRW_URB_WRITE_OWORD;
337 if (c->control_data_header_size_bits > 32)
338 urb_write_flags = urb_write_flags | BRW_URB_WRITE_USE_CHANNEL_MASKS;
339 if (c->control_data_header_size_bits > 128)
340 urb_write_flags = urb_write_flags | BRW_URB_WRITE_PER_SLOT_OFFSET;
341
342 /* If we are using either channel masks or a per-slot offset, then we
343 * need to figure out which DWORD we are trying to write to, using the
344 * formula:
345 *
346 * dword_index = (vertex_count - 1) * bits_per_vertex / 32
347 *
348 * Since bits_per_vertex is a power of two, and is known at compile
349 * time, this can be optimized to:
350 *
351 * dword_index = (vertex_count - 1) >> (6 - log2(bits_per_vertex))
352 */
353 src_reg dword_index(this, glsl_type::uint_type);
354 if (urb_write_flags) {
355 src_reg prev_count(this, glsl_type::uint_type);
356 emit(ADD(dst_reg(prev_count), this->vertex_count, 0xffffffffu));
357 unsigned log2_bits_per_vertex =
358 _mesa_fls(c->control_data_bits_per_vertex);
359 emit(SHR(dst_reg(dword_index), prev_count,
360 (uint32_t) (6 - log2_bits_per_vertex)));
361 }
362
363 /* Start building the URB write message. The first MRF gets a copy of
364 * R0.
365 */
366 int base_mrf = 1;
367 dst_reg mrf_reg(MRF, base_mrf);
368 src_reg r0(retype(brw_vec8_grf(0, 0), BRW_REGISTER_TYPE_UD));
369 vec4_instruction *inst = emit(MOV(mrf_reg, r0));
370 inst->force_writemask_all = true;
371
372 if (urb_write_flags & BRW_URB_WRITE_PER_SLOT_OFFSET) {
373 /* Set the per-slot offset to dword_index / 4, to that we'll write to
374 * the appropriate OWORD within the control data header.
375 */
376 src_reg per_slot_offset(this, glsl_type::uint_type);
377 emit(SHR(dst_reg(per_slot_offset), dword_index, 2u));
378 emit(GS_OPCODE_SET_WRITE_OFFSET, mrf_reg, per_slot_offset, 1u);
379 }
380
381 if (urb_write_flags & BRW_URB_WRITE_USE_CHANNEL_MASKS) {
382 /* Set the channel masks to 1 << (dword_index % 4), so that we'll
383 * write to the appropriate DWORD within the OWORD. We need to do
384 * this computation with force_writemask_all, otherwise garbage data
385 * from invocation 0 might clobber the mask for invocation 1 when
386 * GS_OPCODE_PREPARE_CHANNEL_MASKS tries to OR the two masks
387 * together.
388 */
389 src_reg channel(this, glsl_type::uint_type);
390 inst = emit(AND(dst_reg(channel), dword_index, 3u));
391 inst->force_writemask_all = true;
392 src_reg one(this, glsl_type::uint_type);
393 inst = emit(MOV(dst_reg(one), 1u));
394 inst->force_writemask_all = true;
395 src_reg channel_mask(this, glsl_type::uint_type);
396 inst = emit(SHL(dst_reg(channel_mask), one, channel));
397 inst->force_writemask_all = true;
398 emit(GS_OPCODE_PREPARE_CHANNEL_MASKS, dst_reg(channel_mask),
399 channel_mask);
400 emit(GS_OPCODE_SET_CHANNEL_MASKS, mrf_reg, channel_mask);
401 }
402
403 /* Store the control data bits in the message payload and send it. */
404 dst_reg mrf_reg2(MRF, base_mrf + 1);
405 inst = emit(MOV(mrf_reg2, this->control_data_bits));
406 inst->force_writemask_all = true;
407 inst = emit(GS_OPCODE_URB_WRITE);
408 inst->urb_write_flags = urb_write_flags;
409 /* We need to increment Global Offset by 256-bits to make room for
410 * Broadwell's extra "Vertex Count" payload at the beginning of the
411 * URB entry. Since this is an OWord message, Global Offset is counted
412 * in 128-bit units, so we must set it to 2.
413 */
414 if (devinfo->gen >= 8 && c->prog_data.static_vertex_count == -1)
415 inst->offset = 2;
416 inst->base_mrf = base_mrf;
417 inst->mlen = 2;
418 }
419
420 void
421 vec4_gs_visitor::set_stream_control_data_bits(unsigned stream_id)
422 {
423 /* control_data_bits |= stream_id << ((2 * (vertex_count - 1)) % 32) */
424
425 /* Note: we are calling this *before* increasing vertex_count, so
426 * this->vertex_count == vertex_count - 1 in the formula above.
427 */
428
429 /* Stream mode uses 2 bits per vertex */
430 assert(c->control_data_bits_per_vertex == 2);
431
432 /* Must be a valid stream */
433 assert(stream_id >= 0 && stream_id < MAX_VERTEX_STREAMS);
434
435 /* Control data bits are initialized to 0 so we don't have to set any
436 * bits when sending vertices to stream 0.
437 */
438 if (stream_id == 0)
439 return;
440
441 /* reg::sid = stream_id */
442 src_reg sid(this, glsl_type::uint_type);
443 emit(MOV(dst_reg(sid), stream_id));
444
445 /* reg:shift_count = 2 * (vertex_count - 1) */
446 src_reg shift_count(this, glsl_type::uint_type);
447 emit(SHL(dst_reg(shift_count), this->vertex_count, 1u));
448
449 /* Note: we're relying on the fact that the GEN SHL instruction only pays
450 * attention to the lower 5 bits of its second source argument, so on this
451 * architecture, stream_id << 2 * (vertex_count - 1) is equivalent to
452 * stream_id << ((2 * (vertex_count - 1)) % 32).
453 */
454 src_reg mask(this, glsl_type::uint_type);
455 emit(SHL(dst_reg(mask), sid, shift_count));
456 emit(OR(dst_reg(this->control_data_bits), this->control_data_bits, mask));
457 }
458
459 void
460 vec4_gs_visitor::gs_emit_vertex(int stream_id)
461 {
462 this->current_annotation = "emit vertex: safety check";
463
464 /* Haswell and later hardware ignores the "Render Stream Select" bits
465 * from the 3DSTATE_STREAMOUT packet when the SOL stage is disabled,
466 * and instead sends all primitives down the pipeline for rasterization.
467 * If the SOL stage is enabled, "Render Stream Select" is honored and
468 * primitives bound to non-zero streams are discarded after stream output.
469 *
470 * Since the only purpose of primives sent to non-zero streams is to
471 * be recorded by transform feedback, we can simply discard all geometry
472 * bound to these streams when transform feedback is disabled.
473 */
474 if (stream_id > 0 && !nir->info.has_transform_feedback_varyings)
475 return;
476
477 /* If we're outputting 32 control data bits or less, then we can wait
478 * until the shader is over to output them all. Otherwise we need to
479 * output them as we go. Now is the time to do it, since we're about to
480 * output the vertex_count'th vertex, so it's guaranteed that the
481 * control data bits associated with the (vertex_count - 1)th vertex are
482 * correct.
483 */
484 if (c->control_data_header_size_bits > 32) {
485 this->current_annotation = "emit vertex: emit control data bits";
486 /* Only emit control data bits if we've finished accumulating a batch
487 * of 32 bits. This is the case when:
488 *
489 * (vertex_count * bits_per_vertex) % 32 == 0
490 *
491 * (in other words, when the last 5 bits of vertex_count *
492 * bits_per_vertex are 0). Assuming bits_per_vertex == 2^n for some
493 * integer n (which is always the case, since bits_per_vertex is
494 * always 1 or 2), this is equivalent to requiring that the last 5-n
495 * bits of vertex_count are 0:
496 *
497 * vertex_count & (2^(5-n) - 1) == 0
498 *
499 * 2^(5-n) == 2^5 / 2^n == 32 / bits_per_vertex, so this is
500 * equivalent to:
501 *
502 * vertex_count & (32 / bits_per_vertex - 1) == 0
503 */
504 vec4_instruction *inst =
505 emit(AND(dst_null_d(), this->vertex_count,
506 (uint32_t) (32 / c->control_data_bits_per_vertex - 1)));
507 inst->conditional_mod = BRW_CONDITIONAL_Z;
508
509 emit(IF(BRW_PREDICATE_NORMAL));
510 {
511 /* If vertex_count is 0, then no control data bits have been
512 * accumulated yet, so we skip emitting them.
513 */
514 emit(CMP(dst_null_d(), this->vertex_count, 0u,
515 BRW_CONDITIONAL_NEQ));
516 emit(IF(BRW_PREDICATE_NORMAL));
517 emit_control_data_bits();
518 emit(BRW_OPCODE_ENDIF);
519
520 /* Reset control_data_bits to 0 so we can start accumulating a new
521 * batch.
522 *
523 * Note: in the case where vertex_count == 0, this neutralizes the
524 * effect of any call to EndPrimitive() that the shader may have
525 * made before outputting its first vertex.
526 */
527 inst = emit(MOV(dst_reg(this->control_data_bits), 0u));
528 inst->force_writemask_all = true;
529 }
530 emit(BRW_OPCODE_ENDIF);
531 }
532
533 this->current_annotation = "emit vertex: vertex data";
534 emit_vertex();
535
536 /* In stream mode we have to set control data bits for all vertices
537 * unless we have disabled control data bits completely (which we do
538 * do for GL_POINTS outputs that don't use streams).
539 */
540 if (c->control_data_header_size_bits > 0 &&
541 c->prog_data.control_data_format ==
542 GEN7_GS_CONTROL_DATA_FORMAT_GSCTL_SID) {
543 this->current_annotation = "emit vertex: Stream control data bits";
544 set_stream_control_data_bits(stream_id);
545 }
546
547 this->current_annotation = NULL;
548 }
549
550 void
551 vec4_gs_visitor::gs_end_primitive()
552 {
553 /* We can only do EndPrimitive() functionality when the control data
554 * consists of cut bits. Fortunately, the only time it isn't is when the
555 * output type is points, in which case EndPrimitive() is a no-op.
556 */
557 if (c->prog_data.control_data_format !=
558 GEN7_GS_CONTROL_DATA_FORMAT_GSCTL_CUT) {
559 return;
560 }
561
562 /* Cut bits use one bit per vertex. */
563 assert(c->control_data_bits_per_vertex == 1);
564
565 /* Cut bit n should be set to 1 if EndPrimitive() was called after emitting
566 * vertex n, 0 otherwise. So all we need to do here is mark bit
567 * (vertex_count - 1) % 32 in the cut_bits register to indicate that
568 * EndPrimitive() was called after emitting vertex (vertex_count - 1);
569 * vec4_gs_visitor::emit_control_data_bits() will take care of the rest.
570 *
571 * Note that if EndPrimitve() is called before emitting any vertices, this
572 * will cause us to set bit 31 of the control_data_bits register to 1.
573 * That's fine because:
574 *
575 * - If max_vertices < 32, then vertex number 31 (zero-based) will never be
576 * output, so the hardware will ignore cut bit 31.
577 *
578 * - If max_vertices == 32, then vertex number 31 is guaranteed to be the
579 * last vertex, so setting cut bit 31 has no effect (since the primitive
580 * is automatically ended when the GS terminates).
581 *
582 * - If max_vertices > 32, then the ir_emit_vertex visitor will reset the
583 * control_data_bits register to 0 when the first vertex is emitted.
584 */
585
586 /* control_data_bits |= 1 << ((vertex_count - 1) % 32) */
587 src_reg one(this, glsl_type::uint_type);
588 emit(MOV(dst_reg(one), 1u));
589 src_reg prev_count(this, glsl_type::uint_type);
590 emit(ADD(dst_reg(prev_count), this->vertex_count, 0xffffffffu));
591 src_reg mask(this, glsl_type::uint_type);
592 /* Note: we're relying on the fact that the GEN SHL instruction only pays
593 * attention to the lower 5 bits of its second source argument, so on this
594 * architecture, 1 << (vertex_count - 1) is equivalent to 1 <<
595 * ((vertex_count - 1) % 32).
596 */
597 emit(SHL(dst_reg(mask), one, prev_count));
598 emit(OR(dst_reg(this->control_data_bits), this->control_data_bits, mask));
599 }
600
601 static const unsigned *
602 generate_assembly(struct brw_context *brw,
603 struct gl_shader_program *shader_prog,
604 struct gl_program *prog,
605 struct brw_vue_prog_data *prog_data,
606 void *mem_ctx,
607 const cfg_t *cfg,
608 unsigned *final_assembly_size)
609 {
610 vec4_generator g(brw->intelScreen->compiler, brw,
611 shader_prog, prog, prog_data, mem_ctx,
612 INTEL_DEBUG & DEBUG_GS, "geometry", "GS");
613 return g.generate_assembly(cfg, final_assembly_size);
614 }
615
616 extern "C" const unsigned *
617 brw_gs_emit(struct brw_context *brw,
618 struct gl_shader_program *prog,
619 struct brw_gs_compile *c,
620 void *mem_ctx,
621 int shader_time_index,
622 unsigned *final_assembly_size)
623 {
624 struct gl_shader *shader = prog->_LinkedShaders[MESA_SHADER_GEOMETRY];
625
626 if (brw->gen >= 7) {
627 /* Compile the geometry shader in DUAL_OBJECT dispatch mode, if we can do
628 * so without spilling. If the GS invocations count > 1, then we can't use
629 * dual object mode.
630 */
631 if (c->prog_data.invocations <= 1 &&
632 likely(!(INTEL_DEBUG & DEBUG_NO_DUAL_OBJECT_GS))) {
633 c->prog_data.base.dispatch_mode = DISPATCH_MODE_4X2_DUAL_OBJECT;
634
635 vec4_gs_visitor v(brw->intelScreen->compiler, brw,
636 c, shader->Program->nir,
637 mem_ctx, true /* no_spills */, shader_time_index);
638 if (v.run()) {
639 return generate_assembly(brw, prog, &c->gp->program.Base,
640 &c->prog_data.base, mem_ctx, v.cfg,
641 final_assembly_size);
642 }
643 }
644 }
645
646 /* Either we failed to compile in DUAL_OBJECT mode (probably because it
647 * would have required spilling) or DUAL_OBJECT mode is disabled. So fall
648 * back to DUAL_INSTANCED or SINGLE mode, which consumes fewer registers.
649 *
650 * FIXME: Single dispatch mode requires that the driver can handle
651 * interleaving of input registers, but this is already supported (dual
652 * instance mode has the same requirement). However, to take full advantage
653 * of single dispatch mode to reduce register pressure we would also need to
654 * do interleaved outputs, but currently, the vec4 visitor and generator
655 * classes do not support this, so at the moment register pressure in
656 * single and dual instance modes is the same.
657 *
658 * From the Ivy Bridge PRM, Vol2 Part1 7.2.1.1 "3DSTATE_GS"
659 * "If InstanceCount>1, DUAL_OBJECT mode is invalid. Software will likely
660 * want to use DUAL_INSTANCE mode for higher performance, but SINGLE mode
661 * is also supported. When InstanceCount=1 (one instance per object) software
662 * can decide which dispatch mode to use. DUAL_OBJECT mode would likely be
663 * the best choice for performance, followed by SINGLE mode."
664 *
665 * So SINGLE mode is more performant when invocations == 1 and DUAL_INSTANCE
666 * mode is more performant when invocations > 1. Gen6 only supports
667 * SINGLE mode.
668 */
669 if (c->prog_data.invocations <= 1 || brw->gen < 7)
670 c->prog_data.base.dispatch_mode = DISPATCH_MODE_4X1_SINGLE;
671 else
672 c->prog_data.base.dispatch_mode = DISPATCH_MODE_4X2_DUAL_INSTANCE;
673
674 vec4_gs_visitor *gs = NULL;
675 const unsigned *ret = NULL;
676
677 if (brw->gen >= 7)
678 gs = new vec4_gs_visitor(brw->intelScreen->compiler, brw,
679 c, shader->Program->nir,
680 mem_ctx, false /* no_spills */,
681 shader_time_index);
682 else
683 gs = new gen6_gs_visitor(brw->intelScreen->compiler, brw,
684 c, prog, shader->Program->nir,
685 mem_ctx, false /* no_spills */,
686 shader_time_index);
687
688 if (!gs->run()) {
689 prog->LinkStatus = false;
690 ralloc_strcat(&prog->InfoLog, gs->fail_msg);
691 } else {
692 ret = generate_assembly(brw, prog, &c->gp->program.Base,
693 &c->prog_data.base, mem_ctx, gs->cfg,
694 final_assembly_size);
695 }
696
697 delete gs;
698 return ret;
699 }
700
701
702 } /* namespace brw */