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