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