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