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