util: Fix reduction of line adjacency primitives.
[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 const static unsigned values[][2] = {
82 { 1, 0 }, /* PIPE_PRIM_POINTS */
83 { 2, 2 }, /* PIPE_PRIM_LINES */
84 { 2, 0 }, /* PIPE_PRIM_LINE_LOOP */
85 { 2, 0 }, /* PIPE_PRIM_LINE_STRIP */
86 { 3, 3 }, /* PIPE_PRIM_TRIANGLES */
87 { 3, 0 }, /* PIPE_PRIM_TRIANGLE_STRIP */
88 { 3, 0 }, /* PIPE_PRIM_TRIANGLE_FAN */
89 { 4, 4 }, /* PIPE_PRIM_TRIANGLE_QUADS */
90 { 4, 2 }, /* PIPE_PRIM_TRIANGLE_QUAD_STRIP */
91 { 3, 0 }, /* PIPE_PRIM_TRIANGLE_POLYGON */
92 { 4, 4 }, /* PIPE_PRIM_LINES_ADJACENCY */
93 { 4, 0 }, /* PIPE_PRIM_LINE_STRIP_ADJACENCY */
94 { 6, 5 }, /* PIPE_PRIM_TRIANGLES_ADJACENCY */
95 { 4, 0 }, /* PIPE_PRIM_TRIANGLE_STRIP_ADJACENCY */
96 };
97
98 if (unlikely(pipe_prim >= PIPE_PRIM_MAX)) {
99 *nr = 0;
100 return FALSE;
101 }
102
103 ok = (*nr >= values[pipe_prim][0]);
104 if (values[pipe_prim][1])
105 *nr -= (*nr % values[pipe_prim][1]);
106
107 if (!ok)
108 *nr = 0;
109
110 return ok;
111 }
112
113
114 static INLINE unsigned u_reduced_prim( unsigned pipe_prim )
115 {
116 switch (pipe_prim) {
117 case PIPE_PRIM_POINTS:
118 return PIPE_PRIM_POINTS;
119
120 case PIPE_PRIM_LINES:
121 case PIPE_PRIM_LINES_ADJACENCY:
122 case PIPE_PRIM_LINE_STRIP:
123 case PIPE_PRIM_LINE_STRIP_ADJACENCY:
124 case PIPE_PRIM_LINE_LOOP:
125 return PIPE_PRIM_LINES;
126
127 default:
128 return PIPE_PRIM_TRIANGLES;
129 }
130 }
131
132 static INLINE unsigned
133 u_vertices_per_prim(int primitive)
134 {
135 switch(primitive) {
136 case PIPE_PRIM_POINTS:
137 return 1;
138 case PIPE_PRIM_LINES:
139 case PIPE_PRIM_LINE_LOOP:
140 case PIPE_PRIM_LINE_STRIP:
141 return 2;
142 case PIPE_PRIM_TRIANGLES:
143 case PIPE_PRIM_TRIANGLE_STRIP:
144 case PIPE_PRIM_TRIANGLE_FAN:
145 return 3;
146 case PIPE_PRIM_LINES_ADJACENCY:
147 case PIPE_PRIM_LINE_STRIP_ADJACENCY:
148 return 4;
149 case PIPE_PRIM_TRIANGLES_ADJACENCY:
150 case PIPE_PRIM_TRIANGLE_STRIP_ADJACENCY:
151 return 6;
152
153 /* following primitives should never be used
154 * with geometry shaders abd their size is
155 * undefined */
156 case PIPE_PRIM_POLYGON:
157 case PIPE_PRIM_QUADS:
158 case PIPE_PRIM_QUAD_STRIP:
159 default:
160 debug_printf("Unrecognized geometry shader primitive");
161 return 3;
162 }
163 }
164
165 /**
166 * Returns the number of decomposed primitives for the given
167 * vertex count.
168 * Geometry shader is invoked once for each triangle in
169 * triangle strip, triangle fans and triangles and once
170 * for each line in line strip, line loop, lines.
171 */
172 static INLINE unsigned
173 u_gs_prims_for_vertices(int primitive, int vertices)
174 {
175 switch(primitive) {
176 case PIPE_PRIM_POINTS:
177 return vertices;
178 case PIPE_PRIM_LINES:
179 return vertices / 2;
180 case PIPE_PRIM_LINE_LOOP:
181 return vertices;
182 case PIPE_PRIM_LINE_STRIP:
183 return vertices - 1;
184 case PIPE_PRIM_TRIANGLES:
185 return vertices / 3;
186 case PIPE_PRIM_TRIANGLE_STRIP:
187 return vertices - 2;
188 case PIPE_PRIM_TRIANGLE_FAN:
189 return vertices - 2;
190 case PIPE_PRIM_LINES_ADJACENCY:
191 return vertices / 2;
192 case PIPE_PRIM_LINE_STRIP_ADJACENCY:
193 return vertices - 1;
194 case PIPE_PRIM_TRIANGLES_ADJACENCY:
195 return vertices / 3;
196 case PIPE_PRIM_TRIANGLE_STRIP_ADJACENCY:
197 return vertices - 2;
198
199 /* following primitives should never be used
200 * with geometry shaders abd their size is
201 * undefined */
202 case PIPE_PRIM_POLYGON:
203 case PIPE_PRIM_QUADS:
204 case PIPE_PRIM_QUAD_STRIP:
205 default:
206 debug_printf("Unrecognized geometry shader primitive");
207 return 3;
208 }
209 }
210
211 const char *u_prim_name( unsigned pipe_prim );
212
213 #endif