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