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 "tgsi/core/tgsi_core.h"
44 #define ALIGNED_ATTRIBS 1
46 #define ALIGNED_ATTRIBS 0
49 #define ALIGNED_ATTRIBS 0
53 const struct setup_coefficient
*coef
; /**< will point to quad->coef */
56 GLfloat attr
[FRAG_ATTRIB_MAX
][NUM_CHANNELS
][QUAD_SIZE
] __attribute__(( aligned( 16 ) ));
58 GLfloat attr
[FRAG_ATTRIB_MAX
][NUM_CHANNELS
][QUAD_SIZE
];
64 * Compute quad's attributes values, as constants (GL_FLAT shading).
66 static INLINE
void cinterp( struct exec_machine
*exec
,
72 for (j
= 0; j
< QUAD_SIZE
; j
++) {
73 exec
->attr
[attrib
][i
][j
] = exec
->coef
[attrib
].a0
[i
];
79 * Compute quad's attribute values by linear interpolation.
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
86 static INLINE
void linterp( struct exec_machine
*exec
,
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
);
103 * Compute quad's attribute values by linear interpolation with
104 * perspective correction.
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
114 static INLINE
void pinterp( struct exec_machine
*exec
,
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
);
133 /* This should be done by the fragment shader execution unit (code
134 * generated from the decl instructions). Do it here for now.
137 shade_quad( struct quad_stage
*qs
, struct quad_header
*quad
)
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
;
145 exec
.coef
= quad
->coef
;
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;
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;
159 /* Z and W are done by linear interpolation */
160 if (softpipe
->need_z
) {
161 linterp(&exec
, 0, 2); /* attr[0].z */
164 if (softpipe
->need_w
) {
165 linterp(&exec
, 0, 3); /* attr[0].w */
166 /*invert(&exec, 0, 3);*/
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).
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
);
181 for (i
= 0; i
< NUM_CHANNELS
; i
++)
182 linterp(&exec
, attr
, i
);
185 case INTERP_PERSPECTIVE
:
186 for (i
= 0; i
< NUM_CHANNELS
; i
++)
187 pinterp(&exec
, attr
, i
);
193 /*softpipe->run_fs( tri->fp, quad, &tri->outputs );*/
196 struct tgsi_exec_machine machine
;
197 struct tgsi_exec_vector outputs
[FRAG_ATTRIB_MAX
+ 1];
198 struct tgsi_exec_vector
*aoutputs
;
202 struct tgsi_exec_vector inputs
[FRAG_ATTRIB_MAX
+ 1];
203 struct tgsi_exec_vector
*ainputs
;
207 memset(&machine
, 0, sizeof(machine
));
210 /* init machine state */
211 tgsi_exec_machine_init(
213 softpipe
->fs
.tokens
);
215 /* Consts does not require 16 byte alignment. */
216 machine
.Consts
= softpipe
->fs
.constants
->constant
;
218 aoutputs
= (struct tgsi_exec_vector
*) tgsi_align_128bit( outputs
);
219 machine
.Outputs
= aoutputs
;
221 assert( sizeof( struct tgsi_exec_vector
) == sizeof( exec
.attr
[0] ) );
224 machine
.Inputs
= (struct tgsi_exec_vector
*) exec
.attr
;
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
);
231 ainputs
= (struct tgsi_exec_vector
*) tgsi_align_128bit( inputs
);
232 machine
.Inputs
= ainputs
;
234 /* load input registers */
235 for (i
= 0; i
< softpipe
->nr_attrs
; i
++) {
237 /* Make sure fp_attr_to_slot[] is an identity transform. */
238 assert( softpipe
->fp_attr_to_slot
[i
] == i
);
243 sizeof( ainputs
[0] ) );
247 exec
.attr
[softpipe
->fp_attr_to_slot
[i
]],
248 sizeof( ainputs
[0] ) );
254 tgsi_exec_machine_run( &machine
);
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
) {
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];
270 GLuint attr
= softpipe
->fp_attr_to_slot
[FRAG_ATTRIB_COL0
];
273 memcpy(quad
->outputs
.color
,
275 sizeof(quad
->outputs
.color
));
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];
286 /* shader may cull fragments */
288 qs
->next
->run(qs
->next
, quad
);
293 struct quad_stage
*sp_quad_shade_stage( struct softpipe_context
*softpipe
)
295 struct quad_stage
*stage
= CALLOC_STRUCT(quad_stage
);
297 stage
->softpipe
= softpipe
;
298 stage
->run
= shade_quad
;