5525dfc748df616e315843e95081f6eee8b075d3
[mesa.git] / src / gallium / auxiliary / draw / draw_pt_post_vs.c
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 #include "util/u_memory.h"
29 #include "pipe/p_context.h"
30 #include "draw/draw_context.h"
31 #include "draw/draw_private.h"
32 #include "draw/draw_vbuf.h"
33 #include "draw/draw_pt.h"
34
35 struct pt_post_vs {
36 struct draw_context *draw;
37
38 boolean (*run)( struct pt_post_vs *pvs,
39 struct vertex_header *vertices,
40 unsigned count,
41 unsigned stride );
42 };
43
44
45
46 static INLINE float
47 dot4(const float *a, const float *b)
48 {
49 return (a[0]*b[0] +
50 a[1]*b[1] +
51 a[2]*b[2] +
52 a[3]*b[3]);
53 }
54
55
56
57 static INLINE unsigned
58 compute_clipmask_gl(const float *clip, /*const*/ float plane[][4], unsigned nr)
59 {
60 unsigned mask = 0x0;
61 unsigned i;
62
63 #if 0
64 debug_printf("compute clipmask %f %f %f %f\n",
65 clip[0], clip[1], clip[2], clip[3]);
66 assert(clip[3] != 0.0);
67 #endif
68
69 /* Do the hardwired planes first:
70 */
71 if (-clip[0] + clip[3] < 0) mask |= (1<<0);
72 if ( clip[0] + clip[3] < 0) mask |= (1<<1);
73 if (-clip[1] + clip[3] < 0) mask |= (1<<2);
74 if ( clip[1] + clip[3] < 0) mask |= (1<<3);
75 if ( clip[2] + clip[3] < 0) mask |= (1<<4); /* match mesa clipplane numbering - for now */
76 if (-clip[2] + clip[3] < 0) mask |= (1<<5); /* match mesa clipplane numbering - for now */
77
78 /* Followed by any remaining ones:
79 */
80 for (i = 6; i < nr; i++) {
81 if (dot4(clip, plane[i]) < 0)
82 mask |= (1<<i);
83 }
84
85 return mask;
86 }
87
88
89 /* The normal case - cliptest, rhw divide, viewport transform.
90 *
91 * Also handle identity viewport here at the expense of a few wasted
92 * instructions
93 */
94 static boolean post_vs_cliptest_viewport_gl( struct pt_post_vs *pvs,
95 struct vertex_header *vertices,
96 unsigned count,
97 unsigned stride )
98 {
99 struct vertex_header *out = vertices;
100 const float *scale = pvs->draw->viewport.scale;
101 const float *trans = pvs->draw->viewport.translate;
102 const unsigned pos = draw_current_shader_position_output(pvs->draw);
103 unsigned clipped = 0;
104 unsigned j;
105
106 if (0) debug_printf("%s\n", __FUNCTION__);
107
108 for (j = 0; j < count; j++) {
109 float *position = out->data[pos];
110
111 #if 0
112 debug_printf("%d) io = %p, data = %p = [%f, %f, %f, %f]\n",
113 j, out, position, position[0], position[1], position[2], position[3]);
114 #endif
115
116 out->clip[0] = position[0];
117 out->clip[1] = position[1];
118 out->clip[2] = position[2];
119 out->clip[3] = position[3];
120
121 out->vertex_id = 0xffff;
122 out->clipmask = compute_clipmask_gl(out->clip,
123 pvs->draw->plane,
124 pvs->draw->nr_planes);
125 clipped += out->clipmask;
126
127 if (out->clipmask == 0)
128 {
129 /* divide by w */
130 float w = 1.0f / position[3];
131
132 /* Viewport mapping */
133 position[0] = position[0] * w * scale[0] + trans[0];
134 position[1] = position[1] * w * scale[1] + trans[1];
135 position[2] = position[2] * w * scale[2] + trans[2];
136 position[3] = w;
137 #if 0
138 debug_printf("post viewport: %f %f %f %f\n",
139 position[0],
140 position[1],
141 position[2],
142 position[3]);
143 #endif
144 }
145
146 out = (struct vertex_header *)( (char *)out + stride );
147 }
148
149 return clipped != 0;
150 }
151
152
153
154 /* As above plus edgeflags
155 */
156 static boolean
157 post_vs_cliptest_viewport_gl_edgeflag(struct pt_post_vs *pvs,
158 struct vertex_header *vertices,
159 unsigned count,
160 unsigned stride )
161 {
162 unsigned j;
163 boolean needpipe;
164
165 needpipe = post_vs_cliptest_viewport_gl( pvs, vertices, count, stride);
166
167 /* If present, copy edgeflag VS output into vertex header.
168 * Otherwise, leave header as is.
169 */
170 if (pvs->draw->vs.edgeflag_output) {
171 struct vertex_header *out = vertices;
172 int ef = pvs->draw->vs.edgeflag_output;
173
174 for (j = 0; j < count; j++) {
175 const float *edgeflag = out->data[ef];
176 out->edgeflag = !(edgeflag[0] != 1.0f);
177 needpipe |= !out->edgeflag;
178 out = (struct vertex_header *)( (char *)out + stride );
179 }
180 }
181 return needpipe;
182 }
183
184
185
186
187 /* If bypass_clipping is set, skip cliptest and rhw divide.
188 */
189 static boolean post_vs_viewport( struct pt_post_vs *pvs,
190 struct vertex_header *vertices,
191 unsigned count,
192 unsigned stride )
193 {
194 struct vertex_header *out = vertices;
195 const float *scale = pvs->draw->viewport.scale;
196 const float *trans = pvs->draw->viewport.translate;
197 const unsigned pos = draw_current_shader_position_output(pvs->draw);
198 unsigned j;
199
200 if (0) debug_printf("%s\n", __FUNCTION__);
201 for (j = 0; j < count; j++) {
202 float *position = out->data[pos];
203
204 /* Viewport mapping only, no cliptest/rhw divide
205 */
206 position[0] = position[0] * scale[0] + trans[0];
207 position[1] = position[1] * scale[1] + trans[1];
208 position[2] = position[2] * scale[2] + trans[2];
209
210 out = (struct vertex_header *)((char *)out + stride);
211 }
212
213 return FALSE;
214 }
215
216
217 /* If bypass_clipping is set and we have an identity viewport, nothing
218 * to do.
219 */
220 static boolean post_vs_none( struct pt_post_vs *pvs,
221 struct vertex_header *vertices,
222 unsigned count,
223 unsigned stride )
224 {
225 if (0) debug_printf("%s\n", __FUNCTION__);
226 return FALSE;
227 }
228
229 boolean draw_pt_post_vs_run( struct pt_post_vs *pvs,
230 struct vertex_header *pipeline_verts,
231 unsigned count,
232 unsigned stride )
233 {
234 return pvs->run( pvs, pipeline_verts, count, stride );
235 }
236
237
238 void draw_pt_post_vs_prepare( struct pt_post_vs *pvs,
239 boolean bypass_clipping,
240 boolean bypass_viewport,
241 boolean opengl,
242 boolean need_edgeflags )
243 {
244 if (!need_edgeflags) {
245 if (bypass_clipping) {
246 if (bypass_viewport)
247 pvs->run = post_vs_none;
248 else
249 pvs->run = post_vs_viewport;
250 }
251 else {
252 /* if (opengl) */
253 pvs->run = post_vs_cliptest_viewport_gl;
254 }
255 }
256 else {
257 /* If we need to copy edgeflags to the vertex header, it should
258 * mean we're running the primitive pipeline. Hence the bypass
259 * flags should be false.
260 */
261 assert(!bypass_clipping);
262 assert(!bypass_viewport);
263 pvs->run = post_vs_cliptest_viewport_gl_edgeflag;
264 }
265 }
266
267
268 struct pt_post_vs *draw_pt_post_vs_create( struct draw_context *draw )
269 {
270 struct pt_post_vs *pvs = CALLOC_STRUCT( pt_post_vs );
271 if (!pvs)
272 return NULL;
273
274 pvs->draw = draw;
275
276 return pvs;
277 }
278
279 void draw_pt_post_vs_destroy( struct pt_post_vs *pvs )
280 {
281 FREE(pvs);
282 }