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"
34 #include "main/enums.h"
35 #include "main/glformats.h"
36 #include "main/stencil.h"
39 gen6_upload_blend_state(struct brw_context
*brw
)
41 bool is_buffer_zero_integer_format
= false;
42 struct gl_context
*ctx
= &brw
->ctx
;
43 struct gen6_blend_state
*blend
;
45 int nr_draw_buffers
= ctx
->DrawBuffer
->_NumColorDrawBuffers
;
48 /* We need at least one BLEND_STATE written, because we might do
49 * thread dispatch even if _NumColorDrawBuffers is 0 (for example
50 * for computed depth or alpha test), which will do an FB write
51 * with render target 0, which will reference BLEND_STATE[0] for
54 if (nr_draw_buffers
== 0)
57 size
= sizeof(*blend
) * nr_draw_buffers
;
58 blend
= brw_state_batch(brw
, AUB_TRACE_BLEND_STATE
,
59 size
, 64, &brw
->cc
.blend_state_offset
);
61 memset(blend
, 0, size
);
63 for (b
= 0; b
< nr_draw_buffers
; b
++) {
65 struct gl_renderbuffer
*rb
= ctx
->DrawBuffer
->_ColorDrawBuffers
[b
];
70 rb_type
= _mesa_get_format_datatype(rb
->Format
);
72 rb_type
= GL_UNSIGNED_NORMALIZED
;
74 /* Used for implementing the following bit of GL_EXT_texture_integer:
75 * "Per-fragment operations that require floating-point color
76 * components, including multisample alpha operations, alpha test,
77 * blending, and dithering, have no effect when the corresponding
78 * colors are written to an integer color buffer."
80 integer
= (rb_type
== GL_INT
|| rb_type
== GL_UNSIGNED_INT
);
83 is_buffer_zero_integer_format
= true;
86 if (ctx
->Color
.ColorLogicOpEnabled
) {
87 /* Floating point RTs should have no effect from LogicOp,
88 * except for disabling of blending, but other types should.
90 * However, from the Sandy Bridge PRM, Vol 2 Par 1, Section 8.1.11,
93 * "Logic Ops are only supported on *_UNORM surfaces (excluding
94 * _SRGB variants), otherwise Logic Ops must be DISABLED."
96 WARN_ONCE(ctx
->Color
.LogicOp
!= GL_COPY
&&
97 rb_type
!= GL_UNSIGNED_NORMALIZED
&&
98 rb_type
!= GL_FLOAT
, "Ignoring %s logic op on %s "
100 _mesa_enum_to_string(ctx
->Color
.LogicOp
),
101 _mesa_enum_to_string(rb_type
));
102 if (rb_type
== GL_UNSIGNED_NORMALIZED
) {
103 blend
[b
].blend1
.logic_op_enable
= 1;
104 blend
[b
].blend1
.logic_op_func
=
105 intel_translate_logic_op(ctx
->Color
.LogicOp
);
107 } else if (ctx
->Color
.BlendEnabled
& (1 << b
) && !integer
&&
108 !ctx
->Color
._AdvancedBlendMode
) {
109 GLenum eqRGB
= ctx
->Color
.Blend
[b
].EquationRGB
;
110 GLenum eqA
= ctx
->Color
.Blend
[b
].EquationA
;
111 GLenum srcRGB
= ctx
->Color
.Blend
[b
].SrcRGB
;
112 GLenum dstRGB
= ctx
->Color
.Blend
[b
].DstRGB
;
113 GLenum srcA
= ctx
->Color
.Blend
[b
].SrcA
;
114 GLenum dstA
= ctx
->Color
.Blend
[b
].DstA
;
116 if (eqRGB
== GL_MIN
|| eqRGB
== GL_MAX
) {
117 srcRGB
= dstRGB
= GL_ONE
;
120 if (eqA
== GL_MIN
|| eqA
== GL_MAX
) {
121 srcA
= dstA
= GL_ONE
;
124 /* Due to hardware limitations, the destination may have information
125 * in an alpha channel even when the format specifies no alpha
126 * channel. In order to avoid getting any incorrect blending due to
127 * that alpha channel, coerce the blend factors to values that will
128 * not read the alpha channel, but will instead use the correct
129 * implicit value for alpha.
131 if (rb
&& !_mesa_base_format_has_channel(rb
->_BaseFormat
, GL_TEXTURE_ALPHA_TYPE
))
133 srcRGB
= brw_fix_xRGB_alpha(srcRGB
);
134 srcA
= brw_fix_xRGB_alpha(srcA
);
135 dstRGB
= brw_fix_xRGB_alpha(dstRGB
);
136 dstA
= brw_fix_xRGB_alpha(dstA
);
139 blend
[b
].blend0
.dest_blend_factor
= brw_translate_blend_factor(dstRGB
);
140 blend
[b
].blend0
.source_blend_factor
= brw_translate_blend_factor(srcRGB
);
141 blend
[b
].blend0
.blend_func
= brw_translate_blend_equation(eqRGB
);
143 blend
[b
].blend0
.ia_dest_blend_factor
= brw_translate_blend_factor(dstA
);
144 blend
[b
].blend0
.ia_source_blend_factor
= brw_translate_blend_factor(srcA
);
145 blend
[b
].blend0
.ia_blend_func
= brw_translate_blend_equation(eqA
);
147 blend
[b
].blend0
.blend_enable
= 1;
148 blend
[b
].blend0
.ia_blend_enable
= (srcA
!= srcRGB
||
153 /* See section 8.1.6 "Pre-Blend Color Clamping" of the
154 * SandyBridge PRM Volume 2 Part 1 for HW requirements.
156 * We do our ARB_color_buffer_float CLAMP_FRAGMENT_COLOR
157 * clamping in the fragment shader. For its clamping of
158 * blending, the spec says:
160 * "RESOLVED: For fixed-point color buffers, the inputs and
161 * the result of the blending equation are clamped. For
162 * floating-point color buffers, no clamping occurs."
164 * So, generally, we want clamping to the render target's range.
165 * And, good news, the hardware tables for both pre- and
166 * post-blend color clamping are either ignored, or any are
167 * allowed, or clamping is required but RT range clamping is a
170 blend
[b
].blend1
.pre_blend_clamp_enable
= 1;
171 blend
[b
].blend1
.post_blend_clamp_enable
= 1;
172 blend
[b
].blend1
.clamp_range
= BRW_RENDERTARGET_CLAMPRANGE_FORMAT
;
175 if (ctx
->Color
.AlphaEnabled
&& !integer
) {
176 blend
[b
].blend1
.alpha_test_enable
= 1;
177 blend
[b
].blend1
.alpha_test_func
=
178 intel_translate_compare_func(ctx
->Color
.AlphaFunc
);
183 if (ctx
->Color
.DitherFlag
&& !integer
) {
184 blend
[b
].blend1
.dither_enable
= 1;
185 blend
[b
].blend1
.y_dither_offset
= 0;
186 blend
[b
].blend1
.x_dither_offset
= 0;
189 blend
[b
].blend1
.write_disable_r
= !ctx
->Color
.ColorMask
[b
][0];
190 blend
[b
].blend1
.write_disable_g
= !ctx
->Color
.ColorMask
[b
][1];
191 blend
[b
].blend1
.write_disable_b
= !ctx
->Color
.ColorMask
[b
][2];
192 blend
[b
].blend1
.write_disable_a
= !ctx
->Color
.ColorMask
[b
][3];
194 /* OpenGL specification 3.3 (page 196), section 4.1.3 says:
195 * "If drawbuffer zero is not NONE and the buffer it references has an
196 * integer format, the SAMPLE_ALPHA_TO_COVERAGE and SAMPLE_ALPHA_TO_ONE
197 * operations are skipped."
199 if(!is_buffer_zero_integer_format
) {
200 /* _NEW_MULTISAMPLE */
201 blend
[b
].blend1
.alpha_to_coverage
=
202 _mesa_is_multisample_enabled(ctx
) && ctx
->Multisample
.SampleAlphaToCoverage
;
204 /* From SandyBridge PRM, volume 2 Part 1, section 8.2.3, BLEND_STATE:
205 * DWord 1, Bit 30 (AlphaToOne Enable):
206 * "If Dual Source Blending is enabled, this bit must be disabled"
208 WARN_ONCE(ctx
->Color
.Blend
[b
]._UsesDualSrc
&&
209 _mesa_is_multisample_enabled(ctx
) &&
210 ctx
->Multisample
.SampleAlphaToOne
,
211 "HW workaround: disabling alpha to one with dual src "
213 if (ctx
->Color
.Blend
[b
]._UsesDualSrc
)
214 blend
[b
].blend1
.alpha_to_one
= false;
216 blend
[b
].blend1
.alpha_to_one
=
217 _mesa_is_multisample_enabled(ctx
) && ctx
->Multisample
.SampleAlphaToOne
;
219 blend
[b
].blend1
.alpha_to_coverage_dither
= (brw
->gen
>= 7);
222 blend
[b
].blend1
.alpha_to_coverage
= false;
223 blend
[b
].blend1
.alpha_to_one
= false;
227 /* Point the GPU at the new indirect state. */
230 OUT_BATCH(_3DSTATE_CC_STATE_POINTERS
<< 16 | (4 - 2));
231 OUT_BATCH(brw
->cc
.blend_state_offset
| 1);
237 OUT_BATCH(_3DSTATE_BLEND_STATE_POINTERS
<< 16 | (2 - 2));
238 OUT_BATCH(brw
->cc
.blend_state_offset
| 1);
243 const struct brw_tracked_state gen6_blend_state
= {
245 .mesa
= _NEW_BUFFERS
|
248 .brw
= BRW_NEW_BATCH
|
250 BRW_NEW_STATE_BASE_ADDRESS
,
252 .emit
= gen6_upload_blend_state
,
256 gen6_upload_color_calc_state(struct brw_context
*brw
)
258 struct gl_context
*ctx
= &brw
->ctx
;
259 struct gen6_color_calc_state
*cc
;
261 cc
= brw_state_batch(brw
, AUB_TRACE_CC_STATE
,
262 sizeof(*cc
), 64, &brw
->cc
.state_offset
);
263 memset(cc
, 0, sizeof(*cc
));
266 cc
->cc0
.alpha_test_format
= BRW_ALPHATEST_FORMAT_UNORM8
;
267 UNCLAMPED_FLOAT_TO_UBYTE(cc
->cc1
.alpha_ref_fi
.ui
, ctx
->Color
.AlphaRef
);
271 cc
->cc0
.stencil_ref
= _mesa_get_stencil_ref(ctx
, 0);
272 cc
->cc0
.bf_stencil_ref
=
273 _mesa_get_stencil_ref(ctx
, ctx
->Stencil
._BackFace
);
277 cc
->constant_r
= ctx
->Color
.BlendColorUnclamped
[0];
278 cc
->constant_g
= ctx
->Color
.BlendColorUnclamped
[1];
279 cc
->constant_b
= ctx
->Color
.BlendColorUnclamped
[2];
280 cc
->constant_a
= ctx
->Color
.BlendColorUnclamped
[3];
282 /* Point the GPU at the new indirect state. */
285 OUT_BATCH(_3DSTATE_CC_STATE_POINTERS
<< 16 | (4 - 2));
288 OUT_BATCH(brw
->cc
.state_offset
| 1);
292 OUT_BATCH(_3DSTATE_CC_STATE_POINTERS
<< 16 | (2 - 2));
293 OUT_BATCH(brw
->cc
.state_offset
| 1);
298 const struct brw_tracked_state gen6_color_calc_state
= {
302 .brw
= BRW_NEW_BATCH
|
305 BRW_NEW_STATE_BASE_ADDRESS
,
307 .emit
= gen6_upload_color_calc_state
,