1 /* $Id: blend.c,v 1.9 1999/11/12 04:56:55 kendallb Exp $ */
4 * Mesa 3-D graphics library
7 * Copyright (C) 1999 Brian Paul All Rights Reserved.
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:
16 * The above copyright notice and this permission notice shall be included
17 * in all copies or substantial portions of the Software.
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.
44 void _mesa_BlendFunc( GLenum sfactor
, GLenum dfactor
)
46 GET_CURRENT_CONTEXT(ctx
);
47 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
, "glBlendFunc");
49 if (MESA_VERBOSE
& (VERBOSE_API
|VERBOSE_TEXTURE
))
50 fprintf(stderr
, "glBlendFunc %s %s\n",
51 gl_lookup_enum_by_nr(sfactor
),
52 gl_lookup_enum_by_nr(dfactor
));
58 case GL_ONE_MINUS_DST_COLOR
:
60 case GL_ONE_MINUS_SRC_ALPHA
:
62 case GL_ONE_MINUS_DST_ALPHA
:
63 case GL_SRC_ALPHA_SATURATE
:
64 case GL_CONSTANT_COLOR
:
65 case GL_ONE_MINUS_CONSTANT_COLOR
:
66 case GL_CONSTANT_ALPHA
:
67 case GL_ONE_MINUS_CONSTANT_ALPHA
:
68 ctx
->Color
.BlendSrcRGB
= ctx
->Color
.BlendSrcA
= sfactor
;
71 gl_error( ctx
, GL_INVALID_ENUM
, "glBlendFunc(sfactor)" );
79 case GL_ONE_MINUS_SRC_COLOR
:
81 case GL_ONE_MINUS_SRC_ALPHA
:
83 case GL_ONE_MINUS_DST_ALPHA
:
84 case GL_CONSTANT_COLOR
:
85 case GL_ONE_MINUS_CONSTANT_COLOR
:
86 case GL_CONSTANT_ALPHA
:
87 case GL_ONE_MINUS_CONSTANT_ALPHA
:
88 ctx
->Color
.BlendDstRGB
= ctx
->Color
.BlendDstA
= dfactor
;
91 gl_error( ctx
, GL_INVALID_ENUM
, "glBlendFunc(dfactor)" );
95 if (ctx
->Driver
.BlendFunc
) {
96 (*ctx
->Driver
.BlendFunc
)( ctx
, sfactor
, dfactor
);
99 ctx
->Color
.BlendFunc
= NULL
;
100 ctx
->NewState
|= NEW_RASTER_OPS
;
104 /* GL_INGR_blend_func_separate */
106 _mesa_BlendFuncSeparateINGR( GLenum sfactorRGB
, GLenum dfactorRGB
,
107 GLenum sfactorA
, GLenum dfactorA
)
109 GET_CURRENT_CONTEXT(ctx
);
110 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
, "glBlendFuncSeparate");
112 if (MESA_VERBOSE
& (VERBOSE_API
|VERBOSE_TEXTURE
))
113 fprintf(stderr
, "glBlendFuncSeperate %s %s %s %s\n",
114 gl_lookup_enum_by_nr(sfactorRGB
),
115 gl_lookup_enum_by_nr(dfactorRGB
),
116 gl_lookup_enum_by_nr(sfactorA
),
117 gl_lookup_enum_by_nr(dfactorA
));
119 switch (sfactorRGB
) {
123 case GL_ONE_MINUS_DST_COLOR
:
125 case GL_ONE_MINUS_SRC_ALPHA
:
127 case GL_ONE_MINUS_DST_ALPHA
:
128 case GL_SRC_ALPHA_SATURATE
:
129 case GL_CONSTANT_COLOR
:
130 case GL_ONE_MINUS_CONSTANT_COLOR
:
131 case GL_CONSTANT_ALPHA
:
132 case GL_ONE_MINUS_CONSTANT_ALPHA
:
133 ctx
->Color
.BlendSrcRGB
= sfactorRGB
;
136 gl_error( ctx
, GL_INVALID_ENUM
, "glBlendFuncSeparate(sfactorRGB)" );
140 switch (dfactorRGB
) {
144 case GL_ONE_MINUS_SRC_COLOR
:
146 case GL_ONE_MINUS_SRC_ALPHA
:
148 case GL_ONE_MINUS_DST_ALPHA
:
149 case GL_CONSTANT_COLOR
:
150 case GL_ONE_MINUS_CONSTANT_COLOR
:
151 case GL_CONSTANT_ALPHA
:
152 case GL_ONE_MINUS_CONSTANT_ALPHA
:
153 ctx
->Color
.BlendDstRGB
= dfactorRGB
;
156 gl_error( ctx
, GL_INVALID_ENUM
, "glBlendFuncSeparate(dfactorRGB)" );
164 case GL_ONE_MINUS_DST_COLOR
:
166 case GL_ONE_MINUS_SRC_ALPHA
:
168 case GL_ONE_MINUS_DST_ALPHA
:
169 case GL_SRC_ALPHA_SATURATE
:
170 case GL_CONSTANT_COLOR
:
171 case GL_ONE_MINUS_CONSTANT_COLOR
:
172 case GL_CONSTANT_ALPHA
:
173 case GL_ONE_MINUS_CONSTANT_ALPHA
:
174 ctx
->Color
.BlendSrcA
= sfactorA
;
177 gl_error( ctx
, GL_INVALID_ENUM
, "glBlendFuncSeparate(sfactorA)" );
185 case GL_ONE_MINUS_SRC_COLOR
:
187 case GL_ONE_MINUS_SRC_ALPHA
:
189 case GL_ONE_MINUS_DST_ALPHA
:
190 case GL_CONSTANT_COLOR
:
191 case GL_ONE_MINUS_CONSTANT_COLOR
:
192 case GL_CONSTANT_ALPHA
:
193 case GL_ONE_MINUS_CONSTANT_ALPHA
:
194 ctx
->Color
.BlendDstA
= dfactorA
;
197 gl_error( ctx
, GL_INVALID_ENUM
, "glBlendFuncSeparate(dfactorA)" );
201 ctx
->Color
.BlendFunc
= NULL
;
202 ctx
->NewState
|= NEW_RASTER_OPS
;
204 if (ctx
->Driver
.BlendFuncSeparate
) {
205 (*ctx
->Driver
.BlendFuncSeparate
)( ctx
, sfactorRGB
, dfactorRGB
,
206 sfactorA
, dfactorA
);
212 /* This is really an extension function! */
214 _mesa_BlendEquationEXT( GLenum mode
)
216 GET_CURRENT_CONTEXT(ctx
);
217 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
, "glBlendEquation");
219 if (MESA_VERBOSE
& (VERBOSE_API
|VERBOSE_TEXTURE
))
220 fprintf(stderr
, "glBlendEquation %s\n",
221 gl_lookup_enum_by_nr(mode
));
228 case GL_FUNC_ADD_EXT
:
229 case GL_FUNC_SUBTRACT_EXT
:
230 case GL_FUNC_REVERSE_SUBTRACT_EXT
:
231 ctx
->Color
.BlendEquation
= mode
;
234 gl_error( ctx
, GL_INVALID_ENUM
, "glBlendEquation" );
238 /* This is needed to support 1.1's RGB logic ops AND
239 * 1.0's blending logicops.
241 if (mode
==GL_LOGIC_OP
&& ctx
->Color
.BlendEnabled
) {
242 ctx
->Color
.ColorLogicOpEnabled
= GL_TRUE
;
245 ctx
->Color
.ColorLogicOpEnabled
= GL_FALSE
;
248 ctx
->Color
.BlendFunc
= NULL
;
249 ctx
->NewState
|= NEW_RASTER_OPS
;
251 if (ctx
->Driver
.BlendEquation
)
252 ctx
->Driver
.BlendEquation( ctx
, mode
);
258 _mesa_BlendColorEXT( GLclampf red
, GLclampf green
, GLclampf blue
, GLclampf alpha
)
260 GET_CURRENT_CONTEXT(ctx
);
261 ctx
->Color
.BlendColor
[0] = CLAMP( red
, 0.0F
, 1.0F
);
262 ctx
->Color
.BlendColor
[1] = CLAMP( green
, 0.0F
, 1.0F
);
263 ctx
->Color
.BlendColor
[2] = CLAMP( blue
, 0.0F
, 1.0F
);
264 ctx
->Color
.BlendColor
[3] = CLAMP( alpha
, 0.0F
, 1.0F
);
268 #define _BLENDAPI _ASMAPI
274 * Common transparency blending mode.
276 static void _BLENDAPI
blend_transparency( GLcontext
*ctx
, GLuint n
, const GLubyte mask
[],
277 GLubyte rgba
[][4], CONST GLubyte dest
[][4] )
280 ASSERT(ctx
->Color
.BlendEquation
==GL_FUNC_ADD_EXT
);
281 ASSERT(ctx
->Color
.BlendSrcRGB
==GL_SRC_ALPHA
);
282 ASSERT(ctx
->Color
.BlendDstRGB
==GL_ONE_MINUS_SRC_ALPHA
);
287 GLint t
= rgba
[i
][ACOMP
]; /* t in [0,255] */
289 rgba
[i
][RCOMP
] = dest
[i
][RCOMP
];
290 rgba
[i
][GCOMP
] = dest
[i
][GCOMP
];
291 rgba
[i
][BCOMP
] = dest
[i
][BCOMP
];
292 rgba
[i
][ACOMP
] = dest
[i
][ACOMP
];
299 GLint r
= (rgba
[i
][RCOMP
] * t
+ dest
[i
][RCOMP
] * s
) >> 8;
300 GLint g
= (rgba
[i
][GCOMP
] * t
+ dest
[i
][GCOMP
] * s
) >> 8;
301 GLint b
= (rgba
[i
][BCOMP
] * t
+ dest
[i
][BCOMP
] * s
) >> 8;
302 GLint a
= (rgba
[i
][ACOMP
] * t
+ dest
[i
][ACOMP
] * s
) >> 8;
307 rgba
[i
][RCOMP
] = (GLubyte
) r
;
308 rgba
[i
][GCOMP
] = (GLubyte
) g
;
309 rgba
[i
][BCOMP
] = (GLubyte
) b
;
310 rgba
[i
][ACOMP
] = (GLubyte
) a
;
321 static void _BLENDAPI
blend_add( GLcontext
*ctx
, GLuint n
, const GLubyte mask
[],
322 GLubyte rgba
[][4], CONST GLubyte dest
[][4] )
325 ASSERT(ctx
->Color
.BlendEquation
==GL_FUNC_ADD_EXT
);
326 ASSERT(ctx
->Color
.BlendSrcRGB
==GL_ONE
);
327 ASSERT(ctx
->Color
.BlendDstRGB
==GL_ONE
);
332 GLint r
= rgba
[i
][RCOMP
] + dest
[i
][RCOMP
];
333 GLint g
= rgba
[i
][GCOMP
] + dest
[i
][GCOMP
];
334 GLint b
= rgba
[i
][BCOMP
] + dest
[i
][BCOMP
];
335 GLint a
= rgba
[i
][ACOMP
] + dest
[i
][ACOMP
];
336 rgba
[i
][RCOMP
] = (GLubyte
) MIN2( r
, 255 );
337 rgba
[i
][GCOMP
] = (GLubyte
) MIN2( g
, 255 );
338 rgba
[i
][BCOMP
] = (GLubyte
) MIN2( b
, 255 );
339 rgba
[i
][ACOMP
] = (GLubyte
) MIN2( a
, 255 );
347 * Blend min function (for GL_EXT_blend_minmax)
349 static void _BLENDAPI
blend_min( GLcontext
*ctx
, GLuint n
, const GLubyte mask
[],
350 GLubyte rgba
[][4], CONST GLubyte dest
[][4] )
353 ASSERT(ctx
->Color
.BlendEquation
==GL_MIN_EXT
);
358 rgba
[i
][RCOMP
] = (GLubyte
) MIN2( rgba
[i
][RCOMP
], dest
[i
][RCOMP
] );
359 rgba
[i
][GCOMP
] = (GLubyte
) MIN2( rgba
[i
][GCOMP
], dest
[i
][GCOMP
] );
360 rgba
[i
][BCOMP
] = (GLubyte
) MIN2( rgba
[i
][BCOMP
], dest
[i
][BCOMP
] );
361 rgba
[i
][ACOMP
] = (GLubyte
) MIN2( rgba
[i
][ACOMP
], dest
[i
][ACOMP
] );
369 * Blend max function (for GL_EXT_blend_minmax)
371 static void _BLENDAPI
blend_max( GLcontext
*ctx
, GLuint n
, const GLubyte mask
[],
372 GLubyte rgba
[][4], CONST GLubyte dest
[][4] )
375 ASSERT(ctx
->Color
.BlendEquation
==GL_MAX_EXT
);
380 rgba
[i
][RCOMP
] = (GLubyte
) MAX2( rgba
[i
][RCOMP
], dest
[i
][RCOMP
] );
381 rgba
[i
][GCOMP
] = (GLubyte
) MAX2( rgba
[i
][GCOMP
], dest
[i
][GCOMP
] );
382 rgba
[i
][BCOMP
] = (GLubyte
) MAX2( rgba
[i
][BCOMP
], dest
[i
][BCOMP
] );
383 rgba
[i
][ACOMP
] = (GLubyte
) MAX2( rgba
[i
][ACOMP
], dest
[i
][ACOMP
] );
391 * Modulate: result = src * dest
393 static void _BLENDAPI
blend_modulate( GLcontext
*ctx
, GLuint n
, const GLubyte mask
[],
394 GLubyte rgba
[][4], CONST GLubyte dest
[][4] )
401 GLint r
= (rgba
[i
][RCOMP
] * dest
[i
][RCOMP
]) >> 8;
402 GLint g
= (rgba
[i
][GCOMP
] * dest
[i
][GCOMP
]) >> 8;
403 GLint b
= (rgba
[i
][BCOMP
] * dest
[i
][BCOMP
]) >> 8;
404 GLint a
= (rgba
[i
][ACOMP
] * dest
[i
][ACOMP
]) >> 8;
405 rgba
[i
][RCOMP
] = (GLubyte
) r
;
406 rgba
[i
][GCOMP
] = (GLubyte
) g
;
407 rgba
[i
][BCOMP
] = (GLubyte
) b
;
408 rgba
[i
][ACOMP
] = (GLubyte
) a
;
416 * General case blend pixels.
417 * Input: n - number of pixels
418 * mask - the usual write mask
419 * In/Out: rgba - the incoming and modified pixels
420 * Input: dest - the pixels from the dest color buffer
422 static void _BLENDAPI
blend_general( GLcontext
*ctx
, GLuint n
, const GLubyte mask
[],
423 GLubyte rgba
[][4], CONST GLubyte dest
[][4] )
425 GLfloat rscale
= 1.0F
/ 255.0F
;
426 GLfloat gscale
= 1.0F
/ 255.0F
;
427 GLfloat bscale
= 1.0F
/ 255.0F
;
428 GLfloat ascale
= 1.0F
/ 255.0F
;
433 GLint Rs
, Gs
, Bs
, As
; /* Source colors */
434 GLint Rd
, Gd
, Bd
, Ad
; /* Dest colors */
435 GLfloat sR
, sG
, sB
, sA
; /* Source scaling */
436 GLfloat dR
, dG
, dB
, dA
; /* Dest scaling */
445 /* Frame buffer color */
451 /* Source RGB factor */
452 switch (ctx
->Color
.BlendSrcRGB
) {
460 sR
= (GLfloat
) Rd
* rscale
;
461 sG
= (GLfloat
) Gd
* gscale
;
462 sB
= (GLfloat
) Bd
* bscale
;
464 case GL_ONE_MINUS_DST_COLOR
:
465 sR
= 1.0F
- (GLfloat
) Rd
* rscale
;
466 sG
= 1.0F
- (GLfloat
) Gd
* gscale
;
467 sB
= 1.0F
- (GLfloat
) Bd
* bscale
;
470 sR
= sG
= sB
= (GLfloat
) As
* ascale
;
472 case GL_ONE_MINUS_SRC_ALPHA
:
473 sR
= sG
= sB
= (GLfloat
) 1.0F
- (GLfloat
) As
* ascale
;
476 sR
= sG
= sB
= (GLfloat
) Ad
* ascale
;
478 case GL_ONE_MINUS_DST_ALPHA
:
479 sR
= sG
= sB
= 1.0F
- (GLfloat
) Ad
* ascale
;
481 case GL_SRC_ALPHA_SATURATE
:
482 if (As
< 1.0F
- (GLfloat
) Ad
* ascale
) {
483 sR
= sG
= sB
= (GLfloat
) As
* ascale
;
486 sR
= sG
= sB
= 1.0F
- (GLfloat
) Ad
* ascale
;
489 case GL_CONSTANT_COLOR
:
490 sR
= ctx
->Color
.BlendColor
[0];
491 sG
= ctx
->Color
.BlendColor
[1];
492 sB
= ctx
->Color
.BlendColor
[2];
494 case GL_ONE_MINUS_CONSTANT_COLOR
:
495 sR
= 1.0F
- ctx
->Color
.BlendColor
[0];
496 sG
= 1.0F
- ctx
->Color
.BlendColor
[1];
497 sB
= 1.0F
- ctx
->Color
.BlendColor
[2];
499 case GL_CONSTANT_ALPHA
:
500 sR
= sG
= sB
= ctx
->Color
.BlendColor
[3];
502 case GL_ONE_MINUS_CONSTANT_ALPHA
:
503 sR
= sG
= sB
= 1.0F
- ctx
->Color
.BlendColor
[3];
506 /* this should never happen */
507 gl_problem(ctx
, "Bad blend source RGB factor in do_blend");
511 /* Source Alpha factor */
512 switch (ctx
->Color
.BlendSrcA
) {
520 sA
= (GLfloat
) Ad
* ascale
;
522 case GL_ONE_MINUS_DST_COLOR
:
523 sA
= 1.0F
- (GLfloat
) Ad
* ascale
;
526 sA
= (GLfloat
) As
* ascale
;
528 case GL_ONE_MINUS_SRC_ALPHA
:
529 sA
= (GLfloat
) 1.0F
- (GLfloat
) As
* ascale
;
532 sA
=(GLfloat
) Ad
* ascale
;
534 case GL_ONE_MINUS_DST_ALPHA
:
535 sA
= 1.0F
- (GLfloat
) Ad
* ascale
;
537 case GL_SRC_ALPHA_SATURATE
:
540 case GL_CONSTANT_COLOR
:
541 sA
= ctx
->Color
.BlendColor
[3];
543 case GL_ONE_MINUS_CONSTANT_COLOR
:
544 sA
= 1.0F
- ctx
->Color
.BlendColor
[3];
546 case GL_CONSTANT_ALPHA
:
547 sA
= ctx
->Color
.BlendColor
[3];
549 case GL_ONE_MINUS_CONSTANT_ALPHA
:
550 sA
= 1.0F
- ctx
->Color
.BlendColor
[3];
553 /* this should never happen */
555 gl_problem(ctx
, "Bad blend source A factor in do_blend");
558 /* Dest RGB factor */
559 switch (ctx
->Color
.BlendDstRGB
) {
567 dR
= (GLfloat
) Rs
* rscale
;
568 dG
= (GLfloat
) Gs
* gscale
;
569 dB
= (GLfloat
) Bs
* bscale
;
571 case GL_ONE_MINUS_SRC_COLOR
:
572 dR
= 1.0F
- (GLfloat
) Rs
* rscale
;
573 dG
= 1.0F
- (GLfloat
) Gs
* gscale
;
574 dB
= 1.0F
- (GLfloat
) Bs
* bscale
;
577 dR
= dG
= dB
= (GLfloat
) As
* ascale
;
579 case GL_ONE_MINUS_SRC_ALPHA
:
580 dR
= dG
= dB
= (GLfloat
) 1.0F
- (GLfloat
) As
* ascale
;
583 dR
= dG
= dB
= (GLfloat
) Ad
* ascale
;
585 case GL_ONE_MINUS_DST_ALPHA
:
586 dR
= dG
= dB
= 1.0F
- (GLfloat
) Ad
* ascale
;
588 case GL_CONSTANT_COLOR
:
589 dR
= ctx
->Color
.BlendColor
[0];
590 dG
= ctx
->Color
.BlendColor
[1];
591 dB
= ctx
->Color
.BlendColor
[2];
593 case GL_ONE_MINUS_CONSTANT_COLOR
:
594 dR
= 1.0F
- ctx
->Color
.BlendColor
[0];
595 dG
= 1.0F
- ctx
->Color
.BlendColor
[1];
596 dB
= 1.0F
- ctx
->Color
.BlendColor
[2];
598 case GL_CONSTANT_ALPHA
:
599 dR
= dG
= dB
= ctx
->Color
.BlendColor
[3];
601 case GL_ONE_MINUS_CONSTANT_ALPHA
:
602 dR
= dG
= dB
= 1.0F
- ctx
->Color
.BlendColor
[3] * ascale
;
605 /* this should never happen */
607 gl_problem(ctx
, "Bad blend dest RGB factor in do_blend");
610 /* Dest Alpha factor */
611 switch (ctx
->Color
.BlendDstA
) {
619 dA
= (GLfloat
) As
* ascale
;
621 case GL_ONE_MINUS_SRC_COLOR
:
622 dA
= 1.0F
- (GLfloat
) As
* ascale
;
625 dA
= (GLfloat
) As
* ascale
;
627 case GL_ONE_MINUS_SRC_ALPHA
:
628 dA
= (GLfloat
) 1.0F
- (GLfloat
) As
* ascale
;
631 dA
= (GLfloat
) Ad
* ascale
;
633 case GL_ONE_MINUS_DST_ALPHA
:
634 dA
= 1.0F
- (GLfloat
) Ad
* ascale
;
636 case GL_CONSTANT_COLOR
:
637 dA
= ctx
->Color
.BlendColor
[3];
639 case GL_ONE_MINUS_CONSTANT_COLOR
:
640 dA
= 1.0F
- ctx
->Color
.BlendColor
[3];
642 case GL_CONSTANT_ALPHA
:
643 dA
= ctx
->Color
.BlendColor
[3];
645 case GL_ONE_MINUS_CONSTANT_ALPHA
:
646 dA
= 1.0F
- ctx
->Color
.BlendColor
[3] * ascale
;
649 /* this should never happen */
651 gl_problem(ctx
, "Bad blend dest A factor in do_blend");
655 /* Due to round-off problems we have to clamp against zero. */
656 /* Optimization: we don't have to do this for all src & dst factors */
657 if (dA
< 0.0F
) dA
= 0.0F
;
658 if (dR
< 0.0F
) dR
= 0.0F
;
659 if (dG
< 0.0F
) dG
= 0.0F
;
660 if (dB
< 0.0F
) dB
= 0.0F
;
661 if (sA
< 0.0F
) sA
= 0.0F
;
662 if (sR
< 0.0F
) sR
= 0.0F
;
663 if (sG
< 0.0F
) sG
= 0.0F
;
664 if (sB
< 0.0F
) sB
= 0.0F
;
675 /* compute blended color */
676 if (ctx
->Color
.BlendEquation
==GL_FUNC_ADD_EXT
) {
677 r
= Rs
* sR
+ Rd
* dR
;
678 g
= Gs
* sG
+ Gd
* dG
;
679 b
= Bs
* sB
+ Bd
* dB
;
680 a
= As
* sA
+ Ad
* dA
;
682 else if (ctx
->Color
.BlendEquation
==GL_FUNC_SUBTRACT_EXT
) {
683 r
= Rs
* sR
- Rd
* dR
;
684 g
= Gs
* sG
- Gd
* dG
;
685 b
= Bs
* sB
- Bd
* dB
;
686 a
= As
* sA
- Ad
* dA
;
688 else if (ctx
->Color
.BlendEquation
==GL_FUNC_REVERSE_SUBTRACT_EXT
) {
689 r
= Rd
* dR
- Rs
* sR
;
690 g
= Gd
* dG
- Gs
* sG
;
691 b
= Bd
* dB
- Bs
* sB
;
692 a
= Ad
* dA
- As
* sA
;
695 /* should never get here */
696 r
= g
= b
= a
= 0.0F
; /* silence uninitialized var warning */
697 gl_problem(ctx
, "unexpected BlendEquation in blend_general()");
701 rgba
[i
][RCOMP
] = (GLubyte
) (GLint
) CLAMP( r
, 0.0F
, 255.0F
);
702 rgba
[i
][GCOMP
] = (GLubyte
) (GLint
) CLAMP( g
, 0.0F
, 255.0F
);
703 rgba
[i
][BCOMP
] = (GLubyte
) (GLint
) CLAMP( b
, 0.0F
, 255.0F
);
704 rgba
[i
][ACOMP
] = (GLubyte
) (GLint
) CLAMP( a
, 0.0F
, 255.0F
);
711 #if defined(USE_MMX_ASM)
713 #include "X86/common_x86asm.h"
718 * Analyze current blending parameters to pick fastest blending function.
719 * Result: the ctx->Color.BlendFunc pointer is updated.
721 static void set_blend_function( GLcontext
*ctx
)
723 const GLenum eq
= ctx
->Color
.BlendEquation
;
724 const GLenum srcRGB
= ctx
->Color
.BlendSrcRGB
;
725 const GLenum dstRGB
= ctx
->Color
.BlendDstRGB
;
726 const GLenum srcA
= ctx
->Color
.BlendSrcA
;
727 const GLenum dstA
= ctx
->Color
.BlendDstA
;
729 #if defined(USE_MMX_ASM)
730 /* Hmm. A table here would have 12^4 == way too many entries.
731 * Provide a hook for MMX instead.
733 if (gl_x86_cpu_features
& GL_CPU_MMX
) {
734 gl_mmx_set_blend_function (ctx
);
737 if (srcRGB
!= srcA
|| dstRGB
!= dstA
) {
738 ctx
->Color
.BlendFunc
= blend_general
;
740 else if (eq
==GL_FUNC_ADD_EXT
&& srcRGB
==GL_SRC_ALPHA
741 && dstRGB
==GL_ONE_MINUS_SRC_ALPHA
) {
742 ctx
->Color
.BlendFunc
= blend_transparency
;
744 else if (eq
==GL_FUNC_ADD_EXT
&& srcRGB
==GL_ONE
&& dstRGB
==GL_ONE
) {
745 ctx
->Color
.BlendFunc
= blend_add
;
747 else if (((eq
==GL_FUNC_ADD_EXT
|| eq
==GL_FUNC_REVERSE_SUBTRACT_EXT
)
748 && (srcRGB
==GL_ZERO
&& dstRGB
==GL_SRC_COLOR
))
750 ((eq
==GL_FUNC_ADD_EXT
|| eq
==GL_FUNC_SUBTRACT_EXT
)
751 && (srcRGB
==GL_DST_COLOR
&& dstRGB
==GL_ZERO
))) {
752 ctx
->Color
.BlendFunc
= blend_modulate
;
754 else if (eq
==GL_MIN_EXT
) {
755 ctx
->Color
.BlendFunc
= blend_min
;
757 else if (eq
==GL_MAX_EXT
) {
758 ctx
->Color
.BlendFunc
= blend_max
;
761 ctx
->Color
.BlendFunc
= blend_general
;
768 * Apply the blending operator to a span of pixels.
769 * Input: n - number of pixels in span
770 * x, y - location of leftmost pixel in span in window coords.
771 * mask - boolean mask indicating which pixels to blend.
772 * In/Out: rgba - pixel values
774 void gl_blend_span( GLcontext
*ctx
, GLuint n
, GLint x
, GLint y
,
775 GLubyte rgba
[][4], const GLubyte mask
[] )
777 GLubyte dest
[MAX_WIDTH
][4];
779 /* Check if device driver can do the work */
780 if (ctx
->Color
.BlendEquation
==GL_LOGIC_OP
&& !ctx
->Color
.SWLogicOpEnabled
) {
784 /* Read span of current frame buffer pixels */
785 gl_read_rgba_span( ctx
, n
, x
, y
, dest
);
787 if (!ctx
->Color
.BlendFunc
)
788 set_blend_function(ctx
);
790 (*ctx
->Color
.BlendFunc
)( ctx
, n
, mask
, rgba
, (const GLubyte (*)[4])dest
);
798 * Apply the blending operator to an array of pixels.
799 * Input: n - number of pixels in span
800 * x, y - array of pixel locations
801 * mask - boolean mask indicating which pixels to blend.
802 * In/Out: rgba - pixel values
804 void gl_blend_pixels( GLcontext
*ctx
,
805 GLuint n
, const GLint x
[], const GLint y
[],
806 GLubyte rgba
[][4], const GLubyte mask
[] )
808 GLubyte dest
[PB_SIZE
][4];
810 /* Check if device driver can do the work */
811 if (ctx
->Color
.BlendEquation
==GL_LOGIC_OP
&& !ctx
->Color
.SWLogicOpEnabled
) {
815 /* Read pixels from current color buffer */
816 (*ctx
->Driver
.ReadRGBAPixels
)( ctx
, n
, x
, y
, dest
, mask
);
817 if (ctx
->RasterMask
& ALPHABUF_BIT
) {
818 gl_read_alpha_pixels( ctx
, n
, x
, y
, dest
, mask
);
821 if (!ctx
->Color
.BlendFunc
)
822 set_blend_function(ctx
);
824 (*ctx
->Color
.BlendFunc
)( ctx
, n
, mask
, rgba
, (const GLubyte (*)[4])dest
);