move a comment
[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 #ifdef USE_MMX_ASM
206 typedef void (_ASMAPIP blend_func)( GLcontext *ctx, GLuint n,
207 const GLubyte mask[],
208 GLchan src[][4], CONST GLchan dst[][4] );
209 #else
210 typedef void (*blend_func)( GLcontext *ctx, GLuint n, const GLubyte mask[],
211 GLchan src[][4], CONST GLchan dst[][4] );
212 #endif
213
214 typedef void (*swrast_point_func)( GLcontext *ctx, const SWvertex *);
215
216 typedef void (*swrast_line_func)( GLcontext *ctx,
217 const SWvertex *, const SWvertex *);
218
219 typedef void (*swrast_tri_func)( GLcontext *ctx, const SWvertex *,
220 const SWvertex *, const SWvertex *);
221
222
223 /** \defgroup Bitmasks
224 * Bitmasks to indicate which rasterization options are enabled
225 * (RasterMask)
226 */
227 /*@{*/
228 #define ALPHATEST_BIT 0x001 /**< Alpha-test pixels */
229 #define BLEND_BIT 0x002 /**< Blend pixels */
230 #define DEPTH_BIT 0x004 /**< Depth-test pixels */
231 #define FOG_BIT 0x008 /**< Fog pixels */
232 #define LOGIC_OP_BIT 0x010 /**< Apply logic op in software */
233 #define CLIP_BIT 0x020 /**< Scissor or window clip pixels */
234 #define STENCIL_BIT 0x040 /**< Stencil pixels */
235 #define MASKING_BIT 0x080 /**< Do glColorMask or glIndexMask */
236 #define ALPHABUF_BIT 0x100 /**< Using software alpha buffer */
237 #define MULTI_DRAW_BIT 0x400 /**< Write to more than one color- */
238 /**< buffer or no buffers. */
239 #define OCCLUSION_BIT 0x800 /**< GL_HP_occlusion_test enabled */
240 #define TEXTURE_BIT 0x1000 /**< Texturing really enabled */
241 #define FRAGPROG_BIT 0x2000 /**< Fragment program enabled */
242 /*@}*/
243
244 #define _SWRAST_NEW_RASTERMASK (_NEW_BUFFERS| \
245 _NEW_SCISSOR| \
246 _NEW_COLOR| \
247 _NEW_DEPTH| \
248 _NEW_FOG| \
249 _NEW_PROGRAM| \
250 _NEW_STENCIL| \
251 _NEW_TEXTURE| \
252 _NEW_VIEWPORT| \
253 _NEW_DEPTH)
254
255
256 /**
257 * \struct SWcontext
258 * \brief SWContext?
259 */
260 typedef struct
261 {
262 /** Driver interface:
263 */
264 struct swrast_device_driver Driver;
265
266 /** Configuration mechanisms to make software rasterizer match
267 * characteristics of the hardware rasterizer (if present):
268 */
269 GLboolean AllowVertexFog;
270 GLboolean AllowPixelFog;
271
272 /** Derived values, invalidated on statechanges, updated from
273 * _swrast_validate_derived():
274 */
275 GLuint _RasterMask;
276 GLfloat _MinMagThresh[MAX_TEXTURE_IMAGE_UNITS];
277 GLfloat _backface_sign;
278 GLboolean _PreferPixelFog;
279 GLboolean _AnyTextureCombine;
280
281 /* Accum buffer temporaries.
282 */
283 GLboolean _IntegerAccumMode; /**< Storing unscaled integers? */
284 GLfloat _IntegerAccumScaler; /**< Implicit scale factor */
285
286
287 /* Working values:
288 */
289 GLuint StippleCounter; /**< Line stipple counter */
290 GLuint NewState;
291 GLuint StateChanges;
292 GLenum Primitive; /* current primitive being drawn (ala glBegin) */
293 GLuint CurrentBuffer; /* exactly one of FRONT_LEFT_BIT, BACK_LEFT_BIT, etc*/
294
295 /** Mechanism to allow driver (like X11) to register further
296 * software rasterization routines.
297 */
298 /*@{*/
299 void (*choose_point)( GLcontext * );
300 void (*choose_line)( GLcontext * );
301 void (*choose_triangle)( GLcontext * );
302
303 GLuint invalidate_point;
304 GLuint invalidate_line;
305 GLuint invalidate_triangle;
306 /*@}*/
307
308 /** Function pointers for dispatch behind public entrypoints. */
309 /*@{*/
310 void (*InvalidateState)( GLcontext *ctx, GLuint new_state );
311
312 swrast_point_func Point;
313 swrast_line_func Line;
314 swrast_tri_func Triangle;
315 /*@}*/
316
317 /**
318 * Placeholders for when separate specular (or secondary color) is
319 * enabled but texturing is not.
320 */
321 /*@{*/
322 swrast_point_func SpecPoint;
323 swrast_line_func SpecLine;
324 swrast_tri_func SpecTriangle;
325 /*@}*/
326
327 /**
328 * Typically, we'll allocate a sw_span structure as a local variable
329 * and set its 'array' pointer to point to this object. The reason is
330 * this object is big and causes problems when allocated on the stack
331 * on some systems.
332 */
333 struct span_arrays *SpanArrays;
334
335 /**
336 * Used to buffer N GL_POINTS, instead of rendering one by one.
337 */
338 struct sw_span PointSpan;
339
340 /** Internal hooks, kept uptodate by the same mechanism as above.
341 */
342 blend_func BlendFunc;
343 texture_sample_func TextureSample[MAX_TEXTURE_IMAGE_UNITS];
344
345 /** Buffer for saving the sampled texture colors.
346 * Needed for GL_ARB_texture_env_crossbar implementation.
347 */
348 GLchan *TexelBuffer;
349
350 } SWcontext;
351
352
353 extern void
354 _swrast_validate_derived( GLcontext *ctx );
355
356
357 #define SWRAST_CONTEXT(ctx) ((SWcontext *)ctx->swrast_context)
358
359 #define RENDER_START(SWctx, GLctx) \
360 do { \
361 if ((SWctx)->Driver.SpanRenderStart) { \
362 (*(SWctx)->Driver.SpanRenderStart)(GLctx); \
363 } \
364 } while (0)
365
366 #define RENDER_FINISH(SWctx, GLctx) \
367 do { \
368 if ((SWctx)->Driver.SpanRenderFinish) { \
369 (*(SWctx)->Driver.SpanRenderFinish)(GLctx); \
370 } \
371 } while (0)
372
373
374
375 /*
376 * XXX these macros are just bandages for now in order to make
377 * CHAN_BITS==32 compile cleanly.
378 * These should probably go elsewhere at some point.
379 */
380 #if CHAN_TYPE == GL_FLOAT
381 #define ChanToFixed(X) (X)
382 #define FixedToChan(X) (X)
383 #else
384 #define ChanToFixed(X) IntToFixed(X)
385 #define FixedToChan(X) FixedToInt(X)
386 #endif
387
388 #endif