2 * Mesa 3-D graphics library
5 * Copyright (C) 1999-2007 Brian Paul All Rights Reserved.
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:
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
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.
27 * \file swrast/s_context.h
28 * \brief Software rasterization context and private types.
29 * \author Keith Whitwell <keith@tungstengraphics.com>
33 * \mainpage swrast module
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.
46 #include "main/compiler.h"
47 #include "main/mtypes.h"
48 #include "program/prog_execute.h"
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]);
58 typedef void (_ASMAPIP blend_func
)( struct gl_context
*ctx
, GLuint n
,
60 GLvoid
*src
, const GLvoid
*dst
,
63 typedef void (*swrast_point_func
)( struct gl_context
*ctx
, const SWvertex
*);
65 typedef void (*swrast_line_func
)( struct gl_context
*ctx
,
66 const SWvertex
*, const SWvertex
*);
68 typedef void (*swrast_tri_func
)( struct gl_context
*ctx
, const SWvertex
*,
69 const SWvertex
*, const SWvertex
*);
72 typedef void (*validate_texture_image_func
)(struct gl_context
*ctx
,
73 struct gl_texture_object
*texObj
,
74 GLuint face
, GLuint level
);
79 * Bitmasks to indicate which rasterization options are enabled
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] */
100 #define _SWRAST_NEW_RASTERMASK (_NEW_BUFFERS| \
112 struct swrast_texture_image
;
116 * Fetch a texel from texture image at given position.
118 typedef void (*FetchTexelFunc
)(const struct swrast_texture_image
*texImage
,
119 GLint col
, GLint row
, GLint img
,
123 typedef void (*StoreTexelFunc
)(struct swrast_texture_image
*texImage
,
124 GLint col
, GLint row
, GLint img
,
128 * Subclass of gl_texture_image.
129 * We need extra fields/info to keep tracking of mapped texture buffers,
130 * strides and Fetch/Store functions.
132 struct swrast_texture_image
134 struct gl_texture_image Base
;
136 GLboolean _IsPowerOfTwo
; /**< Are all dimensions powers of two? */
138 /** used for mipmap LOD computation */
139 GLfloat WidthScale
, HeightScale
, DepthScale
;
142 GLubyte
*Data
; /**< The actual texture data in malloc'd memory */
144 GLint TexelSize
; /**< bytes per texel block */
147 FetchTexelFunc FetchTexel
;
148 StoreTexelFunc Store
;
151 /** These fields only valid when texture memory is mapped */
152 GLubyte
**SliceMaps
; /**< points to OneMap or a malloc'd array */
153 GLint RowStride
; /**< bytes per row of blocks */
159 static inline struct swrast_texture_image
*
160 swrast_texture_image(struct gl_texture_image
*img
)
162 return (struct swrast_texture_image
*) img
;
166 static inline const struct swrast_texture_image
*
167 swrast_texture_image_const(const struct gl_texture_image
*img
)
169 return (const struct swrast_texture_image
*) img
;
175 * \brief Per-context state that's private to the software rasterizer module.
179 /** Driver interface:
181 struct swrast_device_driver Driver
;
183 /** Configuration mechanisms to make software rasterizer match
184 * characteristics of the hardware rasterizer (if present):
186 GLboolean AllowVertexFog
;
187 GLboolean AllowPixelFog
;
189 /** Derived values, invalidated on statechanges, updated from
190 * _swrast_validate_derived():
192 GLbitfield _RasterMask
;
193 GLfloat _BackfaceSign
; /** +1 or -1 */
194 GLfloat _BackfaceCullSign
; /** +1, 0, or -1 */
195 GLboolean _PreferPixelFog
; /* Compute fog blend factor per fragment? */
196 GLboolean _TextureCombinePrimary
;
197 GLboolean _FogEnabled
;
198 GLboolean _DeferredTexture
;
200 /** List/array of the fragment attributes to interpolate */
201 GLuint _ActiveAttribs
[FRAG_ATTRIB_MAX
];
202 /** Same info, but as a bitmask */
203 GLbitfield _ActiveAttribMask
;
204 /** Number of fragment attributes to interpolate */
205 GLuint _NumActiveAttribs
;
206 /** Indicates how each attrib is to be interpolated (lines/tris) */
207 GLenum _InterpMode
[FRAG_ATTRIB_MAX
]; /* GL_FLAT or GL_SMOOTH (for now) */
209 /* Accum buffer temporaries.
211 GLboolean _IntegerAccumMode
; /**< Storing unscaled integers? */
212 GLfloat _IntegerAccumScaler
; /**< Implicit scale factor */
216 GLuint StippleCounter
; /**< Line stipple counter */
217 GLuint PointLineFacing
;
220 GLenum Primitive
; /* current primitive being drawn (ala glBegin) */
221 GLboolean SpecularVertexAdd
; /**< Add specular/secondary color per vertex */
223 void (*InvalidateState
)( struct gl_context
*ctx
, GLbitfield new_state
);
226 * When the NewState mask intersects these masks, we invalidate the
227 * Point/Line/Triangle function pointers below.
230 GLbitfield InvalidatePointMask
;
231 GLbitfield InvalidateLineMask
;
232 GLbitfield InvalidateTriangleMask
;
236 * Device drivers plug in functions for these callbacks.
237 * Will be called when the GL state change mask intersects the above masks.
240 void (*choose_point
)( struct gl_context
* );
241 void (*choose_line
)( struct gl_context
* );
242 void (*choose_triangle
)( struct gl_context
* );
246 * Current point, line and triangle drawing functions.
249 swrast_point_func Point
;
250 swrast_line_func Line
;
251 swrast_tri_func Triangle
;
255 * Placeholders for when separate specular (or secondary color) is
256 * enabled but texturing is not.
259 swrast_point_func SpecPoint
;
260 swrast_line_func SpecLine
;
261 swrast_tri_func SpecTriangle
;
265 * Typically, we'll allocate a sw_span structure as a local variable
266 * and set its 'array' pointer to point to this object. The reason is
267 * this object is big and causes problems when allocated on the stack
270 SWspanarrays
*SpanArrays
;
271 SWspanarrays
*ZoomedArrays
; /**< For pixel zooming */
274 * Used to buffer N GL_POINTS, instead of rendering one by one.
278 /** Internal hooks, kept up to date by the same mechanism as above.
280 blend_func BlendFunc
;
281 texture_sample_func TextureSample
[MAX_TEXTURE_IMAGE_UNITS
];
283 /** Buffer for saving the sampled texture colors.
284 * Needed for GL_ARB_texture_env_crossbar implementation.
286 GLfloat
*TexelBuffer
;
288 validate_texture_image_func ValidateTextureImage
;
290 /** State used during execution of fragment programs */
291 struct gl_program_machine FragProgMachine
;
297 _swrast_validate_derived( struct gl_context
*ctx
);
300 _swrast_update_texture_samplers(struct gl_context
*ctx
);
303 /** Return SWcontext for the given struct gl_context */
304 static inline SWcontext
*
305 SWRAST_CONTEXT(struct gl_context
*ctx
)
307 return (SWcontext
*) ctx
->swrast_context
;
310 /** const version of above */
311 static inline const SWcontext
*
312 CONST_SWRAST_CONTEXT(const struct gl_context
*ctx
)
314 return (const SWcontext
*) ctx
->swrast_context
;
319 * Called prior to framebuffer reading/writing.
320 * For drivers that rely on swrast for fallback rendering, this is the
321 * driver's opportunity to map renderbuffers and textures.
324 swrast_render_start(struct gl_context
*ctx
)
326 SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
327 if (swrast
->Driver
.SpanRenderStart
)
328 swrast
->Driver
.SpanRenderStart(ctx
);
332 /** Called after framebuffer reading/writing */
334 swrast_render_finish(struct gl_context
*ctx
)
336 SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
337 if (swrast
->Driver
.SpanRenderFinish
)
338 swrast
->Driver
.SpanRenderFinish(ctx
);
344 * Size of an RGBA pixel, in bytes, for given datatype.
346 #define RGBA_PIXEL_SIZE(TYPE) \
347 ((TYPE == GL_UNSIGNED_BYTE) ? 4 * sizeof(GLubyte) : \
348 ((TYPE == GL_UNSIGNED_SHORT) ? 4 * sizeof(GLushort) \
349 : 4 * sizeof(GLfloat)))
354 * Fixed point arithmetic macros
356 #ifndef FIXED_FRAC_BITS
357 #define FIXED_FRAC_BITS 11
360 #define FIXED_SHIFT FIXED_FRAC_BITS
361 #define FIXED_ONE (1 << FIXED_SHIFT)
362 #define FIXED_HALF (1 << (FIXED_SHIFT-1))
363 #define FIXED_FRAC_MASK (FIXED_ONE - 1)
364 #define FIXED_INT_MASK (~FIXED_FRAC_MASK)
365 #define FIXED_EPSILON 1
366 #define FIXED_SCALE ((float) FIXED_ONE)
367 #define FIXED_DBL_SCALE ((double) FIXED_ONE)
368 #define FloatToFixed(X) (IROUND((X) * FIXED_SCALE))
369 #define FixedToDouble(X) ((X) * (1.0 / FIXED_DBL_SCALE))
370 #define IntToFixed(I) ((I) << FIXED_SHIFT)
371 #define FixedToInt(X) ((X) >> FIXED_SHIFT)
372 #define FixedToUns(X) (((unsigned int)(X)) >> FIXED_SHIFT)
373 #define FixedCeil(X) (((X) + FIXED_ONE - FIXED_EPSILON) & FIXED_INT_MASK)
374 #define FixedFloor(X) ((X) & FIXED_INT_MASK)
375 #define FixedToFloat(X) ((X) * (1.0F / FIXED_SCALE))
376 #define PosFloatToFixed(X) FloatToFixed(X)
377 #define SignedFloatToFixed(X) FloatToFixed(X)
382 * XXX these macros are just bandages for now in order to make
383 * CHAN_BITS==32 compile cleanly.
384 * These should probably go elsewhere at some point.
386 #if CHAN_TYPE == GL_FLOAT
387 #define ChanToFixed(X) (X)
388 #define FixedToChan(X) (X)
390 #define ChanToFixed(X) IntToFixed(X)
391 #define FixedToChan(X) FixedToInt(X)
396 * For looping over fragment attributes in the pointe, line
397 * triangle rasterizers.
399 #define ATTRIB_LOOP_BEGIN \
402 for (a = 0; a < swrast->_NumActiveAttribs; a++) { \
403 const GLuint attr = swrast->_ActiveAttribs[a];
405 #define ATTRIB_LOOP_END } }