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