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