• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 /*
94  * Copy the vertex header along with its data from the current
95  * vertex buffer into a buffer holding vertices arranged
96  * into decomposed primitives (i.e. buffer without the
97  * adjacency vertices)
98  */
99 static void
copy_verts(struct draw_assembler * asmblr,unsigned * indices,unsigned num_indices)100 copy_verts(struct draw_assembler *asmblr,
101            unsigned *indices, unsigned num_indices)
102 {
103    unsigned i;
104 
105    char *output = (char*)asmblr->output_verts->verts;
106    const char *input = (const char*)asmblr->input_verts->verts;
107 
108    for (i = 0; i < num_indices; ++i) {
109       unsigned idx = indices[i];
110       unsigned output_offset =
111          asmblr->output_verts->count * asmblr->output_verts->stride;
112       unsigned input_offset = asmblr->input_verts->stride * idx;
113       memcpy(output + output_offset, input + input_offset,
114              asmblr->input_verts->vertex_size);
115       asmblr->output_verts->count += 1;
116    }
117    ++asmblr->num_prims;
118 }
119 
120 
121 static void
inject_primid(struct draw_assembler * asmblr,unsigned idx,unsigned primid)122 inject_primid(struct draw_assembler *asmblr,
123               unsigned idx,
124               unsigned primid)
125 {
126    int slot = asmblr->primid_slot;
127    char *input = (char*)asmblr->input_verts->verts;
128    unsigned input_offset = asmblr->input_verts->stride * idx;
129    struct vertex_header *v = (struct vertex_header*)(input + input_offset);
130 
131    /* In case the backend doesn't care about it */
132    if (slot < 0) {
133       return;
134    }
135 
136    memcpy(&v->data[slot][0], &primid, sizeof(primid));
137    memcpy(&v->data[slot][1], &primid, sizeof(primid));
138    memcpy(&v->data[slot][2], &primid, sizeof(primid));
139    memcpy(&v->data[slot][3], &primid, sizeof(primid));
140 }
141 
142 
143 static void
prim_point(struct draw_assembler * asmblr,unsigned idx)144 prim_point(struct draw_assembler *asmblr,
145            unsigned idx)
146 {
147    unsigned indices[1];
148 
149    if (asmblr->needs_primid) {
150       inject_primid(asmblr, idx, asmblr->primid++);
151    }
152    indices[0] = idx;
153 
154    copy_verts(asmblr, indices, 1);
155 }
156 
157 static void
prim_line(struct draw_assembler * asmblr,unsigned i0,unsigned i1)158 prim_line(struct draw_assembler *asmblr,
159           unsigned i0, unsigned i1)
160 {
161    unsigned indices[2];
162 
163    if (asmblr->needs_primid) {
164       inject_primid(asmblr, i0, asmblr->primid);
165       inject_primid(asmblr, i1, asmblr->primid++);
166    }
167    indices[0] = i0;
168    indices[1] = i1;
169 
170    copy_verts(asmblr, indices, 2);
171 }
172 
173 static void
prim_tri(struct draw_assembler * asmblr,unsigned i0,unsigned i1,unsigned i2)174 prim_tri(struct draw_assembler *asmblr,
175          unsigned i0, unsigned i1, unsigned i2)
176 {
177    unsigned indices[3];
178 
179    if (asmblr->needs_primid) {
180       inject_primid(asmblr, i0, asmblr->primid);
181       inject_primid(asmblr, i1, asmblr->primid);
182       inject_primid(asmblr, i2, asmblr->primid++);
183    }
184    indices[0] = i0;
185    indices[1] = i1;
186    indices[2] = i2;
187 
188    copy_verts(asmblr, indices, 3);
189 }
190 
191 void
draw_prim_assembler_prepare_outputs(struct draw_assembler * ia)192 draw_prim_assembler_prepare_outputs(struct draw_assembler *ia)
193 {
194    struct draw_context *draw = ia->draw;
195    if (needs_primid(draw)) {
196       ia->primid_slot = draw_alloc_extra_vertex_attrib(
197          ia->draw, TGSI_SEMANTIC_PRIMID, 0);
198    } else {
199       ia->primid_slot = -1;
200    }
201 }
202 
203 
204 #define FUNC assembler_run_linear
205 #define GET_ELT(idx) (start + (idx))
206 #include "draw_prim_assembler_tmp.h"
207 
208 #define FUNC assembler_run_elts
209 #define LOCAL_VARS   const ushort *elts = input_prims->elts;
210 #define GET_ELT(idx) (elts[start + (idx)])
211 #include "draw_prim_assembler_tmp.h"
212 
213 
214 
215 /*
216  * Primitive assembler breaks up adjacency primitives and assembles
217  * the base primitives they represent, e.g. vertices forming
218  * PIPE_PRIM_TRIANGLE_STRIP_ADJACENCY
219  * become vertices forming PIPE_PRIM_TRIANGLES
220  * This is needed because specification says that the adjacency
221  * primitives are only visible in the geometry shader so we need
222  * to get rid of them so that the rest of the pipeline can
223  * process the inputs.
224  */
225 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)226 draw_prim_assembler_run(struct draw_context *draw,
227                         const struct draw_prim_info *input_prims,
228                         const struct draw_vertex_info *input_verts,
229                         struct draw_prim_info *output_prims,
230                         struct draw_vertex_info *output_verts)
231 {
232    struct draw_assembler *asmblr = draw->ia;
233    unsigned start, i;
234    unsigned assembled_prim = u_reduced_prim(input_prims->prim);
235    unsigned max_primitives = u_decomposed_prims_for_vertices(
236       input_prims->prim, input_prims->count);
237    unsigned max_verts = u_vertices_per_prim(assembled_prim) * max_primitives;
238 
239    asmblr->output_prims = output_prims;
240    asmblr->output_verts = output_verts;
241    asmblr->input_prims = input_prims;
242    asmblr->input_verts = input_verts;
243    asmblr->needs_primid = needs_primid(asmblr->draw);
244    asmblr->num_prims = 0;
245 
246    output_prims->linear = TRUE;
247    output_prims->elts = NULL;
248    output_prims->start = 0;
249    output_prims->prim = assembled_prim;
250    output_prims->flags = 0x0;
251    output_prims->primitive_lengths = MALLOC(sizeof(unsigned));
252    output_prims->primitive_lengths[0] = 0;
253    output_prims->primitive_count = 1;
254 
255    output_verts->vertex_size = input_verts->vertex_size;
256    output_verts->stride = input_verts->stride;
257    output_verts->verts = (struct vertex_header*)MALLOC(
258       input_verts->vertex_size * max_verts);
259    output_verts->count = 0;
260 
261 
262    for (start = i = 0; i < input_prims->primitive_count;
263         start += input_prims->primitive_lengths[i], i++)
264    {
265       unsigned count = input_prims->primitive_lengths[i];
266       if (input_prims->linear) {
267          assembler_run_linear(asmblr, input_prims, input_verts,
268                               start, count);
269       } else {
270          assembler_run_elts(asmblr, input_prims, input_verts,
271                             start, count);
272       }
273    }
274 
275    output_prims->primitive_lengths[0] = output_verts->count;
276    output_prims->count = output_verts->count;
277 }
278 
279 struct draw_assembler *
draw_prim_assembler_create(struct draw_context * draw)280 draw_prim_assembler_create(struct draw_context *draw)
281 {
282    struct draw_assembler *ia = CALLOC_STRUCT( draw_assembler );
283 
284    ia->draw = draw;
285 
286    return ia;
287 }
288 
289 void
draw_prim_assembler_destroy(struct draw_assembler * ia)290 draw_prim_assembler_destroy(struct draw_assembler *ia)
291 {
292    FREE(ia);
293 }
294 
295 
296 /*
297  * Called at the very begin of the draw call with a new instance
298  * Used to reset state that should persist between primitive restart.
299  */
300 void
draw_prim_assembler_new_instance(struct draw_assembler * asmblr)301 draw_prim_assembler_new_instance(struct draw_assembler *asmblr)
302 {
303    asmblr->primid = 0;
304 }
305