Initial work for supporting different renderbuffer color depths at runtime.
[mesa.git] / src / mesa / swrast / s_context.c
1 /*
2 * Mesa 3-D graphics library
3 * Version: 6.5.1
4 *
5 * Copyright (C) 1999-2006 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 "teximage.h"
36 #include "swrast.h"
37 #include "s_blend.h"
38 #include "s_context.h"
39 #include "s_lines.h"
40 #include "s_points.h"
41 #include "s_span.h"
42 #include "s_triangle.h"
43 #include "s_texfilter.h"
44
45
46 /**
47 * Recompute the value of swrast->_RasterMask, etc. according to
48 * the current context. The _RasterMask field can be easily tested by
49 * drivers to determine certain basic GL state (does the primitive need
50 * stenciling, logic-op, fog, etc?).
51 */
52 static void
53 _swrast_update_rasterflags( GLcontext *ctx )
54 {
55 SWcontext *swrast = SWRAST_CONTEXT(ctx);
56 GLbitfield rasterMask = 0;
57
58 if (ctx->Color.AlphaEnabled) rasterMask |= ALPHATEST_BIT;
59 if (ctx->Color.BlendEnabled) rasterMask |= BLEND_BIT;
60 if (ctx->Depth.Test) rasterMask |= DEPTH_BIT;
61 if (swrast->_FogEnabled) rasterMask |= FOG_BIT;
62 if (ctx->Scissor.Enabled) rasterMask |= CLIP_BIT;
63 if (ctx->Stencil.Enabled) rasterMask |= STENCIL_BIT;
64 if (ctx->Visual.rgbMode) {
65 const GLuint colorMask = *((GLuint *) &ctx->Color.ColorMask);
66 if (colorMask != 0xffffffff) rasterMask |= MASKING_BIT;
67 if (ctx->Color._LogicOpEnabled) rasterMask |= LOGIC_OP_BIT;
68 if (ctx->Texture._EnabledUnits) rasterMask |= TEXTURE_BIT;
69 }
70 else {
71 if (ctx->Color.IndexMask != 0xffffffff) rasterMask |= MASKING_BIT;
72 if (ctx->Color.IndexLogicOpEnabled) rasterMask |= LOGIC_OP_BIT;
73 }
74
75 if ( ctx->Viewport.X < 0
76 || ctx->Viewport.X + ctx->Viewport.Width > (GLint) ctx->DrawBuffer->Width
77 || ctx->Viewport.Y < 0
78 || ctx->Viewport.Y + ctx->Viewport.Height > (GLint) ctx->DrawBuffer->Height) {
79 rasterMask |= CLIP_BIT;
80 }
81
82 if (ctx->Query.CurrentOcclusionObject)
83 rasterMask |= OCCLUSION_BIT;
84
85
86 /* If we're not drawing to exactly one color buffer set the
87 * MULTI_DRAW_BIT flag. Also set it if we're drawing to no
88 * buffers or the RGBA or CI mask disables all writes.
89 */
90 if (ctx->DrawBuffer->_NumColorDrawBuffers[0] != 1) {
91 /* more than one color buffer designated for writing (or zero buffers) */
92 rasterMask |= MULTI_DRAW_BIT;
93 }
94 else if (ctx->Visual.rgbMode && *((GLuint *) ctx->Color.ColorMask) == 0) {
95 rasterMask |= MULTI_DRAW_BIT; /* all RGBA channels disabled */
96 }
97 else if (!ctx->Visual.rgbMode && ctx->Color.IndexMask==0) {
98 rasterMask |= MULTI_DRAW_BIT; /* all color index bits disabled */
99 }
100
101 if (ctx->FragmentProgram._Enabled) {
102 rasterMask |= FRAGPROG_BIT;
103 }
104
105 if (ctx->ShaderObjects._FragmentShaderPresent) {
106 rasterMask |= FRAGPROG_BIT;
107 }
108
109 if (ctx->ATIFragmentShader._Enabled) {
110 rasterMask |= ATIFRAGSHADER_BIT;
111 }
112
113 #if CHAN_TYPE == GL_FLOAT
114 if (ctx->Color.ClampFragmentColor == GL_TRUE) {
115 rasterMask |= CLAMPING_BIT;
116 }
117 #endif
118
119 SWRAST_CONTEXT(ctx)->_RasterMask = rasterMask;
120 }
121
122
123 /**
124 * Examine polycon culls tate to compute the _BackfaceSign field.
125 * _BackfaceSign will be 0 if no culling, -1 if culling back-faces,
126 * and 1 if culling front-faces. The Polygon FrontFace state also
127 * factors in.
128 */
129 static void
130 _swrast_update_polygon( GLcontext *ctx )
131 {
132 GLfloat backface_sign = 1;
133
134 if (ctx->Polygon.CullFlag) {
135 backface_sign = 1;
136 switch(ctx->Polygon.CullFaceMode) {
137 case GL_BACK:
138 if(ctx->Polygon.FrontFace==GL_CCW)
139 backface_sign = -1;
140 break;
141 case GL_FRONT:
142 if(ctx->Polygon.FrontFace!=GL_CCW)
143 backface_sign = -1;
144 break;
145 default:
146 case GL_FRONT_AND_BACK:
147 backface_sign = 0;
148 break;
149 }
150 }
151 else {
152 backface_sign = 0;
153 }
154
155 SWRAST_CONTEXT(ctx)->_BackfaceSign = backface_sign;
156 }
157
158
159 /**
160 * Update the _PreferPixelFog field to indicate if we need to compute
161 * fog factors per-fragment.
162 */
163 static void
164 _swrast_update_fog_hint( GLcontext *ctx )
165 {
166 SWcontext *swrast = SWRAST_CONTEXT(ctx);
167 swrast->_PreferPixelFog = (!swrast->AllowVertexFog ||
168 ctx->FragmentProgram._Enabled || /* not _Active! */
169 (ctx->Hint.Fog == GL_NICEST &&
170 swrast->AllowPixelFog));
171 }
172
173
174
175 /**
176 * Update the swrast->_AnyTextureCombine flag.
177 */
178 static void
179 _swrast_update_texture_env( GLcontext *ctx )
180 {
181 SWcontext *swrast = SWRAST_CONTEXT(ctx);
182 GLuint i;
183 swrast->_AnyTextureCombine = GL_FALSE;
184 for (i = 0; i < ctx->Const.MaxTextureUnits; i++) {
185 if (ctx->Texture.Unit[i].EnvMode == GL_COMBINE_EXT ||
186 ctx->Texture.Unit[i].EnvMode == GL_COMBINE4_NV) {
187 swrast->_AnyTextureCombine = GL_TRUE;
188 return;
189 }
190 }
191 }
192
193
194 /**
195 * Update swrast->_FogColor and swrast->_FogEnable values.
196 */
197 static void
198 _swrast_update_fog_state( GLcontext *ctx )
199 {
200 SWcontext *swrast = SWRAST_CONTEXT(ctx);
201
202 /* determine if fog is needed, and if so, which fog mode */
203 swrast->_FogEnabled = GL_FALSE;
204 if (ctx->FragmentProgram._Enabled) {
205 if (ctx->FragmentProgram._Current->Base.Target==GL_FRAGMENT_PROGRAM_ARB) {
206 const struct gl_fragment_program *fp
207 = ctx->FragmentProgram._Current;
208 if (fp->FogOption != GL_NONE) {
209 swrast->_FogEnabled = GL_TRUE;
210 swrast->_FogMode = fp->FogOption;
211 }
212 }
213 }
214 else if (ctx->Fog.Enabled) {
215 swrast->_FogEnabled = GL_TRUE;
216 swrast->_FogMode = ctx->Fog.Mode;
217 }
218 }
219
220
221 /**
222 * Update state for running fragment programs. Basically, load the
223 * program parameters with current state values.
224 */
225 static void
226 _swrast_update_fragment_program( GLcontext *ctx )
227 {
228 if (ctx->FragmentProgram._Enabled) {
229 const struct gl_fragment_program *fp = ctx->FragmentProgram._Current;
230 _mesa_load_state_parameters(ctx, fp->Base.Parameters);
231 }
232 }
233
234
235
236 #define _SWRAST_NEW_DERIVED (_SWRAST_NEW_RASTERMASK | \
237 _NEW_TEXTURE | \
238 _NEW_HINT | \
239 _NEW_POLYGON )
240
241 /* State referenced by _swrast_choose_triangle, _swrast_choose_line.
242 */
243 #define _SWRAST_NEW_TRIANGLE (_SWRAST_NEW_DERIVED | \
244 _NEW_RENDERMODE| \
245 _NEW_POLYGON| \
246 _NEW_DEPTH| \
247 _NEW_STENCIL| \
248 _NEW_COLOR| \
249 _NEW_TEXTURE| \
250 _SWRAST_NEW_RASTERMASK| \
251 _NEW_LIGHT| \
252 _NEW_FOG | \
253 _DD_NEW_SEPARATE_SPECULAR)
254
255 #define _SWRAST_NEW_LINE (_SWRAST_NEW_DERIVED | \
256 _NEW_RENDERMODE| \
257 _NEW_LINE| \
258 _NEW_TEXTURE| \
259 _NEW_LIGHT| \
260 _NEW_FOG| \
261 _NEW_DEPTH | \
262 _DD_NEW_SEPARATE_SPECULAR)
263
264 #define _SWRAST_NEW_POINT (_SWRAST_NEW_DERIVED | \
265 _NEW_RENDERMODE | \
266 _NEW_POINT | \
267 _NEW_TEXTURE | \
268 _NEW_LIGHT | \
269 _NEW_FOG | \
270 _DD_NEW_SEPARATE_SPECULAR)
271
272 #define _SWRAST_NEW_TEXTURE_SAMPLE_FUNC _NEW_TEXTURE
273
274 #define _SWRAST_NEW_TEXTURE_ENV_MODE _NEW_TEXTURE
275
276 #define _SWRAST_NEW_BLEND_FUNC _NEW_COLOR
277
278
279
280 /**
281 * Stub for swrast->Triangle to select a true triangle function
282 * after a state change.
283 */
284 static void
285 _swrast_validate_triangle( GLcontext *ctx,
286 const SWvertex *v0,
287 const SWvertex *v1,
288 const SWvertex *v2 )
289 {
290 SWcontext *swrast = SWRAST_CONTEXT(ctx);
291
292 _swrast_validate_derived( ctx );
293 swrast->choose_triangle( ctx );
294
295 if (ctx->Texture._EnabledUnits == 0
296 && NEED_SECONDARY_COLOR(ctx)
297 && !ctx->FragmentProgram._Enabled) {
298 /* separate specular color, but no texture */
299 swrast->SpecTriangle = swrast->Triangle;
300 swrast->Triangle = _swrast_add_spec_terms_triangle;
301 }
302
303 swrast->Triangle( ctx, v0, v1, v2 );
304 }
305
306 /**
307 * Called via swrast->Line. Examine current GL state and choose a software
308 * line routine. Then call it.
309 */
310 static void
311 _swrast_validate_line( GLcontext *ctx, const SWvertex *v0, const SWvertex *v1 )
312 {
313 SWcontext *swrast = SWRAST_CONTEXT(ctx);
314
315 _swrast_validate_derived( ctx );
316 swrast->choose_line( ctx );
317
318 if (ctx->Texture._EnabledUnits == 0
319 && NEED_SECONDARY_COLOR(ctx)
320 && !ctx->FragmentProgram._Enabled) {
321 swrast->SpecLine = swrast->Line;
322 swrast->Line = _swrast_add_spec_terms_line;
323 }
324
325
326 swrast->Line( ctx, v0, v1 );
327 }
328
329 /**
330 * Called via swrast->Point. Examine current GL state and choose a software
331 * point routine. Then call it.
332 */
333 static void
334 _swrast_validate_point( GLcontext *ctx, const SWvertex *v0 )
335 {
336 SWcontext *swrast = SWRAST_CONTEXT(ctx);
337
338 _swrast_validate_derived( ctx );
339 swrast->choose_point( ctx );
340
341 if (ctx->Texture._EnabledUnits == 0
342 && NEED_SECONDARY_COLOR(ctx)
343 && !ctx->FragmentProgram._Enabled) {
344 swrast->SpecPoint = swrast->Point;
345 swrast->Point = _swrast_add_spec_terms_point;
346 }
347
348 swrast->Point( ctx, v0 );
349 }
350
351
352 /**
353 * Called via swrast->BlendFunc. Examine GL state to choose a blending
354 * function, then call it.
355 */
356 static void _ASMAPI
357 _swrast_validate_blend_func(GLcontext *ctx, GLuint n, const GLubyte mask[],
358 GLchan src[][4], CONST GLchan dst[][4],
359 GLenum chanType )
360 {
361 SWcontext *swrast = SWRAST_CONTEXT(ctx);
362
363 _swrast_validate_derived( ctx );
364 _swrast_choose_blend_func( ctx );
365
366 swrast->BlendFunc( ctx, n, mask, src, dst, chanType );
367 }
368
369
370 /**
371 * Make sure we have texture image data for all the textures we may need
372 * for subsequent rendering.
373 */
374 static void
375 _swrast_validate_texture_images(GLcontext *ctx)
376 {
377 SWcontext *swrast = SWRAST_CONTEXT(ctx);
378 GLuint u;
379
380 if (!swrast->ValidateTextureImage || !ctx->Texture._EnabledUnits) {
381 /* no textures enabled, or no way to validate images! */
382 return;
383 }
384
385 for (u = 0; u < ctx->Const.MaxTextureImageUnits; u++) {
386 if (ctx->Texture.Unit[u]._ReallyEnabled) {
387 struct gl_texture_object *texObj = ctx->Texture.Unit[u]._Current;
388 ASSERT(texObj);
389 if (texObj) {
390 GLuint numFaces = (texObj->Target == GL_TEXTURE_CUBE_MAP) ? 6 : 1;
391 GLuint face;
392 for (face = 0; face < numFaces; face++) {
393 GLuint lvl;
394 for (lvl = texObj->BaseLevel; lvl <= texObj->_MaxLevel; lvl++) {
395 struct gl_texture_image *texImg = texObj->Image[face][lvl];
396 if (texImg && !texImg->Data) {
397 swrast->ValidateTextureImage(ctx, texObj, face, lvl);
398 ASSERT(texObj->Image[face][lvl]->Data);
399 }
400 }
401 }
402 }
403 }
404 }
405 }
406
407
408 /**
409 * Free the texture image data attached to all currently enabled
410 * textures. Meant to be called by device drivers when transitioning
411 * from software to hardware rendering.
412 */
413 void
414 _swrast_eject_texture_images(GLcontext *ctx)
415 {
416 GLuint u;
417
418 if (!ctx->Texture._EnabledUnits) {
419 /* no textures enabled */
420 return;
421 }
422
423 for (u = 0; u < ctx->Const.MaxTextureImageUnits; u++) {
424 if (ctx->Texture.Unit[u]._ReallyEnabled) {
425 struct gl_texture_object *texObj = ctx->Texture.Unit[u]._Current;
426 ASSERT(texObj);
427 if (texObj) {
428 GLuint numFaces = (texObj->Target == GL_TEXTURE_CUBE_MAP) ? 6 : 1;
429 GLuint face;
430 for (face = 0; face < numFaces; face++) {
431 GLuint lvl;
432 for (lvl = texObj->BaseLevel; lvl <= texObj->_MaxLevel; lvl++) {
433 struct gl_texture_image *texImg = texObj->Image[face][lvl];
434 if (texImg && texImg->Data) {
435 _mesa_free_texmemory(texImg->Data);
436 texImg->Data = NULL;
437 }
438 }
439 }
440 }
441 }
442 }
443 }
444
445
446
447 static void
448 _swrast_sleep( GLcontext *ctx, GLbitfield new_state )
449 {
450 (void) ctx; (void) new_state;
451 }
452
453
454 static void
455 _swrast_invalidate_state( GLcontext *ctx, GLbitfield new_state )
456 {
457 SWcontext *swrast = SWRAST_CONTEXT(ctx);
458 GLuint i;
459
460 swrast->NewState |= new_state;
461
462 /* After 10 statechanges without any swrast functions being called,
463 * put the module to sleep.
464 */
465 if (++swrast->StateChanges > 10) {
466 swrast->InvalidateState = _swrast_sleep;
467 swrast->NewState = ~0;
468 new_state = ~0;
469 }
470
471 if (new_state & swrast->InvalidateTriangleMask)
472 swrast->Triangle = _swrast_validate_triangle;
473
474 if (new_state & swrast->InvalidateLineMask)
475 swrast->Line = _swrast_validate_line;
476
477 if (new_state & swrast->InvalidatePointMask)
478 swrast->Point = _swrast_validate_point;
479
480 if (new_state & _SWRAST_NEW_BLEND_FUNC)
481 swrast->BlendFunc = _swrast_validate_blend_func;
482
483 if (new_state & _SWRAST_NEW_TEXTURE_SAMPLE_FUNC)
484 for (i = 0 ; i < ctx->Const.MaxTextureImageUnits ; i++)
485 swrast->TextureSample[i] = NULL;
486 }
487
488
489 static void
490 _swrast_update_texture_samplers(GLcontext *ctx)
491 {
492 SWcontext *swrast = SWRAST_CONTEXT(ctx);
493 GLuint u;
494
495 for (u = 0; u < ctx->Const.MaxTextureImageUnits; u++) {
496 const struct gl_texture_object *tObj = ctx->Texture.Unit[u]._Current;
497 if (tObj)
498 swrast->TextureSample[u] =
499 _swrast_choose_texture_sample_func(ctx, tObj);
500 }
501 }
502
503
504 void
505 _swrast_validate_derived( GLcontext *ctx )
506 {
507 SWcontext *swrast = SWRAST_CONTEXT(ctx);
508
509 if (swrast->NewState) {
510 if (swrast->NewState & _NEW_POLYGON)
511 _swrast_update_polygon( ctx );
512
513 if (swrast->NewState & (_NEW_HINT | _NEW_PROGRAM))
514 _swrast_update_fog_hint( ctx );
515
516 if (swrast->NewState & _SWRAST_NEW_TEXTURE_ENV_MODE)
517 _swrast_update_texture_env( ctx );
518
519 if (swrast->NewState & (_NEW_FOG | _NEW_PROGRAM))
520 _swrast_update_fog_state( ctx );
521
522 if (swrast->NewState & _NEW_PROGRAM)
523 _swrast_update_fragment_program( ctx );
524
525 if (swrast->NewState & _NEW_TEXTURE)
526 _swrast_update_texture_samplers( ctx );
527
528 if (swrast->NewState & (_NEW_TEXTURE | _NEW_PROGRAM))
529 _swrast_validate_texture_images( ctx );
530
531 if (swrast->NewState & _SWRAST_NEW_RASTERMASK)
532 _swrast_update_rasterflags( ctx );
533
534 swrast->NewState = 0;
535 swrast->StateChanges = 0;
536 swrast->InvalidateState = _swrast_invalidate_state;
537 }
538 }
539
540 #define SWRAST_DEBUG 0
541
542 /* Public entrypoints: See also s_accum.c, s_bitmap.c, etc.
543 */
544 void
545 _swrast_Quad( GLcontext *ctx,
546 const SWvertex *v0, const SWvertex *v1,
547 const SWvertex *v2, const SWvertex *v3 )
548 {
549 if (SWRAST_DEBUG) {
550 _mesa_debug(ctx, "_swrast_Quad\n");
551 _swrast_print_vertex( ctx, v0 );
552 _swrast_print_vertex( ctx, v1 );
553 _swrast_print_vertex( ctx, v2 );
554 _swrast_print_vertex( ctx, v3 );
555 }
556 SWRAST_CONTEXT(ctx)->Triangle( ctx, v0, v1, v3 );
557 SWRAST_CONTEXT(ctx)->Triangle( ctx, v1, v2, v3 );
558 }
559
560 void
561 _swrast_Triangle( GLcontext *ctx, const SWvertex *v0,
562 const SWvertex *v1, const SWvertex *v2 )
563 {
564 if (SWRAST_DEBUG) {
565 _mesa_debug(ctx, "_swrast_Triangle\n");
566 _swrast_print_vertex( ctx, v0 );
567 _swrast_print_vertex( ctx, v1 );
568 _swrast_print_vertex( ctx, v2 );
569 }
570 SWRAST_CONTEXT(ctx)->Triangle( ctx, v0, v1, v2 );
571 }
572
573 void
574 _swrast_Line( GLcontext *ctx, const SWvertex *v0, const SWvertex *v1 )
575 {
576 if (SWRAST_DEBUG) {
577 _mesa_debug(ctx, "_swrast_Line\n");
578 _swrast_print_vertex( ctx, v0 );
579 _swrast_print_vertex( ctx, v1 );
580 }
581 SWRAST_CONTEXT(ctx)->Line( ctx, v0, v1 );
582 }
583
584 void
585 _swrast_Point( GLcontext *ctx, const SWvertex *v0 )
586 {
587 if (SWRAST_DEBUG) {
588 _mesa_debug(ctx, "_swrast_Point\n");
589 _swrast_print_vertex( ctx, v0 );
590 }
591 SWRAST_CONTEXT(ctx)->Point( ctx, v0 );
592 }
593
594 void
595 _swrast_InvalidateState( GLcontext *ctx, GLbitfield new_state )
596 {
597 if (SWRAST_DEBUG) {
598 _mesa_debug(ctx, "_swrast_InvalidateState\n");
599 }
600 SWRAST_CONTEXT(ctx)->InvalidateState( ctx, new_state );
601 }
602
603 void
604 _swrast_ResetLineStipple( GLcontext *ctx )
605 {
606 if (SWRAST_DEBUG) {
607 _mesa_debug(ctx, "_swrast_ResetLineStipple\n");
608 }
609 SWRAST_CONTEXT(ctx)->StippleCounter = 0;
610 }
611
612 void
613 _swrast_allow_vertex_fog( GLcontext *ctx, GLboolean value )
614 {
615 if (SWRAST_DEBUG) {
616 _mesa_debug(ctx, "_swrast_allow_vertex_fog %d\n", value);
617 }
618 SWRAST_CONTEXT(ctx)->InvalidateState( ctx, _NEW_HINT );
619 SWRAST_CONTEXT(ctx)->AllowVertexFog = value;
620 }
621
622 void
623 _swrast_allow_pixel_fog( GLcontext *ctx, GLboolean value )
624 {
625 if (SWRAST_DEBUG) {
626 _mesa_debug(ctx, "_swrast_allow_pixel_fog %d\n", value);
627 }
628 SWRAST_CONTEXT(ctx)->InvalidateState( ctx, _NEW_HINT );
629 SWRAST_CONTEXT(ctx)->AllowPixelFog = value;
630 }
631
632
633 GLboolean
634 _swrast_CreateContext( GLcontext *ctx )
635 {
636 GLuint i;
637 SWcontext *swrast = (SWcontext *)CALLOC(sizeof(SWcontext));
638
639 if (SWRAST_DEBUG) {
640 _mesa_debug(ctx, "_swrast_CreateContext\n");
641 }
642
643 if (!swrast)
644 return GL_FALSE;
645
646 swrast->NewState = ~0;
647
648 swrast->choose_point = _swrast_choose_point;
649 swrast->choose_line = _swrast_choose_line;
650 swrast->choose_triangle = _swrast_choose_triangle;
651
652 swrast->InvalidatePointMask = _SWRAST_NEW_POINT;
653 swrast->InvalidateLineMask = _SWRAST_NEW_LINE;
654 swrast->InvalidateTriangleMask = _SWRAST_NEW_TRIANGLE;
655
656 swrast->Point = _swrast_validate_point;
657 swrast->Line = _swrast_validate_line;
658 swrast->Triangle = _swrast_validate_triangle;
659 swrast->InvalidateState = _swrast_sleep;
660 swrast->BlendFunc = _swrast_validate_blend_func;
661
662 swrast->AllowVertexFog = GL_TRUE;
663 swrast->AllowPixelFog = GL_TRUE;
664
665 /* Optimized Accum buffer */
666 swrast->_IntegerAccumMode = GL_FALSE;
667 swrast->_IntegerAccumScaler = 0.0;
668
669 for (i = 0; i < MAX_TEXTURE_IMAGE_UNITS; i++)
670 swrast->TextureSample[i] = NULL;
671
672 swrast->SpanArrays = MALLOC_STRUCT(span_arrays);
673 if (!swrast->SpanArrays) {
674 FREE(swrast);
675 return GL_FALSE;
676 }
677 swrast->SpanArrays->ChanType = CHAN_TYPE;
678 #if CHAN_TYPE == GL_UNSIGNED_BYTE
679 swrast->SpanArrays->rgba = swrast->SpanArrays->color.sz1.rgba;
680 swrast->SpanArrays->spec = swrast->SpanArrays->color.sz1.spec;
681 #elif CHAN_TYPE == GL_UNSIGNED_SHORT
682 swrast->SpanArrays->rgba = swrast->SpanArrays->color.sz2.rgba;
683 swrast->SpanArrays->spec = swrast->SpanArrays->color.sz2.spec;
684 #else
685 swrast->SpanArrays->rgba = swrast->SpanArrays->color.sz4.rgba;
686 swrast->SpanArrays->spec = swrast->SpanArrays->color.sz4.spec;
687 #endif
688
689 /* init point span buffer */
690 swrast->PointSpan.primitive = GL_POINT;
691 swrast->PointSpan.start = 0;
692 swrast->PointSpan.end = 0;
693 swrast->PointSpan.facing = 0;
694 swrast->PointSpan.array = swrast->SpanArrays;
695
696 swrast->TexelBuffer = (GLchan *) MALLOC(ctx->Const.MaxTextureImageUnits *
697 MAX_WIDTH * 4 * sizeof(GLchan));
698 if (!swrast->TexelBuffer) {
699 FREE(swrast->SpanArrays);
700 FREE(swrast);
701 return GL_FALSE;
702 }
703
704 ctx->swrast_context = swrast;
705
706 return GL_TRUE;
707 }
708
709 void
710 _swrast_DestroyContext( GLcontext *ctx )
711 {
712 SWcontext *swrast = SWRAST_CONTEXT(ctx);
713
714 if (SWRAST_DEBUG) {
715 _mesa_debug(ctx, "_swrast_DestroyContext\n");
716 }
717
718 FREE( swrast->SpanArrays );
719 FREE( swrast->TexelBuffer );
720 FREE( swrast );
721
722 ctx->swrast_context = 0;
723 }
724
725
726 struct swrast_device_driver *
727 _swrast_GetDeviceDriverReference( GLcontext *ctx )
728 {
729 SWcontext *swrast = SWRAST_CONTEXT(ctx);
730 return &swrast->Driver;
731 }
732
733 void
734 _swrast_flush( GLcontext *ctx )
735 {
736 SWcontext *swrast = SWRAST_CONTEXT(ctx);
737 /* flush any pending fragments from rendering points */
738 if (swrast->PointSpan.end > 0) {
739 if (ctx->Visual.rgbMode) {
740 _swrast_write_rgba_span(ctx, &(swrast->PointSpan));
741 }
742 else {
743 _swrast_write_index_span(ctx, &(swrast->PointSpan));
744 }
745 swrast->PointSpan.end = 0;
746 }
747 }
748
749 void
750 _swrast_render_primitive( GLcontext *ctx, GLenum prim )
751 {
752 SWcontext *swrast = SWRAST_CONTEXT(ctx);
753 if (swrast->Primitive == GL_POINTS && prim != GL_POINTS) {
754 _swrast_flush(ctx);
755 }
756 swrast->Primitive = prim;
757 }
758
759
760 void
761 _swrast_render_start( GLcontext *ctx )
762 {
763 SWcontext *swrast = SWRAST_CONTEXT(ctx);
764 if (swrast->Driver.SpanRenderStart)
765 swrast->Driver.SpanRenderStart( ctx );
766 swrast->PointSpan.end = 0;
767 }
768
769 void
770 _swrast_render_finish( GLcontext *ctx )
771 {
772 SWcontext *swrast = SWRAST_CONTEXT(ctx);
773 if (swrast->Driver.SpanRenderFinish)
774 swrast->Driver.SpanRenderFinish( ctx );
775
776 _swrast_flush(ctx);
777 }
778
779
780 #define SWRAST_DEBUG_VERTICES 0
781
782 void
783 _swrast_print_vertex( GLcontext *ctx, const SWvertex *v )
784 {
785 GLuint i;
786
787 if (SWRAST_DEBUG_VERTICES) {
788 _mesa_debug(ctx, "win %f %f %f %f\n",
789 v->win[0], v->win[1], v->win[2], v->win[3]);
790
791 for (i = 0 ; i < ctx->Const.MaxTextureCoordUnits ; i++)
792 if (ctx->Texture.Unit[i]._ReallyEnabled)
793 _mesa_debug(ctx, "texcoord[%d] %f %f %f %f\n", i,
794 v->texcoord[i][0], v->texcoord[i][1],
795 v->texcoord[i][2], v->texcoord[i][3]);
796
797 #if CHAN_TYPE == GL_FLOAT
798 _mesa_debug(ctx, "color %f %f %f %f\n",
799 v->color[0], v->color[1], v->color[2], v->color[3]);
800 _mesa_debug(ctx, "spec %f %f %f %f\n",
801 v->specular[0], v->specular[1],
802 v->specular[2], v->specular[3]);
803 #else
804 _mesa_debug(ctx, "color %d %d %d %d\n",
805 v->color[0], v->color[1], v->color[2], v->color[3]);
806 _mesa_debug(ctx, "spec %d %d %d %d\n",
807 v->specular[0], v->specular[1],
808 v->specular[2], v->specular[3]);
809 #endif
810 _mesa_debug(ctx, "fog %f\n", v->fog);
811 _mesa_debug(ctx, "index %d\n", v->index);
812 _mesa_debug(ctx, "pointsize %f\n", v->pointSize);
813 _mesa_debug(ctx, "\n");
814 }
815 }