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