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