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