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