i965: split EU defines to brw_eu_defines.h
[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 #include "brw_fs.h"
33 #include "brw_nir.h"
34 #include "common/gen_debug.h"
35
36 namespace brw {
37
38 vec4_gs_visitor::vec4_gs_visitor(const struct brw_compiler *compiler,
39 void *log_data,
40 struct brw_gs_compile *c,
41 struct brw_gs_prog_data *prog_data,
42 const nir_shader *shader,
43 void *mem_ctx,
44 bool no_spills,
45 int shader_time_index)
46 : vec4_visitor(compiler, log_data, &c->key.tex,
47 &prog_data->base, shader, mem_ctx,
48 no_spills, shader_time_index),
49 c(c),
50 gs_prog_data(prog_data)
51 {
52 }
53
54
55 dst_reg *
56 vec4_gs_visitor::make_reg_for_system_value(int location)
57 {
58 dst_reg *reg = new(mem_ctx) dst_reg(this, glsl_type::int_type);
59
60 switch (location) {
61 case SYSTEM_VALUE_INVOCATION_ID:
62 this->current_annotation = "initialize gl_InvocationID";
63 if (gs_prog_data->invocations > 1)
64 emit(GS_OPCODE_GET_INSTANCE_ID, *reg);
65 else
66 emit(MOV(*reg, brw_imm_ud(0)));
67 break;
68 default:
69 unreachable("not reached");
70 }
71
72 return reg;
73 }
74
75
76 int
77 vec4_gs_visitor::setup_varying_inputs(int payload_reg, int *attribute_map,
78 int attributes_per_reg)
79 {
80 /* For geometry shaders there are N copies of the input attributes, where N
81 * is the number of input vertices. attribute_map[BRW_VARYING_SLOT_COUNT *
82 * i + j] represents attribute j for vertex i.
83 *
84 * Note that GS inputs are read from the VUE 256 bits (2 vec4's) at a time,
85 * so the total number of input slots that will be delivered to the GS (and
86 * thus the stride of the input arrays) is urb_read_length * 2.
87 */
88 const unsigned num_input_vertices = nir->info->gs.vertices_in;
89 assert(num_input_vertices <= MAX_GS_INPUT_VERTICES);
90 unsigned input_array_stride = prog_data->urb_read_length * 2;
91
92 for (int slot = 0; slot < c->input_vue_map.num_slots; slot++) {
93 int varying = c->input_vue_map.slot_to_varying[slot];
94 for (unsigned vertex = 0; vertex < num_input_vertices; vertex++) {
95 attribute_map[BRW_VARYING_SLOT_COUNT * vertex + varying] =
96 attributes_per_reg * payload_reg + input_array_stride * vertex +
97 slot;
98 }
99 }
100
101 int regs_used = ALIGN(input_array_stride * num_input_vertices,
102 attributes_per_reg) / attributes_per_reg;
103 return payload_reg + regs_used;
104 }
105
106
107 void
108 vec4_gs_visitor::setup_payload()
109 {
110 int attribute_map[BRW_VARYING_SLOT_COUNT * MAX_GS_INPUT_VERTICES];
111
112 /* If we are in dual instanced or single mode, then attributes are going
113 * to be interleaved, so one register contains two attribute slots.
114 */
115 int attributes_per_reg =
116 prog_data->dispatch_mode == DISPATCH_MODE_4X2_DUAL_OBJECT ? 1 : 2;
117
118 /* If a geometry shader tries to read from an input that wasn't written by
119 * the vertex shader, that produces undefined results, but it shouldn't
120 * crash anything. So initialize attribute_map to zeros--that ensures that
121 * these undefined results are read from r0.
122 */
123 memset(attribute_map, 0, sizeof(attribute_map));
124
125 int reg = 0;
126
127 /* The payload always contains important data in r0, which contains
128 * the URB handles that are passed on to the URB write at the end
129 * of the thread.
130 */
131 reg++;
132
133 /* If the shader uses gl_PrimitiveIDIn, that goes in r1. */
134 if (gs_prog_data->include_primitive_id)
135 attribute_map[VARYING_SLOT_PRIMITIVE_ID] = attributes_per_reg * reg++;
136
137 reg = setup_uniforms(reg);
138
139 reg = setup_varying_inputs(reg, attribute_map, attributes_per_reg);
140
141 lower_attributes_to_hw_regs(attribute_map, attributes_per_reg > 1);
142
143 this->first_non_payload_grf = reg;
144 }
145
146
147 void
148 vec4_gs_visitor::emit_prolog()
149 {
150 /* In vertex shaders, r0.2 is guaranteed to be initialized to zero. In
151 * geometry shaders, it isn't (it contains a bunch of information we don't
152 * need, like the input primitive type). We need r0.2 to be zero in order
153 * to build scratch read/write messages correctly (otherwise this value
154 * will be interpreted as a global offset, causing us to do our scratch
155 * reads/writes to garbage memory). So just set it to zero at the top of
156 * the shader.
157 */
158 this->current_annotation = "clear r0.2";
159 dst_reg r0(retype(brw_vec4_grf(0, 0), BRW_REGISTER_TYPE_UD));
160 vec4_instruction *inst = emit(GS_OPCODE_SET_DWORD_2, r0, brw_imm_ud(0u));
161 inst->force_writemask_all = true;
162
163 /* Create a virtual register to hold the vertex count */
164 this->vertex_count = src_reg(this, glsl_type::uint_type);
165
166 /* Initialize the vertex_count register to 0 */
167 this->current_annotation = "initialize vertex_count";
168 inst = emit(MOV(dst_reg(this->vertex_count), brw_imm_ud(0u)));
169 inst->force_writemask_all = true;
170
171 if (c->control_data_header_size_bits > 0) {
172 /* Create a virtual register to hold the current set of control data
173 * bits.
174 */
175 this->control_data_bits = src_reg(this, glsl_type::uint_type);
176
177 /* If we're outputting more than 32 control data bits, then EmitVertex()
178 * will set control_data_bits to 0 after emitting the first vertex.
179 * Otherwise, we need to initialize it to 0 here.
180 */
181 if (c->control_data_header_size_bits <= 32) {
182 this->current_annotation = "initialize control data bits";
183 inst = emit(MOV(dst_reg(this->control_data_bits), brw_imm_ud(0u)));
184 inst->force_writemask_all = true;
185 }
186 }
187
188 this->current_annotation = NULL;
189 }
190
191 void
192 vec4_gs_visitor::emit_thread_end()
193 {
194 if (c->control_data_header_size_bits > 0) {
195 /* During shader execution, we only ever call emit_control_data_bits()
196 * just prior to outputting a vertex. Therefore, the control data bits
197 * corresponding to the most recently output vertex still need to be
198 * emitted.
199 */
200 current_annotation = "thread end: emit control data bits";
201 emit_control_data_bits();
202 }
203
204 /* MRF 0 is reserved for the debugger, so start with message header
205 * in MRF 1.
206 */
207 int base_mrf = 1;
208
209 bool static_vertex_count = gs_prog_data->static_vertex_count != -1;
210
211 /* If the previous instruction was a URB write, we don't need to issue
212 * a second one - we can just set the EOT bit on the previous write.
213 *
214 * Skip this on Gen8+ unless there's a static vertex count, as we also
215 * need to write the vertex count out, and combining the two may not be
216 * possible (or at least not straightforward).
217 */
218 vec4_instruction *last = (vec4_instruction *) instructions.get_tail();
219 if (last && last->opcode == GS_OPCODE_URB_WRITE &&
220 !(INTEL_DEBUG & DEBUG_SHADER_TIME) &&
221 devinfo->gen >= 8 && static_vertex_count) {
222 last->urb_write_flags = BRW_URB_WRITE_EOT | last->urb_write_flags;
223 return;
224 }
225
226 current_annotation = "thread end";
227 dst_reg mrf_reg(MRF, base_mrf);
228 src_reg r0(retype(brw_vec8_grf(0, 0), BRW_REGISTER_TYPE_UD));
229 vec4_instruction *inst = emit(MOV(mrf_reg, r0));
230 inst->force_writemask_all = true;
231 if (devinfo->gen < 8 || !static_vertex_count)
232 emit(GS_OPCODE_SET_VERTEX_COUNT, mrf_reg, this->vertex_count);
233 if (INTEL_DEBUG & DEBUG_SHADER_TIME)
234 emit_shader_time_end();
235 inst = emit(GS_OPCODE_THREAD_END);
236 inst->base_mrf = base_mrf;
237 inst->mlen = devinfo->gen >= 8 && !static_vertex_count ? 2 : 1;
238 }
239
240
241 void
242 vec4_gs_visitor::emit_urb_write_header(int mrf)
243 {
244 /* The SEND instruction that writes the vertex data to the VUE will use
245 * per_slot_offset=true, which means that DWORDs 3 and 4 of the message
246 * header specify an offset (in multiples of 256 bits) into the URB entry
247 * at which the write should take place.
248 *
249 * So we have to prepare a message header with the appropriate offset
250 * values.
251 */
252 dst_reg mrf_reg(MRF, mrf);
253 src_reg r0(retype(brw_vec8_grf(0, 0), BRW_REGISTER_TYPE_UD));
254 this->current_annotation = "URB write header";
255 vec4_instruction *inst = emit(MOV(mrf_reg, r0));
256 inst->force_writemask_all = true;
257 emit(GS_OPCODE_SET_WRITE_OFFSET, mrf_reg, this->vertex_count,
258 brw_imm_ud(gs_prog_data->output_vertex_size_hwords));
259 }
260
261
262 vec4_instruction *
263 vec4_gs_visitor::emit_urb_write_opcode(bool complete)
264 {
265 /* We don't care whether the vertex is complete, because in general
266 * geometry shaders output multiple vertices, and we don't terminate the
267 * thread until all vertices are complete.
268 */
269 (void) complete;
270
271 vec4_instruction *inst = emit(GS_OPCODE_URB_WRITE);
272 inst->offset = gs_prog_data->control_data_header_size_hwords;
273
274 /* We need to increment Global Offset by 1 to make room for Broadwell's
275 * extra "Vertex Count" payload at the beginning of the URB entry.
276 */
277 if (devinfo->gen >= 8 && gs_prog_data->static_vertex_count == -1)
278 inst->offset++;
279
280 inst->urb_write_flags = BRW_URB_WRITE_PER_SLOT_OFFSET;
281 return inst;
282 }
283
284
285 /**
286 * Write out a batch of 32 control data bits from the control_data_bits
287 * register to the URB.
288 *
289 * The current value of the vertex_count register determines which DWORD in
290 * the URB receives the control data bits. The control_data_bits register is
291 * assumed to contain the correct data for the vertex that was most recently
292 * output, and all previous vertices that share the same DWORD.
293 *
294 * This function takes care of ensuring that if no vertices have been output
295 * yet, no control bits are emitted.
296 */
297 void
298 vec4_gs_visitor::emit_control_data_bits()
299 {
300 assert(c->control_data_bits_per_vertex != 0);
301
302 /* Since the URB_WRITE_OWORD message operates with 128-bit (vec4 sized)
303 * granularity, we need to use two tricks to ensure that the batch of 32
304 * control data bits is written to the appropriate DWORD in the URB. To
305 * select which vec4 we are writing to, we use the "slot {0,1} offset"
306 * fields of the message header. To select which DWORD in the vec4 we are
307 * writing to, we use the channel mask fields of the message header. To
308 * avoid penalizing geometry shaders that emit a small number of vertices
309 * with extra bookkeeping, we only do each of these tricks when
310 * c->prog_data.control_data_header_size_bits is large enough to make it
311 * necessary.
312 *
313 * Note: this means that if we're outputting just a single DWORD of control
314 * data bits, we'll actually replicate it four times since we won't do any
315 * channel masking. But that's not a problem since in this case the
316 * hardware only pays attention to the first DWORD.
317 */
318 enum brw_urb_write_flags urb_write_flags = BRW_URB_WRITE_OWORD;
319 if (c->control_data_header_size_bits > 32)
320 urb_write_flags = urb_write_flags | BRW_URB_WRITE_USE_CHANNEL_MASKS;
321 if (c->control_data_header_size_bits > 128)
322 urb_write_flags = urb_write_flags | BRW_URB_WRITE_PER_SLOT_OFFSET;
323
324 /* If we are using either channel masks or a per-slot offset, then we
325 * need to figure out which DWORD we are trying to write to, using the
326 * formula:
327 *
328 * dword_index = (vertex_count - 1) * bits_per_vertex / 32
329 *
330 * Since bits_per_vertex is a power of two, and is known at compile
331 * time, this can be optimized to:
332 *
333 * dword_index = (vertex_count - 1) >> (6 - log2(bits_per_vertex))
334 */
335 src_reg dword_index(this, glsl_type::uint_type);
336 if (urb_write_flags) {
337 src_reg prev_count(this, glsl_type::uint_type);
338 emit(ADD(dst_reg(prev_count), this->vertex_count,
339 brw_imm_ud(0xffffffffu)));
340 unsigned log2_bits_per_vertex =
341 util_last_bit(c->control_data_bits_per_vertex);
342 emit(SHR(dst_reg(dword_index), prev_count,
343 brw_imm_ud(6 - log2_bits_per_vertex)));
344 }
345
346 /* Start building the URB write message. The first MRF gets a copy of
347 * R0.
348 */
349 int base_mrf = 1;
350 dst_reg mrf_reg(MRF, base_mrf);
351 src_reg r0(retype(brw_vec8_grf(0, 0), BRW_REGISTER_TYPE_UD));
352 vec4_instruction *inst = emit(MOV(mrf_reg, r0));
353 inst->force_writemask_all = true;
354
355 if (urb_write_flags & BRW_URB_WRITE_PER_SLOT_OFFSET) {
356 /* Set the per-slot offset to dword_index / 4, to that we'll write to
357 * the appropriate OWORD within the control data header.
358 */
359 src_reg per_slot_offset(this, glsl_type::uint_type);
360 emit(SHR(dst_reg(per_slot_offset), dword_index, brw_imm_ud(2u)));
361 emit(GS_OPCODE_SET_WRITE_OFFSET, mrf_reg, per_slot_offset,
362 brw_imm_ud(1u));
363 }
364
365 if (urb_write_flags & BRW_URB_WRITE_USE_CHANNEL_MASKS) {
366 /* Set the channel masks to 1 << (dword_index % 4), so that we'll
367 * write to the appropriate DWORD within the OWORD. We need to do
368 * this computation with force_writemask_all, otherwise garbage data
369 * from invocation 0 might clobber the mask for invocation 1 when
370 * GS_OPCODE_PREPARE_CHANNEL_MASKS tries to OR the two masks
371 * together.
372 */
373 src_reg channel(this, glsl_type::uint_type);
374 inst = emit(AND(dst_reg(channel), dword_index, brw_imm_ud(3u)));
375 inst->force_writemask_all = true;
376 src_reg one(this, glsl_type::uint_type);
377 inst = emit(MOV(dst_reg(one), brw_imm_ud(1u)));
378 inst->force_writemask_all = true;
379 src_reg channel_mask(this, glsl_type::uint_type);
380 inst = emit(SHL(dst_reg(channel_mask), one, channel));
381 inst->force_writemask_all = true;
382 emit(GS_OPCODE_PREPARE_CHANNEL_MASKS, dst_reg(channel_mask),
383 channel_mask);
384 emit(GS_OPCODE_SET_CHANNEL_MASKS, mrf_reg, channel_mask);
385 }
386
387 /* Store the control data bits in the message payload and send it. */
388 dst_reg mrf_reg2(MRF, base_mrf + 1);
389 inst = emit(MOV(mrf_reg2, this->control_data_bits));
390 inst->force_writemask_all = true;
391 inst = emit(GS_OPCODE_URB_WRITE);
392 inst->urb_write_flags = urb_write_flags;
393 /* We need to increment Global Offset by 256-bits to make room for
394 * Broadwell's extra "Vertex Count" payload at the beginning of the
395 * URB entry. Since this is an OWord message, Global Offset is counted
396 * in 128-bit units, so we must set it to 2.
397 */
398 if (devinfo->gen >= 8 && gs_prog_data->static_vertex_count == -1)
399 inst->offset = 2;
400 inst->base_mrf = base_mrf;
401 inst->mlen = 2;
402 }
403
404 void
405 vec4_gs_visitor::set_stream_control_data_bits(unsigned stream_id)
406 {
407 /* control_data_bits |= stream_id << ((2 * (vertex_count - 1)) % 32) */
408
409 /* Note: we are calling this *before* increasing vertex_count, so
410 * this->vertex_count == vertex_count - 1 in the formula above.
411 */
412
413 /* Stream mode uses 2 bits per vertex */
414 assert(c->control_data_bits_per_vertex == 2);
415
416 /* Must be a valid stream */
417 assert(stream_id >= 0 && stream_id < MAX_VERTEX_STREAMS);
418
419 /* Control data bits are initialized to 0 so we don't have to set any
420 * bits when sending vertices to stream 0.
421 */
422 if (stream_id == 0)
423 return;
424
425 /* reg::sid = stream_id */
426 src_reg sid(this, glsl_type::uint_type);
427 emit(MOV(dst_reg(sid), brw_imm_ud(stream_id)));
428
429 /* reg:shift_count = 2 * (vertex_count - 1) */
430 src_reg shift_count(this, glsl_type::uint_type);
431 emit(SHL(dst_reg(shift_count), this->vertex_count, brw_imm_ud(1u)));
432
433 /* Note: we're relying on the fact that the GEN SHL instruction only pays
434 * attention to the lower 5 bits of its second source argument, so on this
435 * architecture, stream_id << 2 * (vertex_count - 1) is equivalent to
436 * stream_id << ((2 * (vertex_count - 1)) % 32).
437 */
438 src_reg mask(this, glsl_type::uint_type);
439 emit(SHL(dst_reg(mask), sid, shift_count));
440 emit(OR(dst_reg(this->control_data_bits), this->control_data_bits, mask));
441 }
442
443 void
444 vec4_gs_visitor::gs_emit_vertex(int stream_id)
445 {
446 this->current_annotation = "emit vertex: safety check";
447
448 /* Haswell and later hardware ignores the "Render Stream Select" bits
449 * from the 3DSTATE_STREAMOUT packet when the SOL stage is disabled,
450 * and instead sends all primitives down the pipeline for rasterization.
451 * If the SOL stage is enabled, "Render Stream Select" is honored and
452 * primitives bound to non-zero streams are discarded after stream output.
453 *
454 * Since the only purpose of primives sent to non-zero streams is to
455 * be recorded by transform feedback, we can simply discard all geometry
456 * bound to these streams when transform feedback is disabled.
457 */
458 if (stream_id > 0 && !nir->info->has_transform_feedback_varyings)
459 return;
460
461 /* If we're outputting 32 control data bits or less, then we can wait
462 * until the shader is over to output them all. Otherwise we need to
463 * output them as we go. Now is the time to do it, since we're about to
464 * output the vertex_count'th vertex, so it's guaranteed that the
465 * control data bits associated with the (vertex_count - 1)th vertex are
466 * correct.
467 */
468 if (c->control_data_header_size_bits > 32) {
469 this->current_annotation = "emit vertex: emit control data bits";
470 /* Only emit control data bits if we've finished accumulating a batch
471 * of 32 bits. This is the case when:
472 *
473 * (vertex_count * bits_per_vertex) % 32 == 0
474 *
475 * (in other words, when the last 5 bits of vertex_count *
476 * bits_per_vertex are 0). Assuming bits_per_vertex == 2^n for some
477 * integer n (which is always the case, since bits_per_vertex is
478 * always 1 or 2), this is equivalent to requiring that the last 5-n
479 * bits of vertex_count are 0:
480 *
481 * vertex_count & (2^(5-n) - 1) == 0
482 *
483 * 2^(5-n) == 2^5 / 2^n == 32 / bits_per_vertex, so this is
484 * equivalent to:
485 *
486 * vertex_count & (32 / bits_per_vertex - 1) == 0
487 */
488 vec4_instruction *inst =
489 emit(AND(dst_null_ud(), this->vertex_count,
490 brw_imm_ud(32 / c->control_data_bits_per_vertex - 1)));
491 inst->conditional_mod = BRW_CONDITIONAL_Z;
492
493 emit(IF(BRW_PREDICATE_NORMAL));
494 {
495 /* If vertex_count is 0, then no control data bits have been
496 * accumulated yet, so we skip emitting them.
497 */
498 emit(CMP(dst_null_ud(), this->vertex_count, brw_imm_ud(0u),
499 BRW_CONDITIONAL_NEQ));
500 emit(IF(BRW_PREDICATE_NORMAL));
501 emit_control_data_bits();
502 emit(BRW_OPCODE_ENDIF);
503
504 /* Reset control_data_bits to 0 so we can start accumulating a new
505 * batch.
506 *
507 * Note: in the case where vertex_count == 0, this neutralizes the
508 * effect of any call to EndPrimitive() that the shader may have
509 * made before outputting its first vertex.
510 */
511 inst = emit(MOV(dst_reg(this->control_data_bits), brw_imm_ud(0u)));
512 inst->force_writemask_all = true;
513 }
514 emit(BRW_OPCODE_ENDIF);
515 }
516
517 this->current_annotation = "emit vertex: vertex data";
518 emit_vertex();
519
520 /* In stream mode we have to set control data bits for all vertices
521 * unless we have disabled control data bits completely (which we do
522 * do for GL_POINTS outputs that don't use streams).
523 */
524 if (c->control_data_header_size_bits > 0 &&
525 gs_prog_data->control_data_format ==
526 GEN7_GS_CONTROL_DATA_FORMAT_GSCTL_SID) {
527 this->current_annotation = "emit vertex: Stream control data bits";
528 set_stream_control_data_bits(stream_id);
529 }
530
531 this->current_annotation = NULL;
532 }
533
534 void
535 vec4_gs_visitor::gs_end_primitive()
536 {
537 /* We can only do EndPrimitive() functionality when the control data
538 * consists of cut bits. Fortunately, the only time it isn't is when the
539 * output type is points, in which case EndPrimitive() is a no-op.
540 */
541 if (gs_prog_data->control_data_format !=
542 GEN7_GS_CONTROL_DATA_FORMAT_GSCTL_CUT) {
543 return;
544 }
545
546 if (c->control_data_header_size_bits == 0)
547 return;
548
549 /* Cut bits use one bit per vertex. */
550 assert(c->control_data_bits_per_vertex == 1);
551
552 /* Cut bit n should be set to 1 if EndPrimitive() was called after emitting
553 * vertex n, 0 otherwise. So all we need to do here is mark bit
554 * (vertex_count - 1) % 32 in the cut_bits register to indicate that
555 * EndPrimitive() was called after emitting vertex (vertex_count - 1);
556 * vec4_gs_visitor::emit_control_data_bits() will take care of the rest.
557 *
558 * Note that if EndPrimitve() is called before emitting any vertices, this
559 * will cause us to set bit 31 of the control_data_bits register to 1.
560 * That's fine because:
561 *
562 * - If max_vertices < 32, then vertex number 31 (zero-based) will never be
563 * output, so the hardware will ignore cut bit 31.
564 *
565 * - If max_vertices == 32, then vertex number 31 is guaranteed to be the
566 * last vertex, so setting cut bit 31 has no effect (since the primitive
567 * is automatically ended when the GS terminates).
568 *
569 * - If max_vertices > 32, then the ir_emit_vertex visitor will reset the
570 * control_data_bits register to 0 when the first vertex is emitted.
571 */
572
573 /* control_data_bits |= 1 << ((vertex_count - 1) % 32) */
574 src_reg one(this, glsl_type::uint_type);
575 emit(MOV(dst_reg(one), brw_imm_ud(1u)));
576 src_reg prev_count(this, glsl_type::uint_type);
577 emit(ADD(dst_reg(prev_count), this->vertex_count, brw_imm_ud(0xffffffffu)));
578 src_reg mask(this, glsl_type::uint_type);
579 /* Note: we're relying on the fact that the GEN SHL instruction only pays
580 * attention to the lower 5 bits of its second source argument, so on this
581 * architecture, 1 << (vertex_count - 1) is equivalent to 1 <<
582 * ((vertex_count - 1) % 32).
583 */
584 emit(SHL(dst_reg(mask), one, prev_count));
585 emit(OR(dst_reg(this->control_data_bits), this->control_data_bits, mask));
586 }
587
588 static const GLuint gl_prim_to_hw_prim[GL_TRIANGLE_STRIP_ADJACENCY+1] = {
589 [GL_POINTS] =_3DPRIM_POINTLIST,
590 [GL_LINES] = _3DPRIM_LINELIST,
591 [GL_LINE_LOOP] = _3DPRIM_LINELOOP,
592 [GL_LINE_STRIP] = _3DPRIM_LINESTRIP,
593 [GL_TRIANGLES] = _3DPRIM_TRILIST,
594 [GL_TRIANGLE_STRIP] = _3DPRIM_TRISTRIP,
595 [GL_TRIANGLE_FAN] = _3DPRIM_TRIFAN,
596 [GL_QUADS] = _3DPRIM_QUADLIST,
597 [GL_QUAD_STRIP] = _3DPRIM_QUADSTRIP,
598 [GL_POLYGON] = _3DPRIM_POLYGON,
599 [GL_LINES_ADJACENCY] = _3DPRIM_LINELIST_ADJ,
600 [GL_LINE_STRIP_ADJACENCY] = _3DPRIM_LINESTRIP_ADJ,
601 [GL_TRIANGLES_ADJACENCY] = _3DPRIM_TRILIST_ADJ,
602 [GL_TRIANGLE_STRIP_ADJACENCY] = _3DPRIM_TRISTRIP_ADJ,
603 };
604
605 extern "C" const unsigned *
606 brw_compile_gs(const struct brw_compiler *compiler, void *log_data,
607 void *mem_ctx,
608 const struct brw_gs_prog_key *key,
609 struct brw_gs_prog_data *prog_data,
610 const nir_shader *src_shader,
611 struct gl_program *prog,
612 int shader_time_index,
613 unsigned *final_assembly_size,
614 char **error_str)
615 {
616 struct brw_gs_compile c;
617 memset(&c, 0, sizeof(c));
618 c.key = *key;
619
620 const bool is_scalar = compiler->scalar_stage[MESA_SHADER_GEOMETRY];
621 nir_shader *shader = nir_shader_clone(mem_ctx, src_shader);
622
623 /* The GLSL linker will have already matched up GS inputs and the outputs
624 * of prior stages. The driver does extend VS outputs in some cases, but
625 * only for legacy OpenGL or Gen4-5 hardware, neither of which offer
626 * geometry shader support. So we can safely ignore that.
627 *
628 * For SSO pipelines, we use a fixed VUE map layout based on variable
629 * locations, so we can rely on rendezvous-by-location making this work.
630 */
631 GLbitfield64 inputs_read = shader->info->inputs_read;
632 brw_compute_vue_map(compiler->devinfo,
633 &c.input_vue_map, inputs_read,
634 shader->info->separate_shader);
635
636 shader = brw_nir_apply_sampler_key(shader, compiler, &key->tex, is_scalar);
637 brw_nir_lower_vue_inputs(shader, is_scalar, &c.input_vue_map);
638 brw_nir_lower_vue_outputs(shader, is_scalar);
639 shader = brw_postprocess_nir(shader, compiler, is_scalar);
640
641 prog_data->base.clip_distance_mask =
642 ((1 << shader->info->clip_distance_array_size) - 1);
643 prog_data->base.cull_distance_mask =
644 ((1 << shader->info->cull_distance_array_size) - 1) <<
645 shader->info->clip_distance_array_size;
646
647 prog_data->include_primitive_id =
648 (shader->info->system_values_read & (1 << SYSTEM_VALUE_PRIMITIVE_ID)) != 0;
649
650 prog_data->invocations = shader->info->gs.invocations;
651
652 if (compiler->devinfo->gen >= 8)
653 prog_data->static_vertex_count = nir_gs_count_vertices(shader);
654
655 if (compiler->devinfo->gen >= 7) {
656 if (shader->info->gs.output_primitive == GL_POINTS) {
657 /* When the output type is points, the geometry shader may output data
658 * to multiple streams, and EndPrimitive() has no effect. So we
659 * configure the hardware to interpret the control data as stream ID.
660 */
661 prog_data->control_data_format = GEN7_GS_CONTROL_DATA_FORMAT_GSCTL_SID;
662
663 /* We only have to emit control bits if we are using streams */
664 if (prog && prog->info.gs.uses_streams)
665 c.control_data_bits_per_vertex = 2;
666 else
667 c.control_data_bits_per_vertex = 0;
668 } else {
669 /* When the output type is triangle_strip or line_strip, EndPrimitive()
670 * may be used to terminate the current strip and start a new one
671 * (similar to primitive restart), and outputting data to multiple
672 * streams is not supported. So we configure the hardware to interpret
673 * the control data as EndPrimitive information (a.k.a. "cut bits").
674 */
675 prog_data->control_data_format = GEN7_GS_CONTROL_DATA_FORMAT_GSCTL_CUT;
676
677 /* We only need to output control data if the shader actually calls
678 * EndPrimitive().
679 */
680 c.control_data_bits_per_vertex =
681 shader->info->gs.uses_end_primitive ? 1 : 0;
682 }
683 } else {
684 /* There are no control data bits in gen6. */
685 c.control_data_bits_per_vertex = 0;
686 }
687 c.control_data_header_size_bits =
688 shader->info->gs.vertices_out * c.control_data_bits_per_vertex;
689
690 /* 1 HWORD = 32 bytes = 256 bits */
691 prog_data->control_data_header_size_hwords =
692 ALIGN(c.control_data_header_size_bits, 256) / 256;
693
694 /* Compute the output vertex size.
695 *
696 * From the Ivy Bridge PRM, Vol2 Part1 7.2.1.1 STATE_GS - Output Vertex
697 * Size (p168):
698 *
699 * [0,62] indicating [1,63] 16B units
700 *
701 * Specifies the size of each vertex stored in the GS output entry
702 * (following any Control Header data) as a number of 128-bit units
703 * (minus one).
704 *
705 * Programming Restrictions: The vertex size must be programmed as a
706 * multiple of 32B units with the following exception: Rendering is
707 * disabled (as per SOL stage state) and the vertex size output by the
708 * GS thread is 16B.
709 *
710 * If rendering is enabled (as per SOL state) the vertex size must be
711 * programmed as a multiple of 32B units. In other words, the only time
712 * software can program a vertex size with an odd number of 16B units
713 * is when rendering is disabled.
714 *
715 * Note: B=bytes in the above text.
716 *
717 * It doesn't seem worth the extra trouble to optimize the case where the
718 * vertex size is 16B (especially since this would require special-casing
719 * the GEN assembly that writes to the URB). So we just set the vertex
720 * size to a multiple of 32B (2 vec4's) in all cases.
721 *
722 * The maximum output vertex size is 62*16 = 992 bytes (31 hwords). We
723 * budget that as follows:
724 *
725 * 512 bytes for varyings (a varying component is 4 bytes and
726 * gl_MaxGeometryOutputComponents = 128)
727 * 16 bytes overhead for VARYING_SLOT_PSIZ (each varying slot is 16
728 * bytes)
729 * 16 bytes overhead for gl_Position (we allocate it a slot in the VUE
730 * even if it's not used)
731 * 32 bytes overhead for gl_ClipDistance (we allocate it 2 VUE slots
732 * whenever clip planes are enabled, even if the shader doesn't
733 * write to gl_ClipDistance)
734 * 16 bytes overhead since the VUE size must be a multiple of 32 bytes
735 * (see below)--this causes up to 1 VUE slot to be wasted
736 * 400 bytes available for varying packing overhead
737 *
738 * Worst-case varying packing overhead is 3/4 of a varying slot (12 bytes)
739 * per interpolation type, so this is plenty.
740 *
741 */
742 unsigned output_vertex_size_bytes = prog_data->base.vue_map.num_slots * 16;
743 assert(compiler->devinfo->gen == 6 ||
744 output_vertex_size_bytes <= GEN7_MAX_GS_OUTPUT_VERTEX_SIZE_BYTES);
745 prog_data->output_vertex_size_hwords =
746 ALIGN(output_vertex_size_bytes, 32) / 32;
747
748 /* Compute URB entry size. The maximum allowed URB entry size is 32k.
749 * That divides up as follows:
750 *
751 * 64 bytes for the control data header (cut indices or StreamID bits)
752 * 4096 bytes for varyings (a varying component is 4 bytes and
753 * gl_MaxGeometryTotalOutputComponents = 1024)
754 * 4096 bytes overhead for VARYING_SLOT_PSIZ (each varying slot is 16
755 * bytes/vertex and gl_MaxGeometryOutputVertices is 256)
756 * 4096 bytes overhead for gl_Position (we allocate it a slot in the VUE
757 * even if it's not used)
758 * 8192 bytes overhead for gl_ClipDistance (we allocate it 2 VUE slots
759 * whenever clip planes are enabled, even if the shader doesn't
760 * write to gl_ClipDistance)
761 * 4096 bytes overhead since the VUE size must be a multiple of 32
762 * bytes (see above)--this causes up to 1 VUE slot to be wasted
763 * 8128 bytes available for varying packing overhead
764 *
765 * Worst-case varying packing overhead is 3/4 of a varying slot per
766 * interpolation type, which works out to 3072 bytes, so this would allow
767 * us to accommodate 2 interpolation types without any danger of running
768 * out of URB space.
769 *
770 * In practice, the risk of running out of URB space is very small, since
771 * the above figures are all worst-case, and most of them scale with the
772 * number of output vertices. So we'll just calculate the amount of space
773 * we need, and if it's too large, fail to compile.
774 *
775 * The above is for gen7+ where we have a single URB entry that will hold
776 * all the output. In gen6, we will have to allocate URB entries for every
777 * vertex we emit, so our URB entries only need to be large enough to hold
778 * a single vertex. Also, gen6 does not have a control data header.
779 */
780 unsigned output_size_bytes;
781 if (compiler->devinfo->gen >= 7) {
782 output_size_bytes =
783 prog_data->output_vertex_size_hwords * 32 * shader->info->gs.vertices_out;
784 output_size_bytes += 32 * prog_data->control_data_header_size_hwords;
785 } else {
786 output_size_bytes = prog_data->output_vertex_size_hwords * 32;
787 }
788
789 /* Broadwell stores "Vertex Count" as a full 8 DWord (32 byte) URB output,
790 * which comes before the control header.
791 */
792 if (compiler->devinfo->gen >= 8)
793 output_size_bytes += 32;
794
795 /* Shaders can technically set max_vertices = 0, at which point we
796 * may have a URB size of 0 bytes. Nothing good can come from that,
797 * so enforce a minimum size.
798 */
799 if (output_size_bytes == 0)
800 output_size_bytes = 1;
801
802 unsigned max_output_size_bytes = GEN7_MAX_GS_URB_ENTRY_SIZE_BYTES;
803 if (compiler->devinfo->gen == 6)
804 max_output_size_bytes = GEN6_MAX_GS_URB_ENTRY_SIZE_BYTES;
805 if (output_size_bytes > max_output_size_bytes)
806 return NULL;
807
808
809 /* URB entry sizes are stored as a multiple of 64 bytes in gen7+ and
810 * a multiple of 128 bytes in gen6.
811 */
812 if (compiler->devinfo->gen >= 7)
813 prog_data->base.urb_entry_size = ALIGN(output_size_bytes, 64) / 64;
814 else
815 prog_data->base.urb_entry_size = ALIGN(output_size_bytes, 128) / 128;
816
817 assert(shader->info->gs.output_primitive < ARRAY_SIZE(gl_prim_to_hw_prim));
818 prog_data->output_topology =
819 gl_prim_to_hw_prim[shader->info->gs.output_primitive];
820
821 prog_data->vertices_in = shader->info->gs.vertices_in;
822
823 /* GS inputs are read from the VUE 256 bits (2 vec4's) at a time, so we
824 * need to program a URB read length of ceiling(num_slots / 2).
825 */
826 prog_data->base.urb_read_length = (c.input_vue_map.num_slots + 1) / 2;
827
828 /* Now that prog_data setup is done, we are ready to actually compile the
829 * program.
830 */
831 if (unlikely(INTEL_DEBUG & DEBUG_GS)) {
832 fprintf(stderr, "GS Input ");
833 brw_print_vue_map(stderr, &c.input_vue_map);
834 fprintf(stderr, "GS Output ");
835 brw_print_vue_map(stderr, &prog_data->base.vue_map);
836 }
837
838 if (is_scalar) {
839 fs_visitor v(compiler, log_data, mem_ctx, &c, prog_data, shader,
840 shader_time_index);
841 if (v.run_gs()) {
842 prog_data->base.dispatch_mode = DISPATCH_MODE_SIMD8;
843 prog_data->base.base.dispatch_grf_start_reg = v.payload.num_regs;
844
845 fs_generator g(compiler, log_data, mem_ctx, &c.key,
846 &prog_data->base.base, v.promoted_constants,
847 false, MESA_SHADER_GEOMETRY);
848 if (unlikely(INTEL_DEBUG & DEBUG_GS)) {
849 const char *label =
850 shader->info->label ? shader->info->label : "unnamed";
851 char *name = ralloc_asprintf(mem_ctx, "%s geometry shader %s",
852 label, shader->info->name);
853 g.enable_debug(name);
854 }
855 g.generate_code(v.cfg, 8);
856 return g.get_assembly(final_assembly_size);
857 }
858 }
859
860 if (compiler->devinfo->gen >= 7) {
861 /* Compile the geometry shader in DUAL_OBJECT dispatch mode, if we can do
862 * so without spilling. If the GS invocations count > 1, then we can't use
863 * dual object mode.
864 */
865 if (prog_data->invocations <= 1 &&
866 likely(!(INTEL_DEBUG & DEBUG_NO_DUAL_OBJECT_GS))) {
867 prog_data->base.dispatch_mode = DISPATCH_MODE_4X2_DUAL_OBJECT;
868
869 vec4_gs_visitor v(compiler, log_data, &c, prog_data, shader,
870 mem_ctx, true /* no_spills */, shader_time_index);
871 if (v.run()) {
872 return brw_vec4_generate_assembly(compiler, log_data, mem_ctx,
873 shader, &prog_data->base, v.cfg,
874 final_assembly_size);
875 }
876 }
877 }
878
879 /* Either we failed to compile in DUAL_OBJECT mode (probably because it
880 * would have required spilling) or DUAL_OBJECT mode is disabled. So fall
881 * back to DUAL_INSTANCED or SINGLE mode, which consumes fewer registers.
882 *
883 * FIXME: Single dispatch mode requires that the driver can handle
884 * interleaving of input registers, but this is already supported (dual
885 * instance mode has the same requirement). However, to take full advantage
886 * of single dispatch mode to reduce register pressure we would also need to
887 * do interleaved outputs, but currently, the vec4 visitor and generator
888 * classes do not support this, so at the moment register pressure in
889 * single and dual instance modes is the same.
890 *
891 * From the Ivy Bridge PRM, Vol2 Part1 7.2.1.1 "3DSTATE_GS"
892 * "If InstanceCount>1, DUAL_OBJECT mode is invalid. Software will likely
893 * want to use DUAL_INSTANCE mode for higher performance, but SINGLE mode
894 * is also supported. When InstanceCount=1 (one instance per object) software
895 * can decide which dispatch mode to use. DUAL_OBJECT mode would likely be
896 * the best choice for performance, followed by SINGLE mode."
897 *
898 * So SINGLE mode is more performant when invocations == 1 and DUAL_INSTANCE
899 * mode is more performant when invocations > 1. Gen6 only supports
900 * SINGLE mode.
901 */
902 if (prog_data->invocations <= 1 || compiler->devinfo->gen < 7)
903 prog_data->base.dispatch_mode = DISPATCH_MODE_4X1_SINGLE;
904 else
905 prog_data->base.dispatch_mode = DISPATCH_MODE_4X2_DUAL_INSTANCE;
906
907 vec4_gs_visitor *gs = NULL;
908 const unsigned *ret = NULL;
909
910 if (compiler->devinfo->gen >= 7)
911 gs = new vec4_gs_visitor(compiler, log_data, &c, prog_data,
912 shader, mem_ctx, false /* no_spills */,
913 shader_time_index);
914 else
915 gs = new gen6_gs_visitor(compiler, log_data, &c, prog_data, prog,
916 shader, mem_ctx, false /* no_spills */,
917 shader_time_index);
918
919 if (!gs->run()) {
920 if (error_str)
921 *error_str = ralloc_strdup(mem_ctx, gs->fail_msg);
922 } else {
923 ret = brw_vec4_generate_assembly(compiler, log_data, mem_ctx, shader,
924 &prog_data->base, gs->cfg,
925 final_assembly_size);
926 }
927
928 delete gs;
929 return ret;
930 }
931
932
933 } /* namespace brw */