Avoid clearing HTILE when shader contains address calculation (#3252)

* resource_tracking: Mark image as written when its used with atomics

* texture_cache: Remove meta registered flag

Mostly useless and it is possible for images to switch metas

* vk_rasterizer: Use xor as heuristic for HTILE clear
This commit is contained in:
TheTurtle 2025-07-16 01:28:03 +03:00 committed by GitHub
parent a82698d601
commit 6e350a5085
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
8 changed files with 42 additions and 35 deletions

View File

@ -3,8 +3,6 @@
#pragma once #pragma once
#include <limits>
#include "common/bit_field.h"
#include "shader_recompiler/frontend/opcodes.h" #include "shader_recompiler/frontend/opcodes.h"
namespace Shader::Gcn { namespace Shader::Gcn {

View File

@ -222,6 +222,7 @@ struct Info {
VAddr pgm_base; VAddr pgm_base;
bool has_storage_images{}; bool has_storage_images{};
bool has_discard{}; bool has_discard{};
bool has_bitwise_xor{};
bool has_image_gather{}; bool has_image_gather{};
bool has_image_query{}; bool has_image_query{};
bool uses_buffer_atomic_float_min_max{}; bool uses_buffer_atomic_float_min_max{};

View File

@ -455,11 +455,12 @@ void PatchImageSharp(IR::Block& block, IR::Inst& inst, Info& info, Descriptors&
// Read image sharp. // Read image sharp.
const auto tsharp = TrackSharp(tsharp_handle, info); const auto tsharp = TrackSharp(tsharp_handle, info);
const auto inst_info = inst.Flags<IR::TextureInstInfo>(); const auto inst_info = inst.Flags<IR::TextureInstInfo>();
const bool is_written = inst.GetOpcode() == IR::Opcode::ImageWrite; const bool is_atomic = IsImageAtomicInstruction(inst);
const bool is_written = inst.GetOpcode() == IR::Opcode::ImageWrite || is_atomic;
const ImageResource image_res = { const ImageResource image_res = {
.sharp_idx = tsharp, .sharp_idx = tsharp,
.is_depth = bool(inst_info.is_depth), .is_depth = bool(inst_info.is_depth),
.is_atomic = IsImageAtomicInstruction(inst), .is_atomic = is_atomic,
.is_array = bool(inst_info.is_array), .is_array = bool(inst_info.is_array),
.is_written = is_written, .is_written = is_written,
.is_r128 = bool(inst_info.is_r128), .is_r128 = bool(inst_info.is_r128),

View File

@ -95,6 +95,9 @@ void Visit(Info& info, const IR::Inst& inst) {
case IR::Opcode::DiscardCond: case IR::Opcode::DiscardCond:
info.has_discard = true; info.has_discard = true;
break; break;
case IR::Opcode::BitwiseXor32:
info.has_bitwise_xor = true;
break;
case IR::Opcode::ImageGather: case IR::Opcode::ImageGather:
case IR::Opcode::ImageGatherDref: case IR::Opcode::ImageGatherDref:
info.has_image_gather = true; info.has_image_gather = true;

View File

@ -503,9 +503,13 @@ bool Rasterizer::IsComputeMetaClear(const Pipeline* pipeline) {
return false; return false;
} }
// Most of the time when a metadata is updated with a shader it gets cleared. It means
// we can skip the whole dispatch and update the tracked state instead. Also, it is not
// intended to be consumed and in such rare cases (e.g. HTile introspection, CRAA) we
// will need its full emulation anyways.
const auto& info = pipeline->GetStage(Shader::LogicalStage::Compute); const auto& info = pipeline->GetStage(Shader::LogicalStage::Compute);
// Assume if a shader reads and writes metas at the same time, it is a copy shader. // Assume if a shader reads metadata, it is a copy shader.
for (const auto& desc : info.buffers) { for (const auto& desc : info.buffers) {
const VAddr address = desc.GetSharp(info).base_address; const VAddr address = desc.GetSharp(info).base_address;
if (!desc.IsSpecial() && !desc.is_written && texture_cache.IsMeta(address)) { if (!desc.IsSpecial() && !desc.is_written && texture_cache.IsMeta(address)) {
@ -513,10 +517,15 @@ bool Rasterizer::IsComputeMetaClear(const Pipeline* pipeline) {
} }
} }
// Most of the time when a metadata is updated with a shader it gets cleared. It means // Metadata surfaces are tiled and thus need address calculation to be written properly.
// we can skip the whole dispatch and update the tracked state instead. Also, it is not // If a shader wants to encode HTILE, for example, from a depth image it will have to compute
// intended to be consumed and in such rare cases (e.g. HTile introspection, CRAA) we // proper tile address from dispatch invocation id. This address calculation contains an xor
// will need its full emulation anyways. // operation so use it as a heuristic for metadata writes that are probably not clears.
if (info.has_bitwise_xor) {
return false;
}
// Assume if a shader writes metadata without address calculation, it is a clear shader.
for (const auto& desc : info.buffers) { for (const auto& desc : info.buffers) {
const VAddr address = desc.GetSharp(info).base_address; const VAddr address = desc.GetSharp(info).base_address;
if (!desc.IsSpecial() && desc.is_written && texture_cache.ClearMeta(address)) { if (!desc.IsSpecial() && desc.is_written && texture_cache.ClearMeta(address)) {

View File

@ -30,7 +30,6 @@ enum ImageFlagBits : u32 {
GpuModified = 1 << 3, ///< Contents have been modified from the GPU GpuModified = 1 << 3, ///< Contents have been modified from the GPU
Registered = 1 << 6, ///< True when the image is registered Registered = 1 << 6, ///< True when the image is registered
Picked = 1 << 7, ///< Temporary flag to mark the image as picked Picked = 1 << 7, ///< Temporary flag to mark the image as picked
MetaRegistered = 1 << 8, ///< True when metadata for this surface is known and registered
}; };
DECLARE_ENUM_FLAG_OPERATORS(ImageFlagBits) DECLARE_ENUM_FLAG_OPERATORS(ImageFlagBits)

View File

@ -508,20 +508,16 @@ ImageView& TextureCache::FindRenderTarget(BaseDesc& desc) {
UpdateImage(image_id); UpdateImage(image_id);
// Register meta data for this color buffer // Register meta data for this color buffer
if (!(image.flags & ImageFlagBits::MetaRegistered)) {
if (desc.info.meta_info.cmask_addr) { if (desc.info.meta_info.cmask_addr) {
surface_metas.emplace(desc.info.meta_info.cmask_addr, surface_metas.emplace(desc.info.meta_info.cmask_addr,
MetaDataInfo{.type = MetaDataInfo::Type::CMask}); MetaDataInfo{.type = MetaDataInfo::Type::CMask});
image.info.meta_info.cmask_addr = desc.info.meta_info.cmask_addr; image.info.meta_info.cmask_addr = desc.info.meta_info.cmask_addr;
image.flags |= ImageFlagBits::MetaRegistered;
} }
if (desc.info.meta_info.fmask_addr) { if (desc.info.meta_info.fmask_addr) {
surface_metas.emplace(desc.info.meta_info.fmask_addr, surface_metas.emplace(desc.info.meta_info.fmask_addr,
MetaDataInfo{.type = MetaDataInfo::Type::FMask}); MetaDataInfo{.type = MetaDataInfo::Type::FMask});
image.info.meta_info.fmask_addr = desc.info.meta_info.fmask_addr; image.info.meta_info.fmask_addr = desc.info.meta_info.fmask_addr;
image.flags |= ImageFlagBits::MetaRegistered;
}
} }
return RegisterImageView(image_id, desc.view_info); return RegisterImageView(image_id, desc.view_info);
@ -536,15 +532,11 @@ ImageView& TextureCache::FindDepthTarget(BaseDesc& desc) {
UpdateImage(image_id); UpdateImage(image_id);
// Register meta data for this depth buffer // Register meta data for this depth buffer
if (!(image.flags & ImageFlagBits::MetaRegistered)) {
if (desc.info.meta_info.htile_addr) { if (desc.info.meta_info.htile_addr) {
surface_metas.emplace( surface_metas.emplace(desc.info.meta_info.htile_addr,
desc.info.meta_info.htile_addr,
MetaDataInfo{.type = MetaDataInfo::Type::HTile, MetaDataInfo{.type = MetaDataInfo::Type::HTile,
.clear_mask = image.info.meta_info.htile_clear_mask}); .clear_mask = image.info.meta_info.htile_clear_mask});
image.info.meta_info.htile_addr = desc.info.meta_info.htile_addr; image.info.meta_info.htile_addr = desc.info.meta_info.htile_addr;
image.flags |= ImageFlagBits::MetaRegistered;
}
} }
// If there is a stencil attachment, link depth and stencil. // If there is a stencil attachment, link depth and stencil.

View File

@ -161,10 +161,12 @@ public:
/// Registers an image view for provided image /// Registers an image view for provided image
ImageView& RegisterImageView(ImageId image_id, const ImageViewInfo& view_info); ImageView& RegisterImageView(ImageId image_id, const ImageViewInfo& view_info);
/// Returns true if the specified address is a metadata surface.
bool IsMeta(VAddr address) const { bool IsMeta(VAddr address) const {
return surface_metas.contains(address); return surface_metas.contains(address);
} }
/// Returns true if a slice of the specified metadata surface has been cleared.
bool IsMetaCleared(VAddr address, u32 slice) const { bool IsMetaCleared(VAddr address, u32 slice) const {
const auto& it = surface_metas.find(address); const auto& it = surface_metas.find(address);
if (it != surface_metas.end()) { if (it != surface_metas.end()) {
@ -173,6 +175,7 @@ public:
return false; return false;
} }
/// Clears all slices of the specified metadata surface.
bool ClearMeta(VAddr address) { bool ClearMeta(VAddr address) {
auto it = surface_metas.find(address); auto it = surface_metas.find(address);
if (it != surface_metas.end()) { if (it != surface_metas.end()) {
@ -182,6 +185,7 @@ public:
return false; return false;
} }
/// Updates the state of a slice of the specified metadata surface.
bool TouchMeta(VAddr address, u32 slice, bool is_clear) { bool TouchMeta(VAddr address, u32 slice, bool is_clear) {
auto it = surface_metas.find(address); auto it = surface_metas.find(address);
if (it != surface_metas.end()) { if (it != surface_metas.end()) {