diff --git a/Resources/Shaders/Vulkan/BasicImage.frag b/Resources/Shaders/Vulkan/BasicImage.frag index b3db1195..9837e69d 100644 --- a/Resources/Shaders/Vulkan/BasicImage.frag +++ b/Resources/Shaders/Vulkan/BasicImage.frag @@ -27,10 +27,25 @@ layout(location = 1) in vec2 texCoord; layout(location = 0) out vec4 fragColor; +// The swapchain attachment is VK_FORMAT_B8G8R8A8_SRGB: the hardware +// sRGB-ENCODES whatever this shader writes. But 2D UI textures are +// uploaded as UNORM (raw sRGB bytes sampled verbatim) and the GL +// renderer writes those bytes straight to a non-sRGB framebuffer. +// Without correction the value gets sRGB-encoded a second time -> +// washed-out minimap/UI. Fix: do all math in sRGB space exactly like +// GL, then DECODE to linear so the attachment's encode restores the +// original bytes. +vec3 srgbToLinear(vec3 c) { + bvec3 lo = lessThanEqual(c, vec3(0.04045)); + vec3 linLo = c / 12.92; + vec3 linHi = pow((c + 0.055) / 1.055, vec3(2.4)); + return mix(linHi, linLo, vec3(lo)); +} + void main() { vec2 flippedTexCoord = vec2(texCoord.x, 1.0 - texCoord.y); vec4 col = texture(mainTexture, flippedTexCoord); col.xyz *= col.w; col *= color; - fragColor = col; + fragColor = vec4(srgbToLinear(col.xyz), col.w); } diff --git a/Sources/Draw/Vulkan/VulkanFlatMapRenderer.cpp b/Sources/Draw/Vulkan/VulkanFlatMapRenderer.cpp index c3c55eb9..bec457af 100644 --- a/Sources/Draw/Vulkan/VulkanFlatMapRenderer.cpp +++ b/Sources/Draw/Vulkan/VulkanFlatMapRenderer.cpp @@ -20,6 +20,8 @@ #include "VulkanFlatMapRenderer.h" #include "VulkanRenderer.h" +#include "VulkanImage.h" +#include "VulkanImageWrapper.h" #include #include #include @@ -39,6 +41,13 @@ namespace spades { Handle bmp(GenerateBitmap(0, 0, m.Width(), m.Height()), false); image = renderer.CreateImage(*bmp); + + auto* wrapper = dynamic_cast(image.GetPointerOrNull()); + if (wrapper && wrapper->GetVulkanImage()) { + wrapper->GetVulkanImage()->CreateSampler( + VK_FILTER_NEAREST, VK_FILTER_NEAREST, + VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE, false); + } } VulkanFlatMapRenderer::~VulkanFlatMapRenderer() {} diff --git a/Sources/Draw/Vulkan/VulkanImage.cpp b/Sources/Draw/Vulkan/VulkanImage.cpp index 1702ab26..7c70cf6b 100644 --- a/Sources/Draw/Vulkan/VulkanImage.cpp +++ b/Sources/Draw/Vulkan/VulkanImage.cpp @@ -192,6 +192,10 @@ namespace spades { void VulkanImage::CreateSampler(VkFilter magFilter, VkFilter minFilter, VkSamplerAddressMode addressMode, bool enableAnisotropy) { + if (sampler != VK_NULL_HANDLE) { + vkDestroySampler(device->GetDevice(), sampler, nullptr); + sampler = VK_NULL_HANDLE; + } VkSamplerCreateInfo samplerInfo{}; samplerInfo.sType = VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO; samplerInfo.magFilter = magFilter;