mesa: optimize no-change check in _mesa_BlendEquation()
[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 GLboolean changed;
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 if ( (modeRGB != modeA) && !ctx->Extensions.EXT_blend_equation_separate ) {
444 _mesa_error(ctx, GL_INVALID_OPERATION,
445 "glBlendEquationSeparateEXT not supported by driver");
446 return;
447 }
448
449 if (!legal_blend_equation(ctx, modeRGB)) {
450 _mesa_error(ctx, GL_INVALID_ENUM, "glBlendEquationSeparateEXT(modeRGB)");
451 return;
452 }
453
454 if (!legal_blend_equation(ctx, modeA)) {
455 _mesa_error(ctx, GL_INVALID_ENUM, "glBlendEquationSeparateEXT(modeA)");
456 return;
457 }
458
459 numBuffers = ctx->Extensions.ARB_draw_buffers_blend
460 ? ctx->Const.MaxDrawBuffers : 1;
461
462 changed = GL_FALSE;
463 for (buf = 0; buf < numBuffers; buf++) {
464 if (ctx->Color.Blend[buf].EquationRGB != modeRGB ||
465 ctx->Color.Blend[buf].EquationA != modeA) {
466 changed = GL_TRUE;
467 break;
468 }
469 }
470 if (!changed)
471 return;
472
473 FLUSH_VERTICES(ctx, _NEW_COLOR);
474 for (buf = 0; buf < numBuffers; buf++) {
475 ctx->Color.Blend[buf].EquationRGB = modeRGB;
476 ctx->Color.Blend[buf].EquationA = modeA;
477 }
478 ctx->Color._BlendEquationPerBuffer = GL_FALSE;
479
480 if (ctx->Driver.BlendEquationSeparate)
481 ctx->Driver.BlendEquationSeparate(ctx, modeRGB, modeA);
482 }
483
484
485 /**
486 * Set separate blend equations for one color buffer/target.
487 */
488 void GLAPIENTRY
489 _mesa_BlendEquationSeparateiARB(GLuint buf, GLenum modeRGB, GLenum modeA)
490 {
491 GET_CURRENT_CONTEXT(ctx);
492
493 if (MESA_VERBOSE & VERBOSE_API)
494 _mesa_debug(ctx, "glBlendEquationSeparatei(%u, %s %s)\n", buf,
495 _mesa_enum_to_string(modeRGB),
496 _mesa_enum_to_string(modeA));
497
498 if (buf >= ctx->Const.MaxDrawBuffers) {
499 _mesa_error(ctx, GL_INVALID_VALUE, "glBlendEquationSeparatei(buffer=%u)",
500 buf);
501 return;
502 }
503
504 if (!legal_blend_equation(ctx, modeRGB)) {
505 _mesa_error(ctx, GL_INVALID_ENUM, "glBlendEquationSeparatei(modeRGB)");
506 return;
507 }
508
509 if (!legal_blend_equation(ctx, modeA)) {
510 _mesa_error(ctx, GL_INVALID_ENUM, "glBlendEquationSeparatei(modeA)");
511 return;
512 }
513
514 if (ctx->Color.Blend[buf].EquationRGB == modeRGB &&
515 ctx->Color.Blend[buf].EquationA == modeA)
516 return; /* no change */
517
518 FLUSH_VERTICES(ctx, _NEW_COLOR);
519 ctx->Color.Blend[buf].EquationRGB = modeRGB;
520 ctx->Color.Blend[buf].EquationA = modeA;
521 ctx->Color._BlendEquationPerBuffer = GL_TRUE;
522 }
523
524
525 /**
526 * Set the blending color.
527 *
528 * \param red red color component.
529 * \param green green color component.
530 * \param blue blue color component.
531 * \param alpha alpha color component.
532 *
533 * \sa glBlendColor().
534 *
535 * Clamps the parameters and updates gl_colorbuffer_attrib::BlendColor. On a
536 * change, flushes the vertices and notifies the driver via
537 * dd_function_table::BlendColor callback.
538 */
539 void GLAPIENTRY
540 _mesa_BlendColor( GLclampf red, GLclampf green, GLclampf blue, GLclampf alpha )
541 {
542 GLfloat tmp[4];
543 GET_CURRENT_CONTEXT(ctx);
544
545 tmp[0] = red;
546 tmp[1] = green;
547 tmp[2] = blue;
548 tmp[3] = alpha;
549
550 if (TEST_EQ_4V(tmp, ctx->Color.BlendColorUnclamped))
551 return;
552
553 FLUSH_VERTICES(ctx, _NEW_COLOR);
554 COPY_4FV( ctx->Color.BlendColorUnclamped, tmp );
555
556 ctx->Color.BlendColor[0] = CLAMP(tmp[0], 0.0F, 1.0F);
557 ctx->Color.BlendColor[1] = CLAMP(tmp[1], 0.0F, 1.0F);
558 ctx->Color.BlendColor[2] = CLAMP(tmp[2], 0.0F, 1.0F);
559 ctx->Color.BlendColor[3] = CLAMP(tmp[3], 0.0F, 1.0F);
560
561 if (ctx->Driver.BlendColor)
562 (*ctx->Driver.BlendColor)(ctx, ctx->Color.BlendColor);
563 }
564
565
566 /**
567 * Specify the alpha test function.
568 *
569 * \param func alpha comparison function.
570 * \param ref reference value.
571 *
572 * Verifies the parameters and updates gl_colorbuffer_attrib.
573 * On a change, flushes the vertices and notifies the driver via
574 * dd_function_table::AlphaFunc callback.
575 */
576 void GLAPIENTRY
577 _mesa_AlphaFunc( GLenum func, GLclampf ref )
578 {
579 GET_CURRENT_CONTEXT(ctx);
580
581 if (MESA_VERBOSE & VERBOSE_API)
582 _mesa_debug(ctx, "glAlphaFunc(%s, %f)\n",
583 _mesa_enum_to_string(func), ref);
584
585 if (ctx->Color.AlphaFunc == func && ctx->Color.AlphaRefUnclamped == ref)
586 return; /* no change */
587
588 switch (func) {
589 case GL_NEVER:
590 case GL_LESS:
591 case GL_EQUAL:
592 case GL_LEQUAL:
593 case GL_GREATER:
594 case GL_NOTEQUAL:
595 case GL_GEQUAL:
596 case GL_ALWAYS:
597 FLUSH_VERTICES(ctx, _NEW_COLOR);
598 ctx->Color.AlphaFunc = func;
599 ctx->Color.AlphaRefUnclamped = ref;
600 ctx->Color.AlphaRef = CLAMP(ref, 0.0F, 1.0F);
601
602 if (ctx->Driver.AlphaFunc)
603 ctx->Driver.AlphaFunc(ctx, func, ctx->Color.AlphaRef);
604 return;
605
606 default:
607 _mesa_error( ctx, GL_INVALID_ENUM, "glAlphaFunc(func)" );
608 return;
609 }
610 }
611
612
613 /**
614 * Specify a logic pixel operation for color index rendering.
615 *
616 * \param opcode operation.
617 *
618 * Verifies that \p opcode is a valid enum and updates
619 gl_colorbuffer_attrib::LogicOp.
620 * On a change, flushes the vertices and notifies the driver via the
621 * dd_function_table::LogicOpcode callback.
622 */
623 void GLAPIENTRY
624 _mesa_LogicOp( GLenum opcode )
625 {
626 GET_CURRENT_CONTEXT(ctx);
627
628 if (MESA_VERBOSE & VERBOSE_API)
629 _mesa_debug(ctx, "glLogicOp(%s)\n", _mesa_enum_to_string(opcode));
630
631 switch (opcode) {
632 case GL_CLEAR:
633 case GL_SET:
634 case GL_COPY:
635 case GL_COPY_INVERTED:
636 case GL_NOOP:
637 case GL_INVERT:
638 case GL_AND:
639 case GL_NAND:
640 case GL_OR:
641 case GL_NOR:
642 case GL_XOR:
643 case GL_EQUIV:
644 case GL_AND_REVERSE:
645 case GL_AND_INVERTED:
646 case GL_OR_REVERSE:
647 case GL_OR_INVERTED:
648 break;
649 default:
650 _mesa_error( ctx, GL_INVALID_ENUM, "glLogicOp" );
651 return;
652 }
653
654 if (ctx->Color.LogicOp == opcode)
655 return;
656
657 FLUSH_VERTICES(ctx, _NEW_COLOR);
658 ctx->Color.LogicOp = opcode;
659
660 if (ctx->Driver.LogicOpcode)
661 ctx->Driver.LogicOpcode( ctx, opcode );
662 }
663
664
665 void GLAPIENTRY
666 _mesa_IndexMask( GLuint mask )
667 {
668 GET_CURRENT_CONTEXT(ctx);
669
670 if (ctx->Color.IndexMask == mask)
671 return;
672
673 FLUSH_VERTICES(ctx, _NEW_COLOR);
674 ctx->Color.IndexMask = mask;
675 }
676
677
678 /**
679 * Enable or disable writing of frame buffer color components.
680 *
681 * \param red whether to mask writing of the red color component.
682 * \param green whether to mask writing of the green color component.
683 * \param blue whether to mask writing of the blue color component.
684 * \param alpha whether to mask writing of the alpha color component.
685 *
686 * \sa glColorMask().
687 *
688 * Sets the appropriate value of gl_colorbuffer_attrib::ColorMask. On a
689 * change, flushes the vertices and notifies the driver via the
690 * dd_function_table::ColorMask callback.
691 */
692 void GLAPIENTRY
693 _mesa_ColorMask( GLboolean red, GLboolean green,
694 GLboolean blue, GLboolean alpha )
695 {
696 GET_CURRENT_CONTEXT(ctx);
697 GLubyte tmp[4];
698 GLuint i;
699 GLboolean flushed;
700
701 if (MESA_VERBOSE & VERBOSE_API)
702 _mesa_debug(ctx, "glColorMask(%d, %d, %d, %d)\n",
703 red, green, blue, alpha);
704
705 /* Shouldn't have any information about channel depth in core mesa
706 * -- should probably store these as the native booleans:
707 */
708 tmp[RCOMP] = red ? 0xff : 0x0;
709 tmp[GCOMP] = green ? 0xff : 0x0;
710 tmp[BCOMP] = blue ? 0xff : 0x0;
711 tmp[ACOMP] = alpha ? 0xff : 0x0;
712
713 flushed = GL_FALSE;
714 for (i = 0; i < ctx->Const.MaxDrawBuffers; i++) {
715 if (!TEST_EQ_4V(tmp, ctx->Color.ColorMask[i])) {
716 if (!flushed) {
717 FLUSH_VERTICES(ctx, _NEW_COLOR);
718 }
719 flushed = GL_TRUE;
720 COPY_4UBV(ctx->Color.ColorMask[i], tmp);
721 }
722 }
723
724 if (ctx->Driver.ColorMask)
725 ctx->Driver.ColorMask( ctx, red, green, blue, alpha );
726 }
727
728
729 /**
730 * For GL_EXT_draw_buffers2 and GL3
731 */
732 void GLAPIENTRY
733 _mesa_ColorMaski( GLuint buf, GLboolean red, GLboolean green,
734 GLboolean blue, GLboolean alpha )
735 {
736 GLubyte tmp[4];
737 GET_CURRENT_CONTEXT(ctx);
738
739 if (MESA_VERBOSE & VERBOSE_API)
740 _mesa_debug(ctx, "glColorMaskIndexed %u %d %d %d %d\n",
741 buf, red, green, blue, alpha);
742
743 if (buf >= ctx->Const.MaxDrawBuffers) {
744 _mesa_error(ctx, GL_INVALID_VALUE, "glColorMaskIndexed(buf=%u)", buf);
745 return;
746 }
747
748 /* Shouldn't have any information about channel depth in core mesa
749 * -- should probably store these as the native booleans:
750 */
751 tmp[RCOMP] = red ? 0xff : 0x0;
752 tmp[GCOMP] = green ? 0xff : 0x0;
753 tmp[BCOMP] = blue ? 0xff : 0x0;
754 tmp[ACOMP] = alpha ? 0xff : 0x0;
755
756 if (TEST_EQ_4V(tmp, ctx->Color.ColorMask[buf]))
757 return;
758
759 FLUSH_VERTICES(ctx, _NEW_COLOR);
760 COPY_4UBV(ctx->Color.ColorMask[buf], tmp);
761 }
762
763
764 void GLAPIENTRY
765 _mesa_ClampColor(GLenum target, GLenum clamp)
766 {
767 GET_CURRENT_CONTEXT(ctx);
768
769 if (clamp != GL_TRUE && clamp != GL_FALSE && clamp != GL_FIXED_ONLY_ARB) {
770 _mesa_error(ctx, GL_INVALID_ENUM, "glClampColorARB(clamp)");
771 return;
772 }
773
774 switch (target) {
775 case GL_CLAMP_VERTEX_COLOR_ARB:
776 if (ctx->API == API_OPENGL_CORE &&
777 !ctx->Extensions.ARB_color_buffer_float) {
778 goto invalid_enum;
779 }
780 FLUSH_VERTICES(ctx, _NEW_LIGHT);
781 ctx->Light.ClampVertexColor = clamp;
782 _mesa_update_clamp_vertex_color(ctx, ctx->DrawBuffer);
783 break;
784 case GL_CLAMP_FRAGMENT_COLOR_ARB:
785 if (ctx->API == API_OPENGL_CORE &&
786 !ctx->Extensions.ARB_color_buffer_float) {
787 goto invalid_enum;
788 }
789 FLUSH_VERTICES(ctx, _NEW_FRAG_CLAMP);
790 ctx->Color.ClampFragmentColor = clamp;
791 _mesa_update_clamp_fragment_color(ctx, ctx->DrawBuffer);
792 break;
793 case GL_CLAMP_READ_COLOR_ARB:
794 ctx->Color.ClampReadColor = clamp;
795 break;
796 default:
797 goto invalid_enum;
798 }
799 return;
800
801 invalid_enum:
802 _mesa_error(ctx, GL_INVALID_ENUM, "glClampColor(%s)",
803 _mesa_enum_to_string(target));
804 }
805
806 static GLboolean
807 get_clamp_color(const struct gl_framebuffer *fb, GLenum clamp)
808 {
809 if (clamp == GL_TRUE || clamp == GL_FALSE)
810 return clamp;
811
812 assert(clamp == GL_FIXED_ONLY);
813 if (!fb)
814 return GL_TRUE;
815
816 return fb->_AllColorBuffersFixedPoint;
817 }
818
819 GLboolean
820 _mesa_get_clamp_fragment_color(const struct gl_context *ctx,
821 const struct gl_framebuffer *drawFb)
822 {
823 return get_clamp_color(drawFb, ctx->Color.ClampFragmentColor);
824 }
825
826 GLboolean
827 _mesa_get_clamp_vertex_color(const struct gl_context *ctx,
828 const struct gl_framebuffer *drawFb)
829 {
830 return get_clamp_color(drawFb, ctx->Light.ClampVertexColor);
831 }
832
833 GLboolean
834 _mesa_get_clamp_read_color(const struct gl_context *ctx,
835 const struct gl_framebuffer *readFb)
836 {
837 return get_clamp_color(readFb, ctx->Color.ClampReadColor);
838 }
839
840 /**
841 * Update the ctx->Color._ClampFragmentColor field
842 */
843 void
844 _mesa_update_clamp_fragment_color(struct gl_context *ctx,
845 const struct gl_framebuffer *drawFb)
846 {
847 /* Don't clamp if:
848 * - there is no colorbuffer
849 * - all colorbuffers are unsigned normalized, so clamping has no effect
850 * - there is an integer colorbuffer
851 */
852 if (!drawFb || !drawFb->_HasSNormOrFloatColorBuffer ||
853 drawFb->_IntegerColor)
854 ctx->Color._ClampFragmentColor = GL_FALSE;
855 else
856 ctx->Color._ClampFragmentColor =
857 _mesa_get_clamp_fragment_color(ctx, drawFb);
858 }
859
860 /**
861 * Update the ctx->Color._ClampVertexColor field
862 */
863 void
864 _mesa_update_clamp_vertex_color(struct gl_context *ctx,
865 const struct gl_framebuffer *drawFb)
866 {
867 ctx->Light._ClampVertexColor =
868 _mesa_get_clamp_vertex_color(ctx, drawFb);
869 }
870
871 /**
872 * Returns an appropriate mesa_format for color rendering based on the
873 * GL_FRAMEBUFFER_SRGB state.
874 *
875 * Some drivers implement GL_FRAMEBUFFER_SRGB using a flag on the blend state
876 * (which GL_FRAMEBUFFER_SRGB maps to reasonably), but some have to do so by
877 * overriding the format of the surface. This is a helper for doing the
878 * surface format override variant.
879 */
880 mesa_format
881 _mesa_get_render_format(const struct gl_context *ctx, mesa_format format)
882 {
883 if (ctx->Color.sRGBEnabled)
884 return format;
885 else
886 return _mesa_get_srgb_format_linear(format);
887 }
888
889 /**********************************************************************/
890 /** \name Initialization */
891 /*@{*/
892
893 /**
894 * Initialization of the context's Color attribute group.
895 *
896 * \param ctx GL context.
897 *
898 * Initializes the related fields in the context color attribute group,
899 * __struct gl_contextRec::Color.
900 */
901 void _mesa_init_color( struct gl_context * ctx )
902 {
903 GLuint i;
904
905 /* Color buffer group */
906 ctx->Color.IndexMask = ~0u;
907 memset(ctx->Color.ColorMask, 0xff, sizeof(ctx->Color.ColorMask));
908 ctx->Color.ClearIndex = 0;
909 ASSIGN_4V( ctx->Color.ClearColor.f, 0, 0, 0, 0 );
910 ctx->Color.AlphaEnabled = GL_FALSE;
911 ctx->Color.AlphaFunc = GL_ALWAYS;
912 ctx->Color.AlphaRef = 0;
913 ctx->Color.BlendEnabled = 0x0;
914 for (i = 0; i < ARRAY_SIZE(ctx->Color.Blend); i++) {
915 ctx->Color.Blend[i].SrcRGB = GL_ONE;
916 ctx->Color.Blend[i].DstRGB = GL_ZERO;
917 ctx->Color.Blend[i].SrcA = GL_ONE;
918 ctx->Color.Blend[i].DstA = GL_ZERO;
919 ctx->Color.Blend[i].EquationRGB = GL_FUNC_ADD;
920 ctx->Color.Blend[i].EquationA = GL_FUNC_ADD;
921 }
922 ASSIGN_4V( ctx->Color.BlendColor, 0.0, 0.0, 0.0, 0.0 );
923 ASSIGN_4V( ctx->Color.BlendColorUnclamped, 0.0, 0.0, 0.0, 0.0 );
924 ctx->Color.IndexLogicOpEnabled = GL_FALSE;
925 ctx->Color.ColorLogicOpEnabled = GL_FALSE;
926 ctx->Color.LogicOp = GL_COPY;
927 ctx->Color.DitherFlag = GL_TRUE;
928
929 /* GL_FRONT is not possible on GLES. Instead GL_BACK will render to either
930 * the front or the back buffer depending on the config */
931 if (ctx->Visual.doubleBufferMode || _mesa_is_gles(ctx)) {
932 ctx->Color.DrawBuffer[0] = GL_BACK;
933 }
934 else {
935 ctx->Color.DrawBuffer[0] = GL_FRONT;
936 }
937
938 ctx->Color.ClampFragmentColor = ctx->API == API_OPENGL_COMPAT ?
939 GL_FIXED_ONLY_ARB : GL_FALSE;
940 ctx->Color._ClampFragmentColor = GL_FALSE;
941 ctx->Color.ClampReadColor = GL_FIXED_ONLY_ARB;
942
943 /* GLES 1/2/3 behaves as though GL_FRAMEBUFFER_SRGB is always enabled
944 * if EGL_KHR_gl_colorspace has been used to request sRGB.
945 */
946 ctx->Color.sRGBEnabled = _mesa_is_gles(ctx);
947 }
948
949 /*@}*/