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