Merge branch 'master' of git+ssh://znh@git.freedesktop.org/git/mesa/mesa into 965...
[mesa.git] / src / mesa / swrast / s_context.c
1 /*
2 * Mesa 3-D graphics library
3 * Version: 7.1
4 *
5 * Copyright (C) 1999-2007 Brian Paul All Rights Reserved.
6 *
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:
13 *
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
16 *
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.
23 *
24 * Authors:
25 * Keith Whitwell <keith@tungstengraphics.com>
26 * Brian Paul
27 */
28
29 #include "imports.h"
30 #include "bufferobj.h"
31 #include "context.h"
32 #include "colormac.h"
33 #include "mtypes.h"
34 #include "teximage.h"
35 #include "swrast.h"
36 #include "shader/prog_statevars.h"
37 #include "s_blend.h"
38 #include "s_context.h"
39 #include "s_lines.h"
40 #include "s_points.h"
41 #include "s_span.h"
42 #include "s_triangle.h"
43 #include "s_texfilter.h"
44
45
46 /**
47 * Recompute the value of swrast->_RasterMask, etc. according to
48 * the current context. The _RasterMask field can be easily tested by
49 * drivers to determine certain basic GL state (does the primitive need
50 * stenciling, logic-op, fog, etc?).
51 */
52 static void
53 _swrast_update_rasterflags( GLcontext *ctx )
54 {
55 SWcontext *swrast = SWRAST_CONTEXT(ctx);
56 GLbitfield rasterMask = 0;
57
58 if (ctx->Color.AlphaEnabled) rasterMask |= ALPHATEST_BIT;
59 if (ctx->Color.BlendEnabled) rasterMask |= BLEND_BIT;
60 if (ctx->Depth.Test) rasterMask |= DEPTH_BIT;
61 if (swrast->_FogEnabled) rasterMask |= FOG_BIT;
62 if (ctx->Scissor.Enabled) rasterMask |= CLIP_BIT;
63 if (ctx->Stencil.Enabled) rasterMask |= STENCIL_BIT;
64 if (ctx->Visual.rgbMode) {
65 const GLuint colorMask = *((GLuint *) &ctx->Color.ColorMask);
66 if (colorMask != 0xffffffff) rasterMask |= MASKING_BIT;
67 if (ctx->Color._LogicOpEnabled) rasterMask |= LOGIC_OP_BIT;
68 if (ctx->Texture._EnabledUnits) rasterMask |= TEXTURE_BIT;
69 }
70 else {
71 if (ctx->Color.IndexMask != 0xffffffff) rasterMask |= MASKING_BIT;
72 if (ctx->Color.IndexLogicOpEnabled) rasterMask |= LOGIC_OP_BIT;
73 }
74
75 if ( ctx->Viewport.X < 0
76 || ctx->Viewport.X + ctx->Viewport.Width > (GLint) ctx->DrawBuffer->Width
77 || ctx->Viewport.Y < 0
78 || ctx->Viewport.Y + ctx->Viewport.Height > (GLint) ctx->DrawBuffer->Height) {
79 rasterMask |= CLIP_BIT;
80 }
81
82 if (ctx->Query.CurrentOcclusionObject)
83 rasterMask |= OCCLUSION_BIT;
84
85
86 /* If we're not drawing to exactly one color buffer set the
87 * MULTI_DRAW_BIT flag. Also set it if we're drawing to no
88 * buffers or the RGBA or CI mask disables all writes.
89 */
90 if (ctx->DrawBuffer->_NumColorDrawBuffers[0] != 1) {
91 /* more than one color buffer designated for writing (or zero buffers) */
92 rasterMask |= MULTI_DRAW_BIT;
93 }
94 else if (ctx->Visual.rgbMode && *((GLuint *) ctx->Color.ColorMask) == 0) {
95 rasterMask |= MULTI_DRAW_BIT; /* all RGBA channels disabled */
96 }
97 else if (!ctx->Visual.rgbMode && ctx->Color.IndexMask==0) {
98 rasterMask |= MULTI_DRAW_BIT; /* all color index bits disabled */
99 }
100
101 if (ctx->FragmentProgram._Current) {
102 rasterMask |= FRAGPROG_BIT;
103 }
104
105 if (ctx->ATIFragmentShader._Enabled) {
106 rasterMask |= ATIFRAGSHADER_BIT;
107 }
108
109 #if CHAN_TYPE == GL_FLOAT
110 if (ctx->Color.ClampFragmentColor == GL_TRUE) {
111 rasterMask |= CLAMPING_BIT;
112 }
113 #endif
114
115 SWRAST_CONTEXT(ctx)->_RasterMask = rasterMask;
116 }
117
118
119 /**
120 * Examine polycon culls tate to compute the _BackfaceSign field.
121 * _BackfaceSign will be 0 if no culling, -1 if culling back-faces,
122 * and 1 if culling front-faces. The Polygon FrontFace state also
123 * factors in.
124 */
125 static void
126 _swrast_update_polygon( GLcontext *ctx )
127 {
128 GLfloat backface_sign;
129
130 if (ctx->Polygon.CullFlag) {
131 backface_sign = 1.0;
132 switch (ctx->Polygon.CullFaceMode) {
133 case GL_BACK:
134 if (ctx->Polygon.FrontFace == GL_CCW)
135 backface_sign = -1.0;
136 break;
137 case GL_FRONT:
138 if (ctx->Polygon.FrontFace != GL_CCW)
139 backface_sign = -1.0;
140 break;
141 case GL_FRONT_AND_BACK:
142 /* fallthrough */
143 default:
144 backface_sign = 0.0;
145 break;
146 }
147 }
148 else {
149 backface_sign = 0.0;
150 }
151
152 SWRAST_CONTEXT(ctx)->_BackfaceSign = backface_sign;
153 }
154
155
156 /**
157 * Update the _PreferPixelFog field to indicate if we need to compute
158 * fog blend factors (from the fog coords) per-fragment.
159 */
160 static void
161 _swrast_update_fog_hint( GLcontext *ctx )
162 {
163 SWcontext *swrast = SWRAST_CONTEXT(ctx);
164 swrast->_PreferPixelFog = (!swrast->AllowVertexFog ||
165 ctx->FragmentProgram._Current ||
166 (ctx->Hint.Fog == GL_NICEST &&
167 swrast->AllowPixelFog));
168 }
169
170
171
172 /**
173 * Update the swrast->_AnyTextureCombine flag.
174 */
175 static void
176 _swrast_update_texture_env( GLcontext *ctx )
177 {
178 SWcontext *swrast = SWRAST_CONTEXT(ctx);
179 GLuint i;
180 swrast->_AnyTextureCombine = GL_FALSE;
181 for (i = 0; i < ctx->Const.MaxTextureUnits; i++) {
182 if (ctx->Texture.Unit[i].EnvMode == GL_COMBINE_EXT ||
183 ctx->Texture.Unit[i].EnvMode == GL_COMBINE4_NV) {
184 swrast->_AnyTextureCombine = GL_TRUE;
185 return;
186 }
187 }
188 }
189
190
191 /**
192 * Determine if we can defer texturing/shading until after Z/stencil
193 * testing. This potentially allows us to skip texturing/shading for
194 * lots of fragments.
195 */
196 static void
197 _swrast_update_deferred_texture(GLcontext *ctx)
198 {
199 SWcontext *swrast = SWRAST_CONTEXT(ctx);
200 if (ctx->Color.AlphaEnabled) {
201 /* alpha test depends on post-texture/shader colors */
202 swrast->_DeferredTexture = GL_FALSE;
203 }
204 else {
205 const struct gl_fragment_program *fprog
206 = ctx->FragmentProgram._Current;
207 if (fprog && (fprog->Base.OutputsWritten & (1 << FRAG_RESULT_DEPR))) {
208 /* Z comes from fragment program/shader */
209 swrast->_DeferredTexture = GL_FALSE;
210 }
211 else if (ctx->Query.CurrentOcclusionObject) {
212 /* occlusion query depends on shader discard/kill results */
213 swrast->_DeferredTexture = GL_FALSE;
214 }
215 else {
216 swrast->_DeferredTexture = GL_TRUE;
217 }
218 }
219 }
220
221
222 /**
223 * Update swrast->_FogColor and swrast->_FogEnable values.
224 */
225 static void
226 _swrast_update_fog_state( GLcontext *ctx )
227 {
228 SWcontext *swrast = SWRAST_CONTEXT(ctx);
229 const struct gl_fragment_program *fp = ctx->FragmentProgram._Current;
230
231 /* determine if fog is needed, and if so, which fog mode */
232 swrast->_FogEnabled = GL_FALSE;
233 if (fp && fp->Base.Target == GL_FRAGMENT_PROGRAM_ARB) {
234 if (fp->FogOption != GL_NONE) {
235 swrast->_FogEnabled = GL_TRUE;
236 swrast->_FogMode = fp->FogOption;
237 }
238 }
239 else if (ctx->Fog.Enabled) {
240 swrast->_FogEnabled = GL_TRUE;
241 swrast->_FogMode = ctx->Fog.Mode;
242 }
243 }
244
245
246 /**
247 * Update state for running fragment programs. Basically, load the
248 * program parameters with current state values.
249 */
250 static void
251 _swrast_update_fragment_program(GLcontext *ctx, GLbitfield newState)
252 {
253 const struct gl_fragment_program *fp = ctx->FragmentProgram._Current;
254 if (fp) {
255 #if 0
256 /* XXX Need a way to trigger the initial loading of parameters
257 * even when there's no recent state changes.
258 */
259 if (fp->Base.Parameters->StateFlags & newState)
260 #endif
261 _mesa_load_state_parameters(ctx, fp->Base.Parameters);
262 }
263 }
264
265
266
267 #define _SWRAST_NEW_DERIVED (_SWRAST_NEW_RASTERMASK | \
268 _NEW_TEXTURE | \
269 _NEW_HINT | \
270 _NEW_POLYGON )
271
272 /* State referenced by _swrast_choose_triangle, _swrast_choose_line.
273 */
274 #define _SWRAST_NEW_TRIANGLE (_SWRAST_NEW_DERIVED | \
275 _NEW_RENDERMODE| \
276 _NEW_POLYGON| \
277 _NEW_DEPTH| \
278 _NEW_STENCIL| \
279 _NEW_COLOR| \
280 _NEW_TEXTURE| \
281 _SWRAST_NEW_RASTERMASK| \
282 _NEW_LIGHT| \
283 _NEW_FOG | \
284 _DD_NEW_SEPARATE_SPECULAR)
285
286 #define _SWRAST_NEW_LINE (_SWRAST_NEW_DERIVED | \
287 _NEW_RENDERMODE| \
288 _NEW_LINE| \
289 _NEW_TEXTURE| \
290 _NEW_LIGHT| \
291 _NEW_FOG| \
292 _NEW_DEPTH | \
293 _DD_NEW_SEPARATE_SPECULAR)
294
295 #define _SWRAST_NEW_POINT (_SWRAST_NEW_DERIVED | \
296 _NEW_RENDERMODE | \
297 _NEW_POINT | \
298 _NEW_TEXTURE | \
299 _NEW_LIGHT | \
300 _NEW_FOG | \
301 _DD_NEW_SEPARATE_SPECULAR)
302
303 #define _SWRAST_NEW_TEXTURE_SAMPLE_FUNC _NEW_TEXTURE
304
305 #define _SWRAST_NEW_TEXTURE_ENV_MODE _NEW_TEXTURE
306
307 #define _SWRAST_NEW_BLEND_FUNC _NEW_COLOR
308
309
310
311 /**
312 * Stub for swrast->Triangle to select a true triangle function
313 * after a state change.
314 */
315 static void
316 _swrast_validate_triangle( GLcontext *ctx,
317 const SWvertex *v0,
318 const SWvertex *v1,
319 const SWvertex *v2 )
320 {
321 SWcontext *swrast = SWRAST_CONTEXT(ctx);
322
323 _swrast_validate_derived( ctx );
324 swrast->choose_triangle( ctx );
325 ASSERT(swrast->Triangle);
326
327 if (ctx->Texture._EnabledUnits == 0
328 && NEED_SECONDARY_COLOR(ctx)
329 && !ctx->FragmentProgram._Current) {
330 /* separate specular color, but no texture */
331 swrast->SpecTriangle = swrast->Triangle;
332 swrast->Triangle = _swrast_add_spec_terms_triangle;
333 }
334
335 swrast->Triangle( ctx, v0, v1, v2 );
336 }
337
338 /**
339 * Called via swrast->Line. Examine current GL state and choose a software
340 * line routine. Then call it.
341 */
342 static void
343 _swrast_validate_line( GLcontext *ctx, const SWvertex *v0, const SWvertex *v1 )
344 {
345 SWcontext *swrast = SWRAST_CONTEXT(ctx);
346
347 _swrast_validate_derived( ctx );
348 swrast->choose_line( ctx );
349 ASSERT(swrast->Line);
350
351 if (ctx->Texture._EnabledUnits == 0
352 && NEED_SECONDARY_COLOR(ctx)
353 && !ctx->FragmentProgram._Current) {
354 swrast->SpecLine = swrast->Line;
355 swrast->Line = _swrast_add_spec_terms_line;
356 }
357
358 swrast->Line( ctx, v0, v1 );
359 }
360
361 /**
362 * Called via swrast->Point. Examine current GL state and choose a software
363 * point routine. Then call it.
364 */
365 static void
366 _swrast_validate_point( GLcontext *ctx, const SWvertex *v0 )
367 {
368 SWcontext *swrast = SWRAST_CONTEXT(ctx);
369
370 _swrast_validate_derived( ctx );
371 swrast->choose_point( ctx );
372
373 if (ctx->Texture._EnabledUnits == 0
374 && NEED_SECONDARY_COLOR(ctx)
375 && !ctx->FragmentProgram._Current) {
376 swrast->SpecPoint = swrast->Point;
377 swrast->Point = _swrast_add_spec_terms_point;
378 }
379
380 swrast->Point( ctx, v0 );
381 }
382
383
384 /**
385 * Called via swrast->BlendFunc. Examine GL state to choose a blending
386 * function, then call it.
387 */
388 static void _ASMAPI
389 _swrast_validate_blend_func(GLcontext *ctx, GLuint n, const GLubyte mask[],
390 GLvoid *src, const GLvoid *dst,
391 GLenum chanType )
392 {
393 SWcontext *swrast = SWRAST_CONTEXT(ctx);
394
395 _swrast_validate_derived( ctx ); /* why is this needed? */
396 _swrast_choose_blend_func( ctx, chanType );
397
398 swrast->BlendFunc( ctx, n, mask, src, dst, chanType );
399 }
400
401
402 /**
403 * Make sure we have texture image data for all the textures we may need
404 * for subsequent rendering.
405 */
406 static void
407 _swrast_validate_texture_images(GLcontext *ctx)
408 {
409 SWcontext *swrast = SWRAST_CONTEXT(ctx);
410 GLuint u;
411
412 if (!swrast->ValidateTextureImage || !ctx->Texture._EnabledUnits) {
413 /* no textures enabled, or no way to validate images! */
414 return;
415 }
416
417 for (u = 0; u < ctx->Const.MaxTextureImageUnits; u++) {
418 if (ctx->Texture.Unit[u]._ReallyEnabled) {
419 struct gl_texture_object *texObj = ctx->Texture.Unit[u]._Current;
420 ASSERT(texObj);
421 if (texObj) {
422 GLuint numFaces = (texObj->Target == GL_TEXTURE_CUBE_MAP) ? 6 : 1;
423 GLuint face;
424 for (face = 0; face < numFaces; face++) {
425 GLint lvl;
426 for (lvl = texObj->BaseLevel; lvl <= texObj->_MaxLevel; lvl++) {
427 struct gl_texture_image *texImg = texObj->Image[face][lvl];
428 if (texImg && !texImg->Data) {
429 swrast->ValidateTextureImage(ctx, texObj, face, lvl);
430 ASSERT(texObj->Image[face][lvl]->Data);
431 }
432 }
433 }
434 }
435 }
436 }
437 }
438
439
440 /**
441 * Free the texture image data attached to all currently enabled
442 * textures. Meant to be called by device drivers when transitioning
443 * from software to hardware rendering.
444 */
445 void
446 _swrast_eject_texture_images(GLcontext *ctx)
447 {
448 GLuint u;
449
450 if (!ctx->Texture._EnabledUnits) {
451 /* no textures enabled */
452 return;
453 }
454
455 for (u = 0; u < ctx->Const.MaxTextureImageUnits; u++) {
456 if (ctx->Texture.Unit[u]._ReallyEnabled) {
457 struct gl_texture_object *texObj = ctx->Texture.Unit[u]._Current;
458 ASSERT(texObj);
459 if (texObj) {
460 GLuint numFaces = (texObj->Target == GL_TEXTURE_CUBE_MAP) ? 6 : 1;
461 GLuint face;
462 for (face = 0; face < numFaces; face++) {
463 GLint lvl;
464 for (lvl = texObj->BaseLevel; lvl <= texObj->_MaxLevel; lvl++) {
465 struct gl_texture_image *texImg = texObj->Image[face][lvl];
466 if (texImg && texImg->Data) {
467 _mesa_free_texmemory(texImg->Data);
468 texImg->Data = NULL;
469 }
470 }
471 }
472 }
473 }
474 }
475 }
476
477
478
479 static void
480 _swrast_sleep( GLcontext *ctx, GLbitfield new_state )
481 {
482 (void) ctx; (void) new_state;
483 }
484
485
486 static void
487 _swrast_invalidate_state( GLcontext *ctx, GLbitfield new_state )
488 {
489 SWcontext *swrast = SWRAST_CONTEXT(ctx);
490 GLuint i;
491
492 swrast->NewState |= new_state;
493
494 /* After 10 statechanges without any swrast functions being called,
495 * put the module to sleep.
496 */
497 if (++swrast->StateChanges > 10) {
498 swrast->InvalidateState = _swrast_sleep;
499 swrast->NewState = ~0;
500 new_state = ~0;
501 }
502
503 if (new_state & swrast->InvalidateTriangleMask)
504 swrast->Triangle = _swrast_validate_triangle;
505
506 if (new_state & swrast->InvalidateLineMask)
507 swrast->Line = _swrast_validate_line;
508
509 if (new_state & swrast->InvalidatePointMask)
510 swrast->Point = _swrast_validate_point;
511
512 if (new_state & _SWRAST_NEW_BLEND_FUNC)
513 swrast->BlendFunc = _swrast_validate_blend_func;
514
515 if (new_state & _SWRAST_NEW_TEXTURE_SAMPLE_FUNC)
516 for (i = 0 ; i < ctx->Const.MaxTextureImageUnits ; i++)
517 swrast->TextureSample[i] = NULL;
518 }
519
520
521 void
522 _swrast_update_texture_samplers(GLcontext *ctx)
523 {
524 SWcontext *swrast = SWRAST_CONTEXT(ctx);
525 GLuint u;
526
527 for (u = 0; u < ctx->Const.MaxTextureImageUnits; u++) {
528 const struct gl_texture_object *tObj = ctx->Texture.Unit[u]._Current;
529 /* Note: If tObj is NULL, the sample function will be a simple
530 * function that just returns opaque black (0,0,0,1).
531 */
532 swrast->TextureSample[u] = _swrast_choose_texture_sample_func(ctx, tObj);
533 }
534 }
535
536
537 /**
538 * Update swrast->_ActiveAttribs, swrast->_NumActiveAttribs,
539 * swrast->_ActiveAtttribMask.
540 */
541 static void
542 _swrast_update_active_attribs(GLcontext *ctx)
543 {
544 SWcontext *swrast = SWRAST_CONTEXT(ctx);
545 GLuint attribsMask;
546
547 /*
548 * Compute _ActiveAttribsMask = which fragment attributes are needed.
549 */
550 if (ctx->FragmentProgram._Current) {
551 /* fragment program/shader */
552 attribsMask = ctx->FragmentProgram._Current->Base.InputsRead;
553 attribsMask &= ~FRAG_BIT_WPOS; /* WPOS is always handled specially */
554 }
555 else if (ctx->ATIFragmentShader._Enabled) {
556 attribsMask = ~0; /* XXX fix me */
557 }
558 else {
559 /* fixed function */
560 attribsMask = 0x0;
561
562 #if CHAN_TYPE == GL_FLOAT
563 attribsMask |= FRAG_BIT_COL0;
564 #endif
565
566 if (ctx->Fog.ColorSumEnabled ||
567 (ctx->Light.Enabled &&
568 ctx->Light.Model.ColorControl == GL_SEPARATE_SPECULAR_COLOR)) {
569 attribsMask |= FRAG_BIT_COL1;
570 }
571
572 if (swrast->_FogEnabled)
573 attribsMask |= FRAG_BIT_FOGC;
574
575 attribsMask |= (ctx->Texture._EnabledUnits << FRAG_ATTRIB_TEX0);
576 }
577
578 swrast->_ActiveAttribMask = attribsMask;
579
580 /* Update _ActiveAttribs[] list */
581 {
582 GLuint i, num = 0;
583 for (i = 0; i < FRAG_ATTRIB_MAX; i++) {
584 if (attribsMask & (1 << i)) {
585 swrast->_ActiveAttribs[num++] = i;
586 /* how should this attribute be interpolated? */
587 if (i == FRAG_ATTRIB_COL0 || i == FRAG_ATTRIB_COL1)
588 swrast->_InterpMode[i] = ctx->Light.ShadeModel;
589 else
590 swrast->_InterpMode[i] = GL_SMOOTH;
591 }
592 }
593 swrast->_NumActiveAttribs = num;
594 }
595 }
596
597
598 /**
599 * Update the swrast->_ColorOutputsMask which indicates which color
600 * renderbuffers (aka rendertargets) are being written to by the current
601 * fragment program.
602 * We also take glDrawBuffers() into account to skip outputs that are
603 * set to GL_NONE.
604 */
605 static void
606 _swrast_update_color_outputs(GLcontext *ctx)
607 {
608 SWcontext *swrast = SWRAST_CONTEXT(ctx);
609 const struct gl_framebuffer *fb = ctx->DrawBuffer;
610
611 swrast->_ColorOutputsMask = 0;
612 swrast->_NumColorOutputs = 0;
613
614 if (ctx->FragmentProgram._Current) {
615 const GLbitfield outputsWritten
616 = ctx->FragmentProgram._Current->Base.OutputsWritten;
617 GLuint output;
618 for (output = 0; output < ctx->Const.MaxDrawBuffers; output++) {
619 if ((outputsWritten & (1 << (FRAG_RESULT_DATA0 + output)))
620 && (fb->_NumColorDrawBuffers[output] > 0)) {
621 swrast->_ColorOutputsMask |= (1 << output);
622 swrast->_NumColorOutputs = output + 1;
623 }
624 }
625 }
626 if (swrast->_ColorOutputsMask == 0x0) {
627 /* no fragment program, or frag prog didn't write to gl_FragData[] */
628 if (fb->_NumColorDrawBuffers[0] > 0) {
629 swrast->_ColorOutputsMask = 0x1;
630 swrast->_NumColorOutputs = 1;
631 }
632 }
633 }
634
635
636 void
637 _swrast_validate_derived( GLcontext *ctx )
638 {
639 SWcontext *swrast = SWRAST_CONTEXT(ctx);
640
641 if (swrast->NewState) {
642 if (swrast->NewState & _NEW_POLYGON)
643 _swrast_update_polygon( ctx );
644
645 if (swrast->NewState & (_NEW_HINT | _NEW_PROGRAM))
646 _swrast_update_fog_hint( ctx );
647
648 if (swrast->NewState & _SWRAST_NEW_TEXTURE_ENV_MODE)
649 _swrast_update_texture_env( ctx );
650
651 if (swrast->NewState & (_NEW_FOG | _NEW_PROGRAM))
652 _swrast_update_fog_state( ctx );
653
654 if (swrast->NewState & (_NEW_MODELVIEW |
655 _NEW_PROJECTION |
656 _NEW_TEXTURE_MATRIX |
657 _NEW_FOG |
658 _NEW_LIGHT |
659 _NEW_LINE |
660 _NEW_TEXTURE |
661 _NEW_TRANSFORM |
662 _NEW_POINT |
663 _NEW_VIEWPORT |
664 _NEW_PROGRAM))
665 _swrast_update_fragment_program( ctx, swrast->NewState );
666
667 if (swrast->NewState & (_NEW_TEXTURE | _NEW_PROGRAM)) {
668 _swrast_update_texture_samplers( ctx );
669 _swrast_validate_texture_images(ctx);
670 }
671
672 if (swrast->NewState & (_NEW_COLOR | _NEW_PROGRAM))
673 _swrast_update_deferred_texture(ctx);
674
675 if (swrast->NewState & _SWRAST_NEW_RASTERMASK)
676 _swrast_update_rasterflags( ctx );
677
678 if (swrast->NewState & (_NEW_DEPTH |
679 _NEW_FOG |
680 _NEW_LIGHT |
681 _NEW_PROGRAM |
682 _NEW_TEXTURE))
683 _swrast_update_active_attribs(ctx);
684
685 if (swrast->NewState & (_NEW_PROGRAM | _NEW_BUFFERS))
686 _swrast_update_color_outputs(ctx);
687
688 swrast->NewState = 0;
689 swrast->StateChanges = 0;
690 swrast->InvalidateState = _swrast_invalidate_state;
691 }
692 }
693
694 #define SWRAST_DEBUG 0
695
696 /* Public entrypoints: See also s_accum.c, s_bitmap.c, etc.
697 */
698 void
699 _swrast_Quad( GLcontext *ctx,
700 const SWvertex *v0, const SWvertex *v1,
701 const SWvertex *v2, const SWvertex *v3 )
702 {
703 if (SWRAST_DEBUG) {
704 _mesa_debug(ctx, "_swrast_Quad\n");
705 _swrast_print_vertex( ctx, v0 );
706 _swrast_print_vertex( ctx, v1 );
707 _swrast_print_vertex( ctx, v2 );
708 _swrast_print_vertex( ctx, v3 );
709 }
710 SWRAST_CONTEXT(ctx)->Triangle( ctx, v0, v1, v3 );
711 SWRAST_CONTEXT(ctx)->Triangle( ctx, v1, v2, v3 );
712 }
713
714 void
715 _swrast_Triangle( GLcontext *ctx, const SWvertex *v0,
716 const SWvertex *v1, const SWvertex *v2 )
717 {
718 if (SWRAST_DEBUG) {
719 _mesa_debug(ctx, "_swrast_Triangle\n");
720 _swrast_print_vertex( ctx, v0 );
721 _swrast_print_vertex( ctx, v1 );
722 _swrast_print_vertex( ctx, v2 );
723 }
724 SWRAST_CONTEXT(ctx)->Triangle( ctx, v0, v1, v2 );
725 }
726
727 void
728 _swrast_Line( GLcontext *ctx, const SWvertex *v0, const SWvertex *v1 )
729 {
730 if (SWRAST_DEBUG) {
731 _mesa_debug(ctx, "_swrast_Line\n");
732 _swrast_print_vertex( ctx, v0 );
733 _swrast_print_vertex( ctx, v1 );
734 }
735 SWRAST_CONTEXT(ctx)->Line( ctx, v0, v1 );
736 }
737
738 void
739 _swrast_Point( GLcontext *ctx, const SWvertex *v0 )
740 {
741 if (SWRAST_DEBUG) {
742 _mesa_debug(ctx, "_swrast_Point\n");
743 _swrast_print_vertex( ctx, v0 );
744 }
745 SWRAST_CONTEXT(ctx)->Point( ctx, v0 );
746 }
747
748 void
749 _swrast_InvalidateState( GLcontext *ctx, GLbitfield new_state )
750 {
751 if (SWRAST_DEBUG) {
752 _mesa_debug(ctx, "_swrast_InvalidateState\n");
753 }
754 SWRAST_CONTEXT(ctx)->InvalidateState( ctx, new_state );
755 }
756
757 void
758 _swrast_ResetLineStipple( GLcontext *ctx )
759 {
760 if (SWRAST_DEBUG) {
761 _mesa_debug(ctx, "_swrast_ResetLineStipple\n");
762 }
763 SWRAST_CONTEXT(ctx)->StippleCounter = 0;
764 }
765
766 void
767 _swrast_allow_vertex_fog( GLcontext *ctx, GLboolean value )
768 {
769 if (SWRAST_DEBUG) {
770 _mesa_debug(ctx, "_swrast_allow_vertex_fog %d\n", value);
771 }
772 SWRAST_CONTEXT(ctx)->InvalidateState( ctx, _NEW_HINT );
773 SWRAST_CONTEXT(ctx)->AllowVertexFog = value;
774 }
775
776 void
777 _swrast_allow_pixel_fog( GLcontext *ctx, GLboolean value )
778 {
779 if (SWRAST_DEBUG) {
780 _mesa_debug(ctx, "_swrast_allow_pixel_fog %d\n", value);
781 }
782 SWRAST_CONTEXT(ctx)->InvalidateState( ctx, _NEW_HINT );
783 SWRAST_CONTEXT(ctx)->AllowPixelFog = value;
784 }
785
786
787 GLboolean
788 _swrast_CreateContext( GLcontext *ctx )
789 {
790 GLuint i;
791 SWcontext *swrast = (SWcontext *)CALLOC(sizeof(SWcontext));
792
793 if (SWRAST_DEBUG) {
794 _mesa_debug(ctx, "_swrast_CreateContext\n");
795 }
796
797 if (!swrast)
798 return GL_FALSE;
799
800 swrast->NewState = ~0;
801
802 swrast->choose_point = _swrast_choose_point;
803 swrast->choose_line = _swrast_choose_line;
804 swrast->choose_triangle = _swrast_choose_triangle;
805
806 swrast->InvalidatePointMask = _SWRAST_NEW_POINT;
807 swrast->InvalidateLineMask = _SWRAST_NEW_LINE;
808 swrast->InvalidateTriangleMask = _SWRAST_NEW_TRIANGLE;
809
810 swrast->Point = _swrast_validate_point;
811 swrast->Line = _swrast_validate_line;
812 swrast->Triangle = _swrast_validate_triangle;
813 swrast->InvalidateState = _swrast_sleep;
814 swrast->BlendFunc = _swrast_validate_blend_func;
815
816 swrast->AllowVertexFog = GL_TRUE;
817 swrast->AllowPixelFog = GL_TRUE;
818
819 /* Optimized Accum buffer */
820 swrast->_IntegerAccumMode = GL_FALSE;
821 swrast->_IntegerAccumScaler = 0.0;
822
823 for (i = 0; i < MAX_TEXTURE_IMAGE_UNITS; i++)
824 swrast->TextureSample[i] = NULL;
825
826 swrast->SpanArrays = MALLOC_STRUCT(sw_span_arrays);
827 if (!swrast->SpanArrays) {
828 FREE(swrast);
829 return GL_FALSE;
830 }
831 swrast->SpanArrays->ChanType = CHAN_TYPE;
832 #if CHAN_TYPE == GL_UNSIGNED_BYTE
833 swrast->SpanArrays->rgba = swrast->SpanArrays->rgba8;
834 #elif CHAN_TYPE == GL_UNSIGNED_SHORT
835 swrast->SpanArrays->rgba = swrast->SpanArrays->rgba16;
836 #else
837 swrast->SpanArrays->rgba = swrast->SpanArrays->attribs[FRAG_ATTRIB_COL0];
838 #endif
839
840 /* init point span buffer */
841 swrast->PointSpan.primitive = GL_POINT;
842 swrast->PointSpan.end = 0;
843 swrast->PointSpan.facing = 0;
844 swrast->PointSpan.array = swrast->SpanArrays;
845
846 swrast->TexelBuffer = (GLchan *) MALLOC(ctx->Const.MaxTextureImageUnits *
847 MAX_WIDTH * 4 * sizeof(GLchan));
848 if (!swrast->TexelBuffer) {
849 FREE(swrast->SpanArrays);
850 FREE(swrast);
851 return GL_FALSE;
852 }
853
854 ctx->swrast_context = swrast;
855
856 return GL_TRUE;
857 }
858
859 void
860 _swrast_DestroyContext( GLcontext *ctx )
861 {
862 SWcontext *swrast = SWRAST_CONTEXT(ctx);
863
864 if (SWRAST_DEBUG) {
865 _mesa_debug(ctx, "_swrast_DestroyContext\n");
866 }
867
868 FREE( swrast->SpanArrays );
869 FREE( swrast->TexelBuffer );
870 FREE( swrast );
871
872 ctx->swrast_context = 0;
873 }
874
875
876 struct swrast_device_driver *
877 _swrast_GetDeviceDriverReference( GLcontext *ctx )
878 {
879 SWcontext *swrast = SWRAST_CONTEXT(ctx);
880 return &swrast->Driver;
881 }
882
883 void
884 _swrast_flush( GLcontext *ctx )
885 {
886 SWcontext *swrast = SWRAST_CONTEXT(ctx);
887 /* flush any pending fragments from rendering points */
888 if (swrast->PointSpan.end > 0) {
889 if (ctx->Visual.rgbMode) {
890 _swrast_write_rgba_span(ctx, &(swrast->PointSpan));
891 }
892 else {
893 _swrast_write_index_span(ctx, &(swrast->PointSpan));
894 }
895 swrast->PointSpan.end = 0;
896 }
897 }
898
899 void
900 _swrast_render_primitive( GLcontext *ctx, GLenum prim )
901 {
902 SWcontext *swrast = SWRAST_CONTEXT(ctx);
903 if (swrast->Primitive == GL_POINTS && prim != GL_POINTS) {
904 _swrast_flush(ctx);
905 }
906 swrast->Primitive = prim;
907 }
908
909
910 void
911 _swrast_render_start( GLcontext *ctx )
912 {
913 SWcontext *swrast = SWRAST_CONTEXT(ctx);
914 if (swrast->Driver.SpanRenderStart)
915 swrast->Driver.SpanRenderStart( ctx );
916 swrast->PointSpan.end = 0;
917 }
918
919 void
920 _swrast_render_finish( GLcontext *ctx )
921 {
922 SWcontext *swrast = SWRAST_CONTEXT(ctx);
923 if (swrast->Driver.SpanRenderFinish)
924 swrast->Driver.SpanRenderFinish( ctx );
925
926 _swrast_flush(ctx);
927 }
928
929
930 #define SWRAST_DEBUG_VERTICES 0
931
932 void
933 _swrast_print_vertex( GLcontext *ctx, const SWvertex *v )
934 {
935 GLuint i;
936
937 if (SWRAST_DEBUG_VERTICES) {
938 _mesa_debug(ctx, "win %f %f %f %f\n",
939 v->attrib[FRAG_ATTRIB_WPOS][0],
940 v->attrib[FRAG_ATTRIB_WPOS][1],
941 v->attrib[FRAG_ATTRIB_WPOS][2],
942 v->attrib[FRAG_ATTRIB_WPOS][3]);
943
944 for (i = 0 ; i < ctx->Const.MaxTextureCoordUnits ; i++)
945 if (ctx->Texture.Unit[i]._ReallyEnabled)
946 _mesa_debug(ctx, "texcoord[%d] %f %f %f %f\n", i,
947 v->attrib[FRAG_ATTRIB_TEX0 + i][0],
948 v->attrib[FRAG_ATTRIB_TEX0 + i][1],
949 v->attrib[FRAG_ATTRIB_TEX0 + i][2],
950 v->attrib[FRAG_ATTRIB_TEX0 + i][3]);
951
952 #if CHAN_TYPE == GL_FLOAT
953 _mesa_debug(ctx, "color %f %f %f %f\n",
954 v->color[0], v->color[1], v->color[2], v->color[3]);
955 #else
956 _mesa_debug(ctx, "color %d %d %d %d\n",
957 v->color[0], v->color[1], v->color[2], v->color[3]);
958 #endif
959 _mesa_debug(ctx, "spec %g %g %g %g\n",
960 v->attrib[FRAG_ATTRIB_COL1][0],
961 v->attrib[FRAG_ATTRIB_COL1][1],
962 v->attrib[FRAG_ATTRIB_COL1][2],
963 v->attrib[FRAG_ATTRIB_COL1][3]);
964 _mesa_debug(ctx, "fog %f\n", v->attrib[FRAG_ATTRIB_FOGC][0]);
965 _mesa_debug(ctx, "index %d\n", v->attrib[FRAG_ATTRIB_CI][0]);
966 _mesa_debug(ctx, "pointsize %f\n", v->pointSize);
967 _mesa_debug(ctx, "\n");
968 }
969 }