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