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