a679f4bf05d6c2a03df3b99f77c7cb56bfa3cef8
[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 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.
25 *
26 **************************************************************************/
27
28 #include "draw_gs.h"
29
30 #include "draw_private.h"
31 #include "draw_context.h"
32
33 #include "tgsi/tgsi_parse.h"
34 #include "tgsi/tgsi_exec.h"
35
36 #include "pipe/p_shader_tokens.h"
37
38 #include "util/u_math.h"
39 #include "util/u_memory.h"
40 #include "util/u_prim.h"
41
42 #define MAX_PRIM_VERTICES 6
43 /* fixme: move it from here */
44 #define MAX_PRIMITIVES 64
45
46 boolean
47 draw_gs_init( struct draw_context *draw )
48 {
49 draw->gs.machine = tgsi_exec_machine_create();
50 if (!draw->gs.machine)
51 return FALSE;
52
53 draw->gs.machine->Primitives = align_malloc(
54 MAX_PRIMITIVES * sizeof(struct tgsi_exec_vector), 16);
55 if (!draw->gs.machine->Primitives)
56 return FALSE;
57 memset(draw->gs.machine->Primitives, 0,
58 MAX_PRIMITIVES * sizeof(struct tgsi_exec_vector));
59
60 return TRUE;
61 }
62
63 void draw_gs_destroy( struct draw_context *draw )
64 {
65 if (!draw->gs.machine)
66 return;
67
68 align_free(draw->gs.machine->Primitives);
69
70 tgsi_exec_machine_destroy(draw->gs.machine);
71 }
72
73 void
74 draw_gs_set_constants(struct draw_context *draw,
75 unsigned slot,
76 const void *constants,
77 unsigned size)
78 {
79 }
80
81
82 struct draw_geometry_shader *
83 draw_create_geometry_shader(struct draw_context *draw,
84 const struct pipe_shader_state *state)
85 {
86 struct draw_geometry_shader *gs;
87 int i;
88
89 gs = CALLOC_STRUCT(draw_geometry_shader);
90
91 if (!gs)
92 return NULL;
93
94 gs->draw = draw;
95 gs->state = *state;
96 gs->state.tokens = tgsi_dup_tokens(state->tokens);
97 if (!gs->state.tokens) {
98 FREE(gs);
99 return NULL;
100 }
101
102 tgsi_scan_shader(state->tokens, &gs->info);
103
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;
108
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];
119 }
120
121 gs->machine = draw->gs.machine;
122
123 if (gs)
124 {
125 uint i;
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;
130 }
131 }
132
133 return gs;
134 }
135
136 void draw_bind_geometry_shader(struct draw_context *draw,
137 struct draw_geometry_shader *dgs)
138 {
139 draw_do_flush(draw, DRAW_FLUSH_STATE_CHANGE);
140
141 if (dgs) {
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);
146 }
147 else {
148 draw->gs.geometry_shader = NULL;
149 draw->gs.num_gs_outputs = 0;
150 }
151 }
152
153 void draw_delete_geometry_shader(struct draw_context *draw,
154 struct draw_geometry_shader *dgs)
155 {
156 FREE(dgs);
157 }
158
159 static void draw_fetch_geometry_input(struct draw_geometry_shader *shader,
160 int start_primitive,
161 int num_primitives,
162 const float (*input_ptr)[4],
163 unsigned input_vertex_stride,
164 unsigned inputs_from_vs)
165 {
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);
169 int idx = 0;
170
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;
181 }
182 ++idx;
183 } else {
184 for (j = 0; j < num_primitives; ++j) {
185 int vidx = idx;
186 const float (*prim_ptr)[4];
187 /*debug_printf(" %d) Prim (num_verts = %d)\n", start_primitive + j,
188 num_vertices);*/
189 prim_ptr = (const float (*)[4])(
190 (const char *)input_ptr +
191 (j * num_vertices * input_vertex_stride));
192
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);*/
199 #if 1
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]));
204 #endif
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];
209 #if 0
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]);
215 #endif
216 }
217 }
218 ++vs_slot;
219 idx += num_vertices;
220 }
221 }
222 }
223 /*#define DEBUG_OUTPUTS 1*/
224 static INLINE void
225 draw_geometry_fetch_outputs(struct draw_geometry_shader *shader,
226 int num_primitives,
227 float (**p_output)[4],
228 unsigned vertex_size)
229 {
230 struct tgsi_exec_machine *machine = shader->machine;
231 unsigned prim_idx, j, slot;
232 float (*output)[4];
233
234 output = *p_output;
235
236 /* Unswizzle all output results.
237 */
238
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;
246 #ifdef DEBUG_OUTPUTS
247 debug_printf("%d) Output vert:\n", idx / shader->info.num_outputs);
248 #endif
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];
254 #ifdef DEBUG_OUTPUTS
255 debug_printf("\t%d: %f %f %f %f\n", slot,
256 output[slot][0],
257 output[slot][1],
258 output[slot][2],
259 output[slot][3]);
260 #endif
261 debug_assert(!util_is_inf_or_nan(output[slot][0]));
262 }
263 output = (float (*)[4])((char *)output + vertex_size);
264 }
265 }
266 *p_output = output;
267 }
268
269 int draw_geometry_shader_run(struct draw_geometry_shader *shader,
270 unsigned pipe_prim,
271 const float (*input)[4],
272 float (*output)[4],
273 const void *constants[PIPE_MAX_CONSTANT_BUFFERS],
274 unsigned count,
275 unsigned input_stride,
276 unsigned vertex_size)
277 {
278 struct tgsi_exec_machine *machine = shader->machine;
279 unsigned int i;
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,
285 pipe_prim,
286 count);
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);
291
292 if (0) debug_printf("%s count = %d (prims = %d)\n", __FUNCTION__,
293 count, num_in_primitives);
294
295 shader->emitted_vertices = 0;
296 shader->emitted_primitives = 0;
297
298 for (i = 0; i < PIPE_MAX_CONSTANT_BUFFERS; i++) {
299 machine->Consts[i] = constants[i];
300 }
301
302 for (i = 0; i < shader->info.num_inputs; ++i) {
303 if (shader->info.input_semantic_name[i] != TGSI_SEMANTIC_PRIMID)
304 ++inputs_from_vs;
305 }
306
307 tmp_output = ( float (*)[4])pipeline_verts->data;
308
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
312 * the first one */
313 unsigned out_prim_count;
314
315 draw_fetch_geometry_input(shader, i, max_input_primitives, input,
316 input_stride, inputs_from_vs);
317
318 tgsi_set_exec_mask(machine,
319 1,
320 max_input_primitives > 1,
321 max_input_primitives > 2,
322 max_input_primitives > 3);
323
324 /* run interpreter */
325 tgsi_exec_machine_run(machine);
326
327 out_prim_count =
328 machine->Temps[TGSI_EXEC_TEMP_PRIMITIVE_I].xyzw[TGSI_EXEC_TEMP_PRIMITIVE_C].u[0];
329
330 draw_geometry_fetch_outputs(shader, out_prim_count,
331 &tmp_output, vertex_size);
332 }
333
334 memcpy(output, pipeline_verts->data,
335 shader->info.num_outputs * 4 * sizeof(float) +
336 vertex_size * (shader->emitted_vertices -1));
337
338 FREE(pipeline_verts);
339 return shader->emitted_vertices;
340 }
341
342 void draw_geometry_shader_delete(struct draw_geometry_shader *shader)
343 {
344 FREE((void*) shader->state.tokens);
345 FREE(shader);
346 }
347
348 void draw_geometry_shader_prepare(struct draw_geometry_shader *shader,
349 struct draw_context *draw)
350 {
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,
355 draw->gs.samplers);
356 }
357 }
358
359 int draw_max_output_vertices(struct draw_context *draw,
360 unsigned pipe_prim,
361 unsigned count)
362 {
363 unsigned alloc_count = align( count, 4 );
364
365 if (draw->gs.geometry_shader) {
366 unsigned input_primitives = u_gs_prims_for_vertices(pipe_prim,
367 count);
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);
374 }
375 /*debug_printf("------- alloc count = %d (input = %d)\n",
376 alloc_count, count);*/
377 return alloc_count;
378 }