swrast: introduce new swrast_texture_image struct
[mesa.git] / src / mesa / swrast / s_context.h
1 /*
2 * Mesa 3-D graphics library
3 * Version: 6.5.3
4 *
5 * Copyright (C) 1999-2007 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 "main/compiler.h"
47 #include "main/mtypes.h"
48 #include "program/prog_execute.h"
49 #include "swrast.h"
50 #include "s_span.h"
51
52
53 typedef void (*texture_sample_func)(struct gl_context *ctx,
54 const struct gl_texture_object *tObj,
55 GLuint n, const GLfloat texcoords[][4],
56 const GLfloat lambda[], GLfloat rgba[][4]);
57
58 typedef void (_ASMAPIP blend_func)( struct gl_context *ctx, GLuint n,
59 const GLubyte mask[],
60 GLvoid *src, const GLvoid *dst,
61 GLenum chanType);
62
63 typedef void (*swrast_point_func)( struct gl_context *ctx, const SWvertex *);
64
65 typedef void (*swrast_line_func)( struct gl_context *ctx,
66 const SWvertex *, const SWvertex *);
67
68 typedef void (*swrast_tri_func)( struct gl_context *ctx, const SWvertex *,
69 const SWvertex *, const SWvertex *);
70
71
72 typedef void (*validate_texture_image_func)(struct gl_context *ctx,
73 struct gl_texture_object *texObj,
74 GLuint face, GLuint level);
75
76
77 /**
78 * \defgroup Bitmasks
79 * Bitmasks to indicate which rasterization options are enabled
80 * (RasterMask)
81 */
82 /*@{*/
83 #define ALPHATEST_BIT 0x001 /**< Alpha-test pixels */
84 #define BLEND_BIT 0x002 /**< Blend pixels */
85 #define DEPTH_BIT 0x004 /**< Depth-test pixels */
86 #define FOG_BIT 0x008 /**< Fog pixels */
87 #define LOGIC_OP_BIT 0x010 /**< Apply logic op in software */
88 #define CLIP_BIT 0x020 /**< Scissor or window clip pixels */
89 #define STENCIL_BIT 0x040 /**< Stencil pixels */
90 #define MASKING_BIT 0x080 /**< Do glColorMask or glIndexMask */
91 #define MULTI_DRAW_BIT 0x400 /**< Write to more than one color- */
92 /**< buffer or no buffers. */
93 #define OCCLUSION_BIT 0x800 /**< GL_HP_occlusion_test enabled */
94 #define TEXTURE_BIT 0x1000 /**< Texturing really enabled */
95 #define FRAGPROG_BIT 0x2000 /**< Fragment program enabled */
96 #define ATIFRAGSHADER_BIT 0x4000 /**< ATI Fragment shader enabled */
97 #define CLAMPING_BIT 0x8000 /**< Clamp colors to [0,1] */
98 /*@}*/
99
100 #define _SWRAST_NEW_RASTERMASK (_NEW_BUFFERS| \
101 _NEW_SCISSOR| \
102 _NEW_COLOR| \
103 _NEW_DEPTH| \
104 _NEW_FOG| \
105 _NEW_PROGRAM| \
106 _NEW_STENCIL| \
107 _NEW_TEXTURE| \
108 _NEW_VIEWPORT| \
109 _NEW_DEPTH)
110
111
112 /**
113 * Subclass of gl_texture_image.
114 * We need extra fields/info to keep tracking of mapped texture buffers,
115 * strides and Fetch/Store functions.
116 */
117 struct swrast_texture_image
118 {
119 struct gl_texture_image Base;
120
121 /* XXX new members coming soon */
122 };
123
124
125 /** cast wrapper */
126 static INLINE struct swrast_texture_image *
127 swrast_texture_image(struct gl_texture_image *img)
128 {
129 return (struct swrast_texture_image *) img;
130 }
131
132 /** cast wrapper */
133 static INLINE const struct swrast_texture_image *
134 swrast_texture_image_const(const struct gl_texture_image *img)
135 {
136 return (const struct swrast_texture_image *) img;
137 }
138
139
140
141 /**
142 * \struct SWcontext
143 * \brief Per-context state that's private to the software rasterizer module.
144 */
145 typedef struct
146 {
147 /** Driver interface:
148 */
149 struct swrast_device_driver Driver;
150
151 /** Configuration mechanisms to make software rasterizer match
152 * characteristics of the hardware rasterizer (if present):
153 */
154 GLboolean AllowVertexFog;
155 GLboolean AllowPixelFog;
156
157 /** Derived values, invalidated on statechanges, updated from
158 * _swrast_validate_derived():
159 */
160 GLbitfield _RasterMask;
161 GLfloat _BackfaceSign; /** +1 or -1 */
162 GLfloat _BackfaceCullSign; /** +1, 0, or -1 */
163 GLboolean _PreferPixelFog; /* Compute fog blend factor per fragment? */
164 GLboolean _TextureCombinePrimary;
165 GLboolean _FogEnabled;
166 GLboolean _DeferredTexture;
167
168 /** List/array of the fragment attributes to interpolate */
169 GLuint _ActiveAttribs[FRAG_ATTRIB_MAX];
170 /** Same info, but as a bitmask */
171 GLbitfield _ActiveAttribMask;
172 /** Number of fragment attributes to interpolate */
173 GLuint _NumActiveAttribs;
174 /** Indicates how each attrib is to be interpolated (lines/tris) */
175 GLenum _InterpMode[FRAG_ATTRIB_MAX]; /* GL_FLAT or GL_SMOOTH (for now) */
176
177 /* Accum buffer temporaries.
178 */
179 GLboolean _IntegerAccumMode; /**< Storing unscaled integers? */
180 GLfloat _IntegerAccumScaler; /**< Implicit scale factor */
181
182 /* Working values:
183 */
184 GLuint StippleCounter; /**< Line stipple counter */
185 GLuint PointLineFacing;
186 GLbitfield NewState;
187 GLuint StateChanges;
188 GLenum Primitive; /* current primitive being drawn (ala glBegin) */
189 GLboolean SpecularVertexAdd; /**< Add specular/secondary color per vertex */
190
191 void (*InvalidateState)( struct gl_context *ctx, GLbitfield new_state );
192
193 /**
194 * When the NewState mask intersects these masks, we invalidate the
195 * Point/Line/Triangle function pointers below.
196 */
197 /*@{*/
198 GLbitfield InvalidatePointMask;
199 GLbitfield InvalidateLineMask;
200 GLbitfield InvalidateTriangleMask;
201 /*@}*/
202
203 /**
204 * Device drivers plug in functions for these callbacks.
205 * Will be called when the GL state change mask intersects the above masks.
206 */
207 /*@{*/
208 void (*choose_point)( struct gl_context * );
209 void (*choose_line)( struct gl_context * );
210 void (*choose_triangle)( struct gl_context * );
211 /*@}*/
212
213 /**
214 * Current point, line and triangle drawing functions.
215 */
216 /*@{*/
217 swrast_point_func Point;
218 swrast_line_func Line;
219 swrast_tri_func Triangle;
220 /*@}*/
221
222 /**
223 * Placeholders for when separate specular (or secondary color) is
224 * enabled but texturing is not.
225 */
226 /*@{*/
227 swrast_point_func SpecPoint;
228 swrast_line_func SpecLine;
229 swrast_tri_func SpecTriangle;
230 /*@}*/
231
232 /**
233 * Typically, we'll allocate a sw_span structure as a local variable
234 * and set its 'array' pointer to point to this object. The reason is
235 * this object is big and causes problems when allocated on the stack
236 * on some systems.
237 */
238 SWspanarrays *SpanArrays;
239 SWspanarrays *ZoomedArrays; /**< For pixel zooming */
240
241 /**
242 * Used to buffer N GL_POINTS, instead of rendering one by one.
243 */
244 SWspan PointSpan;
245
246 /** Internal hooks, kept up to date by the same mechanism as above.
247 */
248 blend_func BlendFunc;
249 texture_sample_func TextureSample[MAX_TEXTURE_IMAGE_UNITS];
250
251 /** Buffer for saving the sampled texture colors.
252 * Needed for GL_ARB_texture_env_crossbar implementation.
253 */
254 GLfloat *TexelBuffer;
255
256 validate_texture_image_func ValidateTextureImage;
257
258 /** State used during execution of fragment programs */
259 struct gl_program_machine FragProgMachine;
260
261 } SWcontext;
262
263
264 extern void
265 _swrast_validate_derived( struct gl_context *ctx );
266
267 extern void
268 _swrast_update_texture_samplers(struct gl_context *ctx);
269
270
271 /** Return SWcontext for the given struct gl_context */
272 static INLINE SWcontext *
273 SWRAST_CONTEXT(struct gl_context *ctx)
274 {
275 return (SWcontext *) ctx->swrast_context;
276 }
277
278 /** const version of above */
279 static INLINE const SWcontext *
280 CONST_SWRAST_CONTEXT(const struct gl_context *ctx)
281 {
282 return (const SWcontext *) ctx->swrast_context;
283 }
284
285
286 /**
287 * Called prior to framebuffer reading/writing.
288 * For drivers that rely on swrast for fallback rendering, this is the
289 * driver's opportunity to map renderbuffers and textures.
290 */
291 static INLINE void
292 swrast_render_start(struct gl_context *ctx)
293 {
294 SWcontext *swrast = SWRAST_CONTEXT(ctx);
295 if (swrast->Driver.SpanRenderStart)
296 swrast->Driver.SpanRenderStart(ctx);
297 }
298
299
300 /** Called after framebuffer reading/writing */
301 static INLINE void
302 swrast_render_finish(struct gl_context *ctx)
303 {
304 SWcontext *swrast = SWRAST_CONTEXT(ctx);
305 if (swrast->Driver.SpanRenderFinish)
306 swrast->Driver.SpanRenderFinish(ctx);
307 }
308
309
310
311 /**
312 * Size of an RGBA pixel, in bytes, for given datatype.
313 */
314 #define RGBA_PIXEL_SIZE(TYPE) \
315 ((TYPE == GL_UNSIGNED_BYTE) ? 4 * sizeof(GLubyte) : \
316 ((TYPE == GL_UNSIGNED_SHORT) ? 4 * sizeof(GLushort) \
317 : 4 * sizeof(GLfloat)))
318
319
320
321 /*
322 * Fixed point arithmetic macros
323 */
324 #ifndef FIXED_FRAC_BITS
325 #define FIXED_FRAC_BITS 11
326 #endif
327
328 #define FIXED_SHIFT FIXED_FRAC_BITS
329 #define FIXED_ONE (1 << FIXED_SHIFT)
330 #define FIXED_HALF (1 << (FIXED_SHIFT-1))
331 #define FIXED_FRAC_MASK (FIXED_ONE - 1)
332 #define FIXED_INT_MASK (~FIXED_FRAC_MASK)
333 #define FIXED_EPSILON 1
334 #define FIXED_SCALE ((float) FIXED_ONE)
335 #define FIXED_DBL_SCALE ((double) FIXED_ONE)
336 #define FloatToFixed(X) (IROUND((X) * FIXED_SCALE))
337 #define FixedToDouble(X) ((X) * (1.0 / FIXED_DBL_SCALE))
338 #define IntToFixed(I) ((I) << FIXED_SHIFT)
339 #define FixedToInt(X) ((X) >> FIXED_SHIFT)
340 #define FixedToUns(X) (((unsigned int)(X)) >> FIXED_SHIFT)
341 #define FixedCeil(X) (((X) + FIXED_ONE - FIXED_EPSILON) & FIXED_INT_MASK)
342 #define FixedFloor(X) ((X) & FIXED_INT_MASK)
343 #define FixedToFloat(X) ((X) * (1.0F / FIXED_SCALE))
344 #define PosFloatToFixed(X) FloatToFixed(X)
345 #define SignedFloatToFixed(X) FloatToFixed(X)
346
347
348
349 /*
350 * XXX these macros are just bandages for now in order to make
351 * CHAN_BITS==32 compile cleanly.
352 * These should probably go elsewhere at some point.
353 */
354 #if CHAN_TYPE == GL_FLOAT
355 #define ChanToFixed(X) (X)
356 #define FixedToChan(X) (X)
357 #else
358 #define ChanToFixed(X) IntToFixed(X)
359 #define FixedToChan(X) FixedToInt(X)
360 #endif
361
362
363 /**
364 * For looping over fragment attributes in the pointe, line
365 * triangle rasterizers.
366 */
367 #define ATTRIB_LOOP_BEGIN \
368 { \
369 GLuint a; \
370 for (a = 0; a < swrast->_NumActiveAttribs; a++) { \
371 const GLuint attr = swrast->_ActiveAttribs[a];
372
373 #define ATTRIB_LOOP_END } }
374
375
376
377 #endif