1 /**************************************************************************
3 * Copyright 2013 VMware, Inc.
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:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
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.
26 **************************************************************************/
28 #include "draw_prim_assembler.h"
33 #include "util/u_debug.h"
34 #include "util/u_memory.h"
35 #include "util/u_prim.h"
37 #include "pipe/p_defines.h"
41 struct draw_context
*draw
;
43 struct draw_prim_info
*output_prims
;
44 struct draw_vertex_info
*output_verts
;
46 const struct draw_prim_info
*input_prims
;
47 const struct draw_vertex_info
*input_verts
;
58 needs_primid(const struct draw_context
*draw
)
60 const struct draw_fragment_shader
*fs
= draw
->fs
.fragment_shader
;
61 const struct draw_geometry_shader
*gs
= draw
->gs
.geometry_shader
;
62 if (fs
&& fs
->info
.uses_primid
) {
63 return !gs
|| !gs
->info
.uses_primid
;
69 draw_prim_assembler_is_required(const struct draw_context
*draw
,
70 const struct draw_prim_info
*prim_info
,
71 const struct draw_vertex_info
*vert_info
)
73 switch (prim_info
->prim
) {
74 case PIPE_PRIM_LINES_ADJACENCY
:
75 case PIPE_PRIM_LINE_STRIP_ADJACENCY
:
76 case PIPE_PRIM_TRIANGLES_ADJACENCY
:
77 case PIPE_PRIM_TRIANGLE_STRIP_ADJACENCY
:
80 return needs_primid(draw
);
85 * Copy the vertex header along with its data from the current
86 * vertex buffer into a buffer holding vertices arranged
87 * into decomposed primitives (i.e. buffer without the
91 copy_verts(struct draw_assembler
*asmblr
,
92 unsigned *indices
, unsigned num_indices
)
96 char *output
= (char*)asmblr
->output_verts
->verts
;
97 const char *input
= (const char*)asmblr
->input_verts
->verts
;
99 for (i
= 0; i
< num_indices
; ++i
) {
100 unsigned idx
= indices
[i
];
101 unsigned output_offset
=
102 asmblr
->output_verts
->count
* asmblr
->output_verts
->stride
;
103 unsigned input_offset
= asmblr
->input_verts
->stride
* idx
;
104 memcpy(output
+ output_offset
, input
+ input_offset
,
105 asmblr
->input_verts
->vertex_size
);
106 asmblr
->output_verts
->count
+= 1;
113 inject_primid(struct draw_assembler
*asmblr
,
117 int slot
= asmblr
->primid_slot
;
118 char *input
= (char*)asmblr
->input_verts
->verts
;
119 unsigned input_offset
= asmblr
->input_verts
->stride
* idx
;
120 struct vertex_header
*v
= (struct vertex_header
*)(input
+ input_offset
);
122 /* In case the backend doesn't care about it */
127 memcpy(&v
->data
[slot
][0], &primid
, sizeof(primid
));
128 memcpy(&v
->data
[slot
][1], &primid
, sizeof(primid
));
129 memcpy(&v
->data
[slot
][2], &primid
, sizeof(primid
));
130 memcpy(&v
->data
[slot
][3], &primid
, sizeof(primid
));
135 prim_point(struct draw_assembler
*asmblr
,
140 if (asmblr
->needs_primid
) {
141 inject_primid(asmblr
, idx
, asmblr
->primid
++);
145 copy_verts(asmblr
, indices
, 1);
149 prim_line(struct draw_assembler
*asmblr
,
150 unsigned i0
, unsigned i1
)
154 if (asmblr
->needs_primid
) {
155 inject_primid(asmblr
, i0
, asmblr
->primid
);
156 inject_primid(asmblr
, i1
, asmblr
->primid
++);
161 copy_verts(asmblr
, indices
, 2);
165 prim_tri(struct draw_assembler
*asmblr
,
166 unsigned i0
, unsigned i1
, unsigned i2
)
170 if (asmblr
->needs_primid
) {
171 inject_primid(asmblr
, i0
, asmblr
->primid
);
172 inject_primid(asmblr
, i1
, asmblr
->primid
);
173 inject_primid(asmblr
, i2
, asmblr
->primid
++);
179 copy_verts(asmblr
, indices
, 3);
183 draw_prim_assembler_prepare_outputs(struct draw_assembler
*ia
)
185 struct draw_context
*draw
= ia
->draw
;
186 if (needs_primid(draw
)) {
187 ia
->primid_slot
= draw_alloc_extra_vertex_attrib(
188 ia
->draw
, TGSI_SEMANTIC_PRIMID
, 0);
190 ia
->primid_slot
= -1;
195 #define FUNC assembler_run_linear
196 #define GET_ELT(idx) (start + (idx))
197 #include "draw_prim_assembler_tmp.h"
199 #define FUNC assembler_run_elts
200 #define LOCAL_VARS const ushort *elts = input_prims->elts;
201 #define GET_ELT(idx) (elts[start + (idx)])
202 #include "draw_prim_assembler_tmp.h"
207 * Primitive assembler breaks up adjacency primitives and assembles
208 * the base primitives they represent, e.g. vertices forming
209 * PIPE_PRIM_TRIANGLE_STRIP_ADJACENCY
210 * become vertices forming PIPE_PRIM_TRIANGLES
211 * This is needed because specification says that the adjacency
212 * primitives are only visible in the geometry shader so we need
213 * to get rid of them so that the rest of the pipeline can
214 * process the inputs.
217 draw_prim_assembler_run(struct draw_context
*draw
,
218 const struct draw_prim_info
*input_prims
,
219 const struct draw_vertex_info
*input_verts
,
220 struct draw_prim_info
*output_prims
,
221 struct draw_vertex_info
*output_verts
)
223 struct draw_assembler
*asmblr
= draw
->ia
;
225 unsigned assembled_prim
= u_reduced_prim(input_prims
->prim
);
226 unsigned max_primitives
= u_decomposed_prims_for_vertices(
227 input_prims
->prim
, input_prims
->count
);
228 unsigned max_verts
= u_vertices_per_prim(assembled_prim
) * max_primitives
;
230 asmblr
->output_prims
= output_prims
;
231 asmblr
->output_verts
= output_verts
;
232 asmblr
->input_prims
= input_prims
;
233 asmblr
->input_verts
= input_verts
;
234 asmblr
->needs_primid
= needs_primid(asmblr
->draw
);
235 asmblr
->num_prims
= 0;
237 output_prims
->linear
= TRUE
;
238 output_prims
->elts
= NULL
;
239 output_prims
->start
= 0;
240 output_prims
->prim
= assembled_prim
;
241 output_prims
->flags
= 0x0;
242 output_prims
->primitive_lengths
= MALLOC(sizeof(unsigned));
243 output_prims
->primitive_lengths
[0] = 0;
244 output_prims
->primitive_count
= 1;
246 output_verts
->vertex_size
= input_verts
->vertex_size
;
247 output_verts
->stride
= input_verts
->stride
;
248 output_verts
->verts
= (struct vertex_header
*)MALLOC(
249 input_verts
->vertex_size
* max_verts
);
250 output_verts
->count
= 0;
253 for (start
= i
= 0; i
< input_prims
->primitive_count
;
254 start
+= input_prims
->primitive_lengths
[i
], i
++)
256 unsigned count
= input_prims
->primitive_lengths
[i
];
257 if (input_prims
->linear
) {
258 assembler_run_linear(asmblr
, input_prims
, input_verts
,
261 assembler_run_elts(asmblr
, input_prims
, input_verts
,
266 output_prims
->primitive_lengths
[0] = output_verts
->count
;
267 output_prims
->count
= output_verts
->count
;
270 struct draw_assembler
*
271 draw_prim_assembler_create(struct draw_context
*draw
)
273 struct draw_assembler
*ia
= CALLOC_STRUCT( draw_assembler
);
281 draw_prim_assembler_destroy(struct draw_assembler
*ia
)
288 * Called at the very begin of the draw call with a new instance
289 * Used to reset state that should persist between primitive restart.
292 draw_prim_assembler_new_instance(struct draw_assembler
*asmblr
)