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