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