2f948ee73c094bf4ac9d258154711a6585a5fff5
[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 /* If vertex_count is 0, then no control data bits have been accumulated
352 * yet, so we should do nothing.
353 */
354 emit(CMP(dst_null_d(), this->vertex_count, 0u, BRW_CONDITIONAL_NEQ));
355 emit(IF(BRW_PREDICATE_NORMAL));
356 {
357 /* If we are using either channel masks or a per-slot offset, then we
358 * need to figure out which DWORD we are trying to write to, using the
359 * formula:
360 *
361 * dword_index = (vertex_count - 1) * bits_per_vertex / 32
362 *
363 * Since bits_per_vertex is a power of two, and is known at compile
364 * time, this can be optimized to:
365 *
366 * dword_index = (vertex_count - 1) >> (6 - log2(bits_per_vertex))
367 */
368 src_reg dword_index(this, glsl_type::uint_type);
369 if (urb_write_flags) {
370 src_reg prev_count(this, glsl_type::uint_type);
371 emit(ADD(dst_reg(prev_count), this->vertex_count, 0xffffffffu));
372 unsigned log2_bits_per_vertex =
373 _mesa_fls(c->control_data_bits_per_vertex);
374 emit(SHR(dst_reg(dword_index), prev_count,
375 (uint32_t) (6 - log2_bits_per_vertex)));
376 }
377
378 /* Start building the URB write message. The first MRF gets a copy of
379 * R0.
380 */
381 int base_mrf = 1;
382 dst_reg mrf_reg(MRF, base_mrf);
383 src_reg r0(retype(brw_vec8_grf(0, 0), BRW_REGISTER_TYPE_UD));
384 vec4_instruction *inst = emit(MOV(mrf_reg, r0));
385 inst->force_writemask_all = true;
386
387 if (urb_write_flags & BRW_URB_WRITE_PER_SLOT_OFFSET) {
388 /* Set the per-slot offset to dword_index / 4, to that we'll write to
389 * the appropriate OWORD within the control data header.
390 */
391 src_reg per_slot_offset(this, glsl_type::uint_type);
392 emit(SHR(dst_reg(per_slot_offset), dword_index, 2u));
393 emit(GS_OPCODE_SET_WRITE_OFFSET, mrf_reg, per_slot_offset, 1u);
394 }
395
396 if (urb_write_flags & BRW_URB_WRITE_USE_CHANNEL_MASKS) {
397 /* Set the channel masks to 1 << (dword_index % 4), so that we'll
398 * write to the appropriate DWORD within the OWORD. We need to do
399 * this computation with force_writemask_all, otherwise garbage data
400 * from invocation 0 might clobber the mask for invocation 1 when
401 * GS_OPCODE_PREPARE_CHANNEL_MASKS tries to OR the two masks
402 * together.
403 */
404 src_reg channel(this, glsl_type::uint_type);
405 inst = emit(AND(dst_reg(channel), dword_index, 3u));
406 inst->force_writemask_all = true;
407 src_reg one(this, glsl_type::uint_type);
408 inst = emit(MOV(dst_reg(one), 1u));
409 inst->force_writemask_all = true;
410 src_reg channel_mask(this, glsl_type::uint_type);
411 inst = emit(SHL(dst_reg(channel_mask), one, channel));
412 inst->force_writemask_all = true;
413 emit(GS_OPCODE_PREPARE_CHANNEL_MASKS, dst_reg(channel_mask),
414 channel_mask);
415 emit(GS_OPCODE_SET_CHANNEL_MASKS, mrf_reg, channel_mask);
416 }
417
418 /* Store the control data bits in the message payload and send it. */
419 dst_reg mrf_reg2(MRF, base_mrf + 1);
420 inst = emit(MOV(mrf_reg2, this->control_data_bits));
421 inst->force_writemask_all = true;
422 inst = emit(GS_OPCODE_URB_WRITE);
423 inst->urb_write_flags = urb_write_flags;
424 /* We need to increment Global Offset by 256-bits to make room for
425 * Broadwell's extra "Vertex Count" payload at the beginning of the
426 * URB entry. Since this is an OWord message, Global Offset is counted
427 * in 128-bit units, so we must set it to 2.
428 */
429 if (devinfo->gen >= 8)
430 inst->offset = 2;
431 inst->base_mrf = base_mrf;
432 inst->mlen = 2;
433 }
434 emit(BRW_OPCODE_ENDIF);
435 }
436
437 void
438 vec4_gs_visitor::set_stream_control_data_bits(unsigned stream_id)
439 {
440 /* control_data_bits |= stream_id << ((2 * (vertex_count - 1)) % 32) */
441
442 /* Note: we are calling this *before* increasing vertex_count, so
443 * this->vertex_count == vertex_count - 1 in the formula above.
444 */
445
446 /* Stream mode uses 2 bits per vertex */
447 assert(c->control_data_bits_per_vertex == 2);
448
449 /* Must be a valid stream */
450 assert(stream_id >= 0 && stream_id < MAX_VERTEX_STREAMS);
451
452 /* Control data bits are initialized to 0 so we don't have to set any
453 * bits when sending vertices to stream 0.
454 */
455 if (stream_id == 0)
456 return;
457
458 /* reg::sid = stream_id */
459 src_reg sid(this, glsl_type::uint_type);
460 emit(MOV(dst_reg(sid), stream_id));
461
462 /* reg:shift_count = 2 * (vertex_count - 1) */
463 src_reg shift_count(this, glsl_type::uint_type);
464 emit(SHL(dst_reg(shift_count), this->vertex_count, 1u));
465
466 /* Note: we're relying on the fact that the GEN SHL instruction only pays
467 * attention to the lower 5 bits of its second source argument, so on this
468 * architecture, stream_id << 2 * (vertex_count - 1) is equivalent to
469 * stream_id << ((2 * (vertex_count - 1)) % 32).
470 */
471 src_reg mask(this, glsl_type::uint_type);
472 emit(SHL(dst_reg(mask), sid, shift_count));
473 emit(OR(dst_reg(this->control_data_bits), this->control_data_bits, mask));
474 }
475
476 void
477 vec4_gs_visitor::visit(ir_emit_vertex *ir)
478 {
479 this->current_annotation = "emit vertex: safety check";
480
481 /* Haswell and later hardware ignores the "Render Stream Select" bits
482 * from the 3DSTATE_STREAMOUT packet when the SOL stage is disabled,
483 * and instead sends all primitives down the pipeline for rasterization.
484 * If the SOL stage is enabled, "Render Stream Select" is honored and
485 * primitives bound to non-zero streams are discarded after stream output.
486 *
487 * Since the only purpose of primives sent to non-zero streams is to
488 * be recorded by transform feedback, we can simply discard all geometry
489 * bound to these streams when transform feedback is disabled.
490 */
491 if (ir->stream_id() > 0 && shader_prog->TransformFeedback.NumVarying == 0)
492 return;
493
494 /* To ensure that we don't output more vertices than the shader specified
495 * using max_vertices, do the logic inside a conditional of the form "if
496 * (vertex_count < MAX)"
497 */
498 unsigned num_output_vertices = c->gp->program.VerticesOut;
499 emit(CMP(dst_null_d(), this->vertex_count,
500 src_reg(num_output_vertices), BRW_CONDITIONAL_L));
501 emit(IF(BRW_PREDICATE_NORMAL));
502 {
503 /* If we're outputting 32 control data bits or less, then we can wait
504 * until the shader is over to output them all. Otherwise we need to
505 * output them as we go. Now is the time to do it, since we're about to
506 * output the vertex_count'th vertex, so it's guaranteed that the
507 * control data bits associated with the (vertex_count - 1)th vertex are
508 * correct.
509 */
510 if (c->control_data_header_size_bits > 32) {
511 this->current_annotation = "emit vertex: emit control data bits";
512 /* Only emit control data bits if we've finished accumulating a batch
513 * of 32 bits. This is the case when:
514 *
515 * (vertex_count * bits_per_vertex) % 32 == 0
516 *
517 * (in other words, when the last 5 bits of vertex_count *
518 * bits_per_vertex are 0). Assuming bits_per_vertex == 2^n for some
519 * integer n (which is always the case, since bits_per_vertex is
520 * always 1 or 2), this is equivalent to requiring that the last 5-n
521 * bits of vertex_count are 0:
522 *
523 * vertex_count & (2^(5-n) - 1) == 0
524 *
525 * 2^(5-n) == 2^5 / 2^n == 32 / bits_per_vertex, so this is
526 * equivalent to:
527 *
528 * vertex_count & (32 / bits_per_vertex - 1) == 0
529 */
530 vec4_instruction *inst =
531 emit(AND(dst_null_d(), this->vertex_count,
532 (uint32_t) (32 / c->control_data_bits_per_vertex - 1)));
533 inst->conditional_mod = BRW_CONDITIONAL_Z;
534 emit(IF(BRW_PREDICATE_NORMAL));
535 {
536 emit_control_data_bits();
537
538 /* Reset control_data_bits to 0 so we can start accumulating a new
539 * batch.
540 *
541 * Note: in the case where vertex_count == 0, this neutralizes the
542 * effect of any call to EndPrimitive() that the shader may have
543 * made before outputting its first vertex.
544 */
545 inst = emit(MOV(dst_reg(this->control_data_bits), 0u));
546 inst->force_writemask_all = true;
547 }
548 emit(BRW_OPCODE_ENDIF);
549 }
550
551 this->current_annotation = "emit vertex: vertex data";
552 emit_vertex();
553
554 /* In stream mode we have to set control data bits for all vertices
555 * unless we have disabled control data bits completely (which we do
556 * do for GL_POINTS outputs that don't use streams).
557 */
558 if (c->control_data_header_size_bits > 0 &&
559 c->prog_data.control_data_format ==
560 GEN7_GS_CONTROL_DATA_FORMAT_GSCTL_SID) {
561 this->current_annotation = "emit vertex: Stream control data bits";
562 set_stream_control_data_bits(ir->stream_id());
563 }
564
565 this->current_annotation = "emit vertex: increment vertex count";
566 emit(ADD(dst_reg(this->vertex_count), this->vertex_count,
567 src_reg(1u)));
568 }
569 emit(BRW_OPCODE_ENDIF);
570
571 this->current_annotation = NULL;
572 }
573
574 void
575 vec4_gs_visitor::visit(ir_end_primitive *)
576 {
577 /* We can only do EndPrimitive() functionality when the control data
578 * consists of cut bits. Fortunately, the only time it isn't is when the
579 * output type is points, in which case EndPrimitive() is a no-op.
580 */
581 if (c->prog_data.control_data_format !=
582 GEN7_GS_CONTROL_DATA_FORMAT_GSCTL_CUT) {
583 return;
584 }
585
586 /* Cut bits use one bit per vertex. */
587 assert(c->control_data_bits_per_vertex == 1);
588
589 /* Cut bit n should be set to 1 if EndPrimitive() was called after emitting
590 * vertex n, 0 otherwise. So all we need to do here is mark bit
591 * (vertex_count - 1) % 32 in the cut_bits register to indicate that
592 * EndPrimitive() was called after emitting vertex (vertex_count - 1);
593 * vec4_gs_visitor::emit_control_data_bits() will take care of the rest.
594 *
595 * Note that if EndPrimitve() is called before emitting any vertices, this
596 * will cause us to set bit 31 of the control_data_bits register to 1.
597 * That's fine because:
598 *
599 * - If max_vertices < 32, then vertex number 31 (zero-based) will never be
600 * output, so the hardware will ignore cut bit 31.
601 *
602 * - If max_vertices == 32, then vertex number 31 is guaranteed to be the
603 * last vertex, so setting cut bit 31 has no effect (since the primitive
604 * is automatically ended when the GS terminates).
605 *
606 * - If max_vertices > 32, then the ir_emit_vertex visitor will reset the
607 * control_data_bits register to 0 when the first vertex is emitted.
608 */
609
610 /* control_data_bits |= 1 << ((vertex_count - 1) % 32) */
611 src_reg one(this, glsl_type::uint_type);
612 emit(MOV(dst_reg(one), 1u));
613 src_reg prev_count(this, glsl_type::uint_type);
614 emit(ADD(dst_reg(prev_count), this->vertex_count, 0xffffffffu));
615 src_reg mask(this, glsl_type::uint_type);
616 /* Note: we're relying on the fact that the GEN SHL instruction only pays
617 * attention to the lower 5 bits of its second source argument, so on this
618 * architecture, 1 << (vertex_count - 1) is equivalent to 1 <<
619 * ((vertex_count - 1) % 32).
620 */
621 emit(SHL(dst_reg(mask), one, prev_count));
622 emit(OR(dst_reg(this->control_data_bits), this->control_data_bits, mask));
623 }
624
625 static const unsigned *
626 generate_assembly(struct brw_context *brw,
627 struct gl_shader_program *shader_prog,
628 struct gl_program *prog,
629 struct brw_vue_prog_data *prog_data,
630 void *mem_ctx,
631 const cfg_t *cfg,
632 unsigned *final_assembly_size)
633 {
634 vec4_generator g(brw->intelScreen->compiler, brw,
635 shader_prog, prog, prog_data, mem_ctx,
636 INTEL_DEBUG & DEBUG_GS, "geometry", "GS");
637 return g.generate_assembly(cfg, final_assembly_size);
638 }
639
640 extern "C" const unsigned *
641 brw_gs_emit(struct brw_context *brw,
642 struct gl_shader_program *prog,
643 struct brw_gs_compile *c,
644 void *mem_ctx,
645 unsigned *final_assembly_size)
646 {
647 if (unlikely(INTEL_DEBUG & DEBUG_GS)) {
648 struct brw_shader *shader =
649 (brw_shader *) prog->_LinkedShaders[MESA_SHADER_GEOMETRY];
650
651 brw_dump_ir("geometry", prog, &shader->base, NULL);
652 }
653
654 int st_index = -1;
655 if (INTEL_DEBUG & DEBUG_SHADER_TIME)
656 st_index = brw_get_shader_time_index(brw, prog, NULL, ST_GS);
657
658 if (brw->gen >= 7) {
659 /* Compile the geometry shader in DUAL_OBJECT dispatch mode, if we can do
660 * so without spilling. If the GS invocations count > 1, then we can't use
661 * dual object mode.
662 */
663 if (c->prog_data.invocations <= 1 &&
664 likely(!(INTEL_DEBUG & DEBUG_NO_DUAL_OBJECT_GS))) {
665 c->prog_data.base.dispatch_mode = DISPATCH_MODE_4X2_DUAL_OBJECT;
666
667 vec4_gs_visitor v(brw->intelScreen->compiler, brw,
668 c, prog, mem_ctx, true /* no_spills */, st_index);
669 if (v.run(NULL /* clip planes */)) {
670 return generate_assembly(brw, prog, &c->gp->program.Base,
671 &c->prog_data.base, mem_ctx, v.cfg,
672 final_assembly_size);
673 }
674 }
675 }
676
677 /* Either we failed to compile in DUAL_OBJECT mode (probably because it
678 * would have required spilling) or DUAL_OBJECT mode is disabled. So fall
679 * back to DUAL_INSTANCED or SINGLE mode, which consumes fewer registers.
680 *
681 * FIXME: Single dispatch mode requires that the driver can handle
682 * interleaving of input registers, but this is already supported (dual
683 * instance mode has the same requirement). However, to take full advantage
684 * of single dispatch mode to reduce register pressure we would also need to
685 * do interleaved outputs, but currently, the vec4 visitor and generator
686 * classes do not support this, so at the moment register pressure in
687 * single and dual instance modes is the same.
688 *
689 * From the Ivy Bridge PRM, Vol2 Part1 7.2.1.1 "3DSTATE_GS"
690 * "If InstanceCount>1, DUAL_OBJECT mode is invalid. Software will likely
691 * want to use DUAL_INSTANCE mode for higher performance, but SINGLE mode
692 * is also supported. When InstanceCount=1 (one instance per object) software
693 * can decide which dispatch mode to use. DUAL_OBJECT mode would likely be
694 * the best choice for performance, followed by SINGLE mode."
695 *
696 * So SINGLE mode is more performant when invocations == 1 and DUAL_INSTANCE
697 * mode is more performant when invocations > 1. Gen6 only supports
698 * SINGLE mode.
699 */
700 if (c->prog_data.invocations <= 1 || brw->gen < 7)
701 c->prog_data.base.dispatch_mode = DISPATCH_MODE_4X1_SINGLE;
702 else
703 c->prog_data.base.dispatch_mode = DISPATCH_MODE_4X2_DUAL_INSTANCE;
704
705 vec4_gs_visitor *gs = NULL;
706 const unsigned *ret = NULL;
707
708 if (brw->gen >= 7)
709 gs = new vec4_gs_visitor(brw->intelScreen->compiler, brw,
710 c, prog, mem_ctx, false /* no_spills */,
711 st_index);
712 else
713 gs = new gen6_gs_visitor(brw->intelScreen->compiler, brw,
714 c, prog, mem_ctx, false /* no_spills */,
715 st_index);
716
717 if (!gs->run(NULL /* clip planes */)) {
718 prog->LinkStatus = false;
719 ralloc_strcat(&prog->InfoLog, gs->fail_msg);
720 } else {
721 ret = generate_assembly(brw, prog, &c->gp->program.Base,
722 &c->prog_data.base, mem_ctx, gs->cfg,
723 final_assembly_size);
724 }
725
726 delete gs;
727 return ret;
728 }
729
730
731 } /* namespace brw */