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"
33 const unsigned MAX_GS_INPUT_VERTICES
= 6;
37 vec4_gs_visitor::vec4_gs_visitor(const struct brw_compiler
*compiler
,
39 struct brw_gs_compile
*c
,
40 struct gl_shader_program
*prog
,
43 int shader_time_index
)
44 : vec4_visitor(compiler
, log_data
,
45 &c
->gp
->program
.Base
, &c
->key
.tex
,
46 &c
->prog_data
.base
, prog
, MESA_SHADER_GEOMETRY
, mem_ctx
,
47 no_spills
, shader_time_index
),
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
= c
->gp
->program
.VerticesIn
;
85 assert(num_input_vertices
<= MAX_GS_INPUT_VERTICES
);
86 unsigned input_array_stride
= c
->prog_data
.base
.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 c
->prog_data
.base
.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 (c
->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
, 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
), 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
), 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 (c
->gp
->program
.Base
.InputsRead
& VARYING_BIT_PSIZ
) {
189 this->current_annotation
= "swizzle gl_PointSize input";
190 for (int vertex
= 0; vertex
< c
->gp
->program
.VerticesIn
; 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
;
212 vec4_gs_visitor::emit_program_code()
214 /* We don't support NV_geometry_program4. */
215 unreachable("Unreached");
220 vec4_gs_visitor::emit_thread_end()
222 if (c
->control_data_header_size_bits
> 0) {
223 /* During shader execution, we only ever call emit_control_data_bits()
224 * just prior to outputting a vertex. Therefore, the control data bits
225 * corresponding to the most recently output vertex still need to be
228 current_annotation
= "thread end: emit control data bits";
229 emit_control_data_bits();
232 /* MRF 0 is reserved for the debugger, so start with message header
237 bool static_vertex_count
= c
->prog_data
.static_vertex_count
!= -1;
239 current_annotation
= "thread end";
240 dst_reg
mrf_reg(MRF
, base_mrf
);
241 src_reg
r0(retype(brw_vec8_grf(0, 0), BRW_REGISTER_TYPE_UD
));
242 vec4_instruction
*inst
= emit(MOV(mrf_reg
, r0
));
243 inst
->force_writemask_all
= true;
244 if (devinfo
->gen
< 8 || !static_vertex_count
)
245 emit(GS_OPCODE_SET_VERTEX_COUNT
, mrf_reg
, this->vertex_count
);
246 if (INTEL_DEBUG
& DEBUG_SHADER_TIME
)
247 emit_shader_time_end();
248 inst
= emit(GS_OPCODE_THREAD_END
);
249 inst
->base_mrf
= base_mrf
;
250 inst
->mlen
= devinfo
->gen
>= 8 && !static_vertex_count
? 2 : 1;
255 vec4_gs_visitor::emit_urb_write_header(int mrf
)
257 /* The SEND instruction that writes the vertex data to the VUE will use
258 * per_slot_offset=true, which means that DWORDs 3 and 4 of the message
259 * header specify an offset (in multiples of 256 bits) into the URB entry
260 * at which the write should take place.
262 * So we have to prepare a message header with the appropriate offset
265 dst_reg
mrf_reg(MRF
, mrf
);
266 src_reg
r0(retype(brw_vec8_grf(0, 0), BRW_REGISTER_TYPE_UD
));
267 this->current_annotation
= "URB write header";
268 vec4_instruction
*inst
= emit(MOV(mrf_reg
, r0
));
269 inst
->force_writemask_all
= true;
270 emit(GS_OPCODE_SET_WRITE_OFFSET
, mrf_reg
, this->vertex_count
,
271 (uint32_t) c
->prog_data
.output_vertex_size_hwords
);
276 vec4_gs_visitor::emit_urb_write_opcode(bool complete
)
278 /* We don't care whether the vertex is complete, because in general
279 * geometry shaders output multiple vertices, and we don't terminate the
280 * thread until all vertices are complete.
284 vec4_instruction
*inst
= emit(GS_OPCODE_URB_WRITE
);
285 inst
->offset
= c
->prog_data
.control_data_header_size_hwords
;
287 /* We need to increment Global Offset by 1 to make room for Broadwell's
288 * extra "Vertex Count" payload at the beginning of the URB entry.
290 if (devinfo
->gen
>= 8 && c
->prog_data
.static_vertex_count
== -1)
293 inst
->urb_write_flags
= BRW_URB_WRITE_PER_SLOT_OFFSET
;
299 vec4_gs_visitor::compute_array_stride(ir_dereference_array
*ir
)
301 /* Geometry shader inputs are arrays, but they use an unusual array layout:
302 * instead of all array elements for a given geometry shader input being
303 * stored consecutively, all geometry shader inputs are interleaved into
304 * one giant array. At this stage of compilation, we assume that the
305 * stride of the array is BRW_VARYING_SLOT_COUNT. Later,
306 * setup_attributes() will remap our accesses to the actual input array.
308 ir_dereference_variable
*deref_var
= ir
->array
->as_dereference_variable();
309 if (deref_var
&& deref_var
->var
->data
.mode
== ir_var_shader_in
)
310 return BRW_VARYING_SLOT_COUNT
;
312 return vec4_visitor::compute_array_stride(ir
);
317 * Write out a batch of 32 control data bits from the control_data_bits
318 * register to the URB.
320 * The current value of the vertex_count register determines which DWORD in
321 * the URB receives the control data bits. The control_data_bits register is
322 * assumed to contain the correct data for the vertex that was most recently
323 * output, and all previous vertices that share the same DWORD.
325 * This function takes care of ensuring that if no vertices have been output
326 * yet, no control bits are emitted.
329 vec4_gs_visitor::emit_control_data_bits()
331 assert(c
->control_data_bits_per_vertex
!= 0);
333 /* Since the URB_WRITE_OWORD message operates with 128-bit (vec4 sized)
334 * granularity, we need to use two tricks to ensure that the batch of 32
335 * control data bits is written to the appropriate DWORD in the URB. To
336 * select which vec4 we are writing to, we use the "slot {0,1} offset"
337 * fields of the message header. To select which DWORD in the vec4 we are
338 * writing to, we use the channel mask fields of the message header. To
339 * avoid penalizing geometry shaders that emit a small number of vertices
340 * with extra bookkeeping, we only do each of these tricks when
341 * c->prog_data.control_data_header_size_bits is large enough to make it
344 * Note: this means that if we're outputting just a single DWORD of control
345 * data bits, we'll actually replicate it four times since we won't do any
346 * channel masking. But that's not a problem since in this case the
347 * hardware only pays attention to the first DWORD.
349 enum brw_urb_write_flags urb_write_flags
= BRW_URB_WRITE_OWORD
;
350 if (c
->control_data_header_size_bits
> 32)
351 urb_write_flags
= urb_write_flags
| BRW_URB_WRITE_USE_CHANNEL_MASKS
;
352 if (c
->control_data_header_size_bits
> 128)
353 urb_write_flags
= urb_write_flags
| BRW_URB_WRITE_PER_SLOT_OFFSET
;
355 /* If we are using either channel masks or a per-slot offset, then we
356 * need to figure out which DWORD we are trying to write to, using the
359 * dword_index = (vertex_count - 1) * bits_per_vertex / 32
361 * Since bits_per_vertex is a power of two, and is known at compile
362 * time, this can be optimized to:
364 * dword_index = (vertex_count - 1) >> (6 - log2(bits_per_vertex))
366 src_reg
dword_index(this, glsl_type::uint_type
);
367 if (urb_write_flags
) {
368 src_reg
prev_count(this, glsl_type::uint_type
);
369 emit(ADD(dst_reg(prev_count
), this->vertex_count
, 0xffffffffu
));
370 unsigned log2_bits_per_vertex
=
371 _mesa_fls(c
->control_data_bits_per_vertex
);
372 emit(SHR(dst_reg(dword_index
), prev_count
,
373 (uint32_t) (6 - log2_bits_per_vertex
)));
376 /* Start building the URB write message. The first MRF gets a copy of
380 dst_reg
mrf_reg(MRF
, base_mrf
);
381 src_reg
r0(retype(brw_vec8_grf(0, 0), BRW_REGISTER_TYPE_UD
));
382 vec4_instruction
*inst
= emit(MOV(mrf_reg
, r0
));
383 inst
->force_writemask_all
= true;
385 if (urb_write_flags
& BRW_URB_WRITE_PER_SLOT_OFFSET
) {
386 /* Set the per-slot offset to dword_index / 4, to that we'll write to
387 * the appropriate OWORD within the control data header.
389 src_reg
per_slot_offset(this, glsl_type::uint_type
);
390 emit(SHR(dst_reg(per_slot_offset
), dword_index
, 2u));
391 emit(GS_OPCODE_SET_WRITE_OFFSET
, mrf_reg
, per_slot_offset
, 1u);
394 if (urb_write_flags
& BRW_URB_WRITE_USE_CHANNEL_MASKS
) {
395 /* Set the channel masks to 1 << (dword_index % 4), so that we'll
396 * write to the appropriate DWORD within the OWORD. We need to do
397 * this computation with force_writemask_all, otherwise garbage data
398 * from invocation 0 might clobber the mask for invocation 1 when
399 * GS_OPCODE_PREPARE_CHANNEL_MASKS tries to OR the two masks
402 src_reg
channel(this, glsl_type::uint_type
);
403 inst
= emit(AND(dst_reg(channel
), dword_index
, 3u));
404 inst
->force_writemask_all
= true;
405 src_reg
one(this, glsl_type::uint_type
);
406 inst
= emit(MOV(dst_reg(one
), 1u));
407 inst
->force_writemask_all
= true;
408 src_reg
channel_mask(this, glsl_type::uint_type
);
409 inst
= emit(SHL(dst_reg(channel_mask
), one
, channel
));
410 inst
->force_writemask_all
= true;
411 emit(GS_OPCODE_PREPARE_CHANNEL_MASKS
, dst_reg(channel_mask
),
413 emit(GS_OPCODE_SET_CHANNEL_MASKS
, mrf_reg
, channel_mask
);
416 /* Store the control data bits in the message payload and send it. */
417 dst_reg
mrf_reg2(MRF
, base_mrf
+ 1);
418 inst
= emit(MOV(mrf_reg2
, this->control_data_bits
));
419 inst
->force_writemask_all
= true;
420 inst
= emit(GS_OPCODE_URB_WRITE
);
421 inst
->urb_write_flags
= urb_write_flags
;
422 /* We need to increment Global Offset by 256-bits to make room for
423 * Broadwell's extra "Vertex Count" payload at the beginning of the
424 * URB entry. Since this is an OWord message, Global Offset is counted
425 * in 128-bit units, so we must set it to 2.
427 if (devinfo
->gen
>= 8 && c
->prog_data
.static_vertex_count
== -1)
429 inst
->base_mrf
= base_mrf
;
434 vec4_gs_visitor::set_stream_control_data_bits(unsigned stream_id
)
436 /* control_data_bits |= stream_id << ((2 * (vertex_count - 1)) % 32) */
438 /* Note: we are calling this *before* increasing vertex_count, so
439 * this->vertex_count == vertex_count - 1 in the formula above.
442 /* Stream mode uses 2 bits per vertex */
443 assert(c
->control_data_bits_per_vertex
== 2);
445 /* Must be a valid stream */
446 assert(stream_id
>= 0 && stream_id
< MAX_VERTEX_STREAMS
);
448 /* Control data bits are initialized to 0 so we don't have to set any
449 * bits when sending vertices to stream 0.
454 /* reg::sid = stream_id */
455 src_reg
sid(this, glsl_type::uint_type
);
456 emit(MOV(dst_reg(sid
), stream_id
));
458 /* reg:shift_count = 2 * (vertex_count - 1) */
459 src_reg
shift_count(this, glsl_type::uint_type
);
460 emit(SHL(dst_reg(shift_count
), this->vertex_count
, 1u));
462 /* Note: we're relying on the fact that the GEN SHL instruction only pays
463 * attention to the lower 5 bits of its second source argument, so on this
464 * architecture, stream_id << 2 * (vertex_count - 1) is equivalent to
465 * stream_id << ((2 * (vertex_count - 1)) % 32).
467 src_reg
mask(this, glsl_type::uint_type
);
468 emit(SHL(dst_reg(mask
), sid
, shift_count
));
469 emit(OR(dst_reg(this->control_data_bits
), this->control_data_bits
, mask
));
473 vec4_gs_visitor::gs_emit_vertex(int stream_id
)
475 this->current_annotation
= "emit vertex: safety check";
477 /* Haswell and later hardware ignores the "Render Stream Select" bits
478 * from the 3DSTATE_STREAMOUT packet when the SOL stage is disabled,
479 * and instead sends all primitives down the pipeline for rasterization.
480 * If the SOL stage is enabled, "Render Stream Select" is honored and
481 * primitives bound to non-zero streams are discarded after stream output.
483 * Since the only purpose of primives sent to non-zero streams is to
484 * be recorded by transform feedback, we can simply discard all geometry
485 * bound to these streams when transform feedback is disabled.
487 if (stream_id
> 0 && shader_prog
->TransformFeedback
.NumVarying
== 0)
490 /* If we're outputting 32 control data bits or less, then we can wait
491 * until the shader is over to output them all. Otherwise we need to
492 * output them as we go. Now is the time to do it, since we're about to
493 * output the vertex_count'th vertex, so it's guaranteed that the
494 * control data bits associated with the (vertex_count - 1)th vertex are
497 if (c
->control_data_header_size_bits
> 32) {
498 this->current_annotation
= "emit vertex: emit control data bits";
499 /* Only emit control data bits if we've finished accumulating a batch
500 * of 32 bits. This is the case when:
502 * (vertex_count * bits_per_vertex) % 32 == 0
504 * (in other words, when the last 5 bits of vertex_count *
505 * bits_per_vertex are 0). Assuming bits_per_vertex == 2^n for some
506 * integer n (which is always the case, since bits_per_vertex is
507 * always 1 or 2), this is equivalent to requiring that the last 5-n
508 * bits of vertex_count are 0:
510 * vertex_count & (2^(5-n) - 1) == 0
512 * 2^(5-n) == 2^5 / 2^n == 32 / bits_per_vertex, so this is
515 * vertex_count & (32 / bits_per_vertex - 1) == 0
517 vec4_instruction
*inst
=
518 emit(AND(dst_null_d(), this->vertex_count
,
519 (uint32_t) (32 / c
->control_data_bits_per_vertex
- 1)));
520 inst
->conditional_mod
= BRW_CONDITIONAL_Z
;
522 emit(IF(BRW_PREDICATE_NORMAL
));
524 /* If vertex_count is 0, then no control data bits have been
525 * accumulated yet, so we skip emitting them.
527 emit(CMP(dst_null_d(), this->vertex_count
, 0u,
528 BRW_CONDITIONAL_NEQ
));
529 emit(IF(BRW_PREDICATE_NORMAL
));
530 emit_control_data_bits();
531 emit(BRW_OPCODE_ENDIF
);
533 /* Reset control_data_bits to 0 so we can start accumulating a new
536 * Note: in the case where vertex_count == 0, this neutralizes the
537 * effect of any call to EndPrimitive() that the shader may have
538 * made before outputting its first vertex.
540 inst
= emit(MOV(dst_reg(this->control_data_bits
), 0u));
541 inst
->force_writemask_all
= true;
543 emit(BRW_OPCODE_ENDIF
);
546 this->current_annotation
= "emit vertex: vertex data";
549 /* In stream mode we have to set control data bits for all vertices
550 * unless we have disabled control data bits completely (which we do
551 * do for GL_POINTS outputs that don't use streams).
553 if (c
->control_data_header_size_bits
> 0 &&
554 c
->prog_data
.control_data_format
==
555 GEN7_GS_CONTROL_DATA_FORMAT_GSCTL_SID
) {
556 this->current_annotation
= "emit vertex: Stream control data bits";
557 set_stream_control_data_bits(stream_id
);
560 this->current_annotation
= NULL
;
564 vec4_gs_visitor::visit(ir_emit_vertex
*ir
)
566 /* To ensure that we don't output more vertices than the shader specified
567 * using max_vertices, do the logic inside a conditional of the form "if
568 * (vertex_count < MAX)"
570 unsigned num_output_vertices
= c
->gp
->program
.VerticesOut
;
571 emit(CMP(dst_null_d(), this->vertex_count
,
572 src_reg(num_output_vertices
), BRW_CONDITIONAL_L
));
573 emit(IF(BRW_PREDICATE_NORMAL
));
575 gs_emit_vertex(ir
->stream_id());
577 this->current_annotation
= "emit vertex: increment vertex count";
578 emit(ADD(dst_reg(this->vertex_count
), this->vertex_count
,
581 emit(BRW_OPCODE_ENDIF
);
585 vec4_gs_visitor::gs_end_primitive()
587 /* We can only do EndPrimitive() functionality when the control data
588 * consists of cut bits. Fortunately, the only time it isn't is when the
589 * output type is points, in which case EndPrimitive() is a no-op.
591 if (c
->prog_data
.control_data_format
!=
592 GEN7_GS_CONTROL_DATA_FORMAT_GSCTL_CUT
) {
596 /* Cut bits use one bit per vertex. */
597 assert(c
->control_data_bits_per_vertex
== 1);
599 /* Cut bit n should be set to 1 if EndPrimitive() was called after emitting
600 * vertex n, 0 otherwise. So all we need to do here is mark bit
601 * (vertex_count - 1) % 32 in the cut_bits register to indicate that
602 * EndPrimitive() was called after emitting vertex (vertex_count - 1);
603 * vec4_gs_visitor::emit_control_data_bits() will take care of the rest.
605 * Note that if EndPrimitve() is called before emitting any vertices, this
606 * will cause us to set bit 31 of the control_data_bits register to 1.
607 * That's fine because:
609 * - If max_vertices < 32, then vertex number 31 (zero-based) will never be
610 * output, so the hardware will ignore cut bit 31.
612 * - If max_vertices == 32, then vertex number 31 is guaranteed to be the
613 * last vertex, so setting cut bit 31 has no effect (since the primitive
614 * is automatically ended when the GS terminates).
616 * - If max_vertices > 32, then the ir_emit_vertex visitor will reset the
617 * control_data_bits register to 0 when the first vertex is emitted.
620 /* control_data_bits |= 1 << ((vertex_count - 1) % 32) */
621 src_reg
one(this, glsl_type::uint_type
);
622 emit(MOV(dst_reg(one
), 1u));
623 src_reg
prev_count(this, glsl_type::uint_type
);
624 emit(ADD(dst_reg(prev_count
), this->vertex_count
, 0xffffffffu
));
625 src_reg
mask(this, glsl_type::uint_type
);
626 /* Note: we're relying on the fact that the GEN SHL instruction only pays
627 * attention to the lower 5 bits of its second source argument, so on this
628 * architecture, 1 << (vertex_count - 1) is equivalent to 1 <<
629 * ((vertex_count - 1) % 32).
631 emit(SHL(dst_reg(mask
), one
, prev_count
));
632 emit(OR(dst_reg(this->control_data_bits
), this->control_data_bits
, mask
));
636 vec4_gs_visitor::visit(ir_end_primitive
*)
641 static const unsigned *
642 generate_assembly(struct brw_context
*brw
,
643 struct gl_shader_program
*shader_prog
,
644 struct gl_program
*prog
,
645 struct brw_vue_prog_data
*prog_data
,
648 unsigned *final_assembly_size
)
650 vec4_generator
g(brw
->intelScreen
->compiler
, brw
,
651 shader_prog
, prog
, prog_data
, mem_ctx
,
652 INTEL_DEBUG
& DEBUG_GS
, "geometry", "GS");
653 return g
.generate_assembly(cfg
, final_assembly_size
);
656 extern "C" const unsigned *
657 brw_gs_emit(struct brw_context
*brw
,
658 struct gl_shader_program
*prog
,
659 struct brw_gs_compile
*c
,
661 unsigned *final_assembly_size
)
663 if (unlikely(INTEL_DEBUG
& DEBUG_GS
)) {
664 struct brw_shader
*shader
=
665 (brw_shader
*) prog
->_LinkedShaders
[MESA_SHADER_GEOMETRY
];
667 brw_dump_ir("geometry", prog
, &shader
->base
, NULL
);
671 if (INTEL_DEBUG
& DEBUG_SHADER_TIME
)
672 st_index
= brw_get_shader_time_index(brw
, prog
, NULL
, ST_GS
);
675 /* Compile the geometry shader in DUAL_OBJECT dispatch mode, if we can do
676 * so without spilling. If the GS invocations count > 1, then we can't use
679 if (c
->prog_data
.invocations
<= 1 &&
680 likely(!(INTEL_DEBUG
& DEBUG_NO_DUAL_OBJECT_GS
))) {
681 c
->prog_data
.base
.dispatch_mode
= DISPATCH_MODE_4X2_DUAL_OBJECT
;
683 vec4_gs_visitor
v(brw
->intelScreen
->compiler
, brw
,
684 c
, prog
, mem_ctx
, true /* no_spills */, st_index
);
686 return generate_assembly(brw
, prog
, &c
->gp
->program
.Base
,
687 &c
->prog_data
.base
, mem_ctx
, v
.cfg
,
688 final_assembly_size
);
693 /* Either we failed to compile in DUAL_OBJECT mode (probably because it
694 * would have required spilling) or DUAL_OBJECT mode is disabled. So fall
695 * back to DUAL_INSTANCED or SINGLE mode, which consumes fewer registers.
697 * FIXME: Single dispatch mode requires that the driver can handle
698 * interleaving of input registers, but this is already supported (dual
699 * instance mode has the same requirement). However, to take full advantage
700 * of single dispatch mode to reduce register pressure we would also need to
701 * do interleaved outputs, but currently, the vec4 visitor and generator
702 * classes do not support this, so at the moment register pressure in
703 * single and dual instance modes is the same.
705 * From the Ivy Bridge PRM, Vol2 Part1 7.2.1.1 "3DSTATE_GS"
706 * "If InstanceCount>1, DUAL_OBJECT mode is invalid. Software will likely
707 * want to use DUAL_INSTANCE mode for higher performance, but SINGLE mode
708 * is also supported. When InstanceCount=1 (one instance per object) software
709 * can decide which dispatch mode to use. DUAL_OBJECT mode would likely be
710 * the best choice for performance, followed by SINGLE mode."
712 * So SINGLE mode is more performant when invocations == 1 and DUAL_INSTANCE
713 * mode is more performant when invocations > 1. Gen6 only supports
716 if (c
->prog_data
.invocations
<= 1 || brw
->gen
< 7)
717 c
->prog_data
.base
.dispatch_mode
= DISPATCH_MODE_4X1_SINGLE
;
719 c
->prog_data
.base
.dispatch_mode
= DISPATCH_MODE_4X2_DUAL_INSTANCE
;
721 vec4_gs_visitor
*gs
= NULL
;
722 const unsigned *ret
= NULL
;
725 gs
= new vec4_gs_visitor(brw
->intelScreen
->compiler
, brw
,
726 c
, prog
, mem_ctx
, false /* no_spills */,
729 gs
= new gen6_gs_visitor(brw
->intelScreen
->compiler
, brw
,
730 c
, prog
, mem_ctx
, false /* no_spills */,
734 prog
->LinkStatus
= false;
735 ralloc_strcat(&prog
->InfoLog
, gs
->fail_msg
);
737 ret
= generate_assembly(brw
, prog
, &c
->gp
->program
.Base
,
738 &c
->prog_data
.base
, mem_ctx
, gs
->cfg
,
739 final_assembly_size
);
747 } /* namespace brw */