c9f6ec6bb8f2346b4f0e23b18f21a4fd4ad5d0c3
[mesa.git] / src / mesa / swrast / s_context.h
1 /*
2 * Mesa 3-D graphics library
3 * Version: 6.5.2
4 *
5 * Copyright (C) 1999-2006 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 sw_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 #define SPAN_VARYING 0x2000
70 /*@}*/
71
72 #if 0
73 /* alternate arrangement for code below */
74 struct arrays2 {
75 union {
76 GLubyte sz1[MAX_WIDTH][4]; /* primary color */
77 GLushort sz2[MAX_WIDTH][4];
78 GLfloat sz4[MAX_WIDTH][4];
79 } rgba;
80 union {
81 GLubyte sz1[MAX_WIDTH][4]; /* specular color and temp storage */
82 GLushort sz2[MAX_WIDTH][4];
83 GLfloat sz4[MAX_WIDTH][4];
84 } spec;
85 };
86 #endif
87
88
89
90 /**
91 * \sw_span_arrays
92 * \brief Arrays of fragment values.
93 *
94 * These will either be computed from the span x/xStep values or
95 * filled in by glDraw/CopyPixels, etc.
96 * These arrays are separated out of sw_span to conserve memory.
97 */
98 typedef struct sw_span_arrays {
99 GLenum ChanType; /**< Color channel type, GL_UNSIGNED_BYTE, GL_FLOAT */
100 union {
101 struct {
102 GLubyte rgba[MAX_WIDTH][4]; /**< primary color */
103 GLubyte spec[MAX_WIDTH][4]; /**< specular color and temp storage */
104 } sz1;
105 struct {
106 GLushort rgba[MAX_WIDTH][4];
107 GLushort spec[MAX_WIDTH][4];
108 } sz2;
109 struct {
110 GLfloat rgba[MAX_WIDTH][4];
111 GLfloat spec[MAX_WIDTH][4];
112 } sz4;
113 } color;
114 /** XXX these are temporary fields, pointing into above color arrays */
115 GLchan (*rgba)[4];
116 GLchan (*spec)[4];
117
118 #if 0
119 /* XXX rearrange and unify these arrays to so that we can
120 * index all fragment inputs with the FRAG_ATTRIB_* values:
121 */
122 GLfloat attribs[FRAG_ATTRIB_MAX][MAX_WIDTH][4];
123 /*OR*/
124 typedef GLfloat (*array4f)[4];
125 array4f attribs[FRAG_ATTRIB_MAX];
126 #endif
127
128 GLint x[MAX_WIDTH]; /**< fragment X coords */
129 GLint y[MAX_WIDTH]; /**< fragment Y coords */
130 GLuint z[MAX_WIDTH]; /**< fragment Z coords */
131 GLuint index[MAX_WIDTH]; /**< Color indexes */
132 GLfloat fog[MAX_WIDTH];
133 GLfloat texcoords[MAX_TEXTURE_COORD_UNITS][MAX_WIDTH][4];
134 GLfloat lambda[MAX_TEXTURE_COORD_UNITS][MAX_WIDTH];
135 GLfloat coverage[MAX_WIDTH]; /**< Fragment coverage for AA/smoothing */
136 GLfloat varying[MAX_VARYING][MAX_WIDTH][4]; /**< For shaders */
137
138 /** This mask indicates which fragments are alive or culled */
139 GLubyte mask[MAX_WIDTH];
140 } SWspanarrays;
141
142
143 /**
144 * The SWspan structure describes the colors, Z, fogcoord, texcoords,
145 * etc for either a horizontal run or an array of independent pixels.
146 * We can either specify a base/step to indicate interpolated values, or
147 * fill in explicit arrays of values. The interpMask and arrayMask bitfields
148 * indicate which attributes are active interpolants or arrays, respectively.
149 *
150 * It would be interesting to experiment with multiprocessor rasterization
151 * with this structure. The triangle rasterizer could simply emit a
152 * stream of these structures which would be consumed by one or more
153 * span-processing threads which could run in parallel.
154 */
155 typedef struct sw_span {
156 GLint x, y;
157
158 /** Only need to process pixels between start <= i < end */
159 /** At this time, start is always zero. */
160 GLuint start, end;
161
162 /** This flag indicates that mask[] array is effectively filled with ones */
163 GLboolean writeAll;
164
165 /** either GL_POLYGON, GL_LINE, GL_POLYGON, GL_BITMAP */
166 GLenum primitive;
167
168 /** 0 = front-facing span, 1 = back-facing span (for two-sided stencil) */
169 GLuint facing;
170
171 /**
172 * This bitmask (of \link SpanFlags SPAN_* flags\endlink) indicates
173 * which of the x/xStep variables are relevant.
174 */
175 GLbitfield interpMask;
176
177 /* For horizontal spans, step is the partial derivative wrt X.
178 * For lines, step is the delta from one fragment to the next.
179 */
180 #if CHAN_TYPE == GL_FLOAT
181 GLfloat red, redStep;
182 GLfloat green, greenStep;
183 GLfloat blue, blueStep;
184 GLfloat alpha, alphaStep;
185 GLfloat specRed, specRedStep;
186 GLfloat specGreen, specGreenStep;
187 GLfloat specBlue, specBlueStep;
188 #else /* CHAN_TYPE == GL_UNSIGNED_BYTE or GL_UNSIGNED_SHORT */
189 GLfixed red, redStep;
190 GLfixed green, greenStep;
191 GLfixed blue, blueStep;
192 GLfixed alpha, alphaStep;
193 GLfixed specRed, specRedStep;
194 GLfixed specGreen, specGreenStep;
195 GLfixed specBlue, specBlueStep;
196 #endif
197 GLfixed index, indexStep;
198 GLfixed z, zStep; /* XXX z should probably be GLuint */
199 GLfloat fog, fogStep;
200 GLfloat tex[MAX_TEXTURE_COORD_UNITS][4]; /* s, t, r, q */
201 GLfloat texStepX[MAX_TEXTURE_COORD_UNITS][4];
202 GLfloat texStepY[MAX_TEXTURE_COORD_UNITS][4];
203 GLfixed intTex[2], intTexStep[2]; /* s, t only */
204 GLfloat var[MAX_VARYING][4];
205 GLfloat varStepX[MAX_VARYING][4];
206 GLfloat varStepY[MAX_VARYING][4];
207
208 /* partial derivatives wrt X and Y. */
209 GLfloat dzdx, dzdy;
210 GLfloat w, dwdx, dwdy;
211 GLfloat drdx, drdy;
212 GLfloat dgdx, dgdy;
213 GLfloat dbdx, dbdy;
214 GLfloat dadx, dady;
215 GLfloat dsrdx, dsrdy;
216 GLfloat dsgdx, dsgdy;
217 GLfloat dsbdx, dsbdy;
218 GLfloat dfogdx, dfogdy;
219
220 /**
221 * This bitmask (of \link SpanFlags SPAN_* flags\endlink) indicates
222 * which of the fragment arrays in the span_arrays struct are relevant.
223 */
224 GLbitfield arrayMask;
225
226 /**
227 * We store the arrays of fragment values in a separate struct so
228 * that we can allocate sw_span structs on the stack without using
229 * a lot of memory. The span_arrays struct is about 1.4MB while the
230 * sw_span struct is only about 512 bytes.
231 */
232 SWspanarrays *array;
233 } SWspan;
234
235
236
237 #define INIT_SPAN(S, PRIMITIVE, END, INTERP_MASK, ARRAY_MASK) \
238 do { \
239 (S).primitive = (PRIMITIVE); \
240 (S).interpMask = (INTERP_MASK); \
241 (S).arrayMask = (ARRAY_MASK); \
242 (S).start = 0; \
243 (S).end = (END); \
244 (S).facing = 0; \
245 (S).array = SWRAST_CONTEXT(ctx)->SpanArrays; \
246 } while (0)
247
248
249 typedef void (*texture_sample_func)(GLcontext *ctx,
250 const struct gl_texture_object *tObj,
251 GLuint n, const GLfloat texcoords[][4],
252 const GLfloat lambda[], GLchan rgba[][4]);
253
254 typedef void (_ASMAPIP blend_func)( GLcontext *ctx, GLuint n,
255 const GLubyte mask[],
256 GLvoid *src, const GLvoid *dst,
257 GLenum chanType);
258
259 typedef void (*swrast_point_func)( GLcontext *ctx, const SWvertex *);
260
261 typedef void (*swrast_line_func)( GLcontext *ctx,
262 const SWvertex *, const SWvertex *);
263
264 typedef void (*swrast_tri_func)( GLcontext *ctx, const SWvertex *,
265 const SWvertex *, const SWvertex *);
266
267
268 typedef void (*validate_texture_image_func)(GLcontext *ctx,
269 struct gl_texture_object *texObj,
270 GLuint face, GLuint level);
271
272
273 /** \defgroup Bitmasks
274 * Bitmasks to indicate which rasterization options are enabled
275 * (RasterMask)
276 */
277 /*@{*/
278 #define ALPHATEST_BIT 0x001 /**< Alpha-test pixels */
279 #define BLEND_BIT 0x002 /**< Blend pixels */
280 #define DEPTH_BIT 0x004 /**< Depth-test pixels */
281 #define FOG_BIT 0x008 /**< Fog pixels */
282 #define LOGIC_OP_BIT 0x010 /**< Apply logic op in software */
283 #define CLIP_BIT 0x020 /**< Scissor or window clip pixels */
284 #define STENCIL_BIT 0x040 /**< Stencil pixels */
285 #define MASKING_BIT 0x080 /**< Do glColorMask or glIndexMask */
286 #define MULTI_DRAW_BIT 0x400 /**< Write to more than one color- */
287 /**< buffer or no buffers. */
288 #define OCCLUSION_BIT 0x800 /**< GL_HP_occlusion_test enabled */
289 #define TEXTURE_BIT 0x1000 /**< Texturing really enabled */
290 #define FRAGPROG_BIT 0x2000 /**< Fragment program enabled */
291 #define ATIFRAGSHADER_BIT 0x4000 /**< ATI Fragment shader enabled */
292 #define CLAMPING_BIT 0x8000 /**< Clamp colors to [0,1] */
293 /*@}*/
294
295 #define _SWRAST_NEW_RASTERMASK (_NEW_BUFFERS| \
296 _NEW_SCISSOR| \
297 _NEW_COLOR| \
298 _NEW_DEPTH| \
299 _NEW_FOG| \
300 _NEW_PROGRAM| \
301 _NEW_STENCIL| \
302 _NEW_TEXTURE| \
303 _NEW_VIEWPORT| \
304 _NEW_DEPTH)
305
306
307 /**
308 * \struct SWcontext
309 * \brief Per-context state that's private to the software rasterizer module.
310 */
311 typedef struct
312 {
313 /** Driver interface:
314 */
315 struct swrast_device_driver Driver;
316
317 /** Configuration mechanisms to make software rasterizer match
318 * characteristics of the hardware rasterizer (if present):
319 */
320 GLboolean AllowVertexFog;
321 GLboolean AllowPixelFog;
322
323 /** Derived values, invalidated on statechanges, updated from
324 * _swrast_validate_derived():
325 */
326 GLbitfield _RasterMask;
327 GLfloat _BackfaceSign;
328 GLboolean _PreferPixelFog; /* Compute fog blend factor per fragment? */
329 GLboolean _AnyTextureCombine;
330 GLboolean _FogEnabled;
331 GLenum _FogMode; /* either GL_FOG_MODE or fragment program's fog mode */
332
333 /* Accum buffer temporaries.
334 */
335 GLboolean _IntegerAccumMode; /**< Storing unscaled integers? */
336 GLfloat _IntegerAccumScaler; /**< Implicit scale factor */
337
338 /* Working values:
339 */
340 GLuint StippleCounter; /**< Line stipple counter */
341 GLbitfield NewState;
342 GLuint StateChanges;
343 GLenum Primitive; /* current primitive being drawn (ala glBegin) */
344
345 void (*InvalidateState)( GLcontext *ctx, GLbitfield new_state );
346
347 /**
348 * When the NewState mask intersects these masks, we invalidate the
349 * Point/Line/Triangle function pointers below.
350 */
351 /*@{*/
352 GLbitfield InvalidatePointMask;
353 GLbitfield InvalidateLineMask;
354 GLbitfield InvalidateTriangleMask;
355 /*@}*/
356
357 /**
358 * Device drivers plug in functions for these callbacks.
359 * Will be called when the GL state change mask intersects the above masks.
360 */
361 /*@{*/
362 void (*choose_point)( GLcontext * );
363 void (*choose_line)( GLcontext * );
364 void (*choose_triangle)( GLcontext * );
365 /*@}*/
366
367 /**
368 * Current point, line and triangle drawing functions.
369 */
370 /*@{*/
371 swrast_point_func Point;
372 swrast_line_func Line;
373 swrast_tri_func Triangle;
374 /*@}*/
375
376 /**
377 * Placeholders for when separate specular (or secondary color) is
378 * enabled but texturing is not.
379 */
380 /*@{*/
381 swrast_point_func SpecPoint;
382 swrast_line_func SpecLine;
383 swrast_tri_func SpecTriangle;
384 /*@}*/
385
386 /**
387 * Typically, we'll allocate a sw_span structure as a local variable
388 * and set its 'array' pointer to point to this object. The reason is
389 * this object is big and causes problems when allocated on the stack
390 * on some systems.
391 */
392 SWspanarrays *SpanArrays;
393
394 /**
395 * Used to buffer N GL_POINTS, instead of rendering one by one.
396 */
397 SWspan PointSpan;
398
399 /** Internal hooks, kept up to date by the same mechanism as above.
400 */
401 blend_func BlendFunc;
402 texture_sample_func TextureSample[MAX_TEXTURE_IMAGE_UNITS];
403
404 /** Buffer for saving the sampled texture colors.
405 * Needed for GL_ARB_texture_env_crossbar implementation.
406 */
407 GLchan *TexelBuffer;
408
409 validate_texture_image_func ValidateTextureImage;
410
411 } SWcontext;
412
413
414 extern void
415 _swrast_validate_derived( GLcontext *ctx );
416
417
418 #define SWRAST_CONTEXT(ctx) ((SWcontext *)ctx->swrast_context)
419
420 #define RENDER_START(SWctx, GLctx) \
421 do { \
422 if ((SWctx)->Driver.SpanRenderStart) { \
423 (*(SWctx)->Driver.SpanRenderStart)(GLctx); \
424 } \
425 } while (0)
426
427 #define RENDER_FINISH(SWctx, GLctx) \
428 do { \
429 if ((SWctx)->Driver.SpanRenderFinish) { \
430 (*(SWctx)->Driver.SpanRenderFinish)(GLctx); \
431 } \
432 } while (0)
433
434
435
436 /**
437 * Size of an RGBA pixel, in bytes, for given datatype.
438 */
439 #define RGBA_PIXEL_SIZE(TYPE) \
440 ((TYPE == GL_UNSIGNED_BYTE) ? 4 * sizeof(GLubyte) : \
441 ((TYPE == GL_UNSIGNED_SHORT) ? 4 * sizeof(GLushort) \
442 : 4 * sizeof(GLfloat)))
443
444
445
446 /*
447 * XXX these macros are just bandages for now in order to make
448 * CHAN_BITS==32 compile cleanly.
449 * These should probably go elsewhere at some point.
450 */
451 #if CHAN_TYPE == GL_FLOAT
452 #define ChanToFixed(X) (X)
453 #define FixedToChan(X) (X)
454 #else
455 #define ChanToFixed(X) IntToFixed(X)
456 #define FixedToChan(X) FixedToInt(X)
457 #endif
458
459 #endif