Merge commit 'origin/gallium-0.1'
[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 GLchan texel[4], GLfloat colorOut[4], GLuint swizzle)
41 {
42 if (swizzle == SWIZZLE_NOOP) {
43 colorOut[0] = CHAN_TO_FLOAT(texel[0]);
44 colorOut[1] = CHAN_TO_FLOAT(texel[1]);
45 colorOut[2] = CHAN_TO_FLOAT(texel[2]);
46 colorOut[3] = CHAN_TO_FLOAT(texel[3]);
47 }
48 else {
49 GLfloat vector[6];
50 vector[SWIZZLE_X] = CHAN_TO_FLOAT(texel[0]);
51 vector[SWIZZLE_Y] = CHAN_TO_FLOAT(texel[1]);
52 vector[SWIZZLE_Z] = CHAN_TO_FLOAT(texel[2]);
53 vector[SWIZZLE_W] = CHAN_TO_FLOAT(texel[3]);
54 vector[SWIZZLE_ZERO] = 0.0F;
55 vector[SWIZZLE_ONE] = 1.0F;
56 colorOut[0] = vector[GET_SWZ(swizzle, 0)];
57 colorOut[1] = vector[GET_SWZ(swizzle, 1)];
58 colorOut[2] = vector[GET_SWZ(swizzle, 2)];
59 colorOut[3] = vector[GET_SWZ(swizzle, 3)];
60 }
61 }
62
63
64 /**
65 * Fetch a texel with given lod.
66 * Called via machine->FetchTexelLod()
67 */
68 static void
69 fetch_texel_lod( GLcontext *ctx, const GLfloat texcoord[4], GLfloat lambda,
70 GLuint unit, GLfloat color[4] )
71 {
72 const struct gl_texture_object *texObj = ctx->Texture.Unit[unit]._Current;
73
74 if (texObj) {
75 SWcontext *swrast = SWRAST_CONTEXT(ctx);
76 GLchan rgba[4];
77
78 lambda = CLAMP(lambda, texObj->MinLod, texObj->MaxLod);
79
80 /* XXX use a float-valued TextureSample routine here!!! */
81 swrast->TextureSample[unit](ctx, texObj, 1,
82 (const GLfloat (*)[4]) texcoord,
83 &lambda, &rgba);
84 swizzle_texel(rgba, color, texObj->_Swizzle);
85 }
86 else {
87 ASSIGN_4V(color, 0.0F, 0.0F, 0.0F, 1.0F);
88 }
89 }
90
91
92 /**
93 * Fetch a texel with the given partial derivatives to compute a level
94 * of detail in the mipmap.
95 * Called via machine->FetchTexelDeriv()
96 */
97 static void
98 fetch_texel_deriv( GLcontext *ctx, const GLfloat texcoord[4],
99 const GLfloat texdx[4], const GLfloat texdy[4],
100 GLfloat lodBias, GLuint unit, GLfloat color[4] )
101 {
102 SWcontext *swrast = SWRAST_CONTEXT(ctx);
103 const struct gl_texture_object *texObj = ctx->Texture.Unit[unit]._Current;
104
105 if (texObj) {
106 const struct gl_texture_image *texImg =
107 texObj->Image[0][texObj->BaseLevel];
108 const GLfloat texW = (GLfloat) texImg->WidthScale;
109 const GLfloat texH = (GLfloat) texImg->HeightScale;
110 GLfloat lambda;
111 GLchan rgba[4];
112
113 lambda = _swrast_compute_lambda(texdx[0], texdy[0], /* ds/dx, ds/dy */
114 texdx[1], texdy[1], /* dt/dx, dt/dy */
115 texdx[3], texdy[2], /* dq/dx, dq/dy */
116 texW, texH,
117 texcoord[0], texcoord[1], texcoord[3],
118 1.0F / texcoord[3]) + lodBias;
119
120 lambda = CLAMP(lambda, texObj->MinLod, texObj->MaxLod);
121
122 /* XXX use a float-valued TextureSample routine here!!! */
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 in register FOGC.Y */
166 machine->Attribs[FRAG_ATTRIB_FOGC][col][1] = 1.0 - span->facing;
167 /* Note FOGC.ZW is gl_PointCoord if drawing a sprite */
168 }
169
170 machine->CurElement = col;
171
172 /* init condition codes */
173 machine->CondCodes[0] = COND_EQ;
174 machine->CondCodes[1] = COND_EQ;
175 machine->CondCodes[2] = COND_EQ;
176 machine->CondCodes[3] = COND_EQ;
177
178 /* init call stack */
179 machine->StackDepth = 0;
180
181 machine->FetchTexelLod = fetch_texel_lod;
182 machine->FetchTexelDeriv = fetch_texel_deriv;
183 }
184
185
186 /**
187 * Run fragment program on the pixels in span from 'start' to 'end' - 1.
188 */
189 static void
190 run_program(GLcontext *ctx, SWspan *span, GLuint start, GLuint end)
191 {
192 SWcontext *swrast = SWRAST_CONTEXT(ctx);
193 const struct gl_fragment_program *program = ctx->FragmentProgram._Current;
194 const GLbitfield outputsWritten = program->Base.OutputsWritten;
195 struct gl_program_machine *machine = &swrast->FragProgMachine;
196 GLuint i;
197
198 for (i = start; i < end; i++) {
199 if (span->array->mask[i]) {
200 init_machine(ctx, machine, program, span, i);
201
202 if (_mesa_execute_program(ctx, &program->Base, machine)) {
203
204 /* Store result color */
205 if (outputsWritten & (1 << FRAG_RESULT_COLOR)) {
206 COPY_4V(span->array->attribs[FRAG_ATTRIB_COL0][i],
207 machine->Outputs[FRAG_RESULT_COLOR]);
208 }
209 else {
210 /* Multiple drawbuffers / render targets
211 * Note that colors beyond 0 and 1 will overwrite other
212 * attributes, such as FOGC, TEX0, TEX1, etc. That's OK.
213 */
214 GLuint buf;
215 for (buf = 0; buf < ctx->DrawBuffer->_NumColorDrawBuffers; buf++) {
216 if (outputsWritten & (1 << (FRAG_RESULT_DATA0 + buf))) {
217 COPY_4V(span->array->attribs[FRAG_ATTRIB_COL0 + buf][i],
218 machine->Outputs[FRAG_RESULT_DATA0 + buf]);
219 }
220 }
221 }
222
223 /* Store result depth/z */
224 if (outputsWritten & (1 << FRAG_RESULT_DEPTH)) {
225 const GLfloat depth = machine->Outputs[FRAG_RESULT_DEPTH][2];
226 if (depth <= 0.0)
227 span->array->z[i] = 0;
228 else if (depth >= 1.0)
229 span->array->z[i] = ctx->DrawBuffer->_DepthMax;
230 else
231 span->array->z[i] = IROUND(depth * ctx->DrawBuffer->_DepthMaxF);
232 }
233 }
234 else {
235 /* killed fragment */
236 span->array->mask[i] = GL_FALSE;
237 span->writeAll = GL_FALSE;
238 }
239 }
240 }
241 }
242
243
244 /**
245 * Execute the current fragment program for all the fragments
246 * in the given span.
247 */
248 void
249 _swrast_exec_fragment_program( GLcontext *ctx, SWspan *span )
250 {
251 const struct gl_fragment_program *program = ctx->FragmentProgram._Current;
252
253 /* incoming colors should be floats */
254 if (program->Base.InputsRead & FRAG_BIT_COL0) {
255 ASSERT(span->array->ChanType == GL_FLOAT);
256 }
257
258 ctx->_CurrentProgram = GL_FRAGMENT_PROGRAM_ARB; /* or NV, doesn't matter */
259
260 run_program(ctx, span, 0, span->end);
261
262 if (program->Base.OutputsWritten & (1 << FRAG_RESULT_COLOR)) {
263 span->interpMask &= ~SPAN_RGBA;
264 span->arrayMask |= SPAN_RGBA;
265 }
266
267 if (program->Base.OutputsWritten & (1 << FRAG_RESULT_DEPTH)) {
268 span->interpMask &= ~SPAN_Z;
269 span->arrayMask |= SPAN_Z;
270 }
271
272 ctx->_CurrentProgram = 0;
273 }
274