1 /* $Id: s_context.c,v 1.23 2001/07/13 20:07:37 brianp Exp $ */
4 * Mesa 3-D graphics library
7 * Copyright (C) 1999-2001 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 SWRAST_CONTEXT(ctx
)->_RasterMask
= RasterMask
;
110 _swrast_update_polygon( GLcontext
*ctx
)
112 GLfloat backface_sign
= 1;
114 if (ctx
->Polygon
.CullFlag
) {
116 switch(ctx
->Polygon
.CullFaceMode
) {
118 if(ctx
->Polygon
.FrontFace
==GL_CCW
)
122 if(ctx
->Polygon
.FrontFace
!=GL_CCW
)
126 case GL_FRONT_AND_BACK
:
135 SWRAST_CONTEXT(ctx
)->_backface_sign
= backface_sign
;
140 _swrast_update_hint( GLcontext
*ctx
)
142 SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
143 swrast
->_PreferPixelFog
= (!swrast
->AllowVertexFog
||
144 (ctx
->Hint
.Fog
== GL_NICEST
&&
145 swrast
->AllowPixelFog
));
148 #define _SWRAST_NEW_DERIVED (_SWRAST_NEW_RASTERMASK | \
153 /* State referenced by _swrast_choose_triangle, _swrast_choose_line.
155 #define _SWRAST_NEW_TRIANGLE (_SWRAST_NEW_DERIVED | \
162 _SWRAST_NEW_RASTERMASK| \
165 _DD_NEW_SEPARATE_SPECULAR)
167 #define _SWRAST_NEW_LINE (_SWRAST_NEW_DERIVED | \
174 _DD_NEW_SEPARATE_SPECULAR)
176 #define _SWRAST_NEW_POINT (_SWRAST_NEW_DERIVED | \
182 _DD_NEW_SEPARATE_SPECULAR)
184 #define _SWRAST_NEW_TEXTURE_SAMPLE_FUNC _NEW_TEXTURE
186 #define _SWRAST_NEW_BLEND_FUNC _NEW_COLOR
190 /* Stub for swrast->Triangle to select a true triangle function
191 * after a state change.
194 _swrast_validate_triangle( GLcontext
*ctx
,
199 SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
201 _swrast_validate_derived( ctx
);
202 swrast
->choose_triangle( ctx
);
204 if ((ctx
->_TriangleCaps
& DD_SEPARATE_SPECULAR
) &&
205 !ctx
->Texture
._ReallyEnabled
) {
206 swrast
->SpecTriangle
= swrast
->Triangle
;
207 swrast
->Triangle
= _swrast_add_spec_terms_triangle
;
210 swrast
->Triangle( ctx
, v0
, v1
, v2
);
214 _swrast_validate_line( GLcontext
*ctx
, const SWvertex
*v0
, const SWvertex
*v1
)
216 SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
218 _swrast_validate_derived( ctx
);
219 swrast
->choose_line( ctx
);
221 if ((ctx
->_TriangleCaps
& DD_SEPARATE_SPECULAR
) &&
222 !ctx
->Texture
._ReallyEnabled
) {
223 swrast
->SpecLine
= swrast
->Line
;
224 swrast
->Line
= _swrast_add_spec_terms_line
;
228 swrast
->Line( ctx
, v0
, v1
);
232 _swrast_validate_point( GLcontext
*ctx
, const SWvertex
*v0
)
234 SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
236 _swrast_validate_derived( ctx
);
237 swrast
->choose_point( ctx
);
239 if ((ctx
->_TriangleCaps
& DD_SEPARATE_SPECULAR
) &&
240 !ctx
->Texture
._ReallyEnabled
) {
241 swrast
->SpecPoint
= swrast
->Point
;
242 swrast
->Point
= _swrast_add_spec_terms_point
;
245 swrast
->Point( ctx
, v0
);
249 _swrast_validate_blend_func( GLcontext
*ctx
, GLuint n
,
250 const GLubyte mask
[],
252 CONST GLchan dst
[][4] )
254 SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
256 _swrast_validate_derived( ctx
);
257 _swrast_choose_blend_func( ctx
);
259 swrast
->BlendFunc( ctx
, n
, mask
, src
, dst
);
264 _swrast_validate_texture_sample( GLcontext
*ctx
, GLuint texUnit
,
265 const struct gl_texture_object
*tObj
,
267 const GLfloat s
[], const GLfloat t
[],
268 const GLfloat u
[], const GLfloat lambda
[],
271 SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
273 _swrast_validate_derived( ctx
);
274 _swrast_choose_texture_sample_func( ctx
, texUnit
, tObj
);
276 swrast
->TextureSample
[texUnit
]( ctx
, texUnit
, tObj
, n
, s
, t
, u
,
282 _swrast_sleep( GLcontext
*ctx
, GLuint new_state
)
288 _swrast_invalidate_state( GLcontext
*ctx
, GLuint new_state
)
290 SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
293 swrast
->NewState
|= new_state
;
295 /* After 10 statechanges without any swrast functions being called,
296 * put the module to sleep.
298 if (++swrast
->StateChanges
> 10) {
299 swrast
->InvalidateState
= _swrast_sleep
;
300 swrast
->NewState
= ~0;
304 if (new_state
& swrast
->invalidate_triangle
)
305 swrast
->Triangle
= _swrast_validate_triangle
;
307 if (new_state
& swrast
->invalidate_line
)
308 swrast
->Line
= _swrast_validate_line
;
310 if (new_state
& swrast
->invalidate_point
)
311 swrast
->Point
= _swrast_validate_point
;
313 if (new_state
& _SWRAST_NEW_BLEND_FUNC
)
314 swrast
->BlendFunc
= _swrast_validate_blend_func
;
316 if (new_state
& _SWRAST_NEW_TEXTURE_SAMPLE_FUNC
)
317 for (i
= 0 ; i
< ctx
->Const
.MaxTextureUnits
; i
++)
318 swrast
->TextureSample
[i
] = _swrast_validate_texture_sample
;
321 if (ctx
->Visual
.rgbMode
) {
322 ASSERT(swrast
->Driver
.WriteRGBASpan
);
323 ASSERT(swrast
->Driver
.WriteRGBSpan
);
324 ASSERT(swrast
->Driver
.WriteMonoRGBASpan
);
325 ASSERT(swrast
->Driver
.WriteRGBAPixels
);
326 ASSERT(swrast
->Driver
.WriteMonoRGBAPixels
);
327 ASSERT(swrast
->Driver
.ReadRGBASpan
);
328 ASSERT(swrast
->Driver
.ReadRGBAPixels
);
331 ASSERT(swrast
->Driver
.WriteCI32Span
);
332 ASSERT(swrast
->Driver
.WriteCI8Span
);
333 ASSERT(swrast
->Driver
.WriteMonoCISpan
);
334 ASSERT(swrast
->Driver
.WriteCI32Pixels
);
335 ASSERT(swrast
->Driver
.WriteMonoCIPixels
);
336 ASSERT(swrast
->Driver
.ReadCI32Span
);
337 ASSERT(swrast
->Driver
.ReadCI32Pixels
);
345 _swrast_validate_derived( GLcontext
*ctx
)
347 SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
349 if (swrast
->NewState
)
351 if (swrast
->NewState
& _SWRAST_NEW_RASTERMASK
)
352 _swrast_update_rasterflags( ctx
);
354 if (swrast
->NewState
& _NEW_POLYGON
)
355 _swrast_update_polygon( ctx
);
357 if (swrast
->NewState
& _NEW_HINT
)
358 _swrast_update_hint( ctx
);
360 swrast
->NewState
= 0;
361 swrast
->StateChanges
= 0;
362 swrast
->InvalidateState
= _swrast_invalidate_state
;
366 #define SWRAST_DEBUG 0
368 /* Public entrypoints: See also s_accum.c, s_bitmap.c, etc.
371 _swrast_Quad( GLcontext
*ctx
,
372 const SWvertex
*v0
, const SWvertex
*v1
,
373 const SWvertex
*v2
, const SWvertex
*v3
)
376 fprintf(stderr
, "_swrast_Quad\n");
377 _swrast_print_vertex( ctx
, v0
);
378 _swrast_print_vertex( ctx
, v1
);
379 _swrast_print_vertex( ctx
, v2
);
380 _swrast_print_vertex( ctx
, v3
);
382 SWRAST_CONTEXT(ctx
)->Triangle( ctx
, v0
, v1
, v3
);
383 SWRAST_CONTEXT(ctx
)->Triangle( ctx
, v1
, v2
, v3
);
387 _swrast_Triangle( GLcontext
*ctx
, const SWvertex
*v0
,
388 const SWvertex
*v1
, const SWvertex
*v2
)
391 fprintf(stderr
, "_swrast_Triangle\n");
392 _swrast_print_vertex( ctx
, v0
);
393 _swrast_print_vertex( ctx
, v1
);
394 _swrast_print_vertex( ctx
, v2
);
396 SWRAST_CONTEXT(ctx
)->Triangle( ctx
, v0
, v1
, v2
);
400 _swrast_Line( GLcontext
*ctx
, const SWvertex
*v0
, const SWvertex
*v1
)
403 fprintf(stderr
, "_swrast_Line\n");
404 _swrast_print_vertex( ctx
, v0
);
405 _swrast_print_vertex( ctx
, v1
);
407 SWRAST_CONTEXT(ctx
)->Line( ctx
, v0
, v1
);
411 _swrast_Point( GLcontext
*ctx
, const SWvertex
*v0
)
414 fprintf(stderr
, "_swrast_Point\n");
415 _swrast_print_vertex( ctx
, v0
);
417 SWRAST_CONTEXT(ctx
)->Point( ctx
, v0
);
421 _swrast_InvalidateState( GLcontext
*ctx
, GLuint new_state
)
424 fprintf(stderr
, "_swrast_InvalidateState\n");
426 SWRAST_CONTEXT(ctx
)->InvalidateState( ctx
, new_state
);
430 _swrast_ResetLineStipple( GLcontext
*ctx
)
433 fprintf(stderr
, "_swrast_ResetLineStipple\n");
435 SWRAST_CONTEXT(ctx
)->StippleCounter
= 0;
439 _swrast_allow_vertex_fog( GLcontext
*ctx
, GLboolean value
)
442 fprintf(stderr
, "_swrast_allow_vertex_fog %d\n", value
);
444 SWRAST_CONTEXT(ctx
)->InvalidateState( ctx
, _NEW_HINT
);
445 SWRAST_CONTEXT(ctx
)->AllowVertexFog
= value
;
449 _swrast_allow_pixel_fog( GLcontext
*ctx
, GLboolean value
)
452 fprintf(stderr
, "_swrast_allow_pixel_fog %d\n", value
);
454 SWRAST_CONTEXT(ctx
)->InvalidateState( ctx
, _NEW_HINT
);
455 SWRAST_CONTEXT(ctx
)->AllowPixelFog
= value
;
460 _swrast_CreateContext( GLcontext
*ctx
)
463 SWcontext
*swrast
= (SWcontext
*)CALLOC(sizeof(SWcontext
));
466 fprintf(stderr
, "_swrast_CreateContext\n");
472 swrast
->PB
= _mesa_alloc_pb();
478 swrast
->NewState
= ~0;
480 swrast
->choose_point
= _swrast_choose_point
;
481 swrast
->choose_line
= _swrast_choose_line
;
482 swrast
->choose_triangle
= _swrast_choose_triangle
;
484 swrast
->invalidate_point
= _SWRAST_NEW_POINT
;
485 swrast
->invalidate_line
= _SWRAST_NEW_LINE
;
486 swrast
->invalidate_triangle
= _SWRAST_NEW_TRIANGLE
;
488 swrast
->Point
= _swrast_validate_point
;
489 swrast
->Line
= _swrast_validate_line
;
490 swrast
->Triangle
= _swrast_validate_triangle
;
491 swrast
->InvalidateState
= _swrast_sleep
;
492 swrast
->BlendFunc
= _swrast_validate_blend_func
;
494 swrast
->AllowVertexFog
= GL_TRUE
;
495 swrast
->AllowPixelFog
= GL_TRUE
;
497 /* Optimized Accum buffer */
498 swrast
->_IntegerAccumMode
= GL_TRUE
;
499 swrast
->_IntegerAccumScaler
= 0.0;
502 for (i
= 0 ; i
< MAX_TEXTURE_UNITS
; i
++)
503 swrast
->TextureSample
[i
] = _swrast_validate_texture_sample
;
505 ctx
->swrast_context
= swrast
;
510 _swrast_DestroyContext( GLcontext
*ctx
)
512 SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
515 fprintf(stderr
, "_swrast_DestroyContext\n");
521 ctx
->swrast_context
= 0;
525 struct swrast_device_driver
*
526 _swrast_GetDeviceDriverReference( GLcontext
*ctx
)
528 SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
529 return &swrast
->Driver
;
532 #define SWRAST_DEBUG_VERTICES 1
535 _swrast_print_vertex( GLcontext
*ctx
, const SWvertex
*v
)
539 if (SWRAST_DEBUG_VERTICES
) {
540 fprintf(stderr
, "win %f %f %f %f\n",
541 v
->win
[0], v
->win
[1], v
->win
[2], v
->win
[3]);
545 for (i
= 0 ; i
< ctx
->Const
.MaxTextureUnits
; i
++)
546 fprintf(stderr
, "texcoord[%d] %f %f %f %f\n", i
,
547 v
->texcoord
[i
][0], v
->texcoord
[i
][1],
548 v
->texcoord
[i
][2], v
->texcoord
[i
][3]);
550 #if CHAN_TYPE == GL_FLOAT
551 fprintf(stderr
, "color %f %f %f %f\n",
552 v
->color
[0], v
->color
[1], v
->color
[2], v
->color
[3]);
553 fprintf(stderr
, "spec %f %f %f %f\n",
554 v
->specular
[0], v
->specular
[1], v
->specular
[2], v
->specular
[3]);
556 fprintf(stderr
, "color %d %d %d %d\n",
557 v
->color
[0], v
->color
[1], v
->color
[2], v
->color
[3]);
558 fprintf(stderr
, "spec %d %d %d %d\n",
559 v
->specular
[0], v
->specular
[1], v
->specular
[2], v
->specular
[3]);
561 fprintf(stderr
, "fog %f\n", v
->fog
);
562 fprintf(stderr
, "index %d\n", v
->index
);
563 fprintf(stderr
, "pointsize %f\n", v
->pointSize
);
564 fprintf(stderr
, "\n");