i965/gs: Move vertex_count != 0 check up a level; skip one caller.
[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 struct gl_shader_program *prog,
41 void *mem_ctx,
42 bool no_spills,
43 int shader_time_index)
44 : vec4_visitor(compiler, log_data,
45 &c->gp->program.Base, &c->key.base,
46 &c->prog_data.base, prog, MESA_SHADER_GEOMETRY, mem_ctx,
47 no_spills, shader_time_index),
48 c(c)
49 {
50 }
51
52
53 dst_reg *
54 vec4_gs_visitor::make_reg_for_system_value(ir_variable *ir)
55 {
56 dst_reg *reg = new(mem_ctx) dst_reg(this, ir->type);
57
58 switch (ir->data.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
210 void
211 vec4_gs_visitor::emit_program_code()
212 {
213 /* We don't support NV_geometry_program4. */
214 unreachable("Unreached");
215 }
216
217
218 void
219 vec4_gs_visitor::emit_thread_end()
220 {
221 if (c->control_data_header_size_bits > 0) {
222 /* During shader execution, we only ever call emit_control_data_bits()
223 * just prior to outputting a vertex. Therefore, the control data bits
224 * corresponding to the most recently output vertex still need to be
225 * emitted.
226 */
227 current_annotation = "thread end: emit control data bits";
228 emit_control_data_bits();
229 }
230
231 /* MRF 0 is reserved for the debugger, so start with message header
232 * in MRF 1.
233 */
234 int base_mrf = 1;
235
236 current_annotation = "thread end";
237 dst_reg mrf_reg(MRF, base_mrf);
238 src_reg r0(retype(brw_vec8_grf(0, 0), BRW_REGISTER_TYPE_UD));
239 vec4_instruction *inst = emit(MOV(mrf_reg, r0));
240 inst->force_writemask_all = true;
241 emit(GS_OPCODE_SET_VERTEX_COUNT, mrf_reg, this->vertex_count);
242 if (INTEL_DEBUG & DEBUG_SHADER_TIME)
243 emit_shader_time_end();
244 inst = emit(GS_OPCODE_THREAD_END);
245 inst->base_mrf = base_mrf;
246 inst->mlen = 1;
247 }
248
249
250 void
251 vec4_gs_visitor::emit_urb_write_header(int mrf)
252 {
253 /* The SEND instruction that writes the vertex data to the VUE will use
254 * per_slot_offset=true, which means that DWORDs 3 and 4 of the message
255 * header specify an offset (in multiples of 256 bits) into the URB entry
256 * at which the write should take place.
257 *
258 * So we have to prepare a message header with the appropriate offset
259 * values.
260 */
261 dst_reg mrf_reg(MRF, mrf);
262 src_reg r0(retype(brw_vec8_grf(0, 0), BRW_REGISTER_TYPE_UD));
263 this->current_annotation = "URB write header";
264 vec4_instruction *inst = emit(MOV(mrf_reg, r0));
265 inst->force_writemask_all = true;
266 emit(GS_OPCODE_SET_WRITE_OFFSET, mrf_reg, this->vertex_count,
267 (uint32_t) c->prog_data.output_vertex_size_hwords);
268 }
269
270
271 vec4_instruction *
272 vec4_gs_visitor::emit_urb_write_opcode(bool complete)
273 {
274 /* We don't care whether the vertex is complete, because in general
275 * geometry shaders output multiple vertices, and we don't terminate the
276 * thread until all vertices are complete.
277 */
278 (void) complete;
279
280 vec4_instruction *inst = emit(GS_OPCODE_URB_WRITE);
281 inst->offset = c->prog_data.control_data_header_size_hwords;
282
283 /* We need to increment Global Offset by 1 to make room for Broadwell's
284 * extra "Vertex Count" payload at the beginning of the URB entry.
285 */
286 if (devinfo->gen >= 8)
287 inst->offset++;
288
289 inst->urb_write_flags = BRW_URB_WRITE_PER_SLOT_OFFSET;
290 return inst;
291 }
292
293
294 int
295 vec4_gs_visitor::compute_array_stride(ir_dereference_array *ir)
296 {
297 /* Geometry shader inputs are arrays, but they use an unusual array layout:
298 * instead of all array elements for a given geometry shader input being
299 * stored consecutively, all geometry shader inputs are interleaved into
300 * one giant array. At this stage of compilation, we assume that the
301 * stride of the array is BRW_VARYING_SLOT_COUNT. Later,
302 * setup_attributes() will remap our accesses to the actual input array.
303 */
304 ir_dereference_variable *deref_var = ir->array->as_dereference_variable();
305 if (deref_var && deref_var->var->data.mode == ir_var_shader_in)
306 return BRW_VARYING_SLOT_COUNT;
307 else
308 return vec4_visitor::compute_array_stride(ir);
309 }
310
311
312 /**
313 * Write out a batch of 32 control data bits from the control_data_bits
314 * register to the URB.
315 *
316 * The current value of the vertex_count register determines which DWORD in
317 * the URB receives the control data bits. The control_data_bits register is
318 * assumed to contain the correct data for the vertex that was most recently
319 * output, and all previous vertices that share the same DWORD.
320 *
321 * This function takes care of ensuring that if no vertices have been output
322 * yet, no control bits are emitted.
323 */
324 void
325 vec4_gs_visitor::emit_control_data_bits()
326 {
327 assert(c->control_data_bits_per_vertex != 0);
328
329 /* Since the URB_WRITE_OWORD message operates with 128-bit (vec4 sized)
330 * granularity, we need to use two tricks to ensure that the batch of 32
331 * control data bits is written to the appropriate DWORD in the URB. To
332 * select which vec4 we are writing to, we use the "slot {0,1} offset"
333 * fields of the message header. To select which DWORD in the vec4 we are
334 * writing to, we use the channel mask fields of the message header. To
335 * avoid penalizing geometry shaders that emit a small number of vertices
336 * with extra bookkeeping, we only do each of these tricks when
337 * c->prog_data.control_data_header_size_bits is large enough to make it
338 * necessary.
339 *
340 * Note: this means that if we're outputting just a single DWORD of control
341 * data bits, we'll actually replicate it four times since we won't do any
342 * channel masking. But that's not a problem since in this case the
343 * hardware only pays attention to the first DWORD.
344 */
345 enum brw_urb_write_flags urb_write_flags = BRW_URB_WRITE_OWORD;
346 if (c->control_data_header_size_bits > 32)
347 urb_write_flags = urb_write_flags | BRW_URB_WRITE_USE_CHANNEL_MASKS;
348 if (c->control_data_header_size_bits > 128)
349 urb_write_flags = urb_write_flags | BRW_URB_WRITE_PER_SLOT_OFFSET;
350
351 {
352 /* If we are using either channel masks or a per-slot offset, then we
353 * need to figure out which DWORD we are trying to write to, using the
354 * formula:
355 *
356 * dword_index = (vertex_count - 1) * bits_per_vertex / 32
357 *
358 * Since bits_per_vertex is a power of two, and is known at compile
359 * time, this can be optimized to:
360 *
361 * dword_index = (vertex_count - 1) >> (6 - log2(bits_per_vertex))
362 */
363 src_reg dword_index(this, glsl_type::uint_type);
364 if (urb_write_flags) {
365 src_reg prev_count(this, glsl_type::uint_type);
366 emit(ADD(dst_reg(prev_count), this->vertex_count, 0xffffffffu));
367 unsigned log2_bits_per_vertex =
368 _mesa_fls(c->control_data_bits_per_vertex);
369 emit(SHR(dst_reg(dword_index), prev_count,
370 (uint32_t) (6 - log2_bits_per_vertex)));
371 }
372
373 /* Start building the URB write message. The first MRF gets a copy of
374 * R0.
375 */
376 int base_mrf = 1;
377 dst_reg mrf_reg(MRF, base_mrf);
378 src_reg r0(retype(brw_vec8_grf(0, 0), BRW_REGISTER_TYPE_UD));
379 vec4_instruction *inst = emit(MOV(mrf_reg, r0));
380 inst->force_writemask_all = true;
381
382 if (urb_write_flags & BRW_URB_WRITE_PER_SLOT_OFFSET) {
383 /* Set the per-slot offset to dword_index / 4, to that we'll write to
384 * the appropriate OWORD within the control data header.
385 */
386 src_reg per_slot_offset(this, glsl_type::uint_type);
387 emit(SHR(dst_reg(per_slot_offset), dword_index, 2u));
388 emit(GS_OPCODE_SET_WRITE_OFFSET, mrf_reg, per_slot_offset, 1u);
389 }
390
391 if (urb_write_flags & BRW_URB_WRITE_USE_CHANNEL_MASKS) {
392 /* Set the channel masks to 1 << (dword_index % 4), so that we'll
393 * write to the appropriate DWORD within the OWORD. We need to do
394 * this computation with force_writemask_all, otherwise garbage data
395 * from invocation 0 might clobber the mask for invocation 1 when
396 * GS_OPCODE_PREPARE_CHANNEL_MASKS tries to OR the two masks
397 * together.
398 */
399 src_reg channel(this, glsl_type::uint_type);
400 inst = emit(AND(dst_reg(channel), dword_index, 3u));
401 inst->force_writemask_all = true;
402 src_reg one(this, glsl_type::uint_type);
403 inst = emit(MOV(dst_reg(one), 1u));
404 inst->force_writemask_all = true;
405 src_reg channel_mask(this, glsl_type::uint_type);
406 inst = emit(SHL(dst_reg(channel_mask), one, channel));
407 inst->force_writemask_all = true;
408 emit(GS_OPCODE_PREPARE_CHANNEL_MASKS, dst_reg(channel_mask),
409 channel_mask);
410 emit(GS_OPCODE_SET_CHANNEL_MASKS, mrf_reg, channel_mask);
411 }
412
413 /* Store the control data bits in the message payload and send it. */
414 dst_reg mrf_reg2(MRF, base_mrf + 1);
415 inst = emit(MOV(mrf_reg2, this->control_data_bits));
416 inst->force_writemask_all = true;
417 inst = emit(GS_OPCODE_URB_WRITE);
418 inst->urb_write_flags = urb_write_flags;
419 /* We need to increment Global Offset by 256-bits to make room for
420 * Broadwell's extra "Vertex Count" payload at the beginning of the
421 * URB entry. Since this is an OWord message, Global Offset is counted
422 * in 128-bit units, so we must set it to 2.
423 */
424 if (devinfo->gen >= 8)
425 inst->offset = 2;
426 inst->base_mrf = base_mrf;
427 inst->mlen = 2;
428 }
429 }
430
431 void
432 vec4_gs_visitor::set_stream_control_data_bits(unsigned stream_id)
433 {
434 /* control_data_bits |= stream_id << ((2 * (vertex_count - 1)) % 32) */
435
436 /* Note: we are calling this *before* increasing vertex_count, so
437 * this->vertex_count == vertex_count - 1 in the formula above.
438 */
439
440 /* Stream mode uses 2 bits per vertex */
441 assert(c->control_data_bits_per_vertex == 2);
442
443 /* Must be a valid stream */
444 assert(stream_id >= 0 && stream_id < MAX_VERTEX_STREAMS);
445
446 /* Control data bits are initialized to 0 so we don't have to set any
447 * bits when sending vertices to stream 0.
448 */
449 if (stream_id == 0)
450 return;
451
452 /* reg::sid = stream_id */
453 src_reg sid(this, glsl_type::uint_type);
454 emit(MOV(dst_reg(sid), stream_id));
455
456 /* reg:shift_count = 2 * (vertex_count - 1) */
457 src_reg shift_count(this, glsl_type::uint_type);
458 emit(SHL(dst_reg(shift_count), this->vertex_count, 1u));
459
460 /* Note: we're relying on the fact that the GEN SHL instruction only pays
461 * attention to the lower 5 bits of its second source argument, so on this
462 * architecture, stream_id << 2 * (vertex_count - 1) is equivalent to
463 * stream_id << ((2 * (vertex_count - 1)) % 32).
464 */
465 src_reg mask(this, glsl_type::uint_type);
466 emit(SHL(dst_reg(mask), sid, shift_count));
467 emit(OR(dst_reg(this->control_data_bits), this->control_data_bits, mask));
468 }
469
470 void
471 vec4_gs_visitor::visit(ir_emit_vertex *ir)
472 {
473 this->current_annotation = "emit vertex: safety check";
474
475 /* Haswell and later hardware ignores the "Render Stream Select" bits
476 * from the 3DSTATE_STREAMOUT packet when the SOL stage is disabled,
477 * and instead sends all primitives down the pipeline for rasterization.
478 * If the SOL stage is enabled, "Render Stream Select" is honored and
479 * primitives bound to non-zero streams are discarded after stream output.
480 *
481 * Since the only purpose of primives sent to non-zero streams is to
482 * be recorded by transform feedback, we can simply discard all geometry
483 * bound to these streams when transform feedback is disabled.
484 */
485 if (ir->stream_id() > 0 && shader_prog->TransformFeedback.NumVarying == 0)
486 return;
487
488 /* To ensure that we don't output more vertices than the shader specified
489 * using max_vertices, do the logic inside a conditional of the form "if
490 * (vertex_count < MAX)"
491 */
492 unsigned num_output_vertices = c->gp->program.VerticesOut;
493 emit(CMP(dst_null_d(), this->vertex_count,
494 src_reg(num_output_vertices), BRW_CONDITIONAL_L));
495 emit(IF(BRW_PREDICATE_NORMAL));
496 {
497 /* If we're outputting 32 control data bits or less, then we can wait
498 * until the shader is over to output them all. Otherwise we need to
499 * output them as we go. Now is the time to do it, since we're about to
500 * output the vertex_count'th vertex, so it's guaranteed that the
501 * control data bits associated with the (vertex_count - 1)th vertex are
502 * correct.
503 */
504 if (c->control_data_header_size_bits > 32) {
505 this->current_annotation = "emit vertex: emit control data bits";
506 /* Only emit control data bits if we've finished accumulating a batch
507 * of 32 bits. This is the case when:
508 *
509 * (vertex_count * bits_per_vertex) % 32 == 0
510 *
511 * (in other words, when the last 5 bits of vertex_count *
512 * bits_per_vertex are 0). Assuming bits_per_vertex == 2^n for some
513 * integer n (which is always the case, since bits_per_vertex is
514 * always 1 or 2), this is equivalent to requiring that the last 5-n
515 * bits of vertex_count are 0:
516 *
517 * vertex_count & (2^(5-n) - 1) == 0
518 *
519 * 2^(5-n) == 2^5 / 2^n == 32 / bits_per_vertex, so this is
520 * equivalent to:
521 *
522 * vertex_count & (32 / bits_per_vertex - 1) == 0
523 */
524 vec4_instruction *inst =
525 emit(AND(dst_null_d(), this->vertex_count,
526 (uint32_t) (32 / c->control_data_bits_per_vertex - 1)));
527 inst->conditional_mod = BRW_CONDITIONAL_Z;
528
529 emit(IF(BRW_PREDICATE_NORMAL));
530 {
531 /* If vertex_count is 0, then no control data bits have been
532 * accumulated yet, so we skip emitting them.
533 */
534 emit(CMP(dst_null_d(), this->vertex_count, 0u,
535 BRW_CONDITIONAL_NEQ));
536 emit(IF(BRW_PREDICATE_NORMAL));
537 emit_control_data_bits();
538 emit(BRW_OPCODE_ENDIF);
539
540 /* Reset control_data_bits to 0 so we can start accumulating a new
541 * batch.
542 *
543 * Note: in the case where vertex_count == 0, this neutralizes the
544 * effect of any call to EndPrimitive() that the shader may have
545 * made before outputting its first vertex.
546 */
547 inst = emit(MOV(dst_reg(this->control_data_bits), 0u));
548 inst->force_writemask_all = true;
549 }
550 emit(BRW_OPCODE_ENDIF);
551 }
552
553 this->current_annotation = "emit vertex: vertex data";
554 emit_vertex();
555
556 /* In stream mode we have to set control data bits for all vertices
557 * unless we have disabled control data bits completely (which we do
558 * do for GL_POINTS outputs that don't use streams).
559 */
560 if (c->control_data_header_size_bits > 0 &&
561 c->prog_data.control_data_format ==
562 GEN7_GS_CONTROL_DATA_FORMAT_GSCTL_SID) {
563 this->current_annotation = "emit vertex: Stream control data bits";
564 set_stream_control_data_bits(ir->stream_id());
565 }
566
567 this->current_annotation = "emit vertex: increment vertex count";
568 emit(ADD(dst_reg(this->vertex_count), this->vertex_count,
569 src_reg(1u)));
570 }
571 emit(BRW_OPCODE_ENDIF);
572
573 this->current_annotation = NULL;
574 }
575
576 void
577 vec4_gs_visitor::visit(ir_end_primitive *)
578 {
579 /* We can only do EndPrimitive() functionality when the control data
580 * consists of cut bits. Fortunately, the only time it isn't is when the
581 * output type is points, in which case EndPrimitive() is a no-op.
582 */
583 if (c->prog_data.control_data_format !=
584 GEN7_GS_CONTROL_DATA_FORMAT_GSCTL_CUT) {
585 return;
586 }
587
588 /* Cut bits use one bit per vertex. */
589 assert(c->control_data_bits_per_vertex == 1);
590
591 /* Cut bit n should be set to 1 if EndPrimitive() was called after emitting
592 * vertex n, 0 otherwise. So all we need to do here is mark bit
593 * (vertex_count - 1) % 32 in the cut_bits register to indicate that
594 * EndPrimitive() was called after emitting vertex (vertex_count - 1);
595 * vec4_gs_visitor::emit_control_data_bits() will take care of the rest.
596 *
597 * Note that if EndPrimitve() is called before emitting any vertices, this
598 * will cause us to set bit 31 of the control_data_bits register to 1.
599 * That's fine because:
600 *
601 * - If max_vertices < 32, then vertex number 31 (zero-based) will never be
602 * output, so the hardware will ignore cut bit 31.
603 *
604 * - If max_vertices == 32, then vertex number 31 is guaranteed to be the
605 * last vertex, so setting cut bit 31 has no effect (since the primitive
606 * is automatically ended when the GS terminates).
607 *
608 * - If max_vertices > 32, then the ir_emit_vertex visitor will reset the
609 * control_data_bits register to 0 when the first vertex is emitted.
610 */
611
612 /* control_data_bits |= 1 << ((vertex_count - 1) % 32) */
613 src_reg one(this, glsl_type::uint_type);
614 emit(MOV(dst_reg(one), 1u));
615 src_reg prev_count(this, glsl_type::uint_type);
616 emit(ADD(dst_reg(prev_count), this->vertex_count, 0xffffffffu));
617 src_reg mask(this, glsl_type::uint_type);
618 /* Note: we're relying on the fact that the GEN SHL instruction only pays
619 * attention to the lower 5 bits of its second source argument, so on this
620 * architecture, 1 << (vertex_count - 1) is equivalent to 1 <<
621 * ((vertex_count - 1) % 32).
622 */
623 emit(SHL(dst_reg(mask), one, prev_count));
624 emit(OR(dst_reg(this->control_data_bits), this->control_data_bits, mask));
625 }
626
627 static const unsigned *
628 generate_assembly(struct brw_context *brw,
629 struct gl_shader_program *shader_prog,
630 struct gl_program *prog,
631 struct brw_vue_prog_data *prog_data,
632 void *mem_ctx,
633 const cfg_t *cfg,
634 unsigned *final_assembly_size)
635 {
636 vec4_generator g(brw->intelScreen->compiler, brw,
637 shader_prog, prog, prog_data, mem_ctx,
638 INTEL_DEBUG & DEBUG_GS, "geometry", "GS");
639 return g.generate_assembly(cfg, final_assembly_size);
640 }
641
642 extern "C" const unsigned *
643 brw_gs_emit(struct brw_context *brw,
644 struct gl_shader_program *prog,
645 struct brw_gs_compile *c,
646 void *mem_ctx,
647 unsigned *final_assembly_size)
648 {
649 if (unlikely(INTEL_DEBUG & DEBUG_GS)) {
650 struct brw_shader *shader =
651 (brw_shader *) prog->_LinkedShaders[MESA_SHADER_GEOMETRY];
652
653 brw_dump_ir("geometry", prog, &shader->base, NULL);
654 }
655
656 int st_index = -1;
657 if (INTEL_DEBUG & DEBUG_SHADER_TIME)
658 st_index = brw_get_shader_time_index(brw, prog, NULL, ST_GS);
659
660 if (brw->gen >= 7) {
661 /* Compile the geometry shader in DUAL_OBJECT dispatch mode, if we can do
662 * so without spilling. If the GS invocations count > 1, then we can't use
663 * dual object mode.
664 */
665 if (c->prog_data.invocations <= 1 &&
666 likely(!(INTEL_DEBUG & DEBUG_NO_DUAL_OBJECT_GS))) {
667 c->prog_data.base.dispatch_mode = DISPATCH_MODE_4X2_DUAL_OBJECT;
668
669 vec4_gs_visitor v(brw->intelScreen->compiler, brw,
670 c, prog, mem_ctx, true /* no_spills */, st_index);
671 if (v.run(NULL /* clip planes */)) {
672 return generate_assembly(brw, prog, &c->gp->program.Base,
673 &c->prog_data.base, mem_ctx, v.cfg,
674 final_assembly_size);
675 }
676 }
677 }
678
679 /* Either we failed to compile in DUAL_OBJECT mode (probably because it
680 * would have required spilling) or DUAL_OBJECT mode is disabled. So fall
681 * back to DUAL_INSTANCED or SINGLE mode, which consumes fewer registers.
682 *
683 * FIXME: Single dispatch mode requires that the driver can handle
684 * interleaving of input registers, but this is already supported (dual
685 * instance mode has the same requirement). However, to take full advantage
686 * of single dispatch mode to reduce register pressure we would also need to
687 * do interleaved outputs, but currently, the vec4 visitor and generator
688 * classes do not support this, so at the moment register pressure in
689 * single and dual instance modes is the same.
690 *
691 * From the Ivy Bridge PRM, Vol2 Part1 7.2.1.1 "3DSTATE_GS"
692 * "If InstanceCount>1, DUAL_OBJECT mode is invalid. Software will likely
693 * want to use DUAL_INSTANCE mode for higher performance, but SINGLE mode
694 * is also supported. When InstanceCount=1 (one instance per object) software
695 * can decide which dispatch mode to use. DUAL_OBJECT mode would likely be
696 * the best choice for performance, followed by SINGLE mode."
697 *
698 * So SINGLE mode is more performant when invocations == 1 and DUAL_INSTANCE
699 * mode is more performant when invocations > 1. Gen6 only supports
700 * SINGLE mode.
701 */
702 if (c->prog_data.invocations <= 1 || brw->gen < 7)
703 c->prog_data.base.dispatch_mode = DISPATCH_MODE_4X1_SINGLE;
704 else
705 c->prog_data.base.dispatch_mode = DISPATCH_MODE_4X2_DUAL_INSTANCE;
706
707 vec4_gs_visitor *gs = NULL;
708 const unsigned *ret = NULL;
709
710 if (brw->gen >= 7)
711 gs = new vec4_gs_visitor(brw->intelScreen->compiler, brw,
712 c, prog, mem_ctx, false /* no_spills */,
713 st_index);
714 else
715 gs = new gen6_gs_visitor(brw->intelScreen->compiler, brw,
716 c, prog, mem_ctx, false /* no_spills */,
717 st_index);
718
719 if (!gs->run(NULL /* clip planes */)) {
720 prog->LinkStatus = false;
721 ralloc_strcat(&prog->InfoLog, gs->fail_msg);
722 } else {
723 ret = generate_assembly(brw, prog, &c->gp->program.Base,
724 &c->prog_data.base, mem_ctx, gs->cfg,
725 final_assembly_size);
726 }
727
728 delete gs;
729 return ret;
730 }
731
732
733 } /* namespace brw */