draw: implement pipeline statistics in the draw module
[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 if (draw->collect_statistics) {
244 draw->statistics.ia_vertices += fetch_info->count;
245 draw->statistics.ia_primitives +=
246 u_decomposed_prims_for_vertices(prim_info->prim, fetch_info->count);
247 draw->statistics.vs_invocations += fetch_info->count;
248 }
249
250 /* Fetch into our vertex buffer.
251 */
252 fetch( fpme->fetch, fetch_info, (char *)fetched_vert_info.verts );
253
254 /* Finished with fetch:
255 */
256 fetch_info = NULL;
257 vert_info = &fetched_vert_info;
258
259 /* Run the shader, note that this overwrites the data[] parts of
260 * the pipeline verts.
261 */
262 if (fpme->opt & PT_SHADE) {
263 draw_vertex_shader_run(vshader,
264 draw->pt.user.vs_constants,
265 draw->pt.user.vs_constants_size,
266 vert_info,
267 &vs_vert_info);
268
269 FREE(vert_info->verts);
270 vert_info = &vs_vert_info;
271 }
272
273 if ((fpme->opt & PT_SHADE) && gshader) {
274 draw_geometry_shader_run(gshader,
275 draw->pt.user.gs_constants,
276 draw->pt.user.gs_constants_size,
277 vert_info,
278 prim_info,
279 &vshader->info,
280 &gs_vert_info,
281 &gs_prim_info);
282
283 FREE(vert_info->verts);
284 vert_info = &gs_vert_info;
285 prim_info = &gs_prim_info;
286 }
287
288
289 /* Stream output needs to be done before clipping.
290 *
291 * XXX: Stream output surely needs to respect the prim_info->elt
292 * lists.
293 */
294 draw_pt_so_emit( fpme->so_emit, vert_info, prim_info );
295
296 /*
297 * if there's no position, need to stop now, or the latter stages
298 * will try to access non-existent position output.
299 */
300 if (draw_current_shader_position_output(draw) != -1) {
301
302 if (draw_pt_post_vs_run( fpme->post_vs, vert_info ))
303 {
304 opt |= PT_PIPELINE;
305 }
306
307 /* Do we need to run the pipeline?
308 */
309 if (opt & PT_PIPELINE) {
310 pipeline( fpme, vert_info, prim_info );
311 }
312 else {
313 emit( fpme->emit, vert_info, prim_info );
314 }
315 }
316 FREE(vert_info->verts);
317 }
318
319 static void fetch_pipeline_run( struct draw_pt_middle_end *middle,
320 const unsigned *fetch_elts,
321 unsigned fetch_count,
322 const ushort *draw_elts,
323 unsigned draw_count,
324 unsigned prim_flags )
325 {
326 struct fetch_pipeline_middle_end *fpme = (struct fetch_pipeline_middle_end *)middle;
327 struct draw_fetch_info fetch_info;
328 struct draw_prim_info prim_info;
329
330 fetch_info.linear = FALSE;
331 fetch_info.start = 0;
332 fetch_info.elts = fetch_elts;
333 fetch_info.count = fetch_count;
334
335 prim_info.linear = FALSE;
336 prim_info.start = 0;
337 prim_info.count = draw_count;
338 prim_info.elts = draw_elts;
339 prim_info.prim = fpme->input_prim;
340 prim_info.flags = prim_flags;
341 prim_info.primitive_count = 1;
342 prim_info.primitive_lengths = &draw_count;
343
344 fetch_pipeline_generic( middle, &fetch_info, &prim_info );
345 }
346
347
348 static void fetch_pipeline_linear_run( struct draw_pt_middle_end *middle,
349 unsigned start,
350 unsigned count,
351 unsigned prim_flags)
352 {
353 struct fetch_pipeline_middle_end *fpme = (struct fetch_pipeline_middle_end *)middle;
354 struct draw_fetch_info fetch_info;
355 struct draw_prim_info prim_info;
356
357 fetch_info.linear = TRUE;
358 fetch_info.start = start;
359 fetch_info.count = count;
360 fetch_info.elts = NULL;
361
362 prim_info.linear = TRUE;
363 prim_info.start = 0;
364 prim_info.count = count;
365 prim_info.elts = NULL;
366 prim_info.prim = fpme->input_prim;
367 prim_info.flags = prim_flags;
368 prim_info.primitive_count = 1;
369 prim_info.primitive_lengths = &count;
370
371 fetch_pipeline_generic( middle, &fetch_info, &prim_info );
372 }
373
374
375
376 static boolean fetch_pipeline_linear_run_elts( struct draw_pt_middle_end *middle,
377 unsigned start,
378 unsigned count,
379 const ushort *draw_elts,
380 unsigned draw_count,
381 unsigned prim_flags )
382 {
383 struct fetch_pipeline_middle_end *fpme = (struct fetch_pipeline_middle_end *)middle;
384 struct draw_fetch_info fetch_info;
385 struct draw_prim_info prim_info;
386
387 fetch_info.linear = TRUE;
388 fetch_info.start = start;
389 fetch_info.count = count;
390 fetch_info.elts = NULL;
391
392 prim_info.linear = FALSE;
393 prim_info.start = 0;
394 prim_info.count = draw_count;
395 prim_info.elts = draw_elts;
396 prim_info.prim = fpme->input_prim;
397 prim_info.flags = prim_flags;
398 prim_info.primitive_count = 1;
399 prim_info.primitive_lengths = &draw_count;
400
401 fetch_pipeline_generic( middle, &fetch_info, &prim_info );
402
403 return TRUE;
404 }
405
406
407
408 static void fetch_pipeline_finish( struct draw_pt_middle_end *middle )
409 {
410 /* nothing to do */
411 }
412
413 static void fetch_pipeline_destroy( struct draw_pt_middle_end *middle )
414 {
415 struct fetch_pipeline_middle_end *fpme = (struct fetch_pipeline_middle_end *)middle;
416
417 if (fpme->fetch)
418 draw_pt_fetch_destroy( fpme->fetch );
419
420 if (fpme->emit)
421 draw_pt_emit_destroy( fpme->emit );
422
423 if (fpme->so_emit)
424 draw_pt_so_emit_destroy( fpme->so_emit );
425
426 if (fpme->post_vs)
427 draw_pt_post_vs_destroy( fpme->post_vs );
428
429 FREE(middle);
430 }
431
432
433 struct draw_pt_middle_end *draw_pt_fetch_pipeline_or_emit( struct draw_context *draw )
434 {
435 struct fetch_pipeline_middle_end *fpme = CALLOC_STRUCT( fetch_pipeline_middle_end );
436 if (!fpme)
437 goto fail;
438
439 fpme->base.prepare = fetch_pipeline_prepare;
440 fpme->base.bind_parameters = fetch_pipeline_bind_parameters;
441 fpme->base.run = fetch_pipeline_run;
442 fpme->base.run_linear = fetch_pipeline_linear_run;
443 fpme->base.run_linear_elts = fetch_pipeline_linear_run_elts;
444 fpme->base.finish = fetch_pipeline_finish;
445 fpme->base.destroy = fetch_pipeline_destroy;
446
447 fpme->draw = draw;
448
449 fpme->fetch = draw_pt_fetch_create( draw );
450 if (!fpme->fetch)
451 goto fail;
452
453 fpme->post_vs = draw_pt_post_vs_create( draw );
454 if (!fpme->post_vs)
455 goto fail;
456
457 fpme->emit = draw_pt_emit_create( draw );
458 if (!fpme->emit)
459 goto fail;
460
461 fpme->so_emit = draw_pt_so_emit_create( draw );
462 if (!fpme->so_emit)
463 goto fail;
464
465 return &fpme->base;
466
467 fail:
468 if (fpme)
469 fetch_pipeline_destroy( &fpme->base );
470
471 return NULL;
472 }