Line data Source code
1 : /* Copyright (C) 2026 Wildfire Games. 2 : * This file is part of 0 A.D. 3 : * 4 : * 0 A.D. is free software: you can redistribute it and/or modify 5 : * it under the terms of the GNU General Public License as published by 6 : * the Free Software Foundation, either version 2 of the License, or 7 : * (at your option) any later version. 8 : * 9 : * 0 A.D. is distributed in the hope that it will be useful, 10 : * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 : * GNU General Public License for more details. 13 : * 14 : * You should have received a copy of the GNU General Public License 15 : * along with 0 A.D. If not, see <http://www.gnu.org/licenses/>. 16 : */ 17 : 18 : #include "precompiled.h" 19 : 20 : #include "SamplerManager.h" 21 : 22 : #include "lib/hash.h" 23 : #include "ps/CLogger.h" 24 : #include "renderer/backend/IDevice.h" 25 : #include "renderer/backend/vulkan/Device.h" 26 : #include "renderer/backend/vulkan/Mapping.h" 27 : #include "renderer/backend/vulkan/Utilities.h" 28 : 29 : #include <algorithm> 30 : #include <utility> 31 : 32 : namespace Renderer 33 : { 34 : 35 : namespace Backend 36 : { 37 : 38 : namespace Vulkan 39 : { 40 : 41 0 : size_t CSamplerManager::SamplerDescHash::operator()(const Sampler::Desc& samplerDesc) const 42 : { 43 0 : size_t seed = 0; 44 : 45 0 : hash_combine(seed, samplerDesc.magFilter); 46 0 : hash_combine(seed, samplerDesc.minFilter); 47 0 : hash_combine(seed, samplerDesc.mipFilter); 48 0 : hash_combine(seed, samplerDesc.addressModeU); 49 0 : hash_combine(seed, samplerDesc.addressModeV); 50 0 : hash_combine(seed, samplerDesc.addressModeW); 51 : 52 0 : hash_combine(seed, samplerDesc.mipLODBias); 53 0 : hash_combine(seed, samplerDesc.anisotropyEnabled); 54 0 : hash_combine(seed, samplerDesc.maxAnisotropy); 55 : 56 0 : hash_combine(seed, samplerDesc.borderColor); 57 0 : hash_combine(seed, samplerDesc.compareEnabled); 58 0 : hash_combine(seed, samplerDesc.compareOp); 59 : 60 0 : return seed; 61 : } 62 : 63 0 : bool CSamplerManager::SamplerDescEqual::operator()(const Sampler::Desc& lhs, const Sampler::Desc& rhs) const 64 : { 65 0 : return 66 0 : lhs.magFilter == rhs.magFilter && 67 0 : lhs.minFilter == rhs.minFilter && 68 0 : lhs.mipFilter == rhs.mipFilter && 69 0 : lhs.addressModeU == rhs.addressModeU && 70 0 : lhs.addressModeV == rhs.addressModeV && 71 0 : lhs.addressModeW == rhs.addressModeW && 72 0 : lhs.mipLODBias == rhs.mipLODBias && 73 0 : lhs.anisotropyEnabled == rhs.anisotropyEnabled && 74 0 : lhs.maxAnisotropy == rhs.maxAnisotropy && 75 0 : lhs.borderColor == rhs.borderColor && 76 0 : lhs.compareEnabled == rhs.compareEnabled && 77 0 : lhs.compareOp == rhs.compareOp; 78 : } 79 : 80 0 : CSamplerManager::CSamplerManager(CDevice* device) 81 0 : : m_Device(device) 82 : { 83 0 : } 84 : 85 0 : CSamplerManager::~CSamplerManager() 86 : { 87 0 : for (const auto& it : m_SamplerMap) 88 0 : if (it.second != VK_NULL_HANDLE) 89 : { 90 0 : m_Device->ScheduleObjectToDestroy( 91 0 : VK_OBJECT_TYPE_SAMPLER, it.second, VK_NULL_HANDLE); 92 : } 93 0 : m_SamplerMap.clear(); 94 0 : } 95 : 96 0 : VkSampler CSamplerManager::GetOrCreateSampler( 97 : const Sampler::Desc& samplerDesc) 98 : { 99 0 : auto it = m_SamplerMap.find(samplerDesc); 100 0 : if (it != m_SamplerMap.end()) 101 0 : return it->second; 102 : 103 0 : const IDevice::Capabilities& capabilities = m_Device->GetCapabilities(); 104 : 105 0 : VkSamplerCreateInfo samplerCreateInfo{}; 106 0 : samplerCreateInfo.sType = VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO; 107 0 : samplerCreateInfo.magFilter = samplerDesc.magFilter == Sampler::Filter::LINEAR ? VK_FILTER_LINEAR : VK_FILTER_NEAREST; 108 0 : samplerCreateInfo.minFilter = samplerDesc.minFilter == Sampler::Filter::LINEAR ? VK_FILTER_LINEAR : VK_FILTER_NEAREST; 109 0 : samplerCreateInfo.mipmapMode = samplerDesc.mipFilter == Sampler::Filter::LINEAR ? VK_SAMPLER_MIPMAP_MODE_LINEAR : VK_SAMPLER_MIPMAP_MODE_NEAREST; 110 0 : samplerCreateInfo.addressModeU = Mapping::FromAddressMode(samplerDesc.addressModeU); 111 0 : samplerCreateInfo.addressModeV = Mapping::FromAddressMode(samplerDesc.addressModeV); 112 0 : samplerCreateInfo.addressModeW = Mapping::FromAddressMode(samplerDesc.addressModeW); 113 0 : samplerCreateInfo.mipLodBias = samplerDesc.mipLODBias; 114 0 : if (samplerDesc.anisotropyEnabled && capabilities.anisotropicFiltering) 115 : { 116 0 : samplerCreateInfo.anisotropyEnable = VK_TRUE; 117 0 : samplerCreateInfo.maxAnisotropy = std::min(samplerDesc.maxAnisotropy, capabilities.maxAnisotropy); 118 : } 119 0 : samplerCreateInfo.compareEnable = samplerDesc.compareEnabled ? VK_TRUE : VK_FALSE; 120 0 : samplerCreateInfo.compareOp = Mapping::FromCompareOp(samplerDesc.compareOp); 121 0 : samplerCreateInfo.minLod = -1000.0f; 122 0 : samplerCreateInfo.maxLod = 1000.0f; 123 0 : switch (samplerDesc.borderColor) 124 : { 125 0 : case Sampler::BorderColor::TRANSPARENT_BLACK: 126 0 : samplerCreateInfo.borderColor = VK_BORDER_COLOR_FLOAT_TRANSPARENT_BLACK; 127 0 : break; 128 0 : case Sampler::BorderColor::OPAQUE_BLACK: 129 0 : samplerCreateInfo.borderColor = VK_BORDER_COLOR_FLOAT_OPAQUE_BLACK; 130 0 : break; 131 0 : case Sampler::BorderColor::OPAQUE_WHITE: 132 0 : samplerCreateInfo.borderColor = VK_BORDER_COLOR_FLOAT_OPAQUE_WHITE; 133 0 : break; 134 : } 135 : 136 0 : VkSampler sampler = VK_NULL_HANDLE; 137 0 : ENSURE_VK_SUCCESS(vkCreateSampler( 138 : m_Device->GetVkDevice(), &samplerCreateInfo, nullptr, &sampler)); 139 0 : it = m_SamplerMap.emplace(samplerDesc, sampler).first; 140 : 141 0 : return sampler; 142 : } 143 : 144 0 : void CSamplerManager::CollectStatistics(IDevice::StatisticsVector& statistics) const 145 : { 146 0 : statistics.emplace_back("Samplers count", "", static_cast<uint32_t>(m_SamplerMap.size())); 147 0 : } 148 : 149 : } // namespace Vulkan 150 : 151 : } // namespace Backend 152 : 153 : } // namespace Renderer