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