move mesa32 over to new dir
[reactos.git] / reactos / lib / mesa32 / src / 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 (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 */
120 static 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 */
154 static 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 */
169 static 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 */
188 static 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 */
221 static 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 */
280 static 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 */
306 static 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 */
329 static 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 */
352 static 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 */
375 static 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
405 static void
406 _swrast_sleep( GLcontext *ctx, GLuint new_state )
407 {
408 (void) ctx; (void) new_state;
409 }
410
411
412 static 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
447 void
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 */
481 void
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
497 void
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
510 void
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
521 void
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
531 void
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
540 void
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
549 void
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
559 void
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
570 GLboolean
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 if (ctx->Visual.doubleBufferMode)
603 swrast->CurrentBufferBit = BUFFER_BIT_BACK_LEFT;
604 else
605 swrast->CurrentBufferBit = BUFFER_FRONT_LEFT;
606
607 /* Optimized Accum buffer */
608 swrast->_IntegerAccumMode = GL_FALSE;
609 swrast->_IntegerAccumScaler = 0.0;
610
611 for (i = 0; i < MAX_TEXTURE_IMAGE_UNITS; i++)
612 swrast->TextureSample[i] = _swrast_validate_texture_sample;
613
614 swrast->SpanArrays = MALLOC_STRUCT(span_arrays);
615 if (!swrast->SpanArrays) {
616 FREE(swrast);
617 return GL_FALSE;
618 }
619
620 /* init point span buffer */
621 swrast->PointSpan.primitive = GL_POINT;
622 swrast->PointSpan.start = 0;
623 swrast->PointSpan.end = 0;
624 swrast->PointSpan.facing = 0;
625 swrast->PointSpan.array = swrast->SpanArrays;
626
627 assert(ctx->Const.MaxTextureUnits > 0);
628 assert(ctx->Const.MaxTextureUnits <= MAX_TEXTURE_UNITS);
629
630 swrast->TexelBuffer = (GLchan *) MALLOC(ctx->Const.MaxTextureUnits *
631 MAX_WIDTH * 4 * sizeof(GLchan));
632 if (!swrast->TexelBuffer) {
633 FREE(swrast->SpanArrays);
634 FREE(swrast);
635 return GL_FALSE;
636 }
637
638 ctx->swrast_context = swrast;
639
640 return GL_TRUE;
641 }
642
643 void
644 _swrast_DestroyContext( GLcontext *ctx )
645 {
646 SWcontext *swrast = SWRAST_CONTEXT(ctx);
647
648 if (SWRAST_DEBUG) {
649 _mesa_debug(ctx, "_swrast_DestroyContext\n");
650 }
651
652 FREE( swrast->SpanArrays );
653 FREE( swrast->TexelBuffer );
654 FREE( swrast );
655
656 ctx->swrast_context = 0;
657 }
658
659
660 struct swrast_device_driver *
661 _swrast_GetDeviceDriverReference( GLcontext *ctx )
662 {
663 SWcontext *swrast = SWRAST_CONTEXT(ctx);
664 return &swrast->Driver;
665 }
666
667 void
668 _swrast_flush( GLcontext *ctx )
669 {
670 SWcontext *swrast = SWRAST_CONTEXT(ctx);
671 /* flush any pending fragments from rendering points */
672 if (swrast->PointSpan.end > 0) {
673 if (ctx->Visual.rgbMode) {
674 _swrast_write_rgba_span(ctx, &(swrast->PointSpan));
675 }
676 else {
677 _swrast_write_index_span(ctx, &(swrast->PointSpan));
678 }
679 swrast->PointSpan.end = 0;
680 }
681 }
682
683 void
684 _swrast_render_primitive( GLcontext *ctx, GLenum prim )
685 {
686 SWcontext *swrast = SWRAST_CONTEXT(ctx);
687 if (swrast->Primitive == GL_POINTS && prim != GL_POINTS) {
688 _swrast_flush(ctx);
689 }
690 swrast->Primitive = prim;
691 }
692
693
694 void
695 _swrast_render_start( GLcontext *ctx )
696 {
697 SWcontext *swrast = SWRAST_CONTEXT(ctx);
698 if (swrast->Driver.SpanRenderStart)
699 swrast->Driver.SpanRenderStart( ctx );
700 swrast->PointSpan.end = 0;
701 }
702
703 void
704 _swrast_render_finish( GLcontext *ctx )
705 {
706 SWcontext *swrast = SWRAST_CONTEXT(ctx);
707 if (swrast->Driver.SpanRenderFinish)
708 swrast->Driver.SpanRenderFinish( ctx );
709
710 _swrast_flush(ctx);
711 }
712
713
714 #define SWRAST_DEBUG_VERTICES 0
715
716 void
717 _swrast_print_vertex( GLcontext *ctx, const SWvertex *v )
718 {
719 GLuint i;
720
721 if (SWRAST_DEBUG_VERTICES) {
722 _mesa_debug(ctx, "win %f %f %f %f\n",
723 v->win[0], v->win[1], v->win[2], v->win[3]);
724
725 for (i = 0 ; i < ctx->Const.MaxTextureUnits ; i++)
726 if (ctx->Texture.Unit[i]._ReallyEnabled)
727 _mesa_debug(ctx, "texcoord[%d] %f %f %f %f\n", i,
728 v->texcoord[i][0], v->texcoord[i][1],
729 v->texcoord[i][2], v->texcoord[i][3]);
730
731 #if CHAN_TYPE == GL_FLOAT
732 _mesa_debug(ctx, "color %f %f %f %f\n",
733 v->color[0], v->color[1], v->color[2], v->color[3]);
734 _mesa_debug(ctx, "spec %f %f %f %f\n",
735 v->specular[0], v->specular[1],
736 v->specular[2], v->specular[3]);
737 #else
738 _mesa_debug(ctx, "color %d %d %d %d\n",
739 v->color[0], v->color[1], v->color[2], v->color[3]);
740 _mesa_debug(ctx, "spec %d %d %d %d\n",
741 v->specular[0], v->specular[1],
742 v->specular[2], v->specular[3]);
743 #endif
744 _mesa_debug(ctx, "fog %f\n", v->fog);
745 _mesa_debug(ctx, "index %d\n", v->index);
746 _mesa_debug(ctx, "pointsize %f\n", v->pointSize);
747 _mesa_debug(ctx, "\n");
748 }
749 }