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
26 /*
27 * NOTE: This file is not compiled by itself. It's actually #included
28 * by the generated u_unfilled_gen.c file!
29 */
30
31 #include "u_indices.h"
32 #include "u_indices_priv.h"
33 #include "util/u_prim.h"
34
35
translate_ubyte_ushort(const void * in,unsigned start,unsigned in_nr,unsigned out_nr,unsigned restart_index,void * out)36 static void translate_ubyte_ushort( const void *in,
37 unsigned start,
38 unsigned in_nr,
39 unsigned out_nr,
40 unsigned restart_index,
41 void *out )
42 {
43 const uint8_t *in_ub = (const uint8_t *)in;
44 uint16_t *out_us = (uint16_t *)out;
45 unsigned i;
46 for (i = 0; i < out_nr; i++)
47 out_us[i] = (uint16_t) in_ub[i+start];
48 }
49
generate_linear_ushort(unsigned start,unsigned nr,void * out)50 static void generate_linear_ushort( unsigned start,
51 unsigned nr,
52 void *out )
53 {
54 uint16_t *out_us = (uint16_t *)out;
55 unsigned i;
56 for (i = 0; i < nr; i++)
57 out_us[i] = (uint16_t)(i + start);
58 }
59
generate_linear_uint(unsigned start,unsigned nr,void * out)60 static void generate_linear_uint( unsigned start,
61 unsigned nr,
62 void *out )
63 {
64 unsigned *out_ui = (unsigned *)out;
65 unsigned i;
66 for (i = 0; i < nr; i++)
67 out_ui[i] = i + start;
68 }
69
70
71 /**
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 (MESA_PRIM_LINES).
75 */
76 static unsigned
nr_lines(enum mesa_prim prim,unsigned nr)77 nr_lines(enum mesa_prim prim, unsigned nr)
78 {
79 switch (prim) {
80 case MESA_PRIM_TRIANGLES:
81 return (nr / 3) * 6;
82 case MESA_PRIM_TRIANGLE_STRIP:
83 return (nr - 2) * 6;
84 case MESA_PRIM_TRIANGLE_FAN:
85 return (nr - 2) * 6;
86 case MESA_PRIM_QUADS:
87 return (nr / 4) * 8;
88 case MESA_PRIM_QUAD_STRIP:
89 return (nr - 2) / 2 * 8;
90 case MESA_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,
94 * this should work.
95 */
96 case MESA_PRIM_TRIANGLES_ADJACENCY:
97 return (nr / 6) * 6;
98 case MESA_PRIM_TRIANGLE_STRIP_ADJACENCY:
99 return ((nr - 4) / 2) * 6;
100 default:
101 assert(0);
102 return 0;
103 }
104 }
105
106
107 enum indices_mode
u_unfilled_translator(enum mesa_prim prim,unsigned in_index_size,unsigned nr,unsigned unfilled_mode,enum mesa_prim * out_prim,unsigned * out_index_size,unsigned * out_nr,u_translate_func * out_translate)108 u_unfilled_translator(enum mesa_prim prim,
109 unsigned in_index_size,
110 unsigned nr,
111 unsigned unfilled_mode,
112 enum mesa_prim *out_prim,
113 unsigned *out_index_size,
114 unsigned *out_nr,
115 u_translate_func *out_translate)
116 {
117 unsigned in_idx;
118 unsigned out_idx;
119
120 assert(u_reduced_prim(prim) == MESA_PRIM_TRIANGLES);
121
122 u_unfilled_init();
123
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);
127
128 if (unfilled_mode == PIPE_POLYGON_MODE_POINT) {
129 *out_prim = MESA_PRIM_POINTS;
130 *out_nr = nr;
131
132 switch (in_index_size) {
133 case 1:
134 *out_translate = translate_ubyte_ushort;
135 return U_TRANSLATE_NORMAL;
136 case 2:
137 *out_translate = translate_memcpy_uint;
138 return U_TRANSLATE_MEMCPY;
139 case 4:
140 *out_translate = translate_memcpy_ushort;
141 return U_TRANSLATE_MEMCPY;
142 default:
143 *out_translate = translate_memcpy_uint;
144 *out_nr = 0;
145 assert(0);
146 return U_TRANSLATE_ERROR;
147 }
148 }
149 else {
150 assert(unfilled_mode == PIPE_POLYGON_MODE_LINE);
151 *out_prim = MESA_PRIM_LINES;
152 *out_translate = translate_line[in_idx][out_idx][prim];
153 *out_nr = nr_lines( prim, nr );
154 return U_TRANSLATE_NORMAL;
155 }
156 }
157
158
159 /**
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
165 * 'draw' module.
166 */
167 enum indices_mode
u_unfilled_generator(enum mesa_prim prim,unsigned start,unsigned nr,unsigned unfilled_mode,enum mesa_prim * out_prim,unsigned * out_index_size,unsigned * out_nr,u_generate_func * out_generate)168 u_unfilled_generator(enum mesa_prim prim,
169 unsigned start,
170 unsigned nr,
171 unsigned unfilled_mode,
172 enum mesa_prim *out_prim,
173 unsigned *out_index_size,
174 unsigned *out_nr,
175 u_generate_func *out_generate)
176 {
177 unsigned out_idx;
178
179 assert(u_reduced_prim(prim) == MESA_PRIM_TRIANGLES);
180
181 u_unfilled_init();
182
183 *out_index_size = ((start + nr) > 0xfffe) ? 4 : 2;
184 out_idx = out_size_idx(*out_index_size);
185
186 if (unfilled_mode == PIPE_POLYGON_MODE_POINT) {
187 if (*out_index_size == 4)
188 *out_generate = generate_linear_uint;
189 else
190 *out_generate = generate_linear_ushort;
191
192 *out_prim = MESA_PRIM_POINTS;
193 *out_nr = nr;
194 return U_GENERATE_LINEAR;
195 }
196 else {
197 assert(unfilled_mode == PIPE_POLYGON_MODE_LINE);
198 *out_prim = MESA_PRIM_LINES;
199 *out_generate = generate_line[out_idx][prim];
200 *out_nr = nr_lines( prim, nr );
201
202 return U_GENERATE_REUSABLE;
203 }
204 }
205