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
51 #define SPAN_FOG 0x020
52 #define SPAN_TEXTURE 0x040
53 #define SPAN_INT_TEXTURE 0x080
54 #define SPAN_LAMBDA 0x100
55 #define SPAN_COVERAGE 0x200
56 #define SPAN_FLAT 0x400 /**< flat shading? */
57 /** sw_span::arrayMask only - for span_arrays::x, span_arrays::y */
59 #define SPAN_MASK 0x1000 /**< sw_span::arrayMask only */
65 * \brief Arrays of fragment values.
67 * These will either be computed from the x/xStep values above or
68 * filled in by glDraw/CopyPixels, etc.
69 * These arrays are separated out of sw_span to conserve memory.
72 GLchan rgb
[MAX_WIDTH
][3];
73 GLchan rgba
[MAX_WIDTH
][4];
74 GLuint index
[MAX_WIDTH
];
75 GLchan spec
[MAX_WIDTH
][4]; /* specular color */
76 GLint x
[MAX_WIDTH
]; /**< X/Y used for point/line rendering only */
77 GLint y
[MAX_WIDTH
]; /**< X/Y used for point/line rendering only */
79 GLfloat fog
[MAX_WIDTH
];
80 GLfloat texcoords
[MAX_TEXTURE_COORD_UNITS
][MAX_WIDTH
][4];
81 GLfloat lambda
[MAX_TEXTURE_COORD_UNITS
][MAX_WIDTH
];
82 GLfloat coverage
[MAX_WIDTH
];
84 /** This mask indicates if fragment is alive or culled */
85 GLubyte mask
[MAX_WIDTH
];
91 * \brief Contains data for either a horizontal line or a set of
92 * pixels that are passed through a pipeline of functions before being
95 * The sw_span structure describes the colors, Z, fogcoord, texcoords,
96 * etc for either a horizontal run or an array of independent pixels.
97 * We can either specify a base/step to indicate interpolated values, or
98 * fill in arrays of values. The interpMask and arrayMask bitfields
99 * indicate which are active.
101 * With this structure it's easy to hand-off span rasterization to
102 * subroutines instead of doing it all inline in the triangle functions
103 * like we used to do.
104 * It also cleans up the local variable namespace a great deal.
106 * It would be interesting to experiment with multiprocessor rasterization
107 * with this structure. The triangle rasterizer could simply emit a
108 * stream of these structures which would be consumed by one or more
109 * span-processing threads which could run in parallel.
114 /** Only need to process pixels between start <= i < end */
115 /** At this time, start is always zero. */
118 /** This flag indicates that mask[] array is effectively filled with ones */
121 /** either GL_POLYGON, GL_LINE, GL_POLYGON, GL_BITMAP */
124 /** 0 = front-facing span, 1 = back-facing span (for two-sided stencil) */
128 * This bitmask (of \link SpanFlags SPAN_* flags\endlink) indicates
129 * which of the x/xStep variables are relevant.
133 /* For horizontal spans, step is the partial derivative wrt X.
134 * For lines, step is the delta from one fragment to the next.
136 #if CHAN_TYPE == GL_FLOAT
137 GLfloat red
, redStep
;
138 GLfloat green
, greenStep
;
139 GLfloat blue
, blueStep
;
140 GLfloat alpha
, alphaStep
;
141 GLfloat specRed
, specRedStep
;
142 GLfloat specGreen
, specGreenStep
;
143 GLfloat specBlue
, specBlueStep
;
144 #else /* CHAN_TYPE == GL_UNSIGNED_BYTE or GL_UNSIGNED_SHORT */
145 GLfixed red
, redStep
;
146 GLfixed green
, greenStep
;
147 GLfixed blue
, blueStep
;
148 GLfixed alpha
, alphaStep
;
149 GLfixed specRed
, specRedStep
;
150 GLfixed specGreen
, specGreenStep
;
151 GLfixed specBlue
, specBlueStep
;
153 GLfixed index
, indexStep
;
155 GLfloat fog
, fogStep
;
156 GLfloat tex
[MAX_TEXTURE_COORD_UNITS
][4]; /* s, t, r, q */
157 GLfloat texStepX
[MAX_TEXTURE_COORD_UNITS
][4];
158 GLfloat texStepY
[MAX_TEXTURE_COORD_UNITS
][4];
159 GLfixed intTex
[2], intTexStep
[2]; /* s, t only */
161 /* partial derivatives wrt X and Y. */
163 GLfloat w
, dwdx
, dwdy
;
168 GLfloat dsrdx
, dsrdy
;
169 GLfloat dsgdx
, dsgdy
;
170 GLfloat dsbdx
, dsbdy
;
171 GLfloat dfogdx
, dfogdy
;
174 * This bitmask (of \link SpanFlags SPAN_* flags\endlink) indicates
175 * which of the fragment arrays in the span_arrays struct are relevant.
180 * We store the arrays of fragment values in a separate struct so
181 * that we can allocate sw_span structs on the stack without using
182 * a lot of memory. The span_arrays struct is about 400KB while the
183 * sw_span struct is only about 512 bytes.
185 struct span_arrays
*array
;
189 #define INIT_SPAN(S, PRIMITIVE, END, INTERP_MASK, ARRAY_MASK) \
191 (S).primitive = (PRIMITIVE); \
192 (S).interpMask = (INTERP_MASK); \
193 (S).arrayMask = (ARRAY_MASK); \
197 (S).array = SWRAST_CONTEXT(ctx)->SpanArrays; \
201 typedef void (*texture_sample_func
)(GLcontext
*ctx
, GLuint texUnit
,
202 const struct gl_texture_object
*tObj
,
203 GLuint n
, const GLfloat texcoords
[][4],
204 const GLfloat lambda
[], GLchan rgba
[][4]);
206 typedef void (_ASMAPIP blend_func
)( GLcontext
*ctx
, GLuint n
,
207 const GLubyte mask
[],
208 GLchan src
[][4], CONST GLchan dst
[][4] );
210 typedef void (*swrast_point_func
)( GLcontext
*ctx
, const SWvertex
*);
212 typedef void (*swrast_line_func
)( GLcontext
*ctx
,
213 const SWvertex
*, const SWvertex
*);
215 typedef void (*swrast_tri_func
)( GLcontext
*ctx
, const SWvertex
*,
216 const SWvertex
*, const SWvertex
*);
219 /** \defgroup Bitmasks
220 * Bitmasks to indicate which rasterization options are enabled
224 #define ALPHATEST_BIT 0x001 /**< Alpha-test pixels */
225 #define BLEND_BIT 0x002 /**< Blend pixels */
226 #define DEPTH_BIT 0x004 /**< Depth-test pixels */
227 #define FOG_BIT 0x008 /**< Fog pixels */
228 #define LOGIC_OP_BIT 0x010 /**< Apply logic op in software */
229 #define CLIP_BIT 0x020 /**< Scissor or window clip pixels */
230 #define STENCIL_BIT 0x040 /**< Stencil pixels */
231 #define MASKING_BIT 0x080 /**< Do glColorMask or glIndexMask */
232 #define ALPHABUF_BIT 0x100 /**< Using software alpha buffer */
233 #define MULTI_DRAW_BIT 0x400 /**< Write to more than one color- */
234 /**< buffer or no buffers. */
235 #define OCCLUSION_BIT 0x800 /**< GL_HP_occlusion_test enabled */
236 #define TEXTURE_BIT 0x1000 /**< Texturing really enabled */
237 #define FRAGPROG_BIT 0x2000 /**< Fragment program enabled */
240 #define _SWRAST_NEW_RASTERMASK (_NEW_BUFFERS| \
258 /** Driver interface:
260 struct swrast_device_driver Driver
;
262 /** Configuration mechanisms to make software rasterizer match
263 * characteristics of the hardware rasterizer (if present):
265 GLboolean AllowVertexFog
;
266 GLboolean AllowPixelFog
;
268 /** Derived values, invalidated on statechanges, updated from
269 * _swrast_validate_derived():
272 GLfloat _MinMagThresh
[MAX_TEXTURE_IMAGE_UNITS
];
273 GLfloat _BackfaceSign
;
274 GLboolean _PreferPixelFog
;
275 GLboolean _AnyTextureCombine
;
277 /* Accum buffer temporaries.
279 GLboolean _IntegerAccumMode
; /**< Storing unscaled integers? */
280 GLfloat _IntegerAccumScaler
; /**< Implicit scale factor */
285 GLuint StippleCounter
; /**< Line stipple counter */
288 GLenum Primitive
; /* current primitive being drawn (ala glBegin) */
289 GLuint CurrentBuffer
; /* exactly one of FRONT_LEFT_BIT, BACK_LEFT_BIT, etc*/
291 /** Mechanism to allow driver (like X11) to register further
292 * software rasterization routines.
295 void (*choose_point
)( GLcontext
* );
296 void (*choose_line
)( GLcontext
* );
297 void (*choose_triangle
)( GLcontext
* );
299 GLuint invalidate_point
;
300 GLuint invalidate_line
;
301 GLuint invalidate_triangle
;
304 /** Function pointers for dispatch behind public entrypoints. */
306 void (*InvalidateState
)( GLcontext
*ctx
, GLuint new_state
);
308 swrast_point_func Point
;
309 swrast_line_func Line
;
310 swrast_tri_func Triangle
;
314 * Placeholders for when separate specular (or secondary color) is
315 * enabled but texturing is not.
318 swrast_point_func SpecPoint
;
319 swrast_line_func SpecLine
;
320 swrast_tri_func SpecTriangle
;
324 * Typically, we'll allocate a sw_span structure as a local variable
325 * and set its 'array' pointer to point to this object. The reason is
326 * this object is big and causes problems when allocated on the stack
329 struct span_arrays
*SpanArrays
;
332 * Used to buffer N GL_POINTS, instead of rendering one by one.
334 struct sw_span PointSpan
;
336 /** Internal hooks, kept uptodate by the same mechanism as above.
338 blend_func BlendFunc
;
339 texture_sample_func TextureSample
[MAX_TEXTURE_IMAGE_UNITS
];
341 /** Buffer for saving the sampled texture colors.
342 * Needed for GL_ARB_texture_env_crossbar implementation.
350 _swrast_validate_derived( GLcontext
*ctx
);
353 #define SWRAST_CONTEXT(ctx) ((SWcontext *)ctx->swrast_context)
355 #define RENDER_START(SWctx, GLctx) \
357 if ((SWctx)->Driver.SpanRenderStart) { \
358 (*(SWctx)->Driver.SpanRenderStart)(GLctx); \
362 #define RENDER_FINISH(SWctx, GLctx) \
364 if ((SWctx)->Driver.SpanRenderFinish) { \
365 (*(SWctx)->Driver.SpanRenderFinish)(GLctx); \
372 * XXX these macros are just bandages for now in order to make
373 * CHAN_BITS==32 compile cleanly.
374 * These should probably go elsewhere at some point.
376 #if CHAN_TYPE == GL_FLOAT
377 #define ChanToFixed(X) (X)
378 #define FixedToChan(X) (X)
380 #define ChanToFixed(X) IntToFixed(X)
381 #define FixedToChan(X) FixedToInt(X)