xref: /aosp_15_r20/external/mesa3d/src/gallium/auxiliary/draw/draw_prim_assembler.c (revision 6104692788411f58d303aa86923a9ff6ecaded22)
1 /**************************************************************************
2  *
3  * Copyright 2013 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 "draw_prim_assembler.h"
29 
30 #include "draw_fs.h"
31 #include "draw_gs.h"
32 #include "draw_tess.h"
33 #include "util/u_debug.h"
34 #include "util/u_memory.h"
35 #include "util/u_prim.h"
36 
37 #include "pipe/p_defines.h"
38 
39 
40 struct draw_assembler
41 {
42    struct draw_context *draw;
43 
44    struct draw_prim_info *output_prims;
45    struct draw_vertex_info *output_verts;
46 
47    const struct draw_prim_info *input_prims;
48    const struct draw_vertex_info *input_verts;
49 
50    bool needs_primid;
51    int primid_slot;
52    unsigned primid;
53 
54    unsigned num_prims;
55 };
56 
57 
58 static bool
needs_primid(const struct draw_context * draw)59 needs_primid(const struct draw_context *draw)
60 {
61    const struct draw_fragment_shader *fs = draw->fs.fragment_shader;
62    const struct draw_geometry_shader *gs = draw->gs.geometry_shader;
63    const struct draw_tess_eval_shader *tes = draw->tes.tess_eval_shader;
64    if (fs && fs->info.uses_primid) {
65       if (gs)
66          return !gs->info.uses_primid;
67       else if (tes)
68          return !tes->info.uses_primid;
69       else
70          return true;
71    }
72    return false;
73 }
74 
75 
76 bool
draw_prim_assembler_is_required(const struct draw_context * draw,const struct draw_prim_info * prim_info,const struct draw_vertex_info * vert_info)77 draw_prim_assembler_is_required(const struct draw_context *draw,
78                                 const struct draw_prim_info *prim_info,
79                                 const struct draw_vertex_info *vert_info)
80 {
81    /* viewport index requires primitive boundaries to get correct vertex */
82    if (draw_current_shader_uses_viewport_index(draw))
83       return true;
84    switch (prim_info->prim) {
85    case MESA_PRIM_LINES_ADJACENCY:
86    case MESA_PRIM_LINE_STRIP_ADJACENCY:
87    case MESA_PRIM_TRIANGLES_ADJACENCY:
88    case MESA_PRIM_TRIANGLE_STRIP_ADJACENCY:
89       return true;
90    default:
91       return needs_primid(draw);
92    }
93 }
94 
95 
96 static void
add_prim(struct draw_assembler * asmblr,unsigned length)97 add_prim(struct draw_assembler *asmblr, unsigned length)
98 {
99    struct draw_prim_info *output_prims = asmblr->output_prims;
100 
101    output_prims->primitive_lengths = realloc(output_prims->primitive_lengths, sizeof(unsigned) * (output_prims->primitive_count + 1));
102    output_prims->primitive_lengths[output_prims->primitive_count] = length;
103    output_prims->primitive_count++;
104 }
105 
106 
107 /*
108  * Copy the vertex header along with its data from the current
109  * vertex buffer into a buffer holding vertices arranged
110  * into decomposed primitives (i.e. buffer without the
111  * adjacency vertices)
112  */
113 static void
copy_verts(struct draw_assembler * asmblr,unsigned * indices,unsigned num_indices)114 copy_verts(struct draw_assembler *asmblr,
115            unsigned *indices, unsigned num_indices)
116 {
117    char *output = (char*)asmblr->output_verts->verts;
118    const char *input = (const char*)asmblr->input_verts->verts;
119 
120    for (unsigned i = 0; i < num_indices; ++i) {
121       unsigned idx = indices[i];
122       unsigned output_offset =
123          asmblr->output_verts->count * asmblr->output_verts->stride;
124       unsigned input_offset = asmblr->input_verts->stride * idx;
125       memcpy(output + output_offset, input + input_offset,
126              asmblr->input_verts->vertex_size);
127       asmblr->output_verts->count += 1;
128    }
129    ++asmblr->num_prims;
130 }
131 
132 
133 static void
inject_primid(struct draw_assembler * asmblr,unsigned idx,unsigned primid)134 inject_primid(struct draw_assembler *asmblr,
135               unsigned idx,
136               unsigned primid)
137 {
138    int slot = asmblr->primid_slot;
139    char *input = (char*)asmblr->input_verts->verts;
140    unsigned input_offset = asmblr->input_verts->stride * idx;
141    struct vertex_header *v = (struct vertex_header*)(input + input_offset);
142 
143    /* In case the backend doesn't care about it */
144    if (slot < 0) {
145       return;
146    }
147 
148    memcpy(&v->data[slot][0], &primid, sizeof(primid));
149    memcpy(&v->data[slot][1], &primid, sizeof(primid));
150    memcpy(&v->data[slot][2], &primid, sizeof(primid));
151    memcpy(&v->data[slot][3], &primid, sizeof(primid));
152 }
153 
154 
155 static void
prim_point(struct draw_assembler * asmblr,unsigned idx)156 prim_point(struct draw_assembler *asmblr,
157            unsigned idx)
158 {
159    unsigned indices[1];
160 
161    if (asmblr->needs_primid) {
162       inject_primid(asmblr, idx, asmblr->primid++);
163    }
164    indices[0] = idx;
165 
166    add_prim(asmblr, 1);
167    copy_verts(asmblr, indices, 1);
168 }
169 
170 
171 static void
prim_line(struct draw_assembler * asmblr,unsigned i0,unsigned i1)172 prim_line(struct draw_assembler *asmblr,
173           unsigned i0, unsigned i1)
174 {
175    unsigned indices[2];
176 
177    if (asmblr->needs_primid) {
178       inject_primid(asmblr, i0, asmblr->primid);
179       inject_primid(asmblr, i1, asmblr->primid++);
180    }
181    indices[0] = i0;
182    indices[1] = i1;
183 
184    add_prim(asmblr, 2);
185    copy_verts(asmblr, indices, 2);
186 }
187 
188 
189 static void
prim_tri(struct draw_assembler * asmblr,unsigned i0,unsigned i1,unsigned i2)190 prim_tri(struct draw_assembler *asmblr,
191          unsigned i0, unsigned i1, unsigned i2)
192 {
193    unsigned indices[3];
194 
195    if (asmblr->needs_primid) {
196       inject_primid(asmblr, i0, asmblr->primid);
197       inject_primid(asmblr, i1, asmblr->primid);
198       inject_primid(asmblr, i2, asmblr->primid++);
199    }
200    indices[0] = i0;
201    indices[1] = i1;
202    indices[2] = i2;
203 
204    add_prim(asmblr, 3);
205    copy_verts(asmblr, indices, 3);
206 }
207 
208 
209 static void
prim_quad(struct draw_assembler * asmblr,unsigned i0,unsigned i1,unsigned i2,unsigned i3)210 prim_quad(struct draw_assembler *asmblr,
211           unsigned i0, unsigned i1,
212           unsigned i2, unsigned i3)
213 {
214    unsigned indices[4];
215 
216    if (asmblr->needs_primid) {
217       inject_primid(asmblr, i0, asmblr->primid);
218       inject_primid(asmblr, i1, asmblr->primid);
219       inject_primid(asmblr, i2, asmblr->primid);
220       inject_primid(asmblr, i3, asmblr->primid++);
221    }
222    indices[0] = i0;
223    indices[1] = i1;
224    indices[2] = i2;
225    indices[3] = i3;
226 
227    add_prim(asmblr, 4);
228    copy_verts(asmblr, indices, 4);
229 }
230 
231 
232 void
draw_prim_assembler_prepare_outputs(struct draw_assembler * ia)233 draw_prim_assembler_prepare_outputs(struct draw_assembler *ia)
234 {
235    struct draw_context *draw = ia->draw;
236    if (needs_primid(draw)) {
237       ia->primid_slot = draw_alloc_extra_vertex_attrib(
238          ia->draw, TGSI_SEMANTIC_PRIMID, 0);
239    } else {
240       ia->primid_slot = -1;
241    }
242 }
243 
244 
245 #define FUNC assembler_run_linear
246 #define GET_ELT(idx) (start + (idx))
247 #include "draw_prim_assembler_tmp.h"
248 
249 #define FUNC assembler_run_elts
250 #define LOCAL_VARS   const uint16_t *elts = input_prims->elts;
251 #define GET_ELT(idx) (elts[start + (idx)])
252 #include "draw_prim_assembler_tmp.h"
253 
254 
255 /*
256  * Primitive assembler breaks up adjacency primitives and assembles
257  * the base primitives they represent, e.g. vertices forming
258  * MESA_PRIM_TRIANGLE_STRIP_ADJACENCY
259  * become vertices forming MESA_PRIM_TRIANGLES
260  * This is needed because specification says that the adjacency
261  * primitives are only visible in the geometry shader so we need
262  * to get rid of them so that the rest of the pipeline can
263  * process the inputs.
264  */
265 void
draw_prim_assembler_run(struct draw_context * draw,const struct draw_prim_info * input_prims,const struct draw_vertex_info * input_verts,struct draw_prim_info * output_prims,struct draw_vertex_info * output_verts)266 draw_prim_assembler_run(struct draw_context *draw,
267                         const struct draw_prim_info *input_prims,
268                         const struct draw_vertex_info *input_verts,
269                         struct draw_prim_info *output_prims,
270                         struct draw_vertex_info *output_verts)
271 {
272    struct draw_assembler *asmblr = draw->ia;
273    unsigned start, i;
274    unsigned assembled_prim = (input_prims->prim == MESA_PRIM_QUADS ||
275                               input_prims->prim == MESA_PRIM_QUAD_STRIP) ?
276       MESA_PRIM_QUADS : u_reduced_prim(input_prims->prim);
277    unsigned max_primitives = u_decomposed_prims_for_vertices(
278       input_prims->prim, input_prims->count);
279    unsigned max_verts = mesa_vertices_per_prim(assembled_prim) * max_primitives;
280 
281    asmblr->output_prims = output_prims;
282    asmblr->output_verts = output_verts;
283    asmblr->input_prims = input_prims;
284    asmblr->input_verts = input_verts;
285    asmblr->needs_primid = needs_primid(asmblr->draw);
286    asmblr->num_prims = 0;
287 
288    output_prims->linear = true;
289    output_prims->elts = NULL;
290    output_prims->start = 0;
291    output_prims->prim = assembled_prim;
292    output_prims->flags = 0x0;
293    output_prims->primitive_lengths = MALLOC(sizeof(unsigned));
294    output_prims->primitive_lengths[0] = 0;
295    output_prims->primitive_count = 1;
296 
297    output_verts->vertex_size = input_verts->vertex_size;
298    output_verts->stride = input_verts->stride;
299    output_verts->verts = (struct vertex_header*)MALLOC(
300       input_verts->vertex_size * max_verts + DRAW_EXTRA_VERTICES_PADDING);
301    output_verts->count = 0;
302 
303 
304    for (start = i = 0; i < input_prims->primitive_count;
305         start += input_prims->primitive_lengths[i], i++) {
306       unsigned count = input_prims->primitive_lengths[i];
307       if (input_prims->linear) {
308          assembler_run_linear(asmblr, input_prims, input_verts,
309                               start, count);
310       } else {
311          assembler_run_elts(asmblr, input_prims, input_verts,
312                             start, count);
313       }
314    }
315 
316    output_prims->count = output_verts->count;
317 }
318 
319 
320 struct draw_assembler *
draw_prim_assembler_create(struct draw_context * draw)321 draw_prim_assembler_create(struct draw_context *draw)
322 {
323    struct draw_assembler *ia = CALLOC_STRUCT(draw_assembler);
324 
325    ia->draw = draw;
326 
327    return ia;
328 }
329 
330 
331 void
draw_prim_assembler_destroy(struct draw_assembler * ia)332 draw_prim_assembler_destroy(struct draw_assembler *ia)
333 {
334    FREE(ia);
335 }
336 
337 
338 /*
339  * Called at the very begin of the draw call with a new instance
340  * Used to reset state that should persist between primitive restart.
341  */
342 void
draw_prim_assembler_new_instance(struct draw_assembler * asmblr)343 draw_prim_assembler_new_instance(struct draw_assembler *asmblr)
344 {
345    asmblr->primid = 0;
346 }
347