mesa: add blend_equation_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 void GLAPIENTRY
286 _mesa_BlendFunciARB_no_error(GLuint buf, GLenum sfactor, GLenum dfactor)
287 {
288 _mesa_BlendFuncSeparateiARB_no_error(buf, sfactor, dfactor, sfactor,
289 dfactor);
290 }
291
292
293 /**
294 * Set blend source/dest factors for one color buffer/target.
295 */
296 void GLAPIENTRY
297 _mesa_BlendFunciARB(GLuint buf, GLenum sfactor, GLenum dfactor)
298 {
299 _mesa_BlendFuncSeparateiARB(buf, sfactor, dfactor, sfactor, dfactor);
300 }
301
302
303 static ALWAYS_INLINE void
304 blend_func_separatei(GLuint buf, GLenum sfactorRGB, GLenum dfactorRGB,
305 GLenum sfactorA, GLenum dfactorA, bool no_error)
306 {
307 GET_CURRENT_CONTEXT(ctx);
308
309 if (!no_error) {
310 if (!ctx->Extensions.ARB_draw_buffers_blend) {
311 _mesa_error(ctx, GL_INVALID_OPERATION, "glBlendFunc[Separate]i()");
312 return;
313 }
314
315 if (buf >= ctx->Const.MaxDrawBuffers) {
316 _mesa_error(ctx, GL_INVALID_VALUE, "glBlendFuncSeparatei(buffer=%u)",
317 buf);
318 return;
319 }
320 }
321
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 */
327
328 if (!no_error && !validate_blend_factors(ctx, "glBlendFuncSeparatei",
329 sfactorRGB, dfactorRGB,
330 sfactorA, dfactorA)) {
331 return;
332 }
333
334 FLUSH_VERTICES(ctx, _NEW_COLOR);
335
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;
342 }
343
344
345 void GLAPIENTRY
346 _mesa_BlendFuncSeparateiARB_no_error(GLuint buf, GLenum sfactorRGB,
347 GLenum dfactorRGB, GLenum sfactorA,
348 GLenum dfactorA)
349 {
350 blend_func_separatei(buf, sfactorRGB, dfactorRGB, sfactorA, dfactorA,
351 true);
352 }
353
354
355 /**
356 * Set separate blend source/dest factors for one color buffer/target.
357 */
358 void GLAPIENTRY
359 _mesa_BlendFuncSeparateiARB(GLuint buf, GLenum sfactorRGB, GLenum dfactorRGB,
360 GLenum sfactorA, GLenum dfactorA)
361 {
362 blend_func_separatei(buf, sfactorRGB, dfactorRGB, sfactorA, dfactorA,
363 false);
364 }
365
366
367 /**
368 * Return true if \p mode is a legal blending equation, excluding
369 * GL_KHR_blend_equation_advanced modes.
370 */
371 static bool
372 legal_simple_blend_equation(const struct gl_context *ctx, GLenum mode)
373 {
374 switch (mode) {
375 case GL_FUNC_ADD:
376 case GL_FUNC_SUBTRACT:
377 case GL_FUNC_REVERSE_SUBTRACT:
378 return GL_TRUE;
379 case GL_MIN:
380 case GL_MAX:
381 return ctx->Extensions.EXT_blend_minmax;
382 default:
383 return GL_FALSE;
384 }
385 }
386
387 static enum gl_advanced_blend_mode
388 advanced_blend_mode_from_gl_enum(GLenum mode)
389 {
390 switch (mode) {
391 case GL_MULTIPLY_KHR:
392 return BLEND_MULTIPLY;
393 case GL_SCREEN_KHR:
394 return BLEND_SCREEN;
395 case GL_OVERLAY_KHR:
396 return BLEND_OVERLAY;
397 case GL_DARKEN_KHR:
398 return BLEND_DARKEN;
399 case GL_LIGHTEN_KHR:
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;
413 case GL_HSL_HUE_KHR:
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;
421 default:
422 return BLEND_NONE;
423 }
424 }
425
426 /**
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).
431 */
432 static enum gl_advanced_blend_mode
433 advanced_blend_mode(const struct gl_context *ctx, GLenum mode)
434 {
435 return _mesa_has_KHR_blend_equation_advanced(ctx) ?
436 advanced_blend_mode_from_gl_enum(mode) : BLEND_NONE;
437 }
438
439 /* This is really an extension function! */
440 void GLAPIENTRY
441 _mesa_BlendEquation( GLenum mode )
442 {
443 GET_CURRENT_CONTEXT(ctx);
444 const unsigned numBuffers = num_buffers(ctx);
445 unsigned buf;
446 bool changed = false;
447 enum gl_advanced_blend_mode advanced_mode = advanced_blend_mode(ctx, mode);
448
449 if (MESA_VERBOSE & VERBOSE_API)
450 _mesa_debug(ctx, "glBlendEquation(%s)\n",
451 _mesa_enum_to_string(mode));
452
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) {
458 changed = true;
459 break;
460 }
461 }
462 }
463 else {
464 /* only need to check 0th per-buffer state */
465 if (ctx->Color.Blend[0].EquationRGB != mode ||
466 ctx->Color.Blend[0].EquationA != mode) {
467 changed = true;
468 }
469 }
470
471 if (!changed)
472 return;
473
474
475 if (!legal_simple_blend_equation(ctx, mode) && !advanced_mode) {
476 _mesa_error(ctx, GL_INVALID_ENUM, "glBlendEquation");
477 return;
478 }
479
480 FLUSH_VERTICES(ctx, _NEW_COLOR);
481
482 for (buf = 0; buf < numBuffers; buf++) {
483 ctx->Color.Blend[buf].EquationRGB = mode;
484 ctx->Color.Blend[buf].EquationA = mode;
485 }
486 ctx->Color._BlendEquationPerBuffer = GL_FALSE;
487 ctx->Color._AdvancedBlendMode = advanced_mode;
488
489 if (ctx->Driver.BlendEquationSeparate)
490 ctx->Driver.BlendEquationSeparate(ctx, mode, mode);
491 }
492
493
494 /**
495 * Set blend equation for one color buffer/target.
496 */
497 void GLAPIENTRY
498 _mesa_BlendEquationiARB(GLuint buf, GLenum mode)
499 {
500 GET_CURRENT_CONTEXT(ctx);
501 enum gl_advanced_blend_mode advanced_mode = advanced_blend_mode(ctx, mode);
502
503 if (MESA_VERBOSE & VERBOSE_API)
504 _mesa_debug(ctx, "glBlendEquationi(%u, %s)\n",
505 buf, _mesa_enum_to_string(mode));
506
507 if (buf >= ctx->Const.MaxDrawBuffers) {
508 _mesa_error(ctx, GL_INVALID_VALUE, "glBlendEquationi(buffer=%u)",
509 buf);
510 return;
511 }
512
513 if (!legal_simple_blend_equation(ctx, mode) && !advanced_mode) {
514 _mesa_error(ctx, GL_INVALID_ENUM, "glBlendEquationi");
515 return;
516 }
517
518 if (ctx->Color.Blend[buf].EquationRGB == mode &&
519 ctx->Color.Blend[buf].EquationA == mode)
520 return; /* no change */
521
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;
526
527 if (buf == 0)
528 ctx->Color._AdvancedBlendMode = advanced_mode;
529 }
530
531
532 void GLAPIENTRY
533 _mesa_BlendEquationSeparate( GLenum modeRGB, GLenum modeA )
534 {
535 GET_CURRENT_CONTEXT(ctx);
536 const unsigned numBuffers = num_buffers(ctx);
537 unsigned buf;
538 bool changed = false;
539
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));
544
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) {
550 changed = true;
551 break;
552 }
553 }
554 }
555 else {
556 /* only need to check 0th per-buffer state */
557 if (ctx->Color.Blend[0].EquationRGB != modeRGB ||
558 ctx->Color.Blend[0].EquationA != modeA) {
559 changed = true;
560 }
561 }
562
563 if (!changed)
564 return;
565
566 if ( (modeRGB != modeA) && !ctx->Extensions.EXT_blend_equation_separate ) {
567 _mesa_error(ctx, GL_INVALID_OPERATION,
568 "glBlendEquationSeparateEXT not supported by driver");
569 return;
570 }
571
572 /* Only allow simple blending equations.
573 * The GL_KHR_blend_equation_advanced spec says:
574 *
575 * "NOTE: These enums are not accepted by the <modeRGB> or <modeAlpha>
576 * parameters of BlendEquationSeparate or BlendEquationSeparatei."
577 */
578 if (!legal_simple_blend_equation(ctx, modeRGB)) {
579 _mesa_error(ctx, GL_INVALID_ENUM, "glBlendEquationSeparateEXT(modeRGB)");
580 return;
581 }
582
583 if (!legal_simple_blend_equation(ctx, modeA)) {
584 _mesa_error(ctx, GL_INVALID_ENUM, "glBlendEquationSeparateEXT(modeA)");
585 return;
586 }
587
588 FLUSH_VERTICES(ctx, _NEW_COLOR);
589
590 for (buf = 0; buf < numBuffers; buf++) {
591 ctx->Color.Blend[buf].EquationRGB = modeRGB;
592 ctx->Color.Blend[buf].EquationA = modeA;
593 }
594 ctx->Color._BlendEquationPerBuffer = GL_FALSE;
595 ctx->Color._AdvancedBlendMode = BLEND_NONE;
596
597 if (ctx->Driver.BlendEquationSeparate)
598 ctx->Driver.BlendEquationSeparate(ctx, modeRGB, modeA);
599 }
600
601
602 static void
603 blend_equation_separatei(struct gl_context *ctx, GLuint buf, GLenum modeRGB,
604 GLenum modeA)
605 {
606 if (ctx->Color.Blend[buf].EquationRGB == modeRGB &&
607 ctx->Color.Blend[buf].EquationA == modeA)
608 return; /* no change */
609
610 FLUSH_VERTICES(ctx, _NEW_COLOR);
611 ctx->Color.Blend[buf].EquationRGB = modeRGB;
612 ctx->Color.Blend[buf].EquationA = modeA;
613 ctx->Color._BlendEquationPerBuffer = GL_TRUE;
614 ctx->Color._AdvancedBlendMode = BLEND_NONE;
615 }
616
617
618 /**
619 * Set separate blend equations for one color buffer/target.
620 */
621 void GLAPIENTRY
622 _mesa_BlendEquationSeparateiARB(GLuint buf, GLenum modeRGB, GLenum modeA)
623 {
624 GET_CURRENT_CONTEXT(ctx);
625
626 if (MESA_VERBOSE & VERBOSE_API)
627 _mesa_debug(ctx, "glBlendEquationSeparatei(%u, %s %s)\n", buf,
628 _mesa_enum_to_string(modeRGB),
629 _mesa_enum_to_string(modeA));
630
631 if (buf >= ctx->Const.MaxDrawBuffers) {
632 _mesa_error(ctx, GL_INVALID_VALUE, "glBlendEquationSeparatei(buffer=%u)",
633 buf);
634 return;
635 }
636
637 /* Only allow simple blending equations.
638 * The GL_KHR_blend_equation_advanced spec says:
639 *
640 * "NOTE: These enums are not accepted by the <modeRGB> or <modeAlpha>
641 * parameters of BlendEquationSeparate or BlendEquationSeparatei."
642 */
643 if (!legal_simple_blend_equation(ctx, modeRGB)) {
644 _mesa_error(ctx, GL_INVALID_ENUM, "glBlendEquationSeparatei(modeRGB)");
645 return;
646 }
647
648 if (!legal_simple_blend_equation(ctx, modeA)) {
649 _mesa_error(ctx, GL_INVALID_ENUM, "glBlendEquationSeparatei(modeA)");
650 return;
651 }
652
653 blend_equation_separatei(ctx, buf, modeRGB, modeA);
654 }
655
656
657 /**
658 * Set the blending color.
659 *
660 * \param red red color component.
661 * \param green green color component.
662 * \param blue blue color component.
663 * \param alpha alpha color component.
664 *
665 * \sa glBlendColor().
666 *
667 * Clamps the parameters and updates gl_colorbuffer_attrib::BlendColor. On a
668 * change, flushes the vertices and notifies the driver via
669 * dd_function_table::BlendColor callback.
670 */
671 void GLAPIENTRY
672 _mesa_BlendColor( GLclampf red, GLclampf green, GLclampf blue, GLclampf alpha )
673 {
674 GLfloat tmp[4];
675 GET_CURRENT_CONTEXT(ctx);
676
677 tmp[0] = red;
678 tmp[1] = green;
679 tmp[2] = blue;
680 tmp[3] = alpha;
681
682 if (TEST_EQ_4V(tmp, ctx->Color.BlendColorUnclamped))
683 return;
684
685 FLUSH_VERTICES(ctx, _NEW_COLOR);
686 COPY_4FV( ctx->Color.BlendColorUnclamped, tmp );
687
688 ctx->Color.BlendColor[0] = CLAMP(tmp[0], 0.0F, 1.0F);
689 ctx->Color.BlendColor[1] = CLAMP(tmp[1], 0.0F, 1.0F);
690 ctx->Color.BlendColor[2] = CLAMP(tmp[2], 0.0F, 1.0F);
691 ctx->Color.BlendColor[3] = CLAMP(tmp[3], 0.0F, 1.0F);
692
693 if (ctx->Driver.BlendColor)
694 ctx->Driver.BlendColor(ctx, ctx->Color.BlendColor);
695 }
696
697
698 /**
699 * Specify the alpha test function.
700 *
701 * \param func alpha comparison function.
702 * \param ref reference value.
703 *
704 * Verifies the parameters and updates gl_colorbuffer_attrib.
705 * On a change, flushes the vertices and notifies the driver via
706 * dd_function_table::AlphaFunc callback.
707 */
708 void GLAPIENTRY
709 _mesa_AlphaFunc( GLenum func, GLclampf ref )
710 {
711 GET_CURRENT_CONTEXT(ctx);
712
713 if (MESA_VERBOSE & VERBOSE_API)
714 _mesa_debug(ctx, "glAlphaFunc(%s, %f)\n",
715 _mesa_enum_to_string(func), ref);
716
717 if (ctx->Color.AlphaFunc == func && ctx->Color.AlphaRefUnclamped == ref)
718 return; /* no change */
719
720 switch (func) {
721 case GL_NEVER:
722 case GL_LESS:
723 case GL_EQUAL:
724 case GL_LEQUAL:
725 case GL_GREATER:
726 case GL_NOTEQUAL:
727 case GL_GEQUAL:
728 case GL_ALWAYS:
729 FLUSH_VERTICES(ctx, _NEW_COLOR);
730 ctx->Color.AlphaFunc = func;
731 ctx->Color.AlphaRefUnclamped = ref;
732 ctx->Color.AlphaRef = CLAMP(ref, 0.0F, 1.0F);
733
734 if (ctx->Driver.AlphaFunc)
735 ctx->Driver.AlphaFunc(ctx, func, ctx->Color.AlphaRef);
736 return;
737
738 default:
739 _mesa_error( ctx, GL_INVALID_ENUM, "glAlphaFunc(func)" );
740 return;
741 }
742 }
743
744
745 /**
746 * Specify a logic pixel operation for color index rendering.
747 *
748 * \param opcode operation.
749 *
750 * Verifies that \p opcode is a valid enum and updates
751 * gl_colorbuffer_attrib::LogicOp.
752 * On a change, flushes the vertices and notifies the driver via the
753 * dd_function_table::LogicOpcode callback.
754 */
755 void GLAPIENTRY
756 _mesa_LogicOp( GLenum opcode )
757 {
758 GET_CURRENT_CONTEXT(ctx);
759
760 if (MESA_VERBOSE & VERBOSE_API)
761 _mesa_debug(ctx, "glLogicOp(%s)\n", _mesa_enum_to_string(opcode));
762
763 switch (opcode) {
764 case GL_CLEAR:
765 case GL_SET:
766 case GL_COPY:
767 case GL_COPY_INVERTED:
768 case GL_NOOP:
769 case GL_INVERT:
770 case GL_AND:
771 case GL_NAND:
772 case GL_OR:
773 case GL_NOR:
774 case GL_XOR:
775 case GL_EQUIV:
776 case GL_AND_REVERSE:
777 case GL_AND_INVERTED:
778 case GL_OR_REVERSE:
779 case GL_OR_INVERTED:
780 break;
781 default:
782 _mesa_error( ctx, GL_INVALID_ENUM, "glLogicOp" );
783 return;
784 }
785
786 if (ctx->Color.LogicOp == opcode)
787 return;
788
789 FLUSH_VERTICES(ctx, _NEW_COLOR);
790 ctx->Color.LogicOp = opcode;
791
792 if (ctx->Driver.LogicOpcode)
793 ctx->Driver.LogicOpcode( ctx, opcode );
794 }
795
796
797 void GLAPIENTRY
798 _mesa_IndexMask( GLuint mask )
799 {
800 GET_CURRENT_CONTEXT(ctx);
801
802 if (ctx->Color.IndexMask == mask)
803 return;
804
805 FLUSH_VERTICES(ctx, _NEW_COLOR);
806 ctx->Color.IndexMask = mask;
807 }
808
809
810 /**
811 * Enable or disable writing of frame buffer color components.
812 *
813 * \param red whether to mask writing of the red color component.
814 * \param green whether to mask writing of the green color component.
815 * \param blue whether to mask writing of the blue color component.
816 * \param alpha whether to mask writing of the alpha color component.
817 *
818 * \sa glColorMask().
819 *
820 * Sets the appropriate value of gl_colorbuffer_attrib::ColorMask. On a
821 * change, flushes the vertices and notifies the driver via the
822 * dd_function_table::ColorMask callback.
823 */
824 void GLAPIENTRY
825 _mesa_ColorMask( GLboolean red, GLboolean green,
826 GLboolean blue, GLboolean alpha )
827 {
828 GET_CURRENT_CONTEXT(ctx);
829 GLubyte tmp[4];
830 GLuint i;
831 GLboolean flushed;
832
833 if (MESA_VERBOSE & VERBOSE_API)
834 _mesa_debug(ctx, "glColorMask(%d, %d, %d, %d)\n",
835 red, green, blue, alpha);
836
837 /* Shouldn't have any information about channel depth in core mesa
838 * -- should probably store these as the native booleans:
839 */
840 tmp[RCOMP] = red ? 0xff : 0x0;
841 tmp[GCOMP] = green ? 0xff : 0x0;
842 tmp[BCOMP] = blue ? 0xff : 0x0;
843 tmp[ACOMP] = alpha ? 0xff : 0x0;
844
845 flushed = GL_FALSE;
846 for (i = 0; i < ctx->Const.MaxDrawBuffers; i++) {
847 if (!TEST_EQ_4V(tmp, ctx->Color.ColorMask[i])) {
848 if (!flushed) {
849 FLUSH_VERTICES(ctx, _NEW_COLOR);
850 }
851 flushed = GL_TRUE;
852 COPY_4UBV(ctx->Color.ColorMask[i], tmp);
853 }
854 }
855
856 if (ctx->Driver.ColorMask)
857 ctx->Driver.ColorMask( ctx, red, green, blue, alpha );
858 }
859
860
861 /**
862 * For GL_EXT_draw_buffers2 and GL3
863 */
864 void GLAPIENTRY
865 _mesa_ColorMaski( GLuint buf, GLboolean red, GLboolean green,
866 GLboolean blue, GLboolean alpha )
867 {
868 GLubyte tmp[4];
869 GET_CURRENT_CONTEXT(ctx);
870
871 if (MESA_VERBOSE & VERBOSE_API)
872 _mesa_debug(ctx, "glColorMaskIndexed %u %d %d %d %d\n",
873 buf, red, green, blue, alpha);
874
875 if (buf >= ctx->Const.MaxDrawBuffers) {
876 _mesa_error(ctx, GL_INVALID_VALUE, "glColorMaskIndexed(buf=%u)", buf);
877 return;
878 }
879
880 /* Shouldn't have any information about channel depth in core mesa
881 * -- should probably store these as the native booleans:
882 */
883 tmp[RCOMP] = red ? 0xff : 0x0;
884 tmp[GCOMP] = green ? 0xff : 0x0;
885 tmp[BCOMP] = blue ? 0xff : 0x0;
886 tmp[ACOMP] = alpha ? 0xff : 0x0;
887
888 if (TEST_EQ_4V(tmp, ctx->Color.ColorMask[buf]))
889 return;
890
891 FLUSH_VERTICES(ctx, _NEW_COLOR);
892 COPY_4UBV(ctx->Color.ColorMask[buf], tmp);
893 }
894
895
896 void GLAPIENTRY
897 _mesa_ClampColor(GLenum target, GLenum clamp)
898 {
899 GET_CURRENT_CONTEXT(ctx);
900
901 /* Check for both the extension and the GL version, since the Intel driver
902 * does not advertise the extension in core profiles.
903 */
904 if (ctx->Version <= 30 && !ctx->Extensions.ARB_color_buffer_float) {
905 _mesa_error(ctx, GL_INVALID_OPERATION, "glClampColor()");
906 return;
907 }
908
909 if (clamp != GL_TRUE && clamp != GL_FALSE && clamp != GL_FIXED_ONLY_ARB) {
910 _mesa_error(ctx, GL_INVALID_ENUM, "glClampColorARB(clamp)");
911 return;
912 }
913
914 switch (target) {
915 case GL_CLAMP_VERTEX_COLOR_ARB:
916 if (ctx->API == API_OPENGL_CORE)
917 goto invalid_enum;
918 FLUSH_VERTICES(ctx, _NEW_LIGHT);
919 ctx->Light.ClampVertexColor = clamp;
920 _mesa_update_clamp_vertex_color(ctx, ctx->DrawBuffer);
921 break;
922 case GL_CLAMP_FRAGMENT_COLOR_ARB:
923 if (ctx->API == API_OPENGL_CORE)
924 goto invalid_enum;
925 FLUSH_VERTICES(ctx, _NEW_FRAG_CLAMP);
926 ctx->Color.ClampFragmentColor = clamp;
927 _mesa_update_clamp_fragment_color(ctx, ctx->DrawBuffer);
928 break;
929 case GL_CLAMP_READ_COLOR_ARB:
930 ctx->Color.ClampReadColor = clamp;
931 break;
932 default:
933 goto invalid_enum;
934 }
935 return;
936
937 invalid_enum:
938 _mesa_error(ctx, GL_INVALID_ENUM, "glClampColor(%s)",
939 _mesa_enum_to_string(target));
940 }
941
942 static GLboolean
943 get_clamp_color(const struct gl_framebuffer *fb, GLenum clamp)
944 {
945 if (clamp == GL_TRUE || clamp == GL_FALSE)
946 return clamp;
947
948 assert(clamp == GL_FIXED_ONLY);
949 if (!fb)
950 return GL_TRUE;
951
952 return fb->_AllColorBuffersFixedPoint;
953 }
954
955 GLboolean
956 _mesa_get_clamp_fragment_color(const struct gl_context *ctx,
957 const struct gl_framebuffer *drawFb)
958 {
959 return get_clamp_color(drawFb, ctx->Color.ClampFragmentColor);
960 }
961
962 GLboolean
963 _mesa_get_clamp_vertex_color(const struct gl_context *ctx,
964 const struct gl_framebuffer *drawFb)
965 {
966 return get_clamp_color(drawFb, ctx->Light.ClampVertexColor);
967 }
968
969 GLboolean
970 _mesa_get_clamp_read_color(const struct gl_context *ctx,
971 const struct gl_framebuffer *readFb)
972 {
973 return get_clamp_color(readFb, ctx->Color.ClampReadColor);
974 }
975
976 /**
977 * Update the ctx->Color._ClampFragmentColor field
978 */
979 void
980 _mesa_update_clamp_fragment_color(struct gl_context *ctx,
981 const struct gl_framebuffer *drawFb)
982 {
983 /* Don't clamp if:
984 * - there is no colorbuffer
985 * - all colorbuffers are unsigned normalized, so clamping has no effect
986 * - there is an integer colorbuffer
987 */
988 if (!drawFb || !drawFb->_HasSNormOrFloatColorBuffer ||
989 drawFb->_IntegerBuffers)
990 ctx->Color._ClampFragmentColor = GL_FALSE;
991 else
992 ctx->Color._ClampFragmentColor =
993 _mesa_get_clamp_fragment_color(ctx, drawFb);
994 }
995
996 /**
997 * Update the ctx->Color._ClampVertexColor field
998 */
999 void
1000 _mesa_update_clamp_vertex_color(struct gl_context *ctx,
1001 const struct gl_framebuffer *drawFb)
1002 {
1003 ctx->Light._ClampVertexColor =
1004 _mesa_get_clamp_vertex_color(ctx, drawFb);
1005 }
1006
1007 /**
1008 * Returns an appropriate mesa_format for color rendering based on the
1009 * GL_FRAMEBUFFER_SRGB state.
1010 *
1011 * Some drivers implement GL_FRAMEBUFFER_SRGB using a flag on the blend state
1012 * (which GL_FRAMEBUFFER_SRGB maps to reasonably), but some have to do so by
1013 * overriding the format of the surface. This is a helper for doing the
1014 * surface format override variant.
1015 */
1016 mesa_format
1017 _mesa_get_render_format(const struct gl_context *ctx, mesa_format format)
1018 {
1019 if (ctx->Color.sRGBEnabled)
1020 return format;
1021 else
1022 return _mesa_get_srgb_format_linear(format);
1023 }
1024
1025 /**********************************************************************/
1026 /** \name Initialization */
1027 /*@{*/
1028
1029 /**
1030 * Initialization of the context's Color attribute group.
1031 *
1032 * \param ctx GL context.
1033 *
1034 * Initializes the related fields in the context color attribute group,
1035 * __struct gl_contextRec::Color.
1036 */
1037 void _mesa_init_color( struct gl_context * ctx )
1038 {
1039 GLuint i;
1040
1041 /* Color buffer group */
1042 ctx->Color.IndexMask = ~0u;
1043 memset(ctx->Color.ColorMask, 0xff, sizeof(ctx->Color.ColorMask));
1044 ctx->Color.ClearIndex = 0;
1045 ASSIGN_4V( ctx->Color.ClearColor.f, 0, 0, 0, 0 );
1046 ctx->Color.AlphaEnabled = GL_FALSE;
1047 ctx->Color.AlphaFunc = GL_ALWAYS;
1048 ctx->Color.AlphaRef = 0;
1049 ctx->Color.BlendEnabled = 0x0;
1050 for (i = 0; i < ARRAY_SIZE(ctx->Color.Blend); i++) {
1051 ctx->Color.Blend[i].SrcRGB = GL_ONE;
1052 ctx->Color.Blend[i].DstRGB = GL_ZERO;
1053 ctx->Color.Blend[i].SrcA = GL_ONE;
1054 ctx->Color.Blend[i].DstA = GL_ZERO;
1055 ctx->Color.Blend[i].EquationRGB = GL_FUNC_ADD;
1056 ctx->Color.Blend[i].EquationA = GL_FUNC_ADD;
1057 }
1058 ASSIGN_4V( ctx->Color.BlendColor, 0.0, 0.0, 0.0, 0.0 );
1059 ASSIGN_4V( ctx->Color.BlendColorUnclamped, 0.0, 0.0, 0.0, 0.0 );
1060 ctx->Color.IndexLogicOpEnabled = GL_FALSE;
1061 ctx->Color.ColorLogicOpEnabled = GL_FALSE;
1062 ctx->Color.LogicOp = GL_COPY;
1063 ctx->Color.DitherFlag = GL_TRUE;
1064
1065 /* GL_FRONT is not possible on GLES. Instead GL_BACK will render to either
1066 * the front or the back buffer depending on the config */
1067 if (ctx->Visual.doubleBufferMode || _mesa_is_gles(ctx)) {
1068 ctx->Color.DrawBuffer[0] = GL_BACK;
1069 }
1070 else {
1071 ctx->Color.DrawBuffer[0] = GL_FRONT;
1072 }
1073
1074 ctx->Color.ClampFragmentColor = ctx->API == API_OPENGL_COMPAT ?
1075 GL_FIXED_ONLY_ARB : GL_FALSE;
1076 ctx->Color._ClampFragmentColor = GL_FALSE;
1077 ctx->Color.ClampReadColor = GL_FIXED_ONLY_ARB;
1078
1079 /* GLES 1/2/3 behaves as though GL_FRAMEBUFFER_SRGB is always enabled
1080 * if EGL_KHR_gl_colorspace has been used to request sRGB.
1081 */
1082 ctx->Color.sRGBEnabled = _mesa_is_gles(ctx);
1083
1084 ctx->Color.BlendCoherent = true;
1085 }
1086
1087 /*@}*/