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