mesa: move _mesa_update_fetch_functions() calls into swrast
[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 "main/texfetch.h"
35 #include "program/prog_parameter.h"
36 #include "program/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( struct gl_context *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( struct gl_context *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( struct gl_context *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( struct gl_context *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(struct gl_context *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( struct gl_context *ctx )
248 {
249 SWcontext *swrast = SWRAST_CONTEXT(ctx);
250 const struct gl_fragment_program *fp = ctx->FragmentProgram._Current;
251
252 assert((fp == NULL) || (fp->Base.Target == GL_FRAGMENT_PROGRAM_ARB));
253
254 /* determine if fog is needed, and if so, which fog mode */
255 swrast->_FogEnabled = (fp == NULL && ctx->Fog.Enabled);
256 }
257
258
259 /**
260 * Update state for running fragment programs. Basically, load the
261 * program parameters with current state values.
262 */
263 static void
264 _swrast_update_fragment_program(struct gl_context *ctx, GLbitfield newState)
265 {
266 const struct gl_fragment_program *fp = ctx->FragmentProgram._Current;
267 if (fp) {
268 _mesa_load_state_parameters(ctx, fp->Base.Parameters);
269 }
270 }
271
272
273 /**
274 * See if we can do early diffuse+specular (primary+secondary) color
275 * add per vertex instead of per-fragment.
276 */
277 static void
278 _swrast_update_specular_vertex_add(struct gl_context *ctx)
279 {
280 SWcontext *swrast = SWRAST_CONTEXT(ctx);
281 GLboolean separateSpecular = ctx->Fog.ColorSumEnabled ||
282 (ctx->Light.Enabled &&
283 ctx->Light.Model.ColorControl == GL_SEPARATE_SPECULAR_COLOR);
284
285 swrast->SpecularVertexAdd = (separateSpecular
286 && ctx->Texture._EnabledUnits == 0x0
287 && !ctx->FragmentProgram._Current
288 && !ctx->ATIFragmentShader._Enabled);
289 }
290
291
292 #define _SWRAST_NEW_DERIVED (_SWRAST_NEW_RASTERMASK | \
293 _NEW_PROGRAM_CONSTANTS | \
294 _NEW_TEXTURE | \
295 _NEW_HINT | \
296 _NEW_POLYGON )
297
298 /* State referenced by _swrast_choose_triangle, _swrast_choose_line.
299 */
300 #define _SWRAST_NEW_TRIANGLE (_SWRAST_NEW_DERIVED | \
301 _NEW_RENDERMODE| \
302 _NEW_POLYGON| \
303 _NEW_DEPTH| \
304 _NEW_STENCIL| \
305 _NEW_COLOR| \
306 _NEW_TEXTURE| \
307 _SWRAST_NEW_RASTERMASK| \
308 _NEW_LIGHT| \
309 _NEW_FOG | \
310 _DD_NEW_SEPARATE_SPECULAR)
311
312 #define _SWRAST_NEW_LINE (_SWRAST_NEW_DERIVED | \
313 _NEW_RENDERMODE| \
314 _NEW_LINE| \
315 _NEW_TEXTURE| \
316 _NEW_LIGHT| \
317 _NEW_FOG| \
318 _NEW_DEPTH | \
319 _DD_NEW_SEPARATE_SPECULAR)
320
321 #define _SWRAST_NEW_POINT (_SWRAST_NEW_DERIVED | \
322 _NEW_RENDERMODE | \
323 _NEW_POINT | \
324 _NEW_TEXTURE | \
325 _NEW_LIGHT | \
326 _NEW_FOG | \
327 _DD_NEW_SEPARATE_SPECULAR)
328
329 #define _SWRAST_NEW_TEXTURE_SAMPLE_FUNC _NEW_TEXTURE
330
331 #define _SWRAST_NEW_TEXTURE_ENV_MODE _NEW_TEXTURE
332
333 #define _SWRAST_NEW_BLEND_FUNC _NEW_COLOR
334
335
336
337 /**
338 * Stub for swrast->Triangle to select a true triangle function
339 * after a state change.
340 */
341 static void
342 _swrast_validate_triangle( struct gl_context *ctx,
343 const SWvertex *v0,
344 const SWvertex *v1,
345 const SWvertex *v2 )
346 {
347 SWcontext *swrast = SWRAST_CONTEXT(ctx);
348
349 _swrast_validate_derived( ctx );
350 swrast->choose_triangle( ctx );
351 ASSERT(swrast->Triangle);
352
353 if (swrast->SpecularVertexAdd) {
354 /* separate specular color, but no texture */
355 swrast->SpecTriangle = swrast->Triangle;
356 swrast->Triangle = _swrast_add_spec_terms_triangle;
357 }
358
359 swrast->Triangle( ctx, v0, v1, v2 );
360 }
361
362 /**
363 * Called via swrast->Line. Examine current GL state and choose a software
364 * line routine. Then call it.
365 */
366 static void
367 _swrast_validate_line( struct gl_context *ctx, const SWvertex *v0, const SWvertex *v1 )
368 {
369 SWcontext *swrast = SWRAST_CONTEXT(ctx);
370
371 _swrast_validate_derived( ctx );
372 swrast->choose_line( ctx );
373 ASSERT(swrast->Line);
374
375 if (swrast->SpecularVertexAdd) {
376 swrast->SpecLine = swrast->Line;
377 swrast->Line = _swrast_add_spec_terms_line;
378 }
379
380 swrast->Line( ctx, v0, v1 );
381 }
382
383 /**
384 * Called via swrast->Point. Examine current GL state and choose a software
385 * point routine. Then call it.
386 */
387 static void
388 _swrast_validate_point( struct gl_context *ctx, const SWvertex *v0 )
389 {
390 SWcontext *swrast = SWRAST_CONTEXT(ctx);
391
392 _swrast_validate_derived( ctx );
393 swrast->choose_point( ctx );
394
395 if (swrast->SpecularVertexAdd) {
396 swrast->SpecPoint = swrast->Point;
397 swrast->Point = _swrast_add_spec_terms_point;
398 }
399
400 swrast->Point( ctx, v0 );
401 }
402
403
404 /**
405 * Called via swrast->BlendFunc. Examine GL state to choose a blending
406 * function, then call it.
407 */
408 static void _ASMAPI
409 _swrast_validate_blend_func(struct gl_context *ctx, GLuint n, const GLubyte mask[],
410 GLvoid *src, const GLvoid *dst,
411 GLenum chanType )
412 {
413 SWcontext *swrast = SWRAST_CONTEXT(ctx);
414
415 _swrast_validate_derived( ctx ); /* why is this needed? */
416 _swrast_choose_blend_func( ctx, chanType );
417
418 swrast->BlendFunc( ctx, n, mask, src, dst, chanType );
419 }
420
421 static void
422 _swrast_sleep( struct gl_context *ctx, GLbitfield new_state )
423 {
424 (void) ctx; (void) new_state;
425 }
426
427
428 static void
429 _swrast_invalidate_state( struct gl_context *ctx, GLbitfield new_state )
430 {
431 SWcontext *swrast = SWRAST_CONTEXT(ctx);
432 GLuint i;
433
434 swrast->NewState |= new_state;
435
436 /* After 10 statechanges without any swrast functions being called,
437 * put the module to sleep.
438 */
439 if (++swrast->StateChanges > 10) {
440 swrast->InvalidateState = _swrast_sleep;
441 swrast->NewState = ~0;
442 new_state = ~0;
443 }
444
445 if (new_state & swrast->InvalidateTriangleMask)
446 swrast->Triangle = _swrast_validate_triangle;
447
448 if (new_state & swrast->InvalidateLineMask)
449 swrast->Line = _swrast_validate_line;
450
451 if (new_state & swrast->InvalidatePointMask)
452 swrast->Point = _swrast_validate_point;
453
454 if (new_state & _SWRAST_NEW_BLEND_FUNC)
455 swrast->BlendFunc = _swrast_validate_blend_func;
456
457 if (new_state & _SWRAST_NEW_TEXTURE_SAMPLE_FUNC)
458 for (i = 0 ; i < ctx->Const.MaxTextureImageUnits ; i++)
459 swrast->TextureSample[i] = NULL;
460 }
461
462
463 void
464 _swrast_update_texture_samplers(struct gl_context *ctx)
465 {
466 SWcontext *swrast = SWRAST_CONTEXT(ctx);
467 GLuint u;
468
469 if (!swrast)
470 return; /* pipe hack */
471
472 for (u = 0; u < ctx->Const.MaxTextureImageUnits; u++) {
473 struct gl_texture_object *tObj = ctx->Texture.Unit[u]._Current;
474 /* Note: If tObj is NULL, the sample function will be a simple
475 * function that just returns opaque black (0,0,0,1).
476 */
477 if (tObj) {
478 _mesa_update_fetch_functions(tObj);
479 swrast->TextureSample[u] = _swrast_choose_texture_sample_func(ctx, tObj);
480 }
481 }
482 }
483
484
485 /**
486 * Update swrast->_ActiveAttribs, swrast->_NumActiveAttribs,
487 * swrast->_ActiveAtttribMask.
488 */
489 static void
490 _swrast_update_active_attribs(struct gl_context *ctx)
491 {
492 SWcontext *swrast = SWRAST_CONTEXT(ctx);
493 GLuint attribsMask;
494
495 /*
496 * Compute _ActiveAttribsMask = which fragment attributes are needed.
497 */
498 if (ctx->FragmentProgram._Current) {
499 /* fragment program/shader */
500 attribsMask = ctx->FragmentProgram._Current->Base.InputsRead;
501 attribsMask &= ~FRAG_BIT_WPOS; /* WPOS is always handled specially */
502 }
503 else if (ctx->ATIFragmentShader._Enabled) {
504 attribsMask = ~0; /* XXX fix me */
505 }
506 else {
507 /* fixed function */
508 attribsMask = 0x0;
509
510 #if CHAN_TYPE == GL_FLOAT
511 attribsMask |= FRAG_BIT_COL0;
512 #endif
513
514 if (ctx->Fog.ColorSumEnabled ||
515 (ctx->Light.Enabled &&
516 ctx->Light.Model.ColorControl == GL_SEPARATE_SPECULAR_COLOR)) {
517 attribsMask |= FRAG_BIT_COL1;
518 }
519
520 if (swrast->_FogEnabled)
521 attribsMask |= FRAG_BIT_FOGC;
522
523 attribsMask |= (ctx->Texture._EnabledUnits << FRAG_ATTRIB_TEX0);
524 }
525
526 swrast->_ActiveAttribMask = attribsMask;
527
528 /* Update _ActiveAttribs[] list */
529 {
530 GLuint i, num = 0;
531 for (i = 0; i < FRAG_ATTRIB_MAX; i++) {
532 if (attribsMask & (1 << i)) {
533 swrast->_ActiveAttribs[num++] = i;
534 /* how should this attribute be interpolated? */
535 if (i == FRAG_ATTRIB_COL0 || i == FRAG_ATTRIB_COL1)
536 swrast->_InterpMode[i] = ctx->Light.ShadeModel;
537 else
538 swrast->_InterpMode[i] = GL_SMOOTH;
539 }
540 }
541 swrast->_NumActiveAttribs = num;
542 }
543 }
544
545
546 void
547 _swrast_validate_derived( struct gl_context *ctx )
548 {
549 SWcontext *swrast = SWRAST_CONTEXT(ctx);
550
551 if (swrast->NewState) {
552 if (swrast->NewState & _NEW_POLYGON)
553 _swrast_update_polygon( ctx );
554
555 if (swrast->NewState & (_NEW_HINT | _NEW_PROGRAM))
556 _swrast_update_fog_hint( ctx );
557
558 if (swrast->NewState & _SWRAST_NEW_TEXTURE_ENV_MODE)
559 _swrast_update_texture_env( ctx );
560
561 if (swrast->NewState & (_NEW_FOG | _NEW_PROGRAM))
562 _swrast_update_fog_state( ctx );
563
564 if (swrast->NewState & (_NEW_PROGRAM_CONSTANTS | _NEW_PROGRAM))
565 _swrast_update_fragment_program( ctx, swrast->NewState );
566
567 if (swrast->NewState & (_NEW_TEXTURE | _NEW_PROGRAM)) {
568 _swrast_update_texture_samplers( ctx );
569 }
570
571 if (swrast->NewState & (_NEW_COLOR | _NEW_PROGRAM))
572 _swrast_update_deferred_texture(ctx);
573
574 if (swrast->NewState & _SWRAST_NEW_RASTERMASK)
575 _swrast_update_rasterflags( ctx );
576
577 if (swrast->NewState & (_NEW_DEPTH |
578 _NEW_FOG |
579 _NEW_LIGHT |
580 _NEW_PROGRAM |
581 _NEW_TEXTURE))
582 _swrast_update_active_attribs(ctx);
583
584 if (swrast->NewState & (_NEW_FOG |
585 _NEW_PROGRAM |
586 _NEW_LIGHT |
587 _NEW_TEXTURE))
588 _swrast_update_specular_vertex_add(ctx);
589
590 swrast->NewState = 0;
591 swrast->StateChanges = 0;
592 swrast->InvalidateState = _swrast_invalidate_state;
593 }
594 }
595
596 #define SWRAST_DEBUG 0
597
598 /* Public entrypoints: See also s_accum.c, s_bitmap.c, etc.
599 */
600 void
601 _swrast_Quad( struct gl_context *ctx,
602 const SWvertex *v0, const SWvertex *v1,
603 const SWvertex *v2, const SWvertex *v3 )
604 {
605 if (SWRAST_DEBUG) {
606 _mesa_debug(ctx, "_swrast_Quad\n");
607 _swrast_print_vertex( ctx, v0 );
608 _swrast_print_vertex( ctx, v1 );
609 _swrast_print_vertex( ctx, v2 );
610 _swrast_print_vertex( ctx, v3 );
611 }
612 SWRAST_CONTEXT(ctx)->Triangle( ctx, v0, v1, v3 );
613 SWRAST_CONTEXT(ctx)->Triangle( ctx, v1, v2, v3 );
614 }
615
616 void
617 _swrast_Triangle( struct gl_context *ctx, const SWvertex *v0,
618 const SWvertex *v1, const SWvertex *v2 )
619 {
620 if (SWRAST_DEBUG) {
621 _mesa_debug(ctx, "_swrast_Triangle\n");
622 _swrast_print_vertex( ctx, v0 );
623 _swrast_print_vertex( ctx, v1 );
624 _swrast_print_vertex( ctx, v2 );
625 }
626 SWRAST_CONTEXT(ctx)->Triangle( ctx, v0, v1, v2 );
627 }
628
629 void
630 _swrast_Line( struct gl_context *ctx, const SWvertex *v0, const SWvertex *v1 )
631 {
632 if (SWRAST_DEBUG) {
633 _mesa_debug(ctx, "_swrast_Line\n");
634 _swrast_print_vertex( ctx, v0 );
635 _swrast_print_vertex( ctx, v1 );
636 }
637 SWRAST_CONTEXT(ctx)->Line( ctx, v0, v1 );
638 }
639
640 void
641 _swrast_Point( struct gl_context *ctx, const SWvertex *v0 )
642 {
643 if (SWRAST_DEBUG) {
644 _mesa_debug(ctx, "_swrast_Point\n");
645 _swrast_print_vertex( ctx, v0 );
646 }
647 SWRAST_CONTEXT(ctx)->Point( ctx, v0 );
648 }
649
650 void
651 _swrast_InvalidateState( struct gl_context *ctx, GLbitfield new_state )
652 {
653 if (SWRAST_DEBUG) {
654 _mesa_debug(ctx, "_swrast_InvalidateState\n");
655 }
656 SWRAST_CONTEXT(ctx)->InvalidateState( ctx, new_state );
657 }
658
659 void
660 _swrast_ResetLineStipple( struct gl_context *ctx )
661 {
662 if (SWRAST_DEBUG) {
663 _mesa_debug(ctx, "_swrast_ResetLineStipple\n");
664 }
665 SWRAST_CONTEXT(ctx)->StippleCounter = 0;
666 }
667
668 void
669 _swrast_SetFacing(struct gl_context *ctx, GLuint facing)
670 {
671 SWRAST_CONTEXT(ctx)->PointLineFacing = facing;
672 }
673
674 void
675 _swrast_allow_vertex_fog( struct gl_context *ctx, GLboolean value )
676 {
677 if (SWRAST_DEBUG) {
678 _mesa_debug(ctx, "_swrast_allow_vertex_fog %d\n", value);
679 }
680 SWRAST_CONTEXT(ctx)->InvalidateState( ctx, _NEW_HINT );
681 SWRAST_CONTEXT(ctx)->AllowVertexFog = value;
682 }
683
684 void
685 _swrast_allow_pixel_fog( struct gl_context *ctx, GLboolean value )
686 {
687 if (SWRAST_DEBUG) {
688 _mesa_debug(ctx, "_swrast_allow_pixel_fog %d\n", value);
689 }
690 SWRAST_CONTEXT(ctx)->InvalidateState( ctx, _NEW_HINT );
691 SWRAST_CONTEXT(ctx)->AllowPixelFog = value;
692 }
693
694
695 /**
696 * Initialize native program limits by copying the logical limits.
697 * See comments in init_program_limits() in context.c
698 */
699 static void
700 init_program_native_limits(struct gl_program_constants *prog)
701 {
702 prog->MaxNativeInstructions = prog->MaxInstructions;
703 prog->MaxNativeAluInstructions = prog->MaxAluInstructions;
704 prog->MaxNativeTexInstructions = prog->MaxTexInstructions;
705 prog->MaxNativeTexIndirections = prog->MaxTexIndirections;
706 prog->MaxNativeAttribs = prog->MaxAttribs;
707 prog->MaxNativeTemps = prog->MaxTemps;
708 prog->MaxNativeAddressRegs = prog->MaxAddressRegs;
709 prog->MaxNativeParameters = prog->MaxParameters;
710 }
711
712
713 GLboolean
714 _swrast_CreateContext( struct gl_context *ctx )
715 {
716 GLuint i;
717 SWcontext *swrast = (SWcontext *)CALLOC(sizeof(SWcontext));
718 #ifdef _OPENMP
719 const GLint maxThreads = omp_get_max_threads();
720 #else
721 const GLint maxThreads = 1;
722 #endif
723
724 if (SWRAST_DEBUG) {
725 _mesa_debug(ctx, "_swrast_CreateContext\n");
726 }
727
728 if (!swrast)
729 return GL_FALSE;
730
731 swrast->NewState = ~0;
732
733 swrast->choose_point = _swrast_choose_point;
734 swrast->choose_line = _swrast_choose_line;
735 swrast->choose_triangle = _swrast_choose_triangle;
736
737 swrast->InvalidatePointMask = _SWRAST_NEW_POINT;
738 swrast->InvalidateLineMask = _SWRAST_NEW_LINE;
739 swrast->InvalidateTriangleMask = _SWRAST_NEW_TRIANGLE;
740
741 swrast->Point = _swrast_validate_point;
742 swrast->Line = _swrast_validate_line;
743 swrast->Triangle = _swrast_validate_triangle;
744 swrast->InvalidateState = _swrast_sleep;
745 swrast->BlendFunc = _swrast_validate_blend_func;
746
747 swrast->AllowVertexFog = GL_TRUE;
748 swrast->AllowPixelFog = GL_TRUE;
749
750 /* Optimized Accum buffer */
751 swrast->_IntegerAccumMode = GL_FALSE;
752 swrast->_IntegerAccumScaler = 0.0;
753
754 for (i = 0; i < MAX_TEXTURE_IMAGE_UNITS; i++)
755 swrast->TextureSample[i] = NULL;
756
757 /* SpanArrays is global and shared by all SWspan instances. However, when
758 * using multiple threads, it is necessary to have one SpanArrays instance
759 * per thread.
760 */
761 swrast->SpanArrays = (SWspanarrays *) MALLOC(maxThreads * sizeof(SWspanarrays));
762 if (!swrast->SpanArrays) {
763 FREE(swrast);
764 return GL_FALSE;
765 }
766 for(i = 0; i < maxThreads; i++) {
767 swrast->SpanArrays[i].ChanType = CHAN_TYPE;
768 #if CHAN_TYPE == GL_UNSIGNED_BYTE
769 swrast->SpanArrays[i].rgba = swrast->SpanArrays[i].rgba8;
770 #elif CHAN_TYPE == GL_UNSIGNED_SHORT
771 swrast->SpanArrays[i].rgba = swrast->SpanArrays[i].rgba16;
772 #else
773 swrast->SpanArrays[i].rgba = swrast->SpanArrays[i].attribs[FRAG_ATTRIB_COL0];
774 #endif
775 }
776
777 /* init point span buffer */
778 swrast->PointSpan.primitive = GL_POINT;
779 swrast->PointSpan.end = 0;
780 swrast->PointSpan.facing = 0;
781 swrast->PointSpan.array = swrast->SpanArrays;
782
783 /* TexelBuffer is also global and normally shared by all SWspan instances;
784 * when running with multiple threads, create one per thread.
785 */
786 swrast->TexelBuffer = (GLfloat *) MALLOC(ctx->Const.MaxTextureImageUnits * maxThreads *
787 MAX_WIDTH * 4 * sizeof(GLfloat));
788 if (!swrast->TexelBuffer) {
789 FREE(swrast->SpanArrays);
790 FREE(swrast);
791 return GL_FALSE;
792 }
793
794 init_program_native_limits(&ctx->Const.VertexProgram);
795 init_program_native_limits(&ctx->Const.GeometryProgram);
796 init_program_native_limits(&ctx->Const.FragmentProgram);
797
798 ctx->swrast_context = swrast;
799
800 return GL_TRUE;
801 }
802
803 void
804 _swrast_DestroyContext( struct gl_context *ctx )
805 {
806 SWcontext *swrast = SWRAST_CONTEXT(ctx);
807
808 if (SWRAST_DEBUG) {
809 _mesa_debug(ctx, "_swrast_DestroyContext\n");
810 }
811
812 FREE( swrast->SpanArrays );
813 if (swrast->ZoomedArrays)
814 FREE( swrast->ZoomedArrays );
815 FREE( swrast->TexelBuffer );
816 FREE( swrast );
817
818 ctx->swrast_context = 0;
819 }
820
821
822 struct swrast_device_driver *
823 _swrast_GetDeviceDriverReference( struct gl_context *ctx )
824 {
825 SWcontext *swrast = SWRAST_CONTEXT(ctx);
826 return &swrast->Driver;
827 }
828
829 void
830 _swrast_flush( struct gl_context *ctx )
831 {
832 SWcontext *swrast = SWRAST_CONTEXT(ctx);
833 /* flush any pending fragments from rendering points */
834 if (swrast->PointSpan.end > 0) {
835 _swrast_write_rgba_span(ctx, &(swrast->PointSpan));
836 swrast->PointSpan.end = 0;
837 }
838 }
839
840 void
841 _swrast_render_primitive( struct gl_context *ctx, GLenum prim )
842 {
843 SWcontext *swrast = SWRAST_CONTEXT(ctx);
844 if (swrast->Primitive == GL_POINTS && prim != GL_POINTS) {
845 _swrast_flush(ctx);
846 }
847 swrast->Primitive = prim;
848 }
849
850
851 void
852 _swrast_render_start( struct gl_context *ctx )
853 {
854 SWcontext *swrast = SWRAST_CONTEXT(ctx);
855 if (swrast->Driver.SpanRenderStart)
856 swrast->Driver.SpanRenderStart( ctx );
857 swrast->PointSpan.end = 0;
858 }
859
860 void
861 _swrast_render_finish( struct gl_context *ctx )
862 {
863 SWcontext *swrast = SWRAST_CONTEXT(ctx);
864 if (swrast->Driver.SpanRenderFinish)
865 swrast->Driver.SpanRenderFinish( ctx );
866
867 _swrast_flush(ctx);
868 }
869
870
871 #define SWRAST_DEBUG_VERTICES 0
872
873 void
874 _swrast_print_vertex( struct gl_context *ctx, const SWvertex *v )
875 {
876 GLuint i;
877
878 if (SWRAST_DEBUG_VERTICES) {
879 _mesa_debug(ctx, "win %f %f %f %f\n",
880 v->attrib[FRAG_ATTRIB_WPOS][0],
881 v->attrib[FRAG_ATTRIB_WPOS][1],
882 v->attrib[FRAG_ATTRIB_WPOS][2],
883 v->attrib[FRAG_ATTRIB_WPOS][3]);
884
885 for (i = 0 ; i < ctx->Const.MaxTextureCoordUnits ; i++)
886 if (ctx->Texture.Unit[i]._ReallyEnabled)
887 _mesa_debug(ctx, "texcoord[%d] %f %f %f %f\n", i,
888 v->attrib[FRAG_ATTRIB_TEX0 + i][0],
889 v->attrib[FRAG_ATTRIB_TEX0 + i][1],
890 v->attrib[FRAG_ATTRIB_TEX0 + i][2],
891 v->attrib[FRAG_ATTRIB_TEX0 + i][3]);
892
893 #if CHAN_TYPE == GL_FLOAT
894 _mesa_debug(ctx, "color %f %f %f %f\n",
895 v->color[0], v->color[1], v->color[2], v->color[3]);
896 #else
897 _mesa_debug(ctx, "color %d %d %d %d\n",
898 v->color[0], v->color[1], v->color[2], v->color[3]);
899 #endif
900 _mesa_debug(ctx, "spec %g %g %g %g\n",
901 v->attrib[FRAG_ATTRIB_COL1][0],
902 v->attrib[FRAG_ATTRIB_COL1][1],
903 v->attrib[FRAG_ATTRIB_COL1][2],
904 v->attrib[FRAG_ATTRIB_COL1][3]);
905 _mesa_debug(ctx, "fog %f\n", v->attrib[FRAG_ATTRIB_FOGC][0]);
906 _mesa_debug(ctx, "index %f\n", v->attrib[FRAG_ATTRIB_CI][0]);
907 _mesa_debug(ctx, "pointsize %f\n", v->pointSize);
908 _mesa_debug(ctx, "\n");
909 }
910 }