2 * Copyright © 2013 Intel Corporation
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:
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
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.
25 * \file brw_vec4_gs_visitor.cpp
27 * Geometry-shader-specific code derived from the vec4_visitor class.
30 #include "brw_vec4_gs_visitor.h"
31 #include "gen6_gs_visitor.h"
36 vec4_gs_visitor::vec4_gs_visitor(const struct brw_compiler
*compiler
,
38 struct brw_gs_compile
*c
,
39 struct brw_gs_prog_data
*prog_data
,
40 const nir_shader
*shader
,
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
),
48 gs_prog_data(prog_data
)
54 vec4_gs_visitor::make_reg_for_system_value(int location
,
55 const glsl_type
*type
)
57 dst_reg
*reg
= new(mem_ctx
) dst_reg(this, type
);
60 case SYSTEM_VALUE_INVOCATION_ID
:
61 this->current_annotation
= "initialize gl_InvocationID";
62 emit(GS_OPCODE_GET_INSTANCE_ID
, *reg
);
65 unreachable("not reached");
73 vec4_gs_visitor::setup_varying_inputs(int payload_reg
, int *attribute_map
,
74 int attributes_per_reg
)
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.
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.
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;
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
+
97 int regs_used
= ALIGN(input_array_stride
* num_input_vertices
,
98 attributes_per_reg
) / attributes_per_reg
;
99 return payload_reg
+ regs_used
;
104 vec4_gs_visitor::setup_payload()
106 int attribute_map
[BRW_VARYING_SLOT_COUNT
* MAX_GS_INPUT_VERTICES
];
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.
111 int attributes_per_reg
=
112 prog_data
->dispatch_mode
== DISPATCH_MODE_4X2_DUAL_OBJECT
? 1 : 2;
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.
119 memset(attribute_map
, 0, sizeof(attribute_map
));
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
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
++;
133 reg
= setup_uniforms(reg
);
135 reg
= setup_varying_inputs(reg
, attribute_map
, attributes_per_reg
);
137 lower_attributes_to_hw_regs(attribute_map
, attributes_per_reg
> 1);
139 this->first_non_payload_grf
= reg
;
144 vec4_gs_visitor::emit_prolog()
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
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
, brw_imm_ud(0u));
157 inst
->force_writemask_all
= true;
159 /* Create a virtual register to hold the vertex count */
160 this->vertex_count
= src_reg(this, glsl_type::uint_type
);
162 /* Initialize the vertex_count register to 0 */
163 this->current_annotation
= "initialize vertex_count";
164 inst
= emit(MOV(dst_reg(this->vertex_count
), brw_imm_ud(0u)));
165 inst
->force_writemask_all
= true;
167 if (c
->control_data_header_size_bits
> 0) {
168 /* Create a virtual register to hold the current set of control data
171 this->control_data_bits
= src_reg(this, glsl_type::uint_type
);
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.
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
), brw_imm_ud(0u)));
180 inst
->force_writemask_all
= true;
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.
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
++) {
192 BRW_VARYING_SLOT_COUNT
* vertex
+ VARYING_SLOT_PSIZ
);
193 dst
.type
= BRW_REGISTER_TYPE_F
;
195 dst
.writemask
= WRITEMASK_X
;
196 src
.swizzle
= BRW_SWIZZLE_WWWW
;
197 inst
= emit(MOV(dst
, src
));
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
203 inst
->force_writemask_all
= true;
207 this->current_annotation
= NULL
;
211 vec4_gs_visitor::emit_thread_end()
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
219 current_annotation
= "thread end: emit control data bits";
220 emit_control_data_bits();
223 /* MRF 0 is reserved for the debugger, so start with message header
228 bool static_vertex_count
= gs_prog_data
->static_vertex_count
!= -1;
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.
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).
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
;
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;
261 vec4_gs_visitor::emit_urb_write_header(int mrf
)
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.
268 * So we have to prepare a message header with the appropriate offset
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 brw_imm_ud(gs_prog_data
->output_vertex_size_hwords
));
282 vec4_gs_visitor::emit_urb_write_opcode(bool complete
)
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.
290 vec4_instruction
*inst
= emit(GS_OPCODE_URB_WRITE
);
291 inst
->offset
= gs_prog_data
->control_data_header_size_hwords
;
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.
296 if (devinfo
->gen
>= 8 && gs_prog_data
->static_vertex_count
== -1)
299 inst
->urb_write_flags
= BRW_URB_WRITE_PER_SLOT_OFFSET
;
305 * Write out a batch of 32 control data bits from the control_data_bits
306 * register to the URB.
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.
313 * This function takes care of ensuring that if no vertices have been output
314 * yet, no control bits are emitted.
317 vec4_gs_visitor::emit_control_data_bits()
319 assert(c
->control_data_bits_per_vertex
!= 0);
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
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.
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
;
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
347 * dword_index = (vertex_count - 1) * bits_per_vertex / 32
349 * Since bits_per_vertex is a power of two, and is known at compile
350 * time, this can be optimized to:
352 * dword_index = (vertex_count - 1) >> (6 - log2(bits_per_vertex))
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
,
358 brw_imm_ud(0xffffffffu
)));
359 unsigned log2_bits_per_vertex
=
360 _mesa_fls(c
->control_data_bits_per_vertex
);
361 emit(SHR(dst_reg(dword_index
), prev_count
,
362 brw_imm_ud(6 - log2_bits_per_vertex
)));
365 /* Start building the URB write message. The first MRF gets a copy of
369 dst_reg
mrf_reg(MRF
, base_mrf
);
370 src_reg
r0(retype(brw_vec8_grf(0, 0), BRW_REGISTER_TYPE_UD
));
371 vec4_instruction
*inst
= emit(MOV(mrf_reg
, r0
));
372 inst
->force_writemask_all
= true;
374 if (urb_write_flags
& BRW_URB_WRITE_PER_SLOT_OFFSET
) {
375 /* Set the per-slot offset to dword_index / 4, to that we'll write to
376 * the appropriate OWORD within the control data header.
378 src_reg
per_slot_offset(this, glsl_type::uint_type
);
379 emit(SHR(dst_reg(per_slot_offset
), dword_index
, brw_imm_ud(2u)));
380 emit(GS_OPCODE_SET_WRITE_OFFSET
, mrf_reg
, per_slot_offset
,
384 if (urb_write_flags
& BRW_URB_WRITE_USE_CHANNEL_MASKS
) {
385 /* Set the channel masks to 1 << (dword_index % 4), so that we'll
386 * write to the appropriate DWORD within the OWORD. We need to do
387 * this computation with force_writemask_all, otherwise garbage data
388 * from invocation 0 might clobber the mask for invocation 1 when
389 * GS_OPCODE_PREPARE_CHANNEL_MASKS tries to OR the two masks
392 src_reg
channel(this, glsl_type::uint_type
);
393 inst
= emit(AND(dst_reg(channel
), dword_index
, brw_imm_ud(3u)));
394 inst
->force_writemask_all
= true;
395 src_reg
one(this, glsl_type::uint_type
);
396 inst
= emit(MOV(dst_reg(one
), brw_imm_ud(1u)));
397 inst
->force_writemask_all
= true;
398 src_reg
channel_mask(this, glsl_type::uint_type
);
399 inst
= emit(SHL(dst_reg(channel_mask
), one
, channel
));
400 inst
->force_writemask_all
= true;
401 emit(GS_OPCODE_PREPARE_CHANNEL_MASKS
, dst_reg(channel_mask
),
403 emit(GS_OPCODE_SET_CHANNEL_MASKS
, mrf_reg
, channel_mask
);
406 /* Store the control data bits in the message payload and send it. */
407 dst_reg
mrf_reg2(MRF
, base_mrf
+ 1);
408 inst
= emit(MOV(mrf_reg2
, this->control_data_bits
));
409 inst
->force_writemask_all
= true;
410 inst
= emit(GS_OPCODE_URB_WRITE
);
411 inst
->urb_write_flags
= urb_write_flags
;
412 /* We need to increment Global Offset by 256-bits to make room for
413 * Broadwell's extra "Vertex Count" payload at the beginning of the
414 * URB entry. Since this is an OWord message, Global Offset is counted
415 * in 128-bit units, so we must set it to 2.
417 if (devinfo
->gen
>= 8 && gs_prog_data
->static_vertex_count
== -1)
419 inst
->base_mrf
= base_mrf
;
424 vec4_gs_visitor::set_stream_control_data_bits(unsigned stream_id
)
426 /* control_data_bits |= stream_id << ((2 * (vertex_count - 1)) % 32) */
428 /* Note: we are calling this *before* increasing vertex_count, so
429 * this->vertex_count == vertex_count - 1 in the formula above.
432 /* Stream mode uses 2 bits per vertex */
433 assert(c
->control_data_bits_per_vertex
== 2);
435 /* Must be a valid stream */
436 assert(stream_id
>= 0 && stream_id
< MAX_VERTEX_STREAMS
);
438 /* Control data bits are initialized to 0 so we don't have to set any
439 * bits when sending vertices to stream 0.
444 /* reg::sid = stream_id */
445 src_reg
sid(this, glsl_type::uint_type
);
446 emit(MOV(dst_reg(sid
), brw_imm_ud(stream_id
)));
448 /* reg:shift_count = 2 * (vertex_count - 1) */
449 src_reg
shift_count(this, glsl_type::uint_type
);
450 emit(SHL(dst_reg(shift_count
), this->vertex_count
, brw_imm_ud(1u)));
452 /* Note: we're relying on the fact that the GEN SHL instruction only pays
453 * attention to the lower 5 bits of its second source argument, so on this
454 * architecture, stream_id << 2 * (vertex_count - 1) is equivalent to
455 * stream_id << ((2 * (vertex_count - 1)) % 32).
457 src_reg
mask(this, glsl_type::uint_type
);
458 emit(SHL(dst_reg(mask
), sid
, shift_count
));
459 emit(OR(dst_reg(this->control_data_bits
), this->control_data_bits
, mask
));
463 vec4_gs_visitor::gs_emit_vertex(int stream_id
)
465 this->current_annotation
= "emit vertex: safety check";
467 /* Haswell and later hardware ignores the "Render Stream Select" bits
468 * from the 3DSTATE_STREAMOUT packet when the SOL stage is disabled,
469 * and instead sends all primitives down the pipeline for rasterization.
470 * If the SOL stage is enabled, "Render Stream Select" is honored and
471 * primitives bound to non-zero streams are discarded after stream output.
473 * Since the only purpose of primives sent to non-zero streams is to
474 * be recorded by transform feedback, we can simply discard all geometry
475 * bound to these streams when transform feedback is disabled.
477 if (stream_id
> 0 && !nir
->info
.has_transform_feedback_varyings
)
480 /* If we're outputting 32 control data bits or less, then we can wait
481 * until the shader is over to output them all. Otherwise we need to
482 * output them as we go. Now is the time to do it, since we're about to
483 * output the vertex_count'th vertex, so it's guaranteed that the
484 * control data bits associated with the (vertex_count - 1)th vertex are
487 if (c
->control_data_header_size_bits
> 32) {
488 this->current_annotation
= "emit vertex: emit control data bits";
489 /* Only emit control data bits if we've finished accumulating a batch
490 * of 32 bits. This is the case when:
492 * (vertex_count * bits_per_vertex) % 32 == 0
494 * (in other words, when the last 5 bits of vertex_count *
495 * bits_per_vertex are 0). Assuming bits_per_vertex == 2^n for some
496 * integer n (which is always the case, since bits_per_vertex is
497 * always 1 or 2), this is equivalent to requiring that the last 5-n
498 * bits of vertex_count are 0:
500 * vertex_count & (2^(5-n) - 1) == 0
502 * 2^(5-n) == 2^5 / 2^n == 32 / bits_per_vertex, so this is
505 * vertex_count & (32 / bits_per_vertex - 1) == 0
507 vec4_instruction
*inst
=
508 emit(AND(dst_null_ud(), this->vertex_count
,
509 brw_imm_ud(32 / c
->control_data_bits_per_vertex
- 1)));
510 inst
->conditional_mod
= BRW_CONDITIONAL_Z
;
512 emit(IF(BRW_PREDICATE_NORMAL
));
514 /* If vertex_count is 0, then no control data bits have been
515 * accumulated yet, so we skip emitting them.
517 emit(CMP(dst_null_ud(), this->vertex_count
, brw_imm_ud(0u),
518 BRW_CONDITIONAL_NEQ
));
519 emit(IF(BRW_PREDICATE_NORMAL
));
520 emit_control_data_bits();
521 emit(BRW_OPCODE_ENDIF
);
523 /* Reset control_data_bits to 0 so we can start accumulating a new
526 * Note: in the case where vertex_count == 0, this neutralizes the
527 * effect of any call to EndPrimitive() that the shader may have
528 * made before outputting its first vertex.
530 inst
= emit(MOV(dst_reg(this->control_data_bits
), brw_imm_ud(0u)));
531 inst
->force_writemask_all
= true;
533 emit(BRW_OPCODE_ENDIF
);
536 this->current_annotation
= "emit vertex: vertex data";
539 /* In stream mode we have to set control data bits for all vertices
540 * unless we have disabled control data bits completely (which we do
541 * do for GL_POINTS outputs that don't use streams).
543 if (c
->control_data_header_size_bits
> 0 &&
544 gs_prog_data
->control_data_format
==
545 GEN7_GS_CONTROL_DATA_FORMAT_GSCTL_SID
) {
546 this->current_annotation
= "emit vertex: Stream control data bits";
547 set_stream_control_data_bits(stream_id
);
550 this->current_annotation
= NULL
;
554 vec4_gs_visitor::gs_end_primitive()
556 /* We can only do EndPrimitive() functionality when the control data
557 * consists of cut bits. Fortunately, the only time it isn't is when the
558 * output type is points, in which case EndPrimitive() is a no-op.
560 if (gs_prog_data
->control_data_format
!=
561 GEN7_GS_CONTROL_DATA_FORMAT_GSCTL_CUT
) {
565 /* Cut bits use one bit per vertex. */
566 assert(c
->control_data_bits_per_vertex
== 1);
568 /* Cut bit n should be set to 1 if EndPrimitive() was called after emitting
569 * vertex n, 0 otherwise. So all we need to do here is mark bit
570 * (vertex_count - 1) % 32 in the cut_bits register to indicate that
571 * EndPrimitive() was called after emitting vertex (vertex_count - 1);
572 * vec4_gs_visitor::emit_control_data_bits() will take care of the rest.
574 * Note that if EndPrimitve() is called before emitting any vertices, this
575 * will cause us to set bit 31 of the control_data_bits register to 1.
576 * That's fine because:
578 * - If max_vertices < 32, then vertex number 31 (zero-based) will never be
579 * output, so the hardware will ignore cut bit 31.
581 * - If max_vertices == 32, then vertex number 31 is guaranteed to be the
582 * last vertex, so setting cut bit 31 has no effect (since the primitive
583 * is automatically ended when the GS terminates).
585 * - If max_vertices > 32, then the ir_emit_vertex visitor will reset the
586 * control_data_bits register to 0 when the first vertex is emitted.
589 /* control_data_bits |= 1 << ((vertex_count - 1) % 32) */
590 src_reg
one(this, glsl_type::uint_type
);
591 emit(MOV(dst_reg(one
), brw_imm_ud(1u)));
592 src_reg
prev_count(this, glsl_type::uint_type
);
593 emit(ADD(dst_reg(prev_count
), this->vertex_count
, brw_imm_ud(0xffffffffu
)));
594 src_reg
mask(this, glsl_type::uint_type
);
595 /* Note: we're relying on the fact that the GEN SHL instruction only pays
596 * attention to the lower 5 bits of its second source argument, so on this
597 * architecture, 1 << (vertex_count - 1) is equivalent to 1 <<
598 * ((vertex_count - 1) % 32).
600 emit(SHL(dst_reg(mask
), one
, prev_count
));
601 emit(OR(dst_reg(this->control_data_bits
), this->control_data_bits
, mask
));
604 extern "C" const unsigned *
605 brw_compile_gs(const struct brw_compiler
*compiler
, void *log_data
,
607 const struct brw_gs_prog_key
*key
,
608 struct brw_gs_prog_data
*prog_data
,
609 const nir_shader
*shader
,
610 struct gl_shader_program
*shader_prog
,
611 int shader_time_index
,
612 unsigned *final_assembly_size
,
615 struct brw_gs_compile c
;
616 memset(&c
, 0, sizeof(c
));
619 prog_data
->include_primitive_id
=
620 (shader
->info
.inputs_read
& VARYING_BIT_PRIMITIVE_ID
) != 0;
622 prog_data
->invocations
= shader
->info
.gs
.invocations
;
624 if (compiler
->devinfo
->gen
>= 8)
625 prog_data
->static_vertex_count
= nir_gs_count_vertices(shader
);
627 if (compiler
->devinfo
->gen
>= 7) {
628 if (shader
->info
.gs
.output_primitive
== GL_POINTS
) {
629 /* When the output type is points, the geometry shader may output data
630 * to multiple streams, and EndPrimitive() has no effect. So we
631 * configure the hardware to interpret the control data as stream ID.
633 prog_data
->control_data_format
= GEN7_GS_CONTROL_DATA_FORMAT_GSCTL_SID
;
635 /* We only have to emit control bits if we are using streams */
636 if (shader_prog
&& shader_prog
->Geom
.UsesStreams
)
637 c
.control_data_bits_per_vertex
= 2;
639 c
.control_data_bits_per_vertex
= 0;
641 /* When the output type is triangle_strip or line_strip, EndPrimitive()
642 * may be used to terminate the current strip and start a new one
643 * (similar to primitive restart), and outputting data to multiple
644 * streams is not supported. So we configure the hardware to interpret
645 * the control data as EndPrimitive information (a.k.a. "cut bits").
647 prog_data
->control_data_format
= GEN7_GS_CONTROL_DATA_FORMAT_GSCTL_CUT
;
649 /* We only need to output control data if the shader actually calls
652 c
.control_data_bits_per_vertex
=
653 shader
->info
.gs
.uses_end_primitive
? 1 : 0;
656 /* There are no control data bits in gen6. */
657 c
.control_data_bits_per_vertex
= 0;
659 /* If it is using transform feedback, enable it */
660 if (shader
->info
.has_transform_feedback_varyings
)
661 prog_data
->gen6_xfb_enabled
= true;
663 prog_data
->gen6_xfb_enabled
= false;
665 c
.control_data_header_size_bits
=
666 shader
->info
.gs
.vertices_out
* c
.control_data_bits_per_vertex
;
668 /* 1 HWORD = 32 bytes = 256 bits */
669 prog_data
->control_data_header_size_hwords
=
670 ALIGN(c
.control_data_header_size_bits
, 256) / 256;
672 /* Compute the output vertex size.
674 * From the Ivy Bridge PRM, Vol2 Part1 7.2.1.1 STATE_GS - Output Vertex
677 * [0,62] indicating [1,63] 16B units
679 * Specifies the size of each vertex stored in the GS output entry
680 * (following any Control Header data) as a number of 128-bit units
683 * Programming Restrictions: The vertex size must be programmed as a
684 * multiple of 32B units with the following exception: Rendering is
685 * disabled (as per SOL stage state) and the vertex size output by the
688 * If rendering is enabled (as per SOL state) the vertex size must be
689 * programmed as a multiple of 32B units. In other words, the only time
690 * software can program a vertex size with an odd number of 16B units
691 * is when rendering is disabled.
693 * Note: B=bytes in the above text.
695 * It doesn't seem worth the extra trouble to optimize the case where the
696 * vertex size is 16B (especially since this would require special-casing
697 * the GEN assembly that writes to the URB). So we just set the vertex
698 * size to a multiple of 32B (2 vec4's) in all cases.
700 * The maximum output vertex size is 62*16 = 992 bytes (31 hwords). We
701 * budget that as follows:
703 * 512 bytes for varyings (a varying component is 4 bytes and
704 * gl_MaxGeometryOutputComponents = 128)
705 * 16 bytes overhead for VARYING_SLOT_PSIZ (each varying slot is 16
707 * 16 bytes overhead for gl_Position (we allocate it a slot in the VUE
708 * even if it's not used)
709 * 32 bytes overhead for gl_ClipDistance (we allocate it 2 VUE slots
710 * whenever clip planes are enabled, even if the shader doesn't
711 * write to gl_ClipDistance)
712 * 16 bytes overhead since the VUE size must be a multiple of 32 bytes
713 * (see below)--this causes up to 1 VUE slot to be wasted
714 * 400 bytes available for varying packing overhead
716 * Worst-case varying packing overhead is 3/4 of a varying slot (12 bytes)
717 * per interpolation type, so this is plenty.
720 unsigned output_vertex_size_bytes
= prog_data
->base
.vue_map
.num_slots
* 16;
721 assert(compiler
->devinfo
->gen
== 6 ||
722 output_vertex_size_bytes
<= GEN7_MAX_GS_OUTPUT_VERTEX_SIZE_BYTES
);
723 prog_data
->output_vertex_size_hwords
=
724 ALIGN(output_vertex_size_bytes
, 32) / 32;
726 /* Compute URB entry size. The maximum allowed URB entry size is 32k.
727 * That divides up as follows:
729 * 64 bytes for the control data header (cut indices or StreamID bits)
730 * 4096 bytes for varyings (a varying component is 4 bytes and
731 * gl_MaxGeometryTotalOutputComponents = 1024)
732 * 4096 bytes overhead for VARYING_SLOT_PSIZ (each varying slot is 16
733 * bytes/vertex and gl_MaxGeometryOutputVertices is 256)
734 * 4096 bytes overhead for gl_Position (we allocate it a slot in the VUE
735 * even if it's not used)
736 * 8192 bytes overhead for gl_ClipDistance (we allocate it 2 VUE slots
737 * whenever clip planes are enabled, even if the shader doesn't
738 * write to gl_ClipDistance)
739 * 4096 bytes overhead since the VUE size must be a multiple of 32
740 * bytes (see above)--this causes up to 1 VUE slot to be wasted
741 * 8128 bytes available for varying packing overhead
743 * Worst-case varying packing overhead is 3/4 of a varying slot per
744 * interpolation type, which works out to 3072 bytes, so this would allow
745 * us to accommodate 2 interpolation types without any danger of running
748 * In practice, the risk of running out of URB space is very small, since
749 * the above figures are all worst-case, and most of them scale with the
750 * number of output vertices. So we'll just calculate the amount of space
751 * we need, and if it's too large, fail to compile.
753 * The above is for gen7+ where we have a single URB entry that will hold
754 * all the output. In gen6, we will have to allocate URB entries for every
755 * vertex we emit, so our URB entries only need to be large enough to hold
756 * a single vertex. Also, gen6 does not have a control data header.
758 unsigned output_size_bytes
;
759 if (compiler
->devinfo
->gen
>= 7) {
761 prog_data
->output_vertex_size_hwords
* 32 * shader
->info
.gs
.vertices_out
;
762 output_size_bytes
+= 32 * prog_data
->control_data_header_size_hwords
;
764 output_size_bytes
= prog_data
->output_vertex_size_hwords
* 32;
767 /* Broadwell stores "Vertex Count" as a full 8 DWord (32 byte) URB output,
768 * which comes before the control header.
770 if (compiler
->devinfo
->gen
>= 8)
771 output_size_bytes
+= 32;
773 assert(output_size_bytes
>= 1);
774 unsigned max_output_size_bytes
= GEN7_MAX_GS_URB_ENTRY_SIZE_BYTES
;
775 if (compiler
->devinfo
->gen
== 6)
776 max_output_size_bytes
= GEN6_MAX_GS_URB_ENTRY_SIZE_BYTES
;
777 if (output_size_bytes
> max_output_size_bytes
)
781 /* URB entry sizes are stored as a multiple of 64 bytes in gen7+ and
782 * a multiple of 128 bytes in gen6.
784 if (compiler
->devinfo
->gen
>= 7)
785 prog_data
->base
.urb_entry_size
= ALIGN(output_size_bytes
, 64) / 64;
787 prog_data
->base
.urb_entry_size
= ALIGN(output_size_bytes
, 128) / 128;
789 prog_data
->output_topology
=
790 get_hw_prim_for_gl_prim(shader
->info
.gs
.output_primitive
);
792 /* The GLSL linker will have already matched up GS inputs and the outputs
793 * of prior stages. The driver does extend VS outputs in some cases, but
794 * only for legacy OpenGL or Gen4-5 hardware, neither of which offer
795 * geometry shader support. So we can safely ignore that.
797 * For SSO pipelines, we use a fixed VUE map layout based on variable
798 * locations, so we can rely on rendezvous-by-location making this work.
800 * However, we need to ignore VARYING_SLOT_PRIMITIVE_ID, as it's not
801 * written by previous stages and shows up via payload magic.
803 GLbitfield64 inputs_read
=
804 shader
->info
.inputs_read
& ~VARYING_BIT_PRIMITIVE_ID
;
805 brw_compute_vue_map(compiler
->devinfo
,
806 &c
.input_vue_map
, inputs_read
,
807 shader
->info
.separate_shader
);
809 /* GS inputs are read from the VUE 256 bits (2 vec4's) at a time, so we
810 * need to program a URB read length of ceiling(num_slots / 2).
812 prog_data
->base
.urb_read_length
= (c
.input_vue_map
.num_slots
+ 1) / 2;
814 /* Now that prog_data setup is done, we are ready to actually compile the
817 if (unlikely(INTEL_DEBUG
& DEBUG_GS
)) {
818 fprintf(stderr
, "GS Input ");
819 brw_print_vue_map(stderr
, &c
.input_vue_map
);
820 fprintf(stderr
, "GS Output ");
821 brw_print_vue_map(stderr
, &prog_data
->base
.vue_map
);
824 if (compiler
->scalar_stage
[MESA_SHADER_GEOMETRY
]) {
825 /* TODO: Support instanced GS. We have basically no tests... */
826 assert(prog_data
->invocations
== 1);
828 fs_visitor
v(compiler
, log_data
, mem_ctx
, &c
, prog_data
, shader
,
831 prog_data
->base
.dispatch_mode
= DISPATCH_MODE_SIMD8
;
833 fs_generator
g(compiler
, log_data
, mem_ctx
, &c
.key
,
834 &prog_data
->base
.base
, v
.promoted_constants
,
836 if (unlikely(INTEL_DEBUG
& DEBUG_GS
)) {
838 shader
->info
.label
? shader
->info
.label
: "unnamed";
839 char *name
= ralloc_asprintf(mem_ctx
, "%s geometry shader %s",
840 label
, shader
->info
.name
);
841 g
.enable_debug(name
);
843 g
.generate_code(v
.cfg
, 8);
844 return g
.get_assembly(final_assembly_size
);
848 if (compiler
->devinfo
->gen
>= 7) {
849 /* Compile the geometry shader in DUAL_OBJECT dispatch mode, if we can do
850 * so without spilling. If the GS invocations count > 1, then we can't use
853 if (prog_data
->invocations
<= 1 &&
854 likely(!(INTEL_DEBUG
& DEBUG_NO_DUAL_OBJECT_GS
))) {
855 prog_data
->base
.dispatch_mode
= DISPATCH_MODE_4X2_DUAL_OBJECT
;
857 vec4_gs_visitor
v(compiler
, log_data
, &c
, prog_data
, shader
,
858 mem_ctx
, true /* no_spills */, shader_time_index
);
860 return brw_vec4_generate_assembly(compiler
, log_data
, mem_ctx
,
861 shader
, &prog_data
->base
, v
.cfg
,
862 final_assembly_size
);
867 /* Either we failed to compile in DUAL_OBJECT mode (probably because it
868 * would have required spilling) or DUAL_OBJECT mode is disabled. So fall
869 * back to DUAL_INSTANCED or SINGLE mode, which consumes fewer registers.
871 * FIXME: Single dispatch mode requires that the driver can handle
872 * interleaving of input registers, but this is already supported (dual
873 * instance mode has the same requirement). However, to take full advantage
874 * of single dispatch mode to reduce register pressure we would also need to
875 * do interleaved outputs, but currently, the vec4 visitor and generator
876 * classes do not support this, so at the moment register pressure in
877 * single and dual instance modes is the same.
879 * From the Ivy Bridge PRM, Vol2 Part1 7.2.1.1 "3DSTATE_GS"
880 * "If InstanceCount>1, DUAL_OBJECT mode is invalid. Software will likely
881 * want to use DUAL_INSTANCE mode for higher performance, but SINGLE mode
882 * is also supported. When InstanceCount=1 (one instance per object) software
883 * can decide which dispatch mode to use. DUAL_OBJECT mode would likely be
884 * the best choice for performance, followed by SINGLE mode."
886 * So SINGLE mode is more performant when invocations == 1 and DUAL_INSTANCE
887 * mode is more performant when invocations > 1. Gen6 only supports
890 if (prog_data
->invocations
<= 1 || compiler
->devinfo
->gen
< 7)
891 prog_data
->base
.dispatch_mode
= DISPATCH_MODE_4X1_SINGLE
;
893 prog_data
->base
.dispatch_mode
= DISPATCH_MODE_4X2_DUAL_INSTANCE
;
895 vec4_gs_visitor
*gs
= NULL
;
896 const unsigned *ret
= NULL
;
898 if (compiler
->devinfo
->gen
>= 7)
899 gs
= new vec4_gs_visitor(compiler
, log_data
, &c
, prog_data
,
900 shader
, mem_ctx
, false /* no_spills */,
903 gs
= new gen6_gs_visitor(compiler
, log_data
, &c
, prog_data
, shader_prog
,
904 shader
, mem_ctx
, false /* no_spills */,
909 *error_str
= ralloc_strdup(mem_ctx
, gs
->fail_msg
);
911 ret
= brw_vec4_generate_assembly(compiler
, log_data
, mem_ctx
, shader
,
912 &prog_data
->base
, gs
->cfg
,
913 final_assembly_size
);
921 } /* namespace brw */