Use fp_attr_to_slot[].
[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 "tgsi/core/tgsi_core.h"
41
42 #if 0
43 #if defined __GNUC__
44 #define ALIGNED_ATTRIBS 1
45 #else
46 #define ALIGNED_ATTRIBS 0
47 #endif
48 #else
49 #define ALIGNED_ATTRIBS 0
50 #endif
51
52 struct exec_machine {
53 const struct setup_coefficient *coef; /**< will point to quad->coef */
54
55 #if ALIGNED_ATTRIBS
56 GLfloat attr[FRAG_ATTRIB_MAX][NUM_CHANNELS][QUAD_SIZE] __attribute__(( aligned( 16 ) ));
57 #else
58 GLfloat attr[FRAG_ATTRIB_MAX][NUM_CHANNELS][QUAD_SIZE];
59 #endif
60 };
61
62
63 /**
64 * Compute quad's attributes values, as constants (GL_FLAT shading).
65 */
66 static INLINE void cinterp( struct exec_machine *exec,
67 GLuint attrib,
68 GLuint i )
69 {
70 GLuint j;
71
72 for (j = 0; j < QUAD_SIZE; j++) {
73 exec->attr[attrib][i][j] = exec->coef[attrib].a0[i];
74 }
75 }
76
77
78 /**
79 * Compute quad's attribute values by linear interpolation.
80 *
81 * Push into the fp:
82 *
83 * INPUT[attr] = MAD COEF_A0[attr], COEF_DADX[attr], INPUT_WPOS.xxxx
84 * INPUT[attr] = MAD INPUT[attr], COEF_DADY[attr], INPUT_WPOS.yyyy
85 */
86 static INLINE void linterp( struct exec_machine *exec,
87 GLuint attrib,
88 GLuint i )
89 {
90 GLuint j;
91
92 for (j = 0; j < QUAD_SIZE; j++) {
93 const GLfloat x = exec->attr[FRAG_ATTRIB_WPOS][0][j];
94 const GLfloat y = exec->attr[FRAG_ATTRIB_WPOS][1][j];
95 exec->attr[attrib][i][j] = (exec->coef[attrib].a0[i] +
96 exec->coef[attrib].dadx[i] * x +
97 exec->coef[attrib].dady[i] * y);
98 }
99 }
100
101
102 /**
103 * Compute quad's attribute values by linear interpolation with
104 * perspective correction.
105 *
106 * Push into the fp:
107 *
108 * INPUT[attr] = MAD COEF_DADX[attr], INPUT_WPOS.xxxx, COEF_A0[attr]
109 * INPUT[attr] = MAD COEF_DADY[attr], INPUT_WPOS.yyyy, INPUT[attr]
110 * TMP = RCP INPUT_WPOS.w
111 * INPUT[attr] = MUL INPUT[attr], TMP.xxxx
112 *
113 */
114 static INLINE void pinterp( struct exec_machine *exec,
115 GLuint attrib,
116 GLuint i )
117 {
118 GLuint j;
119
120 for (j = 0; j < QUAD_SIZE; j++) {
121 const GLfloat x = exec->attr[FRAG_ATTRIB_WPOS][0][j];
122 const GLfloat y = exec->attr[FRAG_ATTRIB_WPOS][1][j];
123 /* FRAG_ATTRIB_WPOS.w here is really 1/w */
124 const GLfloat w = 1.0 / exec->attr[FRAG_ATTRIB_WPOS][3][j];
125 exec->attr[attrib][i][j] = ((exec->coef[attrib].a0[i] +
126 exec->coef[attrib].dadx[i] * x +
127 exec->coef[attrib].dady[i] * y) * w);
128 }
129 }
130
131
132
133 /* This should be done by the fragment shader execution unit (code
134 * generated from the decl instructions). Do it here for now.
135 */
136 static void
137 shade_quad( struct quad_stage *qs, struct quad_header *quad )
138 {
139 const struct softpipe_context *softpipe = qs->softpipe;
140 struct exec_machine exec;
141 const GLfloat fx = quad->x0;
142 const GLfloat fy = quad->y0;
143 GLuint attr, i;
144
145 exec.coef = quad->coef;
146
147 /* Position:
148 */
149 exec.attr[FRAG_ATTRIB_WPOS][0][0] = fx;
150 exec.attr[FRAG_ATTRIB_WPOS][0][1] = fx + 1.0;
151 exec.attr[FRAG_ATTRIB_WPOS][0][2] = fx;
152 exec.attr[FRAG_ATTRIB_WPOS][0][3] = fx + 1.0;
153
154 exec.attr[FRAG_ATTRIB_WPOS][1][0] = fy;
155 exec.attr[FRAG_ATTRIB_WPOS][1][1] = fy;
156 exec.attr[FRAG_ATTRIB_WPOS][1][2] = fy + 1.0;
157 exec.attr[FRAG_ATTRIB_WPOS][1][3] = fy + 1.0;
158
159 /* Z and W are done by linear interpolation */
160 if (softpipe->need_z) {
161 linterp(&exec, 0, 2); /* attr[0].z */
162 }
163
164 if (softpipe->need_w) {
165 linterp(&exec, 0, 3); /* attr[0].w */
166 /*invert(&exec, 0, 3);*/
167 }
168
169 /* Interpolate all the remaining attributes. This will get pushed
170 * into the fragment program's responsibilities at some point.
171 * Start at 1 to skip fragment position attribute (computed above).
172 */
173 for (attr = 1; attr < quad->nr_attrs; attr++) {
174 switch (softpipe->interp[attr]) {
175 case INTERP_CONSTANT:
176 for (i = 0; i < NUM_CHANNELS; i++)
177 cinterp(&exec, attr, i);
178 break;
179
180 case INTERP_LINEAR:
181 for (i = 0; i < NUM_CHANNELS; i++)
182 linterp(&exec, attr, i);
183 break;
184
185 case INTERP_PERSPECTIVE:
186 for (i = 0; i < NUM_CHANNELS; i++)
187 pinterp(&exec, attr, i);
188 break;
189 }
190 }
191
192 #if 1
193 /*softpipe->run_fs( tri->fp, quad, &tri->outputs );*/
194
195 {
196 struct tgsi_exec_machine machine;
197 struct tgsi_exec_vector outputs[FRAG_ATTRIB_MAX + 1];
198 struct tgsi_exec_vector *aoutputs;
199 GLuint i;
200
201 #if !ALIGNED_ATTRIBS
202 struct tgsi_exec_vector inputs[FRAG_ATTRIB_MAX + 1];
203 struct tgsi_exec_vector *ainputs;
204 #endif
205
206 #ifdef DEBUG
207 memset(&machine, 0, sizeof(machine));
208 #endif
209
210 /* init machine state */
211 tgsi_exec_machine_init(
212 &machine,
213 softpipe->fs.tokens );
214
215 /* Consts does not require 16 byte alignment. */
216 machine.Consts = softpipe->fs.constants->constant;
217
218 aoutputs = (struct tgsi_exec_vector *) tgsi_align_128bit( outputs );
219 machine.Outputs = aoutputs;
220
221 assert( sizeof( struct tgsi_exec_vector ) == sizeof( exec.attr[0] ) );
222
223 #if ALIGNED_ATTRIBS
224 machine.Inputs = (struct tgsi_exec_vector *) exec.attr;
225
226 for (i = 0; i < softpipe->nr_attrs; i++) {
227 /* Make sure fp_attr_to_slot[] is an identity transform. */
228 assert( softpipe->fp_attr_to_slot[i] == i );
229 }
230 #else
231 ainputs = (struct tgsi_exec_vector *) tgsi_align_128bit( inputs );
232 machine.Inputs = ainputs;
233
234 /* load input registers */
235 for (i = 0; i < softpipe->nr_attrs; i++) {
236 #if 0
237 /* Make sure fp_attr_to_slot[] is an identity transform. */
238 assert( softpipe->fp_attr_to_slot[i] == i );
239
240 memcpy(
241 &ainputs[i],
242 exec.attr[i],
243 sizeof( ainputs[0] ) );
244 #else
245 memcpy(
246 &ainputs[i],
247 exec.attr[softpipe->fp_attr_to_slot[i]],
248 sizeof( ainputs[0] ) );
249 #endif
250 }
251 #endif
252
253 /* run shader */
254 tgsi_exec_machine_run( &machine );
255
256 /* store result color */
257 memcpy(quad->outputs.color,
258 &aoutputs[FRAG_ATTRIB_COL0].xyzw[0].f[0],
259 sizeof(quad->outputs.color));
260 if (softpipe->need_z) {
261 /* XXX temporary */
262 quad->outputs.depth[0] = exec.attr[0][2][0];
263 quad->outputs.depth[1] = exec.attr[0][2][1];
264 quad->outputs.depth[2] = exec.attr[0][2][2];
265 quad->outputs.depth[3] = exec.attr[0][2][3];
266 }
267 }
268 #else
269 {
270 GLuint attr = softpipe->fp_attr_to_slot[FRAG_ATTRIB_COL0];
271 assert(attr);
272
273 memcpy(quad->outputs.color,
274 exec.attr[attr],
275 sizeof(quad->outputs.color));
276
277 if (softpipe->need_z) {
278 quad->outputs.depth[0] = exec.attr[0][2][0];
279 quad->outputs.depth[1] = exec.attr[0][2][1];
280 quad->outputs.depth[2] = exec.attr[0][2][2];
281 quad->outputs.depth[3] = exec.attr[0][2][3];
282 }
283 }
284 #endif
285
286 /* shader may cull fragments */
287 if (quad->mask)
288 qs->next->run(qs->next, quad);
289 }
290
291
292
293 struct quad_stage *sp_quad_shade_stage( struct softpipe_context *softpipe )
294 {
295 struct quad_stage *stage = CALLOC_STRUCT(quad_stage);
296
297 stage->softpipe = softpipe;
298 stage->run = shade_quad;
299
300 return stage;
301 }