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 generate_linear_ushort( unsigned start,
54 ushort *out_us = (ushort *)out;
56 for (i = 0; i < nr; i++)
57 out_us[i] = (ushort)(i + start);
60 static void generate_linear_uint( unsigned start,
64 unsigned *out_ui = (unsigned *)out;
66 for (i = 0; i < nr; i++)
67 out_ui[i] = i + start;
72 * Given a primitive type and number of vertices, return the number of vertices
73 * needed to draw the primitive with fill mode = PIPE_POLYGON_MODE_LINE using
74 * separate lines (PIPE_PRIM_LINES).
77 nr_lines(enum pipe_prim_type prim, unsigned nr)
80 case PIPE_PRIM_TRIANGLES:
82 case PIPE_PRIM_TRIANGLE_STRIP:
84 case PIPE_PRIM_TRIANGLE_FAN:
88 case PIPE_PRIM_QUAD_STRIP:
89 return (nr - 2) / 2 * 8;
90 case PIPE_PRIM_POLYGON:
91 return 2 * nr; /* a line (two verts) for each polygon edge */
92 /* Note: these cases can't really be handled since drawing lines instead
93 * of triangles would also require changing the GS. But if there's no GS,
96 case PIPE_PRIM_TRIANGLES_ADJACENCY:
98 case PIPE_PRIM_TRIANGLE_STRIP_ADJACENCY:
99 return ((nr - 4) / 2) * 6;
108 u_unfilled_translator(enum pipe_prim_type prim,
109 unsigned in_index_size,
111 unsigned unfilled_mode,
112 enum pipe_prim_type *out_prim,
113 unsigned *out_index_size,
115 u_translate_func *out_translate)
120 assert(u_reduced_prim(prim) == PIPE_PRIM_TRIANGLES);
124 in_idx = in_size_idx(in_index_size);
125 *out_index_size = (in_index_size == 4) ? 4 : 2;
126 out_idx = out_size_idx(*out_index_size);
128 if (unfilled_mode == PIPE_POLYGON_MODE_POINT) {
129 *out_prim = PIPE_PRIM_POINTS;
132 switch (in_index_size) {
134 *out_translate = translate_ubyte_ushort;
135 return U_TRANSLATE_NORMAL;
137 *out_translate = translate_memcpy_uint;
138 return U_TRANSLATE_MEMCPY;
140 *out_translate = translate_memcpy_ushort;
141 return U_TRANSLATE_MEMCPY;
143 *out_translate = translate_memcpy_uint;
146 return U_TRANSLATE_ERROR;
150 assert(unfilled_mode == PIPE_POLYGON_MODE_LINE);
151 *out_prim = PIPE_PRIM_LINES;
152 *out_translate = translate_line[in_idx][out_idx][prim];
153 *out_nr = nr_lines( prim, nr );
154 return U_TRANSLATE_NORMAL;
160 * Utility for converting unfilled polygons into points, lines, triangles.
161 * Few drivers have direct support for OpenGL's glPolygonMode.
162 * This function helps with converting triangles into points or lines
163 * when the front and back fill modes are the same. When there's
164 * different front/back fill modes, that can be handled with the
168 u_unfilled_generator(enum pipe_prim_type prim,
171 unsigned unfilled_mode,
172 enum pipe_prim_type *out_prim,
173 unsigned *out_index_size,
175 u_generate_func *out_generate)
179 assert(u_reduced_prim(prim) == PIPE_PRIM_TRIANGLES);
183 *out_index_size = ((start + nr) > 0xfffe) ? 4 : 2;
184 out_idx = out_size_idx(*out_index_size);
186 if (unfilled_mode == PIPE_POLYGON_MODE_POINT) {
187 if (*out_index_size == 4)
188 *out_generate = generate_linear_uint;
190 *out_generate = generate_linear_ushort;
192 *out_prim = PIPE_PRIM_POINTS;
194 return U_GENERATE_LINEAR;
197 assert(unfilled_mode == PIPE_POLYGON_MODE_LINE);
198 *out_prim = PIPE_PRIM_LINES;
199 *out_generate = generate_line[out_idx][prim];
200 *out_nr = nr_lines( prim, nr );
202 return U_GENERATE_REUSABLE;