Compute need_z and need_w properly and use the correct const/linear/perspective inter...
[mesa.git] / src / mesa / pipe / softpipe / sp_quad_fs.c
1 /*
2 * Mesa 3-D graphics library
3 * Version: 6.5
4 *
5 * Copyright (C) 1999-2005 Brian Paul All Rights Reserved.
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
21 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 */
24
25 /* Vertices are just an array of floats, with all the attributes
26 * packed. We currently assume a layout like:
27 *
28 * attr[0][0..3] - window position
29 * attr[1..n][0..3] - remaining attributes.
30 *
31 * Attributes are assumed to be 4 floats wide but are packed so that
32 * all the enabled attributes run contiguously.
33 */
34
35 #include "glheader.h"
36 #include "imports.h"
37 #include "sp_context.h"
38 #include "sp_headers.h"
39 #include "sp_quad.h"
40 #include "core/tgsi_core.h"
41
42 struct exec_machine {
43 const struct setup_coefficient *coef; /**< will point to quad->coef */
44
45 GLfloat attr[FRAG_ATTRIB_MAX][4][QUAD_SIZE];
46 };
47
48
49 /**
50 * Compute quad's attributes values, as constants (GL_FLAT shading).
51 */
52 static INLINE void cinterp( struct exec_machine *exec,
53 GLuint attrib,
54 GLuint i )
55 {
56 GLuint j;
57
58 for (j = 0; j < QUAD_SIZE; j++) {
59 exec->attr[attrib][i][j] = exec->coef[attrib].a0[i];
60 }
61 }
62
63
64 /**
65 * Compute quad's attribute values by linear interpolation.
66 *
67 * Push into the fp:
68 *
69 * INPUT[attr] = MAD COEF_A0[attr], COEF_DADX[attr], INPUT_WPOS.xxxx
70 * INPUT[attr] = MAD INPUT[attr], COEF_DADY[attr], INPUT_WPOS.yyyy
71 */
72 static INLINE void linterp( struct exec_machine *exec,
73 GLuint attrib,
74 GLuint i )
75 {
76 GLuint j;
77
78 for (j = 0; j < QUAD_SIZE; j++) {
79 const GLfloat x = exec->attr[FRAG_ATTRIB_WPOS][0][j];
80 const GLfloat y = exec->attr[FRAG_ATTRIB_WPOS][1][j];
81 exec->attr[attrib][i][j] = (exec->coef[attrib].a0[i] +
82 exec->coef[attrib].dadx[i] * x +
83 exec->coef[attrib].dady[i] * y);
84 }
85 }
86
87
88 /**
89 * Compute quad's attribute values by linear interpolation with
90 * perspective correction.
91 *
92 * Push into the fp:
93 *
94 * INPUT[attr] = MAD COEF_A0[attr], COEF_DADX[attr], INPUT_WPOS.xxxx
95 * INPUT[attr] = MAD INPUT[attr], COEF_DADY[attr], INPUT_WPOS.yyyy
96 * INPUT[attr] = MUL INPUT[attr], INPUT_WPOS.wwww
97 *
98 * (Or should that be 1/w ???)
99 */
100 static INLINE void pinterp( struct exec_machine *exec,
101 GLuint attrib,
102 GLuint i )
103 {
104 GLuint j;
105
106 for (j = 0; j < QUAD_SIZE; j++) {
107 const GLfloat x = exec->attr[FRAG_ATTRIB_WPOS][0][j];
108 const GLfloat y = exec->attr[FRAG_ATTRIB_WPOS][1][j];
109 const GLfloat invW = exec->attr[FRAG_ATTRIB_WPOS][3][j];
110 exec->attr[attrib][i][j] = ((exec->coef[attrib].a0[i] +
111 exec->coef[attrib].dadx[i] * x +
112 exec->coef[attrib].dady[i] * y) * invW);
113 }
114 }
115
116
117
118 /* This should be done by the fragment shader execution unit (code
119 * generated from the decl instructions). Do it here for now.
120 */
121 static void
122 shade_quad( struct quad_stage *qs, struct quad_header *quad )
123 {
124 const struct softpipe_context *softpipe = qs->softpipe;
125 struct exec_machine exec;
126 const GLfloat fx = quad->x0;
127 const GLfloat fy = quad->y0;
128 GLuint i, j;
129
130 exec.coef = quad->coef;
131
132 /* Position:
133 */
134 exec.attr[FRAG_ATTRIB_WPOS][0][0] = fx;
135 exec.attr[FRAG_ATTRIB_WPOS][0][1] = fx + 1.0;
136 exec.attr[FRAG_ATTRIB_WPOS][0][2] = fx;
137 exec.attr[FRAG_ATTRIB_WPOS][0][3] = fx + 1.0;
138
139 exec.attr[FRAG_ATTRIB_WPOS][1][0] = fy;
140 exec.attr[FRAG_ATTRIB_WPOS][1][1] = fy;
141 exec.attr[FRAG_ATTRIB_WPOS][1][2] = fy + 1.0;
142 exec.attr[FRAG_ATTRIB_WPOS][1][3] = fy + 1.0;
143
144 /* Z and W are done by linear interpolation */
145 if (softpipe->need_z) {
146 linterp(&exec, 0, 2); /* attr[0].z */
147 }
148
149 if (softpipe->need_w) {
150 linterp(&exec, 0, 3); /* attr[0].w */
151 /*invert(&exec, 0, 3);*/
152 }
153
154 /* Interpolate all the remaining attributes. This will get pushed
155 * into the fragment program's responsibilities at some point.
156 * Start at 1 to skip fragment position attribute (computed above).
157 */
158 for (i = 1; i < quad->nr_attrs; i++) {
159 switch (softpipe->interp[i]) {
160 case INTERP_CONSTANT:
161 for (j = 0; j < NUM_CHANNELS; j++)
162 cinterp(&exec, i, j);
163 break;
164
165 case INTERP_LINEAR:
166 for (j = 0; j < NUM_CHANNELS; j++)
167 linterp(&exec, i, j);
168 break;
169
170 case INTERP_PERSPECTIVE:
171 for (j = 0; j < NUM_CHANNELS; j++)
172 pinterp(&exec, i, j);
173 break;
174 }
175 }
176
177 #if 1
178 /*softpipe->run_fs( tri->fp, quad, &tri->outputs );*/
179
180 {
181 struct tgsi_exec_machine machine;
182 struct tgsi_exec_vector inputs[FRAG_ATTRIB_MAX + 1];
183 struct tgsi_exec_vector outputs[FRAG_ATTRIB_MAX + 1];
184 struct tgsi_exec_vector *ainputs;
185 struct tgsi_exec_vector *aoutputs;
186 GLuint i /*, total*/;
187
188 #ifdef DEBUG
189 memset(&machine, 0, sizeof(machine));
190 #endif
191
192 ainputs = (struct tgsi_exec_vector *) tgsi_align_128bit( inputs );
193 aoutputs = (struct tgsi_exec_vector *) tgsi_align_128bit( outputs );
194
195 #if 0
196 for( i = total = 0; i < PIPE_ATTRIB_MAX; i++ ) {
197 GLuint attr;
198
199 attr = softpipe->fp_attr_to_slot[i];
200 if( attr || total == 0) {
201 assert( total < FRAG_ATTRIB_MAX );
202 assert( attr < FRAG_ATTRIB_MAX );
203 assert( sizeof( ainputs[0] ) == sizeof( exec.attr[0] ) );
204
205 memcpy(
206 &ainputs[total],
207 exec.attr[attr],
208 sizeof( ainputs[0] ) );
209 total++;
210 }
211 }
212 #else
213 /* load input registers */
214 /* XXX simpler than above, but might not be right... */
215 for (i = 0; i < softpipe->nr_attrs; i++) {
216 memcpy(
217 &ainputs[i],
218 exec.attr[i],
219 sizeof( ainputs[0] ) );
220 }
221 #endif
222
223 /* init machine state */
224 tgsi_exec_machine_init(
225 &machine,
226 softpipe->fs.tokens );
227
228 machine.Inputs = ainputs;
229 machine.Outputs = aoutputs;
230 machine.Consts = softpipe->fs.constants->constant; /* XXX alignment? */
231
232 /* run shader */
233 tgsi_exec_machine_run( &machine );
234
235 /* store result color */
236 memcpy(quad->outputs.color,
237 &aoutputs[FRAG_ATTRIB_COL0].xyzw[0].f[0],
238 sizeof(quad->outputs.color));
239 if (softpipe->need_z) {
240 /* XXX temporary */
241 quad->outputs.depth[0] = exec.attr[0][2][0];
242 quad->outputs.depth[1] = exec.attr[0][2][1];
243 quad->outputs.depth[2] = exec.attr[0][2][2];
244 quad->outputs.depth[3] = exec.attr[0][2][3];
245 }
246 }
247 #else
248 {
249 GLuint attr = softpipe->fp_attr_to_slot[FRAG_ATTRIB_COL0];
250 assert(attr);
251
252 memcpy(quad->outputs.color,
253 exec.attr[attr],
254 sizeof(quad->outputs.color));
255
256 if (softpipe->need_z) {
257 quad->outputs.depth[0] = exec.attr[0][2][0];
258 quad->outputs.depth[1] = exec.attr[0][2][1];
259 quad->outputs.depth[2] = exec.attr[0][2][2];
260 quad->outputs.depth[3] = exec.attr[0][2][3];
261 }
262 }
263 #endif
264
265 /* shader may cull fragments */
266 if (quad->mask)
267 qs->next->run(qs->next, quad);
268 }
269
270
271
272 struct quad_stage *sp_quad_shade_stage( struct softpipe_context *softpipe )
273 {
274 struct quad_stage *stage = CALLOC_STRUCT(quad_stage);
275
276 stage->softpipe = softpipe;
277 stage->run = shade_quad;
278
279 return stage;
280 }