2be1545c8f33c09edeb947afcf33416dba1534a5
[mesa.git] / src / mesa / swrast / s_context.h
1 /* $Id: s_context.h,v 1.24 2003/02/23 04:10:54 brianp Exp $ */
2
3 /*
4 * Mesa 3-D graphics library
5 * Version: 5.1
6 *
7 * Copyright (C) 1999-2003 Brian Paul All Rights Reserved.
8 *
9 * Permission is hereby granted, free of charge, to any person obtaining a
10 * copy of this software and associated documentation files (the "Software"),
11 * to deal in the Software without restriction, including without limitation
12 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
13 * and/or sell copies of the Software, and to permit persons to whom the
14 * Software is furnished to do so, subject to the following conditions:
15 *
16 * The above copyright notice and this permission notice shall be included
17 * in all copies or substantial portions of the Software.
18 *
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
20 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
22 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
23 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
24 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 */
26
27
28 /**
29 * \file swrast/s_context.h
30 * \brief Software rasterization context and private types.
31 * \author Keith Whitwell <keith@tungstengraphics.com>
32 */
33
34 #ifndef S_CONTEXT_H
35 #define S_CONTEXT_H
36
37 #include "mtypes.h"
38 #include "swrast.h"
39
40
41 /**
42 * \struct sw_span
43 * \brief Contains data for either a horizontal line or a set of
44 * pixels that are passed through a pipeline of functions before being
45 * drawn.
46 *
47 * The sw_span structure describes the colors, Z, fogcoord, texcoords,
48 * etc for either a horizontal run or a set of independent pixels. We
49 * can either specify a base/step to indicate interpolated values, or
50 * fill in arrays of values. The interpMask and arrayMask bitfields
51 * indicate which are active.
52 *
53 * With this structure it's easy to hand-off span rasterization to
54 * subroutines instead of doing it all inline in the triangle functions
55 * like we used to do.
56 * It also cleans up the local variable namespace a great deal.
57 *
58 * It would be interesting to experiment with multiprocessor rasterization
59 * with this structure. The triangle rasterizer could simply emit a
60 * stream of these structures which would be consumed by one or more
61 * span-processing threads which could run in parallel.
62 */
63
64
65 /**
66 * \defgroup SpanFlags SPAN_XXX-flags
67 * Bitmasks to indicate which span_arrays need to be computed
68 * (sw_span::interpMask) or have already been filled
69 * (sw_span::arrayMask)
70 */
71 /*@{*/
72 #define SPAN_RGBA 0x001
73 #define SPAN_SPEC 0x002
74 #define SPAN_INDEX 0x004
75 #define SPAN_Z 0x008
76 #define SPAN_FOG 0x010
77 #define SPAN_TEXTURE 0x020
78 #define SPAN_INT_TEXTURE 0x040
79 #define SPAN_LAMBDA 0x080
80 #define SPAN_COVERAGE 0x100
81 #define SPAN_FLAT 0x200 /**< flat shading? */
82 /** sw_span::arrayMask only - for span_arrays::x, span_arrays::y */
83 #define SPAN_XY 0x400
84 #define SPAN_MASK 0x800 /**< sw_span::arrayMask only */
85 /*@}*/
86
87
88 /**
89 * \struct span_arrays
90 * \brief Arrays of fragment values.
91 *
92 * These will either be computed from the x/xStep values above or
93 * filled in by glDraw/CopyPixels, etc.
94 * These arrays are separated out of sw_span to conserve memory.
95 */
96 struct span_arrays {
97 GLchan rgb[MAX_WIDTH][3];
98 GLchan rgba[MAX_WIDTH][4];
99 GLuint index[MAX_WIDTH];
100 GLchan spec[MAX_WIDTH][4]; /* specular color */
101 GLint x[MAX_WIDTH]; /**< X/Y used for point/line rendering only */
102 GLint y[MAX_WIDTH]; /**< X/Y used for point/line rendering only */
103 GLdepth z[MAX_WIDTH];
104 GLfloat fog[MAX_WIDTH];
105 GLfloat texcoords[MAX_TEXTURE_COORD_UNITS][MAX_WIDTH][4];
106 GLfloat lambda[MAX_TEXTURE_COORD_UNITS][MAX_WIDTH];
107 GLfloat coverage[MAX_WIDTH];
108
109 /** This mask indicates if fragment is alive or culled */
110 GLubyte mask[MAX_WIDTH];
111 };
112
113
114 struct sw_span {
115 GLint x, y;
116
117 /** Only need to process pixels between start <= i < end */
118 /** At this time, start is always zero. */
119 GLuint start, end;
120
121 /** This flag indicates that mask[] array is effectively filled with ones */
122 GLboolean writeAll;
123
124 /** either GL_POLYGON, GL_LINE, GL_POLYGON, GL_BITMAP */
125 GLenum primitive;
126
127 /** 0 = front-facing span, 1 = back-facing span (for two-sided stencil) */
128 GLuint facing;
129
130 /**
131 * This bitmask (of \link SpanFlags SPAN_* flags\endlink) indicates
132 * which of the x/xStep variables are relevant.
133 */
134 GLuint interpMask;
135
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;
152 #endif
153 GLfixed index, indexStep;
154 GLfixed z, zStep;
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 */
160
161 /**
162 * This bitmask (of \link SpanFlags SPAN_* flags\endlink) indicates
163 * which of the fragment arrays in the span_arrays struct are relevant.
164 */
165 GLuint arrayMask;
166
167 /**
168 * We store the arrays of fragment values in a separate struct so
169 * that we can allocate sw_span structs on the stack without using
170 * a lot of memory. The span_arrays struct is about 400KB while the
171 * sw_span struct is only about 512 bytes.
172 */
173 struct span_arrays *array;
174 };
175
176
177 #define INIT_SPAN(S, PRIMITIVE, END, INTERP_MASK, ARRAY_MASK) \
178 do { \
179 (S).primitive = (PRIMITIVE); \
180 (S).interpMask = (INTERP_MASK); \
181 (S).arrayMask = (ARRAY_MASK); \
182 (S).start = 0; \
183 (S).end = (END); \
184 (S).facing = 0; \
185 (S).array = SWRAST_CONTEXT(ctx)->SpanArrays; \
186 } while (0)
187
188
189 typedef void (*texture_sample_func)(GLcontext *ctx, GLuint texUnit,
190 const struct gl_texture_object *tObj,
191 GLuint n, const GLfloat texcoords[][4],
192 const GLfloat lambda[], GLchan rgba[][4]);
193
194 #ifdef USE_MMX_ASM
195 typedef void (_ASMAPIP blend_func)( GLcontext *ctx, GLuint n,
196 const GLubyte mask[],
197 GLchan src[][4], CONST GLchan dst[][4] );
198 #else
199 typedef void (*blend_func)( GLcontext *ctx, GLuint n, const GLubyte mask[],
200 GLchan src[][4], CONST GLchan dst[][4] );
201 #endif
202
203 typedef void (*swrast_point_func)( GLcontext *ctx, const SWvertex *);
204
205 typedef void (*swrast_line_func)( GLcontext *ctx,
206 const SWvertex *, const SWvertex *);
207
208 typedef void (*swrast_tri_func)( GLcontext *ctx, const SWvertex *,
209 const SWvertex *, const SWvertex *);
210
211
212 /** \defgroup Bitmasks
213 * Bitmasks to indicate which rasterization options are enabled
214 * (RasterMask)
215 */
216 /*@{*/
217 #define ALPHATEST_BIT 0x001 /**< Alpha-test pixels */
218 #define BLEND_BIT 0x002 /**< Blend pixels */
219 #define DEPTH_BIT 0x004 /**< Depth-test pixels */
220 #define FOG_BIT 0x008 /**< Fog pixels */
221 #define LOGIC_OP_BIT 0x010 /**< Apply logic op in software */
222 #define CLIP_BIT 0x020 /**< Scissor or window clip pixels */
223 #define STENCIL_BIT 0x040 /**< Stencil pixels */
224 #define MASKING_BIT 0x080 /**< Do glColorMask or glIndexMask */
225 #define ALPHABUF_BIT 0x100 /**< Using software alpha buffer */
226 #define MULTI_DRAW_BIT 0x400 /**< Write to more than one color- */
227 /**< buffer or no buffers. */
228 #define OCCLUSION_BIT 0x800 /**< GL_HP_occlusion_test enabled */
229 #define TEXTURE_BIT 0x1000 /**< Texturing really enabled */
230 /*@}*/
231
232 #define _SWRAST_NEW_RASTERMASK (_NEW_BUFFERS| \
233 _NEW_SCISSOR| \
234 _NEW_COLOR| \
235 _NEW_DEPTH| \
236 _NEW_FOG| \
237 _NEW_STENCIL| \
238 _NEW_TEXTURE| \
239 _NEW_VIEWPORT| \
240 _NEW_DEPTH)
241
242
243 /**
244 * \struct SWcontext
245 * \brief SWContext?
246 */
247 typedef struct
248 {
249 /** Driver interface:
250 */
251 struct swrast_device_driver Driver;
252
253 /** Configuration mechanisms to make software rasterizer match
254 * characteristics of the hardware rasterizer (if present):
255 */
256 GLboolean AllowVertexFog;
257 GLboolean AllowPixelFog;
258
259 /** Derived values, invalidated on statechanges, updated from
260 * _swrast_validate_derived():
261 */
262 GLuint _RasterMask;
263 GLfloat _MinMagThresh[MAX_TEXTURE_IMAGE_UNITS];
264 GLfloat _backface_sign;
265 GLboolean _PreferPixelFog;
266 GLboolean _AnyTextureCombine;
267
268 /* Accum buffer temporaries.
269 */
270 GLboolean _IntegerAccumMode; /**< Storing unscaled integers? */
271 GLfloat _IntegerAccumScaler; /**< Implicit scale factor */
272
273
274 /* Working values:
275 */
276 GLuint StippleCounter; /**< Line stipple counter */
277 GLuint NewState;
278 GLuint StateChanges;
279 GLenum Primitive; /* current primitive being drawn (ala glBegin) */
280 GLuint CurrentBuffer; /* exactly one of FRONT_LEFT_BIT, BACK_LEFT_BIT, etc*/
281
282 /** Mechanism to allow driver (like X11) to register further
283 * software rasterization routines.
284 */
285 /*@{*/
286 void (*choose_point)( GLcontext * );
287 void (*choose_line)( GLcontext * );
288 void (*choose_triangle)( GLcontext * );
289
290 GLuint invalidate_point;
291 GLuint invalidate_line;
292 GLuint invalidate_triangle;
293 /*@}*/
294
295 /** Function pointers for dispatch behind public entrypoints. */
296 /*@{*/
297 void (*InvalidateState)( GLcontext *ctx, GLuint new_state );
298
299 swrast_point_func Point;
300 swrast_line_func Line;
301 swrast_tri_func Triangle;
302 /*@}*/
303
304 /**
305 * Placeholders for when separate specular (or secondary color) is
306 * enabled but texturing is not.
307 */
308 /*@{*/
309 swrast_point_func SpecPoint;
310 swrast_line_func SpecLine;
311 swrast_tri_func SpecTriangle;
312 /*@}*/
313
314 /**
315 * Typically, we'll allocate a sw_span structure as a local variable
316 * and set its 'array' pointer to point to this object. The reason is
317 * this object is big and causes problems when allocated on the stack
318 * on some systems.
319 */
320 struct span_arrays *SpanArrays;
321
322 /**
323 * Used to buffer N GL_POINTS, instead of rendering one by one.
324 */
325 struct sw_span PointSpan;
326
327 /** Internal hooks, kept uptodate by the same mechanism as above.
328 */
329 blend_func BlendFunc;
330 texture_sample_func TextureSample[MAX_TEXTURE_IMAGE_UNITS];
331
332 /** Buffer for saving the sampled texture colors.
333 * Needed for GL_ARB_texture_env_crossbar implementation.
334 */
335 GLchan *TexelBuffer;
336
337 } SWcontext;
338
339
340 extern void
341 _swrast_validate_derived( GLcontext *ctx );
342
343
344 #define SWRAST_CONTEXT(ctx) ((SWcontext *)ctx->swrast_context)
345
346 #define RENDER_START(SWctx, GLctx) \
347 do { \
348 if ((SWctx)->Driver.SpanRenderStart) { \
349 (*(SWctx)->Driver.SpanRenderStart)(GLctx); \
350 } \
351 } while (0)
352
353 #define RENDER_FINISH(SWctx, GLctx) \
354 do { \
355 if ((SWctx)->Driver.SpanRenderFinish) { \
356 (*(SWctx)->Driver.SpanRenderFinish)(GLctx); \
357 } \
358 } while (0)
359
360
361
362 /*
363 * XXX these macros are just bandages for now in order to make
364 * CHAN_BITS==32 compile cleanly.
365 * These should probably go elsewhere at some point.
366 */
367 #if CHAN_TYPE == GL_FLOAT
368 #define ChanToFixed(X) (X)
369 #define FixedToChan(X) (X)
370 #else
371 #define ChanToFixed(X) IntToFixed(X)
372 #define FixedToChan(X) FixedToInt(X)
373 #endif
374
375 #endif