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
,
79 unsigned num_primitives
,
80 float (**p_output
)[4])
82 struct tgsi_exec_machine
*machine
= shader
->machine
;
83 unsigned prim_idx
, j
, slot
;
88 /* Unswizzle all output results.
91 for (prim_idx
= 0; prim_idx
< num_primitives
; ++prim_idx
) {
92 unsigned num_verts_per_prim
= machine
->Primitives
[stream
][prim_idx
];
93 unsigned prim_offset
= machine
->PrimitiveOffsets
[stream
][prim_idx
];
94 shader
->stream
[stream
].primitive_lengths
[prim_idx
+ shader
->stream
[stream
].emitted_primitives
] =
95 machine
->Primitives
[stream
][prim_idx
];
96 shader
->stream
[stream
].emitted_vertices
+= num_verts_per_prim
;
98 for (j
= 0; j
< num_verts_per_prim
; j
++) {
99 int idx
= prim_offset
+ j
* shader
->info
.num_outputs
;
101 debug_printf("%d/%d) Output vert:\n", stream
, idx
/ shader
->info
.num_outputs
);
103 for (slot
= 0; slot
< shader
->info
.num_outputs
; slot
++) {
104 output
[slot
][0] = machine
->Outputs
[idx
+ slot
].xyzw
[0].f
[0];
105 output
[slot
][1] = machine
->Outputs
[idx
+ slot
].xyzw
[1].f
[0];
106 output
[slot
][2] = machine
->Outputs
[idx
+ slot
].xyzw
[2].f
[0];
107 output
[slot
][3] = machine
->Outputs
[idx
+ slot
].xyzw
[3].f
[0];
109 debug_printf("\t%d: %f %f %f %f\n", slot
,
116 output
= (float (*)[4])((char *)output
+ shader
->vertex_size
);
120 shader
->stream
[stream
].emitted_primitives
+= num_primitives
;
123 /*#define DEBUG_INPUTS 1*/
124 static void tgsi_fetch_gs_input(struct draw_geometry_shader
*shader
,
126 unsigned num_vertices
,
129 struct tgsi_exec_machine
*machine
= shader
->machine
;
132 unsigned input_vertex_stride
= shader
->input_vertex_stride
;
133 const float (*input_ptr
)[4];
135 input_ptr
= shader
->input
;
137 for (i
= 0; i
< num_vertices
; ++i
) {
138 const float (*input
)[4];
140 debug_printf("%d) vertex index = %d (prim idx = %d)\n",
141 i
, indices
[i
], prim_idx
);
143 input
= (const float (*)[4])(
144 (const char *)input_ptr
+ (indices
[i
] * input_vertex_stride
));
145 for (slot
= 0, vs_slot
= 0; slot
< shader
->info
.num_inputs
; ++slot
) {
146 unsigned idx
= i
* TGSI_EXEC_MAX_INPUT_ATTRIBS
+ slot
;
147 if (shader
->info
.input_semantic_name
[slot
] == TGSI_SEMANTIC_PRIMID
) {
148 machine
->Inputs
[idx
].xyzw
[0].u
[prim_idx
] = shader
->in_prim_idx
;
149 machine
->Inputs
[idx
].xyzw
[1].u
[prim_idx
] = shader
->in_prim_idx
;
150 machine
->Inputs
[idx
].xyzw
[2].u
[prim_idx
] = shader
->in_prim_idx
;
151 machine
->Inputs
[idx
].xyzw
[3].u
[prim_idx
] = shader
->in_prim_idx
;
153 vs_slot
= draw_gs_get_input_index(
154 shader
->info
.input_semantic_name
[slot
],
155 shader
->info
.input_semantic_index
[slot
],
158 debug_printf("VS/GS signature mismatch!\n");
159 machine
->Inputs
[idx
].xyzw
[0].f
[prim_idx
] = 0;
160 machine
->Inputs
[idx
].xyzw
[1].f
[prim_idx
] = 0;
161 machine
->Inputs
[idx
].xyzw
[2].f
[prim_idx
] = 0;
162 machine
->Inputs
[idx
].xyzw
[3].f
[prim_idx
] = 0;
165 debug_printf("\tSlot = %d, vs_slot = %d, idx = %d:\n",
167 assert(!util_is_inf_or_nan(input
[vs_slot
][0]));
168 assert(!util_is_inf_or_nan(input
[vs_slot
][1]));
169 assert(!util_is_inf_or_nan(input
[vs_slot
][2]));
170 assert(!util_is_inf_or_nan(input
[vs_slot
][3]));
172 machine
->Inputs
[idx
].xyzw
[0].f
[prim_idx
] = input
[vs_slot
][0];
173 machine
->Inputs
[idx
].xyzw
[1].f
[prim_idx
] = input
[vs_slot
][1];
174 machine
->Inputs
[idx
].xyzw
[2].f
[prim_idx
] = input
[vs_slot
][2];
175 machine
->Inputs
[idx
].xyzw
[3].f
[prim_idx
] = input
[vs_slot
][3];
177 debug_printf("\t\t%f %f %f %f\n",
178 machine
->Inputs
[idx
].xyzw
[0].f
[prim_idx
],
179 machine
->Inputs
[idx
].xyzw
[1].f
[prim_idx
],
180 machine
->Inputs
[idx
].xyzw
[2].f
[prim_idx
],
181 machine
->Inputs
[idx
].xyzw
[3].f
[prim_idx
]);
190 static void tgsi_gs_prepare(struct draw_geometry_shader
*shader
,
191 const void *constants
[PIPE_MAX_CONSTANT_BUFFERS
],
192 const unsigned constants_size
[PIPE_MAX_CONSTANT_BUFFERS
])
194 struct tgsi_exec_machine
*machine
= shader
->machine
;
196 tgsi_exec_set_constant_buffers(machine
, PIPE_MAX_CONSTANT_BUFFERS
,
197 constants
, constants_size
);
199 if (shader
->info
.uses_invocationid
) {
200 unsigned i
= machine
->SysSemanticToIndex
[TGSI_SEMANTIC_INVOCATIONID
];
201 for (j
= 0; j
< TGSI_QUAD_SIZE
; j
++)
202 machine
->SystemValue
[i
].xyzw
[0].i
[j
] = shader
->invocation_id
;
206 static void tgsi_gs_run(struct draw_geometry_shader
*shader
,
207 unsigned input_primitives
,
210 struct tgsi_exec_machine
*machine
= shader
->machine
;
213 /* run interpreter */
214 tgsi_exec_machine_run(machine
, 0);
216 for (i
= 0; i
< 4; i
++) {
221 prim_i
= TGSI_EXEC_TEMP_PRIMITIVE_I
;
222 prim_c
= TGSI_EXEC_TEMP_PRIMITIVE_C
;
225 prim_i
= TGSI_EXEC_TEMP_PRIMITIVE_S1_I
;
226 prim_c
= TGSI_EXEC_TEMP_PRIMITIVE_S1_C
;
229 prim_i
= TGSI_EXEC_TEMP_PRIMITIVE_S2_I
;
230 prim_c
= TGSI_EXEC_TEMP_PRIMITIVE_S2_C
;
233 prim_i
= TGSI_EXEC_TEMP_PRIMITIVE_S3_I
;
234 prim_c
= TGSI_EXEC_TEMP_PRIMITIVE_S3_C
;
238 out_prims
[i
] = machine
->Temps
[prim_i
].xyzw
[prim_c
].u
[0];
245 llvm_fetch_gs_input(struct draw_geometry_shader
*shader
,
247 unsigned num_vertices
,
252 unsigned input_vertex_stride
= shader
->input_vertex_stride
;
253 const float (*input_ptr
)[4];
254 float (*input_data
)[6][PIPE_MAX_SHADER_INPUTS
][TGSI_NUM_CHANNELS
][TGSI_NUM_CHANNELS
] = &shader
->gs_input
->data
;
256 shader
->llvm_prim_ids
[shader
->fetched_prim_count
] = shader
->in_prim_idx
;
258 input_ptr
= shader
->input
;
260 for (i
= 0; i
< num_vertices
; ++i
) {
261 const float (*input
)[4];
263 debug_printf("%d) vertex index = %d (prim idx = %d)\n",
264 i
, indices
[i
], prim_idx
);
266 input
= (const float (*)[4])(
267 (const char *)input_ptr
+ (indices
[i
] * input_vertex_stride
));
268 for (slot
= 0, vs_slot
= 0; slot
< shader
->info
.num_inputs
; ++slot
) {
269 if (shader
->info
.input_semantic_name
[slot
] == TGSI_SEMANTIC_PRIMID
) {
270 /* skip. we handle system values through gallivm */
271 /* NOTE: If we hit this case here it's an ordinary input not a sv,
272 * even though it probably should be a sv.
273 * Not sure how to set it up as regular input however if that even,
274 * would make sense so hack around this later in gallivm.
277 vs_slot
= draw_gs_get_input_index(
278 shader
->info
.input_semantic_name
[slot
],
279 shader
->info
.input_semantic_index
[slot
],
282 debug_printf("VS/GS signature mismatch!\n");
283 (*input_data
)[i
][slot
][0][prim_idx
] = 0;
284 (*input_data
)[i
][slot
][1][prim_idx
] = 0;
285 (*input_data
)[i
][slot
][2][prim_idx
] = 0;
286 (*input_data
)[i
][slot
][3][prim_idx
] = 0;
289 debug_printf("\tSlot = %d, vs_slot = %d, i = %d:\n",
291 assert(!util_is_inf_or_nan(input
[vs_slot
][0]));
292 assert(!util_is_inf_or_nan(input
[vs_slot
][1]));
293 assert(!util_is_inf_or_nan(input
[vs_slot
][2]));
294 assert(!util_is_inf_or_nan(input
[vs_slot
][3]));
296 (*input_data
)[i
][slot
][0][prim_idx
] = input
[vs_slot
][0];
297 (*input_data
)[i
][slot
][1][prim_idx
] = input
[vs_slot
][1];
298 (*input_data
)[i
][slot
][2][prim_idx
] = input
[vs_slot
][2];
299 (*input_data
)[i
][slot
][3][prim_idx
] = input
[vs_slot
][3];
301 debug_printf("\t\t%f %f %f %f\n",
302 (*input_data
)[i
][slot
][0][prim_idx
],
303 (*input_data
)[i
][slot
][1][prim_idx
],
304 (*input_data
)[i
][slot
][2][prim_idx
],
305 (*input_data
)[i
][slot
][3][prim_idx
]);
315 llvm_fetch_gs_outputs(struct draw_geometry_shader
*shader
,
317 unsigned num_primitives
,
318 float (**p_output
)[4])
321 int vertex_count
= 0;
323 int max_prims_per_invocation
= 0;
324 char *output_ptr
= (char*)shader
->gs_output
;
326 unsigned next_prim_boundary
= shader
->primitive_boundary
;
328 for (i
= 0; i
< shader
->vector_length
; ++i
) {
329 int prims
= shader
->llvm_emitted_primitives
[i
];
330 total_prims
+= prims
;
331 max_prims_per_invocation
= MAX2(max_prims_per_invocation
, prims
);
333 for (i
= 0; i
< shader
->vector_length
; ++i
) {
334 total_verts
+= shader
->llvm_emitted_vertices
[i
];
337 output_ptr
+= shader
->stream
[0].emitted_vertices
* shader
->vertex_size
;
338 for (i
= 0; i
< shader
->vector_length
- 1; ++i
) {
339 int current_verts
= shader
->llvm_emitted_vertices
[i
];
340 int next_verts
= shader
->llvm_emitted_vertices
[i
+ 1];
343 for (j
= 0; j
< current_verts
; ++j
) {
344 struct vertex_header
*vh
= (struct vertex_header
*)
345 (output_ptr
+ shader
->vertex_size
* (i
* next_prim_boundary
+ j
));
346 debug_printf("--- %d) [%f, %f, %f, %f]\n", j
+ vertex_count
,
347 vh
->data
[0][0], vh
->data
[0][1], vh
->data
[0][2], vh
->data
[0][3]);
351 debug_assert(current_verts
<= shader
->max_output_vertices
);
352 debug_assert(next_verts
<= shader
->max_output_vertices
);
354 memmove(output_ptr
+ (vertex_count
+ current_verts
) * shader
->vertex_size
,
355 output_ptr
+ ((i
+ 1) * next_prim_boundary
) * shader
->vertex_size
,
356 shader
->vertex_size
* next_verts
);
358 vertex_count
+= current_verts
;
364 for (i
= 0; i
< total_verts
; ++i
) {
365 struct vertex_header
*vh
= (struct vertex_header
*)(output_ptr
+ shader
->vertex_size
* i
);
366 debug_printf("%d) Vertex:\n", i
);
367 for (j
= 0; j
< shader
->info
.num_outputs
; ++j
) {
368 unsigned *udata
= (unsigned*)vh
->data
[j
];
369 debug_printf(" %d) [%f, %f, %f, %f] [%d, %d, %d, %d]\n", j
,
370 vh
->data
[j
][0], vh
->data
[j
][1], vh
->data
[j
][2], vh
->data
[j
][3],
371 udata
[0], udata
[1], udata
[2], udata
[3]);
379 for (i
= 0; i
< shader
->vector_length
; ++i
) {
380 int num_prims
= shader
->llvm_emitted_primitives
[i
];
381 for (j
= 0; j
< num_prims
; ++j
) {
383 shader
->llvm_prim_lengths
[j
][i
];
384 shader
->stream
[0].primitive_lengths
[shader
->stream
[0].emitted_primitives
+ prim_idx
] =
390 shader
->stream
[0].emitted_primitives
+= total_prims
;
391 shader
->stream
[0].emitted_vertices
+= total_verts
;
395 llvm_gs_prepare(struct draw_geometry_shader
*shader
,
396 const void *constants
[PIPE_MAX_CONSTANT_BUFFERS
],
397 const unsigned constants_size
[PIPE_MAX_CONSTANT_BUFFERS
])
402 llvm_gs_run(struct draw_geometry_shader
*shader
,
403 unsigned input_primitives
, unsigned *out_prims
)
406 char *input
= (char*)shader
->gs_output
;
408 input
+= (shader
->stream
[0].emitted_vertices
* shader
->vertex_size
);
410 ret
= shader
->current_variant
->jit_func(
411 shader
->jit_context
, shader
->gs_input
->data
,
412 (struct vertex_header
*)input
,
414 shader
->draw
->instance_id
,
415 shader
->llvm_prim_ids
,
416 shader
->invocation_id
);
423 static void gs_flush(struct draw_geometry_shader
*shader
)
425 unsigned out_prim_count
[TGSI_MAX_VERTEX_STREAMS
];
427 unsigned input_primitives
= shader
->fetched_prim_count
;
429 if (shader
->draw
->collect_statistics
) {
430 shader
->draw
->statistics
.gs_invocations
+= input_primitives
;
433 debug_assert(input_primitives
> 0 &&
434 input_primitives
<= 4);
436 shader
->run(shader
, input_primitives
, out_prim_count
);
437 for (i
= 0; i
< shader
->num_vertex_streams
; i
++) {
438 shader
->fetch_outputs(shader
, i
, out_prim_count
[i
],
439 &shader
->stream
[i
].tmp_output
);
443 for (i
= 0; i
< shader
->num_vertex_streams
; i
++) {
444 debug_printf("stream %d: PRIM emitted prims = %d (verts=%d), cur prim count = %d\n",
446 shader
->stream
[i
].emitted_primitives
, shader
->stream
[i
].emitted_vertices
,
451 shader
->fetched_prim_count
= 0;
454 static void gs_point(struct draw_geometry_shader
*shader
,
461 shader
->fetch_inputs(shader
, indices
, 1,
462 shader
->fetched_prim_count
);
463 ++shader
->in_prim_idx
;
464 ++shader
->fetched_prim_count
;
466 if (draw_gs_should_flush(shader
))
470 static void gs_line(struct draw_geometry_shader
*shader
,
478 shader
->fetch_inputs(shader
, indices
, 2,
479 shader
->fetched_prim_count
);
480 ++shader
->in_prim_idx
;
481 ++shader
->fetched_prim_count
;
483 if (draw_gs_should_flush(shader
))
487 static void gs_line_adj(struct draw_geometry_shader
*shader
,
488 int i0
, int i1
, int i2
, int i3
)
497 shader
->fetch_inputs(shader
, indices
, 4,
498 shader
->fetched_prim_count
);
499 ++shader
->in_prim_idx
;
500 ++shader
->fetched_prim_count
;
502 if (draw_gs_should_flush(shader
))
506 static void gs_tri(struct draw_geometry_shader
*shader
,
507 int i0
, int i1
, int i2
)
515 shader
->fetch_inputs(shader
, indices
, 3,
516 shader
->fetched_prim_count
);
517 ++shader
->in_prim_idx
;
518 ++shader
->fetched_prim_count
;
520 if (draw_gs_should_flush(shader
))
524 static void gs_tri_adj(struct draw_geometry_shader
*shader
,
525 int i0
, int i1
, int i2
,
526 int i3
, int i4
, int i5
)
537 shader
->fetch_inputs(shader
, indices
, 6,
538 shader
->fetched_prim_count
);
539 ++shader
->in_prim_idx
;
540 ++shader
->fetched_prim_count
;
542 if (draw_gs_should_flush(shader
))
547 #define GET_ELT(idx) (idx)
548 #include "draw_gs_tmp.h"
551 #define FUNC gs_run_elts
552 #define LOCAL_VARS const ushort *elts = input_prims->elts;
553 #define GET_ELT(idx) (elts[idx])
554 #include "draw_gs_tmp.h"
558 * Execute geometry shader.
560 int draw_geometry_shader_run(struct draw_geometry_shader
*shader
,
561 const void *constants
[PIPE_MAX_CONSTANT_BUFFERS
],
562 const unsigned constants_size
[PIPE_MAX_CONSTANT_BUFFERS
],
563 const struct draw_vertex_info
*input_verts
,
564 const struct draw_prim_info
*input_prim
,
565 const struct tgsi_shader_info
*input_info
,
566 struct draw_vertex_info
*output_verts
,
567 struct draw_prim_info
*output_prims
)
569 const float (*input
)[4] = (const float (*)[4])input_verts
->verts
->data
;
570 unsigned input_stride
= input_verts
->vertex_size
;
571 unsigned num_outputs
= draw_total_gs_outputs(shader
->draw
);
572 unsigned vertex_size
= sizeof(struct vertex_header
) + num_outputs
* 4 * sizeof(float);
573 unsigned num_input_verts
= input_prim
->linear
?
576 unsigned num_in_primitives
=
578 MAX2(u_decomposed_prims_for_vertices(input_prim
->prim
,
580 u_decomposed_prims_for_vertices(shader
->input_primitive
,
582 shader
->vector_length
);
583 unsigned max_out_prims
=
584 u_decomposed_prims_for_vertices(shader
->output_primitive
,
585 shader
->max_output_vertices
)
587 /* we allocate exactly one extra vertex per primitive to allow the GS to emit
588 * overflown vertices into some area where they won't harm anyone */
589 unsigned total_verts_per_buffer
= shader
->primitive_boundary
*
593 //Assume at least one primitive
594 max_out_prims
= MAX2(max_out_prims
, 1);
596 for (i
= 0; i
< shader
->num_vertex_streams
; i
++) {
597 /* write all the vertex data into all the streams */
598 output_verts
[i
].vertex_size
= vertex_size
;
599 output_verts
[i
].stride
= output_verts
[i
].vertex_size
;
600 output_verts
[i
].verts
=
601 (struct vertex_header
*)MALLOC(output_verts
[i
].vertex_size
*
602 total_verts_per_buffer
* shader
->num_invocations
);
603 debug_assert(output_verts
[i
].verts
);
607 debug_printf("%s count = %d (in prims # = %d)\n",
608 __FUNCTION__
, num_input_verts
, num_in_primitives
);
609 debug_printf("\tlinear = %d, prim_info->count = %d\n",
610 input_prim
->linear
, input_prim
->count
);
611 debug_printf("\tprim pipe = %s, shader in = %s, shader out = %s\n"
612 u_prim_name(input_prim
->prim
),
613 u_prim_name(shader
->input_primitive
),
614 u_prim_name(shader
->output_primitive
));
615 debug_printf("\tmaxv = %d, maxp = %d, primitive_boundary = %d, "
616 "vertex_size = %d, tverts = %d\n",
617 shader
->max_output_vertices
, max_out_prims
,
618 shader
->primitive_boundary
, output_verts
->vertex_size
,
619 total_verts_per_buffer
);
622 for (i
= 0; i
< shader
->num_vertex_streams
; i
++) {
623 shader
->stream
[i
].emitted_vertices
= 0;
624 shader
->stream
[i
].emitted_primitives
= 0;
625 FREE(shader
->stream
[i
].primitive_lengths
);
626 shader
->stream
[i
].primitive_lengths
= MALLOC(max_out_prims
* sizeof(unsigned) * shader
->num_invocations
);
627 shader
->stream
[i
].tmp_output
= (float (*)[4])output_verts
[i
].verts
->data
;
629 shader
->vertex_size
= vertex_size
;
630 shader
->fetched_prim_count
= 0;
631 shader
->input_vertex_stride
= input_stride
;
632 shader
->input
= input
;
633 shader
->input_info
= input_info
;
636 if (shader
->draw
->llvm
) {
637 shader
->gs_output
= output_verts
[0].verts
;
638 if (max_out_prims
> shader
->max_out_prims
) {
640 if (shader
->llvm_prim_lengths
) {
641 for (i
= 0; i
< shader
->max_out_prims
; ++i
) {
642 align_free(shader
->llvm_prim_lengths
[i
]);
644 FREE(shader
->llvm_prim_lengths
);
647 shader
->llvm_prim_lengths
= MALLOC(max_out_prims
* sizeof(unsigned*));
648 for (i
= 0; i
< max_out_prims
; ++i
) {
649 int vector_size
= shader
->vector_length
* sizeof(unsigned);
650 shader
->llvm_prim_lengths
[i
] =
651 align_malloc(vector_size
, vector_size
);
654 shader
->max_out_prims
= max_out_prims
;
656 shader
->jit_context
->prim_lengths
= shader
->llvm_prim_lengths
;
657 shader
->jit_context
->emitted_vertices
= shader
->llvm_emitted_vertices
;
658 shader
->jit_context
->emitted_prims
= shader
->llvm_emitted_primitives
;
662 for (invocation
= 0; invocation
< shader
->num_invocations
; invocation
++) {
663 shader
->invocation_id
= invocation
;
665 shader
->prepare(shader
, constants
, constants_size
);
667 if (input_prim
->linear
)
668 gs_run(shader
, input_prim
, input_verts
,
669 output_prims
, output_verts
);
671 gs_run_elts(shader
, input_prim
, input_verts
,
672 output_prims
, output_verts
);
674 /* Flush the remaining primitives. Will happen if
675 * num_input_primitives % 4 != 0
677 if (shader
->fetched_prim_count
> 0) {
680 debug_assert(shader
->fetched_prim_count
== 0);
685 for (i
= 0; i
< shader
->num_vertex_streams
; i
++) {
686 output_prims
[i
].linear
= TRUE
;
687 output_prims
[i
].elts
= NULL
;
688 output_prims
[i
].start
= 0;
689 output_prims
[i
].count
= shader
->stream
[i
].emitted_vertices
;
690 output_prims
[i
].prim
= shader
->output_primitive
;
691 output_prims
[i
].flags
= 0x0;
692 output_prims
[i
].primitive_lengths
= shader
->stream
[i
].primitive_lengths
;
693 output_prims
[i
].primitive_count
= shader
->stream
[i
].emitted_primitives
;
694 output_verts
[i
].count
= shader
->stream
[i
].emitted_vertices
;
696 if (shader
->draw
->collect_statistics
) {
698 for (j
= 0; j
< shader
->stream
[i
].emitted_primitives
; ++j
) {
699 shader
->draw
->statistics
.gs_primitives
+=
700 u_decomposed_prims_for_vertices(shader
->output_primitive
,
701 shader
->stream
[i
].primitive_lengths
[j
]);
707 debug_printf("GS finished\n");
708 for (i
= 0; i
< 4; i
++)
709 debug_printf("stream %d: prims = %d verts = %d\n", i
, output_prims
[i
].primitive_count
, output_verts
[i
].count
);
715 void draw_geometry_shader_prepare(struct draw_geometry_shader
*shader
,
716 struct draw_context
*draw
)
718 boolean use_llvm
= draw
->llvm
!= NULL
;
719 if (!use_llvm
&& shader
&& shader
->machine
->Tokens
!= shader
->state
.tokens
) {
720 tgsi_exec_machine_bind_shader(shader
->machine
,
721 shader
->state
.tokens
,
722 draw
->gs
.tgsi
.sampler
,
724 draw
->gs
.tgsi
.buffer
);
730 draw_gs_init( struct draw_context
*draw
)
733 draw
->gs
.tgsi
.machine
= tgsi_exec_machine_create(PIPE_SHADER_GEOMETRY
);
735 for (unsigned i
= 0; i
< TGSI_MAX_VERTEX_STREAMS
; i
++) {
736 draw
->gs
.tgsi
.machine
->Primitives
[i
] = align_malloc(
737 MAX_PRIMITIVES
* sizeof(struct tgsi_exec_vector
), 16);
738 draw
->gs
.tgsi
.machine
->PrimitiveOffsets
[i
] = align_malloc(
739 MAX_PRIMITIVES
* sizeof(struct tgsi_exec_vector
), 16);
740 if (!draw
->gs
.tgsi
.machine
->Primitives
[i
] || !draw
->gs
.tgsi
.machine
->PrimitiveOffsets
[i
])
742 memset(draw
->gs
.tgsi
.machine
->Primitives
[i
], 0,
743 MAX_PRIMITIVES
* sizeof(struct tgsi_exec_vector
));
744 memset(draw
->gs
.tgsi
.machine
->PrimitiveOffsets
[i
], 0,
745 MAX_PRIMITIVES
* sizeof(struct tgsi_exec_vector
));
752 void draw_gs_destroy( struct draw_context
*draw
)
755 if (draw
->gs
.tgsi
.machine
) {
756 for (i
= 0; i
< TGSI_MAX_VERTEX_STREAMS
; i
++)
757 align_free(draw
->gs
.tgsi
.machine
->Primitives
[i
]);
758 tgsi_exec_machine_destroy(draw
->gs
.tgsi
.machine
);
762 struct draw_geometry_shader
*
763 draw_create_geometry_shader(struct draw_context
*draw
,
764 const struct pipe_shader_state
*state
)
767 boolean use_llvm
= draw
->llvm
!= NULL
;
768 struct llvm_geometry_shader
*llvm_gs
= NULL
;
770 struct draw_geometry_shader
*gs
;
775 llvm_gs
= CALLOC_STRUCT(llvm_geometry_shader
);
782 make_empty_list(&llvm_gs
->variants
);
786 gs
= CALLOC_STRUCT(draw_geometry_shader
);
794 gs
->state
.tokens
= tgsi_dup_tokens(state
->tokens
);
795 if (!gs
->state
.tokens
) {
800 tgsi_scan_shader(state
->tokens
, &gs
->info
);
802 /* setup the defaults */
803 gs
->max_out_prims
= 0;
807 /* TODO: change the input array to handle the following
808 vector length, instead of the currently hardcoded
810 gs->vector_length = lp_native_vector_width / 32;*/
811 gs
->vector_length
= TGSI_NUM_CHANNELS
;
815 gs
->vector_length
= 1;
818 gs
->input_primitive
=
819 gs
->info
.properties
[TGSI_PROPERTY_GS_INPUT_PRIM
];
820 gs
->output_primitive
=
821 gs
->info
.properties
[TGSI_PROPERTY_GS_OUTPUT_PRIM
];
822 gs
->max_output_vertices
=
823 gs
->info
.properties
[TGSI_PROPERTY_GS_MAX_OUTPUT_VERTICES
];
824 gs
->num_invocations
=
825 gs
->info
.properties
[TGSI_PROPERTY_GS_INVOCATIONS
];
826 if (!gs
->max_output_vertices
)
827 gs
->max_output_vertices
= 32;
829 /* Primitive boundary is bigger than max_output_vertices by one, because
830 * the specification says that the geometry shader should exit if the
831 * number of emitted vertices is bigger or equal to max_output_vertices and
832 * we can't do that because we're running in the SoA mode, which means that
833 * our storing routines will keep getting called on channels that have
835 * So we need some scratch area where we can keep writing the overflown
836 * vertices without overwriting anything important or crashing.
838 gs
->primitive_boundary
= gs
->max_output_vertices
+ 1;
840 gs
->position_output
= -1;
841 for (i
= 0; i
< gs
->info
.num_outputs
; i
++) {
842 if (gs
->info
.output_semantic_name
[i
] == TGSI_SEMANTIC_POSITION
&&
843 gs
->info
.output_semantic_index
[i
] == 0)
844 gs
->position_output
= i
;
845 if (gs
->info
.output_semantic_name
[i
] == TGSI_SEMANTIC_VIEWPORT_INDEX
)
846 gs
->viewport_index_output
= i
;
847 if (gs
->info
.output_semantic_name
[i
] == TGSI_SEMANTIC_CLIPDIST
) {
848 debug_assert(gs
->info
.output_semantic_index
[i
] <
849 PIPE_MAX_CLIP_OR_CULL_DISTANCE_ELEMENT_COUNT
);
850 gs
->ccdistance_output
[gs
->info
.output_semantic_index
[i
]] = i
;
854 gs
->machine
= draw
->gs
.tgsi
.machine
;
856 gs
->num_vertex_streams
= 1;
857 for (i
= 0; i
< gs
->state
.stream_output
.num_outputs
; i
++) {
858 if (gs
->state
.stream_output
.output
[i
].stream
>= gs
->num_vertex_streams
)
859 gs
->num_vertex_streams
= gs
->state
.stream_output
.output
[i
].stream
+ 1;
864 int vector_size
= gs
->vector_length
* sizeof(float);
865 gs
->gs_input
= align_malloc(sizeof(struct draw_gs_inputs
), 16);
866 memset(gs
->gs_input
, 0, sizeof(struct draw_gs_inputs
));
867 gs
->llvm_prim_lengths
= 0;
869 gs
->llvm_emitted_primitives
= align_malloc(vector_size
, vector_size
);
870 gs
->llvm_emitted_vertices
= align_malloc(vector_size
, vector_size
);
871 gs
->llvm_prim_ids
= align_malloc(vector_size
, vector_size
);
873 gs
->fetch_outputs
= llvm_fetch_gs_outputs
;
874 gs
->fetch_inputs
= llvm_fetch_gs_input
;
875 gs
->prepare
= llvm_gs_prepare
;
876 gs
->run
= llvm_gs_run
;
878 gs
->jit_context
= &draw
->llvm
->gs_jit_context
;
881 llvm_gs
->variant_key_size
=
882 draw_gs_llvm_variant_key_size(
883 MAX2(gs
->info
.file_max
[TGSI_FILE_SAMPLER
]+1,
884 gs
->info
.file_max
[TGSI_FILE_SAMPLER_VIEW
]+1));
888 gs
->fetch_outputs
= tgsi_fetch_gs_outputs
;
889 gs
->fetch_inputs
= tgsi_fetch_gs_input
;
890 gs
->prepare
= tgsi_gs_prepare
;
891 gs
->run
= tgsi_gs_run
;
897 void draw_bind_geometry_shader(struct draw_context
*draw
,
898 struct draw_geometry_shader
*dgs
)
900 draw_do_flush(draw
, DRAW_FLUSH_STATE_CHANGE
);
903 draw
->gs
.geometry_shader
= dgs
;
904 draw
->gs
.num_gs_outputs
= dgs
->info
.num_outputs
;
905 draw
->gs
.position_output
= dgs
->position_output
;
906 draw_geometry_shader_prepare(dgs
, draw
);
909 draw
->gs
.geometry_shader
= NULL
;
910 draw
->gs
.num_gs_outputs
= 0;
914 void draw_delete_geometry_shader(struct draw_context
*draw
,
915 struct draw_geometry_shader
*dgs
)
923 struct llvm_geometry_shader
*shader
= llvm_geometry_shader(dgs
);
924 struct draw_gs_llvm_variant_list_item
*li
;
926 li
= first_elem(&shader
->variants
);
927 while(!at_end(&shader
->variants
, li
)) {
928 struct draw_gs_llvm_variant_list_item
*next
= next_elem(li
);
929 draw_gs_llvm_destroy_variant(li
->base
);
933 assert(shader
->variants_cached
== 0);
935 if (dgs
->llvm_prim_lengths
) {
937 for (i
= 0; i
< dgs
->max_out_prims
; ++i
) {
938 align_free(dgs
->llvm_prim_lengths
[i
]);
940 FREE(dgs
->llvm_prim_lengths
);
942 align_free(dgs
->llvm_emitted_primitives
);
943 align_free(dgs
->llvm_emitted_vertices
);
944 align_free(dgs
->llvm_prim_ids
);
946 align_free(dgs
->gs_input
);
950 for (i
= 0; i
< TGSI_MAX_VERTEX_STREAMS
; i
++)
951 FREE(dgs
->stream
[i
].primitive_lengths
);
952 FREE((void*) dgs
->state
.tokens
);
958 void draw_gs_set_current_variant(struct draw_geometry_shader
*shader
,
959 struct draw_gs_llvm_variant
*variant
)
961 shader
->current_variant
= variant
;
966 * Called at the very begin of the draw call with a new instance
967 * Used to reset state that should persist between primitive restart.
970 draw_geometry_shader_new_instance(struct draw_geometry_shader
*gs
)