Tizen 2.0 Release
[profile/ivi/osmesa.git] / src / mesa / drivers / dri / i965 / gen6_cc.c
1 /*
2  * Copyright © 2009 Intel Corporation
3  *
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:
10  *
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
13  * Software.
14  *
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
21  * IN THE SOFTWARE.
22  *
23  * Authors:
24  *    Eric Anholt <eric@anholt.net>
25  *
26  */
27
28 #include "brw_context.h"
29 #include "brw_state.h"
30 #include "brw_defines.h"
31 #include "brw_util.h"
32 #include "intel_batchbuffer.h"
33 #include "main/macros.h"
34
35 static void
36 prepare_blend_state(struct brw_context *brw)
37 {
38    struct gl_context *ctx = &brw->intel.ctx;
39    struct gen6_blend_state *blend;
40    int b;
41    int nr_draw_buffers = ctx->DrawBuffer->_NumColorDrawBuffers;
42    int size;
43
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
48     * alpha test enable.
49     */
50    if (nr_draw_buffers == 0 && ctx->Color.AlphaEnabled)
51       nr_draw_buffers = 1;
52
53    size = sizeof(*blend) * nr_draw_buffers;
54    blend = brw_state_batch(brw, size, 64, &brw->cc.blend_state_offset);
55
56    memset(blend, 0, size);
57
58    for (b = 0; b < nr_draw_buffers; b++) {
59       /* _NEW_COLOR */
60       if (ctx->Color._LogicOpEnabled) {
61          struct gl_renderbuffer *rb = ctx->DrawBuffer->_ColorDrawBuffers[b];
62          /* _NEW_BUFFERS */
63          /* Floating point RTs should have no effect from LogicOp,
64           * except for disabling of blending
65           */
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);
70          }
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;
78
79          if (eqRGB == GL_MIN || eqRGB == GL_MAX) {
80             srcRGB = dstRGB = GL_ONE;
81          }
82
83          if (eqA == GL_MIN || eqA == GL_MAX) {
84             srcA = dstA = GL_ONE;
85          }
86
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);
90
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);
94
95          blend[b].blend0.blend_enable = 1;
96          blend[b].blend0.ia_blend_enable = (srcA != srcRGB ||
97                                          dstA != dstRGB ||
98                                          eqA != eqRGB);
99       }
100
101
102       /* _NEW_COLOR */
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);
107
108       }
109
110       /* _NEW_COLOR */
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;
115       }
116
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];
121    }
122
123    brw->state.dirty.cache |= CACHE_NEW_BLEND_STATE;
124 }
125
126 const struct brw_tracked_state gen6_blend_state = {
127    .dirty = {
128       .mesa = (_NEW_COLOR |
129                _NEW_BUFFERS),
130       .brw = BRW_NEW_BATCH,
131       .cache = 0,
132    },
133    .prepare = prepare_blend_state,
134 };
135
136 static void
137 gen6_prepare_color_calc_state(struct brw_context *brw)
138 {
139    struct gl_context *ctx = &brw->intel.ctx;
140    struct gen6_color_calc_state *cc;
141
142    cc = brw_state_batch(brw, sizeof(*cc), 64, &brw->cc.state_offset);
143    memset(cc, 0, sizeof(*cc));
144
145    /* _NEW_COLOR */
146    cc->cc0.alpha_test_format = BRW_ALPHATEST_FORMAT_UNORM8;
147    UNCLAMPED_FLOAT_TO_UBYTE(cc->cc1.alpha_ref_fi.ui, ctx->Color.AlphaRef);
148
149    /* _NEW_STENCIL */
150    cc->cc0.stencil_ref = ctx->Stencil.Ref[0];
151    cc->cc0.bf_stencil_ref = ctx->Stencil.Ref[ctx->Stencil._BackFace];
152
153    /* _NEW_COLOR */
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];
158
159    brw->state.dirty.cache |= CACHE_NEW_COLOR_CALC_STATE;
160 }
161
162 const struct brw_tracked_state gen6_color_calc_state = {
163    .dirty = {
164       .mesa = _NEW_COLOR | _NEW_STENCIL,
165       .brw = BRW_NEW_BATCH,
166       .cache = 0,
167    },
168    .prepare = gen6_prepare_color_calc_state,
169 };
170
171 static void upload_cc_state_pointers(struct brw_context *brw)
172 {
173    struct intel_context *intel = &brw->intel;
174
175    BEGIN_BATCH(4);
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);
180    ADVANCE_BATCH();
181 }
182
183 const struct brw_tracked_state gen6_cc_state_pointers = {
184    .dirty = {
185       .mesa = 0,
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)
191    },
192    .emit = upload_cc_state_pointers,
193 };