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