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 * Set blend source/dest factors for one color buffer/target.
289 _mesa_BlendFunciARB(GLuint buf, GLenum sfactor, GLenum dfactor)
291 _mesa_BlendFuncSeparateiARB(buf, sfactor, dfactor, sfactor, dfactor);
296 * Set separate blend source/dest factors for one color buffer/target.
299 _mesa_BlendFuncSeparateiARB(GLuint buf, GLenum sfactorRGB, GLenum dfactorRGB,
300 GLenum sfactorA, GLenum dfactorA)
302 GET_CURRENT_CONTEXT(ctx);
304 if (!ctx->Extensions.ARB_draw_buffers_blend) {
305 _mesa_error(ctx, GL_INVALID_OPERATION, "glBlendFunc[Separate]i()");
309 if (buf >= ctx->Const.MaxDrawBuffers) {
310 _mesa_error(ctx, GL_INVALID_VALUE, "glBlendFuncSeparatei(buffer=%u)",
315 if (ctx->Color.Blend[buf].SrcRGB == sfactorRGB &&
316 ctx->Color.Blend[buf].DstRGB == dfactorRGB &&
317 ctx->Color.Blend[buf].SrcA == sfactorA &&
318 ctx->Color.Blend[buf].DstA == dfactorA)
319 return; /* no change */
321 if (!validate_blend_factors(ctx, "glBlendFuncSeparatei",
322 sfactorRGB, dfactorRGB,
323 sfactorA, dfactorA)) {
327 FLUSH_VERTICES(ctx, _NEW_COLOR);
329 ctx->Color.Blend[buf].SrcRGB = sfactorRGB;
330 ctx->Color.Blend[buf].DstRGB = dfactorRGB;
331 ctx->Color.Blend[buf].SrcA = sfactorA;
332 ctx->Color.Blend[buf].DstA = dfactorA;
333 update_uses_dual_src(ctx, buf);
334 ctx->Color._BlendFuncPerBuffer = GL_TRUE;
339 * Return true if \p mode is a legal blending equation, excluding
340 * GL_KHR_blend_equation_advanced modes.
343 legal_simple_blend_equation(const struct gl_context *ctx, GLenum mode)
347 case GL_FUNC_SUBTRACT:
348 case GL_FUNC_REVERSE_SUBTRACT:
352 return ctx->Extensions.EXT_blend_minmax;
358 static enum gl_advanced_blend_mode
359 advanced_blend_mode_from_gl_enum(GLenum mode)
362 case GL_MULTIPLY_KHR:
363 return BLEND_MULTIPLY;
367 return BLEND_OVERLAY;
371 return BLEND_LIGHTEN;
372 case GL_COLORDODGE_KHR:
373 return BLEND_COLORDODGE;
374 case GL_COLORBURN_KHR:
375 return BLEND_COLORBURN;
376 case GL_HARDLIGHT_KHR:
377 return BLEND_HARDLIGHT;
378 case GL_SOFTLIGHT_KHR:
379 return BLEND_SOFTLIGHT;
380 case GL_DIFFERENCE_KHR:
381 return BLEND_DIFFERENCE;
382 case GL_EXCLUSION_KHR:
383 return BLEND_EXCLUSION;
385 return BLEND_HSL_HUE;
386 case GL_HSL_SATURATION_KHR:
387 return BLEND_HSL_SATURATION;
388 case GL_HSL_COLOR_KHR:
389 return BLEND_HSL_COLOR;
390 case GL_HSL_LUMINOSITY_KHR:
391 return BLEND_HSL_LUMINOSITY;
398 * If \p mode is one of the advanced blending equations defined by
399 * GL_KHR_blend_equation_advanced (and the extension is supported),
400 * return the corresponding BLEND_* enum. Otherwise, return BLEND_NONE
401 * (which can also be treated as false).
403 static enum gl_advanced_blend_mode
404 advanced_blend_mode(const struct gl_context *ctx, GLenum mode)
406 return _mesa_has_KHR_blend_equation_advanced(ctx) ?
407 advanced_blend_mode_from_gl_enum(mode) : BLEND_NONE;
410 /* This is really an extension function! */
412 _mesa_BlendEquation( GLenum mode )
414 GET_CURRENT_CONTEXT(ctx);
415 const unsigned numBuffers = num_buffers(ctx);
417 bool changed = false;
418 enum gl_advanced_blend_mode advanced_mode = advanced_blend_mode(ctx, mode);
420 if (MESA_VERBOSE & VERBOSE_API)
421 _mesa_debug(ctx, "glBlendEquation(%s)\n",
422 _mesa_enum_to_string(mode));
424 if (ctx->Color._BlendEquationPerBuffer) {
425 /* Check all per-buffer states */
426 for (buf = 0; buf < numBuffers; buf++) {
427 if (ctx->Color.Blend[buf].EquationRGB != mode ||
428 ctx->Color.Blend[buf].EquationA != mode) {
435 /* only need to check 0th per-buffer state */
436 if (ctx->Color.Blend[0].EquationRGB != mode ||
437 ctx->Color.Blend[0].EquationA != mode) {
446 if (!legal_simple_blend_equation(ctx, mode) && !advanced_mode) {
447 _mesa_error(ctx, GL_INVALID_ENUM, "glBlendEquation");
451 FLUSH_VERTICES(ctx, _NEW_COLOR);
453 for (buf = 0; buf < numBuffers; buf++) {
454 ctx->Color.Blend[buf].EquationRGB = mode;
455 ctx->Color.Blend[buf].EquationA = mode;
457 ctx->Color._BlendEquationPerBuffer = GL_FALSE;
458 ctx->Color._AdvancedBlendMode = advanced_mode;
460 if (ctx->Driver.BlendEquationSeparate)
461 ctx->Driver.BlendEquationSeparate(ctx, mode, mode);
466 * Set blend equation for one color buffer/target.
469 _mesa_BlendEquationiARB(GLuint buf, GLenum mode)
471 GET_CURRENT_CONTEXT(ctx);
472 enum gl_advanced_blend_mode advanced_mode = advanced_blend_mode(ctx, mode);
474 if (MESA_VERBOSE & VERBOSE_API)
475 _mesa_debug(ctx, "glBlendEquationi(%u, %s)\n",
476 buf, _mesa_enum_to_string(mode));
478 if (buf >= ctx->Const.MaxDrawBuffers) {
479 _mesa_error(ctx, GL_INVALID_VALUE, "glBlendEquationi(buffer=%u)",
484 if (!legal_simple_blend_equation(ctx, mode) && !advanced_mode) {
485 _mesa_error(ctx, GL_INVALID_ENUM, "glBlendEquationi");
489 if (ctx->Color.Blend[buf].EquationRGB == mode &&
490 ctx->Color.Blend[buf].EquationA == mode)
491 return; /* no change */
493 FLUSH_VERTICES(ctx, _NEW_COLOR);
494 ctx->Color.Blend[buf].EquationRGB = mode;
495 ctx->Color.Blend[buf].EquationA = mode;
496 ctx->Color._BlendEquationPerBuffer = GL_TRUE;
499 ctx->Color._AdvancedBlendMode = advanced_mode;
504 _mesa_BlendEquationSeparate( GLenum modeRGB, GLenum modeA )
506 GET_CURRENT_CONTEXT(ctx);
507 const unsigned numBuffers = num_buffers(ctx);
509 bool changed = false;
511 if (MESA_VERBOSE & VERBOSE_API)
512 _mesa_debug(ctx, "glBlendEquationSeparateEXT(%s %s)\n",
513 _mesa_enum_to_string(modeRGB),
514 _mesa_enum_to_string(modeA));
516 if (ctx->Color._BlendEquationPerBuffer) {
517 /* Check all per-buffer states */
518 for (buf = 0; buf < numBuffers; buf++) {
519 if (ctx->Color.Blend[buf].EquationRGB != modeRGB ||
520 ctx->Color.Blend[buf].EquationA != modeA) {
527 /* only need to check 0th per-buffer state */
528 if (ctx->Color.Blend[0].EquationRGB != modeRGB ||
529 ctx->Color.Blend[0].EquationA != modeA) {
537 if ( (modeRGB != modeA) && !ctx->Extensions.EXT_blend_equation_separate ) {
538 _mesa_error(ctx, GL_INVALID_OPERATION,
539 "glBlendEquationSeparateEXT not supported by driver");
543 /* Only allow simple blending equations.
544 * The GL_KHR_blend_equation_advanced spec says:
546 * "NOTE: These enums are not accepted by the <modeRGB> or <modeAlpha>
547 * parameters of BlendEquationSeparate or BlendEquationSeparatei."
549 if (!legal_simple_blend_equation(ctx, modeRGB)) {
550 _mesa_error(ctx, GL_INVALID_ENUM, "glBlendEquationSeparateEXT(modeRGB)");
554 if (!legal_simple_blend_equation(ctx, modeA)) {
555 _mesa_error(ctx, GL_INVALID_ENUM, "glBlendEquationSeparateEXT(modeA)");
559 FLUSH_VERTICES(ctx, _NEW_COLOR);
561 for (buf = 0; buf < numBuffers; buf++) {
562 ctx->Color.Blend[buf].EquationRGB = modeRGB;
563 ctx->Color.Blend[buf].EquationA = modeA;
565 ctx->Color._BlendEquationPerBuffer = GL_FALSE;
566 ctx->Color._AdvancedBlendMode = BLEND_NONE;
568 if (ctx->Driver.BlendEquationSeparate)
569 ctx->Driver.BlendEquationSeparate(ctx, modeRGB, modeA);
574 * Set separate blend equations for one color buffer/target.
577 _mesa_BlendEquationSeparateiARB(GLuint buf, GLenum modeRGB, GLenum modeA)
579 GET_CURRENT_CONTEXT(ctx);
581 if (MESA_VERBOSE & VERBOSE_API)
582 _mesa_debug(ctx, "glBlendEquationSeparatei(%u, %s %s)\n", buf,
583 _mesa_enum_to_string(modeRGB),
584 _mesa_enum_to_string(modeA));
586 if (buf >= ctx->Const.MaxDrawBuffers) {
587 _mesa_error(ctx, GL_INVALID_VALUE, "glBlendEquationSeparatei(buffer=%u)",
592 /* Only allow simple blending equations.
593 * The GL_KHR_blend_equation_advanced spec says:
595 * "NOTE: These enums are not accepted by the <modeRGB> or <modeAlpha>
596 * parameters of BlendEquationSeparate or BlendEquationSeparatei."
598 if (!legal_simple_blend_equation(ctx, modeRGB)) {
599 _mesa_error(ctx, GL_INVALID_ENUM, "glBlendEquationSeparatei(modeRGB)");
603 if (!legal_simple_blend_equation(ctx, modeA)) {
604 _mesa_error(ctx, GL_INVALID_ENUM, "glBlendEquationSeparatei(modeA)");
608 if (ctx->Color.Blend[buf].EquationRGB == modeRGB &&
609 ctx->Color.Blend[buf].EquationA == modeA)
610 return; /* no change */
612 FLUSH_VERTICES(ctx, _NEW_COLOR);
613 ctx->Color.Blend[buf].EquationRGB = modeRGB;
614 ctx->Color.Blend[buf].EquationA = modeA;
615 ctx->Color._BlendEquationPerBuffer = GL_TRUE;
616 ctx->Color._AdvancedBlendMode = BLEND_NONE;
621 * Set the blending color.
623 * \param red red color component.
624 * \param green green color component.
625 * \param blue blue color component.
626 * \param alpha alpha color component.
628 * \sa glBlendColor().
630 * Clamps the parameters and updates gl_colorbuffer_attrib::BlendColor. On a
631 * change, flushes the vertices and notifies the driver via
632 * dd_function_table::BlendColor callback.
635 _mesa_BlendColor( GLclampf red, GLclampf green, GLclampf blue, GLclampf alpha )
638 GET_CURRENT_CONTEXT(ctx);
645 if (TEST_EQ_4V(tmp, ctx->Color.BlendColorUnclamped))
648 FLUSH_VERTICES(ctx, _NEW_COLOR);
649 COPY_4FV( ctx->Color.BlendColorUnclamped, tmp );
651 ctx->Color.BlendColor[0] = CLAMP(tmp[0], 0.0F, 1.0F);
652 ctx->Color.BlendColor[1] = CLAMP(tmp[1], 0.0F, 1.0F);
653 ctx->Color.BlendColor[2] = CLAMP(tmp[2], 0.0F, 1.0F);
654 ctx->Color.BlendColor[3] = CLAMP(tmp[3], 0.0F, 1.0F);
656 if (ctx->Driver.BlendColor)
657 ctx->Driver.BlendColor(ctx, ctx->Color.BlendColor);
662 * Specify the alpha test function.
664 * \param func alpha comparison function.
665 * \param ref reference value.
667 * Verifies the parameters and updates gl_colorbuffer_attrib.
668 * On a change, flushes the vertices and notifies the driver via
669 * dd_function_table::AlphaFunc callback.
672 _mesa_AlphaFunc( GLenum func, GLclampf ref )
674 GET_CURRENT_CONTEXT(ctx);
676 if (MESA_VERBOSE & VERBOSE_API)
677 _mesa_debug(ctx, "glAlphaFunc(%s, %f)\n",
678 _mesa_enum_to_string(func), ref);
680 if (ctx->Color.AlphaFunc == func && ctx->Color.AlphaRefUnclamped == ref)
681 return; /* no change */
692 FLUSH_VERTICES(ctx, _NEW_COLOR);
693 ctx->Color.AlphaFunc = func;
694 ctx->Color.AlphaRefUnclamped = ref;
695 ctx->Color.AlphaRef = CLAMP(ref, 0.0F, 1.0F);
697 if (ctx->Driver.AlphaFunc)
698 ctx->Driver.AlphaFunc(ctx, func, ctx->Color.AlphaRef);
702 _mesa_error( ctx, GL_INVALID_ENUM, "glAlphaFunc(func)" );
709 * Specify a logic pixel operation for color index rendering.
711 * \param opcode operation.
713 * Verifies that \p opcode is a valid enum and updates
714 * gl_colorbuffer_attrib::LogicOp.
715 * On a change, flushes the vertices and notifies the driver via the
716 * dd_function_table::LogicOpcode callback.
719 _mesa_LogicOp( GLenum opcode )
721 GET_CURRENT_CONTEXT(ctx);
723 if (MESA_VERBOSE & VERBOSE_API)
724 _mesa_debug(ctx, "glLogicOp(%s)\n", _mesa_enum_to_string(opcode));
730 case GL_COPY_INVERTED:
740 case GL_AND_INVERTED:
745 _mesa_error( ctx, GL_INVALID_ENUM, "glLogicOp" );
749 if (ctx->Color.LogicOp == opcode)
752 FLUSH_VERTICES(ctx, _NEW_COLOR);
753 ctx->Color.LogicOp = opcode;
755 if (ctx->Driver.LogicOpcode)
756 ctx->Driver.LogicOpcode( ctx, opcode );
761 _mesa_IndexMask( GLuint mask )
763 GET_CURRENT_CONTEXT(ctx);
765 if (ctx->Color.IndexMask == mask)
768 FLUSH_VERTICES(ctx, _NEW_COLOR);
769 ctx->Color.IndexMask = mask;
774 * Enable or disable writing of frame buffer color components.
776 * \param red whether to mask writing of the red color component.
777 * \param green whether to mask writing of the green color component.
778 * \param blue whether to mask writing of the blue color component.
779 * \param alpha whether to mask writing of the alpha color component.
783 * Sets the appropriate value of gl_colorbuffer_attrib::ColorMask. On a
784 * change, flushes the vertices and notifies the driver via the
785 * dd_function_table::ColorMask callback.
788 _mesa_ColorMask( GLboolean red, GLboolean green,
789 GLboolean blue, GLboolean alpha )
791 GET_CURRENT_CONTEXT(ctx);
796 if (MESA_VERBOSE & VERBOSE_API)
797 _mesa_debug(ctx, "glColorMask(%d, %d, %d, %d)\n",
798 red, green, blue, alpha);
800 /* Shouldn't have any information about channel depth in core mesa
801 * -- should probably store these as the native booleans:
803 tmp[RCOMP] = red ? 0xff : 0x0;
804 tmp[GCOMP] = green ? 0xff : 0x0;
805 tmp[BCOMP] = blue ? 0xff : 0x0;
806 tmp[ACOMP] = alpha ? 0xff : 0x0;
809 for (i = 0; i < ctx->Const.MaxDrawBuffers; i++) {
810 if (!TEST_EQ_4V(tmp, ctx->Color.ColorMask[i])) {
812 FLUSH_VERTICES(ctx, _NEW_COLOR);
815 COPY_4UBV(ctx->Color.ColorMask[i], tmp);
819 if (ctx->Driver.ColorMask)
820 ctx->Driver.ColorMask( ctx, red, green, blue, alpha );
825 * For GL_EXT_draw_buffers2 and GL3
828 _mesa_ColorMaski( GLuint buf, GLboolean red, GLboolean green,
829 GLboolean blue, GLboolean alpha )
832 GET_CURRENT_CONTEXT(ctx);
834 if (MESA_VERBOSE & VERBOSE_API)
835 _mesa_debug(ctx, "glColorMaskIndexed %u %d %d %d %d\n",
836 buf, red, green, blue, alpha);
838 if (buf >= ctx->Const.MaxDrawBuffers) {
839 _mesa_error(ctx, GL_INVALID_VALUE, "glColorMaskIndexed(buf=%u)", buf);
843 /* Shouldn't have any information about channel depth in core mesa
844 * -- should probably store these as the native booleans:
846 tmp[RCOMP] = red ? 0xff : 0x0;
847 tmp[GCOMP] = green ? 0xff : 0x0;
848 tmp[BCOMP] = blue ? 0xff : 0x0;
849 tmp[ACOMP] = alpha ? 0xff : 0x0;
851 if (TEST_EQ_4V(tmp, ctx->Color.ColorMask[buf]))
854 FLUSH_VERTICES(ctx, _NEW_COLOR);
855 COPY_4UBV(ctx->Color.ColorMask[buf], tmp);
860 _mesa_ClampColor(GLenum target, GLenum clamp)
862 GET_CURRENT_CONTEXT(ctx);
864 /* Check for both the extension and the GL version, since the Intel driver
865 * does not advertise the extension in core profiles.
867 if (ctx->Version <= 30 && !ctx->Extensions.ARB_color_buffer_float) {
868 _mesa_error(ctx, GL_INVALID_OPERATION, "glClampColor()");
872 if (clamp != GL_TRUE && clamp != GL_FALSE && clamp != GL_FIXED_ONLY_ARB) {
873 _mesa_error(ctx, GL_INVALID_ENUM, "glClampColorARB(clamp)");
878 case GL_CLAMP_VERTEX_COLOR_ARB:
879 if (ctx->API == API_OPENGL_CORE)
881 FLUSH_VERTICES(ctx, _NEW_LIGHT);
882 ctx->Light.ClampVertexColor = clamp;
883 _mesa_update_clamp_vertex_color(ctx, ctx->DrawBuffer);
885 case GL_CLAMP_FRAGMENT_COLOR_ARB:
886 if (ctx->API == API_OPENGL_CORE)
888 FLUSH_VERTICES(ctx, _NEW_FRAG_CLAMP);
889 ctx->Color.ClampFragmentColor = clamp;
890 _mesa_update_clamp_fragment_color(ctx, ctx->DrawBuffer);
892 case GL_CLAMP_READ_COLOR_ARB:
893 ctx->Color.ClampReadColor = clamp;
901 _mesa_error(ctx, GL_INVALID_ENUM, "glClampColor(%s)",
902 _mesa_enum_to_string(target));
906 get_clamp_color(const struct gl_framebuffer *fb, GLenum clamp)
908 if (clamp == GL_TRUE || clamp == GL_FALSE)
911 assert(clamp == GL_FIXED_ONLY);
915 return fb->_AllColorBuffersFixedPoint;
919 _mesa_get_clamp_fragment_color(const struct gl_context *ctx,
920 const struct gl_framebuffer *drawFb)
922 return get_clamp_color(drawFb, ctx->Color.ClampFragmentColor);
926 _mesa_get_clamp_vertex_color(const struct gl_context *ctx,
927 const struct gl_framebuffer *drawFb)
929 return get_clamp_color(drawFb, ctx->Light.ClampVertexColor);
933 _mesa_get_clamp_read_color(const struct gl_context *ctx,
934 const struct gl_framebuffer *readFb)
936 return get_clamp_color(readFb, ctx->Color.ClampReadColor);
940 * Update the ctx->Color._ClampFragmentColor field
943 _mesa_update_clamp_fragment_color(struct gl_context *ctx,
944 const struct gl_framebuffer *drawFb)
947 * - there is no colorbuffer
948 * - all colorbuffers are unsigned normalized, so clamping has no effect
949 * - there is an integer colorbuffer
951 if (!drawFb || !drawFb->_HasSNormOrFloatColorBuffer ||
952 drawFb->_IntegerBuffers)
953 ctx->Color._ClampFragmentColor = GL_FALSE;
955 ctx->Color._ClampFragmentColor =
956 _mesa_get_clamp_fragment_color(ctx, drawFb);
960 * Update the ctx->Color._ClampVertexColor field
963 _mesa_update_clamp_vertex_color(struct gl_context *ctx,
964 const struct gl_framebuffer *drawFb)
966 ctx->Light._ClampVertexColor =
967 _mesa_get_clamp_vertex_color(ctx, drawFb);
971 * Returns an appropriate mesa_format for color rendering based on the
972 * GL_FRAMEBUFFER_SRGB state.
974 * Some drivers implement GL_FRAMEBUFFER_SRGB using a flag on the blend state
975 * (which GL_FRAMEBUFFER_SRGB maps to reasonably), but some have to do so by
976 * overriding the format of the surface. This is a helper for doing the
977 * surface format override variant.
980 _mesa_get_render_format(const struct gl_context *ctx, mesa_format format)
982 if (ctx->Color.sRGBEnabled)
985 return _mesa_get_srgb_format_linear(format);
988 /**********************************************************************/
989 /** \name Initialization */
993 * Initialization of the context's Color attribute group.
995 * \param ctx GL context.
997 * Initializes the related fields in the context color attribute group,
998 * __struct gl_contextRec::Color.
1000 void _mesa_init_color( struct gl_context * ctx )
1004 /* Color buffer group */
1005 ctx->Color.IndexMask = ~0u;
1006 memset(ctx->Color.ColorMask, 0xff, sizeof(ctx->Color.ColorMask));
1007 ctx->Color.ClearIndex = 0;
1008 ASSIGN_4V( ctx->Color.ClearColor.f, 0, 0, 0, 0 );
1009 ctx->Color.AlphaEnabled = GL_FALSE;
1010 ctx->Color.AlphaFunc = GL_ALWAYS;
1011 ctx->Color.AlphaRef = 0;
1012 ctx->Color.BlendEnabled = 0x0;
1013 for (i = 0; i < ARRAY_SIZE(ctx->Color.Blend); i++) {
1014 ctx->Color.Blend[i].SrcRGB = GL_ONE;
1015 ctx->Color.Blend[i].DstRGB = GL_ZERO;
1016 ctx->Color.Blend[i].SrcA = GL_ONE;
1017 ctx->Color.Blend[i].DstA = GL_ZERO;
1018 ctx->Color.Blend[i].EquationRGB = GL_FUNC_ADD;
1019 ctx->Color.Blend[i].EquationA = GL_FUNC_ADD;
1021 ASSIGN_4V( ctx->Color.BlendColor, 0.0, 0.0, 0.0, 0.0 );
1022 ASSIGN_4V( ctx->Color.BlendColorUnclamped, 0.0, 0.0, 0.0, 0.0 );
1023 ctx->Color.IndexLogicOpEnabled = GL_FALSE;
1024 ctx->Color.ColorLogicOpEnabled = GL_FALSE;
1025 ctx->Color.LogicOp = GL_COPY;
1026 ctx->Color.DitherFlag = GL_TRUE;
1028 /* GL_FRONT is not possible on GLES. Instead GL_BACK will render to either
1029 * the front or the back buffer depending on the config */
1030 if (ctx->Visual.doubleBufferMode || _mesa_is_gles(ctx)) {
1031 ctx->Color.DrawBuffer[0] = GL_BACK;
1034 ctx->Color.DrawBuffer[0] = GL_FRONT;
1037 ctx->Color.ClampFragmentColor = ctx->API == API_OPENGL_COMPAT ?
1038 GL_FIXED_ONLY_ARB : GL_FALSE;
1039 ctx->Color._ClampFragmentColor = GL_FALSE;
1040 ctx->Color.ClampReadColor = GL_FIXED_ONLY_ARB;
1042 /* GLES 1/2/3 behaves as though GL_FRAMEBUFFER_SRGB is always enabled
1043 * if EGL_KHR_gl_colorspace has been used to request sRGB.
1045 ctx->Color.sRGBEnabled = _mesa_is_gles(ctx);
1047 ctx->Color.BlendCoherent = true;