Merge remote-tracking branch 'origin/master' into pipe-video
[mesa.git] / src / gallium / auxiliary / util / u_prim.h
1 /**************************************************************************
2 *
3 * Copyright 2008 Tungsten Graphics, Inc., Cedar Park, Texas.
4 * All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 *
26 **************************************************************************/
27
28
29 #ifndef U_BLIT_H
30 #define U_BLIT_H
31
32
33 #include "pipe/p_defines.h"
34 #include "util/u_debug.h"
35
36 #ifdef __cplusplus
37 extern "C" {
38 #endif
39
40 static INLINE boolean u_validate_pipe_prim( unsigned pipe_prim, unsigned nr )
41 {
42 boolean ok = TRUE;
43
44 switch (pipe_prim) {
45 case PIPE_PRIM_POINTS:
46 ok = (nr >= 1);
47 break;
48 case PIPE_PRIM_LINES:
49 ok = (nr >= 2);
50 break;
51 case PIPE_PRIM_LINE_STRIP:
52 case PIPE_PRIM_LINE_LOOP:
53 ok = (nr >= 2);
54 break;
55 case PIPE_PRIM_TRIANGLES:
56 ok = (nr >= 3);
57 break;
58 case PIPE_PRIM_TRIANGLE_STRIP:
59 case PIPE_PRIM_TRIANGLE_FAN:
60 case PIPE_PRIM_POLYGON:
61 ok = (nr >= 3);
62 break;
63 case PIPE_PRIM_QUADS:
64 ok = (nr >= 4);
65 break;
66 case PIPE_PRIM_QUAD_STRIP:
67 ok = (nr >= 4);
68 break;
69 default:
70 ok = 0;
71 break;
72 }
73
74 return ok;
75 }
76
77
78 static INLINE boolean u_trim_pipe_prim( unsigned pipe_prim, unsigned *nr )
79 {
80 boolean ok = TRUE;
81
82 switch (pipe_prim) {
83 case PIPE_PRIM_POINTS:
84 ok = (*nr >= 1);
85 break;
86 case PIPE_PRIM_LINES:
87 ok = (*nr >= 2);
88 *nr -= (*nr % 2);
89 break;
90 case PIPE_PRIM_LINE_STRIP:
91 case PIPE_PRIM_LINE_LOOP:
92 ok = (*nr >= 2);
93 break;
94 case PIPE_PRIM_TRIANGLES:
95 ok = (*nr >= 3);
96 *nr -= (*nr % 3);
97 break;
98 case PIPE_PRIM_TRIANGLE_STRIP:
99 case PIPE_PRIM_TRIANGLE_FAN:
100 case PIPE_PRIM_POLYGON:
101 ok = (*nr >= 3);
102 break;
103 case PIPE_PRIM_QUADS:
104 ok = (*nr >= 4);
105 *nr -= (*nr % 4);
106 break;
107 case PIPE_PRIM_QUAD_STRIP:
108 ok = (*nr >= 4);
109 *nr -= (*nr % 2);
110 break;
111 case PIPE_PRIM_LINES_ADJACENCY:
112 ok = (*nr >= 4);
113 *nr -= (*nr % 4);
114 break;
115 case PIPE_PRIM_LINE_STRIP_ADJACENCY:
116 ok = (*nr >= 4);
117 break;
118 case PIPE_PRIM_TRIANGLES_ADJACENCY:
119 ok = (*nr >= 6);
120 *nr -= (*nr % 5);
121 break;
122 case PIPE_PRIM_TRIANGLE_STRIP_ADJACENCY:
123 ok = (*nr >= 4);
124 break;
125 default:
126 ok = 0;
127 break;
128 }
129
130 if (!ok)
131 *nr = 0;
132
133 return ok;
134 }
135
136
137 static INLINE unsigned u_reduced_prim( unsigned pipe_prim )
138 {
139 switch (pipe_prim) {
140 case PIPE_PRIM_POINTS:
141 return PIPE_PRIM_POINTS;
142
143 case PIPE_PRIM_LINES:
144 case PIPE_PRIM_LINE_STRIP:
145 case PIPE_PRIM_LINE_LOOP:
146 return PIPE_PRIM_LINES;
147
148 default:
149 return PIPE_PRIM_TRIANGLES;
150 }
151 }
152
153 static INLINE unsigned
154 u_vertices_per_prim(int primitive)
155 {
156 switch(primitive) {
157 case PIPE_PRIM_POINTS:
158 return 1;
159 case PIPE_PRIM_LINES:
160 case PIPE_PRIM_LINE_LOOP:
161 case PIPE_PRIM_LINE_STRIP:
162 return 2;
163 case PIPE_PRIM_TRIANGLES:
164 case PIPE_PRIM_TRIANGLE_STRIP:
165 case PIPE_PRIM_TRIANGLE_FAN:
166 return 3;
167 case PIPE_PRIM_LINES_ADJACENCY:
168 case PIPE_PRIM_LINE_STRIP_ADJACENCY:
169 return 4;
170 case PIPE_PRIM_TRIANGLES_ADJACENCY:
171 case PIPE_PRIM_TRIANGLE_STRIP_ADJACENCY:
172 return 6;
173
174 /* following primitives should never be used
175 * with geometry shaders abd their size is
176 * undefined */
177 case PIPE_PRIM_POLYGON:
178 case PIPE_PRIM_QUADS:
179 case PIPE_PRIM_QUAD_STRIP:
180 default:
181 debug_printf("Unrecognized geometry shader primitive");
182 return 3;
183 }
184 }
185
186 /**
187 * Returns the number of decomposed primitives for the given
188 * vertex count.
189 * Geometry shader is invoked once for each triangle in
190 * triangle strip, triangle fans and triangles and once
191 * for each line in line strip, line loop, lines.
192 */
193 static INLINE unsigned
194 u_gs_prims_for_vertices(int primitive, int vertices)
195 {
196 switch(primitive) {
197 case PIPE_PRIM_POINTS:
198 return vertices;
199 case PIPE_PRIM_LINES:
200 return vertices / 2;
201 case PIPE_PRIM_LINE_LOOP:
202 return vertices;
203 case PIPE_PRIM_LINE_STRIP:
204 return vertices - 1;
205 case PIPE_PRIM_TRIANGLES:
206 return vertices / 3;
207 case PIPE_PRIM_TRIANGLE_STRIP:
208 return vertices - 2;
209 case PIPE_PRIM_TRIANGLE_FAN:
210 return vertices - 2;
211 case PIPE_PRIM_LINES_ADJACENCY:
212 return vertices / 2;
213 case PIPE_PRIM_LINE_STRIP_ADJACENCY:
214 return vertices - 1;
215 case PIPE_PRIM_TRIANGLES_ADJACENCY:
216 return vertices / 3;
217 case PIPE_PRIM_TRIANGLE_STRIP_ADJACENCY:
218 return vertices - 2;
219
220 /* following primitives should never be used
221 * with geometry shaders abd their size is
222 * undefined */
223 case PIPE_PRIM_POLYGON:
224 case PIPE_PRIM_QUADS:
225 case PIPE_PRIM_QUAD_STRIP:
226 default:
227 debug_printf("Unrecognized geometry shader primitive");
228 return 3;
229 }
230 }
231
232 const char *u_prim_name( unsigned pipe_prim );
233
234 #endif