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