gen6_cc.c revision d375df220fae47f38944c4832bcbd5f5d568884c
17ee590424c974cb10882e9c4664a6024595fc9deEric Anholt/*
27ee590424c974cb10882e9c4664a6024595fc9deEric Anholt * Copyright © 2009 Intel Corporation
37ee590424c974cb10882e9c4664a6024595fc9deEric Anholt *
47ee590424c974cb10882e9c4664a6024595fc9deEric Anholt * Permission is hereby granted, free of charge, to any person obtaining a
57ee590424c974cb10882e9c4664a6024595fc9deEric Anholt * copy of this software and associated documentation files (the "Software"),
67ee590424c974cb10882e9c4664a6024595fc9deEric Anholt * to deal in the Software without restriction, including without limitation
77ee590424c974cb10882e9c4664a6024595fc9deEric Anholt * the rights to use, copy, modify, merge, publish, distribute, sublicense,
87ee590424c974cb10882e9c4664a6024595fc9deEric Anholt * and/or sell copies of the Software, and to permit persons to whom the
97ee590424c974cb10882e9c4664a6024595fc9deEric Anholt * Software is furnished to do so, subject to the following conditions:
107ee590424c974cb10882e9c4664a6024595fc9deEric Anholt *
117ee590424c974cb10882e9c4664a6024595fc9deEric Anholt * The above copyright notice and this permission notice (including the next
127ee590424c974cb10882e9c4664a6024595fc9deEric Anholt * paragraph) shall be included in all copies or substantial portions of the
137ee590424c974cb10882e9c4664a6024595fc9deEric Anholt * Software.
147ee590424c974cb10882e9c4664a6024595fc9deEric Anholt *
157ee590424c974cb10882e9c4664a6024595fc9deEric Anholt * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
167ee590424c974cb10882e9c4664a6024595fc9deEric Anholt * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
177ee590424c974cb10882e9c4664a6024595fc9deEric Anholt * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
187ee590424c974cb10882e9c4664a6024595fc9deEric Anholt * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
197ee590424c974cb10882e9c4664a6024595fc9deEric Anholt * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
207ee590424c974cb10882e9c4664a6024595fc9deEric Anholt * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
217ee590424c974cb10882e9c4664a6024595fc9deEric Anholt * IN THE SOFTWARE.
227ee590424c974cb10882e9c4664a6024595fc9deEric Anholt *
237ee590424c974cb10882e9c4664a6024595fc9deEric Anholt * Authors:
247ee590424c974cb10882e9c4664a6024595fc9deEric Anholt *    Eric Anholt <eric@anholt.net>
257ee590424c974cb10882e9c4664a6024595fc9deEric Anholt *
267ee590424c974cb10882e9c4664a6024595fc9deEric Anholt */
277ee590424c974cb10882e9c4664a6024595fc9deEric Anholt
287ee590424c974cb10882e9c4664a6024595fc9deEric Anholt#include "brw_context.h"
297ee590424c974cb10882e9c4664a6024595fc9deEric Anholt#include "brw_state.h"
307ee590424c974cb10882e9c4664a6024595fc9deEric Anholt#include "brw_defines.h"
317ee590424c974cb10882e9c4664a6024595fc9deEric Anholt#include "brw_util.h"
327ee590424c974cb10882e9c4664a6024595fc9deEric Anholt#include "intel_batchbuffer.h"
33cb099c2ecfe3cf8d8ff21c3031e3c2d43efd0e69Brian Paul#include "main/macros.h"
347ee590424c974cb10882e9c4664a6024595fc9deEric Anholt
357ee590424c974cb10882e9c4664a6024595fc9deEric Anholtstatic void
3635e8fe5c99b285f348cb8a1bba2931f120f7c0a1Eric Anholtprepare_blend_state(struct brw_context *brw)
377ee590424c974cb10882e9c4664a6024595fc9deEric Anholt{
38f9995b30756140724f41daf963fa06167912be7fKristian Høgsberg   struct gl_context *ctx = &brw->intel.ctx;
3935e8fe5c99b285f348cb8a1bba2931f120f7c0a1Eric Anholt   struct gen6_blend_state *blend;
404b4dc778b6289891d18ad62efdea86b060f07061Eric Anholt   int b;
4135e8fe5c99b285f348cb8a1bba2931f120f7c0a1Eric Anholt   int nr_draw_buffers = ctx->DrawBuffer->_NumColorDrawBuffers;
42367020d87ce8c27aeb58b3887fbd29d216fdc151Eric Anholt   int size;
437ee590424c974cb10882e9c4664a6024595fc9deEric Anholt
44367020d87ce8c27aeb58b3887fbd29d216fdc151Eric Anholt   /* We need at least one BLEND_STATE written, because we might do
45367020d87ce8c27aeb58b3887fbd29d216fdc151Eric Anholt    * thread dispatch even if _NumColorDrawBuffers is 0 (for example
46367020d87ce8c27aeb58b3887fbd29d216fdc151Eric Anholt    * for computed depth or alpha test), which will do an FB write
47367020d87ce8c27aeb58b3887fbd29d216fdc151Eric Anholt    * with render target 0, which will reference BLEND_STATE[0] for
48367020d87ce8c27aeb58b3887fbd29d216fdc151Eric Anholt    * alpha test enable.
49367020d87ce8c27aeb58b3887fbd29d216fdc151Eric Anholt    */
50367020d87ce8c27aeb58b3887fbd29d216fdc151Eric Anholt   if (nr_draw_buffers == 0 && ctx->Color.AlphaEnabled)
51367020d87ce8c27aeb58b3887fbd29d216fdc151Eric Anholt      nr_draw_buffers = 1;
52367020d87ce8c27aeb58b3887fbd29d216fdc151Eric Anholt
53367020d87ce8c27aeb58b3887fbd29d216fdc151Eric Anholt   size = sizeof(*blend) * nr_draw_buffers;
54d375df220fae47f38944c4832bcbd5f5d568884cEric Anholt   blend = brw_state_batch(brw, AUB_TRACE_BLEND_STATE,
55d375df220fae47f38944c4832bcbd5f5d568884cEric Anholt			   size, 64, &brw->cc.blend_state_offset);
567ee590424c974cb10882e9c4664a6024595fc9deEric Anholt
5735e8fe5c99b285f348cb8a1bba2931f120f7c0a1Eric Anholt   memset(blend, 0, size);
5835e8fe5c99b285f348cb8a1bba2931f120f7c0a1Eric Anholt
5935e8fe5c99b285f348cb8a1bba2931f120f7c0a1Eric Anholt   for (b = 0; b < nr_draw_buffers; b++) {
6035e8fe5c99b285f348cb8a1bba2931f120f7c0a1Eric Anholt      /* _NEW_COLOR */
616a02679f0120f095d2678eec4532e27e9627b26dEric Anholt      if (ctx->Color._LogicOpEnabled) {
626a02679f0120f095d2678eec4532e27e9627b26dEric Anholt	 struct gl_renderbuffer *rb = ctx->DrawBuffer->_ColorDrawBuffers[b];
636a02679f0120f095d2678eec4532e27e9627b26dEric Anholt	 /* _NEW_BUFFERS */
646a02679f0120f095d2678eec4532e27e9627b26dEric Anholt	 /* Floating point RTs should have no effect from LogicOp,
656a02679f0120f095d2678eec4532e27e9627b26dEric Anholt	  * except for disabling of blending
666a02679f0120f095d2678eec4532e27e9627b26dEric Anholt	  */
676a02679f0120f095d2678eec4532e27e9627b26dEric Anholt	 if (_mesa_get_format_datatype(rb->Format) != GL_FLOAT) {
686a02679f0120f095d2678eec4532e27e9627b26dEric Anholt	    blend[b].blend1.logic_op_enable = 1;
696a02679f0120f095d2678eec4532e27e9627b26dEric Anholt	    blend[b].blend1.logic_op_func =
706a02679f0120f095d2678eec4532e27e9627b26dEric Anholt	       intel_translate_logic_op(ctx->Color.LogicOp);
716a02679f0120f095d2678eec4532e27e9627b26dEric Anholt	 }
7235e8fe5c99b285f348cb8a1bba2931f120f7c0a1Eric Anholt      } else if (ctx->Color.BlendEnabled & (1 << b)) {
7335e8fe5c99b285f348cb8a1bba2931f120f7c0a1Eric Anholt	 GLenum eqRGB = ctx->Color.Blend[0].EquationRGB;
7435e8fe5c99b285f348cb8a1bba2931f120f7c0a1Eric Anholt	 GLenum eqA = ctx->Color.Blend[0].EquationA;
7535e8fe5c99b285f348cb8a1bba2931f120f7c0a1Eric Anholt	 GLenum srcRGB = ctx->Color.Blend[0].SrcRGB;
7635e8fe5c99b285f348cb8a1bba2931f120f7c0a1Eric Anholt	 GLenum dstRGB = ctx->Color.Blend[0].DstRGB;
7735e8fe5c99b285f348cb8a1bba2931f120f7c0a1Eric Anholt	 GLenum srcA = ctx->Color.Blend[0].SrcA;
7835e8fe5c99b285f348cb8a1bba2931f120f7c0a1Eric Anholt	 GLenum dstA = ctx->Color.Blend[0].DstA;
794b4dc778b6289891d18ad62efdea86b060f07061Eric Anholt
804b4dc778b6289891d18ad62efdea86b060f07061Eric Anholt	 if (eqRGB == GL_MIN || eqRGB == GL_MAX) {
814b4dc778b6289891d18ad62efdea86b060f07061Eric Anholt	    srcRGB = dstRGB = GL_ONE;
824b4dc778b6289891d18ad62efdea86b060f07061Eric Anholt	 }
834b4dc778b6289891d18ad62efdea86b060f07061Eric Anholt
844b4dc778b6289891d18ad62efdea86b060f07061Eric Anholt	 if (eqA == GL_MIN || eqA == GL_MAX) {
854b4dc778b6289891d18ad62efdea86b060f07061Eric Anholt	    srcA = dstA = GL_ONE;
864b4dc778b6289891d18ad62efdea86b060f07061Eric Anholt	 }
874b4dc778b6289891d18ad62efdea86b060f07061Eric Anholt
884b4dc778b6289891d18ad62efdea86b060f07061Eric Anholt	 blend[b].blend0.dest_blend_factor = brw_translate_blend_factor(dstRGB);
894b4dc778b6289891d18ad62efdea86b060f07061Eric Anholt	 blend[b].blend0.source_blend_factor = brw_translate_blend_factor(srcRGB);
904b4dc778b6289891d18ad62efdea86b060f07061Eric Anholt	 blend[b].blend0.blend_func = brw_translate_blend_equation(eqRGB);
914b4dc778b6289891d18ad62efdea86b060f07061Eric Anholt
924b4dc778b6289891d18ad62efdea86b060f07061Eric Anholt	 blend[b].blend0.ia_dest_blend_factor = brw_translate_blend_factor(dstA);
934b4dc778b6289891d18ad62efdea86b060f07061Eric Anholt	 blend[b].blend0.ia_source_blend_factor = brw_translate_blend_factor(srcA);
944b4dc778b6289891d18ad62efdea86b060f07061Eric Anholt	 blend[b].blend0.ia_blend_func = brw_translate_blend_equation(eqA);
954b4dc778b6289891d18ad62efdea86b060f07061Eric Anholt
964b4dc778b6289891d18ad62efdea86b060f07061Eric Anholt	 blend[b].blend0.blend_enable = 1;
974b4dc778b6289891d18ad62efdea86b060f07061Eric Anholt	 blend[b].blend0.ia_blend_enable = (srcA != srcRGB ||
984b4dc778b6289891d18ad62efdea86b060f07061Eric Anholt					 dstA != dstRGB ||
994b4dc778b6289891d18ad62efdea86b060f07061Eric Anholt					 eqA != eqRGB);
1007ee590424c974cb10882e9c4664a6024595fc9deEric Anholt      }
1017ee590424c974cb10882e9c4664a6024595fc9deEric Anholt
10235e8fe5c99b285f348cb8a1bba2931f120f7c0a1Eric Anholt
10335e8fe5c99b285f348cb8a1bba2931f120f7c0a1Eric Anholt      /* _NEW_COLOR */
10435e8fe5c99b285f348cb8a1bba2931f120f7c0a1Eric Anholt      if (ctx->Color.AlphaEnabled) {
1054b4dc778b6289891d18ad62efdea86b060f07061Eric Anholt	 blend[b].blend1.alpha_test_enable = 1;
10635e8fe5c99b285f348cb8a1bba2931f120f7c0a1Eric Anholt	 blend[b].blend1.alpha_test_func =
10735e8fe5c99b285f348cb8a1bba2931f120f7c0a1Eric Anholt	    intel_translate_compare_func(ctx->Color.AlphaFunc);
1087ee590424c974cb10882e9c4664a6024595fc9deEric Anholt
1094b4dc778b6289891d18ad62efdea86b060f07061Eric Anholt      }
1107ee590424c974cb10882e9c4664a6024595fc9deEric Anholt
11135e8fe5c99b285f348cb8a1bba2931f120f7c0a1Eric Anholt      /* _NEW_COLOR */
11235e8fe5c99b285f348cb8a1bba2931f120f7c0a1Eric Anholt      if (ctx->Color.DitherFlag) {
1134b4dc778b6289891d18ad62efdea86b060f07061Eric Anholt	 blend[b].blend1.dither_enable = 1;
1144b4dc778b6289891d18ad62efdea86b060f07061Eric Anholt	 blend[b].blend1.y_dither_offset = 0;
1154b4dc778b6289891d18ad62efdea86b060f07061Eric Anholt	 blend[b].blend1.x_dither_offset = 0;
1164b4dc778b6289891d18ad62efdea86b060f07061Eric Anholt      }
1177ee590424c974cb10882e9c4664a6024595fc9deEric Anholt
11835e8fe5c99b285f348cb8a1bba2931f120f7c0a1Eric Anholt      blend[b].blend1.write_disable_r = !ctx->Color.ColorMask[b][0];
11935e8fe5c99b285f348cb8a1bba2931f120f7c0a1Eric Anholt      blend[b].blend1.write_disable_g = !ctx->Color.ColorMask[b][1];
12035e8fe5c99b285f348cb8a1bba2931f120f7c0a1Eric Anholt      blend[b].blend1.write_disable_b = !ctx->Color.ColorMask[b][2];
12135e8fe5c99b285f348cb8a1bba2931f120f7c0a1Eric Anholt      blend[b].blend1.write_disable_a = !ctx->Color.ColorMask[b][3];
1227ee590424c974cb10882e9c4664a6024595fc9deEric Anholt   }
1237ee590424c974cb10882e9c4664a6024595fc9deEric Anholt
12435e8fe5c99b285f348cb8a1bba2931f120f7c0a1Eric Anholt   brw->state.dirty.cache |= CACHE_NEW_BLEND_STATE;
1257ee590424c974cb10882e9c4664a6024595fc9deEric Anholt}
1267ee590424c974cb10882e9c4664a6024595fc9deEric Anholt
1277ee590424c974cb10882e9c4664a6024595fc9deEric Anholtconst struct brw_tracked_state gen6_blend_state = {
1287ee590424c974cb10882e9c4664a6024595fc9deEric Anholt   .dirty = {
1296a02679f0120f095d2678eec4532e27e9627b26dEric Anholt      .mesa = (_NEW_COLOR |
1306a02679f0120f095d2678eec4532e27e9627b26dEric Anholt	       _NEW_BUFFERS),
13135e8fe5c99b285f348cb8a1bba2931f120f7c0a1Eric Anholt      .brw = BRW_NEW_BATCH,
1327ee590424c974cb10882e9c4664a6024595fc9deEric Anholt      .cache = 0,
1337ee590424c974cb10882e9c4664a6024595fc9deEric Anholt   },
1347ee590424c974cb10882e9c4664a6024595fc9deEric Anholt   .prepare = prepare_blend_state,
1357ee590424c974cb10882e9c4664a6024595fc9deEric Anholt};
1367ee590424c974cb10882e9c4664a6024595fc9deEric Anholt
1377ee590424c974cb10882e9c4664a6024595fc9deEric Anholtstatic void
1382ee1fd2e8f023853b60c242ce7f83a595c0f65ffEric Anholtgen6_prepare_color_calc_state(struct brw_context *brw)
1397ee590424c974cb10882e9c4664a6024595fc9deEric Anholt{
140f9995b30756140724f41daf963fa06167912be7fKristian Høgsberg   struct gl_context *ctx = &brw->intel.ctx;
1412ee1fd2e8f023853b60c242ce7f83a595c0f65ffEric Anholt   struct gen6_color_calc_state *cc;
1427ee590424c974cb10882e9c4664a6024595fc9deEric Anholt
143d375df220fae47f38944c4832bcbd5f5d568884cEric Anholt   cc = brw_state_batch(brw, AUB_TRACE_CC_STATE,
144d375df220fae47f38944c4832bcbd5f5d568884cEric Anholt			sizeof(*cc), 64, &brw->cc.state_offset);
1452ee1fd2e8f023853b60c242ce7f83a595c0f65ffEric Anholt   memset(cc, 0, sizeof(*cc));
1467ee590424c974cb10882e9c4664a6024595fc9deEric Anholt
1477ee590424c974cb10882e9c4664a6024595fc9deEric Anholt   /* _NEW_COLOR */
1482ee1fd2e8f023853b60c242ce7f83a595c0f65ffEric Anholt   cc->cc0.alpha_test_format = BRW_ALPHATEST_FORMAT_UNORM8;
1492ee1fd2e8f023853b60c242ce7f83a595c0f65ffEric Anholt   UNCLAMPED_FLOAT_TO_UBYTE(cc->cc1.alpha_ref_fi.ui, ctx->Color.AlphaRef);
1507ee590424c974cb10882e9c4664a6024595fc9deEric Anholt
1512ee1fd2e8f023853b60c242ce7f83a595c0f65ffEric Anholt   /* _NEW_STENCIL */
1522ee1fd2e8f023853b60c242ce7f83a595c0f65ffEric Anholt   cc->cc0.stencil_ref = ctx->Stencil.Ref[0];
1532ee1fd2e8f023853b60c242ce7f83a595c0f65ffEric Anholt   cc->cc0.bf_stencil_ref = ctx->Stencil.Ref[ctx->Stencil._BackFace];
1547ee590424c974cb10882e9c4664a6024595fc9deEric Anholt
1552ee1fd2e8f023853b60c242ce7f83a595c0f65ffEric Anholt   /* _NEW_COLOR */
1562ee1fd2e8f023853b60c242ce7f83a595c0f65ffEric Anholt   cc->constant_r = ctx->Color.BlendColorUnclamped[0];
1572ee1fd2e8f023853b60c242ce7f83a595c0f65ffEric Anholt   cc->constant_g = ctx->Color.BlendColorUnclamped[1];
1582ee1fd2e8f023853b60c242ce7f83a595c0f65ffEric Anholt   cc->constant_b = ctx->Color.BlendColorUnclamped[2];
1592ee1fd2e8f023853b60c242ce7f83a595c0f65ffEric Anholt   cc->constant_a = ctx->Color.BlendColorUnclamped[3];
1607ee590424c974cb10882e9c4664a6024595fc9deEric Anholt
1612ee1fd2e8f023853b60c242ce7f83a595c0f65ffEric Anholt   brw->state.dirty.cache |= CACHE_NEW_COLOR_CALC_STATE;
1627ee590424c974cb10882e9c4664a6024595fc9deEric Anholt}
1637ee590424c974cb10882e9c4664a6024595fc9deEric Anholt
1647ee590424c974cb10882e9c4664a6024595fc9deEric Anholtconst struct brw_tracked_state gen6_color_calc_state = {
1657ee590424c974cb10882e9c4664a6024595fc9deEric Anholt   .dirty = {
1669d45c7d1ce0c4ab7c981ddd15b296b2117437fa4Eric Anholt      .mesa = _NEW_COLOR | _NEW_STENCIL,
1672ee1fd2e8f023853b60c242ce7f83a595c0f65ffEric Anholt      .brw = BRW_NEW_BATCH,
1687ee590424c974cb10882e9c4664a6024595fc9deEric Anholt      .cache = 0,
1697ee590424c974cb10882e9c4664a6024595fc9deEric Anholt   },
1702ee1fd2e8f023853b60c242ce7f83a595c0f65ffEric Anholt   .prepare = gen6_prepare_color_calc_state,
1717ee590424c974cb10882e9c4664a6024595fc9deEric Anholt};
1727ee590424c974cb10882e9c4664a6024595fc9deEric Anholt
1737ee590424c974cb10882e9c4664a6024595fc9deEric Anholtstatic void upload_cc_state_pointers(struct brw_context *brw)
1747ee590424c974cb10882e9c4664a6024595fc9deEric Anholt{
1757ee590424c974cb10882e9c4664a6024595fc9deEric Anholt   struct intel_context *intel = &brw->intel;
1767ee590424c974cb10882e9c4664a6024595fc9deEric Anholt
1777ee590424c974cb10882e9c4664a6024595fc9deEric Anholt   BEGIN_BATCH(4);
178e31defc825ee94f1d1092e277954abad7097c552Kenneth Graunke   OUT_BATCH(_3DSTATE_CC_STATE_POINTERS << 16 | (4 - 2));
179a82a43e8d99e1715dd11c9c091b5ab734079b6a6Eric Anholt   OUT_BATCH(brw->cc.blend_state_offset | 1);
180a82a43e8d99e1715dd11c9c091b5ab734079b6a6Eric Anholt   OUT_BATCH(brw->cc.depth_stencil_state_offset | 1);
181a82a43e8d99e1715dd11c9c091b5ab734079b6a6Eric Anholt   OUT_BATCH(brw->cc.state_offset | 1);
1827ee590424c974cb10882e9c4664a6024595fc9deEric Anholt   ADVANCE_BATCH();
1837ee590424c974cb10882e9c4664a6024595fc9deEric Anholt}
1847ee590424c974cb10882e9c4664a6024595fc9deEric Anholt
1857ee590424c974cb10882e9c4664a6024595fc9deEric Anholtconst struct brw_tracked_state gen6_cc_state_pointers = {
1867ee590424c974cb10882e9c4664a6024595fc9deEric Anholt   .dirty = {
1877ee590424c974cb10882e9c4664a6024595fc9deEric Anholt      .mesa = 0,
188a09c5c2e3053c48a33134cf28229105bfef52e6fEric Anholt      .brw = (BRW_NEW_BATCH |
189a09c5c2e3053c48a33134cf28229105bfef52e6fEric Anholt	      BRW_NEW_STATE_BASE_ADDRESS),
1907ee590424c974cb10882e9c4664a6024595fc9deEric Anholt      .cache = (CACHE_NEW_BLEND_STATE |
1917ee590424c974cb10882e9c4664a6024595fc9deEric Anholt		CACHE_NEW_COLOR_CALC_STATE |
1927ee590424c974cb10882e9c4664a6024595fc9deEric Anholt		CACHE_NEW_DEPTH_STENCIL_STATE)
1937ee590424c974cb10882e9c4664a6024595fc9deEric Anholt   },
1947ee590424c974cb10882e9c4664a6024595fc9deEric Anholt   .emit = upload_cc_state_pointers,
1957ee590424c974cb10882e9c4664a6024595fc9deEric Anholt};
196