Merge branch 'mesa_7_7_branch'
[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
41 #define MAX_PRIM_VERTICES 6
42 /* fixme: move it from here */
43 #define MAX_PRIMITIVES 64
44
45 boolean
46 draw_gs_init( struct draw_context *draw )
47 {
48 draw->gs.machine = tgsi_exec_machine_create();
49 if (!draw->gs.machine)
50 return FALSE;
51
52 draw->gs.machine->Primitives = align_malloc(
53 MAX_PRIMITIVES * sizeof(struct tgsi_exec_vector), 16);
54 if (!draw->gs.machine->Primitives)
55 return FALSE;
56 memset(draw->gs.machine->Primitives, 0,
57 MAX_PRIMITIVES * sizeof(struct tgsi_exec_vector));
58
59 return TRUE;
60 }
61
62 void draw_gs_destroy( struct draw_context *draw )
63 {
64 if (!draw->gs.machine)
65 return;
66
67 align_free(draw->gs.machine->Primitives);
68
69 tgsi_exec_machine_destroy(draw->gs.machine);
70 }
71
72 void draw_gs_set_constants( struct draw_context *draw,
73 const float (*constants)[4],
74 unsigned size )
75 {
76 }
77
78
79 struct draw_geometry_shader *
80 draw_create_geometry_shader(struct draw_context *draw,
81 const struct pipe_shader_state *state)
82 {
83 struct draw_geometry_shader *gs;
84 int i;
85
86 gs = CALLOC_STRUCT(draw_geometry_shader);
87
88 if (!gs)
89 return NULL;
90
91 gs->state = *state;
92 gs->state.tokens = tgsi_dup_tokens(state->tokens);
93 if (!gs->state.tokens) {
94 FREE(gs);
95 return NULL;
96 }
97
98 tgsi_scan_shader(state->tokens, &gs->info);
99
100 /* setup the defaults */
101 gs->input_primitive = PIPE_PRIM_TRIANGLES;
102 gs->output_primitive = PIPE_PRIM_TRIANGLE_STRIP;
103 gs->max_output_vertices = 32;
104
105 for (i = 0; i < gs->info.num_properties; ++i) {
106 if (gs->info.properties[i].name ==
107 TGSI_PROPERTY_GS_INPUT_PRIM)
108 gs->input_primitive = gs->info.properties[i].data[0];
109 else if (gs->info.properties[i].name ==
110 TGSI_PROPERTY_GS_OUTPUT_PRIM)
111 gs->output_primitive = gs->info.properties[i].data[0];
112 else if (gs->info.properties[i].name ==
113 TGSI_PROPERTY_GS_MAX_VERTICES)
114 gs->max_output_vertices = gs->info.properties[i].data[0];
115 }
116
117 gs->machine = draw->gs.machine;
118
119 if (gs)
120 {
121 uint i;
122 for (i = 0; i < gs->info.num_outputs; i++) {
123 if (gs->info.output_semantic_name[i] == TGSI_SEMANTIC_POSITION &&
124 gs->info.output_semantic_index[i] == 0)
125 gs->position_output = i;
126 }
127 }
128
129 return gs;
130 }
131
132 void draw_bind_geometry_shader(struct draw_context *draw,
133 struct draw_geometry_shader *dgs)
134 {
135 draw_do_flush(draw, DRAW_FLUSH_STATE_CHANGE);
136
137 if (dgs) {
138 draw->gs.geometry_shader = dgs;
139 draw->gs.num_gs_outputs = dgs->info.num_outputs;
140 draw->gs.position_output = dgs->position_output;
141 draw_geometry_shader_prepare(dgs, draw);
142 }
143 else {
144 draw->gs.geometry_shader = NULL;
145 draw->gs.num_gs_outputs = 0;
146 }
147 }
148
149 void draw_delete_geometry_shader(struct draw_context *draw,
150 struct draw_geometry_shader *dgs)
151 {
152 FREE(dgs);
153 }
154
155 static INLINE int num_vertices_for_prim(int prim)
156 {
157 switch(prim) {
158 case PIPE_PRIM_POINTS:
159 return 1;
160 case PIPE_PRIM_LINES:
161 return 2;
162 case PIPE_PRIM_LINE_LOOP:
163 return 2;
164 case PIPE_PRIM_LINE_STRIP:
165 return 2;
166 case PIPE_PRIM_TRIANGLES:
167 return 3;
168 case PIPE_PRIM_TRIANGLE_STRIP:
169 return 3;
170 case PIPE_PRIM_TRIANGLE_FAN:
171 return 3;
172 case PIPE_PRIM_LINES_ADJACENCY:
173 case PIPE_PRIM_LINE_STRIP_ADJACENCY:
174 return 4;
175 case PIPE_PRIM_TRIANGLES_ADJACENCY:
176 case PIPE_PRIM_TRIANGLE_STRIP_ADJACENCY:
177 return 6;
178 default:
179 assert(!"Bad geometry shader input");
180 return 0;
181 }
182 }
183
184 static void draw_fetch_geometry_input(struct draw_geometry_shader *shader,
185 int start_primitive,
186 int num_primitives,
187 const float (*input_ptr)[4],
188 unsigned input_vertex_stride,
189 unsigned inputs_from_vs)
190 {
191 struct tgsi_exec_machine *machine = shader->machine;
192 unsigned slot, vs_slot, k, j;
193 unsigned num_vertices = num_vertices_for_prim(shader->input_primitive);
194 int idx = 0;
195
196 for (slot = 0, vs_slot = 0; slot < shader->info.num_inputs; slot++) {
197 /*debug_printf("Slot = %d (semantic = %d)\n", slot,
198 shader->info.input_semantic_name[slot]);*/
199 if (shader->info.input_semantic_name[slot] ==
200 TGSI_SEMANTIC_PRIMID) {
201 for (j = 0; j < num_primitives; ++j) {
202 machine->Inputs[idx].xyzw[0].f[j] = (float)start_primitive + j;
203 machine->Inputs[idx].xyzw[1].f[j] = (float)start_primitive + j;
204 machine->Inputs[idx].xyzw[2].f[j] = (float)start_primitive + j;
205 machine->Inputs[idx].xyzw[3].f[j] = (float)start_primitive + j;
206 }
207 ++idx;
208 } else {
209 for (j = 0; j < num_primitives; ++j) {
210 int vidx = idx;
211 const float (*prim_ptr)[4];
212 /*debug_printf(" %d) Prim (num_verts = %d)\n", start_primitive + j,
213 num_vertices);*/
214 prim_ptr = (const float (*)[4])(
215 (const char *)input_ptr +
216 (j * num_vertices * input_vertex_stride));
217
218 for (k = 0; k < num_vertices; ++k, ++vidx) {
219 const float (*input)[4];
220 input = (const float (*)[4])(
221 (const char *)prim_ptr + (k * input_vertex_stride));
222 vidx = k * TGSI_EXEC_MAX_INPUT_ATTRIBS + slot;
223 /*debug_printf("\t%d)(%d) Input vert:\n", vidx, k);*/
224 #if 1
225 assert(!util_is_inf_or_nan(input[vs_slot][0]));
226 assert(!util_is_inf_or_nan(input[vs_slot][1]));
227 assert(!util_is_inf_or_nan(input[vs_slot][2]));
228 assert(!util_is_inf_or_nan(input[vs_slot][3]));
229 #endif
230 machine->Inputs[vidx].xyzw[0].f[j] = input[vs_slot][0];
231 machine->Inputs[vidx].xyzw[1].f[j] = input[vs_slot][1];
232 machine->Inputs[vidx].xyzw[2].f[j] = input[vs_slot][2];
233 machine->Inputs[vidx].xyzw[3].f[j] = input[vs_slot][3];
234 #if 0
235 debug_printf("\t\t%d %f %f %f %f\n", slot,
236 machine->Inputs[vidx].xyzw[0].f[j],
237 machine->Inputs[vidx].xyzw[1].f[j],
238 machine->Inputs[vidx].xyzw[2].f[j],
239 machine->Inputs[vidx].xyzw[3].f[j]);
240 #endif
241 }
242 }
243 ++vs_slot;
244 idx += num_vertices;
245 }
246 }
247 }
248
249 static INLINE void
250 draw_geometry_fetch_outputs(struct draw_geometry_shader *shader,
251 int num_primitives,
252 float (*output)[4],
253 unsigned vertex_size)
254 {
255 struct tgsi_exec_machine *machine = shader->machine;
256 unsigned prim_idx, j, slot;
257
258 /* Unswizzle all output results.
259 */
260 /* FIXME: handle all the primitives produced by the gs, not just
261 * the first one
262 unsigned prim_count =
263 mach->Temps[TEMP_PRIMITIVE_I].xyzw[TEMP_PRIMITIVE_C].u[0];*/
264 for (prim_idx = 0; prim_idx < num_primitives; ++prim_idx) {
265 unsigned num_verts_per_prim = machine->Primitives[0];
266 for (j = 0; j < num_verts_per_prim; j++) {
267 int idx = (prim_idx * num_verts_per_prim + j) *
268 shader->info.num_outputs;
269 #ifdef DEBUG_OUTPUTS
270 debug_printf("%d) Output vert:\n", idx);
271 #endif
272 for (slot = 0; slot < shader->info.num_outputs; slot++) {
273 output[slot][0] = machine->Outputs[idx + slot].xyzw[0].f[prim_idx];
274 output[slot][1] = machine->Outputs[idx + slot].xyzw[1].f[prim_idx];
275 output[slot][2] = machine->Outputs[idx + slot].xyzw[2].f[prim_idx];
276 output[slot][3] = machine->Outputs[idx + slot].xyzw[3].f[prim_idx];
277 #ifdef DEBUG_OUTPUTS
278 debug_printf("\t%d: %f %f %f %f\n", slot,
279 output[slot][0],
280 output[slot][1],
281 output[slot][2],
282 output[slot][3]);
283 #endif
284 debug_assert(!util_is_inf_or_nan(output[slot][0]));
285 }
286 output = (float (*)[4])((char *)output + vertex_size);
287 }
288 }
289 }
290
291 void draw_geometry_shader_run(struct draw_geometry_shader *shader,
292 const float (*input)[4],
293 float (*output)[4],
294 const float (*constants)[4],
295 unsigned count,
296 unsigned input_stride,
297 unsigned vertex_size)
298 {
299 struct tgsi_exec_machine *machine = shader->machine;
300 unsigned int i;
301 unsigned num_vertices = num_vertices_for_prim(shader->input_primitive);
302 unsigned num_primitives = count/num_vertices;
303 unsigned inputs_from_vs = 0;
304
305 machine->Consts = constants;
306
307 for (i = 0; i < shader->info.num_inputs; ++i) {
308 if (shader->info.input_semantic_name[i] != TGSI_SEMANTIC_PRIMID)
309 ++inputs_from_vs;
310 }
311
312 for (i = 0; i < num_primitives; ++i) {
313 unsigned int max_primitives = 1;
314
315 draw_fetch_geometry_input(shader, i, max_primitives, input,
316 input_stride, inputs_from_vs);
317
318 tgsi_set_exec_mask(machine,
319 1,
320 max_primitives > 1,
321 max_primitives > 2,
322 max_primitives > 3);
323
324 /* run interpreter */
325 tgsi_exec_machine_run(machine);
326
327 draw_geometry_fetch_outputs(shader, max_primitives,
328 output, vertex_size);
329 }
330 }
331
332 void draw_geometry_shader_delete(struct draw_geometry_shader *shader)
333 {
334 FREE((void*) shader->state.tokens);
335 FREE(shader);
336 }
337
338 void draw_geometry_shader_prepare(struct draw_geometry_shader *shader,
339 struct draw_context *draw)
340 {
341 if (shader->machine->Tokens != shader->state.tokens) {
342 tgsi_exec_machine_bind_shader(shader->machine,
343 shader->state.tokens,
344 draw->gs.num_samplers,
345 draw->gs.samplers);
346 }
347 }