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 * THE AUTHORS 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 "main/texcompress.h"
49 #include "program/prog_execute.h"
51 #include "s_fragprog.h"
55 typedef void (*texture_sample_func
)(struct gl_context
*ctx
,
56 const struct gl_sampler_object
*samp
,
57 const struct gl_texture_object
*tObj
,
58 GLuint n
, const GLfloat texcoords
[][4],
59 const GLfloat lambda
[], GLfloat rgba
[][4]);
61 typedef void (_ASMAPIP blend_func
)( struct gl_context
*ctx
, GLuint n
,
63 GLvoid
*src
, const GLvoid
*dst
,
66 typedef void (*swrast_point_func
)( struct gl_context
*ctx
, const SWvertex
*);
68 typedef void (*swrast_line_func
)( struct gl_context
*ctx
,
69 const SWvertex
*, const SWvertex
*);
71 typedef void (*swrast_tri_func
)( struct gl_context
*ctx
, const SWvertex
*,
72 const SWvertex
*, const SWvertex
*);
75 typedef void (*validate_texture_image_func
)(struct gl_context
*ctx
,
76 struct gl_texture_object
*texObj
,
77 GLuint face
, GLuint level
);
82 * Bitmasks to indicate which rasterization options are enabled
86 #define ALPHATEST_BIT 0x001 /**< Alpha-test pixels */
87 #define BLEND_BIT 0x002 /**< Blend pixels */
88 #define DEPTH_BIT 0x004 /**< Depth-test pixels */
89 #define FOG_BIT 0x008 /**< Fog pixels */
90 #define LOGIC_OP_BIT 0x010 /**< Apply logic op in software */
91 #define CLIP_BIT 0x020 /**< Scissor or window clip pixels */
92 #define STENCIL_BIT 0x040 /**< Stencil pixels */
93 #define MASKING_BIT 0x080 /**< Do glColorMask or glIndexMask */
94 #define MULTI_DRAW_BIT 0x400 /**< Write to more than one color- */
95 /**< buffer or no buffers. */
96 #define OCCLUSION_BIT 0x800 /**< GL_HP_occlusion_test enabled */
97 #define TEXTURE_BIT 0x1000 /**< Texturing really enabled */
98 #define FRAGPROG_BIT 0x2000 /**< Fragment program enabled */
99 #define ATIFRAGSHADER_BIT 0x4000 /**< ATI Fragment shader enabled */
100 #define CLAMPING_BIT 0x8000 /**< Clamp colors to [0,1] */
103 #define _SWRAST_NEW_RASTERMASK (_NEW_BUFFERS| \
115 struct swrast_texture_image
;
119 * Fetch a texel from texture image at given position.
121 typedef void (*FetchTexelFunc
)(const struct swrast_texture_image
*texImage
,
122 GLint col
, GLint row
, GLint img
,
127 * Subclass of gl_texture_image.
128 * We need extra fields/info to keep tracking of mapped texture buffers,
129 * strides and Fetch functions.
131 struct swrast_texture_image
133 struct gl_texture_image Base
;
135 GLboolean _IsPowerOfTwo
; /**< Are all dimensions powers of two? */
137 /** used for mipmap LOD computation */
138 GLfloat WidthScale
, HeightScale
, DepthScale
;
140 /** These fields only valid when texture memory is mapped */
141 GLint RowStride
; /**< Padded width in units of texels */
142 GLuint
*ImageOffsets
; /**< if 3D texture: array [Depth] of offsets to
143 each 2D slice in 'Data', in texels */
144 GLubyte
*Map
; /**< Pointer to mapped image memory */
146 /** Malloc'd texture memory */
149 FetchTexelFunc FetchTexel
;
151 /** For fetching texels from compressed textures */
152 compressed_fetch_func FetchCompressedTexel
;
157 static inline struct swrast_texture_image
*
158 swrast_texture_image(struct gl_texture_image
*img
)
160 return (struct swrast_texture_image
*) img
;
164 static inline const struct swrast_texture_image
*
165 swrast_texture_image_const(const struct gl_texture_image
*img
)
167 return (const struct swrast_texture_image
*) img
;
172 * Subclass of gl_renderbuffer with extra fields needed for software
175 struct swrast_renderbuffer
177 struct gl_renderbuffer Base
;
179 GLubyte
*Buffer
; /**< The malloc'd memory for buffer */
181 /** These fields are only valid while buffer is mapped for rendering */
183 GLint RowStride
; /**< in bytes */
185 /** For span rendering */
191 static inline struct swrast_renderbuffer
*
192 swrast_renderbuffer(struct gl_renderbuffer
*img
)
194 return (struct swrast_renderbuffer
*) img
;
201 * \brief Per-context state that's private to the software rasterizer module.
205 /** Driver interface:
207 struct swrast_device_driver Driver
;
209 /** Configuration mechanisms to make software rasterizer match
210 * characteristics of the hardware rasterizer (if present):
212 GLboolean AllowVertexFog
;
213 GLboolean AllowPixelFog
;
215 /** Derived values, invalidated on statechanges, updated from
216 * _swrast_validate_derived():
218 GLbitfield _RasterMask
;
219 GLfloat _BackfaceSign
; /** +1 or -1 */
220 GLfloat _BackfaceCullSign
; /** +1, 0, or -1 */
221 GLboolean _PreferPixelFog
; /* Compute fog blend factor per fragment? */
222 GLboolean _TextureCombinePrimary
;
223 GLboolean _FogEnabled
;
224 GLboolean _DeferredTexture
;
226 /** List/array of the fragment attributes to interpolate */
227 GLuint _ActiveAttribs
[VARYING_SLOT_MAX
];
228 /** Same info, but as a bitmask of VARYING_BIT_x bits */
229 GLbitfield64 _ActiveAttribMask
;
230 /** Number of fragment attributes to interpolate */
231 GLuint _NumActiveAttribs
;
232 /** Indicates how each attrib is to be interpolated (lines/tris) */
233 GLenum _InterpMode
[VARYING_SLOT_MAX
]; /* GL_FLAT or GL_SMOOTH (for now) */
237 GLuint StippleCounter
; /**< Line stipple counter */
238 GLuint PointLineFacing
;
241 GLenum Primitive
; /* current primitive being drawn (ala glBegin) */
242 GLboolean SpecularVertexAdd
; /**< Add specular/secondary color per vertex */
244 void (*InvalidateState
)( struct gl_context
*ctx
, GLbitfield new_state
);
247 * When the NewState mask intersects these masks, we invalidate the
248 * Point/Line/Triangle function pointers below.
251 GLbitfield InvalidatePointMask
;
252 GLbitfield InvalidateLineMask
;
253 GLbitfield InvalidateTriangleMask
;
257 * Device drivers plug in functions for these callbacks.
258 * Will be called when the GL state change mask intersects the above masks.
261 void (*choose_point
)( struct gl_context
* );
262 void (*choose_line
)( struct gl_context
* );
263 void (*choose_triangle
)( struct gl_context
* );
267 * Current point, line and triangle drawing functions.
270 swrast_point_func Point
;
271 swrast_line_func Line
;
272 swrast_tri_func Triangle
;
276 * Placeholders for when separate specular (or secondary color) is
277 * enabled but texturing is not.
280 swrast_point_func SpecPoint
;
281 swrast_line_func SpecLine
;
282 swrast_tri_func SpecTriangle
;
286 * Typically, we'll allocate a sw_span structure as a local variable
287 * and set its 'array' pointer to point to this object. The reason is
288 * this object is big and causes problems when allocated on the stack
291 SWspanarrays
*SpanArrays
;
292 SWspanarrays
*ZoomedArrays
; /**< For pixel zooming */
295 * Used to buffer N GL_POINTS, instead of rendering one by one.
299 /** Internal hooks, kept up to date by the same mechanism as above.
301 blend_func BlendFunc
;
302 texture_sample_func TextureSample
[MAX_TEXTURE_IMAGE_UNITS
];
304 /** Buffer for saving the sampled texture colors.
305 * Needed for GL_ARB_texture_env_crossbar implementation.
307 GLfloat
*TexelBuffer
;
309 validate_texture_image_func ValidateTextureImage
;
311 /** State used during execution of fragment programs */
312 struct gl_program_machine FragProgMachine
;
314 /** Temporary arrays for stencil operations. To avoid large stack
318 GLubyte
*buf1
, *buf2
, *buf3
, *buf4
;
325 _swrast_validate_derived( struct gl_context
*ctx
);
328 _swrast_update_texture_samplers(struct gl_context
*ctx
);
331 /** Return SWcontext for the given struct gl_context */
332 static inline SWcontext
*
333 SWRAST_CONTEXT(struct gl_context
*ctx
)
335 return (SWcontext
*) ctx
->swrast_context
;
338 /** const version of above */
339 static inline const SWcontext
*
340 CONST_SWRAST_CONTEXT(const struct gl_context
*ctx
)
342 return (const SWcontext
*) ctx
->swrast_context
;
347 * Called prior to framebuffer reading/writing.
348 * For drivers that rely on swrast for fallback rendering, this is the
349 * driver's opportunity to map renderbuffers and textures.
352 swrast_render_start(struct gl_context
*ctx
)
354 SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
355 if (swrast
->Driver
.SpanRenderStart
)
356 swrast
->Driver
.SpanRenderStart(ctx
);
360 /** Called after framebuffer reading/writing */
362 swrast_render_finish(struct gl_context
*ctx
)
364 SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
365 if (swrast
->Driver
.SpanRenderFinish
)
366 swrast
->Driver
.SpanRenderFinish(ctx
);
371 _swrast_span_render_start(struct gl_context
*ctx
);
374 _swrast_span_render_finish(struct gl_context
*ctx
);
377 _swrast_map_textures(struct gl_context
*ctx
);
380 _swrast_unmap_textures(struct gl_context
*ctx
);
383 _swrast_map_texture(struct gl_context
*ctx
, struct gl_texture_object
*texObj
);
386 _swrast_unmap_texture(struct gl_context
*ctx
, struct gl_texture_object
*texObj
);
390 _swrast_map_renderbuffers(struct gl_context
*ctx
);
393 _swrast_unmap_renderbuffers(struct gl_context
*ctx
);
397 * Size of an RGBA pixel, in bytes, for given datatype.
399 #define RGBA_PIXEL_SIZE(TYPE) \
400 ((TYPE == GL_UNSIGNED_BYTE) ? 4 * sizeof(GLubyte) : \
401 ((TYPE == GL_UNSIGNED_SHORT) ? 4 * sizeof(GLushort) \
402 : 4 * sizeof(GLfloat)))
407 * Fixed point arithmetic macros
409 #ifndef FIXED_FRAC_BITS
410 #define FIXED_FRAC_BITS 11
413 #define FIXED_SHIFT FIXED_FRAC_BITS
414 #define FIXED_ONE (1 << FIXED_SHIFT)
415 #define FIXED_HALF (1 << (FIXED_SHIFT-1))
416 #define FIXED_FRAC_MASK (FIXED_ONE - 1)
417 #define FIXED_INT_MASK (~FIXED_FRAC_MASK)
418 #define FIXED_EPSILON 1
419 #define FIXED_SCALE ((float) FIXED_ONE)
420 #define FIXED_DBL_SCALE ((double) FIXED_ONE)
421 #define FloatToFixed(X) (IROUND((X) * FIXED_SCALE))
422 #define FixedToDouble(X) ((X) * (1.0 / FIXED_DBL_SCALE))
423 #define IntToFixed(I) ((I) << FIXED_SHIFT)
424 #define FixedToInt(X) ((X) >> FIXED_SHIFT)
425 #define FixedToUns(X) (((unsigned int)(X)) >> FIXED_SHIFT)
426 #define FixedCeil(X) (((X) + FIXED_ONE - FIXED_EPSILON) & FIXED_INT_MASK)
427 #define FixedFloor(X) ((X) & FIXED_INT_MASK)
428 #define FixedToFloat(X) ((X) * (1.0F / FIXED_SCALE))
429 #define PosFloatToFixed(X) FloatToFixed(X)
430 #define SignedFloatToFixed(X) FloatToFixed(X)
435 * XXX these macros are just bandages for now in order to make
436 * CHAN_BITS==32 compile cleanly.
437 * These should probably go elsewhere at some point.
439 #if CHAN_TYPE == GL_FLOAT
440 #define ChanToFixed(X) (X)
441 #define FixedToChan(X) (X)
443 #define ChanToFixed(X) IntToFixed(X)
444 #define FixedToChan(X) FixedToInt(X)
449 * For looping over fragment attributes in the pointe, line
450 * triangle rasterizers.
452 #define ATTRIB_LOOP_BEGIN \
455 for (a = 0; a < swrast->_NumActiveAttribs; a++) { \
456 const GLuint attr = swrast->_ActiveAttribs[a];
458 #define ATTRIB_LOOP_END } }
462 * Return the address of a pixel value in a mapped renderbuffer.
464 static inline GLubyte
*
465 _swrast_pixel_address(struct gl_renderbuffer
*rb
, GLint x
, GLint y
)
467 struct swrast_renderbuffer
*srb
= swrast_renderbuffer(rb
);
468 const GLint bpp
= _mesa_get_format_bytes(rb
->Format
);
469 const GLint rowStride
= srb
->RowStride
;
472 /* NOTE: using <= only because of s_tritemp.h which gets a pixel
473 * address but doesn't necessarily access it.
475 assert(x
<= (GLint
) rb
->Width
);
476 assert(y
<= (GLint
) rb
->Height
);
478 return (GLubyte
*) srb
->Map
+ y
* rowStride
+ x
* bpp
;