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