1 /*
2 * Mesa 3-D graphics library
3 *
4 * Copyright (C) 1999-2007 Brian Paul All Rights Reserved.
5 *
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:
12 *
13 * The above copyright notice and this permission notice shall be included
14 * in all copies or substantial portions of the Software.
15 *
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.
23 */
24
25
26 /**
27 * \file swrast/s_context.h
28 * \brief Software rasterization context and private types.
29 * \author Keith Whitwell <keithw@vmware.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/mtypes.h"
47 #include "main/texcompress.h"
48 #include "program/prog_execute.h"
49 #include "swrast.h"
50 #include "s_fragprog.h"
51 #include "s_span.h"
52 #include "util/rounding.h"
53
54
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]);
60
61 typedef void (*swrast_blend_func)(struct gl_context *ctx, GLuint n,
62 const GLubyte mask[],
63 GLvoid *src, const GLvoid *dst,
64 GLenum chanType);
65
66 typedef void (*swrast_point_func)( struct gl_context *ctx, const SWvertex *);
67
68 typedef void (*swrast_line_func)( struct gl_context *ctx,
69 const SWvertex *, const SWvertex *);
70
71 typedef void (*swrast_tri_func)( struct gl_context *ctx, const SWvertex *,
72 const SWvertex *, const SWvertex *);
73
74
75 typedef void (*validate_texture_image_func)(struct gl_context *ctx,
76 struct gl_texture_object *texObj,
77 GLuint face, GLuint level);
78
79
80 /**
81 * \defgroup Bitmasks
82 * Bitmasks to indicate which rasterization options are enabled
83 * (RasterMask)
84 */
85 /*@{*/
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] */
101 /*@}*/
102
103 #define _SWRAST_NEW_RASTERMASK (_NEW_BUFFERS| \
104 _NEW_SCISSOR| \
105 _NEW_COLOR| \
106 _NEW_DEPTH| \
107 _NEW_FOG| \
108 _NEW_PROGRAM| \
109 _NEW_STENCIL| \
110 _NEW_TEXTURE| \
111 _NEW_VIEWPORT| \
112 _NEW_DEPTH)
113
114
115 struct swrast_texture_image;
116
117
118 /**
119 * Fetch a texel from texture image at given position.
120 */
121 typedef void (*FetchTexelFunc)(const struct swrast_texture_image *texImage,
122 GLint col, GLint row, GLint img,
123 GLfloat *texelOut);
124
125
126 /**
127 * Subclass of gl_texture_image.
128 * We need extra fields/info to keep tracking of mapped texture buffers,
129 * strides and Fetch functions.
130 */
131 struct swrast_texture_image
132 {
133 struct gl_texture_image Base;
134
135 GLboolean _IsPowerOfTwo; /**< Are all dimensions powers of two? */
136
137 /** used for mipmap LOD computation */
138 GLfloat WidthScale, HeightScale, DepthScale;
139
140 /**
141 * Byte stride between rows in ImageSlices.
142 *
143 * For compressed textures, this is the byte stride between one row of
144 * blocks and the next row of blocks.
145 *
146 * Only valid while one of the ImageSlices is mapped, and must be the same
147 * between all slices.
148 */
149 GLint RowStride;
150 /**
151 * When a texture image is mapped for swrast, this array contains pointers
152 * to the beginning of each slice.
153 *
154 * For swrast-allocated textures, these pointers will always stay
155 * initialized to point within Buffer.
156 */
157 void **ImageSlices;
158
159 /** Malloc'd texture memory */
160 GLubyte *Buffer;
161
162 FetchTexelFunc FetchTexel;
163
164 /** For fetching texels from compressed textures */
165 compressed_fetch_func FetchCompressedTexel;
166 };
167
168
169 /** cast wrapper */
170 static inline struct swrast_texture_image *
swrast_texture_image(struct gl_texture_image * img)171 swrast_texture_image(struct gl_texture_image *img)
172 {
173 return (struct swrast_texture_image *) img;
174 }
175
176 /** cast wrapper */
177 static inline const struct swrast_texture_image *
swrast_texture_image_const(const struct gl_texture_image * img)178 swrast_texture_image_const(const struct gl_texture_image *img)
179 {
180 return (const struct swrast_texture_image *) img;
181 }
182
183
184 /**
185 * Subclass of gl_renderbuffer with extra fields needed for software
186 * rendering.
187 */
188 struct swrast_renderbuffer
189 {
190 struct gl_renderbuffer Base;
191
192 GLubyte *Buffer; /**< The malloc'd memory for buffer */
193
194 /** These fields are only valid while buffer is mapped for rendering */
195 GLubyte *Map;
196 GLint RowStride; /**< in bytes */
197
198 /** For span rendering */
199 GLenum ColorType;
200 };
201
202
203 /** cast wrapper */
204 static inline struct swrast_renderbuffer *
swrast_renderbuffer(struct gl_renderbuffer * img)205 swrast_renderbuffer(struct gl_renderbuffer *img)
206 {
207 return (struct swrast_renderbuffer *) img;
208 }
209
210
211
212 /**
213 * \struct SWcontext
214 * \brief Per-context state that's private to the software rasterizer module.
215 */
216 typedef struct
217 {
218 /** Driver interface:
219 */
220 struct swrast_device_driver Driver;
221
222 /** Configuration mechanisms to make software rasterizer match
223 * characteristics of the hardware rasterizer (if present):
224 */
225 GLboolean AllowVertexFog;
226 GLboolean AllowPixelFog;
227
228 /** Derived values, invalidated on statechanges, updated from
229 * _swrast_validate_derived():
230 */
231 GLbitfield _RasterMask;
232 GLfloat _BackfaceSign; /** +1 or -1 */
233 GLfloat _BackfaceCullSign; /** +1, 0, or -1 */
234 GLboolean _PreferPixelFog; /* Compute fog blend factor per fragment? */
235 GLboolean _TextureCombinePrimary;
236 GLboolean _FogEnabled;
237 GLboolean _DeferredTexture;
238
239 /** List/array of the fragment attributes to interpolate */
240 GLuint _ActiveAttribs[VARYING_SLOT_MAX];
241 /** Same info, but as a bitmask of VARYING_BIT_x bits */
242 GLbitfield64 _ActiveAttribMask;
243 /** Number of fragment attributes to interpolate */
244 GLuint _NumActiveAttribs;
245 /** Indicates how each attrib is to be interpolated (lines/tris) */
246 GLenum _InterpMode[VARYING_SLOT_MAX]; /* GL_FLAT or GL_SMOOTH (for now) */
247
248 /* Working values:
249 */
250 GLuint StippleCounter; /**< Line stipple counter */
251 GLuint PointLineFacing;
252 GLbitfield NewState;
253 GLuint StateChanges;
254 GLenum Primitive; /* current primitive being drawn (ala glBegin) */
255 GLboolean SpecularVertexAdd; /**< Add specular/secondary color per vertex */
256
257 void (*InvalidateState)( struct gl_context *ctx, GLbitfield new_state );
258
259 /**
260 * When the NewState mask intersects these masks, we invalidate the
261 * Point/Line/Triangle function pointers below.
262 */
263 /*@{*/
264 GLbitfield InvalidatePointMask;
265 GLbitfield InvalidateLineMask;
266 GLbitfield InvalidateTriangleMask;
267 /*@}*/
268
269 /**
270 * Device drivers plug in functions for these callbacks.
271 * Will be called when the GL state change mask intersects the above masks.
272 */
273 /*@{*/
274 void (*choose_point)( struct gl_context * );
275 void (*choose_line)( struct gl_context * );
276 void (*choose_triangle)( struct gl_context * );
277 /*@}*/
278
279 /**
280 * Current point, line and triangle drawing functions.
281 */
282 /*@{*/
283 swrast_point_func Point;
284 swrast_line_func Line;
285 swrast_tri_func Triangle;
286 /*@}*/
287
288 /**
289 * Placeholders for when separate specular (or secondary color) is
290 * enabled but texturing is not.
291 */
292 /*@{*/
293 swrast_point_func SpecPoint;
294 swrast_line_func SpecLine;
295 swrast_tri_func SpecTriangle;
296 /*@}*/
297
298 /**
299 * Typically, we'll allocate a sw_span structure as a local variable
300 * and set its 'array' pointer to point to this object. The reason is
301 * this object is big and causes problems when allocated on the stack
302 * on some systems.
303 */
304 SWspanarrays *SpanArrays;
305 SWspanarrays *ZoomedArrays; /**< For pixel zooming */
306
307 /**
308 * Used to buffer N GL_POINTS, instead of rendering one by one.
309 */
310 SWspan PointSpan;
311
312 /** Internal hooks, kept up to date by the same mechanism as above.
313 */
314 swrast_blend_func BlendFunc;
315 texture_sample_func TextureSample[MAX_COMBINED_TEXTURE_IMAGE_UNITS];
316
317 /** Buffer for saving the sampled texture colors.
318 * Needed for GL_ARB_texture_env_crossbar implementation.
319 */
320 GLfloat *TexelBuffer;
321
322 validate_texture_image_func ValidateTextureImage;
323
324 /** State used during execution of fragment programs */
325 struct gl_program_machine FragProgMachine;
326
327 /** Temporary arrays for stencil operations. To avoid large stack
328 * allocations.
329 */
330 struct {
331 GLubyte *buf1, *buf2, *buf3, *buf4;
332 } stencil_temp;
333
334 } SWcontext;
335
336
337 extern void
338 _swrast_validate_derived( struct gl_context *ctx );
339
340 extern void
341 _swrast_update_texture_samplers(struct gl_context *ctx);
342
343
344 /** Return SWcontext for the given struct gl_context */
345 static inline SWcontext *
SWRAST_CONTEXT(struct gl_context * ctx)346 SWRAST_CONTEXT(struct gl_context *ctx)
347 {
348 return (SWcontext *) ctx->swrast_context;
349 }
350
351 /** const version of above */
352 static inline const SWcontext *
CONST_SWRAST_CONTEXT(const struct gl_context * ctx)353 CONST_SWRAST_CONTEXT(const struct gl_context *ctx)
354 {
355 return (const SWcontext *) ctx->swrast_context;
356 }
357
358
359 /**
360 * Called prior to framebuffer reading/writing.
361 * For drivers that rely on swrast for fallback rendering, this is the
362 * driver's opportunity to map renderbuffers and textures.
363 */
364 static inline void
swrast_render_start(struct gl_context * ctx)365 swrast_render_start(struct gl_context *ctx)
366 {
367 SWcontext *swrast = SWRAST_CONTEXT(ctx);
368 if (swrast->Driver.SpanRenderStart)
369 swrast->Driver.SpanRenderStart(ctx);
370 }
371
372
373 /** Called after framebuffer reading/writing */
374 static inline void
swrast_render_finish(struct gl_context * ctx)375 swrast_render_finish(struct gl_context *ctx)
376 {
377 SWcontext *swrast = SWRAST_CONTEXT(ctx);
378 if (swrast->Driver.SpanRenderFinish)
379 swrast->Driver.SpanRenderFinish(ctx);
380 }
381
382
383 extern void
384 _swrast_span_render_start(struct gl_context *ctx);
385
386 extern void
387 _swrast_span_render_finish(struct gl_context *ctx);
388
389 extern void
390 _swrast_map_textures(struct gl_context *ctx);
391
392 extern void
393 _swrast_unmap_textures(struct gl_context *ctx);
394
395 extern unsigned int
396 _swrast_teximage_slice_height(struct gl_texture_image *texImage);
397
398 extern void
399 _swrast_map_texture(struct gl_context *ctx, struct gl_texture_object *texObj);
400
401 extern void
402 _swrast_unmap_texture(struct gl_context *ctx, struct gl_texture_object *texObj);
403
404
405 extern void
406 _swrast_map_renderbuffers(struct gl_context *ctx);
407
408 extern void
409 _swrast_unmap_renderbuffers(struct gl_context *ctx);
410
411
412 /**
413 * Size of an RGBA pixel, in bytes, for given datatype.
414 */
415 #define RGBA_PIXEL_SIZE(TYPE) \
416 ((TYPE == GL_UNSIGNED_BYTE) ? 4 * sizeof(GLubyte) : \
417 ((TYPE == GL_UNSIGNED_SHORT) ? 4 * sizeof(GLushort) \
418 : 4 * sizeof(GLfloat)))
419
420
421
422 /*
423 * Fixed point arithmetic macros
424 */
425 #ifndef FIXED_FRAC_BITS
426 #define FIXED_FRAC_BITS 11
427 #endif
428
429 #define FIXED_SHIFT FIXED_FRAC_BITS
430 #define FIXED_ONE (1 << FIXED_SHIFT)
431 #define FIXED_HALF (1 << (FIXED_SHIFT-1))
432 #define FIXED_FRAC_MASK (FIXED_ONE - 1)
433 #define FIXED_INT_MASK (~FIXED_FRAC_MASK)
434 #define FIXED_EPSILON 1
435 #define FIXED_SCALE ((float) FIXED_ONE)
436 #define FIXED_DBL_SCALE ((double) FIXED_ONE)
437 #define FloatToFixed(X) (lroundf((X) * FIXED_SCALE))
438 #define FixedToDouble(X) ((X) * (1.0 / FIXED_DBL_SCALE))
439 #define IntToFixed(I) ((I) << FIXED_SHIFT)
440 #define FixedToInt(X) ((X) >> FIXED_SHIFT)
441 #define FixedToUns(X) (((unsigned int)(X)) >> FIXED_SHIFT)
442 #define FixedCeil(X) (((X) + FIXED_ONE - FIXED_EPSILON) & FIXED_INT_MASK)
443 #define FixedFloor(X) ((X) & FIXED_INT_MASK)
444 #define FixedToFloat(X) ((X) * (1.0F / FIXED_SCALE))
445 #define PosFloatToFixed(X) FloatToFixed(X)
446 #define SignedFloatToFixed(X) FloatToFixed(X)
447
448
449
450 /*
451 * XXX these macros are just bandages for now in order to make
452 * CHAN_BITS==32 compile cleanly.
453 * These should probably go elsewhere at some point.
454 */
455 #if CHAN_TYPE == GL_FLOAT
456 #define ChanToFixed(X) (X)
457 #define FixedToChan(X) (X)
458 #else
459 #define ChanToFixed(X) IntToFixed(X)
460 #define FixedToChan(X) FixedToInt(X)
461 #endif
462
463
464 /**
465 * For looping over fragment attributes in the pointe, line
466 * triangle rasterizers.
467 */
468 #define ATTRIB_LOOP_BEGIN \
469 { \
470 GLuint a; \
471 for (a = 0; a < swrast->_NumActiveAttribs; a++) { \
472 const GLuint attr = swrast->_ActiveAttribs[a];
473
474 #define ATTRIB_LOOP_END } }
475
476
477 /**
478 * Return the address of a pixel value in a mapped renderbuffer.
479 */
480 static inline GLubyte *
_swrast_pixel_address(struct gl_renderbuffer * rb,GLint x,GLint y)481 _swrast_pixel_address(struct gl_renderbuffer *rb, GLint x, GLint y)
482 {
483 struct swrast_renderbuffer *srb = swrast_renderbuffer(rb);
484 const GLint bpp = _mesa_get_format_bytes(rb->Format);
485 const GLint rowStride = srb->RowStride;
486 assert(x >= 0);
487 assert(y >= 0);
488 /* NOTE: using <= only because of s_tritemp.h which gets a pixel
489 * address but doesn't necessarily access it.
490 */
491 assert(x <= (GLint) rb->Width);
492 assert(y <= (GLint) rb->Height);
493 assert(srb->Map);
494 return (GLubyte *) srb->Map + y * rowStride + x * bpp;
495 }
496
497
498
499 #endif
500