tgsi/ureg: make the dst register match the src indirection
[mesa.git] / src / gallium / auxiliary / draw / draw_gs.c
1 /**************************************************************************
2 *
3 * Copyright 2009 VMware, Inc.
4 * All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 *
26 **************************************************************************/
27
28 #include "draw_gs.h"
29
30 #include "draw_private.h"
31 #include "draw_context.h"
32 #ifdef HAVE_LLVM
33 #include "draw_llvm.h"
34 #endif
35
36 #include "tgsi/tgsi_parse.h"
37 #include "tgsi/tgsi_exec.h"
38
39 #include "pipe/p_shader_tokens.h"
40
41 #include "util/u_math.h"
42 #include "util/u_memory.h"
43 #include "util/u_prim.h"
44
45 /* fixme: move it from here */
46 #define MAX_PRIMITIVES 64
47
48 static INLINE int
49 draw_gs_get_input_index(int semantic, int index,
50 const struct tgsi_shader_info *input_info)
51 {
52 int i;
53 const ubyte *input_semantic_names = input_info->output_semantic_name;
54 const ubyte *input_semantic_indices = input_info->output_semantic_index;
55 for (i = 0; i < PIPE_MAX_SHADER_OUTPUTS; i++) {
56 if (input_semantic_names[i] == semantic &&
57 input_semantic_indices[i] == index)
58 return i;
59 }
60 return -1;
61 }
62
63 /**
64 * We execute geometry shaders in the SOA mode, so ideally we want to
65 * flush when the number of currently fetched primitives is equal to
66 * the number of elements in the SOA vector. This ensures that the
67 * throughput is optimized for the given vector instrunction set.
68 */
69 static INLINE boolean
70 draw_gs_should_flush(struct draw_geometry_shader *shader)
71 {
72 return (shader->fetched_prim_count == shader->vector_length);
73 }
74
75 /*#define DEBUG_OUTPUTS 1*/
76 static void
77 tgsi_fetch_gs_outputs(struct draw_geometry_shader *shader,
78 unsigned num_primitives,
79 float (**p_output)[4])
80 {
81 struct tgsi_exec_machine *machine = shader->machine;
82 unsigned prim_idx, j, slot;
83 unsigned current_idx = 0;
84 float (*output)[4];
85
86 output = *p_output;
87
88 /* Unswizzle all output results.
89 */
90
91 for (prim_idx = 0; prim_idx < num_primitives; ++prim_idx) {
92 unsigned num_verts_per_prim = machine->Primitives[prim_idx];
93 shader->primitive_lengths[prim_idx + shader->emitted_primitives] =
94 machine->Primitives[prim_idx];
95 shader->emitted_vertices += num_verts_per_prim;
96 for (j = 0; j < num_verts_per_prim; j++, current_idx++) {
97 int idx = current_idx * shader->info.num_outputs;
98 #ifdef DEBUG_OUTPUTS
99 debug_printf("%d) Output vert:\n", idx / shader->info.num_outputs);
100 #endif
101 for (slot = 0; slot < shader->info.num_outputs; slot++) {
102 output[slot][0] = machine->Outputs[idx + slot].xyzw[0].f[0];
103 output[slot][1] = machine->Outputs[idx + slot].xyzw[1].f[0];
104 output[slot][2] = machine->Outputs[idx + slot].xyzw[2].f[0];
105 output[slot][3] = machine->Outputs[idx + slot].xyzw[3].f[0];
106 #ifdef DEBUG_OUTPUTS
107 debug_printf("\t%d: %f %f %f %f\n", slot,
108 output[slot][0],
109 output[slot][1],
110 output[slot][2],
111 output[slot][3]);
112 #endif
113 debug_assert(!util_is_inf_or_nan(output[slot][0]));
114 }
115 output = (float (*)[4])((char *)output + shader->vertex_size);
116 }
117 }
118 *p_output = output;
119 shader->emitted_primitives += num_primitives;
120 }
121
122 /*#define DEBUG_INPUTS 1*/
123 static void tgsi_fetch_gs_input(struct draw_geometry_shader *shader,
124 unsigned *indices,
125 unsigned num_vertices,
126 unsigned prim_idx)
127 {
128 struct tgsi_exec_machine *machine = shader->machine;
129 unsigned slot, vs_slot, i;
130 unsigned input_vertex_stride = shader->input_vertex_stride;
131 const float (*input_ptr)[4];
132
133 input_ptr = shader->input;
134
135 for (i = 0; i < num_vertices; ++i) {
136 const float (*input)[4];
137 #if DEBUG_INPUTS
138 debug_printf("%d) vertex index = %d (prim idx = %d)\n",
139 i, indices[i], prim_idx);
140 #endif
141 input = (const float (*)[4])(
142 (const char *)input_ptr + (indices[i] * input_vertex_stride));
143 for (slot = 0, vs_slot = 0; slot < shader->info.num_inputs; ++slot) {
144 unsigned idx = i * TGSI_EXEC_MAX_INPUT_ATTRIBS + slot;
145 if (shader->info.input_semantic_name[slot] == TGSI_SEMANTIC_PRIMID) {
146 machine->Inputs[idx].xyzw[0].f[prim_idx] =
147 (float)shader->in_prim_idx;
148 machine->Inputs[idx].xyzw[1].f[prim_idx] =
149 (float)shader->in_prim_idx;
150 machine->Inputs[idx].xyzw[2].f[prim_idx] =
151 (float)shader->in_prim_idx;
152 machine->Inputs[idx].xyzw[3].f[prim_idx] =
153 (float)shader->in_prim_idx;
154 } else {
155 vs_slot = draw_gs_get_input_index(
156 shader->info.input_semantic_name[slot],
157 shader->info.input_semantic_index[slot],
158 shader->input_info);
159 if (vs_slot < 0) {
160 debug_printf("VS/GS signature mismatch!\n");
161 machine->Inputs[idx].xyzw[0].f[prim_idx] = 0;
162 machine->Inputs[idx].xyzw[1].f[prim_idx] = 0;
163 machine->Inputs[idx].xyzw[2].f[prim_idx] = 0;
164 machine->Inputs[idx].xyzw[3].f[prim_idx] = 0;
165 } else {
166 #if DEBUG_INPUTS
167 debug_printf("\tSlot = %d, vs_slot = %d, idx = %d:\n",
168 slot, vs_slot, idx);
169 assert(!util_is_inf_or_nan(input[vs_slot][0]));
170 assert(!util_is_inf_or_nan(input[vs_slot][1]));
171 assert(!util_is_inf_or_nan(input[vs_slot][2]));
172 assert(!util_is_inf_or_nan(input[vs_slot][3]));
173 #endif
174 machine->Inputs[idx].xyzw[0].f[prim_idx] = input[vs_slot][0];
175 machine->Inputs[idx].xyzw[1].f[prim_idx] = input[vs_slot][1];
176 machine->Inputs[idx].xyzw[2].f[prim_idx] = input[vs_slot][2];
177 machine->Inputs[idx].xyzw[3].f[prim_idx] = input[vs_slot][3];
178 #if DEBUG_INPUTS
179 debug_printf("\t\t%f %f %f %f\n",
180 machine->Inputs[idx].xyzw[0].f[prim_idx],
181 machine->Inputs[idx].xyzw[1].f[prim_idx],
182 machine->Inputs[idx].xyzw[2].f[prim_idx],
183 machine->Inputs[idx].xyzw[3].f[prim_idx]);
184 #endif
185 ++vs_slot;
186 }
187 }
188 }
189 }
190 }
191
192 static void tgsi_gs_prepare(struct draw_geometry_shader *shader,
193 const void *constants[PIPE_MAX_CONSTANT_BUFFERS],
194 const unsigned constants_size[PIPE_MAX_CONSTANT_BUFFERS])
195 {
196 struct tgsi_exec_machine *machine = shader->machine;
197
198 tgsi_exec_set_constant_buffers(machine, PIPE_MAX_CONSTANT_BUFFERS,
199 constants, constants_size);
200 }
201
202 static unsigned tgsi_gs_run(struct draw_geometry_shader *shader,
203 unsigned input_primitives)
204 {
205 struct tgsi_exec_machine *machine = shader->machine;
206
207 tgsi_set_exec_mask(machine,
208 1,
209 input_primitives > 1,
210 input_primitives > 2,
211 input_primitives > 3);
212
213 /* run interpreter */
214 tgsi_exec_machine_run(machine);
215
216 return
217 machine->Temps[TGSI_EXEC_TEMP_PRIMITIVE_I].xyzw[TGSI_EXEC_TEMP_PRIMITIVE_C].u[0];
218 }
219
220 #ifdef HAVE_LLVM
221
222 static void
223 llvm_fetch_gs_input(struct draw_geometry_shader *shader,
224 unsigned *indices,
225 unsigned num_vertices,
226 unsigned prim_idx)
227 {
228 unsigned slot, vs_slot, i;
229 unsigned input_vertex_stride = shader->input_vertex_stride;
230 const float (*input_ptr)[4];
231 float (*input_data)[6][PIPE_MAX_SHADER_INPUTS][TGSI_NUM_CHANNELS][TGSI_NUM_CHANNELS] = &shader->gs_input->data;
232
233 shader->llvm_prim_ids[shader->fetched_prim_count] =
234 shader->in_prim_idx;
235
236 input_ptr = shader->input;
237
238 for (i = 0; i < num_vertices; ++i) {
239 const float (*input)[4];
240 #if DEBUG_INPUTS
241 debug_printf("%d) vertex index = %d (prim idx = %d)\n",
242 i, indices[i], prim_idx);
243 #endif
244 input = (const float (*)[4])(
245 (const char *)input_ptr + (indices[i] * input_vertex_stride));
246 for (slot = 0, vs_slot = 0; slot < shader->info.num_inputs; ++slot) {
247 if (shader->info.input_semantic_name[slot] == TGSI_SEMANTIC_PRIMID) {
248 /* skip. we handle system values through gallivm */
249 } else {
250 vs_slot = draw_gs_get_input_index(
251 shader->info.input_semantic_name[slot],
252 shader->info.input_semantic_index[slot],
253 shader->input_info);
254 if (vs_slot < 0) {
255 debug_printf("VS/GS signature mismatch!\n");
256 (*input_data)[i][slot][0][prim_idx] = 0;
257 (*input_data)[i][slot][1][prim_idx] = 0;
258 (*input_data)[i][slot][2][prim_idx] = 0;
259 (*input_data)[i][slot][3][prim_idx] = 0;
260 } else {
261 #if DEBUG_INPUTS
262 debug_printf("\tSlot = %d, vs_slot = %d, i = %d:\n",
263 slot, vs_slot, i);
264 assert(!util_is_inf_or_nan(input[vs_slot][0]));
265 assert(!util_is_inf_or_nan(input[vs_slot][1]));
266 assert(!util_is_inf_or_nan(input[vs_slot][2]));
267 assert(!util_is_inf_or_nan(input[vs_slot][3]));
268 #endif
269 (*input_data)[i][slot][0][prim_idx] = input[vs_slot][0];
270 (*input_data)[i][slot][1][prim_idx] = input[vs_slot][1];
271 (*input_data)[i][slot][2][prim_idx] = input[vs_slot][2];
272 (*input_data)[i][slot][3][prim_idx] = input[vs_slot][3];
273 #if DEBUG_INPUTS
274 debug_printf("\t\t%f %f %f %f\n",
275 (*input_data)[i][slot][0][prim_idx],
276 (*input_data)[i][slot][1][prim_idx],
277 (*input_data)[i][slot][2][prim_idx],
278 (*input_data)[i][slot][3][prim_idx]);
279 #endif
280 ++vs_slot;
281 }
282 }
283 }
284 }
285 }
286
287 static void
288 llvm_fetch_gs_outputs(struct draw_geometry_shader *shader,
289 unsigned num_primitives,
290 float (**p_output)[4])
291 {
292 int total_verts = 0;
293 int vertex_count = 0;
294 int total_prims = 0;
295 int max_prims_per_invocation = 0;
296 char *output_ptr = (char*)shader->gs_output;
297 int i, j, prim_idx;
298 unsigned next_prim_boundary = shader->primitive_boundary;
299
300 for (i = 0; i < shader->vector_length; ++i) {
301 int prims = shader->llvm_emitted_primitives[i];
302 total_prims += prims;
303 max_prims_per_invocation = MAX2(max_prims_per_invocation, prims);
304 }
305 for (i = 0; i < shader->vector_length; ++i) {
306 total_verts += shader->llvm_emitted_vertices[i];
307 }
308
309 output_ptr += shader->emitted_vertices * shader->vertex_size;
310 for (i = 0; i < shader->vector_length - 1; ++i) {
311 int current_verts = shader->llvm_emitted_vertices[i];
312 int next_verts = shader->llvm_emitted_vertices[i + 1];
313 #if 0
314 int j;
315 for (j = 0; j < current_verts; ++j) {
316 struct vertex_header *vh = (struct vertex_header *)
317 (output_ptr + shader->vertex_size * (i * next_prim_boundary + j));
318 debug_printf("--- %d) [%f, %f, %f, %f]\n", j + vertex_count,
319 vh->data[0][0], vh->data[0][1], vh->data[0][2], vh->data[0][3]);
320
321 }
322 #endif
323 debug_assert(current_verts <= shader->max_output_vertices);
324 debug_assert(next_verts <= shader->max_output_vertices);
325 if (next_verts) {
326 memmove(output_ptr + (vertex_count + current_verts) * shader->vertex_size,
327 output_ptr + ((i + 1) * next_prim_boundary) * shader->vertex_size,
328 shader->vertex_size * next_verts);
329 }
330 vertex_count += current_verts;
331 }
332
333 #if 0
334 {
335 int i;
336 for (i = 0; i < total_verts; ++i) {
337 struct vertex_header *vh = (struct vertex_header *)(output_ptr + shader->vertex_size * i);
338 debug_printf("%d) [%f, %f, %f, %f]\n", i,
339 vh->data[0][0], vh->data[0][1], vh->data[0][2], vh->data[0][3]);
340
341 }
342 }
343 #endif
344
345 prim_idx = 0;
346 for (i = 0; i < shader->vector_length; ++i) {
347 int num_prims = shader->llvm_emitted_primitives[i];
348 for (j = 0; j < num_prims; ++j) {
349 int prim_length =
350 shader->llvm_prim_lengths[j][i];
351 shader->primitive_lengths[shader->emitted_primitives + prim_idx] =
352 prim_length;
353 ++prim_idx;
354 }
355 }
356
357 shader->emitted_primitives += total_prims;
358 shader->emitted_vertices += total_verts;
359 }
360
361 static void
362 llvm_gs_prepare(struct draw_geometry_shader *shader,
363 const void *constants[PIPE_MAX_CONSTANT_BUFFERS],
364 const unsigned constants_size[PIPE_MAX_CONSTANT_BUFFERS])
365 {
366 }
367
368 static unsigned
369 llvm_gs_run(struct draw_geometry_shader *shader,
370 unsigned input_primitives)
371 {
372 unsigned ret;
373 char *input = (char*)shader->gs_output;
374
375 input += (shader->emitted_vertices * shader->vertex_size);
376
377 ret = shader->current_variant->jit_func(
378 shader->jit_context, shader->gs_input->data,
379 (struct vertex_header*)input,
380 input_primitives,
381 shader->draw->instance_id,
382 shader->llvm_prim_ids);
383
384 return ret;
385 }
386
387 #endif
388
389 static void gs_flush(struct draw_geometry_shader *shader)
390 {
391 unsigned out_prim_count;
392
393 unsigned input_primitives = shader->fetched_prim_count;
394
395 if (shader->draw->collect_statistics) {
396 shader->draw->statistics.gs_invocations += input_primitives;
397 }
398
399 debug_assert(input_primitives > 0 &&
400 input_primitives <= 4);
401
402 out_prim_count = shader->run(shader, input_primitives);
403 shader->fetch_outputs(shader, out_prim_count,
404 &shader->tmp_output);
405
406 #if 0
407 debug_printf("PRIM emitted prims = %d (verts=%d), cur prim count = %d\n",
408 shader->emitted_primitives, shader->emitted_vertices,
409 out_prim_count);
410 #endif
411
412 shader->fetched_prim_count = 0;
413 }
414
415 static void gs_point(struct draw_geometry_shader *shader,
416 int idx)
417 {
418 unsigned indices[1];
419
420 indices[0] = idx;
421
422 shader->fetch_inputs(shader, indices, 1,
423 shader->fetched_prim_count);
424 ++shader->in_prim_idx;
425 ++shader->fetched_prim_count;
426
427 if (draw_gs_should_flush(shader))
428 gs_flush(shader);
429 }
430
431 static void gs_line(struct draw_geometry_shader *shader,
432 int i0, int i1)
433 {
434 unsigned indices[2];
435
436 indices[0] = i0;
437 indices[1] = i1;
438
439 shader->fetch_inputs(shader, indices, 2,
440 shader->fetched_prim_count);
441 ++shader->in_prim_idx;
442 ++shader->fetched_prim_count;
443
444 if (draw_gs_should_flush(shader))
445 gs_flush(shader);
446 }
447
448 static void gs_line_adj(struct draw_geometry_shader *shader,
449 int i0, int i1, int i2, int i3)
450 {
451 unsigned indices[4];
452
453 indices[0] = i0;
454 indices[1] = i1;
455 indices[2] = i2;
456 indices[3] = i3;
457
458 shader->fetch_inputs(shader, indices, 4,
459 shader->fetched_prim_count);
460 ++shader->in_prim_idx;
461 ++shader->fetched_prim_count;
462
463 if (draw_gs_should_flush(shader))
464 gs_flush(shader);
465 }
466
467 static void gs_tri(struct draw_geometry_shader *shader,
468 int i0, int i1, int i2)
469 {
470 unsigned indices[3];
471
472 indices[0] = i0;
473 indices[1] = i1;
474 indices[2] = i2;
475
476 shader->fetch_inputs(shader, indices, 3,
477 shader->fetched_prim_count);
478 ++shader->in_prim_idx;
479 ++shader->fetched_prim_count;
480
481 if (draw_gs_should_flush(shader))
482 gs_flush(shader);
483 }
484
485 static void gs_tri_adj(struct draw_geometry_shader *shader,
486 int i0, int i1, int i2,
487 int i3, int i4, int i5)
488 {
489 unsigned indices[6];
490
491 indices[0] = i0;
492 indices[1] = i1;
493 indices[2] = i2;
494 indices[3] = i3;
495 indices[4] = i4;
496 indices[5] = i5;
497
498 shader->fetch_inputs(shader, indices, 6,
499 shader->fetched_prim_count);
500 ++shader->in_prim_idx;
501 ++shader->fetched_prim_count;
502
503 if (draw_gs_should_flush(shader))
504 gs_flush(shader);
505 }
506
507 #define FUNC gs_run
508 #define GET_ELT(idx) (idx)
509 #include "draw_gs_tmp.h"
510
511
512 #define FUNC gs_run_elts
513 #define LOCAL_VARS const ushort *elts = input_prims->elts;
514 #define GET_ELT(idx) (elts[idx])
515 #include "draw_gs_tmp.h"
516
517
518 /**
519 * Execute geometry shader.
520 */
521 int draw_geometry_shader_run(struct draw_geometry_shader *shader,
522 const void *constants[PIPE_MAX_CONSTANT_BUFFERS],
523 const unsigned constants_size[PIPE_MAX_CONSTANT_BUFFERS],
524 const struct draw_vertex_info *input_verts,
525 const struct draw_prim_info *input_prim,
526 const struct tgsi_shader_info *input_info,
527 struct draw_vertex_info *output_verts,
528 struct draw_prim_info *output_prims )
529 {
530 const float (*input)[4] = (const float (*)[4])input_verts->verts->data;
531 unsigned input_stride = input_verts->vertex_size;
532 unsigned num_outputs = shader->info.num_outputs;
533 unsigned vertex_size = sizeof(struct vertex_header) + num_outputs * 4 * sizeof(float);
534 unsigned num_input_verts = input_prim->linear ?
535 input_verts->count :
536 input_prim->count;
537 unsigned num_in_primitives =
538 align(
539 MAX2(u_decomposed_prims_for_vertices(input_prim->prim,
540 num_input_verts),
541 u_decomposed_prims_for_vertices(shader->input_primitive,
542 num_input_verts)),
543 shader->vector_length);
544 unsigned max_out_prims =
545 u_decomposed_prims_for_vertices(shader->output_primitive,
546 shader->max_output_vertices)
547 * num_in_primitives;
548
549 //Assume at least one primitive
550 max_out_prims = MAX2(max_out_prims, 1);
551
552
553 output_verts->vertex_size = vertex_size;
554 output_verts->stride = output_verts->vertex_size;
555 /* we allocate exactly one extra vertex per primitive to allow the GS to emit
556 * overflown vertices into some area where they won't harm anyone */
557 output_verts->verts =
558 (struct vertex_header *)MALLOC(output_verts->vertex_size *
559 max_out_prims *
560 shader->primitive_boundary);
561
562 #if 0
563 debug_printf("%s count = %d (in prims # = %d)\n",
564 __FUNCTION__, num_input_verts, num_in_primitives);
565 debug_printf("\tlinear = %d, prim_info->count = %d\n",
566 input_prim->linear, input_prim->count);
567 debug_printf("\tprim pipe = %s, shader in = %s, shader out = %s, max out = %d\n",
568 u_prim_name(input_prim->prim),
569 u_prim_name(shader->input_primitive),
570 u_prim_name(shader->output_primitive),
571 shader->max_output_vertices);
572 #endif
573
574 shader->emitted_vertices = 0;
575 shader->emitted_primitives = 0;
576 shader->vertex_size = vertex_size;
577 shader->tmp_output = (float (*)[4])output_verts->verts->data;
578 shader->fetched_prim_count = 0;
579 shader->input_vertex_stride = input_stride;
580 shader->input = input;
581 shader->input_info = input_info;
582 FREE(shader->primitive_lengths);
583 shader->primitive_lengths = MALLOC(max_out_prims * sizeof(unsigned));
584
585
586 #ifdef HAVE_LLVM
587 if (draw_get_option_use_llvm()) {
588 shader->gs_output = output_verts->verts;
589 if (max_out_prims > shader->max_out_prims) {
590 unsigned i;
591 if (shader->llvm_prim_lengths) {
592 for (i = 0; i < shader->max_out_prims; ++i) {
593 align_free(shader->llvm_prim_lengths[i]);
594 }
595 FREE(shader->llvm_prim_lengths);
596 }
597
598 shader->llvm_prim_lengths = MALLOC(max_out_prims * sizeof(unsigned*));
599 for (i = 0; i < max_out_prims; ++i) {
600 int vector_size = shader->vector_length * sizeof(unsigned);
601 shader->llvm_prim_lengths[i] =
602 align_malloc(vector_size, vector_size);
603 }
604
605 shader->max_out_prims = max_out_prims;
606 }
607 shader->jit_context->prim_lengths = shader->llvm_prim_lengths;
608 shader->jit_context->emitted_vertices = shader->llvm_emitted_vertices;
609 shader->jit_context->emitted_prims = shader->llvm_emitted_primitives;
610 }
611 #endif
612
613 shader->prepare(shader, constants, constants_size);
614
615 if (input_prim->linear)
616 gs_run(shader, input_prim, input_verts,
617 output_prims, output_verts);
618 else
619 gs_run_elts(shader, input_prim, input_verts,
620 output_prims, output_verts);
621
622 /* Flush the remaining primitives. Will happen if
623 * num_input_primitives % 4 != 0
624 */
625 if (shader->fetched_prim_count > 0) {
626 gs_flush(shader);
627 }
628
629 debug_assert(shader->fetched_prim_count == 0);
630
631 /* Update prim_info:
632 */
633 output_prims->linear = TRUE;
634 output_prims->elts = NULL;
635 output_prims->start = 0;
636 output_prims->count = shader->emitted_vertices;
637 output_prims->prim = shader->output_primitive;
638 output_prims->flags = 0x0;
639 output_prims->primitive_lengths = shader->primitive_lengths;
640 output_prims->primitive_count = shader->emitted_primitives;
641 output_verts->count = shader->emitted_vertices;
642
643 if (shader->draw->collect_statistics) {
644 unsigned i;
645 for (i = 0; i < shader->emitted_primitives; ++i) {
646 shader->draw->statistics.gs_primitives +=
647 u_decomposed_prims_for_vertices(shader->output_primitive,
648 shader->primitive_lengths[i]);
649 }
650 }
651
652 #if 0
653 debug_printf("GS finished, prims = %d, verts = %d\n",
654 output_prims->primitive_count,
655 output_verts->count);
656 #endif
657
658 return shader->emitted_vertices;
659 }
660
661 void draw_geometry_shader_prepare(struct draw_geometry_shader *shader,
662 struct draw_context *draw)
663 {
664 if (shader && shader->machine->Tokens != shader->state.tokens) {
665 tgsi_exec_machine_bind_shader(shader->machine,
666 shader->state.tokens,
667 draw->gs.tgsi.sampler);
668 }
669 }
670
671
672 boolean
673 draw_gs_init( struct draw_context *draw )
674 {
675 draw->gs.tgsi.machine = tgsi_exec_machine_create();
676 if (!draw->gs.tgsi.machine)
677 return FALSE;
678
679 draw->gs.tgsi.machine->Primitives = align_malloc(
680 MAX_PRIMITIVES * sizeof(struct tgsi_exec_vector), 16);
681 if (!draw->gs.tgsi.machine->Primitives)
682 return FALSE;
683 memset(draw->gs.tgsi.machine->Primitives, 0,
684 MAX_PRIMITIVES * sizeof(struct tgsi_exec_vector));
685
686 return TRUE;
687 }
688
689 void draw_gs_destroy( struct draw_context *draw )
690 {
691 if (draw->gs.tgsi.machine) {
692 align_free(draw->gs.tgsi.machine->Primitives);
693 tgsi_exec_machine_destroy(draw->gs.tgsi.machine);
694 }
695 }
696
697 struct draw_geometry_shader *
698 draw_create_geometry_shader(struct draw_context *draw,
699 const struct pipe_shader_state *state)
700 {
701 #ifdef HAVE_LLVM
702 boolean use_llvm = draw_get_option_use_llvm();
703 struct llvm_geometry_shader *llvm_gs;
704 #endif
705 struct draw_geometry_shader *gs;
706 unsigned i;
707
708 #ifdef HAVE_LLVM
709 if (use_llvm) {
710 llvm_gs = CALLOC_STRUCT(llvm_geometry_shader);
711
712 if (llvm_gs == NULL)
713 return NULL;
714
715 gs = &llvm_gs->base;
716
717 make_empty_list(&llvm_gs->variants);
718 } else
719 #endif
720 {
721 gs = CALLOC_STRUCT(draw_geometry_shader);
722 }
723
724 if (!gs)
725 return NULL;
726
727 gs->draw = draw;
728 gs->state = *state;
729 gs->state.tokens = tgsi_dup_tokens(state->tokens);
730 if (!gs->state.tokens) {
731 FREE(gs);
732 return NULL;
733 }
734
735 tgsi_scan_shader(state->tokens, &gs->info);
736
737 /* setup the defaults */
738 gs->input_primitive = PIPE_PRIM_TRIANGLES;
739 gs->output_primitive = PIPE_PRIM_TRIANGLE_STRIP;
740 gs->max_output_vertices = 32;
741 gs->max_out_prims = 0;
742
743 #ifdef HAVE_LLVM
744 if (use_llvm) {
745 /* TODO: change the input array to handle the following
746 vector length, instead of the currently hardcoded
747 TGSI_NUM_CHANNELS
748 gs->vector_length = lp_native_vector_width / 32;*/
749 gs->vector_length = TGSI_NUM_CHANNELS;
750 } else
751 #endif
752 {
753 gs->vector_length = 1;
754 }
755
756 for (i = 0; i < gs->info.num_properties; ++i) {
757 if (gs->info.properties[i].name ==
758 TGSI_PROPERTY_GS_INPUT_PRIM)
759 gs->input_primitive = gs->info.properties[i].data[0];
760 else if (gs->info.properties[i].name ==
761 TGSI_PROPERTY_GS_OUTPUT_PRIM)
762 gs->output_primitive = gs->info.properties[i].data[0];
763 else if (gs->info.properties[i].name ==
764 TGSI_PROPERTY_GS_MAX_OUTPUT_VERTICES)
765 gs->max_output_vertices = gs->info.properties[i].data[0];
766 }
767 /* Primitive boundary is bigger than max_output_vertices by one, because
768 * the specification says that the geometry shader should exit if the
769 * number of emitted vertices is bigger or equal to max_output_vertices and
770 * we can't do that because we're running in the SoA mode, which means that
771 * our storing routines will keep getting called on channels that have
772 * overflown.
773 * So we need some scratch area where we can keep writing the overflown
774 * vertices without overwriting anything important or crashing.
775 */
776 gs->primitive_boundary = gs->max_output_vertices + 1;
777
778 for (i = 0; i < gs->info.num_outputs; i++) {
779 if (gs->info.output_semantic_name[i] == TGSI_SEMANTIC_POSITION &&
780 gs->info.output_semantic_index[i] == 0)
781 gs->position_output = i;
782 }
783
784 gs->machine = draw->gs.tgsi.machine;
785
786 #ifdef HAVE_LLVM
787 if (use_llvm) {
788 int vector_size = gs->vector_length * sizeof(float);
789 gs->gs_input = align_malloc(sizeof(struct draw_gs_inputs), 16);
790 memset(gs->gs_input, 0, sizeof(struct draw_gs_inputs));
791 gs->llvm_prim_lengths = 0;
792
793 gs->llvm_emitted_primitives = align_malloc(vector_size, vector_size);
794 gs->llvm_emitted_vertices = align_malloc(vector_size, vector_size);
795 gs->llvm_prim_ids = align_malloc(vector_size, vector_size);
796
797 gs->fetch_outputs = llvm_fetch_gs_outputs;
798 gs->fetch_inputs = llvm_fetch_gs_input;
799 gs->prepare = llvm_gs_prepare;
800 gs->run = llvm_gs_run;
801
802 gs->jit_context = &draw->llvm->gs_jit_context;
803
804
805 llvm_gs->variant_key_size =
806 draw_gs_llvm_variant_key_size(
807 MAX2(gs->info.file_max[TGSI_FILE_SAMPLER]+1,
808 gs->info.file_max[TGSI_FILE_SAMPLER_VIEW]+1));
809 } else
810 #endif
811 {
812 gs->fetch_outputs = tgsi_fetch_gs_outputs;
813 gs->fetch_inputs = tgsi_fetch_gs_input;
814 gs->prepare = tgsi_gs_prepare;
815 gs->run = tgsi_gs_run;
816 }
817
818 return gs;
819 }
820
821 void draw_bind_geometry_shader(struct draw_context *draw,
822 struct draw_geometry_shader *dgs)
823 {
824 draw_do_flush(draw, DRAW_FLUSH_STATE_CHANGE);
825
826 if (dgs) {
827 draw->gs.geometry_shader = dgs;
828 draw->gs.num_gs_outputs = dgs->info.num_outputs;
829 draw->gs.position_output = dgs->position_output;
830 draw_geometry_shader_prepare(dgs, draw);
831 }
832 else {
833 draw->gs.geometry_shader = NULL;
834 draw->gs.num_gs_outputs = 0;
835 }
836 }
837
838 void draw_delete_geometry_shader(struct draw_context *draw,
839 struct draw_geometry_shader *dgs)
840 {
841 if (!dgs) {
842 return;
843 }
844 #ifdef HAVE_LLVM
845 if (draw_get_option_use_llvm()) {
846 struct llvm_geometry_shader *shader = llvm_geometry_shader(dgs);
847 struct draw_gs_llvm_variant_list_item *li;
848
849 li = first_elem(&shader->variants);
850 while(!at_end(&shader->variants, li)) {
851 struct draw_gs_llvm_variant_list_item *next = next_elem(li);
852 draw_gs_llvm_destroy_variant(li->base);
853 li = next;
854 }
855
856 assert(shader->variants_cached == 0);
857
858 if (dgs->llvm_prim_lengths) {
859 unsigned i;
860 for (i = 0; i < dgs->max_out_prims; ++i) {
861 align_free(dgs->llvm_prim_lengths[i]);
862 }
863 FREE(dgs->llvm_prim_lengths);
864 }
865 align_free(dgs->llvm_emitted_primitives);
866 align_free(dgs->llvm_emitted_vertices);
867 align_free(dgs->llvm_prim_ids);
868
869 align_free(dgs->gs_input);
870 }
871 #endif
872
873 FREE(dgs->primitive_lengths);
874 FREE((void*) dgs->state.tokens);
875 FREE(dgs);
876 }
877
878
879 #ifdef HAVE_LLVM
880 void draw_gs_set_current_variant(struct draw_geometry_shader *shader,
881 struct draw_gs_llvm_variant *variant)
882 {
883 shader->current_variant = variant;
884 }
885 #endif
886
887 /*
888 * Called at the very begin of the draw call with a new instance
889 * Used to reset state that should persist between primitive restart.
890 */
891 void
892 draw_geometry_shader_new_instance(struct draw_geometry_shader *gs)
893 {
894 if (!gs)
895 return;
896
897 gs->in_prim_idx = 0;
898 }