Merge branch 'mesa_7_6_branch' into mesa_7_7_branch
[mesa.git] / src / mesa / swrast / s_fragprog.c
1 /*
2 * Mesa 3-D graphics library
3 * Version: 7.0.3
4 *
5 * Copyright (C) 1999-2007 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 #include "main/glheader.h"
26 #include "main/colormac.h"
27 #include "main/context.h"
28 #include "main/texstate.h"
29 #include "shader/prog_instruction.h"
30
31 #include "s_fragprog.h"
32 #include "s_span.h"
33
34
35 /**
36 * Apply texture object's swizzle (X/Y/Z/W/0/1) to incoming 'texel'
37 * and return results in 'colorOut'.
38 */
39 static INLINE void
40 swizzle_texel(const GLfloat texel[4], GLfloat colorOut[4], GLuint swizzle)
41 {
42 if (swizzle == SWIZZLE_NOOP) {
43 COPY_4V(colorOut, texel);
44 }
45 else {
46 GLfloat vector[6];
47 vector[SWIZZLE_X] = texel[0];
48 vector[SWIZZLE_Y] = texel[1];
49 vector[SWIZZLE_Z] = texel[2];
50 vector[SWIZZLE_W] = texel[3];
51 vector[SWIZZLE_ZERO] = 0.0F;
52 vector[SWIZZLE_ONE] = 1.0F;
53 colorOut[0] = vector[GET_SWZ(swizzle, 0)];
54 colorOut[1] = vector[GET_SWZ(swizzle, 1)];
55 colorOut[2] = vector[GET_SWZ(swizzle, 2)];
56 colorOut[3] = vector[GET_SWZ(swizzle, 3)];
57 }
58 }
59
60
61 /**
62 * Fetch a texel with given lod.
63 * Called via machine->FetchTexelLod()
64 */
65 static void
66 fetch_texel_lod( GLcontext *ctx, const GLfloat texcoord[4], GLfloat lambda,
67 GLuint unit, GLfloat color[4] )
68 {
69 const struct gl_texture_object *texObj = ctx->Texture.Unit[unit]._Current;
70
71 if (texObj) {
72 SWcontext *swrast = SWRAST_CONTEXT(ctx);
73 GLfloat rgba[4];
74
75 lambda = CLAMP(lambda, texObj->MinLod, texObj->MaxLod);
76
77 swrast->TextureSample[unit](ctx, texObj, 1,
78 (const GLfloat (*)[4]) texcoord,
79 &lambda, &rgba);
80 swizzle_texel(rgba, color, texObj->_Swizzle);
81 }
82 else {
83 ASSIGN_4V(color, 0.0F, 0.0F, 0.0F, 1.0F);
84 }
85 }
86
87
88 /**
89 * Fetch a texel with the given partial derivatives to compute a level
90 * of detail in the mipmap.
91 * Called via machine->FetchTexelDeriv()
92 * \param lodBias the lod bias which may be specified by a TXB instruction,
93 * otherwise zero.
94 */
95 static void
96 fetch_texel_deriv( GLcontext *ctx, const GLfloat texcoord[4],
97 const GLfloat texdx[4], const GLfloat texdy[4],
98 GLfloat lodBias, GLuint unit, GLfloat color[4] )
99 {
100 SWcontext *swrast = SWRAST_CONTEXT(ctx);
101 const struct gl_texture_unit *texUnit = &ctx->Texture.Unit[unit];
102 const struct gl_texture_object *texObj = texUnit->_Current;
103
104 if (texObj) {
105 const struct gl_texture_image *texImg =
106 texObj->Image[0][texObj->BaseLevel];
107 const GLfloat texW = (GLfloat) texImg->WidthScale;
108 const GLfloat texH = (GLfloat) texImg->HeightScale;
109 GLfloat lambda;
110 GLfloat rgba[4];
111
112 lambda = _swrast_compute_lambda(texdx[0], texdy[0], /* ds/dx, ds/dy */
113 texdx[1], texdy[1], /* dt/dx, dt/dy */
114 texdx[3], texdy[3], /* dq/dx, dq/dy */
115 texW, texH,
116 texcoord[0], texcoord[1], texcoord[3],
117 1.0F / texcoord[3]);
118
119 lambda += lodBias + texUnit->LodBias + texObj->LodBias;
120
121 lambda = CLAMP(lambda, texObj->MinLod, texObj->MaxLod);
122
123 swrast->TextureSample[unit](ctx, texObj, 1,
124 (const GLfloat (*)[4]) texcoord,
125 &lambda, &rgba);
126 swizzle_texel(rgba, color, texObj->_Swizzle);
127 }
128 else {
129 ASSIGN_4V(color, 0.0F, 0.0F, 0.0F, 1.0F);
130 }
131 }
132
133
134 /**
135 * Initialize the virtual fragment program machine state prior to running
136 * fragment program on a fragment. This involves initializing the input
137 * registers, condition codes, etc.
138 * \param machine the virtual machine state to init
139 * \param program the fragment program we're about to run
140 * \param span the span of pixels we'll operate on
141 * \param col which element (column) of the span we'll operate on
142 */
143 static void
144 init_machine(GLcontext *ctx, struct gl_program_machine *machine,
145 const struct gl_fragment_program *program,
146 const SWspan *span, GLuint col)
147 {
148 if (program->Base.Target == GL_FRAGMENT_PROGRAM_NV) {
149 /* Clear temporary registers (undefined for ARB_f_p) */
150 _mesa_bzero(machine->Temporaries,
151 MAX_PROGRAM_TEMPS * 4 * sizeof(GLfloat));
152 }
153
154 /* Setup pointer to input attributes */
155 machine->Attribs = span->array->attribs;
156
157 machine->DerivX = (GLfloat (*)[4]) span->attrStepX;
158 machine->DerivY = (GLfloat (*)[4]) span->attrStepY;
159 machine->NumDeriv = FRAG_ATTRIB_MAX;
160
161 machine->Samplers = program->Base.SamplerUnits;
162
163 /* if running a GLSL program (not ARB_fragment_program) */
164 if (ctx->Shader.CurrentProgram) {
165 /* Store front/back facing value */
166 machine->Attribs[FRAG_ATTRIB_FACE][col][0] = 1.0 - span->facing;
167 }
168
169 machine->CurElement = col;
170
171 /* init condition codes */
172 machine->CondCodes[0] = COND_EQ;
173 machine->CondCodes[1] = COND_EQ;
174 machine->CondCodes[2] = COND_EQ;
175 machine->CondCodes[3] = COND_EQ;
176
177 /* init call stack */
178 machine->StackDepth = 0;
179
180 machine->FetchTexelLod = fetch_texel_lod;
181 machine->FetchTexelDeriv = fetch_texel_deriv;
182 }
183
184
185 /**
186 * Run fragment program on the pixels in span from 'start' to 'end' - 1.
187 */
188 static void
189 run_program(GLcontext *ctx, SWspan *span, GLuint start, GLuint end)
190 {
191 SWcontext *swrast = SWRAST_CONTEXT(ctx);
192 const struct gl_fragment_program *program = ctx->FragmentProgram._Current;
193 const GLbitfield64 outputsWritten = program->Base.OutputsWritten;
194 struct gl_program_machine *machine = &swrast->FragProgMachine;
195 GLuint i;
196
197 for (i = start; i < end; i++) {
198 if (span->array->mask[i]) {
199 init_machine(ctx, machine, program, span, i);
200
201 if (_mesa_execute_program(ctx, &program->Base, machine)) {
202
203 /* Store result color */
204 if (outputsWritten & BITFIELD64_BIT(FRAG_RESULT_COLOR)) {
205 COPY_4V(span->array->attribs[FRAG_ATTRIB_COL0][i],
206 machine->Outputs[FRAG_RESULT_COLOR]);
207 }
208 else {
209 /* Multiple drawbuffers / render targets
210 * Note that colors beyond 0 and 1 will overwrite other
211 * attributes, such as FOGC, TEX0, TEX1, etc. That's OK.
212 */
213 GLuint buf;
214 for (buf = 0; buf < ctx->DrawBuffer->_NumColorDrawBuffers; buf++) {
215 if (outputsWritten & BITFIELD64_BIT(FRAG_RESULT_DATA0 + buf)) {
216 COPY_4V(span->array->attribs[FRAG_ATTRIB_COL0 + buf][i],
217 machine->Outputs[FRAG_RESULT_DATA0 + buf]);
218 }
219 }
220 }
221
222 /* Store result depth/z */
223 if (outputsWritten & BITFIELD64_BIT(FRAG_RESULT_DEPTH)) {
224 const GLfloat depth = machine->Outputs[FRAG_RESULT_DEPTH][2];
225 if (depth <= 0.0)
226 span->array->z[i] = 0;
227 else if (depth >= 1.0)
228 span->array->z[i] = ctx->DrawBuffer->_DepthMax;
229 else
230 span->array->z[i] = IROUND(depth * ctx->DrawBuffer->_DepthMaxF);
231 }
232 }
233 else {
234 /* killed fragment */
235 span->array->mask[i] = GL_FALSE;
236 span->writeAll = GL_FALSE;
237 }
238 }
239 }
240 }
241
242
243 /**
244 * Execute the current fragment program for all the fragments
245 * in the given span.
246 */
247 void
248 _swrast_exec_fragment_program( GLcontext *ctx, SWspan *span )
249 {
250 const struct gl_fragment_program *program = ctx->FragmentProgram._Current;
251
252 /* incoming colors should be floats */
253 if (program->Base.InputsRead & FRAG_BIT_COL0) {
254 ASSERT(span->array->ChanType == GL_FLOAT);
255 }
256
257 run_program(ctx, span, 0, span->end);
258
259 if (program->Base.OutputsWritten & BITFIELD64_BIT(FRAG_RESULT_COLOR)) {
260 span->interpMask &= ~SPAN_RGBA;
261 span->arrayMask |= SPAN_RGBA;
262 }
263
264 if (program->Base.OutputsWritten & BITFIELD64_BIT(FRAG_RESULT_DEPTH)) {
265 span->interpMask &= ~SPAN_Z;
266 span->arrayMask |= SPAN_Z;
267 }
268 }
269