2 * Mesa 3-D graphics library
5 * Copyright (C) 1999-2008 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>
29 #include "main/imports.h"
30 #include "main/bufferobj.h"
31 #include "main/colormac.h"
32 #include "main/mtypes.h"
33 #include "main/teximage.h"
34 #include "program/prog_parameter.h"
35 #include "program/prog_statevars.h"
38 #include "s_context.h"
42 #include "s_texfetch.h"
43 #include "s_triangle.h"
44 #include "s_texfilter.h"
48 * Recompute the value of swrast->_RasterMask, etc. according to
49 * the current context. The _RasterMask field can be easily tested by
50 * drivers to determine certain basic GL state (does the primitive need
51 * stenciling, logic-op, fog, etc?).
54 _swrast_update_rasterflags( struct gl_context
*ctx
)
56 SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
57 GLbitfield rasterMask
= 0;
60 if (ctx
->Color
.AlphaEnabled
) rasterMask
|= ALPHATEST_BIT
;
61 if (ctx
->Color
.BlendEnabled
) rasterMask
|= BLEND_BIT
;
62 if (ctx
->Depth
.Test
) rasterMask
|= DEPTH_BIT
;
63 if (swrast
->_FogEnabled
) rasterMask
|= FOG_BIT
;
64 if (ctx
->Scissor
.Enabled
) rasterMask
|= CLIP_BIT
;
65 if (ctx
->Stencil
._Enabled
) rasterMask
|= STENCIL_BIT
;
66 for (i
= 0; i
< ctx
->Const
.MaxDrawBuffers
; i
++) {
67 if (!ctx
->Color
.ColorMask
[i
][0] ||
68 !ctx
->Color
.ColorMask
[i
][1] ||
69 !ctx
->Color
.ColorMask
[i
][2] ||
70 !ctx
->Color
.ColorMask
[i
][3]) {
71 rasterMask
|= MASKING_BIT
;
75 if (ctx
->Color
.ColorLogicOpEnabled
) rasterMask
|= LOGIC_OP_BIT
;
76 if (ctx
->Texture
._EnabledUnits
) rasterMask
|= TEXTURE_BIT
;
77 if ( ctx
->Viewport
.X
< 0
78 || ctx
->Viewport
.X
+ ctx
->Viewport
.Width
> (GLint
) ctx
->DrawBuffer
->Width
79 || ctx
->Viewport
.Y
< 0
80 || ctx
->Viewport
.Y
+ ctx
->Viewport
.Height
> (GLint
) ctx
->DrawBuffer
->Height
) {
81 rasterMask
|= CLIP_BIT
;
84 if (ctx
->Query
.CurrentOcclusionObject
)
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
->DrawBuffer
->_NumColorDrawBuffers
!= 1) {
93 /* more than one color buffer designated for writing (or zero buffers) */
94 rasterMask
|= MULTI_DRAW_BIT
;
97 for (i
= 0; i
< ctx
->Const
.MaxDrawBuffers
; i
++) {
98 if (ctx
->Color
.ColorMask
[i
][0] +
99 ctx
->Color
.ColorMask
[i
][1] +
100 ctx
->Color
.ColorMask
[i
][2] +
101 ctx
->Color
.ColorMask
[i
][3] == 0) {
102 rasterMask
|= MULTI_DRAW_BIT
; /* all RGBA channels disabled */
108 if (ctx
->FragmentProgram
._Current
) {
109 rasterMask
|= FRAGPROG_BIT
;
112 if (ctx
->ATIFragmentShader
._Enabled
) {
113 rasterMask
|= ATIFRAGSHADER_BIT
;
116 #if CHAN_TYPE == GL_FLOAT
117 if (ctx
->Color
.ClampFragmentColor
== GL_TRUE
) {
118 rasterMask
|= CLAMPING_BIT
;
122 SWRAST_CONTEXT(ctx
)->_RasterMask
= rasterMask
;
127 * Examine polygon cull state to compute the _BackfaceCullSign field.
128 * _BackfaceCullSign will be 0 if no culling, -1 if culling back-faces,
129 * and 1 if culling front-faces. The Polygon FrontFace state also
133 _swrast_update_polygon( struct gl_context
*ctx
)
135 GLfloat backface_sign
;
137 if (ctx
->Polygon
.CullFlag
) {
138 switch (ctx
->Polygon
.CullFaceMode
) {
140 backface_sign
= -1.0F
;
143 backface_sign
= 1.0F
;
145 case GL_FRONT_AND_BACK
:
148 backface_sign
= 0.0F
;
152 backface_sign
= 0.0F
;
155 SWRAST_CONTEXT(ctx
)->_BackfaceCullSign
= backface_sign
;
157 /* This is for front/back-face determination, but not for culling */
158 SWRAST_CONTEXT(ctx
)->_BackfaceSign
159 = (ctx
->Polygon
.FrontFace
== GL_CW
) ? -1.0F
: 1.0F
;
165 * Update the _PreferPixelFog field to indicate if we need to compute
166 * fog blend factors (from the fog coords) per-fragment.
169 _swrast_update_fog_hint( struct gl_context
*ctx
)
171 SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
172 swrast
->_PreferPixelFog
= (!swrast
->AllowVertexFog
||
173 ctx
->FragmentProgram
._Current
||
174 (ctx
->Hint
.Fog
== GL_NICEST
&&
175 swrast
->AllowPixelFog
));
181 * Update the swrast->_TextureCombinePrimary flag.
184 _swrast_update_texture_env( struct gl_context
*ctx
)
186 SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
189 swrast
->_TextureCombinePrimary
= GL_FALSE
;
191 for (i
= 0; i
< ctx
->Const
.MaxTextureUnits
; i
++) {
192 const struct gl_tex_env_combine_state
*combine
=
193 ctx
->Texture
.Unit
[i
]._CurrentCombine
;
195 for (term
= 0; term
< combine
->_NumArgsRGB
; term
++) {
196 if (combine
->SourceRGB
[term
] == GL_PRIMARY_COLOR
) {
197 swrast
->_TextureCombinePrimary
= GL_TRUE
;
200 if (combine
->SourceA
[term
] == GL_PRIMARY_COLOR
) {
201 swrast
->_TextureCombinePrimary
= GL_TRUE
;
210 * Determine if we can defer texturing/shading until after Z/stencil
211 * testing. This potentially allows us to skip texturing/shading for
215 _swrast_update_deferred_texture(struct gl_context
*ctx
)
217 SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
218 if (ctx
->Color
.AlphaEnabled
) {
219 /* alpha test depends on post-texture/shader colors */
220 swrast
->_DeferredTexture
= GL_FALSE
;
223 const struct gl_fragment_program
*fprog
224 = ctx
->FragmentProgram
._Current
;
225 if (fprog
&& (fprog
->Base
.OutputsWritten
& (1 << FRAG_RESULT_DEPTH
))) {
226 /* Z comes from fragment program/shader */
227 swrast
->_DeferredTexture
= GL_FALSE
;
229 else if (fprog
&& fprog
->UsesKill
) {
230 swrast
->_DeferredTexture
= GL_FALSE
;
232 else if (ctx
->Query
.CurrentOcclusionObject
) {
233 /* occlusion query depends on shader discard/kill results */
234 swrast
->_DeferredTexture
= GL_FALSE
;
237 swrast
->_DeferredTexture
= GL_TRUE
;
244 * Update swrast->_FogColor and swrast->_FogEnable values.
247 _swrast_update_fog_state( struct gl_context
*ctx
)
249 SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
250 const struct gl_fragment_program
*fp
= ctx
->FragmentProgram
._Current
;
252 assert((fp
== NULL
) ||
253 (fp
->Base
.Target
== GL_FRAGMENT_PROGRAM_ARB
) ||
254 (fp
->Base
.Target
== GL_FRAGMENT_PROGRAM_NV
));
256 /* determine if fog is needed, and if so, which fog mode */
257 swrast
->_FogEnabled
= (fp
== NULL
&& ctx
->Fog
.Enabled
);
262 * Update state for running fragment programs. Basically, load the
263 * program parameters with current state values.
266 _swrast_update_fragment_program(struct gl_context
*ctx
, GLbitfield newState
)
268 const struct gl_fragment_program
*fp
= ctx
->FragmentProgram
._Current
;
270 _mesa_load_state_parameters(ctx
, fp
->Base
.Parameters
);
276 * See if we can do early diffuse+specular (primary+secondary) color
277 * add per vertex instead of per-fragment.
280 _swrast_update_specular_vertex_add(struct gl_context
*ctx
)
282 SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
283 GLboolean separateSpecular
= ctx
->Fog
.ColorSumEnabled
||
284 (ctx
->Light
.Enabled
&&
285 ctx
->Light
.Model
.ColorControl
== GL_SEPARATE_SPECULAR_COLOR
);
287 swrast
->SpecularVertexAdd
= (separateSpecular
288 && ctx
->Texture
._EnabledUnits
== 0x0
289 && !ctx
->FragmentProgram
._Current
290 && !ctx
->ATIFragmentShader
._Enabled
);
294 #define _SWRAST_NEW_DERIVED (_SWRAST_NEW_RASTERMASK | \
295 _NEW_PROGRAM_CONSTANTS | \
300 /* State referenced by _swrast_choose_triangle, _swrast_choose_line.
302 #define _SWRAST_NEW_TRIANGLE (_SWRAST_NEW_DERIVED | \
309 _SWRAST_NEW_RASTERMASK| \
312 _DD_NEW_SEPARATE_SPECULAR)
314 #define _SWRAST_NEW_LINE (_SWRAST_NEW_DERIVED | \
321 _DD_NEW_SEPARATE_SPECULAR)
323 #define _SWRAST_NEW_POINT (_SWRAST_NEW_DERIVED | \
329 _DD_NEW_SEPARATE_SPECULAR)
331 #define _SWRAST_NEW_TEXTURE_SAMPLE_FUNC _NEW_TEXTURE
333 #define _SWRAST_NEW_TEXTURE_ENV_MODE _NEW_TEXTURE
335 #define _SWRAST_NEW_BLEND_FUNC _NEW_COLOR
340 * Stub for swrast->Triangle to select a true triangle function
341 * after a state change.
344 _swrast_validate_triangle( struct gl_context
*ctx
,
349 SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
351 _swrast_validate_derived( ctx
);
352 swrast
->choose_triangle( ctx
);
353 ASSERT(swrast
->Triangle
);
355 if (swrast
->SpecularVertexAdd
) {
356 /* separate specular color, but no texture */
357 swrast
->SpecTriangle
= swrast
->Triangle
;
358 swrast
->Triangle
= _swrast_add_spec_terms_triangle
;
361 swrast
->Triangle( ctx
, v0
, v1
, v2
);
365 * Called via swrast->Line. Examine current GL state and choose a software
366 * line routine. Then call it.
369 _swrast_validate_line( struct gl_context
*ctx
, const SWvertex
*v0
, const SWvertex
*v1
)
371 SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
373 _swrast_validate_derived( ctx
);
374 swrast
->choose_line( ctx
);
375 ASSERT(swrast
->Line
);
377 if (swrast
->SpecularVertexAdd
) {
378 swrast
->SpecLine
= swrast
->Line
;
379 swrast
->Line
= _swrast_add_spec_terms_line
;
382 swrast
->Line( ctx
, v0
, v1
);
386 * Called via swrast->Point. Examine current GL state and choose a software
387 * point routine. Then call it.
390 _swrast_validate_point( struct gl_context
*ctx
, const SWvertex
*v0
)
392 SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
394 _swrast_validate_derived( ctx
);
395 swrast
->choose_point( ctx
);
397 if (swrast
->SpecularVertexAdd
) {
398 swrast
->SpecPoint
= swrast
->Point
;
399 swrast
->Point
= _swrast_add_spec_terms_point
;
402 swrast
->Point( ctx
, v0
);
407 * Called via swrast->BlendFunc. Examine GL state to choose a blending
408 * function, then call it.
411 _swrast_validate_blend_func(struct gl_context
*ctx
, GLuint n
, const GLubyte mask
[],
412 GLvoid
*src
, const GLvoid
*dst
,
415 SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
417 _swrast_validate_derived( ctx
); /* why is this needed? */
418 _swrast_choose_blend_func( ctx
, chanType
);
420 swrast
->BlendFunc( ctx
, n
, mask
, src
, dst
, chanType
);
424 _swrast_sleep( struct gl_context
*ctx
, GLbitfield new_state
)
426 (void) ctx
; (void) new_state
;
431 _swrast_invalidate_state( struct gl_context
*ctx
, GLbitfield new_state
)
433 SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
436 swrast
->NewState
|= new_state
;
438 /* After 10 statechanges without any swrast functions being called,
439 * put the module to sleep.
441 if (++swrast
->StateChanges
> 10) {
442 swrast
->InvalidateState
= _swrast_sleep
;
443 swrast
->NewState
= ~0;
447 if (new_state
& swrast
->InvalidateTriangleMask
)
448 swrast
->Triangle
= _swrast_validate_triangle
;
450 if (new_state
& swrast
->InvalidateLineMask
)
451 swrast
->Line
= _swrast_validate_line
;
453 if (new_state
& swrast
->InvalidatePointMask
)
454 swrast
->Point
= _swrast_validate_point
;
456 if (new_state
& _SWRAST_NEW_BLEND_FUNC
)
457 swrast
->BlendFunc
= _swrast_validate_blend_func
;
459 if (new_state
& _SWRAST_NEW_TEXTURE_SAMPLE_FUNC
)
460 for (i
= 0 ; i
< ctx
->Const
.MaxTextureImageUnits
; i
++)
461 swrast
->TextureSample
[i
] = NULL
;
466 _swrast_update_texture_samplers(struct gl_context
*ctx
)
468 SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
472 return; /* pipe hack */
474 for (u
= 0; u
< ctx
->Const
.MaxTextureImageUnits
; u
++) {
475 struct gl_texture_object
*tObj
= ctx
->Texture
.Unit
[u
]._Current
;
476 /* Note: If tObj is NULL, the sample function will be a simple
477 * function that just returns opaque black (0,0,0,1).
480 _mesa_update_fetch_functions(tObj
);
482 swrast
->TextureSample
[u
] = _swrast_choose_texture_sample_func(ctx
, tObj
);
488 * Update swrast->_ActiveAttribs, swrast->_NumActiveAttribs,
489 * swrast->_ActiveAtttribMask.
492 _swrast_update_active_attribs(struct gl_context
*ctx
)
494 SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
498 * Compute _ActiveAttribsMask = which fragment attributes are needed.
500 if (ctx
->FragmentProgram
._Current
) {
501 /* fragment program/shader */
502 attribsMask
= ctx
->FragmentProgram
._Current
->Base
.InputsRead
;
503 attribsMask
&= ~FRAG_BIT_WPOS
; /* WPOS is always handled specially */
505 else if (ctx
->ATIFragmentShader
._Enabled
) {
506 attribsMask
= ~0; /* XXX fix me */
512 #if CHAN_TYPE == GL_FLOAT
513 attribsMask
|= FRAG_BIT_COL0
;
516 if (ctx
->Fog
.ColorSumEnabled
||
517 (ctx
->Light
.Enabled
&&
518 ctx
->Light
.Model
.ColorControl
== GL_SEPARATE_SPECULAR_COLOR
)) {
519 attribsMask
|= FRAG_BIT_COL1
;
522 if (swrast
->_FogEnabled
)
523 attribsMask
|= FRAG_BIT_FOGC
;
525 attribsMask
|= (ctx
->Texture
._EnabledUnits
<< FRAG_ATTRIB_TEX0
);
528 swrast
->_ActiveAttribMask
= attribsMask
;
530 /* Update _ActiveAttribs[] list */
533 for (i
= 0; i
< FRAG_ATTRIB_MAX
; i
++) {
534 if (attribsMask
& (1 << i
)) {
535 swrast
->_ActiveAttribs
[num
++] = i
;
536 /* how should this attribute be interpolated? */
537 if (i
== FRAG_ATTRIB_COL0
|| i
== FRAG_ATTRIB_COL1
)
538 swrast
->_InterpMode
[i
] = ctx
->Light
.ShadeModel
;
540 swrast
->_InterpMode
[i
] = GL_SMOOTH
;
543 swrast
->_NumActiveAttribs
= num
;
549 _swrast_validate_derived( struct gl_context
*ctx
)
551 SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
553 if (swrast
->NewState
) {
554 if (swrast
->NewState
& _NEW_POLYGON
)
555 _swrast_update_polygon( ctx
);
557 if (swrast
->NewState
& (_NEW_HINT
| _NEW_PROGRAM
))
558 _swrast_update_fog_hint( ctx
);
560 if (swrast
->NewState
& _SWRAST_NEW_TEXTURE_ENV_MODE
)
561 _swrast_update_texture_env( ctx
);
563 if (swrast
->NewState
& (_NEW_FOG
| _NEW_PROGRAM
))
564 _swrast_update_fog_state( ctx
);
566 if (swrast
->NewState
& (_NEW_PROGRAM_CONSTANTS
| _NEW_PROGRAM
))
567 _swrast_update_fragment_program( ctx
, swrast
->NewState
);
569 if (swrast
->NewState
& (_NEW_TEXTURE
| _NEW_PROGRAM
)) {
570 _swrast_update_texture_samplers( ctx
);
573 if (swrast
->NewState
& (_NEW_COLOR
| _NEW_PROGRAM
))
574 _swrast_update_deferred_texture(ctx
);
576 if (swrast
->NewState
& _SWRAST_NEW_RASTERMASK
)
577 _swrast_update_rasterflags( ctx
);
579 if (swrast
->NewState
& (_NEW_DEPTH
|
584 _swrast_update_active_attribs(ctx
);
586 if (swrast
->NewState
& (_NEW_FOG
|
590 _swrast_update_specular_vertex_add(ctx
);
592 swrast
->NewState
= 0;
593 swrast
->StateChanges
= 0;
594 swrast
->InvalidateState
= _swrast_invalidate_state
;
598 #define SWRAST_DEBUG 0
600 /* Public entrypoints: See also s_bitmap.c, etc.
603 _swrast_Quad( struct gl_context
*ctx
,
604 const SWvertex
*v0
, const SWvertex
*v1
,
605 const SWvertex
*v2
, const SWvertex
*v3
)
608 _mesa_debug(ctx
, "_swrast_Quad\n");
609 _swrast_print_vertex( ctx
, v0
);
610 _swrast_print_vertex( ctx
, v1
);
611 _swrast_print_vertex( ctx
, v2
);
612 _swrast_print_vertex( ctx
, v3
);
614 SWRAST_CONTEXT(ctx
)->Triangle( ctx
, v0
, v1
, v3
);
615 SWRAST_CONTEXT(ctx
)->Triangle( ctx
, v1
, v2
, v3
);
619 _swrast_Triangle( struct gl_context
*ctx
, const SWvertex
*v0
,
620 const SWvertex
*v1
, const SWvertex
*v2
)
623 _mesa_debug(ctx
, "_swrast_Triangle\n");
624 _swrast_print_vertex( ctx
, v0
);
625 _swrast_print_vertex( ctx
, v1
);
626 _swrast_print_vertex( ctx
, v2
);
628 SWRAST_CONTEXT(ctx
)->Triangle( ctx
, v0
, v1
, v2
);
632 _swrast_Line( struct gl_context
*ctx
, const SWvertex
*v0
, const SWvertex
*v1
)
635 _mesa_debug(ctx
, "_swrast_Line\n");
636 _swrast_print_vertex( ctx
, v0
);
637 _swrast_print_vertex( ctx
, v1
);
639 SWRAST_CONTEXT(ctx
)->Line( ctx
, v0
, v1
);
643 _swrast_Point( struct gl_context
*ctx
, const SWvertex
*v0
)
646 _mesa_debug(ctx
, "_swrast_Point\n");
647 _swrast_print_vertex( ctx
, v0
);
649 SWRAST_CONTEXT(ctx
)->Point( ctx
, v0
);
653 _swrast_InvalidateState( struct gl_context
*ctx
, GLbitfield new_state
)
656 _mesa_debug(ctx
, "_swrast_InvalidateState\n");
658 SWRAST_CONTEXT(ctx
)->InvalidateState( ctx
, new_state
);
662 _swrast_ResetLineStipple( struct gl_context
*ctx
)
665 _mesa_debug(ctx
, "_swrast_ResetLineStipple\n");
667 SWRAST_CONTEXT(ctx
)->StippleCounter
= 0;
671 _swrast_SetFacing(struct gl_context
*ctx
, GLuint facing
)
673 SWRAST_CONTEXT(ctx
)->PointLineFacing
= facing
;
677 _swrast_allow_vertex_fog( struct gl_context
*ctx
, GLboolean value
)
680 _mesa_debug(ctx
, "_swrast_allow_vertex_fog %d\n", value
);
682 SWRAST_CONTEXT(ctx
)->InvalidateState( ctx
, _NEW_HINT
);
683 SWRAST_CONTEXT(ctx
)->AllowVertexFog
= value
;
687 _swrast_allow_pixel_fog( struct gl_context
*ctx
, GLboolean value
)
690 _mesa_debug(ctx
, "_swrast_allow_pixel_fog %d\n", value
);
692 SWRAST_CONTEXT(ctx
)->InvalidateState( ctx
, _NEW_HINT
);
693 SWRAST_CONTEXT(ctx
)->AllowPixelFog
= value
;
698 * Initialize native program limits by copying the logical limits.
699 * See comments in init_program_limits() in context.c
702 init_program_native_limits(struct gl_program_constants
*prog
)
704 prog
->MaxNativeInstructions
= prog
->MaxInstructions
;
705 prog
->MaxNativeAluInstructions
= prog
->MaxAluInstructions
;
706 prog
->MaxNativeTexInstructions
= prog
->MaxTexInstructions
;
707 prog
->MaxNativeTexIndirections
= prog
->MaxTexIndirections
;
708 prog
->MaxNativeAttribs
= prog
->MaxAttribs
;
709 prog
->MaxNativeTemps
= prog
->MaxTemps
;
710 prog
->MaxNativeAddressRegs
= prog
->MaxAddressRegs
;
711 prog
->MaxNativeParameters
= prog
->MaxParameters
;
716 _swrast_CreateContext( struct gl_context
*ctx
)
719 SWcontext
*swrast
= (SWcontext
*)CALLOC(sizeof(SWcontext
));
721 const GLint maxThreads
= omp_get_max_threads();
723 const GLint maxThreads
= 1;
727 _mesa_debug(ctx
, "_swrast_CreateContext\n");
733 swrast
->NewState
= ~0;
735 swrast
->choose_point
= _swrast_choose_point
;
736 swrast
->choose_line
= _swrast_choose_line
;
737 swrast
->choose_triangle
= _swrast_choose_triangle
;
739 swrast
->InvalidatePointMask
= _SWRAST_NEW_POINT
;
740 swrast
->InvalidateLineMask
= _SWRAST_NEW_LINE
;
741 swrast
->InvalidateTriangleMask
= _SWRAST_NEW_TRIANGLE
;
743 swrast
->Point
= _swrast_validate_point
;
744 swrast
->Line
= _swrast_validate_line
;
745 swrast
->Triangle
= _swrast_validate_triangle
;
746 swrast
->InvalidateState
= _swrast_sleep
;
747 swrast
->BlendFunc
= _swrast_validate_blend_func
;
749 swrast
->AllowVertexFog
= GL_TRUE
;
750 swrast
->AllowPixelFog
= GL_TRUE
;
752 swrast
->Driver
.SpanRenderStart
= _swrast_span_render_start
;
753 swrast
->Driver
.SpanRenderFinish
= _swrast_span_render_finish
;
755 ctx
->Driver
.MapTexture
= _swrast_map_texture
;
756 ctx
->Driver
.UnmapTexture
= _swrast_unmap_texture
;
758 for (i
= 0; i
< MAX_TEXTURE_IMAGE_UNITS
; i
++)
759 swrast
->TextureSample
[i
] = NULL
;
761 /* SpanArrays is global and shared by all SWspan instances. However, when
762 * using multiple threads, it is necessary to have one SpanArrays instance
765 swrast
->SpanArrays
= (SWspanarrays
*) MALLOC(maxThreads
* sizeof(SWspanarrays
));
766 if (!swrast
->SpanArrays
) {
770 for(i
= 0; i
< maxThreads
; i
++) {
771 swrast
->SpanArrays
[i
].ChanType
= CHAN_TYPE
;
772 #if CHAN_TYPE == GL_UNSIGNED_BYTE
773 swrast
->SpanArrays
[i
].rgba
= swrast
->SpanArrays
[i
].rgba8
;
774 #elif CHAN_TYPE == GL_UNSIGNED_SHORT
775 swrast
->SpanArrays
[i
].rgba
= swrast
->SpanArrays
[i
].rgba16
;
777 swrast
->SpanArrays
[i
].rgba
= swrast
->SpanArrays
[i
].attribs
[FRAG_ATTRIB_COL0
];
781 /* init point span buffer */
782 swrast
->PointSpan
.primitive
= GL_POINT
;
783 swrast
->PointSpan
.end
= 0;
784 swrast
->PointSpan
.facing
= 0;
785 swrast
->PointSpan
.array
= swrast
->SpanArrays
;
787 init_program_native_limits(&ctx
->Const
.VertexProgram
);
788 init_program_native_limits(&ctx
->Const
.GeometryProgram
);
789 init_program_native_limits(&ctx
->Const
.FragmentProgram
);
791 ctx
->swrast_context
= swrast
;
797 _swrast_DestroyContext( struct gl_context
*ctx
)
799 SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
802 _mesa_debug(ctx
, "_swrast_DestroyContext\n");
805 FREE( swrast
->SpanArrays
);
806 if (swrast
->ZoomedArrays
)
807 FREE( swrast
->ZoomedArrays
);
808 FREE( swrast
->TexelBuffer
);
811 ctx
->swrast_context
= 0;
815 struct swrast_device_driver
*
816 _swrast_GetDeviceDriverReference( struct gl_context
*ctx
)
818 SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
819 return &swrast
->Driver
;
823 _swrast_flush( struct gl_context
*ctx
)
825 SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
826 /* flush any pending fragments from rendering points */
827 if (swrast
->PointSpan
.end
> 0) {
828 _swrast_write_rgba_span(ctx
, &(swrast
->PointSpan
));
829 swrast
->PointSpan
.end
= 0;
834 _swrast_render_primitive( struct gl_context
*ctx
, GLenum prim
)
836 SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
837 if (swrast
->Primitive
== GL_POINTS
&& prim
!= GL_POINTS
) {
840 swrast
->Primitive
= prim
;
844 /** called via swrast->Driver.SpanRenderStart() */
846 _swrast_span_render_start(struct gl_context
*ctx
)
848 _swrast_map_textures(ctx
);
849 _swrast_map_renderbuffers(ctx
);
853 /** called via swrast->Driver.SpanRenderFinish() */
855 _swrast_span_render_finish(struct gl_context
*ctx
)
857 _swrast_unmap_textures(ctx
);
858 _swrast_unmap_renderbuffers(ctx
);
863 _swrast_render_start( struct gl_context
*ctx
)
865 SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
866 if (swrast
->Driver
.SpanRenderStart
)
867 swrast
->Driver
.SpanRenderStart( ctx
);
868 swrast
->PointSpan
.end
= 0;
872 _swrast_render_finish( struct gl_context
*ctx
)
874 SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
875 if (swrast
->Driver
.SpanRenderFinish
)
876 swrast
->Driver
.SpanRenderFinish( ctx
);
882 #define SWRAST_DEBUG_VERTICES 0
885 _swrast_print_vertex( struct gl_context
*ctx
, const SWvertex
*v
)
889 if (SWRAST_DEBUG_VERTICES
) {
890 _mesa_debug(ctx
, "win %f %f %f %f\n",
891 v
->attrib
[FRAG_ATTRIB_WPOS
][0],
892 v
->attrib
[FRAG_ATTRIB_WPOS
][1],
893 v
->attrib
[FRAG_ATTRIB_WPOS
][2],
894 v
->attrib
[FRAG_ATTRIB_WPOS
][3]);
896 for (i
= 0 ; i
< ctx
->Const
.MaxTextureCoordUnits
; i
++)
897 if (ctx
->Texture
.Unit
[i
]._ReallyEnabled
)
898 _mesa_debug(ctx
, "texcoord[%d] %f %f %f %f\n", i
,
899 v
->attrib
[FRAG_ATTRIB_TEX0
+ i
][0],
900 v
->attrib
[FRAG_ATTRIB_TEX0
+ i
][1],
901 v
->attrib
[FRAG_ATTRIB_TEX0
+ i
][2],
902 v
->attrib
[FRAG_ATTRIB_TEX0
+ i
][3]);
904 #if CHAN_TYPE == GL_FLOAT
905 _mesa_debug(ctx
, "color %f %f %f %f\n",
906 v
->color
[0], v
->color
[1], v
->color
[2], v
->color
[3]);
908 _mesa_debug(ctx
, "color %d %d %d %d\n",
909 v
->color
[0], v
->color
[1], v
->color
[2], v
->color
[3]);
911 _mesa_debug(ctx
, "spec %g %g %g %g\n",
912 v
->attrib
[FRAG_ATTRIB_COL1
][0],
913 v
->attrib
[FRAG_ATTRIB_COL1
][1],
914 v
->attrib
[FRAG_ATTRIB_COL1
][2],
915 v
->attrib
[FRAG_ATTRIB_COL1
][3]);
916 _mesa_debug(ctx
, "fog %f\n", v
->attrib
[FRAG_ATTRIB_FOGC
][0]);
917 _mesa_debug(ctx
, "index %f\n", v
->attrib
[FRAG_ATTRIB_CI
][0]);
918 _mesa_debug(ctx
, "pointsize %f\n", v
->pointSize
);
919 _mesa_debug(ctx
, "\n");