Path: blob/21.2-virgl/src/intel/isl/isl_emit_depth_stencil.c
4547 views
/*1* Copyright 2016 Intel Corporation2*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, ARISING19* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS20* IN THE SOFTWARE.21*/2223#include <stdint.h>2425#define __gen_address_type uint64_t26#define __gen_user_data void2728static uint64_t29__gen_combine_address(__attribute__((unused)) void *data,30__attribute__((unused)) void *loc, uint64_t addr,31uint32_t delta)32{33return addr + delta;34}3536#include "genxml/gen_macros.h"37#include "genxml/genX_pack.h"3839#include "isl_priv.h"4041static const uint32_t isl_encode_ds_surftype[] = {42#if GFX_VER >= 943/* From the SKL PRM, "3DSTATE_DEPTH_STENCIL::SurfaceType":44*45* "If depth/stencil is enabled with 1D render target, depth/stencil46* surface type needs to be set to 2D surface type and height set to 1.47* Depth will use (legacy) TileY and stencil will use TileW. For this48* case only, the Surface Type of the depth buffer can be 2D while the49* Surface Type of the render target(s) are 1D, representing an50* exception to a programming note above.51*/52[ISL_SURF_DIM_1D] = SURFTYPE_2D,53#else54[ISL_SURF_DIM_1D] = SURFTYPE_1D,55#endif56[ISL_SURF_DIM_2D] = SURFTYPE_2D,57[ISL_SURF_DIM_3D] = SURFTYPE_3D,58};5960void61isl_genX(emit_depth_stencil_hiz_s)(const struct isl_device *dev, void *batch,62const struct isl_depth_stencil_hiz_emit_info *restrict info)63{64struct GENX(3DSTATE_DEPTH_BUFFER) db = {65GENX(3DSTATE_DEPTH_BUFFER_header),66};6768if (info->depth_surf) {69db.SurfaceType = isl_encode_ds_surftype[info->depth_surf->dim];70db.SurfaceFormat = isl_surf_get_depth_format(dev, info->depth_surf);71db.Width = info->depth_surf->logical_level0_px.width - 1;72db.Height = info->depth_surf->logical_level0_px.height - 1;73if (db.SurfaceType == SURFTYPE_3D)74db.Depth = info->depth_surf->logical_level0_px.depth - 1;75} else if (info->stencil_surf) {76db.SurfaceType = isl_encode_ds_surftype[info->stencil_surf->dim];77db.SurfaceFormat = D32_FLOAT;78db.Width = info->stencil_surf->logical_level0_px.width - 1;79db.Height = info->stencil_surf->logical_level0_px.height - 1;80if (db.SurfaceType == SURFTYPE_3D)81db.Depth = info->stencil_surf->logical_level0_px.depth - 1;82} else {83db.SurfaceType = SURFTYPE_NULL;84db.SurfaceFormat = D32_FLOAT;85}8687if (info->depth_surf || info->stencil_surf) {88/* These are based entirely on the view */89db.RenderTargetViewExtent = info->view->array_len - 1;90db.LOD = info->view->base_level;91db.MinimumArrayElement = info->view->base_array_layer;9293/* From the Haswell PRM docs for 3DSTATE_DEPTH_BUFFER::Depth94*95* "This field specifies the total number of levels for a volume96* texture or the number of array elements allowed to be accessed97* starting at the Minimum Array Element for arrayed surfaces. If the98* volume texture is MIP-mapped, this field specifies the depth of99* the base MIP level."100*101* For 3D surfaces, we set it to the correct depth above. For non-3D102* surfaces, this is the same as RenderTargetViewExtent.103*/104if (db.SurfaceType != SURFTYPE_3D)105db.Depth = db.RenderTargetViewExtent;106}107108if (info->depth_surf) {109#if GFX_VER >= 7110db.DepthWriteEnable = true;111#endif112db.SurfaceBaseAddress = info->depth_address;113#if GFX_VER >= 6114db.MOCS = info->mocs;115#endif116117#if GFX_VER <= 6118db.TiledSurface = info->depth_surf->tiling != ISL_TILING_LINEAR;119db.TileWalk = info->depth_surf->tiling == ISL_TILING_Y0 ? TILEWALK_YMAJOR :120TILEWALK_XMAJOR;121db.MIPMapLayoutMode = MIPLAYOUT_BELOW;122#endif123124db.SurfacePitch = info->depth_surf->row_pitch_B - 1;125#if GFX_VER >= 8126db.SurfaceQPitch =127isl_surf_get_array_pitch_el_rows(info->depth_surf) >> 2;128#endif129130#if GFX_VER >= 12131db.ControlSurfaceEnable = db.DepthBufferCompressionEnable =132isl_aux_usage_has_ccs(info->hiz_usage);133#endif134}135136#if GFX_VER == 5 || GFX_VER == 6137const bool separate_stencil =138info->stencil_surf && info->stencil_surf->format == ISL_FORMAT_R8_UINT;139if (separate_stencil || info->hiz_usage == ISL_AUX_USAGE_HIZ) {140assert(ISL_DEV_USE_SEPARATE_STENCIL(dev));141db.SeparateStencilBufferEnable = true;142db.HierarchicalDepthBufferEnable = true;143}144#endif145146#if GFX_VER >= 6147struct GENX(3DSTATE_STENCIL_BUFFER) sb = {148GENX(3DSTATE_STENCIL_BUFFER_header),149};150#else151# define sb db152#endif153154if (info->stencil_surf) {155#if GFX_VER >= 7 && GFX_VER < 12156db.StencilWriteEnable = true;157#endif158#if GFX_VER >= 12159sb.StencilWriteEnable = true;160sb.SurfaceType = SURFTYPE_2D;161sb.Width = info->stencil_surf->logical_level0_px.width - 1;162sb.Height = info->stencil_surf->logical_level0_px.height - 1;163sb.Depth = sb.RenderTargetViewExtent = info->view->array_len - 1;164sb.SurfLOD = info->view->base_level;165sb.MinimumArrayElement = info->view->base_array_layer;166assert(info->stencil_aux_usage == ISL_AUX_USAGE_NONE ||167info->stencil_aux_usage == ISL_AUX_USAGE_STC_CCS);168sb.StencilCompressionEnable =169info->stencil_aux_usage == ISL_AUX_USAGE_STC_CCS;170sb.ControlSurfaceEnable = sb.StencilCompressionEnable;171#elif GFX_VERx10 >= 75172sb.StencilBufferEnable = true;173#endif174sb.SurfaceBaseAddress = info->stencil_address;175#if GFX_VER >= 6176sb.MOCS = info->mocs;177#endif178sb.SurfacePitch = info->stencil_surf->row_pitch_B - 1;179#if GFX_VER >= 8180sb.SurfaceQPitch =181isl_surf_get_array_pitch_el_rows(info->stencil_surf) >> 2;182#endif183} else {184#if GFX_VER >= 12185sb.SurfaceType = SURFTYPE_NULL;186187/* The docs seem to indicate that if surf-type is null, then we may need188* to match the depth-buffer value for `Depth`. It may be a189* documentation bug, since the other fields don't require this.190*191* TODO: Confirm documentation and remove seeting of `Depth` if not192* required.193*/194sb.Depth = db.Depth;195#endif196}197198#if GFX_VER >= 6199struct GENX(3DSTATE_HIER_DEPTH_BUFFER) hiz = {200GENX(3DSTATE_HIER_DEPTH_BUFFER_header),201};202struct GENX(3DSTATE_CLEAR_PARAMS) clear = {203GENX(3DSTATE_CLEAR_PARAMS_header),204};205206assert(info->hiz_usage == ISL_AUX_USAGE_NONE ||207isl_aux_usage_has_hiz(info->hiz_usage));208if (isl_aux_usage_has_hiz(info->hiz_usage)) {209assert(GFX_VER >= 12 || info->hiz_usage == ISL_AUX_USAGE_HIZ);210db.HierarchicalDepthBufferEnable = true;211212hiz.SurfaceBaseAddress = info->hiz_address;213hiz.MOCS = info->mocs;214hiz.SurfacePitch = info->hiz_surf->row_pitch_B - 1;215#if GFX_VER >= 12216hiz.HierarchicalDepthBufferWriteThruEnable =217info->hiz_usage == ISL_AUX_USAGE_HIZ_CCS_WT;218219/* The bspec docs for this bit are fairly unclear about exactly what is220* and isn't supported with HiZ write-through. It's fairly clear that221* you can't sample from a multisampled depth buffer with CCS. This222* limitation isn't called out explicitly but the docs for the CCS_E223* value of RENDER_SURFACE_STATE::AuxiliarySurfaceMode say:224*225* "If Number of multisamples > 1, programming this value means MSAA226* compression is enabled for that surface. Auxillary surface is MSC227* with tile y."228*229* Since this interpretation ignores whether the surface is230* depth/stencil or not and since multisampled depth buffers use231* ISL_MSAA_LAYOUT_INTERLEAVED which is incompatible with MCS232* compression, this means that we can't even specify MSAA depth CCS in233* RENDER_SURFACE_STATE::AuxiliarySurfaceMode. The BSpec also says, for234* 3DSTATE_HIER_DEPTH_BUFFER::HierarchicalDepthBufferWriteThruEnable,235*236* "This bit must NOT be set for >1x MSAA modes, since sampler237* doesn't support sampling from >1x MSAA depth buffer."238*239* Again, this is all focused around what the sampler can do and not240* what the depth hardware can do.241*242* Reading even more internal docs which can't be quoted here makes it243* pretty clear that, even if it's not currently called out in the244* BSpec, HiZ+CCS write-through isn't intended to work with MSAA and we245* shouldn't try to use it. Treat it as if it's disallowed even if the246* BSpec doesn't explicitly document that.247*/248if (hiz.HierarchicalDepthBufferWriteThruEnable)249assert(info->depth_surf->samples == 1);250#endif251252#if GFX_VER >= 8253/* From the SKL PRM Vol2a:254*255* The interpretation of this field is dependent on Surface Type256* as follows:257* - SURFTYPE_1D: distance in pixels between array slices258* - SURFTYPE_2D/CUBE: distance in rows between array slices259* - SURFTYPE_3D: distance in rows between R - slices260*261* Unfortunately, the docs aren't 100% accurate here. They fail to262* mention that the 1-D rule only applies to linear 1-D images.263* Since depth and HiZ buffers are always tiled, they are treated as264* 2-D images. Prior to Sky Lake, this field is always in rows.265*/266hiz.SurfaceQPitch =267isl_surf_get_array_pitch_sa_rows(info->hiz_surf) >> 2;268#endif269270clear.DepthClearValueValid = true;271#if GFX_VER >= 8272clear.DepthClearValue = info->depth_clear_value;273#else274switch (info->depth_surf->format) {275case ISL_FORMAT_R32_FLOAT: {276union { float f; uint32_t u; } fu;277fu.f = info->depth_clear_value;278clear.DepthClearValue = fu.u;279break;280}281case ISL_FORMAT_R24_UNORM_X8_TYPELESS:282clear.DepthClearValue = info->depth_clear_value * ((1u << 24) - 1);283break;284case ISL_FORMAT_R16_UNORM:285clear.DepthClearValue = info->depth_clear_value * ((1u << 16) - 1);286break;287default:288unreachable("Invalid depth type");289}290#endif291}292#endif /* GFX_VER >= 6 */293294/* Pack everything into the batch */295uint32_t *dw = batch;296GENX(3DSTATE_DEPTH_BUFFER_pack)(NULL, dw, &db);297dw += GENX(3DSTATE_DEPTH_BUFFER_length);298299#if GFX_VER >= 6300GENX(3DSTATE_STENCIL_BUFFER_pack)(NULL, dw, &sb);301dw += GENX(3DSTATE_STENCIL_BUFFER_length);302303GENX(3DSTATE_HIER_DEPTH_BUFFER_pack)(NULL, dw, &hiz);304dw += GENX(3DSTATE_HIER_DEPTH_BUFFER_length);305306#if GFX_VERx10 == 120307/* Wa_14010455700308*309* To avoid sporadic corruptions “Set 0x7010[9] when Depth Buffer Surface310* Format is D16_UNORM , surface type is not NULL & 1X_MSAA”.311*/312bool enable_14010455700 =313info->depth_surf && info->depth_surf->samples == 1 &&314db.SurfaceType != SURFTYPE_NULL && db.SurfaceFormat == D16_UNORM;315struct GENX(COMMON_SLICE_CHICKEN1) chicken1 = {316.HIZPlaneOptimizationdisablebit = enable_14010455700,317.HIZPlaneOptimizationdisablebitMask = true,318};319uint32_t chicken1_dw;320GENX(COMMON_SLICE_CHICKEN1_pack)(NULL, &chicken1_dw, &chicken1);321322struct GENX(MI_LOAD_REGISTER_IMM) lri = {323GENX(MI_LOAD_REGISTER_IMM_header),324.RegisterOffset = GENX(COMMON_SLICE_CHICKEN1_num),325.DataDWord = chicken1_dw,326};327GENX(MI_LOAD_REGISTER_IMM_pack)(NULL, dw, &lri);328dw += GENX(MI_LOAD_REGISTER_IMM_length);329330/* Wa_1806527549331*332* Set HIZ_CHICKEN (7018h) bit 13 = 1 when depth buffer is D16_UNORM.333*/334struct GENX(HIZ_CHICKEN) hiz_chicken = {335.HZDepthTestLEGEOptimizationDisable = db.SurfaceFormat == D16_UNORM,336.HZDepthTestLEGEOptimizationDisableMask = true,337};338uint32_t hiz_chicken_dw;339GENX(HIZ_CHICKEN_pack)(NULL, &hiz_chicken_dw, &hiz_chicken);340341struct GENX(MI_LOAD_REGISTER_IMM) lri2 = {342GENX(MI_LOAD_REGISTER_IMM_header),343.RegisterOffset = GENX(HIZ_CHICKEN_num),344.DataDWord = hiz_chicken_dw,345};346GENX(MI_LOAD_REGISTER_IMM_pack)(NULL, dw, &lri2);347dw += GENX(MI_LOAD_REGISTER_IMM_length);348#endif349350GENX(3DSTATE_CLEAR_PARAMS_pack)(NULL, dw, &clear);351dw += GENX(3DSTATE_CLEAR_PARAMS_length);352#endif353}354355356