draw wip
[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
211 fetched_vert_info.count = fetch_info->count;
212 fetched_vert_info.vertex_size = fpme->vertex_size;
213 fetched_vert_info.verts =
214 (struct vertex_header *)MALLOC(fetched_vert_info.vertex_size *
215 fetch_info->count);
216 if (!fetched_vert_info.verts) {
217 assert(0);
218 return;
219 }
220
221 /* Fetch into our vertex buffer.
222 */
223 fetch( fpme->fetch, fetch_info, (char *)fetched_vert_info.verts );
224
225 /* Finished with fetch:
226 */
227 fetch_info = NULL;
228 vert_info = &fetched_vert_info;
229
230 /* Run the shader, note that this overwrites the data[] parts of
231 * the pipeline verts.
232 */
233 if (fpme->opt & PT_SHADE) {
234 draw_vertex_shader_run(vshader,
235 draw->pt.user.vs_constants,
236 vert_info,
237 &vs_vert_info);
238
239 FREE(vert_info->verts);
240 vert_info = &vs_vert_info;
241 }
242
243 if ((fpme->opt & PT_SHADE) && gshader) {
244 draw_geometry_shader_run(gshader,
245 draw->pt.user.gs_constants,
246 vert_info,
247 prim_info,
248 &gs_vert_info,
249 &gs_prim_info);
250
251
252 FREE(vert_info->verts);
253 vert_info = &gs_vert_info;
254 prim_info = &gs_prim_info;
255 }
256
257
258 /* Stream output needs to be done before clipping.
259 *
260 * XXX: Stream output surely needs to respect the prim_info->elt
261 * lists.
262 */
263 draw_pt_so_emit( fpme->so_emit,
264 vert_info,
265 prim_info );
266
267 if (draw_pt_post_vs_run( fpme->post_vs,
268 vert_info ))
269 {
270 opt |= PT_PIPELINE;
271 }
272
273 /* Do we need to run the pipeline?
274 */
275 if (opt & PT_PIPELINE) {
276 pipeline( fpme->draw,
277 vert_info,
278 prim_info );
279 }
280 else {
281 emit( fpme->emit,
282 vert_info,
283 prim_info );
284 }
285
286 }
287
288 static void fetch_pipeline_run( struct draw_pt_middle_end *middle,
289 const unsigned *fetch_elts,
290 unsigned fetch_count,
291 const ushort *draw_elts,
292 unsigned draw_count )
293 {
294 struct draw_fetch_info fetch_info;
295 struct draw_prim_info prim_info;
296
297 fetch_info.linear = FALSE;
298 fetch_info.start = 0;
299 fetch_info.elts = fetch_elts;
300 fetch_info.count = fetch_count;
301
302 prim_info.linear = FALSE;
303 prim_info.start = 0;
304 prim_info.count = draw_count;
305 prim_info.elts = draw_elts;
306
307 fetch_pipeline_generic( middle, &fetch_info, &prim_info );
308 }
309
310
311 static void fetch_pipeline_linear_run( struct draw_pt_middle_end *middle,
312 unsigned start,
313 unsigned count)
314 {
315 struct draw_fetch_info fetch_info;
316 struct draw_prim_info prim_info;
317
318 fetch_info.linear = TRUE;
319 fetch_info.start = start;
320 fetch_info.count = count;
321 fetch_info.elts = NULL;
322
323 prim_info.linear = TRUE;
324 prim_info.start = 0;
325 prim_info.count = count;
326 prim_info.elts = NULL;
327
328 fetch_pipeline_generic( middle, &fetch_info, &prim_info );
329 }
330
331
332
333 static boolean fetch_pipeline_linear_run_elts( struct draw_pt_middle_end *middle,
334 unsigned start,
335 unsigned count,
336 const ushort *draw_elts,
337 unsigned draw_count )
338 {
339 struct draw_fetch_info fetch_info;
340 struct draw_prim_info prim_info;
341
342 fetch_info.linear = TRUE;
343 fetch_info.start = start;
344 fetch_info.count = count;
345 fetch_info.elts = NULL;
346
347 prim_info.linear = FALSE;
348 prim_info.start = 0;
349 prim_info.count = draw_count;
350 prim_info.elts = draw_elts;
351
352 fetch_pipeline_generic( middle, &fetch_info, &prim_info );
353 }
354
355
356
357 static void fetch_pipeline_finish( struct draw_pt_middle_end *middle )
358 {
359 /* nothing to do */
360 }
361
362 static void fetch_pipeline_destroy( struct draw_pt_middle_end *middle )
363 {
364 struct fetch_pipeline_middle_end *fpme = (struct fetch_pipeline_middle_end *)middle;
365
366 if (fpme->fetch)
367 draw_pt_fetch_destroy( fpme->fetch );
368
369 if (fpme->emit)
370 draw_pt_emit_destroy( fpme->emit );
371
372 if (fpme->so_emit)
373 draw_pt_so_emit_destroy( fpme->so_emit );
374
375 if (fpme->post_vs)
376 draw_pt_post_vs_destroy( fpme->post_vs );
377
378 FREE(middle);
379 }
380
381
382 struct draw_pt_middle_end *draw_pt_fetch_pipeline_or_emit( struct draw_context *draw )
383 {
384 struct fetch_pipeline_middle_end *fpme = CALLOC_STRUCT( fetch_pipeline_middle_end );
385 if (!fpme)
386 goto fail;
387
388 fpme->base.prepare = fetch_pipeline_prepare;
389 fpme->base.run = fetch_pipeline_run;
390 fpme->base.run_linear = fetch_pipeline_linear_run;
391 fpme->base.run_linear_elts = fetch_pipeline_linear_run_elts;
392 fpme->base.finish = fetch_pipeline_finish;
393 fpme->base.destroy = fetch_pipeline_destroy;
394
395 fpme->draw = draw;
396
397 fpme->fetch = draw_pt_fetch_create( draw );
398 if (!fpme->fetch)
399 goto fail;
400
401 fpme->post_vs = draw_pt_post_vs_create( draw );
402 if (!fpme->post_vs)
403 goto fail;
404
405 fpme->emit = draw_pt_emit_create( draw );
406 if (!fpme->emit)
407 goto fail;
408
409 fpme->so_emit = draw_pt_so_emit_create( draw );
410 if (!fpme->so_emit)
411 goto fail;
412
413 return &fpme->base;
414
415 fail:
416 if (fpme)
417 fetch_pipeline_destroy( &fpme->base );
418
419 return NULL;
420 }