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