Path: blob/21.2-virgl/src/gallium/drivers/r600/cayman_msaa.c
4570 views
/*1* Copyright 2014 Advanced Micro Devices, Inc.2*3* Permission is hereby granted, free of charge, to any person obtaining a4* copy of this software and associated documentation files (the "Software"),5* to deal in the Software without restriction, including without limitation6* the rights to use, copy, modify, merge, publish, distribute, sublicense,7* and/or sell copies of the Software, and to permit persons to whom the8* Software is furnished to do so, subject to the following conditions:9*10* The above copyright notice and this permission notice (including the next11* paragraph) shall be included in all copies or substantial portions of the12* Software.13*14* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR15* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,16* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL17* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER18* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,19* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE20* SOFTWARE.21*22* Authors: Marek Olšák <[email protected]>23*24*/2526#include "r600_cs.h"27#include "evergreend.h"2829/* 2xMSAA30* There are two locations (4, 4), (-4, -4). */31const uint32_t eg_sample_locs_2x[4] = {32FILL_SREG(4, 4, -4, -4, 4, 4, -4, -4),33FILL_SREG(4, 4, -4, -4, 4, 4, -4, -4),34FILL_SREG(4, 4, -4, -4, 4, 4, -4, -4),35FILL_SREG(4, 4, -4, -4, 4, 4, -4, -4),36};37const unsigned eg_max_dist_2x = 4;38/* 4xMSAA39* There are 4 locations: (-2, 6), (6, -2), (-6, 2), (2, 6). */40const uint32_t eg_sample_locs_4x[4] = {41FILL_SREG(-2, -6, 6, -2, -6, 2, 2, 6),42FILL_SREG(-2, -6, 6, -2, -6, 2, 2, 6),43FILL_SREG(-2, -6, 6, -2, -6, 2, 2, 6),44FILL_SREG(-2, -6, 6, -2, -6, 2, 2, 6),45};46const unsigned eg_max_dist_4x = 6;4748/* Cayman 8xMSAA */49static const uint32_t cm_sample_locs_8x[] = {50FILL_SREG( 1, -3, -1, 3, 5, 1, -3, -5),51FILL_SREG( 1, -3, -1, 3, 5, 1, -3, -5),52FILL_SREG( 1, -3, -1, 3, 5, 1, -3, -5),53FILL_SREG( 1, -3, -1, 3, 5, 1, -3, -5),54FILL_SREG(-5, 5, -7, -1, 3, 7, 7, -7),55FILL_SREG(-5, 5, -7, -1, 3, 7, 7, -7),56FILL_SREG(-5, 5, -7, -1, 3, 7, 7, -7),57FILL_SREG(-5, 5, -7, -1, 3, 7, 7, -7),58};59static const unsigned cm_max_dist_8x = 8;60/* Cayman 16xMSAA */61static const uint32_t cm_sample_locs_16x[] = {62FILL_SREG( 1, 1, -1, -3, -3, 2, 4, -1),63FILL_SREG( 1, 1, -1, -3, -3, 2, 4, -1),64FILL_SREG( 1, 1, -1, -3, -3, 2, 4, -1),65FILL_SREG( 1, 1, -1, -3, -3, 2, 4, -1),66FILL_SREG(-5, -2, 2, 5, 5, 3, 3, -5),67FILL_SREG(-5, -2, 2, 5, 5, 3, 3, -5),68FILL_SREG(-5, -2, 2, 5, 5, 3, 3, -5),69FILL_SREG(-5, -2, 2, 5, 5, 3, 3, -5),70FILL_SREG(-2, 6, 0, -7, -4, -6, -6, 4),71FILL_SREG(-2, 6, 0, -7, -4, -6, -6, 4),72FILL_SREG(-2, 6, 0, -7, -4, -6, -6, 4),73FILL_SREG(-2, 6, 0, -7, -4, -6, -6, 4),74FILL_SREG(-8, 0, 7, -4, 6, 7, -7, -8),75FILL_SREG(-8, 0, 7, -4, 6, 7, -7, -8),76FILL_SREG(-8, 0, 7, -4, 6, 7, -7, -8),77FILL_SREG(-8, 0, 7, -4, 6, 7, -7, -8),78};79static const unsigned cm_max_dist_16x = 8;8081void cayman_get_sample_position(struct pipe_context *ctx, unsigned sample_count,82unsigned sample_index, float *out_value)83{84int offset, index;85struct {86int idx:4;87} val;88switch (sample_count) {89case 1:90default:91out_value[0] = out_value[1] = 0.5;92break;93case 2:94offset = 4 * (sample_index * 2);95val.idx = (eg_sample_locs_2x[0] >> offset) & 0xf;96out_value[0] = (float)(val.idx + 8) / 16.0f;97val.idx = (eg_sample_locs_2x[0] >> (offset + 4)) & 0xf;98out_value[1] = (float)(val.idx + 8) / 16.0f;99break;100case 4:101offset = 4 * (sample_index * 2);102val.idx = (eg_sample_locs_4x[0] >> offset) & 0xf;103out_value[0] = (float)(val.idx + 8) / 16.0f;104val.idx = (eg_sample_locs_4x[0] >> (offset + 4)) & 0xf;105out_value[1] = (float)(val.idx + 8) / 16.0f;106break;107case 8:108offset = 4 * (sample_index % 4 * 2);109index = (sample_index / 4) * 4;110val.idx = (cm_sample_locs_8x[index] >> offset) & 0xf;111out_value[0] = (float)(val.idx + 8) / 16.0f;112val.idx = (cm_sample_locs_8x[index] >> (offset + 4)) & 0xf;113out_value[1] = (float)(val.idx + 8) / 16.0f;114break;115case 16:116offset = 4 * (sample_index % 4 * 2);117index = (sample_index / 4) * 4;118val.idx = (cm_sample_locs_16x[index] >> offset) & 0xf;119out_value[0] = (float)(val.idx + 8) / 16.0f;120val.idx = (cm_sample_locs_16x[index] >> (offset + 4)) & 0xf;121out_value[1] = (float)(val.idx + 8) / 16.0f;122break;123}124}125126void cayman_init_msaa(struct pipe_context *ctx)127{128struct r600_common_context *rctx = (struct r600_common_context*)ctx;129int i;130131cayman_get_sample_position(ctx, 1, 0, rctx->sample_locations_1x[0]);132133for (i = 0; i < 2; i++)134cayman_get_sample_position(ctx, 2, i, rctx->sample_locations_2x[i]);135for (i = 0; i < 4; i++)136cayman_get_sample_position(ctx, 4, i, rctx->sample_locations_4x[i]);137for (i = 0; i < 8; i++)138cayman_get_sample_position(ctx, 8, i, rctx->sample_locations_8x[i]);139for (i = 0; i < 16; i++)140cayman_get_sample_position(ctx, 16, i, rctx->sample_locations_16x[i]);141}142143static void cayman_emit_msaa_sample_locs(struct radeon_cmdbuf *cs, int nr_samples)144{145switch (nr_samples) {146default:147case 1:148radeon_set_context_reg(cs, CM_R_028BF8_PA_SC_AA_SAMPLE_LOCS_PIXEL_X0Y0_0, 0);149radeon_set_context_reg(cs, CM_R_028C08_PA_SC_AA_SAMPLE_LOCS_PIXEL_X1Y0_0, 0);150radeon_set_context_reg(cs, CM_R_028C18_PA_SC_AA_SAMPLE_LOCS_PIXEL_X0Y1_0, 0);151radeon_set_context_reg(cs, CM_R_028C28_PA_SC_AA_SAMPLE_LOCS_PIXEL_X1Y1_0, 0);152break;153case 2:154radeon_set_context_reg(cs, CM_R_028BF8_PA_SC_AA_SAMPLE_LOCS_PIXEL_X0Y0_0, eg_sample_locs_2x[0]);155radeon_set_context_reg(cs, CM_R_028C08_PA_SC_AA_SAMPLE_LOCS_PIXEL_X1Y0_0, eg_sample_locs_2x[1]);156radeon_set_context_reg(cs, CM_R_028C18_PA_SC_AA_SAMPLE_LOCS_PIXEL_X0Y1_0, eg_sample_locs_2x[2]);157radeon_set_context_reg(cs, CM_R_028C28_PA_SC_AA_SAMPLE_LOCS_PIXEL_X1Y1_0, eg_sample_locs_2x[3]);158break;159case 4:160radeon_set_context_reg(cs, CM_R_028BF8_PA_SC_AA_SAMPLE_LOCS_PIXEL_X0Y0_0, eg_sample_locs_4x[0]);161radeon_set_context_reg(cs, CM_R_028C08_PA_SC_AA_SAMPLE_LOCS_PIXEL_X1Y0_0, eg_sample_locs_4x[1]);162radeon_set_context_reg(cs, CM_R_028C18_PA_SC_AA_SAMPLE_LOCS_PIXEL_X0Y1_0, eg_sample_locs_4x[2]);163radeon_set_context_reg(cs, CM_R_028C28_PA_SC_AA_SAMPLE_LOCS_PIXEL_X1Y1_0, eg_sample_locs_4x[3]);164break;165case 8:166radeon_set_context_reg_seq(cs, CM_R_028BF8_PA_SC_AA_SAMPLE_LOCS_PIXEL_X0Y0_0, 14);167radeon_emit(cs, cm_sample_locs_8x[0]);168radeon_emit(cs, cm_sample_locs_8x[4]);169radeon_emit(cs, 0);170radeon_emit(cs, 0);171radeon_emit(cs, cm_sample_locs_8x[1]);172radeon_emit(cs, cm_sample_locs_8x[5]);173radeon_emit(cs, 0);174radeon_emit(cs, 0);175radeon_emit(cs, cm_sample_locs_8x[2]);176radeon_emit(cs, cm_sample_locs_8x[6]);177radeon_emit(cs, 0);178radeon_emit(cs, 0);179radeon_emit(cs, cm_sample_locs_8x[3]);180radeon_emit(cs, cm_sample_locs_8x[7]);181break;182case 16:183radeon_set_context_reg_seq(cs, CM_R_028BF8_PA_SC_AA_SAMPLE_LOCS_PIXEL_X0Y0_0, 16);184radeon_emit(cs, cm_sample_locs_16x[0]);185radeon_emit(cs, cm_sample_locs_16x[4]);186radeon_emit(cs, cm_sample_locs_16x[8]);187radeon_emit(cs, cm_sample_locs_16x[12]);188radeon_emit(cs, cm_sample_locs_16x[1]);189radeon_emit(cs, cm_sample_locs_16x[5]);190radeon_emit(cs, cm_sample_locs_16x[9]);191radeon_emit(cs, cm_sample_locs_16x[13]);192radeon_emit(cs, cm_sample_locs_16x[2]);193radeon_emit(cs, cm_sample_locs_16x[6]);194radeon_emit(cs, cm_sample_locs_16x[10]);195radeon_emit(cs, cm_sample_locs_16x[14]);196radeon_emit(cs, cm_sample_locs_16x[3]);197radeon_emit(cs, cm_sample_locs_16x[7]);198radeon_emit(cs, cm_sample_locs_16x[11]);199radeon_emit(cs, cm_sample_locs_16x[15]);200break;201}202}203204void cayman_emit_msaa_state(struct radeon_cmdbuf *cs, int nr_samples,205int ps_iter_samples, int overrast_samples)206{207int setup_samples = nr_samples > 1 ? nr_samples :208overrast_samples > 1 ? overrast_samples : 0;209/* Required by OpenGL line rasterization.210*211* TODO: We should also enable perpendicular endcaps for AA lines,212* but that requires implementing line stippling in the pixel213* shader. SC can only do line stippling with axis-aligned214* endcaps.215*/216unsigned sc_line_cntl = S_028BDC_DX10_DIAMOND_TEST_ENA(1);217unsigned sc_mode_cntl_1 =218EG_S_028A4C_FORCE_EOV_CNTDWN_ENABLE(1) |219EG_S_028A4C_FORCE_EOV_REZ_ENABLE(1);220221if (nr_samples > 1) {222cayman_emit_msaa_sample_locs(cs, nr_samples);223}224225if (setup_samples > 1) {226/* indexed by log2(nr_samples) */227const unsigned max_dist[] = {2280,229eg_max_dist_2x,230eg_max_dist_4x,231cm_max_dist_8x,232cm_max_dist_16x233};234unsigned log_samples = util_logbase2(setup_samples);235unsigned log_ps_iter_samples =236util_logbase2(util_next_power_of_two(ps_iter_samples));237238radeon_set_context_reg_seq(cs, CM_R_028BDC_PA_SC_LINE_CNTL, 2);239radeon_emit(cs, sc_line_cntl |240S_028BDC_EXPAND_LINE_WIDTH(1)); /* CM_R_028BDC_PA_SC_LINE_CNTL */241radeon_emit(cs, S_028BE0_MSAA_NUM_SAMPLES(log_samples) |242S_028BE0_MAX_SAMPLE_DIST(max_dist[log_samples]) |243S_028BE0_MSAA_EXPOSED_SAMPLES(log_samples)); /* CM_R_028BE0_PA_SC_AA_CONFIG */244245if (nr_samples > 1) {246radeon_set_context_reg(cs, CM_R_028804_DB_EQAA,247S_028804_MAX_ANCHOR_SAMPLES(log_samples) |248S_028804_PS_ITER_SAMPLES(log_ps_iter_samples) |249S_028804_MASK_EXPORT_NUM_SAMPLES(log_samples) |250S_028804_ALPHA_TO_MASK_NUM_SAMPLES(log_samples) |251S_028804_HIGH_QUALITY_INTERSECTIONS(1) |252S_028804_STATIC_ANCHOR_ASSOCIATIONS(1));253radeon_set_context_reg(cs, EG_R_028A4C_PA_SC_MODE_CNTL_1,254EG_S_028A4C_PS_ITER_SAMPLE(ps_iter_samples > 1) |255sc_mode_cntl_1);256} else if (overrast_samples > 1) {257radeon_set_context_reg(cs, CM_R_028804_DB_EQAA,258S_028804_HIGH_QUALITY_INTERSECTIONS(1) |259S_028804_STATIC_ANCHOR_ASSOCIATIONS(1) |260S_028804_OVERRASTERIZATION_AMOUNT(log_samples));261radeon_set_context_reg(cs, EG_R_028A4C_PA_SC_MODE_CNTL_1,262sc_mode_cntl_1);263}264} else {265radeon_set_context_reg_seq(cs, CM_R_028BDC_PA_SC_LINE_CNTL, 2);266radeon_emit(cs, sc_line_cntl); /* CM_R_028BDC_PA_SC_LINE_CNTL */267radeon_emit(cs, 0); /* CM_R_028BE0_PA_SC_AA_CONFIG */268269radeon_set_context_reg(cs, CM_R_028804_DB_EQAA,270S_028804_HIGH_QUALITY_INTERSECTIONS(1) |271S_028804_STATIC_ANCHOR_ASSOCIATIONS(1));272radeon_set_context_reg(cs, EG_R_028A4C_PA_SC_MODE_CNTL_1,273sc_mode_cntl_1);274}275}276277278