gen6_multisample_state.c revision 2a9ab29ed9ff3a5e92ade4c44b4deaededc1c4bc
119e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry/*
219e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry * Copyright © 2012 Intel Corporation
319e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry *
419e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry * Permission is hereby granted, free of charge, to any person obtaining a
519e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry * copy of this software and associated documentation files (the "Software"),
619e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry * to deal in the Software without restriction, including without limitation
719e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry * the rights to use, copy, modify, merge, publish, distribute, sublicense,
819e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry * and/or sell copies of the Software, and to permit persons to whom the
919e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry * Software is furnished to do so, subject to the following conditions:
1019e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry *
1119e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry * The above copyright notice and this permission notice (including the next
1219e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry * paragraph) shall be included in all copies or substantial portions of the
1319e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry * Software.
1419e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry *
1519e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
1619e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
1719e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
1819e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
1919e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
2019e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
2119e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry * IN THE SOFTWARE.
2219e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry */
2319e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry
2419e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry#include "intel_batchbuffer.h"
2519e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry
2619e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry#include "brw_context.h"
2719e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry#include "brw_defines.h"
2819e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry
2919e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry
3019e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry/**
3119e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry * 3DSTATE_MULTISAMPLE
3219e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry */
3319e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berryvoid
3419e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berrygen6_emit_3dstate_multisample(struct brw_context *brw,
3519e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry                              unsigned num_samples)
3619e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry{
3719e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry   struct intel_context *intel = &brw->intel;
3819e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry
392a9ab29ed9ff3a5e92ade4c44b4deaededc1c4bcPaul Berry   uint32_t number_of_multisamples = 0;
402a9ab29ed9ff3a5e92ade4c44b4deaededc1c4bcPaul Berry   uint32_t sample_positions_3210 = 0;
412a9ab29ed9ff3a5e92ade4c44b4deaededc1c4bcPaul Berry   uint32_t sample_positions_7654 = 0;
422a9ab29ed9ff3a5e92ade4c44b4deaededc1c4bcPaul Berry
432a9ab29ed9ff3a5e92ade4c44b4deaededc1c4bcPaul Berry   switch (num_samples) {
442a9ab29ed9ff3a5e92ade4c44b4deaededc1c4bcPaul Berry   case 0:
452a9ab29ed9ff3a5e92ade4c44b4deaededc1c4bcPaul Berry      number_of_multisamples = MS_NUMSAMPLES_1;
462a9ab29ed9ff3a5e92ade4c44b4deaededc1c4bcPaul Berry      break;
472a9ab29ed9ff3a5e92ade4c44b4deaededc1c4bcPaul Berry   case 4:
482a9ab29ed9ff3a5e92ade4c44b4deaededc1c4bcPaul Berry      number_of_multisamples = MS_NUMSAMPLES_4;
492a9ab29ed9ff3a5e92ade4c44b4deaededc1c4bcPaul Berry      /* Sample positions:
502a9ab29ed9ff3a5e92ade4c44b4deaededc1c4bcPaul Berry       *   2 6 a e
512a9ab29ed9ff3a5e92ade4c44b4deaededc1c4bcPaul Berry       * 2   0
522a9ab29ed9ff3a5e92ade4c44b4deaededc1c4bcPaul Berry       * 6       1
532a9ab29ed9ff3a5e92ade4c44b4deaededc1c4bcPaul Berry       * a 2
542a9ab29ed9ff3a5e92ade4c44b4deaededc1c4bcPaul Berry       * e     3
552a9ab29ed9ff3a5e92ade4c44b4deaededc1c4bcPaul Berry       */
562a9ab29ed9ff3a5e92ade4c44b4deaededc1c4bcPaul Berry      sample_positions_3210 = 0xae2ae662;
572a9ab29ed9ff3a5e92ade4c44b4deaededc1c4bcPaul Berry      break;
582a9ab29ed9ff3a5e92ade4c44b4deaededc1c4bcPaul Berry   case 8:
592a9ab29ed9ff3a5e92ade4c44b4deaededc1c4bcPaul Berry      number_of_multisamples = MS_NUMSAMPLES_8;
602a9ab29ed9ff3a5e92ade4c44b4deaededc1c4bcPaul Berry      /* Sample positions are based on a solution to the "8 queens" puzzle.
612a9ab29ed9ff3a5e92ade4c44b4deaededc1c4bcPaul Berry       * Rationale: in a solution to the 8 queens puzzle, no two queens share
622a9ab29ed9ff3a5e92ade4c44b4deaededc1c4bcPaul Berry       * a row, column, or diagonal.  This is a desirable property for samples
632a9ab29ed9ff3a5e92ade4c44b4deaededc1c4bcPaul Berry       * in a multisampling pattern, because it ensures that the samples are
642a9ab29ed9ff3a5e92ade4c44b4deaededc1c4bcPaul Berry       * relatively uniformly distributed through the pixel.
652a9ab29ed9ff3a5e92ade4c44b4deaededc1c4bcPaul Berry       *
662a9ab29ed9ff3a5e92ade4c44b4deaededc1c4bcPaul Berry       * There are several solutions to the 8 queens puzzle (see
672a9ab29ed9ff3a5e92ade4c44b4deaededc1c4bcPaul Berry       * http://en.wikipedia.org/wiki/Eight_queens_puzzle).  This solution was
682a9ab29ed9ff3a5e92ade4c44b4deaededc1c4bcPaul Berry       * chosen because it has a queen close to the center; this should
692a9ab29ed9ff3a5e92ade4c44b4deaededc1c4bcPaul Berry       * improve the accuracy of centroid interpolation, since the hardware
702a9ab29ed9ff3a5e92ade4c44b4deaededc1c4bcPaul Berry       * implements centroid interpolation by choosing the centermost sample
712a9ab29ed9ff3a5e92ade4c44b4deaededc1c4bcPaul Berry       * that overlaps with the primitive being drawn.
722a9ab29ed9ff3a5e92ade4c44b4deaededc1c4bcPaul Berry       *
732a9ab29ed9ff3a5e92ade4c44b4deaededc1c4bcPaul Berry       * Note: from the Ivy Bridge PRM, Vol2 Part1 p304 (3DSTATE_MULTISAMPLE:
742a9ab29ed9ff3a5e92ade4c44b4deaededc1c4bcPaul Berry       * Programming Notes):
752a9ab29ed9ff3a5e92ade4c44b4deaededc1c4bcPaul Berry       *
762a9ab29ed9ff3a5e92ade4c44b4deaededc1c4bcPaul Berry       *     "When programming the sample offsets (for NUMSAMPLES_4 or _8 and
772a9ab29ed9ff3a5e92ade4c44b4deaededc1c4bcPaul Berry       *     MSRASTMODE_xxx_PATTERN), the order of the samples 0 to 3 (or 7
782a9ab29ed9ff3a5e92ade4c44b4deaededc1c4bcPaul Berry       *     for 8X) must have monotonically increasing distance from the
792a9ab29ed9ff3a5e92ade4c44b4deaededc1c4bcPaul Berry       *     pixel center. This is required to get the correct centroid
802a9ab29ed9ff3a5e92ade4c44b4deaededc1c4bcPaul Berry       *     computation in the device."
812a9ab29ed9ff3a5e92ade4c44b4deaededc1c4bcPaul Berry       *
822a9ab29ed9ff3a5e92ade4c44b4deaededc1c4bcPaul Berry       * Sample positions:
832a9ab29ed9ff3a5e92ade4c44b4deaededc1c4bcPaul Berry       *   1 3 5 7 9 b d f
842a9ab29ed9ff3a5e92ade4c44b4deaededc1c4bcPaul Berry       * 1     5
852a9ab29ed9ff3a5e92ade4c44b4deaededc1c4bcPaul Berry       * 3           2
862a9ab29ed9ff3a5e92ade4c44b4deaededc1c4bcPaul Berry       * 5               6
872a9ab29ed9ff3a5e92ade4c44b4deaededc1c4bcPaul Berry       * 7 4
882a9ab29ed9ff3a5e92ade4c44b4deaededc1c4bcPaul Berry       * 9       0
892a9ab29ed9ff3a5e92ade4c44b4deaededc1c4bcPaul Berry       * b             3
902a9ab29ed9ff3a5e92ade4c44b4deaededc1c4bcPaul Berry       * d         1
912a9ab29ed9ff3a5e92ade4c44b4deaededc1c4bcPaul Berry       * f   7
922a9ab29ed9ff3a5e92ade4c44b4deaededc1c4bcPaul Berry       */
932a9ab29ed9ff3a5e92ade4c44b4deaededc1c4bcPaul Berry      sample_positions_3210 = 0xdbb39d79;
942a9ab29ed9ff3a5e92ade4c44b4deaededc1c4bcPaul Berry      sample_positions_7654 = 0x3ff55117;
952a9ab29ed9ff3a5e92ade4c44b4deaededc1c4bcPaul Berry      break;
962a9ab29ed9ff3a5e92ade4c44b4deaededc1c4bcPaul Berry   default:
972a9ab29ed9ff3a5e92ade4c44b4deaededc1c4bcPaul Berry      assert(!"Unrecognized num_samples in gen6_emit_3dstate_multisample");
982a9ab29ed9ff3a5e92ade4c44b4deaededc1c4bcPaul Berry      break;
992a9ab29ed9ff3a5e92ade4c44b4deaededc1c4bcPaul Berry   }
10019e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry
10119e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry   int len = intel->gen >= 7 ? 4 : 3;
10219e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry   BEGIN_BATCH(len);
10319e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry   OUT_BATCH(_3DSTATE_MULTISAMPLE << 16 | (len - 2));
1042a9ab29ed9ff3a5e92ade4c44b4deaededc1c4bcPaul Berry   OUT_BATCH(MS_PIXEL_LOCATION_CENTER | number_of_multisamples);
1052a9ab29ed9ff3a5e92ade4c44b4deaededc1c4bcPaul Berry   OUT_BATCH(sample_positions_3210);
10619e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry   if (intel->gen >= 7)
1072a9ab29ed9ff3a5e92ade4c44b4deaededc1c4bcPaul Berry      OUT_BATCH(sample_positions_7654);
10819e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry   ADVANCE_BATCH();
10919e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry}
11019e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry
11119e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry
11219e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry/**
11319e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry * 3DSTATE_SAMPLE_MASK
11419e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry */
11519e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berryvoid
11619e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berrygen6_emit_3dstate_sample_mask(struct brw_context *brw,
1179ea60ce58f8494e0b79771f93227f4b8181731dePaul Berry                              unsigned num_samples, float coverage,
1189ea60ce58f8494e0b79771f93227f4b8181731dePaul Berry                              bool coverage_invert)
11919e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry{
12019e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry   struct intel_context *intel = &brw->intel;
12119e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry
122ab014adaed14a9ca213447dc913d0dce7906be56Paul Berry   /* TODO: 8x MSAA not implemented */
123ab014adaed14a9ca213447dc913d0dce7906be56Paul Berry   assert(num_samples <= 4);
12419e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry
12519e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry   BEGIN_BATCH(2);
12619e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry   OUT_BATCH(_3DSTATE_SAMPLE_MASK << 16 | (2 - 2));
1279ea60ce58f8494e0b79771f93227f4b8181731dePaul Berry   if (num_samples > 0) {
1289ea60ce58f8494e0b79771f93227f4b8181731dePaul Berry      int coverage_int = (int) (num_samples * coverage + 0.5);
1299ea60ce58f8494e0b79771f93227f4b8181731dePaul Berry      uint32_t coverage_bits = (1 << coverage_int) - 1;
1309ea60ce58f8494e0b79771f93227f4b8181731dePaul Berry      if (coverage_invert)
1319ea60ce58f8494e0b79771f93227f4b8181731dePaul Berry         coverage_bits ^= (1 << num_samples) - 1;
1329ea60ce58f8494e0b79771f93227f4b8181731dePaul Berry      OUT_BATCH(coverage_bits);
1339ea60ce58f8494e0b79771f93227f4b8181731dePaul Berry   } else {
1349ea60ce58f8494e0b79771f93227f4b8181731dePaul Berry      OUT_BATCH(1);
1359ea60ce58f8494e0b79771f93227f4b8181731dePaul Berry   }
13619e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry   ADVANCE_BATCH();
13719e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry}
13819e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry
13919e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry
14019e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berrystatic void upload_multisample_state(struct brw_context *brw)
14119e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry{
14219e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry   struct intel_context *intel = &brw->intel;
14319e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry   struct gl_context *ctx = &intel->ctx;
1449ea60ce58f8494e0b79771f93227f4b8181731dePaul Berry   float coverage = 1.0;
1459ea60ce58f8494e0b79771f93227f4b8181731dePaul Berry   float coverage_invert = false;
1469ea60ce58f8494e0b79771f93227f4b8181731dePaul Berry
147691c55f3560e5b8b9db9ecd2c089f13b41ec684fPaul Berry   /* _NEW_BUFFERS */
148691c55f3560e5b8b9db9ecd2c089f13b41ec684fPaul Berry   unsigned num_samples = ctx->DrawBuffer->Visual.samples;
149691c55f3560e5b8b9db9ecd2c089f13b41ec684fPaul Berry
1509ea60ce58f8494e0b79771f93227f4b8181731dePaul Berry   /* _NEW_MULTISAMPLE */
1519ea60ce58f8494e0b79771f93227f4b8181731dePaul Berry   if (ctx->Multisample._Enabled && ctx->Multisample.SampleCoverage) {
1529ea60ce58f8494e0b79771f93227f4b8181731dePaul Berry      coverage = ctx->Multisample.SampleCoverageValue;
1539ea60ce58f8494e0b79771f93227f4b8181731dePaul Berry      coverage_invert = ctx->Multisample.SampleCoverageInvert;
1549ea60ce58f8494e0b79771f93227f4b8181731dePaul Berry   }
15519e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry
15619e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry   /* 3DSTATE_MULTISAMPLE is nonpipelined. */
15719e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry   intel_emit_post_sync_nonzero_flush(intel);
15819e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry
15919e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry   gen6_emit_3dstate_multisample(brw, num_samples);
1609ea60ce58f8494e0b79771f93227f4b8181731dePaul Berry   gen6_emit_3dstate_sample_mask(brw, num_samples, coverage, coverage_invert);
16119e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry}
16219e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry
16319e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry
16419e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berryconst struct brw_tracked_state gen6_multisample_state = {
16519e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry   .dirty = {
1669ea60ce58f8494e0b79771f93227f4b8181731dePaul Berry      .mesa = _NEW_BUFFERS |
1679ea60ce58f8494e0b79771f93227f4b8181731dePaul Berry              _NEW_MULTISAMPLE,
16819e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry      .brw = BRW_NEW_CONTEXT,
16919e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry      .cache = 0
17019e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry   },
17119e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry   .emit = upload_multisample_state
17219e9b24626c2b9d7abef054d57bb2a52106c545bPaul Berry};
173