i965: Move pre-draw resolve buffers to dd::UpdateState
[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
32 const unsigned MAX_GS_INPUT_VERTICES = 6;
33
34 namespace brw {
35
36 vec4_gs_visitor::vec4_gs_visitor(struct brw_context *brw,
37 struct brw_gs_compile *c,
38 struct gl_shader_program *prog,
39 void *mem_ctx,
40 bool no_spills)
41 : vec4_visitor(brw, &c->base, &c->gp->program.Base, &c->key.base,
42 &c->prog_data.base, prog, MESA_SHADER_GEOMETRY, mem_ctx,
43 INTEL_DEBUG & DEBUG_GS, no_spills,
44 ST_GS, ST_GS_WRITTEN, ST_GS_RESET),
45 c(c)
46 {
47 }
48
49
50 dst_reg *
51 vec4_gs_visitor::make_reg_for_system_value(ir_variable *ir)
52 {
53 dst_reg *reg = new(mem_ctx) dst_reg(this, ir->type);
54
55 switch (ir->data.location) {
56 case SYSTEM_VALUE_INVOCATION_ID:
57 this->current_annotation = "initialize gl_InvocationID";
58 emit(GS_OPCODE_GET_INSTANCE_ID, *reg);
59 break;
60 default:
61 unreachable("not reached");
62 }
63
64 return reg;
65 }
66
67
68 int
69 vec4_gs_visitor::setup_varying_inputs(int payload_reg, int *attribute_map,
70 int attributes_per_reg)
71 {
72 /* For geometry shaders there are N copies of the input attributes, where N
73 * is the number of input vertices. attribute_map[BRW_VARYING_SLOT_COUNT *
74 * i + j] represents attribute j for vertex i.
75 *
76 * Note that GS inputs are read from the VUE 256 bits (2 vec4's) at a time,
77 * so the total number of input slots that will be delivered to the GS (and
78 * thus the stride of the input arrays) is urb_read_length * 2.
79 */
80 const unsigned num_input_vertices = c->gp->program.VerticesIn;
81 assert(num_input_vertices <= MAX_GS_INPUT_VERTICES);
82 unsigned input_array_stride = c->prog_data.base.urb_read_length * 2;
83
84 for (int slot = 0; slot < c->input_vue_map.num_slots; slot++) {
85 int varying = c->input_vue_map.slot_to_varying[slot];
86 for (unsigned vertex = 0; vertex < num_input_vertices; vertex++) {
87 attribute_map[BRW_VARYING_SLOT_COUNT * vertex + varying] =
88 attributes_per_reg * payload_reg + input_array_stride * vertex +
89 slot;
90 }
91 }
92
93 int regs_used = ALIGN(input_array_stride * num_input_vertices,
94 attributes_per_reg) / attributes_per_reg;
95 return payload_reg + regs_used;
96 }
97
98
99 void
100 vec4_gs_visitor::setup_payload()
101 {
102 int attribute_map[BRW_VARYING_SLOT_COUNT * MAX_GS_INPUT_VERTICES];
103
104 /* If we are in dual instanced mode, then attributes are going to be
105 * interleaved, so one register contains two attribute slots.
106 */
107 int attributes_per_reg = c->prog_data.dual_instanced_dispatch ? 2 : 1;
108
109 /* If a geometry shader tries to read from an input that wasn't written by
110 * the vertex shader, that produces undefined results, but it shouldn't
111 * crash anything. So initialize attribute_map to zeros--that ensures that
112 * these undefined results are read from r0.
113 */
114 memset(attribute_map, 0, sizeof(attribute_map));
115
116 int reg = 0;
117
118 /* The payload always contains important data in r0, which contains
119 * the URB handles that are passed on to the URB write at the end
120 * of the thread.
121 */
122 reg++;
123
124 /* If the shader uses gl_PrimitiveIDIn, that goes in r1. */
125 if (c->prog_data.include_primitive_id)
126 attribute_map[VARYING_SLOT_PRIMITIVE_ID] = attributes_per_reg * reg++;
127
128 reg = setup_uniforms(reg);
129
130 reg = setup_varying_inputs(reg, attribute_map, attributes_per_reg);
131
132 lower_attributes_to_hw_regs(attribute_map,
133 c->prog_data.dual_instanced_dispatch);
134
135 this->first_non_payload_grf = reg;
136 }
137
138
139 void
140 vec4_gs_visitor::emit_prolog()
141 {
142 /* In vertex shaders, r0.2 is guaranteed to be initialized to zero. In
143 * geometry shaders, it isn't (it contains a bunch of information we don't
144 * need, like the input primitive type). We need r0.2 to be zero in order
145 * to build scratch read/write messages correctly (otherwise this value
146 * will be interpreted as a global offset, causing us to do our scratch
147 * reads/writes to garbage memory). So just set it to zero at the top of
148 * the shader.
149 */
150 this->current_annotation = "clear r0.2";
151 dst_reg r0(retype(brw_vec4_grf(0, 0), BRW_REGISTER_TYPE_UD));
152 vec4_instruction *inst = emit(GS_OPCODE_SET_DWORD_2_IMMED, r0, 0u);
153 inst->force_writemask_all = true;
154
155 /* Create a virtual register to hold the vertex count */
156 this->vertex_count = src_reg(this, glsl_type::uint_type);
157
158 /* Initialize the vertex_count register to 0 */
159 this->current_annotation = "initialize vertex_count";
160 inst = emit(MOV(dst_reg(this->vertex_count), 0u));
161 inst->force_writemask_all = true;
162
163 if (c->control_data_header_size_bits > 0) {
164 /* Create a virtual register to hold the current set of control data
165 * bits.
166 */
167 this->control_data_bits = src_reg(this, glsl_type::uint_type);
168
169 /* If we're outputting more than 32 control data bits, then EmitVertex()
170 * will set control_data_bits to 0 after emitting the first vertex.
171 * Otherwise, we need to initialize it to 0 here.
172 */
173 if (c->control_data_header_size_bits <= 32) {
174 this->current_annotation = "initialize control data bits";
175 inst = emit(MOV(dst_reg(this->control_data_bits), 0u));
176 inst->force_writemask_all = true;
177 }
178 }
179
180 /* If the geometry shader uses the gl_PointSize input, we need to fix it up
181 * to account for the fact that the vertex shader stored it in the w
182 * component of VARYING_SLOT_PSIZ.
183 */
184 if (c->gp->program.Base.InputsRead & VARYING_BIT_PSIZ) {
185 this->current_annotation = "swizzle gl_PointSize input";
186 for (int vertex = 0; vertex < c->gp->program.VerticesIn; vertex++) {
187 dst_reg dst(ATTR,
188 BRW_VARYING_SLOT_COUNT * vertex + VARYING_SLOT_PSIZ);
189 dst.type = BRW_REGISTER_TYPE_F;
190 src_reg src(dst);
191 dst.writemask = WRITEMASK_X;
192 src.swizzle = BRW_SWIZZLE_WWWW;
193 inst = emit(MOV(dst, src));
194
195 /* In dual instanced dispatch mode, dst has a width of 4, so we need
196 * to make sure the MOV happens regardless of which channels are
197 * enabled.
198 */
199 inst->force_writemask_all = true;
200 }
201 }
202
203 this->current_annotation = NULL;
204 }
205
206
207 void
208 vec4_gs_visitor::emit_program_code()
209 {
210 /* We don't support NV_geometry_program4. */
211 unreachable("Unreached");
212 }
213
214
215 void
216 vec4_gs_visitor::emit_thread_end()
217 {
218 if (c->control_data_header_size_bits > 0) {
219 /* During shader execution, we only ever call emit_control_data_bits()
220 * just prior to outputting a vertex. Therefore, the control data bits
221 * corresponding to the most recently output vertex still need to be
222 * emitted.
223 */
224 current_annotation = "thread end: emit control data bits";
225 emit_control_data_bits();
226 }
227
228 /* MRF 0 is reserved for the debugger, so start with message header
229 * in MRF 1.
230 */
231 int base_mrf = 1;
232
233 current_annotation = "thread end";
234 dst_reg mrf_reg(MRF, base_mrf);
235 src_reg r0(retype(brw_vec8_grf(0, 0), BRW_REGISTER_TYPE_UD));
236 vec4_instruction *inst = emit(MOV(mrf_reg, r0));
237 inst->force_writemask_all = true;
238 emit(GS_OPCODE_SET_VERTEX_COUNT, mrf_reg, this->vertex_count);
239 if (INTEL_DEBUG & DEBUG_SHADER_TIME)
240 emit_shader_time_end();
241 inst = emit(GS_OPCODE_THREAD_END);
242 inst->base_mrf = base_mrf;
243 inst->mlen = 1;
244 }
245
246
247 void
248 vec4_gs_visitor::emit_urb_write_header(int mrf)
249 {
250 /* The SEND instruction that writes the vertex data to the VUE will use
251 * per_slot_offset=true, which means that DWORDs 3 and 4 of the message
252 * header specify an offset (in multiples of 256 bits) into the URB entry
253 * at which the write should take place.
254 *
255 * So we have to prepare a message header with the appropriate offset
256 * values.
257 */
258 dst_reg mrf_reg(MRF, mrf);
259 src_reg r0(retype(brw_vec8_grf(0, 0), BRW_REGISTER_TYPE_UD));
260 this->current_annotation = "URB write header";
261 vec4_instruction *inst = emit(MOV(mrf_reg, r0));
262 inst->force_writemask_all = true;
263 emit(GS_OPCODE_SET_WRITE_OFFSET, mrf_reg, this->vertex_count,
264 (uint32_t) c->prog_data.output_vertex_size_hwords);
265 }
266
267
268 vec4_instruction *
269 vec4_gs_visitor::emit_urb_write_opcode(bool complete)
270 {
271 /* We don't care whether the vertex is complete, because in general
272 * geometry shaders output multiple vertices, and we don't terminate the
273 * thread until all vertices are complete.
274 */
275 (void) complete;
276
277 vec4_instruction *inst = emit(GS_OPCODE_URB_WRITE);
278 inst->offset = c->prog_data.control_data_header_size_hwords;
279
280 /* We need to increment Global Offset by 1 to make room for Broadwell's
281 * extra "Vertex Count" payload at the beginning of the URB entry.
282 */
283 if (brw->gen >= 8)
284 inst->offset++;
285
286 inst->urb_write_flags = BRW_URB_WRITE_PER_SLOT_OFFSET;
287 return inst;
288 }
289
290
291 int
292 vec4_gs_visitor::compute_array_stride(ir_dereference_array *ir)
293 {
294 /* Geometry shader inputs are arrays, but they use an unusual array layout:
295 * instead of all array elements for a given geometry shader input being
296 * stored consecutively, all geometry shader inputs are interleaved into
297 * one giant array. At this stage of compilation, we assume that the
298 * stride of the array is BRW_VARYING_SLOT_COUNT. Later,
299 * setup_attributes() will remap our accesses to the actual input array.
300 */
301 ir_dereference_variable *deref_var = ir->array->as_dereference_variable();
302 if (deref_var && deref_var->var->data.mode == ir_var_shader_in)
303 return BRW_VARYING_SLOT_COUNT;
304 else
305 return vec4_visitor::compute_array_stride(ir);
306 }
307
308
309 /**
310 * Write out a batch of 32 control data bits from the control_data_bits
311 * register to the URB.
312 *
313 * The current value of the vertex_count register determines which DWORD in
314 * the URB receives the control data bits. The control_data_bits register is
315 * assumed to contain the correct data for the vertex that was most recently
316 * output, and all previous vertices that share the same DWORD.
317 *
318 * This function takes care of ensuring that if no vertices have been output
319 * yet, no control bits are emitted.
320 */
321 void
322 vec4_gs_visitor::emit_control_data_bits()
323 {
324 assert(c->control_data_bits_per_vertex != 0);
325
326 /* Since the URB_WRITE_OWORD message operates with 128-bit (vec4 sized)
327 * granularity, we need to use two tricks to ensure that the batch of 32
328 * control data bits is written to the appropriate DWORD in the URB. To
329 * select which vec4 we are writing to, we use the "slot {0,1} offset"
330 * fields of the message header. To select which DWORD in the vec4 we are
331 * writing to, we use the channel mask fields of the message header. To
332 * avoid penalizing geometry shaders that emit a small number of vertices
333 * with extra bookkeeping, we only do each of these tricks when
334 * c->prog_data.control_data_header_size_bits is large enough to make it
335 * necessary.
336 *
337 * Note: this means that if we're outputting just a single DWORD of control
338 * data bits, we'll actually replicate it four times since we won't do any
339 * channel masking. But that's not a problem since in this case the
340 * hardware only pays attention to the first DWORD.
341 */
342 enum brw_urb_write_flags urb_write_flags = BRW_URB_WRITE_OWORD;
343 if (c->control_data_header_size_bits > 32)
344 urb_write_flags = urb_write_flags | BRW_URB_WRITE_USE_CHANNEL_MASKS;
345 if (c->control_data_header_size_bits > 128)
346 urb_write_flags = urb_write_flags | BRW_URB_WRITE_PER_SLOT_OFFSET;
347
348 /* If vertex_count is 0, then no control data bits have been accumulated
349 * yet, so we should do nothing.
350 */
351 emit(CMP(dst_null_d(), this->vertex_count, 0u, BRW_CONDITIONAL_NEQ));
352 emit(IF(BRW_PREDICATE_NORMAL));
353 {
354 /* If we are using either channel masks or a per-slot offset, then we
355 * need to figure out which DWORD we are trying to write to, using the
356 * formula:
357 *
358 * dword_index = (vertex_count - 1) * bits_per_vertex / 32
359 *
360 * Since bits_per_vertex is a power of two, and is known at compile
361 * time, this can be optimized to:
362 *
363 * dword_index = (vertex_count - 1) >> (6 - log2(bits_per_vertex))
364 */
365 src_reg dword_index(this, glsl_type::uint_type);
366 if (urb_write_flags) {
367 src_reg prev_count(this, glsl_type::uint_type);
368 emit(ADD(dst_reg(prev_count), this->vertex_count, 0xffffffffu));
369 unsigned log2_bits_per_vertex =
370 _mesa_fls(c->control_data_bits_per_vertex);
371 emit(SHR(dst_reg(dword_index), prev_count,
372 (uint32_t) (6 - log2_bits_per_vertex)));
373 }
374
375 /* Start building the URB write message. The first MRF gets a copy of
376 * R0.
377 */
378 int base_mrf = 1;
379 dst_reg mrf_reg(MRF, base_mrf);
380 src_reg r0(retype(brw_vec8_grf(0, 0), BRW_REGISTER_TYPE_UD));
381 vec4_instruction *inst = emit(MOV(mrf_reg, r0));
382 inst->force_writemask_all = true;
383
384 if (urb_write_flags & BRW_URB_WRITE_PER_SLOT_OFFSET) {
385 /* Set the per-slot offset to dword_index / 4, to that we'll write to
386 * the appropriate OWORD within the control data header.
387 */
388 src_reg per_slot_offset(this, glsl_type::uint_type);
389 emit(SHR(dst_reg(per_slot_offset), dword_index, 2u));
390 emit(GS_OPCODE_SET_WRITE_OFFSET, mrf_reg, per_slot_offset, 1u);
391 }
392
393 if (urb_write_flags & BRW_URB_WRITE_USE_CHANNEL_MASKS) {
394 /* Set the channel masks to 1 << (dword_index % 4), so that we'll
395 * write to the appropriate DWORD within the OWORD. We need to do
396 * this computation with force_writemask_all, otherwise garbage data
397 * from invocation 0 might clobber the mask for invocation 1 when
398 * GS_OPCODE_PREPARE_CHANNEL_MASKS tries to OR the two masks
399 * together.
400 */
401 src_reg channel(this, glsl_type::uint_type);
402 inst = emit(AND(dst_reg(channel), dword_index, 3u));
403 inst->force_writemask_all = true;
404 src_reg one(this, glsl_type::uint_type);
405 inst = emit(MOV(dst_reg(one), 1u));
406 inst->force_writemask_all = true;
407 src_reg channel_mask(this, glsl_type::uint_type);
408 inst = emit(SHL(dst_reg(channel_mask), one, channel));
409 inst->force_writemask_all = true;
410 emit(GS_OPCODE_PREPARE_CHANNEL_MASKS, dst_reg(channel_mask),
411 channel_mask);
412 emit(GS_OPCODE_SET_CHANNEL_MASKS, mrf_reg, channel_mask);
413 }
414
415 /* Store the control data bits in the message payload and send it. */
416 dst_reg mrf_reg2(MRF, base_mrf + 1);
417 inst = emit(MOV(mrf_reg2, this->control_data_bits));
418 inst->force_writemask_all = true;
419 inst = emit(GS_OPCODE_URB_WRITE);
420 inst->urb_write_flags = urb_write_flags;
421 /* We need to increment Global Offset by 256-bits to make room for
422 * Broadwell's extra "Vertex Count" payload at the beginning of the
423 * URB entry. Since this is an OWord message, Global Offset is counted
424 * in 128-bit units, so we must set it to 2.
425 */
426 if (brw->gen >= 8)
427 inst->offset = 2;
428 inst->base_mrf = base_mrf;
429 inst->mlen = 2;
430 }
431 emit(BRW_OPCODE_ENDIF);
432 }
433
434 void
435 vec4_gs_visitor::set_stream_control_data_bits(unsigned stream_id)
436 {
437 /* control_data_bits |= stream_id << ((2 * (vertex_count - 1)) % 32) */
438
439 /* Note: we are calling this *before* increasing vertex_count, so
440 * this->vertex_count == vertex_count - 1 in the formula above.
441 */
442
443 /* Stream mode uses 2 bits per vertex */
444 assert(c->control_data_bits_per_vertex == 2);
445
446 /* Must be a valid stream */
447 assert(stream_id >= 0 && stream_id < MAX_VERTEX_STREAMS);
448
449 /* Control data bits are initialized to 0 so we don't have to set any
450 * bits when sending vertices to stream 0.
451 */
452 if (stream_id == 0)
453 return;
454
455 /* reg::sid = stream_id */
456 src_reg sid(this, glsl_type::uint_type);
457 emit(MOV(dst_reg(sid), stream_id));
458
459 /* reg:shift_count = 2 * (vertex_count - 1) */
460 src_reg shift_count(this, glsl_type::uint_type);
461 emit(SHL(dst_reg(shift_count), this->vertex_count, 1u));
462
463 /* Note: we're relying on the fact that the GEN SHL instruction only pays
464 * attention to the lower 5 bits of its second source argument, so on this
465 * architecture, stream_id << 2 * (vertex_count - 1) is equivalent to
466 * stream_id << ((2 * (vertex_count - 1)) % 32).
467 */
468 src_reg mask(this, glsl_type::uint_type);
469 emit(SHL(dst_reg(mask), sid, shift_count));
470 emit(OR(dst_reg(this->control_data_bits), this->control_data_bits, mask));
471 }
472
473 void
474 vec4_gs_visitor::visit(ir_emit_vertex *ir)
475 {
476 this->current_annotation = "emit vertex: safety check";
477
478 /* To ensure that we don't output more vertices than the shader specified
479 * using max_vertices, do the logic inside a conditional of the form "if
480 * (vertex_count < MAX)"
481 */
482 unsigned num_output_vertices = c->gp->program.VerticesOut;
483 emit(CMP(dst_null_d(), this->vertex_count,
484 src_reg(num_output_vertices), BRW_CONDITIONAL_L));
485 emit(IF(BRW_PREDICATE_NORMAL));
486 {
487 /* If we're outputting 32 control data bits or less, then we can wait
488 * until the shader is over to output them all. Otherwise we need to
489 * output them as we go. Now is the time to do it, since we're about to
490 * output the vertex_count'th vertex, so it's guaranteed that the
491 * control data bits associated with the (vertex_count - 1)th vertex are
492 * correct.
493 */
494 if (c->control_data_header_size_bits > 32) {
495 this->current_annotation = "emit vertex: emit control data bits";
496 /* Only emit control data bits if we've finished accumulating a batch
497 * of 32 bits. This is the case when:
498 *
499 * (vertex_count * bits_per_vertex) % 32 == 0
500 *
501 * (in other words, when the last 5 bits of vertex_count *
502 * bits_per_vertex are 0). Assuming bits_per_vertex == 2^n for some
503 * integer n (which is always the case, since bits_per_vertex is
504 * always 1 or 2), this is equivalent to requiring that the last 5-n
505 * bits of vertex_count are 0:
506 *
507 * vertex_count & (2^(5-n) - 1) == 0
508 *
509 * 2^(5-n) == 2^5 / 2^n == 32 / bits_per_vertex, so this is
510 * equivalent to:
511 *
512 * vertex_count & (32 / bits_per_vertex - 1) == 0
513 */
514 vec4_instruction *inst =
515 emit(AND(dst_null_d(), this->vertex_count,
516 (uint32_t) (32 / c->control_data_bits_per_vertex - 1)));
517 inst->conditional_mod = BRW_CONDITIONAL_Z;
518 emit(IF(BRW_PREDICATE_NORMAL));
519 {
520 emit_control_data_bits();
521
522 /* Reset control_data_bits to 0 so we can start accumulating a new
523 * batch.
524 *
525 * Note: in the case where vertex_count == 0, this neutralizes the
526 * effect of any call to EndPrimitive() that the shader may have
527 * made before outputting its first vertex.
528 */
529 inst = emit(MOV(dst_reg(this->control_data_bits), 0u));
530 inst->force_writemask_all = true;
531 }
532 emit(BRW_OPCODE_ENDIF);
533 }
534
535 this->current_annotation = "emit vertex: vertex data";
536 emit_vertex();
537
538 /* In stream mode we have to set control data bits for all vertices
539 * unless we have disabled control data bits completely (which we do
540 * do for GL_POINTS outputs that don't use streams).
541 */
542 if (c->control_data_header_size_bits > 0 &&
543 c->prog_data.control_data_format ==
544 GEN7_GS_CONTROL_DATA_FORMAT_GSCTL_SID) {
545 this->current_annotation = "emit vertex: Stream control data bits";
546 set_stream_control_data_bits(ir->stream_id());
547 }
548
549 this->current_annotation = "emit vertex: increment vertex count";
550 emit(ADD(dst_reg(this->vertex_count), this->vertex_count,
551 src_reg(1u)));
552 }
553 emit(BRW_OPCODE_ENDIF);
554
555 this->current_annotation = NULL;
556 }
557
558 void
559 vec4_gs_visitor::visit(ir_end_primitive *)
560 {
561 /* We can only do EndPrimitive() functionality when the control data
562 * consists of cut bits. Fortunately, the only time it isn't is when the
563 * output type is points, in which case EndPrimitive() is a no-op.
564 */
565 if (c->prog_data.control_data_format !=
566 GEN7_GS_CONTROL_DATA_FORMAT_GSCTL_CUT) {
567 return;
568 }
569
570 /* Cut bits use one bit per vertex. */
571 assert(c->control_data_bits_per_vertex == 1);
572
573 /* Cut bit n should be set to 1 if EndPrimitive() was called after emitting
574 * vertex n, 0 otherwise. So all we need to do here is mark bit
575 * (vertex_count - 1) % 32 in the cut_bits register to indicate that
576 * EndPrimitive() was called after emitting vertex (vertex_count - 1);
577 * vec4_gs_visitor::emit_control_data_bits() will take care of the rest.
578 *
579 * Note that if EndPrimitve() is called before emitting any vertices, this
580 * will cause us to set bit 31 of the control_data_bits register to 1.
581 * That's fine because:
582 *
583 * - If max_vertices < 32, then vertex number 31 (zero-based) will never be
584 * output, so the hardware will ignore cut bit 31.
585 *
586 * - If max_vertices == 32, then vertex number 31 is guaranteed to be the
587 * last vertex, so setting cut bit 31 has no effect (since the primitive
588 * is automatically ended when the GS terminates).
589 *
590 * - If max_vertices > 32, then the ir_emit_vertex visitor will reset the
591 * control_data_bits register to 0 when the first vertex is emitted.
592 */
593
594 /* control_data_bits |= 1 << ((vertex_count - 1) % 32) */
595 src_reg one(this, glsl_type::uint_type);
596 emit(MOV(dst_reg(one), 1u));
597 src_reg prev_count(this, glsl_type::uint_type);
598 emit(ADD(dst_reg(prev_count), this->vertex_count, 0xffffffffu));
599 src_reg mask(this, glsl_type::uint_type);
600 /* Note: we're relying on the fact that the GEN SHL instruction only pays
601 * attention to the lower 5 bits of its second source argument, so on this
602 * architecture, 1 << (vertex_count - 1) is equivalent to 1 <<
603 * ((vertex_count - 1) % 32).
604 */
605 emit(SHL(dst_reg(mask), one, prev_count));
606 emit(OR(dst_reg(this->control_data_bits), this->control_data_bits, mask));
607 }
608
609 static const unsigned *
610 generate_assembly(struct brw_context *brw,
611 struct gl_shader_program *shader_prog,
612 struct gl_program *prog,
613 struct brw_vec4_prog_data *prog_data,
614 void *mem_ctx,
615 exec_list *instructions,
616 unsigned *final_assembly_size)
617 {
618 vec4_generator g(brw, shader_prog, prog, prog_data, mem_ctx,
619 INTEL_DEBUG & DEBUG_GS);
620 return g.generate_assembly(instructions, final_assembly_size);
621 }
622
623 extern "C" const unsigned *
624 brw_gs_emit(struct brw_context *brw,
625 struct gl_shader_program *prog,
626 struct brw_gs_compile *c,
627 void *mem_ctx,
628 unsigned *final_assembly_size)
629 {
630 if (unlikely(INTEL_DEBUG & DEBUG_GS)) {
631 struct brw_shader *shader =
632 (brw_shader *) prog->_LinkedShaders[MESA_SHADER_GEOMETRY];
633
634 brw_dump_ir(brw, "geometry", prog, &shader->base, NULL);
635 }
636
637 /* Compile the geometry shader in DUAL_OBJECT dispatch mode, if we can do
638 * so without spilling. If the GS invocations count > 1, then we can't use
639 * dual object mode.
640 */
641 if (c->prog_data.invocations <= 1 &&
642 likely(!(INTEL_DEBUG & DEBUG_NO_DUAL_OBJECT_GS))) {
643 c->prog_data.dual_instanced_dispatch = false;
644
645 vec4_gs_visitor v(brw, c, prog, mem_ctx, true /* no_spills */);
646 if (v.run()) {
647 return generate_assembly(brw, prog, &c->gp->program.Base,
648 &c->prog_data.base, mem_ctx, &v.instructions,
649 final_assembly_size);
650 }
651 }
652
653 /* Either we failed to compile in DUAL_OBJECT mode (probably because it
654 * would have required spilling) or DUAL_OBJECT mode is disabled. So fall
655 * back to DUAL_INSTANCED mode, which consumes fewer registers.
656 *
657 * FIXME: In an ideal world we'd fall back to SINGLE mode, which would
658 * allow us to interleave general purpose registers (resulting in even less
659 * likelihood of spilling). But at the moment, the vec4 generator and
660 * visitor classes don't have the infrastructure to interleave general
661 * purpose registers, so DUAL_INSTANCED is the best we can do.
662 */
663 c->prog_data.dual_instanced_dispatch = true;
664
665 vec4_gs_visitor v(brw, c, prog, mem_ctx, false /* no_spills */);
666 if (!v.run()) {
667 prog->LinkStatus = false;
668 ralloc_strcat(&prog->InfoLog, v.fail_msg);
669 return NULL;
670 }
671
672 return generate_assembly(brw, prog, &c->gp->program.Base, &c->prog_data.base,
673 mem_ctx, &v.instructions, final_assembly_size);
674 }
675
676
677 } /* namespace brw */