mesa: add blend_func_separatei() helper
[mesa.git] / src / mesa / main / blend.c
1 /**
2 * \file blend.c
3 * Blending operations.
4 */
5
6 /*
7 * Mesa 3-D graphics library
8 *
9 * Copyright (C) 1999-2006 Brian Paul All Rights Reserved.
10 *
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:
17 *
18 * The above copyright notice and this permission notice shall be included
19 * in all copies or substantial portions of the Software.
20 *
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.
28 */
29
30
31
32 #include "glheader.h"
33 #include "blend.h"
34 #include "context.h"
35 #include "enums.h"
36 #include "macros.h"
37 #include "mtypes.h"
38
39
40
41 /**
42 * Check if given blend source factor is legal.
43 * \return GL_TRUE if legal, GL_FALSE otherwise.
44 */
45 static GLboolean
46 legal_src_factor(const struct gl_context *ctx, GLenum factor)
47 {
48 switch (factor) {
49 case GL_SRC_COLOR:
50 case GL_ONE_MINUS_SRC_COLOR:
51 case GL_ZERO:
52 case GL_ONE:
53 case GL_DST_COLOR:
54 case GL_ONE_MINUS_DST_COLOR:
55 case GL_SRC_ALPHA:
56 case GL_ONE_MINUS_SRC_ALPHA:
57 case GL_DST_ALPHA:
58 case GL_ONE_MINUS_DST_ALPHA:
59 case GL_SRC_ALPHA_SATURATE:
60 return GL_TRUE;
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;
66 case GL_SRC1_COLOR:
67 case GL_SRC1_ALPHA:
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;
72 default:
73 return GL_FALSE;
74 }
75 }
76
77
78 /**
79 * Check if given blend destination factor is legal.
80 * \return GL_TRUE if legal, GL_FALSE otherwise.
81 */
82 static GLboolean
83 legal_dst_factor(const struct gl_context *ctx, GLenum factor)
84 {
85 switch (factor) {
86 case GL_DST_COLOR:
87 case GL_ONE_MINUS_DST_COLOR:
88 case GL_ZERO:
89 case GL_ONE:
90 case GL_SRC_COLOR:
91 case GL_ONE_MINUS_SRC_COLOR:
92 case GL_SRC_ALPHA:
93 case GL_ONE_MINUS_SRC_ALPHA:
94 case GL_DST_ALPHA:
95 case GL_ONE_MINUS_DST_ALPHA:
96 return GL_TRUE;
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);
106 case GL_SRC1_COLOR:
107 case GL_SRC1_ALPHA:
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;
112 default:
113 return GL_FALSE;
114 }
115 }
116
117
118 /**
119 * Check if src/dest RGB/A blend factors are legal. If not generate
120 * a GL error.
121 * \return GL_TRUE if factors are legal, GL_FALSE otherwise.
122 */
123 static GLboolean
124 validate_blend_factors(struct gl_context *ctx, const char *func,
125 GLenum sfactorRGB, GLenum dfactorRGB,
126 GLenum sfactorA, GLenum dfactorA)
127 {
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));
132 return GL_FALSE;
133 }
134
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));
139 return GL_FALSE;
140 }
141
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));
146 return GL_FALSE;
147 }
148
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));
153 return GL_FALSE;
154 }
155
156 return GL_TRUE;
157 }
158
159
160 /**
161 * Specify the blending operation.
162 *
163 * \param sfactor source factor operator.
164 * \param dfactor destination factor operator.
165 *
166 * \sa glBlendFunc, glBlendFuncSeparateEXT
167 */
168 void GLAPIENTRY
169 _mesa_BlendFunc( GLenum sfactor, GLenum dfactor )
170 {
171 _mesa_BlendFuncSeparate(sfactor, dfactor, sfactor, dfactor);
172 }
173
174 static GLboolean
175 blend_factor_is_dual_src(GLenum factor)
176 {
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);
181 }
182
183 static void
184 update_uses_dual_src(struct gl_context *ctx, int buf)
185 {
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));
191 }
192
193
194 /**
195 * Return the number of per-buffer blend states to update in
196 * glBlendFunc, glBlendFuncSeparate, glBlendEquation, etc.
197 */
198 static inline unsigned
199 num_buffers(const struct gl_context *ctx)
200 {
201 return ctx->Extensions.ARB_draw_buffers_blend
202 ? ctx->Const.MaxDrawBuffers : 1;
203 }
204
205
206 /**
207 * Set the separate blend source/dest factors for all draw buffers.
208 *
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.
213 */
214 void GLAPIENTRY
215 _mesa_BlendFuncSeparate( GLenum sfactorRGB, GLenum dfactorRGB,
216 GLenum sfactorA, GLenum dfactorA )
217 {
218 GET_CURRENT_CONTEXT(ctx);
219 const unsigned numBuffers = num_buffers(ctx);
220 unsigned buf;
221 bool changed = false;
222
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));
229
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) {
238 changed = true;
239 break;
240 }
241 }
242 }
243 else {
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) {
249 changed = true;
250 }
251 }
252
253 if (!changed)
254 return;
255
256 if (!validate_blend_factors(ctx, "glBlendFuncSeparate",
257 sfactorRGB, dfactorRGB,
258 sfactorA, dfactorA)) {
259 return;
260 }
261
262 FLUSH_VERTICES(ctx, _NEW_COLOR);
263
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;
269 }
270
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;
274 }
275
276 ctx->Color._BlendFuncPerBuffer = GL_FALSE;
277
278 if (ctx->Driver.BlendFuncSeparate) {
279 ctx->Driver.BlendFuncSeparate(ctx, sfactorRGB, dfactorRGB,
280 sfactorA, dfactorA);
281 }
282 }
283
284
285 /**
286 * Set blend source/dest factors for one color buffer/target.
287 */
288 void GLAPIENTRY
289 _mesa_BlendFunciARB(GLuint buf, GLenum sfactor, GLenum dfactor)
290 {
291 _mesa_BlendFuncSeparateiARB(buf, sfactor, dfactor, sfactor, dfactor);
292 }
293
294
295 static ALWAYS_INLINE void
296 blend_func_separatei(GLuint buf, GLenum sfactorRGB, GLenum dfactorRGB,
297 GLenum sfactorA, GLenum dfactorA, bool no_error)
298 {
299 GET_CURRENT_CONTEXT(ctx);
300
301 if (!no_error) {
302 if (!ctx->Extensions.ARB_draw_buffers_blend) {
303 _mesa_error(ctx, GL_INVALID_OPERATION, "glBlendFunc[Separate]i()");
304 return;
305 }
306
307 if (buf >= ctx->Const.MaxDrawBuffers) {
308 _mesa_error(ctx, GL_INVALID_VALUE, "glBlendFuncSeparatei(buffer=%u)",
309 buf);
310 return;
311 }
312 }
313
314 if (ctx->Color.Blend[buf].SrcRGB == sfactorRGB &&
315 ctx->Color.Blend[buf].DstRGB == dfactorRGB &&
316 ctx->Color.Blend[buf].SrcA == sfactorA &&
317 ctx->Color.Blend[buf].DstA == dfactorA)
318 return; /* no change */
319
320 if (!no_error && !validate_blend_factors(ctx, "glBlendFuncSeparatei",
321 sfactorRGB, dfactorRGB,
322 sfactorA, dfactorA)) {
323 return;
324 }
325
326 FLUSH_VERTICES(ctx, _NEW_COLOR);
327
328 ctx->Color.Blend[buf].SrcRGB = sfactorRGB;
329 ctx->Color.Blend[buf].DstRGB = dfactorRGB;
330 ctx->Color.Blend[buf].SrcA = sfactorA;
331 ctx->Color.Blend[buf].DstA = dfactorA;
332 update_uses_dual_src(ctx, buf);
333 ctx->Color._BlendFuncPerBuffer = GL_TRUE;
334 }
335
336
337 /**
338 * Set separate blend source/dest factors for one color buffer/target.
339 */
340 void GLAPIENTRY
341 _mesa_BlendFuncSeparateiARB(GLuint buf, GLenum sfactorRGB, GLenum dfactorRGB,
342 GLenum sfactorA, GLenum dfactorA)
343 {
344 blend_func_separatei(buf, sfactorRGB, dfactorRGB, sfactorA, dfactorA,
345 false);
346 }
347
348
349 /**
350 * Return true if \p mode is a legal blending equation, excluding
351 * GL_KHR_blend_equation_advanced modes.
352 */
353 static bool
354 legal_simple_blend_equation(const struct gl_context *ctx, GLenum mode)
355 {
356 switch (mode) {
357 case GL_FUNC_ADD:
358 case GL_FUNC_SUBTRACT:
359 case GL_FUNC_REVERSE_SUBTRACT:
360 return GL_TRUE;
361 case GL_MIN:
362 case GL_MAX:
363 return ctx->Extensions.EXT_blend_minmax;
364 default:
365 return GL_FALSE;
366 }
367 }
368
369 static enum gl_advanced_blend_mode
370 advanced_blend_mode_from_gl_enum(GLenum mode)
371 {
372 switch (mode) {
373 case GL_MULTIPLY_KHR:
374 return BLEND_MULTIPLY;
375 case GL_SCREEN_KHR:
376 return BLEND_SCREEN;
377 case GL_OVERLAY_KHR:
378 return BLEND_OVERLAY;
379 case GL_DARKEN_KHR:
380 return BLEND_DARKEN;
381 case GL_LIGHTEN_KHR:
382 return BLEND_LIGHTEN;
383 case GL_COLORDODGE_KHR:
384 return BLEND_COLORDODGE;
385 case GL_COLORBURN_KHR:
386 return BLEND_COLORBURN;
387 case GL_HARDLIGHT_KHR:
388 return BLEND_HARDLIGHT;
389 case GL_SOFTLIGHT_KHR:
390 return BLEND_SOFTLIGHT;
391 case GL_DIFFERENCE_KHR:
392 return BLEND_DIFFERENCE;
393 case GL_EXCLUSION_KHR:
394 return BLEND_EXCLUSION;
395 case GL_HSL_HUE_KHR:
396 return BLEND_HSL_HUE;
397 case GL_HSL_SATURATION_KHR:
398 return BLEND_HSL_SATURATION;
399 case GL_HSL_COLOR_KHR:
400 return BLEND_HSL_COLOR;
401 case GL_HSL_LUMINOSITY_KHR:
402 return BLEND_HSL_LUMINOSITY;
403 default:
404 return BLEND_NONE;
405 }
406 }
407
408 /**
409 * If \p mode is one of the advanced blending equations defined by
410 * GL_KHR_blend_equation_advanced (and the extension is supported),
411 * return the corresponding BLEND_* enum. Otherwise, return BLEND_NONE
412 * (which can also be treated as false).
413 */
414 static enum gl_advanced_blend_mode
415 advanced_blend_mode(const struct gl_context *ctx, GLenum mode)
416 {
417 return _mesa_has_KHR_blend_equation_advanced(ctx) ?
418 advanced_blend_mode_from_gl_enum(mode) : BLEND_NONE;
419 }
420
421 /* This is really an extension function! */
422 void GLAPIENTRY
423 _mesa_BlendEquation( GLenum mode )
424 {
425 GET_CURRENT_CONTEXT(ctx);
426 const unsigned numBuffers = num_buffers(ctx);
427 unsigned buf;
428 bool changed = false;
429 enum gl_advanced_blend_mode advanced_mode = advanced_blend_mode(ctx, mode);
430
431 if (MESA_VERBOSE & VERBOSE_API)
432 _mesa_debug(ctx, "glBlendEquation(%s)\n",
433 _mesa_enum_to_string(mode));
434
435 if (ctx->Color._BlendEquationPerBuffer) {
436 /* Check all per-buffer states */
437 for (buf = 0; buf < numBuffers; buf++) {
438 if (ctx->Color.Blend[buf].EquationRGB != mode ||
439 ctx->Color.Blend[buf].EquationA != mode) {
440 changed = true;
441 break;
442 }
443 }
444 }
445 else {
446 /* only need to check 0th per-buffer state */
447 if (ctx->Color.Blend[0].EquationRGB != mode ||
448 ctx->Color.Blend[0].EquationA != mode) {
449 changed = true;
450 }
451 }
452
453 if (!changed)
454 return;
455
456
457 if (!legal_simple_blend_equation(ctx, mode) && !advanced_mode) {
458 _mesa_error(ctx, GL_INVALID_ENUM, "glBlendEquation");
459 return;
460 }
461
462 FLUSH_VERTICES(ctx, _NEW_COLOR);
463
464 for (buf = 0; buf < numBuffers; buf++) {
465 ctx->Color.Blend[buf].EquationRGB = mode;
466 ctx->Color.Blend[buf].EquationA = mode;
467 }
468 ctx->Color._BlendEquationPerBuffer = GL_FALSE;
469 ctx->Color._AdvancedBlendMode = advanced_mode;
470
471 if (ctx->Driver.BlendEquationSeparate)
472 ctx->Driver.BlendEquationSeparate(ctx, mode, mode);
473 }
474
475
476 /**
477 * Set blend equation for one color buffer/target.
478 */
479 void GLAPIENTRY
480 _mesa_BlendEquationiARB(GLuint buf, GLenum mode)
481 {
482 GET_CURRENT_CONTEXT(ctx);
483 enum gl_advanced_blend_mode advanced_mode = advanced_blend_mode(ctx, mode);
484
485 if (MESA_VERBOSE & VERBOSE_API)
486 _mesa_debug(ctx, "glBlendEquationi(%u, %s)\n",
487 buf, _mesa_enum_to_string(mode));
488
489 if (buf >= ctx->Const.MaxDrawBuffers) {
490 _mesa_error(ctx, GL_INVALID_VALUE, "glBlendEquationi(buffer=%u)",
491 buf);
492 return;
493 }
494
495 if (!legal_simple_blend_equation(ctx, mode) && !advanced_mode) {
496 _mesa_error(ctx, GL_INVALID_ENUM, "glBlendEquationi");
497 return;
498 }
499
500 if (ctx->Color.Blend[buf].EquationRGB == mode &&
501 ctx->Color.Blend[buf].EquationA == mode)
502 return; /* no change */
503
504 FLUSH_VERTICES(ctx, _NEW_COLOR);
505 ctx->Color.Blend[buf].EquationRGB = mode;
506 ctx->Color.Blend[buf].EquationA = mode;
507 ctx->Color._BlendEquationPerBuffer = GL_TRUE;
508
509 if (buf == 0)
510 ctx->Color._AdvancedBlendMode = advanced_mode;
511 }
512
513
514 void GLAPIENTRY
515 _mesa_BlendEquationSeparate( GLenum modeRGB, GLenum modeA )
516 {
517 GET_CURRENT_CONTEXT(ctx);
518 const unsigned numBuffers = num_buffers(ctx);
519 unsigned buf;
520 bool changed = false;
521
522 if (MESA_VERBOSE & VERBOSE_API)
523 _mesa_debug(ctx, "glBlendEquationSeparateEXT(%s %s)\n",
524 _mesa_enum_to_string(modeRGB),
525 _mesa_enum_to_string(modeA));
526
527 if (ctx->Color._BlendEquationPerBuffer) {
528 /* Check all per-buffer states */
529 for (buf = 0; buf < numBuffers; buf++) {
530 if (ctx->Color.Blend[buf].EquationRGB != modeRGB ||
531 ctx->Color.Blend[buf].EquationA != modeA) {
532 changed = true;
533 break;
534 }
535 }
536 }
537 else {
538 /* only need to check 0th per-buffer state */
539 if (ctx->Color.Blend[0].EquationRGB != modeRGB ||
540 ctx->Color.Blend[0].EquationA != modeA) {
541 changed = true;
542 }
543 }
544
545 if (!changed)
546 return;
547
548 if ( (modeRGB != modeA) && !ctx->Extensions.EXT_blend_equation_separate ) {
549 _mesa_error(ctx, GL_INVALID_OPERATION,
550 "glBlendEquationSeparateEXT not supported by driver");
551 return;
552 }
553
554 /* Only allow simple blending equations.
555 * The GL_KHR_blend_equation_advanced spec says:
556 *
557 * "NOTE: These enums are not accepted by the <modeRGB> or <modeAlpha>
558 * parameters of BlendEquationSeparate or BlendEquationSeparatei."
559 */
560 if (!legal_simple_blend_equation(ctx, modeRGB)) {
561 _mesa_error(ctx, GL_INVALID_ENUM, "glBlendEquationSeparateEXT(modeRGB)");
562 return;
563 }
564
565 if (!legal_simple_blend_equation(ctx, modeA)) {
566 _mesa_error(ctx, GL_INVALID_ENUM, "glBlendEquationSeparateEXT(modeA)");
567 return;
568 }
569
570 FLUSH_VERTICES(ctx, _NEW_COLOR);
571
572 for (buf = 0; buf < numBuffers; buf++) {
573 ctx->Color.Blend[buf].EquationRGB = modeRGB;
574 ctx->Color.Blend[buf].EquationA = modeA;
575 }
576 ctx->Color._BlendEquationPerBuffer = GL_FALSE;
577 ctx->Color._AdvancedBlendMode = BLEND_NONE;
578
579 if (ctx->Driver.BlendEquationSeparate)
580 ctx->Driver.BlendEquationSeparate(ctx, modeRGB, modeA);
581 }
582
583
584 /**
585 * Set separate blend equations for one color buffer/target.
586 */
587 void GLAPIENTRY
588 _mesa_BlendEquationSeparateiARB(GLuint buf, GLenum modeRGB, GLenum modeA)
589 {
590 GET_CURRENT_CONTEXT(ctx);
591
592 if (MESA_VERBOSE & VERBOSE_API)
593 _mesa_debug(ctx, "glBlendEquationSeparatei(%u, %s %s)\n", buf,
594 _mesa_enum_to_string(modeRGB),
595 _mesa_enum_to_string(modeA));
596
597 if (buf >= ctx->Const.MaxDrawBuffers) {
598 _mesa_error(ctx, GL_INVALID_VALUE, "glBlendEquationSeparatei(buffer=%u)",
599 buf);
600 return;
601 }
602
603 /* Only allow simple blending equations.
604 * The GL_KHR_blend_equation_advanced spec says:
605 *
606 * "NOTE: These enums are not accepted by the <modeRGB> or <modeAlpha>
607 * parameters of BlendEquationSeparate or BlendEquationSeparatei."
608 */
609 if (!legal_simple_blend_equation(ctx, modeRGB)) {
610 _mesa_error(ctx, GL_INVALID_ENUM, "glBlendEquationSeparatei(modeRGB)");
611 return;
612 }
613
614 if (!legal_simple_blend_equation(ctx, modeA)) {
615 _mesa_error(ctx, GL_INVALID_ENUM, "glBlendEquationSeparatei(modeA)");
616 return;
617 }
618
619 if (ctx->Color.Blend[buf].EquationRGB == modeRGB &&
620 ctx->Color.Blend[buf].EquationA == modeA)
621 return; /* no change */
622
623 FLUSH_VERTICES(ctx, _NEW_COLOR);
624 ctx->Color.Blend[buf].EquationRGB = modeRGB;
625 ctx->Color.Blend[buf].EquationA = modeA;
626 ctx->Color._BlendEquationPerBuffer = GL_TRUE;
627 ctx->Color._AdvancedBlendMode = BLEND_NONE;
628 }
629
630
631 /**
632 * Set the blending color.
633 *
634 * \param red red color component.
635 * \param green green color component.
636 * \param blue blue color component.
637 * \param alpha alpha color component.
638 *
639 * \sa glBlendColor().
640 *
641 * Clamps the parameters and updates gl_colorbuffer_attrib::BlendColor. On a
642 * change, flushes the vertices and notifies the driver via
643 * dd_function_table::BlendColor callback.
644 */
645 void GLAPIENTRY
646 _mesa_BlendColor( GLclampf red, GLclampf green, GLclampf blue, GLclampf alpha )
647 {
648 GLfloat tmp[4];
649 GET_CURRENT_CONTEXT(ctx);
650
651 tmp[0] = red;
652 tmp[1] = green;
653 tmp[2] = blue;
654 tmp[3] = alpha;
655
656 if (TEST_EQ_4V(tmp, ctx->Color.BlendColorUnclamped))
657 return;
658
659 FLUSH_VERTICES(ctx, _NEW_COLOR);
660 COPY_4FV( ctx->Color.BlendColorUnclamped, tmp );
661
662 ctx->Color.BlendColor[0] = CLAMP(tmp[0], 0.0F, 1.0F);
663 ctx->Color.BlendColor[1] = CLAMP(tmp[1], 0.0F, 1.0F);
664 ctx->Color.BlendColor[2] = CLAMP(tmp[2], 0.0F, 1.0F);
665 ctx->Color.BlendColor[3] = CLAMP(tmp[3], 0.0F, 1.0F);
666
667 if (ctx->Driver.BlendColor)
668 ctx->Driver.BlendColor(ctx, ctx->Color.BlendColor);
669 }
670
671
672 /**
673 * Specify the alpha test function.
674 *
675 * \param func alpha comparison function.
676 * \param ref reference value.
677 *
678 * Verifies the parameters and updates gl_colorbuffer_attrib.
679 * On a change, flushes the vertices and notifies the driver via
680 * dd_function_table::AlphaFunc callback.
681 */
682 void GLAPIENTRY
683 _mesa_AlphaFunc( GLenum func, GLclampf ref )
684 {
685 GET_CURRENT_CONTEXT(ctx);
686
687 if (MESA_VERBOSE & VERBOSE_API)
688 _mesa_debug(ctx, "glAlphaFunc(%s, %f)\n",
689 _mesa_enum_to_string(func), ref);
690
691 if (ctx->Color.AlphaFunc == func && ctx->Color.AlphaRefUnclamped == ref)
692 return; /* no change */
693
694 switch (func) {
695 case GL_NEVER:
696 case GL_LESS:
697 case GL_EQUAL:
698 case GL_LEQUAL:
699 case GL_GREATER:
700 case GL_NOTEQUAL:
701 case GL_GEQUAL:
702 case GL_ALWAYS:
703 FLUSH_VERTICES(ctx, _NEW_COLOR);
704 ctx->Color.AlphaFunc = func;
705 ctx->Color.AlphaRefUnclamped = ref;
706 ctx->Color.AlphaRef = CLAMP(ref, 0.0F, 1.0F);
707
708 if (ctx->Driver.AlphaFunc)
709 ctx->Driver.AlphaFunc(ctx, func, ctx->Color.AlphaRef);
710 return;
711
712 default:
713 _mesa_error( ctx, GL_INVALID_ENUM, "glAlphaFunc(func)" );
714 return;
715 }
716 }
717
718
719 /**
720 * Specify a logic pixel operation for color index rendering.
721 *
722 * \param opcode operation.
723 *
724 * Verifies that \p opcode is a valid enum and updates
725 * gl_colorbuffer_attrib::LogicOp.
726 * On a change, flushes the vertices and notifies the driver via the
727 * dd_function_table::LogicOpcode callback.
728 */
729 void GLAPIENTRY
730 _mesa_LogicOp( GLenum opcode )
731 {
732 GET_CURRENT_CONTEXT(ctx);
733
734 if (MESA_VERBOSE & VERBOSE_API)
735 _mesa_debug(ctx, "glLogicOp(%s)\n", _mesa_enum_to_string(opcode));
736
737 switch (opcode) {
738 case GL_CLEAR:
739 case GL_SET:
740 case GL_COPY:
741 case GL_COPY_INVERTED:
742 case GL_NOOP:
743 case GL_INVERT:
744 case GL_AND:
745 case GL_NAND:
746 case GL_OR:
747 case GL_NOR:
748 case GL_XOR:
749 case GL_EQUIV:
750 case GL_AND_REVERSE:
751 case GL_AND_INVERTED:
752 case GL_OR_REVERSE:
753 case GL_OR_INVERTED:
754 break;
755 default:
756 _mesa_error( ctx, GL_INVALID_ENUM, "glLogicOp" );
757 return;
758 }
759
760 if (ctx->Color.LogicOp == opcode)
761 return;
762
763 FLUSH_VERTICES(ctx, _NEW_COLOR);
764 ctx->Color.LogicOp = opcode;
765
766 if (ctx->Driver.LogicOpcode)
767 ctx->Driver.LogicOpcode( ctx, opcode );
768 }
769
770
771 void GLAPIENTRY
772 _mesa_IndexMask( GLuint mask )
773 {
774 GET_CURRENT_CONTEXT(ctx);
775
776 if (ctx->Color.IndexMask == mask)
777 return;
778
779 FLUSH_VERTICES(ctx, _NEW_COLOR);
780 ctx->Color.IndexMask = mask;
781 }
782
783
784 /**
785 * Enable or disable writing of frame buffer color components.
786 *
787 * \param red whether to mask writing of the red color component.
788 * \param green whether to mask writing of the green color component.
789 * \param blue whether to mask writing of the blue color component.
790 * \param alpha whether to mask writing of the alpha color component.
791 *
792 * \sa glColorMask().
793 *
794 * Sets the appropriate value of gl_colorbuffer_attrib::ColorMask. On a
795 * change, flushes the vertices and notifies the driver via the
796 * dd_function_table::ColorMask callback.
797 */
798 void GLAPIENTRY
799 _mesa_ColorMask( GLboolean red, GLboolean green,
800 GLboolean blue, GLboolean alpha )
801 {
802 GET_CURRENT_CONTEXT(ctx);
803 GLubyte tmp[4];
804 GLuint i;
805 GLboolean flushed;
806
807 if (MESA_VERBOSE & VERBOSE_API)
808 _mesa_debug(ctx, "glColorMask(%d, %d, %d, %d)\n",
809 red, green, blue, alpha);
810
811 /* Shouldn't have any information about channel depth in core mesa
812 * -- should probably store these as the native booleans:
813 */
814 tmp[RCOMP] = red ? 0xff : 0x0;
815 tmp[GCOMP] = green ? 0xff : 0x0;
816 tmp[BCOMP] = blue ? 0xff : 0x0;
817 tmp[ACOMP] = alpha ? 0xff : 0x0;
818
819 flushed = GL_FALSE;
820 for (i = 0; i < ctx->Const.MaxDrawBuffers; i++) {
821 if (!TEST_EQ_4V(tmp, ctx->Color.ColorMask[i])) {
822 if (!flushed) {
823 FLUSH_VERTICES(ctx, _NEW_COLOR);
824 }
825 flushed = GL_TRUE;
826 COPY_4UBV(ctx->Color.ColorMask[i], tmp);
827 }
828 }
829
830 if (ctx->Driver.ColorMask)
831 ctx->Driver.ColorMask( ctx, red, green, blue, alpha );
832 }
833
834
835 /**
836 * For GL_EXT_draw_buffers2 and GL3
837 */
838 void GLAPIENTRY
839 _mesa_ColorMaski( GLuint buf, GLboolean red, GLboolean green,
840 GLboolean blue, GLboolean alpha )
841 {
842 GLubyte tmp[4];
843 GET_CURRENT_CONTEXT(ctx);
844
845 if (MESA_VERBOSE & VERBOSE_API)
846 _mesa_debug(ctx, "glColorMaskIndexed %u %d %d %d %d\n",
847 buf, red, green, blue, alpha);
848
849 if (buf >= ctx->Const.MaxDrawBuffers) {
850 _mesa_error(ctx, GL_INVALID_VALUE, "glColorMaskIndexed(buf=%u)", buf);
851 return;
852 }
853
854 /* Shouldn't have any information about channel depth in core mesa
855 * -- should probably store these as the native booleans:
856 */
857 tmp[RCOMP] = red ? 0xff : 0x0;
858 tmp[GCOMP] = green ? 0xff : 0x0;
859 tmp[BCOMP] = blue ? 0xff : 0x0;
860 tmp[ACOMP] = alpha ? 0xff : 0x0;
861
862 if (TEST_EQ_4V(tmp, ctx->Color.ColorMask[buf]))
863 return;
864
865 FLUSH_VERTICES(ctx, _NEW_COLOR);
866 COPY_4UBV(ctx->Color.ColorMask[buf], tmp);
867 }
868
869
870 void GLAPIENTRY
871 _mesa_ClampColor(GLenum target, GLenum clamp)
872 {
873 GET_CURRENT_CONTEXT(ctx);
874
875 /* Check for both the extension and the GL version, since the Intel driver
876 * does not advertise the extension in core profiles.
877 */
878 if (ctx->Version <= 30 && !ctx->Extensions.ARB_color_buffer_float) {
879 _mesa_error(ctx, GL_INVALID_OPERATION, "glClampColor()");
880 return;
881 }
882
883 if (clamp != GL_TRUE && clamp != GL_FALSE && clamp != GL_FIXED_ONLY_ARB) {
884 _mesa_error(ctx, GL_INVALID_ENUM, "glClampColorARB(clamp)");
885 return;
886 }
887
888 switch (target) {
889 case GL_CLAMP_VERTEX_COLOR_ARB:
890 if (ctx->API == API_OPENGL_CORE)
891 goto invalid_enum;
892 FLUSH_VERTICES(ctx, _NEW_LIGHT);
893 ctx->Light.ClampVertexColor = clamp;
894 _mesa_update_clamp_vertex_color(ctx, ctx->DrawBuffer);
895 break;
896 case GL_CLAMP_FRAGMENT_COLOR_ARB:
897 if (ctx->API == API_OPENGL_CORE)
898 goto invalid_enum;
899 FLUSH_VERTICES(ctx, _NEW_FRAG_CLAMP);
900 ctx->Color.ClampFragmentColor = clamp;
901 _mesa_update_clamp_fragment_color(ctx, ctx->DrawBuffer);
902 break;
903 case GL_CLAMP_READ_COLOR_ARB:
904 ctx->Color.ClampReadColor = clamp;
905 break;
906 default:
907 goto invalid_enum;
908 }
909 return;
910
911 invalid_enum:
912 _mesa_error(ctx, GL_INVALID_ENUM, "glClampColor(%s)",
913 _mesa_enum_to_string(target));
914 }
915
916 static GLboolean
917 get_clamp_color(const struct gl_framebuffer *fb, GLenum clamp)
918 {
919 if (clamp == GL_TRUE || clamp == GL_FALSE)
920 return clamp;
921
922 assert(clamp == GL_FIXED_ONLY);
923 if (!fb)
924 return GL_TRUE;
925
926 return fb->_AllColorBuffersFixedPoint;
927 }
928
929 GLboolean
930 _mesa_get_clamp_fragment_color(const struct gl_context *ctx,
931 const struct gl_framebuffer *drawFb)
932 {
933 return get_clamp_color(drawFb, ctx->Color.ClampFragmentColor);
934 }
935
936 GLboolean
937 _mesa_get_clamp_vertex_color(const struct gl_context *ctx,
938 const struct gl_framebuffer *drawFb)
939 {
940 return get_clamp_color(drawFb, ctx->Light.ClampVertexColor);
941 }
942
943 GLboolean
944 _mesa_get_clamp_read_color(const struct gl_context *ctx,
945 const struct gl_framebuffer *readFb)
946 {
947 return get_clamp_color(readFb, ctx->Color.ClampReadColor);
948 }
949
950 /**
951 * Update the ctx->Color._ClampFragmentColor field
952 */
953 void
954 _mesa_update_clamp_fragment_color(struct gl_context *ctx,
955 const struct gl_framebuffer *drawFb)
956 {
957 /* Don't clamp if:
958 * - there is no colorbuffer
959 * - all colorbuffers are unsigned normalized, so clamping has no effect
960 * - there is an integer colorbuffer
961 */
962 if (!drawFb || !drawFb->_HasSNormOrFloatColorBuffer ||
963 drawFb->_IntegerBuffers)
964 ctx->Color._ClampFragmentColor = GL_FALSE;
965 else
966 ctx->Color._ClampFragmentColor =
967 _mesa_get_clamp_fragment_color(ctx, drawFb);
968 }
969
970 /**
971 * Update the ctx->Color._ClampVertexColor field
972 */
973 void
974 _mesa_update_clamp_vertex_color(struct gl_context *ctx,
975 const struct gl_framebuffer *drawFb)
976 {
977 ctx->Light._ClampVertexColor =
978 _mesa_get_clamp_vertex_color(ctx, drawFb);
979 }
980
981 /**
982 * Returns an appropriate mesa_format for color rendering based on the
983 * GL_FRAMEBUFFER_SRGB state.
984 *
985 * Some drivers implement GL_FRAMEBUFFER_SRGB using a flag on the blend state
986 * (which GL_FRAMEBUFFER_SRGB maps to reasonably), but some have to do so by
987 * overriding the format of the surface. This is a helper for doing the
988 * surface format override variant.
989 */
990 mesa_format
991 _mesa_get_render_format(const struct gl_context *ctx, mesa_format format)
992 {
993 if (ctx->Color.sRGBEnabled)
994 return format;
995 else
996 return _mesa_get_srgb_format_linear(format);
997 }
998
999 /**********************************************************************/
1000 /** \name Initialization */
1001 /*@{*/
1002
1003 /**
1004 * Initialization of the context's Color attribute group.
1005 *
1006 * \param ctx GL context.
1007 *
1008 * Initializes the related fields in the context color attribute group,
1009 * __struct gl_contextRec::Color.
1010 */
1011 void _mesa_init_color( struct gl_context * ctx )
1012 {
1013 GLuint i;
1014
1015 /* Color buffer group */
1016 ctx->Color.IndexMask = ~0u;
1017 memset(ctx->Color.ColorMask, 0xff, sizeof(ctx->Color.ColorMask));
1018 ctx->Color.ClearIndex = 0;
1019 ASSIGN_4V( ctx->Color.ClearColor.f, 0, 0, 0, 0 );
1020 ctx->Color.AlphaEnabled = GL_FALSE;
1021 ctx->Color.AlphaFunc = GL_ALWAYS;
1022 ctx->Color.AlphaRef = 0;
1023 ctx->Color.BlendEnabled = 0x0;
1024 for (i = 0; i < ARRAY_SIZE(ctx->Color.Blend); i++) {
1025 ctx->Color.Blend[i].SrcRGB = GL_ONE;
1026 ctx->Color.Blend[i].DstRGB = GL_ZERO;
1027 ctx->Color.Blend[i].SrcA = GL_ONE;
1028 ctx->Color.Blend[i].DstA = GL_ZERO;
1029 ctx->Color.Blend[i].EquationRGB = GL_FUNC_ADD;
1030 ctx->Color.Blend[i].EquationA = GL_FUNC_ADD;
1031 }
1032 ASSIGN_4V( ctx->Color.BlendColor, 0.0, 0.0, 0.0, 0.0 );
1033 ASSIGN_4V( ctx->Color.BlendColorUnclamped, 0.0, 0.0, 0.0, 0.0 );
1034 ctx->Color.IndexLogicOpEnabled = GL_FALSE;
1035 ctx->Color.ColorLogicOpEnabled = GL_FALSE;
1036 ctx->Color.LogicOp = GL_COPY;
1037 ctx->Color.DitherFlag = GL_TRUE;
1038
1039 /* GL_FRONT is not possible on GLES. Instead GL_BACK will render to either
1040 * the front or the back buffer depending on the config */
1041 if (ctx->Visual.doubleBufferMode || _mesa_is_gles(ctx)) {
1042 ctx->Color.DrawBuffer[0] = GL_BACK;
1043 }
1044 else {
1045 ctx->Color.DrawBuffer[0] = GL_FRONT;
1046 }
1047
1048 ctx->Color.ClampFragmentColor = ctx->API == API_OPENGL_COMPAT ?
1049 GL_FIXED_ONLY_ARB : GL_FALSE;
1050 ctx->Color._ClampFragmentColor = GL_FALSE;
1051 ctx->Color.ClampReadColor = GL_FIXED_ONLY_ARB;
1052
1053 /* GLES 1/2/3 behaves as though GL_FRAMEBUFFER_SRGB is always enabled
1054 * if EGL_KHR_gl_colorspace has been used to request sRGB.
1055 */
1056 ctx->Color.sRGBEnabled = _mesa_is_gles(ctx);
1057
1058 ctx->Color.BlendCoherent = true;
1059 }
1060
1061 /*@}*/