Implemented GL_ARB_texture_env_crossbar.
[mesa.git] / src / mesa / swrast / s_context.c
1 /* $Id: s_context.c,v 1.32 2002/05/02 00:59:20 brianp Exp $ */
2
3 /*
4 * Mesa 3-D graphics library
5 * Version: 4.1
6 *
7 * Copyright (C) 1999-2002 Brian Paul All Rights Reserved.
8 *
9 * Permission is hereby granted, free of charge, to any person obtaining a
10 * copy of this software and associated documentation files (the "Software"),
11 * to deal in the Software without restriction, including without limitation
12 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
13 * and/or sell copies of the Software, and to permit persons to whom the
14 * Software is furnished to do so, subject to the following conditions:
15 *
16 * The above copyright notice and this permission notice shall be included
17 * in all copies or substantial portions of the Software.
18 *
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
20 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
22 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
23 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
24 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 *
26 * Authors:
27 * Keith Whitwell <keithw@valinux.com>
28 */
29
30 #include "glheader.h"
31 #include "mtypes.h"
32 #include "mem.h"
33
34 #include "swrast.h"
35 #include "s_points.h"
36 #include "s_lines.h"
37 #include "s_triangle.h"
38 #include "s_blend.h"
39 #include "s_context.h"
40 #include "s_texture.h"
41
42
43 /*
44 * Recompute the value of swrast->_RasterMask, etc. according to
45 * the current context.
46 */
47 static void
48 _swrast_update_rasterflags( GLcontext *ctx )
49 {
50 GLuint RasterMask = 0;
51
52 if (ctx->Color.AlphaEnabled) RasterMask |= ALPHATEST_BIT;
53 if (ctx->Color.BlendEnabled) RasterMask |= BLEND_BIT;
54 if (ctx->Depth.Test) RasterMask |= DEPTH_BIT;
55 if (ctx->Fog.Enabled) RasterMask |= FOG_BIT;
56 if (ctx->Scissor.Enabled) RasterMask |= CLIP_BIT;
57 if (ctx->Stencil.Enabled) RasterMask |= STENCIL_BIT;
58 if (ctx->Visual.rgbMode) {
59 const GLuint colorMask = *((GLuint *) &ctx->Color.ColorMask);
60 if (colorMask != 0xffffffff) RasterMask |= MASKING_BIT;
61 if (ctx->Color.ColorLogicOpEnabled) RasterMask |= LOGIC_OP_BIT;
62 if (ctx->Texture._ReallyEnabled) RasterMask |= TEXTURE_BIT;
63 }
64 else {
65 if (ctx->Color.IndexMask != 0xffffffff) RasterMask |= MASKING_BIT;
66 if (ctx->Color.IndexLogicOpEnabled) RasterMask |= LOGIC_OP_BIT;
67 }
68
69 if (ctx->DrawBuffer->UseSoftwareAlphaBuffers
70 && ctx->Color.ColorMask[ACOMP]
71 && ctx->Color.DrawBuffer != GL_NONE)
72 RasterMask |= ALPHABUF_BIT;
73
74 if ( ctx->Viewport.X < 0
75 || ctx->Viewport.X + ctx->Viewport.Width > (GLint) ctx->DrawBuffer->Width
76 || ctx->Viewport.Y < 0
77 || ctx->Viewport.Y + ctx->Viewport.Height > (GLint) ctx->DrawBuffer->Height) {
78 RasterMask |= CLIP_BIT;
79 }
80
81 if (ctx->Depth.OcclusionTest)
82 RasterMask |= OCCLUSION_BIT;
83
84
85 /* If we're not drawing to exactly one color buffer set the
86 * MULTI_DRAW_BIT flag. Also set it if we're drawing to no
87 * buffers or the RGBA or CI mask disables all writes.
88 */
89 if (ctx->Color.MultiDrawBuffer) {
90 RasterMask |= MULTI_DRAW_BIT;
91 }
92 else if (ctx->Color.DrawBuffer==GL_NONE) {
93 RasterMask |= MULTI_DRAW_BIT;
94 }
95 else if (ctx->Visual.rgbMode && *((GLuint *) ctx->Color.ColorMask) == 0) {
96 RasterMask |= MULTI_DRAW_BIT; /* all RGBA channels disabled */
97 }
98 else if (!ctx->Visual.rgbMode && ctx->Color.IndexMask==0) {
99 RasterMask |= MULTI_DRAW_BIT; /* all color index bits disabled */
100 }
101
102 SWRAST_CONTEXT(ctx)->_RasterMask = RasterMask;
103 }
104
105
106 static void
107 _swrast_update_polygon( GLcontext *ctx )
108 {
109 GLfloat backface_sign = 1;
110
111 if (ctx->Polygon.CullFlag) {
112 backface_sign = 1;
113 switch(ctx->Polygon.CullFaceMode) {
114 case GL_BACK:
115 if(ctx->Polygon.FrontFace==GL_CCW)
116 backface_sign = -1;
117 break;
118 case GL_FRONT:
119 if(ctx->Polygon.FrontFace!=GL_CCW)
120 backface_sign = -1;
121 break;
122 default:
123 case GL_FRONT_AND_BACK:
124 backface_sign = 0;
125 break;
126 }
127 }
128 else {
129 backface_sign = 0;
130 }
131
132 SWRAST_CONTEXT(ctx)->_backface_sign = backface_sign;
133 }
134
135
136 static void
137 _swrast_update_hint( GLcontext *ctx )
138 {
139 SWcontext *swrast = SWRAST_CONTEXT(ctx);
140 swrast->_PreferPixelFog = (!swrast->AllowVertexFog ||
141 (ctx->Hint.Fog == GL_NICEST &&
142 swrast->AllowPixelFog));
143 }
144
145
146 /*
147 * Update the swrast->_AnyTextureCombine flag.
148 */
149 static void
150 _swrast_update_texture_env( GLcontext *ctx )
151 {
152 SWcontext *swrast = SWRAST_CONTEXT(ctx);
153 GLuint i;
154 swrast->_AnyTextureCombine = GL_FALSE;
155 for (i = 0; i < ctx->Const.MaxTextureUnits; i++) {
156 if (ctx->Texture.Unit[i].EnvMode == GL_COMBINE_EXT ||
157 ctx->Texture.Unit[i].EnvMode == GL_COMBINE4_NV) {
158 swrast->_AnyTextureCombine = GL_TRUE;
159 return;
160 }
161 }
162 }
163
164
165 #define _SWRAST_NEW_DERIVED (_SWRAST_NEW_RASTERMASK | \
166 _NEW_TEXTURE | \
167 _NEW_HINT | \
168 _NEW_POLYGON )
169
170 /* State referenced by _swrast_choose_triangle, _swrast_choose_line.
171 */
172 #define _SWRAST_NEW_TRIANGLE (_SWRAST_NEW_DERIVED | \
173 _NEW_RENDERMODE| \
174 _NEW_POLYGON| \
175 _NEW_DEPTH| \
176 _NEW_STENCIL| \
177 _NEW_COLOR| \
178 _NEW_TEXTURE| \
179 _SWRAST_NEW_RASTERMASK| \
180 _NEW_LIGHT| \
181 _NEW_FOG | \
182 _DD_NEW_SEPARATE_SPECULAR)
183
184 #define _SWRAST_NEW_LINE (_SWRAST_NEW_DERIVED | \
185 _NEW_RENDERMODE| \
186 _NEW_LINE| \
187 _NEW_TEXTURE| \
188 _NEW_LIGHT| \
189 _NEW_FOG| \
190 _NEW_DEPTH | \
191 _DD_NEW_SEPARATE_SPECULAR)
192
193 #define _SWRAST_NEW_POINT (_SWRAST_NEW_DERIVED | \
194 _NEW_RENDERMODE | \
195 _NEW_POINT | \
196 _NEW_TEXTURE | \
197 _NEW_LIGHT | \
198 _NEW_FOG | \
199 _DD_NEW_SEPARATE_SPECULAR)
200
201 #define _SWRAST_NEW_TEXTURE_SAMPLE_FUNC _NEW_TEXTURE
202
203 #define _SWRAST_NEW_TEXTURE_ENV_MODE _NEW_TEXTURE
204
205 #define _SWRAST_NEW_BLEND_FUNC _NEW_COLOR
206
207
208
209 /* Stub for swrast->Triangle to select a true triangle function
210 * after a state change.
211 */
212 static void
213 _swrast_validate_triangle( GLcontext *ctx,
214 const SWvertex *v0,
215 const SWvertex *v1,
216 const SWvertex *v2 )
217 {
218 SWcontext *swrast = SWRAST_CONTEXT(ctx);
219
220 _swrast_validate_derived( ctx );
221 swrast->choose_triangle( ctx );
222
223 if ((ctx->_TriangleCaps & DD_SEPARATE_SPECULAR) &&
224 !ctx->Texture._ReallyEnabled) {
225 swrast->SpecTriangle = swrast->Triangle;
226 swrast->Triangle = _swrast_add_spec_terms_triangle;
227 }
228
229 swrast->Triangle( ctx, v0, v1, v2 );
230 }
231
232 static void
233 _swrast_validate_line( GLcontext *ctx, const SWvertex *v0, const SWvertex *v1 )
234 {
235 SWcontext *swrast = SWRAST_CONTEXT(ctx);
236
237 _swrast_validate_derived( ctx );
238 swrast->choose_line( ctx );
239
240 if ((ctx->_TriangleCaps & DD_SEPARATE_SPECULAR) &&
241 !ctx->Texture._ReallyEnabled) {
242 swrast->SpecLine = swrast->Line;
243 swrast->Line = _swrast_add_spec_terms_line;
244 }
245
246
247 swrast->Line( ctx, v0, v1 );
248 }
249
250 static void
251 _swrast_validate_point( GLcontext *ctx, const SWvertex *v0 )
252 {
253 SWcontext *swrast = SWRAST_CONTEXT(ctx);
254
255 _swrast_validate_derived( ctx );
256 swrast->choose_point( ctx );
257
258 if ((ctx->_TriangleCaps & DD_SEPARATE_SPECULAR) &&
259 !ctx->Texture._ReallyEnabled) {
260 swrast->SpecPoint = swrast->Point;
261 swrast->Point = _swrast_add_spec_terms_point;
262 }
263
264 swrast->Point( ctx, v0 );
265 }
266
267 static void
268 _swrast_validate_blend_func( GLcontext *ctx, GLuint n,
269 const GLubyte mask[],
270 GLchan src[][4],
271 CONST GLchan dst[][4] )
272 {
273 SWcontext *swrast = SWRAST_CONTEXT(ctx);
274
275 _swrast_validate_derived( ctx );
276 _swrast_choose_blend_func( ctx );
277
278 swrast->BlendFunc( ctx, n, mask, src, dst );
279 }
280
281
282 static void
283 _swrast_validate_texture_sample( GLcontext *ctx, GLuint texUnit,
284 const struct gl_texture_object *tObj,
285 GLuint n, GLfloat texcoords[][4],
286 const GLfloat lambda[], GLchan rgba[][4] )
287 {
288 SWcontext *swrast = SWRAST_CONTEXT(ctx);
289
290 _swrast_validate_derived( ctx );
291 _swrast_choose_texture_sample_func( ctx, texUnit, tObj );
292
293 swrast->TextureSample[texUnit]( ctx, texUnit, tObj, n, texcoords,
294 lambda, rgba );
295 }
296
297
298 static void
299 _swrast_sleep( GLcontext *ctx, GLuint new_state )
300 {
301 }
302
303
304 static void
305 _swrast_invalidate_state( GLcontext *ctx, GLuint new_state )
306 {
307 SWcontext *swrast = SWRAST_CONTEXT(ctx);
308 GLuint i;
309
310 swrast->NewState |= new_state;
311
312 /* After 10 statechanges without any swrast functions being called,
313 * put the module to sleep.
314 */
315 if (++swrast->StateChanges > 10) {
316 swrast->InvalidateState = _swrast_sleep;
317 swrast->NewState = ~0;
318 new_state = ~0;
319 }
320
321 if (new_state & swrast->invalidate_triangle)
322 swrast->Triangle = _swrast_validate_triangle;
323
324 if (new_state & swrast->invalidate_line)
325 swrast->Line = _swrast_validate_line;
326
327 if (new_state & swrast->invalidate_point)
328 swrast->Point = _swrast_validate_point;
329
330 if (new_state & _SWRAST_NEW_BLEND_FUNC)
331 swrast->BlendFunc = _swrast_validate_blend_func;
332
333 if (new_state & _SWRAST_NEW_TEXTURE_SAMPLE_FUNC)
334 for (i = 0 ; i < ctx->Const.MaxTextureUnits ; i++)
335 swrast->TextureSample[i] = _swrast_validate_texture_sample;
336
337 if (ctx->Visual.rgbMode) {
338 ASSERT(swrast->Driver.WriteRGBASpan);
339 ASSERT(swrast->Driver.WriteRGBSpan);
340 ASSERT(swrast->Driver.WriteMonoRGBASpan);
341 ASSERT(swrast->Driver.WriteRGBAPixels);
342 ASSERT(swrast->Driver.WriteMonoRGBAPixels);
343 ASSERT(swrast->Driver.ReadRGBASpan);
344 ASSERT(swrast->Driver.ReadRGBAPixels);
345 }
346 else {
347 ASSERT(swrast->Driver.WriteCI32Span);
348 ASSERT(swrast->Driver.WriteCI8Span);
349 ASSERT(swrast->Driver.WriteMonoCISpan);
350 ASSERT(swrast->Driver.WriteCI32Pixels);
351 ASSERT(swrast->Driver.WriteMonoCIPixels);
352 ASSERT(swrast->Driver.ReadCI32Span);
353 ASSERT(swrast->Driver.ReadCI32Pixels);
354 }
355 }
356
357
358 void
359 _swrast_validate_derived( GLcontext *ctx )
360 {
361 SWcontext *swrast = SWRAST_CONTEXT(ctx);
362
363 if (swrast->NewState) {
364 if (swrast->NewState & _SWRAST_NEW_RASTERMASK)
365 _swrast_update_rasterflags( ctx );
366
367 if (swrast->NewState & _NEW_POLYGON)
368 _swrast_update_polygon( ctx );
369
370 if (swrast->NewState & _NEW_HINT)
371 _swrast_update_hint( ctx );
372
373 if (swrast->NewState & _SWRAST_NEW_TEXTURE_ENV_MODE)
374 _swrast_update_texture_env( ctx );
375
376 swrast->NewState = 0;
377 swrast->StateChanges = 0;
378 swrast->InvalidateState = _swrast_invalidate_state;
379 }
380 }
381
382 #define SWRAST_DEBUG 0
383
384 /* Public entrypoints: See also s_accum.c, s_bitmap.c, etc.
385 */
386 void
387 _swrast_Quad( GLcontext *ctx,
388 const SWvertex *v0, const SWvertex *v1,
389 const SWvertex *v2, const SWvertex *v3 )
390 {
391 if (SWRAST_DEBUG) {
392 fprintf(stderr, "_swrast_Quad\n");
393 _swrast_print_vertex( ctx, v0 );
394 _swrast_print_vertex( ctx, v1 );
395 _swrast_print_vertex( ctx, v2 );
396 _swrast_print_vertex( ctx, v3 );
397 }
398 SWRAST_CONTEXT(ctx)->Triangle( ctx, v0, v1, v3 );
399 SWRAST_CONTEXT(ctx)->Triangle( ctx, v1, v2, v3 );
400 }
401
402 void
403 _swrast_Triangle( GLcontext *ctx, const SWvertex *v0,
404 const SWvertex *v1, const SWvertex *v2 )
405 {
406 if (SWRAST_DEBUG) {
407 fprintf(stderr, "_swrast_Triangle\n");
408 _swrast_print_vertex( ctx, v0 );
409 _swrast_print_vertex( ctx, v1 );
410 _swrast_print_vertex( ctx, v2 );
411 }
412 SWRAST_CONTEXT(ctx)->Triangle( ctx, v0, v1, v2 );
413 }
414
415 void
416 _swrast_Line( GLcontext *ctx, const SWvertex *v0, const SWvertex *v1 )
417 {
418 if (SWRAST_DEBUG) {
419 fprintf(stderr, "_swrast_Line\n");
420 _swrast_print_vertex( ctx, v0 );
421 _swrast_print_vertex( ctx, v1 );
422 }
423 SWRAST_CONTEXT(ctx)->Line( ctx, v0, v1 );
424 }
425
426 void
427 _swrast_Point( GLcontext *ctx, const SWvertex *v0 )
428 {
429 if (SWRAST_DEBUG) {
430 fprintf(stderr, "_swrast_Point\n");
431 _swrast_print_vertex( ctx, v0 );
432 }
433 SWRAST_CONTEXT(ctx)->Point( ctx, v0 );
434 }
435
436 void
437 _swrast_InvalidateState( GLcontext *ctx, GLuint new_state )
438 {
439 if (SWRAST_DEBUG) {
440 fprintf(stderr, "_swrast_InvalidateState\n");
441 }
442 SWRAST_CONTEXT(ctx)->InvalidateState( ctx, new_state );
443 }
444
445 void
446 _swrast_ResetLineStipple( GLcontext *ctx )
447 {
448 if (SWRAST_DEBUG) {
449 fprintf(stderr, "_swrast_ResetLineStipple\n");
450 }
451 SWRAST_CONTEXT(ctx)->StippleCounter = 0;
452 }
453
454 void
455 _swrast_allow_vertex_fog( GLcontext *ctx, GLboolean value )
456 {
457 if (SWRAST_DEBUG) {
458 fprintf(stderr, "_swrast_allow_vertex_fog %d\n", value);
459 }
460 SWRAST_CONTEXT(ctx)->InvalidateState( ctx, _NEW_HINT );
461 SWRAST_CONTEXT(ctx)->AllowVertexFog = value;
462 }
463
464 void
465 _swrast_allow_pixel_fog( GLcontext *ctx, GLboolean value )
466 {
467 if (SWRAST_DEBUG) {
468 fprintf(stderr, "_swrast_allow_pixel_fog %d\n", value);
469 }
470 SWRAST_CONTEXT(ctx)->InvalidateState( ctx, _NEW_HINT );
471 SWRAST_CONTEXT(ctx)->AllowPixelFog = value;
472 }
473
474
475 GLboolean
476 _swrast_CreateContext( GLcontext *ctx )
477 {
478 GLuint i;
479 SWcontext *swrast = (SWcontext *)CALLOC(sizeof(SWcontext));
480
481 if (SWRAST_DEBUG) {
482 fprintf(stderr, "_swrast_CreateContext\n");
483 }
484
485 if (!swrast)
486 return GL_FALSE;
487
488 swrast->NewState = ~0;
489
490 swrast->choose_point = _swrast_choose_point;
491 swrast->choose_line = _swrast_choose_line;
492 swrast->choose_triangle = _swrast_choose_triangle;
493
494 swrast->invalidate_point = _SWRAST_NEW_POINT;
495 swrast->invalidate_line = _SWRAST_NEW_LINE;
496 swrast->invalidate_triangle = _SWRAST_NEW_TRIANGLE;
497
498 swrast->Point = _swrast_validate_point;
499 swrast->Line = _swrast_validate_line;
500 swrast->Triangle = _swrast_validate_triangle;
501 swrast->InvalidateState = _swrast_sleep;
502 swrast->BlendFunc = _swrast_validate_blend_func;
503
504 swrast->AllowVertexFog = GL_TRUE;
505 swrast->AllowPixelFog = GL_TRUE;
506
507 /* Optimized Accum buffer */
508 swrast->_IntegerAccumMode = GL_TRUE;
509 swrast->_IntegerAccumScaler = 0.0;
510
511 for (i = 0 ; i < MAX_TEXTURE_UNITS ; i++)
512 swrast->TextureSample[i] = _swrast_validate_texture_sample;
513
514 swrast->span = (struct sw_span *) MALLOC(sizeof(struct sw_span));
515 if (!swrast->span) {
516 FREE(swrast);
517 return GL_FALSE;
518 }
519
520 assert(ctx->Const.MaxTextureUnits > 0);
521 assert(ctx->Const.MaxTextureUnits <= MAX_TEXTURE_UNITS);
522
523 swrast->TexelBuffer = (GLchan *) MALLOC(ctx->Const.MaxTextureUnits *
524 MAX_WIDTH * 4 * sizeof(GLchan));
525 if (!swrast->TexelBuffer) {
526 FREE(swrast->span);
527 FREE(swrast);
528 return GL_FALSE;
529 }
530
531 ctx->swrast_context = swrast;
532
533 return GL_TRUE;
534 }
535
536 void
537 _swrast_DestroyContext( GLcontext *ctx )
538 {
539 SWcontext *swrast = SWRAST_CONTEXT(ctx);
540
541 if (SWRAST_DEBUG) {
542 fprintf(stderr, "_swrast_DestroyContext\n");
543 }
544
545 FREE( swrast->span );
546 FREE( swrast->TexelBuffer );
547 FREE( swrast );
548
549 ctx->swrast_context = 0;
550 }
551
552
553 struct swrast_device_driver *
554 _swrast_GetDeviceDriverReference( GLcontext *ctx )
555 {
556 SWcontext *swrast = SWRAST_CONTEXT(ctx);
557 return &swrast->Driver;
558 }
559
560 #define SWRAST_DEBUG_VERTICES 0
561
562 void
563 _swrast_print_vertex( GLcontext *ctx, const SWvertex *v )
564 {
565 GLuint i;
566
567 if (SWRAST_DEBUG_VERTICES) {
568 fprintf(stderr, "win %f %f %f %f\n",
569 v->win[0], v->win[1], v->win[2], v->win[3]);
570
571 for (i = 0 ; i < ctx->Const.MaxTextureUnits ; i++)
572 if (ctx->Texture.Unit[i]._ReallyEnabled)
573 fprintf(stderr, "texcoord[%d] %f %f %f %f\n", i,
574 v->texcoord[i][0], v->texcoord[i][1],
575 v->texcoord[i][2], v->texcoord[i][3]);
576
577 #if CHAN_TYPE == GL_FLOAT
578 fprintf(stderr, "color %f %f %f %f\n",
579 v->color[0], v->color[1], v->color[2], v->color[3]);
580 fprintf(stderr, "spec %f %f %f %f\n",
581 v->specular[0], v->specular[1], v->specular[2], v->specular[3]);
582 #else
583 fprintf(stderr, "color %d %d %d %d\n",
584 v->color[0], v->color[1], v->color[2], v->color[3]);
585 fprintf(stderr, "spec %d %d %d %d\n",
586 v->specular[0], v->specular[1], v->specular[2], v->specular[3]);
587 #endif
588 fprintf(stderr, "fog %f\n", v->fog);
589 fprintf(stderr, "index %d\n", v->index);
590 fprintf(stderr, "pointsize %f\n", v->pointSize);
591 fprintf(stderr, "\n");
592 }
593 }
594
595
596 void
597 _swrast_flush( GLcontext *ctx )
598 {
599 /* no-op */
600 }