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