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