📄 s_span.c
字号:
/* * Mesa 3-D graphics library * Version: 6.5 * * Copyright (C) 1999-2006 Brian Paul All Rights Reserved. * * Permission is hereby granted, free of charge, to any person obtaining a * copy of this software and associated documentation files (the "Software"), * to deal in the Software without restriction, including without limitation * the rights to use, copy, modify, merge, publish, distribute, sublicense, * and/or sell copies of the Software, and to permit persons to whom the * Software is furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included * in all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. *//** * \file swrast/s_span.c * \brief Span processing functions used by all rasterization functions. * This is where all the per-fragment tests are performed * \author Brian Paul */#include "glheader.h"#include "colormac.h"#include "context.h"#include "macros.h"#include "imports.h"#include "s_atifragshader.h"#include "s_alpha.h"
#include "s_arbshader.h"#include "s_blend.h"#include "s_context.h"#include "s_depth.h"#include "s_fog.h"#include "s_logic.h"#include "s_masking.h"#include "s_nvfragprog.h"#include "s_span.h"#include "s_stencil.h"#include "s_texcombine.h"/** * Init span's Z interpolation values to the RasterPos Z. * Used during setup for glDraw/CopyPixels. */void_swrast_span_default_z( GLcontext *ctx, struct sw_span *span ){ const GLfloat depthMax = ctx->DrawBuffer->_DepthMaxF; if (ctx->DrawBuffer->Visual.depthBits <= 16) span->z = FloatToFixed(ctx->Current.RasterPos[2] * depthMax + 0.5F); else span->z = (GLint) (ctx->Current.RasterPos[2] * depthMax + 0.5F); span->zStep = 0; span->interpMask |= SPAN_Z;}/** * Init span's fog interpolation values to the RasterPos fog. * Used during setup for glDraw/CopyPixels. */void_swrast_span_default_fog( GLcontext *ctx, struct sw_span *span ){ span->fog = _swrast_z_to_fogfactor(ctx, ctx->Current.RasterDistance); span->fogStep = span->dfogdx = span->dfogdy = 0.0F; span->interpMask |= SPAN_FOG;}/** * Init span's rgba or index interpolation values to the RasterPos color. * Used during setup for glDraw/CopyPixels. */void_swrast_span_default_color( GLcontext *ctx, struct sw_span *span ){ if (ctx->Visual.rgbMode) { GLchan r, g, b, a; UNCLAMPED_FLOAT_TO_CHAN(r, ctx->Current.RasterColor[0]); UNCLAMPED_FLOAT_TO_CHAN(g, ctx->Current.RasterColor[1]); UNCLAMPED_FLOAT_TO_CHAN(b, ctx->Current.RasterColor[2]); UNCLAMPED_FLOAT_TO_CHAN(a, ctx->Current.RasterColor[3]);#if CHAN_TYPE == GL_FLOAT span->red = r; span->green = g; span->blue = b; span->alpha = a;#else span->red = IntToFixed(r); span->green = IntToFixed(g); span->blue = IntToFixed(b); span->alpha = IntToFixed(a);#endif span->redStep = 0; span->greenStep = 0; span->blueStep = 0; span->alphaStep = 0; span->interpMask |= SPAN_RGBA; } else { span->index = FloatToFixed(ctx->Current.RasterIndex); span->indexStep = 0; span->interpMask |= SPAN_INDEX; }}/** * Init span's texcoord interpolation values to the RasterPos texcoords. * Used during setup for glDraw/CopyPixels. */void_swrast_span_default_texcoords( GLcontext *ctx, struct sw_span *span ){ GLuint i; for (i = 0; i < ctx->Const.MaxTextureUnits; i++) { const GLfloat *tc = ctx->Current.RasterTexCoords[i]; if (ctx->FragmentProgram._Active || ctx->ATIFragmentShader._Enabled) { COPY_4V(span->tex[i], tc); } else if (tc[3] > 0.0F) { /* use (s/q, t/q, r/q, 1) */ span->tex[i][0] = tc[0] / tc[3]; span->tex[i][1] = tc[1] / tc[3]; span->tex[i][2] = tc[2] / tc[3]; span->tex[i][3] = 1.0; } else { ASSIGN_4V(span->tex[i], 0.0F, 0.0F, 0.0F, 1.0F); } ASSIGN_4V(span->texStepX[i], 0.0F, 0.0F, 0.0F, 0.0F); ASSIGN_4V(span->texStepY[i], 0.0F, 0.0F, 0.0F, 0.0F); } span->interpMask |= SPAN_TEXTURE;}/* Fill in the span.color.rgba array from the interpolation values */static voidinterpolate_colors(GLcontext *ctx, struct sw_span *span){ const GLuint n = span->end; GLchan (*rgba)[4] = span->array->rgba; GLuint i; (void) ctx; ASSERT((span->interpMask & SPAN_RGBA) && !(span->arrayMask & SPAN_RGBA)); if (span->interpMask & SPAN_FLAT) { /* constant color */ GLchan color[4]; color[RCOMP] = FixedToChan(span->red); color[GCOMP] = FixedToChan(span->green); color[BCOMP] = FixedToChan(span->blue); color[ACOMP] = FixedToChan(span->alpha); for (i = 0; i < n; i++) { COPY_CHAN4(span->array->rgba[i], color); } } else { /* interpolate */#if CHAN_TYPE == GL_FLOAT GLfloat r = span->red; GLfloat g = span->green; GLfloat b = span->blue; GLfloat a = span->alpha; const GLfloat dr = span->redStep; const GLfloat dg = span->greenStep; const GLfloat db = span->blueStep; const GLfloat da = span->alphaStep;#else GLfixed r = span->red; GLfixed g = span->green; GLfixed b = span->blue; GLfixed a = span->alpha; const GLint dr = span->redStep; const GLint dg = span->greenStep; const GLint db = span->blueStep; const GLint da = span->alphaStep;#endif for (i = 0; i < n; i++) { rgba[i][RCOMP] = FixedToChan(r); rgba[i][GCOMP] = FixedToChan(g); rgba[i][BCOMP] = FixedToChan(b); rgba[i][ACOMP] = FixedToChan(a); r += dr; g += dg; b += db; a += da; } } span->arrayMask |= SPAN_RGBA;}/* Fill in the span.color.index array from the interpolation values */static voidinterpolate_indexes(GLcontext *ctx, struct sw_span *span){ GLfixed index = span->index; const GLint indexStep = span->indexStep; const GLuint n = span->end; GLuint *indexes = span->array->index; GLuint i; (void) ctx; ASSERT((span->interpMask & SPAN_INDEX) && !(span->arrayMask & SPAN_INDEX)); if ((span->interpMask & SPAN_FLAT) || (indexStep == 0)) { /* constant color */ index = FixedToInt(index); for (i = 0; i < n; i++) { indexes[i] = index; } } else { /* interpolate */ for (i = 0; i < n; i++) { indexes[i] = FixedToInt(index); index += indexStep; } } span->arrayMask |= SPAN_INDEX; span->interpMask &= ~SPAN_INDEX;}/* Fill in the span.->array->spec array from the interpolation values */static voidinterpolate_specular(GLcontext *ctx, struct sw_span *span){ (void) ctx; if (span->interpMask & SPAN_FLAT) { /* constant color */ const GLchan r = FixedToChan(span->specRed); const GLchan g = FixedToChan(span->specGreen); const GLchan b = FixedToChan(span->specBlue); GLuint i; for (i = 0; i < span->end; i++) { span->array->spec[i][RCOMP] = r; span->array->spec[i][GCOMP] = g; span->array->spec[i][BCOMP] = b; } } else { /* interpolate */#if CHAN_TYPE == GL_FLOAT GLfloat r = span->specRed; GLfloat g = span->specGreen; GLfloat b = span->specBlue;#else GLfixed r = span->specRed; GLfixed g = span->specGreen; GLfixed b = span->specBlue;#endif GLuint i; for (i = 0; i < span->end; i++) { span->array->spec[i][RCOMP] = FixedToChan(r); span->array->spec[i][GCOMP] = FixedToChan(g); span->array->spec[i][BCOMP] = FixedToChan(b); r += span->specRedStep; g += span->specGreenStep; b += span->specBlueStep; } } span->arrayMask |= SPAN_SPEC;}/* Fill in the span.array.fog values from the interpolation values */static voidinterpolate_fog(const GLcontext *ctx, struct sw_span *span){ GLfloat *fog = span->array->fog; const GLfloat fogStep = span->fogStep; GLfloat fogCoord = span->fog; const GLuint haveW = (span->interpMask & SPAN_W); const GLfloat wStep = haveW ? span->dwdx : 0.0F; GLfloat w = haveW ? span->w : 1.0F; GLuint i; for (i = 0; i < span->end; i++) { fog[i] = fogCoord / w; fogCoord += fogStep; w += wStep; } span->arrayMask |= SPAN_FOG;}/* Fill in the span.zArray array from the interpolation values */void_swrast_span_interpolate_z( const GLcontext *ctx, struct sw_span *span ){ const GLuint n = span->end; GLuint i; ASSERT((span->interpMask & SPAN_Z) && !(span->arrayMask & SPAN_Z)); if (ctx->DrawBuffer->Visual.depthBits <= 16) { GLfixed zval = span->z; GLuint *z = span->array->z; for (i = 0; i < n; i++) { z[i] = FixedToInt(zval); zval += span->zStep; } } else { /* Deep Z buffer, no fixed->int shift */ GLuint zval = span->z; GLuint *z = span->array->z; for (i = 0; i < n; i++) { z[i] = zval; zval += span->zStep; } } span->interpMask &= ~SPAN_Z; span->arrayMask |= SPAN_Z;}/* * This the ideal solution, as given in the OpenGL spec. */#if 0static GLfloatcompute_lambda(GLfloat dsdx, GLfloat dsdy, GLfloat dtdx, GLfloat dtdy, GLfloat dqdx, GLfloat dqdy, GLfloat texW, GLfloat texH, GLfloat s, GLfloat t, GLfloat q, GLfloat invQ){ GLfloat dudx = texW * ((s + dsdx) / (q + dqdx) - s * invQ); GLfloat dvdx = texH * ((t + dtdx) / (q + dqdx) - t * invQ); GLfloat dudy = texW * ((s + dsdy) / (q + dqdy) - s * invQ); GLfloat dvdy = texH * ((t + dtdy) / (q + dqdy) - t * invQ); GLfloat x = SQRTF(dudx * dudx + dvdx * dvdx); GLfloat y = SQRTF(dudy * dudy + dvdy * dvdy); GLfloat rho = MAX2(x, y); GLfloat lambda = LOG2(rho); return lambda;}#endif/* * This is a faster approximation */GLfloat_swrast_compute_lambda(GLfloat dsdx, GLfloat dsdy, GLfloat dtdx, GLfloat dtdy, GLfloat dqdx, GLfloat dqdy, GLfloat texW, GLfloat texH, GLfloat s, GLfloat t, GLfloat q, GLfloat invQ){ GLfloat dsdx2 = (s + dsdx) / (q + dqdx) - s * invQ; GLfloat dtdx2 = (t + dtdx) / (q + dqdx) - t * invQ; GLfloat dsdy2 = (s + dsdy) / (q + dqdy) - s * invQ; GLfloat dtdy2 = (t + dtdy) / (q + dqdy) - t * invQ; GLfloat maxU, maxV, rho, lambda; dsdx2 = FABSF(dsdx2); dsdy2 = FABSF(dsdy2); dtdx2 = FABSF(dtdx2); dtdy2 = FABSF(dtdy2); maxU = MAX2(dsdx2, dsdy2) * texW; maxV = MAX2(dtdx2, dtdy2) * texH; rho = MAX2(maxU, maxV); lambda = LOG2(rho); return lambda;}/** * Fill in the span.texcoords array from the interpolation values. * Note: in the places where we divide by Q (or mult by invQ) we're * really doing two things: perspective correction and texcoord * projection. Remember, for texcoord (s,t,r,q) we need to index * texels with (s/q, t/q, r/q). * If we're using a fragment program, we never do the division * for texcoord projection. That's done by the TXP instruction * or user-written code. */static void
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -