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