swrast: remove old texture_apply() code; always use texture combine code
[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/mtypes.h"
47 #include "shader/prog_execute.h"
48 #include "swrast.h"
49 #include "s_span.h"
50
51
52 typedef void (*texture_sample_func)(GLcontext *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)( GLcontext *ctx, GLuint n,
58 const GLubyte mask[],
59 GLvoid *src, const GLvoid *dst,
60 GLenum chanType);
61
62 typedef void (*swrast_point_func)( GLcontext *ctx, const SWvertex *);
63
64 typedef void (*swrast_line_func)( GLcontext *ctx,
65 const SWvertex *, const SWvertex *);
66
67 typedef void (*swrast_tri_func)( GLcontext *ctx, const SWvertex *,
68 const SWvertex *, const SWvertex *);
69
70
71 typedef void (*validate_texture_image_func)(GLcontext *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 OCCLUSION_BIT 0x800 /**< GL_HP_occlusion_test enabled */
93 #define TEXTURE_BIT 0x1000 /**< Texturing really enabled */
94 #define FRAGPROG_BIT 0x2000 /**< Fragment program enabled */
95 #define ATIFRAGSHADER_BIT 0x4000 /**< ATI Fragment shader enabled */
96 #define CLAMPING_BIT 0x8000 /**< Clamp colors to [0,1] */
97 /*@}*/
98
99 #define _SWRAST_NEW_RASTERMASK (_NEW_BUFFERS| \
100 _NEW_SCISSOR| \
101 _NEW_COLOR| \
102 _NEW_DEPTH| \
103 _NEW_FOG| \
104 _NEW_PROGRAM| \
105 _NEW_STENCIL| \
106 _NEW_TEXTURE| \
107 _NEW_VIEWPORT| \
108 _NEW_DEPTH)
109
110
111 /**
112 * \struct SWcontext
113 * \brief Per-context state that's private to the software rasterizer module.
114 */
115 typedef struct
116 {
117 /** Driver interface:
118 */
119 struct swrast_device_driver Driver;
120
121 /** Configuration mechanisms to make software rasterizer match
122 * characteristics of the hardware rasterizer (if present):
123 */
124 GLboolean AllowVertexFog;
125 GLboolean AllowPixelFog;
126
127 /** Derived values, invalidated on statechanges, updated from
128 * _swrast_validate_derived():
129 */
130 GLbitfield _RasterMask;
131 GLfloat _BackfaceSign; /** +1 or -1 */
132 GLfloat _BackfaceCullSign; /** +1, 0, or -1 */
133 GLboolean _PreferPixelFog; /* Compute fog blend factor per fragment? */
134 GLboolean _TextureCombinePrimary;
135 GLboolean _FogEnabled;
136 GLboolean _DeferredTexture;
137 GLenum _FogMode; /* either GL_FOG_MODE or fragment program's fog mode */
138
139 /** List/array of the fragment attributes to interpolate */
140 GLuint _ActiveAttribs[FRAG_ATTRIB_MAX];
141 /** Same info, but as a bitmask */
142 GLbitfield _ActiveAttribMask;
143 /** Number of fragment attributes to interpolate */
144 GLuint _NumActiveAttribs;
145 /** Indicates how each attrib is to be interpolated (lines/tris) */
146 GLenum _InterpMode[FRAG_ATTRIB_MAX]; /* GL_FLAT or GL_SMOOTH (for now) */
147
148 /* Accum buffer temporaries.
149 */
150 GLboolean _IntegerAccumMode; /**< Storing unscaled integers? */
151 GLfloat _IntegerAccumScaler; /**< Implicit scale factor */
152
153 /* Working values:
154 */
155 GLuint StippleCounter; /**< Line stipple counter */
156 GLuint PointLineFacing;
157 GLbitfield NewState;
158 GLuint StateChanges;
159 GLenum Primitive; /* current primitive being drawn (ala glBegin) */
160
161 void (*InvalidateState)( GLcontext *ctx, GLbitfield new_state );
162
163 /**
164 * When the NewState mask intersects these masks, we invalidate the
165 * Point/Line/Triangle function pointers below.
166 */
167 /*@{*/
168 GLbitfield InvalidatePointMask;
169 GLbitfield InvalidateLineMask;
170 GLbitfield InvalidateTriangleMask;
171 /*@}*/
172
173 /**
174 * Device drivers plug in functions for these callbacks.
175 * Will be called when the GL state change mask intersects the above masks.
176 */
177 /*@{*/
178 void (*choose_point)( GLcontext * );
179 void (*choose_line)( GLcontext * );
180 void (*choose_triangle)( GLcontext * );
181 /*@}*/
182
183 /**
184 * Current point, line and triangle drawing functions.
185 */
186 /*@{*/
187 swrast_point_func Point;
188 swrast_line_func Line;
189 swrast_tri_func Triangle;
190 /*@}*/
191
192 /**
193 * Placeholders for when separate specular (or secondary color) is
194 * enabled but texturing is not.
195 */
196 /*@{*/
197 swrast_point_func SpecPoint;
198 swrast_line_func SpecLine;
199 swrast_tri_func SpecTriangle;
200 /*@}*/
201
202 /**
203 * Typically, we'll allocate a sw_span structure as a local variable
204 * and set its 'array' pointer to point to this object. The reason is
205 * this object is big and causes problems when allocated on the stack
206 * on some systems.
207 */
208 SWspanarrays *SpanArrays;
209 SWspanarrays *ZoomedArrays; /**< For pixel zooming */
210
211 /**
212 * Used to buffer N GL_POINTS, instead of rendering one by one.
213 */
214 SWspan PointSpan;
215
216 /** Internal hooks, kept up to date by the same mechanism as above.
217 */
218 blend_func BlendFunc;
219 texture_sample_func TextureSample[MAX_TEXTURE_IMAGE_UNITS];
220
221 /** Buffer for saving the sampled texture colors.
222 * Needed for GL_ARB_texture_env_crossbar implementation.
223 */
224 GLfloat *TexelBuffer;
225
226 validate_texture_image_func ValidateTextureImage;
227
228 /** State used during execution of fragment programs */
229 struct gl_program_machine FragProgMachine;
230
231 } SWcontext;
232
233
234 extern void
235 _swrast_validate_derived( GLcontext *ctx );
236
237 extern void
238 _swrast_update_texture_samplers(GLcontext *ctx);
239
240
241 /** Return SWcontext for the given GLcontext */
242 static INLINE SWcontext *
243 SWRAST_CONTEXT(GLcontext *ctx)
244 {
245 return (SWcontext *) ctx->swrast_context;
246 }
247
248 /** const version of above */
249 static INLINE const SWcontext *
250 CONST_SWRAST_CONTEXT(const GLcontext *ctx)
251 {
252 return (const SWcontext *) ctx->swrast_context;
253 }
254
255
256 /**
257 * Called prior to framebuffer reading/writing.
258 * For drivers that rely on swrast for fallback rendering, this is the
259 * driver's opportunity to map renderbuffers and textures.
260 */
261 static INLINE void
262 swrast_render_start(GLcontext *ctx)
263 {
264 SWcontext *swrast = SWRAST_CONTEXT(ctx);
265 if (swrast->Driver.SpanRenderStart)
266 swrast->Driver.SpanRenderStart(ctx);
267 }
268
269
270 /** Called after framebuffer reading/writing */
271 static INLINE void
272 swrast_render_finish(GLcontext *ctx)
273 {
274 SWcontext *swrast = SWRAST_CONTEXT(ctx);
275 if (swrast->Driver.SpanRenderFinish)
276 swrast->Driver.SpanRenderFinish(ctx);
277 }
278
279
280
281 /**
282 * Size of an RGBA pixel, in bytes, for given datatype.
283 */
284 #define RGBA_PIXEL_SIZE(TYPE) \
285 ((TYPE == GL_UNSIGNED_BYTE) ? 4 * sizeof(GLubyte) : \
286 ((TYPE == GL_UNSIGNED_SHORT) ? 4 * sizeof(GLushort) \
287 : 4 * sizeof(GLfloat)))
288
289
290
291 /*
292 * Fixed point arithmetic macros
293 */
294 #ifndef FIXED_FRAC_BITS
295 #define FIXED_FRAC_BITS 11
296 #endif
297
298 #define FIXED_SHIFT FIXED_FRAC_BITS
299 #define FIXED_ONE (1 << FIXED_SHIFT)
300 #define FIXED_HALF (1 << (FIXED_SHIFT-1))
301 #define FIXED_FRAC_MASK (FIXED_ONE - 1)
302 #define FIXED_INT_MASK (~FIXED_FRAC_MASK)
303 #define FIXED_EPSILON 1
304 #define FIXED_SCALE ((float) FIXED_ONE)
305 #define FIXED_DBL_SCALE ((double) FIXED_ONE)
306 #define FloatToFixed(X) (IROUND((X) * FIXED_SCALE))
307 #define FixedToDouble(X) ((X) * (1.0 / FIXED_DBL_SCALE))
308 #define IntToFixed(I) ((I) << FIXED_SHIFT)
309 #define FixedToInt(X) ((X) >> FIXED_SHIFT)
310 #define FixedToUns(X) (((unsigned int)(X)) >> FIXED_SHIFT)
311 #define FixedCeil(X) (((X) + FIXED_ONE - FIXED_EPSILON) & FIXED_INT_MASK)
312 #define FixedFloor(X) ((X) & FIXED_INT_MASK)
313 #define FixedToFloat(X) ((X) * (1.0F / FIXED_SCALE))
314 #define PosFloatToFixed(X) FloatToFixed(X)
315 #define SignedFloatToFixed(X) FloatToFixed(X)
316
317
318
319 /*
320 * XXX these macros are just bandages for now in order to make
321 * CHAN_BITS==32 compile cleanly.
322 * These should probably go elsewhere at some point.
323 */
324 #if CHAN_TYPE == GL_FLOAT
325 #define ChanToFixed(X) (X)
326 #define FixedToChan(X) (X)
327 #else
328 #define ChanToFixed(X) IntToFixed(X)
329 #define FixedToChan(X) FixedToInt(X)
330 #endif
331
332
333 /**
334 * For looping over fragment attributes in the pointe, line
335 * triangle rasterizers.
336 */
337 #define ATTRIB_LOOP_BEGIN \
338 { \
339 GLuint a; \
340 for (a = 0; a < swrast->_NumActiveAttribs; a++) { \
341 const GLuint attr = swrast->_ActiveAttribs[a];
342
343 #define ATTRIB_LOOP_END } }
344
345
346
347 #endif