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