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.
27 * NOTE: This file is not compiled by itself. It's actually #included
28 * by the generated u_unfilled_gen.c file!
31 #include "u_indices.h"
32 #include "u_indices_priv.h"
33 #include "util/u_prim.h"
36 static void translate_ubyte_ushort( const void *in,
40 unsigned restart_index,
43 const ubyte *in_ub = (const ubyte *)in;
44 ushort *out_us = (ushort *)out;
46 for (i = 0; i < out_nr; i++)
47 out_us[i] = (ushort) in_ub[i+start];
50 static void translate_memcpy_ushort( const void *in,
54 unsigned restart_index,
57 memcpy(out, &((short *)in)[start], out_nr*sizeof(short));
60 static void translate_memcpy_uint( const void *in,
64 unsigned restart_index,
67 memcpy(out, &((int *)in)[start], out_nr*sizeof(int));
71 static void generate_linear_ushort( unsigned start,
75 ushort *out_us = (ushort *)out;
77 for (i = 0; i < nr; i++)
78 out_us[i] = (ushort)(i + start);
81 static void generate_linear_uint( unsigned start,
85 unsigned *out_ui = (unsigned *)out;
87 for (i = 0; i < nr; i++)
88 out_ui[i] = i + start;
93 * Given a primitive type and number of vertices, return the number of vertices
94 * needed to draw the primitive with fill mode = PIPE_POLYGON_MODE_LINE using
95 * separate lines (PIPE_PRIM_LINES).
98 nr_lines(unsigned prim, unsigned nr)
101 case PIPE_PRIM_TRIANGLES:
103 case PIPE_PRIM_TRIANGLE_STRIP:
105 case PIPE_PRIM_TRIANGLE_FAN:
107 case PIPE_PRIM_QUADS:
109 case PIPE_PRIM_QUAD_STRIP:
110 return (nr - 2) / 2 * 8;
111 case PIPE_PRIM_POLYGON:
112 return 2 * nr; /* a line (two verts) for each polygon edge */
113 /* Note: these cases can't really be handled since drawing lines instead
114 * of triangles would also require changing the GS. But if there's no GS,
117 case PIPE_PRIM_TRIANGLES_ADJACENCY:
119 case PIPE_PRIM_TRIANGLE_STRIP_ADJACENCY:
120 return ((nr - 4) / 2) * 6;
129 u_unfilled_translator(unsigned prim,
130 unsigned in_index_size,
132 unsigned unfilled_mode,
134 unsigned *out_index_size,
136 u_translate_func *out_translate)
141 assert(u_reduced_prim(prim) == PIPE_PRIM_TRIANGLES);
145 in_idx = in_size_idx(in_index_size);
146 *out_index_size = (in_index_size == 4) ? 4 : 2;
147 out_idx = out_size_idx(*out_index_size);
149 if (unfilled_mode == PIPE_POLYGON_MODE_POINT) {
150 *out_prim = PIPE_PRIM_POINTS;
153 switch (in_index_size) {
155 *out_translate = translate_ubyte_ushort;
156 return U_TRANSLATE_NORMAL;
158 *out_translate = translate_memcpy_uint;
159 return U_TRANSLATE_MEMCPY;
161 *out_translate = translate_memcpy_ushort;
162 return U_TRANSLATE_MEMCPY;
164 *out_translate = translate_memcpy_uint;
167 return U_TRANSLATE_ERROR;
171 assert(unfilled_mode == PIPE_POLYGON_MODE_LINE);
172 *out_prim = PIPE_PRIM_LINES;
173 *out_translate = translate_line[in_idx][out_idx][prim];
174 *out_nr = nr_lines( prim, nr );
175 return U_TRANSLATE_NORMAL;
181 * Utility for converting unfilled polygons into points, lines, triangles.
182 * Few drivers have direct support for OpenGL's glPolygonMode.
183 * This function helps with converting triangles into points or lines
184 * when the front and back fill modes are the same. When there's
185 * different front/back fill modes, that can be handled with the
189 u_unfilled_generator(unsigned prim,
192 unsigned unfilled_mode,
194 unsigned *out_index_size,
196 u_generate_func *out_generate)
200 assert(u_reduced_prim(prim) == PIPE_PRIM_TRIANGLES);
204 *out_index_size = ((start + nr) > 0xfffe) ? 4 : 2;
205 out_idx = out_size_idx(*out_index_size);
207 if (unfilled_mode == PIPE_POLYGON_MODE_POINT) {
208 if (*out_index_size == 4)
209 *out_generate = generate_linear_uint;
211 *out_generate = generate_linear_ushort;
213 *out_prim = PIPE_PRIM_POINTS;
215 return U_GENERATE_LINEAR;
218 assert(unfilled_mode == PIPE_POLYGON_MODE_LINE);
219 *out_prim = PIPE_PRIM_LINES;
220 *out_generate = generate_line[out_idx][prim];
221 *out_nr = nr_lines( prim, nr );
223 return U_GENERATE_REUSABLE;