draw/gs: Return early if the passed geometry shader is null
[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 debug_assert(0);
61 return -1;
62 }
63
64 /**
65 * We execute geometry shaders in the SOA mode, so ideally we want to
66 * flush when the number of currently fetched primitives is equal to
67 * the number of elements in the SOA vector. This ensures that the
68 * throughput is optimized for the given vector instrunction set.
69 */
70 static INLINE boolean
71 draw_gs_should_flush(struct draw_geometry_shader *shader)
72 {
73 return (shader->fetched_prim_count == shader->vector_length);
74 }
75
76 /*#define DEBUG_OUTPUTS 1*/
77 static void
78 tgsi_fetch_gs_outputs(struct draw_geometry_shader *shader,
79 unsigned num_primitives,
80 float (**p_output)[4])
81 {
82 struct tgsi_exec_machine *machine = shader->machine;
83 unsigned prim_idx, j, slot;
84 unsigned current_idx = 0;
85 float (*output)[4];
86
87 output = *p_output;
88
89 /* Unswizzle all output results.
90 */
91
92 for (prim_idx = 0; prim_idx < num_primitives; ++prim_idx) {
93 unsigned num_verts_per_prim = machine->Primitives[prim_idx];
94 shader->primitive_lengths[prim_idx + shader->emitted_primitives] =
95 machine->Primitives[prim_idx];
96 shader->emitted_vertices += num_verts_per_prim;
97 for (j = 0; j < num_verts_per_prim; j++, current_idx++) {
98 int idx = current_idx * shader->info.num_outputs;
99 #ifdef DEBUG_OUTPUTS
100 debug_printf("%d) Output vert:\n", idx / shader->info.num_outputs);
101 #endif
102 for (slot = 0; slot < shader->info.num_outputs; slot++) {
103 output[slot][0] = machine->Outputs[idx + slot].xyzw[0].f[0];
104 output[slot][1] = machine->Outputs[idx + slot].xyzw[1].f[0];
105 output[slot][2] = machine->Outputs[idx + slot].xyzw[2].f[0];
106 output[slot][3] = machine->Outputs[idx + slot].xyzw[3].f[0];
107 #ifdef DEBUG_OUTPUTS
108 debug_printf("\t%d: %f %f %f %f\n", slot,
109 output[slot][0],
110 output[slot][1],
111 output[slot][2],
112 output[slot][3]);
113 #endif
114 debug_assert(!util_is_inf_or_nan(output[slot][0]));
115 }
116 output = (float (*)[4])((char *)output + shader->vertex_size);
117 }
118 }
119 *p_output = output;
120 shader->emitted_primitives += num_primitives;
121 }
122
123 /*#define DEBUG_INPUTS 1*/
124 static void tgsi_fetch_gs_input(struct draw_geometry_shader *shader,
125 unsigned *indices,
126 unsigned num_vertices,
127 unsigned prim_idx)
128 {
129 struct tgsi_exec_machine *machine = shader->machine;
130 unsigned slot, vs_slot, i;
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 DEBUG_INPUTS
161 debug_printf("\tSlot = %d, vs_slot = %d, idx = %d:\n",
162 slot, vs_slot, idx);
163 assert(!util_is_inf_or_nan(input[vs_slot][0]));
164 assert(!util_is_inf_or_nan(input[vs_slot][1]));
165 assert(!util_is_inf_or_nan(input[vs_slot][2]));
166 assert(!util_is_inf_or_nan(input[vs_slot][3]));
167 #endif
168 machine->Inputs[idx].xyzw[0].f[prim_idx] = input[vs_slot][0];
169 machine->Inputs[idx].xyzw[1].f[prim_idx] = input[vs_slot][1];
170 machine->Inputs[idx].xyzw[2].f[prim_idx] = input[vs_slot][2];
171 machine->Inputs[idx].xyzw[3].f[prim_idx] = input[vs_slot][3];
172 #if DEBUG_INPUTS
173 debug_printf("\t\t%f %f %f %f\n",
174 machine->Inputs[idx].xyzw[0].f[prim_idx],
175 machine->Inputs[idx].xyzw[1].f[prim_idx],
176 machine->Inputs[idx].xyzw[2].f[prim_idx],
177 machine->Inputs[idx].xyzw[3].f[prim_idx]);
178 #endif
179 ++vs_slot;
180 }
181 }
182 }
183 }
184
185 static void tgsi_gs_prepare(struct draw_geometry_shader *shader,
186 const void *constants[PIPE_MAX_CONSTANT_BUFFERS],
187 const unsigned constants_size[PIPE_MAX_CONSTANT_BUFFERS])
188 {
189 struct tgsi_exec_machine *machine = shader->machine;
190
191 tgsi_exec_set_constant_buffers(machine, PIPE_MAX_CONSTANT_BUFFERS,
192 constants, constants_size);
193 }
194
195 static unsigned tgsi_gs_run(struct draw_geometry_shader *shader,
196 unsigned input_primitives)
197 {
198 struct tgsi_exec_machine *machine = shader->machine;
199
200 tgsi_set_exec_mask(machine,
201 1,
202 input_primitives > 1,
203 input_primitives > 2,
204 input_primitives > 3);
205
206 /* run interpreter */
207 tgsi_exec_machine_run(machine);
208
209 return
210 machine->Temps[TGSI_EXEC_TEMP_PRIMITIVE_I].xyzw[TGSI_EXEC_TEMP_PRIMITIVE_C].u[0];
211 }
212
213 #ifdef HAVE_LLVM
214
215 static void
216 llvm_fetch_gs_input(struct draw_geometry_shader *shader,
217 unsigned *indices,
218 unsigned num_vertices,
219 unsigned prim_idx)
220 {
221 unsigned slot, vs_slot, i;
222 unsigned input_vertex_stride = shader->input_vertex_stride;
223 const float (*input_ptr)[4];
224 float (*input_data)[6][PIPE_MAX_SHADER_INPUTS][TGSI_NUM_CHANNELS][TGSI_NUM_CHANNELS] = &shader->gs_input->data;
225
226 shader->llvm_prim_ids[shader->fetched_prim_count] =
227 shader->in_prim_idx;
228
229 input_ptr = shader->input;
230
231 for (i = 0; i < num_vertices; ++i) {
232 const float (*input)[4];
233 #if DEBUG_INPUTS
234 debug_printf("%d) vertex index = %d (prim idx = %d)\n",
235 i, indices[i], prim_idx);
236 #endif
237 input = (const float (*)[4])(
238 (const char *)input_ptr + (indices[i] * input_vertex_stride));
239 for (slot = 0, vs_slot = 0; slot < shader->info.num_inputs; ++slot) {
240 if (shader->info.input_semantic_name[slot] == TGSI_SEMANTIC_PRIMID) {
241 /* skip. we handle system values through gallivm */
242 } else {
243 vs_slot = draw_gs_get_input_index(
244 shader->info.input_semantic_name[slot],
245 shader->info.input_semantic_index[slot],
246 shader->input_info);
247 #if DEBUG_INPUTS
248 debug_printf("\tSlot = %d, vs_slot = %d, i = %d:\n",
249 slot, vs_slot, i);
250 assert(!util_is_inf_or_nan(input[vs_slot][0]));
251 assert(!util_is_inf_or_nan(input[vs_slot][1]));
252 assert(!util_is_inf_or_nan(input[vs_slot][2]));
253 assert(!util_is_inf_or_nan(input[vs_slot][3]));
254 #endif
255 (*input_data)[i][slot][0][prim_idx] = input[vs_slot][0];
256 (*input_data)[i][slot][1][prim_idx] = input[vs_slot][1];
257 (*input_data)[i][slot][2][prim_idx] = input[vs_slot][2];
258 (*input_data)[i][slot][3][prim_idx] = input[vs_slot][3];
259 #if DEBUG_INPUTS
260 debug_printf("\t\t%f %f %f %f\n",
261 (*input_data)[i][slot][0][prim_idx],
262 (*input_data)[i][slot][1][prim_idx],
263 (*input_data)[i][slot][2][prim_idx],
264 (*input_data)[i][slot][3][prim_idx]);
265 #endif
266 ++vs_slot;
267 }
268 }
269 }
270 }
271
272 static void
273 llvm_fetch_gs_outputs(struct draw_geometry_shader *shader,
274 unsigned num_primitives,
275 float (**p_output)[4])
276 {
277 int total_verts = 0;
278 int vertex_count = 0;
279 int total_prims = 0;
280 int max_prims_per_invocation = 0;
281 char *output_ptr = (char*)shader->gs_output;
282 int i, j, prim_idx;
283
284 for (i = 0; i < shader->vector_length; ++i) {
285 int prims = shader->llvm_emitted_primitives[i];
286 total_prims += prims;
287 max_prims_per_invocation = MAX2(max_prims_per_invocation, prims);
288 }
289 for (i = 0; i < shader->vector_length; ++i) {
290 total_verts += shader->llvm_emitted_vertices[i];
291 }
292
293
294 output_ptr += shader->emitted_vertices * shader->vertex_size;
295 for (i = 0; i < shader->vector_length - 1; ++i) {
296 int current_verts = shader->llvm_emitted_vertices[i];
297
298 if (current_verts != shader->max_output_vertices) {
299 memcpy(output_ptr + (vertex_count + current_verts) * shader->vertex_size,
300 output_ptr + (vertex_count + shader->max_output_vertices) * shader->vertex_size,
301 shader->vertex_size * (total_verts - vertex_count));
302 }
303 vertex_count += current_verts;
304 }
305
306 prim_idx = 0;
307 for (i = 0; i < shader->vector_length; ++i) {
308 int num_prims = shader->llvm_emitted_primitives[i];
309 for (j = 0; j < num_prims; ++j) {
310 int prim_length =
311 shader->llvm_prim_lengths[j][i];
312 shader->primitive_lengths[shader->emitted_primitives + prim_idx] =
313 prim_length;
314 ++prim_idx;
315 }
316 }
317
318 shader->emitted_primitives += total_prims;
319 shader->emitted_vertices += total_verts;
320 }
321
322 static void
323 llvm_gs_prepare(struct draw_geometry_shader *shader,
324 const void *constants[PIPE_MAX_CONSTANT_BUFFERS],
325 const unsigned constants_size[PIPE_MAX_CONSTANT_BUFFERS])
326 {
327 }
328
329 static unsigned
330 llvm_gs_run(struct draw_geometry_shader *shader,
331 unsigned input_primitives)
332 {
333 unsigned ret;
334 char *input = (char*)shader->gs_output;
335
336 input += (shader->emitted_vertices * shader->vertex_size);
337
338 ret = shader->current_variant->jit_func(
339 shader->jit_context, shader->gs_input->data,
340 (struct vertex_header*)input,
341 input_primitives,
342 shader->draw->instance_id,
343 shader->llvm_prim_ids);
344
345 return ret;
346 }
347
348 #endif
349
350 static void gs_flush(struct draw_geometry_shader *shader)
351 {
352 unsigned out_prim_count;
353
354 unsigned input_primitives = shader->fetched_prim_count;
355
356 if (shader->draw->collect_statistics) {
357 shader->draw->statistics.gs_invocations += input_primitives;
358 }
359
360 debug_assert(input_primitives > 0 &&
361 input_primitives <= 4);
362
363 out_prim_count = shader->run(shader, input_primitives);
364 shader->fetch_outputs(shader, out_prim_count,
365 &shader->tmp_output);
366
367 #if 0
368 debug_printf("PRIM emitted prims = %d (verts=%d), cur prim count = %d\n",
369 shader->emitted_primitives, shader->emitted_vertices,
370 out_prim_count);
371 #endif
372
373 shader->fetched_prim_count = 0;
374 }
375
376 static void gs_point(struct draw_geometry_shader *shader,
377 int idx)
378 {
379 unsigned indices[1];
380
381 indices[0] = idx;
382
383 shader->fetch_inputs(shader, indices, 1,
384 shader->fetched_prim_count);
385 ++shader->in_prim_idx;
386 ++shader->fetched_prim_count;
387
388 if (draw_gs_should_flush(shader))
389 gs_flush(shader);
390 }
391
392 static void gs_line(struct draw_geometry_shader *shader,
393 int i0, int i1)
394 {
395 unsigned indices[2];
396
397 indices[0] = i0;
398 indices[1] = i1;
399
400 shader->fetch_inputs(shader, indices, 2,
401 shader->fetched_prim_count);
402 ++shader->in_prim_idx;
403 ++shader->fetched_prim_count;
404
405 if (draw_gs_should_flush(shader))
406 gs_flush(shader);
407 }
408
409 static void gs_line_adj(struct draw_geometry_shader *shader,
410 int i0, int i1, int i2, int i3)
411 {
412 unsigned indices[4];
413
414 indices[0] = i0;
415 indices[1] = i1;
416 indices[2] = i2;
417 indices[3] = i3;
418
419 shader->fetch_inputs(shader, indices, 4,
420 shader->fetched_prim_count);
421 ++shader->in_prim_idx;
422 ++shader->fetched_prim_count;
423
424 if (draw_gs_should_flush(shader))
425 gs_flush(shader);
426 }
427
428 static void gs_tri(struct draw_geometry_shader *shader,
429 int i0, int i1, int i2)
430 {
431 unsigned indices[3];
432
433 indices[0] = i0;
434 indices[1] = i1;
435 indices[2] = i2;
436
437 shader->fetch_inputs(shader, indices, 3,
438 shader->fetched_prim_count);
439 ++shader->in_prim_idx;
440 ++shader->fetched_prim_count;
441
442 if (draw_gs_should_flush(shader))
443 gs_flush(shader);
444 }
445
446 static void gs_tri_adj(struct draw_geometry_shader *shader,
447 int i0, int i1, int i2,
448 int i3, int i4, int i5)
449 {
450 unsigned indices[6];
451
452 indices[0] = i0;
453 indices[1] = i1;
454 indices[2] = i2;
455 indices[3] = i3;
456 indices[4] = i4;
457 indices[5] = i5;
458
459 shader->fetch_inputs(shader, indices, 6,
460 shader->fetched_prim_count);
461 ++shader->in_prim_idx;
462 ++shader->fetched_prim_count;
463
464 if (draw_gs_should_flush(shader))
465 gs_flush(shader);
466 }
467
468 #define FUNC gs_run
469 #define GET_ELT(idx) (idx)
470 #include "draw_gs_tmp.h"
471
472
473 #define FUNC gs_run_elts
474 #define LOCAL_VARS const ushort *elts = input_prims->elts;
475 #define GET_ELT(idx) (elts[idx])
476 #include "draw_gs_tmp.h"
477
478
479 /**
480 * Execute geometry shader.
481 */
482 int draw_geometry_shader_run(struct draw_geometry_shader *shader,
483 const void *constants[PIPE_MAX_CONSTANT_BUFFERS],
484 const unsigned constants_size[PIPE_MAX_CONSTANT_BUFFERS],
485 const struct draw_vertex_info *input_verts,
486 const struct draw_prim_info *input_prim,
487 const struct tgsi_shader_info *input_info,
488 struct draw_vertex_info *output_verts,
489 struct draw_prim_info *output_prims )
490 {
491 const float (*input)[4] = (const float (*)[4])input_verts->verts->data;
492 unsigned input_stride = input_verts->vertex_size;
493 unsigned num_outputs = shader->info.num_outputs;
494 unsigned vertex_size = sizeof(struct vertex_header) + num_outputs * 4 * sizeof(float);
495 unsigned num_input_verts = input_prim->linear ?
496 input_verts->count :
497 input_prim->count;
498 unsigned num_in_primitives =
499 align(
500 MAX2(u_decomposed_prims_for_vertices(input_prim->prim,
501 num_input_verts),
502 u_decomposed_prims_for_vertices(shader->input_primitive,
503 num_input_verts)),
504 shader->vector_length);
505 unsigned max_out_prims =
506 u_decomposed_prims_for_vertices(shader->output_primitive,
507 shader->max_output_vertices)
508 * num_in_primitives;
509
510 //Assume at least one primitive
511 max_out_prims = MAX2(max_out_prims, 1);
512
513
514 output_verts->vertex_size = vertex_size;
515 output_verts->stride = output_verts->vertex_size;
516 output_verts->verts =
517 (struct vertex_header *)MALLOC(output_verts->vertex_size *
518 max_out_prims *
519 shader->max_output_vertices);
520
521 #if 0
522 debug_printf("%s count = %d (in prims # = %d)\n",
523 __FUNCTION__, num_input_verts, num_in_primitives);
524 debug_printf("\tlinear = %d, prim_info->count = %d\n",
525 input_prim->linear, input_prim->count);
526 debug_printf("\tprim pipe = %s, shader in = %s, shader out = %s, max out = %d\n",
527 u_prim_name(input_prim->prim),
528 u_prim_name(shader->input_primitive),
529 u_prim_name(shader->output_primitive),
530 shader->max_output_vertices);
531 #endif
532
533 shader->emitted_vertices = 0;
534 shader->emitted_primitives = 0;
535 shader->vertex_size = vertex_size;
536 shader->tmp_output = (float (*)[4])output_verts->verts->data;
537 shader->in_prim_idx = 0;
538 shader->fetched_prim_count = 0;
539 shader->input_vertex_stride = input_stride;
540 shader->input = input;
541 shader->input_info = input_info;
542 FREE(shader->primitive_lengths);
543 shader->primitive_lengths = MALLOC(max_out_prims * sizeof(unsigned));
544
545
546 #ifdef HAVE_LLVM
547 if (draw_get_option_use_llvm()) {
548 shader->gs_output = output_verts->verts;
549 if (max_out_prims > shader->max_out_prims) {
550 unsigned i;
551 if (shader->llvm_prim_lengths) {
552 for (i = 0; i < shader->max_out_prims; ++i) {
553 align_free(shader->llvm_prim_lengths[i]);
554 }
555 FREE(shader->llvm_prim_lengths);
556 }
557
558 shader->llvm_prim_lengths = MALLOC(max_out_prims * sizeof(unsigned*));
559 for (i = 0; i < max_out_prims; ++i) {
560 int vector_size = shader->vector_length * sizeof(unsigned);
561 shader->llvm_prim_lengths[i] =
562 align_malloc(vector_size, vector_size);
563 }
564
565 shader->max_out_prims = max_out_prims;
566 }
567 shader->jit_context->prim_lengths = shader->llvm_prim_lengths;
568 shader->jit_context->emitted_vertices = shader->llvm_emitted_vertices;
569 shader->jit_context->emitted_prims = shader->llvm_emitted_primitives;
570 }
571 #endif
572
573 shader->prepare(shader, constants, constants_size);
574
575 if (input_prim->linear)
576 gs_run(shader, input_prim, input_verts,
577 output_prims, output_verts);
578 else
579 gs_run_elts(shader, input_prim, input_verts,
580 output_prims, output_verts);
581
582 /* Flush the remaining primitives. Will happen if
583 * num_input_primitives % 4 != 0
584 */
585 if (shader->fetched_prim_count > 0) {
586 gs_flush(shader);
587 }
588
589 debug_assert(shader->fetched_prim_count == 0);
590
591 /* Update prim_info:
592 */
593 output_prims->linear = TRUE;
594 output_prims->elts = NULL;
595 output_prims->start = 0;
596 output_prims->count = shader->emitted_vertices;
597 output_prims->prim = shader->output_primitive;
598 output_prims->flags = 0x0;
599 output_prims->primitive_lengths = shader->primitive_lengths;
600 output_prims->primitive_count = shader->emitted_primitives;
601 output_verts->count = shader->emitted_vertices;
602
603 if (shader->draw->collect_statistics) {
604 unsigned i;
605 for (i = 0; i < shader->emitted_primitives; ++i) {
606 shader->draw->statistics.gs_primitives +=
607 u_decomposed_prims_for_vertices(shader->output_primitive,
608 shader->primitive_lengths[i]);
609 }
610 }
611
612 #if 0
613 debug_printf("GS finished, prims = %d, verts = %d\n",
614 output_prims->primitive_count,
615 output_verts->count);
616 #endif
617
618 return shader->emitted_vertices;
619 }
620
621 void draw_geometry_shader_prepare(struct draw_geometry_shader *shader,
622 struct draw_context *draw)
623 {
624 if (shader && shader->machine->Tokens != shader->state.tokens) {
625 tgsi_exec_machine_bind_shader(shader->machine,
626 shader->state.tokens,
627 draw->gs.tgsi.sampler);
628 }
629 }
630
631
632 boolean
633 draw_gs_init( struct draw_context *draw )
634 {
635 draw->gs.tgsi.machine = tgsi_exec_machine_create();
636 if (!draw->gs.tgsi.machine)
637 return FALSE;
638
639 draw->gs.tgsi.machine->Primitives = align_malloc(
640 MAX_PRIMITIVES * sizeof(struct tgsi_exec_vector), 16);
641 if (!draw->gs.tgsi.machine->Primitives)
642 return FALSE;
643 memset(draw->gs.tgsi.machine->Primitives, 0,
644 MAX_PRIMITIVES * sizeof(struct tgsi_exec_vector));
645
646 return TRUE;
647 }
648
649 void draw_gs_destroy( struct draw_context *draw )
650 {
651 if (draw->gs.tgsi.machine) {
652 align_free(draw->gs.tgsi.machine->Primitives);
653 tgsi_exec_machine_destroy(draw->gs.tgsi.machine);
654 }
655 }
656
657 struct draw_geometry_shader *
658 draw_create_geometry_shader(struct draw_context *draw,
659 const struct pipe_shader_state *state)
660 {
661 #ifdef HAVE_LLVM
662 boolean use_llvm = draw_get_option_use_llvm();
663 struct llvm_geometry_shader *llvm_gs;
664 #endif
665 struct draw_geometry_shader *gs;
666 unsigned i;
667
668 #ifdef HAVE_LLVM
669 if (use_llvm) {
670 llvm_gs = CALLOC_STRUCT(llvm_geometry_shader);
671
672 if (llvm_gs == NULL)
673 return NULL;
674
675 gs = &llvm_gs->base;
676
677 make_empty_list(&llvm_gs->variants);
678 } else
679 #endif
680 {
681 gs = CALLOC_STRUCT(draw_geometry_shader);
682 }
683
684 if (!gs)
685 return NULL;
686
687 gs->draw = draw;
688 gs->state = *state;
689 gs->state.tokens = tgsi_dup_tokens(state->tokens);
690 if (!gs->state.tokens) {
691 FREE(gs);
692 return NULL;
693 }
694
695 tgsi_scan_shader(state->tokens, &gs->info);
696
697 /* setup the defaults */
698 gs->input_primitive = PIPE_PRIM_TRIANGLES;
699 gs->output_primitive = PIPE_PRIM_TRIANGLE_STRIP;
700 gs->max_output_vertices = 32;
701 gs->max_out_prims = 0;
702
703 #ifdef HAVE_LLVM
704 if (use_llvm) {
705 /* TODO: change the input array to handle the following
706 vector length, instead of the currently hardcoded
707 TGSI_NUM_CHANNELS
708 gs->vector_length = lp_native_vector_width / 32;*/
709 gs->vector_length = TGSI_NUM_CHANNELS;
710 } else
711 #endif
712 {
713 gs->vector_length = 1;
714 }
715
716 for (i = 0; i < gs->info.num_properties; ++i) {
717 if (gs->info.properties[i].name ==
718 TGSI_PROPERTY_GS_INPUT_PRIM)
719 gs->input_primitive = gs->info.properties[i].data[0];
720 else if (gs->info.properties[i].name ==
721 TGSI_PROPERTY_GS_OUTPUT_PRIM)
722 gs->output_primitive = gs->info.properties[i].data[0];
723 else if (gs->info.properties[i].name ==
724 TGSI_PROPERTY_GS_MAX_OUTPUT_VERTICES)
725 gs->max_output_vertices = gs->info.properties[i].data[0];
726 }
727
728 for (i = 0; i < gs->info.num_outputs; i++) {
729 if (gs->info.output_semantic_name[i] == TGSI_SEMANTIC_POSITION &&
730 gs->info.output_semantic_index[i] == 0)
731 gs->position_output = i;
732 }
733
734 gs->machine = draw->gs.tgsi.machine;
735
736 #ifdef HAVE_LLVM
737 if (use_llvm) {
738 int vector_size = gs->vector_length * sizeof(float);
739 gs->gs_input = align_malloc(sizeof(struct draw_gs_inputs), 16);
740 memset(gs->gs_input, 0, sizeof(struct draw_gs_inputs));
741 gs->llvm_prim_lengths = 0;
742
743 gs->llvm_emitted_primitives = align_malloc(vector_size, vector_size);
744 gs->llvm_emitted_vertices = align_malloc(vector_size, vector_size);
745 gs->llvm_prim_ids = align_malloc(vector_size, vector_size);
746
747 gs->fetch_outputs = llvm_fetch_gs_outputs;
748 gs->fetch_inputs = llvm_fetch_gs_input;
749 gs->prepare = llvm_gs_prepare;
750 gs->run = llvm_gs_run;
751
752 gs->jit_context = &draw->llvm->gs_jit_context;
753
754
755 llvm_gs->variant_key_size =
756 draw_gs_llvm_variant_key_size(
757 MAX2(gs->info.file_max[TGSI_FILE_SAMPLER]+1,
758 gs->info.file_max[TGSI_FILE_SAMPLER_VIEW]+1));
759 } else
760 #endif
761 {
762 gs->fetch_outputs = tgsi_fetch_gs_outputs;
763 gs->fetch_inputs = tgsi_fetch_gs_input;
764 gs->prepare = tgsi_gs_prepare;
765 gs->run = tgsi_gs_run;
766 }
767
768 return gs;
769 }
770
771 void draw_bind_geometry_shader(struct draw_context *draw,
772 struct draw_geometry_shader *dgs)
773 {
774 draw_do_flush(draw, DRAW_FLUSH_STATE_CHANGE);
775
776 if (dgs) {
777 draw->gs.geometry_shader = dgs;
778 draw->gs.num_gs_outputs = dgs->info.num_outputs;
779 draw->gs.position_output = dgs->position_output;
780 draw_geometry_shader_prepare(dgs, draw);
781 }
782 else {
783 draw->gs.geometry_shader = NULL;
784 draw->gs.num_gs_outputs = 0;
785 }
786 }
787
788 void draw_delete_geometry_shader(struct draw_context *draw,
789 struct draw_geometry_shader *dgs)
790 {
791 if (!dgs) {
792 return;
793 }
794 #ifdef HAVE_LLVM
795 if (draw_get_option_use_llvm()) {
796 struct llvm_geometry_shader *shader = llvm_geometry_shader(dgs);
797 struct draw_gs_llvm_variant_list_item *li;
798
799 li = first_elem(&shader->variants);
800 while(!at_end(&shader->variants, li)) {
801 struct draw_gs_llvm_variant_list_item *next = next_elem(li);
802 draw_gs_llvm_destroy_variant(li->base);
803 li = next;
804 }
805
806 assert(shader->variants_cached == 0);
807
808 if (dgs->llvm_prim_lengths) {
809 unsigned i;
810 for (i = 0; i < dgs->max_out_prims; ++i) {
811 align_free(dgs->llvm_prim_lengths[i]);
812 }
813 FREE(dgs->llvm_prim_lengths);
814 }
815 align_free(dgs->llvm_emitted_primitives);
816 align_free(dgs->llvm_emitted_vertices);
817 align_free(dgs->llvm_prim_ids);
818
819 align_free(dgs->gs_input);
820 }
821 #endif
822
823 FREE(dgs->primitive_lengths);
824 FREE((void*) dgs->state.tokens);
825 FREE(dgs);
826 }
827
828
829 #ifdef HAVE_LLVM
830 void draw_gs_set_current_variant(struct draw_geometry_shader *shader,
831 struct draw_gs_llvm_variant *variant)
832 {
833 shader->current_variant = variant;
834 }
835 #endif