From ba759f5bc0b5dec3bc5cf488b108795e381a6096 Mon Sep 17 00:00:00 2001 From: niki Date: Mon, 19 Sep 2022 14:23:46 +0200 Subject: [PATCH] vulkan: use SamplerState::toKey for caching --- src/modules/graphics/Texture.h | 4 ---- src/modules/graphics/vulkan/Graphics.cpp | 13 ++++++------- src/modules/graphics/vulkan/Graphics.h | 14 +++----------- src/modules/graphics/vulkan/Texture.cpp | 5 +++-- 4 files changed, 12 insertions(+), 24 deletions(-) diff --git a/src/modules/graphics/Texture.h b/src/modules/graphics/Texture.h index de2f7bbab..17402ac57 100644 --- a/src/modules/graphics/Texture.h +++ b/src/modules/graphics/Texture.h @@ -139,10 +139,6 @@ struct SamplerState static bool getConstant(const char *in, WrapMode &out); static bool getConstant(WrapMode in, const char *&out); static std::vector getConstants(WrapMode); - - bool operator==(const SamplerState& other) const { - return memcmp(this, &other, sizeof(SamplerState)) == 0; - } }; /** diff --git a/src/modules/graphics/vulkan/Graphics.cpp b/src/modules/graphics/vulkan/Graphics.cpp index ccf32703c..882089e8d 100644 --- a/src/modules/graphics/vulkan/Graphics.cpp +++ b/src/modules/graphics/vulkan/Graphics.cpp @@ -2495,10 +2495,9 @@ void Graphics::endRenderPass() Vulkan::cmdTransitionImageLayout(commandBuffers.at(currentFrame), image, VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL, VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL); } -VkSampler Graphics::createSampler(const SamplerState &samplerState) +VkSampler Graphics::createSampler(uint64 samplerKey) { - VkPhysicalDeviceProperties properties{}; - vkGetPhysicalDeviceProperties(physicalDevice, &properties); + auto samplerState = SamplerState::fromKey(samplerKey); VkSamplerCreateInfo samplerInfo{}; samplerInfo.sType = VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO; @@ -2575,15 +2574,15 @@ std::set &Graphics::getUsedShadersInFrame() return usedShadersInFrame; } -VkSampler Graphics::getCachedSampler(const SamplerState &samplerState) +VkSampler Graphics::getCachedSampler(uint64 samplerkey) { - auto it = samplers.find(samplerState); + auto it = samplers.find(samplerkey); if (it != samplers.end()) return it->second; else { - VkSampler sampler = createSampler(samplerState); - samplers.insert({samplerState, sampler}); + VkSampler sampler = createSampler(samplerkey); + samplers.insert({ samplerkey, sampler }); return sampler; } } diff --git a/src/modules/graphics/vulkan/Graphics.h b/src/modules/graphics/vulkan/Graphics.h index 253a78bdf..e49f44d41 100644 --- a/src/modules/graphics/vulkan/Graphics.h +++ b/src/modules/graphics/vulkan/Graphics.h @@ -193,14 +193,6 @@ struct GraphicsPipelineConfigurationHasher } }; -struct SamplerStateHasher -{ - size_t operator()(const SamplerState &samplerState) const - { - return XXH32(&samplerState, sizeof(SamplerState), 0); - } -}; - struct BatchedDrawBuffers { StreamBuffer *vertexBuffer1; @@ -316,7 +308,7 @@ public: uint32_t getFrameIndex() const; const VkDeviceSize getMinUniformBufferOffsetAlignment() const; graphics::Texture *getDefaultTexture() const; - VkSampler getCachedSampler(const SamplerState &samplerState); + VkSampler getCachedSampler(uint64); void setComputeShader(Shader *computeShader); std::set &getUsedShadersInFrame(); graphics::Shader::BuiltinUniformData getCurrentBuiltinUniformData(); @@ -387,7 +379,7 @@ private: void setDefaultRenderPass(); void startRenderPass(); void endRenderPass(); - VkSampler createSampler(const SamplerState &samplerState); + VkSampler createSampler(uint64 samplerKey); void cleanupUnusedObjects(); uint32_t vulkanApiVersion = VK_VERSION_1_0; @@ -423,7 +415,7 @@ private: std::unordered_map renderPassUsages; std::unordered_map framebufferUsages; std::unordered_map pipelineUsages; - std::unordered_map samplers; + std::unordered_map samplers; VkCommandPool commandPool = VK_NULL_HANDLE; std::vector commandBuffers; Shader* computeShader = nullptr; diff --git a/src/modules/graphics/vulkan/Texture.cpp b/src/modules/graphics/vulkan/Texture.cpp index 38262d5f1..0178cf183 100644 --- a/src/modules/graphics/vulkan/Texture.cpp +++ b/src/modules/graphics/vulkan/Texture.cpp @@ -152,7 +152,7 @@ bool Texture::loadVolatile() clear(); createTextureImageView(); - textureSampler = vgfx->getCachedSampler(samplerState); + textureSampler = vgfx->getCachedSampler(samplerState.toKey()); if (!isPixelFormatDepthStencil(format) && mipmapCount > 1 && getMipmapsMode() != MIPMAPS_NONE) generateMipmaps(); @@ -250,7 +250,8 @@ ptrdiff_t Texture::getHandle() const void Texture::setSamplerState(const SamplerState &s) { love::graphics::Texture::setSamplerState(s); - textureSampler = vgfx->getCachedSampler(s); + + textureSampler = vgfx->getCachedSampler(s.toKey()); } VkImageLayout Texture::getImageLayout() const