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