Merge remote branch 'main/master' into radeon-rewrite
[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_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 #if 0
65 debug_printf("compute clipmask %f %f %f %f\n",
66 clip[0], clip[1], clip[2], clip[3]);
67 assert(clip[3] != 0.0);
68 #endif
69
70 /* Do the hardwired planes first:
71 */
72 if (-clip[0] + clip[3] < 0) mask |= (1<<0);
73 if ( clip[0] + clip[3] < 0) mask |= (1<<1);
74 if (-clip[1] + clip[3] < 0) mask |= (1<<2);
75 if ( clip[1] + clip[3] < 0) mask |= (1<<3);
76 if ( clip[2] + clip[3] < 0) mask |= (1<<4); /* match mesa clipplane numbering - for now */
77 if (-clip[2] + clip[3] < 0) mask |= (1<<5); /* match mesa clipplane numbering - for now */
78
79 /* Followed by any remaining ones:
80 */
81 for (i = 6; i < nr; i++) {
82 if (dot4(clip, plane[i]) < 0)
83 mask |= (1<<i);
84 }
85
86 return mask;
87 }
88
89
90 /* The normal case - cliptest, rhw divide, viewport transform.
91 *
92 * Also handle identity viewport here at the expense of a few wasted
93 * instructions
94 */
95 static boolean post_vs_cliptest_viewport_gl( struct pt_post_vs *pvs,
96 struct vertex_header *vertices,
97 unsigned count,
98 unsigned stride )
99 {
100 struct vertex_header *out = vertices;
101 const float *scale = pvs->draw->viewport.scale;
102 const float *trans = pvs->draw->viewport.translate;
103 const unsigned pos = pvs->draw->vs.position_output;
104 unsigned clipped = 0;
105 unsigned j;
106
107 if (0) debug_printf("%s\n");
108
109 for (j = 0; j < count; j++) {
110 float *position = out->data[pos];
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 + stride );
143 }
144
145 return clipped != 0;
146 }
147
148
149
150 /* If bypass_clipping is set, skip cliptest and rhw divide.
151 */
152 static boolean post_vs_viewport( struct pt_post_vs *pvs,
153 struct vertex_header *vertices,
154 unsigned count,
155 unsigned stride )
156 {
157 struct vertex_header *out = vertices;
158 const float *scale = pvs->draw->viewport.scale;
159 const float *trans = pvs->draw->viewport.translate;
160 const unsigned pos = pvs->draw->vs.position_output;
161 unsigned j;
162
163 if (0) debug_printf("%s\n", __FUNCTION__);
164 for (j = 0; j < count; j++) {
165 float *position = out->data[pos];
166
167 /* Viewport mapping only, no cliptest/rhw divide
168 */
169 position[0] = position[0] * scale[0] + trans[0];
170 position[1] = position[1] * scale[1] + trans[1];
171 position[2] = position[2] * scale[2] + trans[2];
172
173 out = (struct vertex_header *)((char *)out + stride);
174 }
175
176 return FALSE;
177 }
178
179
180 /* If bypass_clipping is set and we have an identity viewport, nothing
181 * to do.
182 */
183 static boolean post_vs_none( struct pt_post_vs *pvs,
184 struct vertex_header *vertices,
185 unsigned count,
186 unsigned stride )
187 {
188 if (0) debug_printf("%s\n", __FUNCTION__);
189 return FALSE;
190 }
191
192 boolean draw_pt_post_vs_run( struct pt_post_vs *pvs,
193 struct vertex_header *pipeline_verts,
194 unsigned count,
195 unsigned stride )
196 {
197 return pvs->run( pvs, pipeline_verts, count, stride );
198 }
199
200
201 void draw_pt_post_vs_prepare( struct pt_post_vs *pvs,
202 boolean bypass_clipping,
203 boolean bypass_viewport,
204 boolean opengl )
205 {
206 if (bypass_clipping) {
207 if (bypass_viewport)
208 pvs->run = post_vs_none;
209 else
210 pvs->run = post_vs_viewport;
211 }
212 else {
213 //if (opengl)
214 pvs->run = post_vs_cliptest_viewport_gl;
215 }
216 }
217
218
219 struct pt_post_vs *draw_pt_post_vs_create( struct draw_context *draw )
220 {
221 struct pt_post_vs *pvs = CALLOC_STRUCT( pt_post_vs );
222 if (!pvs)
223 return NULL;
224
225 pvs->draw = draw;
226
227 return pvs;
228 }
229
230 void draw_pt_post_vs_destroy( struct pt_post_vs *pvs )
231 {
232 FREE(pvs);
233 }