s_context.c revision 55187ea63e980b32c7a701855571332f4357d634
1/*
2 * Mesa 3-D graphics library
3 * Version:  6.5
4 *
5 * Copyright (C) 1999-2005  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 "program.h"
35#include "texobj.h"
36#include "nvfragprog.h"
37
38#include "swrast.h"
39#include "s_blend.h"
40#include "s_context.h"
41#include "s_lines.h"
42#include "s_points.h"
43#include "s_span.h"
44#include "s_triangle.h"
45#include "s_texfilter.h"
46
47
48/**
49 * Recompute the value of swrast->_RasterMask, etc. according to
50 * the current context.  The _RasterMask field can be easily tested by
51 * drivers to determine certain basic GL state (does the primitive need
52 * stenciling, logic-op, fog, etc?).
53 */
54static void
55_swrast_update_rasterflags( GLcontext *ctx )
56{
57   GLuint 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 (ctx->Fog.Enabled)                  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[0] != 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._Active) {
103      rasterMask |= FRAGPROG_BIT;
104   }
105
106   if (ctx->ATIFragmentShader._Enabled) {
107      rasterMask |= ATIFRAGSHADER_BIT;
108   }
109
110   SWRAST_CONTEXT(ctx)->_RasterMask = rasterMask;
111}
112
113
114/**
115 * Examine polycon culls tate to compute the _BackfaceSign field.
116 * _BackfaceSign will be 0 if no culling, -1 if culling back-faces,
117 * and 1 if culling front-faces.  The Polygon FrontFace state also
118 * factors in.
119 */
120static void
121_swrast_update_polygon( GLcontext *ctx )
122{
123   GLfloat backface_sign = 1;
124
125   if (ctx->Polygon.CullFlag) {
126      backface_sign = 1;
127      switch(ctx->Polygon.CullFaceMode) {
128      case GL_BACK:
129	 if(ctx->Polygon.FrontFace==GL_CCW)
130	    backface_sign = -1;
131	 break;
132      case GL_FRONT:
133	 if(ctx->Polygon.FrontFace!=GL_CCW)
134	    backface_sign = -1;
135	 break;
136      default:
137      case GL_FRONT_AND_BACK:
138	 backface_sign = 0;
139	 break;
140      }
141   }
142   else {
143      backface_sign = 0;
144   }
145
146   SWRAST_CONTEXT(ctx)->_BackfaceSign = backface_sign;
147}
148
149
150/**
151 * Update the _PreferPixelFog field to indicate if we need to compute
152 * fog factors per-fragment.
153 */
154static void
155_swrast_update_fog_hint( GLcontext *ctx )
156{
157   SWcontext *swrast = SWRAST_CONTEXT(ctx);
158   swrast->_PreferPixelFog = (!swrast->AllowVertexFog ||
159                              ctx->FragmentProgram._Enabled || /* not _Active! */
160			      (ctx->Hint.Fog == GL_NICEST &&
161			       swrast->AllowPixelFog));
162}
163
164
165
166/**
167 * Update the swrast->_AnyTextureCombine flag.
168 */
169static void
170_swrast_update_texture_env( GLcontext *ctx )
171{
172   SWcontext *swrast = SWRAST_CONTEXT(ctx);
173   GLuint i;
174   swrast->_AnyTextureCombine = GL_FALSE;
175   for (i = 0; i < ctx->Const.MaxTextureUnits; i++) {
176      if (ctx->Texture.Unit[i].EnvMode == GL_COMBINE_EXT ||
177          ctx->Texture.Unit[i].EnvMode == GL_COMBINE4_NV) {
178         swrast->_AnyTextureCombine = GL_TRUE;
179         return;
180      }
181   }
182}
183
184
185/**
186 * Update swrast->_FogColor and swrast->_FogEnable values.
187 */
188static void
189_swrast_update_fog_state( GLcontext *ctx )
190{
191   SWcontext *swrast = SWRAST_CONTEXT(ctx);
192
193   /* convert fog color to GLchan values */
194   CLAMPED_FLOAT_TO_CHAN(swrast->_FogColor[RCOMP], ctx->Fog.Color[RCOMP]);
195   CLAMPED_FLOAT_TO_CHAN(swrast->_FogColor[GCOMP], ctx->Fog.Color[GCOMP]);
196   CLAMPED_FLOAT_TO_CHAN(swrast->_FogColor[BCOMP], ctx->Fog.Color[BCOMP]);
197
198   /* determine if fog is needed, and if so, which fog mode */
199   swrast->_FogEnabled = GL_FALSE;
200   if (ctx->FragmentProgram._Active) {
201      if (ctx->FragmentProgram._Current->Base.Target==GL_FRAGMENT_PROGRAM_ARB) {
202         const struct fragment_program *p
203            = (struct fragment_program *) ctx->FragmentProgram._Current;
204         if (p->FogOption != GL_NONE) {
205            swrast->_FogEnabled = GL_TRUE;
206            swrast->_FogMode = p->FogOption;
207         }
208      }
209   }
210   else if (ctx->Fog.Enabled) {
211      swrast->_FogEnabled = GL_TRUE;
212      swrast->_FogMode = ctx->Fog.Mode;
213   }
214}
215
216
217/**
218 * Update state for running fragment programs.  Basically, load the
219 * program parameters with current state values.
220 */
221static void
222_swrast_update_fragment_program( GLcontext *ctx )
223{
224   if (ctx->FragmentProgram._Active) {
225      struct fragment_program *program = ctx->FragmentProgram._Current;
226      _mesa_load_state_parameters(ctx, program->Parameters);
227   }
228}
229
230
231
232#define _SWRAST_NEW_DERIVED (_SWRAST_NEW_RASTERMASK |	\
233			     _NEW_TEXTURE |		\
234			     _NEW_HINT |		\
235			     _NEW_POLYGON )
236
237/* State referenced by _swrast_choose_triangle, _swrast_choose_line.
238 */
239#define _SWRAST_NEW_TRIANGLE (_SWRAST_NEW_DERIVED |		\
240			      _NEW_RENDERMODE|			\
241                              _NEW_POLYGON|			\
242                              _NEW_DEPTH|			\
243                              _NEW_STENCIL|			\
244                              _NEW_COLOR|			\
245                              _NEW_TEXTURE|			\
246                              _SWRAST_NEW_RASTERMASK|		\
247                              _NEW_LIGHT|			\
248                              _NEW_FOG |			\
249			      _DD_NEW_SEPARATE_SPECULAR)
250
251#define _SWRAST_NEW_LINE (_SWRAST_NEW_DERIVED |		\
252			  _NEW_RENDERMODE|		\
253                          _NEW_LINE|			\
254                          _NEW_TEXTURE|			\
255                          _NEW_LIGHT|			\
256                          _NEW_FOG|			\
257                          _NEW_DEPTH |			\
258                          _DD_NEW_SEPARATE_SPECULAR)
259
260#define _SWRAST_NEW_POINT (_SWRAST_NEW_DERIVED |	\
261			   _NEW_RENDERMODE |		\
262			   _NEW_POINT |			\
263			   _NEW_TEXTURE |		\
264			   _NEW_LIGHT |			\
265			   _NEW_FOG |			\
266                           _DD_NEW_SEPARATE_SPECULAR)
267
268#define _SWRAST_NEW_TEXTURE_SAMPLE_FUNC _NEW_TEXTURE
269
270#define _SWRAST_NEW_TEXTURE_ENV_MODE _NEW_TEXTURE
271
272#define _SWRAST_NEW_BLEND_FUNC _NEW_COLOR
273
274
275
276/**
277 * Stub for swrast->Triangle to select a true triangle function
278 * after a state change.
279 */
280static void
281_swrast_validate_triangle( GLcontext *ctx,
282			   const SWvertex *v0,
283                           const SWvertex *v1,
284                           const SWvertex *v2 )
285{
286   SWcontext *swrast = SWRAST_CONTEXT(ctx);
287
288   _swrast_validate_derived( ctx );
289   swrast->choose_triangle( ctx );
290
291   if (ctx->Texture._EnabledUnits == 0
292       && NEED_SECONDARY_COLOR(ctx)
293       && !ctx->FragmentProgram._Active) {
294      /* separate specular color, but no texture */
295      swrast->SpecTriangle = swrast->Triangle;
296      swrast->Triangle = _swrast_add_spec_terms_triangle;
297   }
298
299   swrast->Triangle( ctx, v0, v1, v2 );
300}
301
302/**
303 * Called via swrast->Line.  Examine current GL state and choose a software
304 * line routine.  Then call it.
305 */
306static void
307_swrast_validate_line( GLcontext *ctx, const SWvertex *v0, const SWvertex *v1 )
308{
309   SWcontext *swrast = SWRAST_CONTEXT(ctx);
310
311   _swrast_validate_derived( ctx );
312   swrast->choose_line( ctx );
313
314   if (ctx->Texture._EnabledUnits == 0
315       && NEED_SECONDARY_COLOR(ctx)
316       && !ctx->FragmentProgram._Active) {
317      swrast->SpecLine = swrast->Line;
318      swrast->Line = _swrast_add_spec_terms_line;
319   }
320
321
322   swrast->Line( ctx, v0, v1 );
323}
324
325/**
326 * Called via swrast->Point.  Examine current GL state and choose a software
327 * point routine.  Then call it.
328 */
329static void
330_swrast_validate_point( GLcontext *ctx, const SWvertex *v0 )
331{
332   SWcontext *swrast = SWRAST_CONTEXT(ctx);
333
334   _swrast_validate_derived( ctx );
335   swrast->choose_point( ctx );
336
337   if (ctx->Texture._EnabledUnits == 0
338       && NEED_SECONDARY_COLOR(ctx)
339       && !ctx->FragmentProgram._Active) {
340      swrast->SpecPoint = swrast->Point;
341      swrast->Point = _swrast_add_spec_terms_point;
342   }
343
344   swrast->Point( ctx, v0 );
345}
346
347
348/**
349 * Called via swrast->BlendFunc.  Examine GL state to choose a blending
350 * function, then call it.
351 */
352static void _ASMAPI
353_swrast_validate_blend_func( GLcontext *ctx, GLuint n,
354			     const GLubyte mask[],
355			     GLchan src[][4],
356			     CONST GLchan dst[][4] )
357{
358   SWcontext *swrast = SWRAST_CONTEXT(ctx);
359
360   _swrast_validate_derived( ctx );
361   _swrast_choose_blend_func( ctx );
362
363   swrast->BlendFunc( ctx, n, mask, src, dst );
364}
365
366
367/**
368 * Called via the swrast->TextureSample[i] function pointer.
369 * Basically, given a texture object, an array of texture coords
370 * and an array of level-of-detail values, return an array of colors.
371 * In this case, determine the correct texture sampling routine
372 * (depending on filter mode, texture dimensions, etc) then call the
373 * sampler routine.
374 */
375static void
376_swrast_validate_texture_sample( GLcontext *ctx, GLuint texUnit,
377				 const struct gl_texture_object *tObj,
378				 GLuint n, const GLfloat texcoords[][4],
379				 const GLfloat lambda[], GLchan rgba[][4] )
380{
381   SWcontext *swrast = SWRAST_CONTEXT(ctx);
382
383   _swrast_validate_derived( ctx );
384
385   /* Compute min/mag filter threshold */
386   if (tObj && tObj->MinFilter != tObj->MagFilter) {
387      if (tObj->MagFilter == GL_LINEAR
388          && (tObj->MinFilter == GL_NEAREST_MIPMAP_NEAREST ||
389              tObj->MinFilter == GL_NEAREST_MIPMAP_LINEAR)) {
390         swrast->_MinMagThresh[texUnit] = 0.5F;
391      }
392      else {
393         swrast->_MinMagThresh[texUnit] = 0.0F;
394      }
395   }
396
397   swrast->TextureSample[texUnit] =
398      _swrast_choose_texture_sample_func( ctx, tObj );
399
400   swrast->TextureSample[texUnit]( ctx, texUnit, tObj, n, texcoords,
401                                   lambda, rgba );
402}
403
404
405static void
406_swrast_sleep( GLcontext *ctx, GLuint new_state )
407{
408   (void) ctx; (void) new_state;
409}
410
411
412static void
413_swrast_invalidate_state( GLcontext *ctx, GLuint new_state )
414{
415   SWcontext *swrast = SWRAST_CONTEXT(ctx);
416   GLuint i;
417
418   swrast->NewState |= new_state;
419
420   /* After 10 statechanges without any swrast functions being called,
421    * put the module to sleep.
422    */
423   if (++swrast->StateChanges > 10) {
424      swrast->InvalidateState = _swrast_sleep;
425      swrast->NewState = ~0;
426      new_state = ~0;
427   }
428
429   if (new_state & swrast->invalidate_triangle)
430      swrast->Triangle = _swrast_validate_triangle;
431
432   if (new_state & swrast->invalidate_line)
433      swrast->Line = _swrast_validate_line;
434
435   if (new_state & swrast->invalidate_point)
436      swrast->Point = _swrast_validate_point;
437
438   if (new_state & _SWRAST_NEW_BLEND_FUNC)
439      swrast->BlendFunc = _swrast_validate_blend_func;
440
441   if (new_state & _SWRAST_NEW_TEXTURE_SAMPLE_FUNC)
442      for (i = 0 ; i < ctx->Const.MaxTextureUnits ; i++)
443	 swrast->TextureSample[i] = _swrast_validate_texture_sample;
444}
445
446
447void
448_swrast_validate_derived( GLcontext *ctx )
449{
450   SWcontext *swrast = SWRAST_CONTEXT(ctx);
451
452   if (swrast->NewState) {
453      if (swrast->NewState & _SWRAST_NEW_RASTERMASK)
454 	 _swrast_update_rasterflags( ctx );
455
456      if (swrast->NewState & _NEW_POLYGON)
457	 _swrast_update_polygon( ctx );
458
459      if (swrast->NewState & (_NEW_HINT | _NEW_PROGRAM))
460	 _swrast_update_fog_hint( ctx );
461
462      if (swrast->NewState & _SWRAST_NEW_TEXTURE_ENV_MODE)
463	 _swrast_update_texture_env( ctx );
464
465      if (swrast->NewState & (_NEW_FOG | _NEW_PROGRAM))
466         _swrast_update_fog_state( ctx );
467
468      if (swrast->NewState & _NEW_PROGRAM)
469	 _swrast_update_fragment_program( ctx );
470
471      swrast->NewState = 0;
472      swrast->StateChanges = 0;
473      swrast->InvalidateState = _swrast_invalidate_state;
474   }
475}
476
477#define SWRAST_DEBUG 0
478
479/* Public entrypoints:  See also s_accum.c, s_bitmap.c, etc.
480 */
481void
482_swrast_Quad( GLcontext *ctx,
483	      const SWvertex *v0, const SWvertex *v1,
484              const SWvertex *v2, const SWvertex *v3 )
485{
486   if (SWRAST_DEBUG) {
487      _mesa_debug(ctx, "_swrast_Quad\n");
488      _swrast_print_vertex( ctx, v0 );
489      _swrast_print_vertex( ctx, v1 );
490      _swrast_print_vertex( ctx, v2 );
491      _swrast_print_vertex( ctx, v3 );
492   }
493   SWRAST_CONTEXT(ctx)->Triangle( ctx, v0, v1, v3 );
494   SWRAST_CONTEXT(ctx)->Triangle( ctx, v1, v2, v3 );
495}
496
497void
498_swrast_Triangle( GLcontext *ctx, const SWvertex *v0,
499                  const SWvertex *v1, const SWvertex *v2 )
500{
501   if (SWRAST_DEBUG) {
502      _mesa_debug(ctx, "_swrast_Triangle\n");
503      _swrast_print_vertex( ctx, v0 );
504      _swrast_print_vertex( ctx, v1 );
505      _swrast_print_vertex( ctx, v2 );
506   }
507   SWRAST_CONTEXT(ctx)->Triangle( ctx, v0, v1, v2 );
508}
509
510void
511_swrast_Line( GLcontext *ctx, const SWvertex *v0, const SWvertex *v1 )
512{
513   if (SWRAST_DEBUG) {
514      _mesa_debug(ctx, "_swrast_Line\n");
515      _swrast_print_vertex( ctx, v0 );
516      _swrast_print_vertex( ctx, v1 );
517   }
518   SWRAST_CONTEXT(ctx)->Line( ctx, v0, v1 );
519}
520
521void
522_swrast_Point( GLcontext *ctx, const SWvertex *v0 )
523{
524   if (SWRAST_DEBUG) {
525      _mesa_debug(ctx, "_swrast_Point\n");
526      _swrast_print_vertex( ctx, v0 );
527   }
528   SWRAST_CONTEXT(ctx)->Point( ctx, v0 );
529}
530
531void
532_swrast_InvalidateState( GLcontext *ctx, GLuint new_state )
533{
534   if (SWRAST_DEBUG) {
535      _mesa_debug(ctx, "_swrast_InvalidateState\n");
536   }
537   SWRAST_CONTEXT(ctx)->InvalidateState( ctx, new_state );
538}
539
540void
541_swrast_ResetLineStipple( GLcontext *ctx )
542{
543   if (SWRAST_DEBUG) {
544      _mesa_debug(ctx, "_swrast_ResetLineStipple\n");
545   }
546   SWRAST_CONTEXT(ctx)->StippleCounter = 0;
547}
548
549void
550_swrast_allow_vertex_fog( GLcontext *ctx, GLboolean value )
551{
552   if (SWRAST_DEBUG) {
553      _mesa_debug(ctx, "_swrast_allow_vertex_fog %d\n", value);
554   }
555   SWRAST_CONTEXT(ctx)->InvalidateState( ctx, _NEW_HINT );
556   SWRAST_CONTEXT(ctx)->AllowVertexFog = value;
557}
558
559void
560_swrast_allow_pixel_fog( GLcontext *ctx, GLboolean value )
561{
562   if (SWRAST_DEBUG) {
563      _mesa_debug(ctx, "_swrast_allow_pixel_fog %d\n", value);
564   }
565   SWRAST_CONTEXT(ctx)->InvalidateState( ctx, _NEW_HINT );
566   SWRAST_CONTEXT(ctx)->AllowPixelFog = value;
567}
568
569
570GLboolean
571_swrast_CreateContext( GLcontext *ctx )
572{
573   GLuint i;
574   SWcontext *swrast = (SWcontext *)CALLOC(sizeof(SWcontext));
575
576   if (SWRAST_DEBUG) {
577      _mesa_debug(ctx, "_swrast_CreateContext\n");
578   }
579
580   if (!swrast)
581      return GL_FALSE;
582
583   swrast->NewState = ~0;
584
585   swrast->choose_point = _swrast_choose_point;
586   swrast->choose_line = _swrast_choose_line;
587   swrast->choose_triangle = _swrast_choose_triangle;
588
589   swrast->invalidate_point = _SWRAST_NEW_POINT;
590   swrast->invalidate_line = _SWRAST_NEW_LINE;
591   swrast->invalidate_triangle = _SWRAST_NEW_TRIANGLE;
592
593   swrast->Point = _swrast_validate_point;
594   swrast->Line = _swrast_validate_line;
595   swrast->Triangle = _swrast_validate_triangle;
596   swrast->InvalidateState = _swrast_sleep;
597   swrast->BlendFunc = _swrast_validate_blend_func;
598
599   swrast->AllowVertexFog = GL_TRUE;
600   swrast->AllowPixelFog = GL_TRUE;
601
602   /* Optimized Accum buffer */
603   swrast->_IntegerAccumMode = GL_FALSE;
604   swrast->_IntegerAccumScaler = 0.0;
605
606   for (i = 0; i < MAX_TEXTURE_IMAGE_UNITS; i++)
607      swrast->TextureSample[i] = _swrast_validate_texture_sample;
608
609   swrast->SpanArrays = MALLOC_STRUCT(span_arrays);
610   if (!swrast->SpanArrays) {
611      FREE(swrast);
612      return GL_FALSE;
613   }
614
615   /* init point span buffer */
616   swrast->PointSpan.primitive = GL_POINT;
617   swrast->PointSpan.start = 0;
618   swrast->PointSpan.end = 0;
619   swrast->PointSpan.facing = 0;
620   swrast->PointSpan.array = swrast->SpanArrays;
621
622   assert(ctx->Const.MaxTextureUnits > 0);
623   assert(ctx->Const.MaxTextureUnits <= MAX_TEXTURE_UNITS);
624
625   swrast->TexelBuffer = (GLchan *) MALLOC(ctx->Const.MaxTextureUnits *
626                                           MAX_WIDTH * 4 * sizeof(GLchan));
627   if (!swrast->TexelBuffer) {
628      FREE(swrast->SpanArrays);
629      FREE(swrast);
630      return GL_FALSE;
631   }
632
633   ctx->swrast_context = swrast;
634
635   return GL_TRUE;
636}
637
638void
639_swrast_DestroyContext( GLcontext *ctx )
640{
641   SWcontext *swrast = SWRAST_CONTEXT(ctx);
642
643   if (SWRAST_DEBUG) {
644      _mesa_debug(ctx, "_swrast_DestroyContext\n");
645   }
646
647   FREE( swrast->SpanArrays );
648   FREE( swrast->TexelBuffer );
649   FREE( swrast );
650
651   ctx->swrast_context = 0;
652}
653
654
655struct swrast_device_driver *
656_swrast_GetDeviceDriverReference( GLcontext *ctx )
657{
658   SWcontext *swrast = SWRAST_CONTEXT(ctx);
659   return &swrast->Driver;
660}
661
662void
663_swrast_flush( GLcontext *ctx )
664{
665   SWcontext *swrast = SWRAST_CONTEXT(ctx);
666   /* flush any pending fragments from rendering points */
667   if (swrast->PointSpan.end > 0) {
668      if (ctx->Visual.rgbMode) {
669         _swrast_write_rgba_span(ctx, &(swrast->PointSpan));
670      }
671      else {
672         _swrast_write_index_span(ctx, &(swrast->PointSpan));
673      }
674      swrast->PointSpan.end = 0;
675   }
676}
677
678void
679_swrast_render_primitive( GLcontext *ctx, GLenum prim )
680{
681   SWcontext *swrast = SWRAST_CONTEXT(ctx);
682   if (swrast->Primitive == GL_POINTS && prim != GL_POINTS) {
683      _swrast_flush(ctx);
684   }
685   swrast->Primitive = prim;
686}
687
688
689void
690_swrast_render_start( GLcontext *ctx )
691{
692   SWcontext *swrast = SWRAST_CONTEXT(ctx);
693   if (swrast->Driver.SpanRenderStart)
694      swrast->Driver.SpanRenderStart( ctx );
695   swrast->PointSpan.end = 0;
696}
697
698void
699_swrast_render_finish( GLcontext *ctx )
700{
701   SWcontext *swrast = SWRAST_CONTEXT(ctx);
702   if (swrast->Driver.SpanRenderFinish)
703      swrast->Driver.SpanRenderFinish( ctx );
704
705   _swrast_flush(ctx);
706}
707
708
709#define SWRAST_DEBUG_VERTICES 0
710
711void
712_swrast_print_vertex( GLcontext *ctx, const SWvertex *v )
713{
714   GLuint i;
715
716   if (SWRAST_DEBUG_VERTICES) {
717      _mesa_debug(ctx, "win %f %f %f %f\n",
718                  v->win[0], v->win[1], v->win[2], v->win[3]);
719
720      for (i = 0 ; i < ctx->Const.MaxTextureUnits ; i++)
721	 if (ctx->Texture.Unit[i]._ReallyEnabled)
722	    _mesa_debug(ctx, "texcoord[%d] %f %f %f %f\n", i,
723                        v->texcoord[i][0], v->texcoord[i][1],
724                        v->texcoord[i][2], v->texcoord[i][3]);
725
726#if CHAN_TYPE == GL_FLOAT
727      _mesa_debug(ctx, "color %f %f %f %f\n",
728                  v->color[0], v->color[1], v->color[2], v->color[3]);
729      _mesa_debug(ctx, "spec %f %f %f %f\n",
730                  v->specular[0], v->specular[1],
731                  v->specular[2], v->specular[3]);
732#else
733      _mesa_debug(ctx, "color %d %d %d %d\n",
734                  v->color[0], v->color[1], v->color[2], v->color[3]);
735      _mesa_debug(ctx, "spec %d %d %d %d\n",
736                  v->specular[0], v->specular[1],
737                  v->specular[2], v->specular[3]);
738#endif
739      _mesa_debug(ctx, "fog %f\n", v->fog);
740      _mesa_debug(ctx, "index %d\n", v->index);
741      _mesa_debug(ctx, "pointsize %f\n", v->pointSize);
742      _mesa_debug(ctx, "\n");
743   }
744}
745