15d4c5c15d275912344d3e36fad3e31bec6d7a57
[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->state = *state;
95 gs->state.tokens = tgsi_dup_tokens(state->tokens);
96 if (!gs->state.tokens) {
97 FREE(gs);
98 return NULL;
99 }
100
101 tgsi_scan_shader(state->tokens, &gs->info);
102
103 /* setup the defaults */
104 gs->input_primitive = PIPE_PRIM_TRIANGLES;
105 gs->output_primitive = PIPE_PRIM_TRIANGLE_STRIP;
106 gs->max_output_vertices = 32;
107
108 for (i = 0; i < gs->info.num_properties; ++i) {
109 if (gs->info.properties[i].name ==
110 TGSI_PROPERTY_GS_INPUT_PRIM)
111 gs->input_primitive = gs->info.properties[i].data[0];
112 else if (gs->info.properties[i].name ==
113 TGSI_PROPERTY_GS_OUTPUT_PRIM)
114 gs->output_primitive = gs->info.properties[i].data[0];
115 else if (gs->info.properties[i].name ==
116 TGSI_PROPERTY_GS_MAX_OUTPUT_VERTICES)
117 gs->max_output_vertices = gs->info.properties[i].data[0];
118 }
119
120 gs->machine = draw->gs.machine;
121
122 if (gs)
123 {
124 uint i;
125 for (i = 0; i < gs->info.num_outputs; i++) {
126 if (gs->info.output_semantic_name[i] == TGSI_SEMANTIC_POSITION &&
127 gs->info.output_semantic_index[i] == 0)
128 gs->position_output = i;
129 }
130 }
131
132 return gs;
133 }
134
135 void draw_bind_geometry_shader(struct draw_context *draw,
136 struct draw_geometry_shader *dgs)
137 {
138 draw_do_flush(draw, DRAW_FLUSH_STATE_CHANGE);
139
140 if (dgs) {
141 draw->gs.geometry_shader = dgs;
142 draw->gs.num_gs_outputs = dgs->info.num_outputs;
143 draw->gs.position_output = dgs->position_output;
144 draw_geometry_shader_prepare(dgs, draw);
145 }
146 else {
147 draw->gs.geometry_shader = NULL;
148 draw->gs.num_gs_outputs = 0;
149 }
150 }
151
152 void draw_delete_geometry_shader(struct draw_context *draw,
153 struct draw_geometry_shader *dgs)
154 {
155 FREE(dgs);
156 }
157
158 static void draw_fetch_geometry_input(struct draw_geometry_shader *shader,
159 int start_primitive,
160 int num_primitives,
161 const float (*input_ptr)[4],
162 unsigned input_vertex_stride,
163 unsigned inputs_from_vs)
164 {
165 struct tgsi_exec_machine *machine = shader->machine;
166 unsigned slot, vs_slot, k, j;
167 unsigned num_vertices = u_vertices_per_prim(shader->input_primitive);
168 int idx = 0;
169
170 for (slot = 0, vs_slot = 0; slot < shader->info.num_inputs; slot++) {
171 /*debug_printf("Slot = %d (semantic = %d)\n", slot,
172 shader->info.input_semantic_name[slot]);*/
173 if (shader->info.input_semantic_name[slot] ==
174 TGSI_SEMANTIC_PRIMID) {
175 for (j = 0; j < num_primitives; ++j) {
176 machine->Inputs[idx].xyzw[0].f[j] = (float)start_primitive + j;
177 machine->Inputs[idx].xyzw[1].f[j] = (float)start_primitive + j;
178 machine->Inputs[idx].xyzw[2].f[j] = (float)start_primitive + j;
179 machine->Inputs[idx].xyzw[3].f[j] = (float)start_primitive + j;
180 }
181 ++idx;
182 } else {
183 for (j = 0; j < num_primitives; ++j) {
184 int vidx = idx;
185 const float (*prim_ptr)[4];
186 /*debug_printf(" %d) Prim (num_verts = %d)\n", start_primitive + j,
187 num_vertices);*/
188 prim_ptr = (const float (*)[4])(
189 (const char *)input_ptr +
190 (j * num_vertices * input_vertex_stride));
191
192 for (k = 0; k < num_vertices; ++k, ++vidx) {
193 const float (*input)[4];
194 input = (const float (*)[4])(
195 (const char *)prim_ptr + (k * input_vertex_stride));
196 vidx = k * TGSI_EXEC_MAX_INPUT_ATTRIBS + slot;
197 /*debug_printf("\t%d)(%d) Input vert:\n", vidx, k);*/
198 #if 1
199 assert(!util_is_inf_or_nan(input[vs_slot][0]));
200 assert(!util_is_inf_or_nan(input[vs_slot][1]));
201 assert(!util_is_inf_or_nan(input[vs_slot][2]));
202 assert(!util_is_inf_or_nan(input[vs_slot][3]));
203 #endif
204 machine->Inputs[vidx].xyzw[0].f[j] = input[vs_slot][0];
205 machine->Inputs[vidx].xyzw[1].f[j] = input[vs_slot][1];
206 machine->Inputs[vidx].xyzw[2].f[j] = input[vs_slot][2];
207 machine->Inputs[vidx].xyzw[3].f[j] = input[vs_slot][3];
208 #if 0
209 debug_printf("\t\t%d %f %f %f %f\n", slot,
210 machine->Inputs[vidx].xyzw[0].f[j],
211 machine->Inputs[vidx].xyzw[1].f[j],
212 machine->Inputs[vidx].xyzw[2].f[j],
213 machine->Inputs[vidx].xyzw[3].f[j]);
214 #endif
215 }
216 }
217 ++vs_slot;
218 idx += num_vertices;
219 }
220 }
221 }
222 /*#define DEBUG_OUTPUTS 1*/
223 static INLINE void
224 draw_geometry_fetch_outputs(struct draw_geometry_shader *shader,
225 int num_primitives,
226 float (*output)[4],
227 unsigned vertex_size)
228 {
229 struct tgsi_exec_machine *machine = shader->machine;
230 unsigned prim_idx, j, slot;
231
232 /* Unswizzle all output results.
233 */
234
235 shader->emitted_primitives += num_primitives;
236 for (prim_idx = 0; prim_idx < num_primitives; ++prim_idx) {
237 unsigned num_verts_per_prim = machine->Primitives[prim_idx];
238 shader->emitted_vertices += num_verts_per_prim;
239 for (j = 0; j < num_verts_per_prim; j++) {
240 int idx = (prim_idx * num_verts_per_prim + j) *
241 shader->info.num_outputs;
242 #ifdef DEBUG_OUTPUTS
243 debug_printf("%d) Output vert:\n", idx);
244 #endif
245 for (slot = 0; slot < shader->info.num_outputs; slot++) {
246 output[slot][0] = machine->Outputs[idx + slot].xyzw[0].f[0];
247 output[slot][1] = machine->Outputs[idx + slot].xyzw[1].f[0];
248 output[slot][2] = machine->Outputs[idx + slot].xyzw[2].f[0];
249 output[slot][3] = machine->Outputs[idx + slot].xyzw[3].f[0];
250 #ifdef DEBUG_OUTPUTS
251 debug_printf("\t%d: %f %f %f %f\n", slot,
252 output[slot][0],
253 output[slot][1],
254 output[slot][2],
255 output[slot][3]);
256 #endif
257 debug_assert(!util_is_inf_or_nan(output[slot][0]));
258 }
259 output = (float (*)[4])((char *)output + vertex_size);
260 }
261 }
262 }
263
264 int draw_geometry_shader_run(struct draw_geometry_shader *shader,
265 const float (*input)[4],
266 float (*output)[4],
267 const void *constants[PIPE_MAX_CONSTANT_BUFFERS],
268 unsigned count,
269 unsigned input_stride,
270 unsigned vertex_size)
271 {
272 struct tgsi_exec_machine *machine = shader->machine;
273 unsigned int i;
274 unsigned num_in_vertices = u_vertices_per_prim(shader->input_primitive);
275 unsigned num_in_primitives = count/num_in_vertices;
276 unsigned inputs_from_vs = 0;
277
278 if (0) debug_printf("%s count = %d\n", __FUNCTION__, count);
279
280 shader->emitted_vertices = 0;
281 shader->emitted_primitives = 0;
282
283 for (i = 0; i < PIPE_MAX_CONSTANT_BUFFERS; i++) {
284 machine->Consts[i] = constants[i];
285 }
286
287 for (i = 0; i < shader->info.num_inputs; ++i) {
288 if (shader->info.input_semantic_name[i] != TGSI_SEMANTIC_PRIMID)
289 ++inputs_from_vs;
290 }
291
292 for (i = 0; i < num_in_primitives; ++i) {
293 unsigned int max_input_primitives = 1;
294 /* FIXME: handle all the primitives produced by the gs, not just
295 * the first one */
296 unsigned out_prim_count;
297
298 draw_fetch_geometry_input(shader, i, max_input_primitives, input,
299 input_stride, inputs_from_vs);
300
301 tgsi_set_exec_mask(machine,
302 1,
303 max_input_primitives > 1,
304 max_input_primitives > 2,
305 max_input_primitives > 3);
306
307 /* run interpreter */
308 tgsi_exec_machine_run(machine);
309
310 out_prim_count =
311 machine->Temps[TGSI_EXEC_TEMP_PRIMITIVE_I].xyzw[TGSI_EXEC_TEMP_PRIMITIVE_C].u[0];
312
313 draw_geometry_fetch_outputs(shader, out_prim_count,
314 output, vertex_size);
315 }
316 return shader->emitted_vertices;
317 }
318
319 void draw_geometry_shader_delete(struct draw_geometry_shader *shader)
320 {
321 FREE((void*) shader->state.tokens);
322 FREE(shader);
323 }
324
325 void draw_geometry_shader_prepare(struct draw_geometry_shader *shader,
326 struct draw_context *draw)
327 {
328 if (shader && shader->machine->Tokens != shader->state.tokens) {
329 tgsi_exec_machine_bind_shader(shader->machine,
330 shader->state.tokens,
331 draw->gs.num_samplers,
332 draw->gs.samplers);
333 }
334 }