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