0c12a63ae0b274a7a7ae88397f23dececa320e6d
[mesa.git] / src / mesa / swrast / s_context.c
1 /*
2 * Mesa 3-D graphics library
3 *
4 * Copyright (C) 1999-2008 Brian Paul All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included
14 * in all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
20 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22 * OTHER DEALINGS IN THE SOFTWARE.
23 *
24 * Authors:
25 * Keith Whitwell <keithw@vmware.com> Brian Paul
26 */
27
28 #include "main/imports.h"
29 #include "main/bufferobj.h"
30 #include "main/mtypes.h"
31 #include "main/samplerobj.h"
32 #include "main/state.h"
33 #include "main/stencil.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.EnableFlags) rasterMask |= CLIP_BIT;
66 if (_mesa_stencil_is_enabled(ctx)) rasterMask |= STENCIL_BIT;
67 for (i = 0; i < ctx->Const.MaxDrawBuffers; i++) {
68 if (GET_COLORMASK(ctx->Color.ColorMask, i) != 0xf) {
69 rasterMask |= MASKING_BIT;
70 break;
71 }
72 }
73 if (ctx->Color.ColorLogicOpEnabled) rasterMask |= LOGIC_OP_BIT;
74 if (ctx->Texture._MaxEnabledTexImageUnit >= 0) rasterMask |= TEXTURE_BIT;
75 if ( ctx->ViewportArray[0].X < 0
76 || ctx->ViewportArray[0].X + ctx->ViewportArray[0].Width > (GLfloat) ctx->DrawBuffer->Width
77 || ctx->ViewportArray[0].Y < 0
78 || ctx->ViewportArray[0].Y + ctx->ViewportArray[0].Height > (GLfloat) ctx->DrawBuffer->Height) {
79 rasterMask |= CLIP_BIT;
80 }
81
82 if (ctx->Query.CurrentOcclusionObject)
83 rasterMask |= OCCLUSION_BIT;
84
85
86 /* If we're not drawing to exactly one color buffer set the
87 * MULTI_DRAW_BIT flag. Also set it if we're drawing to no
88 * buffers or the RGBA or CI mask disables all writes.
89 */
90 if (ctx->DrawBuffer->_NumColorDrawBuffers != 1) {
91 /* more than one color buffer designated for writing (or zero buffers) */
92 rasterMask |= MULTI_DRAW_BIT;
93 }
94
95 for (i = 0; i < ctx->Const.MaxDrawBuffers; i++) {
96 if (GET_COLORMASK(ctx->Color.ColorMask, i) == 0) {
97 rasterMask |= MULTI_DRAW_BIT; /* all RGBA channels disabled */
98 break;
99 }
100 }
101
102
103 if (_swrast_use_fragment_program(ctx)) {
104 rasterMask |= FRAGPROG_BIT;
105 }
106
107 if (_mesa_ati_fragment_shader_enabled(ctx)) {
108 rasterMask |= ATIFRAGSHADER_BIT;
109 }
110
111 #if CHAN_TYPE == GL_FLOAT
112 if (ctx->Color.ClampFragmentColor == GL_TRUE) {
113 rasterMask |= CLAMPING_BIT;
114 }
115 #endif
116
117 SWRAST_CONTEXT(ctx)->_RasterMask = rasterMask;
118 }
119
120
121 /**
122 * Examine polygon cull state to compute the _BackfaceCullSign field.
123 * _BackfaceCullSign will be 0 if no culling, -1 if culling back-faces,
124 * and 1 if culling front-faces. The Polygon FrontFace state also
125 * factors in.
126 */
127 static void
128 _swrast_update_polygon( struct gl_context *ctx )
129 {
130 GLfloat backface_sign;
131
132 if (ctx->Polygon.CullFlag) {
133 switch (ctx->Polygon.CullFaceMode) {
134 case GL_BACK:
135 backface_sign = -1.0F;
136 break;
137 case GL_FRONT:
138 backface_sign = 1.0F;
139 break;
140 case GL_FRONT_AND_BACK:
141 /* fallthrough */
142 default:
143 backface_sign = 0.0F;
144 }
145 }
146 else {
147 backface_sign = 0.0F;
148 }
149
150 SWRAST_CONTEXT(ctx)->_BackfaceCullSign = backface_sign;
151
152 /* This is for front/back-face determination, but not for culling */
153 SWRAST_CONTEXT(ctx)->_BackfaceSign
154 = (ctx->Polygon.FrontFace == GL_CW) ? -1.0F : 1.0F;
155 }
156
157
158
159 /**
160 * Update the _PreferPixelFog field to indicate if we need to compute
161 * fog blend factors (from the fog coords) per-fragment.
162 */
163 static void
164 _swrast_update_fog_hint( struct gl_context *ctx )
165 {
166 SWcontext *swrast = SWRAST_CONTEXT(ctx);
167 swrast->_PreferPixelFog = (!swrast->AllowVertexFog ||
168 _swrast_use_fragment_program(ctx) ||
169 (ctx->Hint.Fog == GL_NICEST &&
170 swrast->AllowPixelFog));
171 }
172
173
174
175 /**
176 * Update the swrast->_TextureCombinePrimary flag.
177 */
178 static void
179 _swrast_update_texture_env( struct gl_context *ctx )
180 {
181 SWcontext *swrast = SWRAST_CONTEXT(ctx);
182 GLuint i;
183
184 swrast->_TextureCombinePrimary = GL_FALSE;
185
186 for (i = 0; i < ctx->Const.MaxTextureUnits; i++) {
187 const struct gl_tex_env_combine_state *combine =
188 ctx->Texture.FixedFuncUnit[i]._CurrentCombine;
189 GLuint term;
190 for (term = 0; term < combine->_NumArgsRGB; term++) {
191 if (combine->SourceRGB[term] == GL_PRIMARY_COLOR) {
192 swrast->_TextureCombinePrimary = GL_TRUE;
193 return;
194 }
195 if (combine->SourceA[term] == GL_PRIMARY_COLOR) {
196 swrast->_TextureCombinePrimary = GL_TRUE;
197 return;
198 }
199 }
200 }
201 }
202
203
204 /**
205 * Determine if we can defer texturing/shading until after Z/stencil
206 * testing. This potentially allows us to skip texturing/shading for
207 * lots of fragments.
208 */
209 static void
210 _swrast_update_deferred_texture(struct gl_context *ctx)
211 {
212 SWcontext *swrast = SWRAST_CONTEXT(ctx);
213 if (ctx->Color.AlphaEnabled) {
214 /* alpha test depends on post-texture/shader colors */
215 swrast->_DeferredTexture = GL_FALSE;
216 }
217 else {
218 GLboolean use_fprog = _swrast_use_fragment_program(ctx);
219 const struct gl_program *fprog = ctx->FragmentProgram._Current;
220 if (use_fprog &&
221 (fprog->info.outputs_written & (1 << FRAG_RESULT_DEPTH))) {
222 /* Z comes from fragment program/shader */
223 swrast->_DeferredTexture = GL_FALSE;
224 }
225 else if (use_fprog && fprog->info.fs.uses_discard) {
226 swrast->_DeferredTexture = GL_FALSE;
227 }
228 else if (ctx->Query.CurrentOcclusionObject) {
229 /* occlusion query depends on shader discard/kill results */
230 swrast->_DeferredTexture = GL_FALSE;
231 }
232 else {
233 swrast->_DeferredTexture = GL_TRUE;
234 }
235 }
236 }
237
238
239 /**
240 * Update swrast->_FogColor and swrast->_FogEnable values.
241 */
242 static void
243 _swrast_update_fog_state( struct gl_context *ctx )
244 {
245 SWcontext *swrast = SWRAST_CONTEXT(ctx);
246 const struct gl_program *fp = ctx->FragmentProgram._Current;
247
248 assert(fp == NULL || fp->Target == GL_FRAGMENT_PROGRAM_ARB);
249 (void) fp; /* silence unused var warning */
250
251 /* determine if fog is needed, and if so, which fog mode */
252 swrast->_FogEnabled = (!_swrast_use_fragment_program(ctx) &&
253 ctx->Fog.Enabled);
254 }
255
256
257 /**
258 * Update state for running fragment programs. Basically, load the
259 * program parameters with current state values.
260 */
261 static void
262 _swrast_update_fragment_program(struct gl_context *ctx, GLbitfield newState)
263 {
264 if (!_swrast_use_fragment_program(ctx))
265 return;
266
267 _mesa_load_state_parameters(ctx,
268 ctx->FragmentProgram._Current->Parameters);
269 }
270
271
272 /**
273 * See if we can do early diffuse+specular (primary+secondary) color
274 * add per vertex instead of per-fragment.
275 */
276 static void
277 _swrast_update_specular_vertex_add(struct gl_context *ctx)
278 {
279 SWcontext *swrast = SWRAST_CONTEXT(ctx);
280 GLboolean separateSpecular = ctx->Fog.ColorSumEnabled ||
281 (ctx->Light.Enabled &&
282 ctx->Light.Model.ColorControl == GL_SEPARATE_SPECULAR_COLOR);
283
284 swrast->SpecularVertexAdd = (separateSpecular
285 && ctx->Texture._MaxEnabledTexImageUnit == -1
286 && !_swrast_use_fragment_program(ctx)
287 && !_mesa_ati_fragment_shader_enabled(ctx));
288 }
289
290
291 #define _SWRAST_NEW_DERIVED (_SWRAST_NEW_RASTERMASK | \
292 _NEW_PROGRAM_CONSTANTS | \
293 _NEW_TEXTURE | \
294 _NEW_HINT | \
295 _NEW_POLYGON )
296
297 /* State referenced by _swrast_choose_triangle, _swrast_choose_line.
298 */
299 #define _SWRAST_NEW_TRIANGLE (_SWRAST_NEW_DERIVED | \
300 _NEW_RENDERMODE| \
301 _NEW_POLYGON| \
302 _NEW_DEPTH| \
303 _NEW_STENCIL| \
304 _NEW_COLOR| \
305 _NEW_TEXTURE| \
306 _SWRAST_NEW_RASTERMASK| \
307 _NEW_LIGHT| \
308 _NEW_FOG | \
309 _MESA_NEW_SEPARATE_SPECULAR)
310
311 #define _SWRAST_NEW_LINE (_SWRAST_NEW_DERIVED | \
312 _NEW_RENDERMODE| \
313 _NEW_LINE| \
314 _NEW_TEXTURE| \
315 _NEW_LIGHT| \
316 _NEW_FOG| \
317 _NEW_DEPTH | \
318 _MESA_NEW_SEPARATE_SPECULAR)
319
320 #define _SWRAST_NEW_POINT (_SWRAST_NEW_DERIVED | \
321 _NEW_RENDERMODE | \
322 _NEW_POINT | \
323 _NEW_TEXTURE | \
324 _NEW_LIGHT | \
325 _NEW_FOG | \
326 _MESA_NEW_SEPARATE_SPECULAR)
327
328 #define _SWRAST_NEW_TEXTURE_SAMPLE_FUNC _NEW_TEXTURE
329
330 #define _SWRAST_NEW_TEXTURE_ENV_MODE _NEW_TEXTURE
331
332 #define _SWRAST_NEW_BLEND_FUNC _NEW_COLOR
333
334
335
336 /**
337 * Stub for swrast->Triangle to select a true triangle function
338 * after a state change.
339 */
340 static void
341 _swrast_validate_triangle( struct gl_context *ctx,
342 const SWvertex *v0,
343 const SWvertex *v1,
344 const SWvertex *v2 )
345 {
346 SWcontext *swrast = SWRAST_CONTEXT(ctx);
347
348 _swrast_validate_derived( ctx );
349 swrast->choose_triangle( ctx );
350 assert(swrast->Triangle);
351
352 if (swrast->SpecularVertexAdd) {
353 /* separate specular color, but no texture */
354 swrast->SpecTriangle = swrast->Triangle;
355 swrast->Triangle = _swrast_add_spec_terms_triangle;
356 }
357
358 swrast->Triangle( ctx, v0, v1, v2 );
359 }
360
361 /**
362 * Called via swrast->Line. Examine current GL state and choose a software
363 * line routine. Then call it.
364 */
365 static void
366 _swrast_validate_line( struct gl_context *ctx, const SWvertex *v0, const SWvertex *v1 )
367 {
368 SWcontext *swrast = SWRAST_CONTEXT(ctx);
369
370 _swrast_validate_derived( ctx );
371 swrast->choose_line( ctx );
372 assert(swrast->Line);
373
374 if (swrast->SpecularVertexAdd) {
375 swrast->SpecLine = swrast->Line;
376 swrast->Line = _swrast_add_spec_terms_line;
377 }
378
379 swrast->Line( ctx, v0, v1 );
380 }
381
382 /**
383 * Called via swrast->Point. Examine current GL state and choose a software
384 * point routine. Then call it.
385 */
386 static void
387 _swrast_validate_point( struct gl_context *ctx, const SWvertex *v0 )
388 {
389 SWcontext *swrast = SWRAST_CONTEXT(ctx);
390
391 _swrast_validate_derived( ctx );
392 swrast->choose_point( ctx );
393
394 if (swrast->SpecularVertexAdd) {
395 swrast->SpecPoint = swrast->Point;
396 swrast->Point = _swrast_add_spec_terms_point;
397 }
398
399 swrast->Point( ctx, v0 );
400 }
401
402
403 /**
404 * Called via swrast->BlendFunc. Examine GL state to choose a blending
405 * function, then call it.
406 */
407 static void
408 _swrast_validate_blend_func(struct gl_context *ctx, GLuint n, const GLubyte mask[],
409 GLvoid *src, const GLvoid *dst,
410 GLenum chanType )
411 {
412 SWcontext *swrast = SWRAST_CONTEXT(ctx);
413
414 _swrast_validate_derived( ctx ); /* why is this needed? */
415 _swrast_choose_blend_func( ctx, chanType );
416
417 swrast->BlendFunc( ctx, n, mask, src, dst, chanType );
418 }
419
420 static void
421 _swrast_sleep( struct gl_context *ctx, GLbitfield new_state )
422 {
423 (void) ctx; (void) new_state;
424 }
425
426
427 static void
428 _swrast_invalidate_state( struct gl_context *ctx, GLbitfield new_state )
429 {
430 SWcontext *swrast = SWRAST_CONTEXT(ctx);
431 GLuint i;
432
433 swrast->NewState |= new_state;
434
435 /* After 10 statechanges without any swrast functions being called,
436 * put the module to sleep.
437 */
438 if (++swrast->StateChanges > 10) {
439 swrast->InvalidateState = _swrast_sleep;
440 swrast->NewState = ~0;
441 new_state = ~0;
442 }
443
444 if (new_state & swrast->InvalidateTriangleMask)
445 swrast->Triangle = _swrast_validate_triangle;
446
447 if (new_state & swrast->InvalidateLineMask)
448 swrast->Line = _swrast_validate_line;
449
450 if (new_state & swrast->InvalidatePointMask)
451 swrast->Point = _swrast_validate_point;
452
453 if (new_state & _SWRAST_NEW_BLEND_FUNC)
454 swrast->BlendFunc = _swrast_validate_blend_func;
455
456 if (new_state & _SWRAST_NEW_TEXTURE_SAMPLE_FUNC)
457 for (i = 0 ; i < ARRAY_SIZE(swrast->TextureSample); i++)
458 swrast->TextureSample[i] = NULL;
459 }
460
461
462 void
463 _swrast_update_texture_samplers(struct gl_context *ctx)
464 {
465 SWcontext *swrast = SWRAST_CONTEXT(ctx);
466 GLuint u;
467
468 if (!swrast)
469 return; /* pipe hack */
470
471 for (u = 0; u < ARRAY_SIZE(swrast->TextureSample); u++) {
472 struct gl_texture_object *tObj = ctx->Texture.Unit[u]._Current;
473 /* Note: If tObj is NULL, the sample function will be a simple
474 * function that just returns opaque black (0,0,0,1).
475 */
476 _mesa_update_fetch_functions(ctx, u);
477 swrast->TextureSample[u] =
478 _swrast_choose_texture_sample_func(ctx, tObj,
479 _mesa_get_samplerobj(ctx, u));
480 }
481 }
482
483
484 /**
485 * Update swrast->_ActiveAttribs, swrast->_NumActiveAttribs,
486 * swrast->_ActiveAtttribMask.
487 */
488 static void
489 _swrast_update_active_attribs(struct gl_context *ctx)
490 {
491 SWcontext *swrast = SWRAST_CONTEXT(ctx);
492 GLbitfield64 attribsMask;
493
494 /*
495 * Compute _ActiveAttribsMask = which fragment attributes are needed.
496 */
497 if (_swrast_use_fragment_program(ctx)) {
498 /* fragment program/shader */
499 attribsMask = ctx->FragmentProgram._Current->info.inputs_read;
500 attribsMask &= ~VARYING_BIT_POS; /* WPOS is always handled specially */
501 }
502 else if (_mesa_ati_fragment_shader_enabled(ctx)) {
503 attribsMask = VARYING_BIT_COL0 | VARYING_BIT_COL1 |
504 VARYING_BIT_FOGC | VARYING_BITS_TEX_ANY;
505 }
506 else {
507 /* fixed function */
508 attribsMask = 0x0;
509
510 #if CHAN_TYPE == GL_FLOAT
511 attribsMask |= VARYING_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 |= VARYING_BIT_COL1;
518 }
519
520 if (swrast->_FogEnabled)
521 attribsMask |= VARYING_BIT_FOGC;
522
523 attribsMask |= (ctx->Texture._EnabledCoordUnits << VARYING_SLOT_TEX0);
524 }
525
526 swrast->_ActiveAttribMask = attribsMask;
527
528 /* Update _ActiveAttribs[] list */
529 {
530 GLuint i, num = 0;
531 for (i = 0; i < VARYING_SLOT_MAX; i++) {
532 if (attribsMask & BITFIELD64_BIT(i)) {
533 swrast->_ActiveAttribs[num++] = i;
534 /* how should this attribute be interpolated? */
535 if (i == VARYING_SLOT_COL0 || i == VARYING_SLOT_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_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 = calloc(1, sizeof(SWcontext));
718 #ifdef _OPENMP
719 const GLuint maxThreads = omp_get_max_threads();
720 #else
721 const GLuint maxThreads = 1;
722 #endif
723
724 assert(ctx->Const.MaxViewportWidth <= SWRAST_MAX_WIDTH);
725 assert(ctx->Const.MaxViewportHeight <= SWRAST_MAX_WIDTH);
726
727 assert(ctx->Const.MaxRenderbufferSize <= SWRAST_MAX_WIDTH);
728
729 /* make sure largest texture image is <= SWRAST_MAX_WIDTH in size */
730 assert((1 << (ctx->Const.MaxTextureLevels - 1)) <= SWRAST_MAX_WIDTH);
731 assert((1 << (ctx->Const.MaxCubeTextureLevels - 1)) <= SWRAST_MAX_WIDTH);
732 assert((1 << (ctx->Const.Max3DTextureLevels - 1)) <= SWRAST_MAX_WIDTH);
733
734 assert(PROG_MAX_WIDTH == SWRAST_MAX_WIDTH);
735
736 if (SWRAST_DEBUG) {
737 _mesa_debug(ctx, "_swrast_CreateContext\n");
738 }
739
740 if (!swrast)
741 return GL_FALSE;
742
743 swrast->NewState = ~0;
744
745 swrast->choose_point = _swrast_choose_point;
746 swrast->choose_line = _swrast_choose_line;
747 swrast->choose_triangle = _swrast_choose_triangle;
748
749 swrast->InvalidatePointMask = _SWRAST_NEW_POINT;
750 swrast->InvalidateLineMask = _SWRAST_NEW_LINE;
751 swrast->InvalidateTriangleMask = _SWRAST_NEW_TRIANGLE;
752
753 swrast->Point = _swrast_validate_point;
754 swrast->Line = _swrast_validate_line;
755 swrast->Triangle = _swrast_validate_triangle;
756 swrast->InvalidateState = _swrast_sleep;
757 swrast->BlendFunc = _swrast_validate_blend_func;
758
759 swrast->AllowVertexFog = GL_TRUE;
760 swrast->AllowPixelFog = GL_TRUE;
761
762 swrast->Driver.SpanRenderStart = _swrast_span_render_start;
763 swrast->Driver.SpanRenderFinish = _swrast_span_render_finish;
764
765 for (i = 0; i < ARRAY_SIZE(swrast->TextureSample); i++)
766 swrast->TextureSample[i] = NULL;
767
768 /* SpanArrays is global and shared by all SWspan instances. However, when
769 * using multiple threads, it is necessary to have one SpanArrays instance
770 * per thread.
771 */
772 swrast->SpanArrays = malloc(maxThreads * sizeof(SWspanarrays));
773 if (!swrast->SpanArrays) {
774 free(swrast);
775 return GL_FALSE;
776 }
777 for(i = 0; i < maxThreads; i++) {
778 swrast->SpanArrays[i].ChanType = CHAN_TYPE;
779 #if CHAN_TYPE == GL_UNSIGNED_BYTE
780 swrast->SpanArrays[i].rgba = swrast->SpanArrays[i].rgba8;
781 #elif CHAN_TYPE == GL_UNSIGNED_SHORT
782 swrast->SpanArrays[i].rgba = swrast->SpanArrays[i].rgba16;
783 #else
784 swrast->SpanArrays[i].rgba = swrast->SpanArrays[i].attribs[VARYING_SLOT_COL0];
785 #endif
786 }
787
788 /* init point span buffer */
789 swrast->PointSpan.primitive = GL_POINT;
790 swrast->PointSpan.end = 0;
791 swrast->PointSpan.facing = 0;
792 swrast->PointSpan.array = swrast->SpanArrays;
793
794 init_program_native_limits(&ctx->Const.Program[MESA_SHADER_VERTEX]);
795 init_program_native_limits(&ctx->Const.Program[MESA_SHADER_GEOMETRY]);
796 init_program_native_limits(&ctx->Const.Program[MESA_SHADER_FRAGMENT]);
797
798 ctx->swrast_context = swrast;
799
800 swrast->stencil_temp.buf1 = malloc(SWRAST_MAX_WIDTH * sizeof(GLubyte));
801 swrast->stencil_temp.buf2 = malloc(SWRAST_MAX_WIDTH * sizeof(GLubyte));
802 swrast->stencil_temp.buf3 = malloc(SWRAST_MAX_WIDTH * sizeof(GLubyte));
803 swrast->stencil_temp.buf4 = malloc(SWRAST_MAX_WIDTH * sizeof(GLubyte));
804
805 if (!swrast->stencil_temp.buf1 ||
806 !swrast->stencil_temp.buf2 ||
807 !swrast->stencil_temp.buf3 ||
808 !swrast->stencil_temp.buf4) {
809 _swrast_DestroyContext(ctx);
810 return GL_FALSE;
811 }
812
813 return GL_TRUE;
814 }
815
816 void
817 _swrast_DestroyContext( struct gl_context *ctx )
818 {
819 SWcontext *swrast = SWRAST_CONTEXT(ctx);
820
821 if (SWRAST_DEBUG) {
822 _mesa_debug(ctx, "_swrast_DestroyContext\n");
823 }
824
825 free( swrast->SpanArrays );
826 free( swrast->ZoomedArrays );
827 free( swrast->TexelBuffer );
828
829 free(swrast->stencil_temp.buf1);
830 free(swrast->stencil_temp.buf2);
831 free(swrast->stencil_temp.buf3);
832 free(swrast->stencil_temp.buf4);
833
834 free( swrast );
835
836 ctx->swrast_context = 0;
837 }
838
839
840 struct swrast_device_driver *
841 _swrast_GetDeviceDriverReference( struct gl_context *ctx )
842 {
843 SWcontext *swrast = SWRAST_CONTEXT(ctx);
844 return &swrast->Driver;
845 }
846
847 void
848 _swrast_flush( struct gl_context *ctx )
849 {
850 SWcontext *swrast = SWRAST_CONTEXT(ctx);
851 /* flush any pending fragments from rendering points */
852 if (swrast->PointSpan.end > 0) {
853 _swrast_write_rgba_span(ctx, &(swrast->PointSpan));
854 swrast->PointSpan.end = 0;
855 }
856 }
857
858 void
859 _swrast_render_primitive( struct gl_context *ctx, GLenum prim )
860 {
861 SWcontext *swrast = SWRAST_CONTEXT(ctx);
862 if (swrast->Primitive == GL_POINTS && prim != GL_POINTS) {
863 _swrast_flush(ctx);
864 }
865 swrast->Primitive = prim;
866 }
867
868
869 /** called via swrast->Driver.SpanRenderStart() */
870 void
871 _swrast_span_render_start(struct gl_context *ctx)
872 {
873 _swrast_map_textures(ctx);
874 _swrast_map_renderbuffers(ctx);
875 }
876
877
878 /** called via swrast->Driver.SpanRenderFinish() */
879 void
880 _swrast_span_render_finish(struct gl_context *ctx)
881 {
882 _swrast_unmap_textures(ctx);
883 _swrast_unmap_renderbuffers(ctx);
884 }
885
886
887 void
888 _swrast_render_start( struct gl_context *ctx )
889 {
890 SWcontext *swrast = SWRAST_CONTEXT(ctx);
891 if (swrast->Driver.SpanRenderStart)
892 swrast->Driver.SpanRenderStart( ctx );
893 swrast->PointSpan.end = 0;
894 }
895
896 void
897 _swrast_render_finish( struct gl_context *ctx )
898 {
899 SWcontext *swrast = SWRAST_CONTEXT(ctx);
900 struct gl_query_object *query = ctx->Query.CurrentOcclusionObject;
901
902 _swrast_flush(ctx);
903
904 if (swrast->Driver.SpanRenderFinish)
905 swrast->Driver.SpanRenderFinish( ctx );
906
907 if (query && (query->Target == GL_ANY_SAMPLES_PASSED ||
908 query->Target == GL_ANY_SAMPLES_PASSED_CONSERVATIVE))
909 query->Result = !!query->Result;
910 }
911
912
913 #define SWRAST_DEBUG_VERTICES 0
914
915 void
916 _swrast_print_vertex( struct gl_context *ctx, const SWvertex *v )
917 {
918 GLuint i;
919
920 if (SWRAST_DEBUG_VERTICES) {
921 _mesa_debug(ctx, "win %f %f %f %f\n",
922 v->attrib[VARYING_SLOT_POS][0],
923 v->attrib[VARYING_SLOT_POS][1],
924 v->attrib[VARYING_SLOT_POS][2],
925 v->attrib[VARYING_SLOT_POS][3]);
926
927 for (i = 0 ; i < ctx->Const.MaxTextureCoordUnits ; i++)
928 if (ctx->Texture.Unit[i]._Current)
929 _mesa_debug(ctx, "texcoord[%d] %f %f %f %f\n", i,
930 v->attrib[VARYING_SLOT_TEX0 + i][0],
931 v->attrib[VARYING_SLOT_TEX0 + i][1],
932 v->attrib[VARYING_SLOT_TEX0 + i][2],
933 v->attrib[VARYING_SLOT_TEX0 + i][3]);
934
935 #if CHAN_TYPE == GL_FLOAT
936 _mesa_debug(ctx, "color %f %f %f %f\n",
937 v->color[0], v->color[1], v->color[2], v->color[3]);
938 #else
939 _mesa_debug(ctx, "color %d %d %d %d\n",
940 v->color[0], v->color[1], v->color[2], v->color[3]);
941 #endif
942 _mesa_debug(ctx, "spec %g %g %g %g\n",
943 v->attrib[VARYING_SLOT_COL1][0],
944 v->attrib[VARYING_SLOT_COL1][1],
945 v->attrib[VARYING_SLOT_COL1][2],
946 v->attrib[VARYING_SLOT_COL1][3]);
947 _mesa_debug(ctx, "fog %f\n", v->attrib[VARYING_SLOT_FOGC][0]);
948 _mesa_debug(ctx, "index %f\n", v->attrib[VARYING_SLOT_CI][0]);
949 _mesa_debug(ctx, "pointsize %f\n", v->pointSize);
950 _mesa_debug(ctx, "\n");
951 }
952 }