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 TUNGSTEN GRAPHICS 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 "tgsi/tgsi_parse.h"
34 #include "tgsi/tgsi_exec.h"
36 #include "pipe/p_shader_tokens.h"
38 #include "util/u_math.h"
39 #include "util/u_memory.h"
40 #include "util/u_prim.h"
42 #define MAX_PRIM_VERTICES 6
43 /* fixme: move it from here */
44 #define MAX_PRIMITIVES 64
47 draw_gs_init( struct draw_context
*draw
)
49 draw
->gs
.machine
= tgsi_exec_machine_create();
50 if (!draw
->gs
.machine
)
53 draw
->gs
.machine
->Primitives
= align_malloc(
54 MAX_PRIMITIVES
* sizeof(struct tgsi_exec_vector
), 16);
55 if (!draw
->gs
.machine
->Primitives
)
57 memset(draw
->gs
.machine
->Primitives
, 0,
58 MAX_PRIMITIVES
* sizeof(struct tgsi_exec_vector
));
63 void draw_gs_destroy( struct draw_context
*draw
)
65 if (!draw
->gs
.machine
)
68 align_free(draw
->gs
.machine
->Primitives
);
70 tgsi_exec_machine_destroy(draw
->gs
.machine
);
74 draw_gs_set_constants(struct draw_context
*draw
,
76 const void *constants
,
82 struct draw_geometry_shader
*
83 draw_create_geometry_shader(struct draw_context
*draw
,
84 const struct pipe_shader_state
*state
)
86 struct draw_geometry_shader
*gs
;
89 gs
= CALLOC_STRUCT(draw_geometry_shader
);
96 gs
->state
.tokens
= tgsi_dup_tokens(state
->tokens
);
97 if (!gs
->state
.tokens
) {
102 tgsi_scan_shader(state
->tokens
, &gs
->info
);
104 /* setup the defaults */
105 gs
->input_primitive
= PIPE_PRIM_TRIANGLES
;
106 gs
->output_primitive
= PIPE_PRIM_TRIANGLE_STRIP
;
107 gs
->max_output_vertices
= 32;
109 for (i
= 0; i
< gs
->info
.num_properties
; ++i
) {
110 if (gs
->info
.properties
[i
].name
==
111 TGSI_PROPERTY_GS_INPUT_PRIM
)
112 gs
->input_primitive
= gs
->info
.properties
[i
].data
[0];
113 else if (gs
->info
.properties
[i
].name
==
114 TGSI_PROPERTY_GS_OUTPUT_PRIM
)
115 gs
->output_primitive
= gs
->info
.properties
[i
].data
[0];
116 else if (gs
->info
.properties
[i
].name
==
117 TGSI_PROPERTY_GS_MAX_OUTPUT_VERTICES
)
118 gs
->max_output_vertices
= gs
->info
.properties
[i
].data
[0];
121 gs
->machine
= draw
->gs
.machine
;
126 for (i
= 0; i
< gs
->info
.num_outputs
; i
++) {
127 if (gs
->info
.output_semantic_name
[i
] == TGSI_SEMANTIC_POSITION
&&
128 gs
->info
.output_semantic_index
[i
] == 0)
129 gs
->position_output
= i
;
136 void draw_bind_geometry_shader(struct draw_context
*draw
,
137 struct draw_geometry_shader
*dgs
)
139 draw_do_flush(draw
, DRAW_FLUSH_STATE_CHANGE
);
142 draw
->gs
.geometry_shader
= dgs
;
143 draw
->gs
.num_gs_outputs
= dgs
->info
.num_outputs
;
144 draw
->gs
.position_output
= dgs
->position_output
;
145 draw_geometry_shader_prepare(dgs
, draw
);
148 draw
->gs
.geometry_shader
= NULL
;
149 draw
->gs
.num_gs_outputs
= 0;
153 void draw_delete_geometry_shader(struct draw_context
*draw
,
154 struct draw_geometry_shader
*dgs
)
159 static void draw_fetch_geometry_input(struct draw_geometry_shader
*shader
,
162 const float (*input_ptr
)[4],
163 unsigned input_vertex_stride
,
164 unsigned inputs_from_vs
)
166 struct tgsi_exec_machine
*machine
= shader
->machine
;
167 unsigned slot
, vs_slot
, k
, j
;
168 unsigned num_vertices
= u_vertices_per_prim(shader
->input_primitive
);
171 for (slot
= 0, vs_slot
= 0; slot
< shader
->info
.num_inputs
; slot
++) {
172 /*debug_printf("Slot = %d (semantic = %d)\n", slot,
173 shader->info.input_semantic_name[slot]);*/
174 if (shader
->info
.input_semantic_name
[slot
] ==
175 TGSI_SEMANTIC_PRIMID
) {
176 for (j
= 0; j
< num_primitives
; ++j
) {
177 machine
->Inputs
[idx
].xyzw
[0].f
[j
] = (float)start_primitive
+ j
;
178 machine
->Inputs
[idx
].xyzw
[1].f
[j
] = (float)start_primitive
+ j
;
179 machine
->Inputs
[idx
].xyzw
[2].f
[j
] = (float)start_primitive
+ j
;
180 machine
->Inputs
[idx
].xyzw
[3].f
[j
] = (float)start_primitive
+ j
;
184 for (j
= 0; j
< num_primitives
; ++j
) {
186 const float (*prim_ptr
)[4];
187 /*debug_printf(" %d) Prim (num_verts = %d)\n", start_primitive + j,
189 prim_ptr
= (const float (*)[4])(
190 (const char *)input_ptr
+
191 (j
* num_vertices
* input_vertex_stride
));
193 for (k
= 0; k
< num_vertices
; ++k
, ++vidx
) {
194 const float (*input
)[4];
195 input
= (const float (*)[4])(
196 (const char *)prim_ptr
+ (k
* input_vertex_stride
));
197 vidx
= k
* TGSI_EXEC_MAX_INPUT_ATTRIBS
+ slot
;
198 /*debug_printf("\t%d)(%d) Input vert:\n", vidx, k);*/
200 assert(!util_is_inf_or_nan(input
[vs_slot
][0]));
201 assert(!util_is_inf_or_nan(input
[vs_slot
][1]));
202 assert(!util_is_inf_or_nan(input
[vs_slot
][2]));
203 assert(!util_is_inf_or_nan(input
[vs_slot
][3]));
205 machine
->Inputs
[vidx
].xyzw
[0].f
[j
] = input
[vs_slot
][0];
206 machine
->Inputs
[vidx
].xyzw
[1].f
[j
] = input
[vs_slot
][1];
207 machine
->Inputs
[vidx
].xyzw
[2].f
[j
] = input
[vs_slot
][2];
208 machine
->Inputs
[vidx
].xyzw
[3].f
[j
] = input
[vs_slot
][3];
210 debug_printf("\t\t%d %f %f %f %f\n", slot
,
211 machine
->Inputs
[vidx
].xyzw
[0].f
[j
],
212 machine
->Inputs
[vidx
].xyzw
[1].f
[j
],
213 machine
->Inputs
[vidx
].xyzw
[2].f
[j
],
214 machine
->Inputs
[vidx
].xyzw
[3].f
[j
]);
223 /*#define DEBUG_OUTPUTS 1*/
225 draw_geometry_fetch_outputs(struct draw_geometry_shader
*shader
,
227 float (**p_output
)[4],
228 unsigned vertex_size
)
230 struct tgsi_exec_machine
*machine
= shader
->machine
;
231 unsigned prim_idx
, j
, slot
;
236 /* Unswizzle all output results.
239 shader
->emitted_primitives
+= num_primitives
;
240 for (prim_idx
= 0; prim_idx
< num_primitives
; ++prim_idx
) {
241 unsigned num_verts_per_prim
= machine
->Primitives
[prim_idx
];
242 shader
->emitted_vertices
+= num_verts_per_prim
;
243 for (j
= 0; j
< num_verts_per_prim
; j
++) {
244 int idx
= (prim_idx
* num_verts_per_prim
+ j
) *
245 shader
->info
.num_outputs
;
247 debug_printf("%d) Output vert:\n", idx
/ shader
->info
.num_outputs
);
249 for (slot
= 0; slot
< shader
->info
.num_outputs
; slot
++) {
250 output
[slot
][0] = machine
->Outputs
[idx
+ slot
].xyzw
[0].f
[0];
251 output
[slot
][1] = machine
->Outputs
[idx
+ slot
].xyzw
[1].f
[0];
252 output
[slot
][2] = machine
->Outputs
[idx
+ slot
].xyzw
[2].f
[0];
253 output
[slot
][3] = machine
->Outputs
[idx
+ slot
].xyzw
[3].f
[0];
255 debug_printf("\t%d: %f %f %f %f\n", slot
,
261 debug_assert(!util_is_inf_or_nan(output
[slot
][0]));
263 output
= (float (*)[4])((char *)output
+ vertex_size
);
269 int draw_geometry_shader_run(struct draw_geometry_shader
*shader
,
271 const float (*input
)[4],
273 const void *constants
[PIPE_MAX_CONSTANT_BUFFERS
],
275 unsigned input_stride
,
276 unsigned vertex_size
)
278 struct tgsi_exec_machine
*machine
= shader
->machine
;
280 unsigned num_in_primitives
=
281 u_gs_prims_for_vertices(pipe_prim
, count
);
282 unsigned inputs_from_vs
= 0;
283 float (*tmp_output
)[4];
284 unsigned alloc_count
= draw_max_output_vertices(shader
->draw
,
287 /* this is bad, but we can't be overwriting the output array
288 * because it's the same as input array here */
289 struct vertex_header
*pipeline_verts
=
290 (struct vertex_header
*)MALLOC(vertex_size
* alloc_count
);
292 if (0) debug_printf("%s count = %d (prims = %d)\n", __FUNCTION__
,
293 count
, num_in_primitives
);
295 shader
->emitted_vertices
= 0;
296 shader
->emitted_primitives
= 0;
298 for (i
= 0; i
< PIPE_MAX_CONSTANT_BUFFERS
; i
++) {
299 machine
->Consts
[i
] = constants
[i
];
302 for (i
= 0; i
< shader
->info
.num_inputs
; ++i
) {
303 if (shader
->info
.input_semantic_name
[i
] != TGSI_SEMANTIC_PRIMID
)
307 tmp_output
= ( float (*)[4])pipeline_verts
->data
;
309 for (i
= 0; i
< num_in_primitives
; ++i
) {
310 unsigned int max_input_primitives
= 1;
311 /* FIXME: handle all the primitives produced by the gs, not just
313 unsigned out_prim_count
;
315 draw_fetch_geometry_input(shader
, i
, max_input_primitives
, input
,
316 input_stride
, inputs_from_vs
);
318 tgsi_set_exec_mask(machine
,
320 max_input_primitives
> 1,
321 max_input_primitives
> 2,
322 max_input_primitives
> 3);
324 /* run interpreter */
325 tgsi_exec_machine_run(machine
);
328 machine
->Temps
[TGSI_EXEC_TEMP_PRIMITIVE_I
].xyzw
[TGSI_EXEC_TEMP_PRIMITIVE_C
].u
[0];
330 draw_geometry_fetch_outputs(shader
, out_prim_count
,
331 &tmp_output
, vertex_size
);
334 memcpy(output
, pipeline_verts
->data
,
335 shader
->info
.num_outputs
* 4 * sizeof(float) +
336 vertex_size
* (shader
->emitted_vertices
-1));
338 FREE(pipeline_verts
);
339 return shader
->emitted_vertices
;
342 void draw_geometry_shader_delete(struct draw_geometry_shader
*shader
)
344 FREE((void*) shader
->state
.tokens
);
348 void draw_geometry_shader_prepare(struct draw_geometry_shader
*shader
,
349 struct draw_context
*draw
)
351 if (shader
&& shader
->machine
->Tokens
!= shader
->state
.tokens
) {
352 tgsi_exec_machine_bind_shader(shader
->machine
,
353 shader
->state
.tokens
,
354 draw
->gs
.num_samplers
,
359 int draw_max_output_vertices(struct draw_context
*draw
,
363 unsigned alloc_count
= align( count
, 4 );
365 if (draw
->gs
.geometry_shader
) {
366 unsigned input_primitives
= u_gs_prims_for_vertices(pipe_prim
,
368 /* max GS output is number of input primitives * max output
369 * vertices per each invocation */
370 unsigned gs_max_verts
= input_primitives
*
371 draw
->gs
.geometry_shader
->max_output_vertices
;
372 if (gs_max_verts
> count
)
373 alloc_count
= align(gs_max_verts
, 4);
375 /*debug_printf("------- alloc count = %d (input = %d)\n",
376 alloc_count, count);*/