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