4f4ea3ee9503485dd6ef313edf197d80c62a1409
[mesa.git] / src / mesa / swrast / s_context.c
1 /* $Id: s_context.c,v 1.14 2001/02/16 18:56:46 keithw Exp $ */
2
3 /*
4 * Mesa 3-D graphics library
5 * Version: 3.5
6 *
7 * Copyright (C) 1999 Brian Paul All Rights Reserved.
8 *
9 * Permission is hereby granted, free of charge, to any person obtaining a
10 * copy of this software and associated documentation files (the "Software"),
11 * to deal in the Software without restriction, including without limitation
12 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
13 * and/or sell copies of the Software, and to permit persons to whom the
14 * Software is furnished to do so, subject to the following conditions:
15 *
16 * The above copyright notice and this permission notice shall be included
17 * in all copies or substantial portions of the Software.
18 *
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
20 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
22 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
23 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
24 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 *
26 * Authors:
27 * Keith Whitwell <keithw@valinux.com>
28 */
29
30 #include "glheader.h"
31 #include "mtypes.h"
32 #include "mem.h"
33
34 #include "s_pb.h"
35 #include "s_points.h"
36 #include "s_lines.h"
37 #include "s_triangle.h"
38 #include "s_blend.h"
39 #include "s_context.h"
40 #include "s_texture.h"
41
42
43
44
45
46 /*
47 * Recompute the value of swrast->_RasterMask, etc. according to
48 * the current context.
49 */
50 static void
51 _swrast_update_rasterflags( GLcontext *ctx )
52 {
53 GLuint RasterMask = 0;
54
55 if (ctx->Color.AlphaEnabled) RasterMask |= ALPHATEST_BIT;
56 if (ctx->Color.BlendEnabled) RasterMask |= BLEND_BIT;
57 if (ctx->Depth.Test) RasterMask |= DEPTH_BIT;
58 if (ctx->Fog.Enabled) RasterMask |= FOG_BIT;
59 if (ctx->Scissor.Enabled) RasterMask |= SCISSOR_BIT;
60 if (ctx->Stencil.Enabled) RasterMask |= STENCIL_BIT;
61 if (ctx->Visual.rgbMode) {
62 const GLuint colorMask = *((GLuint *) &ctx->Color.ColorMask);
63 if (colorMask != 0xffffffff) RasterMask |= MASKING_BIT;
64 if (ctx->Color.ColorLogicOpEnabled) RasterMask |= LOGIC_OP_BIT;
65 if (ctx->Texture._ReallyEnabled) RasterMask |= TEXTURE_BIT;
66 }
67 else {
68 if (ctx->Color.IndexMask != 0xffffffff) RasterMask |= MASKING_BIT;
69 if (ctx->Color.IndexLogicOpEnabled) RasterMask |= LOGIC_OP_BIT;
70 }
71
72 if (ctx->DrawBuffer->UseSoftwareAlphaBuffers
73 && ctx->Color.ColorMask[ACOMP]
74 && ctx->Color.DrawBuffer != GL_NONE)
75 RasterMask |= ALPHABUF_BIT;
76
77 if ( ctx->Viewport.X < 0
78 || ctx->Viewport.X + ctx->Viewport.Width > ctx->DrawBuffer->Width
79 || ctx->Viewport.Y < 0
80 || ctx->Viewport.Y + ctx->Viewport.Height > ctx->DrawBuffer->Height) {
81 RasterMask |= WINCLIP_BIT;
82 }
83
84 if (ctx->Depth.OcclusionTest)
85 RasterMask |= OCCLUSION_BIT;
86
87
88 /* If we're not drawing to exactly one color buffer set the
89 * MULTI_DRAW_BIT flag. Also set it if we're drawing to no
90 * buffers or the RGBA or CI mask disables all writes.
91 */
92 if (ctx->Color.MultiDrawBuffer) {
93 RasterMask |= MULTI_DRAW_BIT;
94 }
95 else if (ctx->Color.DrawBuffer==GL_NONE) {
96 RasterMask |= MULTI_DRAW_BIT;
97 }
98 else if (ctx->Visual.rgbMode && *((GLuint *) ctx->Color.ColorMask) == 0) {
99 RasterMask |= MULTI_DRAW_BIT; /* all RGBA channels disabled */
100 }
101 else if (!ctx->Visual.rgbMode && ctx->Color.IndexMask==0) {
102 RasterMask |= MULTI_DRAW_BIT; /* all color index bits disabled */
103 }
104
105 if ( ctx->Viewport.X<0
106 || ctx->Viewport.X + ctx->Viewport.Width > ctx->DrawBuffer->Width
107 || ctx->Viewport.Y<0
108 || ctx->Viewport.Y + ctx->Viewport.Height > ctx->DrawBuffer->Height) {
109 RasterMask |= WINCLIP_BIT;
110 }
111
112 SWRAST_CONTEXT(ctx)->_RasterMask = RasterMask;
113 }
114
115
116 static void
117 _swrast_update_polygon( GLcontext *ctx )
118 {
119 GLfloat backface_sign = 1;
120
121 if (ctx->Polygon.CullFlag) {
122 backface_sign = 1;
123 switch(ctx->Polygon.CullFaceMode) {
124 case GL_BACK:
125 if(ctx->Polygon.FrontFace==GL_CCW)
126 backface_sign = -1;
127 break;
128 case GL_FRONT:
129 if(ctx->Polygon.FrontFace!=GL_CCW)
130 backface_sign = -1;
131 break;
132 default:
133 case GL_FRONT_AND_BACK:
134 backface_sign = 0;
135 break;
136 }
137 }
138 else {
139 backface_sign = 0;
140 }
141
142 SWRAST_CONTEXT(ctx)->_backface_sign = backface_sign;
143 }
144
145
146 static void
147 _swrast_update_hint( GLcontext *ctx )
148 {
149 SWcontext *swrast = SWRAST_CONTEXT(ctx);
150 swrast->_PreferPixelFog = (!swrast->AllowVertexFog ||
151 (ctx->Hint.Fog == GL_NICEST &&
152 swrast->AllowPixelFog));
153 }
154
155 #define _SWRAST_NEW_DERIVED (_SWRAST_NEW_RASTERMASK | \
156 _NEW_TEXTURE | \
157 _NEW_HINT | \
158 _NEW_POLYGON )
159
160 /* State referenced by _swrast_choose_triangle, _swrast_choose_line.
161 */
162 #define _SWRAST_NEW_TRIANGLE (_SWRAST_NEW_DERIVED | \
163 _NEW_RENDERMODE| \
164 _NEW_POLYGON| \
165 _NEW_DEPTH| \
166 _NEW_STENCIL| \
167 _NEW_COLOR| \
168 _NEW_TEXTURE| \
169 _SWRAST_NEW_RASTERMASK| \
170 _NEW_LIGHT| \
171 _NEW_FOG | \
172 _DD_NEW_SEPERATE_SPECULAR)
173
174 #define _SWRAST_NEW_LINE (_SWRAST_NEW_DERIVED | \
175 _NEW_RENDERMODE| \
176 _NEW_LINE| \
177 _NEW_TEXTURE| \
178 _NEW_LIGHT| \
179 _NEW_FOG| \
180 _NEW_DEPTH | \
181 _DD_NEW_SEPERATE_SPECULAR)
182
183 #define _SWRAST_NEW_POINT (_SWRAST_NEW_DERIVED | \
184 _NEW_RENDERMODE | \
185 _NEW_POINT | \
186 _NEW_TEXTURE | \
187 _NEW_LIGHT | \
188 _NEW_FOG | \
189 _DD_NEW_SEPERATE_SPECULAR)
190
191 #define _SWRAST_NEW_TEXTURE_SAMPLE_FUNC _NEW_TEXTURE
192
193 #define _SWRAST_NEW_BLEND_FUNC _NEW_COLOR
194
195
196
197 /* Stub for swrast->Triangle to select a true triangle function
198 * after a state change.
199 */
200 static void
201 _swrast_validate_triangle( GLcontext *ctx,
202 const SWvertex *v0,
203 const SWvertex *v1,
204 const SWvertex *v2 )
205 {
206 SWcontext *swrast = SWRAST_CONTEXT(ctx);
207
208 _swrast_validate_derived( ctx );
209 swrast->choose_triangle( ctx );
210
211 if ((ctx->_TriangleCaps & DD_SEPERATE_SPECULAR) &&
212 !ctx->Texture._ReallyEnabled) {
213 swrast->SpecTriangle = swrast->Triangle;
214 swrast->Triangle = _swrast_add_spec_terms_triangle;
215 }
216
217 swrast->Triangle( ctx, v0, v1, v2 );
218 }
219
220 static void
221 _swrast_validate_line( GLcontext *ctx, const SWvertex *v0, const SWvertex *v1 )
222 {
223 SWcontext *swrast = SWRAST_CONTEXT(ctx);
224
225 _swrast_validate_derived( ctx );
226 swrast->choose_line( ctx );
227
228 if ((ctx->_TriangleCaps & DD_SEPERATE_SPECULAR) &&
229 !ctx->Texture._ReallyEnabled) {
230 swrast->SpecLine = swrast->Line;
231 swrast->Line = _swrast_add_spec_terms_line;
232 }
233
234
235 swrast->Line( ctx, v0, v1 );
236 }
237
238 static void
239 _swrast_validate_point( GLcontext *ctx, const SWvertex *v0 )
240 {
241 SWcontext *swrast = SWRAST_CONTEXT(ctx);
242
243 _swrast_validate_derived( ctx );
244 swrast->choose_point( ctx );
245
246 if ((ctx->_TriangleCaps & DD_SEPERATE_SPECULAR) &&
247 !ctx->Texture._ReallyEnabled) {
248 swrast->SpecPoint = swrast->Point;
249 swrast->Point = _swrast_add_spec_terms_point;
250 }
251
252 swrast->Point( ctx, v0 );
253 }
254
255 static void
256 _swrast_validate_blend_func( GLcontext *ctx, GLuint n,
257 const GLubyte mask[],
258 GLchan src[][4],
259 CONST GLchan dst[][4] )
260 {
261 SWcontext *swrast = SWRAST_CONTEXT(ctx);
262
263 _swrast_validate_derived( ctx );
264 _swrast_choose_blend_func( ctx );
265
266 swrast->BlendFunc( ctx, n, mask, src, dst );
267 }
268
269
270 static void
271 _swrast_validate_texture_sample( GLcontext *ctx, GLuint texUnit,
272 const struct gl_texture_object *tObj,
273 GLuint n,
274 const GLfloat s[], const GLfloat t[],
275 const GLfloat u[], const GLfloat lambda[],
276 GLchan rgba[][4] )
277 {
278 SWcontext *swrast = SWRAST_CONTEXT(ctx);
279
280 _swrast_validate_derived( ctx );
281 _swrast_choose_texture_sample_func( ctx, texUnit, tObj );
282
283 swrast->TextureSample[texUnit]( ctx, texUnit, tObj, n, s, t, u,
284 lambda, rgba );
285 }
286
287
288 static void
289 _swrast_sleep( GLcontext *ctx, GLuint new_state )
290 {
291 }
292
293
294 static void
295 _swrast_invalidate_state( GLcontext *ctx, GLuint new_state )
296 {
297 SWcontext *swrast = SWRAST_CONTEXT(ctx);
298 GLuint i;
299
300 swrast->NewState |= new_state;
301
302 /* After 10 statechanges without any swrast functions being called,
303 * put the module to sleep.
304 */
305 if (++swrast->StateChanges > 10) {
306 swrast->InvalidateState = _swrast_sleep;
307 swrast->NewState = ~0;
308 new_state = ~0;
309 }
310
311 if (new_state & swrast->invalidate_triangle)
312 swrast->Triangle = _swrast_validate_triangle;
313
314 if (new_state & swrast->invalidate_line)
315 swrast->Line = _swrast_validate_line;
316
317 if (new_state & swrast->invalidate_point)
318 swrast->Point = _swrast_validate_point;
319
320 if (new_state & _SWRAST_NEW_BLEND_FUNC)
321 swrast->BlendFunc = _swrast_validate_blend_func;
322
323 if (new_state & _SWRAST_NEW_TEXTURE_SAMPLE_FUNC)
324 for (i = 0 ; i < ctx->Const.MaxTextureUnits ; i++)
325 swrast->TextureSample[i] = _swrast_validate_texture_sample;
326 }
327
328
329
330 void
331 _swrast_validate_derived( GLcontext *ctx )
332 {
333 SWcontext *swrast = SWRAST_CONTEXT(ctx);
334
335 if (swrast->NewState)
336 {
337 if (swrast->NewState & _SWRAST_NEW_RASTERMASK)
338 _swrast_update_rasterflags( ctx );
339
340 if (swrast->NewState & _NEW_TEXTURE)
341 swrast->_MultiTextureEnabled =
342 ctx->Texture._ReallyEnabled > TEXTURE0_ANY;
343
344 if (swrast->NewState & _NEW_POLYGON)
345 _swrast_update_polygon( ctx );
346
347 if (swrast->NewState & _NEW_HINT)
348 _swrast_update_hint( ctx );
349
350 swrast->NewState = 0;
351 swrast->StateChanges = 0;
352 swrast->InvalidateState = _swrast_invalidate_state;
353 }
354 }
355
356
357
358 /* Public entrypoints: See also s_accum.c, s_bitmap.c, etc.
359 */
360 void
361 _swrast_Quad( GLcontext *ctx,
362 const SWvertex *v0, const SWvertex *v1,
363 const SWvertex *v2, const SWvertex *v3 )
364 {
365 SWRAST_CONTEXT(ctx)->Triangle( ctx, v0, v1, v2 );
366 SWRAST_CONTEXT(ctx)->Triangle( ctx, v0, v2, v3 );
367 }
368
369 void
370 _swrast_Triangle( GLcontext *ctx, const SWvertex *v0,
371 const SWvertex *v1, const SWvertex *v2 )
372 {
373 /* fprintf(stderr, "%s\n", __FUNCTION__); */
374 /* _swrast_print_vertex( ctx, v0 ); */
375 /* _swrast_print_vertex( ctx, v1 ); */
376 /* _swrast_print_vertex( ctx, v2 ); */
377 SWRAST_CONTEXT(ctx)->Triangle( ctx, v0, v1, v2 );
378 }
379
380 void
381 _swrast_Line( GLcontext *ctx, const SWvertex *v0, const SWvertex *v1 )
382 {
383 /* fprintf(stderr, "%s\n", __FUNCTION__); */
384 /* _swrast_print_vertex( ctx, v0 ); */
385 /* _swrast_print_vertex( ctx, v1 ); */
386 SWRAST_CONTEXT(ctx)->Line( ctx, v0, v1 );
387 }
388
389 void
390 _swrast_Point( GLcontext *ctx, const SWvertex *v0 )
391 {
392 /* fprintf(stderr, "%s\n", __FUNCTION__); */
393 /* _swrast_print_vertex( ctx, v0 ); */
394 SWRAST_CONTEXT(ctx)->Point( ctx, v0 );
395 }
396
397 void
398 _swrast_InvalidateState( GLcontext *ctx, GLuint new_state )
399 {
400 SWRAST_CONTEXT(ctx)->InvalidateState( ctx, new_state );
401 }
402
403 void
404 _swrast_ResetLineStipple( GLcontext *ctx )
405 {
406 SWRAST_CONTEXT(ctx)->StippleCounter = 0;
407 }
408
409 void
410 _swrast_allow_vertex_fog( GLcontext *ctx, GLboolean value )
411 {
412 SWRAST_CONTEXT(ctx)->InvalidateState( ctx, _NEW_HINT );
413 SWRAST_CONTEXT(ctx)->AllowVertexFog = value;
414 }
415
416 void
417 _swrast_allow_pixel_fog( GLcontext *ctx, GLboolean value )
418 {
419 SWRAST_CONTEXT(ctx)->InvalidateState( ctx, _NEW_HINT );
420 SWRAST_CONTEXT(ctx)->AllowPixelFog = value;
421 }
422
423
424 GLboolean
425 _swrast_CreateContext( GLcontext *ctx )
426 {
427 GLuint i;
428 SWcontext *swrast = (SWcontext *)CALLOC(sizeof(SWcontext));
429 if (!swrast)
430 return GL_FALSE;
431
432 swrast->PB = gl_alloc_pb();
433 if (!swrast->PB) {
434 FREE(swrast);
435 return GL_FALSE;
436 }
437
438 swrast->NewState = ~0;
439
440 swrast->choose_point = _swrast_choose_point;
441 swrast->choose_line = _swrast_choose_line;
442 swrast->choose_triangle = _swrast_choose_triangle;
443
444 swrast->invalidate_point = _SWRAST_NEW_POINT;
445 swrast->invalidate_line = _SWRAST_NEW_LINE;
446 swrast->invalidate_triangle = _SWRAST_NEW_TRIANGLE;
447
448 swrast->Point = _swrast_validate_point;
449 swrast->Line = _swrast_validate_line;
450 swrast->Triangle = _swrast_validate_triangle;
451 swrast->InvalidateState = _swrast_sleep;
452 swrast->BlendFunc = _swrast_validate_blend_func;
453
454 swrast->AllowVertexFog = GL_TRUE;
455 swrast->AllowPixelFog = GL_TRUE;
456
457 /* Optimized Accum buffer */
458 swrast->_IntegerAccumMode = GL_TRUE;
459 swrast->_IntegerAccumScaler = 0.0;
460
461
462 for (i = 0 ; i < MAX_TEXTURE_UNITS ; i++)
463 swrast->TextureSample[i] = _swrast_validate_texture_sample;
464
465 ctx->swrast_context = swrast;
466 return GL_TRUE;
467 }
468
469 void
470 _swrast_DestroyContext( GLcontext *ctx )
471 {
472 SWcontext *swrast = SWRAST_CONTEXT(ctx);
473
474 FREE( swrast->PB );
475 FREE( swrast );
476
477 ctx->swrast_context = 0;
478 }
479
480
481 void
482 _swrast_print_vertex( GLcontext *ctx, const SWvertex *v )
483 {
484 GLuint i;
485
486 fprintf(stderr, "win %f %f %f %f\n",
487 v->win[0], v->win[1], v->win[2], v->win[3]);
488
489 for (i = 0 ; i < ctx->Const.MaxTextureUnits ; i++)
490 fprintf(stderr, "texcoord[%d] %f %f %f %f\n", i,
491 v->texcoord[i][0], v->texcoord[i][1],
492 v->texcoord[i][2], v->texcoord[i][3]);
493
494 fprintf(stderr, "color %d %d %d %d\n",
495 v->color[0], v->color[1], v->color[2], v->color[3]);
496 fprintf(stderr, "spec %d %d %d %d\n",
497 v->specular[0], v->specular[1], v->specular[2], v->specular[3]);
498 fprintf(stderr, "fog %f\n", v->fog);
499 fprintf(stderr, "index %d\n", v->index);
500 fprintf(stderr, "pointsize %f\n", v->pointSize);
501 fprintf(stderr, "\n");
502 }