s/Tungsten Graphics/VMware/
[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
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 /* we allocate exactly one extra vertex per primitive to allow the GS to emit
563 * overflown vertices into some area where they won't harm anyone */
564 output_verts->verts =
565 (struct vertex_header *)MALLOC(output_verts->vertex_size *
566 max_out_prims *
567 shader->primitive_boundary);
568
569 #if 0
570 debug_printf("%s count = %d (in prims # = %d)\n",
571 __FUNCTION__, num_input_verts, num_in_primitives);
572 debug_printf("\tlinear = %d, prim_info->count = %d\n",
573 input_prim->linear, input_prim->count);
574 debug_printf("\tprim pipe = %s, shader in = %s, shader out = %s, max out = %d\n",
575 u_prim_name(input_prim->prim),
576 u_prim_name(shader->input_primitive),
577 u_prim_name(shader->output_primitive),
578 shader->max_output_vertices);
579 #endif
580
581 shader->emitted_vertices = 0;
582 shader->emitted_primitives = 0;
583 shader->vertex_size = vertex_size;
584 shader->tmp_output = (float (*)[4])output_verts->verts->data;
585 shader->fetched_prim_count = 0;
586 shader->input_vertex_stride = input_stride;
587 shader->input = input;
588 shader->input_info = input_info;
589 FREE(shader->primitive_lengths);
590 shader->primitive_lengths = MALLOC(max_out_prims * sizeof(unsigned));
591
592
593 #ifdef HAVE_LLVM
594 if (draw_get_option_use_llvm()) {
595 shader->gs_output = output_verts->verts;
596 if (max_out_prims > shader->max_out_prims) {
597 unsigned i;
598 if (shader->llvm_prim_lengths) {
599 for (i = 0; i < shader->max_out_prims; ++i) {
600 align_free(shader->llvm_prim_lengths[i]);
601 }
602 FREE(shader->llvm_prim_lengths);
603 }
604
605 shader->llvm_prim_lengths = MALLOC(max_out_prims * sizeof(unsigned*));
606 for (i = 0; i < max_out_prims; ++i) {
607 int vector_size = shader->vector_length * sizeof(unsigned);
608 shader->llvm_prim_lengths[i] =
609 align_malloc(vector_size, vector_size);
610 }
611
612 shader->max_out_prims = max_out_prims;
613 }
614 shader->jit_context->prim_lengths = shader->llvm_prim_lengths;
615 shader->jit_context->emitted_vertices = shader->llvm_emitted_vertices;
616 shader->jit_context->emitted_prims = shader->llvm_emitted_primitives;
617 }
618 #endif
619
620 shader->prepare(shader, constants, constants_size);
621
622 if (input_prim->linear)
623 gs_run(shader, input_prim, input_verts,
624 output_prims, output_verts);
625 else
626 gs_run_elts(shader, input_prim, input_verts,
627 output_prims, output_verts);
628
629 /* Flush the remaining primitives. Will happen if
630 * num_input_primitives % 4 != 0
631 */
632 if (shader->fetched_prim_count > 0) {
633 gs_flush(shader);
634 }
635
636 debug_assert(shader->fetched_prim_count == 0);
637
638 /* Update prim_info:
639 */
640 output_prims->linear = TRUE;
641 output_prims->elts = NULL;
642 output_prims->start = 0;
643 output_prims->count = shader->emitted_vertices;
644 output_prims->prim = shader->output_primitive;
645 output_prims->flags = 0x0;
646 output_prims->primitive_lengths = shader->primitive_lengths;
647 output_prims->primitive_count = shader->emitted_primitives;
648 output_verts->count = shader->emitted_vertices;
649
650 if (shader->draw->collect_statistics) {
651 unsigned i;
652 for (i = 0; i < shader->emitted_primitives; ++i) {
653 shader->draw->statistics.gs_primitives +=
654 u_decomposed_prims_for_vertices(shader->output_primitive,
655 shader->primitive_lengths[i]);
656 }
657 }
658
659 #if 0
660 debug_printf("GS finished, prims = %d, verts = %d\n",
661 output_prims->primitive_count,
662 output_verts->count);
663 #endif
664
665 return shader->emitted_vertices;
666 }
667
668 void draw_geometry_shader_prepare(struct draw_geometry_shader *shader,
669 struct draw_context *draw)
670 {
671 #ifdef HAVE_LLVM
672 boolean use_llvm = draw_get_option_use_llvm();
673 #else
674 boolean use_llvm = FALSE;
675 #endif
676 if (!use_llvm && shader && shader->machine->Tokens != shader->state.tokens) {
677 tgsi_exec_machine_bind_shader(shader->machine,
678 shader->state.tokens,
679 draw->gs.tgsi.sampler);
680 }
681 }
682
683
684 boolean
685 draw_gs_init( struct draw_context *draw )
686 {
687 draw->gs.tgsi.machine = tgsi_exec_machine_create();
688 if (!draw->gs.tgsi.machine)
689 return FALSE;
690
691 draw->gs.tgsi.machine->Primitives = align_malloc(
692 MAX_PRIMITIVES * sizeof(struct tgsi_exec_vector), 16);
693 if (!draw->gs.tgsi.machine->Primitives)
694 return FALSE;
695 memset(draw->gs.tgsi.machine->Primitives, 0,
696 MAX_PRIMITIVES * sizeof(struct tgsi_exec_vector));
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_get_option_use_llvm();
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 for (i = 0; i < gs->info.num_outputs; i++) {
791 if (gs->info.output_semantic_name[i] == TGSI_SEMANTIC_POSITION &&
792 gs->info.output_semantic_index[i] == 0)
793 gs->position_output = i;
794 if (gs->info.output_semantic_name[i] == TGSI_SEMANTIC_VIEWPORT_INDEX)
795 gs->viewport_index_output = i;
796 if (gs->info.output_semantic_name[i] == TGSI_SEMANTIC_CLIPDIST) {
797 debug_assert(gs->info.output_semantic_index[i] <
798 PIPE_MAX_CLIP_OR_CULL_DISTANCE_ELEMENT_COUNT);
799 gs->clipdistance_output[gs->info.output_semantic_index[i]] = i;
800 }
801 if (gs->info.output_semantic_name[i] == TGSI_SEMANTIC_CULLDIST) {
802 debug_assert(gs->info.output_semantic_index[i] <
803 PIPE_MAX_CLIP_OR_CULL_DISTANCE_ELEMENT_COUNT);
804 gs->culldistance_output[gs->info.output_semantic_index[i]] = i;
805 }
806 }
807
808 gs->machine = draw->gs.tgsi.machine;
809
810 #ifdef HAVE_LLVM
811 if (use_llvm) {
812 int vector_size = gs->vector_length * sizeof(float);
813 gs->gs_input = align_malloc(sizeof(struct draw_gs_inputs), 16);
814 memset(gs->gs_input, 0, sizeof(struct draw_gs_inputs));
815 gs->llvm_prim_lengths = 0;
816
817 gs->llvm_emitted_primitives = align_malloc(vector_size, vector_size);
818 gs->llvm_emitted_vertices = align_malloc(vector_size, vector_size);
819 gs->llvm_prim_ids = align_malloc(vector_size, vector_size);
820
821 gs->fetch_outputs = llvm_fetch_gs_outputs;
822 gs->fetch_inputs = llvm_fetch_gs_input;
823 gs->prepare = llvm_gs_prepare;
824 gs->run = llvm_gs_run;
825
826 gs->jit_context = &draw->llvm->gs_jit_context;
827
828
829 llvm_gs->variant_key_size =
830 draw_gs_llvm_variant_key_size(
831 MAX2(gs->info.file_max[TGSI_FILE_SAMPLER]+1,
832 gs->info.file_max[TGSI_FILE_SAMPLER_VIEW]+1));
833 } else
834 #endif
835 {
836 gs->fetch_outputs = tgsi_fetch_gs_outputs;
837 gs->fetch_inputs = tgsi_fetch_gs_input;
838 gs->prepare = tgsi_gs_prepare;
839 gs->run = tgsi_gs_run;
840 }
841
842 return gs;
843 }
844
845 void draw_bind_geometry_shader(struct draw_context *draw,
846 struct draw_geometry_shader *dgs)
847 {
848 draw_do_flush(draw, DRAW_FLUSH_STATE_CHANGE);
849
850 if (dgs) {
851 draw->gs.geometry_shader = dgs;
852 draw->gs.num_gs_outputs = dgs->info.num_outputs;
853 draw->gs.position_output = dgs->position_output;
854 draw_geometry_shader_prepare(dgs, draw);
855 }
856 else {
857 draw->gs.geometry_shader = NULL;
858 draw->gs.num_gs_outputs = 0;
859 }
860 }
861
862 void draw_delete_geometry_shader(struct draw_context *draw,
863 struct draw_geometry_shader *dgs)
864 {
865 if (!dgs) {
866 return;
867 }
868 #ifdef HAVE_LLVM
869 if (draw_get_option_use_llvm()) {
870 struct llvm_geometry_shader *shader = llvm_geometry_shader(dgs);
871 struct draw_gs_llvm_variant_list_item *li;
872
873 li = first_elem(&shader->variants);
874 while(!at_end(&shader->variants, li)) {
875 struct draw_gs_llvm_variant_list_item *next = next_elem(li);
876 draw_gs_llvm_destroy_variant(li->base);
877 li = next;
878 }
879
880 assert(shader->variants_cached == 0);
881
882 if (dgs->llvm_prim_lengths) {
883 unsigned i;
884 for (i = 0; i < dgs->max_out_prims; ++i) {
885 align_free(dgs->llvm_prim_lengths[i]);
886 }
887 FREE(dgs->llvm_prim_lengths);
888 }
889 align_free(dgs->llvm_emitted_primitives);
890 align_free(dgs->llvm_emitted_vertices);
891 align_free(dgs->llvm_prim_ids);
892
893 align_free(dgs->gs_input);
894 }
895 #endif
896
897 FREE(dgs->primitive_lengths);
898 FREE((void*) dgs->state.tokens);
899 FREE(dgs);
900 }
901
902
903 #ifdef HAVE_LLVM
904 void draw_gs_set_current_variant(struct draw_geometry_shader *shader,
905 struct draw_gs_llvm_variant *variant)
906 {
907 shader->current_variant = variant;
908 }
909 #endif
910
911 /*
912 * Called at the very begin of the draw call with a new instance
913 * Used to reset state that should persist between primitive restart.
914 */
915 void
916 draw_geometry_shader_new_instance(struct draw_geometry_shader *gs)
917 {
918 if (!gs)
919 return;
920
921 gs->in_prim_idx = 0;
922 }