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