1 /*
2 * Copyright 2009 VMware, Inc.
3 * All Rights Reserved.
4 *
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:
11 *
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
14 * Software.
15 *
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.
23 */
24
25 #include "u_indices.h"
26 #include "u_indices_priv.h"
27
translate_memcpy_ushort(const void * in,unsigned start,unsigned in_nr,unsigned out_nr,unsigned restart_index,void * out)28 static void translate_memcpy_ushort( const void *in,
29 unsigned start,
30 unsigned in_nr,
31 unsigned out_nr,
32 unsigned restart_index,
33 void *out )
34 {
35 memcpy(out, &((short *)in)[start], out_nr*sizeof(short));
36 }
37
translate_memcpy_uint(const void * in,unsigned start,unsigned in_nr,unsigned out_nr,unsigned restart_index,void * out)38 static void translate_memcpy_uint( const void *in,
39 unsigned start,
40 unsigned in_nr,
41 unsigned out_nr,
42 unsigned restart_index,
43 void *out )
44 {
45 memcpy(out, &((int *)in)[start], out_nr*sizeof(int));
46 }
47
translate_byte_to_ushort(const void * in,unsigned start,UNUSED unsigned in_nr,unsigned out_nr,UNUSED unsigned restart_index,void * out)48 static void translate_byte_to_ushort( const void *in,
49 unsigned start,
50 UNUSED unsigned in_nr,
51 unsigned out_nr,
52 UNUSED unsigned restart_index,
53 void *out )
54 {
55 uint8_t *src = (uint8_t *)in + start;
56 uint16_t *dst = out;
57 while (out_nr--) {
58 *dst++ = *src++;
59 }
60 }
61
62 /**
63 * Translate indexes when a driver can't support certain types
64 * of drawing. Example include:
65 * - Translate 1-byte indexes into 2-byte indexes
66 * - Translate PIPE_PRIM_QUADS into PIPE_PRIM_TRIANGLES when the hardware
67 * doesn't support the former.
68 * - Translate from first provoking vertex to last provoking vertex and
69 * vice versa.
70 *
71 * Note that this function is used for indexed primitives.
72 *
73 * \param hw_mask mask of (1 << PIPE_PRIM_x) flags indicating which types
74 * of primitives are supported by the hardware.
75 * \param prim incoming PIPE_PRIM_x
76 * \param in_index_size bytes per index value (1, 2 or 4)
77 * \param nr number of incoming vertices
78 * \param in_pv incoming provoking vertex convention (PV_FIRST or PV_LAST)
79 * \param out_pv desired provoking vertex convention (PV_FIRST or PV_LAST)
80 * \param prim_restart whether primitive restart is disable or enabled
81 * \param out_prim returns new PIPE_PRIM_x we'll translate to
82 * \param out_index_size returns bytes per new index value (2 or 4)
83 * \param out_nr returns number of new vertices
84 * \param out_translate returns the translation function to use by the caller
85 */
86 enum indices_mode
u_index_translator(unsigned hw_mask,enum pipe_prim_type prim,unsigned in_index_size,unsigned nr,unsigned in_pv,unsigned out_pv,unsigned prim_restart,enum pipe_prim_type * out_prim,unsigned * out_index_size,unsigned * out_nr,u_translate_func * out_translate)87 u_index_translator(unsigned hw_mask,
88 enum pipe_prim_type prim,
89 unsigned in_index_size,
90 unsigned nr,
91 unsigned in_pv,
92 unsigned out_pv,
93 unsigned prim_restart,
94 enum pipe_prim_type *out_prim,
95 unsigned *out_index_size,
96 unsigned *out_nr,
97 u_translate_func *out_translate)
98 {
99 unsigned in_idx;
100 unsigned out_idx;
101 enum indices_mode ret = U_TRANSLATE_NORMAL;
102
103 assert(in_index_size == 1 ||
104 in_index_size == 2 ||
105 in_index_size == 4);
106
107 u_index_init();
108
109 in_idx = in_size_idx(in_index_size);
110 *out_index_size = (in_index_size == 4) ? 4 : 2;
111 out_idx = out_size_idx(*out_index_size);
112
113 if ((hw_mask & (1<<prim)) &&
114 in_pv == out_pv)
115 {
116 if (in_index_size == 4)
117 *out_translate = translate_memcpy_uint;
118 else if (in_index_size == 2)
119 *out_translate = translate_memcpy_ushort;
120 else
121 *out_translate = translate_byte_to_ushort;
122
123 *out_prim = prim;
124 *out_nr = nr;
125
126 return U_TRANSLATE_MEMCPY;
127 }
128 else {
129 *out_translate = translate[in_idx][out_idx][in_pv][out_pv][prim_restart][prim];
130
131 switch (prim) {
132 case PIPE_PRIM_POINTS:
133 *out_prim = PIPE_PRIM_POINTS;
134 *out_nr = nr;
135 break;
136
137 case PIPE_PRIM_LINES:
138 *out_prim = PIPE_PRIM_LINES;
139 *out_nr = nr;
140 break;
141
142 case PIPE_PRIM_LINE_STRIP:
143 *out_prim = PIPE_PRIM_LINES;
144 *out_nr = (nr - 1) * 2;
145 break;
146
147 case PIPE_PRIM_LINE_LOOP:
148 *out_prim = PIPE_PRIM_LINES;
149 *out_nr = nr * 2;
150 break;
151
152 case PIPE_PRIM_TRIANGLES:
153 *out_prim = PIPE_PRIM_TRIANGLES;
154 *out_nr = nr;
155 break;
156
157 case PIPE_PRIM_TRIANGLE_STRIP:
158 *out_prim = PIPE_PRIM_TRIANGLES;
159 *out_nr = (nr - 2) * 3;
160 break;
161
162 case PIPE_PRIM_TRIANGLE_FAN:
163 *out_prim = PIPE_PRIM_TRIANGLES;
164 *out_nr = (nr - 2) * 3;
165 break;
166
167 case PIPE_PRIM_QUADS:
168 *out_prim = PIPE_PRIM_TRIANGLES;
169 *out_nr = (nr / 4) * 6;
170 break;
171
172 case PIPE_PRIM_QUAD_STRIP:
173 *out_prim = PIPE_PRIM_TRIANGLES;
174 *out_nr = (nr - 2) * 3;
175 break;
176
177 case PIPE_PRIM_POLYGON:
178 *out_prim = PIPE_PRIM_TRIANGLES;
179 *out_nr = (nr - 2) * 3;
180 break;
181
182 case PIPE_PRIM_LINES_ADJACENCY:
183 *out_prim = PIPE_PRIM_LINES_ADJACENCY;
184 *out_nr = nr;
185 break;
186
187 case PIPE_PRIM_LINE_STRIP_ADJACENCY:
188 *out_prim = PIPE_PRIM_LINES_ADJACENCY;
189 *out_nr = (nr - 3) * 4;
190 break;
191
192 case PIPE_PRIM_TRIANGLES_ADJACENCY:
193 *out_prim = PIPE_PRIM_TRIANGLES_ADJACENCY;
194 *out_nr = nr;
195 break;
196
197 case PIPE_PRIM_TRIANGLE_STRIP_ADJACENCY:
198 *out_prim = PIPE_PRIM_TRIANGLES_ADJACENCY;
199 *out_nr = ((nr - 4) / 2) * 6;
200 break;
201
202 default:
203 assert(0);
204 *out_prim = PIPE_PRIM_POINTS;
205 *out_nr = nr;
206 return U_TRANSLATE_ERROR;
207 }
208 }
209
210 return ret;
211 }
212
213
214 /**
215 * If a driver does not support a particular gallium primitive type
216 * (such as PIPE_PRIM_QUAD_STRIP) this function can be used to help
217 * convert the primitive into a simpler type (like PIPE_PRIM_TRIANGLES).
218 *
219 * The generator functions generates a number of ushort or uint indexes
220 * for drawing the new type of primitive.
221 *
222 * Note that this function is used for non-indexed primitives.
223 *
224 * \param hw_mask a bitmask of (1 << PIPE_PRIM_x) values that indicates
225 * kind of primitives are supported by the driver.
226 * \param prim the PIPE_PRIM_x that the user wants to draw
227 * \param start index of first vertex to draw
228 * \param nr number of vertices to draw
229 * \param in_pv user's provoking vertex (PV_FIRST/LAST)
230 * \param out_pv desired proking vertex for the hardware (PV_FIRST/LAST)
231 * \param out_prim returns the new primitive type for the driver
232 * \param out_index_size returns OUT_USHORT or OUT_UINT
233 * \param out_nr returns new number of vertices to draw
234 * \param out_generate returns pointer to the generator function
235 */
236 enum indices_mode
u_index_generator(unsigned hw_mask,enum pipe_prim_type prim,unsigned start,unsigned nr,unsigned in_pv,unsigned out_pv,enum pipe_prim_type * out_prim,unsigned * out_index_size,unsigned * out_nr,u_generate_func * out_generate)237 u_index_generator(unsigned hw_mask,
238 enum pipe_prim_type prim,
239 unsigned start,
240 unsigned nr,
241 unsigned in_pv,
242 unsigned out_pv,
243 enum pipe_prim_type *out_prim,
244 unsigned *out_index_size,
245 unsigned *out_nr,
246 u_generate_func *out_generate)
247 {
248 unsigned out_idx;
249
250 u_index_init();
251
252 *out_index_size = ((start + nr) > 0xfffe) ? 4 : 2;
253 out_idx = out_size_idx(*out_index_size);
254
255 if ((hw_mask & (1<<prim)) &&
256 (in_pv == out_pv)) {
257
258 *out_generate = generate[out_idx][in_pv][out_pv][PIPE_PRIM_POINTS];
259 *out_prim = prim;
260 *out_nr = nr;
261 return U_GENERATE_LINEAR;
262 }
263 else {
264 *out_generate = generate[out_idx][in_pv][out_pv][prim];
265
266 switch (prim) {
267 case PIPE_PRIM_POINTS:
268 *out_prim = PIPE_PRIM_POINTS;
269 *out_nr = nr;
270 return U_GENERATE_REUSABLE;
271
272 case PIPE_PRIM_LINES:
273 *out_prim = PIPE_PRIM_LINES;
274 *out_nr = nr;
275 return U_GENERATE_REUSABLE;
276
277 case PIPE_PRIM_LINE_STRIP:
278 *out_prim = PIPE_PRIM_LINES;
279 *out_nr = (nr - 1) * 2;
280 return U_GENERATE_REUSABLE;
281
282 case PIPE_PRIM_LINE_LOOP:
283 *out_prim = PIPE_PRIM_LINES;
284 *out_nr = nr * 2;
285 return U_GENERATE_ONE_OFF;
286
287 case PIPE_PRIM_TRIANGLES:
288 *out_prim = PIPE_PRIM_TRIANGLES;
289 *out_nr = nr;
290 return U_GENERATE_REUSABLE;
291
292 case PIPE_PRIM_TRIANGLE_STRIP:
293 *out_prim = PIPE_PRIM_TRIANGLES;
294 *out_nr = (nr - 2) * 3;
295 return U_GENERATE_REUSABLE;
296
297 case PIPE_PRIM_TRIANGLE_FAN:
298 *out_prim = PIPE_PRIM_TRIANGLES;
299 *out_nr = (nr - 2) * 3;
300 return U_GENERATE_REUSABLE;
301
302 case PIPE_PRIM_QUADS:
303 *out_prim = PIPE_PRIM_TRIANGLES;
304 *out_nr = (nr / 4) * 6;
305 return U_GENERATE_REUSABLE;
306
307 case PIPE_PRIM_QUAD_STRIP:
308 *out_prim = PIPE_PRIM_TRIANGLES;
309 *out_nr = (nr - 2) * 3;
310 return U_GENERATE_REUSABLE;
311
312 case PIPE_PRIM_POLYGON:
313 *out_prim = PIPE_PRIM_TRIANGLES;
314 *out_nr = (nr - 2) * 3;
315 return U_GENERATE_REUSABLE;
316
317 case PIPE_PRIM_LINES_ADJACENCY:
318 *out_prim = PIPE_PRIM_LINES_ADJACENCY;
319 *out_nr = nr;
320 return U_GENERATE_REUSABLE;
321
322 case PIPE_PRIM_LINE_STRIP_ADJACENCY:
323 *out_prim = PIPE_PRIM_LINES_ADJACENCY;
324 *out_nr = (nr - 3) * 4;
325 return U_GENERATE_REUSABLE;
326
327 case PIPE_PRIM_TRIANGLES_ADJACENCY:
328 *out_prim = PIPE_PRIM_TRIANGLES_ADJACENCY;
329 *out_nr = nr;
330 return U_GENERATE_REUSABLE;
331
332 case PIPE_PRIM_TRIANGLE_STRIP_ADJACENCY:
333 *out_prim = PIPE_PRIM_TRIANGLES_ADJACENCY;
334 *out_nr = ((nr - 4) / 2) * 6;
335 return U_GENERATE_REUSABLE;
336
337 default:
338 assert(0);
339 *out_generate = generate[out_idx][in_pv][out_pv][PIPE_PRIM_POINTS];
340 *out_prim = PIPE_PRIM_POINTS;
341 *out_nr = nr;
342 return U_TRANSLATE_ERROR;
343 }
344 }
345 }
346