draw: do not use draw_get_option_use_llvm() inside draw execution paths
[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 VMWARE 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, i;
130 int vs_slot;
131 unsigned input_vertex_stride = shader->input_vertex_stride;
132 const float (*input_ptr)[4];
133
134 input_ptr = shader->input;
135
136 for (i = 0; i < num_vertices; ++i) {
137 const float (*input)[4];
138 #if DEBUG_INPUTS
139 debug_printf("%d) vertex index = %d (prim idx = %d)\n",
140 i, indices[i], prim_idx);
141 #endif
142 input = (const float (*)[4])(
143 (const char *)input_ptr + (indices[i] * input_vertex_stride));
144 for (slot = 0, vs_slot = 0; slot < shader->info.num_inputs; ++slot) {
145 unsigned idx = i * TGSI_EXEC_MAX_INPUT_ATTRIBS + slot;
146 if (shader->info.input_semantic_name[slot] == TGSI_SEMANTIC_PRIMID) {
147 machine->Inputs[idx].xyzw[0].f[prim_idx] =
148 (float)shader->in_prim_idx;
149 machine->Inputs[idx].xyzw[1].f[prim_idx] =
150 (float)shader->in_prim_idx;
151 machine->Inputs[idx].xyzw[2].f[prim_idx] =
152 (float)shader->in_prim_idx;
153 machine->Inputs[idx].xyzw[3].f[prim_idx] =
154 (float)shader->in_prim_idx;
155 } else {
156 vs_slot = draw_gs_get_input_index(
157 shader->info.input_semantic_name[slot],
158 shader->info.input_semantic_index[slot],
159 shader->input_info);
160 if (vs_slot < 0) {
161 debug_printf("VS/GS signature mismatch!\n");
162 machine->Inputs[idx].xyzw[0].f[prim_idx] = 0;
163 machine->Inputs[idx].xyzw[1].f[prim_idx] = 0;
164 machine->Inputs[idx].xyzw[2].f[prim_idx] = 0;
165 machine->Inputs[idx].xyzw[3].f[prim_idx] = 0;
166 } else {
167 #if DEBUG_INPUTS
168 debug_printf("\tSlot = %d, vs_slot = %d, idx = %d:\n",
169 slot, vs_slot, idx);
170 assert(!util_is_inf_or_nan(input[vs_slot][0]));
171 assert(!util_is_inf_or_nan(input[vs_slot][1]));
172 assert(!util_is_inf_or_nan(input[vs_slot][2]));
173 assert(!util_is_inf_or_nan(input[vs_slot][3]));
174 #endif
175 machine->Inputs[idx].xyzw[0].f[prim_idx] = input[vs_slot][0];
176 machine->Inputs[idx].xyzw[1].f[prim_idx] = input[vs_slot][1];
177 machine->Inputs[idx].xyzw[2].f[prim_idx] = input[vs_slot][2];
178 machine->Inputs[idx].xyzw[3].f[prim_idx] = input[vs_slot][3];
179 #if DEBUG_INPUTS
180 debug_printf("\t\t%f %f %f %f\n",
181 machine->Inputs[idx].xyzw[0].f[prim_idx],
182 machine->Inputs[idx].xyzw[1].f[prim_idx],
183 machine->Inputs[idx].xyzw[2].f[prim_idx],
184 machine->Inputs[idx].xyzw[3].f[prim_idx]);
185 #endif
186 ++vs_slot;
187 }
188 }
189 }
190 }
191 }
192
193 static void tgsi_gs_prepare(struct draw_geometry_shader *shader,
194 const void *constants[PIPE_MAX_CONSTANT_BUFFERS],
195 const unsigned constants_size[PIPE_MAX_CONSTANT_BUFFERS])
196 {
197 struct tgsi_exec_machine *machine = shader->machine;
198
199 tgsi_exec_set_constant_buffers(machine, PIPE_MAX_CONSTANT_BUFFERS,
200 constants, constants_size);
201 }
202
203 static unsigned tgsi_gs_run(struct draw_geometry_shader *shader,
204 unsigned input_primitives)
205 {
206 struct tgsi_exec_machine *machine = shader->machine;
207
208 tgsi_set_exec_mask(machine,
209 1,
210 input_primitives > 1,
211 input_primitives > 2,
212 input_primitives > 3);
213
214 /* run interpreter */
215 tgsi_exec_machine_run(machine);
216
217 return
218 machine->Temps[TGSI_EXEC_TEMP_PRIMITIVE_I].xyzw[TGSI_EXEC_TEMP_PRIMITIVE_C].u[0];
219 }
220
221 #ifdef HAVE_LLVM
222
223 static void
224 llvm_fetch_gs_input(struct draw_geometry_shader *shader,
225 unsigned *indices,
226 unsigned num_vertices,
227 unsigned prim_idx)
228 {
229 unsigned slot, i;
230 int vs_slot;
231 unsigned input_vertex_stride = shader->input_vertex_stride;
232 const float (*input_ptr)[4];
233 float (*input_data)[6][PIPE_MAX_SHADER_INPUTS][TGSI_NUM_CHANNELS][TGSI_NUM_CHANNELS] = &shader->gs_input->data;
234
235 shader->llvm_prim_ids[shader->fetched_prim_count] =
236 shader->in_prim_idx;
237
238 input_ptr = shader->input;
239
240 for (i = 0; i < num_vertices; ++i) {
241 const float (*input)[4];
242 #if DEBUG_INPUTS
243 debug_printf("%d) vertex index = %d (prim idx = %d)\n",
244 i, indices[i], prim_idx);
245 #endif
246 input = (const float (*)[4])(
247 (const char *)input_ptr + (indices[i] * input_vertex_stride));
248 for (slot = 0, vs_slot = 0; slot < shader->info.num_inputs; ++slot) {
249 if (shader->info.input_semantic_name[slot] == TGSI_SEMANTIC_PRIMID) {
250 /* skip. we handle system values through gallivm */
251 } else {
252 vs_slot = draw_gs_get_input_index(
253 shader->info.input_semantic_name[slot],
254 shader->info.input_semantic_index[slot],
255 shader->input_info);
256 if (vs_slot < 0) {
257 debug_printf("VS/GS signature mismatch!\n");
258 (*input_data)[i][slot][0][prim_idx] = 0;
259 (*input_data)[i][slot][1][prim_idx] = 0;
260 (*input_data)[i][slot][2][prim_idx] = 0;
261 (*input_data)[i][slot][3][prim_idx] = 0;
262 } else {
263 #if DEBUG_INPUTS
264 debug_printf("\tSlot = %d, vs_slot = %d, i = %d:\n",
265 slot, vs_slot, i);
266 assert(!util_is_inf_or_nan(input[vs_slot][0]));
267 assert(!util_is_inf_or_nan(input[vs_slot][1]));
268 assert(!util_is_inf_or_nan(input[vs_slot][2]));
269 assert(!util_is_inf_or_nan(input[vs_slot][3]));
270 #endif
271 (*input_data)[i][slot][0][prim_idx] = input[vs_slot][0];
272 (*input_data)[i][slot][1][prim_idx] = input[vs_slot][1];
273 (*input_data)[i][slot][2][prim_idx] = input[vs_slot][2];
274 (*input_data)[i][slot][3][prim_idx] = input[vs_slot][3];
275 #if DEBUG_INPUTS
276 debug_printf("\t\t%f %f %f %f\n",
277 (*input_data)[i][slot][0][prim_idx],
278 (*input_data)[i][slot][1][prim_idx],
279 (*input_data)[i][slot][2][prim_idx],
280 (*input_data)[i][slot][3][prim_idx]);
281 #endif
282 ++vs_slot;
283 }
284 }
285 }
286 }
287 }
288
289 static void
290 llvm_fetch_gs_outputs(struct draw_geometry_shader *shader,
291 unsigned num_primitives,
292 float (**p_output)[4])
293 {
294 int total_verts = 0;
295 int vertex_count = 0;
296 int total_prims = 0;
297 int max_prims_per_invocation = 0;
298 char *output_ptr = (char*)shader->gs_output;
299 int i, j, prim_idx;
300 unsigned next_prim_boundary = shader->primitive_boundary;
301
302 for (i = 0; i < shader->vector_length; ++i) {
303 int prims = shader->llvm_emitted_primitives[i];
304 total_prims += prims;
305 max_prims_per_invocation = MAX2(max_prims_per_invocation, prims);
306 }
307 for (i = 0; i < shader->vector_length; ++i) {
308 total_verts += shader->llvm_emitted_vertices[i];
309 }
310
311 output_ptr += shader->emitted_vertices * shader->vertex_size;
312 for (i = 0; i < shader->vector_length - 1; ++i) {
313 int current_verts = shader->llvm_emitted_vertices[i];
314 int next_verts = shader->llvm_emitted_vertices[i + 1];
315 #if 0
316 int j;
317 for (j = 0; j < current_verts; ++j) {
318 struct vertex_header *vh = (struct vertex_header *)
319 (output_ptr + shader->vertex_size * (i * next_prim_boundary + j));
320 debug_printf("--- %d) [%f, %f, %f, %f]\n", j + vertex_count,
321 vh->data[0][0], vh->data[0][1], vh->data[0][2], vh->data[0][3]);
322
323 }
324 #endif
325 debug_assert(current_verts <= shader->max_output_vertices);
326 debug_assert(next_verts <= shader->max_output_vertices);
327 if (next_verts) {
328 memmove(output_ptr + (vertex_count + current_verts) * shader->vertex_size,
329 output_ptr + ((i + 1) * next_prim_boundary) * shader->vertex_size,
330 shader->vertex_size * next_verts);
331 }
332 vertex_count += current_verts;
333 }
334
335 #if 0
336 {
337 int i;
338 for (i = 0; i < total_verts; ++i) {
339 struct vertex_header *vh = (struct vertex_header *)(output_ptr + shader->vertex_size * i);
340 debug_printf("%d) Vertex:\n", i);
341 for (j = 0; j < shader->info.num_outputs; ++j) {
342 unsigned *udata = (unsigned*)vh->data[j];
343 debug_printf(" %d) [%f, %f, %f, %f] [%d, %d, %d, %d]\n", j,
344 vh->data[j][0], vh->data[j][1], vh->data[j][2], vh->data[j][3],
345 udata[0], udata[1], udata[2], udata[3]);
346 }
347
348 }
349 }
350 #endif
351
352 prim_idx = 0;
353 for (i = 0; i < shader->vector_length; ++i) {
354 int num_prims = shader->llvm_emitted_primitives[i];
355 for (j = 0; j < num_prims; ++j) {
356 int prim_length =
357 shader->llvm_prim_lengths[j][i];
358 shader->primitive_lengths[shader->emitted_primitives + prim_idx] =
359 prim_length;
360 ++prim_idx;
361 }
362 }
363
364 shader->emitted_primitives += total_prims;
365 shader->emitted_vertices += total_verts;
366 }
367
368 static void
369 llvm_gs_prepare(struct draw_geometry_shader *shader,
370 const void *constants[PIPE_MAX_CONSTANT_BUFFERS],
371 const unsigned constants_size[PIPE_MAX_CONSTANT_BUFFERS])
372 {
373 }
374
375 static unsigned
376 llvm_gs_run(struct draw_geometry_shader *shader,
377 unsigned input_primitives)
378 {
379 unsigned ret;
380 char *input = (char*)shader->gs_output;
381
382 input += (shader->emitted_vertices * shader->vertex_size);
383
384 ret = shader->current_variant->jit_func(
385 shader->jit_context, shader->gs_input->data,
386 (struct vertex_header*)input,
387 input_primitives,
388 shader->draw->instance_id,
389 shader->llvm_prim_ids);
390
391 return ret;
392 }
393
394 #endif
395
396 static void gs_flush(struct draw_geometry_shader *shader)
397 {
398 unsigned out_prim_count;
399
400 unsigned input_primitives = shader->fetched_prim_count;
401
402 if (shader->draw->collect_statistics) {
403 shader->draw->statistics.gs_invocations += input_primitives;
404 }
405
406 debug_assert(input_primitives > 0 &&
407 input_primitives <= 4);
408
409 out_prim_count = shader->run(shader, input_primitives);
410 shader->fetch_outputs(shader, out_prim_count,
411 &shader->tmp_output);
412
413 #if 0
414 debug_printf("PRIM emitted prims = %d (verts=%d), cur prim count = %d\n",
415 shader->emitted_primitives, shader->emitted_vertices,
416 out_prim_count);
417 #endif
418
419 shader->fetched_prim_count = 0;
420 }
421
422 static void gs_point(struct draw_geometry_shader *shader,
423 int idx)
424 {
425 unsigned indices[1];
426
427 indices[0] = idx;
428
429 shader->fetch_inputs(shader, indices, 1,
430 shader->fetched_prim_count);
431 ++shader->in_prim_idx;
432 ++shader->fetched_prim_count;
433
434 if (draw_gs_should_flush(shader))
435 gs_flush(shader);
436 }
437
438 static void gs_line(struct draw_geometry_shader *shader,
439 int i0, int i1)
440 {
441 unsigned indices[2];
442
443 indices[0] = i0;
444 indices[1] = i1;
445
446 shader->fetch_inputs(shader, indices, 2,
447 shader->fetched_prim_count);
448 ++shader->in_prim_idx;
449 ++shader->fetched_prim_count;
450
451 if (draw_gs_should_flush(shader))
452 gs_flush(shader);
453 }
454
455 static void gs_line_adj(struct draw_geometry_shader *shader,
456 int i0, int i1, int i2, int i3)
457 {
458 unsigned indices[4];
459
460 indices[0] = i0;
461 indices[1] = i1;
462 indices[2] = i2;
463 indices[3] = i3;
464
465 shader->fetch_inputs(shader, indices, 4,
466 shader->fetched_prim_count);
467 ++shader->in_prim_idx;
468 ++shader->fetched_prim_count;
469
470 if (draw_gs_should_flush(shader))
471 gs_flush(shader);
472 }
473
474 static void gs_tri(struct draw_geometry_shader *shader,
475 int i0, int i1, int i2)
476 {
477 unsigned indices[3];
478
479 indices[0] = i0;
480 indices[1] = i1;
481 indices[2] = i2;
482
483 shader->fetch_inputs(shader, indices, 3,
484 shader->fetched_prim_count);
485 ++shader->in_prim_idx;
486 ++shader->fetched_prim_count;
487
488 if (draw_gs_should_flush(shader))
489 gs_flush(shader);
490 }
491
492 static void gs_tri_adj(struct draw_geometry_shader *shader,
493 int i0, int i1, int i2,
494 int i3, int i4, int i5)
495 {
496 unsigned indices[6];
497
498 indices[0] = i0;
499 indices[1] = i1;
500 indices[2] = i2;
501 indices[3] = i3;
502 indices[4] = i4;
503 indices[5] = i5;
504
505 shader->fetch_inputs(shader, indices, 6,
506 shader->fetched_prim_count);
507 ++shader->in_prim_idx;
508 ++shader->fetched_prim_count;
509
510 if (draw_gs_should_flush(shader))
511 gs_flush(shader);
512 }
513
514 #define FUNC gs_run
515 #define GET_ELT(idx) (idx)
516 #include "draw_gs_tmp.h"
517
518
519 #define FUNC gs_run_elts
520 #define LOCAL_VARS const ushort *elts = input_prims->elts;
521 #define GET_ELT(idx) (elts[idx])
522 #include "draw_gs_tmp.h"
523
524
525 /**
526 * Execute geometry shader.
527 */
528 int draw_geometry_shader_run(struct draw_geometry_shader *shader,
529 const void *constants[PIPE_MAX_CONSTANT_BUFFERS],
530 const unsigned constants_size[PIPE_MAX_CONSTANT_BUFFERS],
531 const struct draw_vertex_info *input_verts,
532 const struct draw_prim_info *input_prim,
533 const struct tgsi_shader_info *input_info,
534 struct draw_vertex_info *output_verts,
535 struct draw_prim_info *output_prims )
536 {
537 const float (*input)[4] = (const float (*)[4])input_verts->verts->data;
538 unsigned input_stride = input_verts->vertex_size;
539 unsigned num_outputs = draw_total_gs_outputs(shader->draw);
540 unsigned vertex_size = sizeof(struct vertex_header) + num_outputs * 4 * sizeof(float);
541 unsigned num_input_verts = input_prim->linear ?
542 input_verts->count :
543 input_prim->count;
544 unsigned num_in_primitives =
545 align(
546 MAX2(u_decomposed_prims_for_vertices(input_prim->prim,
547 num_input_verts),
548 u_decomposed_prims_for_vertices(shader->input_primitive,
549 num_input_verts)),
550 shader->vector_length);
551 unsigned max_out_prims =
552 u_decomposed_prims_for_vertices(shader->output_primitive,
553 shader->max_output_vertices)
554 * num_in_primitives;
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 unsigned total_verts_per_buffer = shader->primitive_boundary *
558 num_in_primitives;
559
560 //Assume at least one primitive
561 max_out_prims = MAX2(max_out_prims, 1);
562
563
564 output_verts->vertex_size = vertex_size;
565 output_verts->stride = output_verts->vertex_size;
566 output_verts->verts =
567 (struct vertex_header *)MALLOC(output_verts->vertex_size *
568 total_verts_per_buffer);
569 debug_assert(output_verts->verts);
570
571 #if 0
572 debug_printf("%s count = %d (in prims # = %d)\n",
573 __FUNCTION__, num_input_verts, num_in_primitives);
574 debug_printf("\tlinear = %d, prim_info->count = %d\n",
575 input_prim->linear, input_prim->count);
576 debug_printf("\tprim pipe = %s, shader in = %s, shader out = %s\n"
577 u_prim_name(input_prim->prim),
578 u_prim_name(shader->input_primitive),
579 u_prim_name(shader->output_primitive));
580 debug_printf("\tmaxv = %d, maxp = %d, primitive_boundary = %d, "
581 "vertex_size = %d, tverts = %d\n",
582 shader->max_output_vertices, max_out_prims,
583 shader->primitive_boundary, output_verts->vertex_size,
584 total_verts_per_buffer);
585 #endif
586
587 shader->emitted_vertices = 0;
588 shader->emitted_primitives = 0;
589 shader->vertex_size = vertex_size;
590 shader->tmp_output = (float (*)[4])output_verts->verts->data;
591 shader->fetched_prim_count = 0;
592 shader->input_vertex_stride = input_stride;
593 shader->input = input;
594 shader->input_info = input_info;
595 FREE(shader->primitive_lengths);
596 shader->primitive_lengths = MALLOC(max_out_prims * sizeof(unsigned));
597
598
599 #ifdef HAVE_LLVM
600 if (shader->draw->llvm) {
601 shader->gs_output = output_verts->verts;
602 if (max_out_prims > shader->max_out_prims) {
603 unsigned i;
604 if (shader->llvm_prim_lengths) {
605 for (i = 0; i < shader->max_out_prims; ++i) {
606 align_free(shader->llvm_prim_lengths[i]);
607 }
608 FREE(shader->llvm_prim_lengths);
609 }
610
611 shader->llvm_prim_lengths = MALLOC(max_out_prims * sizeof(unsigned*));
612 for (i = 0; i < max_out_prims; ++i) {
613 int vector_size = shader->vector_length * sizeof(unsigned);
614 shader->llvm_prim_lengths[i] =
615 align_malloc(vector_size, vector_size);
616 }
617
618 shader->max_out_prims = max_out_prims;
619 }
620 shader->jit_context->prim_lengths = shader->llvm_prim_lengths;
621 shader->jit_context->emitted_vertices = shader->llvm_emitted_vertices;
622 shader->jit_context->emitted_prims = shader->llvm_emitted_primitives;
623 }
624 #endif
625
626 shader->prepare(shader, constants, constants_size);
627
628 if (input_prim->linear)
629 gs_run(shader, input_prim, input_verts,
630 output_prims, output_verts);
631 else
632 gs_run_elts(shader, input_prim, input_verts,
633 output_prims, output_verts);
634
635 /* Flush the remaining primitives. Will happen if
636 * num_input_primitives % 4 != 0
637 */
638 if (shader->fetched_prim_count > 0) {
639 gs_flush(shader);
640 }
641
642 debug_assert(shader->fetched_prim_count == 0);
643
644 /* Update prim_info:
645 */
646 output_prims->linear = TRUE;
647 output_prims->elts = NULL;
648 output_prims->start = 0;
649 output_prims->count = shader->emitted_vertices;
650 output_prims->prim = shader->output_primitive;
651 output_prims->flags = 0x0;
652 output_prims->primitive_lengths = shader->primitive_lengths;
653 output_prims->primitive_count = shader->emitted_primitives;
654 output_verts->count = shader->emitted_vertices;
655
656 if (shader->draw->collect_statistics) {
657 unsigned i;
658 for (i = 0; i < shader->emitted_primitives; ++i) {
659 shader->draw->statistics.gs_primitives +=
660 u_decomposed_prims_for_vertices(shader->output_primitive,
661 shader->primitive_lengths[i]);
662 }
663 }
664
665 #if 0
666 debug_printf("GS finished, prims = %d, verts = %d\n",
667 output_prims->primitive_count,
668 output_verts->count);
669 #endif
670
671 return shader->emitted_vertices;
672 }
673
674 void draw_geometry_shader_prepare(struct draw_geometry_shader *shader,
675 struct draw_context *draw)
676 {
677 boolean use_llvm = draw->llvm != NULL;
678 if (!use_llvm && shader && shader->machine->Tokens != shader->state.tokens) {
679 tgsi_exec_machine_bind_shader(shader->machine,
680 shader->state.tokens,
681 draw->gs.tgsi.sampler);
682 }
683 }
684
685
686 boolean
687 draw_gs_init( struct draw_context *draw )
688 {
689 if (!draw->llvm) {
690 draw->gs.tgsi.machine = tgsi_exec_machine_create();
691 if (!draw->gs.tgsi.machine)
692 return FALSE;
693
694 draw->gs.tgsi.machine->Primitives = align_malloc(
695 MAX_PRIMITIVES * sizeof(struct tgsi_exec_vector), 16);
696 if (!draw->gs.tgsi.machine->Primitives)
697 return FALSE;
698 memset(draw->gs.tgsi.machine->Primitives, 0,
699 MAX_PRIMITIVES * sizeof(struct tgsi_exec_vector));
700 }
701
702 return TRUE;
703 }
704
705 void draw_gs_destroy( struct draw_context *draw )
706 {
707 if (draw->gs.tgsi.machine) {
708 align_free(draw->gs.tgsi.machine->Primitives);
709 tgsi_exec_machine_destroy(draw->gs.tgsi.machine);
710 }
711 }
712
713 struct draw_geometry_shader *
714 draw_create_geometry_shader(struct draw_context *draw,
715 const struct pipe_shader_state *state)
716 {
717 #ifdef HAVE_LLVM
718 boolean use_llvm = draw->llvm != NULL;
719 struct llvm_geometry_shader *llvm_gs;
720 #endif
721 struct draw_geometry_shader *gs;
722 unsigned i;
723
724 #ifdef HAVE_LLVM
725 if (use_llvm) {
726 llvm_gs = CALLOC_STRUCT(llvm_geometry_shader);
727
728 if (llvm_gs == NULL)
729 return NULL;
730
731 gs = &llvm_gs->base;
732
733 make_empty_list(&llvm_gs->variants);
734 } else
735 #endif
736 {
737 gs = CALLOC_STRUCT(draw_geometry_shader);
738 }
739
740 if (!gs)
741 return NULL;
742
743 gs->draw = draw;
744 gs->state = *state;
745 gs->state.tokens = tgsi_dup_tokens(state->tokens);
746 if (!gs->state.tokens) {
747 FREE(gs);
748 return NULL;
749 }
750
751 tgsi_scan_shader(state->tokens, &gs->info);
752
753 /* setup the defaults */
754 gs->input_primitive = PIPE_PRIM_TRIANGLES;
755 gs->output_primitive = PIPE_PRIM_TRIANGLE_STRIP;
756 gs->max_output_vertices = 32;
757 gs->max_out_prims = 0;
758
759 #ifdef HAVE_LLVM
760 if (use_llvm) {
761 /* TODO: change the input array to handle the following
762 vector length, instead of the currently hardcoded
763 TGSI_NUM_CHANNELS
764 gs->vector_length = lp_native_vector_width / 32;*/
765 gs->vector_length = TGSI_NUM_CHANNELS;
766 } else
767 #endif
768 {
769 gs->vector_length = 1;
770 }
771
772 for (i = 0; i < gs->info.num_properties; ++i) {
773 if (gs->info.properties[i].name ==
774 TGSI_PROPERTY_GS_INPUT_PRIM)
775 gs->input_primitive = gs->info.properties[i].data[0];
776 else if (gs->info.properties[i].name ==
777 TGSI_PROPERTY_GS_OUTPUT_PRIM)
778 gs->output_primitive = gs->info.properties[i].data[0];
779 else if (gs->info.properties[i].name ==
780 TGSI_PROPERTY_GS_MAX_OUTPUT_VERTICES)
781 gs->max_output_vertices = gs->info.properties[i].data[0];
782 }
783 /* Primitive boundary is bigger than max_output_vertices by one, because
784 * the specification says that the geometry shader should exit if the
785 * number of emitted vertices is bigger or equal to max_output_vertices and
786 * we can't do that because we're running in the SoA mode, which means that
787 * our storing routines will keep getting called on channels that have
788 * overflown.
789 * So we need some scratch area where we can keep writing the overflown
790 * vertices without overwriting anything important or crashing.
791 */
792 gs->primitive_boundary = gs->max_output_vertices + 1;
793
794 for (i = 0; i < gs->info.num_outputs; i++) {
795 if (gs->info.output_semantic_name[i] == TGSI_SEMANTIC_POSITION &&
796 gs->info.output_semantic_index[i] == 0)
797 gs->position_output = i;
798 if (gs->info.output_semantic_name[i] == TGSI_SEMANTIC_VIEWPORT_INDEX)
799 gs->viewport_index_output = i;
800 if (gs->info.output_semantic_name[i] == TGSI_SEMANTIC_CLIPDIST) {
801 debug_assert(gs->info.output_semantic_index[i] <
802 PIPE_MAX_CLIP_OR_CULL_DISTANCE_ELEMENT_COUNT);
803 gs->clipdistance_output[gs->info.output_semantic_index[i]] = i;
804 }
805 if (gs->info.output_semantic_name[i] == TGSI_SEMANTIC_CULLDIST) {
806 debug_assert(gs->info.output_semantic_index[i] <
807 PIPE_MAX_CLIP_OR_CULL_DISTANCE_ELEMENT_COUNT);
808 gs->culldistance_output[gs->info.output_semantic_index[i]] = i;
809 }
810 }
811
812 gs->machine = draw->gs.tgsi.machine;
813
814 #ifdef HAVE_LLVM
815 if (use_llvm) {
816 int vector_size = gs->vector_length * sizeof(float);
817 gs->gs_input = align_malloc(sizeof(struct draw_gs_inputs), 16);
818 memset(gs->gs_input, 0, sizeof(struct draw_gs_inputs));
819 gs->llvm_prim_lengths = 0;
820
821 gs->llvm_emitted_primitives = align_malloc(vector_size, vector_size);
822 gs->llvm_emitted_vertices = align_malloc(vector_size, vector_size);
823 gs->llvm_prim_ids = align_malloc(vector_size, vector_size);
824
825 gs->fetch_outputs = llvm_fetch_gs_outputs;
826 gs->fetch_inputs = llvm_fetch_gs_input;
827 gs->prepare = llvm_gs_prepare;
828 gs->run = llvm_gs_run;
829
830 gs->jit_context = &draw->llvm->gs_jit_context;
831
832
833 llvm_gs->variant_key_size =
834 draw_gs_llvm_variant_key_size(
835 MAX2(gs->info.file_max[TGSI_FILE_SAMPLER]+1,
836 gs->info.file_max[TGSI_FILE_SAMPLER_VIEW]+1));
837 } else
838 #endif
839 {
840 gs->fetch_outputs = tgsi_fetch_gs_outputs;
841 gs->fetch_inputs = tgsi_fetch_gs_input;
842 gs->prepare = tgsi_gs_prepare;
843 gs->run = tgsi_gs_run;
844 }
845
846 return gs;
847 }
848
849 void draw_bind_geometry_shader(struct draw_context *draw,
850 struct draw_geometry_shader *dgs)
851 {
852 draw_do_flush(draw, DRAW_FLUSH_STATE_CHANGE);
853
854 if (dgs) {
855 draw->gs.geometry_shader = dgs;
856 draw->gs.num_gs_outputs = dgs->info.num_outputs;
857 draw->gs.position_output = dgs->position_output;
858 draw_geometry_shader_prepare(dgs, draw);
859 }
860 else {
861 draw->gs.geometry_shader = NULL;
862 draw->gs.num_gs_outputs = 0;
863 }
864 }
865
866 void draw_delete_geometry_shader(struct draw_context *draw,
867 struct draw_geometry_shader *dgs)
868 {
869 if (!dgs) {
870 return;
871 }
872 #ifdef HAVE_LLVM
873 if (draw->llvm) {
874 struct llvm_geometry_shader *shader = llvm_geometry_shader(dgs);
875 struct draw_gs_llvm_variant_list_item *li;
876
877 li = first_elem(&shader->variants);
878 while(!at_end(&shader->variants, li)) {
879 struct draw_gs_llvm_variant_list_item *next = next_elem(li);
880 draw_gs_llvm_destroy_variant(li->base);
881 li = next;
882 }
883
884 assert(shader->variants_cached == 0);
885
886 if (dgs->llvm_prim_lengths) {
887 unsigned i;
888 for (i = 0; i < dgs->max_out_prims; ++i) {
889 align_free(dgs->llvm_prim_lengths[i]);
890 }
891 FREE(dgs->llvm_prim_lengths);
892 }
893 align_free(dgs->llvm_emitted_primitives);
894 align_free(dgs->llvm_emitted_vertices);
895 align_free(dgs->llvm_prim_ids);
896
897 align_free(dgs->gs_input);
898 }
899 #endif
900
901 FREE(dgs->primitive_lengths);
902 FREE((void*) dgs->state.tokens);
903 FREE(dgs);
904 }
905
906
907 #ifdef HAVE_LLVM
908 void draw_gs_set_current_variant(struct draw_geometry_shader *shader,
909 struct draw_gs_llvm_variant *variant)
910 {
911 shader->current_variant = variant;
912 }
913 #endif
914
915 /*
916 * Called at the very begin of the draw call with a new instance
917 * Used to reset state that should persist between primitive restart.
918 */
919 void
920 draw_geometry_shader_new_instance(struct draw_geometry_shader *gs)
921 {
922 if (!gs)
923 return;
924
925 gs->in_prim_idx = 0;
926 }