1 /**************************************************************************
2 *
3 * Copyright 2007 VMware, Inc.
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 VMWARE 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_prim_assembler.h"
35 #include "draw/draw_pt.h"
36 #include "draw/draw_vs.h"
37 #include "draw/draw_gs.h"
38
39
40 struct fetch_pipeline_middle_end {
41 struct draw_pt_middle_end base;
42 struct draw_context *draw;
43
44 struct pt_emit *emit;
45 struct pt_so_emit *so_emit;
46 struct pt_fetch *fetch;
47 struct pt_post_vs *post_vs;
48
49 unsigned vertex_data_offset;
50 unsigned vertex_size;
51 unsigned input_prim;
52 unsigned opt;
53 };
54
55
56 /** cast wrapper */
57 static inline struct fetch_pipeline_middle_end *
fetch_pipeline_middle_end(struct draw_pt_middle_end * middle)58 fetch_pipeline_middle_end(struct draw_pt_middle_end *middle)
59 {
60 return (struct fetch_pipeline_middle_end *) middle;
61 }
62
63
64 /**
65 * Prepare/validate middle part of the vertex pipeline.
66 * NOTE: if you change this function, also look at the LLVM
67 * function llvm_middle_end_prepare() for similar changes.
68 */
69 static void
fetch_pipeline_prepare(struct draw_pt_middle_end * middle,unsigned prim,unsigned opt,unsigned * max_vertices)70 fetch_pipeline_prepare(struct draw_pt_middle_end *middle,
71 unsigned prim,
72 unsigned opt,
73 unsigned *max_vertices)
74 {
75 struct fetch_pipeline_middle_end *fpme = fetch_pipeline_middle_end(middle);
76 struct draw_context *draw = fpme->draw;
77 struct draw_vertex_shader *vs = draw->vs.vertex_shader;
78 struct draw_geometry_shader *gs = draw->gs.geometry_shader;
79 unsigned i;
80 unsigned instance_id_index = ~0;
81 const unsigned gs_out_prim = (gs ? gs->output_primitive :
82 u_assembled_prim(prim));
83 unsigned nr_vs_outputs = draw_total_vs_outputs(draw);
84 unsigned nr = MAX2(vs->info.num_inputs, nr_vs_outputs);
85 unsigned point_clip = draw->rasterizer->fill_front == PIPE_POLYGON_MODE_POINT ||
86 gs_out_prim == PIPE_PRIM_POINTS;
87
88 if (gs) {
89 nr = MAX2(nr, gs->info.num_outputs + 1);
90 }
91
92 /* Scan for instanceID system value.
93 */
94 for (i = 0; i < vs->info.num_inputs; i++) {
95 if (vs->info.input_semantic_name[i] == TGSI_SEMANTIC_INSTANCEID) {
96 instance_id_index = i;
97 break;
98 }
99 }
100
101 fpme->input_prim = prim;
102 fpme->opt = opt;
103
104 /* Always leave room for the vertex header whether we need it or
105 * not. It's hard to get rid of it in particular because of the
106 * viewport code in draw_pt_post_vs.c.
107 */
108 fpme->vertex_size = sizeof(struct vertex_header) + nr * 4 * sizeof(float);
109
110 draw_pt_fetch_prepare( fpme->fetch,
111 vs->info.num_inputs,
112 fpme->vertex_size,
113 instance_id_index );
114 draw_pt_post_vs_prepare( fpme->post_vs,
115 draw->clip_xy,
116 draw->clip_z,
117 draw->clip_user,
118 point_clip ? draw->guard_band_points_xy :
119 draw->guard_band_xy,
120 draw->bypass_viewport,
121 draw->rasterizer->clip_halfz,
122 (draw->vs.edgeflag_output ? TRUE : FALSE) );
123
124 draw_pt_so_emit_prepare( fpme->so_emit, FALSE );
125
126 if (!(opt & PT_PIPELINE)) {
127 draw_pt_emit_prepare( fpme->emit,
128 gs_out_prim,
129 max_vertices );
130
131 *max_vertices = MAX2( *max_vertices, 4096 );
132 }
133 else {
134 /* limit max fetches by limiting max_vertices */
135 *max_vertices = 4096;
136 }
137
138 /* No need to prepare the shader.
139 */
140 vs->prepare(vs, draw);
141
142 /* Make sure that the vertex size didn't change at any point above */
143 assert(nr_vs_outputs == draw_total_vs_outputs(draw));
144 }
145
146
147 static void
fetch_pipeline_bind_parameters(struct draw_pt_middle_end * middle)148 fetch_pipeline_bind_parameters(struct draw_pt_middle_end *middle)
149 {
150 /* No-op since the vertex shader executor and drawing pipeline
151 * just grab the constants, viewport, etc. from the draw context state.
152 */
153 }
154
155
fetch(struct pt_fetch * fetch,const struct draw_fetch_info * fetch_info,char * output)156 static void fetch( struct pt_fetch *fetch,
157 const struct draw_fetch_info *fetch_info,
158 char *output)
159 {
160 if (fetch_info->linear) {
161 draw_pt_fetch_run_linear( fetch,
162 fetch_info->start,
163 fetch_info->count,
164 output );
165 }
166 else {
167 draw_pt_fetch_run( fetch,
168 fetch_info->elts,
169 fetch_info->count,
170 output );
171 }
172 }
173
174
pipeline(struct fetch_pipeline_middle_end * fpme,const struct draw_vertex_info * vert_info,const struct draw_prim_info * prim_info)175 static void pipeline(struct fetch_pipeline_middle_end *fpme,
176 const struct draw_vertex_info *vert_info,
177 const struct draw_prim_info *prim_info)
178 {
179 if (prim_info->linear)
180 draw_pipeline_run_linear( fpme->draw,
181 vert_info,
182 prim_info);
183 else
184 draw_pipeline_run( fpme->draw,
185 vert_info,
186 prim_info );
187 }
188
189
190 static void
emit(struct pt_emit * emit,const struct draw_vertex_info * vert_info,const struct draw_prim_info * prim_info)191 emit(struct pt_emit *emit,
192 const struct draw_vertex_info *vert_info,
193 const struct draw_prim_info *prim_info)
194 {
195 if (prim_info->linear) {
196 draw_pt_emit_linear(emit, vert_info, prim_info);
197 }
198 else {
199 draw_pt_emit(emit, vert_info, prim_info);
200 }
201 }
202
203
204 static void
draw_vertex_shader_run(struct draw_vertex_shader * vshader,const void * constants[PIPE_MAX_CONSTANT_BUFFERS],unsigned const_size[PIPE_MAX_CONSTANT_BUFFERS],const struct draw_fetch_info * fetch_info,const struct draw_vertex_info * input_verts,struct draw_vertex_info * output_verts)205 draw_vertex_shader_run(struct draw_vertex_shader *vshader,
206 const void *constants[PIPE_MAX_CONSTANT_BUFFERS],
207 unsigned const_size[PIPE_MAX_CONSTANT_BUFFERS],
208 const struct draw_fetch_info *fetch_info,
209 const struct draw_vertex_info *input_verts,
210 struct draw_vertex_info *output_verts)
211 {
212 output_verts->vertex_size = input_verts->vertex_size;
213 output_verts->stride = input_verts->vertex_size;
214 output_verts->count = input_verts->count;
215 output_verts->verts =
216 (struct vertex_header *)MALLOC(output_verts->vertex_size *
217 align(output_verts->count, 4));
218
219 vshader->run_linear(vshader,
220 (const float (*)[4])input_verts->verts->data,
221 ( float (*)[4])output_verts->verts->data,
222 constants,
223 const_size,
224 input_verts->count,
225 input_verts->vertex_size,
226 input_verts->vertex_size,
227 fetch_info->elts);
228 }
229
230
231 static void
fetch_pipeline_generic(struct draw_pt_middle_end * middle,const struct draw_fetch_info * fetch_info,const struct draw_prim_info * in_prim_info)232 fetch_pipeline_generic(struct draw_pt_middle_end *middle,
233 const struct draw_fetch_info *fetch_info,
234 const struct draw_prim_info *in_prim_info)
235 {
236 struct fetch_pipeline_middle_end *fpme = fetch_pipeline_middle_end(middle);
237 struct draw_context *draw = fpme->draw;
238 struct draw_vertex_shader *vshader = draw->vs.vertex_shader;
239 struct draw_geometry_shader *gshader = draw->gs.geometry_shader;
240 struct draw_prim_info gs_prim_info[TGSI_MAX_VERTEX_STREAMS];
241 struct draw_vertex_info fetched_vert_info;
242 struct draw_vertex_info vs_vert_info;
243 struct draw_vertex_info gs_vert_info[TGSI_MAX_VERTEX_STREAMS];
244 struct draw_vertex_info *vert_info;
245 struct draw_prim_info ia_prim_info;
246 struct draw_vertex_info ia_vert_info;
247 const struct draw_prim_info *prim_info = in_prim_info;
248 boolean free_prim_info = FALSE;
249 unsigned opt = fpme->opt;
250 int num_vertex_streams = 1;
251
252 fetched_vert_info.count = fetch_info->count;
253 fetched_vert_info.vertex_size = fpme->vertex_size;
254 fetched_vert_info.stride = fpme->vertex_size;
255 fetched_vert_info.verts =
256 (struct vertex_header *)MALLOC(fpme->vertex_size *
257 align(fetch_info->count, 4));
258 if (!fetched_vert_info.verts) {
259 assert(0);
260 return;
261 }
262 if (draw->collect_statistics) {
263 draw->statistics.ia_vertices += prim_info->count;
264 draw->statistics.ia_primitives +=
265 u_decomposed_prims_for_vertices(prim_info->prim, fetch_info->count);
266 draw->statistics.vs_invocations += fetch_info->count;
267 }
268
269 /* Fetch into our vertex buffer.
270 */
271 fetch( fpme->fetch, fetch_info, (char *)fetched_vert_info.verts );
272
273 vert_info = &fetched_vert_info;
274
275 /* Run the shader, note that this overwrites the data[] parts of
276 * the pipeline verts.
277 * Need fetch info to get vertex id correct.
278 */
279 if (fpme->opt & PT_SHADE) {
280 draw_vertex_shader_run(vshader,
281 draw->pt.user.vs_constants,
282 draw->pt.user.vs_constants_size,
283 fetch_info,
284 vert_info,
285 &vs_vert_info);
286
287 FREE(vert_info->verts);
288 vert_info = &vs_vert_info;
289 }
290
291 /* Finished with fetch:
292 */
293 fetch_info = NULL;
294
295 if ((fpme->opt & PT_SHADE) && gshader) {
296 draw_geometry_shader_run(gshader,
297 draw->pt.user.gs_constants,
298 draw->pt.user.gs_constants_size,
299 vert_info,
300 prim_info,
301 &vshader->info,
302 gs_vert_info,
303 gs_prim_info);
304
305 FREE(vert_info->verts);
306 vert_info = &gs_vert_info[0];
307 prim_info = &gs_prim_info[0];
308 num_vertex_streams = gshader->num_vertex_streams;
309
310 /*
311 * pt emit can only handle ushort number of vertices (see
312 * render->allocate_vertices).
313 * vsplit guarantees there's never more than 4096, however GS can
314 * easily blow this up (by a factor of 256 (or even 1024) max).
315 */
316 if (vert_info->count > 65535) {
317 opt |= PT_PIPELINE;
318 }
319 } else {
320 if (draw_prim_assembler_is_required(draw, prim_info, vert_info)) {
321 draw_prim_assembler_run(draw, prim_info, vert_info,
322 &ia_prim_info, &ia_vert_info);
323
324 if (ia_vert_info.count) {
325 FREE(vert_info->verts);
326 vert_info = &ia_vert_info;
327 prim_info = &ia_prim_info;
328 free_prim_info = TRUE;
329 }
330 }
331 }
332 if (prim_info->count == 0) {
333 debug_printf("GS/IA didn't emit any vertices!\n");
334
335 FREE(vert_info->verts);
336 if (free_prim_info) {
337 FREE(prim_info->primitive_lengths);
338 }
339 return;
340 }
341
342
343 /* Stream output needs to be done before clipping.
344 *
345 * XXX: Stream output surely needs to respect the prim_info->elt
346 * lists.
347 */
348 draw_pt_so_emit( fpme->so_emit, num_vertex_streams, vert_info, prim_info );
349
350 draw_stats_clipper_primitives(draw, prim_info);
351
352 /*
353 * if there's no position, need to stop now, or the latter stages
354 * will try to access non-existent position output.
355 */
356 if (draw_current_shader_position_output(draw) != -1) {
357
358 if (draw_pt_post_vs_run( fpme->post_vs, vert_info, prim_info ))
359 {
360 opt |= PT_PIPELINE;
361 }
362
363 /* Do we need to run the pipeline?
364 */
365 if (opt & PT_PIPELINE) {
366 pipeline( fpme, vert_info, prim_info );
367 }
368 else {
369 emit( fpme->emit, vert_info, prim_info );
370 }
371 }
372 FREE(vert_info->verts);
373 if (free_prim_info) {
374 FREE(prim_info->primitive_lengths);
375 }
376 }
377
378
379 static inline unsigned
prim_type(unsigned prim,unsigned flags)380 prim_type(unsigned prim, unsigned flags)
381 {
382 if (flags & DRAW_LINE_LOOP_AS_STRIP)
383 return PIPE_PRIM_LINE_STRIP;
384 else
385 return prim;
386 }
387
388
389 static void
fetch_pipeline_run(struct draw_pt_middle_end * middle,const unsigned * fetch_elts,unsigned fetch_count,const ushort * draw_elts,unsigned draw_count,unsigned prim_flags)390 fetch_pipeline_run(struct draw_pt_middle_end *middle,
391 const unsigned *fetch_elts,
392 unsigned fetch_count,
393 const ushort *draw_elts,
394 unsigned draw_count,
395 unsigned prim_flags)
396 {
397 struct fetch_pipeline_middle_end *fpme = fetch_pipeline_middle_end(middle);
398 struct draw_fetch_info fetch_info;
399 struct draw_prim_info prim_info;
400
401 fetch_info.linear = FALSE;
402 fetch_info.start = 0;
403 fetch_info.elts = fetch_elts;
404 fetch_info.count = fetch_count;
405
406 prim_info.linear = FALSE;
407 prim_info.start = 0;
408 prim_info.count = draw_count;
409 prim_info.elts = draw_elts;
410 prim_info.prim = prim_type(fpme->input_prim, prim_flags);
411 prim_info.flags = prim_flags;
412 prim_info.primitive_count = 1;
413 prim_info.primitive_lengths = &draw_count;
414
415 fetch_pipeline_generic( middle, &fetch_info, &prim_info );
416 }
417
418
419 static void
fetch_pipeline_linear_run(struct draw_pt_middle_end * middle,unsigned start,unsigned count,unsigned prim_flags)420 fetch_pipeline_linear_run(struct draw_pt_middle_end *middle,
421 unsigned start,
422 unsigned count,
423 unsigned prim_flags)
424 {
425 struct fetch_pipeline_middle_end *fpme = fetch_pipeline_middle_end(middle);
426 struct draw_fetch_info fetch_info;
427 struct draw_prim_info prim_info;
428
429 fetch_info.linear = TRUE;
430 fetch_info.start = start;
431 fetch_info.count = count;
432 fetch_info.elts = NULL;
433
434 prim_info.linear = TRUE;
435 prim_info.start = 0;
436 prim_info.count = count;
437 prim_info.elts = NULL;
438 prim_info.prim = prim_type(fpme->input_prim, prim_flags);
439 prim_info.flags = prim_flags;
440 prim_info.primitive_count = 1;
441 prim_info.primitive_lengths = &count;
442
443 fetch_pipeline_generic( middle, &fetch_info, &prim_info );
444 }
445
446
447
448 static boolean
fetch_pipeline_linear_run_elts(struct draw_pt_middle_end * middle,unsigned start,unsigned count,const ushort * draw_elts,unsigned draw_count,unsigned prim_flags)449 fetch_pipeline_linear_run_elts(struct draw_pt_middle_end *middle,
450 unsigned start,
451 unsigned count,
452 const ushort *draw_elts,
453 unsigned draw_count,
454 unsigned prim_flags )
455 {
456 struct fetch_pipeline_middle_end *fpme = fetch_pipeline_middle_end(middle);
457 struct draw_fetch_info fetch_info;
458 struct draw_prim_info prim_info;
459
460 fetch_info.linear = TRUE;
461 fetch_info.start = start;
462 fetch_info.count = count;
463 fetch_info.elts = NULL;
464
465 prim_info.linear = FALSE;
466 prim_info.start = 0;
467 prim_info.count = draw_count;
468 prim_info.elts = draw_elts;
469 prim_info.prim = prim_type(fpme->input_prim, prim_flags);
470 prim_info.flags = prim_flags;
471 prim_info.primitive_count = 1;
472 prim_info.primitive_lengths = &draw_count;
473
474 fetch_pipeline_generic( middle, &fetch_info, &prim_info );
475
476 return TRUE;
477 }
478
479
480 static void
fetch_pipeline_finish(struct draw_pt_middle_end * middle)481 fetch_pipeline_finish( struct draw_pt_middle_end *middle )
482 {
483 /* nothing to do */
484 }
485
486
487 static void
fetch_pipeline_destroy(struct draw_pt_middle_end * middle)488 fetch_pipeline_destroy( struct draw_pt_middle_end *middle )
489 {
490 struct fetch_pipeline_middle_end *fpme = fetch_pipeline_middle_end(middle);
491
492 if (fpme->fetch)
493 draw_pt_fetch_destroy( fpme->fetch );
494
495 if (fpme->emit)
496 draw_pt_emit_destroy( fpme->emit );
497
498 if (fpme->so_emit)
499 draw_pt_so_emit_destroy( fpme->so_emit );
500
501 if (fpme->post_vs)
502 draw_pt_post_vs_destroy( fpme->post_vs );
503
504 FREE(middle);
505 }
506
507
508 struct draw_pt_middle_end *
draw_pt_fetch_pipeline_or_emit(struct draw_context * draw)509 draw_pt_fetch_pipeline_or_emit(struct draw_context *draw)
510 {
511 struct fetch_pipeline_middle_end *fpme =
512 CALLOC_STRUCT( fetch_pipeline_middle_end );
513 if (!fpme)
514 goto fail;
515
516 fpme->base.prepare = fetch_pipeline_prepare;
517 fpme->base.bind_parameters = fetch_pipeline_bind_parameters;
518 fpme->base.run = fetch_pipeline_run;
519 fpme->base.run_linear = fetch_pipeline_linear_run;
520 fpme->base.run_linear_elts = fetch_pipeline_linear_run_elts;
521 fpme->base.finish = fetch_pipeline_finish;
522 fpme->base.destroy = fetch_pipeline_destroy;
523
524 fpme->draw = draw;
525
526 fpme->fetch = draw_pt_fetch_create( draw );
527 if (!fpme->fetch)
528 goto fail;
529
530 fpme->post_vs = draw_pt_post_vs_create( draw );
531 if (!fpme->post_vs)
532 goto fail;
533
534 fpme->emit = draw_pt_emit_create( draw );
535 if (!fpme->emit)
536 goto fail;
537
538 fpme->so_emit = draw_pt_so_emit_create( draw );
539 if (!fpme->so_emit)
540 goto fail;
541
542 return &fpme->base;
543
544 fail:
545 if (fpme)
546 fetch_pipeline_destroy( &fpme->base );
547
548 return NULL;
549 }
550