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