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