make llvm draw paths compile with the latest changes
[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 "pipe/p_util.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_vertex.h"
34 #include "draw/draw_pt.h"
35
36 struct pt_post_vs {
37 struct draw_context *draw;
38
39 boolean (*run)( struct pt_post_vs *pvs,
40 struct vertex_header *vertices,
41 unsigned count,
42 unsigned stride );
43 };
44
45
46
47 static INLINE float
48 dot4(const float *a, const float *b)
49 {
50 return (a[0]*b[0] +
51 a[1]*b[1] +
52 a[2]*b[2] +
53 a[3]*b[3]);
54 }
55
56
57
58 static INLINE unsigned
59 compute_clipmask_gl(const float *clip, /*const*/ float plane[][4], unsigned nr)
60 {
61 unsigned mask = 0x0;
62 unsigned i;
63
64 /* Do the hardwired planes first:
65 */
66 if (-clip[0] + clip[3] < 0) mask |= (1<<0);
67 if ( clip[0] + clip[3] < 0) mask |= (1<<1);
68 if (-clip[1] + clip[3] < 0) mask |= (1<<2);
69 if ( clip[1] + clip[3] < 0) mask |= (1<<3);
70 if ( clip[2] + clip[3] < 0) mask |= (1<<4); /* match mesa clipplane numbering - for now */
71 if (-clip[2] + clip[3] < 0) mask |= (1<<5); /* match mesa clipplane numbering - for now */
72
73 /* Followed by any remaining ones:
74 */
75 for (i = 6; i < nr; i++) {
76 if (dot4(clip, plane[i]) < 0)
77 mask |= (1<<i);
78 }
79
80 return mask;
81 }
82
83
84 /* The normal case - cliptest, rhw divide, viewport transform.
85 *
86 * Also handle identity viewport here at the expense of a few wasted
87 * instructions
88 */
89 static boolean post_vs_cliptest_viewport_gl( struct pt_post_vs *pvs,
90 struct vertex_header *vertices,
91 unsigned count,
92 unsigned stride )
93 {
94 struct vertex_header *out = vertices;
95 const float *scale = pvs->draw->viewport.scale;
96 const float *trans = pvs->draw->viewport.translate;
97 unsigned j;
98 unsigned clipped = 0;
99
100 for (j = 0; j < count; j++) {
101 out->clip[0] = out->data[0][0];
102 out->clip[1] = out->data[0][1];
103 out->clip[2] = out->data[0][2];
104 out->clip[3] = out->data[0][3];
105
106 out->vertex_id = 0xffff;
107 out->edgeflag = 1;
108 out->clipmask = compute_clipmask_gl(out->clip,
109 pvs->draw->plane,
110 pvs->draw->nr_planes);
111 clipped += out->clipmask;
112
113 if (out->clipmask == 0)
114 {
115 /* divide by w */
116 float w = 1.0f / out->data[0][3];
117
118 /* Viewport mapping */
119 out->data[0][0] = out->data[0][0] * w * scale[0] + trans[0];
120 out->data[0][1] = out->data[0][1] * w * scale[1] + trans[1];
121 out->data[0][2] = out->data[0][2] * w * scale[2] + trans[2];
122 out->data[0][3] = w;
123 }
124
125 out = (struct vertex_header *)( (char *)out + stride );
126 }
127
128 return clipped != 0;
129 }
130
131
132
133 /* If bypass_clipping is set, skip cliptest and rhw divide.
134 */
135 static boolean post_vs_viewport( struct pt_post_vs *pvs,
136 struct vertex_header *vertices,
137 unsigned count,
138 unsigned stride )
139 {
140 struct vertex_header *out = vertices;
141 const float *scale = pvs->draw->viewport.scale;
142 const float *trans = pvs->draw->viewport.translate;
143 unsigned j;
144
145 debug_printf("%s\n", __FUNCTION__);
146 for (j = 0; j < count; j++) {
147 /* Viewport mapping only, no cliptest/rhw divide
148 */
149 out->data[0][0] = out->data[0][0] * scale[0] + trans[0];
150 out->data[0][1] = out->data[0][1] * scale[1] + trans[1];
151 out->data[0][2] = out->data[0][2] * scale[2] + trans[2];
152
153 out = (struct vertex_header *)((char *)out + stride);
154 }
155
156 return FALSE;
157 }
158
159
160 /* If bypass_clipping is set and we have an identity viewport, nothing
161 * to do.
162 */
163 static boolean post_vs_none( struct pt_post_vs *pvs,
164 struct vertex_header *vertices,
165 unsigned count,
166 unsigned stride )
167 {
168 debug_printf("%s\n", __FUNCTION__);
169 return FALSE;
170 }
171
172 boolean draw_pt_post_vs_run( struct pt_post_vs *pvs,
173 struct vertex_header *pipeline_verts,
174 unsigned count,
175 unsigned stride )
176 {
177 return pvs->run( pvs, pipeline_verts, count, stride );
178 }
179
180
181 void draw_pt_post_vs_prepare( struct pt_post_vs *pvs,
182 boolean bypass_clipping,
183 boolean identity_viewport,
184 boolean opengl )
185 {
186 if (bypass_clipping) {
187 if (identity_viewport)
188 pvs->run = post_vs_none;
189 else
190 pvs->run = post_vs_viewport;
191 }
192 else {
193 //if (opengl)
194 pvs->run = post_vs_cliptest_viewport_gl;
195 }
196 }
197
198
199 struct pt_post_vs *draw_pt_post_vs_create( struct draw_context *draw )
200 {
201 struct pt_post_vs *pvs = CALLOC_STRUCT( pt_post_vs );
202 if (!pvs)
203 return NULL;
204
205 pvs->draw = draw;
206
207 return pvs;
208 }
209
210 void draw_pt_post_vs_destroy( struct pt_post_vs *pvs )
211 {
212 FREE(pvs);
213 }