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