Path: blob/21.2-virgl/src/intel/isl/isl_surface_state.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"4041#if GFX_VER >= 842static const uint8_t isl_encode_halign[] = {43[4] = HALIGN4,44[8] = HALIGN8,45[16] = HALIGN16,46};47#elif GFX_VER >= 748static const uint8_t isl_encode_halign[] = {49[4] = HALIGN_4,50[8] = HALIGN_8,51};52#endif5354#if GFX_VER >= 855static const uint8_t isl_encode_valign[] = {56[4] = VALIGN4,57[8] = VALIGN8,58[16] = VALIGN16,59};60#elif GFX_VER >= 661static const uint8_t isl_encode_valign[] = {62[2] = VALIGN_2,63[4] = VALIGN_4,64};65#endif6667#if GFX_VER >= 868static const uint8_t isl_encode_tiling[] = {69[ISL_TILING_LINEAR] = LINEAR,70[ISL_TILING_X] = XMAJOR,71[ISL_TILING_Y0] = YMAJOR,72[ISL_TILING_Yf] = YMAJOR,73[ISL_TILING_Ys] = YMAJOR,74#if GFX_VER <= 1175[ISL_TILING_W] = WMAJOR,76#endif77};78#endif7980#if GFX_VER >= 781static const uint32_t isl_encode_multisample_layout[] = {82[ISL_MSAA_LAYOUT_NONE] = MSFMT_MSS,83[ISL_MSAA_LAYOUT_INTERLEAVED] = MSFMT_DEPTH_STENCIL,84[ISL_MSAA_LAYOUT_ARRAY] = MSFMT_MSS,85};86#endif8788#if GFX_VER >= 1289static const uint32_t isl_encode_aux_mode[] = {90[ISL_AUX_USAGE_NONE] = AUX_NONE,91[ISL_AUX_USAGE_MC] = AUX_NONE,92[ISL_AUX_USAGE_MCS] = AUX_CCS_E,93[ISL_AUX_USAGE_GFX12_CCS_E] = AUX_CCS_E,94[ISL_AUX_USAGE_CCS_E] = AUX_CCS_E,95[ISL_AUX_USAGE_HIZ_CCS_WT] = AUX_CCS_E,96[ISL_AUX_USAGE_MCS_CCS] = AUX_MCS_LCE,97[ISL_AUX_USAGE_STC_CCS] = AUX_CCS_E,98};99#elif GFX_VER >= 9100static const uint32_t isl_encode_aux_mode[] = {101[ISL_AUX_USAGE_NONE] = AUX_NONE,102[ISL_AUX_USAGE_HIZ] = AUX_HIZ,103[ISL_AUX_USAGE_MCS] = AUX_CCS_D,104[ISL_AUX_USAGE_CCS_D] = AUX_CCS_D,105[ISL_AUX_USAGE_CCS_E] = AUX_CCS_E,106};107#elif GFX_VER >= 8108static const uint32_t isl_encode_aux_mode[] = {109[ISL_AUX_USAGE_NONE] = AUX_NONE,110[ISL_AUX_USAGE_HIZ] = AUX_HIZ,111[ISL_AUX_USAGE_MCS] = AUX_MCS,112[ISL_AUX_USAGE_CCS_D] = AUX_MCS,113};114#endif115116static uint8_t117get_surftype(enum isl_surf_dim dim, isl_surf_usage_flags_t usage)118{119switch (dim) {120default:121unreachable("bad isl_surf_dim");122case ISL_SURF_DIM_1D:123assert(!(usage & ISL_SURF_USAGE_CUBE_BIT));124return SURFTYPE_1D;125case ISL_SURF_DIM_2D:126if ((usage & ISL_SURF_USAGE_CUBE_BIT) &&127(usage & ISL_SURF_USAGE_TEXTURE_BIT)) {128/* We need SURFTYPE_CUBE to make cube sampling work */129return SURFTYPE_CUBE;130} else {131/* Everything else (render and storage) treat cubes as plain132* 2D array textures133*/134return SURFTYPE_2D;135}136case ISL_SURF_DIM_3D:137assert(!(usage & ISL_SURF_USAGE_CUBE_BIT));138return SURFTYPE_3D;139}140}141142/**143* Get the horizontal and vertical alignment in the units expected by the144* hardware. Note that this does NOT give you the actual hardware enum values145* but an index into the isl_encode_[hv]align arrays above.146*/147UNUSED static struct isl_extent3d148get_image_alignment(const struct isl_surf *surf)149{150if (GFX_VER >= 9) {151if (isl_tiling_is_std_y(surf->tiling) ||152surf->dim_layout == ISL_DIM_LAYOUT_GFX9_1D) {153/* The hardware ignores the alignment values. Anyway, the surface's154* true alignment is likely outside the enum range of HALIGN* and155* VALIGN*.156*/157return isl_extent3d(4, 4, 1);158} else {159/* In Skylake, RENDER_SUFFACE_STATE.SurfaceVerticalAlignment is in units160* of surface elements (not pixels nor samples). For compressed formats,161* a "surface element" is defined as a compression block. For example,162* if SurfaceVerticalAlignment is VALIGN_4 and SurfaceFormat is an ETC2163* format (ETC2 has a block height of 4), then the vertical alignment is164* 4 compression blocks or, equivalently, 16 pixels.165*/166return isl_surf_get_image_alignment_el(surf);167}168} else {169/* Pre-Skylake, RENDER_SUFFACE_STATE.SurfaceVerticalAlignment is in170* units of surface samples. For example, if SurfaceVerticalAlignment171* is VALIGN_4 and the surface is singlesampled, then for any surface172* format (compressed or not) the vertical alignment is173* 4 pixels.174*/175return isl_surf_get_image_alignment_sa(surf);176}177}178179#if GFX_VER >= 8180static uint32_t181get_qpitch(const struct isl_surf *surf)182{183switch (surf->dim_layout) {184default:185unreachable("Bad isl_surf_dim");186case ISL_DIM_LAYOUT_GFX4_2D:187if (GFX_VER >= 9) {188if (surf->dim == ISL_SURF_DIM_3D && surf->tiling == ISL_TILING_W) {189/* This is rather annoying and completely undocumented. It190* appears that the hardware has a bug (or undocumented feature)191* regarding stencil buffers most likely related to the way192* W-tiling is handled as modified Y-tiling. If you bind a 3-D193* stencil buffer normally, and use texelFetch on it, the z or194* array index will get implicitly multiplied by 2 for no obvious195* reason. The fix appears to be to divide qpitch by 2 for196* W-tiled surfaces.197*/198return isl_surf_get_array_pitch_el_rows(surf) / 2;199} else {200return isl_surf_get_array_pitch_el_rows(surf);201}202} else {203/* From the Broadwell PRM for RENDER_SURFACE_STATE.QPitch204*205* "This field must be set to an integer multiple of the Surface206* Vertical Alignment. For compressed textures (BC*, FXT1,207* ETC*, and EAC* Surface Formats), this field is in units of208* rows in the uncompressed surface, and must be set to an209* integer multiple of the vertical alignment parameter "j"210* defined in the Common Surface Formats section."211*/212return isl_surf_get_array_pitch_sa_rows(surf);213}214case ISL_DIM_LAYOUT_GFX9_1D:215/* QPitch is usually expressed as rows of surface elements (where216* a surface element is an compression block or a single surface217* sample). Skylake 1D is an outlier.218*219* From the Skylake BSpec >> Memory Views >> Common Surface220* Formats >> Surface Layout and Tiling >> 1D Surfaces:221*222* Surface QPitch specifies the distance in pixels between array223* slices.224*/225return isl_surf_get_array_pitch_el(surf);226case ISL_DIM_LAYOUT_GFX4_3D:227/* QPitch doesn't make sense for ISL_DIM_LAYOUT_GFX4_3D since it uses a228* different pitch at each LOD. Also, the QPitch field is ignored for229* these surfaces. From the Broadwell PRM documentation for QPitch:230*231* This field specifies the distance in rows between array slices. It232* is used only in the following cases:233* - Surface Array is enabled OR234* - Number of Mulitsamples is not NUMSAMPLES_1 and Multisampled235* Surface Storage Format set to MSFMT_MSS OR236* - Surface Type is SURFTYPE_CUBE237*238* None of the three conditions above can possibly apply to a 3D surface239* so it is safe to just set QPitch to 0.240*/241return 0;242}243}244#endif /* GFX_VER >= 8 */245246void247isl_genX(surf_fill_state_s)(const struct isl_device *dev, void *state,248const struct isl_surf_fill_state_info *restrict info)249{250struct GENX(RENDER_SURFACE_STATE) s = { 0 };251252s.SurfaceType = get_surftype(info->surf->dim, info->view->usage);253254if (info->view->usage & ISL_SURF_USAGE_RENDER_TARGET_BIT)255assert(isl_format_supports_rendering(dev->info, info->view->format));256else if (info->view->usage & ISL_SURF_USAGE_TEXTURE_BIT)257assert(isl_format_supports_sampling(dev->info, info->view->format));258259/* From the Sky Lake PRM Vol. 2d, RENDER_SURFACE_STATE::SurfaceFormat260*261* This field cannot be a compressed (BC*, DXT*, FXT*, ETC*, EAC*)262* format if the Surface Type is SURFTYPE_1D263*/264if (info->surf->dim == ISL_SURF_DIM_1D)265assert(!isl_format_is_compressed(info->view->format));266267if (isl_format_is_compressed(info->surf->format)) {268/* You're not allowed to make a view of a compressed format with any269* format other than the surface format. None of the userspace APIs270* allow for this directly and doing so would mess up a number of271* surface parameters such as Width, Height, and alignments. Ideally,272* we'd like to assert that the two formats match. However, we have an273* S3TC workaround that requires us to do reinterpretation. So assert274* that they're at least the same bpb and block size.275*/276ASSERTED const struct isl_format_layout *surf_fmtl =277isl_format_get_layout(info->surf->format);278ASSERTED const struct isl_format_layout *view_fmtl =279isl_format_get_layout(info->surf->format);280assert(surf_fmtl->bpb == view_fmtl->bpb);281assert(surf_fmtl->bw == view_fmtl->bw);282assert(surf_fmtl->bh == view_fmtl->bh);283}284285s.SurfaceFormat = info->view->format;286287#if GFX_VER >= 12288/* The BSpec description of this field says:289*290* "This bit field, when set, indicates if the resource is created as291* Depth/Stencil resource."292*293* "SW must set this bit for any resource that was created with294* Depth/Stencil resource flag. Setting this bit allows HW to properly295* interpret the data-layout for various cases. For any resource that's296* created without Depth/Stencil resource flag, it must be reset."297*298* Even though the docs for this bit seem to imply that it's required for299* anything which might have been used for depth/stencil, empirical300* evidence suggests that it only affects CCS compression usage. There are301* a few things which back this up:302*303* 1. The docs are also pretty clear that this bit was added as part304* of enabling Gfx12 depth/stencil lossless compression.305*306* 2. The only new difference between depth/stencil and color images on307* Gfx12 (where the bit was added) is how they treat CCS compression.308* All other differences such as alignment requirements and MSAA layout309* are already covered by other bits.310*311* Under these assumptions, it makes sense for ISL to model this bit as312* being an extension of AuxiliarySurfaceMode where STC_CCS and HIZ_CCS_WT313* are indicated by AuxiliarySurfaceMode == CCS_E and DepthStencilResource314* == true.315*/316s.DepthStencilResource = info->aux_usage == ISL_AUX_USAGE_HIZ_CCS_WT ||317info->aux_usage == ISL_AUX_USAGE_STC_CCS;318#endif319320#if GFX_VER <= 5321s.ColorBufferComponentWriteDisables = info->write_disables;322s.ColorBlendEnable = info->blend_enable;323#else324assert(info->write_disables == 0);325#endif326327#if GFX_VERx10 == 75328s.IntegerSurfaceFormat =329isl_format_has_int_channel((enum isl_format) s.SurfaceFormat);330#endif331332assert(info->surf->logical_level0_px.width > 0 &&333info->surf->logical_level0_px.height > 0);334335s.Width = info->surf->logical_level0_px.width - 1;336s.Height = info->surf->logical_level0_px.height - 1;337338/* In the gfx6 PRM Volume 1 Part 1: Graphics Core, Section 7.18.3.7.1339* (Surface Arrays For all surfaces other than separate stencil buffer):340*341* "[DevSNB] Errata: Sampler MSAA Qpitch will be 4 greater than the value342* calculated in the equation above , for every other odd Surface Height343* starting from 1 i.e. 1,5,9,13"344*345* Since this Qpitch errata only impacts the sampler, we have to adjust the346* input for the rendering surface to achieve the same qpitch. For the347* affected heights, we increment the height by 1 for the rendering348* surface.349*/350if (GFX_VER == 6 && (info->view->usage & ISL_SURF_USAGE_RENDER_TARGET_BIT) &&351info->surf->samples > 1 &&352(info->surf->logical_level0_px.height % 4) == 1)353s.Height++;354355switch (s.SurfaceType) {356case SURFTYPE_1D:357case SURFTYPE_2D:358/* From the Ivy Bridge PRM >> RENDER_SURFACE_STATE::MinimumArrayElement:359*360* "If Number of Multisamples is not MULTISAMPLECOUNT_1, this field361* must be set to zero if this surface is used with sampling engine362* messages."363*364* This restriction appears to exist only on Ivy Bridge.365*/366if (GFX_VERx10 == 70 && !ISL_DEV_IS_BAYTRAIL(dev) &&367(info->view->usage & ISL_SURF_USAGE_TEXTURE_BIT) &&368info->surf->samples > 1)369assert(info->view->base_array_layer == 0);370371s.MinimumArrayElement = info->view->base_array_layer;372373/* From the Broadwell PRM >> RENDER_SURFACE_STATE::Depth:374*375* For SURFTYPE_1D, 2D, and CUBE: The range of this field is reduced376* by one for each increase from zero of Minimum Array Element. For377* example, if Minimum Array Element is set to 1024 on a 2D surface,378* the range of this field is reduced to [0,1023].379*380* In other words, 'Depth' is the number of array layers.381*/382s.Depth = info->view->array_len - 1;383384/* From the Broadwell PRM >> RENDER_SURFACE_STATE::RenderTargetViewExtent:385*386* For Render Target and Typed Dataport 1D and 2D Surfaces:387* This field must be set to the same value as the Depth field.388*/389if (info->view->usage & (ISL_SURF_USAGE_RENDER_TARGET_BIT |390ISL_SURF_USAGE_STORAGE_BIT))391s.RenderTargetViewExtent = s.Depth;392break;393case SURFTYPE_CUBE:394s.MinimumArrayElement = info->view->base_array_layer;395/* Same as SURFTYPE_2D, but divided by 6 */396s.Depth = info->view->array_len / 6 - 1;397if (info->view->usage & (ISL_SURF_USAGE_RENDER_TARGET_BIT |398ISL_SURF_USAGE_STORAGE_BIT))399s.RenderTargetViewExtent = s.Depth;400break;401case SURFTYPE_3D:402/* From the Broadwell PRM >> RENDER_SURFACE_STATE::Depth:403*404* If the volume texture is MIP-mapped, this field specifies the405* depth of the base MIP level.406*/407s.Depth = info->surf->logical_level0_px.depth - 1;408409/* From the Broadwell PRM >> RENDER_SURFACE_STATE::RenderTargetViewExtent:410*411* For Render Target and Typed Dataport 3D Surfaces: This field412* indicates the extent of the accessible 'R' coordinates minus 1 on413* the LOD currently being rendered to.414*415* The docs specify that this only matters for render targets and416* surfaces used with typed dataport messages. Prior to Ivy Bridge, the417* Depth field has more bits than RenderTargetViewExtent so we can have418* textures with more levels than we can render to. In order to prevent419* assert-failures in the packing function below, we only set the field420* when it's actually going to be used by the hardware.421*422* Similaraly, the MinimumArrayElement field is ignored by all hardware423* prior to Sky Lake when texturing and we want it set to 0 anyway.424* Since it's already initialized to 0, we can just leave it alone for425* texture surfaces.426*/427if (info->view->usage & (ISL_SURF_USAGE_RENDER_TARGET_BIT |428ISL_SURF_USAGE_STORAGE_BIT)) {429s.MinimumArrayElement = info->view->base_array_layer;430s.RenderTargetViewExtent = info->view->array_len - 1;431}432break;433default:434unreachable("bad SurfaceType");435}436437#if GFX_VER >= 12438/* Wa_1806565034: Only set SurfaceArray if arrayed surface is > 1. */439s.SurfaceArray = info->surf->dim != ISL_SURF_DIM_3D &&440info->view->array_len > 1;441#elif GFX_VER >= 7442s.SurfaceArray = info->surf->dim != ISL_SURF_DIM_3D;443#endif444445if (info->view->usage & ISL_SURF_USAGE_RENDER_TARGET_BIT) {446/* For render target surfaces, the hardware interprets field447* MIPCount/LOD as LOD. The Broadwell PRM says:448*449* MIPCountLOD defines the LOD that will be rendered into.450* SurfaceMinLOD is ignored.451*/452s.MIPCountLOD = info->view->base_level;453s.SurfaceMinLOD = 0;454} else {455/* For non render target surfaces, the hardware interprets field456* MIPCount/LOD as MIPCount. The range of levels accessible by the457* sampler engine is [SurfaceMinLOD, SurfaceMinLOD + MIPCountLOD].458*/459s.SurfaceMinLOD = info->view->base_level;460s.MIPCountLOD = MAX(info->view->levels, 1) - 1;461}462463#if GFX_VER >= 9464/* We don't use miptails yet. The PRM recommends that you set "Mip Tail465* Start LOD" to 15 to prevent the hardware from trying to use them.466*/467s.TiledResourceMode = NONE;468s.MipTailStartLOD = 15;469#endif470471#if GFX_VER >= 6472const struct isl_extent3d image_align = get_image_alignment(info->surf);473s.SurfaceVerticalAlignment = isl_encode_valign[image_align.height];474#if GFX_VER >= 7475s.SurfaceHorizontalAlignment = isl_encode_halign[image_align.width];476#endif477#endif478479if (info->surf->dim_layout == ISL_DIM_LAYOUT_GFX9_1D) {480/* For gfx9 1-D textures, surface pitch is ignored */481s.SurfacePitch = 0;482} else {483s.SurfacePitch = info->surf->row_pitch_B - 1;484}485486#if GFX_VER >= 8487s.SurfaceQPitch = get_qpitch(info->surf) >> 2;488#elif GFX_VER == 7489s.SurfaceArraySpacing = info->surf->array_pitch_span ==490ISL_ARRAY_PITCH_SPAN_COMPACT;491#endif492493#if GFX_VER >= 8494assert(GFX_VER < 12 || info->surf->tiling != ISL_TILING_W);495s.TileMode = isl_encode_tiling[info->surf->tiling];496#else497s.TiledSurface = info->surf->tiling != ISL_TILING_LINEAR,498s.TileWalk = info->surf->tiling == ISL_TILING_Y0 ? TILEWALK_YMAJOR :499TILEWALK_XMAJOR,500#endif501502#if GFX_VER >= 8503s.RenderCacheReadWriteMode = WriteOnlyCache;504#else505s.RenderCacheReadWriteMode = 0;506#endif507508#if GFX_VER >= 11509/* We've seen dEQP failures when enabling this bit with UINT formats,510* which particularly affects blorp_copy() operations. It shouldn't511* have any effect on UINT textures anyway, so disable it for them.512*/513s.EnableUnormPathInColorPipe =514!isl_format_has_int_channel(info->view->format);515#endif516517s.CubeFaceEnablePositiveZ = 1;518s.CubeFaceEnableNegativeZ = 1;519s.CubeFaceEnablePositiveY = 1;520s.CubeFaceEnableNegativeY = 1;521s.CubeFaceEnablePositiveX = 1;522s.CubeFaceEnableNegativeX = 1;523524#if GFX_VER >= 6525s.NumberofMultisamples = ffs(info->surf->samples) - 1;526#if GFX_VER >= 7527s.MultisampledSurfaceStorageFormat =528isl_encode_multisample_layout[info->surf->msaa_layout];529#endif530#endif531532#if (GFX_VERx10 >= 75)533if (info->view->usage & ISL_SURF_USAGE_RENDER_TARGET_BIT)534assert(isl_swizzle_supports_rendering(dev->info, info->view->swizzle));535536s.ShaderChannelSelectRed = (enum GENX(ShaderChannelSelect)) info->view->swizzle.r;537s.ShaderChannelSelectGreen = (enum GENX(ShaderChannelSelect)) info->view->swizzle.g;538s.ShaderChannelSelectBlue = (enum GENX(ShaderChannelSelect)) info->view->swizzle.b;539s.ShaderChannelSelectAlpha = (enum GENX(ShaderChannelSelect)) info->view->swizzle.a;540#else541assert(isl_swizzle_is_identity(info->view->swizzle));542#endif543544s.SurfaceBaseAddress = info->address;545546#if GFX_VER >= 6547s.MOCS = info->mocs;548#endif549550#if GFX_VERx10 >= 45551if (info->x_offset_sa != 0 || info->y_offset_sa != 0) {552/* There are fairly strict rules about when the offsets can be used.553* These are mostly taken from the Sky Lake PRM documentation for554* RENDER_SURFACE_STATE.555*/556assert(info->surf->tiling != ISL_TILING_LINEAR);557assert(info->surf->dim == ISL_SURF_DIM_2D);558assert(isl_is_pow2(isl_format_get_layout(info->view->format)->bpb));559assert(info->surf->levels == 1);560assert(info->surf->logical_level0_px.array_len == 1);561assert(info->aux_usage == ISL_AUX_USAGE_NONE);562563if (GFX_VER >= 8) {564/* Broadwell added more rules. */565assert(info->surf->samples == 1);566if (isl_format_get_layout(info->view->format)->bpb == 8)567assert(info->x_offset_sa % 16 == 0);568if (isl_format_get_layout(info->view->format)->bpb == 16)569assert(info->x_offset_sa % 8 == 0);570}571572#if GFX_VER >= 7573s.SurfaceArray = false;574#endif575}576577const unsigned x_div = 4;578const unsigned y_div = GFX_VER >= 8 ? 4 : 2;579assert(info->x_offset_sa % x_div == 0);580assert(info->y_offset_sa % y_div == 0);581s.XOffset = info->x_offset_sa / x_div;582s.YOffset = info->y_offset_sa / y_div;583#else584assert(info->x_offset_sa == 0);585assert(info->y_offset_sa == 0);586#endif587588#if GFX_VER >= 7589if (info->aux_usage != ISL_AUX_USAGE_NONE) {590/* Check valid aux usages per-gen */591if (GFX_VER >= 12) {592assert(info->aux_usage == ISL_AUX_USAGE_MCS ||593info->aux_usage == ISL_AUX_USAGE_CCS_E ||594info->aux_usage == ISL_AUX_USAGE_GFX12_CCS_E ||595info->aux_usage == ISL_AUX_USAGE_MC ||596info->aux_usage == ISL_AUX_USAGE_HIZ_CCS_WT ||597info->aux_usage == ISL_AUX_USAGE_MCS_CCS ||598info->aux_usage == ISL_AUX_USAGE_STC_CCS);599} else if (GFX_VER >= 9) {600assert(info->aux_usage == ISL_AUX_USAGE_HIZ ||601info->aux_usage == ISL_AUX_USAGE_MCS ||602info->aux_usage == ISL_AUX_USAGE_CCS_D ||603info->aux_usage == ISL_AUX_USAGE_CCS_E);604} else if (GFX_VER >= 8) {605assert(info->aux_usage == ISL_AUX_USAGE_HIZ ||606info->aux_usage == ISL_AUX_USAGE_MCS ||607info->aux_usage == ISL_AUX_USAGE_CCS_D);608} else if (GFX_VER >= 7) {609assert(info->aux_usage == ISL_AUX_USAGE_MCS ||610info->aux_usage == ISL_AUX_USAGE_CCS_D);611}612613/* The docs don't appear to say anything whatsoever about compression614* and the data port. Testing seems to indicate that the data port615* completely ignores the AuxiliarySurfaceMode field.616*617* On gfx12 HDC supports compression.618*/619if (GFX_VER < 12)620assert(!(info->view->usage & ISL_SURF_USAGE_STORAGE_BIT));621622if (isl_surf_usage_is_depth(info->surf->usage))623assert(isl_aux_usage_has_hiz(info->aux_usage));624625if (isl_surf_usage_is_stencil(info->surf->usage))626assert(info->aux_usage == ISL_AUX_USAGE_STC_CCS);627628if (isl_aux_usage_has_hiz(info->aux_usage)) {629/* For Gfx8-10, there are some restrictions around sampling from HiZ.630* The Skylake PRM docs for RENDER_SURFACE_STATE::AuxiliarySurfaceMode631* say:632*633* "If this field is set to AUX_HIZ, Number of Multisamples must634* be MULTISAMPLECOUNT_1, and Surface Type cannot be SURFTYPE_3D."635*636* On Gfx12, the docs are a bit less obvious but the restriction is637* the same. The limitation isn't called out explicitly but the docs638* for the CCS_E value of RENDER_SURFACE_STATE::AuxiliarySurfaceMode639* say:640*641* "If Number of multisamples > 1, programming this value means642* MSAA compression is enabled for that surface. Auxillary surface643* is MSC with tile y."644*645* Since this interpretation ignores whether the surface is646* depth/stencil or not and since multisampled depth buffers use647* ISL_MSAA_LAYOUT_INTERLEAVED which is incompatible with MCS648* compression, this means that we can't even specify MSAA depth CCS649* in RENDER_SURFACE_STATE::AuxiliarySurfaceMode.650*/651assert(info->surf->samples == 1);652653/* The dimension must not be 3D */654assert(info->surf->dim != ISL_SURF_DIM_3D);655656/* The format must be one of the following: */657switch (info->view->format) {658case ISL_FORMAT_R32_FLOAT:659case ISL_FORMAT_R24_UNORM_X8_TYPELESS:660case ISL_FORMAT_R16_UNORM:661break;662default:663assert(!"Incompatible HiZ Sampling format");664break;665}666}667668#if GFX_VER >= 12669s.MemoryCompressionEnable = info->aux_usage == ISL_AUX_USAGE_MC;670#endif671#if GFX_VER >= 8672s.AuxiliarySurfaceMode = isl_encode_aux_mode[info->aux_usage];673#else674s.MCSEnable = true;675#endif676}677678/* The auxiliary buffer info is filled when it's useable by the HW.679*680* Starting with Gfx12, the only form of compression that can be used681* with RENDER_SURFACE_STATE which requires an aux surface is MCS.682* HiZ still requires a surface but the HiZ surface can only be683* accessed through 3DSTATE_HIER_DEPTH_BUFFER.684*685* On all earlier hardware, an aux surface is required for all forms686* of compression.687*/688if ((GFX_VER < 12 && info->aux_usage != ISL_AUX_USAGE_NONE) ||689(GFX_VER >= 12 && isl_aux_usage_has_mcs(info->aux_usage))) {690691assert(info->aux_surf != NULL);692693struct isl_tile_info tile_info;694isl_surf_get_tile_info(info->aux_surf, &tile_info);695uint32_t pitch_in_tiles =696info->aux_surf->row_pitch_B / tile_info.phys_extent_B.width;697698s.AuxiliarySurfaceBaseAddress = info->aux_address;699s.AuxiliarySurfacePitch = pitch_in_tiles - 1;700701#if GFX_VER >= 8702/* Auxiliary surfaces in ISL have compressed formats but the hardware703* doesn't expect our definition of the compression, it expects qpitch704* in units of samples on the main surface.705*/706s.AuxiliarySurfaceQPitch =707isl_surf_get_array_pitch_sa_rows(info->aux_surf) >> 2;708#endif709}710#endif711712#if GFX_VER >= 8 && GFX_VER < 11713/* From the CHV PRM, Volume 2d, page 321 (RENDER_SURFACE_STATE dword 0714* bit 9 "Sampler L2 Bypass Mode Disable" Programming Notes):715*716* This bit must be set for the following surface types: BC2_UNORM717* BC3_UNORM BC5_UNORM BC5_SNORM BC7_UNORM718*/719if (GFX_VER >= 9 || dev->info->is_cherryview) {720switch (info->view->format) {721case ISL_FORMAT_BC2_UNORM:722case ISL_FORMAT_BC3_UNORM:723case ISL_FORMAT_BC5_UNORM:724case ISL_FORMAT_BC5_SNORM:725case ISL_FORMAT_BC7_UNORM:726s.SamplerL2BypassModeDisable = true;727break;728default:729/* From the SKL PRM, Programming Note under Sampler Output Channel730* Mapping:731*732* If a surface has an associated HiZ Auxilliary surface, the733* Sampler L2 Bypass Mode Disable field in the RENDER_SURFACE_STATE734* must be set.735*/736if (GFX_VER >= 9 && info->aux_usage == ISL_AUX_USAGE_HIZ)737s.SamplerL2BypassModeDisable = true;738break;739}740}741#endif742743if (isl_aux_usage_has_fast_clears(info->aux_usage)) {744if (info->use_clear_address) {745#if GFX_VER >= 10746s.ClearValueAddressEnable = true;747s.ClearValueAddress = info->clear_address;748#else749unreachable("Gfx9 and earlier do not support indirect clear colors");750#endif751}752753#if GFX_VER == 11754/*755* From BXML > GT > Shared Functions > vol5c Shared Functions >756* [Structure] RENDER_SURFACE_STATE [BDW+] > ClearColorConversionEnable:757*758* Project: Gfx11759*760* "Enables Pixel backend hw to convert clear values into native format761* and write back to clear address, so that display and sampler can use762* the converted value for resolving fast cleared RTs."763*764* Summary:765* Clear color conversion must be enabled if the clear color is stored766* indirectly and fast color clears are enabled.767*/768if (info->use_clear_address) {769s.ClearColorConversionEnable = true;770}771#endif772773#if GFX_VER >= 12774assert(info->use_clear_address);775#elif GFX_VER >= 9776if (!info->use_clear_address) {777s.RedClearColor = info->clear_color.u32[0];778s.GreenClearColor = info->clear_color.u32[1];779s.BlueClearColor = info->clear_color.u32[2];780s.AlphaClearColor = info->clear_color.u32[3];781}782#elif GFX_VER >= 7783/* Prior to Sky Lake, we only have one bit for the clear color which784* gives us 0 or 1 in whatever the surface's format happens to be.785*/786if (isl_format_has_int_channel(info->view->format)) {787for (unsigned i = 0; i < 4; i++) {788assert(info->clear_color.u32[i] == 0 ||789info->clear_color.u32[i] == 1);790}791s.RedClearColor = info->clear_color.u32[0] != 0;792s.GreenClearColor = info->clear_color.u32[1] != 0;793s.BlueClearColor = info->clear_color.u32[2] != 0;794s.AlphaClearColor = info->clear_color.u32[3] != 0;795} else {796for (unsigned i = 0; i < 4; i++) {797assert(info->clear_color.f32[i] == 0.0f ||798info->clear_color.f32[i] == 1.0f);799}800s.RedClearColor = info->clear_color.f32[0] != 0.0f;801s.GreenClearColor = info->clear_color.f32[1] != 0.0f;802s.BlueClearColor = info->clear_color.f32[2] != 0.0f;803s.AlphaClearColor = info->clear_color.f32[3] != 0.0f;804}805#endif806}807808GENX(RENDER_SURFACE_STATE_pack)(NULL, state, &s);809}810811void812isl_genX(buffer_fill_state_s)(const struct isl_device *dev, void *state,813const struct isl_buffer_fill_state_info *restrict info)814{815uint64_t buffer_size = info->size_B;816817/* Uniform and Storage buffers need to have surface size not less that the818* aligned 32-bit size of the buffer. To calculate the array lenght on819* unsized arrays in StorageBuffer the last 2 bits store the padding size820* added to the surface, so we can calculate latter the original buffer821* size to know the number of elements.822*823* surface_size = isl_align(buffer_size, 4) +824* (isl_align(buffer_size) - buffer_size)825*826* buffer_size = (surface_size & ~3) - (surface_size & 3)827*/828if ((info->format == ISL_FORMAT_RAW ||829info->stride_B < isl_format_get_layout(info->format)->bpb / 8) &&830!info->is_scratch) {831assert(info->stride_B == 1);832uint64_t aligned_size = isl_align(buffer_size, 4);833buffer_size = aligned_size + (aligned_size - buffer_size);834}835836uint32_t num_elements = buffer_size / info->stride_B;837838if (GFX_VER >= 7) {839/* From the IVB PRM, SURFACE_STATE::Height,840*841* For typed buffer and structured buffer surfaces, the number842* of entries in the buffer ranges from 1 to 2^27. For raw buffer843* surfaces, the number of entries in the buffer is the number of bytes844* which can range from 1 to 2^30.845*/846if (info->format == ISL_FORMAT_RAW) {847assert(num_elements <= (1ull << 30));848assert(num_elements > 0);849} else {850assert(num_elements <= (1ull << 27));851}852} else {853assert(num_elements <= (1ull << 27));854}855856struct GENX(RENDER_SURFACE_STATE) s = { 0, };857858s.SurfaceFormat = info->format;859860s.SurfaceType = SURFTYPE_BUFFER;861#if GFX_VERx10 >= 125862if (info->is_scratch) {863/* From the BSpec:864*865* "For surfaces of type SURFTYPE_SCRATCH, valid range of pitch is:866* [63,262143] -> [64B, 256KB]. Also, for SURFTYPE_SCRATCH, the867* pitch must be a multiple of 64bytes."868*/869assert(info->format == ISL_FORMAT_RAW);870assert(info->stride_B % 64 == 0);871assert(info->stride_B <= 256 * 1024);872s.SurfaceType = SURFTYPE_SCRATCH;873}874#else875assert(!info->is_scratch);876#endif877878s.SurfacePitch = info->stride_B - 1;879880#if GFX_VER >= 6881s.SurfaceVerticalAlignment = isl_encode_valign[4];882#if GFX_VER >= 7883s.SurfaceHorizontalAlignment = isl_encode_halign[4];884s.SurfaceArray = false;885#endif886#endif887888#if GFX_VER >= 7889s.Height = ((num_elements - 1) >> 7) & 0x3fff;890s.Width = (num_elements - 1) & 0x7f;891s.Depth = ((num_elements - 1) >> 21) & 0x3ff;892#else893s.Height = ((num_elements - 1) >> 7) & 0x1fff;894s.Width = (num_elements - 1) & 0x7f;895s.Depth = ((num_elements - 1) >> 20) & 0x7f;896#endif897898if (GFX_VER == 12 && dev->info->revision == 0) {899/* TGL-LP A0 has a HW bug (fixed in later HW) which causes buffer900* textures with very close base addresses (delta < 64B) to corrupt each901* other. We can sort-of work around this by making small buffer902* textures 1D textures instead. This doesn't fix the problem for large903* buffer textures but the liklihood of large, overlapping, and very904* close buffer textures is fairly low and the point is to hack around905* the bug so we can run apps and tests.906*/907if (info->format != ISL_FORMAT_RAW &&908info->stride_B == isl_format_get_layout(info->format)->bpb / 8 &&909num_elements <= (1 << 14)) {910s.SurfaceType = SURFTYPE_1D;911s.Width = num_elements - 1;912s.Height = 0;913s.Depth = 0;914}915}916917#if GFX_VER >= 6918s.NumberofMultisamples = MULTISAMPLECOUNT_1;919#endif920921#if (GFX_VER >= 8)922s.TileMode = LINEAR;923#else924s.TiledSurface = false;925#endif926927#if (GFX_VER >= 8)928s.RenderCacheReadWriteMode = WriteOnlyCache;929#else930s.RenderCacheReadWriteMode = 0;931#endif932933s.SurfaceBaseAddress = info->address;934#if GFX_VER >= 6935s.MOCS = info->mocs;936#endif937938#if (GFX_VERx10 >= 75)939s.ShaderChannelSelectRed = (enum GENX(ShaderChannelSelect)) info->swizzle.r;940s.ShaderChannelSelectGreen = (enum GENX(ShaderChannelSelect)) info->swizzle.g;941s.ShaderChannelSelectBlue = (enum GENX(ShaderChannelSelect)) info->swizzle.b;942s.ShaderChannelSelectAlpha = (enum GENX(ShaderChannelSelect)) info->swizzle.a;943#endif944945GENX(RENDER_SURFACE_STATE_pack)(NULL, state, &s);946}947948void949isl_genX(null_fill_state)(void *state,950const struct isl_null_fill_state_info *restrict info)951{952struct GENX(RENDER_SURFACE_STATE) s = {953.SurfaceType = SURFTYPE_NULL,954/* We previously had this format set to B8G8R8A8_UNORM but ran into955* hangs on IVB. R32_UINT seems to work for everybody.956*957* https://gitlab.freedesktop.org/mesa/mesa/-/issues/1872958*/959.SurfaceFormat = ISL_FORMAT_R32_UINT,960#if GFX_VER >= 7961.SurfaceArray = info->size.depth > 1,962#endif963#if GFX_VER >= 8964.TileMode = YMAJOR,965#else966.TiledSurface = true,967.TileWalk = TILEWALK_YMAJOR,968#endif969#if GFX_VER == 7970/* According to PRMs: "Volume 4 Part 1: Subsystem and Cores – Shared971* Functions"972*973* RENDER_SURFACE_STATE::Surface Vertical Alignment974*975* "This field must be set to VALIGN_4 for all tiled Y Render Target976* surfaces."977*978* Affect IVB, HSW.979*/980.SurfaceVerticalAlignment = VALIGN_4,981#endif982.MIPCountLOD = info->levels,983.Width = info->size.width - 1,984.Height = info->size.height - 1,985.Depth = info->size.depth - 1,986.RenderTargetViewExtent = info->size.depth - 1,987#if GFX_VER <= 5988.MinimumArrayElement = info->minimum_array_element,989.ColorBufferComponentWriteDisables = 0xf,990#endif991};992GENX(RENDER_SURFACE_STATE_pack)(NULL, state, &s);993}994995996