2 * Mesa 3-D graphics library
5 * Copyright (C) 1999-2003 Brian Paul All Rights Reserved.
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:
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
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.
27 * \file swrast/s_context.h
28 * \brief Software rasterization context and private types.
29 * \author Keith Whitwell <keith@tungstengraphics.com>
40 * \defgroup SpanFlags SPAN_XXX-flags
41 * Bitmasks to indicate which span_arrays need to be computed
42 * (sw_span::interpMask) or have already been filled
43 * (sw_span::arrayMask)
46 #define SPAN_RGBA 0x001
47 #define SPAN_SPEC 0x002
48 #define SPAN_INDEX 0x004
50 #define SPAN_FOG 0x010
51 #define SPAN_TEXTURE 0x020
52 #define SPAN_INT_TEXTURE 0x040
53 #define SPAN_LAMBDA 0x080
54 #define SPAN_COVERAGE 0x100
55 #define SPAN_FLAT 0x200 /**< flat shading? */
56 /** sw_span::arrayMask only - for span_arrays::x, span_arrays::y */
58 #define SPAN_MASK 0x800 /**< sw_span::arrayMask only */
64 * \brief Arrays of fragment values.
66 * These will either be computed from the x/xStep values above or
67 * filled in by glDraw/CopyPixels, etc.
68 * These arrays are separated out of sw_span to conserve memory.
71 GLchan rgb
[MAX_WIDTH
][3];
72 GLchan rgba
[MAX_WIDTH
][4];
73 GLuint index
[MAX_WIDTH
];
74 GLchan spec
[MAX_WIDTH
][4]; /* specular color */
75 GLint x
[MAX_WIDTH
]; /**< X/Y used for point/line rendering only */
76 GLint y
[MAX_WIDTH
]; /**< X/Y used for point/line rendering only */
78 GLfloat fog
[MAX_WIDTH
];
79 GLfloat texcoords
[MAX_TEXTURE_COORD_UNITS
][MAX_WIDTH
][4];
80 GLfloat lambda
[MAX_TEXTURE_COORD_UNITS
][MAX_WIDTH
];
81 GLfloat coverage
[MAX_WIDTH
];
83 /** This mask indicates if fragment is alive or culled */
84 GLubyte mask
[MAX_WIDTH
];
90 * \brief Contains data for either a horizontal line or a set of
91 * pixels that are passed through a pipeline of functions before being
94 * The sw_span structure describes the colors, Z, fogcoord, texcoords,
95 * etc for either a horizontal run or an array of independent pixels.
96 * We can either specify a base/step to indicate interpolated values, or
97 * fill in arrays of values. The interpMask and arrayMask bitfields
98 * indicate which are active.
100 * With this structure it's easy to hand-off span rasterization to
101 * subroutines instead of doing it all inline in the triangle functions
102 * like we used to do.
103 * It also cleans up the local variable namespace a great deal.
105 * It would be interesting to experiment with multiprocessor rasterization
106 * with this structure. The triangle rasterizer could simply emit a
107 * stream of these structures which would be consumed by one or more
108 * span-processing threads which could run in parallel.
113 /** Only need to process pixels between start <= i < end */
114 /** At this time, start is always zero. */
117 /** This flag indicates that mask[] array is effectively filled with ones */
120 /** either GL_POLYGON, GL_LINE, GL_POLYGON, GL_BITMAP */
123 /** 0 = front-facing span, 1 = back-facing span (for two-sided stencil) */
127 * This bitmask (of \link SpanFlags SPAN_* flags\endlink) indicates
128 * which of the x/xStep variables are relevant.
132 /* For horizontal spans, step is the partial derivative wrt X.
133 * For lines, step is the delta from one fragment to the next.
135 #if CHAN_TYPE == GL_FLOAT
136 GLfloat red
, redStep
;
137 GLfloat green
, greenStep
;
138 GLfloat blue
, blueStep
;
139 GLfloat alpha
, alphaStep
;
140 GLfloat specRed
, specRedStep
;
141 GLfloat specGreen
, specGreenStep
;
142 GLfloat specBlue
, specBlueStep
;
143 #else /* CHAN_TYPE == GL_UNSIGNED_BYTE or GL_UNSIGNED_SHORT */
144 GLfixed red
, redStep
;
145 GLfixed green
, greenStep
;
146 GLfixed blue
, blueStep
;
147 GLfixed alpha
, alphaStep
;
148 GLfixed specRed
, specRedStep
;
149 GLfixed specGreen
, specGreenStep
;
150 GLfixed specBlue
, specBlueStep
;
152 GLfixed index
, indexStep
;
154 GLfloat fog
, fogStep
;
155 GLfloat tex
[MAX_TEXTURE_COORD_UNITS
][4]; /* s, t, r, q */
156 GLfloat texStepX
[MAX_TEXTURE_COORD_UNITS
][4];
157 GLfloat texStepY
[MAX_TEXTURE_COORD_UNITS
][4];
158 GLfixed intTex
[2], intTexStep
[2]; /* s, t only */
160 /* partial derivatives wrt X and Y. */
162 GLfloat w
, dwdx
, dwdy
;
167 GLfloat dsrdx
, dsrdy
;
168 GLfloat dsgdx
, dsgdy
;
169 GLfloat dsbdx
, dsbdy
;
170 GLfloat dfogdx
, dfogdy
;
173 * This bitmask (of \link SpanFlags SPAN_* flags\endlink) indicates
174 * which of the fragment arrays in the span_arrays struct are relevant.
179 * We store the arrays of fragment values in a separate struct so
180 * that we can allocate sw_span structs on the stack without using
181 * a lot of memory. The span_arrays struct is about 400KB while the
182 * sw_span struct is only about 512 bytes.
184 struct span_arrays
*array
;
188 #define INIT_SPAN(S, PRIMITIVE, END, INTERP_MASK, ARRAY_MASK) \
190 (S).primitive = (PRIMITIVE); \
191 (S).interpMask = (INTERP_MASK); \
192 (S).arrayMask = (ARRAY_MASK); \
196 (S).array = SWRAST_CONTEXT(ctx)->SpanArrays; \
200 typedef void (*texture_sample_func
)(GLcontext
*ctx
, GLuint texUnit
,
201 const struct gl_texture_object
*tObj
,
202 GLuint n
, const GLfloat texcoords
[][4],
203 const GLfloat lambda
[], GLchan rgba
[][4]);
205 typedef void (_ASMAPIP blend_func
)( GLcontext
*ctx
, GLuint n
,
206 const GLubyte mask
[],
207 GLchan src
[][4], CONST GLchan dst
[][4] );
209 typedef void (*swrast_point_func
)( GLcontext
*ctx
, const SWvertex
*);
211 typedef void (*swrast_line_func
)( GLcontext
*ctx
,
212 const SWvertex
*, const SWvertex
*);
214 typedef void (*swrast_tri_func
)( GLcontext
*ctx
, const SWvertex
*,
215 const SWvertex
*, const SWvertex
*);
218 /** \defgroup Bitmasks
219 * Bitmasks to indicate which rasterization options are enabled
223 #define ALPHATEST_BIT 0x001 /**< Alpha-test pixels */
224 #define BLEND_BIT 0x002 /**< Blend pixels */
225 #define DEPTH_BIT 0x004 /**< Depth-test pixels */
226 #define FOG_BIT 0x008 /**< Fog pixels */
227 #define LOGIC_OP_BIT 0x010 /**< Apply logic op in software */
228 #define CLIP_BIT 0x020 /**< Scissor or window clip pixels */
229 #define STENCIL_BIT 0x040 /**< Stencil pixels */
230 #define MASKING_BIT 0x080 /**< Do glColorMask or glIndexMask */
231 #define ALPHABUF_BIT 0x100 /**< Using software alpha buffer */
232 #define MULTI_DRAW_BIT 0x400 /**< Write to more than one color- */
233 /**< buffer or no buffers. */
234 #define OCCLUSION_BIT 0x800 /**< GL_HP_occlusion_test enabled */
235 #define TEXTURE_BIT 0x1000 /**< Texturing really enabled */
236 #define FRAGPROG_BIT 0x2000 /**< Fragment program enabled */
239 #define _SWRAST_NEW_RASTERMASK (_NEW_BUFFERS| \
257 /** Driver interface:
259 struct swrast_device_driver Driver
;
261 /** Configuration mechanisms to make software rasterizer match
262 * characteristics of the hardware rasterizer (if present):
264 GLboolean AllowVertexFog
;
265 GLboolean AllowPixelFog
;
267 /** Derived values, invalidated on statechanges, updated from
268 * _swrast_validate_derived():
271 GLfloat _MinMagThresh
[MAX_TEXTURE_IMAGE_UNITS
];
272 GLfloat _BackfaceSign
;
273 GLboolean _PreferPixelFog
;
274 GLboolean _AnyTextureCombine
;
276 /* Accum buffer temporaries.
278 GLboolean _IntegerAccumMode
; /**< Storing unscaled integers? */
279 GLfloat _IntegerAccumScaler
; /**< Implicit scale factor */
284 GLuint StippleCounter
; /**< Line stipple counter */
287 GLenum Primitive
; /* current primitive being drawn (ala glBegin) */
288 GLuint CurrentBuffer
; /* exactly one of FRONT_LEFT_BIT, BACK_LEFT_BIT, etc*/
290 /** Mechanism to allow driver (like X11) to register further
291 * software rasterization routines.
294 void (*choose_point
)( GLcontext
* );
295 void (*choose_line
)( GLcontext
* );
296 void (*choose_triangle
)( GLcontext
* );
298 GLuint invalidate_point
;
299 GLuint invalidate_line
;
300 GLuint invalidate_triangle
;
303 /** Function pointers for dispatch behind public entrypoints. */
305 void (*InvalidateState
)( GLcontext
*ctx
, GLuint new_state
);
307 swrast_point_func Point
;
308 swrast_line_func Line
;
309 swrast_tri_func Triangle
;
313 * Placeholders for when separate specular (or secondary color) is
314 * enabled but texturing is not.
317 swrast_point_func SpecPoint
;
318 swrast_line_func SpecLine
;
319 swrast_tri_func SpecTriangle
;
323 * Typically, we'll allocate a sw_span structure as a local variable
324 * and set its 'array' pointer to point to this object. The reason is
325 * this object is big and causes problems when allocated on the stack
328 struct span_arrays
*SpanArrays
;
331 * Used to buffer N GL_POINTS, instead of rendering one by one.
333 struct sw_span PointSpan
;
335 /** Internal hooks, kept uptodate by the same mechanism as above.
337 blend_func BlendFunc
;
338 texture_sample_func TextureSample
[MAX_TEXTURE_IMAGE_UNITS
];
340 /** Buffer for saving the sampled texture colors.
341 * Needed for GL_ARB_texture_env_crossbar implementation.
349 _swrast_validate_derived( GLcontext
*ctx
);
352 #define SWRAST_CONTEXT(ctx) ((SWcontext *)ctx->swrast_context)
354 #define RENDER_START(SWctx, GLctx) \
356 if ((SWctx)->Driver.SpanRenderStart) { \
357 (*(SWctx)->Driver.SpanRenderStart)(GLctx); \
361 #define RENDER_FINISH(SWctx, GLctx) \
363 if ((SWctx)->Driver.SpanRenderFinish) { \
364 (*(SWctx)->Driver.SpanRenderFinish)(GLctx); \
371 * XXX these macros are just bandages for now in order to make
372 * CHAN_BITS==32 compile cleanly.
373 * These should probably go elsewhere at some point.
375 #if CHAN_TYPE == GL_FLOAT
376 #define ChanToFixed(X) (X)
377 #define FixedToChan(X) (X)
379 #define ChanToFixed(X) IntToFixed(X)
380 #define FixedToChan(X) FixedToInt(X)