mesa: allocate pixel zoom arrays on heap, not stack
[mesa.git] / src / mesa / swrast / s_context.c
1 /*
2 * Mesa 3-D graphics library
3 * Version: 7.1
4 *
5 * Copyright (C) 1999-2007 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 "teximage.h"
35 #include "swrast.h"
36 #include "shader/prog_statevars.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 != 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._Current) {
102 rasterMask |= FRAGPROG_BIT;
103 }
104
105 if (ctx->ATIFragmentShader._Enabled) {
106 rasterMask |= ATIFRAGSHADER_BIT;
107 }
108
109 #if CHAN_TYPE == GL_FLOAT
110 if (ctx->Color.ClampFragmentColor == GL_TRUE) {
111 rasterMask |= CLAMPING_BIT;
112 }
113 #endif
114
115 SWRAST_CONTEXT(ctx)->_RasterMask = rasterMask;
116 }
117
118
119 /**
120 * Examine polygon cull state to compute the _BackfaceCullSign field.
121 * _BackfaceCullSign 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;
129
130 if (ctx->Polygon.CullFlag) {
131 switch (ctx->Polygon.CullFaceMode) {
132 case GL_BACK:
133 backface_sign = -1.0;
134 break;
135 case GL_FRONT:
136 backface_sign = 1.0;
137 break;
138 case GL_FRONT_AND_BACK:
139 /* fallthrough */
140 default:
141 backface_sign = 0.0;
142 }
143 }
144 else {
145 backface_sign = 0.0;
146 }
147
148 SWRAST_CONTEXT(ctx)->_BackfaceCullSign = backface_sign;
149
150 /* This is for front/back-face determination, but not for culling */
151 SWRAST_CONTEXT(ctx)->_BackfaceSign
152 = (ctx->Polygon.FrontFace == GL_CW) ? -1.0 : 1.0;
153 }
154
155
156
157 /**
158 * Update the _PreferPixelFog field to indicate if we need to compute
159 * fog blend factors (from the fog coords) per-fragment.
160 */
161 static void
162 _swrast_update_fog_hint( GLcontext *ctx )
163 {
164 SWcontext *swrast = SWRAST_CONTEXT(ctx);
165 swrast->_PreferPixelFog = (!swrast->AllowVertexFog ||
166 ctx->FragmentProgram._Current ||
167 (ctx->Hint.Fog == GL_NICEST &&
168 swrast->AllowPixelFog));
169 }
170
171
172
173 /**
174 * Update the swrast->_AnyTextureCombine flag.
175 */
176 static void
177 _swrast_update_texture_env( GLcontext *ctx )
178 {
179 SWcontext *swrast = SWRAST_CONTEXT(ctx);
180 GLuint i;
181 swrast->_AnyTextureCombine = GL_FALSE;
182 for (i = 0; i < ctx->Const.MaxTextureUnits; i++) {
183 if (ctx->Texture.Unit[i].EnvMode == GL_COMBINE_EXT ||
184 ctx->Texture.Unit[i].EnvMode == GL_COMBINE4_NV) {
185 swrast->_AnyTextureCombine = GL_TRUE;
186 return;
187 }
188 }
189 }
190
191
192 /**
193 * Determine if we can defer texturing/shading until after Z/stencil
194 * testing. This potentially allows us to skip texturing/shading for
195 * lots of fragments.
196 */
197 static void
198 _swrast_update_deferred_texture(GLcontext *ctx)
199 {
200 SWcontext *swrast = SWRAST_CONTEXT(ctx);
201 if (ctx->Color.AlphaEnabled) {
202 /* alpha test depends on post-texture/shader colors */
203 swrast->_DeferredTexture = GL_FALSE;
204 }
205 else {
206 const struct gl_fragment_program *fprog
207 = ctx->FragmentProgram._Current;
208 if (fprog && (fprog->Base.OutputsWritten & (1 << FRAG_RESULT_DEPR))) {
209 /* Z comes from fragment program/shader */
210 swrast->_DeferredTexture = GL_FALSE;
211 }
212 else if (ctx->Query.CurrentOcclusionObject) {
213 /* occlusion query depends on shader discard/kill results */
214 swrast->_DeferredTexture = GL_FALSE;
215 }
216 else {
217 swrast->_DeferredTexture = GL_TRUE;
218 }
219 }
220 }
221
222
223 /**
224 * Update swrast->_FogColor and swrast->_FogEnable values.
225 */
226 static void
227 _swrast_update_fog_state( GLcontext *ctx )
228 {
229 SWcontext *swrast = SWRAST_CONTEXT(ctx);
230 const struct gl_fragment_program *fp = ctx->FragmentProgram._Current;
231
232 /* determine if fog is needed, and if so, which fog mode */
233 swrast->_FogEnabled = GL_FALSE;
234 if (fp && fp->Base.Target == GL_FRAGMENT_PROGRAM_ARB) {
235 if (fp->FogOption != GL_NONE) {
236 swrast->_FogEnabled = GL_TRUE;
237 swrast->_FogMode = fp->FogOption;
238 }
239 }
240 else if (ctx->Fog.Enabled) {
241 swrast->_FogEnabled = GL_TRUE;
242 swrast->_FogMode = ctx->Fog.Mode;
243 }
244 }
245
246
247 /**
248 * Update state for running fragment programs. Basically, load the
249 * program parameters with current state values.
250 */
251 static void
252 _swrast_update_fragment_program(GLcontext *ctx, GLbitfield newState)
253 {
254 const struct gl_fragment_program *fp = ctx->FragmentProgram._Current;
255 if (fp) {
256 #if 0
257 /* XXX Need a way to trigger the initial loading of parameters
258 * even when there's no recent state changes.
259 */
260 if (fp->Base.Parameters->StateFlags & newState)
261 #endif
262 _mesa_load_state_parameters(ctx, fp->Base.Parameters);
263 }
264 }
265
266
267
268 #define _SWRAST_NEW_DERIVED (_SWRAST_NEW_RASTERMASK | \
269 _NEW_TEXTURE | \
270 _NEW_HINT | \
271 _NEW_POLYGON )
272
273 /* State referenced by _swrast_choose_triangle, _swrast_choose_line.
274 */
275 #define _SWRAST_NEW_TRIANGLE (_SWRAST_NEW_DERIVED | \
276 _NEW_RENDERMODE| \
277 _NEW_POLYGON| \
278 _NEW_DEPTH| \
279 _NEW_STENCIL| \
280 _NEW_COLOR| \
281 _NEW_TEXTURE| \
282 _SWRAST_NEW_RASTERMASK| \
283 _NEW_LIGHT| \
284 _NEW_FOG | \
285 _DD_NEW_SEPARATE_SPECULAR)
286
287 #define _SWRAST_NEW_LINE (_SWRAST_NEW_DERIVED | \
288 _NEW_RENDERMODE| \
289 _NEW_LINE| \
290 _NEW_TEXTURE| \
291 _NEW_LIGHT| \
292 _NEW_FOG| \
293 _NEW_DEPTH | \
294 _DD_NEW_SEPARATE_SPECULAR)
295
296 #define _SWRAST_NEW_POINT (_SWRAST_NEW_DERIVED | \
297 _NEW_RENDERMODE | \
298 _NEW_POINT | \
299 _NEW_TEXTURE | \
300 _NEW_LIGHT | \
301 _NEW_FOG | \
302 _DD_NEW_SEPARATE_SPECULAR)
303
304 #define _SWRAST_NEW_TEXTURE_SAMPLE_FUNC _NEW_TEXTURE
305
306 #define _SWRAST_NEW_TEXTURE_ENV_MODE _NEW_TEXTURE
307
308 #define _SWRAST_NEW_BLEND_FUNC _NEW_COLOR
309
310
311
312 /**
313 * Stub for swrast->Triangle to select a true triangle function
314 * after a state change.
315 */
316 static void
317 _swrast_validate_triangle( GLcontext *ctx,
318 const SWvertex *v0,
319 const SWvertex *v1,
320 const SWvertex *v2 )
321 {
322 SWcontext *swrast = SWRAST_CONTEXT(ctx);
323
324 _swrast_validate_derived( ctx );
325 swrast->choose_triangle( ctx );
326 ASSERT(swrast->Triangle);
327
328 if (ctx->Texture._EnabledUnits == 0
329 && NEED_SECONDARY_COLOR(ctx)
330 && !ctx->FragmentProgram._Current) {
331 /* separate specular color, but no texture */
332 swrast->SpecTriangle = swrast->Triangle;
333 swrast->Triangle = _swrast_add_spec_terms_triangle;
334 }
335
336 swrast->Triangle( ctx, v0, v1, v2 );
337 }
338
339 /**
340 * Called via swrast->Line. Examine current GL state and choose a software
341 * line routine. Then call it.
342 */
343 static void
344 _swrast_validate_line( GLcontext *ctx, const SWvertex *v0, const SWvertex *v1 )
345 {
346 SWcontext *swrast = SWRAST_CONTEXT(ctx);
347
348 _swrast_validate_derived( ctx );
349 swrast->choose_line( ctx );
350 ASSERT(swrast->Line);
351
352 if (ctx->Texture._EnabledUnits == 0
353 && NEED_SECONDARY_COLOR(ctx)
354 && !ctx->FragmentProgram._Current) {
355 swrast->SpecLine = swrast->Line;
356 swrast->Line = _swrast_add_spec_terms_line;
357 }
358
359 swrast->Line( ctx, v0, v1 );
360 }
361
362 /**
363 * Called via swrast->Point. Examine current GL state and choose a software
364 * point routine. Then call it.
365 */
366 static void
367 _swrast_validate_point( GLcontext *ctx, const SWvertex *v0 )
368 {
369 SWcontext *swrast = SWRAST_CONTEXT(ctx);
370
371 _swrast_validate_derived( ctx );
372 swrast->choose_point( ctx );
373
374 if (ctx->Texture._EnabledUnits == 0
375 && NEED_SECONDARY_COLOR(ctx)
376 && !ctx->FragmentProgram._Current) {
377 swrast->SpecPoint = swrast->Point;
378 swrast->Point = _swrast_add_spec_terms_point;
379 }
380
381 swrast->Point( ctx, v0 );
382 }
383
384
385 /**
386 * Called via swrast->BlendFunc. Examine GL state to choose a blending
387 * function, then call it.
388 */
389 static void _ASMAPI
390 _swrast_validate_blend_func(GLcontext *ctx, GLuint n, const GLubyte mask[],
391 GLvoid *src, const GLvoid *dst,
392 GLenum chanType )
393 {
394 SWcontext *swrast = SWRAST_CONTEXT(ctx);
395
396 _swrast_validate_derived( ctx ); /* why is this needed? */
397 _swrast_choose_blend_func( ctx, chanType );
398
399 swrast->BlendFunc( ctx, n, mask, src, dst, chanType );
400 }
401
402
403 /**
404 * Make sure we have texture image data for all the textures we may need
405 * for subsequent rendering.
406 */
407 static void
408 _swrast_validate_texture_images(GLcontext *ctx)
409 {
410 SWcontext *swrast = SWRAST_CONTEXT(ctx);
411 GLuint u;
412
413 if (!swrast->ValidateTextureImage || !ctx->Texture._EnabledUnits) {
414 /* no textures enabled, or no way to validate images! */
415 return;
416 }
417
418 for (u = 0; u < ctx->Const.MaxTextureImageUnits; u++) {
419 if (ctx->Texture.Unit[u]._ReallyEnabled) {
420 struct gl_texture_object *texObj = ctx->Texture.Unit[u]._Current;
421 ASSERT(texObj);
422 if (texObj) {
423 GLuint numFaces = (texObj->Target == GL_TEXTURE_CUBE_MAP) ? 6 : 1;
424 GLuint face;
425 for (face = 0; face < numFaces; face++) {
426 GLint lvl;
427 for (lvl = texObj->BaseLevel; lvl <= texObj->_MaxLevel; lvl++) {
428 struct gl_texture_image *texImg = texObj->Image[face][lvl];
429 if (texImg && !texImg->Data) {
430 swrast->ValidateTextureImage(ctx, texObj, face, lvl);
431 ASSERT(texObj->Image[face][lvl]->Data);
432 }
433 }
434 }
435 }
436 }
437 }
438 }
439
440
441 /**
442 * Free the texture image data attached to all currently enabled
443 * textures. Meant to be called by device drivers when transitioning
444 * from software to hardware rendering.
445 */
446 void
447 _swrast_eject_texture_images(GLcontext *ctx)
448 {
449 GLuint u;
450
451 if (!ctx->Texture._EnabledUnits) {
452 /* no textures enabled */
453 return;
454 }
455
456 for (u = 0; u < ctx->Const.MaxTextureImageUnits; u++) {
457 if (ctx->Texture.Unit[u]._ReallyEnabled) {
458 struct gl_texture_object *texObj = ctx->Texture.Unit[u]._Current;
459 ASSERT(texObj);
460 if (texObj) {
461 GLuint numFaces = (texObj->Target == GL_TEXTURE_CUBE_MAP) ? 6 : 1;
462 GLuint face;
463 for (face = 0; face < numFaces; face++) {
464 GLint lvl;
465 for (lvl = texObj->BaseLevel; lvl <= texObj->_MaxLevel; lvl++) {
466 struct gl_texture_image *texImg = texObj->Image[face][lvl];
467 if (texImg && texImg->Data) {
468 _mesa_free_texmemory(texImg->Data);
469 texImg->Data = NULL;
470 }
471 }
472 }
473 }
474 }
475 }
476 }
477
478
479
480 static void
481 _swrast_sleep( GLcontext *ctx, GLbitfield new_state )
482 {
483 (void) ctx; (void) new_state;
484 }
485
486
487 static void
488 _swrast_invalidate_state( GLcontext *ctx, GLbitfield new_state )
489 {
490 SWcontext *swrast = SWRAST_CONTEXT(ctx);
491 GLuint i;
492
493 swrast->NewState |= new_state;
494
495 /* After 10 statechanges without any swrast functions being called,
496 * put the module to sleep.
497 */
498 if (++swrast->StateChanges > 10) {
499 swrast->InvalidateState = _swrast_sleep;
500 swrast->NewState = ~0;
501 new_state = ~0;
502 }
503
504 if (new_state & swrast->InvalidateTriangleMask)
505 swrast->Triangle = _swrast_validate_triangle;
506
507 if (new_state & swrast->InvalidateLineMask)
508 swrast->Line = _swrast_validate_line;
509
510 if (new_state & swrast->InvalidatePointMask)
511 swrast->Point = _swrast_validate_point;
512
513 if (new_state & _SWRAST_NEW_BLEND_FUNC)
514 swrast->BlendFunc = _swrast_validate_blend_func;
515
516 if (new_state & _SWRAST_NEW_TEXTURE_SAMPLE_FUNC)
517 for (i = 0 ; i < ctx->Const.MaxTextureImageUnits ; i++)
518 swrast->TextureSample[i] = NULL;
519 }
520
521
522 void
523 _swrast_update_texture_samplers(GLcontext *ctx)
524 {
525 SWcontext *swrast = SWRAST_CONTEXT(ctx);
526 GLuint u;
527
528 for (u = 0; u < ctx->Const.MaxTextureImageUnits; u++) {
529 const struct gl_texture_object *tObj = ctx->Texture.Unit[u]._Current;
530 /* Note: If tObj is NULL, the sample function will be a simple
531 * function that just returns opaque black (0,0,0,1).
532 */
533 swrast->TextureSample[u] = _swrast_choose_texture_sample_func(ctx, tObj);
534 }
535 }
536
537
538 /**
539 * Update swrast->_ActiveAttribs, swrast->_NumActiveAttribs,
540 * swrast->_ActiveAtttribMask.
541 */
542 static void
543 _swrast_update_active_attribs(GLcontext *ctx)
544 {
545 SWcontext *swrast = SWRAST_CONTEXT(ctx);
546 GLuint attribsMask;
547
548 /*
549 * Compute _ActiveAttribsMask = which fragment attributes are needed.
550 */
551 if (ctx->FragmentProgram._Current) {
552 /* fragment program/shader */
553 attribsMask = ctx->FragmentProgram._Current->Base.InputsRead;
554 attribsMask &= ~FRAG_BIT_WPOS; /* WPOS is always handled specially */
555 }
556 else if (ctx->ATIFragmentShader._Enabled) {
557 attribsMask = ~0; /* XXX fix me */
558 }
559 else {
560 /* fixed function */
561 attribsMask = 0x0;
562
563 #if CHAN_TYPE == GL_FLOAT
564 attribsMask |= FRAG_BIT_COL0;
565 #endif
566
567 if (ctx->Fog.ColorSumEnabled ||
568 (ctx->Light.Enabled &&
569 ctx->Light.Model.ColorControl == GL_SEPARATE_SPECULAR_COLOR)) {
570 attribsMask |= FRAG_BIT_COL1;
571 }
572
573 if (swrast->_FogEnabled)
574 attribsMask |= FRAG_BIT_FOGC;
575
576 attribsMask |= (ctx->Texture._EnabledUnits << FRAG_ATTRIB_TEX0);
577 }
578
579 swrast->_ActiveAttribMask = attribsMask;
580
581 /* Update _ActiveAttribs[] list */
582 {
583 GLuint i, num = 0;
584 for (i = 0; i < FRAG_ATTRIB_MAX; i++) {
585 if (attribsMask & (1 << i)) {
586 swrast->_ActiveAttribs[num++] = i;
587 /* how should this attribute be interpolated? */
588 if (i == FRAG_ATTRIB_COL0 || i == FRAG_ATTRIB_COL1)
589 swrast->_InterpMode[i] = ctx->Light.ShadeModel;
590 else
591 swrast->_InterpMode[i] = GL_SMOOTH;
592 }
593 }
594 swrast->_NumActiveAttribs = num;
595 }
596 }
597
598
599 void
600 _swrast_validate_derived( GLcontext *ctx )
601 {
602 SWcontext *swrast = SWRAST_CONTEXT(ctx);
603
604 if (swrast->NewState) {
605 if (swrast->NewState & _NEW_POLYGON)
606 _swrast_update_polygon( ctx );
607
608 if (swrast->NewState & (_NEW_HINT | _NEW_PROGRAM))
609 _swrast_update_fog_hint( ctx );
610
611 if (swrast->NewState & _SWRAST_NEW_TEXTURE_ENV_MODE)
612 _swrast_update_texture_env( ctx );
613
614 if (swrast->NewState & (_NEW_FOG | _NEW_PROGRAM))
615 _swrast_update_fog_state( ctx );
616
617 if (swrast->NewState & (_NEW_MODELVIEW |
618 _NEW_PROJECTION |
619 _NEW_TEXTURE_MATRIX |
620 _NEW_FOG |
621 _NEW_LIGHT |
622 _NEW_LINE |
623 _NEW_TEXTURE |
624 _NEW_TRANSFORM |
625 _NEW_POINT |
626 _NEW_VIEWPORT |
627 _NEW_PROGRAM))
628 _swrast_update_fragment_program( ctx, swrast->NewState );
629
630 if (swrast->NewState & (_NEW_TEXTURE | _NEW_PROGRAM)) {
631 _swrast_update_texture_samplers( ctx );
632 _swrast_validate_texture_images(ctx);
633 }
634
635 if (swrast->NewState & (_NEW_COLOR | _NEW_PROGRAM))
636 _swrast_update_deferred_texture(ctx);
637
638 if (swrast->NewState & _SWRAST_NEW_RASTERMASK)
639 _swrast_update_rasterflags( ctx );
640
641 if (swrast->NewState & (_NEW_DEPTH |
642 _NEW_FOG |
643 _NEW_LIGHT |
644 _NEW_PROGRAM |
645 _NEW_TEXTURE))
646 _swrast_update_active_attribs(ctx);
647
648 swrast->NewState = 0;
649 swrast->StateChanges = 0;
650 swrast->InvalidateState = _swrast_invalidate_state;
651 }
652 }
653
654 #define SWRAST_DEBUG 0
655
656 /* Public entrypoints: See also s_accum.c, s_bitmap.c, etc.
657 */
658 void
659 _swrast_Quad( GLcontext *ctx,
660 const SWvertex *v0, const SWvertex *v1,
661 const SWvertex *v2, const SWvertex *v3 )
662 {
663 if (SWRAST_DEBUG) {
664 _mesa_debug(ctx, "_swrast_Quad\n");
665 _swrast_print_vertex( ctx, v0 );
666 _swrast_print_vertex( ctx, v1 );
667 _swrast_print_vertex( ctx, v2 );
668 _swrast_print_vertex( ctx, v3 );
669 }
670 SWRAST_CONTEXT(ctx)->Triangle( ctx, v0, v1, v3 );
671 SWRAST_CONTEXT(ctx)->Triangle( ctx, v1, v2, v3 );
672 }
673
674 void
675 _swrast_Triangle( GLcontext *ctx, const SWvertex *v0,
676 const SWvertex *v1, const SWvertex *v2 )
677 {
678 if (SWRAST_DEBUG) {
679 _mesa_debug(ctx, "_swrast_Triangle\n");
680 _swrast_print_vertex( ctx, v0 );
681 _swrast_print_vertex( ctx, v1 );
682 _swrast_print_vertex( ctx, v2 );
683 }
684 SWRAST_CONTEXT(ctx)->Triangle( ctx, v0, v1, v2 );
685 }
686
687 void
688 _swrast_Line( GLcontext *ctx, const SWvertex *v0, const SWvertex *v1 )
689 {
690 if (SWRAST_DEBUG) {
691 _mesa_debug(ctx, "_swrast_Line\n");
692 _swrast_print_vertex( ctx, v0 );
693 _swrast_print_vertex( ctx, v1 );
694 }
695 SWRAST_CONTEXT(ctx)->Line( ctx, v0, v1 );
696 }
697
698 void
699 _swrast_Point( GLcontext *ctx, const SWvertex *v0 )
700 {
701 if (SWRAST_DEBUG) {
702 _mesa_debug(ctx, "_swrast_Point\n");
703 _swrast_print_vertex( ctx, v0 );
704 }
705 SWRAST_CONTEXT(ctx)->Point( ctx, v0 );
706 }
707
708 void
709 _swrast_InvalidateState( GLcontext *ctx, GLbitfield new_state )
710 {
711 if (SWRAST_DEBUG) {
712 _mesa_debug(ctx, "_swrast_InvalidateState\n");
713 }
714 SWRAST_CONTEXT(ctx)->InvalidateState( ctx, new_state );
715 }
716
717 void
718 _swrast_ResetLineStipple( GLcontext *ctx )
719 {
720 if (SWRAST_DEBUG) {
721 _mesa_debug(ctx, "_swrast_ResetLineStipple\n");
722 }
723 SWRAST_CONTEXT(ctx)->StippleCounter = 0;
724 }
725
726 void
727 _swrast_SetFacing(GLcontext *ctx, GLuint facing)
728 {
729 SWRAST_CONTEXT(ctx)->PointLineFacing = facing;
730 }
731
732 void
733 _swrast_allow_vertex_fog( GLcontext *ctx, GLboolean value )
734 {
735 if (SWRAST_DEBUG) {
736 _mesa_debug(ctx, "_swrast_allow_vertex_fog %d\n", value);
737 }
738 SWRAST_CONTEXT(ctx)->InvalidateState( ctx, _NEW_HINT );
739 SWRAST_CONTEXT(ctx)->AllowVertexFog = value;
740 }
741
742 void
743 _swrast_allow_pixel_fog( GLcontext *ctx, GLboolean value )
744 {
745 if (SWRAST_DEBUG) {
746 _mesa_debug(ctx, "_swrast_allow_pixel_fog %d\n", value);
747 }
748 SWRAST_CONTEXT(ctx)->InvalidateState( ctx, _NEW_HINT );
749 SWRAST_CONTEXT(ctx)->AllowPixelFog = value;
750 }
751
752
753 GLboolean
754 _swrast_CreateContext( GLcontext *ctx )
755 {
756 GLuint i;
757 SWcontext *swrast = (SWcontext *)CALLOC(sizeof(SWcontext));
758
759 if (SWRAST_DEBUG) {
760 _mesa_debug(ctx, "_swrast_CreateContext\n");
761 }
762
763 if (!swrast)
764 return GL_FALSE;
765
766 swrast->NewState = ~0;
767
768 swrast->choose_point = _swrast_choose_point;
769 swrast->choose_line = _swrast_choose_line;
770 swrast->choose_triangle = _swrast_choose_triangle;
771
772 swrast->InvalidatePointMask = _SWRAST_NEW_POINT;
773 swrast->InvalidateLineMask = _SWRAST_NEW_LINE;
774 swrast->InvalidateTriangleMask = _SWRAST_NEW_TRIANGLE;
775
776 swrast->Point = _swrast_validate_point;
777 swrast->Line = _swrast_validate_line;
778 swrast->Triangle = _swrast_validate_triangle;
779 swrast->InvalidateState = _swrast_sleep;
780 swrast->BlendFunc = _swrast_validate_blend_func;
781
782 swrast->AllowVertexFog = GL_TRUE;
783 swrast->AllowPixelFog = GL_TRUE;
784
785 /* Optimized Accum buffer */
786 swrast->_IntegerAccumMode = GL_FALSE;
787 swrast->_IntegerAccumScaler = 0.0;
788
789 for (i = 0; i < MAX_TEXTURE_IMAGE_UNITS; i++)
790 swrast->TextureSample[i] = NULL;
791
792 swrast->SpanArrays = MALLOC_STRUCT(sw_span_arrays);
793 if (!swrast->SpanArrays) {
794 FREE(swrast);
795 return GL_FALSE;
796 }
797 swrast->SpanArrays->ChanType = CHAN_TYPE;
798 #if CHAN_TYPE == GL_UNSIGNED_BYTE
799 swrast->SpanArrays->rgba = swrast->SpanArrays->rgba8;
800 #elif CHAN_TYPE == GL_UNSIGNED_SHORT
801 swrast->SpanArrays->rgba = swrast->SpanArrays->rgba16;
802 #else
803 swrast->SpanArrays->rgba = swrast->SpanArrays->attribs[FRAG_ATTRIB_COL0];
804 #endif
805
806 /* init point span buffer */
807 swrast->PointSpan.primitive = GL_POINT;
808 swrast->PointSpan.end = 0;
809 swrast->PointSpan.facing = 0;
810 swrast->PointSpan.array = swrast->SpanArrays;
811
812 swrast->TexelBuffer = (GLchan *) MALLOC(ctx->Const.MaxTextureImageUnits *
813 MAX_WIDTH * 4 * sizeof(GLchan));
814 if (!swrast->TexelBuffer) {
815 FREE(swrast->SpanArrays);
816 FREE(swrast);
817 return GL_FALSE;
818 }
819
820 ctx->swrast_context = swrast;
821
822 return GL_TRUE;
823 }
824
825 void
826 _swrast_DestroyContext( GLcontext *ctx )
827 {
828 SWcontext *swrast = SWRAST_CONTEXT(ctx);
829
830 if (SWRAST_DEBUG) {
831 _mesa_debug(ctx, "_swrast_DestroyContext\n");
832 }
833
834 FREE( swrast->SpanArrays );
835 if (swrast->ZoomedArrays)
836 FREE( swrast->ZoomedArrays );
837 FREE( swrast->TexelBuffer );
838 FREE( swrast );
839
840 ctx->swrast_context = 0;
841 }
842
843
844 struct swrast_device_driver *
845 _swrast_GetDeviceDriverReference( GLcontext *ctx )
846 {
847 SWcontext *swrast = SWRAST_CONTEXT(ctx);
848 return &swrast->Driver;
849 }
850
851 void
852 _swrast_flush( GLcontext *ctx )
853 {
854 SWcontext *swrast = SWRAST_CONTEXT(ctx);
855 /* flush any pending fragments from rendering points */
856 if (swrast->PointSpan.end > 0) {
857 if (ctx->Visual.rgbMode) {
858 _swrast_write_rgba_span(ctx, &(swrast->PointSpan));
859 }
860 else {
861 _swrast_write_index_span(ctx, &(swrast->PointSpan));
862 }
863 swrast->PointSpan.end = 0;
864 }
865 }
866
867 void
868 _swrast_render_primitive( GLcontext *ctx, GLenum prim )
869 {
870 SWcontext *swrast = SWRAST_CONTEXT(ctx);
871 if (swrast->Primitive == GL_POINTS && prim != GL_POINTS) {
872 _swrast_flush(ctx);
873 }
874 swrast->Primitive = prim;
875 }
876
877
878 void
879 _swrast_render_start( GLcontext *ctx )
880 {
881 SWcontext *swrast = SWRAST_CONTEXT(ctx);
882 if (swrast->Driver.SpanRenderStart)
883 swrast->Driver.SpanRenderStart( ctx );
884 swrast->PointSpan.end = 0;
885 }
886
887 void
888 _swrast_render_finish( GLcontext *ctx )
889 {
890 SWcontext *swrast = SWRAST_CONTEXT(ctx);
891 if (swrast->Driver.SpanRenderFinish)
892 swrast->Driver.SpanRenderFinish( ctx );
893
894 _swrast_flush(ctx);
895 }
896
897
898 #define SWRAST_DEBUG_VERTICES 0
899
900 void
901 _swrast_print_vertex( GLcontext *ctx, const SWvertex *v )
902 {
903 GLuint i;
904
905 if (SWRAST_DEBUG_VERTICES) {
906 _mesa_debug(ctx, "win %f %f %f %f\n",
907 v->attrib[FRAG_ATTRIB_WPOS][0],
908 v->attrib[FRAG_ATTRIB_WPOS][1],
909 v->attrib[FRAG_ATTRIB_WPOS][2],
910 v->attrib[FRAG_ATTRIB_WPOS][3]);
911
912 for (i = 0 ; i < ctx->Const.MaxTextureCoordUnits ; i++)
913 if (ctx->Texture.Unit[i]._ReallyEnabled)
914 _mesa_debug(ctx, "texcoord[%d] %f %f %f %f\n", i,
915 v->attrib[FRAG_ATTRIB_TEX0 + i][0],
916 v->attrib[FRAG_ATTRIB_TEX0 + i][1],
917 v->attrib[FRAG_ATTRIB_TEX0 + i][2],
918 v->attrib[FRAG_ATTRIB_TEX0 + i][3]);
919
920 #if CHAN_TYPE == GL_FLOAT
921 _mesa_debug(ctx, "color %f %f %f %f\n",
922 v->color[0], v->color[1], v->color[2], v->color[3]);
923 #else
924 _mesa_debug(ctx, "color %d %d %d %d\n",
925 v->color[0], v->color[1], v->color[2], v->color[3]);
926 #endif
927 _mesa_debug(ctx, "spec %g %g %g %g\n",
928 v->attrib[FRAG_ATTRIB_COL1][0],
929 v->attrib[FRAG_ATTRIB_COL1][1],
930 v->attrib[FRAG_ATTRIB_COL1][2],
931 v->attrib[FRAG_ATTRIB_COL1][3]);
932 _mesa_debug(ctx, "fog %f\n", v->attrib[FRAG_ATTRIB_FOGC][0]);
933 _mesa_debug(ctx, "index %d\n", v->attrib[FRAG_ATTRIB_CI][0]);
934 _mesa_debug(ctx, "pointsize %f\n", v->pointSize);
935 _mesa_debug(ctx, "\n");
936 }
937 }