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