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