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