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