1 /* $Id: s_context.c,v 1.13 2001/02/16 18:14:41 keithw Exp $ */
4 * Mesa 3-D graphics library
7 * Copyright (C) 1999 Brian Paul All Rights Reserved.
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:
16 * The above copyright notice and this permission notice shall be included
17 * in all copies or substantial portions of the Software.
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.
27 * Keith Whitwell <keithw@valinux.com>
37 #include "s_triangle.h"
39 #include "s_context.h"
40 #include "s_texture.h"
47 * Recompute the value of swrast->_RasterMask, etc. according to
48 * the current context.
51 _swrast_update_rasterflags( GLcontext
*ctx
)
53 GLuint RasterMask
= 0;
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
;
68 if (ctx
->Color
.IndexMask
!= 0xffffffff) RasterMask
|= MASKING_BIT
;
69 if (ctx
->Color
.IndexLogicOpEnabled
) RasterMask
|= LOGIC_OP_BIT
;
72 if (ctx
->DrawBuffer
->UseSoftwareAlphaBuffers
73 && ctx
->Color
.ColorMask
[ACOMP
]
74 && ctx
->Color
.DrawBuffer
!= GL_NONE
)
75 RasterMask
|= ALPHABUF_BIT
;
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
;
84 if (ctx
->Depth
.OcclusionTest
)
85 RasterMask
|= OCCLUSION_BIT
;
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.
92 if (ctx
->Color
.MultiDrawBuffer
) {
93 RasterMask
|= MULTI_DRAW_BIT
;
95 else if (ctx
->Color
.DrawBuffer
==GL_NONE
) {
96 RasterMask
|= MULTI_DRAW_BIT
;
98 else if (ctx
->Visual
.rgbMode
&& *((GLuint
*) ctx
->Color
.ColorMask
) == 0) {
99 RasterMask
|= MULTI_DRAW_BIT
; /* all RGBA channels disabled */
101 else if (!ctx
->Visual
.rgbMode
&& ctx
->Color
.IndexMask
==0) {
102 RasterMask
|= MULTI_DRAW_BIT
; /* all color index bits disabled */
105 if ( ctx
->Viewport
.X
<0
106 || ctx
->Viewport
.X
+ ctx
->Viewport
.Width
> ctx
->DrawBuffer
->Width
108 || ctx
->Viewport
.Y
+ ctx
->Viewport
.Height
> ctx
->DrawBuffer
->Height
) {
109 RasterMask
|= WINCLIP_BIT
;
112 SWRAST_CONTEXT(ctx
)->_RasterMask
= RasterMask
;
117 _swrast_update_polygon( GLcontext
*ctx
)
119 GLfloat backface_sign
= 1;
121 if (ctx
->Polygon
.CullFlag
) {
123 switch(ctx
->Polygon
.CullFaceMode
) {
125 if(ctx
->Polygon
.FrontFace
==GL_CCW
)
129 if(ctx
->Polygon
.FrontFace
!=GL_CCW
)
133 case GL_FRONT_AND_BACK
:
142 SWRAST_CONTEXT(ctx
)->_backface_sign
= backface_sign
;
147 _swrast_update_hint( GLcontext
*ctx
)
149 SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
150 swrast
->_PreferPixelFog
= (!swrast
->AllowVertexFog
||
151 (ctx
->Hint
.Fog
== GL_NICEST
&&
152 swrast
->AllowPixelFog
));
155 #define _SWRAST_NEW_TRIANGLE (_NEW_RENDERMODE| \
162 _SWRAST_NEW_RASTERMASK| \
165 _DD_NEW_SEPERATE_SPECULAR)
167 #define _SWRAST_NEW_LINE (_NEW_RENDERMODE| \
173 _DD_NEW_SEPERATE_SPECULAR)
175 #define _SWRAST_NEW_POINT (_NEW_RENDERMODE | \
180 _DD_NEW_SEPERATE_SPECULAR)
182 #define _SWRAST_NEW_TEXTURE_SAMPLE_FUNC _NEW_TEXTURE
184 #define _SWRAST_NEW_BLEND_FUNC _NEW_COLOR
188 /* Stub for swrast->Triangle to select a true triangle function
189 * after a state change.
192 _swrast_validate_triangle( GLcontext
*ctx
,
197 SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
199 _swrast_validate_derived( ctx
);
200 swrast
->choose_triangle( ctx
);
202 if ((ctx
->_TriangleCaps
& DD_SEPERATE_SPECULAR
) &&
203 !ctx
->Texture
._ReallyEnabled
) {
204 swrast
->SpecTriangle
= swrast
->Triangle
;
205 swrast
->Triangle
= _swrast_add_spec_terms_triangle
;
208 swrast
->Triangle( ctx
, v0
, v1
, v2
);
212 _swrast_validate_line( GLcontext
*ctx
, const SWvertex
*v0
, const SWvertex
*v1
)
214 SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
216 _swrast_validate_derived( ctx
);
217 swrast
->choose_line( ctx
);
219 if ((ctx
->_TriangleCaps
& DD_SEPERATE_SPECULAR
) &&
220 !ctx
->Texture
._ReallyEnabled
) {
221 swrast
->SpecLine
= swrast
->Line
;
222 swrast
->Line
= _swrast_add_spec_terms_line
;
226 swrast
->Line( ctx
, v0
, v1
);
230 _swrast_validate_point( GLcontext
*ctx
, const SWvertex
*v0
)
232 SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
234 _swrast_validate_derived( ctx
);
235 swrast
->choose_point( ctx
);
237 if ((ctx
->_TriangleCaps
& DD_SEPERATE_SPECULAR
) &&
238 !ctx
->Texture
._ReallyEnabled
) {
239 swrast
->SpecPoint
= swrast
->Point
;
240 swrast
->Point
= _swrast_add_spec_terms_point
;
243 swrast
->Point( ctx
, v0
);
247 _swrast_validate_blend_func( GLcontext
*ctx
, GLuint n
,
248 const GLubyte mask
[],
250 CONST GLchan dst
[][4] )
252 SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
254 _swrast_validate_derived( ctx
);
255 _swrast_choose_blend_func( ctx
);
257 swrast
->BlendFunc( ctx
, n
, mask
, src
, dst
);
262 _swrast_validate_texture_sample( GLcontext
*ctx
, GLuint texUnit
,
263 const struct gl_texture_object
*tObj
,
265 const GLfloat s
[], const GLfloat t
[],
266 const GLfloat u
[], const GLfloat lambda
[],
269 SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
271 _swrast_validate_derived( ctx
);
272 _swrast_choose_texture_sample_func( ctx
, texUnit
, tObj
);
274 swrast
->TextureSample
[texUnit
]( ctx
, texUnit
, tObj
, n
, s
, t
, u
,
280 _swrast_sleep( GLcontext
*ctx
, GLuint new_state
)
286 _swrast_invalidate_state( GLcontext
*ctx
, GLuint new_state
)
288 SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
291 swrast
->NewState
|= new_state
;
293 /* After 10 statechanges without any swrast functions being called,
294 * put the module to sleep.
296 if (++swrast
->StateChanges
> 10) {
297 swrast
->InvalidateState
= _swrast_sleep
;
298 swrast
->NewState
= ~0;
302 if (new_state
& swrast
->invalidate_triangle
)
303 swrast
->Triangle
= _swrast_validate_triangle
;
305 if (new_state
& swrast
->invalidate_line
)
306 swrast
->Line
= _swrast_validate_line
;
308 if (new_state
& swrast
->invalidate_point
)
309 swrast
->Point
= _swrast_validate_point
;
311 if (new_state
& _SWRAST_NEW_BLEND_FUNC
)
312 swrast
->BlendFunc
= _swrast_validate_blend_func
;
314 if (new_state
& _SWRAST_NEW_TEXTURE_SAMPLE_FUNC
)
315 for (i
= 0 ; i
< ctx
->Const
.MaxTextureUnits
; i
++)
316 swrast
->TextureSample
[i
] = _swrast_validate_texture_sample
;
322 _swrast_validate_derived( GLcontext
*ctx
)
324 SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
326 if (swrast
->NewState
)
328 if (swrast
->NewState
& _SWRAST_NEW_RASTERMASK
)
329 _swrast_update_rasterflags( ctx
);
331 if (swrast
->NewState
& _NEW_TEXTURE
)
332 swrast
->_MultiTextureEnabled
=
333 ctx
->Texture
._ReallyEnabled
> TEXTURE0_ANY
;
335 if (swrast
->NewState
& _NEW_POLYGON
)
336 _swrast_update_polygon( ctx
);
338 if (swrast
->NewState
& _NEW_HINT
)
339 _swrast_update_hint( ctx
);
341 swrast
->NewState
= 0;
342 swrast
->StateChanges
= 0;
343 swrast
->InvalidateState
= _swrast_invalidate_state
;
349 /* Public entrypoints: See also s_accum.c, s_bitmap.c, etc.
352 _swrast_Quad( GLcontext
*ctx
,
353 const SWvertex
*v0
, const SWvertex
*v1
,
354 const SWvertex
*v2
, const SWvertex
*v3
)
356 SWRAST_CONTEXT(ctx
)->Triangle( ctx
, v0
, v1
, v2
);
357 SWRAST_CONTEXT(ctx
)->Triangle( ctx
, v0
, v2
, v3
);
361 _swrast_Triangle( GLcontext
*ctx
, const SWvertex
*v0
,
362 const SWvertex
*v1
, const SWvertex
*v2
)
364 /* fprintf(stderr, "%s\n", __FUNCTION__); */
365 /* _swrast_print_vertex( ctx, v0 ); */
366 /* _swrast_print_vertex( ctx, v1 ); */
367 /* _swrast_print_vertex( ctx, v2 ); */
368 SWRAST_CONTEXT(ctx
)->Triangle( ctx
, v0
, v1
, v2
);
372 _swrast_Line( GLcontext
*ctx
, const SWvertex
*v0
, const SWvertex
*v1
)
374 /* fprintf(stderr, "%s\n", __FUNCTION__); */
375 /* _swrast_print_vertex( ctx, v0 ); */
376 /* _swrast_print_vertex( ctx, v1 ); */
377 SWRAST_CONTEXT(ctx
)->Line( ctx
, v0
, v1
);
381 _swrast_Point( GLcontext
*ctx
, const SWvertex
*v0
)
383 /* fprintf(stderr, "%s\n", __FUNCTION__); */
384 /* _swrast_print_vertex( ctx, v0 ); */
385 SWRAST_CONTEXT(ctx
)->Point( ctx
, v0
);
389 _swrast_InvalidateState( GLcontext
*ctx
, GLuint new_state
)
391 SWRAST_CONTEXT(ctx
)->InvalidateState( ctx
, new_state
);
395 _swrast_ResetLineStipple( GLcontext
*ctx
)
397 SWRAST_CONTEXT(ctx
)->StippleCounter
= 0;
401 _swrast_allow_vertex_fog( GLcontext
*ctx
, GLboolean value
)
403 SWRAST_CONTEXT(ctx
)->InvalidateState( ctx
, _NEW_HINT
);
404 SWRAST_CONTEXT(ctx
)->AllowVertexFog
= value
;
408 _swrast_allow_pixel_fog( GLcontext
*ctx
, GLboolean value
)
410 SWRAST_CONTEXT(ctx
)->InvalidateState( ctx
, _NEW_HINT
);
411 SWRAST_CONTEXT(ctx
)->AllowPixelFog
= value
;
416 _swrast_CreateContext( GLcontext
*ctx
)
419 SWcontext
*swrast
= (SWcontext
*)CALLOC(sizeof(SWcontext
));
423 swrast
->PB
= gl_alloc_pb();
429 swrast
->NewState
= ~0;
431 swrast
->choose_point
= _swrast_choose_point
;
432 swrast
->choose_line
= _swrast_choose_line
;
433 swrast
->choose_triangle
= _swrast_choose_triangle
;
435 swrast
->invalidate_point
= _SWRAST_NEW_POINT
;
436 swrast
->invalidate_line
= _SWRAST_NEW_LINE
;
437 swrast
->invalidate_triangle
= _SWRAST_NEW_TRIANGLE
;
439 swrast
->Point
= _swrast_validate_point
;
440 swrast
->Line
= _swrast_validate_line
;
441 swrast
->Triangle
= _swrast_validate_triangle
;
442 swrast
->InvalidateState
= _swrast_sleep
;
443 swrast
->BlendFunc
= _swrast_validate_blend_func
;
445 swrast
->AllowVertexFog
= GL_TRUE
;
446 swrast
->AllowPixelFog
= GL_TRUE
;
448 /* Optimized Accum buffer */
449 swrast
->_IntegerAccumMode
= GL_TRUE
;
450 swrast
->_IntegerAccumScaler
= 0.0;
453 for (i
= 0 ; i
< MAX_TEXTURE_UNITS
; i
++)
454 swrast
->TextureSample
[i
] = _swrast_validate_texture_sample
;
456 ctx
->swrast_context
= swrast
;
461 _swrast_DestroyContext( GLcontext
*ctx
)
463 SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
468 ctx
->swrast_context
= 0;
473 _swrast_print_vertex( GLcontext
*ctx
, const SWvertex
*v
)
477 fprintf(stderr
, "win %f %f %f %f\n",
478 v
->win
[0], v
->win
[1], v
->win
[2], v
->win
[3]);
480 for (i
= 0 ; i
< ctx
->Const
.MaxTextureUnits
; i
++)
481 fprintf(stderr
, "texcoord[%d] %f %f %f %f\n", i
,
482 v
->texcoord
[i
][0], v
->texcoord
[i
][1],
483 v
->texcoord
[i
][2], v
->texcoord
[i
][3]);
485 fprintf(stderr
, "color %d %d %d %d\n",
486 v
->color
[0], v
->color
[1], v
->color
[2], v
->color
[3]);
487 fprintf(stderr
, "spec %d %d %d %d\n",
488 v
->specular
[0], v
->specular
[1], v
->specular
[2], v
->specular
[3]);
489 fprintf(stderr
, "fog %f\n", v
->fog
);
490 fprintf(stderr
, "index %d\n", v
->index
);
491 fprintf(stderr
, "pointsize %f\n", v
->pointSize
);
492 fprintf(stderr
, "\n");