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