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