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