xref: /aosp_15_r20/external/mesa3d/src/gallium/auxiliary/draw/draw_pt_fetch_shade_pipeline.c (revision 6104692788411f58d303aa86923a9ff6ecaded22)
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                                gs_vert_info,
293                                gs_prim_info);
294 
295       FREE(vert_info->verts);
296       vert_info = &gs_vert_info[0];
297       prim_info = &gs_prim_info[0];
298       num_vertex_streams = gshader->num_vertex_streams;
299 
300       /*
301        * pt emit can only handle ushort number of vertices (see
302        * render->allocate_vertices).
303        * vsplit guarantees there's never more than 4096, however GS can
304        * easily blow this up (by a factor of 256 (or even 1024) max).
305        */
306       if (vert_info->count > 65535) {
307          opt |= PT_PIPELINE;
308       }
309    } else {
310       if (draw_prim_assembler_is_required(draw, prim_info, vert_info)) {
311          draw_prim_assembler_run(draw, prim_info, vert_info,
312                                  &ia_prim_info, &ia_vert_info);
313 
314          if (ia_vert_info.count) {
315             FREE(vert_info->verts);
316             vert_info = &ia_vert_info;
317             prim_info = &ia_prim_info;
318             free_prim_info = true;
319          }
320       }
321    }
322    if (prim_info->count == 0) {
323       debug_printf("GS/IA didn't emit any vertices!\n");
324 
325       FREE(vert_info->verts);
326       if (free_prim_info) {
327          FREE(prim_info->primitive_lengths);
328       }
329       return;
330    }
331 
332 
333    /* Stream output needs to be done before clipping.
334     *
335     * XXX: Stream output surely needs to respect the prim_info->elt
336     *      lists.
337     */
338    draw_pt_so_emit(fpme->so_emit, num_vertex_streams, vert_info, prim_info);
339 
340    draw_stats_clipper_primitives(draw, prim_info);
341 
342    /*
343     * if there's no position, need to stop now, or the latter stages
344     * will try to access non-existent position output.
345     */
346    if (draw_current_shader_position_output(draw) != -1) {
347       if (draw_pt_post_vs_run(fpme->post_vs, vert_info, prim_info)) {
348          opt |= PT_PIPELINE;
349       }
350 
351       /* Do we need to run the pipeline?
352        */
353       if (opt & PT_PIPELINE) {
354          pipeline(fpme, vert_info, prim_info);
355       }
356       else {
357          emit(fpme->emit, vert_info, prim_info);
358       }
359    }
360    FREE(vert_info->verts);
361    if (free_prim_info) {
362       FREE(prim_info->primitive_lengths);
363    }
364 }
365 
366 
367 static inline unsigned
prim_type(unsigned prim,unsigned flags)368 prim_type(unsigned prim, unsigned flags)
369 {
370    if (flags & DRAW_LINE_LOOP_AS_STRIP)
371       return MESA_PRIM_LINE_STRIP;
372    else
373       return prim;
374 }
375 
376 
377 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)378 fetch_pipeline_run(struct draw_pt_middle_end *middle,
379                    const unsigned *fetch_elts,
380                    unsigned fetch_count,
381                    const uint16_t *draw_elts,
382                    unsigned draw_count,
383                    unsigned prim_flags)
384 {
385    struct fetch_pipeline_middle_end *fpme = fetch_pipeline_middle_end(middle);
386    struct draw_fetch_info fetch_info;
387    struct draw_prim_info prim_info;
388 
389    fetch_info.linear = false;
390    fetch_info.start = 0;
391    fetch_info.elts = fetch_elts;
392    fetch_info.count = fetch_count;
393 
394    prim_info.linear = false;
395    prim_info.start = 0;
396    prim_info.count = draw_count;
397    prim_info.elts = draw_elts;
398    prim_info.prim = prim_type(fpme->input_prim, prim_flags);
399    prim_info.flags = prim_flags;
400    prim_info.primitive_count = 1;
401    prim_info.primitive_lengths = &draw_count;
402 
403    fetch_pipeline_generic(middle, &fetch_info, &prim_info);
404 }
405 
406 
407 static void
fetch_pipeline_linear_run(struct draw_pt_middle_end * middle,unsigned start,unsigned count,unsigned prim_flags)408 fetch_pipeline_linear_run(struct draw_pt_middle_end *middle,
409                           unsigned start,
410                           unsigned count,
411                           unsigned prim_flags)
412 {
413    struct fetch_pipeline_middle_end *fpme = fetch_pipeline_middle_end(middle);
414    struct draw_fetch_info fetch_info;
415    struct draw_prim_info prim_info;
416 
417    fetch_info.linear = true;
418    fetch_info.start = start;
419    fetch_info.count = count;
420    fetch_info.elts = NULL;
421 
422    prim_info.linear = true;
423    prim_info.start = 0;
424    prim_info.count = count;
425    prim_info.elts = NULL;
426    prim_info.prim = prim_type(fpme->input_prim, prim_flags);
427    prim_info.flags = prim_flags;
428    prim_info.primitive_count = 1;
429    prim_info.primitive_lengths = &count;
430 
431    fetch_pipeline_generic(middle, &fetch_info, &prim_info);
432 }
433 
434 
435 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)436 fetch_pipeline_linear_run_elts(struct draw_pt_middle_end *middle,
437                                unsigned start,
438                                unsigned count,
439                                const uint16_t *draw_elts,
440                                unsigned draw_count,
441                                unsigned prim_flags)
442 {
443    struct fetch_pipeline_middle_end *fpme = fetch_pipeline_middle_end(middle);
444    struct draw_fetch_info fetch_info;
445    struct draw_prim_info prim_info;
446 
447    fetch_info.linear = true;
448    fetch_info.start = start;
449    fetch_info.count = count;
450    fetch_info.elts = NULL;
451 
452    prim_info.linear = false;
453    prim_info.start = 0;
454    prim_info.count = draw_count;
455    prim_info.elts = draw_elts;
456    prim_info.prim = prim_type(fpme->input_prim, prim_flags);
457    prim_info.flags = prim_flags;
458    prim_info.primitive_count = 1;
459    prim_info.primitive_lengths = &draw_count;
460 
461    fetch_pipeline_generic(middle, &fetch_info, &prim_info);
462 
463    return true;
464 }
465 
466 
467 static void
fetch_pipeline_finish(struct draw_pt_middle_end * middle)468 fetch_pipeline_finish(struct draw_pt_middle_end *middle)
469 {
470    /* nothing to do */
471 }
472 
473 
474 static void
fetch_pipeline_destroy(struct draw_pt_middle_end * middle)475 fetch_pipeline_destroy(struct draw_pt_middle_end *middle)
476 {
477    struct fetch_pipeline_middle_end *fpme = fetch_pipeline_middle_end(middle);
478 
479    if (fpme->fetch)
480       draw_pt_fetch_destroy(fpme->fetch);
481 
482    if (fpme->emit)
483       draw_pt_emit_destroy(fpme->emit);
484 
485    if (fpme->so_emit)
486       draw_pt_so_emit_destroy(fpme->so_emit);
487 
488    if (fpme->post_vs)
489       draw_pt_post_vs_destroy(fpme->post_vs);
490 
491    FREE(middle);
492 }
493 
494 
495 struct draw_pt_middle_end *
draw_pt_fetch_pipeline_or_emit(struct draw_context * draw)496 draw_pt_fetch_pipeline_or_emit(struct draw_context *draw)
497 {
498    struct fetch_pipeline_middle_end *fpme =
499       CALLOC_STRUCT(fetch_pipeline_middle_end);
500    if (!fpme)
501       goto fail;
502 
503    fpme->base.prepare        = fetch_pipeline_prepare;
504    fpme->base.bind_parameters  = fetch_pipeline_bind_parameters;
505    fpme->base.run            = fetch_pipeline_run;
506    fpme->base.run_linear     = fetch_pipeline_linear_run;
507    fpme->base.run_linear_elts = fetch_pipeline_linear_run_elts;
508    fpme->base.finish         = fetch_pipeline_finish;
509    fpme->base.destroy        = fetch_pipeline_destroy;
510 
511    fpme->draw = draw;
512 
513    fpme->fetch = draw_pt_fetch_create(draw);
514    if (!fpme->fetch)
515       goto fail;
516 
517    fpme->post_vs = draw_pt_post_vs_create(draw);
518    if (!fpme->post_vs)
519       goto fail;
520 
521    fpme->emit = draw_pt_emit_create(draw);
522    if (!fpme->emit)
523       goto fail;
524 
525    fpme->so_emit = draw_pt_so_emit_create(draw);
526    if (!fpme->so_emit)
527       goto fail;
528 
529    return &fpme->base;
530 
531  fail:
532    if (fpme)
533       fetch_pipeline_destroy(&fpme->base);
534 
535    return NULL;
536 }
537