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