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