Merge commit 'origin/gallium-0.1' into gallium-0.2
[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 * Fetch a texel with given lod.
37 * Called via machine->FetchTexelLod()
38 */
39 static void
40 fetch_texel_lod( GLcontext *ctx, const GLfloat texcoord[4], GLfloat lambda,
41 GLuint unit, GLfloat color[4] )
42 {
43 GLchan rgba[4];
44 SWcontext *swrast = SWRAST_CONTEXT(ctx);
45 const struct gl_texture_object *texObj = ctx->Texture.Unit[unit]._Current;
46
47 if (texObj)
48 lambda = CLAMP(lambda, texObj->MinLod, texObj->MaxLod);
49
50 /* XXX use a float-valued TextureSample routine here!!! */
51 swrast->TextureSample[unit](ctx, texObj, 1, (const GLfloat (*)[4]) texcoord,
52 &lambda, &rgba);
53 color[0] = CHAN_TO_FLOAT(rgba[0]);
54 color[1] = CHAN_TO_FLOAT(rgba[1]);
55 color[2] = CHAN_TO_FLOAT(rgba[2]);
56 color[3] = CHAN_TO_FLOAT(rgba[3]);
57 }
58
59
60 /**
61 * Fetch a texel with the given partial derivatives to compute a level
62 * of detail in the mipmap.
63 * Called via machine->FetchTexelDeriv()
64 */
65 static void
66 fetch_texel_deriv( GLcontext *ctx, const GLfloat texcoord[4],
67 const GLfloat texdx[4], const GLfloat texdy[4],
68 GLfloat lodBias, GLuint unit, GLfloat color[4] )
69 {
70 SWcontext *swrast = SWRAST_CONTEXT(ctx);
71 const struct gl_texture_object *texObj = ctx->Texture.Unit[unit]._Current;
72 GLfloat lambda;
73 GLchan rgba[4];
74
75 if (texObj) {
76 const struct gl_texture_image *texImg = texObj->Image[0][texObj->BaseLevel];
77 const GLfloat texW = (GLfloat) texImg->WidthScale;
78 const GLfloat texH = (GLfloat) texImg->HeightScale;
79
80 lambda = _swrast_compute_lambda(texdx[0], texdy[0], /* ds/dx, ds/dy */
81 texdx[1], texdy[1], /* dt/dx, dt/dy */
82 texdx[3], texdy[2], /* dq/dx, dq/dy */
83 texW, texH,
84 texcoord[0], texcoord[1], texcoord[3],
85 1.0F / texcoord[3]) + lodBias;
86
87 lambda = CLAMP(lambda, texObj->MinLod, texObj->MaxLod);
88 }
89
90 swrast->TextureSample[unit](ctx, texObj, 1, (const GLfloat (*)[4]) texcoord,
91 &lambda, &rgba);
92 color[0] = CHAN_TO_FLOAT(rgba[0]);
93 color[1] = CHAN_TO_FLOAT(rgba[1]);
94 color[2] = CHAN_TO_FLOAT(rgba[2]);
95 color[3] = CHAN_TO_FLOAT(rgba[3]);
96 }
97
98
99 /**
100 * Initialize the virtual fragment program machine state prior to running
101 * fragment program on a fragment. This involves initializing the input
102 * registers, condition codes, etc.
103 * \param machine the virtual machine state to init
104 * \param program the fragment program we're about to run
105 * \param span the span of pixels we'll operate on
106 * \param col which element (column) of the span we'll operate on
107 */
108 static void
109 init_machine(GLcontext *ctx, struct gl_program_machine *machine,
110 const struct gl_fragment_program *program,
111 const SWspan *span, GLuint col)
112 {
113 if (program->Base.Target == GL_FRAGMENT_PROGRAM_NV) {
114 /* Clear temporary registers (undefined for ARB_f_p) */
115 _mesa_bzero(machine->Temporaries,
116 MAX_PROGRAM_TEMPS * 4 * sizeof(GLfloat));
117 }
118
119 /* Setup pointer to input attributes */
120 machine->Attribs = span->array->attribs;
121
122 machine->DerivX = (GLfloat (*)[4]) span->attrStepX;
123 machine->DerivY = (GLfloat (*)[4]) span->attrStepY;
124 machine->NumDeriv = FRAG_ATTRIB_MAX;
125
126 machine->Samplers = program->Base.SamplerUnits;
127
128 /* if running a GLSL program (not ARB_fragment_program) */
129 if (ctx->Shader.CurrentProgram) {
130 /* Store front/back facing value in register FOGC.Y */
131 machine->Attribs[FRAG_ATTRIB_FOGC][col][1] = 1.0 - span->facing;
132 /* Note FOGC.ZW is gl_PointCoord if drawing a sprite */
133 }
134
135 machine->CurElement = col;
136
137 /* init condition codes */
138 machine->CondCodes[0] = COND_EQ;
139 machine->CondCodes[1] = COND_EQ;
140 machine->CondCodes[2] = COND_EQ;
141 machine->CondCodes[3] = COND_EQ;
142
143 /* init call stack */
144 machine->StackDepth = 0;
145
146 machine->FetchTexelLod = fetch_texel_lod;
147 machine->FetchTexelDeriv = fetch_texel_deriv;
148 }
149
150
151 /**
152 * Run fragment program on the pixels in span from 'start' to 'end' - 1.
153 */
154 static void
155 run_program(GLcontext *ctx, SWspan *span, GLuint start, GLuint end)
156 {
157 SWcontext *swrast = SWRAST_CONTEXT(ctx);
158 const struct gl_fragment_program *program = ctx->FragmentProgram._Current;
159 const GLbitfield outputsWritten = program->Base.OutputsWritten;
160 struct gl_program_machine *machine = &swrast->FragProgMachine;
161 GLuint i;
162
163 for (i = start; i < end; i++) {
164 if (span->array->mask[i]) {
165 init_machine(ctx, machine, program, span, i);
166
167 if (_mesa_execute_program(ctx, &program->Base, machine)) {
168
169 /* Store result color */
170 if (outputsWritten & (1 << FRAG_RESULT_COLR)) {
171 COPY_4V(span->array->attribs[FRAG_ATTRIB_COL0][i],
172 machine->Outputs[FRAG_RESULT_COLR]);
173 }
174 else {
175 /* Multiple drawbuffers / render targets
176 * Note that colors beyond 0 and 1 will overwrite other
177 * attributes, such as FOGC, TEX0, TEX1, etc. That's OK.
178 */
179 GLuint buf;
180 for (buf = 0; buf < ctx->DrawBuffer->_NumColorDrawBuffers; buf++) {
181 if (outputsWritten & (1 << (FRAG_RESULT_DATA0 + buf))) {
182 COPY_4V(span->array->attribs[FRAG_ATTRIB_COL0 + buf][i],
183 machine->Outputs[FRAG_RESULT_DATA0 + buf]);
184 }
185 }
186 }
187
188 /* Store result depth/z */
189 if (outputsWritten & (1 << FRAG_RESULT_DEPR)) {
190 const GLfloat depth = machine->Outputs[FRAG_RESULT_DEPR][2];
191 if (depth <= 0.0)
192 span->array->z[i] = 0;
193 else if (depth >= 1.0)
194 span->array->z[i] = ctx->DrawBuffer->_DepthMax;
195 else
196 span->array->z[i] = IROUND(depth * ctx->DrawBuffer->_DepthMaxF);
197 }
198 }
199 else {
200 /* killed fragment */
201 span->array->mask[i] = GL_FALSE;
202 span->writeAll = GL_FALSE;
203 }
204 }
205 }
206 }
207
208
209 /**
210 * Execute the current fragment program for all the fragments
211 * in the given span.
212 */
213 void
214 _swrast_exec_fragment_program( GLcontext *ctx, SWspan *span )
215 {
216 const struct gl_fragment_program *program = ctx->FragmentProgram._Current;
217
218 /* incoming colors should be floats */
219 if (program->Base.InputsRead & FRAG_BIT_COL0) {
220 ASSERT(span->array->ChanType == GL_FLOAT);
221 }
222
223 ctx->_CurrentProgram = GL_FRAGMENT_PROGRAM_ARB; /* or NV, doesn't matter */
224
225 run_program(ctx, span, 0, span->end);
226
227 if (program->Base.OutputsWritten & (1 << FRAG_RESULT_COLR)) {
228 span->interpMask &= ~SPAN_RGBA;
229 span->arrayMask |= SPAN_RGBA;
230 }
231
232 if (program->Base.OutputsWritten & (1 << FRAG_RESULT_DEPR)) {
233 span->interpMask &= ~SPAN_Z;
234 span->arrayMask |= SPAN_Z;
235 }
236
237 ctx->_CurrentProgram = 0;
238 }
239