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_byte_to_ushort( const void *in,
30 UNUSED unsigned in_nr,
32 UNUSED unsigned restart_index,
35 uint8_t *src = (uint8_t *)in + start;
43 u_index_prim_type_convert(unsigned hw_mask, enum pipe_prim_type prim, bool pv_matches)
45 if ((hw_mask & (1<<prim)) && pv_matches)
49 case PIPE_PRIM_POINTS:
50 return PIPE_PRIM_POINTS;
52 case PIPE_PRIM_LINE_STRIP:
53 case PIPE_PRIM_LINE_LOOP:
54 return PIPE_PRIM_LINES;
55 case PIPE_PRIM_TRIANGLES:
56 case PIPE_PRIM_TRIANGLE_STRIP:
57 case PIPE_PRIM_TRIANGLE_FAN:
59 case PIPE_PRIM_QUAD_STRIP:
60 case PIPE_PRIM_POLYGON:
61 return PIPE_PRIM_TRIANGLES;
62 case PIPE_PRIM_LINES_ADJACENCY:
63 case PIPE_PRIM_LINE_STRIP_ADJACENCY:
64 return PIPE_PRIM_LINES_ADJACENCY;
65 case PIPE_PRIM_TRIANGLES_ADJACENCY:
66 case PIPE_PRIM_TRIANGLE_STRIP_ADJACENCY:
67 return PIPE_PRIM_TRIANGLES_ADJACENCY;
68 case PIPE_PRIM_PATCHES:
69 return PIPE_PRIM_PATCHES;
74 return PIPE_PRIM_POINTS;
78 * Translate indexes when a driver can't support certain types
79 * of drawing. Example include:
80 * - Translate 1-byte indexes into 2-byte indexes
81 * - Translate PIPE_PRIM_QUADS into PIPE_PRIM_TRIANGLES when the hardware
82 * doesn't support the former.
83 * - Translate from first provoking vertex to last provoking vertex and
86 * Note that this function is used for indexed primitives.
88 * \param hw_mask mask of (1 << PIPE_PRIM_x) flags indicating which types
89 * of primitives are supported by the hardware.
90 * \param prim incoming PIPE_PRIM_x
91 * \param in_index_size bytes per index value (1, 2 or 4)
92 * \param nr number of incoming vertices
93 * \param in_pv incoming provoking vertex convention (PV_FIRST or PV_LAST)
94 * \param out_pv desired provoking vertex convention (PV_FIRST or PV_LAST)
95 * \param prim_restart whether primitive restart is disable or enabled
96 * \param out_prim returns new PIPE_PRIM_x we'll translate to
97 * \param out_index_size returns bytes per new index value (2 or 4)
98 * \param out_nr returns number of new vertices
99 * \param out_translate returns the translation function to use by the caller
102 u_index_translator(unsigned hw_mask,
103 enum pipe_prim_type prim,
104 unsigned in_index_size,
108 unsigned prim_restart,
109 enum pipe_prim_type *out_prim,
110 unsigned *out_index_size,
112 u_translate_func *out_translate)
116 enum indices_mode ret = U_TRANSLATE_NORMAL;
118 assert(in_index_size == 1 ||
119 in_index_size == 2 ||
124 in_idx = in_size_idx(in_index_size);
125 *out_index_size = u_index_size_convert(in_index_size);
126 out_idx = out_size_idx(*out_index_size);
128 if ((hw_mask & (1<<prim)) &&
131 if (in_index_size == 4)
132 *out_translate = translate_memcpy_uint;
133 else if (in_index_size == 2)
134 *out_translate = translate_memcpy_ushort;
136 *out_translate = translate_byte_to_ushort;
141 return U_TRANSLATE_MEMCPY;
143 *out_translate = translate[in_idx][out_idx][in_pv][out_pv][prim_restart][prim];
144 *out_prim = u_index_prim_type_convert(hw_mask, prim, in_pv == out_pv);
145 *out_nr = u_index_count_converted_indices(hw_mask, in_pv == out_pv, prim, nr);
151 u_index_count_converted_indices(unsigned hw_mask, bool pv_matches, enum pipe_prim_type prim, unsigned nr)
153 if ((hw_mask & (1<<prim)) && pv_matches)
157 case PIPE_PRIM_POINTS:
158 case PIPE_PRIM_PATCHES:
160 case PIPE_PRIM_LINES:
162 case PIPE_PRIM_LINE_STRIP:
164 case PIPE_PRIM_LINE_LOOP:
166 case PIPE_PRIM_TRIANGLES:
168 case PIPE_PRIM_TRIANGLE_STRIP:
170 case PIPE_PRIM_TRIANGLE_FAN:
172 case PIPE_PRIM_QUADS:
174 case PIPE_PRIM_QUAD_STRIP:
176 case PIPE_PRIM_POLYGON:
178 case PIPE_PRIM_LINES_ADJACENCY:
180 case PIPE_PRIM_LINE_STRIP_ADJACENCY:
182 case PIPE_PRIM_TRIANGLES_ADJACENCY:
184 case PIPE_PRIM_TRIANGLE_STRIP_ADJACENCY:
185 return ((nr - 4) / 2) * 6;
195 * If a driver does not support a particular gallium primitive type
196 * (such as PIPE_PRIM_QUAD_STRIP) this function can be used to help
197 * convert the primitive into a simpler type (like PIPE_PRIM_TRIANGLES).
199 * The generator functions generates a number of ushort or uint indexes
200 * for drawing the new type of primitive.
202 * Note that this function is used for non-indexed primitives.
204 * \param hw_mask a bitmask of (1 << PIPE_PRIM_x) values that indicates
205 * kind of primitives are supported by the driver.
206 * \param prim the PIPE_PRIM_x that the user wants to draw
207 * \param start index of first vertex to draw
208 * \param nr number of vertices to draw
209 * \param in_pv user's provoking vertex (PV_FIRST/LAST)
210 * \param out_pv desired proking vertex for the hardware (PV_FIRST/LAST)
211 * \param out_prim returns the new primitive type for the driver
212 * \param out_index_size returns OUT_USHORT or OUT_UINT
213 * \param out_nr returns new number of vertices to draw
214 * \param out_generate returns pointer to the generator function
217 u_index_generator(unsigned hw_mask,
218 enum pipe_prim_type prim,
223 enum pipe_prim_type *out_prim,
224 unsigned *out_index_size,
226 u_generate_func *out_generate)
232 *out_index_size = ((start + nr) > 0xfffe) ? 4 : 2;
233 out_idx = out_size_idx(*out_index_size);
234 *out_prim = u_index_prim_type_convert(hw_mask, prim, in_pv == out_pv);
235 *out_nr = u_index_count_converted_indices(hw_mask, in_pv == out_pv, prim, nr);
237 if ((hw_mask & (1<<prim)) &&
240 *out_generate = generate[out_idx][in_pv][out_pv][PIPE_PRIM_POINTS];
241 return U_GENERATE_LINEAR;
243 *out_generate = generate[out_idx][in_pv][out_pv][prim];
244 return prim == PIPE_PRIM_LINE_LOOP ? U_GENERATE_ONE_OFF : U_GENERATE_REUSABLE;