blob: f76cfab943795554ccc833dc971e6b9269e5b242 [file] [log] [blame]
Brian6debc802007-05-24 16:50:22 -06001/**************************************************************************
2 *
José Fonseca87712852014-01-17 16:27:50 +00003 * Copyright 2007 VMware, Inc.
Brian6debc802007-05-24 16:50:22 -06004 * All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
José Fonseca87712852014-01-17 16:27:50 +000021 * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
Brian6debc802007-05-24 16:50:22 -060022 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 *
26 **************************************************************************/
27
28 /*
29 * Authors:
José Fonseca87712852014-01-17 16:27:50 +000030 * Keith Whitwell <keithw@vmware.com>
Brian6debc802007-05-24 16:50:22 -060031 * Brian Paul
32 */
33
34
35#include "st_context.h"
36#include "st_atom.h"
Zack Rusine16c0452007-09-17 07:56:56 -040037
Keith Whitwell943964a2007-06-14 18:23:43 +010038#include "pipe/p_context.h"
39#include "pipe/p_defines.h"
Brian339e7ec2008-03-11 18:54:31 -060040#include "cso_cache/cso_context.h"
Brian6debc802007-05-24 16:50:22 -060041
Brian7d95efd2008-03-19 11:12:48 -060042#include "main/macros.h"
Brian6debc802007-05-24 16:50:22 -060043
44/**
Keith Whitwell943964a2007-06-14 18:23:43 +010045 * Convert GLenum blend tokens to pipe tokens.
Brian6debc802007-05-24 16:50:22 -060046 * Both blend factors and blend funcs are accepted.
47 */
48static GLuint
Briance0f2e82007-10-16 08:53:08 -060049translate_blend(GLenum blend)
Brian6debc802007-05-24 16:50:22 -060050{
51 switch (blend) {
52 /* blend functions */
53 case GL_FUNC_ADD:
Keith Whitwell943964a2007-06-14 18:23:43 +010054 return PIPE_BLEND_ADD;
Brian6debc802007-05-24 16:50:22 -060055 case GL_FUNC_SUBTRACT:
Keith Whitwell943964a2007-06-14 18:23:43 +010056 return PIPE_BLEND_SUBTRACT;
Brian6debc802007-05-24 16:50:22 -060057 case GL_FUNC_REVERSE_SUBTRACT:
Keith Whitwell943964a2007-06-14 18:23:43 +010058 return PIPE_BLEND_REVERSE_SUBTRACT;
Brian6debc802007-05-24 16:50:22 -060059 case GL_MIN:
Keith Whitwell943964a2007-06-14 18:23:43 +010060 return PIPE_BLEND_MIN;
Brian6debc802007-05-24 16:50:22 -060061 case GL_MAX:
Keith Whitwell943964a2007-06-14 18:23:43 +010062 return PIPE_BLEND_MAX;
Brian6debc802007-05-24 16:50:22 -060063
64 /* blend factors */
65 case GL_ONE:
Keith Whitwell943964a2007-06-14 18:23:43 +010066 return PIPE_BLENDFACTOR_ONE;
Brian6debc802007-05-24 16:50:22 -060067 case GL_SRC_COLOR:
Keith Whitwell943964a2007-06-14 18:23:43 +010068 return PIPE_BLENDFACTOR_SRC_COLOR;
Brian6debc802007-05-24 16:50:22 -060069 case GL_SRC_ALPHA:
Keith Whitwell943964a2007-06-14 18:23:43 +010070 return PIPE_BLENDFACTOR_SRC_ALPHA;
Brian6debc802007-05-24 16:50:22 -060071 case GL_DST_ALPHA:
Keith Whitwell943964a2007-06-14 18:23:43 +010072 return PIPE_BLENDFACTOR_DST_ALPHA;
Brian6debc802007-05-24 16:50:22 -060073 case GL_DST_COLOR:
Keith Whitwell943964a2007-06-14 18:23:43 +010074 return PIPE_BLENDFACTOR_DST_COLOR;
Brian6debc802007-05-24 16:50:22 -060075 case GL_SRC_ALPHA_SATURATE:
Keith Whitwell943964a2007-06-14 18:23:43 +010076 return PIPE_BLENDFACTOR_SRC_ALPHA_SATURATE;
Brian6debc802007-05-24 16:50:22 -060077 case GL_CONSTANT_COLOR:
Keith Whitwell943964a2007-06-14 18:23:43 +010078 return PIPE_BLENDFACTOR_CONST_COLOR;
Brian6debc802007-05-24 16:50:22 -060079 case GL_CONSTANT_ALPHA:
Keith Whitwell943964a2007-06-14 18:23:43 +010080 return PIPE_BLENDFACTOR_CONST_ALPHA;
Dave Airliea21df962012-03-24 13:36:17 +000081 case GL_SRC1_COLOR:
Keith Whitwell943964a2007-06-14 18:23:43 +010082 return PIPE_BLENDFACTOR_SRC1_COLOR;
Dave Airliea21df962012-03-24 13:36:17 +000083 case GL_SRC1_ALPHA:
Keith Whitwell943964a2007-06-14 18:23:43 +010084 return PIPE_BLENDFACTOR_SRC1_ALPHA;
Brian6debc802007-05-24 16:50:22 -060085 case GL_ZERO:
Keith Whitwell943964a2007-06-14 18:23:43 +010086 return PIPE_BLENDFACTOR_ZERO;
Brian6debc802007-05-24 16:50:22 -060087 case GL_ONE_MINUS_SRC_COLOR:
Keith Whitwell943964a2007-06-14 18:23:43 +010088 return PIPE_BLENDFACTOR_INV_SRC_COLOR;
Brian6debc802007-05-24 16:50:22 -060089 case GL_ONE_MINUS_SRC_ALPHA:
Keith Whitwell943964a2007-06-14 18:23:43 +010090 return PIPE_BLENDFACTOR_INV_SRC_ALPHA;
Brian6debc802007-05-24 16:50:22 -060091 case GL_ONE_MINUS_DST_COLOR:
Keith Whitwell943964a2007-06-14 18:23:43 +010092 return PIPE_BLENDFACTOR_INV_DST_COLOR;
Briance0f2e82007-10-16 08:53:08 -060093 case GL_ONE_MINUS_DST_ALPHA:
94 return PIPE_BLENDFACTOR_INV_DST_ALPHA;
Brian6debc802007-05-24 16:50:22 -060095 case GL_ONE_MINUS_CONSTANT_COLOR:
Keith Whitwell943964a2007-06-14 18:23:43 +010096 return PIPE_BLENDFACTOR_INV_CONST_COLOR;
Brian6debc802007-05-24 16:50:22 -060097 case GL_ONE_MINUS_CONSTANT_ALPHA:
Keith Whitwell943964a2007-06-14 18:23:43 +010098 return PIPE_BLENDFACTOR_INV_CONST_ALPHA;
Dave Airliea21df962012-03-24 13:36:17 +000099 case GL_ONE_MINUS_SRC1_COLOR:
Keith Whitwell943964a2007-06-14 18:23:43 +0100100 return PIPE_BLENDFACTOR_INV_SRC1_COLOR;
Dave Airliea21df962012-03-24 13:36:17 +0000101 case GL_ONE_MINUS_SRC1_ALPHA:
Keith Whitwell943964a2007-06-14 18:23:43 +0100102 return PIPE_BLENDFACTOR_INV_SRC1_ALPHA;
Brian6debc802007-05-24 16:50:22 -0600103 default:
Briance0f2e82007-10-16 08:53:08 -0600104 assert("invalid GL token in translate_blend()" == NULL);
Brian6debc802007-05-24 16:50:22 -0600105 return 0;
106 }
107}
108
109
110/**
Keith Whitwell943964a2007-06-14 18:23:43 +0100111 * Convert GLenum logicop tokens to pipe tokens.
Brian6debc802007-05-24 16:50:22 -0600112 */
113static GLuint
Briance0f2e82007-10-16 08:53:08 -0600114translate_logicop(GLenum logicop)
Brian6debc802007-05-24 16:50:22 -0600115{
116 switch (logicop) {
117 case GL_CLEAR:
Keith Whitwell943964a2007-06-14 18:23:43 +0100118 return PIPE_LOGICOP_CLEAR;
Brian6debc802007-05-24 16:50:22 -0600119 case GL_NOR:
Keith Whitwell943964a2007-06-14 18:23:43 +0100120 return PIPE_LOGICOP_NOR;
Brian6debc802007-05-24 16:50:22 -0600121 case GL_AND_INVERTED:
Keith Whitwell943964a2007-06-14 18:23:43 +0100122 return PIPE_LOGICOP_AND_INVERTED;
Brian6debc802007-05-24 16:50:22 -0600123 case GL_COPY_INVERTED:
Keith Whitwell943964a2007-06-14 18:23:43 +0100124 return PIPE_LOGICOP_COPY_INVERTED;
Brian6debc802007-05-24 16:50:22 -0600125 case GL_AND_REVERSE:
Keith Whitwell943964a2007-06-14 18:23:43 +0100126 return PIPE_LOGICOP_AND_REVERSE;
Brian6debc802007-05-24 16:50:22 -0600127 case GL_INVERT:
Keith Whitwell943964a2007-06-14 18:23:43 +0100128 return PIPE_LOGICOP_INVERT;
Brian6debc802007-05-24 16:50:22 -0600129 case GL_XOR:
Keith Whitwell943964a2007-06-14 18:23:43 +0100130 return PIPE_LOGICOP_XOR;
Brian6debc802007-05-24 16:50:22 -0600131 case GL_NAND:
Keith Whitwell943964a2007-06-14 18:23:43 +0100132 return PIPE_LOGICOP_NAND;
Brian6debc802007-05-24 16:50:22 -0600133 case GL_AND:
Keith Whitwell943964a2007-06-14 18:23:43 +0100134 return PIPE_LOGICOP_AND;
Brian6debc802007-05-24 16:50:22 -0600135 case GL_EQUIV:
Keith Whitwell943964a2007-06-14 18:23:43 +0100136 return PIPE_LOGICOP_EQUIV;
Brian6debc802007-05-24 16:50:22 -0600137 case GL_NOOP:
Keith Whitwell943964a2007-06-14 18:23:43 +0100138 return PIPE_LOGICOP_NOOP;
Brian6debc802007-05-24 16:50:22 -0600139 case GL_OR_INVERTED:
Keith Whitwell943964a2007-06-14 18:23:43 +0100140 return PIPE_LOGICOP_OR_INVERTED;
Brian6debc802007-05-24 16:50:22 -0600141 case GL_COPY:
Keith Whitwell943964a2007-06-14 18:23:43 +0100142 return PIPE_LOGICOP_COPY;
Brian6debc802007-05-24 16:50:22 -0600143 case GL_OR_REVERSE:
Keith Whitwell943964a2007-06-14 18:23:43 +0100144 return PIPE_LOGICOP_OR_REVERSE;
Brian6debc802007-05-24 16:50:22 -0600145 case GL_OR:
Keith Whitwell943964a2007-06-14 18:23:43 +0100146 return PIPE_LOGICOP_OR;
Brian6debc802007-05-24 16:50:22 -0600147 case GL_SET:
Keith Whitwell943964a2007-06-14 18:23:43 +0100148 return PIPE_LOGICOP_SET;
Brian6debc802007-05-24 16:50:22 -0600149 default:
Briance0f2e82007-10-16 08:53:08 -0600150 assert("invalid GL token in translate_logicop()" == NULL);
Brian6debc802007-05-24 16:50:22 -0600151 return 0;
152 }
153}
154
Roland Scheidegger9bc80ff2010-01-26 15:35:31 +0100155/**
156 * Figure out if colormasks are different per rt.
157 */
158static GLboolean
Brian Paul99feecc2011-06-14 09:15:36 -0600159colormask_per_rt(const struct gl_context *ctx)
Roland Scheidegger5fae3612010-01-25 19:27:05 +0100160{
Roland Scheidegger9bc80ff2010-01-26 15:35:31 +0100161 /* a bit suboptimal have to compare lots of values */
Roland Scheidegger5fae3612010-01-25 19:27:05 +0100162 unsigned i;
163 for (i = 1; i < ctx->Const.MaxDrawBuffers; i++) {
Roland Scheidegger9bc80ff2010-01-26 15:35:31 +0100164 if (memcmp(ctx->Color.ColorMask[0], ctx->Color.ColorMask[i], 4)) {
165 return GL_TRUE;
Roland Scheidegger5fae3612010-01-25 19:27:05 +0100166 }
167 }
Roland Scheidegger9bc80ff2010-01-26 15:35:31 +0100168 return GL_FALSE;
169}
170
171/**
Brian Paul74713e22011-01-11 15:07:38 -0700172 * Figure out if blend enables/state are different per rt.
Roland Scheidegger9bc80ff2010-01-26 15:35:31 +0100173 */
174static GLboolean
Brian Paul99feecc2011-06-14 09:15:36 -0600175blend_per_rt(const struct gl_context *ctx)
Roland Scheidegger9bc80ff2010-01-26 15:35:31 +0100176{
177 if (ctx->Color.BlendEnabled &&
Jan Vesely3cb10cc2015-01-15 13:41:04 -0500178 (ctx->Color.BlendEnabled != ((1U << ctx->Const.MaxDrawBuffers) - 1))) {
Brian Paul74713e22011-01-11 15:07:38 -0700179 /* This can only happen if GL_EXT_draw_buffers2 is enabled */
180 return GL_TRUE;
181 }
182 if (ctx->Color._BlendFuncPerBuffer || ctx->Color._BlendEquationPerBuffer) {
183 /* this can only happen if GL_ARB_draw_buffers_blend is enabled */
Roland Scheidegger9bc80ff2010-01-26 15:35:31 +0100184 return GL_TRUE;
185 }
186 return GL_FALSE;
Roland Scheidegger5fae3612010-01-25 19:27:05 +0100187}
Brian6debc802007-05-24 16:50:22 -0600188
189static void
190update_blend( struct st_context *st )
191{
Brian339e7ec2008-03-11 18:54:31 -0600192 struct pipe_blend_state *blend = &st->state.blend;
Brian Paul99feecc2011-06-14 09:15:36 -0600193 const struct gl_context *ctx = st->ctx;
Roland Scheidegger5fae3612010-01-25 19:27:05 +0100194 unsigned num_state = 1;
Fabian Bielera476ca12011-02-10 16:45:41 +0100195 unsigned i, j;
Brian6debc802007-05-24 16:50:22 -0600196
Brian339e7ec2008-03-11 18:54:31 -0600197 memset(blend, 0, sizeof(*blend));
Brian6debc802007-05-24 16:50:22 -0600198
Brian Paul99feecc2011-06-14 09:15:36 -0600199 if (blend_per_rt(ctx) || colormask_per_rt(ctx)) {
200 num_state = ctx->Const.MaxDrawBuffers;
Roland Scheidegger5fae3612010-01-25 19:27:05 +0100201 blend->independent_blend_enable = 1;
202 }
Ian Romanick3538bff2011-09-09 14:07:54 -0700203 if (ctx->Color.ColorLogicOpEnabled) {
Brian6debc802007-05-24 16:50:22 -0600204 /* logicop enabled */
Brian339e7ec2008-03-11 18:54:31 -0600205 blend->logicop_enable = 1;
Brian Paul99feecc2011-06-14 09:15:36 -0600206 blend->logicop_func = translate_logicop(ctx->Color.LogicOp);
Brian6debc802007-05-24 16:50:22 -0600207 }
Ilia Mirkin6b7511c2017-01-01 23:47:25 -0500208 else if (ctx->Color.BlendEnabled && !ctx->Color._AdvancedBlendMode) {
Brian6debc802007-05-24 16:50:22 -0600209 /* blending enabled */
Fabian Bielera476ca12011-02-10 16:45:41 +0100210 for (i = 0, j = 0; i < num_state; i++) {
Brian6debc802007-05-24 16:50:22 -0600211
Brian Paul99feecc2011-06-14 09:15:36 -0600212 blend->rt[i].blend_enable = (ctx->Color.BlendEnabled >> i) & 0x1;
Brian6debc802007-05-24 16:50:22 -0600213
Brian Paul99feecc2011-06-14 09:15:36 -0600214 if (ctx->Extensions.ARB_draw_buffers_blend)
Fabian Bielera476ca12011-02-10 16:45:41 +0100215 j = i;
216
Brian Paul74713e22011-01-11 15:07:38 -0700217 blend->rt[i].rgb_func =
Brian Paul99feecc2011-06-14 09:15:36 -0600218 translate_blend(ctx->Color.Blend[j].EquationRGB);
Brian Paul74713e22011-01-11 15:07:38 -0700219
Brian Paul99feecc2011-06-14 09:15:36 -0600220 if (ctx->Color.Blend[i].EquationRGB == GL_MIN ||
221 ctx->Color.Blend[i].EquationRGB == GL_MAX) {
Roland Scheidegger5fae3612010-01-25 19:27:05 +0100222 /* Min/max are special */
223 blend->rt[i].rgb_src_factor = PIPE_BLENDFACTOR_ONE;
224 blend->rt[i].rgb_dst_factor = PIPE_BLENDFACTOR_ONE;
225 }
226 else {
Brian Paul74713e22011-01-11 15:07:38 -0700227 blend->rt[i].rgb_src_factor =
Brian Paul99feecc2011-06-14 09:15:36 -0600228 translate_blend(ctx->Color.Blend[j].SrcRGB);
Brian Paul74713e22011-01-11 15:07:38 -0700229 blend->rt[i].rgb_dst_factor =
Brian Paul99feecc2011-06-14 09:15:36 -0600230 translate_blend(ctx->Color.Blend[j].DstRGB);
Roland Scheidegger5fae3612010-01-25 19:27:05 +0100231 }
232
Brian Paul74713e22011-01-11 15:07:38 -0700233 blend->rt[i].alpha_func =
Brian Paul99feecc2011-06-14 09:15:36 -0600234 translate_blend(ctx->Color.Blend[j].EquationA);
Brian Paul74713e22011-01-11 15:07:38 -0700235
Brian Paul99feecc2011-06-14 09:15:36 -0600236 if (ctx->Color.Blend[i].EquationA == GL_MIN ||
237 ctx->Color.Blend[i].EquationA == GL_MAX) {
Roland Scheidegger5fae3612010-01-25 19:27:05 +0100238 /* Min/max are special */
239 blend->rt[i].alpha_src_factor = PIPE_BLENDFACTOR_ONE;
240 blend->rt[i].alpha_dst_factor = PIPE_BLENDFACTOR_ONE;
241 }
242 else {
Brian Paul74713e22011-01-11 15:07:38 -0700243 blend->rt[i].alpha_src_factor =
Brian Paul99feecc2011-06-14 09:15:36 -0600244 translate_blend(ctx->Color.Blend[j].SrcA);
Brian Paul74713e22011-01-11 15:07:38 -0700245 blend->rt[i].alpha_dst_factor =
Brian Paul99feecc2011-06-14 09:15:36 -0600246 translate_blend(ctx->Color.Blend[j].DstA);
Roland Scheidegger5fae3612010-01-25 19:27:05 +0100247 }
Brian11188072007-07-03 17:13:04 -0600248 }
Brian6debc802007-05-24 16:50:22 -0600249 }
250 else {
251 /* no blending / logicop */
252 }
253
Brian86352ff2007-07-12 12:20:14 -0600254 /* Colormask - maybe reverse these bits? */
Roland Scheidegger5fae3612010-01-25 19:27:05 +0100255 for (i = 0; i < num_state; i++) {
Brian Paul99feecc2011-06-14 09:15:36 -0600256 if (ctx->Color.ColorMask[i][0])
Roland Scheidegger5fae3612010-01-25 19:27:05 +0100257 blend->rt[i].colormask |= PIPE_MASK_R;
Brian Paul99feecc2011-06-14 09:15:36 -0600258 if (ctx->Color.ColorMask[i][1])
Roland Scheidegger5fae3612010-01-25 19:27:05 +0100259 blend->rt[i].colormask |= PIPE_MASK_G;
Brian Paul99feecc2011-06-14 09:15:36 -0600260 if (ctx->Color.ColorMask[i][2])
Roland Scheidegger5fae3612010-01-25 19:27:05 +0100261 blend->rt[i].colormask |= PIPE_MASK_B;
Brian Paul99feecc2011-06-14 09:15:36 -0600262 if (ctx->Color.ColorMask[i][3])
Roland Scheidegger5fae3612010-01-25 19:27:05 +0100263 blend->rt[i].colormask |= PIPE_MASK_A;
264 }
Brian86352ff2007-07-12 12:20:14 -0600265
Brian Paul971122a2014-07-03 12:02:16 -0600266 blend->dither = ctx->Color.DitherFlag;
Brian86352ff2007-07-12 12:20:14 -0600267
Brian Paula01872f2016-09-15 15:13:07 -0600268 if (ctx->Multisample.Enabled &&
Marek Olšák54f8efe2016-09-16 22:39:15 +0200269 ctx->DrawBuffer->Visual.sampleBuffers > 0 &&
270 !(ctx->DrawBuffer->_IntegerBuffers & 0x1)) {
Brian Paula01872f2016-09-15 15:13:07 -0600271 /* Unlike in gallium/d3d10 these operations are only performed
272 * if both msaa is enabled and we have a multisample buffer.
273 */
Brian Paul971122a2014-07-03 12:02:16 -0600274 blend->alpha_to_coverage = ctx->Multisample.SampleAlphaToCoverage;
275 blend->alpha_to_one = ctx->Multisample.SampleAlphaToOne;
Roland Scheidegger127328b2010-05-17 21:19:03 +0200276 }
277
Brian339e7ec2008-03-11 18:54:31 -0600278 cso_set_blend(st->cso_context, blend);
Brian284efcf2007-07-03 17:19:30 -0600279
Brian7d95efd2008-03-19 11:12:48 -0600280 {
281 struct pipe_blend_color bc;
Brian Paul99feecc2011-06-14 09:15:36 -0600282 COPY_4FV(bc.color, ctx->Color.BlendColorUnclamped);
Brian7d95efd2008-03-19 11:12:48 -0600283 cso_set_blend_color(st->cso_context, &bc);
Brian284efcf2007-07-03 17:19:30 -0600284 }
Brian6debc802007-05-24 16:50:22 -0600285}
286
287
288const struct st_tracked_state st_update_blend = {
Alan Hourihane54507122008-05-02 10:08:03 +0000289 update_blend, /* update */
Brian6debc802007-05-24 16:50:22 -0600290};