geometry shaders: make gs work with changable primitives and variable number of vertices
[mesa.git] / src / gallium / auxiliary / draw / draw_pt_fetch_shade_pipeline.c
1 /**************************************************************************
2 *
3 * Copyright 2007 Tungsten Graphics, Inc., Cedar Park, Texas.
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 "util/u_math.h"
29 #include "util/u_memory.h"
30 #include "draw/draw_context.h"
31 #include "draw/draw_vbuf.h"
32 #include "draw/draw_vertex.h"
33 #include "draw/draw_pt.h"
34 #include "draw/draw_vs.h"
35 #include "draw/draw_gs.h"
36
37
38 struct fetch_pipeline_middle_end {
39 struct draw_pt_middle_end base;
40 struct draw_context *draw;
41
42 struct pt_emit *emit;
43 struct pt_so_emit *so_emit;
44 struct pt_fetch *fetch;
45 struct pt_post_vs *post_vs;
46
47 unsigned vertex_data_offset;
48 unsigned vertex_size;
49 unsigned input_prim;
50 unsigned output_prim;
51 unsigned opt;
52 };
53
54
55 static void fetch_pipeline_prepare( struct draw_pt_middle_end *middle,
56 unsigned in_prim,
57 unsigned out_prim,
58 unsigned opt,
59 unsigned *max_vertices )
60 {
61 struct fetch_pipeline_middle_end *fpme = (struct fetch_pipeline_middle_end *)middle;
62 struct draw_context *draw = fpme->draw;
63 struct draw_vertex_shader *vs = draw->vs.vertex_shader;
64 unsigned i;
65 unsigned instance_id_index = ~0;
66
67 /* Add one to num_outputs because the pipeline occasionally tags on
68 * an additional texcoord, eg for AA lines.
69 */
70 unsigned nr = MAX2( vs->info.num_inputs,
71 vs->info.num_outputs + 1 );
72
73 /* Scan for instanceID system value.
74 */
75 for (i = 0; i < vs->info.num_inputs; i++) {
76 if (vs->info.input_semantic_name[i] == TGSI_SEMANTIC_INSTANCEID) {
77 instance_id_index = i;
78 break;
79 }
80 }
81
82 fpme->input_prim = in_prim;
83 fpme->output_prim = out_prim;
84 fpme->opt = opt;
85
86 /* Always leave room for the vertex header whether we need it or
87 * not. It's hard to get rid of it in particular because of the
88 * viewport code in draw_pt_post_vs.c.
89 */
90 fpme->vertex_size = sizeof(struct vertex_header) + nr * 4 * sizeof(float);
91
92
93
94 draw_pt_fetch_prepare( fpme->fetch,
95 vs->info.num_inputs,
96 fpme->vertex_size,
97 instance_id_index );
98 /* XXX: it's not really gl rasterization rules we care about here,
99 * but gl vs dx9 clip spaces.
100 */
101 draw_pt_post_vs_prepare( fpme->post_vs,
102 (boolean)draw->bypass_clipping,
103 (boolean)draw->identity_viewport,
104 (boolean)draw->rasterizer->gl_rasterization_rules,
105 (draw->vs.edgeflag_output ? true : false) );
106
107 draw_pt_so_emit_prepare( fpme->so_emit, out_prim );
108
109 if (!(opt & PT_PIPELINE)) {
110 draw_pt_emit_prepare( fpme->emit,
111 out_prim,
112 max_vertices );
113
114 *max_vertices = MAX2( *max_vertices,
115 DRAW_PIPE_MAX_VERTICES );
116 }
117 else {
118 *max_vertices = DRAW_PIPE_MAX_VERTICES;
119 }
120
121 /* return even number */
122 *max_vertices = *max_vertices & ~1;
123
124 /* No need to prepare the shader.
125 */
126 vs->prepare(vs, draw);
127 }
128
129
130
131 static void fetch_pipeline_run( struct draw_pt_middle_end *middle,
132 const unsigned *fetch_elts,
133 unsigned fetch_count,
134 const ushort *draw_elts,
135 unsigned draw_count )
136 {
137 struct fetch_pipeline_middle_end *fpme = (struct fetch_pipeline_middle_end *)middle;
138 struct draw_context *draw = fpme->draw;
139 struct draw_vertex_shader *vshader = draw->vs.vertex_shader;
140 struct draw_geometry_shader *gshader = draw->gs.geometry_shader;
141 unsigned opt = fpme->opt;
142 struct vertex_header *pipeline_verts;
143 unsigned alloc_count = align( fetch_count, 4 );
144
145 if (draw->gs.geometry_shader &&
146 draw->gs.geometry_shader->max_output_vertices > fetch_count) {
147 alloc_count = align(draw->gs.geometry_shader->max_output_vertices, 4);
148 }
149
150 pipeline_verts =
151 (struct vertex_header *)MALLOC(fpme->vertex_size * alloc_count);
152
153 if (!pipeline_verts) {
154 /* Not much we can do here - just skip the rendering.
155 */
156 assert(0);
157 return;
158 }
159
160 /* Fetch into our vertex buffer
161 */
162 draw_pt_fetch_run( fpme->fetch,
163 fetch_elts,
164 fetch_count,
165 (char *)pipeline_verts );
166
167 /* Run the shader, note that this overwrites the data[] parts of
168 * the pipeline verts.
169 */
170 if (opt & PT_SHADE)
171 {
172 vshader->run_linear(vshader,
173 (const float (*)[4])pipeline_verts->data,
174 ( float (*)[4])pipeline_verts->data,
175 draw->pt.user.vs_constants,
176 fetch_count,
177 fpme->vertex_size,
178 fpme->vertex_size);
179 if (gshader)
180 fetch_count =
181 draw_geometry_shader_run(gshader,
182 (const float (*)[4])pipeline_verts->data,
183 ( float (*)[4])pipeline_verts->data,
184 draw->pt.user.gs_constants,
185 fetch_count,
186 fpme->vertex_size,
187 fpme->vertex_size);
188 }
189
190 /* stream output needs to be done before clipping */
191 draw_pt_so_emit( fpme->so_emit,
192 (const float (*)[4])pipeline_verts->data,
193 fetch_count,
194 fpme->vertex_size );
195
196 if (draw_pt_post_vs_run( fpme->post_vs,
197 pipeline_verts,
198 fetch_count,
199 fpme->vertex_size ))
200 {
201 opt |= PT_PIPELINE;
202 }
203
204 /* Do we need to run the pipeline?
205 */
206 if (opt & PT_PIPELINE) {
207 draw_pipeline_run( fpme->draw,
208 fpme->output_prim,
209 pipeline_verts,
210 fetch_count,
211 fpme->vertex_size,
212 draw_elts,
213 draw_count );
214 }
215 else {
216 draw_pt_emit( fpme->emit,
217 (const float (*)[4])pipeline_verts->data,
218 fetch_count,
219 fpme->vertex_size,
220 draw_elts,
221 draw_count );
222 }
223
224
225 FREE(pipeline_verts);
226 }
227
228
229 static void fetch_pipeline_linear_run( struct draw_pt_middle_end *middle,
230 unsigned start,
231 unsigned count)
232 {
233 struct fetch_pipeline_middle_end *fpme = (struct fetch_pipeline_middle_end *)middle;
234 struct draw_context *draw = fpme->draw;
235 struct draw_vertex_shader *shader = draw->vs.vertex_shader;
236 struct draw_geometry_shader *geometry_shader = draw->gs.geometry_shader;
237 unsigned opt = fpme->opt;
238 struct vertex_header *pipeline_verts;
239 unsigned alloc_count = align( count, 4 );
240
241 if (geometry_shader && geometry_shader->max_output_vertices > count) {
242 alloc_count = align(geometry_shader->max_output_vertices, 4);
243 }
244
245 pipeline_verts =
246 (struct vertex_header *)MALLOC(fpme->vertex_size * alloc_count);
247
248 if (!pipeline_verts) {
249 /* Not much we can do here - just skip the rendering.
250 */
251 assert(0);
252 return;
253 }
254
255 /* Fetch into our vertex buffer
256 */
257 draw_pt_fetch_run_linear( fpme->fetch,
258 start,
259 count,
260 (char *)pipeline_verts );
261
262 /* Run the shader, note that this overwrites the data[] parts of
263 * the pipeline verts.
264 */
265 if (opt & PT_SHADE)
266 {
267 shader->run_linear(shader,
268 (const float (*)[4])pipeline_verts->data,
269 ( float (*)[4])pipeline_verts->data,
270 draw->pt.user.vs_constants,
271 count,
272 fpme->vertex_size,
273 fpme->vertex_size);
274
275 if (geometry_shader)
276 count = draw_geometry_shader_run(geometry_shader,
277 (const float (*)[4])pipeline_verts->data,
278 ( float (*)[4])pipeline_verts->data,
279 draw->pt.user.gs_constants,
280 count,
281 fpme->vertex_size,
282 fpme->vertex_size);
283 }
284
285 /* stream output needs to be done before clipping */
286 draw_pt_so_emit( fpme->so_emit,
287 (const float (*)[4])pipeline_verts->data,
288 count,
289 fpme->vertex_size );
290
291 if (draw_pt_post_vs_run( fpme->post_vs,
292 pipeline_verts,
293 count,
294 fpme->vertex_size ))
295 {
296 opt |= PT_PIPELINE;
297 }
298
299 /* Do we need to run the pipeline?
300 */
301 if (opt & PT_PIPELINE) {
302 draw_pipeline_run_linear( fpme->draw,
303 fpme->output_prim,
304 pipeline_verts,
305 count,
306 fpme->vertex_size);
307 }
308 else {
309 draw_pt_emit_linear( fpme->emit,
310 (const float (*)[4])pipeline_verts->data,
311 fpme->vertex_size,
312 count );
313 }
314
315 FREE(pipeline_verts);
316 }
317
318
319
320 static boolean fetch_pipeline_linear_run_elts( struct draw_pt_middle_end *middle,
321 unsigned start,
322 unsigned count,
323 const ushort *draw_elts,
324 unsigned draw_count )
325 {
326 struct fetch_pipeline_middle_end *fpme = (struct fetch_pipeline_middle_end *)middle;
327 struct draw_context *draw = fpme->draw;
328 struct draw_vertex_shader *shader = draw->vs.vertex_shader;
329 struct draw_geometry_shader *geometry_shader = draw->gs.geometry_shader;
330 unsigned opt = fpme->opt;
331 struct vertex_header *pipeline_verts;
332 unsigned alloc_count = align( count, 4 );
333
334 if (draw->gs.geometry_shader &&
335 draw->gs.geometry_shader->max_output_vertices > count) {
336 alloc_count = align(draw->gs.geometry_shader->max_output_vertices, 4);
337 }
338
339 pipeline_verts =
340 (struct vertex_header *)MALLOC(fpme->vertex_size * alloc_count);
341
342 if (!pipeline_verts)
343 return FALSE;
344
345 /* Fetch into our vertex buffer
346 */
347 draw_pt_fetch_run_linear( fpme->fetch,
348 start,
349 count,
350 (char *)pipeline_verts );
351
352 /* Run the shader, note that this overwrites the data[] parts of
353 * the pipeline verts.
354 */
355 if (opt & PT_SHADE)
356 {
357 shader->run_linear(shader,
358 (const float (*)[4])pipeline_verts->data,
359 ( float (*)[4])pipeline_verts->data,
360 draw->pt.user.vs_constants,
361 count,
362 fpme->vertex_size,
363 fpme->vertex_size);
364
365 if (geometry_shader)
366 count = draw_geometry_shader_run(geometry_shader,
367 (const float (*)[4])pipeline_verts->data,
368 ( float (*)[4])pipeline_verts->data,
369 draw->pt.user.gs_constants,
370 count,
371 fpme->vertex_size,
372 fpme->vertex_size);
373 }
374
375 /* stream output needs to be done before clipping */
376 draw_pt_so_emit( fpme->so_emit,
377 (const float (*)[4])pipeline_verts->data,
378 count,
379 fpme->vertex_size );
380
381 if (draw_pt_post_vs_run( fpme->post_vs,
382 pipeline_verts,
383 count,
384 fpme->vertex_size ))
385 {
386 opt |= PT_PIPELINE;
387 }
388
389 /* Do we need to run the pipeline?
390 */
391 if (opt & PT_PIPELINE) {
392 draw_pipeline_run( fpme->draw,
393 fpme->output_prim,
394 pipeline_verts,
395 count,
396 fpme->vertex_size,
397 draw_elts,
398 draw_count );
399 }
400 else {
401 draw_pt_emit( fpme->emit,
402 (const float (*)[4])pipeline_verts->data,
403 count,
404 fpme->vertex_size,
405 draw_elts,
406 draw_count );
407 }
408
409 FREE(pipeline_verts);
410 return TRUE;
411 }
412
413
414
415 static void fetch_pipeline_finish( struct draw_pt_middle_end *middle )
416 {
417 /* nothing to do */
418 }
419
420 static void fetch_pipeline_destroy( struct draw_pt_middle_end *middle )
421 {
422 struct fetch_pipeline_middle_end *fpme = (struct fetch_pipeline_middle_end *)middle;
423
424 if (fpme->fetch)
425 draw_pt_fetch_destroy( fpme->fetch );
426
427 if (fpme->emit)
428 draw_pt_emit_destroy( fpme->emit );
429
430 if (fpme->so_emit)
431 draw_pt_so_emit_destroy( fpme->so_emit );
432
433 if (fpme->post_vs)
434 draw_pt_post_vs_destroy( fpme->post_vs );
435
436 FREE(middle);
437 }
438
439
440 struct draw_pt_middle_end *draw_pt_fetch_pipeline_or_emit( struct draw_context *draw )
441 {
442 struct fetch_pipeline_middle_end *fpme = CALLOC_STRUCT( fetch_pipeline_middle_end );
443 if (!fpme)
444 goto fail;
445
446 fpme->base.prepare = fetch_pipeline_prepare;
447 fpme->base.run = fetch_pipeline_run;
448 fpme->base.run_linear = fetch_pipeline_linear_run;
449 fpme->base.run_linear_elts = fetch_pipeline_linear_run_elts;
450 fpme->base.finish = fetch_pipeline_finish;
451 fpme->base.destroy = fetch_pipeline_destroy;
452
453 fpme->draw = draw;
454
455 fpme->fetch = draw_pt_fetch_create( draw );
456 if (!fpme->fetch)
457 goto fail;
458
459 fpme->post_vs = draw_pt_post_vs_create( draw );
460 if (!fpme->post_vs)
461 goto fail;
462
463 fpme->emit = draw_pt_emit_create( draw );
464 if (!fpme->emit)
465 goto fail;
466
467 fpme->so_emit = draw_pt_so_emit_create( draw );
468 if (!fpme->so_emit)
469 goto fail;
470
471 return &fpme->base;
472
473 fail:
474 if (fpme)
475 fetch_pipeline_destroy( &fpme->base );
476
477 return NULL;
478 }