draw/llvmpipe: fix transform feedback position + enable other extensions
[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 &gs_vert_info,
274 &gs_prim_info);
275
276 FREE(vert_info->verts);
277 vert_info = &gs_vert_info;
278 prim_info = &gs_prim_info;
279 }
280
281
282 /* Stream output needs to be done before clipping.
283 *
284 * XXX: Stream output surely needs to respect the prim_info->elt
285 * lists.
286 */
287 draw_pt_so_emit( fpme->so_emit,
288 vert_info,
289 prim_info );
290
291 if (draw_pt_post_vs_run( fpme->post_vs,
292 vert_info ))
293 {
294 opt |= PT_PIPELINE;
295 }
296
297 /* Do we need to run the pipeline?
298 */
299 if (opt & PT_PIPELINE) {
300 pipeline( fpme,
301 vert_info,
302 prim_info );
303 }
304 else {
305 emit( fpme->emit,
306 vert_info,
307 prim_info );
308 }
309 FREE(vert_info->verts);
310 }
311
312 static void fetch_pipeline_run( struct draw_pt_middle_end *middle,
313 const unsigned *fetch_elts,
314 unsigned fetch_count,
315 const ushort *draw_elts,
316 unsigned draw_count,
317 unsigned prim_flags )
318 {
319 struct fetch_pipeline_middle_end *fpme = (struct fetch_pipeline_middle_end *)middle;
320 struct draw_fetch_info fetch_info;
321 struct draw_prim_info prim_info;
322
323 fetch_info.linear = FALSE;
324 fetch_info.start = 0;
325 fetch_info.elts = fetch_elts;
326 fetch_info.count = fetch_count;
327
328 prim_info.linear = FALSE;
329 prim_info.start = 0;
330 prim_info.count = draw_count;
331 prim_info.elts = draw_elts;
332 prim_info.prim = fpme->input_prim;
333 prim_info.flags = prim_flags;
334 prim_info.primitive_count = 1;
335 prim_info.primitive_lengths = &draw_count;
336
337 fetch_pipeline_generic( middle, &fetch_info, &prim_info );
338 }
339
340
341 static void fetch_pipeline_linear_run( struct draw_pt_middle_end *middle,
342 unsigned start,
343 unsigned count,
344 unsigned prim_flags)
345 {
346 struct fetch_pipeline_middle_end *fpme = (struct fetch_pipeline_middle_end *)middle;
347 struct draw_fetch_info fetch_info;
348 struct draw_prim_info prim_info;
349
350 fetch_info.linear = TRUE;
351 fetch_info.start = start;
352 fetch_info.count = count;
353 fetch_info.elts = NULL;
354
355 prim_info.linear = TRUE;
356 prim_info.start = 0;
357 prim_info.count = count;
358 prim_info.elts = NULL;
359 prim_info.prim = fpme->input_prim;
360 prim_info.flags = prim_flags;
361 prim_info.primitive_count = 1;
362 prim_info.primitive_lengths = &count;
363
364 fetch_pipeline_generic( middle, &fetch_info, &prim_info );
365 }
366
367
368
369 static boolean fetch_pipeline_linear_run_elts( struct draw_pt_middle_end *middle,
370 unsigned start,
371 unsigned count,
372 const ushort *draw_elts,
373 unsigned draw_count,
374 unsigned prim_flags )
375 {
376 struct fetch_pipeline_middle_end *fpme = (struct fetch_pipeline_middle_end *)middle;
377 struct draw_fetch_info fetch_info;
378 struct draw_prim_info prim_info;
379
380 fetch_info.linear = TRUE;
381 fetch_info.start = start;
382 fetch_info.count = count;
383 fetch_info.elts = NULL;
384
385 prim_info.linear = FALSE;
386 prim_info.start = 0;
387 prim_info.count = draw_count;
388 prim_info.elts = draw_elts;
389 prim_info.prim = fpme->input_prim;
390 prim_info.flags = prim_flags;
391 prim_info.primitive_count = 1;
392 prim_info.primitive_lengths = &draw_count;
393
394 fetch_pipeline_generic( middle, &fetch_info, &prim_info );
395
396 return TRUE;
397 }
398
399
400
401 static void fetch_pipeline_finish( struct draw_pt_middle_end *middle )
402 {
403 /* nothing to do */
404 }
405
406 static void fetch_pipeline_destroy( struct draw_pt_middle_end *middle )
407 {
408 struct fetch_pipeline_middle_end *fpme = (struct fetch_pipeline_middle_end *)middle;
409
410 if (fpme->fetch)
411 draw_pt_fetch_destroy( fpme->fetch );
412
413 if (fpme->emit)
414 draw_pt_emit_destroy( fpme->emit );
415
416 if (fpme->so_emit)
417 draw_pt_so_emit_destroy( fpme->so_emit );
418
419 if (fpme->post_vs)
420 draw_pt_post_vs_destroy( fpme->post_vs );
421
422 FREE(middle);
423 }
424
425
426 struct draw_pt_middle_end *draw_pt_fetch_pipeline_or_emit( struct draw_context *draw )
427 {
428 struct fetch_pipeline_middle_end *fpme = CALLOC_STRUCT( fetch_pipeline_middle_end );
429 if (!fpme)
430 goto fail;
431
432 fpme->base.prepare = fetch_pipeline_prepare;
433 fpme->base.bind_parameters = fetch_pipeline_bind_parameters;
434 fpme->base.run = fetch_pipeline_run;
435 fpme->base.run_linear = fetch_pipeline_linear_run;
436 fpme->base.run_linear_elts = fetch_pipeline_linear_run_elts;
437 fpme->base.finish = fetch_pipeline_finish;
438 fpme->base.destroy = fetch_pipeline_destroy;
439
440 fpme->draw = draw;
441
442 fpme->fetch = draw_pt_fetch_create( draw );
443 if (!fpme->fetch)
444 goto fail;
445
446 fpme->post_vs = draw_pt_post_vs_create( draw );
447 if (!fpme->post_vs)
448 goto fail;
449
450 fpme->emit = draw_pt_emit_create( draw );
451 if (!fpme->emit)
452 goto fail;
453
454 fpme->so_emit = draw_pt_so_emit_create( draw );
455 if (!fpme->so_emit)
456 goto fail;
457
458 return &fpme->base;
459
460 fail:
461 if (fpme)
462 fetch_pipeline_destroy( &fpme->base );
463
464 return NULL;
465 }