1 /**************************************************************************
3 * Copyright 2009 VMware, Inc.
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:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
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.
26 **************************************************************************/
30 #include "draw_private.h"
31 #include "draw_context.h"
33 #include "draw_llvm.h"
36 #include "tgsi/tgsi_parse.h"
37 #include "tgsi/tgsi_exec.h"
39 #include "pipe/p_shader_tokens.h"
41 #include "util/u_math.h"
42 #include "util/u_memory.h"
43 #include "util/u_prim.h"
45 /* fixme: move it from here */
46 #define MAX_PRIMITIVES 64
49 draw_gs_get_input_index(int semantic
, int index
,
50 const struct tgsi_shader_info
*input_info
)
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
)
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.
70 draw_gs_should_flush(struct draw_geometry_shader
*shader
)
72 return (shader
->fetched_prim_count
== shader
->vector_length
);
75 /*#define DEBUG_OUTPUTS 1*/
77 tgsi_fetch_gs_outputs(struct draw_geometry_shader
*shader
,
78 unsigned num_primitives
,
79 float (**p_output
)[4])
81 struct tgsi_exec_machine
*machine
= shader
->machine
;
82 unsigned prim_idx
, j
, slot
;
83 unsigned current_idx
= 0;
88 /* Unswizzle all output results.
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
;
99 debug_printf("%d) Output vert:\n", idx
/ shader
->info
.num_outputs
);
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];
107 debug_printf("\t%d: %f %f %f %f\n", slot
,
114 output
= (float (*)[4])((char *)output
+ shader
->vertex_size
);
118 shader
->emitted_primitives
+= num_primitives
;
121 /*#define DEBUG_INPUTS 1*/
122 static void tgsi_fetch_gs_input(struct draw_geometry_shader
*shader
,
124 unsigned num_vertices
,
127 struct tgsi_exec_machine
*machine
= shader
->machine
;
130 unsigned input_vertex_stride
= shader
->input_vertex_stride
;
131 const float (*input_ptr
)[4];
133 input_ptr
= shader
->input
;
135 for (i
= 0; i
< num_vertices
; ++i
) {
136 const float (*input
)[4];
138 debug_printf("%d) vertex index = %d (prim idx = %d)\n",
139 i
, indices
[i
], prim_idx
);
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
;
151 vs_slot
= draw_gs_get_input_index(
152 shader
->info
.input_semantic_name
[slot
],
153 shader
->info
.input_semantic_index
[slot
],
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;
163 debug_printf("\tSlot = %d, vs_slot = %d, idx = %d:\n",
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]));
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];
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
]);
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
])
192 struct tgsi_exec_machine
*machine
= shader
->machine
;
194 tgsi_exec_set_constant_buffers(machine
, PIPE_MAX_CONSTANT_BUFFERS
,
195 constants
, constants_size
);
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
;
204 static unsigned tgsi_gs_run(struct draw_geometry_shader
*shader
,
205 unsigned input_primitives
)
207 struct tgsi_exec_machine
*machine
= shader
->machine
;
209 tgsi_set_exec_mask(machine
,
211 input_primitives
> 1,
212 input_primitives
> 2,
213 input_primitives
> 3);
215 /* run interpreter */
216 tgsi_exec_machine_run(machine
);
219 machine
->Temps
[TGSI_EXEC_TEMP_PRIMITIVE_I
].xyzw
[TGSI_EXEC_TEMP_PRIMITIVE_C
].u
[0];
225 llvm_fetch_gs_input(struct draw_geometry_shader
*shader
,
227 unsigned num_vertices
,
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
;
236 shader
->llvm_prim_ids
[shader
->fetched_prim_count
] = shader
->in_prim_idx
;
238 input_ptr
= shader
->input
;
240 for (i
= 0; i
< num_vertices
; ++i
) {
241 const float (*input
)[4];
243 debug_printf("%d) vertex index = %d (prim idx = %d)\n",
244 i
, indices
[i
], prim_idx
);
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.
257 vs_slot
= draw_gs_get_input_index(
258 shader
->info
.input_semantic_name
[slot
],
259 shader
->info
.input_semantic_index
[slot
],
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;
269 debug_printf("\tSlot = %d, vs_slot = %d, i = %d:\n",
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]));
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];
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
]);
295 llvm_fetch_gs_outputs(struct draw_geometry_shader
*shader
,
296 unsigned num_primitives
,
297 float (**p_output
)[4])
300 int vertex_count
= 0;
302 int max_prims_per_invocation
= 0;
303 char *output_ptr
= (char*)shader
->gs_output
;
305 unsigned next_prim_boundary
= shader
->primitive_boundary
;
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
);
312 for (i
= 0; i
< shader
->vector_length
; ++i
) {
313 total_verts
+= shader
->llvm_emitted_vertices
[i
];
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];
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]);
330 debug_assert(current_verts
<= shader
->max_output_vertices
);
331 debug_assert(next_verts
<= shader
->max_output_vertices
);
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
);
337 vertex_count
+= current_verts
;
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]);
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
) {
362 shader
->llvm_prim_lengths
[j
][i
];
363 shader
->primitive_lengths
[shader
->emitted_primitives
+ prim_idx
] =
369 shader
->emitted_primitives
+= total_prims
;
370 shader
->emitted_vertices
+= total_verts
;
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
])
381 llvm_gs_run(struct draw_geometry_shader
*shader
,
382 unsigned input_primitives
)
385 char *input
= (char*)shader
->gs_output
;
387 input
+= (shader
->emitted_vertices
* shader
->vertex_size
);
389 ret
= shader
->current_variant
->jit_func(
390 shader
->jit_context
, shader
->gs_input
->data
,
391 (struct vertex_header
*)input
,
393 shader
->draw
->instance_id
,
394 shader
->llvm_prim_ids
,
395 shader
->invocation_id
);
402 static void gs_flush(struct draw_geometry_shader
*shader
)
404 unsigned out_prim_count
;
406 unsigned input_primitives
= shader
->fetched_prim_count
;
408 if (shader
->draw
->collect_statistics
) {
409 shader
->draw
->statistics
.gs_invocations
+= input_primitives
;
412 debug_assert(input_primitives
> 0 &&
413 input_primitives
<= 4);
415 out_prim_count
= shader
->run(shader
, input_primitives
);
416 shader
->fetch_outputs(shader
, out_prim_count
,
417 &shader
->tmp_output
);
420 debug_printf("PRIM emitted prims = %d (verts=%d), cur prim count = %d\n",
421 shader
->emitted_primitives
, shader
->emitted_vertices
,
425 shader
->fetched_prim_count
= 0;
428 static void gs_point(struct draw_geometry_shader
*shader
,
435 shader
->fetch_inputs(shader
, indices
, 1,
436 shader
->fetched_prim_count
);
437 ++shader
->in_prim_idx
;
438 ++shader
->fetched_prim_count
;
440 if (draw_gs_should_flush(shader
))
444 static void gs_line(struct draw_geometry_shader
*shader
,
452 shader
->fetch_inputs(shader
, indices
, 2,
453 shader
->fetched_prim_count
);
454 ++shader
->in_prim_idx
;
455 ++shader
->fetched_prim_count
;
457 if (draw_gs_should_flush(shader
))
461 static void gs_line_adj(struct draw_geometry_shader
*shader
,
462 int i0
, int i1
, int i2
, int i3
)
471 shader
->fetch_inputs(shader
, indices
, 4,
472 shader
->fetched_prim_count
);
473 ++shader
->in_prim_idx
;
474 ++shader
->fetched_prim_count
;
476 if (draw_gs_should_flush(shader
))
480 static void gs_tri(struct draw_geometry_shader
*shader
,
481 int i0
, int i1
, int i2
)
489 shader
->fetch_inputs(shader
, indices
, 3,
490 shader
->fetched_prim_count
);
491 ++shader
->in_prim_idx
;
492 ++shader
->fetched_prim_count
;
494 if (draw_gs_should_flush(shader
))
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
)
511 shader
->fetch_inputs(shader
, indices
, 6,
512 shader
->fetched_prim_count
);
513 ++shader
->in_prim_idx
;
514 ++shader
->fetched_prim_count
;
516 if (draw_gs_should_flush(shader
))
521 #define GET_ELT(idx) (idx)
522 #include "draw_gs_tmp.h"
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"
532 * Execute geometry shader.
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
)
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
?
550 unsigned num_in_primitives
=
552 MAX2(u_decomposed_prims_for_vertices(input_prim
->prim
,
554 u_decomposed_prims_for_vertices(shader
->input_primitive
,
556 shader
->vector_length
);
557 unsigned max_out_prims
=
558 u_decomposed_prims_for_vertices(shader
->output_primitive
,
559 shader
->max_output_vertices
)
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
*
566 //Assume at least one primitive
567 max_out_prims
= MAX2(max_out_prims
, 1);
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
);
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
);
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
);
606 if (shader
->draw
->llvm
) {
607 shader
->gs_output
= output_verts
->verts
;
608 if (max_out_prims
> shader
->max_out_prims
) {
610 if (shader
->llvm_prim_lengths
) {
611 for (i
= 0; i
< shader
->max_out_prims
; ++i
) {
612 align_free(shader
->llvm_prim_lengths
[i
]);
614 FREE(shader
->llvm_prim_lengths
);
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
);
624 shader
->max_out_prims
= max_out_prims
;
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
;
632 for (invocation
= 0; invocation
< shader
->num_invocations
; invocation
++) {
633 shader
->invocation_id
= invocation
;
635 shader
->prepare(shader
, constants
, constants_size
);
637 if (input_prim
->linear
)
638 gs_run(shader
, input_prim
, input_verts
,
639 output_prims
, output_verts
);
641 gs_run_elts(shader
, input_prim
, input_verts
,
642 output_prims
, output_verts
);
644 /* Flush the remaining primitives. Will happen if
645 * num_input_primitives % 4 != 0
647 if (shader
->fetched_prim_count
> 0) {
650 debug_assert(shader
->fetched_prim_count
== 0);
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
;
665 if (shader
->draw
->collect_statistics
) {
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
]);
675 debug_printf("GS finished, prims = %d, verts = %d\n",
676 output_prims
->primitive_count
,
677 output_verts
->count
);
680 return shader
->emitted_vertices
;
683 void draw_geometry_shader_prepare(struct draw_geometry_shader
*shader
,
684 struct draw_context
*draw
)
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
);
696 draw_gs_init( struct draw_context
*draw
)
699 draw
->gs
.tgsi
.machine
= tgsi_exec_machine_create();
700 if (!draw
->gs
.tgsi
.machine
)
703 draw
->gs
.tgsi
.machine
->Primitives
= align_malloc(
704 MAX_PRIMITIVES
* sizeof(struct tgsi_exec_vector
), 16);
705 if (!draw
->gs
.tgsi
.machine
->Primitives
)
707 memset(draw
->gs
.tgsi
.machine
->Primitives
, 0,
708 MAX_PRIMITIVES
* sizeof(struct tgsi_exec_vector
));
714 void draw_gs_destroy( struct draw_context
*draw
)
716 if (draw
->gs
.tgsi
.machine
) {
717 align_free(draw
->gs
.tgsi
.machine
->Primitives
);
718 tgsi_exec_machine_destroy(draw
->gs
.tgsi
.machine
);
722 struct draw_geometry_shader
*
723 draw_create_geometry_shader(struct draw_context
*draw
,
724 const struct pipe_shader_state
*state
)
727 boolean use_llvm
= draw
->llvm
!= NULL
;
728 struct llvm_geometry_shader
*llvm_gs
= NULL
;
730 struct draw_geometry_shader
*gs
;
735 llvm_gs
= CALLOC_STRUCT(llvm_geometry_shader
);
742 make_empty_list(&llvm_gs
->variants
);
746 gs
= CALLOC_STRUCT(draw_geometry_shader
);
754 gs
->state
.tokens
= tgsi_dup_tokens(state
->tokens
);
755 if (!gs
->state
.tokens
) {
760 tgsi_scan_shader(state
->tokens
, &gs
->info
);
762 /* setup the defaults */
763 gs
->max_out_prims
= 0;
767 /* TODO: change the input array to handle the following
768 vector length, instead of the currently hardcoded
770 gs->vector_length = lp_native_vector_width / 32;*/
771 gs
->vector_length
= TGSI_NUM_CHANNELS
;
775 gs
->vector_length
= 1;
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;
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
795 * So we need some scratch area where we can keep writing the overflown
796 * vertices without overwriting anything important or crashing.
798 gs
->primitive_boundary
= gs
->max_output_vertices
+ 1;
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
;
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
;
819 gs
->machine
= draw
->gs
.tgsi
.machine
;
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;
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
);
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
;
837 gs
->jit_context
= &draw
->llvm
->gs_jit_context
;
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));
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
;
856 void draw_bind_geometry_shader(struct draw_context
*draw
,
857 struct draw_geometry_shader
*dgs
)
859 draw_do_flush(draw
, DRAW_FLUSH_STATE_CHANGE
);
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
);
868 draw
->gs
.geometry_shader
= NULL
;
869 draw
->gs
.num_gs_outputs
= 0;
873 void draw_delete_geometry_shader(struct draw_context
*draw
,
874 struct draw_geometry_shader
*dgs
)
881 struct llvm_geometry_shader
*shader
= llvm_geometry_shader(dgs
);
882 struct draw_gs_llvm_variant_list_item
*li
;
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
);
891 assert(shader
->variants_cached
== 0);
893 if (dgs
->llvm_prim_lengths
) {
895 for (i
= 0; i
< dgs
->max_out_prims
; ++i
) {
896 align_free(dgs
->llvm_prim_lengths
[i
]);
898 FREE(dgs
->llvm_prim_lengths
);
900 align_free(dgs
->llvm_emitted_primitives
);
901 align_free(dgs
->llvm_emitted_vertices
);
902 align_free(dgs
->llvm_prim_ids
);
904 align_free(dgs
->gs_input
);
908 FREE(dgs
->primitive_lengths
);
909 FREE((void*) dgs
->state
.tokens
);
915 void draw_gs_set_current_variant(struct draw_geometry_shader
*shader
,
916 struct draw_gs_llvm_variant
*variant
)
918 shader
->current_variant
= variant
;
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.
927 draw_geometry_shader_new_instance(struct draw_geometry_shader
*gs
)