📄 r300_state.c
字号:
/*Copyright (C) The Weather Channel, Inc. 2002.Copyright (C) 2004 Nicolai Haehnle.All Rights Reserved.The Weather Channel (TM) funded Tungsten Graphics to develop theinitial release of the Radeon 8500 driver under the XFree86 license.This notice must be preserved.Permission is hereby granted, free of charge, to any person obtaininga copy of this software and associated documentation files (the"Software"), to deal in the Software without restriction, includingwithout limitation the rights to use, copy, modify, merge, publish,distribute, sublicense, and/or sell copies of the Software, and topermit persons to whom the Software is furnished to do so, subject tothe following conditions:The above copyright notice and this permission notice (including thenext paragraph) shall be included in all copies or substantialportions of the Software.THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OFMERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE COPYRIGHT OWNER(S) AND/OR ITS SUPPLIERS BELIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTIONOF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTIONWITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.**************************************************************************//* * Authors: * Nicolai Haehnle <prefect_@gmx.net> */#include "glheader.h"#include "state.h"#include "imports.h"#include "enums.h"#include "macros.h"#include "context.h"#include "dd.h"#include "simple_list.h"#include "api_arrayelt.h"#include "swrast/swrast.h"#include "swrast_setup/swrast_setup.h"#include "array_cache/acache.h"#include "tnl/tnl.h"#include "texformat.h"#include "radeon_ioctl.h"#include "radeon_state.h"#include "r300_context.h"#include "r300_ioctl.h"#include "r300_state.h"#include "r300_reg.h"#include "r300_program.h"#include "r300_emit.h"#include "r300_fragprog.h"#include "r300_tex.h"#include "r300_maos.h"#include "drirenderbuffer.h"static void r300AlphaFunc(GLcontext * ctx, GLenum func, GLfloat ref){ r300ContextPtr rmesa = R300_CONTEXT(ctx); int pp_misc = rmesa->hw.at.cmd[R300_AT_ALPHA_TEST]; GLubyte refByte; CLAMPED_FLOAT_TO_UBYTE(refByte, ref); R300_STATECHANGE(rmesa, at); pp_misc &= ~(R300_ALPHA_TEST_OP_MASK | R300_REF_ALPHA_MASK); pp_misc |= (refByte & R300_REF_ALPHA_MASK); switch (func) { case GL_NEVER: pp_misc |= R300_ALPHA_TEST_FAIL; break; case GL_LESS: pp_misc |= R300_ALPHA_TEST_LESS; break; case GL_EQUAL: pp_misc |= R300_ALPHA_TEST_EQUAL; break; case GL_LEQUAL: pp_misc |= R300_ALPHA_TEST_LEQUAL; break; case GL_GREATER: pp_misc |= R300_ALPHA_TEST_GREATER; break; case GL_NOTEQUAL: pp_misc |= R300_ALPHA_TEST_NEQUAL; break; case GL_GEQUAL: pp_misc |= R300_ALPHA_TEST_GEQUAL; break; case GL_ALWAYS: pp_misc |= R300_ALPHA_TEST_PASS; //pp_misc &= ~R300_ALPHA_TEST_ENABLE; break; } rmesa->hw.at.cmd[R300_AT_ALPHA_TEST] = pp_misc;}static void r300BlendColor(GLcontext * ctx, const GLfloat cf[4]){ GLubyte color[4]; r300ContextPtr rmesa = R300_CONTEXT(ctx); R300_STATECHANGE(rmesa, unk4E10); CLAMPED_FLOAT_TO_UBYTE(color[0], cf[0]); CLAMPED_FLOAT_TO_UBYTE(color[1], cf[1]); CLAMPED_FLOAT_TO_UBYTE(color[2], cf[2]); CLAMPED_FLOAT_TO_UBYTE(color[3], cf[3]); rmesa->hw.unk4E10.cmd[1] = r300PackColor(4, color[3], color[0], color[1], color[2]);}/** * Calculate the hardware blend factor setting. This same function is used * for source and destination of both alpha and RGB. * * \returns * The hardware register value for the specified blend factor. This value * will need to be shifted into the correct position for either source or * destination factor. * * \todo * Since the two cases where source and destination are handled differently * are essentially error cases, they should never happen. Determine if these * cases can be removed. */static int blend_factor(GLenum factor, GLboolean is_src){ int func; switch (factor) { case GL_ZERO: func = R200_BLEND_GL_ZERO; break; case GL_ONE: func = R200_BLEND_GL_ONE; break; case GL_DST_COLOR: func = R200_BLEND_GL_DST_COLOR; break; case GL_ONE_MINUS_DST_COLOR: func = R200_BLEND_GL_ONE_MINUS_DST_COLOR; break; case GL_SRC_COLOR: func = R200_BLEND_GL_SRC_COLOR; break; case GL_ONE_MINUS_SRC_COLOR: func = R200_BLEND_GL_ONE_MINUS_SRC_COLOR; break; case GL_SRC_ALPHA: func = R200_BLEND_GL_SRC_ALPHA; break; case GL_ONE_MINUS_SRC_ALPHA: func = R200_BLEND_GL_ONE_MINUS_SRC_ALPHA; break; case GL_DST_ALPHA: func = R200_BLEND_GL_DST_ALPHA; break; case GL_ONE_MINUS_DST_ALPHA: func = R200_BLEND_GL_ONE_MINUS_DST_ALPHA; break; case GL_SRC_ALPHA_SATURATE: func = (is_src) ? R200_BLEND_GL_SRC_ALPHA_SATURATE : R200_BLEND_GL_ZERO; break; case GL_CONSTANT_COLOR: func = R200_BLEND_GL_CONST_COLOR; break; case GL_ONE_MINUS_CONSTANT_COLOR: func = R200_BLEND_GL_ONE_MINUS_CONST_COLOR; break; case GL_CONSTANT_ALPHA: func = R200_BLEND_GL_CONST_ALPHA; break; case GL_ONE_MINUS_CONSTANT_ALPHA: func = R200_BLEND_GL_ONE_MINUS_CONST_ALPHA; break; default: fprintf(stderr, "unknown blend factor %x\n", factor); func = (is_src) ? R200_BLEND_GL_ONE : R200_BLEND_GL_ZERO; } return func;}/** * Sets both the blend equation and the blend function. * This is done in a single * function because some blend equations (i.e., \c GL_MIN and \c GL_MAX) * change the interpretation of the blend function. * Also, make sure that blend function and blend equation are set to their default * value if color blending is not enabled, since at least blend equations GL_MIN * and GL_FUNC_REVERSE_SUBTRACT will cause wrong results otherwise for * unknown reasons. *//* helper function */static void r300_set_blend_cntl(r300ContextPtr r300, int func, int eqn, int cbits, int funcA, int eqnA){ GLuint new_ablend, new_cblend;#if 0 fprintf(stderr, "eqnA=%08x funcA=%08x eqn=%08x func=%08x cbits=%08x\n", eqnA, funcA, eqn, func, cbits);#endif new_ablend = eqnA | funcA; new_cblend = eqn | func; /* Some blend factor combinations don't seem to work when the * BLEND_NO_SEPARATE bit is set. * * Especially problematic candidates are the ONE_MINUS_* flags, * but I can't see a real pattern. */#if 0 if (new_ablend == new_cblend) { new_cblend |= R300_BLEND_NO_SEPARATE; }#endif new_cblend |= cbits; if((new_ablend != r300->hw.bld.cmd[R300_BLD_ABLEND]) || (new_cblend != r300->hw.bld.cmd[R300_BLD_CBLEND])) { R300_STATECHANGE(r300, bld); r300->hw.bld.cmd[R300_BLD_ABLEND]=new_ablend; r300->hw.bld.cmd[R300_BLD_CBLEND]=new_cblend; }}static void r300_set_blend_state(GLcontext * ctx){ r300ContextPtr r300 = R300_CONTEXT(ctx); int func = (R200_BLEND_GL_ONE << R200_SRC_BLEND_SHIFT) | (R200_BLEND_GL_ZERO << R200_DST_BLEND_SHIFT); int eqn = R200_COMB_FCN_ADD_CLAMP; int funcA = (R200_BLEND_GL_ONE << R200_SRC_BLEND_SHIFT) | (R200_BLEND_GL_ZERO << R200_DST_BLEND_SHIFT); int eqnA = R200_COMB_FCN_ADD_CLAMP; if (ctx->Color._LogicOpEnabled || !ctx->Color.BlendEnabled) { r300_set_blend_cntl(r300, func, eqn, 0, func, eqn); return; } func = (blend_factor(ctx->Color.BlendSrcRGB, GL_TRUE) << R200_SRC_BLEND_SHIFT) | (blend_factor(ctx->Color.BlendDstRGB, GL_FALSE) << R200_DST_BLEND_SHIFT); switch (ctx->Color.BlendEquationRGB) { case GL_FUNC_ADD: eqn = R300_COMB_FCN_ADD_CLAMP; break; case GL_FUNC_SUBTRACT: eqn = R300_COMB_FCN_SUB_CLAMP; break; case GL_FUNC_REVERSE_SUBTRACT: eqn = R200_COMB_FCN_RSUB_CLAMP; break; case GL_MIN: eqn = R200_COMB_FCN_MIN; func = (R200_BLEND_GL_ONE << R200_SRC_BLEND_SHIFT) | (R200_BLEND_GL_ONE << R200_DST_BLEND_SHIFT); break; case GL_MAX: eqn = R200_COMB_FCN_MAX; func = (R200_BLEND_GL_ONE << R200_SRC_BLEND_SHIFT) | (R200_BLEND_GL_ONE << R200_DST_BLEND_SHIFT); break; default: fprintf(stderr, "[%s:%u] Invalid RGB blend equation (0x%04x).\n", __func__, __LINE__, ctx->Color.BlendEquationRGB); return; } funcA = (blend_factor(ctx->Color.BlendSrcA, GL_TRUE) << R200_SRC_BLEND_SHIFT) | (blend_factor(ctx->Color.BlendDstA, GL_FALSE) << R200_DST_BLEND_SHIFT); switch (ctx->Color.BlendEquationA) { case GL_FUNC_ADD: eqnA = R300_COMB_FCN_ADD_CLAMP; break; case GL_FUNC_SUBTRACT: eqnA = R300_COMB_FCN_SUB_CLAMP; break; case GL_FUNC_REVERSE_SUBTRACT: eqnA = R200_COMB_FCN_RSUB_CLAMP; break; case GL_MIN: eqnA = R200_COMB_FCN_MIN; funcA = (R200_BLEND_GL_ONE << R200_SRC_BLEND_SHIFT) | (R200_BLEND_GL_ONE << R200_DST_BLEND_SHIFT); break; case GL_MAX: eqnA = R200_COMB_FCN_MAX; funcA = (R200_BLEND_GL_ONE << R200_SRC_BLEND_SHIFT) | (R200_BLEND_GL_ONE << R200_DST_BLEND_SHIFT); break; default: fprintf(stderr, "[%s:%u] Invalid A blend equation (0x%04x).\n", __func__, __LINE__, ctx->Color.BlendEquationA); return; } r300_set_blend_cntl(r300, func, eqn, R300_BLEND_UNKNOWN | R300_BLEND_ENABLE, funcA, eqnA);}static void r300BlendEquationSeparate(GLcontext * ctx, GLenum modeRGB, GLenum modeA){ r300_set_blend_state(ctx);}static void r300BlendFuncSeparate(GLcontext * ctx, GLenum sfactorRGB, GLenum dfactorRGB, GLenum sfactorA, GLenum dfactorA){ r300_set_blend_state(ctx);}/** * Update our tracked culling state based on Mesa's state. */static void r300UpdateCulling(GLcontext* ctx){ r300ContextPtr r300 = R300_CONTEXT(ctx); uint32_t val = 0; R300_STATECHANGE(r300, cul); if (ctx->Polygon.CullFlag) { if (ctx->Polygon.CullFaceMode == GL_FRONT_AND_BACK) val = R300_CULL_FRONT|R300_CULL_BACK; else if (ctx->Polygon.CullFaceMode == GL_FRONT) val = R300_CULL_FRONT; else val = R300_CULL_BACK; if (ctx->Polygon.FrontFace == GL_CW) val |= R300_FRONT_FACE_CW; else val |= R300_FRONT_FACE_CCW; } r300->hw.cul.cmd[R300_CUL_CULL] = val;}static void update_early_z(GLcontext* ctx){ /* updates register 0x4f14 if depth test is not enabled it should be 0x00000000 if depth is enabled and alpha not it should be 0x00000001 if depth and alpha is enabled it should be 0x00000000 */ r300ContextPtr r300 = R300_CONTEXT(ctx); R300_STATECHANGE(r300, unk4F10); if (ctx->Color.AlphaEnabled) /* disable early Z */ r300->hw.unk4F10.cmd[2] = 0x00000000; else { if (ctx->Depth.Test) /* enable early Z */ r300->hw.unk4F10.cmd[2] = 0x00000001; else /* disable early Z */ r300->hw.unk4F10.cmd[2] = 0x00000000; }}/** * Handle glEnable()/glDisable(). * * \note Mesa already filters redundant calls to glEnable/glDisable. */static void r300Enable(GLcontext* ctx, GLenum cap, GLboolean state){ r300ContextPtr r300 = R300_CONTEXT(ctx); uint32_t newval; if (RADEON_DEBUG & DEBUG_STATE) fprintf(stderr, "%s( %s = %s )\n", __FUNCTION__, _mesa_lookup_enum_by_nr(cap), state ? "GL_TRUE" : "GL_FALSE"); switch (cap) { /* Fast track this one... */ case GL_TEXTURE_1D: case GL_TEXTURE_2D: case GL_TEXTURE_3D: break; case GL_ALPHA_TEST: R300_STATECHANGE(r300, at); if (state) { r300->hw.at.cmd[R300_AT_ALPHA_TEST] |= R300_ALPHA_TEST_ENABLE; } else { r300->hw.at.cmd[R300_AT_ALPHA_TEST] &= ~R300_ALPHA_TEST_ENABLE; } update_early_z(ctx); break;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -