📄 s_tritemp.h
字号:
/* * 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. *//* * Triangle Rasterizer Template * * This file is #include'd to generate custom triangle rasterizers. * * The following macros may be defined to indicate what auxillary information * must be interplated across the triangle: * INTERP_Z - if defined, interpolate vertex Z values * INTERP_W - if defined, interpolate vertex W values * INTERP_FOG - if defined, interpolate fog values * INTERP_RGB - if defined, interpolate RGB values * INTERP_ALPHA - if defined, interpolate Alpha values (req's INTERP_RGB) * INTERP_SPEC - if defined, interpolate specular RGB values * INTERP_INDEX - if defined, interpolate color index values * INTERP_INT_TEX - if defined, interpolate integer ST texcoords * (fast, simple 2-D texture mapping) * INTERP_TEX - if defined, interpolate set 0 float STRQ texcoords * NOTE: OpenGL STRQ = Mesa STUV (R was taken for red) * INTERP_MULTITEX - if defined, interpolate N units of STRQ texcoords * * When one can directly address pixels in the color buffer the following * macros can be defined and used to compute pixel addresses during * rasterization (see pRow): * PIXEL_TYPE - the datatype of a pixel (GLubyte, GLushort, GLuint) * BYTES_PER_ROW - number of bytes per row in the color buffer * PIXEL_ADDRESS(X,Y) - returns the address of pixel at (X,Y) where * Y==0 at bottom of screen and increases upward. * * Similarly, for direct depth buffer access, this type is used for depth * buffer addressing: * DEPTH_TYPE - either GLushort or GLuint * * Optionally, one may provide one-time setup code per triangle: * SETUP_CODE - code which is to be executed once per triangle * CLEANUP_CODE - code to execute at end of triangle * * The following macro MUST be defined: * RENDER_SPAN(span) - code to write a span of pixels. * * This code was designed for the origin to be in the lower-left corner. * * Inspired by triangle rasterizer code written by Allen Akin. Thanks Allen! * * * Some notes on rasterization accuracy: * * This code uses fixed point arithmetic (the GLfixed type) to iterate * over the triangle edges and interpolate ancillary data (such as Z, * color, secondary color, etc). The number of fractional bits in * GLfixed and the value of SUB_PIXEL_BITS has a direct bearing on the * accuracy of rasterization. * * If SUB_PIXEL_BITS=4 then we'll snap the vertices to the nearest * 1/16 of a pixel. If we're walking up a long, nearly vertical edge * (dx=1/16, dy=1024) we'll need 4 + 10 = 14 fractional bits in * GLfixed to walk the edge without error. If the maximum viewport * height is 4K pixels, then we'll need 4 + 12 = 16 fractional bits. * * Historically, Mesa has used 11 fractional bits in GLfixed, snaps * vertices to 1/16 pixel and allowed a maximum viewport height of 2K * pixels. 11 fractional bits is actually insufficient for accurately * rasterizing some triangles. More recently, the maximum viewport * height was increased to 4K pixels. Thus, Mesa should be using 16 * fractional bits in GLfixed. Unfortunately, there may be some issues * with setting FIXED_FRAC_BITS=16, such as multiplication overflow. * This will have to be examined in some detail... * * For now, if you find rasterization errors, particularly with tall, * sliver triangles, try increasing FIXED_FRAC_BITS and/or decreasing * SUB_PIXEL_BITS. *//* * ColorTemp is used for intermediate color values. */#if CHAN_TYPE == GL_FLOAT#define ColorTemp GLfloat#else#define ColorTemp GLint /* same as GLfixed */#endif/* * Walk triangle edges with GLfixed or GLdouble */#if TRIANGLE_WALK_DOUBLE#define GLinterp GLdouble#define InterpToInt(X) ((GLint) (X))#define INTERP_ONE 1.0#else#define GLinterp GLfixed#define InterpToInt(X) FixedToInt(X)#define INTERP_ONE FIXED_ONE#endif/* * Either loop over all texture units, or just use unit zero. */#ifdef INTERP_MULTITEX#define TEX_UNIT_LOOP(CODE) \ { \ GLuint u; \ for (u = 0; u < ctx->Const.MaxTextureUnits; u++) { \ if (ctx->Texture._EnabledCoordUnits & (1 << u)) { \ CODE \ } \ } \ }#define INTERP_TEX#elif defined(INTERP_TEX)#define TEX_UNIT_LOOP(CODE) \ { \ const GLuint u = 0; \ CODE \ }#endif/* * Some code we unfortunately need to prevent negative interpolated colors. */#ifndef CLAMP_INTERPOLANT#define CLAMP_INTERPOLANT(CHANNEL, CHANNELSTEP, LEN) \do { \ GLfixed endVal = span.CHANNEL + (LEN) * span.CHANNELSTEP; \ if (endVal < 0) { \ span.CHANNEL -= endVal; \ } \ if (span.CHANNEL < 0) { \ span.CHANNEL = 0; \ } \} while (0)#endifstatic void NAME(GLcontext *ctx, const SWvertex *v0, const SWvertex *v1, const SWvertex *v2 ){ typedef struct { const SWvertex *v0, *v1; /* Y(v0) < Y(v1) */#if TRIANGLE_WALK_DOUBLE GLdouble dx; /* X(v1) - X(v0) */ GLdouble dy; /* Y(v1) - Y(v0) */ GLdouble dxdy; /* dx/dy */ GLdouble adjy; /* adjust from v[0]->fy to fsy, scaled */ GLdouble fsx; /* first sample point x coord */ GLdouble fsy; GLdouble fx0; /*X of lower endpoint */#else GLfloat dx; /* X(v1) - X(v0) */ GLfloat dy; /* Y(v1) - Y(v0) */ GLfloat dxdy; /* dx/dy */ GLfixed fdxdy; /* dx/dy in fixed-point */ GLfloat adjy; /* adjust from v[0]->fy to fsy, scaled */ GLfixed fsx; /* first sample point x coord */ GLfixed fsy; GLfixed fx0; /* fixed pt X of lower endpoint */#endif GLint lines; /* number of lines to be sampled on this edge */ } EdgeT;#ifdef INTERP_Z const GLint depthBits = ctx->DrawBuffer->Visual.depthBits; const GLint fixedToDepthShift = depthBits <= 16 ? FIXED_SHIFT : 0; const GLfloat maxDepth = ctx->DrawBuffer->_DepthMaxF;#define FixedToDepth(F) ((F) >> fixedToDepthShift)#endif EdgeT eMaj, eTop, eBot; GLfloat oneOverArea; const SWvertex *vMin, *vMid, *vMax; /* Y(vMin)<=Y(vMid)<=Y(vMax) */ GLfloat bf = SWRAST_CONTEXT(ctx)->_BackfaceSign;#if !TRIANGLE_WALK_DOUBLE const GLint snapMask = ~((FIXED_ONE / (1 << SUB_PIXEL_BITS)) - 1); /* for x/y coord snapping */#endif GLinterp vMin_fx, vMin_fy, vMid_fx, vMid_fy, vMax_fx, vMax_fy; struct sw_span span; INIT_SPAN(span, GL_POLYGON, 0, 0, 0);#ifdef INTERP_Z (void) fixedToDepthShift;#endif /* printf("%s()\n", __FUNCTION__); printf(" %g, %g, %g\n", v0->win[0], v0->win[1], v0->win[2]); printf(" %g, %g, %g\n", v1->win[0], v1->win[1], v1->win[2]); printf(" %g, %g, %g\n", v2->win[0], v2->win[1], v2->win[2]); */ /* ASSERT(v0->win[2] >= 0.0); ASSERT(v1->win[2] >= 0.0); ASSERT(v2->win[2] >= 0.0); */ /* Compute fixed point x,y coords w/ half-pixel offsets and snapping. * And find the order of the 3 vertices along the Y axis. */ {#if TRIANGLE_WALK_DOUBLE const GLdouble fy0 = v0->win[1] - 0.5; const GLdouble fy1 = v1->win[1] - 0.5; const GLdouble fy2 = v2->win[1] - 0.5;#else const GLfixed fy0 = FloatToFixed(v0->win[1] - 0.5F) & snapMask; const GLfixed fy1 = FloatToFixed(v1->win[1] - 0.5F) & snapMask; const GLfixed fy2 = FloatToFixed(v2->win[1] - 0.5F) & snapMask;#endif if (fy0 <= fy1) { if (fy1 <= fy2) { /* y0 <= y1 <= y2 */ vMin = v0; vMid = v1; vMax = v2; vMin_fy = fy0; vMid_fy = fy1; vMax_fy = fy2; } else if (fy2 <= fy0) { /* y2 <= y0 <= y1 */ vMin = v2; vMid = v0; vMax = v1; vMin_fy = fy2; vMid_fy = fy0; vMax_fy = fy1; } else { /* y0 <= y2 <= y1 */ vMin = v0; vMid = v2; vMax = v1; vMin_fy = fy0; vMid_fy = fy2; vMax_fy = fy1; bf = -bf; } } else { if (fy0 <= fy2) { /* y1 <= y0 <= y2 */ vMin = v1; vMid = v0; vMax = v2; vMin_fy = fy1; vMid_fy = fy0; vMax_fy = fy2; bf = -bf; } else if (fy2 <= fy1) { /* y2 <= y1 <= y0 */ vMin = v2; vMid = v1; vMax = v0; vMin_fy = fy2; vMid_fy = fy1; vMax_fy = fy0; bf = -bf; } else { /* y1 <= y2 <= y0 */ vMin = v1; vMid = v2; vMax = v0; vMin_fy = fy1; vMid_fy = fy2; vMax_fy = fy0; } } /* fixed point X coords */#if TRIANGLE_WALK_DOUBLE vMin_fx = vMin->win[0] + 0.5; vMid_fx = vMid->win[0] + 0.5; vMax_fx = vMax->win[0] + 0.5;#else vMin_fx = FloatToFixed(vMin->win[0] + 0.5F) & snapMask; vMid_fx = FloatToFixed(vMid->win[0] + 0.5F) & snapMask; vMax_fx = FloatToFixed(vMax->win[0] + 0.5F) & snapMask;#endif } /* vertex/edge relationship */ eMaj.v0 = vMin; eMaj.v1 = vMax; /*TODO: .v1's not needed */ eTop.v0 = vMid; eTop.v1 = vMax; eBot.v0 = vMin; eBot.v1 = vMid; /* compute deltas for each edge: vertex[upper] - vertex[lower] */#if TRIANGLE_WALK_DOUBLE eMaj.dx = vMax_fx - vMin_fx; eMaj.dy = vMax_fy - vMin_fy; eTop.dx = vMax_fx - vMid_fx; eTop.dy = vMax_fy - vMid_fy; eBot.dx = vMid_fx - vMin_fx; eBot.dy = vMid_fy - vMin_fy;#else eMaj.dx = FixedToFloat(vMax_fx - vMin_fx); eMaj.dy = FixedToFloat(vMax_fy - vMin_fy); eTop.dx = FixedToFloat(vMax_fx - vMid_fx); eTop.dy = FixedToFloat(vMax_fy - vMid_fy); eBot.dx = FixedToFloat(vMid_fx - vMin_fx); eBot.dy = FixedToFloat(vMid_fy - vMin_fy);#endif /* compute area, oneOverArea and perform backface culling */ {#if TRIANGLE_WALK_DOUBLE const GLdouble area = eMaj.dx * eBot.dy - eBot.dx * eMaj.dy;#else const GLfloat area = eMaj.dx * eBot.dy - eBot.dx * eMaj.dy;#endif /* Do backface culling */ if (area * bf < 0.0) return; if (IS_INF_OR_NAN(area) || area == 0.0F) return; oneOverArea = 1.0F / area; } span.facing = ctx->_Facing; /* for 2-sided stencil test */ /* Edge setup. For a triangle strip these could be reused... */ {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -