diff options
| author | 2019-03-12 16:57:03 -0300 | |
|---|---|---|
| committer | 2019-03-12 20:20:57 -0300 | |
| commit | aa59d77c3bd7745f03e9bc1d1dca17bb0c338c1f (patch) | |
| tree | db24e62b8d523488b1ac028e8816e7792fbf5638 /src | |
| parent | video_core/texture: Add a raw representation of TSCEntry (diff) | |
| download | yuzu-aa59d77c3bd7745f03e9bc1d1dca17bb0c338c1f.tar.gz yuzu-aa59d77c3bd7745f03e9bc1d1dca17bb0c338c1f.tar.xz yuzu-aa59d77c3bd7745f03e9bc1d1dca17bb0c338c1f.zip | |
vk_sampler_cache: Implement a sampler cache
Diffstat (limited to 'src')
| -rw-r--r-- | src/video_core/CMakeLists.txt | 2 | ||||
| -rw-r--r-- | src/video_core/renderer_vulkan/vk_sampler_cache.cpp | 81 | ||||
| -rw-r--r-- | src/video_core/renderer_vulkan/vk_sampler_cache.h | 56 | ||||
| -rw-r--r-- | src/video_core/textures/texture.h | 2 |
4 files changed, 140 insertions, 1 deletions
diff --git a/src/video_core/CMakeLists.txt b/src/video_core/CMakeLists.txt index 0c3038c52..14b76680f 100644 --- a/src/video_core/CMakeLists.txt +++ b/src/video_core/CMakeLists.txt | |||
| @@ -123,6 +123,8 @@ if (ENABLE_VULKAN) | |||
| 123 | renderer_vulkan/vk_memory_manager.h | 123 | renderer_vulkan/vk_memory_manager.h |
| 124 | renderer_vulkan/vk_resource_manager.cpp | 124 | renderer_vulkan/vk_resource_manager.cpp |
| 125 | renderer_vulkan/vk_resource_manager.h | 125 | renderer_vulkan/vk_resource_manager.h |
| 126 | renderer_vulkan/vk_sampler_cache.cpp | ||
| 127 | renderer_vulkan/vk_sampler_cache.h | ||
| 126 | renderer_vulkan/vk_scheduler.cpp | 128 | renderer_vulkan/vk_scheduler.cpp |
| 127 | renderer_vulkan/vk_scheduler.h | 129 | renderer_vulkan/vk_scheduler.h |
| 128 | renderer_vulkan/vk_stream_buffer.cpp | 130 | renderer_vulkan/vk_stream_buffer.cpp |
diff --git a/src/video_core/renderer_vulkan/vk_sampler_cache.cpp b/src/video_core/renderer_vulkan/vk_sampler_cache.cpp new file mode 100644 index 000000000..f099d78ab --- /dev/null +++ b/src/video_core/renderer_vulkan/vk_sampler_cache.cpp | |||
| @@ -0,0 +1,81 @@ | |||
| 1 | // Copyright 2019 yuzu Emulator Project | ||
| 2 | // Licensed under GPLv2 or any later version | ||
| 3 | // Refer to the license.txt file included. | ||
| 4 | |||
| 5 | #include <cstring> | ||
| 6 | #include <optional> | ||
| 7 | #include <unordered_map> | ||
| 8 | |||
| 9 | #include "common/assert.h" | ||
| 10 | #include "common/cityhash.h" | ||
| 11 | #include "video_core/renderer_vulkan/declarations.h" | ||
| 12 | #include "video_core/renderer_vulkan/maxwell_to_vk.h" | ||
| 13 | #include "video_core/renderer_vulkan/vk_sampler_cache.h" | ||
| 14 | #include "video_core/textures/texture.h" | ||
| 15 | |||
| 16 | namespace Vulkan { | ||
| 17 | |||
| 18 | static std::optional<vk::BorderColor> TryConvertBorderColor(std::array<float, 4> color) { | ||
| 19 | // TODO(Rodrigo): Manage integer border colors | ||
| 20 | if (color == std::array<float, 4>{0, 0, 0, 0}) { | ||
| 21 | return vk::BorderColor::eFloatTransparentBlack; | ||
| 22 | } else if (color == std::array<float, 4>{0, 0, 0, 1}) { | ||
| 23 | return vk::BorderColor::eFloatOpaqueBlack; | ||
| 24 | } else if (color == std::array<float, 4>{1, 1, 1, 1}) { | ||
| 25 | return vk::BorderColor::eFloatOpaqueWhite; | ||
| 26 | } else { | ||
| 27 | return {}; | ||
| 28 | } | ||
| 29 | } | ||
| 30 | |||
| 31 | std::size_t SamplerCacheKey::Hash() const { | ||
| 32 | static_assert(sizeof(raw) % sizeof(u64) == 0); | ||
| 33 | return static_cast<std::size_t>( | ||
| 34 | Common::CityHash64(reinterpret_cast<const char*>(raw.data()), sizeof(raw) / sizeof(u64))); | ||
| 35 | } | ||
| 36 | |||
| 37 | bool SamplerCacheKey::operator==(const SamplerCacheKey& rhs) const { | ||
| 38 | return std::memcmp(raw.data(), rhs.raw.data(), sizeof(raw)) == 0; | ||
| 39 | } | ||
| 40 | |||
| 41 | VKSamplerCache::VKSamplerCache(const VKDevice& device) : device{device} {} | ||
| 42 | |||
| 43 | VKSamplerCache::~VKSamplerCache() = default; | ||
| 44 | |||
| 45 | vk::Sampler VKSamplerCache::GetSampler(const Tegra::Texture::TSCEntry& tsc) { | ||
| 46 | const auto [entry, is_cache_miss] = cache.try_emplace(SamplerCacheKey{tsc}); | ||
| 47 | auto& sampler = entry->second; | ||
| 48 | if (is_cache_miss) { | ||
| 49 | sampler = CreateSampler(tsc); | ||
| 50 | } | ||
| 51 | return *sampler; | ||
| 52 | } | ||
| 53 | |||
| 54 | UniqueSampler VKSamplerCache::CreateSampler(const Tegra::Texture::TSCEntry& tsc) { | ||
| 55 | const float max_anisotropy = tsc.GetMaxAnisotropy(); | ||
| 56 | const bool has_anisotropy = max_anisotropy > 1.0f; | ||
| 57 | |||
| 58 | const auto border_color = tsc.GetBorderColor(); | ||
| 59 | const auto vk_border_color = TryConvertBorderColor(border_color); | ||
| 60 | UNIMPLEMENTED_IF_MSG(!vk_border_color, "Unimplemented border color {} {} {} {}", | ||
| 61 | border_color[0], border_color[1], border_color[2], border_color[3]); | ||
| 62 | |||
| 63 | constexpr bool unnormalized_coords = false; | ||
| 64 | |||
| 65 | const vk::SamplerCreateInfo sampler_ci( | ||
| 66 | {}, MaxwellToVK::Sampler::Filter(tsc.mag_filter), | ||
| 67 | MaxwellToVK::Sampler::Filter(tsc.min_filter), | ||
| 68 | MaxwellToVK::Sampler::MipmapMode(tsc.mipmap_filter), | ||
| 69 | MaxwellToVK::Sampler::WrapMode(tsc.wrap_u), MaxwellToVK::Sampler::WrapMode(tsc.wrap_v), | ||
| 70 | MaxwellToVK::Sampler::WrapMode(tsc.wrap_p), tsc.GetLodBias(), has_anisotropy, | ||
| 71 | max_anisotropy, tsc.depth_compare_enabled, | ||
| 72 | MaxwellToVK::Sampler::DepthCompareFunction(tsc.depth_compare_func), tsc.GetMinLod(), | ||
| 73 | tsc.GetMaxLod(), vk_border_color.value_or(vk::BorderColor::eFloatTransparentBlack), | ||
| 74 | unnormalized_coords); | ||
| 75 | |||
| 76 | const auto& dld = device.GetDispatchLoader(); | ||
| 77 | const auto dev = device.GetLogical(); | ||
| 78 | return dev.createSamplerUnique(sampler_ci, nullptr, dld); | ||
| 79 | } | ||
| 80 | |||
| 81 | } // namespace Vulkan | ||
diff --git a/src/video_core/renderer_vulkan/vk_sampler_cache.h b/src/video_core/renderer_vulkan/vk_sampler_cache.h new file mode 100644 index 000000000..c6394dc87 --- /dev/null +++ b/src/video_core/renderer_vulkan/vk_sampler_cache.h | |||
| @@ -0,0 +1,56 @@ | |||
| 1 | // Copyright 2019 yuzu Emulator Project | ||
| 2 | // Licensed under GPLv2 or any later version | ||
| 3 | // Refer to the license.txt file included. | ||
| 4 | |||
| 5 | #pragma once | ||
| 6 | |||
| 7 | #include <unordered_map> | ||
| 8 | |||
| 9 | #include "common/common_types.h" | ||
| 10 | #include "video_core/renderer_vulkan/declarations.h" | ||
| 11 | #include "video_core/textures/texture.h" | ||
| 12 | |||
| 13 | namespace Vulkan { | ||
| 14 | |||
| 15 | class VKDevice; | ||
| 16 | |||
| 17 | struct SamplerCacheKey final : public Tegra::Texture::TSCEntry { | ||
| 18 | std::size_t Hash() const; | ||
| 19 | |||
| 20 | bool operator==(const SamplerCacheKey& rhs) const; | ||
| 21 | |||
| 22 | bool operator!=(const SamplerCacheKey& rhs) const { | ||
| 23 | return !operator==(rhs); | ||
| 24 | } | ||
| 25 | }; | ||
| 26 | |||
| 27 | } // namespace Vulkan | ||
| 28 | |||
| 29 | namespace std { | ||
| 30 | |||
| 31 | template <> | ||
| 32 | struct hash<Vulkan::SamplerCacheKey> { | ||
| 33 | std::size_t operator()(const Vulkan::SamplerCacheKey& k) const noexcept { | ||
| 34 | return k.Hash(); | ||
| 35 | } | ||
| 36 | }; | ||
| 37 | |||
| 38 | } // namespace std | ||
| 39 | |||
| 40 | namespace Vulkan { | ||
| 41 | |||
| 42 | class VKSamplerCache { | ||
| 43 | public: | ||
| 44 | explicit VKSamplerCache(const VKDevice& device); | ||
| 45 | ~VKSamplerCache(); | ||
| 46 | |||
| 47 | vk::Sampler GetSampler(const Tegra::Texture::TSCEntry& tsc); | ||
| 48 | |||
| 49 | private: | ||
| 50 | UniqueSampler CreateSampler(const Tegra::Texture::TSCEntry& tsc); | ||
| 51 | |||
| 52 | const VKDevice& device; | ||
| 53 | std::unordered_map<SamplerCacheKey, UniqueSampler> cache; | ||
| 54 | }; | ||
| 55 | |||
| 56 | } // namespace Vulkan | ||
diff --git a/src/video_core/textures/texture.h b/src/video_core/textures/texture.h index a2b98245b..ff1165053 100644 --- a/src/video_core/textures/texture.h +++ b/src/video_core/textures/texture.h | |||
| @@ -311,7 +311,7 @@ struct TSCEntry { | |||
| 311 | }; | 311 | }; |
| 312 | std::array<f32, 4> border_color; | 312 | std::array<f32, 4> border_color; |
| 313 | }; | 313 | }; |
| 314 | std::array<u32, 8> raw; | 314 | std::array<u8, 0x20> raw; |
| 315 | }; | 315 | }; |
| 316 | 316 | ||
| 317 | float GetMaxAnisotropy() const { | 317 | float GetMaxAnisotropy() const { |