📄 s_texcombine.c
字号:
/* * Mesa 3-D graphics library * Version: 6.5.1 * * 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. */#include "glheader.h"#include "context.h"#include "colormac.h"#include "imports.h"#include "macros.h"#include "pixel.h"#include "s_context.h"#include "s_texcombine.h"#define PROD(A,B) ( (GLuint)(A) * ((GLuint)(B)+1) )#define S_PROD(A,B) ( (GLint)(A) * ((GLint)(B)+1) )#if CHAN_BITS == 32typedef GLfloat ChanTemp;#elsetypedef GLuint ChanTemp;#endif/** * Do texture application for GL_ARB/EXT_texture_env_combine. * This function also supports GL_{EXT,ARB}_texture_env_dot3 and * GL_ATI_texture_env_combine3. Since "classic" texture environments are * implemented using GL_ARB_texture_env_combine-like state, this same function * is used for classic texture environment application as well. * * \param ctx rendering context * \param textureUnit the texture unit to apply * \param n number of fragments to process (span width) * \param primary_rgba incoming fragment color array * \param texelBuffer pointer to texel colors for all texture units * * \param rgba incoming colors, which get modified here */static voidtexture_combine( const GLcontext *ctx, GLuint unit, GLuint n, CONST GLchan (*primary_rgba)[4], CONST GLchan *texelBuffer, GLchan (*rgba)[4] ){ const struct gl_texture_unit *textureUnit = &(ctx->Texture.Unit[unit]); const GLchan (*argRGB [3])[4]; const GLchan (*argA [3])[4]; const GLuint RGBshift = textureUnit->_CurrentCombine->ScaleShiftRGB; const GLuint Ashift = textureUnit->_CurrentCombine->ScaleShiftA;#if CHAN_TYPE == GL_FLOAT const GLchan RGBmult = (GLfloat) (1 << RGBshift); const GLchan Amult = (GLfloat) (1 << Ashift);#else const GLint half = (CHAN_MAX + 1) / 2;#endif static const GLchan one[4] = { CHAN_MAX, CHAN_MAX, CHAN_MAX, CHAN_MAX }; static const GLchan zero[4] = { 0, 0, 0, 0 }; const GLuint numColorArgs = textureUnit->_CurrentCombine->_NumArgsRGB; const GLuint numAlphaArgs = textureUnit->_CurrentCombine->_NumArgsA; GLchan ccolor[3][MAX_WIDTH][4]; GLuint i, j; ASSERT(ctx->Extensions.EXT_texture_env_combine || ctx->Extensions.ARB_texture_env_combine); ASSERT(SWRAST_CONTEXT(ctx)->_AnyTextureCombine); /* printf("modeRGB 0x%x modeA 0x%x srcRGB1 0x%x srcA1 0x%x srcRGB2 0x%x srcA2 0x%x\n", textureUnit->_CurrentCombine->ModeRGB, textureUnit->_CurrentCombine->ModeA, textureUnit->_CurrentCombine->SourceRGB[0], textureUnit->_CurrentCombine->SourceA[0], textureUnit->_CurrentCombine->SourceRGB[1], textureUnit->_CurrentCombine->SourceA[1]); */ /* * Do operand setup for up to 3 operands. Loop over the terms. */ for (j = 0; j < numColorArgs; j++) { const GLenum srcRGB = textureUnit->_CurrentCombine->SourceRGB[j]; switch (srcRGB) { case GL_TEXTURE: argRGB[j] = (const GLchan (*)[4]) (texelBuffer + unit * (n * 4 * sizeof(GLchan))); break; case GL_PRIMARY_COLOR: argRGB[j] = primary_rgba; break; case GL_PREVIOUS: argRGB[j] = (const GLchan (*)[4]) rgba; break; case GL_CONSTANT: { GLchan (*c)[4] = ccolor[j]; GLchan red, green, blue, alpha; UNCLAMPED_FLOAT_TO_CHAN(red, textureUnit->EnvColor[0]); UNCLAMPED_FLOAT_TO_CHAN(green, textureUnit->EnvColor[1]); UNCLAMPED_FLOAT_TO_CHAN(blue, textureUnit->EnvColor[2]); UNCLAMPED_FLOAT_TO_CHAN(alpha, textureUnit->EnvColor[3]); for (i = 0; i < n; i++) { c[i][RCOMP] = red; c[i][GCOMP] = green; c[i][BCOMP] = blue; c[i][ACOMP] = alpha; } argRGB[j] = (const GLchan (*)[4]) ccolor[j]; } break; /* GL_ATI_texture_env_combine3 allows GL_ZERO & GL_ONE as sources. */ case GL_ZERO: argRGB[j] = & zero; break; case GL_ONE: argRGB[j] = & one; break; default: /* ARB_texture_env_crossbar source */ { const GLuint srcUnit = srcRGB - GL_TEXTURE0; ASSERT(srcUnit < ctx->Const.MaxTextureUnits); if (!ctx->Texture.Unit[srcUnit]._ReallyEnabled) return; argRGB[j] = (const GLchan (*)[4]) (texelBuffer + srcUnit * (n * 4 * sizeof(GLchan))); } } if (textureUnit->_CurrentCombine->OperandRGB[j] != GL_SRC_COLOR) { const GLchan (*src)[4] = argRGB[j]; GLchan (*dst)[4] = ccolor[j]; /* point to new arg[j] storage */ argRGB[j] = (const GLchan (*)[4]) ccolor[j]; if (textureUnit->_CurrentCombine->OperandRGB[j] == GL_ONE_MINUS_SRC_COLOR) { for (i = 0; i < n; i++) { dst[i][RCOMP] = CHAN_MAX - src[i][RCOMP]; dst[i][GCOMP] = CHAN_MAX - src[i][GCOMP]; dst[i][BCOMP] = CHAN_MAX - src[i][BCOMP]; } } else if (textureUnit->_CurrentCombine->OperandRGB[j] == GL_SRC_ALPHA) { for (i = 0; i < n; i++) { dst[i][RCOMP] = src[i][ACOMP]; dst[i][GCOMP] = src[i][ACOMP]; dst[i][BCOMP] = src[i][ACOMP]; } } else { ASSERT(textureUnit->_CurrentCombine->OperandRGB[j] ==GL_ONE_MINUS_SRC_ALPHA); for (i = 0; i < n; i++) { dst[i][RCOMP] = CHAN_MAX - src[i][ACOMP]; dst[i][GCOMP] = CHAN_MAX - src[i][ACOMP]; dst[i][BCOMP] = CHAN_MAX - src[i][ACOMP]; } } } } /* * Set up the argA[i] pointers */ for (j = 0; j < numAlphaArgs; j++) { const GLenum srcA = textureUnit->_CurrentCombine->SourceA[j]; switch (srcA) { case GL_TEXTURE: argA[j] = (const GLchan (*)[4]) (texelBuffer + unit * (n * 4 * sizeof(GLchan))); break; case GL_PRIMARY_COLOR: argA[j] = primary_rgba; break; case GL_PREVIOUS: argA[j] = (const GLchan (*)[4]) rgba; break; case GL_CONSTANT: { GLchan alpha, (*c)[4] = ccolor[j]; UNCLAMPED_FLOAT_TO_CHAN(alpha, textureUnit->EnvColor[3]); for (i = 0; i < n; i++) c[i][ACOMP] = alpha; argA[j] = (const GLchan (*)[4]) ccolor[j]; } break; /* GL_ATI_texture_env_combine3 allows GL_ZERO & GL_ONE as sources. */ case GL_ZERO: argA[j] = & zero; break; case GL_ONE: argA[j] = & one; break; default: /* ARB_texture_env_crossbar source */ { const GLuint srcUnit = srcA - GL_TEXTURE0; ASSERT(srcUnit < ctx->Const.MaxTextureUnits); if (!ctx->Texture.Unit[srcUnit]._ReallyEnabled) return; argA[j] = (const GLchan (*)[4]) (texelBuffer + srcUnit * (n * 4 * sizeof(GLchan))); } } if (textureUnit->_CurrentCombine->OperandA[j] == GL_ONE_MINUS_SRC_ALPHA) { const GLchan (*src)[4] = argA[j]; GLchan (*dst)[4] = ccolor[j]; argA[j] = (const GLchan (*)[4]) ccolor[j]; for (i = 0; i < n; i++) { dst[i][ACOMP] = CHAN_MAX - src[i][ACOMP]; } } } /* * Do the texture combine. */ switch (textureUnit->_CurrentCombine->ModeRGB) { case GL_REPLACE: { const GLchan (*arg0)[4] = (const GLchan (*)[4]) argRGB[0]; if (RGBshift) { for (i = 0; i < n; i++) {#if CHAN_TYPE == GL_FLOAT rgba[i][RCOMP] = arg0[i][RCOMP] * RGBmult; rgba[i][GCOMP] = arg0[i][GCOMP] * RGBmult; rgba[i][BCOMP] = arg0[i][BCOMP] * RGBmult;#else GLuint r = (GLuint) arg0[i][RCOMP] << RGBshift; GLuint g = (GLuint) arg0[i][GCOMP] << RGBshift; GLuint b = (GLuint) arg0[i][BCOMP] << RGBshift; rgba[i][RCOMP] = MIN2(r, CHAN_MAX); rgba[i][GCOMP] = MIN2(g, CHAN_MAX); rgba[i][BCOMP] = MIN2(b, CHAN_MAX);#endif } } else { for (i = 0; i < n; i++) { rgba[i][RCOMP] = arg0[i][RCOMP]; rgba[i][GCOMP] = arg0[i][GCOMP]; rgba[i][BCOMP] = arg0[i][BCOMP]; } } } break; case GL_MODULATE: { const GLchan (*arg0)[4] = (const GLchan (*)[4]) argRGB[0]; const GLchan (*arg1)[4] = (const GLchan (*)[4]) argRGB[1];#if CHAN_TYPE != GL_FLOAT const GLint shift = CHAN_BITS - RGBshift;#endif for (i = 0; i < n; i++) {#if CHAN_TYPE == GL_FLOAT rgba[i][RCOMP] = arg0[i][RCOMP] * arg1[i][RCOMP] * RGBmult; rgba[i][GCOMP] = arg0[i][GCOMP] * arg1[i][GCOMP] * RGBmult; rgba[i][BCOMP] = arg0[i][BCOMP] * arg1[i][BCOMP] * RGBmult;#else GLuint r = PROD(arg0[i][RCOMP], arg1[i][RCOMP]) >> shift; GLuint g = PROD(arg0[i][GCOMP], arg1[i][GCOMP]) >> shift; GLuint b = PROD(arg0[i][BCOMP], arg1[i][BCOMP]) >> shift; rgba[i][RCOMP] = (GLchan) MIN2(r, CHAN_MAX); rgba[i][GCOMP] = (GLchan) MIN2(g, CHAN_MAX); rgba[i][BCOMP] = (GLchan) MIN2(b, CHAN_MAX);#endif } } break; case GL_ADD: { const GLchan (*arg0)[4] = (const GLchan (*)[4]) argRGB[0]; const GLchan (*arg1)[4] = (const GLchan (*)[4]) argRGB[1]; for (i = 0; i < n; i++) {#if CHAN_TYPE == GL_FLOAT rgba[i][RCOMP] = (arg0[i][RCOMP] + arg1[i][RCOMP]) * RGBmult; rgba[i][GCOMP] = (arg0[i][GCOMP] + arg1[i][GCOMP]) * RGBmult; rgba[i][BCOMP] = (arg0[i][BCOMP] + arg1[i][BCOMP]) * RGBmult;#else GLint r = ((GLint) arg0[i][RCOMP] + (GLint) arg1[i][RCOMP]) << RGBshift; GLint g = ((GLint) arg0[i][GCOMP] + (GLint) arg1[i][GCOMP]) << RGBshift; GLint b = ((GLint) arg0[i][BCOMP] + (GLint) arg1[i][BCOMP]) << RGBshift; rgba[i][RCOMP] = (GLchan) MIN2(r, CHAN_MAX); rgba[i][GCOMP] = (GLchan) MIN2(g, CHAN_MAX); rgba[i][BCOMP] = (GLchan) MIN2(b, CHAN_MAX);#endif } } break; case GL_ADD_SIGNED: { const GLchan (*arg0)[4] = (const GLchan (*)[4]) argRGB[0]; const GLchan (*arg1)[4] = (const GLchan (*)[4]) argRGB[1]; for (i = 0; i < n; i++) {#if CHAN_TYPE == GL_FLOAT rgba[i][RCOMP] = (arg0[i][RCOMP] + arg1[i][RCOMP] - 0.5) * RGBmult; rgba[i][GCOMP] = (arg0[i][GCOMP] + arg1[i][GCOMP] - 0.5) * RGBmult; rgba[i][BCOMP] = (arg0[i][BCOMP] + arg1[i][BCOMP] - 0.5) * RGBmult;#else GLint r = (GLint) arg0[i][RCOMP] + (GLint) arg1[i][RCOMP] -half; GLint g = (GLint) arg0[i][GCOMP] + (GLint) arg1[i][GCOMP] -half; GLint b = (GLint) arg0[i][BCOMP] + (GLint) arg1[i][BCOMP] -half; r = (r < 0) ? 0 : r << RGBshift; g = (g < 0) ? 0 : g << RGBshift; b = (b < 0) ? 0 : b << RGBshift; rgba[i][RCOMP] = (GLchan) MIN2(r, CHAN_MAX); rgba[i][GCOMP] = (GLchan) MIN2(g, CHAN_MAX); rgba[i][BCOMP] = (GLchan) MIN2(b, CHAN_MAX);#endif } } break; case GL_INTERPOLATE: { const GLchan (*arg0)[4] = (const GLchan (*)[4]) argRGB[0]; const GLchan (*arg1)[4] = (const GLchan (*)[4]) argRGB[1]; const GLchan (*arg2)[4] = (const GLchan (*)[4]) argRGB[2];#if CHAN_TYPE != GL_FLOAT const GLint shift = CHAN_BITS - RGBshift;#endif for (i = 0; i < n; i++) {#if CHAN_TYPE == GL_FLOAT rgba[i][RCOMP] = (arg0[i][RCOMP] * arg2[i][RCOMP] + arg1[i][RCOMP] * (CHAN_MAXF - arg2[i][RCOMP])) * RGBmult; rgba[i][GCOMP] = (arg0[i][GCOMP] * arg2[i][GCOMP] + arg1[i][GCOMP] * (CHAN_MAXF - arg2[i][GCOMP])) * RGBmult; rgba[i][BCOMP] = (arg0[i][BCOMP] * arg2[i][BCOMP] + arg1[i][BCOMP] * (CHAN_MAXF - arg2[i][BCOMP])) * RGBmult;#else GLuint r = (PROD(arg0[i][RCOMP], arg2[i][RCOMP]) + PROD(arg1[i][RCOMP], CHAN_MAX - arg2[i][RCOMP])) >> shift; GLuint g = (PROD(arg0[i][GCOMP], arg2[i][GCOMP]) + PROD(arg1[i][GCOMP], CHAN_MAX - arg2[i][GCOMP])) >> shift; GLuint b = (PROD(arg0[i][BCOMP], arg2[i][BCOMP]) + PROD(arg1[i][BCOMP], CHAN_MAX - arg2[i][BCOMP])) >> shift; rgba[i][RCOMP] = (GLchan) MIN2(r, CHAN_MAX); rgba[i][GCOMP] = (GLchan) MIN2(g, CHAN_MAX); rgba[i][BCOMP] = (GLchan) MIN2(b, CHAN_MAX);#endif } } break; case GL_SUBTRACT: { const GLchan (*arg0)[4] = (const GLchan (*)[4]) argRGB[0]; const GLchan (*arg1)[4] = (const GLchan (*)[4]) argRGB[1]; for (i = 0; i < n; i++) {#if CHAN_TYPE == GL_FLOAT rgba[i][RCOMP] = (arg0[i][RCOMP] - arg1[i][RCOMP]) * RGBmult; rgba[i][GCOMP] = (arg0[i][GCOMP] - arg1[i][GCOMP]) * RGBmult; rgba[i][BCOMP] = (arg0[i][BCOMP] - arg1[i][BCOMP]) * RGBmult;#else GLint r = ((GLint) arg0[i][RCOMP] - (GLint) arg1[i][RCOMP]) << RGBshift; GLint g = ((GLint) arg0[i][GCOMP] - (GLint) arg1[i][GCOMP]) << RGBshift; GLint b = ((GLint) arg0[i][BCOMP] - (GLint) arg1[i][BCOMP]) << RGBshift; rgba[i][RCOMP] = (GLchan) CLAMP(r, 0, CHAN_MAX); rgba[i][GCOMP] = (GLchan) CLAMP(g, 0, CHAN_MAX); rgba[i][BCOMP] = (GLchan) CLAMP(b, 0, CHAN_MAX);#endif } } break;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -