diff --git a/core/libs/prx/libSceAgcDriver/Graphics/include/Sampler.hpp b/core/libs/prx/libSceAgcDriver/Graphics/include/Sampler.hpp index 56192712ceb7..42e07ad1570d 100644 --- a/core/libs/prx/libSceAgcDriver/Graphics/include/Sampler.hpp +++ b/core/libs/prx/libSceAgcDriver/Graphics/include/Sampler.hpp @@ -86,6 +86,7 @@ class SamplerCache { void RequireFilterMinmax(const Context& context, VkFormat format, std::uint64_t samplerMask, std::span> samplers); bool ForcesDegammaOn(std::uint32_t guestFormat, std::uint64_t samplerMask, std::span> samplers); +bool DisablesDegammaOn(std::uint32_t guestFormat, std::uint64_t samplerMask, std::span> samplers); void RequireBorderSwizzle(std::uint32_t bcSwizzle, std::uint64_t samplerMask, std::span> samplers); std::uint32_t BorderMappingKey(const std::optional& mapping); std::optional OpaqueBlackBorderMapping(const Context& context, std::span pairings); diff --git a/core/libs/prx/libSceAgcDriver/Graphics/include/ShaderResources.hpp b/core/libs/prx/libSceAgcDriver/Graphics/include/ShaderResources.hpp index 7e1beeeb3ae6..83bc2e3e1624 100644 --- a/core/libs/prx/libSceAgcDriver/Graphics/include/ShaderResources.hpp +++ b/core/libs/prx/libSceAgcDriver/Graphics/include/ShaderResources.hpp @@ -76,7 +76,7 @@ TextureCacheUse TextureCacheUsage(); void OverrideCacheBudgetForTest(std::uint64_t bytes); void SampledFromStorageForTest(bool enabled); std::uint64_t SampledTextureCacheBudget(const Context& context); -std::shared_ptr CachedSampledTexture(const Context& context, std::span words, VkImageViewType multisampleViewType = VK_IMAGE_VIEW_TYPE_2D); +std::shared_ptr CachedSampledTexture(const Context& context, std::span words, VkImageViewType multisampleViewType = VK_IMAGE_VIEW_TYPE_2D, bool unormView = false); bool SampledTexturesShareEntry(std::span first, std::span second); bool AnyPendingCpuWrites(const Context& context); bool CompletePendingCpuWrites(const Context& context, std::uint64_t address, std::size_t bytes); @@ -786,7 +786,7 @@ class ShaderResources { std::vector textureFirstLayer; std::unordered_set arrayViewTextures; std::vector textureBaseLevel; - std::vector textureSrgb; + std::vector textureGamma; std::vector> storageTextures; std::vector multisampleStorageViews; std::vector storageMips; diff --git a/core/libs/prx/libSceAgcDriver/Graphics/include/Texture.hpp b/core/libs/prx/libSceAgcDriver/Graphics/include/Texture.hpp index 6f3583134665..55cf6baaa376 100644 --- a/core/libs/prx/libSceAgcDriver/Graphics/include/Texture.hpp +++ b/core/libs/prx/libSceAgcDriver/Graphics/include/Texture.hpp @@ -88,8 +88,8 @@ struct OwnedImage { class Texture { public: - Texture(const Context& context, TextureDetiler& detiler, const GuestTextureResource& descriptor, VkComponentMapping components, std::span snapshot, bool depthCompare = false, bool layerRows = false, const VkClearColorValue* clear = nullptr, std::optional> packedSlabs = std::nullopt, std::span sources = {}); - Texture(const Context& context, TextureDetiler& detiler, const GuestTextureResource& descriptor, VkComponentMapping components, std::shared_ptr staging, bool depthCompare = false, bool layerRows = false, std::optional> packedSlabs = std::nullopt); + Texture(const Context& context, TextureDetiler& detiler, const GuestTextureResource& descriptor, VkComponentMapping components, std::span snapshot, bool depthCompare = false, bool layerRows = false, const VkClearColorValue* clear = nullptr, std::optional> packedSlabs = std::nullopt, std::span sources = {}, bool unormView = false); + Texture(const Context& context, TextureDetiler& detiler, const GuestTextureResource& descriptor, VkComponentMapping components, std::shared_ptr staging, bool depthCompare = false, bool layerRows = false, std::optional> packedSlabs = std::nullopt, bool unormView = false); Texture(const Context& context, const std::shared_ptr& source, const GuestTextureResource& descriptor, VkComponentMapping components); // A view of a storage image's own VkImage: the sampled texture follows the image's content, so a // compute pass writing it and the next pass sampling it share one image and copy nothing. @@ -104,7 +104,7 @@ class Texture { static VkFormat DepthCopyFormat(VkFormat colorFormat); static bool CanDepthCopyFrom(const StorageTexture& source, const GuestTextureResource& descriptor); struct BlockCopyTag {}; - Texture(const Context& context, const std::shared_ptr& source, const GuestTextureResource& descriptor, VkComponentMapping components, BlockCopyTag); + Texture(const Context& context, const std::shared_ptr& source, const GuestTextureResource& descriptor, VkComponentMapping components, BlockCopyTag, bool unormView = false); static bool CanBlockCopyFrom(const StorageTexture& source, const GuestTextureResource& descriptor); struct RepackCopyTag {}; Texture(const Context& context, const std::shared_ptr& source, const GuestTextureResource& descriptor, VkComponentMapping components, RepackCopyTag); @@ -125,9 +125,11 @@ class Texture { VkImageView FirstLayerView() const { return firstLayerView; } VkImageView BaseLevelView() const; bool HasSrgbView() const { return srgbInfo.has_value(); } + bool HasUnormView() const { return unormInfo.has_value(); } bool HasArrayView() const { return arrayInfo.has_value(); } VkImageView ArrayView() const; VkImageView SrgbView(bool firstLayer, bool baseLevel) const; + VkImageView SampledGammaView(bool firstLayer, bool baseLevel, bool srgb) const; struct ViewRange { VkImageViewType type; std::uint32_t levels; @@ -163,13 +165,13 @@ class Texture { void MarkReleased() { released.store(true, std::memory_order_release); } private: - Texture(const Context& context, TextureDetiler& detiler, const GuestTextureResource& descriptor, VkComponentMapping components, std::span snapshot, std::shared_ptr staging, bool depthCompare, bool layerRows, const VkClearColorValue* clear, std::optional> packedSlabs, std::span sources); + Texture(const Context& context, TextureDetiler& detiler, const GuestTextureResource& descriptor, VkComponentMapping components, std::span snapshot, std::shared_ptr staging, bool depthCompare, bool layerRows, const VkClearColorValue* clear, std::optional> packedSlabs, std::span sources, bool unormView); void release() noexcept; bool refreshBlockCopy(); bool refreshSrgbExpand(); void createFirstLayerView(const GuestTextureResource& descriptor, VkImageViewCreateInfo viewInfo); void prepareBaseLevelView(const GuestTextureResource& descriptor, VkImageViewCreateInfo viewInfo); - void prepareSrgbView(const GuestTextureResource& descriptor, VkImageViewCreateInfo viewInfo); + void prepareSrgbView(const GuestTextureResource& descriptor, VkImageViewCreateInfo viewInfo, bool unormView = false); // Held by value: cached textures outlive the Context of the draw that created them. Context context; @@ -185,9 +187,11 @@ class Texture { mutable std::mutex baseLevelMutex; mutable VkImageView baseLevelView = VK_NULL_HANDLE; std::optional srgbInfo; + std::optional unormInfo; float srgbMinLod = 0.0f; mutable std::mutex srgbMutex; mutable std::array srgbViews{}; + mutable std::array unormViews{}; std::optional arrayInfo; float arrayMinLod = 0.0f; mutable std::mutex arrayMutex; @@ -216,7 +220,7 @@ class Texture { }; VkFormat AttachmentProxyFormat(const Context& context, VkFormat format); -VkFormat SrgbTwinFormat(const Context& context, VkFormat format); +VkFormat SrgbTwinFormat(const Context& context, VkFormat format, bool unormView = false); class UnitFlags { public: diff --git a/core/libs/prx/libSceAgcDriver/Graphics/src/MultisampleTarget.cpp b/core/libs/prx/libSceAgcDriver/Graphics/src/MultisampleTarget.cpp index 6055106cc52e..06c2e06a5e34 100644 --- a/core/libs/prx/libSceAgcDriver/Graphics/src/MultisampleTarget.cpp +++ b/core/libs/prx/libSceAgcDriver/Graphics/src/MultisampleTarget.cpp @@ -746,7 +746,7 @@ class MultisampleTarget : public std::enable_shared_from_this }); const std::array key{static_cast(components.r), static_cast(components.g), static_cast(components.b), static_cast(components.a), static_cast(viewType)}; auto& texture = textures[key]; - if (texture == nullptr) texture = std::make_shared(context, image, target.format, VK_IMAGE_ASPECT_COLOR_BIT, components, viewType, 0, std::shared_ptr{}, orderFormat != VK_FORMAT_UNDEFINED ? VK_IMAGE_USAGE_SAMPLED_BIT : VkImageUsageFlags{0}); + if (texture == nullptr) texture = std::make_shared(context, image, target.format, VK_IMAGE_ASPECT_COLOR_BIT, components, viewType, 0, std::shared_ptr{}, VK_IMAGE_USAGE_SAMPLED_BIT); return {shared_from_this(), texture.get()}; } diff --git a/core/libs/prx/libSceAgcDriver/Graphics/src/Sampler.cpp b/core/libs/prx/libSceAgcDriver/Graphics/src/Sampler.cpp index f3fd1ec63ce7..74fa18362026 100644 --- a/core/libs/prx/libSceAgcDriver/Graphics/src/Sampler.cpp +++ b/core/libs/prx/libSceAgcDriver/Graphics/src/Sampler.cpp @@ -77,7 +77,7 @@ namespace AgcDriver::Graphics { Sampler::Sampler(const Context& context, const GuestSamplerResource& descriptor, const std::optional& borderMapping) : context(context), borderMapping(borderMapping) { Validate(context, descriptor); forcesDegamma = descriptor.forceDegamma; - disablesDegamma = descriptor.disableDegamma; + disablesDegamma = descriptor.disableDegamma && !descriptor.forceDegamma; VkSamplerCreateInfo info{VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO}; VkSamplerReductionModeCreateInfoEXT reduction{VK_STRUCTURE_TYPE_SAMPLER_REDUCTION_MODE_CREATE_INFO_EXT, nullptr, descriptor.reductionMode}; @@ -202,6 +202,21 @@ namespace AgcDriver::Graphics { return false; } + bool DisablesDegammaOn(std::uint32_t guestFormat, std::uint64_t samplerMask, std::span> samplers) { + bool disabled = false; + bool enabled = false; + for (auto mask = samplerMask; mask != 0u; mask &= mask - 1u) { + const auto element = static_cast(std::countr_zero(mask)); + Require(element < samplers.size(), "a sampled texture is paired with an unbound sampler element " + std::to_string(element)); + const auto& sampler = samplers[element]; + disabled = disabled || sampler->DisablesDegamma(); + enabled = enabled || !sampler->DisablesDegamma(); + } + if (!IsSrgbTextureFormat(guestFormat)) return false; + Require(!disabled || !enabled, "an sRGB texture is paired with both ordinary and SKIP_DEGAMMA samplers, which needs distinct sampled views and is not implemented"); + return disabled; + } + void RequireBorderSwizzle(std::uint32_t bcSwizzle, std::uint64_t samplerMask, std::span> samplers) { constexpr std::uint32_t BcSwizzleXyzw = 0u; constexpr std::uint32_t BcSwizzleZyxw = 4u; diff --git a/core/libs/prx/libSceAgcDriver/Graphics/src/ShaderResources.cpp b/core/libs/prx/libSceAgcDriver/Graphics/src/ShaderResources.cpp index 83d168ce56ab..1406c2d40281 100644 --- a/core/libs/prx/libSceAgcDriver/Graphics/src/ShaderResources.cpp +++ b/core/libs/prx/libSceAgcDriver/Graphics/src/ShaderResources.cpp @@ -146,6 +146,7 @@ struct TextureKey { std::array words; std::array components; bool depthCompare = false; + bool unormView = false; AllocationIdentity allocation; bool operator==(const TextureKey&) const = default; }; @@ -158,15 +159,16 @@ std::uint64_t hashWords(std::uint64_t hash, const std::uint32_t* words, std::siz struct TextureKeyHash { std::size_t operator()(const TextureKey& key) const noexcept { auto hash = 14695981039346656037ull ^ reinterpret_cast(key.device); - hash = hashWords(hash, key.words.data(), key.words.size()) ^ static_cast(key.depthCompare); + hash = hashWords(hash, key.words.data(), key.words.size()) ^ static_cast(key.depthCompare) ^ (static_cast(key.unormView) << 1u); for (const auto value : {key.allocation.rangeAddress, key.allocation.rangeBytes, key.allocation.allocationAddress, key.allocation.allocationBytes, key.allocation.backingSerial, static_cast(key.allocation.flags)}) hash = (hash ^ value) * 1099511628211ull; return static_cast(hashWords(hash, key.components.data(), key.components.size())); } }; -TextureKey MakeTextureKey(VkDevice device, std::span words, VkComponentMapping components, bool depthCompare = false) { +TextureKey MakeTextureKey(VkDevice device, std::span words, VkComponentMapping components, bool depthCompare = false, bool unormView = false) { TextureKey key{device, {}, {static_cast(components.r), static_cast(components.g), static_cast(components.b), static_cast(components.a)}}; key.depthCompare = depthCompare; + key.unormView = unormView; key.allocation = ImageAllocationIdentity(words); std::copy(words.begin(), words.end(), key.words.begin()); key.words[5] &= ~((0xfffu << 8u) | (1u << 25u)); @@ -1013,9 +1015,9 @@ PendingLookup pendingLookupSource(const Context& context, const GuestTextureReso } // `guestBytes` is the surface size when the caller described the surface already (0: described here). -std::shared_ptr cachedTexture(const Context& context, std::span words, const GuestTextureResource& requested, VkComponentMapping components, std::uint64_t guestBytes = 0, bool depthCompare = false, VkImageViewType multisampleViewType = VK_IMAGE_VIEW_TYPE_2D, bool layerRows = false); +std::shared_ptr cachedTexture(const Context& context, std::span words, const GuestTextureResource& requested, VkComponentMapping components, std::uint64_t guestBytes = 0, bool depthCompare = false, VkImageViewType multisampleViewType = VK_IMAGE_VIEW_TYPE_2D, bool layerRows = false, bool unormView = false); -std::shared_ptr lookupSampledTexture(const Context& context, std::span words, const GuestTextureResource& requested, VkComponentMapping components, std::uint64_t guestBytes, bool depthCompare, VkImageViewType multisampleViewType, bool layerRows) { +std::shared_ptr lookupSampledTexture(const Context& context, std::span words, const GuestTextureResource& requested, VkComponentMapping components, std::uint64_t guestBytes, bool depthCompare, VkImageViewType multisampleViewType, bool layerRows, bool unormView) { Require(!layerRows || (requested.dimension == TextureDimension::k1DArray && !depthCompare), "only a sampled 1D array is laid out as rows of a 2D image"); const auto resource = layerRows ? requested : ArrayLayerWindow(requested, context.limits.maxImageArrayLayers); if (resource.baseAddress != requested.baseAddress) guestBytes = 0; @@ -1050,13 +1052,13 @@ std::shared_ptr lookupSampledTexture(const Context& context, std::span< if (depthBitsWidth == 32u && resource.format != float32) { auto bits = resource; bits.format = float32; - return cachedTexture(context, words, bits, components, guestBytes, depthCompare, VK_IMAGE_VIEW_TYPE_2D, layerRows); + return cachedTexture(context, words, bits, components, guestBytes, depthCompare, VK_IMAGE_VIEW_TYPE_2D, layerRows, unormView); } constexpr auto unorm16 = static_cast(ShaderRecompiler::IrBufferFormat::Format16UNorm); if (depthBitsWidth == 16u && resource.format != unorm16) { auto normalized = resource; normalized.format = unorm16; - return cachedTexture(context, words, normalized, components, guestBytes, depthCompare, VK_IMAGE_VIEW_TYPE_2D, layerRows); + return cachedTexture(context, words, normalized, components, guestBytes, depthCompare, VK_IMAGE_VIEW_TYPE_2D, layerRows, unormView); } const auto viewedWindow = [&] { const auto window = ViewedArrayWindow(resource, false); @@ -1069,7 +1071,7 @@ std::shared_ptr lookupSampledTexture(const Context& context, std::span< static std::atomic windows{0}; const auto count = windows.fetch_add(1) + 1; if (count <= 5 || (count & (count - 1)) == 0) std::fprintf(stderr, "[gpu] sampled array view 0x%llx (%ux%u format %u, dimension %d, slices %u..%u) served from its viewed slices at 0x%llx; %llu such views\n", static_cast(resource.baseAddress), resource.width, resource.height, resource.format, static_cast(resource.dimension), resource.baseArray, resource.depthOrLastArray, static_cast(window.baseAddress), static_cast(count)); - return cachedTexture(context, words, window, components, 0, depthCompare, VK_IMAGE_VIEW_TYPE_2D, layerRows); + return cachedTexture(context, words, window, components, 0, depthCompare, VK_IMAGE_VIEW_TYPE_2D, layerRows, unormView); } if (guestBytes == 0) guestBytes = SurfaceGuestBytes(resource); const auto residency = depthCompare || layerRows || SrgbExpandsToRgba8(context, resource.format) ? std::nullopt : MinLodResidentLevels(resource); @@ -1213,7 +1215,7 @@ std::shared_ptr lookupSampledTexture(const Context& context, std::span< return texture; } if (source != nullptr && blockCopy) { - auto texture = std::make_shared(context, source, resource, components, Texture::BlockCopyTag{}); + auto texture = std::make_shared(context, source, resource, components, Texture::BlockCopyTag{}, unormView); texture->RefreshDepthCopy(); return texture; } @@ -1228,12 +1230,12 @@ std::shared_ptr lookupSampledTexture(const Context& context, std::span< return texture; } if (source != nullptr) return std::make_shared(context, source, resource, components); - if (const auto clear = SampledClearColor(context, resource, *keys, depthCompare)) return std::make_shared(context, *context.detiler, resource, components, std::span{}, false, layerRows, &*clear); + if (const auto clear = SampledClearColor(context, resource, *keys, depthCompare)) return std::make_shared(context, *context.detiler, resource, components, std::span{}, false, layerRows, &*clear, std::nullopt, std::span{}, unormView); const auto slabs = slabbed ? committedSlabs(resource, guestBytes, std::nullopt) : std::nullopt; if (slabs.has_value()) reportCommittedSlabs(resource, *slabs, guestBytes); std::vector snapshot(snapshotBytes(slabs, bytes)); readSnapshot(resource, *keys, slabs, snapshot); - return std::make_shared(context, *context.detiler, resource, components, snapshot, depthCompare, layerRows, nullptr, packedSlabs(slabs)); + return std::make_shared(context, *context.detiler, resource, components, snapshot, depthCompare, layerRows, nullptr, packedSlabs(slabs), std::span{}, unormView); } auto snapshotGeneration = generation; std::optional>> committed; @@ -1248,7 +1250,7 @@ std::shared_ptr lookupSampledTexture(const Context& context, std::span< auto slabs = slabbed ? committedSlabs(resource, guestBytes, committed) : std::nullopt; const auto snapshotSize = snapshotBytes(slabs, bytes); auto& cache = Textures(); - const auto key = MakeTextureKey(context.device, words, components, depthCompare); + const auto key = MakeTextureKey(context.device, words, components, depthCompare, unormView); std::lock_guard lock(cache.mutex); const CacheHold held(sampledCacheHeld); dropUncachedViews(cache); @@ -1331,7 +1333,7 @@ std::shared_ptr lookupSampledTexture(const Context& context, std::span< entry.texture = createTexture(source, resource, components, Texture::DepthCopyTag{}); entry.texture->RefreshDepthCopy(); } else if (blockCopy) { - entry.texture = createTexture(source, resource, components, Texture::BlockCopyTag{}); + entry.texture = createTexture(source, resource, components, Texture::BlockCopyTag{}, unormView); entry.texture->RefreshDepthCopy(); } else if (IsRepackedTextureFormat(resource.format)) { entry.texture = createTexture(source, resource, components, Texture::RepackCopyTag{}); @@ -1346,11 +1348,11 @@ std::shared_ptr lookupSampledTexture(const Context& context, std::span< if (!source->Cached()) entry.accounted += heldBytes(*source); counters.fromStorage.fetch_add(1, std::memory_order_relaxed); } else if (clear) { - entry.texture = createTexture(*context.detiler, resource, components, std::span{}, false, layerRows, &*clear); + entry.texture = createTexture(*context.detiler, resource, components, std::span{}, false, layerRows, &*clear, std::nullopt, std::span{}, unormView); entry.accounted = entry.texture->AllocationBytes(); counters.cleared.fetch_add(1, std::memory_order_relaxed); } else if (!gpuSources.empty()) { - entry.texture = createTexture(*context.detiler, resource, components, std::span{}, false, false, nullptr, std::nullopt, std::span(gpuSources)); + entry.texture = createTexture(*context.detiler, resource, components, std::span{}, false, false, nullptr, std::nullopt, std::span(gpuSources), unormView); entry.accounted = entry.texture->AllocationBytes(); counters.imported.fetch_add(1, std::memory_order_relaxed); } else { @@ -1378,8 +1380,8 @@ std::shared_ptr lookupSampledTexture(const Context& context, std::span< for (std::size_t i = 0; i < read.size(); i += 64) nonzero += read[i] != std::byte{0}; std::fprintf(stderr, "[texture] 0x%llx %ux%u format %u tile %d: %zu of %zu sampled bytes nonzero\n", static_cast(resource.baseAddress), resource.width, resource.height, resource.format, static_cast(resource.tileMode), nonzero, read.size() / 64); } - if (staging != nullptr) entry.texture = createTexture(*context.detiler, resource, components, std::move(staging), depthCompare, layerRows, packedSlabs(entry.slabs)); - else entry.texture = createTexture(*context.detiler, resource, components, entry.bytes, depthCompare, layerRows, nullptr, packedSlabs(entry.slabs)); + if (staging != nullptr) entry.texture = createTexture(*context.detiler, resource, components, std::move(staging), depthCompare, layerRows, packedSlabs(entry.slabs), unormView); + else entry.texture = createTexture(*context.detiler, resource, components, entry.bytes, depthCompare, layerRows, nullptr, packedSlabs(entry.slabs), std::span{}, unormView); entry.accounted = entry.texture->AllocationBytes(); if (entry.bytes.size() > SnapshotCompareBytes && (entry.generation != 0 || entry.untracked[0] != 0)) parkSnapshot(cache, entry.bytes); counters.snapshots.fetch_add(1, std::memory_order_relaxed); @@ -1482,7 +1484,7 @@ std::shared_ptr provedSampledLookup(const Context& context, SampledLook return texture; } -std::shared_ptr cachedTexture(const Context& context, std::span words, const GuestTextureResource& requested, VkComponentMapping components, std::uint64_t guestBytes, bool depthCompare, VkImageViewType multisampleViewType, bool layerRows) { +std::shared_ptr cachedTexture(const Context& context, std::span words, const GuestTextureResource& requested, VkComponentMapping components, std::uint64_t guestBytes, bool depthCompare, VkImageViewType multisampleViewType, bool layerRows, bool unormView) { auto& memo = ShaderRecompiler::ThreadOwned(sampledLookupMemoSlot); struct Nesting { std::uint32_t& depth; @@ -1491,7 +1493,7 @@ std::shared_ptr cachedTexture(const Context& context, std::span 8 || RuntimeSwitches::Set(RuntimeSwitch::NoSampledLookupMemo)) { const Nesting nested(memo.depth); - return lookupSampledTexture(context, words, requested, components, guestBytes, depthCompare, multisampleViewType, layerRows); + return lookupSampledTexture(context, words, requested, components, guestBytes, depthCompare, multisampleViewType, layerRows, unormView); } const Nesting outer(memo.depth); SampledLookupKey key; @@ -1501,7 +1503,7 @@ std::shared_ptr cachedTexture(const Context& context, std::span(components.r) | (static_cast(components.g) << 8u) | (static_cast(components.b) << 16u) | (static_cast(components.a) << 24u); - key.flags = (depthCompare ? 1u : 0u) | (layerRows ? 2u : 0u) | (static_cast(multisampleViewType) << 2u); + key.flags = (depthCompare ? 1u : 0u) | (layerRows ? 2u : 0u) | (static_cast(multisampleViewType) << 2u) | (unormView ? 0x80000000u : 0u); if (const auto found = memo.proofs.find(key); found != memo.proofs.end()) { if (auto texture = provedSampledLookup(context, found->second)) { auto& cache = Textures(); @@ -1519,12 +1521,12 @@ std::shared_ptr cachedTexture(const Context& context, std::span= SampledLookupMemoEntries) memo.proofs.clear(); - memo.proofs.insert_or_assign(key, SampledLookupProof{texture, MakeTextureKey(context.device, words, components, depthCompare), record, pendingSerial, imports, allocations, unwatched}); + memo.proofs.insert_or_assign(key, SampledLookupProof{texture, MakeTextureKey(context.device, words, components, depthCompare, unormView), record, pendingSerial, imports, allocations, unwatched}); return texture; } @@ -1953,9 +1955,9 @@ std::uint64_t SampledTextureCacheBudget(const Context& context) { return sampledBudget(context, cache); } -std::shared_ptr CachedSampledTexture(const Context& context, std::span words, VkImageViewType multisampleViewType) { +std::shared_ptr CachedSampledTexture(const Context& context, std::span words, VkImageViewType multisampleViewType, bool unormView) { const auto resource = DecodeTextureResource(words); - return cachedTexture(context, words, resource, ViewComponents(resource), 0, false, multisampleViewType); + return cachedTexture(context, words, resource, ViewComponents(resource), 0, false, multisampleViewType, false, unormView); } bool SampledTexturesShareEntry(std::span first, std::span second) { @@ -3251,7 +3253,7 @@ void ShaderResources::buildComplete() { write.pImageInfo = images.data() + images.size(); for (const auto index : binding.imageAllocations) { if (index == std::numeric_limits::max()) images.push_back(absentImage(binding.layout.binding)); - else images.push_back({VK_NULL_HANDLE, textureSrgb[index] ? textures[index]->SrgbView(textureFirstLayer[index], textureBaseLevel[index]) : textureBaseLevel[index] ? textures[index]->BaseLevelView() : textureFirstLayer[index] ? textures[index]->FirstLayerView() : textures[index]->View(), textures[index]->Layout()}); + else images.push_back({VK_NULL_HANDLE, textureGamma[index] ? textures[index]->SampledGammaView(textureFirstLayer[index], textureBaseLevel[index], textureGamma[index] == 1u) : textureBaseLevel[index] ? textures[index]->BaseLevelView() : textureFirstLayer[index] ? textures[index]->FirstLayerView() : textures[index]->View(), textures[index]->Layout()}); } Require(binding.imageAllocations.size() == binding.layout.descriptorCount, "descriptor allocations disagree with compact binding"); break; @@ -4644,7 +4646,7 @@ bool ShaderResources::reresolveElements(const std::vector& elements const auto first = std::lower_bound(indexImagePositions.begin(), indexImagePositions.end(), i, [](const ImagePosition& entry, std::size_t texture) { return entry.texture < texture; }); for (auto entry = first; entry != indexImagePositions.end() && entry->texture == i; ++entry) { const auto& current = textures[i]; - const auto view = entry->table ? (textureFirstLayer[i] ? current->FirstLayerView() : current->View()) : textureSrgb[i] ? current->SrgbView(textureFirstLayer[i], textureBaseLevel[i]) : textureBaseLevel[i] ? current->BaseLevelView() : textureFirstLayer[i] ? current->FirstLayerView() : current->View(); + const auto view = entry->table ? (textureFirstLayer[i] ? current->FirstLayerView() : current->View()) : textureGamma[i] ? current->SampledGammaView(textureFirstLayer[i], textureBaseLevel[i], textureGamma[i] == 1u) : textureBaseLevel[i] ? current->BaseLevelView() : textureFirstLayer[i] ? current->FirstLayerView() : current->View(); descriptorImages[entry->position] = {VK_NULL_HANDLE, view, current->Layout()}; } rebound = true; @@ -6348,7 +6350,7 @@ void ShaderResources::resolveImageBinding(const ShaderRecompiler::DescriptorBind } const bool forcedSrgb = ForcesDegammaOn(resource.format, binding.imageSamplers[element], shaderSamplers); RequireNonSeamlessCube(resource.dimension == TextureDimension::kCube, binding.imageSamplers[element], shaderSamplers); - RequireDisablesDegammaOn(resource.format, binding.imageSamplers[element], shaderSamplers); + const bool skippedSrgb = DisablesDegammaOn(resource.format, binding.imageSamplers[element], shaderSamplers); const bool depthCompare = !binding.imageDepthCompare.empty() && binding.imageDepthCompare.at(element); const bool firstLayer = (binding.imageShape == ShaderRecompiler::DescriptorImageShape::Image2D && (resource.dimension == TextureDimension::k2DArray || (depthCompare && resource.dimension == TextureDimension::k1DArray))) || (binding.imageShape == ShaderRecompiler::DescriptorImageShape::Image1D && resource.dimension == TextureDimension::k1DArray); const bool depth1D = depthCompare && ((binding.imageShape == ShaderRecompiler::DescriptorImageShape::Image2D && resource.dimension == TextureDimension::k1D) || (binding.imageShape == ShaderRecompiler::DescriptorImageShape::Image2DArray && resource.dimension == TextureDimension::k1DArray)); @@ -6362,16 +6364,17 @@ void ShaderResources::resolveImageBinding(const ShaderRecompiler::DescriptorBind if (resource.dccAddress != 0) CompletePendingCpuWrites(context, resource.dccAddress, DccKeyBytes(guestBytes)); std::shared_ptr texture; const VkImageViewType multisampleViewType = binding.imageShape == ShaderRecompiler::DescriptorImageShape::Image2DMultisampleArray ? VK_IMAGE_VIEW_TYPE_2D_ARRAY : VK_IMAGE_VIEW_TYPE_2D; - if (previousSampled.texture != nullptr && previousSampled.depthCompare == depthCompare && previousSampled.multisampleViewType == multisampleViewType && previousSampled.firstSample == firstSample && previousSampled.layerRows == layerRows && words.size() == 8 && std::equal(words.begin(), words.end(), previousSampled.words.begin())) texture = previousSampled.texture; - if (texture == nullptr && !firstSample && record != nullptr && record->texture != nullptr) { + if (previousSampled.texture != nullptr && (!skippedSrgb || previousSampled.texture->HasUnormView()) && previousSampled.depthCompare == depthCompare && previousSampled.multisampleViewType == multisampleViewType && previousSampled.firstSample == firstSample && previousSampled.layerRows == layerRows && words.size() == 8 && std::equal(words.begin(), words.end(), previousSampled.words.begin())) texture = previousSampled.texture; + if (texture == nullptr && !firstSample && record != nullptr && record->texture != nullptr && (!skippedSrgb || record->texture->HasUnormView())) { texture = fastTexture(*record); if (TextureCountersReported()) (texture != nullptr ? counters.fastHits : counters.fastMisses).fetch_add(1, std::memory_order_relaxed); } if (texture == nullptr && firstSample) { Require(!depthCompare, "reading sample 0 of a multisampled texture with a depth comparison through a 2D image is not implemented"); texture = DepthSurfaceAt(resource.baseAddress) ? DepthSurfaceFirstSample(context, words, resource, components) : MultisampleTargetFirstSample(context, resource, components); + Require(!skippedSrgb || texture->HasUnormView(), "reading sample 0 of a multisampled sRGB texture through a sampler that skips degamma is not implemented"); } - if (texture == nullptr) texture = cachedTexture(context, words, resource, components, guestBytes, depthCompare, multisampleViewType, layerRows); + if (texture == nullptr) texture = cachedTexture(context, words, resource, components, guestBytes, depthCompare, multisampleViewType, layerRows, skippedSrgb); if (resource.dimension == TextureDimension::k2DMsaa) multisampleSources.push_back(resource.baseAddress); bool baseLevel = false; if (element < binding.imageUnnormalized.size() && binding.imageUnnormalized[element]) { @@ -6408,7 +6411,7 @@ void ShaderResources::resolveImageBinding(const ShaderRecompiler::DescriptorBind textures.push_back(std::move(texture)); textureFirstLayer.push_back(firstLayer); textureBaseLevel.push_back(baseLevel); - textureSrgb.push_back(forcedSrgb); + textureGamma.push_back(skippedSrgb ? 2u : forcedSrgb ? 1u : 0u); describedRanges.push_back({"texture", resource.baseAddress, guestBytes, resource.width, resource.height, resource.format, static_cast(resource.tileMode), resource.dccAddress}); item.imageAllocations.push_back(textures.size() - 1); } diff --git a/core/libs/prx/libSceAgcDriver/Graphics/src/Texture.cpp b/core/libs/prx/libSceAgcDriver/Graphics/src/Texture.cpp index ea800a17f60d..46318324270c 100644 --- a/core/libs/prx/libSceAgcDriver/Graphics/src/Texture.cpp +++ b/core/libs/prx/libSceAgcDriver/Graphics/src/Texture.cpp @@ -537,11 +537,11 @@ static VkDeviceSize BindSparseLevels(const Context& context, OwnedImage& owned, return bytes; } -Texture::Texture(const Context& context, TextureDetiler& detiler, const GuestTextureResource& descriptor, VkComponentMapping components, std::span snapshot, bool depthCompare, bool layerRows, const VkClearColorValue* clear, std::optional> packedSlabs, std::span sources) : Texture(context, detiler, descriptor, components, snapshot, nullptr, depthCompare, layerRows, clear, packedSlabs, sources) {} +Texture::Texture(const Context& context, TextureDetiler& detiler, const GuestTextureResource& descriptor, VkComponentMapping components, std::span snapshot, bool depthCompare, bool layerRows, const VkClearColorValue* clear, std::optional> packedSlabs, std::span sources, bool unormView) : Texture(context, detiler, descriptor, components, snapshot, nullptr, depthCompare, layerRows, clear, packedSlabs, sources, unormView) {} -Texture::Texture(const Context& context, TextureDetiler& detiler, const GuestTextureResource& descriptor, VkComponentMapping components, std::shared_ptr staging, bool depthCompare, bool layerRows, std::optional> packedSlabs) : Texture(context, detiler, descriptor, components, {}, std::move(staging), depthCompare, layerRows, nullptr, packedSlabs, {}) {} +Texture::Texture(const Context& context, TextureDetiler& detiler, const GuestTextureResource& descriptor, VkComponentMapping components, std::shared_ptr staging, bool depthCompare, bool layerRows, std::optional> packedSlabs, bool unormView) : Texture(context, detiler, descriptor, components, {}, std::move(staging), depthCompare, layerRows, nullptr, packedSlabs, {}, unormView) {} -Texture::Texture(const Context& context, TextureDetiler& detiler, const GuestTextureResource& descriptor, VkComponentMapping components, std::span snapshot, std::shared_ptr staging, bool depthCompare, bool layerRows, const VkClearColorValue* clear, std::optional> packedSlabs, std::span sources) : context(context) { +Texture::Texture(const Context& context, TextureDetiler& detiler, const GuestTextureResource& descriptor, VkComponentMapping components, std::span snapshot, std::shared_ptr staging, bool depthCompare, bool layerRows, const VkClearColorValue* clear, std::optional> packedSlabs, std::span sources, bool unormView) : context(context) { static const bool profile = std::getenv("APS5_PROFILE_DRAW") != nullptr; PhaseTimer timer; @@ -625,7 +625,7 @@ Texture::Texture(const Context& context, TextureDetiler& detiler, const GuestTex const auto rowLayers = layerRows ? geometry.imageLayers - descriptor.baseArray : 0u; Require(!layerRows || (descriptor.baseArray < geometry.imageLayers && rowLayers <= context.limits.maxImageDimension2D / 2u), "a 1D array past maxImageArrayLayers whose viewed slices need more than maxImageDimension2D rows is not implemented"); VkImageCreateInfo imageInfo{VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO}; - imageInfo.flags = (descriptor.dimension == TextureDimension::kCube ? VK_IMAGE_CREATE_CUBE_COMPATIBLE_BIT : 0u) | (SrgbTwinFormat(context, vkFormat) != VK_FORMAT_UNDEFINED ? VK_IMAGE_CREATE_MUTABLE_FORMAT_BIT : 0u); + imageInfo.flags = (descriptor.dimension == TextureDimension::kCube ? VK_IMAGE_CREATE_CUBE_COMPATIBLE_BIT : 0u) | (SrgbTwinFormat(context, vkFormat, unormView) != VK_FORMAT_UNDEFINED ? VK_IMAGE_CREATE_MUTABLE_FORMAT_BIT : 0u); imageInfo.imageType = depth1D || layerRows ? VK_IMAGE_TYPE_2D : ImageTypeFor(descriptor.dimension); imageInfo.format = vkFormat; imageInfo.extent = {descriptor.width, layerRows ? rowLayers * 2u : depth1D ? 1u : descriptor.height, geometry.imageDepth}; @@ -965,7 +965,7 @@ Texture::Texture(const Context& context, TextureDetiler& detiler, const GuestTex viewRange = {viewInfo.viewType, viewInfo.subresourceRange.levelCount, viewInfo.subresourceRange.layerCount}; createFirstLayerView(descriptor, viewInfo); prepareBaseLevelView(descriptor, viewInfo); - prepareSrgbView(descriptor, viewInfo); + prepareSrgbView(descriptor, viewInfo, unormView); if (profile) { auto& totals = Profile(); totals.view += timer.lap(); @@ -1088,7 +1088,7 @@ Texture::Texture(const Context& context, const std::shared_ptr& } } -Texture::Texture(const Context& context, const std::shared_ptr& source, const GuestTextureResource& descriptor, VkComponentMapping components, BlockCopyTag) : context(context), storageSource(source), perUseCopy(PerUseCopy::RefreshedOnReuse) { +Texture::Texture(const Context& context, const std::shared_ptr& source, const GuestTextureResource& descriptor, VkComponentMapping components, BlockCopyTag, bool unormView) : context(context), storageSource(source), perUseCopy(PerUseCopy::RefreshedOnReuse) { try { Require(source != nullptr && CanBlockCopyFrom(*source, descriptor), "storage image cannot serve a block copy of the sampled texture"); blockFormat = SampledTextureFormat(context, descriptor.format); @@ -1111,7 +1111,7 @@ Texture::Texture(const Context& context, const std::shared_ptr& imageInfo.arrayLayers = copyLayers; imageInfo.samples = VK_SAMPLE_COUNT_1_BIT; imageInfo.tiling = VK_IMAGE_TILING_OPTIMAL; - imageInfo.flags = SrgbTwinFormat(context, blockFormat) != VK_FORMAT_UNDEFINED ? VK_IMAGE_CREATE_MUTABLE_FORMAT_BIT : 0u; + imageInfo.flags = SrgbTwinFormat(context, blockFormat, unormView) != VK_FORMAT_UNDEFINED ? VK_IMAGE_CREATE_MUTABLE_FORMAT_BIT : 0u; imageInfo.usage = VK_IMAGE_USAGE_TRANSFER_DST_BIT | VK_IMAGE_USAGE_SAMPLED_BIT; imageInfo.sharingMode = VK_SHARING_MODE_EXCLUSIVE; imageInfo.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED; @@ -1138,7 +1138,7 @@ Texture::Texture(const Context& context, const std::shared_ptr& viewRange = {viewInfo.viewType, viewInfo.subresourceRange.levelCount, viewInfo.subresourceRange.layerCount}; createFirstLayerView(descriptor, viewInfo); prepareBaseLevelView(descriptor, viewInfo); - prepareSrgbView(descriptor, viewInfo); + prepareSrgbView(descriptor, viewInfo, unormView); } catch (...) { release(); throw; @@ -1246,6 +1246,7 @@ Texture::Texture(const Context& context, const std::shared_ptr& viewRange = {viewInfo.viewType, viewInfo.subresourceRange.levelCount, viewInfo.subresourceRange.layerCount}; createFirstLayerView(descriptor, viewInfo); prepareBaseLevelView(descriptor, viewInfo); + prepareSrgbView(descriptor, viewInfo, true); sourceDescriptor = descriptor; countedBytes = allocationBytes; SampledMemoryCounter().fetch_add(countedBytes, std::memory_order_relaxed); @@ -1483,7 +1484,7 @@ Texture::Texture(const Context& context, const std::shared_ptr& viewRange = {viewInfo.viewType, viewInfo.subresourceRange.levelCount, viewInfo.subresourceRange.layerCount}; createFirstLayerView(descriptor, viewInfo); prepareBaseLevelView(descriptor, viewInfo); - prepareSrgbView(descriptor, viewInfo); + prepareSrgbView(descriptor, viewInfo, true); if (profile) { auto& totals = Profile(); totals.view += timer.lap(); @@ -1552,21 +1553,28 @@ void Texture::prepareBaseLevelView(const GuestTextureResource& descriptor, VkIma baseLevelInfo = viewInfo; } -void Texture::prepareSrgbView(const GuestTextureResource& descriptor, VkImageViewCreateInfo viewInfo) { - const auto format = SrgbTwinFormat(context, viewInfo.format); +void Texture::prepareSrgbView(const GuestTextureResource& descriptor, VkImageViewCreateInfo viewInfo, bool unormView) { + const auto format = SrgbTwinFormat(context, viewInfo.format, unormView); if (format == VK_FORMAT_UNDEFINED) return; viewInfo.pNext = nullptr; viewInfo.format = format; - srgbInfo = viewInfo; + if (IsSrgbTextureFormat(descriptor.format)) unormInfo = viewInfo; + else srgbInfo = viewInfo; srgbMinLod = context.imageViewMinLod ? EffectiveMinLod(descriptor) : 0.0f; } VkImageView Texture::SrgbView(bool firstLayer, bool baseLevel) const { - Require(srgbInfo.has_value(), "AGC graphics: a texture of view format " + std::to_string(viewFormat) + " has no sRGB view for a sampler that forces sRGB decoding"); + Require(HasSrgbView(), "a texture has no sRGB view for a sampler that forces sRGB decoding"); + return SampledGammaView(firstLayer, baseLevel, true); +} + +VkImageView Texture::SampledGammaView(bool firstLayer, bool baseLevel, bool srgb) const { + const auto& prepared = srgb ? srgbInfo : unormInfo; + Require(prepared.has_value(), "a texture of view format " + std::to_string(viewFormat) + " has no " + (srgb ? "sRGB" : "UNORM") + " view for the sampler decode mode"); std::lock_guard lock(srgbMutex); - auto& slot = srgbViews[baseLevel ? 2u : firstLayer ? 1u : 0u]; + auto& slot = (srgb ? srgbViews : unormViews)[baseLevel ? 2u : firstLayer ? 1u : 0u]; if (slot != VK_NULL_HANDLE) return slot; - VkImageViewCreateInfo info = *srgbInfo; + VkImageViewCreateInfo info = *prepared; VkImageViewUsageCreateInfo usage{VK_STRUCTURE_TYPE_IMAGE_VIEW_USAGE_CREATE_INFO}; usage.usage = VK_IMAGE_USAGE_SAMPLED_BIT; info.pNext = &usage; @@ -1632,9 +1640,11 @@ void Texture::release() noexcept { baseLevelView = VK_NULL_HANDLE; if (arrayView) context.Function("vkDestroyImageView")(context.device, arrayView, nullptr); arrayView = VK_NULL_HANDLE; - for (auto& srgbView : srgbViews) { - if (srgbView) context.Function("vkDestroyImageView")(context.device, srgbView, nullptr); - srgbView = VK_NULL_HANDLE; + for (auto* views : {&srgbViews, &unormViews}) { + for (auto& gammaView : *views) { + if (gammaView) context.Function("vkDestroyImageView")(context.device, gammaView, nullptr); + gammaView = VK_NULL_HANDLE; + } } // The image and its memory go with the last holder: this texture, or the batch still uploading it. owned.reset(); @@ -1816,9 +1826,19 @@ bool StorageClearAvailable(const Context& context, std::uint32_t guestFormat, Dc return IsDccClear(keys) && StorageFormatAvailable(context, guestFormat) && ClearColorFor(StorageFormatForGuest(context, guestFormat), keys, clear); } -VkFormat SrgbTwinFormat(const Context& context, VkFormat format) { +VkFormat SrgbTwinFormat(const Context& context, VkFormat format, bool unormView) { const auto decoded = [&](ShaderRecompiler::IrBufferFormat srgb) { return (context.srgbDecodeFormats & ShaderRecompiler::SrgbDecodeBit(srgb)) != 0u; }; switch (format) { + case VK_FORMAT_R8_SRGB: return unormView ? VK_FORMAT_R8_UNORM : VK_FORMAT_UNDEFINED; + case VK_FORMAT_R8G8_SRGB: return unormView ? VK_FORMAT_R8G8_UNORM : VK_FORMAT_UNDEFINED; + case VK_FORMAT_R8G8B8A8_SRGB: return unormView ? VK_FORMAT_R8G8B8A8_UNORM : VK_FORMAT_UNDEFINED; + case VK_FORMAT_B8G8R8A8_SRGB: return unormView ? VK_FORMAT_B8G8R8A8_UNORM : VK_FORMAT_UNDEFINED; + case VK_FORMAT_A8B8G8R8_SRGB_PACK32: return unormView ? VK_FORMAT_A8B8G8R8_UNORM_PACK32 : VK_FORMAT_UNDEFINED; + case VK_FORMAT_BC1_RGB_SRGB_BLOCK: return unormView ? VK_FORMAT_BC1_RGB_UNORM_BLOCK : VK_FORMAT_UNDEFINED; + case VK_FORMAT_BC1_RGBA_SRGB_BLOCK: return unormView ? VK_FORMAT_BC1_RGBA_UNORM_BLOCK : VK_FORMAT_UNDEFINED; + case VK_FORMAT_BC2_SRGB_BLOCK: return unormView ? VK_FORMAT_BC2_UNORM_BLOCK : VK_FORMAT_UNDEFINED; + case VK_FORMAT_BC3_SRGB_BLOCK: return unormView ? VK_FORMAT_BC3_UNORM_BLOCK : VK_FORMAT_UNDEFINED; + case VK_FORMAT_BC7_SRGB_BLOCK: return unormView ? VK_FORMAT_BC7_UNORM_BLOCK : VK_FORMAT_UNDEFINED; case VK_FORMAT_R8_UNORM: return decoded(ShaderRecompiler::IrBufferFormat::Format8Srgb) ? VK_FORMAT_UNDEFINED : VK_FORMAT_R8_SRGB; case VK_FORMAT_R8G8_UNORM: return decoded(ShaderRecompiler::IrBufferFormat::Format8_8Srgb) ? VK_FORMAT_UNDEFINED : VK_FORMAT_R8G8_SRGB; case VK_FORMAT_R8_SNORM: return decoded(ShaderRecompiler::IrBufferFormat::Format8Srgb) ? VK_FORMAT_UNDEFINED : VK_FORMAT_R8_SRGB; diff --git a/core/libs/prx/libSceAgcDriver/tests/Graphics.cpp b/core/libs/prx/libSceAgcDriver/tests/Graphics.cpp index 658b818266e8..5e6496fba84b 100644 --- a/core/libs/prx/libSceAgcDriver/tests/Graphics.cpp +++ b/core/libs/prx/libSceAgcDriver/tests/Graphics.cpp @@ -25,6 +25,7 @@ #include "prx/libSceAgcDriver/Graphics/include/ParallelCompare.hpp" #include "prx/libSceAgcDriver/Graphics/include/DepthSurface.hpp" #include "prx/libSceAgcDriver/Graphics/include/Texture.hpp" +#include "prx/libSceAgcDriver/Graphics/include/Sampler.hpp" #include "prx/libSceAgcDriver/Graphics/include/UnitShadow.hpp" #include "prx/libSceAgcDriver/Graphics/include/TextureTiling.hpp" #include "SceShaders.hpp" @@ -6233,6 +6234,8 @@ struct MockVulkan { struct ImageBufferCopy { VkImage image; VkBuffer buffer; VkBufferImageCopy region; bool fromImage; }; std::vector imageBufferCopies; std::set destroyedImages; + struct ImageCreation { VkFormat format; VkImageCreateFlags flags; }; + std::vector imageCreations; std::map bufferSizes; std::map bufferUsage; std::map allocationFlags; @@ -6381,7 +6384,7 @@ VKAPI_ATTR void VKAPI_CALL mockFreeMemory(VkDevice, VkDeviceMemory memory, const --mock.live; } -VKAPI_ATTR VkResult VKAPI_CALL mockCreateImage(VkDevice, const VkImageCreateInfo*, const VkAllocationCallbacks*, VkImage* image) { *image = makeHandle(); ++mock.live; return VK_SUCCESS; } +VKAPI_ATTR VkResult VKAPI_CALL mockCreateImage(VkDevice, const VkImageCreateInfo* info, const VkAllocationCallbacks*, VkImage* image) { *image = makeHandle(); mock.imageCreations.push_back({info->format, info->flags}); ++mock.live; return VK_SUCCESS; } VKAPI_ATTR void VKAPI_CALL mockGetImageMemoryRequirements(VkDevice, VkImage, VkMemoryRequirements* requirements) { *requirements = {4096, 1, 1}; } VKAPI_ATTR void VKAPI_CALL mockGetImageMemoryRequirements2(VkDevice device, const VkImageMemoryRequirementsInfo2* info, VkMemoryRequirements2* requirements) { @@ -8287,6 +8290,69 @@ void unsetFormatStorageImageTests() { } } +void sampledGammaAllocationTests() { + mock = MockVulkan{}; + mock.allowCopies = true; + auto context = mockContext(); + context.device = makeHandle(); + context.physical = makeHandle(); + context.formatProperties = mockFormatProperties; + context.deviceProc = mockDepthCopyProc; + context.memory.memoryTypes[0].propertyFlags |= VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT; + context.memory.memoryHeapCount = 1; + context.memory.memoryHeaps[0] = {std::uint64_t{4} << 30u, VK_MEMORY_HEAP_DEVICE_LOCAL_BIT}; + context.limits.maxMemoryAllocationCount = 4096; + context.limits.maxImageDimension2D = 4096; + context.limits.maxPerStageDescriptorSampledImages = 8; + context.limits.maxDescriptorSetSampledImages = 8; + context.limits.maxPerStageDescriptorSamplers = 8; + context.limits.maxDescriptorSetSamplers = 8; + context.limits.maxPerStageResources = 64; + context.limits.maxSamplerAnisotropy = 1.0f; + alignas(256) std::array guest{}; + guest.fill(std::byte{0x5b}); + const auto address = reinterpret_cast(guest.data()); + { + AgcDriver::Graphics::TextureDetiler detiler(context); + context.detiler = &detiler; + AgcDriver::Graphics::TextureCache textures(context); + AgcDriver::Graphics::SamplerCache samplers; + context.textureCache = &textures; + context.samplerCache = &samplers; + try { + for (const auto skip : {false, true, false}) { + ShaderRecompiler::RecompileResult vertex, fragment; + auto sampled = makeBinding(Role::GuestImages, ShaderRecompiler::RuntimeAbi::FirstImageBinding, 1, + {static_cast(address >> 8u), static_cast((address >> 40u) & 0xffu) | (130u << 20u) | (3u << 30u), 3u | (15u << 14u), 0xfacu | (9u << 28u), 0u, 0u, 0u, 0u}); + sampled.kind = ShaderRecompiler::DescriptorKind::SampledImage; + sampled.imageWritten.clear(); + sampled.imageShape = ShaderRecompiler::DescriptorImageShape::Image2D; + sampled.imageSamplers = {1u}; + auto sampler = makeBinding(Role::GuestSamplers, static_cast(ShaderRecompiler::RuntimeAbi::Binding::Samplers), 1, {0x92u | (skip ? 1u << 31u : 0u), 0u, 0u, 0u}); + sampler.kind = ShaderRecompiler::DescriptorKind::Sampler; + sampler.samplerDepthCompare = {false}; + vertex.bindings = {sampled, sampler}; + const auto before = mock.imageCreations.size(); + const auto color = AgcDriver::Graphics::DecodeState(makeState()).color; + AgcDriver::Graphics::ShaderResources resources(context, vertex, fragment, color, 0, 0); + bool made = false; + for (auto i = before; i < mock.imageCreations.size(); ++i) { + const auto& image = mock.imageCreations[i]; + if (image.format != VK_FORMAT_R8G8B8A8_SRGB) continue; + made = true; + Require(((image.flags & VK_IMAGE_CREATE_MUTABLE_FORMAT_BIT) != 0) == skip, "ordinary sRGB images must stay immutable; only SKIP_DEGAMMA requests mutable images"); + } + if (skip || before == 0u) Require(made, "a first-use sRGB allocation or its SKIP-capable replacement was not created"); + } + } catch (...) { + AgcDriver::Graphics::ClearCachedTextures(context.device); + throw; + } + AgcDriver::Graphics::ClearCachedTextures(context.device); + } + Require(mock.live == 0, "sampled gamma allocation test leaked Vulkan objects"); +} + void descriptorCacheTests() { mock = MockVulkan{}; auto context = mockContext(); @@ -16311,6 +16377,7 @@ int main(int argc, char** argv) { pushConstantTests(); resourceTests(); unsetFormatStorageImageTests(); + sampledGammaAllocationTests(); oversizedDescriptorTests(); viewAtRangeLimitTests(); guestBufferViewBytesTests(); diff --git a/core/libs/prx/libSceAgcDriver/tests/cmake/agc_driver_image_sample_skip_degamma_tests.cmake b/core/libs/prx/libSceAgcDriver/tests/cmake/agc_driver_image_sample_skip_degamma_tests.cmake new file mode 100644 index 000000000000..38a873d7b950 --- /dev/null +++ b/core/libs/prx/libSceAgcDriver/tests/cmake/agc_driver_image_sample_skip_degamma_tests.cmake @@ -0,0 +1,14 @@ +if(BUILD_TESTING) + foreach(mode point linear) + add_test(NAME agc_driver_image_sample_skip_degamma_${mode} COMMAND agc_driver_image_sample_force_degamma_tests --skip-${mode}) + set_tests_properties(agc_driver_image_sample_skip_degamma_${mode} PROPERTIES + ENVIRONMENT "ANYPS5_NO_SHADER_CACHE=1" SKIP_RETURN_CODE 77 TIMEOUT 30) + add_test(NAME agc_driver_image_sample_skip_degamma_${mode}_uncached COMMAND agc_driver_image_sample_force_degamma_tests --skip-${mode}) + set_tests_properties(agc_driver_image_sample_skip_degamma_${mode}_uncached PROPERTIES + ENVIRONMENT "ANYPS5_NO_SHADER_CACHE=1;APS5_NO_TEXTURE_CACHE=1" SKIP_RETURN_CODE 77 TIMEOUT 30) + if(WIN32) + set_tests_properties(agc_driver_image_sample_skip_degamma_${mode} agc_driver_image_sample_skip_degamma_${mode}_uncached PROPERTIES ENVIRONMENT_MODIFICATION + "PATH=path_list_prepend:$") + endif() + endforeach() +endif() diff --git a/core/libs/prx/libSceAgcDriver/tests/cmake/agc_driver_sampled_cache_budget_tests.cmake b/core/libs/prx/libSceAgcDriver/tests/cmake/agc_driver_sampled_cache_budget_tests.cmake index 65fef8aa031e..4b3b224c7557 100644 --- a/core/libs/prx/libSceAgcDriver/tests/cmake/agc_driver_sampled_cache_budget_tests.cmake +++ b/core/libs/prx/libSceAgcDriver/tests/cmake/agc_driver_sampled_cache_budget_tests.cmake @@ -46,9 +46,10 @@ if(BUILD_TESTING) set_tests_properties(agc_driver_sampled_lookup_memo_proofs_off PROPERTIES ENVIRONMENT "ANYPS5_NO_SHADER_CACHE=1;APS5_NO_SAMPLED_LOOKUP_MEMO=1" SKIP_RETURN_CODE 77 TIMEOUT 30) add_test(NAME agc_driver_sampled_lookup_memo_proofs_import COMMAND agc_driver_sampled_cache_budget_tests lookup-memo-proofs-import) add_test(NAME agc_driver_sampled_lookup_memo_proofs_pending COMMAND agc_driver_sampled_cache_budget_tests lookup-memo-proofs-pending) - set_tests_properties(agc_driver_sampled_lookup_memo_proofs_import agc_driver_sampled_lookup_memo_proofs_pending PROPERTIES ENVIRONMENT "ANYPS5_NO_SHADER_CACHE=1" SKIP_RETURN_CODE 77 TIMEOUT 30) + add_test(NAME agc_driver_sampled_lookup_memo_proofs_unorm_view COMMAND agc_driver_sampled_cache_budget_tests lookup-memo-proofs-unorm-view) + set_tests_properties(agc_driver_sampled_lookup_memo_proofs_import agc_driver_sampled_lookup_memo_proofs_pending agc_driver_sampled_lookup_memo_proofs_unorm_view PROPERTIES ENVIRONMENT "ANYPS5_NO_SHADER_CACHE=1" SKIP_RETURN_CODE 77 TIMEOUT 30) if(WIN32) - set_tests_properties(agc_driver_sampled_lookup_memo_proofs agc_driver_sampled_lookup_memo_proofs_off agc_driver_sampled_lookup_memo_proofs_import agc_driver_sampled_lookup_memo_proofs_pending PROPERTIES ENVIRONMENT_MODIFICATION + set_tests_properties(agc_driver_sampled_lookup_memo_proofs agc_driver_sampled_lookup_memo_proofs_off agc_driver_sampled_lookup_memo_proofs_import agc_driver_sampled_lookup_memo_proofs_pending agc_driver_sampled_lookup_memo_proofs_unorm_view PROPERTIES ENVIRONMENT_MODIFICATION "PATH=path_list_prepend:$") endif() add_test(NAME agc_driver_resource_cache_storage_admission COMMAND agc_driver_sampled_cache_budget_tests resource-admission) diff --git a/core/libs/prx/libSceAgcDriver/tests/execution/ImageSampleForceDegamma.cpp b/core/libs/prx/libSceAgcDriver/tests/execution/ImageSampleForceDegamma.cpp index 090a7de73471..0fe736f70141 100644 --- a/core/libs/prx/libSceAgcDriver/tests/execution/ImageSampleForceDegamma.cpp +++ b/core/libs/prx/libSceAgcDriver/tests/execution/ImageSampleForceDegamma.cpp @@ -156,11 +156,29 @@ void Reject(AgcDriver::VulkanDevice& device, std::uint32_t format, std::string_v } -int main() { +int main(int argc, char** argv) { try { const auto device = OpenVulkanTestDevice(); if (!device) return VulkanTestSkipped; FillTexels(); + if (argc == 2 && (std::string_view(argv[1]) == "--skip-point" || std::string_view(argv[1]) == "--skip-linear")) { + constexpr std::uint32_t SkipDegamma = 1u << 31u; + const bool linear = std::string_view(argv[1]) == "--skip-linear"; + const auto filter = linear ? FilterBilinear : FilterPoint; + FillInput(!linear); + const auto srgb = Sample(*device, Format8888Srgb, filter, 0u); + if (!linear) RequireDecoded(srgb, "ordinary sRGB control"); + RequireSame(Sample(*device, Format8888Srgb, filter, 0u), srgb, "ordinary sRGB cache admission control"); + const auto unorm = Sample(*device, Format8888UNorm, filter, 0u); + Require(srgb != unorm, "sRGB control must differ from encoded UNORM values"); + RequireSame(Sample(*device, Format8888Srgb, filter, SkipDegamma), unorm, "SKIP_DEGAMMA", "through the sRGB descriptor with SKIP_DEGAMMA", "through the UNORM descriptor"); + RequireSame(Sample(*device, Format8888Srgb, filter, SkipDegamma), unorm, "SKIP_DEGAMMA cache admission control"); + RequireSame(Sample(*device, Format8888Srgb, filter, 0u), srgb, "sRGB after SKIP_DEGAMMA", "after changing sampler mode", "before changing sampler mode"); + RequireSame(Sample(*device, Format8888UNorm, filter, SkipDegamma), unorm, "SKIP_DEGAMMA on UNORM", "with SKIP_DEGAMMA", "without SKIP_DEGAMMA"); + RequireSame(Sample(*device, Format8888Srgb, filter, SkipDegamma | ForceDegamma), srgb, "FORCE_DEGAMMA precedence", "with both sampler bits", "with neither sampler bit"); + std::puts("image sample skip degamma tests passed"); + return 0; + } FillInput(true); const auto forcedPoint = Sample(*device, Format8888Srgb, FilterPoint, ForceDegamma); RequireDecoded(forcedPoint, "8_8_8_8_SRGB"); diff --git a/core/libs/prx/libSceAgcDriver/tests/execution/SampleMultisampleTarget.cpp b/core/libs/prx/libSceAgcDriver/tests/execution/SampleMultisampleTarget.cpp index a467c4542b54..a99eeefabcf0 100644 --- a/core/libs/prx/libSceAgcDriver/tests/execution/SampleMultisampleTarget.cpp +++ b/core/libs/prx/libSceAgcDriver/tests/execution/SampleMultisampleTarget.cpp @@ -80,7 +80,9 @@ constexpr std::uint32_t Uint16AbgrExport = 7; constexpr std::uint32_t Format16161616Float = 71; constexpr std::uint32_t Unorm8x4SwappedInfo = 0x8828u; constexpr std::uint32_t Uint8x4Info = 0x8428u; +constexpr std::uint32_t Srgb8x4Info = 0x8628u; constexpr std::uint32_t Format8888Unorm = 56; +constexpr std::uint32_t Format8888Srgb = 130; constexpr std::uint32_t Format8888Uint = 60; constexpr std::uint32_t Format8888Sint = 61; constexpr std::uint32_t Format8Uint = 5; @@ -314,6 +316,20 @@ AgcDriver::Graphics::ColorTarget DecodeSwappedTarget(const Block& block) { return color; } +AgcDriver::Graphics::ColorTarget DecodeSrgbTarget(const Block& block) { + AgcDriver::Registers cx; + cx[0x318] = static_cast(block.Address() >> 8u); + cx[0x31b] = 0; + cx[0x31c] = Srgb8x4Info; + cx[0x31d] = MultisampledAttrib; + cx[0x3b0] = ((Side - 1u) << 14u) | (Side - 1u); + cx[0x3b8] = TiledAttrib3; + cx[0x390] = static_cast(block.Address() >> 40u); + const auto color = AgcDriver::Graphics::DecodeColorBuffer(cx, 0); + Require(color.format == VK_FORMAT_R8G8B8A8_SRGB && color.samples == Samples && color.extent.width == Side && color.extent.height == Side, "the sRGB 8_8_8_8 target did not decode as a 16x16 RGBA8 sRGB surface of 4 samples"); + return color; +} + AgcDriver::Graphics::ColorTarget DecodePackedFloatTarget(const Block& block) { AgcDriver::Registers cx; cx[0x318] = static_cast(block.Address() >> 8u); @@ -1033,6 +1049,22 @@ void RequireRefused(AgcDriver::VulkanDevice& device, const std::array& texture) { + auto sampler = PointMipSampler; + sampler[0] |= 1u << 31u; + std::string refusal; + try { + std::vector userData; + const auto program = CompileProgram(device, PlaneCode, texture, userData, sampler); + Buffer.fill(0u); + device.Dispatch(program, 1, 1, 1, {}, reinterpret_cast(PlaneCode.data())); + device.WaitIdle(); + } catch (const std::exception& error) { + refusal = error.what(); + } + Require(refusal.find("reading sample 0 of a multisampled sRGB texture through a sampler that skips degamma is not implemented") != std::string::npos, "sample 0 of a 4x target read as sRGB through a SKIP_DEGAMMA sampler was not refused: " + refusal); +} + std::vector> StoreInitial(const Block& block, std::uint32_t fragments, std::uint32_t pipeBankXor, bool uniform) { using namespace AgcDriver::Graphics; const MultisampleTextureLayout layout(TextureTileMode::kR64KBX, 8u, fragments, Side, Side, 1u, pipeBankXor); @@ -2570,6 +2602,10 @@ int main(int argc, char** argv) { const auto swappedSamples = LoadSamples(*device, swapped, Format8888Unorm); CheckSwappedSamples(swappedSamples); CheckFirstSampleReads(*device, TargetDescriptor(swapped, SampleShift, Format8888Unorm), swappedSamples, true, "from a 4x BGRA8 target read through an RGBA8 descriptor"); + Block srgbTarget(4u * BlockBytes); + Draw(*device, TargetState(DecodeSrgbTarget(srgbTarget)), 0, 2u, Unorm8PixelCode); + RequireSkipDegammaFirstSampleRefused(*device, TargetDescriptor(srgbTarget, SampleShift, Format8888Srgb)); + std::puts("sample 0 of a 4x sRGB color target read through a SKIP_DEGAMMA sampler is refused before descriptor creation"); std::puts("a 4x BGRA8 color target loaded through an RGBA8 texture descriptor reads its drawn color with red and blue swapped"); Block packedFloat(4u * BlockBytes); Draw(*device, TargetState(DecodePackedFloatTarget(packedFloat))); diff --git a/core/libs/prx/libSceAgcDriver/tests/execution/SampledCacheBudget.cpp b/core/libs/prx/libSceAgcDriver/tests/execution/SampledCacheBudget.cpp index 3c991b0ad0f0..ef06ebd2fd7f 100644 --- a/core/libs/prx/libSceAgcDriver/tests/execution/SampledCacheBudget.cpp +++ b/core/libs/prx/libSceAgcDriver/tests/execution/SampledCacheBudget.cpp @@ -54,6 +54,7 @@ using TestSupport::FetchTexels; constexpr std::uint32_t Side = 256; constexpr std::uint32_t Format32UInt = 20; +constexpr std::uint32_t Format8888Srgb = 130; constexpr std::uint32_t FormatBc5UNorm = 177; constexpr std::uint32_t TileR64KBX = 0x1b; constexpr std::uint32_t Type2D = 9; @@ -1775,6 +1776,19 @@ void lookupMemoProofTests(const Context& context, const std::string& mode) { Require(GuestAllocations::GuestAllocationsGeneration_nid_postfix() != allocations, "adding a guest allocation did not move the allocation generation"); dropped(surfaces[3]->Address(), "a guest allocation was added"); } + if (mode == "lookup-memo-proofs-unorm-view") { + ClearCachedTextures(context.device); + Recorder::CountPresent(); + const auto words = Descriptor(surfaces[0]->Address(), Side, Format8888Srgb); + const auto srgb = CachedSampledTexture(context, words); + Require(srgb != nullptr && CachedSampledTexture(context, words) == srgb, "the second sRGB lookup of an unchanged descriptor returned another texture"); + const auto before = FullSampledLookups(); + const auto unorm = CachedSampledTexture(context, words, VK_IMAGE_VIEW_TYPE_2D, true); + Require(unorm != nullptr && unorm != srgb, "a UNORM view lookup of an sRGB descriptor returned the sRGB texture"); + Require(FullSampledLookups() != before, "a UNORM view lookup of an sRGB descriptor was served from the memo of the sRGB lookup"); + Require(CachedSampledTexture(context, words) == srgb, "the sRGB lookup after a UNORM view lookup returned another texture"); + Require(CachedSampledTexture(context, words, VK_IMAGE_VIEW_TYPE_2D, true) == unorm, "the second UNORM view lookup returned another texture"); + } if (mode == "lookup-memo-proofs-import") { if (context.hostImportAlignment != 0) { remembered(surfaces[4]->Address(), "host import"); diff --git a/docs/dev/debt/changes/agc-skip-degamma-sampled-views.md b/docs/dev/debt/changes/agc-skip-degamma-sampled-views.md new file mode 100644 index 000000000000..00a6f50bdd78 --- /dev/null +++ b/docs/dev/debt/changes/agc-skip-degamma-sampled-views.md @@ -0,0 +1,5 @@ +# libSceAgcDriver + +## change [DISABLE_DEGAMMA] + +- [DISABLE_DEGAMMA](../../../core/libs/prx/libSceAgcDriver/Graphics/src/GuestSamplerResource.cpp) (libSceAgcDriver) - S# word 0 bit 31 (RDNA 2 SKIP_DEGAMMA) selects a compatible UNORM sampled view for a direct sRGB image, before filtering. Owning images request mutability only for SKIP; cache keys include that capability. Local gfx1032 hardware measurements cover R8, RG8, RGBA8, BC1/2/3/7, point and bilinear sampling, with two stable repeats: skipped sRGB equals UNORM in all64 lanes, and FORCE_DEGAMMA wins when both bits are set. Non-sRGB formats need no skip conversion. Bindless sRGB tables, textures without a compatible UNORM view, sample 0 of a multisampled sRGB texture read through a 2D image and an sRGB image shared by samplers requesting different decode modes still throw. The shader path for devices without native R8/RG8 sRGB sampling is not extended. PS5 gfx1013 equivalence and Windows/macOS are unverified.