From 2ebae034a49db1e9a18614bbdc2a461088b2980f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Henrik=20Rydg=C3=A5rd?= Date: Wed, 6 Dec 2017 16:01:56 +0100 Subject: [PATCH] Vulkan: Show samplers in "shader debug" --- Common/Hashmaps.h | 4 ++-- GPU/Common/ShaderCommon.h | 1 + GPU/Common/TextureCacheCommon.h | 7 +++++++ GPU/Vulkan/GPU_Vulkan.cpp | 12 ++++++++++-- GPU/Vulkan/TextureCacheVulkan.cpp | 32 +++++++++++++++++++++++++++++++ GPU/Vulkan/TextureCacheVulkan.h | 6 ++++++ UI/DevScreens.cpp | 1 + 7 files changed, 59 insertions(+), 4 deletions(-) diff --git a/Common/Hashmaps.h b/Common/Hashmaps.h index 941c24a646..b716321e3e 100644 --- a/Common/Hashmaps.h +++ b/Common/Hashmaps.h @@ -121,7 +121,7 @@ public: } template - inline void Iterate(T func) { + inline void Iterate(T func) const { for (size_t i = 0; i < map.size(); i++) { if (state[i] == BucketState::TAKEN) { func(map[i].key, map[i].value); @@ -267,7 +267,7 @@ public: } template - void Iterate(T func) { + void Iterate(T func) const { for (size_t i = 0; i < map.size(); i++) { if (state[i] == BucketState::TAKEN) { func(map[i].hash, map[i].value); diff --git a/GPU/Common/ShaderCommon.h b/GPU/Common/ShaderCommon.h index 5f17e236dc..c78318f927 100644 --- a/GPU/Common/ShaderCommon.h +++ b/GPU/Common/ShaderCommon.h @@ -35,6 +35,7 @@ enum DebugShaderType { SHADER_TYPE_VERTEXLOADER = 3, // Not really a shader, but might as well re-use this mechanism SHADER_TYPE_PIPELINE = 4, // Vulkan and DX12 combines a bunch of state into pipeline objects. Might as well make them inspectable. SHADER_TYPE_DEPAL = 5, + SHADER_TYPE_SAMPLER = 6, // Not really a shader either. Need to rename this enum... }; enum DebugShaderStringType { diff --git a/GPU/Common/TextureCacheCommon.h b/GPU/Common/TextureCacheCommon.h index 5289123f81..868b313629 100644 --- a/GPU/Common/TextureCacheCommon.h +++ b/GPU/Common/TextureCacheCommon.h @@ -78,6 +78,13 @@ struct SamplerCacheKey { bool operator < (const SamplerCacheKey &other) const { return fullKey < other.fullKey; } + void ToString(std::string *str) const { + str->resize(sizeof(*this)); + memcpy(&(*str)[0], this, sizeof(*this)); + } + void FromString(const std::string &str) { + memcpy(this, &str[0], sizeof(*this)); + } }; // TODO: Shrink this struct. There is some fluff. diff --git a/GPU/Vulkan/GPU_Vulkan.cpp b/GPU/Vulkan/GPU_Vulkan.cpp index e2b184a4a0..869ec3fb99 100644 --- a/GPU/Vulkan/GPU_Vulkan.cpp +++ b/GPU/Vulkan/GPU_Vulkan.cpp @@ -715,8 +715,12 @@ std::vector GPU_Vulkan::DebugGetShaderIDs(DebugShaderType type) { } else if (type == SHADER_TYPE_DEPAL) { ///... return std::vector(); - } else { + } else if (type == SHADER_TYPE_VERTEX || type == SHADER_TYPE_FRAGMENT) { return shaderManagerVulkan_->DebugGetShaderIDs(type); + } else if (type == SHADER_TYPE_SAMPLER) { + return textureCacheVulkan_->DebugGetSamplerIDs(); + } else { + return std::vector(); } } @@ -727,7 +731,11 @@ std::string GPU_Vulkan::DebugGetShaderString(std::string id, DebugShaderType typ return pipelineManager_->DebugGetObjectString(id, type, stringType); } else if (type == SHADER_TYPE_DEPAL) { return ""; - } else { + } else if (type == SHADER_TYPE_SAMPLER) { + return textureCacheVulkan_->DebugGetSamplerString(id, stringType); + } else if (type == SHADER_TYPE_VERTEX || type == SHADER_TYPE_FRAGMENT) { return shaderManagerVulkan_->DebugGetShaderString(id, type, stringType); + } else { + return std::string(); } } diff --git a/GPU/Vulkan/TextureCacheVulkan.cpp b/GPU/Vulkan/TextureCacheVulkan.cpp index d16bdbea5a..a75a939ce6 100644 --- a/GPU/Vulkan/TextureCacheVulkan.cpp +++ b/GPU/Vulkan/TextureCacheVulkan.cpp @@ -109,6 +109,20 @@ VkSampler SamplerCache::GetOrCreateSampler(const SamplerCacheKey &key) { return sampler; } +std::string SamplerCache::DebugGetSamplerString(std::string id, DebugShaderStringType stringType) { + SamplerCacheKey key; + key.FromString(id); + return StringFromFormat("%s/%s mag:%s min:%s mip:%s maxLod:%f minLod:%f bias:%f", + key.sClamp ? "Clamp" : "Wrap", + key.tClamp ? "Clamp" : "Wrap", + key.magFilt ? "Linear" : "Nearest", + key.minFilt ? "Linear" : "Nearest", + key.mipFilt ? "Linear" : "Nearest", + key.maxLevel / 256.0f, + key.minLevel / 256.0f, + key.lodBias / 256.0f); +} + void SamplerCache::DeviceLost() { cache_.Iterate([&](const SamplerCacheKey &key, VkSampler sampler) { vulkan_->Delete().QueueDeleteSampler(sampler); @@ -120,6 +134,16 @@ void SamplerCache::DeviceRestore(VulkanContext *vulkan) { vulkan_ = vulkan; } +std::vector SamplerCache::DebugGetSamplerIDs() const { + std::vector ids; + cache_.Iterate([&](const SamplerCacheKey &id, VkSampler sampler) { + std::string idstr; + id.ToString(&idstr); + ids.push_back(idstr); + }); + return ids; +} + TextureCacheVulkan::TextureCacheVulkan(Draw::DrawContext *draw, VulkanContext *vulkan) : TextureCacheCommon(draw), vulkan_(vulkan), @@ -823,3 +847,11 @@ void TextureCacheVulkan::GetStats(char *ptr, size_t size) { snprintf(ptr, size, "Alloc: %d slabs\nSlab min/max: %d/%d\nAlloc usage: %d%%", allocator_->GetBlockCount(), allocator_->GetMinSlabSize(), allocator_->GetMaxSlabSize(), allocator_->ComputeUsagePercent()); } + +std::vector TextureCacheVulkan::DebugGetSamplerIDs() const { + return samplerCache_.DebugGetSamplerIDs(); +} + +std::string TextureCacheVulkan::DebugGetSamplerString(std::string id, DebugShaderStringType stringType) { + return samplerCache_.DebugGetSamplerString(id, stringType); +} \ No newline at end of file diff --git a/GPU/Vulkan/TextureCacheVulkan.h b/GPU/Vulkan/TextureCacheVulkan.h index 30ca6cd166..23d17351f2 100644 --- a/GPU/Vulkan/TextureCacheVulkan.h +++ b/GPU/Vulkan/TextureCacheVulkan.h @@ -56,6 +56,9 @@ public: void DeviceLost(); void DeviceRestore(VulkanContext *vulkan); + std::vector DebugGetSamplerIDs() const; + std::string DebugGetSamplerString(std::string id, DebugShaderStringType stringType); + private: VulkanContext *vulkan_; DenseHashMap cache_; @@ -111,6 +114,9 @@ public: VulkanDeviceAllocator *GetAllocator() { return allocator_; } + std::vector DebugGetSamplerIDs() const; + std::string DebugGetSamplerString(std::string id, DebugShaderStringType stringType); + protected: void BindTexture(TexCacheEntry *entry) override; void Unbind() override; diff --git a/UI/DevScreens.cpp b/UI/DevScreens.cpp index dfd6e9e876..d69db27b3f 100644 --- a/UI/DevScreens.cpp +++ b/UI/DevScreens.cpp @@ -921,6 +921,7 @@ struct { DebugShaderType type; const char *name; } shaderTypes[] = { { SHADER_TYPE_VERTEXLOADER, "VertexLoader" }, { SHADER_TYPE_PIPELINE, "Pipeline" }, { SHADER_TYPE_DEPAL, "Depal" }, + { SHADER_TYPE_SAMPLER, "Sampler" }, }; void ShaderListScreen::CreateViews() {