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