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 "sp_tex_sample.h"
41 #include "tgsi/core/tgsi_core.h"
45 #define ALIGNED_ATTRIBS 1
47 #define ALIGNED_ATTRIBS 0
50 #define ALIGNED_ATTRIBS 0
54 struct quad_shade_stage
56 struct quad_stage stage
;
57 struct tgsi_sampler samplers
[PIPE_MAX_SAMPLERS
];
62 static INLINE
struct quad_shade_stage
*
63 quad_shade_stage(struct quad_stage
*qs
)
65 return (struct quad_shade_stage
*) qs
;
71 const struct setup_coefficient
*coef
; /**< will point to quad->coef */
74 GLfloat attr
[FRAG_ATTRIB_MAX
][NUM_CHANNELS
][QUAD_SIZE
] __attribute__(( aligned( 16 ) ));
76 GLfloat attr
[FRAG_ATTRIB_MAX
][NUM_CHANNELS
][QUAD_SIZE
];
82 * Compute quad's attributes values, as constants (GL_FLAT shading).
84 static INLINE
void cinterp( struct exec_machine
*exec
,
90 for (j
= 0; j
< QUAD_SIZE
; j
++) {
91 exec
->attr
[attrib
][i
][j
] = exec
->coef
[attrib
].a0
[i
];
97 * Compute quad's attribute values by linear interpolation.
101 * INPUT[attr] = MAD COEF_A0[attr], COEF_DADX[attr], INPUT_WPOS.xxxx
102 * INPUT[attr] = MAD INPUT[attr], COEF_DADY[attr], INPUT_WPOS.yyyy
104 static INLINE
void linterp( struct exec_machine
*exec
,
110 for (j
= 0; j
< QUAD_SIZE
; j
++) {
111 const GLfloat x
= exec
->attr
[FRAG_ATTRIB_WPOS
][0][j
];
112 const GLfloat y
= exec
->attr
[FRAG_ATTRIB_WPOS
][1][j
];
113 exec
->attr
[attrib
][i
][j
] = (exec
->coef
[attrib
].a0
[i
] +
114 exec
->coef
[attrib
].dadx
[i
] * x
+
115 exec
->coef
[attrib
].dady
[i
] * y
);
121 * Compute quad's attribute values by linear interpolation with
122 * perspective correction.
126 * INPUT[attr] = MAD COEF_DADX[attr], INPUT_WPOS.xxxx, COEF_A0[attr]
127 * INPUT[attr] = MAD COEF_DADY[attr], INPUT_WPOS.yyyy, INPUT[attr]
128 * TMP = RCP INPUT_WPOS.w
129 * INPUT[attr] = MUL INPUT[attr], TMP.xxxx
132 static INLINE
void pinterp( struct exec_machine
*exec
,
138 for (j
= 0; j
< QUAD_SIZE
; j
++) {
139 const GLfloat x
= exec
->attr
[FRAG_ATTRIB_WPOS
][0][j
];
140 const GLfloat y
= exec
->attr
[FRAG_ATTRIB_WPOS
][1][j
];
141 /* FRAG_ATTRIB_WPOS.w here is really 1/w */
142 const GLfloat w
= 1.0 / exec
->attr
[FRAG_ATTRIB_WPOS
][3][j
];
143 exec
->attr
[attrib
][i
][j
] = ((exec
->coef
[attrib
].a0
[i
] +
144 exec
->coef
[attrib
].dadx
[i
] * x
+
145 exec
->coef
[attrib
].dady
[i
] * y
) * w
);
150 /* This should be done by the fragment shader execution unit (code
151 * generated from the decl instructions). Do it here for now.
154 shade_quad( struct quad_stage
*qs
, struct quad_header
*quad
)
156 struct quad_shade_stage
*qss
= quad_shade_stage(qs
);
157 struct softpipe_context
*softpipe
= qs
->softpipe
;
158 struct exec_machine exec
;
159 const GLfloat fx
= quad
->x0
;
160 const GLfloat fy
= quad
->y0
;
163 exec
.coef
= quad
->coef
;
167 exec
.attr
[FRAG_ATTRIB_WPOS
][0][0] = fx
;
168 exec
.attr
[FRAG_ATTRIB_WPOS
][0][1] = fx
+ 1.0;
169 exec
.attr
[FRAG_ATTRIB_WPOS
][0][2] = fx
;
170 exec
.attr
[FRAG_ATTRIB_WPOS
][0][3] = fx
+ 1.0;
172 exec
.attr
[FRAG_ATTRIB_WPOS
][1][0] = fy
;
173 exec
.attr
[FRAG_ATTRIB_WPOS
][1][1] = fy
;
174 exec
.attr
[FRAG_ATTRIB_WPOS
][1][2] = fy
+ 1.0;
175 exec
.attr
[FRAG_ATTRIB_WPOS
][1][3] = fy
+ 1.0;
177 /* Z and W are done by linear interpolation */
178 if (softpipe
->need_z
) {
179 linterp(&exec
, 0, 2); /* attr[0].z */
182 if (softpipe
->need_w
) {
183 linterp(&exec
, 0, 3); /* attr[0].w */
184 /*invert(&exec, 0, 3);*/
187 /* Interpolate all the remaining attributes. This will get pushed
188 * into the fragment program's responsibilities at some point.
189 * Start at 1 to skip fragment position attribute (computed above).
191 for (attr
= 1; attr
< quad
->nr_attrs
; attr
++) {
192 switch (softpipe
->interp
[attr
]) {
193 case INTERP_CONSTANT
:
194 for (i
= 0; i
< NUM_CHANNELS
; i
++)
195 cinterp(&exec
, attr
, i
);
199 for (i
= 0; i
< NUM_CHANNELS
; i
++)
200 linterp(&exec
, attr
, i
);
203 case INTERP_PERSPECTIVE
:
204 for (i
= 0; i
< NUM_CHANNELS
; i
++)
205 pinterp(&exec
, attr
, i
);
211 /*softpipe->run_fs( tri->fp, quad, &tri->outputs );*/
214 struct tgsi_exec_machine machine
;
215 struct tgsi_exec_vector outputs
[FRAG_ATTRIB_MAX
+ 1];
216 struct tgsi_exec_vector
*aoutputs
;
220 struct tgsi_exec_vector inputs
[FRAG_ATTRIB_MAX
+ 1];
221 struct tgsi_exec_vector
*ainputs
;
225 memset(&machine
, 0, sizeof(machine
));
228 /* init machine state */
229 tgsi_exec_machine_init(
232 PIPE_MAX_SAMPLERS
, qss
->samplers
);
234 /* Consts does not require 16 byte alignment. */
235 machine
.Consts
= softpipe
->fs
.constants
->constant
;
237 aoutputs
= (struct tgsi_exec_vector
*) tgsi_align_128bit( outputs
);
238 machine
.Outputs
= aoutputs
;
240 assert( sizeof( struct tgsi_exec_vector
) == sizeof( exec
.attr
[0] ) );
243 machine
.Inputs
= (struct tgsi_exec_vector
*) exec
.attr
;
245 for (i
= 0; i
< softpipe
->nr_attrs
; i
++) {
246 /* Make sure fp_attr_to_slot[] is an identity transform. */
247 assert( softpipe
->fp_attr_to_slot
[i
] == i
);
250 ainputs
= (struct tgsi_exec_vector
*) tgsi_align_128bit( inputs
);
251 machine
.Inputs
= ainputs
;
253 /* load input registers */
254 for (i
= 0; i
< softpipe
->nr_attrs
; i
++) {
256 /* Make sure fp_attr_to_slot[] is an identity transform. */
258 assert( softpipe->fp_attr_to_slot[i] == i );
263 sizeof( ainputs
[0] ) );
267 exec
.attr
[softpipe
->fp_attr_to_slot
[i
]],
268 sizeof( ainputs
[0] ) );
274 tgsi_exec_machine_run( &machine
);
276 /* store result color */
277 memcpy(quad
->outputs
.color
,
278 &aoutputs
[FRAG_ATTRIB_COL0
].xyzw
[0].f
[0],
279 sizeof(quad
->outputs
.color
));
280 if (softpipe
->need_z
) {
282 quad
->outputs
.depth
[0] = exec
.attr
[0][2][0];
283 quad
->outputs
.depth
[1] = exec
.attr
[0][2][1];
284 quad
->outputs
.depth
[2] = exec
.attr
[0][2][2];
285 quad
->outputs
.depth
[3] = exec
.attr
[0][2][3];
290 GLuint attr
= softpipe
->fp_attr_to_slot
[FRAG_ATTRIB_COL0
];
293 memcpy(quad
->outputs
.color
,
295 sizeof(quad
->outputs
.color
));
297 if (softpipe
->need_z
) {
298 quad
->outputs
.depth
[0] = exec
.attr
[0][2][0];
299 quad
->outputs
.depth
[1] = exec
.attr
[0][2][1];
300 quad
->outputs
.depth
[2] = exec
.attr
[0][2][2];
301 quad
->outputs
.depth
[3] = exec
.attr
[0][2][3];
306 /* shader may cull fragments */
308 qs
->next
->run(qs
->next
, quad
);
313 * Per-primitive (or per-begin?) setup
315 static void shade_begin(struct quad_stage
*qs
)
317 struct quad_shade_stage
*qss
= quad_shade_stage(qs
);
318 struct softpipe_context
*softpipe
= qs
->softpipe
;
320 for (i
= 0; i
< PIPE_MAX_SAMPLERS
; i
++) {
321 qss
->samplers
[i
].state
= &softpipe
->sampler
[i
];
322 qss
->samplers
[i
].texture
= softpipe
->texture
[i
];
323 qss
->samplers
[i
].get_sample
= sp_get_sample
;
324 qss
->samplers
[i
].pipe
= &softpipe
->pipe
;
325 /* init cache info here */
326 qss
->samplers
[i
].cache_x
=
327 qss
->samplers
[i
].cache_y
= -1;
328 qss
->samplers
[i
].cache_level
= -1;
332 qs
->next
->begin(qs
->next
);
336 struct quad_stage
*sp_quad_shade_stage( struct softpipe_context
*softpipe
)
338 struct quad_shade_stage
*stage
= CALLOC_STRUCT(quad_shade_stage
);
340 stage
->stage
.softpipe
= softpipe
;
341 stage
->stage
.begin
= shade_begin
;
342 stage
->stage
.run
= shade_quad
;
344 return &stage
->stage
;