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 OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
21 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
22 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23 * OTHER DEALINGS IN THE SOFTWARE.
28 * \file swrast/s_context.h
29 * \brief Software rasterization context and private types.
30 * \author Keith Whitwell <keith@tungstengraphics.com>
34 * \mainpage swrast module
36 * This module, software rasterization, contains the software fallback
37 * routines for drawing points, lines, triangles, bitmaps and images.
38 * All rendering boils down to writing spans (arrays) of pixels with
39 * particular colors. The span-writing routines must be implemented
40 * by the device driver.
47 #include "main/compiler.h"
48 #include "main/mtypes.h"
49 #include "main/texcompress.h"
50 #include "program/prog_execute.h"
52 #include "s_fragprog.h"
56 typedef void (*texture_sample_func
)(struct gl_context
*ctx
,
57 const struct gl_sampler_object
*samp
,
58 const struct gl_texture_object
*tObj
,
59 GLuint n
, const GLfloat texcoords
[][4],
60 const GLfloat lambda
[], GLfloat rgba
[][4]);
62 typedef void (_ASMAPIP blend_func
)( struct gl_context
*ctx
, GLuint n
,
64 GLvoid
*src
, const GLvoid
*dst
,
67 typedef void (*swrast_point_func
)( struct gl_context
*ctx
, const SWvertex
*);
69 typedef void (*swrast_line_func
)( struct gl_context
*ctx
,
70 const SWvertex
*, const SWvertex
*);
72 typedef void (*swrast_tri_func
)( struct gl_context
*ctx
, const SWvertex
*,
73 const SWvertex
*, const SWvertex
*);
76 typedef void (*validate_texture_image_func
)(struct gl_context
*ctx
,
77 struct gl_texture_object
*texObj
,
78 GLuint face
, GLuint level
);
83 * Bitmasks to indicate which rasterization options are enabled
87 #define ALPHATEST_BIT 0x001 /**< Alpha-test pixels */
88 #define BLEND_BIT 0x002 /**< Blend pixels */
89 #define DEPTH_BIT 0x004 /**< Depth-test pixels */
90 #define FOG_BIT 0x008 /**< Fog pixels */
91 #define LOGIC_OP_BIT 0x010 /**< Apply logic op in software */
92 #define CLIP_BIT 0x020 /**< Scissor or window clip pixels */
93 #define STENCIL_BIT 0x040 /**< Stencil pixels */
94 #define MASKING_BIT 0x080 /**< Do glColorMask or glIndexMask */
95 #define MULTI_DRAW_BIT 0x400 /**< Write to more than one color- */
96 /**< buffer or no buffers. */
97 #define OCCLUSION_BIT 0x800 /**< GL_HP_occlusion_test enabled */
98 #define TEXTURE_BIT 0x1000 /**< Texturing really enabled */
99 #define FRAGPROG_BIT 0x2000 /**< Fragment program enabled */
100 #define ATIFRAGSHADER_BIT 0x4000 /**< ATI Fragment shader enabled */
101 #define CLAMPING_BIT 0x8000 /**< Clamp colors to [0,1] */
104 #define _SWRAST_NEW_RASTERMASK (_NEW_BUFFERS| \
116 struct swrast_texture_image
;
120 * Fetch a texel from texture image at given position.
122 typedef void (*FetchTexelFunc
)(const struct swrast_texture_image
*texImage
,
123 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 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
;
141 /** These fields only valid when texture memory is mapped */
142 GLint RowStride
; /**< Padded width in units of texels */
143 void **ImageSlices
; /**< if 3D texture: array [Depth] of offsets to
144 each 2D slice in 'Data', in texels */
145 GLubyte
*Map
; /**< Pointer to mapped image memory */
147 /** Malloc'd texture memory */
150 FetchTexelFunc FetchTexel
;
152 /** For fetching texels from compressed textures */
153 compressed_fetch_func FetchCompressedTexel
;
158 static inline struct swrast_texture_image
*
159 swrast_texture_image(struct gl_texture_image
*img
)
161 return (struct swrast_texture_image
*) img
;
165 static inline const struct swrast_texture_image
*
166 swrast_texture_image_const(const struct gl_texture_image
*img
)
168 return (const struct swrast_texture_image
*) img
;
173 * Subclass of gl_renderbuffer with extra fields needed for software
176 struct swrast_renderbuffer
178 struct gl_renderbuffer Base
;
180 GLubyte
*Buffer
; /**< The malloc'd memory for buffer */
182 /** These fields are only valid while buffer is mapped for rendering */
184 GLint RowStride
; /**< in bytes */
186 /** For span rendering */
192 static inline struct swrast_renderbuffer
*
193 swrast_renderbuffer(struct gl_renderbuffer
*img
)
195 return (struct swrast_renderbuffer
*) img
;
202 * \brief Per-context state that's private to the software rasterizer module.
206 /** Driver interface:
208 struct swrast_device_driver Driver
;
210 /** Configuration mechanisms to make software rasterizer match
211 * characteristics of the hardware rasterizer (if present):
213 GLboolean AllowVertexFog
;
214 GLboolean AllowPixelFog
;
216 /** Derived values, invalidated on statechanges, updated from
217 * _swrast_validate_derived():
219 GLbitfield _RasterMask
;
220 GLfloat _BackfaceSign
; /** +1 or -1 */
221 GLfloat _BackfaceCullSign
; /** +1, 0, or -1 */
222 GLboolean _PreferPixelFog
; /* Compute fog blend factor per fragment? */
223 GLboolean _TextureCombinePrimary
;
224 GLboolean _FogEnabled
;
225 GLboolean _DeferredTexture
;
227 /** List/array of the fragment attributes to interpolate */
228 GLuint _ActiveAttribs
[VARYING_SLOT_MAX
];
229 /** Same info, but as a bitmask of VARYING_BIT_x bits */
230 GLbitfield64 _ActiveAttribMask
;
231 /** Number of fragment attributes to interpolate */
232 GLuint _NumActiveAttribs
;
233 /** Indicates how each attrib is to be interpolated (lines/tris) */
234 GLenum _InterpMode
[VARYING_SLOT_MAX
]; /* GL_FLAT or GL_SMOOTH (for now) */
238 GLuint StippleCounter
; /**< Line stipple counter */
239 GLuint PointLineFacing
;
242 GLenum Primitive
; /* current primitive being drawn (ala glBegin) */
243 GLboolean SpecularVertexAdd
; /**< Add specular/secondary color per vertex */
245 void (*InvalidateState
)( struct gl_context
*ctx
, GLbitfield new_state
);
248 * When the NewState mask intersects these masks, we invalidate the
249 * Point/Line/Triangle function pointers below.
252 GLbitfield InvalidatePointMask
;
253 GLbitfield InvalidateLineMask
;
254 GLbitfield InvalidateTriangleMask
;
258 * Device drivers plug in functions for these callbacks.
259 * Will be called when the GL state change mask intersects the above masks.
262 void (*choose_point
)( struct gl_context
* );
263 void (*choose_line
)( struct gl_context
* );
264 void (*choose_triangle
)( struct gl_context
* );
268 * Current point, line and triangle drawing functions.
271 swrast_point_func Point
;
272 swrast_line_func Line
;
273 swrast_tri_func Triangle
;
277 * Placeholders for when separate specular (or secondary color) is
278 * enabled but texturing is not.
281 swrast_point_func SpecPoint
;
282 swrast_line_func SpecLine
;
283 swrast_tri_func SpecTriangle
;
287 * Typically, we'll allocate a sw_span structure as a local variable
288 * and set its 'array' pointer to point to this object. The reason is
289 * this object is big and causes problems when allocated on the stack
292 SWspanarrays
*SpanArrays
;
293 SWspanarrays
*ZoomedArrays
; /**< For pixel zooming */
296 * Used to buffer N GL_POINTS, instead of rendering one by one.
300 /** Internal hooks, kept up to date by the same mechanism as above.
302 blend_func BlendFunc
;
303 texture_sample_func TextureSample
[MAX_TEXTURE_IMAGE_UNITS
];
305 /** Buffer for saving the sampled texture colors.
306 * Needed for GL_ARB_texture_env_crossbar implementation.
308 GLfloat
*TexelBuffer
;
310 validate_texture_image_func ValidateTextureImage
;
312 /** State used during execution of fragment programs */
313 struct gl_program_machine FragProgMachine
;
315 /** Temporary arrays for stencil operations. To avoid large stack
319 GLubyte
*buf1
, *buf2
, *buf3
, *buf4
;
326 _swrast_validate_derived( struct gl_context
*ctx
);
329 _swrast_update_texture_samplers(struct gl_context
*ctx
);
332 /** Return SWcontext for the given struct gl_context */
333 static inline SWcontext
*
334 SWRAST_CONTEXT(struct gl_context
*ctx
)
336 return (SWcontext
*) ctx
->swrast_context
;
339 /** const version of above */
340 static inline const SWcontext
*
341 CONST_SWRAST_CONTEXT(const struct gl_context
*ctx
)
343 return (const SWcontext
*) ctx
->swrast_context
;
348 * Called prior to framebuffer reading/writing.
349 * For drivers that rely on swrast for fallback rendering, this is the
350 * driver's opportunity to map renderbuffers and textures.
353 swrast_render_start(struct gl_context
*ctx
)
355 SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
356 if (swrast
->Driver
.SpanRenderStart
)
357 swrast
->Driver
.SpanRenderStart(ctx
);
361 /** Called after framebuffer reading/writing */
363 swrast_render_finish(struct gl_context
*ctx
)
365 SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
366 if (swrast
->Driver
.SpanRenderFinish
)
367 swrast
->Driver
.SpanRenderFinish(ctx
);
372 _swrast_span_render_start(struct gl_context
*ctx
);
375 _swrast_span_render_finish(struct gl_context
*ctx
);
378 _swrast_map_textures(struct gl_context
*ctx
);
381 _swrast_unmap_textures(struct gl_context
*ctx
);
384 _swrast_teximage_slice_height(struct gl_texture_image
*texImage
);
387 _swrast_map_texture(struct gl_context
*ctx
, struct gl_texture_object
*texObj
);
390 _swrast_unmap_texture(struct gl_context
*ctx
, struct gl_texture_object
*texObj
);
394 _swrast_map_renderbuffers(struct gl_context
*ctx
);
397 _swrast_unmap_renderbuffers(struct gl_context
*ctx
);
401 * Size of an RGBA pixel, in bytes, for given datatype.
403 #define RGBA_PIXEL_SIZE(TYPE) \
404 ((TYPE == GL_UNSIGNED_BYTE) ? 4 * sizeof(GLubyte) : \
405 ((TYPE == GL_UNSIGNED_SHORT) ? 4 * sizeof(GLushort) \
406 : 4 * sizeof(GLfloat)))
411 * Fixed point arithmetic macros
413 #ifndef FIXED_FRAC_BITS
414 #define FIXED_FRAC_BITS 11
417 #define FIXED_SHIFT FIXED_FRAC_BITS
418 #define FIXED_ONE (1 << FIXED_SHIFT)
419 #define FIXED_HALF (1 << (FIXED_SHIFT-1))
420 #define FIXED_FRAC_MASK (FIXED_ONE - 1)
421 #define FIXED_INT_MASK (~FIXED_FRAC_MASK)
422 #define FIXED_EPSILON 1
423 #define FIXED_SCALE ((float) FIXED_ONE)
424 #define FIXED_DBL_SCALE ((double) FIXED_ONE)
425 #define FloatToFixed(X) (IROUND((X) * FIXED_SCALE))
426 #define FixedToDouble(X) ((X) * (1.0 / FIXED_DBL_SCALE))
427 #define IntToFixed(I) ((I) << FIXED_SHIFT)
428 #define FixedToInt(X) ((X) >> FIXED_SHIFT)
429 #define FixedToUns(X) (((unsigned int)(X)) >> FIXED_SHIFT)
430 #define FixedCeil(X) (((X) + FIXED_ONE - FIXED_EPSILON) & FIXED_INT_MASK)
431 #define FixedFloor(X) ((X) & FIXED_INT_MASK)
432 #define FixedToFloat(X) ((X) * (1.0F / FIXED_SCALE))
433 #define PosFloatToFixed(X) FloatToFixed(X)
434 #define SignedFloatToFixed(X) FloatToFixed(X)
439 * XXX these macros are just bandages for now in order to make
440 * CHAN_BITS==32 compile cleanly.
441 * These should probably go elsewhere at some point.
443 #if CHAN_TYPE == GL_FLOAT
444 #define ChanToFixed(X) (X)
445 #define FixedToChan(X) (X)
447 #define ChanToFixed(X) IntToFixed(X)
448 #define FixedToChan(X) FixedToInt(X)
453 * For looping over fragment attributes in the pointe, line
454 * triangle rasterizers.
456 #define ATTRIB_LOOP_BEGIN \
459 for (a = 0; a < swrast->_NumActiveAttribs; a++) { \
460 const GLuint attr = swrast->_ActiveAttribs[a];
462 #define ATTRIB_LOOP_END } }
466 * Return the address of a pixel value in a mapped renderbuffer.
468 static inline GLubyte
*
469 _swrast_pixel_address(struct gl_renderbuffer
*rb
, GLint x
, GLint y
)
471 struct swrast_renderbuffer
*srb
= swrast_renderbuffer(rb
);
472 const GLint bpp
= _mesa_get_format_bytes(rb
->Format
);
473 const GLint rowStride
= srb
->RowStride
;
476 /* NOTE: using <= only because of s_tritemp.h which gets a pixel
477 * address but doesn't necessarily access it.
479 assert(x
<= (GLint
) rb
->Width
);
480 assert(y
<= (GLint
) rb
->Height
);
482 return (GLubyte
*) srb
->Map
+ y
* rowStride
+ x
* bpp
;