draw: finish the new pipeline setup
[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 output_prim;
52 unsigned opt;
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 static void fetch( struct pt_fetch *fetch,
131 const struct draw_fetch_info *fetch_info,
132 char *output)
133 {
134 if (fetch_info->linear) {
135 draw_pt_fetch_run_linear( fetch,
136 fetch_info->start,
137 fetch_info->count,
138 output );
139 }
140 else {
141 draw_pt_fetch_run( fetch,
142 fetch_info->elts,
143 fetch_info->count,
144 output );
145 }
146 }
147
148
149 static void pipeline(struct fetch_pipeline_middle_end *fpme,
150 const struct draw_vertex_info *vert_info,
151 const struct draw_prim_info *prim_info)
152 {
153 if (prim_info->linear)
154 draw_pipeline_run_linear( fpme->draw,
155 vert_info,
156 prim_info);
157 else
158 draw_pipeline_run( fpme->draw,
159 vert_info,
160 prim_info );
161 }
162
163 static void emit(struct pt_emit *emit,
164 const struct draw_vertex_info *vert_info,
165 const struct draw_prim_info *prim_info)
166 {
167 if (prim_info->linear) {
168 draw_pt_emit_linear(emit, vert_info, prim_info);
169 }
170 else {
171 draw_pt_emit(emit, vert_info, prim_info);
172 }
173 }
174
175
176 static void draw_vertex_shader_run(struct draw_vertex_shader *vshader,
177 const void *constants[PIPE_MAX_CONSTANT_BUFFERS],
178 const struct draw_vertex_info *input_verts,
179 struct draw_vertex_info *output_verts )
180 {
181 output_verts->vertex_size = input_verts->vertex_size;
182 output_verts->stride = input_verts->vertex_size;
183 output_verts->count = input_verts->count;
184 output_verts->verts =
185 (struct vertex_header *)MALLOC(output_verts->vertex_size *
186 output_verts->count);
187
188 vshader->run_linear(vshader,
189 (const float (*)[4])input_verts->verts->data,
190 ( float (*)[4])output_verts->verts->data,
191 constants,
192 input_verts->count,
193 input_verts->vertex_size,
194 input_verts->vertex_size);
195 }
196
197 static void fetch_pipeline_generic( struct draw_pt_middle_end *middle,
198 const struct draw_fetch_info *fetch_info,
199 const struct draw_prim_info *prim_info )
200 {
201 struct fetch_pipeline_middle_end *fpme = (struct fetch_pipeline_middle_end *)middle;
202 struct draw_context *draw = fpme->draw;
203 struct draw_vertex_shader *vshader = draw->vs.vertex_shader;
204 struct draw_geometry_shader *gshader = draw->gs.geometry_shader;
205 struct draw_prim_info gs_prim_info;
206 struct draw_vertex_info fetched_vert_info;
207 struct draw_vertex_info vs_vert_info;
208 struct draw_vertex_info gs_vert_info;
209 struct draw_vertex_info *vert_info;
210 unsigned opt = fpme->opt;
211
212 fetched_vert_info.count = fetch_info->count;
213 fetched_vert_info.vertex_size = fpme->vertex_size;
214 fetched_vert_info.stride = fpme->vertex_size;
215 fetched_vert_info.verts =
216 (struct vertex_header *)MALLOC(fpme->vertex_size *
217 align(fetch_info->count, 4));
218 if (!fetched_vert_info.verts) {
219 assert(0);
220 return;
221 }
222
223 /* Fetch into our vertex buffer.
224 */
225 fetch( fpme->fetch, fetch_info, (char *)fetched_vert_info.verts );
226
227 /* Finished with fetch:
228 */
229 fetch_info = NULL;
230 vert_info = &fetched_vert_info;
231
232 /* Run the shader, note that this overwrites the data[] parts of
233 * the pipeline verts.
234 */
235 if (fpme->opt & PT_SHADE) {
236 draw_vertex_shader_run(vshader,
237 draw->pt.user.vs_constants,
238 vert_info,
239 &vs_vert_info);
240
241 FREE(vert_info->verts);
242 vert_info = &vs_vert_info;
243 }
244
245 if ((fpme->opt & PT_SHADE) && gshader) {
246 draw_geometry_shader_run(gshader,
247 draw->pt.user.gs_constants,
248 vert_info,
249 prim_info,
250 &gs_vert_info,
251 &gs_prim_info);
252
253 FREE(vert_info->verts);
254 vert_info = &gs_vert_info;
255 prim_info = &gs_prim_info;
256 }
257
258
259 /* Stream output needs to be done before clipping.
260 *
261 * XXX: Stream output surely needs to respect the prim_info->elt
262 * lists.
263 */
264 draw_pt_so_emit( fpme->so_emit,
265 vert_info,
266 prim_info );
267
268 if (draw_pt_post_vs_run( fpme->post_vs,
269 vert_info ))
270 {
271 opt |= PT_PIPELINE;
272 }
273
274 /* Do we need to run the pipeline?
275 */
276 if (opt & PT_PIPELINE) {
277 pipeline( fpme,
278 vert_info,
279 prim_info );
280 }
281 else {
282 emit( fpme->emit,
283 vert_info,
284 prim_info );
285 }
286 FREE(vert_info->verts);
287 }
288
289 static void fetch_pipeline_run( struct draw_pt_middle_end *middle,
290 const unsigned *fetch_elts,
291 unsigned fetch_count,
292 const ushort *draw_elts,
293 unsigned draw_count )
294 {
295 struct fetch_pipeline_middle_end *fpme = (struct fetch_pipeline_middle_end *)middle;
296 struct draw_fetch_info fetch_info;
297 struct draw_prim_info prim_info;
298
299 fetch_info.linear = FALSE;
300 fetch_info.start = 0;
301 fetch_info.elts = fetch_elts;
302 fetch_info.count = fetch_count;
303
304 prim_info.linear = FALSE;
305 prim_info.start = 0;
306 prim_info.count = draw_count;
307 prim_info.elts = draw_elts;
308 prim_info.prim = fpme->input_prim;
309 prim_info.primitive_count = 1;
310 prim_info.primitive_lengths = &draw_count;
311
312 fetch_pipeline_generic( middle, &fetch_info, &prim_info );
313 }
314
315
316 static void fetch_pipeline_linear_run( struct draw_pt_middle_end *middle,
317 unsigned start,
318 unsigned count)
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 = TRUE;
325 fetch_info.start = start;
326 fetch_info.count = count;
327 fetch_info.elts = NULL;
328
329 prim_info.linear = TRUE;
330 prim_info.start = 0;
331 prim_info.count = count;
332 prim_info.elts = NULL;
333 prim_info.prim = fpme->input_prim;
334 prim_info.primitive_count = 1;
335 prim_info.primitive_lengths = &count;
336
337 fetch_pipeline_generic( middle, &fetch_info, &prim_info );
338 }
339
340
341
342 static boolean fetch_pipeline_linear_run_elts( struct draw_pt_middle_end *middle,
343 unsigned start,
344 unsigned count,
345 const ushort *draw_elts,
346 unsigned draw_count )
347 {
348 struct fetch_pipeline_middle_end *fpme = (struct fetch_pipeline_middle_end *)middle;
349 struct draw_fetch_info fetch_info;
350 struct draw_prim_info prim_info;
351
352 fetch_info.linear = TRUE;
353 fetch_info.start = start;
354 fetch_info.count = count;
355 fetch_info.elts = NULL;
356
357 prim_info.linear = FALSE;
358 prim_info.start = 0;
359 prim_info.count = draw_count;
360 prim_info.elts = draw_elts;
361 prim_info.prim = fpme->input_prim;
362 prim_info.primitive_count = 1;
363 prim_info.primitive_lengths = &draw_count;
364
365 fetch_pipeline_generic( middle, &fetch_info, &prim_info );
366
367 return TRUE;
368 }
369
370
371
372 static void fetch_pipeline_finish( struct draw_pt_middle_end *middle )
373 {
374 /* nothing to do */
375 }
376
377 static void fetch_pipeline_destroy( struct draw_pt_middle_end *middle )
378 {
379 struct fetch_pipeline_middle_end *fpme = (struct fetch_pipeline_middle_end *)middle;
380
381 if (fpme->fetch)
382 draw_pt_fetch_destroy( fpme->fetch );
383
384 if (fpme->emit)
385 draw_pt_emit_destroy( fpme->emit );
386
387 if (fpme->so_emit)
388 draw_pt_so_emit_destroy( fpme->so_emit );
389
390 if (fpme->post_vs)
391 draw_pt_post_vs_destroy( fpme->post_vs );
392
393 FREE(middle);
394 }
395
396
397 struct draw_pt_middle_end *draw_pt_fetch_pipeline_or_emit( struct draw_context *draw )
398 {
399 struct fetch_pipeline_middle_end *fpme = CALLOC_STRUCT( fetch_pipeline_middle_end );
400 if (!fpme)
401 goto fail;
402
403 fpme->base.prepare = fetch_pipeline_prepare;
404 fpme->base.run = fetch_pipeline_run;
405 fpme->base.run_linear = fetch_pipeline_linear_run;
406 fpme->base.run_linear_elts = fetch_pipeline_linear_run_elts;
407 fpme->base.finish = fetch_pipeline_finish;
408 fpme->base.destroy = fetch_pipeline_destroy;
409
410 fpme->draw = draw;
411
412 fpme->fetch = draw_pt_fetch_create( draw );
413 if (!fpme->fetch)
414 goto fail;
415
416 fpme->post_vs = draw_pt_post_vs_create( draw );
417 if (!fpme->post_vs)
418 goto fail;
419
420 fpme->emit = draw_pt_emit_create( draw );
421 if (!fpme->emit)
422 goto fail;
423
424 fpme->so_emit = draw_pt_so_emit_create( draw );
425 if (!fpme->so_emit)
426 goto fail;
427
428 return &fpme->base;
429
430 fail:
431 if (fpme)
432 fetch_pipeline_destroy( &fpme->base );
433
434 return NULL;
435 }