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