1 /* $Id: blend.c,v 1.22 2000/10/30 13:31:59 keithw Exp $ */
4 * Mesa 3-D graphics library
7 * Copyright (C) 1999-2000 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.
45 _mesa_BlendFunc( GLenum sfactor
, GLenum dfactor
)
47 GET_CURRENT_CONTEXT(ctx
);
48 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
, "glBlendFunc");
50 if (MESA_VERBOSE
& (VERBOSE_API
|VERBOSE_TEXTURE
))
51 fprintf(stderr
, "glBlendFunc %s %s\n",
52 gl_lookup_enum_by_nr(sfactor
),
53 gl_lookup_enum_by_nr(dfactor
));
57 case GL_ONE_MINUS_SRC_COLOR
:
58 if (!ctx
->Extensions
.NV_blend_square
) {
59 gl_error( ctx
, GL_INVALID_ENUM
, "glBlendFunc(sfactor)" );
66 case GL_ONE_MINUS_DST_COLOR
:
68 case GL_ONE_MINUS_SRC_ALPHA
:
70 case GL_ONE_MINUS_DST_ALPHA
:
71 case GL_SRC_ALPHA_SATURATE
:
72 case GL_CONSTANT_COLOR
:
73 case GL_ONE_MINUS_CONSTANT_COLOR
:
74 case GL_CONSTANT_ALPHA
:
75 case GL_ONE_MINUS_CONSTANT_ALPHA
:
76 ctx
->Color
.BlendSrcRGB
= ctx
->Color
.BlendSrcA
= sfactor
;
79 gl_error( ctx
, GL_INVALID_ENUM
, "glBlendFunc(sfactor)" );
85 case GL_ONE_MINUS_DST_COLOR
:
86 if (!ctx
->Extensions
.NV_blend_square
) {
87 gl_error( ctx
, GL_INVALID_ENUM
, "glBlendFunc(dfactor)" );
94 case GL_ONE_MINUS_SRC_COLOR
:
96 case GL_ONE_MINUS_SRC_ALPHA
:
98 case GL_ONE_MINUS_DST_ALPHA
:
99 case GL_CONSTANT_COLOR
:
100 case GL_ONE_MINUS_CONSTANT_COLOR
:
101 case GL_CONSTANT_ALPHA
:
102 case GL_ONE_MINUS_CONSTANT_ALPHA
:
103 ctx
->Color
.BlendDstRGB
= ctx
->Color
.BlendDstA
= dfactor
;
106 gl_error( ctx
, GL_INVALID_ENUM
, "glBlendFunc(dfactor)" );
110 if (ctx
->Driver
.BlendFunc
) {
111 (*ctx
->Driver
.BlendFunc
)( ctx
, sfactor
, dfactor
);
114 ctx
->Color
.BlendFunc
= NULL
;
115 ctx
->NewState
|= _NEW_COLOR
;
119 /* GL_EXT_blend_func_separate */
121 _mesa_BlendFuncSeparateEXT( GLenum sfactorRGB
, GLenum dfactorRGB
,
122 GLenum sfactorA
, GLenum dfactorA
)
124 GET_CURRENT_CONTEXT(ctx
);
125 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
, "glBlendFuncSeparate");
127 if (MESA_VERBOSE
& (VERBOSE_API
|VERBOSE_TEXTURE
))
128 fprintf(stderr
, "glBlendFuncSeperate %s %s %s %s\n",
129 gl_lookup_enum_by_nr(sfactorRGB
),
130 gl_lookup_enum_by_nr(dfactorRGB
),
131 gl_lookup_enum_by_nr(sfactorA
),
132 gl_lookup_enum_by_nr(dfactorA
));
134 switch (sfactorRGB
) {
136 case GL_ONE_MINUS_SRC_COLOR
:
137 if (!ctx
->Extensions
.NV_blend_square
) {
138 gl_error(ctx
, GL_INVALID_ENUM
, "glBlendFuncSeparate(sfactorRGB)");
145 case GL_ONE_MINUS_DST_COLOR
:
147 case GL_ONE_MINUS_SRC_ALPHA
:
149 case GL_ONE_MINUS_DST_ALPHA
:
150 case GL_SRC_ALPHA_SATURATE
:
151 case GL_CONSTANT_COLOR
:
152 case GL_ONE_MINUS_CONSTANT_COLOR
:
153 case GL_CONSTANT_ALPHA
:
154 case GL_ONE_MINUS_CONSTANT_ALPHA
:
155 ctx
->Color
.BlendSrcRGB
= sfactorRGB
;
158 gl_error(ctx
, GL_INVALID_ENUM
, "glBlendFuncSeparate(sfactorRGB)");
162 switch (dfactorRGB
) {
164 case GL_ONE_MINUS_DST_COLOR
:
165 if (!ctx
->Extensions
.NV_blend_square
) {
166 gl_error(ctx
, GL_INVALID_ENUM
, "glBlendFuncSeparate(dfactorRGB)");
173 case GL_ONE_MINUS_SRC_COLOR
:
175 case GL_ONE_MINUS_SRC_ALPHA
:
177 case GL_ONE_MINUS_DST_ALPHA
:
178 case GL_CONSTANT_COLOR
:
179 case GL_ONE_MINUS_CONSTANT_COLOR
:
180 case GL_CONSTANT_ALPHA
:
181 case GL_ONE_MINUS_CONSTANT_ALPHA
:
182 ctx
->Color
.BlendDstRGB
= dfactorRGB
;
185 gl_error(ctx
, GL_INVALID_ENUM
, "glBlendFuncSeparate(dfactorRGB)");
191 case GL_ONE_MINUS_SRC_COLOR
:
192 if (!ctx
->Extensions
.NV_blend_square
) {
193 gl_error(ctx
, GL_INVALID_ENUM
, "glBlendFuncSeparate(sfactorA)");
200 case GL_ONE_MINUS_DST_COLOR
:
202 case GL_ONE_MINUS_SRC_ALPHA
:
204 case GL_ONE_MINUS_DST_ALPHA
:
205 case GL_SRC_ALPHA_SATURATE
:
206 case GL_CONSTANT_COLOR
:
207 case GL_ONE_MINUS_CONSTANT_COLOR
:
208 case GL_CONSTANT_ALPHA
:
209 case GL_ONE_MINUS_CONSTANT_ALPHA
:
210 ctx
->Color
.BlendSrcA
= sfactorA
;
213 gl_error(ctx
, GL_INVALID_ENUM
, "glBlendFuncSeparate(sfactorA)");
219 case GL_ONE_MINUS_DST_COLOR
:
220 if (!ctx
->Extensions
.NV_blend_square
) {
221 gl_error(ctx
, GL_INVALID_ENUM
, "glBlendFuncSeparate(dfactorA)");
228 case GL_ONE_MINUS_SRC_COLOR
:
230 case GL_ONE_MINUS_SRC_ALPHA
:
232 case GL_ONE_MINUS_DST_ALPHA
:
233 case GL_CONSTANT_COLOR
:
234 case GL_ONE_MINUS_CONSTANT_COLOR
:
235 case GL_CONSTANT_ALPHA
:
236 case GL_ONE_MINUS_CONSTANT_ALPHA
:
237 ctx
->Color
.BlendDstA
= dfactorA
;
240 gl_error( ctx
, GL_INVALID_ENUM
, "glBlendFuncSeparate(dfactorA)" );
244 ctx
->Color
.BlendFunc
= NULL
;
245 ctx
->NewState
|= _NEW_COLOR
;
247 if (ctx
->Driver
.BlendFuncSeparate
) {
248 (*ctx
->Driver
.BlendFuncSeparate
)( ctx
, sfactorRGB
, dfactorRGB
,
249 sfactorA
, dfactorA
);
255 /* This is really an extension function! */
257 _mesa_BlendEquation( GLenum mode
)
259 GET_CURRENT_CONTEXT(ctx
);
260 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
, "glBlendEquation");
262 if (MESA_VERBOSE
& (VERBOSE_API
|VERBOSE_TEXTURE
))
263 fprintf(stderr
, "glBlendEquation %s\n",
264 gl_lookup_enum_by_nr(mode
));
269 case GL_FUNC_ADD_EXT
:
270 if (ctx
->Extensions
.EXT_blend_minmax
) {
271 ctx
->Color
.BlendEquation
= mode
;
274 gl_error(ctx
, GL_INVALID_ENUM
, "glBlendEquation");
278 ctx
->Color
.BlendEquation
= mode
;
280 case GL_FUNC_SUBTRACT_EXT
:
281 case GL_FUNC_REVERSE_SUBTRACT_EXT
:
282 if (ctx
->Extensions
.EXT_blend_subtract
) {
283 ctx
->Color
.BlendEquation
= mode
;
286 gl_error(ctx
, GL_INVALID_ENUM
, "glBlendEquation");
291 gl_error( ctx
, GL_INVALID_ENUM
, "glBlendEquation" );
295 /* This is needed to support 1.1's RGB logic ops AND
296 * 1.0's blending logicops.
298 if (mode
==GL_LOGIC_OP
&& ctx
->Color
.BlendEnabled
) {
299 ctx
->Color
.ColorLogicOpEnabled
= GL_TRUE
;
302 ctx
->Color
.ColorLogicOpEnabled
= GL_FALSE
;
305 ctx
->Color
.BlendFunc
= NULL
;
306 ctx
->NewState
|= _NEW_COLOR
;
308 if (ctx
->Driver
.BlendEquation
)
309 ctx
->Driver
.BlendEquation( ctx
, mode
);
315 _mesa_BlendColor( GLclampf red
, GLclampf green
, GLclampf blue
, GLclampf alpha
)
317 GET_CURRENT_CONTEXT(ctx
);
318 ctx
->Color
.BlendColor
[0] = CLAMP( red
, 0.0F
, 1.0F
);
319 ctx
->Color
.BlendColor
[1] = CLAMP( green
, 0.0F
, 1.0F
);
320 ctx
->Color
.BlendColor
[2] = CLAMP( blue
, 0.0F
, 1.0F
);
321 ctx
->Color
.BlendColor
[3] = CLAMP( alpha
, 0.0F
, 1.0F
);
322 ctx
->NewState
|= _NEW_COLOR
;
326 #define _BLENDAPI _ASMAPI
332 * Common transparency blending mode.
334 static void _BLENDAPI
335 blend_transparency( GLcontext
*ctx
, GLuint n
, const GLubyte mask
[],
336 GLchan rgba
[][4], CONST GLchan dest
[][4] )
339 ASSERT(ctx
->Color
.BlendEquation
==GL_FUNC_ADD_EXT
);
340 ASSERT(ctx
->Color
.BlendSrcRGB
==GL_SRC_ALPHA
);
341 ASSERT(ctx
->Color
.BlendDstRGB
==GL_ONE_MINUS_SRC_ALPHA
);
346 const GLint t
= rgba
[i
][ACOMP
]; /* t in [0, CHAN_MAX] */
349 rgba
[i
][RCOMP
] = dest
[i
][RCOMP
];
350 rgba
[i
][GCOMP
] = dest
[i
][GCOMP
];
351 rgba
[i
][BCOMP
] = dest
[i
][BCOMP
];
352 rgba
[i
][ACOMP
] = dest
[i
][ACOMP
];
354 else if (t
== CHAN_MAX
) {
355 /* 100% alpha, no-op */
359 /* This is pretty close, but Glean complains */
360 const GLint s
= CHAN_MAX
- t
;
361 const GLint r
= (rgba
[i
][RCOMP
] * t
+ dest
[i
][RCOMP
] * s
+ 1) >> 8;
362 const GLint g
= (rgba
[i
][GCOMP
] * t
+ dest
[i
][GCOMP
] * s
+ 1) >> 8;
363 const GLint b
= (rgba
[i
][BCOMP
] * t
+ dest
[i
][BCOMP
] * s
+ 1) >> 8;
364 const GLint a
= (rgba
[i
][ACOMP
] * t
+ dest
[i
][ACOMP
] * s
+ 1) >> 8;
366 /* This is slower but satisfies Glean */
367 const GLint s
= CHAN_MAX
- t
;
368 const GLint r
= (rgba
[i
][RCOMP
] * t
+ dest
[i
][RCOMP
] * s
) / 255;
369 const GLint g
= (rgba
[i
][GCOMP
] * t
+ dest
[i
][GCOMP
] * s
) / 255;
370 const GLint b
= (rgba
[i
][BCOMP
] * t
+ dest
[i
][BCOMP
] * s
) / 255;
371 const GLint a
= (rgba
[i
][ACOMP
] * t
+ dest
[i
][ACOMP
] * s
) / 255;
373 /* This satisfies Glean and should be reasonably fast */
374 /* Contributed by Nathan Hand */
376 #define DIV255(X) (((X) << 8) + (X) + 256) >> 16
380 const GLint s
= CHAN_MAX
- t
;
381 const GLint r
= DIV255(rgba
[i
][RCOMP
] * t
+ dest
[i
][RCOMP
] * s
);
382 const GLint g
= DIV255(rgba
[i
][GCOMP
] * t
+ dest
[i
][GCOMP
] * s
);
383 const GLint b
= DIV255(rgba
[i
][BCOMP
] * t
+ dest
[i
][BCOMP
] * s
);
384 const GLint a
= DIV255(rgba
[i
][ACOMP
] * t
+ dest
[i
][ACOMP
] * s
);
387 ASSERT(r
<= CHAN_MAX
);
388 ASSERT(g
<= CHAN_MAX
);
389 ASSERT(b
<= CHAN_MAX
);
390 ASSERT(a
<= CHAN_MAX
);
391 rgba
[i
][RCOMP
] = (GLchan
) r
;
392 rgba
[i
][GCOMP
] = (GLchan
) g
;
393 rgba
[i
][BCOMP
] = (GLchan
) b
;
394 rgba
[i
][ACOMP
] = (GLchan
) a
;
405 static void _BLENDAPI
406 blend_add( GLcontext
*ctx
, GLuint n
, const GLubyte mask
[],
407 GLchan rgba
[][4], CONST GLchan dest
[][4] )
410 ASSERT(ctx
->Color
.BlendEquation
==GL_FUNC_ADD_EXT
);
411 ASSERT(ctx
->Color
.BlendSrcRGB
==GL_ONE
);
412 ASSERT(ctx
->Color
.BlendDstRGB
==GL_ONE
);
417 GLint r
= rgba
[i
][RCOMP
] + dest
[i
][RCOMP
];
418 GLint g
= rgba
[i
][GCOMP
] + dest
[i
][GCOMP
];
419 GLint b
= rgba
[i
][BCOMP
] + dest
[i
][BCOMP
];
420 GLint a
= rgba
[i
][ACOMP
] + dest
[i
][ACOMP
];
421 rgba
[i
][RCOMP
] = (GLchan
) MIN2( r
, CHAN_MAX
);
422 rgba
[i
][GCOMP
] = (GLchan
) MIN2( g
, CHAN_MAX
);
423 rgba
[i
][BCOMP
] = (GLchan
) MIN2( b
, CHAN_MAX
);
424 rgba
[i
][ACOMP
] = (GLchan
) MIN2( a
, CHAN_MAX
);
432 * Blend min function (for GL_EXT_blend_minmax)
434 static void _BLENDAPI
435 blend_min( GLcontext
*ctx
, GLuint n
, const GLubyte mask
[],
436 GLchan rgba
[][4], CONST GLchan dest
[][4] )
439 ASSERT(ctx
->Color
.BlendEquation
==GL_MIN_EXT
);
444 rgba
[i
][RCOMP
] = (GLchan
) MIN2( rgba
[i
][RCOMP
], dest
[i
][RCOMP
] );
445 rgba
[i
][GCOMP
] = (GLchan
) MIN2( rgba
[i
][GCOMP
], dest
[i
][GCOMP
] );
446 rgba
[i
][BCOMP
] = (GLchan
) MIN2( rgba
[i
][BCOMP
], dest
[i
][BCOMP
] );
447 rgba
[i
][ACOMP
] = (GLchan
) MIN2( rgba
[i
][ACOMP
], dest
[i
][ACOMP
] );
455 * Blend max function (for GL_EXT_blend_minmax)
457 static void _BLENDAPI
458 blend_max( GLcontext
*ctx
, GLuint n
, const GLubyte mask
[],
459 GLchan rgba
[][4], CONST GLchan dest
[][4] )
462 ASSERT(ctx
->Color
.BlendEquation
==GL_MAX_EXT
);
467 rgba
[i
][RCOMP
] = (GLchan
) MAX2( rgba
[i
][RCOMP
], dest
[i
][RCOMP
] );
468 rgba
[i
][GCOMP
] = (GLchan
) MAX2( rgba
[i
][GCOMP
], dest
[i
][GCOMP
] );
469 rgba
[i
][BCOMP
] = (GLchan
) MAX2( rgba
[i
][BCOMP
], dest
[i
][BCOMP
] );
470 rgba
[i
][ACOMP
] = (GLchan
) MAX2( rgba
[i
][ACOMP
], dest
[i
][ACOMP
] );
478 * Modulate: result = src * dest
480 static void _BLENDAPI
481 blend_modulate( GLcontext
*ctx
, GLuint n
, const GLubyte mask
[],
482 GLchan rgba
[][4], CONST GLchan dest
[][4] )
489 GLint r
= (rgba
[i
][RCOMP
] * dest
[i
][RCOMP
]) >> 8;
490 GLint g
= (rgba
[i
][GCOMP
] * dest
[i
][GCOMP
]) >> 8;
491 GLint b
= (rgba
[i
][BCOMP
] * dest
[i
][BCOMP
]) >> 8;
492 GLint a
= (rgba
[i
][ACOMP
] * dest
[i
][ACOMP
]) >> 8;
493 rgba
[i
][RCOMP
] = (GLchan
) r
;
494 rgba
[i
][GCOMP
] = (GLchan
) g
;
495 rgba
[i
][BCOMP
] = (GLchan
) b
;
496 rgba
[i
][ACOMP
] = (GLchan
) a
;
504 * General case blend pixels.
505 * Input: n - number of pixels
506 * mask - the usual write mask
507 * In/Out: rgba - the incoming and modified pixels
508 * Input: dest - the pixels from the dest color buffer
510 static void _BLENDAPI
511 blend_general( GLcontext
*ctx
, GLuint n
, const GLubyte mask
[],
512 GLchan rgba
[][4], CONST GLchan dest
[][4] )
514 GLfloat rscale
= 1.0F
/ CHAN_MAXF
;
515 GLfloat gscale
= 1.0F
/ CHAN_MAXF
;
516 GLfloat bscale
= 1.0F
/ CHAN_MAXF
;
517 GLfloat ascale
= 1.0F
/ CHAN_MAXF
;
522 GLint Rs
, Gs
, Bs
, As
; /* Source colors */
523 GLint Rd
, Gd
, Bd
, Ad
; /* Dest colors */
524 GLfloat sR
, sG
, sB
, sA
; /* Source scaling */
525 GLfloat dR
, dG
, dB
, dA
; /* Dest scaling */
534 /* Frame buffer color */
540 /* Source RGB factor */
541 switch (ctx
->Color
.BlendSrcRGB
) {
549 sR
= (GLfloat
) Rd
* rscale
;
550 sG
= (GLfloat
) Gd
* gscale
;
551 sB
= (GLfloat
) Bd
* bscale
;
553 case GL_ONE_MINUS_DST_COLOR
:
554 sR
= 1.0F
- (GLfloat
) Rd
* rscale
;
555 sG
= 1.0F
- (GLfloat
) Gd
* gscale
;
556 sB
= 1.0F
- (GLfloat
) Bd
* bscale
;
559 sR
= sG
= sB
= (GLfloat
) As
* ascale
;
561 case GL_ONE_MINUS_SRC_ALPHA
:
562 sR
= sG
= sB
= (GLfloat
) 1.0F
- (GLfloat
) As
* ascale
;
565 sR
= sG
= sB
= (GLfloat
) Ad
* ascale
;
567 case GL_ONE_MINUS_DST_ALPHA
:
568 sR
= sG
= sB
= 1.0F
- (GLfloat
) Ad
* ascale
;
570 case GL_SRC_ALPHA_SATURATE
:
571 if (As
< CHAN_MAX
- Ad
) {
572 sR
= sG
= sB
= (GLfloat
) As
* ascale
;
575 sR
= sG
= sB
= 1.0F
- (GLfloat
) Ad
* ascale
;
578 case GL_CONSTANT_COLOR
:
579 sR
= ctx
->Color
.BlendColor
[0];
580 sG
= ctx
->Color
.BlendColor
[1];
581 sB
= ctx
->Color
.BlendColor
[2];
583 case GL_ONE_MINUS_CONSTANT_COLOR
:
584 sR
= 1.0F
- ctx
->Color
.BlendColor
[0];
585 sG
= 1.0F
- ctx
->Color
.BlendColor
[1];
586 sB
= 1.0F
- ctx
->Color
.BlendColor
[2];
588 case GL_CONSTANT_ALPHA
:
589 sR
= sG
= sB
= ctx
->Color
.BlendColor
[3];
591 case GL_ONE_MINUS_CONSTANT_ALPHA
:
592 sR
= sG
= sB
= 1.0F
- ctx
->Color
.BlendColor
[3];
594 case GL_SRC_COLOR
: /* GL_NV_blend_square */
595 sR
= (GLfloat
) Rs
* rscale
;
596 sG
= (GLfloat
) Gs
* gscale
;
597 sB
= (GLfloat
) Bs
* bscale
;
599 case GL_ONE_MINUS_SRC_COLOR
: /* GL_NV_blend_square */
600 sR
= 1.0F
- (GLfloat
) Rs
* rscale
;
601 sG
= 1.0F
- (GLfloat
) Gs
* gscale
;
602 sB
= 1.0F
- (GLfloat
) Bs
* bscale
;
605 /* this should never happen */
606 gl_problem(ctx
, "Bad blend source RGB factor in do_blend");
610 /* Source Alpha factor */
611 switch (ctx
->Color
.BlendSrcA
) {
619 sA
= (GLfloat
) Ad
* ascale
;
621 case GL_ONE_MINUS_DST_COLOR
:
622 sA
= 1.0F
- (GLfloat
) Ad
* ascale
;
625 sA
= (GLfloat
) As
* ascale
;
627 case GL_ONE_MINUS_SRC_ALPHA
:
628 sA
= (GLfloat
) 1.0F
- (GLfloat
) As
* ascale
;
631 sA
=(GLfloat
) Ad
* ascale
;
633 case GL_ONE_MINUS_DST_ALPHA
:
634 sA
= 1.0F
- (GLfloat
) Ad
* ascale
;
636 case GL_SRC_ALPHA_SATURATE
:
639 case GL_CONSTANT_COLOR
:
640 sA
= ctx
->Color
.BlendColor
[3];
642 case GL_ONE_MINUS_CONSTANT_COLOR
:
643 sA
= 1.0F
- ctx
->Color
.BlendColor
[3];
645 case GL_CONSTANT_ALPHA
:
646 sA
= ctx
->Color
.BlendColor
[3];
648 case GL_ONE_MINUS_CONSTANT_ALPHA
:
649 sA
= 1.0F
- ctx
->Color
.BlendColor
[3];
651 case GL_SRC_COLOR
: /* GL_NV_blend_square */
652 sA
= (GLfloat
) As
* ascale
;
654 case GL_ONE_MINUS_SRC_COLOR
: /* GL_NV_blend_square */
655 sA
= 1.0F
- (GLfloat
) As
* ascale
;
658 /* this should never happen */
660 gl_problem(ctx
, "Bad blend source A factor in do_blend");
663 /* Dest RGB factor */
664 switch (ctx
->Color
.BlendDstRGB
) {
672 dR
= (GLfloat
) Rs
* rscale
;
673 dG
= (GLfloat
) Gs
* gscale
;
674 dB
= (GLfloat
) Bs
* bscale
;
676 case GL_ONE_MINUS_SRC_COLOR
:
677 dR
= 1.0F
- (GLfloat
) Rs
* rscale
;
678 dG
= 1.0F
- (GLfloat
) Gs
* gscale
;
679 dB
= 1.0F
- (GLfloat
) Bs
* bscale
;
682 dR
= dG
= dB
= (GLfloat
) As
* ascale
;
684 case GL_ONE_MINUS_SRC_ALPHA
:
685 dR
= dG
= dB
= (GLfloat
) 1.0F
- (GLfloat
) As
* ascale
;
688 dR
= dG
= dB
= (GLfloat
) Ad
* ascale
;
690 case GL_ONE_MINUS_DST_ALPHA
:
691 dR
= dG
= dB
= 1.0F
- (GLfloat
) Ad
* ascale
;
693 case GL_CONSTANT_COLOR
:
694 dR
= ctx
->Color
.BlendColor
[0];
695 dG
= ctx
->Color
.BlendColor
[1];
696 dB
= ctx
->Color
.BlendColor
[2];
698 case GL_ONE_MINUS_CONSTANT_COLOR
:
699 dR
= 1.0F
- ctx
->Color
.BlendColor
[0];
700 dG
= 1.0F
- ctx
->Color
.BlendColor
[1];
701 dB
= 1.0F
- ctx
->Color
.BlendColor
[2];
703 case GL_CONSTANT_ALPHA
:
704 dR
= dG
= dB
= ctx
->Color
.BlendColor
[3];
706 case GL_ONE_MINUS_CONSTANT_ALPHA
:
707 dR
= dG
= dB
= 1.0F
- ctx
->Color
.BlendColor
[3];
709 case GL_DST_COLOR
: /* GL_NV_blend_square */
710 dR
= (GLfloat
) Rd
* rscale
;
711 dG
= (GLfloat
) Gd
* gscale
;
712 dB
= (GLfloat
) Bd
* bscale
;
714 case GL_ONE_MINUS_DST_COLOR
: /* GL_NV_blend_square */
715 dR
= 1.0F
- (GLfloat
) Rd
* rscale
;
716 dG
= 1.0F
- (GLfloat
) Gd
* gscale
;
717 dB
= 1.0F
- (GLfloat
) Bd
* bscale
;
720 /* this should never happen */
722 gl_problem(ctx
, "Bad blend dest RGB factor in do_blend");
725 /* Dest Alpha factor */
726 switch (ctx
->Color
.BlendDstA
) {
734 dA
= (GLfloat
) As
* ascale
;
736 case GL_ONE_MINUS_SRC_COLOR
:
737 dA
= 1.0F
- (GLfloat
) As
* ascale
;
740 dA
= (GLfloat
) As
* ascale
;
742 case GL_ONE_MINUS_SRC_ALPHA
:
743 dA
= (GLfloat
) 1.0F
- (GLfloat
) As
* ascale
;
746 dA
= (GLfloat
) Ad
* ascale
;
748 case GL_ONE_MINUS_DST_ALPHA
:
749 dA
= 1.0F
- (GLfloat
) Ad
* ascale
;
751 case GL_CONSTANT_COLOR
:
752 dA
= ctx
->Color
.BlendColor
[3];
754 case GL_ONE_MINUS_CONSTANT_COLOR
:
755 dA
= 1.0F
- ctx
->Color
.BlendColor
[3];
757 case GL_CONSTANT_ALPHA
:
758 dA
= ctx
->Color
.BlendColor
[3];
760 case GL_ONE_MINUS_CONSTANT_ALPHA
:
761 dA
= 1.0F
- ctx
->Color
.BlendColor
[3];
763 case GL_DST_COLOR
: /* GL_NV_blend_square */
764 dA
= (GLfloat
) Ad
* ascale
;
766 case GL_ONE_MINUS_DST_COLOR
: /* GL_NV_blend_square */
767 dA
= 1.0F
- (GLfloat
) Ad
* ascale
;
770 /* this should never happen */
772 gl_problem(ctx
, "Bad blend dest A factor in do_blend");
776 /* Due to round-off problems we have to clamp against zero. */
777 /* Optimization: we don't have to do this for all src & dst factors */
778 if (dA
< 0.0F
) dA
= 0.0F
;
779 if (dR
< 0.0F
) dR
= 0.0F
;
780 if (dG
< 0.0F
) dG
= 0.0F
;
781 if (dB
< 0.0F
) dB
= 0.0F
;
782 if (sA
< 0.0F
) sA
= 0.0F
;
783 if (sR
< 0.0F
) sR
= 0.0F
;
784 if (sG
< 0.0F
) sG
= 0.0F
;
785 if (sB
< 0.0F
) sB
= 0.0F
;
796 /* compute blended color */
797 if (ctx
->Color
.BlendEquation
==GL_FUNC_ADD_EXT
) {
798 r
= Rs
* sR
+ Rd
* dR
+ 0.5F
;
799 g
= Gs
* sG
+ Gd
* dG
+ 0.5F
;
800 b
= Bs
* sB
+ Bd
* dB
+ 0.5F
;
801 a
= As
* sA
+ Ad
* dA
+ 0.5F
;
803 else if (ctx
->Color
.BlendEquation
==GL_FUNC_SUBTRACT_EXT
) {
804 r
= Rs
* sR
- Rd
* dR
+ 0.5F
;
805 g
= Gs
* sG
- Gd
* dG
+ 0.5F
;
806 b
= Bs
* sB
- Bd
* dB
+ 0.5F
;
807 a
= As
* sA
- Ad
* dA
+ 0.5F
;
809 else if (ctx
->Color
.BlendEquation
==GL_FUNC_REVERSE_SUBTRACT_EXT
) {
810 r
= Rd
* dR
- Rs
* sR
+ 0.5F
;
811 g
= Gd
* dG
- Gs
* sG
+ 0.5F
;
812 b
= Bd
* dB
- Bs
* sB
+ 0.5F
;
813 a
= Ad
* dA
- As
* sA
+ 0.5F
;
816 /* should never get here */
817 r
= g
= b
= a
= 0.0F
; /* silence uninitialized var warning */
818 gl_problem(ctx
, "unexpected BlendEquation in blend_general()");
822 rgba
[i
][RCOMP
] = (GLchan
) (GLint
) CLAMP( r
, 0.0F
, CHAN_MAXF
);
823 rgba
[i
][GCOMP
] = (GLchan
) (GLint
) CLAMP( g
, 0.0F
, CHAN_MAXF
);
824 rgba
[i
][BCOMP
] = (GLchan
) (GLint
) CLAMP( b
, 0.0F
, CHAN_MAXF
);
825 rgba
[i
][ACOMP
] = (GLchan
) (GLint
) CLAMP( a
, 0.0F
, CHAN_MAXF
);
832 #if defined(USE_MMX_ASM)
834 #include "X86/common_x86_asm.h"
839 * Analyze current blending parameters to pick fastest blending function.
840 * Result: the ctx->Color.BlendFunc pointer is updated.
842 static void set_blend_function( GLcontext
*ctx
)
844 const GLenum eq
= ctx
->Color
.BlendEquation
;
845 const GLenum srcRGB
= ctx
->Color
.BlendSrcRGB
;
846 const GLenum dstRGB
= ctx
->Color
.BlendDstRGB
;
847 const GLenum srcA
= ctx
->Color
.BlendSrcA
;
848 const GLenum dstA
= ctx
->Color
.BlendDstA
;
850 #if defined(USE_MMX_ASM)
851 /* Hmm. A table here would have 12^4 == way too many entries.
852 * Provide a hook for MMX instead.
855 gl_mmx_set_blend_function( ctx
);
859 if (srcRGB
!= srcA
|| dstRGB
!= dstA
) {
860 ctx
->Color
.BlendFunc
= blend_general
;
862 else if (eq
==GL_FUNC_ADD_EXT
&& srcRGB
==GL_SRC_ALPHA
863 && dstRGB
==GL_ONE_MINUS_SRC_ALPHA
) {
864 ctx
->Color
.BlendFunc
= blend_transparency
;
866 else if (eq
==GL_FUNC_ADD_EXT
&& srcRGB
==GL_ONE
&& dstRGB
==GL_ONE
) {
867 ctx
->Color
.BlendFunc
= blend_add
;
869 else if (((eq
==GL_FUNC_ADD_EXT
|| eq
==GL_FUNC_REVERSE_SUBTRACT_EXT
)
870 && (srcRGB
==GL_ZERO
&& dstRGB
==GL_SRC_COLOR
))
872 ((eq
==GL_FUNC_ADD_EXT
|| eq
==GL_FUNC_SUBTRACT_EXT
)
873 && (srcRGB
==GL_DST_COLOR
&& dstRGB
==GL_ZERO
))) {
874 ctx
->Color
.BlendFunc
= blend_modulate
;
876 else if (eq
==GL_MIN_EXT
) {
877 ctx
->Color
.BlendFunc
= blend_min
;
879 else if (eq
==GL_MAX_EXT
) {
880 ctx
->Color
.BlendFunc
= blend_max
;
883 ctx
->Color
.BlendFunc
= blend_general
;
890 * Apply the blending operator to a span of pixels.
891 * Input: n - number of pixels in span
892 * x, y - location of leftmost pixel in span in window coords.
893 * mask - boolean mask indicating which pixels to blend.
894 * In/Out: rgba - pixel values
897 _mesa_blend_span( GLcontext
*ctx
, GLuint n
, GLint x
, GLint y
,
898 GLchan rgba
[][4], const GLubyte mask
[] )
900 GLchan dest
[MAX_WIDTH
][4];
902 /* Check if device driver can do the work */
903 if (ctx
->Color
.BlendEquation
==GL_LOGIC_OP
&&
904 !ctx
->Color
.ColorLogicOpEnabled
) {
908 /* Read span of current frame buffer pixels */
909 gl_read_rgba_span( ctx
, ctx
->DrawBuffer
, n
, x
, y
, dest
);
911 if (!ctx
->Color
.BlendFunc
)
912 set_blend_function(ctx
);
914 (*ctx
->Color
.BlendFunc
)( ctx
, n
, mask
, rgba
, (const GLchan (*)[4])dest
);
920 * Apply the blending operator to an array of pixels.
921 * Input: n - number of pixels in span
922 * x, y - array of pixel locations
923 * mask - boolean mask indicating which pixels to blend.
924 * In/Out: rgba - pixel values
927 _mesa_blend_pixels( GLcontext
*ctx
,
928 GLuint n
, const GLint x
[], const GLint y
[],
929 GLchan rgba
[][4], const GLchan mask
[] )
931 GLchan dest
[PB_SIZE
][4];
933 /* Check if device driver can do the work */
934 if (ctx
->Color
.BlendEquation
==GL_LOGIC_OP
&&
935 !ctx
->Color
.ColorLogicOpEnabled
) {
939 /* Read pixels from current color buffer */
940 (*ctx
->Driver
.ReadRGBAPixels
)( ctx
, n
, x
, y
, dest
, mask
);
941 if (ctx
->RasterMask
& ALPHABUF_BIT
) {
942 _mesa_read_alpha_pixels( ctx
, n
, x
, y
, dest
, mask
);
945 if (!ctx
->Color
.BlendFunc
)
946 set_blend_function(ctx
);
948 (*ctx
->Color
.BlendFunc
)( ctx
, n
, mask
, rgba
, (const GLchan (*)[4])dest
);