2 * Copyright 2009 VMware, Inc.
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * on the rights to use, copy, modify, merge, publish, distribute, sub
9 * license, and/or sell copies of the Software, and to permit persons to whom
10 * the Software is furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice (including the next
13 * paragraph) shall be included in all copies or substantial portions of the
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
19 * VMWARE AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
20 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
21 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
22 * USE OR OTHER DEALINGS IN THE SOFTWARE.
25 #include "u_indices.h"
26 #include "u_indices_priv.h"
28 static void translate_memcpy_ushort( const void *in
,
32 unsigned restart_index
,
35 memcpy(out
, &((short *)in
)[start
], out_nr
*sizeof(short));
38 static void translate_memcpy_uint( const void *in
,
42 unsigned restart_index
,
45 memcpy(out
, &((int *)in
)[start
], out_nr
*sizeof(int));
50 * Translate indexes when a driver can't support certain types
51 * of drawing. Example include:
52 * - Translate 1-byte indexes into 2-byte indexes
53 * - Translate PIPE_PRIM_QUADS into PIPE_PRIM_TRIANGLES when the hardware
54 * doesn't support the former.
55 * - Translate from first provoking vertex to last provoking vertex and
58 * Note that this function is used for indexed primitives.
60 * \param hw_mask mask of (1 << PIPE_PRIM_x) flags indicating which types
61 * of primitives are supported by the hardware.
62 * \param prim incoming PIPE_PRIM_x
63 * \param in_index_size bytes per index value (1, 2 or 4)
64 * \param nr number of incoming vertices
65 * \param in_pv incoming provoking vertex convention (PV_FIRST or PV_LAST)
66 * \param out_pv desired provoking vertex convention (PV_FIRST or PV_LAST)
67 * \param prim_restart whether primitive restart is disable or enabled
68 * \param out_prim returns new PIPE_PRIM_x we'll translate to
69 * \param out_index_size returns bytes per new index value (2 or 4)
70 * \param out_nr returns number of new vertices
71 * \param out_translate returns the translation function to use by the caller
74 u_index_translator(unsigned hw_mask
,
75 enum pipe_prim_type prim
,
76 unsigned in_index_size
,
80 unsigned prim_restart
,
82 unsigned *out_index_size
,
84 u_translate_func
*out_translate
)
88 int ret
= U_TRANSLATE_NORMAL
;
90 assert(in_index_size
== 1 ||
96 in_idx
= in_size_idx(in_index_size
);
97 *out_index_size
= (in_index_size
== 4) ? 4 : 2;
98 out_idx
= out_size_idx(*out_index_size
);
100 if ((hw_mask
& (1<<prim
)) &&
101 in_index_size
== *out_index_size
&&
104 /* Index translation not really needed */
105 if (in_index_size
== 4)
106 *out_translate
= translate_memcpy_uint
;
108 *out_translate
= translate_memcpy_ushort
;
113 return U_TRANSLATE_MEMCPY
;
116 *out_translate
= translate
[in_idx
][out_idx
][in_pv
][out_pv
][prim_restart
][prim
];
119 case PIPE_PRIM_POINTS
:
120 *out_prim
= PIPE_PRIM_POINTS
;
124 case PIPE_PRIM_LINES
:
125 *out_prim
= PIPE_PRIM_LINES
;
129 case PIPE_PRIM_LINE_STRIP
:
130 *out_prim
= PIPE_PRIM_LINES
;
131 *out_nr
= (nr
- 1) * 2;
134 case PIPE_PRIM_LINE_LOOP
:
135 *out_prim
= PIPE_PRIM_LINES
;
139 case PIPE_PRIM_TRIANGLES
:
140 *out_prim
= PIPE_PRIM_TRIANGLES
;
144 case PIPE_PRIM_TRIANGLE_STRIP
:
145 *out_prim
= PIPE_PRIM_TRIANGLES
;
146 *out_nr
= (nr
- 2) * 3;
149 case PIPE_PRIM_TRIANGLE_FAN
:
150 *out_prim
= PIPE_PRIM_TRIANGLES
;
151 *out_nr
= (nr
- 2) * 3;
154 case PIPE_PRIM_QUADS
:
155 *out_prim
= PIPE_PRIM_TRIANGLES
;
156 *out_nr
= (nr
/ 4) * 6;
159 case PIPE_PRIM_QUAD_STRIP
:
160 *out_prim
= PIPE_PRIM_TRIANGLES
;
161 *out_nr
= (nr
- 2) * 3;
164 case PIPE_PRIM_POLYGON
:
165 *out_prim
= PIPE_PRIM_TRIANGLES
;
166 *out_nr
= (nr
- 2) * 3;
169 case PIPE_PRIM_LINES_ADJACENCY
:
170 *out_prim
= PIPE_PRIM_LINES_ADJACENCY
;
174 case PIPE_PRIM_LINE_STRIP_ADJACENCY
:
175 *out_prim
= PIPE_PRIM_LINES_ADJACENCY
;
176 *out_nr
= (nr
- 3) * 4;
179 case PIPE_PRIM_TRIANGLES_ADJACENCY
:
180 *out_prim
= PIPE_PRIM_TRIANGLES_ADJACENCY
;
184 case PIPE_PRIM_TRIANGLE_STRIP_ADJACENCY
:
185 *out_prim
= PIPE_PRIM_TRIANGLES_ADJACENCY
;
186 *out_nr
= ((nr
- 4) / 2) * 6;
191 *out_prim
= PIPE_PRIM_POINTS
;
193 return U_TRANSLATE_ERROR
;
202 * If a driver does not support a particular gallium primitive type
203 * (such as PIPE_PRIM_QUAD_STRIP) this function can be used to help
204 * convert the primitive into a simpler type (like PIPE_PRIM_TRIANGLES).
206 * The generator functions generates a number of ushort or uint indexes
207 * for drawing the new type of primitive.
209 * Note that this function is used for non-indexed primitives.
211 * \param hw_mask a bitmask of (1 << PIPE_PRIM_x) values that indicates
212 * kind of primitives are supported by the driver.
213 * \param prim the PIPE_PRIM_x that the user wants to draw
214 * \param start index of first vertex to draw
215 * \param nr number of vertices to draw
216 * \param in_pv user's provoking vertex (PV_FIRST/LAST)
217 * \param out_pv desired proking vertex for the hardware (PV_FIRST/LAST)
218 * \param out_prim returns the new primitive type for the driver
219 * \param out_index_size returns OUT_USHORT or OUT_UINT
220 * \param out_nr returns new number of vertices to draw
221 * \param out_generate returns pointer to the generator function
224 u_index_generator(unsigned hw_mask
,
225 enum pipe_prim_type prim
,
230 enum pipe_prim_type
*out_prim
,
231 unsigned *out_index_size
,
233 u_generate_func
*out_generate
)
239 *out_index_size
= ((start
+ nr
) > 0xfffe) ? 4 : 2;
240 out_idx
= out_size_idx(*out_index_size
);
242 if ((hw_mask
& (1<<prim
)) &&
245 *out_generate
= generate
[out_idx
][in_pv
][out_pv
][PIPE_PRIM_POINTS
];
248 return U_GENERATE_LINEAR
;
251 *out_generate
= generate
[out_idx
][in_pv
][out_pv
][prim
];
254 case PIPE_PRIM_POINTS
:
255 *out_prim
= PIPE_PRIM_POINTS
;
257 return U_GENERATE_REUSABLE
;
259 case PIPE_PRIM_LINES
:
260 *out_prim
= PIPE_PRIM_LINES
;
262 return U_GENERATE_REUSABLE
;
264 case PIPE_PRIM_LINE_STRIP
:
265 *out_prim
= PIPE_PRIM_LINES
;
266 *out_nr
= (nr
- 1) * 2;
267 return U_GENERATE_REUSABLE
;
269 case PIPE_PRIM_LINE_LOOP
:
270 *out_prim
= PIPE_PRIM_LINES
;
272 return U_GENERATE_ONE_OFF
;
274 case PIPE_PRIM_TRIANGLES
:
275 *out_prim
= PIPE_PRIM_TRIANGLES
;
277 return U_GENERATE_REUSABLE
;
279 case PIPE_PRIM_TRIANGLE_STRIP
:
280 *out_prim
= PIPE_PRIM_TRIANGLES
;
281 *out_nr
= (nr
- 2) * 3;
282 return U_GENERATE_REUSABLE
;
284 case PIPE_PRIM_TRIANGLE_FAN
:
285 *out_prim
= PIPE_PRIM_TRIANGLES
;
286 *out_nr
= (nr
- 2) * 3;
287 return U_GENERATE_REUSABLE
;
289 case PIPE_PRIM_QUADS
:
290 *out_prim
= PIPE_PRIM_TRIANGLES
;
291 *out_nr
= (nr
/ 4) * 6;
292 return U_GENERATE_REUSABLE
;
294 case PIPE_PRIM_QUAD_STRIP
:
295 *out_prim
= PIPE_PRIM_TRIANGLES
;
296 *out_nr
= (nr
- 2) * 3;
297 return U_GENERATE_REUSABLE
;
299 case PIPE_PRIM_POLYGON
:
300 *out_prim
= PIPE_PRIM_TRIANGLES
;
301 *out_nr
= (nr
- 2) * 3;
302 return U_GENERATE_REUSABLE
;
304 case PIPE_PRIM_LINES_ADJACENCY
:
305 *out_prim
= PIPE_PRIM_LINES_ADJACENCY
;
307 return U_GENERATE_REUSABLE
;
309 case PIPE_PRIM_LINE_STRIP_ADJACENCY
:
310 *out_prim
= PIPE_PRIM_LINES_ADJACENCY
;
311 *out_nr
= (nr
- 3) * 4;
312 return U_GENERATE_REUSABLE
;
314 case PIPE_PRIM_TRIANGLES_ADJACENCY
:
315 *out_prim
= PIPE_PRIM_TRIANGLES_ADJACENCY
;
317 return U_GENERATE_REUSABLE
;
319 case PIPE_PRIM_TRIANGLE_STRIP_ADJACENCY
:
320 *out_prim
= PIPE_PRIM_TRIANGLES_ADJACENCY
;
321 *out_nr
= ((nr
- 4) / 2) * 6;
322 return U_GENERATE_REUSABLE
;
326 *out_generate
= generate
[out_idx
][in_pv
][out_pv
][PIPE_PRIM_POINTS
];
327 *out_prim
= PIPE_PRIM_POINTS
;
329 return U_TRANSLATE_ERROR
;