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