s_context.h revision 01e04c3f
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
53
54typedef 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]);
59
60typedef void (*blend_func)(struct gl_context *ctx, GLuint n,
61                           const GLubyte mask[],
62                           GLvoid *src, const GLvoid *dst,
63                           GLenum chanType);
64
65typedef void (*swrast_point_func)( struct gl_context *ctx, const SWvertex *);
66
67typedef void (*swrast_line_func)( struct gl_context *ctx,
68                                  const SWvertex *, const SWvertex *);
69
70typedef void (*swrast_tri_func)( struct gl_context *ctx, const SWvertex *,
71                                 const SWvertex *, const SWvertex *);
72
73
74typedef void (*validate_texture_image_func)(struct gl_context *ctx,
75                                            struct gl_texture_object *texObj,
76                                            GLuint face, GLuint level);
77
78
79/**
80 * \defgroup Bitmasks
81 * Bitmasks to indicate which rasterization options are enabled
82 * (RasterMask)
83 */
84/*@{*/
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] */
100/*@}*/
101
102#define _SWRAST_NEW_RASTERMASK (_NEW_BUFFERS|	\
103			        _NEW_SCISSOR|	\
104			        _NEW_COLOR|	\
105			        _NEW_DEPTH|	\
106			        _NEW_FOG|	\
107                                _NEW_PROGRAM|   \
108			        _NEW_STENCIL|	\
109			        _NEW_TEXTURE|	\
110			        _NEW_VIEWPORT|	\
111			        _NEW_DEPTH)
112
113
114struct swrast_texture_image;
115
116
117/**
118 * Fetch a texel from texture image at given position.
119 */
120typedef void (*FetchTexelFunc)(const struct swrast_texture_image *texImage,
121                               GLint col, GLint row, GLint img,
122                               GLfloat *texelOut);
123
124
125/**
126 * Subclass of gl_texture_image.
127 * We need extra fields/info to keep tracking of mapped texture buffers,
128 * strides and Fetch functions.
129 */
130struct swrast_texture_image
131{
132   struct gl_texture_image Base;
133
134   GLboolean _IsPowerOfTwo;  /**< Are all dimensions powers of two? */
135
136   /** used for mipmap LOD computation */
137   GLfloat WidthScale, HeightScale, DepthScale;
138
139   /**
140    * Byte stride between rows in ImageSlices.
141    *
142    * For compressed textures, this is the byte stride between one row of
143    * blocks and the next row of blocks.
144    *
145    * Only valid while one of the ImageSlices is mapped, and must be the same
146    * between all slices.
147    */
148   GLint RowStride;
149   /**
150    * When a texture image is mapped for swrast, this array contains pointers
151    * to the beginning of each slice.
152    *
153    * For swrast-allocated textures, these pointers will always stay
154    * initialized to point within Buffer.
155    */
156   void **ImageSlices;
157
158   /** Malloc'd texture memory */
159   GLubyte *Buffer;
160
161   FetchTexelFunc FetchTexel;
162
163   /** For fetching texels from compressed textures */
164   compressed_fetch_func FetchCompressedTexel;
165};
166
167
168/** cast wrapper */
169static inline struct swrast_texture_image *
170swrast_texture_image(struct gl_texture_image *img)
171{
172   return (struct swrast_texture_image *) img;
173}
174
175/** cast wrapper */
176static inline const struct swrast_texture_image *
177swrast_texture_image_const(const struct gl_texture_image *img)
178{
179   return (const struct swrast_texture_image *) img;
180}
181
182
183/**
184 * Subclass of gl_renderbuffer with extra fields needed for software
185 * rendering.
186 */
187struct swrast_renderbuffer
188{
189   struct gl_renderbuffer Base;
190
191   GLubyte *Buffer;     /**< The malloc'd memory for buffer */
192
193   /** These fields are only valid while buffer is mapped for rendering */
194   GLubyte *Map;
195   GLint RowStride;    /**< in bytes */
196
197   /** For span rendering */
198   GLenum ColorType;
199};
200
201
202/** cast wrapper */
203static inline struct swrast_renderbuffer *
204swrast_renderbuffer(struct gl_renderbuffer *img)
205{
206   return (struct swrast_renderbuffer *) img;
207}
208
209
210
211/**
212 * \struct SWcontext
213 * \brief  Per-context state that's private to the software rasterizer module.
214 */
215typedef struct
216{
217   /** Driver interface:
218    */
219   struct swrast_device_driver Driver;
220
221   /** Configuration mechanisms to make software rasterizer match
222    * characteristics of the hardware rasterizer (if present):
223    */
224   GLboolean AllowVertexFog;
225   GLboolean AllowPixelFog;
226
227   /** Derived values, invalidated on statechanges, updated from
228    * _swrast_validate_derived():
229    */
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;
237
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) */
246
247   /* Working values:
248    */
249   GLuint StippleCounter;    /**< Line stipple counter */
250   GLuint PointLineFacing;
251   GLbitfield NewState;
252   GLuint StateChanges;
253   GLenum Primitive;    /* current primitive being drawn (ala glBegin) */
254   GLboolean SpecularVertexAdd; /**< Add specular/secondary color per vertex */
255
256   void (*InvalidateState)( struct gl_context *ctx, GLbitfield new_state );
257
258   /**
259    * When the NewState mask intersects these masks, we invalidate the
260    * Point/Line/Triangle function pointers below.
261    */
262   /*@{*/
263   GLbitfield InvalidatePointMask;
264   GLbitfield InvalidateLineMask;
265   GLbitfield InvalidateTriangleMask;
266   /*@}*/
267
268   /**
269    * Device drivers plug in functions for these callbacks.
270    * Will be called when the GL state change mask intersects the above masks.
271    */
272   /*@{*/
273   void (*choose_point)( struct gl_context * );
274   void (*choose_line)( struct gl_context * );
275   void (*choose_triangle)( struct gl_context * );
276   /*@}*/
277
278   /**
279    * Current point, line and triangle drawing functions.
280    */
281   /*@{*/
282   swrast_point_func Point;
283   swrast_line_func Line;
284   swrast_tri_func Triangle;
285   /*@}*/
286
287   /**
288    * Placeholders for when separate specular (or secondary color) is
289    * enabled but texturing is not.
290    */
291   /*@{*/
292   swrast_point_func SpecPoint;
293   swrast_line_func SpecLine;
294   swrast_tri_func SpecTriangle;
295   /*@}*/
296
297   /**
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
301    * on some systems.
302    */
303   SWspanarrays *SpanArrays;
304   SWspanarrays *ZoomedArrays;  /**< For pixel zooming */
305
306   /**
307    * Used to buffer N GL_POINTS, instead of rendering one by one.
308    */
309   SWspan PointSpan;
310
311   /** Internal hooks, kept up to date by the same mechanism as above.
312    */
313   blend_func BlendFunc;
314   texture_sample_func TextureSample[MAX_COMBINED_TEXTURE_IMAGE_UNITS];
315
316   /** Buffer for saving the sampled texture colors.
317    * Needed for GL_ARB_texture_env_crossbar implementation.
318    */
319   GLfloat *TexelBuffer;
320
321   validate_texture_image_func ValidateTextureImage;
322
323   /** State used during execution of fragment programs */
324   struct gl_program_machine FragProgMachine;
325
326   /** Temporary arrays for stencil operations.  To avoid large stack
327    * allocations.
328    */
329   struct {
330      GLubyte *buf1, *buf2, *buf3, *buf4;
331   } stencil_temp;
332
333} SWcontext;
334
335
336extern void
337_swrast_validate_derived( struct gl_context *ctx );
338
339extern void
340_swrast_update_texture_samplers(struct gl_context *ctx);
341
342
343/** Return SWcontext for the given struct gl_context */
344static inline SWcontext *
345SWRAST_CONTEXT(struct gl_context *ctx)
346{
347   return (SWcontext *) ctx->swrast_context;
348}
349
350/** const version of above */
351static inline const SWcontext *
352CONST_SWRAST_CONTEXT(const struct gl_context *ctx)
353{
354   return (const SWcontext *) ctx->swrast_context;
355}
356
357
358/**
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.
362 */
363static inline void
364swrast_render_start(struct gl_context *ctx)
365{
366   SWcontext *swrast = SWRAST_CONTEXT(ctx);
367   if (swrast->Driver.SpanRenderStart)
368      swrast->Driver.SpanRenderStart(ctx);
369}
370
371
372/** Called after framebuffer reading/writing */
373static inline void
374swrast_render_finish(struct gl_context *ctx)
375{
376   SWcontext *swrast = SWRAST_CONTEXT(ctx);
377   if (swrast->Driver.SpanRenderFinish)
378      swrast->Driver.SpanRenderFinish(ctx);
379}
380
381
382extern void
383_swrast_span_render_start(struct gl_context *ctx);
384
385extern void
386_swrast_span_render_finish(struct gl_context *ctx);
387
388extern void
389_swrast_map_textures(struct gl_context *ctx);
390
391extern void
392_swrast_unmap_textures(struct gl_context *ctx);
393
394extern unsigned int
395_swrast_teximage_slice_height(struct gl_texture_image *texImage);
396
397extern void
398_swrast_map_texture(struct gl_context *ctx, struct gl_texture_object *texObj);
399
400extern void
401_swrast_unmap_texture(struct gl_context *ctx, struct gl_texture_object *texObj);
402
403
404extern void
405_swrast_map_renderbuffers(struct gl_context *ctx);
406
407extern void
408_swrast_unmap_renderbuffers(struct gl_context *ctx);
409
410
411/**
412 * Size of an RGBA pixel, in bytes, for given datatype.
413 */
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)))
418
419
420
421/*
422 * Fixed point arithmetic macros
423 */
424#ifndef FIXED_FRAC_BITS
425#define FIXED_FRAC_BITS 11
426#endif
427
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)
446
447
448
449/*
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.
453 */
454#if CHAN_TYPE == GL_FLOAT
455#define ChanToFixed(X)  (X)
456#define FixedToChan(X)  (X)
457#else
458#define ChanToFixed(X)  IntToFixed(X)
459#define FixedToChan(X)  FixedToInt(X)
460#endif
461
462
463/**
464 * For looping over fragment attributes in the pointe, line
465 * triangle rasterizers.
466 */
467#define ATTRIB_LOOP_BEGIN                                \
468   {                                                     \
469      GLuint a;                                          \
470      for (a = 0; a < swrast->_NumActiveAttribs; a++) {  \
471         const GLuint attr = swrast->_ActiveAttribs[a];
472
473#define ATTRIB_LOOP_END } }
474
475
476/**
477 * Return the address of a pixel value in a mapped renderbuffer.
478 */
479static inline GLubyte *
480_swrast_pixel_address(struct gl_renderbuffer *rb, GLint x, GLint y)
481{
482   struct swrast_renderbuffer *srb = swrast_renderbuffer(rb);
483   const GLint bpp = _mesa_get_format_bytes(rb->Format);
484   const GLint rowStride = srb->RowStride;
485   assert(x >= 0);
486   assert(y >= 0);
487   /* NOTE: using <= only because of s_tritemp.h which gets a pixel
488    * address but doesn't necessarily access it.
489    */
490   assert(x <= (GLint) rb->Width);
491   assert(y <= (GLint) rb->Height);
492   assert(srb->Map);
493   return (GLubyte *) srb->Map + y * rowStride + x * bpp;
494}
495
496
497
498#endif
499