7 * Mesa 3-D graphics library
9 * Copyright (C) 1999-2006 Brian Paul All Rights Reserved.
11 * Permission is hereby granted, free of charge, to any person obtaining a
12 * copy of this software and associated documentation files (the "Software"),
13 * to deal in the Software without restriction, including without limitation
14 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
15 * and/or sell copies of the Software, and to permit persons to whom the
16 * Software is furnished to do so, subject to the following conditions:
18 * The above copyright notice and this permission notice shall be included
19 * in all copies or substantial portions of the Software.
21 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
22 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
24 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
25 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
26 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
27 * OTHER DEALINGS IN THE SOFTWARE.
42 * Check if given blend source factor is legal.
43 * \return GL_TRUE if legal, GL_FALSE otherwise.
46 legal_src_factor(const struct gl_context
*ctx
, GLenum factor
)
50 case GL_ONE_MINUS_SRC_COLOR
:
54 case GL_ONE_MINUS_DST_COLOR
:
56 case GL_ONE_MINUS_SRC_ALPHA
:
58 case GL_ONE_MINUS_DST_ALPHA
:
59 case GL_SRC_ALPHA_SATURATE
:
61 case GL_CONSTANT_COLOR
:
62 case GL_ONE_MINUS_CONSTANT_COLOR
:
63 case GL_CONSTANT_ALPHA
:
64 case GL_ONE_MINUS_CONSTANT_ALPHA
:
65 return _mesa_is_desktop_gl(ctx
) || ctx
->API
== API_OPENGLES2
;
68 case GL_ONE_MINUS_SRC1_COLOR
:
69 case GL_ONE_MINUS_SRC1_ALPHA
:
70 return ctx
->API
!= API_OPENGLES
71 && ctx
->Extensions
.ARB_blend_func_extended
;
79 * Check if given blend destination factor is legal.
80 * \return GL_TRUE if legal, GL_FALSE otherwise.
83 legal_dst_factor(const struct gl_context
*ctx
, GLenum factor
)
87 case GL_ONE_MINUS_DST_COLOR
:
91 case GL_ONE_MINUS_SRC_COLOR
:
93 case GL_ONE_MINUS_SRC_ALPHA
:
95 case GL_ONE_MINUS_DST_ALPHA
:
97 case GL_CONSTANT_COLOR
:
98 case GL_ONE_MINUS_CONSTANT_COLOR
:
99 case GL_CONSTANT_ALPHA
:
100 case GL_ONE_MINUS_CONSTANT_ALPHA
:
101 return _mesa_is_desktop_gl(ctx
) || ctx
->API
== API_OPENGLES2
;
102 case GL_SRC_ALPHA_SATURATE
:
103 return (ctx
->API
!= API_OPENGLES
104 && ctx
->Extensions
.ARB_blend_func_extended
)
105 || _mesa_is_gles3(ctx
);
108 case GL_ONE_MINUS_SRC1_COLOR
:
109 case GL_ONE_MINUS_SRC1_ALPHA
:
110 return ctx
->API
!= API_OPENGLES
111 && ctx
->Extensions
.ARB_blend_func_extended
;
119 * Check if src/dest RGB/A blend factors are legal. If not generate
121 * \return GL_TRUE if factors are legal, GL_FALSE otherwise.
124 validate_blend_factors(struct gl_context
*ctx
, const char *func
,
125 GLenum sfactorRGB
, GLenum dfactorRGB
,
126 GLenum sfactorA
, GLenum dfactorA
)
128 if (!legal_src_factor(ctx
, sfactorRGB
)) {
129 _mesa_error(ctx
, GL_INVALID_ENUM
,
130 "%s(sfactorRGB = %s)", func
,
131 _mesa_enum_to_string(sfactorRGB
));
135 if (!legal_dst_factor(ctx
, dfactorRGB
)) {
136 _mesa_error(ctx
, GL_INVALID_ENUM
,
137 "%s(dfactorRGB = %s)", func
,
138 _mesa_enum_to_string(dfactorRGB
));
142 if (sfactorA
!= sfactorRGB
&& !legal_src_factor(ctx
, sfactorA
)) {
143 _mesa_error(ctx
, GL_INVALID_ENUM
,
144 "%s(sfactorA = %s)", func
,
145 _mesa_enum_to_string(sfactorA
));
149 if (dfactorA
!= dfactorRGB
&& !legal_dst_factor(ctx
, dfactorA
)) {
150 _mesa_error(ctx
, GL_INVALID_ENUM
,
151 "%s(dfactorA = %s)", func
,
152 _mesa_enum_to_string(dfactorA
));
161 * Specify the blending operation.
163 * \param sfactor source factor operator.
164 * \param dfactor destination factor operator.
166 * \sa glBlendFunc, glBlendFuncSeparateEXT
169 _mesa_BlendFunc( GLenum sfactor
, GLenum dfactor
)
171 _mesa_BlendFuncSeparate(sfactor
, dfactor
, sfactor
, dfactor
);
175 blend_factor_is_dual_src(GLenum factor
)
177 return (factor
== GL_SRC1_COLOR
||
178 factor
== GL_SRC1_ALPHA
||
179 factor
== GL_ONE_MINUS_SRC1_COLOR
||
180 factor
== GL_ONE_MINUS_SRC1_ALPHA
);
184 update_uses_dual_src(struct gl_context
*ctx
, int buf
)
186 ctx
->Color
.Blend
[buf
]._UsesDualSrc
=
187 (blend_factor_is_dual_src(ctx
->Color
.Blend
[buf
].SrcRGB
) ||
188 blend_factor_is_dual_src(ctx
->Color
.Blend
[buf
].DstRGB
) ||
189 blend_factor_is_dual_src(ctx
->Color
.Blend
[buf
].SrcA
) ||
190 blend_factor_is_dual_src(ctx
->Color
.Blend
[buf
].DstA
));
195 * Return the number of per-buffer blend states to update in
196 * glBlendFunc, glBlendFuncSeparate, glBlendEquation, etc.
198 static inline unsigned
199 num_buffers(const struct gl_context
*ctx
)
201 return ctx
->Extensions
.ARB_draw_buffers_blend
202 ? ctx
->Const
.MaxDrawBuffers
: 1;
207 * Set the separate blend source/dest factors for all draw buffers.
209 * \param sfactorRGB RGB source factor operator.
210 * \param dfactorRGB RGB destination factor operator.
211 * \param sfactorA alpha source factor operator.
212 * \param dfactorA alpha destination factor operator.
215 _mesa_BlendFuncSeparate( GLenum sfactorRGB
, GLenum dfactorRGB
,
216 GLenum sfactorA
, GLenum dfactorA
)
218 GET_CURRENT_CONTEXT(ctx
);
219 const unsigned numBuffers
= num_buffers(ctx
);
221 bool changed
= false;
223 if (MESA_VERBOSE
& VERBOSE_API
)
224 _mesa_debug(ctx
, "glBlendFuncSeparate %s %s %s %s\n",
225 _mesa_enum_to_string(sfactorRGB
),
226 _mesa_enum_to_string(dfactorRGB
),
227 _mesa_enum_to_string(sfactorA
),
228 _mesa_enum_to_string(dfactorA
));
230 /* Check if we're really changing any state. If not, return early. */
231 if (ctx
->Color
._BlendFuncPerBuffer
) {
232 /* Check all per-buffer states */
233 for (buf
= 0; buf
< numBuffers
; buf
++) {
234 if (ctx
->Color
.Blend
[buf
].SrcRGB
!= sfactorRGB
||
235 ctx
->Color
.Blend
[buf
].DstRGB
!= dfactorRGB
||
236 ctx
->Color
.Blend
[buf
].SrcA
!= sfactorA
||
237 ctx
->Color
.Blend
[buf
].DstA
!= dfactorA
) {
244 /* only need to check 0th per-buffer state */
245 if (ctx
->Color
.Blend
[0].SrcRGB
!= sfactorRGB
||
246 ctx
->Color
.Blend
[0].DstRGB
!= dfactorRGB
||
247 ctx
->Color
.Blend
[0].SrcA
!= sfactorA
||
248 ctx
->Color
.Blend
[0].DstA
!= dfactorA
) {
256 if (!validate_blend_factors(ctx
, "glBlendFuncSeparate",
257 sfactorRGB
, dfactorRGB
,
258 sfactorA
, dfactorA
)) {
262 FLUSH_VERTICES(ctx
, _NEW_COLOR
);
264 for (buf
= 0; buf
< numBuffers
; buf
++) {
265 ctx
->Color
.Blend
[buf
].SrcRGB
= sfactorRGB
;
266 ctx
->Color
.Blend
[buf
].DstRGB
= dfactorRGB
;
267 ctx
->Color
.Blend
[buf
].SrcA
= sfactorA
;
268 ctx
->Color
.Blend
[buf
].DstA
= dfactorA
;
271 update_uses_dual_src(ctx
, 0);
272 for (buf
= 1; buf
< numBuffers
; buf
++) {
273 ctx
->Color
.Blend
[buf
]._UsesDualSrc
= ctx
->Color
.Blend
[0]._UsesDualSrc
;
276 ctx
->Color
._BlendFuncPerBuffer
= GL_FALSE
;
278 if (ctx
->Driver
.BlendFuncSeparate
) {
279 ctx
->Driver
.BlendFuncSeparate(ctx
, sfactorRGB
, dfactorRGB
,
286 _mesa_BlendFunciARB_no_error(GLuint buf
, GLenum sfactor
, GLenum dfactor
)
288 _mesa_BlendFuncSeparateiARB_no_error(buf
, sfactor
, dfactor
, sfactor
,
294 * Set blend source/dest factors for one color buffer/target.
297 _mesa_BlendFunciARB(GLuint buf
, GLenum sfactor
, GLenum dfactor
)
299 _mesa_BlendFuncSeparateiARB(buf
, sfactor
, dfactor
, sfactor
, dfactor
);
303 static ALWAYS_INLINE
void
304 blend_func_separatei(GLuint buf
, GLenum sfactorRGB
, GLenum dfactorRGB
,
305 GLenum sfactorA
, GLenum dfactorA
, bool no_error
)
307 GET_CURRENT_CONTEXT(ctx
);
310 if (!ctx
->Extensions
.ARB_draw_buffers_blend
) {
311 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glBlendFunc[Separate]i()");
315 if (buf
>= ctx
->Const
.MaxDrawBuffers
) {
316 _mesa_error(ctx
, GL_INVALID_VALUE
, "glBlendFuncSeparatei(buffer=%u)",
322 if (ctx
->Color
.Blend
[buf
].SrcRGB
== sfactorRGB
&&
323 ctx
->Color
.Blend
[buf
].DstRGB
== dfactorRGB
&&
324 ctx
->Color
.Blend
[buf
].SrcA
== sfactorA
&&
325 ctx
->Color
.Blend
[buf
].DstA
== dfactorA
)
326 return; /* no change */
328 if (!no_error
&& !validate_blend_factors(ctx
, "glBlendFuncSeparatei",
329 sfactorRGB
, dfactorRGB
,
330 sfactorA
, dfactorA
)) {
334 FLUSH_VERTICES(ctx
, _NEW_COLOR
);
336 ctx
->Color
.Blend
[buf
].SrcRGB
= sfactorRGB
;
337 ctx
->Color
.Blend
[buf
].DstRGB
= dfactorRGB
;
338 ctx
->Color
.Blend
[buf
].SrcA
= sfactorA
;
339 ctx
->Color
.Blend
[buf
].DstA
= dfactorA
;
340 update_uses_dual_src(ctx
, buf
);
341 ctx
->Color
._BlendFuncPerBuffer
= GL_TRUE
;
346 _mesa_BlendFuncSeparateiARB_no_error(GLuint buf
, GLenum sfactorRGB
,
347 GLenum dfactorRGB
, GLenum sfactorA
,
350 blend_func_separatei(buf
, sfactorRGB
, dfactorRGB
, sfactorA
, dfactorA
,
356 * Set separate blend source/dest factors for one color buffer/target.
359 _mesa_BlendFuncSeparateiARB(GLuint buf
, GLenum sfactorRGB
, GLenum dfactorRGB
,
360 GLenum sfactorA
, GLenum dfactorA
)
362 blend_func_separatei(buf
, sfactorRGB
, dfactorRGB
, sfactorA
, dfactorA
,
368 * Return true if \p mode is a legal blending equation, excluding
369 * GL_KHR_blend_equation_advanced modes.
372 legal_simple_blend_equation(const struct gl_context
*ctx
, GLenum mode
)
376 case GL_FUNC_SUBTRACT
:
377 case GL_FUNC_REVERSE_SUBTRACT
:
381 return ctx
->Extensions
.EXT_blend_minmax
;
387 static enum gl_advanced_blend_mode
388 advanced_blend_mode_from_gl_enum(GLenum mode
)
391 case GL_MULTIPLY_KHR
:
392 return BLEND_MULTIPLY
;
396 return BLEND_OVERLAY
;
400 return BLEND_LIGHTEN
;
401 case GL_COLORDODGE_KHR
:
402 return BLEND_COLORDODGE
;
403 case GL_COLORBURN_KHR
:
404 return BLEND_COLORBURN
;
405 case GL_HARDLIGHT_KHR
:
406 return BLEND_HARDLIGHT
;
407 case GL_SOFTLIGHT_KHR
:
408 return BLEND_SOFTLIGHT
;
409 case GL_DIFFERENCE_KHR
:
410 return BLEND_DIFFERENCE
;
411 case GL_EXCLUSION_KHR
:
412 return BLEND_EXCLUSION
;
414 return BLEND_HSL_HUE
;
415 case GL_HSL_SATURATION_KHR
:
416 return BLEND_HSL_SATURATION
;
417 case GL_HSL_COLOR_KHR
:
418 return BLEND_HSL_COLOR
;
419 case GL_HSL_LUMINOSITY_KHR
:
420 return BLEND_HSL_LUMINOSITY
;
427 * If \p mode is one of the advanced blending equations defined by
428 * GL_KHR_blend_equation_advanced (and the extension is supported),
429 * return the corresponding BLEND_* enum. Otherwise, return BLEND_NONE
430 * (which can also be treated as false).
432 static enum gl_advanced_blend_mode
433 advanced_blend_mode(const struct gl_context
*ctx
, GLenum mode
)
435 return _mesa_has_KHR_blend_equation_advanced(ctx
) ?
436 advanced_blend_mode_from_gl_enum(mode
) : BLEND_NONE
;
439 /* This is really an extension function! */
441 _mesa_BlendEquation( GLenum mode
)
443 GET_CURRENT_CONTEXT(ctx
);
444 const unsigned numBuffers
= num_buffers(ctx
);
446 bool changed
= false;
447 enum gl_advanced_blend_mode advanced_mode
= advanced_blend_mode(ctx
, mode
);
449 if (MESA_VERBOSE
& VERBOSE_API
)
450 _mesa_debug(ctx
, "glBlendEquation(%s)\n",
451 _mesa_enum_to_string(mode
));
453 if (ctx
->Color
._BlendEquationPerBuffer
) {
454 /* Check all per-buffer states */
455 for (buf
= 0; buf
< numBuffers
; buf
++) {
456 if (ctx
->Color
.Blend
[buf
].EquationRGB
!= mode
||
457 ctx
->Color
.Blend
[buf
].EquationA
!= mode
) {
464 /* only need to check 0th per-buffer state */
465 if (ctx
->Color
.Blend
[0].EquationRGB
!= mode
||
466 ctx
->Color
.Blend
[0].EquationA
!= mode
) {
475 if (!legal_simple_blend_equation(ctx
, mode
) && !advanced_mode
) {
476 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBlendEquation");
480 FLUSH_VERTICES(ctx
, _NEW_COLOR
);
482 for (buf
= 0; buf
< numBuffers
; buf
++) {
483 ctx
->Color
.Blend
[buf
].EquationRGB
= mode
;
484 ctx
->Color
.Blend
[buf
].EquationA
= mode
;
486 ctx
->Color
._BlendEquationPerBuffer
= GL_FALSE
;
487 ctx
->Color
._AdvancedBlendMode
= advanced_mode
;
489 if (ctx
->Driver
.BlendEquationSeparate
)
490 ctx
->Driver
.BlendEquationSeparate(ctx
, mode
, mode
);
495 * Set blend equation for one color buffer/target.
498 _mesa_BlendEquationiARB(GLuint buf
, GLenum mode
)
500 GET_CURRENT_CONTEXT(ctx
);
501 enum gl_advanced_blend_mode advanced_mode
= advanced_blend_mode(ctx
, mode
);
503 if (MESA_VERBOSE
& VERBOSE_API
)
504 _mesa_debug(ctx
, "glBlendEquationi(%u, %s)\n",
505 buf
, _mesa_enum_to_string(mode
));
507 if (buf
>= ctx
->Const
.MaxDrawBuffers
) {
508 _mesa_error(ctx
, GL_INVALID_VALUE
, "glBlendEquationi(buffer=%u)",
513 if (!legal_simple_blend_equation(ctx
, mode
) && !advanced_mode
) {
514 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBlendEquationi");
518 if (ctx
->Color
.Blend
[buf
].EquationRGB
== mode
&&
519 ctx
->Color
.Blend
[buf
].EquationA
== mode
)
520 return; /* no change */
522 FLUSH_VERTICES(ctx
, _NEW_COLOR
);
523 ctx
->Color
.Blend
[buf
].EquationRGB
= mode
;
524 ctx
->Color
.Blend
[buf
].EquationA
= mode
;
525 ctx
->Color
._BlendEquationPerBuffer
= GL_TRUE
;
528 ctx
->Color
._AdvancedBlendMode
= advanced_mode
;
533 _mesa_BlendEquationSeparate( GLenum modeRGB
, GLenum modeA
)
535 GET_CURRENT_CONTEXT(ctx
);
536 const unsigned numBuffers
= num_buffers(ctx
);
538 bool changed
= false;
540 if (MESA_VERBOSE
& VERBOSE_API
)
541 _mesa_debug(ctx
, "glBlendEquationSeparateEXT(%s %s)\n",
542 _mesa_enum_to_string(modeRGB
),
543 _mesa_enum_to_string(modeA
));
545 if (ctx
->Color
._BlendEquationPerBuffer
) {
546 /* Check all per-buffer states */
547 for (buf
= 0; buf
< numBuffers
; buf
++) {
548 if (ctx
->Color
.Blend
[buf
].EquationRGB
!= modeRGB
||
549 ctx
->Color
.Blend
[buf
].EquationA
!= modeA
) {
556 /* only need to check 0th per-buffer state */
557 if (ctx
->Color
.Blend
[0].EquationRGB
!= modeRGB
||
558 ctx
->Color
.Blend
[0].EquationA
!= modeA
) {
566 if ( (modeRGB
!= modeA
) && !ctx
->Extensions
.EXT_blend_equation_separate
) {
567 _mesa_error(ctx
, GL_INVALID_OPERATION
,
568 "glBlendEquationSeparateEXT not supported by driver");
572 /* Only allow simple blending equations.
573 * The GL_KHR_blend_equation_advanced spec says:
575 * "NOTE: These enums are not accepted by the <modeRGB> or <modeAlpha>
576 * parameters of BlendEquationSeparate or BlendEquationSeparatei."
578 if (!legal_simple_blend_equation(ctx
, modeRGB
)) {
579 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBlendEquationSeparateEXT(modeRGB)");
583 if (!legal_simple_blend_equation(ctx
, modeA
)) {
584 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBlendEquationSeparateEXT(modeA)");
588 FLUSH_VERTICES(ctx
, _NEW_COLOR
);
590 for (buf
= 0; buf
< numBuffers
; buf
++) {
591 ctx
->Color
.Blend
[buf
].EquationRGB
= modeRGB
;
592 ctx
->Color
.Blend
[buf
].EquationA
= modeA
;
594 ctx
->Color
._BlendEquationPerBuffer
= GL_FALSE
;
595 ctx
->Color
._AdvancedBlendMode
= BLEND_NONE
;
597 if (ctx
->Driver
.BlendEquationSeparate
)
598 ctx
->Driver
.BlendEquationSeparate(ctx
, modeRGB
, modeA
);
603 * Set separate blend equations for one color buffer/target.
606 _mesa_BlendEquationSeparateiARB(GLuint buf
, GLenum modeRGB
, GLenum modeA
)
608 GET_CURRENT_CONTEXT(ctx
);
610 if (MESA_VERBOSE
& VERBOSE_API
)
611 _mesa_debug(ctx
, "glBlendEquationSeparatei(%u, %s %s)\n", buf
,
612 _mesa_enum_to_string(modeRGB
),
613 _mesa_enum_to_string(modeA
));
615 if (buf
>= ctx
->Const
.MaxDrawBuffers
) {
616 _mesa_error(ctx
, GL_INVALID_VALUE
, "glBlendEquationSeparatei(buffer=%u)",
621 /* Only allow simple blending equations.
622 * The GL_KHR_blend_equation_advanced spec says:
624 * "NOTE: These enums are not accepted by the <modeRGB> or <modeAlpha>
625 * parameters of BlendEquationSeparate or BlendEquationSeparatei."
627 if (!legal_simple_blend_equation(ctx
, modeRGB
)) {
628 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBlendEquationSeparatei(modeRGB)");
632 if (!legal_simple_blend_equation(ctx
, modeA
)) {
633 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBlendEquationSeparatei(modeA)");
637 if (ctx
->Color
.Blend
[buf
].EquationRGB
== modeRGB
&&
638 ctx
->Color
.Blend
[buf
].EquationA
== modeA
)
639 return; /* no change */
641 FLUSH_VERTICES(ctx
, _NEW_COLOR
);
642 ctx
->Color
.Blend
[buf
].EquationRGB
= modeRGB
;
643 ctx
->Color
.Blend
[buf
].EquationA
= modeA
;
644 ctx
->Color
._BlendEquationPerBuffer
= GL_TRUE
;
645 ctx
->Color
._AdvancedBlendMode
= BLEND_NONE
;
650 * Set the blending color.
652 * \param red red color component.
653 * \param green green color component.
654 * \param blue blue color component.
655 * \param alpha alpha color component.
657 * \sa glBlendColor().
659 * Clamps the parameters and updates gl_colorbuffer_attrib::BlendColor. On a
660 * change, flushes the vertices and notifies the driver via
661 * dd_function_table::BlendColor callback.
664 _mesa_BlendColor( GLclampf red
, GLclampf green
, GLclampf blue
, GLclampf alpha
)
667 GET_CURRENT_CONTEXT(ctx
);
674 if (TEST_EQ_4V(tmp
, ctx
->Color
.BlendColorUnclamped
))
677 FLUSH_VERTICES(ctx
, _NEW_COLOR
);
678 COPY_4FV( ctx
->Color
.BlendColorUnclamped
, tmp
);
680 ctx
->Color
.BlendColor
[0] = CLAMP(tmp
[0], 0.0F
, 1.0F
);
681 ctx
->Color
.BlendColor
[1] = CLAMP(tmp
[1], 0.0F
, 1.0F
);
682 ctx
->Color
.BlendColor
[2] = CLAMP(tmp
[2], 0.0F
, 1.0F
);
683 ctx
->Color
.BlendColor
[3] = CLAMP(tmp
[3], 0.0F
, 1.0F
);
685 if (ctx
->Driver
.BlendColor
)
686 ctx
->Driver
.BlendColor(ctx
, ctx
->Color
.BlendColor
);
691 * Specify the alpha test function.
693 * \param func alpha comparison function.
694 * \param ref reference value.
696 * Verifies the parameters and updates gl_colorbuffer_attrib.
697 * On a change, flushes the vertices and notifies the driver via
698 * dd_function_table::AlphaFunc callback.
701 _mesa_AlphaFunc( GLenum func
, GLclampf ref
)
703 GET_CURRENT_CONTEXT(ctx
);
705 if (MESA_VERBOSE
& VERBOSE_API
)
706 _mesa_debug(ctx
, "glAlphaFunc(%s, %f)\n",
707 _mesa_enum_to_string(func
), ref
);
709 if (ctx
->Color
.AlphaFunc
== func
&& ctx
->Color
.AlphaRefUnclamped
== ref
)
710 return; /* no change */
721 FLUSH_VERTICES(ctx
, _NEW_COLOR
);
722 ctx
->Color
.AlphaFunc
= func
;
723 ctx
->Color
.AlphaRefUnclamped
= ref
;
724 ctx
->Color
.AlphaRef
= CLAMP(ref
, 0.0F
, 1.0F
);
726 if (ctx
->Driver
.AlphaFunc
)
727 ctx
->Driver
.AlphaFunc(ctx
, func
, ctx
->Color
.AlphaRef
);
731 _mesa_error( ctx
, GL_INVALID_ENUM
, "glAlphaFunc(func)" );
738 * Specify a logic pixel operation for color index rendering.
740 * \param opcode operation.
742 * Verifies that \p opcode is a valid enum and updates
743 * gl_colorbuffer_attrib::LogicOp.
744 * On a change, flushes the vertices and notifies the driver via the
745 * dd_function_table::LogicOpcode callback.
748 _mesa_LogicOp( GLenum opcode
)
750 GET_CURRENT_CONTEXT(ctx
);
752 if (MESA_VERBOSE
& VERBOSE_API
)
753 _mesa_debug(ctx
, "glLogicOp(%s)\n", _mesa_enum_to_string(opcode
));
759 case GL_COPY_INVERTED
:
769 case GL_AND_INVERTED
:
774 _mesa_error( ctx
, GL_INVALID_ENUM
, "glLogicOp" );
778 if (ctx
->Color
.LogicOp
== opcode
)
781 FLUSH_VERTICES(ctx
, _NEW_COLOR
);
782 ctx
->Color
.LogicOp
= opcode
;
784 if (ctx
->Driver
.LogicOpcode
)
785 ctx
->Driver
.LogicOpcode( ctx
, opcode
);
790 _mesa_IndexMask( GLuint mask
)
792 GET_CURRENT_CONTEXT(ctx
);
794 if (ctx
->Color
.IndexMask
== mask
)
797 FLUSH_VERTICES(ctx
, _NEW_COLOR
);
798 ctx
->Color
.IndexMask
= mask
;
803 * Enable or disable writing of frame buffer color components.
805 * \param red whether to mask writing of the red color component.
806 * \param green whether to mask writing of the green color component.
807 * \param blue whether to mask writing of the blue color component.
808 * \param alpha whether to mask writing of the alpha color component.
812 * Sets the appropriate value of gl_colorbuffer_attrib::ColorMask. On a
813 * change, flushes the vertices and notifies the driver via the
814 * dd_function_table::ColorMask callback.
817 _mesa_ColorMask( GLboolean red
, GLboolean green
,
818 GLboolean blue
, GLboolean alpha
)
820 GET_CURRENT_CONTEXT(ctx
);
825 if (MESA_VERBOSE
& VERBOSE_API
)
826 _mesa_debug(ctx
, "glColorMask(%d, %d, %d, %d)\n",
827 red
, green
, blue
, alpha
);
829 /* Shouldn't have any information about channel depth in core mesa
830 * -- should probably store these as the native booleans:
832 tmp
[RCOMP
] = red
? 0xff : 0x0;
833 tmp
[GCOMP
] = green
? 0xff : 0x0;
834 tmp
[BCOMP
] = blue
? 0xff : 0x0;
835 tmp
[ACOMP
] = alpha
? 0xff : 0x0;
838 for (i
= 0; i
< ctx
->Const
.MaxDrawBuffers
; i
++) {
839 if (!TEST_EQ_4V(tmp
, ctx
->Color
.ColorMask
[i
])) {
841 FLUSH_VERTICES(ctx
, _NEW_COLOR
);
844 COPY_4UBV(ctx
->Color
.ColorMask
[i
], tmp
);
848 if (ctx
->Driver
.ColorMask
)
849 ctx
->Driver
.ColorMask( ctx
, red
, green
, blue
, alpha
);
854 * For GL_EXT_draw_buffers2 and GL3
857 _mesa_ColorMaski( GLuint buf
, GLboolean red
, GLboolean green
,
858 GLboolean blue
, GLboolean alpha
)
861 GET_CURRENT_CONTEXT(ctx
);
863 if (MESA_VERBOSE
& VERBOSE_API
)
864 _mesa_debug(ctx
, "glColorMaskIndexed %u %d %d %d %d\n",
865 buf
, red
, green
, blue
, alpha
);
867 if (buf
>= ctx
->Const
.MaxDrawBuffers
) {
868 _mesa_error(ctx
, GL_INVALID_VALUE
, "glColorMaskIndexed(buf=%u)", buf
);
872 /* Shouldn't have any information about channel depth in core mesa
873 * -- should probably store these as the native booleans:
875 tmp
[RCOMP
] = red
? 0xff : 0x0;
876 tmp
[GCOMP
] = green
? 0xff : 0x0;
877 tmp
[BCOMP
] = blue
? 0xff : 0x0;
878 tmp
[ACOMP
] = alpha
? 0xff : 0x0;
880 if (TEST_EQ_4V(tmp
, ctx
->Color
.ColorMask
[buf
]))
883 FLUSH_VERTICES(ctx
, _NEW_COLOR
);
884 COPY_4UBV(ctx
->Color
.ColorMask
[buf
], tmp
);
889 _mesa_ClampColor(GLenum target
, GLenum clamp
)
891 GET_CURRENT_CONTEXT(ctx
);
893 /* Check for both the extension and the GL version, since the Intel driver
894 * does not advertise the extension in core profiles.
896 if (ctx
->Version
<= 30 && !ctx
->Extensions
.ARB_color_buffer_float
) {
897 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glClampColor()");
901 if (clamp
!= GL_TRUE
&& clamp
!= GL_FALSE
&& clamp
!= GL_FIXED_ONLY_ARB
) {
902 _mesa_error(ctx
, GL_INVALID_ENUM
, "glClampColorARB(clamp)");
907 case GL_CLAMP_VERTEX_COLOR_ARB
:
908 if (ctx
->API
== API_OPENGL_CORE
)
910 FLUSH_VERTICES(ctx
, _NEW_LIGHT
);
911 ctx
->Light
.ClampVertexColor
= clamp
;
912 _mesa_update_clamp_vertex_color(ctx
, ctx
->DrawBuffer
);
914 case GL_CLAMP_FRAGMENT_COLOR_ARB
:
915 if (ctx
->API
== API_OPENGL_CORE
)
917 FLUSH_VERTICES(ctx
, _NEW_FRAG_CLAMP
);
918 ctx
->Color
.ClampFragmentColor
= clamp
;
919 _mesa_update_clamp_fragment_color(ctx
, ctx
->DrawBuffer
);
921 case GL_CLAMP_READ_COLOR_ARB
:
922 ctx
->Color
.ClampReadColor
= clamp
;
930 _mesa_error(ctx
, GL_INVALID_ENUM
, "glClampColor(%s)",
931 _mesa_enum_to_string(target
));
935 get_clamp_color(const struct gl_framebuffer
*fb
, GLenum clamp
)
937 if (clamp
== GL_TRUE
|| clamp
== GL_FALSE
)
940 assert(clamp
== GL_FIXED_ONLY
);
944 return fb
->_AllColorBuffersFixedPoint
;
948 _mesa_get_clamp_fragment_color(const struct gl_context
*ctx
,
949 const struct gl_framebuffer
*drawFb
)
951 return get_clamp_color(drawFb
, ctx
->Color
.ClampFragmentColor
);
955 _mesa_get_clamp_vertex_color(const struct gl_context
*ctx
,
956 const struct gl_framebuffer
*drawFb
)
958 return get_clamp_color(drawFb
, ctx
->Light
.ClampVertexColor
);
962 _mesa_get_clamp_read_color(const struct gl_context
*ctx
,
963 const struct gl_framebuffer
*readFb
)
965 return get_clamp_color(readFb
, ctx
->Color
.ClampReadColor
);
969 * Update the ctx->Color._ClampFragmentColor field
972 _mesa_update_clamp_fragment_color(struct gl_context
*ctx
,
973 const struct gl_framebuffer
*drawFb
)
976 * - there is no colorbuffer
977 * - all colorbuffers are unsigned normalized, so clamping has no effect
978 * - there is an integer colorbuffer
980 if (!drawFb
|| !drawFb
->_HasSNormOrFloatColorBuffer
||
981 drawFb
->_IntegerBuffers
)
982 ctx
->Color
._ClampFragmentColor
= GL_FALSE
;
984 ctx
->Color
._ClampFragmentColor
=
985 _mesa_get_clamp_fragment_color(ctx
, drawFb
);
989 * Update the ctx->Color._ClampVertexColor field
992 _mesa_update_clamp_vertex_color(struct gl_context
*ctx
,
993 const struct gl_framebuffer
*drawFb
)
995 ctx
->Light
._ClampVertexColor
=
996 _mesa_get_clamp_vertex_color(ctx
, drawFb
);
1000 * Returns an appropriate mesa_format for color rendering based on the
1001 * GL_FRAMEBUFFER_SRGB state.
1003 * Some drivers implement GL_FRAMEBUFFER_SRGB using a flag on the blend state
1004 * (which GL_FRAMEBUFFER_SRGB maps to reasonably), but some have to do so by
1005 * overriding the format of the surface. This is a helper for doing the
1006 * surface format override variant.
1009 _mesa_get_render_format(const struct gl_context
*ctx
, mesa_format format
)
1011 if (ctx
->Color
.sRGBEnabled
)
1014 return _mesa_get_srgb_format_linear(format
);
1017 /**********************************************************************/
1018 /** \name Initialization */
1022 * Initialization of the context's Color attribute group.
1024 * \param ctx GL context.
1026 * Initializes the related fields in the context color attribute group,
1027 * __struct gl_contextRec::Color.
1029 void _mesa_init_color( struct gl_context
* ctx
)
1033 /* Color buffer group */
1034 ctx
->Color
.IndexMask
= ~0u;
1035 memset(ctx
->Color
.ColorMask
, 0xff, sizeof(ctx
->Color
.ColorMask
));
1036 ctx
->Color
.ClearIndex
= 0;
1037 ASSIGN_4V( ctx
->Color
.ClearColor
.f
, 0, 0, 0, 0 );
1038 ctx
->Color
.AlphaEnabled
= GL_FALSE
;
1039 ctx
->Color
.AlphaFunc
= GL_ALWAYS
;
1040 ctx
->Color
.AlphaRef
= 0;
1041 ctx
->Color
.BlendEnabled
= 0x0;
1042 for (i
= 0; i
< ARRAY_SIZE(ctx
->Color
.Blend
); i
++) {
1043 ctx
->Color
.Blend
[i
].SrcRGB
= GL_ONE
;
1044 ctx
->Color
.Blend
[i
].DstRGB
= GL_ZERO
;
1045 ctx
->Color
.Blend
[i
].SrcA
= GL_ONE
;
1046 ctx
->Color
.Blend
[i
].DstA
= GL_ZERO
;
1047 ctx
->Color
.Blend
[i
].EquationRGB
= GL_FUNC_ADD
;
1048 ctx
->Color
.Blend
[i
].EquationA
= GL_FUNC_ADD
;
1050 ASSIGN_4V( ctx
->Color
.BlendColor
, 0.0, 0.0, 0.0, 0.0 );
1051 ASSIGN_4V( ctx
->Color
.BlendColorUnclamped
, 0.0, 0.0, 0.0, 0.0 );
1052 ctx
->Color
.IndexLogicOpEnabled
= GL_FALSE
;
1053 ctx
->Color
.ColorLogicOpEnabled
= GL_FALSE
;
1054 ctx
->Color
.LogicOp
= GL_COPY
;
1055 ctx
->Color
.DitherFlag
= GL_TRUE
;
1057 /* GL_FRONT is not possible on GLES. Instead GL_BACK will render to either
1058 * the front or the back buffer depending on the config */
1059 if (ctx
->Visual
.doubleBufferMode
|| _mesa_is_gles(ctx
)) {
1060 ctx
->Color
.DrawBuffer
[0] = GL_BACK
;
1063 ctx
->Color
.DrawBuffer
[0] = GL_FRONT
;
1066 ctx
->Color
.ClampFragmentColor
= ctx
->API
== API_OPENGL_COMPAT
?
1067 GL_FIXED_ONLY_ARB
: GL_FALSE
;
1068 ctx
->Color
._ClampFragmentColor
= GL_FALSE
;
1069 ctx
->Color
.ClampReadColor
= GL_FIXED_ONLY_ARB
;
1071 /* GLES 1/2/3 behaves as though GL_FRAMEBUFFER_SRGB is always enabled
1072 * if EGL_KHR_gl_colorspace has been used to request sRGB.
1074 ctx
->Color
.sRGBEnabled
= _mesa_is_gles(ctx
);
1076 ctx
->Color
.BlendCoherent
= true;