2 * Mesa 3-D graphics library
5 * Copyright (C) 1999-2003 Brian Paul All Rights Reserved.
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:
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
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.
25 * Keith Whitwell <keith@tungstengraphics.com>
33 #include "nvfragprog.h"
37 #include "s_context.h"
41 #include "s_triangle.h"
42 #include "s_texture.h"
46 * Recompute the value of swrast->_RasterMask, etc. according to
47 * the current context.
50 _swrast_update_rasterflags( GLcontext
*ctx
)
52 GLuint RasterMask
= 0;
54 if (ctx
->Color
.AlphaEnabled
) RasterMask
|= ALPHATEST_BIT
;
55 if (ctx
->Color
.BlendEnabled
) RasterMask
|= BLEND_BIT
;
56 if (ctx
->Depth
.Test
) RasterMask
|= DEPTH_BIT
;
57 if (ctx
->Fog
.Enabled
) RasterMask
|= FOG_BIT
;
58 if (ctx
->Scissor
.Enabled
) RasterMask
|= CLIP_BIT
;
59 if (ctx
->Stencil
.Enabled
) RasterMask
|= STENCIL_BIT
;
60 if (ctx
->Visual
.rgbMode
) {
61 const GLuint colorMask
= *((GLuint
*) &ctx
->Color
.ColorMask
);
62 if (colorMask
!= 0xffffffff) RasterMask
|= MASKING_BIT
;
63 if (ctx
->Color
._LogicOpEnabled
) RasterMask
|= LOGIC_OP_BIT
;
64 if (ctx
->Texture
._EnabledUnits
) RasterMask
|= TEXTURE_BIT
;
67 if (ctx
->Color
.IndexMask
!= 0xffffffff) RasterMask
|= MASKING_BIT
;
68 if (ctx
->Color
.IndexLogicOpEnabled
) RasterMask
|= LOGIC_OP_BIT
;
71 if (ctx
->DrawBuffer
->UseSoftwareAlphaBuffers
72 && ctx
->Color
.ColorMask
[ACOMP
]
73 && ctx
->Color
.DrawBuffer
!= GL_NONE
)
74 RasterMask
|= ALPHABUF_BIT
;
76 if ( ctx
->Viewport
.X
< 0
77 || ctx
->Viewport
.X
+ ctx
->Viewport
.Width
> (GLint
) ctx
->DrawBuffer
->Width
78 || ctx
->Viewport
.Y
< 0
79 || ctx
->Viewport
.Y
+ ctx
->Viewport
.Height
> (GLint
) ctx
->DrawBuffer
->Height
) {
80 RasterMask
|= CLIP_BIT
;
83 if (ctx
->Depth
.OcclusionTest
|| ctx
->Occlusion
.Active
)
84 RasterMask
|= OCCLUSION_BIT
;
87 /* If we're not drawing to exactly one color buffer set the
88 * MULTI_DRAW_BIT flag. Also set it if we're drawing to no
89 * buffers or the RGBA or CI mask disables all writes.
91 if (ctx
->Color
._DrawDestMask
!= FRONT_LEFT_BIT
&&
92 ctx
->Color
._DrawDestMask
!= BACK_LEFT_BIT
&&
93 ctx
->Color
._DrawDestMask
!= FRONT_RIGHT_BIT
&&
94 ctx
->Color
._DrawDestMask
!= BACK_RIGHT_BIT
) {
95 /* more than one color buffer designated for writing (or zero buffers) */
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
->FragmentProgram
.Enabled
) {
106 RasterMask
|= FRAGPROG_BIT
;
109 SWRAST_CONTEXT(ctx
)->_RasterMask
= RasterMask
;
114 _swrast_update_polygon( GLcontext
*ctx
)
116 GLfloat backface_sign
= 1;
118 if (ctx
->Polygon
.CullFlag
) {
120 switch(ctx
->Polygon
.CullFaceMode
) {
122 if(ctx
->Polygon
.FrontFace
==GL_CCW
)
126 if(ctx
->Polygon
.FrontFace
!=GL_CCW
)
130 case GL_FRONT_AND_BACK
:
139 SWRAST_CONTEXT(ctx
)->_BackfaceSign
= backface_sign
;
144 _swrast_update_hint( GLcontext
*ctx
)
146 SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
147 swrast
->_PreferPixelFog
= (!swrast
->AllowVertexFog
||
148 (ctx
->Hint
.Fog
== GL_NICEST
&&
149 swrast
->AllowPixelFog
));
154 * Update the swrast->_AnyTextureCombine flag.
157 _swrast_update_texture_env( GLcontext
*ctx
)
159 SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
161 swrast
->_AnyTextureCombine
= GL_FALSE
;
162 for (i
= 0; i
< ctx
->Const
.MaxTextureUnits
; i
++) {
163 if (ctx
->Texture
.Unit
[i
].EnvMode
== GL_COMBINE_EXT
||
164 ctx
->Texture
.Unit
[i
].EnvMode
== GL_COMBINE4_NV
) {
165 swrast
->_AnyTextureCombine
= GL_TRUE
;
172 #define _SWRAST_NEW_DERIVED (_SWRAST_NEW_RASTERMASK | \
177 /* State referenced by _swrast_choose_triangle, _swrast_choose_line.
179 #define _SWRAST_NEW_TRIANGLE (_SWRAST_NEW_DERIVED | \
186 _SWRAST_NEW_RASTERMASK| \
189 _DD_NEW_SEPARATE_SPECULAR)
191 #define _SWRAST_NEW_LINE (_SWRAST_NEW_DERIVED | \
198 _DD_NEW_SEPARATE_SPECULAR)
200 #define _SWRAST_NEW_POINT (_SWRAST_NEW_DERIVED | \
206 _DD_NEW_SEPARATE_SPECULAR)
208 #define _SWRAST_NEW_TEXTURE_SAMPLE_FUNC _NEW_TEXTURE
210 #define _SWRAST_NEW_TEXTURE_ENV_MODE _NEW_TEXTURE
212 #define _SWRAST_NEW_BLEND_FUNC _NEW_COLOR
216 /* Stub for swrast->Triangle to select a true triangle function
217 * after a state change.
220 _swrast_validate_triangle( GLcontext
*ctx
,
225 SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
227 _swrast_validate_derived( ctx
);
228 swrast
->choose_triangle( ctx
);
230 if (ctx
->Texture
._EnabledUnits
== 0
231 && NEED_SECONDARY_COLOR(ctx
)
232 && !ctx
->FragmentProgram
.Enabled
) {
233 /* separate specular color, but no texture */
234 swrast
->SpecTriangle
= swrast
->Triangle
;
235 swrast
->Triangle
= _swrast_add_spec_terms_triangle
;
238 swrast
->Triangle( ctx
, v0
, v1
, v2
);
242 _swrast_validate_line( GLcontext
*ctx
, const SWvertex
*v0
, const SWvertex
*v1
)
244 SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
246 _swrast_validate_derived( ctx
);
247 swrast
->choose_line( ctx
);
249 if (ctx
->Texture
._EnabledUnits
== 0
250 && NEED_SECONDARY_COLOR(ctx
)
251 && !ctx
->FragmentProgram
.Enabled
) {
252 swrast
->SpecLine
= swrast
->Line
;
253 swrast
->Line
= _swrast_add_spec_terms_line
;
257 swrast
->Line( ctx
, v0
, v1
);
261 _swrast_validate_point( GLcontext
*ctx
, const SWvertex
*v0
)
263 SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
265 _swrast_validate_derived( ctx
);
266 swrast
->choose_point( ctx
);
268 if (ctx
->Texture
._EnabledUnits
== 0
269 && NEED_SECONDARY_COLOR(ctx
)
270 && !ctx
->FragmentProgram
.Enabled
) {
271 swrast
->SpecPoint
= swrast
->Point
;
272 swrast
->Point
= _swrast_add_spec_terms_point
;
275 swrast
->Point( ctx
, v0
);
280 _swrast_validate_blend_func( GLcontext
*ctx
, GLuint n
,
281 const GLubyte mask
[],
283 CONST GLchan dst
[][4] )
285 SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
287 _swrast_validate_derived( ctx
);
288 _swrast_choose_blend_func( ctx
);
290 swrast
->BlendFunc( ctx
, n
, mask
, src
, dst
);
295 _swrast_validate_texture_sample( GLcontext
*ctx
, GLuint texUnit
,
296 const struct gl_texture_object
*tObj
,
297 GLuint n
, const GLfloat texcoords
[][4],
298 const GLfloat lambda
[], GLchan rgba
[][4] )
300 SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
302 _swrast_validate_derived( ctx
);
304 /* Compute min/mag filter threshold */
305 if (tObj
->MinFilter
!= tObj
->MagFilter
) {
306 if (tObj
->MagFilter
== GL_LINEAR
307 && (tObj
->MinFilter
== GL_NEAREST_MIPMAP_NEAREST
||
308 tObj
->MinFilter
== GL_NEAREST_MIPMAP_LINEAR
)) {
309 swrast
->_MinMagThresh
[texUnit
] = 0.5F
;
312 swrast
->_MinMagThresh
[texUnit
] = 0.0F
;
316 swrast
->TextureSample
[texUnit
] =
317 _swrast_choose_texture_sample_func( ctx
, tObj
);
319 swrast
->TextureSample
[texUnit
]( ctx
, texUnit
, tObj
, n
, texcoords
,
325 _swrast_sleep( GLcontext
*ctx
, GLuint new_state
)
331 _swrast_invalidate_state( GLcontext
*ctx
, GLuint new_state
)
333 SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
336 swrast
->NewState
|= new_state
;
338 /* After 10 statechanges without any swrast functions being called,
339 * put the module to sleep.
341 if (++swrast
->StateChanges
> 10) {
342 swrast
->InvalidateState
= _swrast_sleep
;
343 swrast
->NewState
= ~0;
347 if (new_state
& swrast
->invalidate_triangle
)
348 swrast
->Triangle
= _swrast_validate_triangle
;
350 if (new_state
& swrast
->invalidate_line
)
351 swrast
->Line
= _swrast_validate_line
;
353 if (new_state
& swrast
->invalidate_point
)
354 swrast
->Point
= _swrast_validate_point
;
356 if (new_state
& _SWRAST_NEW_BLEND_FUNC
)
357 swrast
->BlendFunc
= _swrast_validate_blend_func
;
359 if (new_state
& _SWRAST_NEW_TEXTURE_SAMPLE_FUNC
)
360 for (i
= 0 ; i
< ctx
->Const
.MaxTextureUnits
; i
++)
361 swrast
->TextureSample
[i
] = _swrast_validate_texture_sample
;
363 if (ctx
->Visual
.rgbMode
) {
364 ASSERT(swrast
->Driver
.WriteRGBASpan
);
365 ASSERT(swrast
->Driver
.WriteRGBSpan
);
366 ASSERT(swrast
->Driver
.WriteMonoRGBASpan
);
367 ASSERT(swrast
->Driver
.WriteRGBAPixels
);
368 ASSERT(swrast
->Driver
.WriteMonoRGBAPixels
);
369 ASSERT(swrast
->Driver
.ReadRGBASpan
);
370 ASSERT(swrast
->Driver
.ReadRGBAPixels
);
373 ASSERT(swrast
->Driver
.WriteCI32Span
);
374 ASSERT(swrast
->Driver
.WriteCI8Span
);
375 ASSERT(swrast
->Driver
.WriteMonoCISpan
);
376 ASSERT(swrast
->Driver
.WriteCI32Pixels
);
377 ASSERT(swrast
->Driver
.WriteMonoCIPixels
);
378 ASSERT(swrast
->Driver
.ReadCI32Span
);
379 ASSERT(swrast
->Driver
.ReadCI32Pixels
);
385 _swrast_validate_derived( GLcontext
*ctx
)
387 SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
389 if (swrast
->NewState
) {
390 if (swrast
->NewState
& _SWRAST_NEW_RASTERMASK
)
391 _swrast_update_rasterflags( ctx
);
393 if (swrast
->NewState
& _NEW_POLYGON
)
394 _swrast_update_polygon( ctx
);
396 if (swrast
->NewState
& _NEW_HINT
)
397 _swrast_update_hint( ctx
);
399 if (swrast
->NewState
& _SWRAST_NEW_TEXTURE_ENV_MODE
)
400 _swrast_update_texture_env( ctx
);
402 swrast
->NewState
= 0;
403 swrast
->StateChanges
= 0;
404 swrast
->InvalidateState
= _swrast_invalidate_state
;
408 #define SWRAST_DEBUG 0
410 /* Public entrypoints: See also s_accum.c, s_bitmap.c, etc.
413 _swrast_Quad( GLcontext
*ctx
,
414 const SWvertex
*v0
, const SWvertex
*v1
,
415 const SWvertex
*v2
, const SWvertex
*v3
)
418 _mesa_debug(ctx
, "_swrast_Quad\n");
419 _swrast_print_vertex( ctx
, v0
);
420 _swrast_print_vertex( ctx
, v1
);
421 _swrast_print_vertex( ctx
, v2
);
422 _swrast_print_vertex( ctx
, v3
);
424 SWRAST_CONTEXT(ctx
)->Triangle( ctx
, v0
, v1
, v3
);
425 SWRAST_CONTEXT(ctx
)->Triangle( ctx
, v1
, v2
, v3
);
429 _swrast_Triangle( GLcontext
*ctx
, const SWvertex
*v0
,
430 const SWvertex
*v1
, const SWvertex
*v2
)
433 _mesa_debug(ctx
, "_swrast_Triangle\n");
434 _swrast_print_vertex( ctx
, v0
);
435 _swrast_print_vertex( ctx
, v1
);
436 _swrast_print_vertex( ctx
, v2
);
438 SWRAST_CONTEXT(ctx
)->Triangle( ctx
, v0
, v1
, v2
);
442 _swrast_Line( GLcontext
*ctx
, const SWvertex
*v0
, const SWvertex
*v1
)
445 _mesa_debug(ctx
, "_swrast_Line\n");
446 _swrast_print_vertex( ctx
, v0
);
447 _swrast_print_vertex( ctx
, v1
);
449 SWRAST_CONTEXT(ctx
)->Line( ctx
, v0
, v1
);
453 _swrast_Point( GLcontext
*ctx
, const SWvertex
*v0
)
456 _mesa_debug(ctx
, "_swrast_Point\n");
457 _swrast_print_vertex( ctx
, v0
);
459 SWRAST_CONTEXT(ctx
)->Point( ctx
, v0
);
463 _swrast_InvalidateState( GLcontext
*ctx
, GLuint new_state
)
466 _mesa_debug(ctx
, "_swrast_InvalidateState\n");
468 SWRAST_CONTEXT(ctx
)->InvalidateState( ctx
, new_state
);
472 _swrast_ResetLineStipple( GLcontext
*ctx
)
475 _mesa_debug(ctx
, "_swrast_ResetLineStipple\n");
477 SWRAST_CONTEXT(ctx
)->StippleCounter
= 0;
481 _swrast_allow_vertex_fog( GLcontext
*ctx
, GLboolean value
)
484 _mesa_debug(ctx
, "_swrast_allow_vertex_fog %d\n", value
);
486 SWRAST_CONTEXT(ctx
)->InvalidateState( ctx
, _NEW_HINT
);
487 SWRAST_CONTEXT(ctx
)->AllowVertexFog
= value
;
491 _swrast_allow_pixel_fog( GLcontext
*ctx
, GLboolean value
)
494 _mesa_debug(ctx
, "_swrast_allow_pixel_fog %d\n", value
);
496 SWRAST_CONTEXT(ctx
)->InvalidateState( ctx
, _NEW_HINT
);
497 SWRAST_CONTEXT(ctx
)->AllowPixelFog
= value
;
502 _swrast_CreateContext( GLcontext
*ctx
)
505 SWcontext
*swrast
= (SWcontext
*)CALLOC(sizeof(SWcontext
));
508 _mesa_debug(ctx
, "_swrast_CreateContext\n");
514 swrast
->NewState
= ~0;
516 swrast
->choose_point
= _swrast_choose_point
;
517 swrast
->choose_line
= _swrast_choose_line
;
518 swrast
->choose_triangle
= _swrast_choose_triangle
;
520 swrast
->invalidate_point
= _SWRAST_NEW_POINT
;
521 swrast
->invalidate_line
= _SWRAST_NEW_LINE
;
522 swrast
->invalidate_triangle
= _SWRAST_NEW_TRIANGLE
;
524 swrast
->Point
= _swrast_validate_point
;
525 swrast
->Line
= _swrast_validate_line
;
526 swrast
->Triangle
= _swrast_validate_triangle
;
527 swrast
->InvalidateState
= _swrast_sleep
;
528 swrast
->BlendFunc
= _swrast_validate_blend_func
;
530 swrast
->AllowVertexFog
= GL_TRUE
;
531 swrast
->AllowPixelFog
= GL_TRUE
;
533 if (ctx
->Visual
.doubleBufferMode
)
534 swrast
->CurrentBuffer
= BACK_LEFT_BIT
;
536 swrast
->CurrentBuffer
= FRONT_LEFT_BIT
;
538 /* Optimized Accum buffer */
539 swrast
->_IntegerAccumMode
= GL_TRUE
;
540 swrast
->_IntegerAccumScaler
= 0.0;
542 for (i
= 0; i
< MAX_TEXTURE_IMAGE_UNITS
; i
++)
543 swrast
->TextureSample
[i
] = _swrast_validate_texture_sample
;
545 swrast
->SpanArrays
= MALLOC_STRUCT(span_arrays
);
546 if (!swrast
->SpanArrays
) {
551 /* init point span buffer */
552 swrast
->PointSpan
.primitive
= GL_POINT
;
553 swrast
->PointSpan
.start
= 0;
554 swrast
->PointSpan
.end
= 0;
555 swrast
->PointSpan
.facing
= 0;
556 swrast
->PointSpan
.array
= swrast
->SpanArrays
;
558 assert(ctx
->Const
.MaxTextureUnits
> 0);
559 assert(ctx
->Const
.MaxTextureUnits
<= MAX_TEXTURE_UNITS
);
561 swrast
->TexelBuffer
= (GLchan
*) MALLOC(ctx
->Const
.MaxTextureUnits
*
562 MAX_WIDTH
* 4 * sizeof(GLchan
));
563 if (!swrast
->TexelBuffer
) {
564 FREE(swrast
->SpanArrays
);
569 ctx
->swrast_context
= swrast
;
575 _swrast_DestroyContext( GLcontext
*ctx
)
577 SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
580 _mesa_debug(ctx
, "_swrast_DestroyContext\n");
583 FREE( swrast
->SpanArrays
);
584 FREE( swrast
->TexelBuffer
);
587 ctx
->swrast_context
= 0;
591 struct swrast_device_driver
*
592 _swrast_GetDeviceDriverReference( GLcontext
*ctx
)
594 SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
595 return &swrast
->Driver
;
599 _swrast_flush( GLcontext
*ctx
)
601 SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
602 /* flush any pending fragments from rendering points */
603 if (swrast
->PointSpan
.end
> 0) {
604 if (ctx
->Visual
.rgbMode
) {
605 if (ctx
->Texture
._EnabledCoordUnits
)
606 _swrast_write_texture_span(ctx
, &(swrast
->PointSpan
));
608 _swrast_write_rgba_span(ctx
, &(swrast
->PointSpan
));
611 _swrast_write_index_span(ctx
, &(swrast
->PointSpan
));
613 swrast
->PointSpan
.end
= 0;
618 _swrast_render_primitive( GLcontext
*ctx
, GLenum prim
)
620 SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
621 if (swrast
->Primitive
== GL_POINTS
&& prim
!= GL_POINTS
) {
624 swrast
->Primitive
= prim
;
629 _swrast_render_start( GLcontext
*ctx
)
631 SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
632 if (swrast
->Driver
.SpanRenderStart
)
633 swrast
->Driver
.SpanRenderStart( ctx
);
634 swrast
->PointSpan
.end
= 0;
638 _swrast_render_finish( GLcontext
*ctx
)
640 SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
641 if (swrast
->Driver
.SpanRenderFinish
)
642 swrast
->Driver
.SpanRenderFinish( ctx
);
648 #define SWRAST_DEBUG_VERTICES 0
651 _swrast_print_vertex( GLcontext
*ctx
, const SWvertex
*v
)
655 if (SWRAST_DEBUG_VERTICES
) {
656 _mesa_debug(ctx
, "win %f %f %f %f\n",
657 v
->win
[0], v
->win
[1], v
->win
[2], v
->win
[3]);
659 for (i
= 0 ; i
< ctx
->Const
.MaxTextureUnits
; i
++)
660 if (ctx
->Texture
.Unit
[i
]._ReallyEnabled
)
661 _mesa_debug(ctx
, "texcoord[%d] %f %f %f %f\n", i
,
662 v
->texcoord
[i
][0], v
->texcoord
[i
][1],
663 v
->texcoord
[i
][2], v
->texcoord
[i
][3]);
665 #if CHAN_TYPE == GL_FLOAT
666 _mesa_debug(ctx
, "color %f %f %f %f\n",
667 v
->color
[0], v
->color
[1], v
->color
[2], v
->color
[3]);
668 _mesa_debug(ctx
, "spec %f %f %f %f\n",
669 v
->specular
[0], v
->specular
[1],
670 v
->specular
[2], v
->specular
[3]);
672 _mesa_debug(ctx
, "color %d %d %d %d\n",
673 v
->color
[0], v
->color
[1], v
->color
[2], v
->color
[3]);
674 _mesa_debug(ctx
, "spec %d %d %d %d\n",
675 v
->specular
[0], v
->specular
[1],
676 v
->specular
[2], v
->specular
[3]);
678 _mesa_debug(ctx
, "fog %f\n", v
->fog
);
679 _mesa_debug(ctx
, "index %d\n", v
->index
);
680 _mesa_debug(ctx
, "pointsize %f\n", v
->pointSize
);
681 _mesa_debug(ctx
, "\n");