| 1 |
/* |
| 2 |
* Mesa 3-D graphics library |
| 3 |
* Version: 6.1 |
| 4 |
* |
| 5 |
* Copyright (C) 1999-2004 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 |
|
| 26 |
/** |
| 27 |
* \file swrast/s_context.h |
| 28 |
* \brief Software rasterization context and private types. |
| 29 |
* \author Keith Whitwell <[email protected]> |
| 30 |
*/ |
| 31 |
|
| 32 |
/** |
| 33 |
* \mainpage swrast module |
| 34 |
* |
| 35 |
* This module, software rasterization, contains the software fallback |
| 36 |
* routines for drawing points, lines, triangles, bitmaps and images. |
| 37 |
* All rendering boils down to writing spans (arrays) of pixels with |
| 38 |
* particular colors. The span-writing routines must be implemented |
| 39 |
* by the device driver. |
| 40 |
*/ |
| 41 |
|
| 42 |
|
| 43 |
#ifndef S_CONTEXT_H |
| 44 |
#define S_CONTEXT_H |
| 45 |
|
| 46 |
#include "mtypes.h" |
| 47 |
#include "swrast.h" |
| 48 |
|
| 49 |
|
| 50 |
/** |
| 51 |
* \defgroup SpanFlags SPAN_XXX-flags |
| 52 |
* Bitmasks to indicate which span_arrays need to be computed |
| 53 |
* (sw_span::interpMask) or have already been filled |
| 54 |
* (sw_span::arrayMask) |
| 55 |
*/ |
| 56 |
/*@{*/ |
| 57 |
#define SPAN_RGBA 0x001 |
| 58 |
#define SPAN_SPEC 0x002 |
| 59 |
#define SPAN_INDEX 0x004 |
| 60 |
#define SPAN_Z 0x008 |
| 61 |
#define SPAN_W 0x010 |
| 62 |
#define SPAN_FOG 0x020 |
| 63 |
#define SPAN_TEXTURE 0x040 |
| 64 |
#define SPAN_INT_TEXTURE 0x080 |
| 65 |
#define SPAN_LAMBDA 0x100 |
| 66 |
#define SPAN_COVERAGE 0x200 |
| 67 |
#define SPAN_FLAT 0x400 /**< flat shading? */ |
| 68 |
/** sw_span::arrayMask only - for span_arrays::x, span_arrays::y */ |
| 69 |
#define SPAN_XY 0x800 |
| 70 |
#define SPAN_MASK 0x1000 /**< sw_span::arrayMask only */ |
| 71 |
/*@}*/ |
| 72 |
|
| 73 |
|
| 74 |
/** |
| 75 |
* \struct span_arrays |
| 76 |
* \brief Arrays of fragment values. |
| 77 |
* |
| 78 |
* These will either be computed from the x/xStep values above or |
| 79 |
* filled in by glDraw/CopyPixels, etc. |
| 80 |
* These arrays are separated out of sw_span to conserve memory. |
| 81 |
*/ |
| 82 |
struct span_arrays { |
| 83 |
GLchan rgb[MAX_WIDTH][3]; |
| 84 |
GLchan rgba[MAX_WIDTH][4]; |
| 85 |
GLuint index[MAX_WIDTH]; |
| 86 |
GLchan spec[MAX_WIDTH][4]; /* specular color */ |
| 87 |
GLint x[MAX_WIDTH]; /**< X/Y used for point/line rendering only */ |
| 88 |
GLint y[MAX_WIDTH]; /**< X/Y used for point/line rendering only */ |
| 89 |
GLdepth z[MAX_WIDTH]; |
| 90 |
GLfloat fog[MAX_WIDTH]; |
| 91 |
GLfloat texcoords[MAX_TEXTURE_COORD_UNITS][MAX_WIDTH][4]; |
| 92 |
GLfloat lambda[MAX_TEXTURE_COORD_UNITS][MAX_WIDTH]; |
| 93 |
GLfloat coverage[MAX_WIDTH]; |
| 94 |
|
| 95 |
/** This mask indicates if fragment is alive or culled */ |
| 96 |
GLubyte mask[MAX_WIDTH]; |
| 97 |
}; |
| 98 |
|
| 99 |
|
| 100 |
/** |
| 101 |
* \struct sw_span |
| 102 |
* \brief Contains data for either a horizontal line or a set of |
| 103 |
* pixels that are passed through a pipeline of functions before being |
| 104 |
* drawn. |
| 105 |
* |
| 106 |
* The sw_span structure describes the colors, Z, fogcoord, texcoords, |
| 107 |
* etc for either a horizontal run or an array of independent pixels. |
| 108 |
* We can either specify a base/step to indicate interpolated values, or |
| 109 |
* fill in arrays of values. The interpMask and arrayMask bitfields |
| 110 |
* indicate which are active. |
| 111 |
* |
| 112 |
* With this structure it's easy to hand-off span rasterization to |
| 113 |
* subroutines instead of doing it all inline in the triangle functions |
| 114 |
* like we used to do. |
| 115 |
* It also cleans up the local variable namespace a great deal. |
| 116 |
* |
| 117 |
* It would be interesting to experiment with multiprocessor rasterization |
| 118 |
* with this structure. The triangle rasterizer could simply emit a |
| 119 |
* stream of these structures which would be consumed by one or more |
| 120 |
* span-processing threads which could run in parallel. |
| 121 |
*/ |
| 122 |
struct sw_span { |
| 123 |
GLint x, y; |
| 124 |
|
| 125 |
/** Only need to process pixels between start <= i < end */ |
| 126 |
/** At this time, start is always zero. */ |
| 127 |
GLuint start, end; |
| 128 |
|
| 129 |
/** This flag indicates that mask[] array is effectively filled with ones */ |
| 130 |
GLboolean writeAll; |
| 131 |
|
| 132 |
/** either GL_POLYGON, GL_LINE, GL_POLYGON, GL_BITMAP */ |
| 133 |
GLenum primitive; |
| 134 |
|
| 135 |
/** 0 = front-facing span, 1 = back-facing span (for two-sided stencil) */ |
| 136 |
GLuint facing; |
| 137 |
|
| 138 |
/** |
| 139 |
* This bitmask (of \link SpanFlags SPAN_* flags\endlink) indicates |
| 140 |
* which of the x/xStep variables are relevant. |
| 141 |
*/ |
| 142 |
GLuint interpMask; |
| 143 |
|
| 144 |
/* For horizontal spans, step is the partial derivative wrt X. |
| 145 |
* For lines, step is the delta from one fragment to the next. |
| 146 |
*/ |
| 147 |
#if CHAN_TYPE == GL_FLOAT |
| 148 |
GLfloat red, redStep; |
| 149 |
GLfloat green, greenStep; |
| 150 |
GLfloat blue, blueStep; |
| 151 |
GLfloat alpha, alphaStep; |
| 152 |
GLfloat specRed, specRedStep; |
| 153 |
GLfloat specGreen, specGreenStep; |
| 154 |
GLfloat specBlue, specBlueStep; |
| 155 |
#else /* CHAN_TYPE == GL_UNSIGNED_BYTE or GL_UNSIGNED_SHORT */ |
| 156 |
GLfixed red, redStep; |
| 157 |
GLfixed green, greenStep; |
| 158 |
GLfixed blue, blueStep; |
| 159 |
GLfixed alpha, alphaStep; |
| 160 |
GLfixed specRed, specRedStep; |
| 161 |
GLfixed specGreen, specGreenStep; |
| 162 |
GLfixed specBlue, specBlueStep; |
| 163 |
#endif |
| 164 |
GLfixed index, indexStep; |
| 165 |
GLfixed z, zStep; |
| 166 |
GLfloat fog, fogStep; |
| 167 |
GLfloat tex[MAX_TEXTURE_COORD_UNITS][4]; /* s, t, r, q */ |
| 168 |
GLfloat texStepX[MAX_TEXTURE_COORD_UNITS][4]; |
| 169 |
GLfloat texStepY[MAX_TEXTURE_COORD_UNITS][4]; |
| 170 |
GLfixed intTex[2], intTexStep[2]; /* s, t only */ |
| 171 |
|
| 172 |
/* partial derivatives wrt X and Y. */ |
| 173 |
GLfloat dzdx, dzdy; |
| 174 |
GLfloat w, dwdx, dwdy; |
| 175 |
GLfloat drdx, drdy; |
| 176 |
GLfloat dgdx, dgdy; |
| 177 |
GLfloat dbdx, dbdy; |
| 178 |
GLfloat dadx, dady; |
| 179 |
GLfloat dsrdx, dsrdy; |
| 180 |
GLfloat dsgdx, dsgdy; |
| 181 |
GLfloat dsbdx, dsbdy; |
| 182 |
GLfloat dfogdx, dfogdy; |
| 183 |
|
| 184 |
/** |
| 185 |
* This bitmask (of \link SpanFlags SPAN_* flags\endlink) indicates |
| 186 |
* which of the fragment arrays in the span_arrays struct are relevant. |
| 187 |
*/ |
| 188 |
GLuint arrayMask; |
| 189 |
|
| 190 |
/** |
| 191 |
* We store the arrays of fragment values in a separate struct so |
| 192 |
* that we can allocate sw_span structs on the stack without using |
| 193 |
* a lot of memory. The span_arrays struct is about 400KB while the |
| 194 |
* sw_span struct is only about 512 bytes. |
| 195 |
*/ |
| 196 |
struct span_arrays *array; |
| 197 |
}; |
| 198 |
|
| 199 |
|
| 200 |
#define INIT_SPAN(S, PRIMITIVE, END, INTERP_MASK, ARRAY_MASK) \ |
| 201 |
do { \ |
| 202 |
(S).primitive = (PRIMITIVE); \ |
| 203 |
(S).interpMask = (INTERP_MASK); \ |
| 204 |
(S).arrayMask = (ARRAY_MASK); \ |
| 205 |
(S).start = 0; \ |
| 206 |
(S).end = (END); \ |
| 207 |
(S).facing = 0; \ |
| 208 |
(S).array = SWRAST_CONTEXT(ctx)->SpanArrays; \ |
| 209 |
} while (0) |
| 210 |
|
| 211 |
|
| 212 |
typedef void (*texture_sample_func)(GLcontext *ctx, GLuint texUnit, |
| 213 |
const struct gl_texture_object *tObj, |
| 214 |
GLuint n, const GLfloat texcoords[][4], |
| 215 |
const GLfloat lambda[], GLchan rgba[][4]); |
| 216 |
|
| 217 |
typedef void (_ASMAPIP blend_func)( GLcontext *ctx, GLuint n, |
| 218 |
const GLubyte mask[], |
| 219 |
GLchan src[][4], CONST GLchan dst[][4] ); |
| 220 |
|
| 221 |
typedef void (*swrast_point_func)( GLcontext *ctx, const SWvertex *); |
| 222 |
|
| 223 |
typedef void (*swrast_line_func)( GLcontext *ctx, |
| 224 |
const SWvertex *, const SWvertex *); |
| 225 |
|
| 226 |
typedef void (*swrast_tri_func)( GLcontext *ctx, const SWvertex *, |
| 227 |
const SWvertex *, const SWvertex *); |
| 228 |
|
| 229 |
|
| 230 |
/** \defgroup Bitmasks |
| 231 |
* Bitmasks to indicate which rasterization options are enabled |
| 232 |
* (RasterMask) |
| 233 |
*/ |
| 234 |
/*@{*/ |
| 235 |
#define ALPHATEST_BIT 0x001 /**< Alpha-test pixels */ |
| 236 |
#define BLEND_BIT 0x002 /**< Blend pixels */ |
| 237 |
#define DEPTH_BIT 0x004 /**< Depth-test pixels */ |
| 238 |
#define FOG_BIT 0x008 /**< Fog pixels */ |
| 239 |
#define LOGIC_OP_BIT 0x010 /**< Apply logic op in software */ |
| 240 |
#define CLIP_BIT 0x020 /**< Scissor or window clip pixels */ |
| 241 |
#define STENCIL_BIT 0x040 /**< Stencil pixels */ |
| 242 |
#define MASKING_BIT 0x080 /**< Do glColorMask or glIndexMask */ |
| 243 |
#define MULTI_DRAW_BIT 0x400 /**< Write to more than one color- */ |
| 244 |
/**< buffer or no buffers. */ |
| 245 |
#define OCCLUSION_BIT 0x800 /**< GL_HP_occlusion_test enabled */ |
| 246 |
#define TEXTURE_BIT 0x1000 /**< Texturing really enabled */ |
| 247 |
#define FRAGPROG_BIT 0x2000 /**< Fragment program enabled */ |
| 248 |
#define ATIFRAGSHADER_BIT 0x4000 /**< ATI Fragment shader enabled */ |
| 249 |
#define CLAMPING_BIT 0x8000 /**< Clamp colors to [0,1] */ |
| 250 |
/*@}*/ |
| 251 |
|
| 252 |
#define _SWRAST_NEW_RASTERMASK (_NEW_BUFFERS| \ |
| 253 |
_NEW_SCISSOR| \ |
| 254 |
_NEW_COLOR| \ |
| 255 |
_NEW_DEPTH| \ |
| 256 |
_NEW_FOG| \ |
| 257 |
_NEW_PROGRAM| \ |
| 258 |
_NEW_STENCIL| \ |
| 259 |
_NEW_TEXTURE| \ |
| 260 |
_NEW_VIEWPORT| \ |
| 261 |
_NEW_DEPTH) |
| 262 |
|
| 263 |
|
| 264 |
/** |
| 265 |
* \struct SWcontext |
| 266 |
* \brief SWContext? |
| 267 |
*/ |
| 268 |
typedef struct |
| 269 |
{ |
| 270 |
/** Driver interface: |
| 271 |
*/ |
| 272 |
struct swrast_device_driver Driver; |
| 273 |
|
| 274 |
/** Configuration mechanisms to make software rasterizer match |
| 275 |
* characteristics of the hardware rasterizer (if present): |
| 276 |
*/ |
| 277 |
GLboolean AllowVertexFog; |
| 278 |
GLboolean AllowPixelFog; |
| 279 |
|
| 280 |
/** Derived values, invalidated on statechanges, updated from |
| 281 |
* _swrast_validate_derived(): |
| 282 |
*/ |
| 283 |
GLuint _RasterMask; |
| 284 |
GLfloat _MinMagThresh[MAX_TEXTURE_IMAGE_UNITS]; |
| 285 |
GLfloat _BackfaceSign; |
| 286 |
GLboolean _PreferPixelFog; /* Compute fog blend factor per fragment? */ |
| 287 |
GLboolean _AnyTextureCombine; |
| 288 |
GLchan _FogColor[3]; |
| 289 |
GLboolean _FogEnabled; |
| 290 |
GLenum _FogMode; /* either GL_FOG_MODE or fragment program's fog mode */ |
| 291 |
|
| 292 |
/* Accum buffer temporaries. |
| 293 |
*/ |
| 294 |
GLboolean _IntegerAccumMode; /**< Storing unscaled integers? */ |
| 295 |
GLfloat _IntegerAccumScaler; /**< Implicit scale factor */ |
| 296 |
|
| 297 |
/* Working values: |
| 298 |
*/ |
| 299 |
GLuint StippleCounter; /**< Line stipple counter */ |
| 300 |
GLuint NewState; |
| 301 |
GLuint StateChanges; |
| 302 |
GLenum Primitive; /* current primitive being drawn (ala glBegin) */ |
| 303 |
GLbitfield CurrentBufferBit; /* exactly one the of DD_*_BIT buffer bits */ |
| 304 |
|
| 305 |
/** Mechanism to allow driver (like X11) to register further |
| 306 |
* software rasterization routines. |
| 307 |
*/ |
| 308 |
/*@{*/ |
| 309 |
void (*choose_point)( GLcontext * ); |
| 310 |
void (*choose_line)( GLcontext * ); |
| 311 |
void (*choose_triangle)( GLcontext * ); |
| 312 |
|
| 313 |
GLuint invalidate_point; |
| 314 |
GLuint invalidate_line; |
| 315 |
GLuint invalidate_triangle; |
| 316 |
/*@}*/ |
| 317 |
|
| 318 |
/** Function pointers for dispatch behind public entrypoints. */ |
| 319 |
/*@{*/ |
| 320 |
void (*InvalidateState)( GLcontext *ctx, GLuint new_state ); |
| 321 |
|
| 322 |
swrast_point_func Point; |
| 323 |
swrast_line_func Line; |
| 324 |
swrast_tri_func Triangle; |
| 325 |
/*@}*/ |
| 326 |
|
| 327 |
/** |
| 328 |
* Placeholders for when separate specular (or secondary color) is |
| 329 |
* enabled but texturing is not. |
| 330 |
*/ |
| 331 |
/*@{*/ |
| 332 |
swrast_point_func SpecPoint; |
| 333 |
swrast_line_func SpecLine; |
| 334 |
swrast_tri_func SpecTriangle; |
| 335 |
/*@}*/ |
| 336 |
|
| 337 |
/** |
| 338 |
* Typically, we'll allocate a sw_span structure as a local variable |
| 339 |
* and set its 'array' pointer to point to this object. The reason is |
| 340 |
* this object is big and causes problems when allocated on the stack |
| 341 |
* on some systems. |
| 342 |
*/ |
| 343 |
struct span_arrays *SpanArrays; |
| 344 |
|
| 345 |
/** |
| 346 |
* Used to buffer N GL_POINTS, instead of rendering one by one. |
| 347 |
*/ |
| 348 |
struct sw_span PointSpan; |
| 349 |
|
| 350 |
/** Internal hooks, kept uptodate by the same mechanism as above. |
| 351 |
*/ |
| 352 |
blend_func BlendFunc; |
| 353 |
texture_sample_func TextureSample[MAX_TEXTURE_IMAGE_UNITS]; |
| 354 |
|
| 355 |
/** Buffer for saving the sampled texture colors. |
| 356 |
* Needed for GL_ARB_texture_env_crossbar implementation. |
| 357 |
*/ |
| 358 |
GLchan *TexelBuffer; |
| 359 |
|
| 360 |
} SWcontext; |
| 361 |
|
| 362 |
|
| 363 |
extern void |
| 364 |
_swrast_validate_derived( GLcontext *ctx ); |
| 365 |
|
| 366 |
|
| 367 |
#define SWRAST_CONTEXT(ctx) ((SWcontext *)ctx->swrast_context) |
| 368 |
|
| 369 |
#define RENDER_START(SWctx, GLctx) \ |
| 370 |
do { \ |
| 371 |
if ((SWctx)->Driver.SpanRenderStart) { \ |
| 372 |
(*(SWctx)->Driver.SpanRenderStart)(GLctx); \ |
| 373 |
} \ |
| 374 |
} while (0) |
| 375 |
|
| 376 |
#define RENDER_FINISH(SWctx, GLctx) \ |
| 377 |
do { \ |
| 378 |
if ((SWctx)->Driver.SpanRenderFinish) { \ |
| 379 |
(*(SWctx)->Driver.SpanRenderFinish)(GLctx); \ |
| 380 |
} \ |
| 381 |
} while (0) |
| 382 |
|
| 383 |
|
| 384 |
|
| 385 |
/* |
| 386 |
* XXX these macros are just bandages for now in order to make |
| 387 |
* CHAN_BITS==32 compile cleanly. |
| 388 |
* These should probably go elsewhere at some point. |
| 389 |
*/ |
| 390 |
#if CHAN_TYPE == GL_FLOAT |
| 391 |
#define ChanToFixed(X) (X) |
| 392 |
#define FixedToChan(X) (X) |
| 393 |
#else |
| 394 |
#define ChanToFixed(X) IntToFixed(X) |
| 395 |
#define FixedToChan(X) FixedToInt(X) |
| 396 |
#endif |
| 397 |
|
| 398 |
|
| 399 |
|
| 400 |
extern void |
| 401 |
_swrast_translate_program( GLcontext *ctx ); |
| 402 |
|
| 403 |
extern GLboolean |
| 404 |
_swrast_execute_codegen_program(GLcontext *ctx, |
| 405 |
const struct fragment_program *program, |
| 406 |
GLuint maxInst, |
| 407 |
struct fp_machine *machine, |
| 408 |
const struct sw_span *span, |
| 409 |
GLuint column ); |
| 410 |
|
| 411 |
|
| 412 |
#endif |