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