s_context.h revision 9520f483b8f1e45fa474674b415554988de5d8d3
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
53typedef 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
58typedef void (_ASMAPIP blend_func)( struct gl_context *ctx, GLuint n,
59                                    const GLubyte mask[],
60                                    GLvoid *src, const GLvoid *dst,
61                                    GLenum chanType);
62
63typedef void (*swrast_point_func)( struct gl_context *ctx, const SWvertex *);
64
65typedef void (*swrast_line_func)( struct gl_context *ctx,
66                                  const SWvertex *, const SWvertex *);
67
68typedef void (*swrast_tri_func)( struct gl_context *ctx, const SWvertex *,
69                                 const SWvertex *, const SWvertex *);
70
71
72typedef 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
112struct swrast_texture_image;
113
114
115typedef void (*FetchTexelFuncC)(const struct swrast_texture_image *texImage,
116                                GLint col, GLint row, GLint img,
117                                GLchan *texelOut);
118
119/**
120 * As above, but returns floats.
121 * Used for depth component images and for upcoming signed/float
122 * texture images.
123 */
124typedef void (*FetchTexelFuncF)(const struct swrast_texture_image *texImage,
125                                GLint col, GLint row, GLint img,
126                                GLfloat *texelOut);
127
128
129typedef void (*StoreTexelFunc)(struct swrast_texture_image *texImage,
130                               GLint col, GLint row, GLint img,
131                               const void *texel);
132
133/**
134 * Subclass of gl_texture_image.
135 * We need extra fields/info to keep tracking of mapped texture buffers,
136 * strides and Fetch/Store functions.
137 */
138struct swrast_texture_image
139{
140   struct gl_texture_image Base;
141
142   GLboolean _IsPowerOfTwo;  /**< Are all dimensions powers of two? */
143
144   /** used for mipmap LOD computation */
145   GLfloat WidthScale, HeightScale, DepthScale;
146
147#if 0
148   GLubyte *Data;    /**< The actual texture data in malloc'd memory */
149
150   GLint TexelSize;  /**< bytes per texel block */
151#endif
152
153   FetchTexelFuncC FetchTexelc;
154   FetchTexelFuncF FetchTexelf;
155   StoreTexelFunc Store;
156
157#if 0
158   /** These fields only valid when texture memory is mapped */
159   GLubyte **SliceMaps;  /**< points to OneMap or a malloc'd array */
160   GLint RowStride;  /**< bytes per row of blocks */
161#endif
162};
163
164
165/** cast wrapper */
166static inline struct swrast_texture_image *
167swrast_texture_image(struct gl_texture_image *img)
168{
169   return (struct swrast_texture_image *) img;
170}
171
172/** cast wrapper */
173static inline const struct swrast_texture_image *
174swrast_texture_image_const(const struct gl_texture_image *img)
175{
176   return (const struct swrast_texture_image *) img;
177}
178
179
180/**
181 * \struct SWcontext
182 * \brief  Per-context state that's private to the software rasterizer module.
183 */
184typedef struct
185{
186   /** Driver interface:
187    */
188   struct swrast_device_driver Driver;
189
190   /** Configuration mechanisms to make software rasterizer match
191    * characteristics of the hardware rasterizer (if present):
192    */
193   GLboolean AllowVertexFog;
194   GLboolean AllowPixelFog;
195
196   /** Derived values, invalidated on statechanges, updated from
197    * _swrast_validate_derived():
198    */
199   GLbitfield _RasterMask;
200   GLfloat _BackfaceSign;      /** +1 or -1 */
201   GLfloat _BackfaceCullSign;  /** +1, 0, or -1 */
202   GLboolean _PreferPixelFog;    /* Compute fog blend factor per fragment? */
203   GLboolean _TextureCombinePrimary;
204   GLboolean _FogEnabled;
205   GLboolean _DeferredTexture;
206
207   /** List/array of the fragment attributes to interpolate */
208   GLuint _ActiveAttribs[FRAG_ATTRIB_MAX];
209   /** Same info, but as a bitmask */
210   GLbitfield _ActiveAttribMask;
211   /** Number of fragment attributes to interpolate */
212   GLuint _NumActiveAttribs;
213   /** Indicates how each attrib is to be interpolated (lines/tris) */
214   GLenum _InterpMode[FRAG_ATTRIB_MAX]; /* GL_FLAT or GL_SMOOTH (for now) */
215
216   /* Accum buffer temporaries.
217    */
218   GLboolean _IntegerAccumMode;	/**< Storing unscaled integers? */
219   GLfloat _IntegerAccumScaler;	/**< Implicit scale factor */
220
221   /* Working values:
222    */
223   GLuint StippleCounter;    /**< Line stipple counter */
224   GLuint PointLineFacing;
225   GLbitfield NewState;
226   GLuint StateChanges;
227   GLenum Primitive;    /* current primitive being drawn (ala glBegin) */
228   GLboolean SpecularVertexAdd; /**< Add specular/secondary color per vertex */
229
230   void (*InvalidateState)( struct gl_context *ctx, GLbitfield new_state );
231
232   /**
233    * When the NewState mask intersects these masks, we invalidate the
234    * Point/Line/Triangle function pointers below.
235    */
236   /*@{*/
237   GLbitfield InvalidatePointMask;
238   GLbitfield InvalidateLineMask;
239   GLbitfield InvalidateTriangleMask;
240   /*@}*/
241
242   /**
243    * Device drivers plug in functions for these callbacks.
244    * Will be called when the GL state change mask intersects the above masks.
245    */
246   /*@{*/
247   void (*choose_point)( struct gl_context * );
248   void (*choose_line)( struct gl_context * );
249   void (*choose_triangle)( struct gl_context * );
250   /*@}*/
251
252   /**
253    * Current point, line and triangle drawing functions.
254    */
255   /*@{*/
256   swrast_point_func Point;
257   swrast_line_func Line;
258   swrast_tri_func Triangle;
259   /*@}*/
260
261   /**
262    * Placeholders for when separate specular (or secondary color) is
263    * enabled but texturing is not.
264    */
265   /*@{*/
266   swrast_point_func SpecPoint;
267   swrast_line_func SpecLine;
268   swrast_tri_func SpecTriangle;
269   /*@}*/
270
271   /**
272    * Typically, we'll allocate a sw_span structure as a local variable
273    * and set its 'array' pointer to point to this object.  The reason is
274    * this object is big and causes problems when allocated on the stack
275    * on some systems.
276    */
277   SWspanarrays *SpanArrays;
278   SWspanarrays *ZoomedArrays;  /**< For pixel zooming */
279
280   /**
281    * Used to buffer N GL_POINTS, instead of rendering one by one.
282    */
283   SWspan PointSpan;
284
285   /** Internal hooks, kept up to date by the same mechanism as above.
286    */
287   blend_func BlendFunc;
288   texture_sample_func TextureSample[MAX_TEXTURE_IMAGE_UNITS];
289
290   /** Buffer for saving the sampled texture colors.
291    * Needed for GL_ARB_texture_env_crossbar implementation.
292    */
293   GLfloat *TexelBuffer;
294
295   validate_texture_image_func ValidateTextureImage;
296
297   /** State used during execution of fragment programs */
298   struct gl_program_machine FragProgMachine;
299
300} SWcontext;
301
302
303extern void
304_swrast_validate_derived( struct gl_context *ctx );
305
306extern void
307_swrast_update_texture_samplers(struct gl_context *ctx);
308
309
310/** Return SWcontext for the given struct gl_context */
311static inline SWcontext *
312SWRAST_CONTEXT(struct gl_context *ctx)
313{
314   return (SWcontext *) ctx->swrast_context;
315}
316
317/** const version of above */
318static inline const SWcontext *
319CONST_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 */
330static inline void
331swrast_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 */
340static inline void
341swrast_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
350/**
351 * Size of an RGBA pixel, in bytes, for given datatype.
352 */
353#define RGBA_PIXEL_SIZE(TYPE)                                     \
354         ((TYPE == GL_UNSIGNED_BYTE) ? 4 * sizeof(GLubyte) :      \
355          ((TYPE == GL_UNSIGNED_SHORT) ? 4 * sizeof(GLushort)     \
356           : 4 * sizeof(GLfloat)))
357
358
359
360/*
361 * Fixed point arithmetic macros
362 */
363#ifndef FIXED_FRAC_BITS
364#define FIXED_FRAC_BITS 11
365#endif
366
367#define FIXED_SHIFT     FIXED_FRAC_BITS
368#define FIXED_ONE       (1 << FIXED_SHIFT)
369#define FIXED_HALF      (1 << (FIXED_SHIFT-1))
370#define FIXED_FRAC_MASK (FIXED_ONE - 1)
371#define FIXED_INT_MASK  (~FIXED_FRAC_MASK)
372#define FIXED_EPSILON   1
373#define FIXED_SCALE     ((float) FIXED_ONE)
374#define FIXED_DBL_SCALE ((double) FIXED_ONE)
375#define FloatToFixed(X) (IROUND((X) * FIXED_SCALE))
376#define FixedToDouble(X) ((X) * (1.0 / FIXED_DBL_SCALE))
377#define IntToFixed(I)   ((I) << FIXED_SHIFT)
378#define FixedToInt(X)   ((X) >> FIXED_SHIFT)
379#define FixedToUns(X)   (((unsigned int)(X)) >> FIXED_SHIFT)
380#define FixedCeil(X)    (((X) + FIXED_ONE - FIXED_EPSILON) & FIXED_INT_MASK)
381#define FixedFloor(X)   ((X) & FIXED_INT_MASK)
382#define FixedToFloat(X) ((X) * (1.0F / FIXED_SCALE))
383#define PosFloatToFixed(X)      FloatToFixed(X)
384#define SignedFloatToFixed(X)   FloatToFixed(X)
385
386
387
388/*
389 * XXX these macros are just bandages for now in order to make
390 * CHAN_BITS==32 compile cleanly.
391 * These should probably go elsewhere at some point.
392 */
393#if CHAN_TYPE == GL_FLOAT
394#define ChanToFixed(X)  (X)
395#define FixedToChan(X)  (X)
396#else
397#define ChanToFixed(X)  IntToFixed(X)
398#define FixedToChan(X)  FixedToInt(X)
399#endif
400
401
402/**
403 * For looping over fragment attributes in the pointe, line
404 * triangle rasterizers.
405 */
406#define ATTRIB_LOOP_BEGIN                                \
407   {                                                     \
408      GLuint a;                                          \
409      for (a = 0; a < swrast->_NumActiveAttribs; a++) {  \
410         const GLuint attr = swrast->_ActiveAttribs[a];
411
412#define ATTRIB_LOOP_END } }
413
414
415
416#endif
417