2 * Copyright © 2009 Intel Corporation
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
24 * Eric Anholt <eric@anholt.net>
28 #include "brw_context.h"
29 #include "brw_state.h"
30 #include "brw_defines.h"
32 #include "intel_batchbuffer.h"
33 #include "main/macros.h"
36 prepare_blend_state(struct brw_context *brw)
38 struct gl_context *ctx = &brw->intel.ctx;
39 struct gen6_blend_state *blend;
41 int nr_draw_buffers = ctx->DrawBuffer->_NumColorDrawBuffers;
44 /* We need at least one BLEND_STATE written, because we might do
45 * thread dispatch even if _NumColorDrawBuffers is 0 (for example
46 * for computed depth or alpha test), which will do an FB write
47 * with render target 0, which will reference BLEND_STATE[0] for
50 if (nr_draw_buffers == 0 && ctx->Color.AlphaEnabled)
53 size = sizeof(*blend) * nr_draw_buffers;
54 blend = brw_state_batch(brw, size, 64, &brw->cc.blend_state_offset);
56 memset(blend, 0, size);
58 for (b = 0; b < nr_draw_buffers; b++) {
60 if (ctx->Color._LogicOpEnabled) {
61 struct gl_renderbuffer *rb = ctx->DrawBuffer->_ColorDrawBuffers[b];
63 /* Floating point RTs should have no effect from LogicOp,
64 * except for disabling of blending
66 if (_mesa_get_format_datatype(rb->Format) != GL_FLOAT) {
67 blend[b].blend1.logic_op_enable = 1;
68 blend[b].blend1.logic_op_func =
69 intel_translate_logic_op(ctx->Color.LogicOp);
71 } else if (ctx->Color.BlendEnabled & (1 << b)) {
72 GLenum eqRGB = ctx->Color.Blend[0].EquationRGB;
73 GLenum eqA = ctx->Color.Blend[0].EquationA;
74 GLenum srcRGB = ctx->Color.Blend[0].SrcRGB;
75 GLenum dstRGB = ctx->Color.Blend[0].DstRGB;
76 GLenum srcA = ctx->Color.Blend[0].SrcA;
77 GLenum dstA = ctx->Color.Blend[0].DstA;
79 if (eqRGB == GL_MIN || eqRGB == GL_MAX) {
80 srcRGB = dstRGB = GL_ONE;
83 if (eqA == GL_MIN || eqA == GL_MAX) {
87 blend[b].blend0.dest_blend_factor = brw_translate_blend_factor(dstRGB);
88 blend[b].blend0.source_blend_factor = brw_translate_blend_factor(srcRGB);
89 blend[b].blend0.blend_func = brw_translate_blend_equation(eqRGB);
91 blend[b].blend0.ia_dest_blend_factor = brw_translate_blend_factor(dstA);
92 blend[b].blend0.ia_source_blend_factor = brw_translate_blend_factor(srcA);
93 blend[b].blend0.ia_blend_func = brw_translate_blend_equation(eqA);
95 blend[b].blend0.blend_enable = 1;
96 blend[b].blend0.ia_blend_enable = (srcA != srcRGB ||
103 if (ctx->Color.AlphaEnabled) {
104 blend[b].blend1.alpha_test_enable = 1;
105 blend[b].blend1.alpha_test_func =
106 intel_translate_compare_func(ctx->Color.AlphaFunc);
111 if (ctx->Color.DitherFlag) {
112 blend[b].blend1.dither_enable = 1;
113 blend[b].blend1.y_dither_offset = 0;
114 blend[b].blend1.x_dither_offset = 0;
117 blend[b].blend1.write_disable_r = !ctx->Color.ColorMask[b][0];
118 blend[b].blend1.write_disable_g = !ctx->Color.ColorMask[b][1];
119 blend[b].blend1.write_disable_b = !ctx->Color.ColorMask[b][2];
120 blend[b].blend1.write_disable_a = !ctx->Color.ColorMask[b][3];
123 brw->state.dirty.cache |= CACHE_NEW_BLEND_STATE;
126 const struct brw_tracked_state gen6_blend_state = {
128 .mesa = (_NEW_COLOR |
130 .brw = BRW_NEW_BATCH,
133 .prepare = prepare_blend_state,
137 gen6_prepare_color_calc_state(struct brw_context *brw)
139 struct gl_context *ctx = &brw->intel.ctx;
140 struct gen6_color_calc_state *cc;
142 cc = brw_state_batch(brw, sizeof(*cc), 64, &brw->cc.state_offset);
143 memset(cc, 0, sizeof(*cc));
146 cc->cc0.alpha_test_format = BRW_ALPHATEST_FORMAT_UNORM8;
147 UNCLAMPED_FLOAT_TO_UBYTE(cc->cc1.alpha_ref_fi.ui, ctx->Color.AlphaRef);
150 cc->cc0.stencil_ref = ctx->Stencil.Ref[0];
151 cc->cc0.bf_stencil_ref = ctx->Stencil.Ref[ctx->Stencil._BackFace];
154 cc->constant_r = ctx->Color.BlendColorUnclamped[0];
155 cc->constant_g = ctx->Color.BlendColorUnclamped[1];
156 cc->constant_b = ctx->Color.BlendColorUnclamped[2];
157 cc->constant_a = ctx->Color.BlendColorUnclamped[3];
159 brw->state.dirty.cache |= CACHE_NEW_COLOR_CALC_STATE;
162 const struct brw_tracked_state gen6_color_calc_state = {
164 .mesa = _NEW_COLOR | _NEW_STENCIL,
165 .brw = BRW_NEW_BATCH,
168 .prepare = gen6_prepare_color_calc_state,
171 static void upload_cc_state_pointers(struct brw_context *brw)
173 struct intel_context *intel = &brw->intel;
176 OUT_BATCH(_3DSTATE_CC_STATE_POINTERS << 16 | (4 - 2));
177 OUT_BATCH(brw->cc.blend_state_offset | 1);
178 OUT_BATCH(brw->cc.depth_stencil_state_offset | 1);
179 OUT_BATCH(brw->cc.state_offset | 1);
183 const struct brw_tracked_state gen6_cc_state_pointers = {
186 .brw = (BRW_NEW_BATCH |
187 BRW_NEW_STATE_BASE_ADDRESS),
188 .cache = (CACHE_NEW_BLEND_STATE |
189 CACHE_NEW_COLOR_CALC_STATE |
190 CACHE_NEW_DEPTH_STENCIL_STATE)
192 .emit = upload_cc_state_pointers,