2 * Mesa 3-D graphics library
5 * Copyright (C) 1999-2005 Brian Paul All Rights Reserved.
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:
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
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.
25 /* Vertices are just an array of floats, with all the attributes
26 * packed. We currently assume a layout like:
28 * attr[0][0..3] - window position
29 * attr[1..n][0..3] - remaining attributes.
31 * Attributes are assumed to be 4 floats wide but are packed so that
32 * all the enabled attributes run contiguously.
37 #include "sp_context.h"
38 #include "sp_headers.h"
40 #include "core/tgsi_core.h"
43 #define ALIGNED_ATTRIBS 1
45 #define ALIGNED_ATTRIBS 0
49 const struct setup_coefficient
*coef
; /**< will point to quad->coef */
52 GLfloat attr
[FRAG_ATTRIB_MAX
][NUM_CHANNELS
][QUAD_SIZE
] __attribute__(( aligned( 16 ) ));
54 GLfloat attr
[FRAG_ATTRIB_MAX
][NUM_CHANNELS
][QUAD_SIZE
];
60 * Compute quad's attributes values, as constants (GL_FLAT shading).
62 static INLINE
void cinterp( struct exec_machine
*exec
,
68 for (j
= 0; j
< QUAD_SIZE
; j
++) {
69 exec
->attr
[attrib
][i
][j
] = exec
->coef
[attrib
].a0
[i
];
75 * Compute quad's attribute values by linear interpolation.
79 * INPUT[attr] = MAD COEF_A0[attr], COEF_DADX[attr], INPUT_WPOS.xxxx
80 * INPUT[attr] = MAD INPUT[attr], COEF_DADY[attr], INPUT_WPOS.yyyy
82 static INLINE
void linterp( struct exec_machine
*exec
,
88 for (j
= 0; j
< QUAD_SIZE
; j
++) {
89 const GLfloat x
= exec
->attr
[FRAG_ATTRIB_WPOS
][0][j
];
90 const GLfloat y
= exec
->attr
[FRAG_ATTRIB_WPOS
][1][j
];
91 exec
->attr
[attrib
][i
][j
] = (exec
->coef
[attrib
].a0
[i
] +
92 exec
->coef
[attrib
].dadx
[i
] * x
+
93 exec
->coef
[attrib
].dady
[i
] * y
);
99 * Compute quad's attribute values by linear interpolation with
100 * perspective correction.
104 * INPUT[attr] = MAD COEF_DADX[attr], INPUT_WPOS.xxxx, COEF_A0[attr]
105 * INPUT[attr] = MAD COEF_DADY[attr], INPUT_WPOS.yyyy, INPUT[attr]
106 * TMP = RCP INPUT_WPOS.w
107 * INPUT[attr] = MUL INPUT[attr], TMP.xxxx
110 static INLINE
void pinterp( struct exec_machine
*exec
,
116 for (j
= 0; j
< QUAD_SIZE
; j
++) {
117 const GLfloat x
= exec
->attr
[FRAG_ATTRIB_WPOS
][0][j
];
118 const GLfloat y
= exec
->attr
[FRAG_ATTRIB_WPOS
][1][j
];
119 /* FRAG_ATTRIB_WPOS.w here is really 1/w */
120 const GLfloat w
= 1.0 / exec
->attr
[FRAG_ATTRIB_WPOS
][3][j
];
121 exec
->attr
[attrib
][i
][j
] = ((exec
->coef
[attrib
].a0
[i
] +
122 exec
->coef
[attrib
].dadx
[i
] * x
+
123 exec
->coef
[attrib
].dady
[i
] * y
) * w
);
129 /* This should be done by the fragment shader execution unit (code
130 * generated from the decl instructions). Do it here for now.
133 shade_quad( struct quad_stage
*qs
, struct quad_header
*quad
)
135 const struct softpipe_context
*softpipe
= qs
->softpipe
;
136 struct exec_machine exec
;
137 const GLfloat fx
= quad
->x0
;
138 const GLfloat fy
= quad
->y0
;
141 exec
.coef
= quad
->coef
;
145 exec
.attr
[FRAG_ATTRIB_WPOS
][0][0] = fx
;
146 exec
.attr
[FRAG_ATTRIB_WPOS
][0][1] = fx
+ 1.0;
147 exec
.attr
[FRAG_ATTRIB_WPOS
][0][2] = fx
;
148 exec
.attr
[FRAG_ATTRIB_WPOS
][0][3] = fx
+ 1.0;
150 exec
.attr
[FRAG_ATTRIB_WPOS
][1][0] = fy
;
151 exec
.attr
[FRAG_ATTRIB_WPOS
][1][1] = fy
;
152 exec
.attr
[FRAG_ATTRIB_WPOS
][1][2] = fy
+ 1.0;
153 exec
.attr
[FRAG_ATTRIB_WPOS
][1][3] = fy
+ 1.0;
155 /* Z and W are done by linear interpolation */
156 if (softpipe
->need_z
) {
157 linterp(&exec
, 0, 2); /* attr[0].z */
160 if (softpipe
->need_w
) {
161 linterp(&exec
, 0, 3); /* attr[0].w */
162 /*invert(&exec, 0, 3);*/
165 /* Interpolate all the remaining attributes. This will get pushed
166 * into the fragment program's responsibilities at some point.
167 * Start at 1 to skip fragment position attribute (computed above).
169 for (attr
= 1; attr
< quad
->nr_attrs
; attr
++) {
170 switch (softpipe
->interp
[attr
]) {
171 case INTERP_CONSTANT
:
172 for (i
= 0; i
< NUM_CHANNELS
; i
++)
173 cinterp(&exec
, attr
, i
);
177 for (i
= 0; i
< NUM_CHANNELS
; i
++)
178 linterp(&exec
, attr
, i
);
181 case INTERP_PERSPECTIVE
:
182 for (i
= 0; i
< NUM_CHANNELS
; i
++)
183 pinterp(&exec
, attr
, i
);
189 /*softpipe->run_fs( tri->fp, quad, &tri->outputs );*/
192 struct tgsi_exec_machine machine
;
193 struct tgsi_exec_vector outputs
[FRAG_ATTRIB_MAX
+ 1];
194 struct tgsi_exec_vector
*aoutputs
;
198 struct tgsi_exec_vector inputs
[FRAG_ATTRIB_MAX
+ 1];
199 struct tgsi_exec_vector
*ainputs
;
203 memset(&machine
, 0, sizeof(machine
));
206 /* init machine state */
207 tgsi_exec_machine_init(
209 softpipe
->fs
.tokens
);
211 /* Consts does not require 16 byte alignment. */
212 machine
.Consts
= softpipe
->fs
.constants
->constant
;
214 aoutputs
= (struct tgsi_exec_vector
*) tgsi_align_128bit( outputs
);
215 machine
.Outputs
= aoutputs
;
217 assert( sizeof( struct tgsi_exec_vector
) == sizeof( exec
.attr
[0] ) );
220 machine
.Inputs
= (struct tgsi_exec_vector
*) exec
.attr
;
222 for (i
= 0; i
< softpipe
->nr_attrs
; i
++) {
223 /* Make sure fp_attr_to_slot[] is an identity transform. */
224 assert( softpipe
->fp_attr_to_slot
[i
] == i
);
227 ainputs
= (struct tgsi_exec_vector
*) tgsi_align_128bit( inputs
);
228 machine
.Inputs
= ainputs
;
230 /* load input registers */
231 for (i
= 0; i
< softpipe
->nr_attrs
; i
++) {
232 /* Make sure fp_attr_to_slot[] is an identity transform. */
233 assert( softpipe
->fp_attr_to_slot
[i
] == i
);
238 sizeof( ainputs
[0] ) );
243 tgsi_exec_machine_run( &machine
);
245 /* store result color */
246 memcpy(quad
->outputs
.color
,
247 &aoutputs
[FRAG_ATTRIB_COL0
].xyzw
[0].f
[0],
248 sizeof(quad
->outputs
.color
));
249 if (softpipe
->need_z
) {
251 quad
->outputs
.depth
[0] = exec
.attr
[0][2][0];
252 quad
->outputs
.depth
[1] = exec
.attr
[0][2][1];
253 quad
->outputs
.depth
[2] = exec
.attr
[0][2][2];
254 quad
->outputs
.depth
[3] = exec
.attr
[0][2][3];
259 GLuint attr
= softpipe
->fp_attr_to_slot
[FRAG_ATTRIB_COL0
];
262 memcpy(quad
->outputs
.color
,
264 sizeof(quad
->outputs
.color
));
266 if (softpipe
->need_z
) {
267 quad
->outputs
.depth
[0] = exec
.attr
[0][2][0];
268 quad
->outputs
.depth
[1] = exec
.attr
[0][2][1];
269 quad
->outputs
.depth
[2] = exec
.attr
[0][2][2];
270 quad
->outputs
.depth
[3] = exec
.attr
[0][2][3];
275 /* shader may cull fragments */
277 qs
->next
->run(qs
->next
, quad
);
282 struct quad_stage
*sp_quad_shade_stage( struct softpipe_context
*softpipe
)
284 struct quad_stage
*stage
= CALLOC_STRUCT(quad_stage
);
286 stage
->softpipe
= softpipe
;
287 stage
->run
= shade_quad
;