fix ugly bug triggered by macro expansion
[mesa.git] / src / mesa / swrast / s_context.h
1 /*
2 * Mesa 3-D graphics library
3 * Version: 5.1
4 *
5 * Copyright (C) 1999-2003 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 <keith@tungstengraphics.com>
30 */
31
32 #ifndef S_CONTEXT_H
33 #define S_CONTEXT_H
34
35 #include "mtypes.h"
36 #include "swrast.h"
37
38
39 /**
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)
44 */
45 /*@{*/
46 #define SPAN_RGBA 0x001
47 #define SPAN_SPEC 0x002
48 #define SPAN_INDEX 0x004
49 #define SPAN_Z 0x008
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 */
57 #define SPAN_XY 0x400
58 #define SPAN_MASK 0x800 /**< sw_span::arrayMask only */
59 /*@}*/
60
61
62 /**
63 * \struct span_arrays
64 * \brief Arrays of fragment values.
65 *
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.
69 */
70 struct span_arrays {
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 */
77 GLdepth z[MAX_WIDTH];
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];
82
83 /** This mask indicates if fragment is alive or culled */
84 GLubyte mask[MAX_WIDTH];
85 };
86
87
88 /**
89 * \struct sw_span
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
92 * drawn.
93 *
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.
99 *
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.
104 *
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.
109 */
110 struct sw_span {
111 GLint x, y;
112
113 /** Only need to process pixels between start <= i < end */
114 /** At this time, start is always zero. */
115 GLuint start, end;
116
117 /** This flag indicates that mask[] array is effectively filled with ones */
118 GLboolean writeAll;
119
120 /** either GL_POLYGON, GL_LINE, GL_POLYGON, GL_BITMAP */
121 GLenum primitive;
122
123 /** 0 = front-facing span, 1 = back-facing span (for two-sided stencil) */
124 GLuint facing;
125
126 /**
127 * This bitmask (of \link SpanFlags SPAN_* flags\endlink) indicates
128 * which of the x/xStep variables are relevant.
129 */
130 GLuint interpMask;
131
132 /* For horizontal spans, step is the partial derivative wrt X.
133 * For lines, step is the delta from one fragment to the next.
134 */
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;
151 #endif
152 GLfixed index, indexStep;
153 GLfixed z, zStep;
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 */
159
160 /* partial derivatives wrt X and Y. */
161 GLfloat dzdx, dzdy;
162 GLfloat w, dwdx, dwdy;
163 GLfloat drdx, drdy;
164 GLfloat dgdx, dgdy;
165 GLfloat dbdx, dbdy;
166 GLfloat dadx, dady;
167 GLfloat dsrdx, dsrdy;
168 GLfloat dsgdx, dsgdy;
169 GLfloat dsbdx, dsbdy;
170 GLfloat dfogdx, dfogdy;
171
172 /**
173 * This bitmask (of \link SpanFlags SPAN_* flags\endlink) indicates
174 * which of the fragment arrays in the span_arrays struct are relevant.
175 */
176 GLuint arrayMask;
177
178 /**
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.
183 */
184 struct span_arrays *array;
185 };
186
187
188 #define INIT_SPAN(S, PRIMITIVE, END, INTERP_MASK, ARRAY_MASK) \
189 do { \
190 (S).primitive = (PRIMITIVE); \
191 (S).interpMask = (INTERP_MASK); \
192 (S).arrayMask = (ARRAY_MASK); \
193 (S).start = 0; \
194 (S).end = (END); \
195 (S).facing = 0; \
196 (S).array = SWRAST_CONTEXT(ctx)->SpanArrays; \
197 } while (0)
198
199
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]);
204
205 typedef void (_ASMAPIP blend_func)( GLcontext *ctx, GLuint n,
206 const GLubyte mask[],
207 GLchan src[][4], CONST GLchan dst[][4] );
208
209 typedef void (*swrast_point_func)( GLcontext *ctx, const SWvertex *);
210
211 typedef void (*swrast_line_func)( GLcontext *ctx,
212 const SWvertex *, const SWvertex *);
213
214 typedef void (*swrast_tri_func)( GLcontext *ctx, const SWvertex *,
215 const SWvertex *, const SWvertex *);
216
217
218 /** \defgroup Bitmasks
219 * Bitmasks to indicate which rasterization options are enabled
220 * (RasterMask)
221 */
222 /*@{*/
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 */
237 /*@}*/
238
239 #define _SWRAST_NEW_RASTERMASK (_NEW_BUFFERS| \
240 _NEW_SCISSOR| \
241 _NEW_COLOR| \
242 _NEW_DEPTH| \
243 _NEW_FOG| \
244 _NEW_PROGRAM| \
245 _NEW_STENCIL| \
246 _NEW_TEXTURE| \
247 _NEW_VIEWPORT| \
248 _NEW_DEPTH)
249
250
251 /**
252 * \struct SWcontext
253 * \brief SWContext?
254 */
255 typedef struct
256 {
257 /** Driver interface:
258 */
259 struct swrast_device_driver Driver;
260
261 /** Configuration mechanisms to make software rasterizer match
262 * characteristics of the hardware rasterizer (if present):
263 */
264 GLboolean AllowVertexFog;
265 GLboolean AllowPixelFog;
266
267 /** Derived values, invalidated on statechanges, updated from
268 * _swrast_validate_derived():
269 */
270 GLuint _RasterMask;
271 GLfloat _MinMagThresh[MAX_TEXTURE_IMAGE_UNITS];
272 GLfloat _BackfaceSign;
273 GLboolean _PreferPixelFog;
274 GLboolean _AnyTextureCombine;
275
276 /* Accum buffer temporaries.
277 */
278 GLboolean _IntegerAccumMode; /**< Storing unscaled integers? */
279 GLfloat _IntegerAccumScaler; /**< Implicit scale factor */
280
281
282 /* Working values:
283 */
284 GLuint StippleCounter; /**< Line stipple counter */
285 GLuint NewState;
286 GLuint StateChanges;
287 GLenum Primitive; /* current primitive being drawn (ala glBegin) */
288 GLuint CurrentBuffer; /* exactly one of FRONT_LEFT_BIT, BACK_LEFT_BIT, etc*/
289
290 /** Mechanism to allow driver (like X11) to register further
291 * software rasterization routines.
292 */
293 /*@{*/
294 void (*choose_point)( GLcontext * );
295 void (*choose_line)( GLcontext * );
296 void (*choose_triangle)( GLcontext * );
297
298 GLuint invalidate_point;
299 GLuint invalidate_line;
300 GLuint invalidate_triangle;
301 /*@}*/
302
303 /** Function pointers for dispatch behind public entrypoints. */
304 /*@{*/
305 void (*InvalidateState)( GLcontext *ctx, GLuint new_state );
306
307 swrast_point_func Point;
308 swrast_line_func Line;
309 swrast_tri_func Triangle;
310 /*@}*/
311
312 /**
313 * Placeholders for when separate specular (or secondary color) is
314 * enabled but texturing is not.
315 */
316 /*@{*/
317 swrast_point_func SpecPoint;
318 swrast_line_func SpecLine;
319 swrast_tri_func SpecTriangle;
320 /*@}*/
321
322 /**
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
326 * on some systems.
327 */
328 struct span_arrays *SpanArrays;
329
330 /**
331 * Used to buffer N GL_POINTS, instead of rendering one by one.
332 */
333 struct sw_span PointSpan;
334
335 /** Internal hooks, kept uptodate by the same mechanism as above.
336 */
337 blend_func BlendFunc;
338 texture_sample_func TextureSample[MAX_TEXTURE_IMAGE_UNITS];
339
340 /** Buffer for saving the sampled texture colors.
341 * Needed for GL_ARB_texture_env_crossbar implementation.
342 */
343 GLchan *TexelBuffer;
344
345 } SWcontext;
346
347
348 extern void
349 _swrast_validate_derived( GLcontext *ctx );
350
351
352 #define SWRAST_CONTEXT(ctx) ((SWcontext *)ctx->swrast_context)
353
354 #define RENDER_START(SWctx, GLctx) \
355 do { \
356 if ((SWctx)->Driver.SpanRenderStart) { \
357 (*(SWctx)->Driver.SpanRenderStart)(GLctx); \
358 } \
359 } while (0)
360
361 #define RENDER_FINISH(SWctx, GLctx) \
362 do { \
363 if ((SWctx)->Driver.SpanRenderFinish) { \
364 (*(SWctx)->Driver.SpanRenderFinish)(GLctx); \
365 } \
366 } while (0)
367
368
369
370 /*
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.
374 */
375 #if CHAN_TYPE == GL_FLOAT
376 #define ChanToFixed(X) (X)
377 #define FixedToChan(X) (X)
378 #else
379 #define ChanToFixed(X) IntToFixed(X)
380 #define FixedToChan(X) FixedToInt(X)
381 #endif
382
383 #endif