update a couple MESA_FORMAT_Zxxx occurances
[mesa.git] / src / mesa / swrast / s_context.c
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 "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._Active) {
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 /* convert fog color to GLchan values */
203 CLAMPED_FLOAT_TO_CHAN(swrast->_FogColor[RCOMP], ctx->Fog.Color[RCOMP]);
204 CLAMPED_FLOAT_TO_CHAN(swrast->_FogColor[GCOMP], ctx->Fog.Color[GCOMP]);
205 CLAMPED_FLOAT_TO_CHAN(swrast->_FogColor[BCOMP], ctx->Fog.Color[BCOMP]);
206
207 /* determine if fog is needed, and if so, which fog mode */
208 swrast->_FogEnabled = GL_FALSE;
209 if (ctx->FragmentProgram._Active) {
210 if (ctx->FragmentProgram._Current->Base.Target==GL_FRAGMENT_PROGRAM_ARB) {
211 const struct fragment_program *p
212 = (struct fragment_program *) ctx->FragmentProgram._Current;
213 if (p->FogOption != GL_NONE) {
214 swrast->_FogEnabled = GL_TRUE;
215 swrast->_FogMode = p->FogOption;
216 }
217 }
218 }
219 else if (ctx->Fog.Enabled) {
220 swrast->_FogEnabled = GL_TRUE;
221 swrast->_FogMode = ctx->Fog.Mode;
222 }
223 }
224
225
226 /**
227 * Update state for running fragment programs. Basically, load the
228 * program parameters with current state values.
229 */
230 static void
231 _swrast_update_fragment_program( GLcontext *ctx )
232 {
233 if (ctx->FragmentProgram._Active) {
234 struct fragment_program *program = ctx->FragmentProgram._Current;
235 _mesa_load_state_parameters(ctx, program->Base.Parameters);
236 }
237 }
238
239
240
241 #define _SWRAST_NEW_DERIVED (_SWRAST_NEW_RASTERMASK | \
242 _NEW_TEXTURE | \
243 _NEW_HINT | \
244 _NEW_POLYGON )
245
246 /* State referenced by _swrast_choose_triangle, _swrast_choose_line.
247 */
248 #define _SWRAST_NEW_TRIANGLE (_SWRAST_NEW_DERIVED | \
249 _NEW_RENDERMODE| \
250 _NEW_POLYGON| \
251 _NEW_DEPTH| \
252 _NEW_STENCIL| \
253 _NEW_COLOR| \
254 _NEW_TEXTURE| \
255 _SWRAST_NEW_RASTERMASK| \
256 _NEW_LIGHT| \
257 _NEW_FOG | \
258 _DD_NEW_SEPARATE_SPECULAR)
259
260 #define _SWRAST_NEW_LINE (_SWRAST_NEW_DERIVED | \
261 _NEW_RENDERMODE| \
262 _NEW_LINE| \
263 _NEW_TEXTURE| \
264 _NEW_LIGHT| \
265 _NEW_FOG| \
266 _NEW_DEPTH | \
267 _DD_NEW_SEPARATE_SPECULAR)
268
269 #define _SWRAST_NEW_POINT (_SWRAST_NEW_DERIVED | \
270 _NEW_RENDERMODE | \
271 _NEW_POINT | \
272 _NEW_TEXTURE | \
273 _NEW_LIGHT | \
274 _NEW_FOG | \
275 _DD_NEW_SEPARATE_SPECULAR)
276
277 #define _SWRAST_NEW_TEXTURE_SAMPLE_FUNC _NEW_TEXTURE
278
279 #define _SWRAST_NEW_TEXTURE_ENV_MODE _NEW_TEXTURE
280
281 #define _SWRAST_NEW_BLEND_FUNC _NEW_COLOR
282
283
284
285 /**
286 * Stub for swrast->Triangle to select a true triangle function
287 * after a state change.
288 */
289 static void
290 _swrast_validate_triangle( GLcontext *ctx,
291 const SWvertex *v0,
292 const SWvertex *v1,
293 const SWvertex *v2 )
294 {
295 SWcontext *swrast = SWRAST_CONTEXT(ctx);
296
297 _swrast_validate_derived( ctx );
298 swrast->choose_triangle( ctx );
299
300 if (ctx->Texture._EnabledUnits == 0
301 && NEED_SECONDARY_COLOR(ctx)
302 && !ctx->FragmentProgram._Active) {
303 /* separate specular color, but no texture */
304 swrast->SpecTriangle = swrast->Triangle;
305 swrast->Triangle = _swrast_add_spec_terms_triangle;
306 }
307
308 swrast->Triangle( ctx, v0, v1, v2 );
309 }
310
311 /**
312 * Called via swrast->Line. Examine current GL state and choose a software
313 * line routine. Then call it.
314 */
315 static void
316 _swrast_validate_line( GLcontext *ctx, const SWvertex *v0, const SWvertex *v1 )
317 {
318 SWcontext *swrast = SWRAST_CONTEXT(ctx);
319
320 _swrast_validate_derived( ctx );
321 swrast->choose_line( ctx );
322
323 if (ctx->Texture._EnabledUnits == 0
324 && NEED_SECONDARY_COLOR(ctx)
325 && !ctx->FragmentProgram._Active) {
326 swrast->SpecLine = swrast->Line;
327 swrast->Line = _swrast_add_spec_terms_line;
328 }
329
330
331 swrast->Line( ctx, v0, v1 );
332 }
333
334 /**
335 * Called via swrast->Point. Examine current GL state and choose a software
336 * point routine. Then call it.
337 */
338 static void
339 _swrast_validate_point( GLcontext *ctx, const SWvertex *v0 )
340 {
341 SWcontext *swrast = SWRAST_CONTEXT(ctx);
342
343 _swrast_validate_derived( ctx );
344 swrast->choose_point( ctx );
345
346 if (ctx->Texture._EnabledUnits == 0
347 && NEED_SECONDARY_COLOR(ctx)
348 && !ctx->FragmentProgram._Active) {
349 swrast->SpecPoint = swrast->Point;
350 swrast->Point = _swrast_add_spec_terms_point;
351 }
352
353 swrast->Point( ctx, v0 );
354 }
355
356
357 /**
358 * Called via swrast->BlendFunc. Examine GL state to choose a blending
359 * function, then call it.
360 */
361 static void _ASMAPI
362 _swrast_validate_blend_func( GLcontext *ctx, GLuint n,
363 const GLubyte mask[],
364 GLchan src[][4],
365 CONST GLchan dst[][4] )
366 {
367 SWcontext *swrast = SWRAST_CONTEXT(ctx);
368
369 _swrast_validate_derived( ctx );
370 _swrast_choose_blend_func( ctx );
371
372 swrast->BlendFunc( ctx, n, mask, src, dst );
373 }
374
375
376 /**
377 * Make sure we have texture image data for all the textures we may need
378 * for subsequent rendering.
379 */
380 static void
381 _swrast_validate_texture_images(GLcontext *ctx)
382 {
383 SWcontext *swrast = SWRAST_CONTEXT(ctx);
384 GLuint u;
385
386 if (!swrast->ValidateTextureImage || !ctx->Texture._EnabledUnits) {
387 /* no textures enabled, or no way to validate images! */
388 return;
389 }
390
391 for (u = 0; u < ctx->Const.MaxTextureImageUnits; u++) {
392 if (ctx->Texture.Unit[u]._ReallyEnabled) {
393 struct gl_texture_object *texObj = ctx->Texture.Unit[u]._Current;
394 ASSERT(texObj);
395 if (texObj) {
396 GLuint numFaces = (texObj->Target == GL_TEXTURE_CUBE_MAP) ? 6 : 1;
397 GLuint face;
398 for (face = 0; face < numFaces; face++) {
399 GLuint lvl;
400 for (lvl = texObj->BaseLevel; lvl <= texObj->_MaxLevel; lvl++) {
401 struct gl_texture_image *texImg = texObj->Image[face][lvl];
402 if (texImg && !texImg->Data) {
403 swrast->ValidateTextureImage(ctx, texObj, face, lvl);
404 ASSERT(texObj->Image[face][lvl]->Data);
405 }
406 }
407 }
408 }
409 }
410 }
411 }
412
413
414 /**
415 * Free the texture image data attached to all currently enabled
416 * textures. Meant to be called by device drivers when transitioning
417 * from software to hardware rendering.
418 */
419 void
420 _swrast_eject_texture_images(GLcontext *ctx)
421 {
422 GLuint u;
423
424 if (!ctx->Texture._EnabledUnits) {
425 /* no textures enabled */
426 return;
427 }
428
429 for (u = 0; u < ctx->Const.MaxTextureImageUnits; u++) {
430 if (ctx->Texture.Unit[u]._ReallyEnabled) {
431 struct gl_texture_object *texObj = ctx->Texture.Unit[u]._Current;
432 ASSERT(texObj);
433 if (texObj) {
434 GLuint numFaces = (texObj->Target == GL_TEXTURE_CUBE_MAP) ? 6 : 1;
435 GLuint face;
436 for (face = 0; face < numFaces; face++) {
437 GLuint lvl;
438 for (lvl = texObj->BaseLevel; lvl <= texObj->_MaxLevel; lvl++) {
439 struct gl_texture_image *texImg = texObj->Image[face][lvl];
440 if (texImg && texImg->Data) {
441 _mesa_free_texmemory(texImg->Data);
442 texImg->Data = NULL;
443 }
444 }
445 }
446 }
447 }
448 }
449 }
450
451
452
453 static void
454 _swrast_sleep( GLcontext *ctx, GLbitfield new_state )
455 {
456 (void) ctx; (void) new_state;
457 }
458
459
460 static void
461 _swrast_invalidate_state( GLcontext *ctx, GLbitfield new_state )
462 {
463 SWcontext *swrast = SWRAST_CONTEXT(ctx);
464 GLuint i;
465
466 swrast->NewState |= new_state;
467
468 /* After 10 statechanges without any swrast functions being called,
469 * put the module to sleep.
470 */
471 if (++swrast->StateChanges > 10) {
472 swrast->InvalidateState = _swrast_sleep;
473 swrast->NewState = ~0;
474 new_state = ~0;
475 }
476
477 if (new_state & swrast->InvalidateTriangleMask)
478 swrast->Triangle = _swrast_validate_triangle;
479
480 if (new_state & swrast->InvalidateLineMask)
481 swrast->Line = _swrast_validate_line;
482
483 if (new_state & swrast->InvalidatePointMask)
484 swrast->Point = _swrast_validate_point;
485
486 if (new_state & _SWRAST_NEW_BLEND_FUNC)
487 swrast->BlendFunc = _swrast_validate_blend_func;
488
489 if (new_state & _SWRAST_NEW_TEXTURE_SAMPLE_FUNC)
490 for (i = 0 ; i < ctx->Const.MaxTextureUnits ; i++)
491 swrast->TextureSample[i] = NULL;
492 }
493
494
495 static void
496 _swrast_update_texture_samplers(GLcontext *ctx)
497 {
498 SWcontext *swrast = SWRAST_CONTEXT(ctx);
499 GLuint u;
500
501 for (u = 0; u < ctx->Const.MaxTextureImageUnits; u++) {
502 const struct gl_texture_object *tObj = ctx->Texture.Unit[u]._Current;
503 if (tObj)
504 swrast->TextureSample[u] =
505 _swrast_choose_texture_sample_func(ctx, tObj);
506 }
507 }
508
509
510 void
511 _swrast_validate_derived( GLcontext *ctx )
512 {
513 SWcontext *swrast = SWRAST_CONTEXT(ctx);
514
515 if (swrast->NewState) {
516 if (swrast->NewState & _SWRAST_NEW_RASTERMASK)
517 _swrast_update_rasterflags( ctx );
518
519 if (swrast->NewState & _NEW_POLYGON)
520 _swrast_update_polygon( ctx );
521
522 if (swrast->NewState & (_NEW_HINT | _NEW_PROGRAM))
523 _swrast_update_fog_hint( ctx );
524
525 if (swrast->NewState & _SWRAST_NEW_TEXTURE_ENV_MODE)
526 _swrast_update_texture_env( ctx );
527
528 if (swrast->NewState & (_NEW_FOG | _NEW_PROGRAM))
529 _swrast_update_fog_state( ctx );
530
531 if (swrast->NewState & _NEW_PROGRAM)
532 _swrast_update_fragment_program( ctx );
533
534 if (swrast->NewState & _NEW_TEXTURE)
535 _swrast_update_texture_samplers( ctx );
536
537 if (swrast->NewState & (_NEW_TEXTURE | _NEW_PROGRAM))
538 _swrast_validate_texture_images( ctx );
539
540 swrast->NewState = 0;
541 swrast->StateChanges = 0;
542 swrast->InvalidateState = _swrast_invalidate_state;
543 }
544 }
545
546 #define SWRAST_DEBUG 0
547
548 /* Public entrypoints: See also s_accum.c, s_bitmap.c, etc.
549 */
550 void
551 _swrast_Quad( GLcontext *ctx,
552 const SWvertex *v0, const SWvertex *v1,
553 const SWvertex *v2, const SWvertex *v3 )
554 {
555 if (SWRAST_DEBUG) {
556 _mesa_debug(ctx, "_swrast_Quad\n");
557 _swrast_print_vertex( ctx, v0 );
558 _swrast_print_vertex( ctx, v1 );
559 _swrast_print_vertex( ctx, v2 );
560 _swrast_print_vertex( ctx, v3 );
561 }
562 SWRAST_CONTEXT(ctx)->Triangle( ctx, v0, v1, v3 );
563 SWRAST_CONTEXT(ctx)->Triangle( ctx, v1, v2, v3 );
564 }
565
566 void
567 _swrast_Triangle( GLcontext *ctx, const SWvertex *v0,
568 const SWvertex *v1, const SWvertex *v2 )
569 {
570 if (SWRAST_DEBUG) {
571 _mesa_debug(ctx, "_swrast_Triangle\n");
572 _swrast_print_vertex( ctx, v0 );
573 _swrast_print_vertex( ctx, v1 );
574 _swrast_print_vertex( ctx, v2 );
575 }
576 SWRAST_CONTEXT(ctx)->Triangle( ctx, v0, v1, v2 );
577 }
578
579 void
580 _swrast_Line( GLcontext *ctx, const SWvertex *v0, const SWvertex *v1 )
581 {
582 if (SWRAST_DEBUG) {
583 _mesa_debug(ctx, "_swrast_Line\n");
584 _swrast_print_vertex( ctx, v0 );
585 _swrast_print_vertex( ctx, v1 );
586 }
587 SWRAST_CONTEXT(ctx)->Line( ctx, v0, v1 );
588 }
589
590 void
591 _swrast_Point( GLcontext *ctx, const SWvertex *v0 )
592 {
593 if (SWRAST_DEBUG) {
594 _mesa_debug(ctx, "_swrast_Point\n");
595 _swrast_print_vertex( ctx, v0 );
596 }
597 SWRAST_CONTEXT(ctx)->Point( ctx, v0 );
598 }
599
600 void
601 _swrast_InvalidateState( GLcontext *ctx, GLbitfield new_state )
602 {
603 if (SWRAST_DEBUG) {
604 _mesa_debug(ctx, "_swrast_InvalidateState\n");
605 }
606 SWRAST_CONTEXT(ctx)->InvalidateState( ctx, new_state );
607 }
608
609 void
610 _swrast_ResetLineStipple( GLcontext *ctx )
611 {
612 if (SWRAST_DEBUG) {
613 _mesa_debug(ctx, "_swrast_ResetLineStipple\n");
614 }
615 SWRAST_CONTEXT(ctx)->StippleCounter = 0;
616 }
617
618 void
619 _swrast_allow_vertex_fog( GLcontext *ctx, GLboolean value )
620 {
621 if (SWRAST_DEBUG) {
622 _mesa_debug(ctx, "_swrast_allow_vertex_fog %d\n", value);
623 }
624 SWRAST_CONTEXT(ctx)->InvalidateState( ctx, _NEW_HINT );
625 SWRAST_CONTEXT(ctx)->AllowVertexFog = value;
626 }
627
628 void
629 _swrast_allow_pixel_fog( GLcontext *ctx, GLboolean value )
630 {
631 if (SWRAST_DEBUG) {
632 _mesa_debug(ctx, "_swrast_allow_pixel_fog %d\n", value);
633 }
634 SWRAST_CONTEXT(ctx)->InvalidateState( ctx, _NEW_HINT );
635 SWRAST_CONTEXT(ctx)->AllowPixelFog = value;
636 }
637
638
639 GLboolean
640 _swrast_CreateContext( GLcontext *ctx )
641 {
642 GLuint i;
643 SWcontext *swrast = (SWcontext *)CALLOC(sizeof(SWcontext));
644
645 if (SWRAST_DEBUG) {
646 _mesa_debug(ctx, "_swrast_CreateContext\n");
647 }
648
649 if (!swrast)
650 return GL_FALSE;
651
652 swrast->NewState = ~0;
653
654 swrast->choose_point = _swrast_choose_point;
655 swrast->choose_line = _swrast_choose_line;
656 swrast->choose_triangle = _swrast_choose_triangle;
657
658 swrast->InvalidatePointMask = _SWRAST_NEW_POINT;
659 swrast->InvalidateLineMask = _SWRAST_NEW_LINE;
660 swrast->InvalidateTriangleMask = _SWRAST_NEW_TRIANGLE;
661
662 swrast->Point = _swrast_validate_point;
663 swrast->Line = _swrast_validate_line;
664 swrast->Triangle = _swrast_validate_triangle;
665 swrast->InvalidateState = _swrast_sleep;
666 swrast->BlendFunc = _swrast_validate_blend_func;
667
668 swrast->AllowVertexFog = GL_TRUE;
669 swrast->AllowPixelFog = GL_TRUE;
670
671 /* Optimized Accum buffer */
672 swrast->_IntegerAccumMode = GL_FALSE;
673 swrast->_IntegerAccumScaler = 0.0;
674
675 for (i = 0; i < MAX_TEXTURE_IMAGE_UNITS; i++)
676 swrast->TextureSample[i] = NULL;
677
678 swrast->SpanArrays = MALLOC_STRUCT(span_arrays);
679 if (!swrast->SpanArrays) {
680 FREE(swrast);
681 return GL_FALSE;
682 }
683
684 /* init point span buffer */
685 swrast->PointSpan.primitive = GL_POINT;
686 swrast->PointSpan.start = 0;
687 swrast->PointSpan.end = 0;
688 swrast->PointSpan.facing = 0;
689 swrast->PointSpan.array = swrast->SpanArrays;
690
691 assert(ctx->Const.MaxTextureUnits > 0);
692 assert(ctx->Const.MaxTextureUnits <= MAX_TEXTURE_UNITS);
693
694 swrast->TexelBuffer = (GLchan *) MALLOC(ctx->Const.MaxTextureUnits *
695 MAX_WIDTH * 4 * sizeof(GLchan));
696 if (!swrast->TexelBuffer) {
697 FREE(swrast->SpanArrays);
698 FREE(swrast);
699 return GL_FALSE;
700 }
701
702 ctx->swrast_context = swrast;
703
704 return GL_TRUE;
705 }
706
707 void
708 _swrast_DestroyContext( GLcontext *ctx )
709 {
710 SWcontext *swrast = SWRAST_CONTEXT(ctx);
711
712 if (SWRAST_DEBUG) {
713 _mesa_debug(ctx, "_swrast_DestroyContext\n");
714 }
715
716 FREE( swrast->SpanArrays );
717 FREE( swrast->TexelBuffer );
718 FREE( swrast );
719
720 ctx->swrast_context = 0;
721 }
722
723
724 struct swrast_device_driver *
725 _swrast_GetDeviceDriverReference( GLcontext *ctx )
726 {
727 SWcontext *swrast = SWRAST_CONTEXT(ctx);
728 return &swrast->Driver;
729 }
730
731 void
732 _swrast_flush( GLcontext *ctx )
733 {
734 SWcontext *swrast = SWRAST_CONTEXT(ctx);
735 /* flush any pending fragments from rendering points */
736 if (swrast->PointSpan.end > 0) {
737 if (ctx->Visual.rgbMode) {
738 _swrast_write_rgba_span(ctx, &(swrast->PointSpan));
739 }
740 else {
741 _swrast_write_index_span(ctx, &(swrast->PointSpan));
742 }
743 swrast->PointSpan.end = 0;
744 }
745 }
746
747 void
748 _swrast_render_primitive( GLcontext *ctx, GLenum prim )
749 {
750 SWcontext *swrast = SWRAST_CONTEXT(ctx);
751 if (swrast->Primitive == GL_POINTS && prim != GL_POINTS) {
752 _swrast_flush(ctx);
753 }
754 swrast->Primitive = prim;
755 }
756
757
758 void
759 _swrast_render_start( GLcontext *ctx )
760 {
761 SWcontext *swrast = SWRAST_CONTEXT(ctx);
762 if (swrast->Driver.SpanRenderStart)
763 swrast->Driver.SpanRenderStart( ctx );
764 swrast->PointSpan.end = 0;
765 }
766
767 void
768 _swrast_render_finish( GLcontext *ctx )
769 {
770 SWcontext *swrast = SWRAST_CONTEXT(ctx);
771 if (swrast->Driver.SpanRenderFinish)
772 swrast->Driver.SpanRenderFinish( ctx );
773
774 _swrast_flush(ctx);
775 }
776
777
778 #define SWRAST_DEBUG_VERTICES 0
779
780 void
781 _swrast_print_vertex( GLcontext *ctx, const SWvertex *v )
782 {
783 GLuint i;
784
785 if (SWRAST_DEBUG_VERTICES) {
786 _mesa_debug(ctx, "win %f %f %f %f\n",
787 v->win[0], v->win[1], v->win[2], v->win[3]);
788
789 for (i = 0 ; i < ctx->Const.MaxTextureUnits ; i++)
790 if (ctx->Texture.Unit[i]._ReallyEnabled)
791 _mesa_debug(ctx, "texcoord[%d] %f %f %f %f\n", i,
792 v->texcoord[i][0], v->texcoord[i][1],
793 v->texcoord[i][2], v->texcoord[i][3]);
794
795 #if CHAN_TYPE == GL_FLOAT
796 _mesa_debug(ctx, "color %f %f %f %f\n",
797 v->color[0], v->color[1], v->color[2], v->color[3]);
798 _mesa_debug(ctx, "spec %f %f %f %f\n",
799 v->specular[0], v->specular[1],
800 v->specular[2], v->specular[3]);
801 #else
802 _mesa_debug(ctx, "color %d %d %d %d\n",
803 v->color[0], v->color[1], v->color[2], v->color[3]);
804 _mesa_debug(ctx, "spec %d %d %d %d\n",
805 v->specular[0], v->specular[1],
806 v->specular[2], v->specular[3]);
807 #endif
808 _mesa_debug(ctx, "fog %f\n", v->fog);
809 _mesa_debug(ctx, "index %d\n", v->index);
810 _mesa_debug(ctx, "pointsize %f\n", v->pointSize);
811 _mesa_debug(ctx, "\n");
812 }
813 }