2 * Mesa 3-D graphics library
4 * Copyright (C) 1999-2007 Brian Paul All Rights Reserved.
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included
14 * in all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
20 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22 * OTHER DEALINGS IN THE SOFTWARE.
27 * \file swrast/s_context.h
28 * \brief Software rasterization context and private types.
29 * \author Keith Whitwell <keithw@vmware.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/mtypes.h"
47 #include "main/texcompress.h"
48 #include "program/prog_execute.h"
50 #include "s_fragprog.h"
54 typedef void (*texture_sample_func
)(struct gl_context
*ctx
,
55 const struct gl_sampler_object
*samp
,
56 const struct gl_texture_object
*tObj
,
57 GLuint n
, const GLfloat texcoords
[][4],
58 const GLfloat lambda
[], GLfloat rgba
[][4]);
60 typedef void (*swrast_blend_func
)(struct gl_context
*ctx
, GLuint n
,
62 GLvoid
*src
, const GLvoid
*dst
,
65 typedef void (*swrast_point_func
)( struct gl_context
*ctx
, const SWvertex
*);
67 typedef void (*swrast_line_func
)( struct gl_context
*ctx
,
68 const SWvertex
*, const SWvertex
*);
70 typedef void (*swrast_tri_func
)( struct gl_context
*ctx
, const SWvertex
*,
71 const SWvertex
*, const SWvertex
*);
74 typedef void (*validate_texture_image_func
)(struct gl_context
*ctx
,
75 struct gl_texture_object
*texObj
,
76 GLuint face
, GLuint level
);
81 * Bitmasks to indicate which rasterization options are enabled
85 #define ALPHATEST_BIT 0x001 /**< Alpha-test pixels */
86 #define BLEND_BIT 0x002 /**< Blend pixels */
87 #define DEPTH_BIT 0x004 /**< Depth-test pixels */
88 #define FOG_BIT 0x008 /**< Fog pixels */
89 #define LOGIC_OP_BIT 0x010 /**< Apply logic op in software */
90 #define CLIP_BIT 0x020 /**< Scissor or window clip pixels */
91 #define STENCIL_BIT 0x040 /**< Stencil pixels */
92 #define MASKING_BIT 0x080 /**< Do glColorMask or glIndexMask */
93 #define MULTI_DRAW_BIT 0x400 /**< Write to more than one color- */
94 /**< buffer or no buffers. */
95 #define OCCLUSION_BIT 0x800 /**< GL_HP_occlusion_test enabled */
96 #define TEXTURE_BIT 0x1000 /**< Texturing really enabled */
97 #define FRAGPROG_BIT 0x2000 /**< Fragment program enabled */
98 #define ATIFRAGSHADER_BIT 0x4000 /**< ATI Fragment shader enabled */
99 #define CLAMPING_BIT 0x8000 /**< Clamp colors to [0,1] */
102 #define _SWRAST_NEW_RASTERMASK (_NEW_BUFFERS| \
114 struct swrast_texture_image
;
118 * Fetch a texel from texture image at given position.
120 typedef void (*FetchTexelFunc
)(const struct swrast_texture_image
*texImage
,
121 GLint col
, GLint row
, GLint img
,
126 * Subclass of gl_texture_image.
127 * We need extra fields/info to keep tracking of mapped texture buffers,
128 * strides and Fetch functions.
130 struct swrast_texture_image
132 struct gl_texture_image Base
;
134 GLboolean _IsPowerOfTwo
; /**< Are all dimensions powers of two? */
136 /** used for mipmap LOD computation */
137 GLfloat WidthScale
, HeightScale
, DepthScale
;
140 * Byte stride between rows in ImageSlices.
142 * For compressed textures, this is the byte stride between one row of
143 * blocks and the next row of blocks.
145 * Only valid while one of the ImageSlices is mapped, and must be the same
146 * between all slices.
150 * When a texture image is mapped for swrast, this array contains pointers
151 * to the beginning of each slice.
153 * For swrast-allocated textures, these pointers will always stay
154 * initialized to point within Buffer.
158 /** Malloc'd texture memory */
161 FetchTexelFunc FetchTexel
;
163 /** For fetching texels from compressed textures */
164 compressed_fetch_func FetchCompressedTexel
;
169 static inline struct swrast_texture_image
*
170 swrast_texture_image(struct gl_texture_image
*img
)
172 return (struct swrast_texture_image
*) img
;
176 static inline const struct swrast_texture_image
*
177 swrast_texture_image_const(const struct gl_texture_image
*img
)
179 return (const struct swrast_texture_image
*) img
;
184 * Subclass of gl_renderbuffer with extra fields needed for software
187 struct swrast_renderbuffer
189 struct gl_renderbuffer Base
;
191 GLubyte
*Buffer
; /**< The malloc'd memory for buffer */
193 /** These fields are only valid while buffer is mapped for rendering */
195 GLint RowStride
; /**< in bytes */
197 /** For span rendering */
203 static inline struct swrast_renderbuffer
*
204 swrast_renderbuffer(struct gl_renderbuffer
*img
)
206 return (struct swrast_renderbuffer
*) img
;
213 * \brief Per-context state that's private to the software rasterizer module.
217 /** Driver interface:
219 struct swrast_device_driver Driver
;
221 /** Configuration mechanisms to make software rasterizer match
222 * characteristics of the hardware rasterizer (if present):
224 GLboolean AllowVertexFog
;
225 GLboolean AllowPixelFog
;
227 /** Derived values, invalidated on statechanges, updated from
228 * _swrast_validate_derived():
230 GLbitfield _RasterMask
;
231 GLfloat _BackfaceSign
; /** +1 or -1 */
232 GLfloat _BackfaceCullSign
; /** +1, 0, or -1 */
233 GLboolean _PreferPixelFog
; /* Compute fog blend factor per fragment? */
234 GLboolean _TextureCombinePrimary
;
235 GLboolean _FogEnabled
;
236 GLboolean _DeferredTexture
;
238 /** List/array of the fragment attributes to interpolate */
239 GLuint _ActiveAttribs
[VARYING_SLOT_MAX
];
240 /** Same info, but as a bitmask of VARYING_BIT_x bits */
241 GLbitfield64 _ActiveAttribMask
;
242 /** Number of fragment attributes to interpolate */
243 GLuint _NumActiveAttribs
;
244 /** Indicates how each attrib is to be interpolated (lines/tris) */
245 GLenum _InterpMode
[VARYING_SLOT_MAX
]; /* GL_FLAT or GL_SMOOTH (for now) */
249 GLuint StippleCounter
; /**< Line stipple counter */
250 GLuint PointLineFacing
;
253 GLenum Primitive
; /* current primitive being drawn (ala glBegin) */
254 GLboolean SpecularVertexAdd
; /**< Add specular/secondary color per vertex */
256 void (*InvalidateState
)( struct gl_context
*ctx
, GLbitfield new_state
);
259 * When the NewState mask intersects these masks, we invalidate the
260 * Point/Line/Triangle function pointers below.
263 GLbitfield InvalidatePointMask
;
264 GLbitfield InvalidateLineMask
;
265 GLbitfield InvalidateTriangleMask
;
269 * Device drivers plug in functions for these callbacks.
270 * Will be called when the GL state change mask intersects the above masks.
273 void (*choose_point
)( struct gl_context
* );
274 void (*choose_line
)( struct gl_context
* );
275 void (*choose_triangle
)( struct gl_context
* );
279 * Current point, line and triangle drawing functions.
282 swrast_point_func Point
;
283 swrast_line_func Line
;
284 swrast_tri_func Triangle
;
288 * Placeholders for when separate specular (or secondary color) is
289 * enabled but texturing is not.
292 swrast_point_func SpecPoint
;
293 swrast_line_func SpecLine
;
294 swrast_tri_func SpecTriangle
;
298 * Typically, we'll allocate a sw_span structure as a local variable
299 * and set its 'array' pointer to point to this object. The reason is
300 * this object is big and causes problems when allocated on the stack
303 SWspanarrays
*SpanArrays
;
304 SWspanarrays
*ZoomedArrays
; /**< For pixel zooming */
307 * Used to buffer N GL_POINTS, instead of rendering one by one.
311 /** Internal hooks, kept up to date by the same mechanism as above.
313 swrast_blend_func BlendFunc
;
314 texture_sample_func TextureSample
[MAX_COMBINED_TEXTURE_IMAGE_UNITS
];
316 /** Buffer for saving the sampled texture colors.
317 * Needed for GL_ARB_texture_env_crossbar implementation.
319 GLfloat
*TexelBuffer
;
321 validate_texture_image_func ValidateTextureImage
;
323 /** State used during execution of fragment programs */
324 struct gl_program_machine FragProgMachine
;
326 /** Temporary arrays for stencil operations. To avoid large stack
330 GLubyte
*buf1
, *buf2
, *buf3
, *buf4
;
337 _swrast_validate_derived( struct gl_context
*ctx
);
340 _swrast_update_texture_samplers(struct gl_context
*ctx
);
343 /** Return SWcontext for the given struct gl_context */
344 static inline SWcontext
*
345 SWRAST_CONTEXT(struct gl_context
*ctx
)
347 return (SWcontext
*) ctx
->swrast_context
;
350 /** const version of above */
351 static inline const SWcontext
*
352 CONST_SWRAST_CONTEXT(const struct gl_context
*ctx
)
354 return (const SWcontext
*) ctx
->swrast_context
;
359 * Called prior to framebuffer reading/writing.
360 * For drivers that rely on swrast for fallback rendering, this is the
361 * driver's opportunity to map renderbuffers and textures.
364 swrast_render_start(struct gl_context
*ctx
)
366 SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
367 if (swrast
->Driver
.SpanRenderStart
)
368 swrast
->Driver
.SpanRenderStart(ctx
);
372 /** Called after framebuffer reading/writing */
374 swrast_render_finish(struct gl_context
*ctx
)
376 SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
377 if (swrast
->Driver
.SpanRenderFinish
)
378 swrast
->Driver
.SpanRenderFinish(ctx
);
383 _swrast_span_render_start(struct gl_context
*ctx
);
386 _swrast_span_render_finish(struct gl_context
*ctx
);
389 _swrast_map_textures(struct gl_context
*ctx
);
392 _swrast_unmap_textures(struct gl_context
*ctx
);
395 _swrast_teximage_slice_height(struct gl_texture_image
*texImage
);
398 _swrast_map_texture(struct gl_context
*ctx
, struct gl_texture_object
*texObj
);
401 _swrast_unmap_texture(struct gl_context
*ctx
, struct gl_texture_object
*texObj
);
405 _swrast_map_renderbuffers(struct gl_context
*ctx
);
408 _swrast_unmap_renderbuffers(struct gl_context
*ctx
);
412 * Size of an RGBA pixel, in bytes, for given datatype.
414 #define RGBA_PIXEL_SIZE(TYPE) \
415 ((TYPE == GL_UNSIGNED_BYTE) ? 4 * sizeof(GLubyte) : \
416 ((TYPE == GL_UNSIGNED_SHORT) ? 4 * sizeof(GLushort) \
417 : 4 * sizeof(GLfloat)))
422 * Fixed point arithmetic macros
424 #ifndef FIXED_FRAC_BITS
425 #define FIXED_FRAC_BITS 11
428 #define FIXED_SHIFT FIXED_FRAC_BITS
429 #define FIXED_ONE (1 << FIXED_SHIFT)
430 #define FIXED_HALF (1 << (FIXED_SHIFT-1))
431 #define FIXED_FRAC_MASK (FIXED_ONE - 1)
432 #define FIXED_INT_MASK (~FIXED_FRAC_MASK)
433 #define FIXED_EPSILON 1
434 #define FIXED_SCALE ((float) FIXED_ONE)
435 #define FIXED_DBL_SCALE ((double) FIXED_ONE)
436 #define FloatToFixed(X) (IROUND((X) * FIXED_SCALE))
437 #define FixedToDouble(X) ((X) * (1.0 / FIXED_DBL_SCALE))
438 #define IntToFixed(I) ((I) << FIXED_SHIFT)
439 #define FixedToInt(X) ((X) >> FIXED_SHIFT)
440 #define FixedToUns(X) (((unsigned int)(X)) >> FIXED_SHIFT)
441 #define FixedCeil(X) (((X) + FIXED_ONE - FIXED_EPSILON) & FIXED_INT_MASK)
442 #define FixedFloor(X) ((X) & FIXED_INT_MASK)
443 #define FixedToFloat(X) ((X) * (1.0F / FIXED_SCALE))
444 #define PosFloatToFixed(X) FloatToFixed(X)
445 #define SignedFloatToFixed(X) FloatToFixed(X)
450 * XXX these macros are just bandages for now in order to make
451 * CHAN_BITS==32 compile cleanly.
452 * These should probably go elsewhere at some point.
454 #if CHAN_TYPE == GL_FLOAT
455 #define ChanToFixed(X) (X)
456 #define FixedToChan(X) (X)
458 #define ChanToFixed(X) IntToFixed(X)
459 #define FixedToChan(X) FixedToInt(X)
464 * For looping over fragment attributes in the pointe, line
465 * triangle rasterizers.
467 #define ATTRIB_LOOP_BEGIN \
470 for (a = 0; a < swrast->_NumActiveAttribs; a++) { \
471 const GLuint attr = swrast->_ActiveAttribs[a];
473 #define ATTRIB_LOOP_END } }
477 * Return the address of a pixel value in a mapped renderbuffer.
479 static inline GLubyte
*
480 _swrast_pixel_address(struct gl_renderbuffer
*rb
, GLint x
, GLint y
)
482 struct swrast_renderbuffer
*srb
= swrast_renderbuffer(rb
);
483 const GLint bpp
= _mesa_get_format_bytes(rb
->Format
);
484 const GLint rowStride
= srb
->RowStride
;
487 /* NOTE: using <= only because of s_tritemp.h which gets a pixel
488 * address but doesn't necessarily access it.
490 assert(x
<= (GLint
) rb
->Width
);
491 assert(y
<= (GLint
) rb
->Height
);
493 return (GLubyte
*) srb
->Map
+ y
* rowStride
+ x
* bpp
;