[MESA]
[reactos.git] / reactos / dll / opengl / mesa / src / mesa / swrast / s_context.c
1 /*
2 * Mesa 3-D graphics library
3 * Version: 7.1
4 *
5 * Copyright (C) 1999-2008 Brian Paul All Rights Reserved.
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
21 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 *
24 * Authors:
25 * Keith Whitwell <keith@tungstengraphics.com>
26 * Brian Paul
27 */
28
29 #include "main/imports.h"
30 #include "main/bufferobj.h"
31 #include "main/colormac.h"
32 #include "main/mtypes.h"
33 #include "main/teximage.h"
34 #include "program/prog_parameter.h"
35 #include "program/prog_statevars.h"
36 #include "swrast.h"
37 #include "s_blend.h"
38 #include "s_context.h"
39 #include "s_lines.h"
40 #include "s_points.h"
41 #include "s_span.h"
42 #include "s_texfetch.h"
43 #include "s_triangle.h"
44 #include "s_texfilter.h"
45
46
47 /**
48 * Recompute the value of swrast->_RasterMask, etc. according to
49 * the current context. The _RasterMask field can be easily tested by
50 * drivers to determine certain basic GL state (does the primitive need
51 * stenciling, logic-op, fog, etc?).
52 */
53 static void
54 _swrast_update_rasterflags( struct gl_context *ctx )
55 {
56 SWcontext *swrast = SWRAST_CONTEXT(ctx);
57 GLbitfield rasterMask = 0;
58 GLuint i;
59
60 if (ctx->Color.AlphaEnabled) rasterMask |= ALPHATEST_BIT;
61 if (ctx->Color.BlendEnabled) rasterMask |= BLEND_BIT;
62 if (ctx->Depth.Test) rasterMask |= DEPTH_BIT;
63 if (swrast->_FogEnabled) rasterMask |= FOG_BIT;
64 if (ctx->Scissor.Enabled) rasterMask |= CLIP_BIT;
65 if (ctx->Stencil._Enabled) rasterMask |= STENCIL_BIT;
66 for (i = 0; i < ctx->Const.MaxDrawBuffers; i++) {
67 if (!ctx->Color.ColorMask[i][0] ||
68 !ctx->Color.ColorMask[i][1] ||
69 !ctx->Color.ColorMask[i][2] ||
70 !ctx->Color.ColorMask[i][3]) {
71 rasterMask |= MASKING_BIT;
72 break;
73 }
74 }
75 if (ctx->Color.ColorLogicOpEnabled) rasterMask |= LOGIC_OP_BIT;
76 if (ctx->Texture._EnabledUnits) rasterMask |= TEXTURE_BIT;
77 if ( ctx->Viewport.X < 0
78 || ctx->Viewport.X + ctx->Viewport.Width > (GLint) ctx->DrawBuffer->Width
79 || ctx->Viewport.Y < 0
80 || ctx->Viewport.Y + ctx->Viewport.Height > (GLint) ctx->DrawBuffer->Height) {
81 rasterMask |= CLIP_BIT;
82 }
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->DrawBuffer->_NumColorDrawBuffers != 1) {
90 /* more than one color buffer designated for writing (or zero buffers) */
91 rasterMask |= MULTI_DRAW_BIT;
92 }
93
94 for (i = 0; i < ctx->Const.MaxDrawBuffers; i++) {
95 if (ctx->Color.ColorMask[i][0] +
96 ctx->Color.ColorMask[i][1] +
97 ctx->Color.ColorMask[i][2] +
98 ctx->Color.ColorMask[i][3] == 0) {
99 rasterMask |= MULTI_DRAW_BIT; /* all RGBA channels disabled */
100 break;
101 }
102 }
103
104 #if CHAN_TYPE == GL_FLOAT
105 if (ctx->Color.ClampFragmentColor == GL_TRUE) {
106 rasterMask |= CLAMPING_BIT;
107 }
108 #endif
109
110 SWRAST_CONTEXT(ctx)->_RasterMask = rasterMask;
111 }
112
113
114 /**
115 * Examine polygon cull state to compute the _BackfaceCullSign field.
116 * _BackfaceCullSign will be 0 if no culling, -1 if culling back-faces,
117 * and 1 if culling front-faces. The Polygon FrontFace state also
118 * factors in.
119 */
120 static void
121 _swrast_update_polygon( struct gl_context *ctx )
122 {
123 GLfloat backface_sign;
124
125 if (ctx->Polygon.CullFlag) {
126 switch (ctx->Polygon.CullFaceMode) {
127 case GL_BACK:
128 backface_sign = -1.0F;
129 break;
130 case GL_FRONT:
131 backface_sign = 1.0F;
132 break;
133 case GL_FRONT_AND_BACK:
134 /* fallthrough */
135 default:
136 backface_sign = 0.0F;
137 }
138 }
139 else {
140 backface_sign = 0.0F;
141 }
142
143 SWRAST_CONTEXT(ctx)->_BackfaceCullSign = backface_sign;
144
145 /* This is for front/back-face determination, but not for culling */
146 SWRAST_CONTEXT(ctx)->_BackfaceSign
147 = (ctx->Polygon.FrontFace == GL_CW) ? -1.0F : 1.0F;
148 }
149
150
151
152 /**
153 * Update the _PreferPixelFog field to indicate if we need to compute
154 * fog blend factors (from the fog coords) per-fragment.
155 */
156 static void
157 _swrast_update_fog_hint( struct gl_context *ctx )
158 {
159 SWcontext *swrast = SWRAST_CONTEXT(ctx);
160 swrast->_PreferPixelFog = (!swrast->AllowVertexFog ||
161 (ctx->Hint.Fog == GL_NICEST &&
162 swrast->AllowPixelFog));
163 }
164
165
166
167 /**
168 * Update the swrast->_TextureCombinePrimary flag.
169 */
170 static void
171 _swrast_update_texture_env( struct gl_context *ctx )
172 {
173 SWcontext *swrast = SWRAST_CONTEXT(ctx);
174 GLuint i;
175
176 swrast->_TextureCombinePrimary = GL_FALSE;
177
178 for (i = 0; i < ctx->Const.MaxTextureUnits; i++) {
179 const struct gl_tex_env_combine_state *combine =
180 ctx->Texture.Unit[i]._CurrentCombine;
181 GLuint term;
182 for (term = 0; term < combine->_NumArgsRGB; term++) {
183 if (combine->SourceRGB[term] == GL_PRIMARY_COLOR) {
184 swrast->_TextureCombinePrimary = GL_TRUE;
185 return;
186 }
187 if (combine->SourceA[term] == GL_PRIMARY_COLOR) {
188 swrast->_TextureCombinePrimary = GL_TRUE;
189 return;
190 }
191 }
192 }
193 }
194
195
196 /**
197 * Determine if we can defer texturing/shading until after Z/stencil
198 * testing. This potentially allows us to skip texturing/shading for
199 * lots of fragments.
200 */
201 static void
202 _swrast_update_deferred_texture(struct gl_context *ctx)
203 {
204 SWcontext *swrast = SWRAST_CONTEXT(ctx);
205 if (ctx->Color.AlphaEnabled) {
206 /* alpha test depends on post-texture/shader colors */
207 swrast->_DeferredTexture = GL_FALSE;
208 }
209 else {
210 swrast->_DeferredTexture = GL_TRUE;
211 }
212 }
213
214
215 /**
216 * Update swrast->_FogColor and swrast->_FogEnable values.
217 */
218 static void
219 _swrast_update_fog_state( struct gl_context *ctx )
220 {
221 SWcontext *swrast = SWRAST_CONTEXT(ctx);
222 const struct gl_fragment_program *fp = ctx->FragmentProgram._Current;
223
224 assert((fp == NULL) ||
225 (fp->Base.Target == GL_FRAGMENT_PROGRAM_ARB) ||
226 (fp->Base.Target == GL_FRAGMENT_PROGRAM_NV));
227
228 /* determine if fog is needed, and if so, which fog mode */
229 swrast->_FogEnabled = ctx->Fog.Enabled;
230 }
231
232
233 /**
234 * See if we can do early diffuse+specular (primary+secondary) color
235 * add per vertex instead of per-fragment.
236 */
237 static void
238 _swrast_update_specular_vertex_add(struct gl_context *ctx)
239 {
240 SWcontext *swrast = SWRAST_CONTEXT(ctx);
241 GLboolean separateSpecular = ctx->Fog.ColorSumEnabled ||
242 (ctx->Light.Enabled &&
243 ctx->Light.Model.ColorControl == GL_SEPARATE_SPECULAR_COLOR);
244
245 swrast->SpecularVertexAdd = (separateSpecular
246 && ctx->Texture._EnabledUnits == 0x0);
247 }
248
249
250 #define _SWRAST_NEW_DERIVED (_SWRAST_NEW_RASTERMASK | \
251 _NEW_PROGRAM_CONSTANTS | \
252 _NEW_TEXTURE | \
253 _NEW_HINT | \
254 _NEW_POLYGON )
255
256 /* State referenced by _swrast_choose_triangle, _swrast_choose_line.
257 */
258 #define _SWRAST_NEW_TRIANGLE (_SWRAST_NEW_DERIVED | \
259 _NEW_RENDERMODE| \
260 _NEW_POLYGON| \
261 _NEW_DEPTH| \
262 _NEW_STENCIL| \
263 _NEW_COLOR| \
264 _NEW_TEXTURE| \
265 _SWRAST_NEW_RASTERMASK| \
266 _NEW_LIGHT| \
267 _NEW_FOG | \
268 _DD_NEW_SEPARATE_SPECULAR)
269
270 #define _SWRAST_NEW_LINE (_SWRAST_NEW_DERIVED | \
271 _NEW_RENDERMODE| \
272 _NEW_LINE| \
273 _NEW_TEXTURE| \
274 _NEW_LIGHT| \
275 _NEW_FOG| \
276 _NEW_DEPTH | \
277 _DD_NEW_SEPARATE_SPECULAR)
278
279 #define _SWRAST_NEW_POINT (_SWRAST_NEW_DERIVED | \
280 _NEW_RENDERMODE | \
281 _NEW_POINT | \
282 _NEW_TEXTURE | \
283 _NEW_LIGHT | \
284 _NEW_FOG | \
285 _DD_NEW_SEPARATE_SPECULAR)
286
287 #define _SWRAST_NEW_TEXTURE_SAMPLE_FUNC _NEW_TEXTURE
288
289 #define _SWRAST_NEW_TEXTURE_ENV_MODE _NEW_TEXTURE
290
291 #define _SWRAST_NEW_BLEND_FUNC _NEW_COLOR
292
293
294
295 /**
296 * Stub for swrast->Triangle to select a true triangle function
297 * after a state change.
298 */
299 static void
300 _swrast_validate_triangle( struct gl_context *ctx,
301 const SWvertex *v0,
302 const SWvertex *v1,
303 const SWvertex *v2 )
304 {
305 SWcontext *swrast = SWRAST_CONTEXT(ctx);
306
307 _swrast_validate_derived( ctx );
308 swrast->choose_triangle( ctx );
309 ASSERT(swrast->Triangle);
310
311 if (swrast->SpecularVertexAdd) {
312 /* separate specular color, but no texture */
313 swrast->SpecTriangle = swrast->Triangle;
314 swrast->Triangle = _swrast_add_spec_terms_triangle;
315 }
316
317 swrast->Triangle( ctx, v0, v1, v2 );
318 }
319
320 /**
321 * Called via swrast->Line. Examine current GL state and choose a software
322 * line routine. Then call it.
323 */
324 static void
325 _swrast_validate_line( struct gl_context *ctx, const SWvertex *v0, const SWvertex *v1 )
326 {
327 SWcontext *swrast = SWRAST_CONTEXT(ctx);
328
329 _swrast_validate_derived( ctx );
330 swrast->choose_line( ctx );
331 ASSERT(swrast->Line);
332
333 if (swrast->SpecularVertexAdd) {
334 swrast->SpecLine = swrast->Line;
335 swrast->Line = _swrast_add_spec_terms_line;
336 }
337
338 swrast->Line( ctx, v0, v1 );
339 }
340
341 /**
342 * Called via swrast->Point. Examine current GL state and choose a software
343 * point routine. Then call it.
344 */
345 static void
346 _swrast_validate_point( struct gl_context *ctx, const SWvertex *v0 )
347 {
348 SWcontext *swrast = SWRAST_CONTEXT(ctx);
349
350 _swrast_validate_derived( ctx );
351 swrast->choose_point( ctx );
352
353 if (swrast->SpecularVertexAdd) {
354 swrast->SpecPoint = swrast->Point;
355 swrast->Point = _swrast_add_spec_terms_point;
356 }
357
358 swrast->Point( ctx, v0 );
359 }
360
361
362 /**
363 * Called via swrast->BlendFunc. Examine GL state to choose a blending
364 * function, then call it.
365 */
366 static void _ASMAPI
367 _swrast_validate_blend_func(struct gl_context *ctx, GLuint n, const GLubyte mask[],
368 GLvoid *src, const GLvoid *dst,
369 GLenum chanType )
370 {
371 SWcontext *swrast = SWRAST_CONTEXT(ctx);
372
373 _swrast_validate_derived( ctx ); /* why is this needed? */
374 _swrast_choose_blend_func( ctx, chanType );
375
376 swrast->BlendFunc( ctx, n, mask, src, dst, chanType );
377 }
378
379 static void
380 _swrast_sleep( struct gl_context *ctx, GLbitfield new_state )
381 {
382 (void) ctx; (void) new_state;
383 }
384
385
386 static void
387 _swrast_invalidate_state( struct gl_context *ctx, GLbitfield new_state )
388 {
389 SWcontext *swrast = SWRAST_CONTEXT(ctx);
390 GLuint i;
391
392 swrast->NewState |= new_state;
393
394 /* After 10 statechanges without any swrast functions being called,
395 * put the module to sleep.
396 */
397 if (++swrast->StateChanges > 10) {
398 swrast->InvalidateState = _swrast_sleep;
399 swrast->NewState = ~0;
400 new_state = ~0;
401 }
402
403 if (new_state & swrast->InvalidateTriangleMask)
404 swrast->Triangle = _swrast_validate_triangle;
405
406 if (new_state & swrast->InvalidateLineMask)
407 swrast->Line = _swrast_validate_line;
408
409 if (new_state & swrast->InvalidatePointMask)
410 swrast->Point = _swrast_validate_point;
411
412 if (new_state & _SWRAST_NEW_BLEND_FUNC)
413 swrast->BlendFunc = _swrast_validate_blend_func;
414
415 if (new_state & _SWRAST_NEW_TEXTURE_SAMPLE_FUNC)
416 for (i = 0 ; i < ctx->Const.MaxTextureImageUnits ; i++)
417 swrast->TextureSample[i] = NULL;
418 }
419
420
421 void
422 _swrast_update_texture_samplers(struct gl_context *ctx)
423 {
424 SWcontext *swrast = SWRAST_CONTEXT(ctx);
425 GLuint u;
426
427 if (!swrast)
428 return; /* pipe hack */
429
430 for (u = 0; u < ctx->Const.MaxTextureImageUnits; u++) {
431 struct gl_texture_object *tObj = ctx->Texture.Unit[u]._Current;
432 /* Note: If tObj is NULL, the sample function will be a simple
433 * function that just returns opaque black (0,0,0,1).
434 */
435 if (tObj) {
436 _mesa_update_fetch_functions(tObj);
437 }
438 swrast->TextureSample[u] = _swrast_choose_texture_sample_func(ctx, tObj);
439 }
440 }
441
442
443 /**
444 * Update swrast->_ActiveAttribs, swrast->_NumActiveAttribs,
445 * swrast->_ActiveAtttribMask.
446 */
447 static void
448 _swrast_update_active_attribs(struct gl_context *ctx)
449 {
450 SWcontext *swrast = SWRAST_CONTEXT(ctx);
451 GLbitfield64 attribsMask;
452
453 /*
454 * Compute _ActiveAttribsMask = which fragment attributes are needed.
455 */
456 attribsMask = 0x0;
457
458 #if CHAN_TYPE == GL_FLOAT
459 attribsMask |= FRAG_BIT_COL0;
460 #endif
461
462 if (ctx->Fog.ColorSumEnabled ||
463 (ctx->Light.Enabled &&
464 ctx->Light.Model.ColorControl == GL_SEPARATE_SPECULAR_COLOR)) {
465 attribsMask |= FRAG_BIT_COL1;
466 }
467
468 if (swrast->_FogEnabled)
469 attribsMask |= FRAG_BIT_FOGC;
470
471 attribsMask |= (ctx->Texture._EnabledUnits << FRAG_ATTRIB_TEX0);
472
473 swrast->_ActiveAttribMask = attribsMask;
474
475 /* Update _ActiveAttribs[] list */
476 {
477 GLuint i, num = 0;
478 for (i = 0; i < FRAG_ATTRIB_MAX; i++) {
479 if (attribsMask & BITFIELD64_BIT(i)) {
480 swrast->_ActiveAttribs[num++] = i;
481 /* how should this attribute be interpolated? */
482 if (i == FRAG_ATTRIB_COL0 || i == FRAG_ATTRIB_COL1)
483 swrast->_InterpMode[i] = ctx->Light.ShadeModel;
484 else
485 swrast->_InterpMode[i] = GL_SMOOTH;
486 }
487 }
488 swrast->_NumActiveAttribs = num;
489 }
490 }
491
492
493 void
494 _swrast_validate_derived( struct gl_context *ctx )
495 {
496 SWcontext *swrast = SWRAST_CONTEXT(ctx);
497
498 if (swrast->NewState) {
499 if (swrast->NewState & _NEW_POLYGON)
500 _swrast_update_polygon( ctx );
501
502 if (swrast->NewState & (_NEW_HINT | _NEW_PROGRAM))
503 _swrast_update_fog_hint( ctx );
504
505 if (swrast->NewState & _SWRAST_NEW_TEXTURE_ENV_MODE)
506 _swrast_update_texture_env( ctx );
507
508 if (swrast->NewState & (_NEW_FOG | _NEW_PROGRAM))
509 _swrast_update_fog_state( ctx );
510
511 if (swrast->NewState & (_NEW_TEXTURE | _NEW_PROGRAM)) {
512 _swrast_update_texture_samplers( ctx );
513 }
514
515 if (swrast->NewState & (_NEW_COLOR | _NEW_PROGRAM))
516 _swrast_update_deferred_texture(ctx);
517
518 if (swrast->NewState & _SWRAST_NEW_RASTERMASK)
519 _swrast_update_rasterflags( ctx );
520
521 if (swrast->NewState & (_NEW_DEPTH |
522 _NEW_FOG |
523 _NEW_LIGHT |
524 _NEW_PROGRAM |
525 _NEW_TEXTURE))
526 _swrast_update_active_attribs(ctx);
527
528 if (swrast->NewState & (_NEW_FOG |
529 _NEW_PROGRAM |
530 _NEW_LIGHT |
531 _NEW_TEXTURE))
532 _swrast_update_specular_vertex_add(ctx);
533
534 swrast->NewState = 0;
535 swrast->StateChanges = 0;
536 swrast->InvalidateState = _swrast_invalidate_state;
537 }
538 }
539
540 #define SWRAST_DEBUG 0
541
542 /* Public entrypoints: See also s_bitmap.c, etc.
543 */
544 void
545 _swrast_Quad( struct gl_context *ctx,
546 const SWvertex *v0, const SWvertex *v1,
547 const SWvertex *v2, const SWvertex *v3 )
548 {
549 if (SWRAST_DEBUG) {
550 _mesa_debug(ctx, "_swrast_Quad\n");
551 _swrast_print_vertex( ctx, v0 );
552 _swrast_print_vertex( ctx, v1 );
553 _swrast_print_vertex( ctx, v2 );
554 _swrast_print_vertex( ctx, v3 );
555 }
556 SWRAST_CONTEXT(ctx)->Triangle( ctx, v0, v1, v3 );
557 SWRAST_CONTEXT(ctx)->Triangle( ctx, v1, v2, v3 );
558 }
559
560 void
561 _swrast_Triangle( struct gl_context *ctx, const SWvertex *v0,
562 const SWvertex *v1, const SWvertex *v2 )
563 {
564 if (SWRAST_DEBUG) {
565 _mesa_debug(ctx, "_swrast_Triangle\n");
566 _swrast_print_vertex( ctx, v0 );
567 _swrast_print_vertex( ctx, v1 );
568 _swrast_print_vertex( ctx, v2 );
569 }
570 SWRAST_CONTEXT(ctx)->Triangle( ctx, v0, v1, v2 );
571 }
572
573 void
574 _swrast_Line( struct gl_context *ctx, const SWvertex *v0, const SWvertex *v1 )
575 {
576 if (SWRAST_DEBUG) {
577 _mesa_debug(ctx, "_swrast_Line\n");
578 _swrast_print_vertex( ctx, v0 );
579 _swrast_print_vertex( ctx, v1 );
580 }
581 SWRAST_CONTEXT(ctx)->Line( ctx, v0, v1 );
582 }
583
584 void
585 _swrast_Point( struct gl_context *ctx, const SWvertex *v0 )
586 {
587 if (SWRAST_DEBUG) {
588 _mesa_debug(ctx, "_swrast_Point\n");
589 _swrast_print_vertex( ctx, v0 );
590 }
591 SWRAST_CONTEXT(ctx)->Point( ctx, v0 );
592 }
593
594 void
595 _swrast_InvalidateState( struct gl_context *ctx, GLbitfield new_state )
596 {
597 if (SWRAST_DEBUG) {
598 _mesa_debug(ctx, "_swrast_InvalidateState\n");
599 }
600 SWRAST_CONTEXT(ctx)->InvalidateState( ctx, new_state );
601 }
602
603 void
604 _swrast_ResetLineStipple( struct gl_context *ctx )
605 {
606 if (SWRAST_DEBUG) {
607 _mesa_debug(ctx, "_swrast_ResetLineStipple\n");
608 }
609 SWRAST_CONTEXT(ctx)->StippleCounter = 0;
610 }
611
612 void
613 _swrast_SetFacing(struct gl_context *ctx, GLuint facing)
614 {
615 SWRAST_CONTEXT(ctx)->PointLineFacing = facing;
616 }
617
618 void
619 _swrast_allow_vertex_fog( struct gl_context *ctx, GLboolean value )
620 {
621 if (SWRAST_DEBUG) {
622 _mesa_debug(ctx, "_swrast_allow_vertex_fog %d\n", value);
623 }
624 SWRAST_CONTEXT(ctx)->InvalidateState( ctx, _NEW_HINT );
625 SWRAST_CONTEXT(ctx)->AllowVertexFog = value;
626 }
627
628 void
629 _swrast_allow_pixel_fog( struct gl_context *ctx, GLboolean value )
630 {
631 if (SWRAST_DEBUG) {
632 _mesa_debug(ctx, "_swrast_allow_pixel_fog %d\n", value);
633 }
634 SWRAST_CONTEXT(ctx)->InvalidateState( ctx, _NEW_HINT );
635 SWRAST_CONTEXT(ctx)->AllowPixelFog = value;
636 }
637
638
639 /**
640 * Initialize native program limits by copying the logical limits.
641 * See comments in init_program_limits() in context.c
642 */
643 static void
644 init_program_native_limits(struct gl_program_constants *prog)
645 {
646 prog->MaxNativeInstructions = prog->MaxInstructions;
647 prog->MaxNativeAluInstructions = prog->MaxAluInstructions;
648 prog->MaxNativeTexInstructions = prog->MaxTexInstructions;
649 prog->MaxNativeTexIndirections = prog->MaxTexIndirections;
650 prog->MaxNativeAttribs = prog->MaxAttribs;
651 prog->MaxNativeTemps = prog->MaxTemps;
652 prog->MaxNativeAddressRegs = prog->MaxAddressRegs;
653 prog->MaxNativeParameters = prog->MaxParameters;
654 }
655
656
657 GLboolean
658 _swrast_CreateContext( struct gl_context *ctx )
659 {
660 GLuint i;
661 SWcontext *swrast = (SWcontext *)CALLOC(sizeof(SWcontext));
662 #ifdef _OPENMP
663 const GLuint maxThreads = omp_get_max_threads();
664 #else
665 const GLuint maxThreads = 1;
666 #endif
667
668 if (SWRAST_DEBUG) {
669 _mesa_debug(ctx, "_swrast_CreateContext\n");
670 }
671
672 if (!swrast)
673 return GL_FALSE;
674
675 swrast->NewState = ~0;
676
677 swrast->choose_point = _swrast_choose_point;
678 swrast->choose_line = _swrast_choose_line;
679 swrast->choose_triangle = _swrast_choose_triangle;
680
681 swrast->InvalidatePointMask = _SWRAST_NEW_POINT;
682 swrast->InvalidateLineMask = _SWRAST_NEW_LINE;
683 swrast->InvalidateTriangleMask = _SWRAST_NEW_TRIANGLE;
684
685 swrast->Point = _swrast_validate_point;
686 swrast->Line = _swrast_validate_line;
687 swrast->Triangle = _swrast_validate_triangle;
688 swrast->InvalidateState = _swrast_sleep;
689 swrast->BlendFunc = _swrast_validate_blend_func;
690
691 swrast->AllowVertexFog = GL_TRUE;
692 swrast->AllowPixelFog = GL_TRUE;
693
694 swrast->Driver.SpanRenderStart = _swrast_span_render_start;
695 swrast->Driver.SpanRenderFinish = _swrast_span_render_finish;
696
697 for (i = 0; i < MAX_TEXTURE_IMAGE_UNITS; i++)
698 swrast->TextureSample[i] = NULL;
699
700 /* SpanArrays is global and shared by all SWspan instances. However, when
701 * using multiple threads, it is necessary to have one SpanArrays instance
702 * per thread.
703 */
704 swrast->SpanArrays = (SWspanarrays *) MALLOC(maxThreads * sizeof(SWspanarrays));
705 if (!swrast->SpanArrays) {
706 FREE(swrast);
707 return GL_FALSE;
708 }
709 for(i = 0; i < maxThreads; i++) {
710 swrast->SpanArrays[i].ChanType = CHAN_TYPE;
711 #if CHAN_TYPE == GL_UNSIGNED_BYTE
712 swrast->SpanArrays[i].rgba = swrast->SpanArrays[i].rgba8;
713 #elif CHAN_TYPE == GL_UNSIGNED_SHORT
714 swrast->SpanArrays[i].rgba = swrast->SpanArrays[i].rgba16;
715 #else
716 swrast->SpanArrays[i].rgba = swrast->SpanArrays[i].attribs[FRAG_ATTRIB_COL0];
717 #endif
718 }
719
720 /* init point span buffer */
721 swrast->PointSpan.primitive = GL_POINT;
722 swrast->PointSpan.end = 0;
723 swrast->PointSpan.facing = 0;
724 swrast->PointSpan.array = swrast->SpanArrays;
725
726 init_program_native_limits(&ctx->Const.VertexProgram);
727 init_program_native_limits(&ctx->Const.FragmentProgram);
728
729 ctx->swrast_context = swrast;
730
731 return GL_TRUE;
732 }
733
734 void
735 _swrast_DestroyContext( struct gl_context *ctx )
736 {
737 SWcontext *swrast = SWRAST_CONTEXT(ctx);
738
739 if (SWRAST_DEBUG) {
740 _mesa_debug(ctx, "_swrast_DestroyContext\n");
741 }
742
743 FREE( swrast->SpanArrays );
744 if (swrast->ZoomedArrays)
745 FREE( swrast->ZoomedArrays );
746 FREE( swrast->TexelBuffer );
747 FREE( swrast );
748
749 ctx->swrast_context = 0;
750 }
751
752
753 struct swrast_device_driver *
754 _swrast_GetDeviceDriverReference( struct gl_context *ctx )
755 {
756 SWcontext *swrast = SWRAST_CONTEXT(ctx);
757 return &swrast->Driver;
758 }
759
760 void
761 _swrast_flush( struct gl_context *ctx )
762 {
763 SWcontext *swrast = SWRAST_CONTEXT(ctx);
764 /* flush any pending fragments from rendering points */
765 if (swrast->PointSpan.end > 0) {
766 _swrast_write_rgba_span(ctx, &(swrast->PointSpan));
767 swrast->PointSpan.end = 0;
768 }
769 }
770
771 void
772 _swrast_render_primitive( struct gl_context *ctx, GLenum prim )
773 {
774 SWcontext *swrast = SWRAST_CONTEXT(ctx);
775 if (swrast->Primitive == GL_POINTS && prim != GL_POINTS) {
776 _swrast_flush(ctx);
777 }
778 swrast->Primitive = prim;
779 }
780
781
782 /** called via swrast->Driver.SpanRenderStart() */
783 void
784 _swrast_span_render_start(struct gl_context *ctx)
785 {
786 _swrast_map_textures(ctx);
787 _swrast_map_renderbuffers(ctx);
788 }
789
790
791 /** called via swrast->Driver.SpanRenderFinish() */
792 void
793 _swrast_span_render_finish(struct gl_context *ctx)
794 {
795 _swrast_unmap_textures(ctx);
796 _swrast_unmap_renderbuffers(ctx);
797 }
798
799
800 void
801 _swrast_render_start( struct gl_context *ctx )
802 {
803 SWcontext *swrast = SWRAST_CONTEXT(ctx);
804 if (swrast->Driver.SpanRenderStart)
805 swrast->Driver.SpanRenderStart( ctx );
806 swrast->PointSpan.end = 0;
807 }
808
809 void
810 _swrast_render_finish( struct gl_context *ctx )
811 {
812 SWcontext *swrast = SWRAST_CONTEXT(ctx);
813
814 _swrast_flush(ctx);
815
816 if (swrast->Driver.SpanRenderFinish)
817 swrast->Driver.SpanRenderFinish( ctx );
818 }
819
820
821 #define SWRAST_DEBUG_VERTICES 0
822
823 void
824 _swrast_print_vertex( struct gl_context *ctx, const SWvertex *v )
825 {
826 GLuint i;
827
828 if (SWRAST_DEBUG_VERTICES) {
829 _mesa_debug(ctx, "win %f %f %f %f\n",
830 v->attrib[FRAG_ATTRIB_WPOS][0],
831 v->attrib[FRAG_ATTRIB_WPOS][1],
832 v->attrib[FRAG_ATTRIB_WPOS][2],
833 v->attrib[FRAG_ATTRIB_WPOS][3]);
834
835 for (i = 0 ; i < ctx->Const.MaxTextureCoordUnits ; i++)
836 if (ctx->Texture.Unit[i]._ReallyEnabled)
837 _mesa_debug(ctx, "texcoord[%d] %f %f %f %f\n", i,
838 v->attrib[FRAG_ATTRIB_TEX0 + i][0],
839 v->attrib[FRAG_ATTRIB_TEX0 + i][1],
840 v->attrib[FRAG_ATTRIB_TEX0 + i][2],
841 v->attrib[FRAG_ATTRIB_TEX0 + i][3]);
842
843 #if CHAN_TYPE == GL_FLOAT
844 _mesa_debug(ctx, "color %f %f %f %f\n",
845 v->color[0], v->color[1], v->color[2], v->color[3]);
846 #else
847 _mesa_debug(ctx, "color %d %d %d %d\n",
848 v->color[0], v->color[1], v->color[2], v->color[3]);
849 #endif
850 _mesa_debug(ctx, "spec %g %g %g %g\n",
851 v->attrib[FRAG_ATTRIB_COL1][0],
852 v->attrib[FRAG_ATTRIB_COL1][1],
853 v->attrib[FRAG_ATTRIB_COL1][2],
854 v->attrib[FRAG_ATTRIB_COL1][3]);
855 _mesa_debug(ctx, "fog %f\n", v->attrib[FRAG_ATTRIB_FOGC][0]);
856 _mesa_debug(ctx, "index %f\n", v->attrib[FRAG_ATTRIB_CI][0]);
857 _mesa_debug(ctx, "pointsize %f\n", v->pointSize);
858 _mesa_debug(ctx, "\n");
859 }
860 }