From ceaa2c30f5f5913721b0416143c83313cec664f0 Mon Sep 17 00:00:00 2001 From: bailwillharr Date: Tue, 15 Nov 2022 13:59:43 +0000 Subject: [PATCH] Do some things --- include/gfx.hpp | 5 +++++ include/gfx_device.hpp | 2 +- include/resources/texture.hpp | 2 +- src/gfx_device_vulkan.cpp | 21 ++++++++++++++++++--- src/resources/texture.cpp | 9 +++++++-- 5 files changed, 32 insertions(+), 7 deletions(-) diff --git a/include/gfx.hpp b/include/gfx.hpp index 5b5862d..e54dc37 100644 --- a/include/gfx.hpp +++ b/include/gfx.hpp @@ -30,6 +30,11 @@ namespace engine::gfx { VEC3, }; + enum class TextureFilter { + LINEAR, + NEAREST, + }; + struct VertexBufferDesc { uint64_t size; }; diff --git a/include/gfx_device.hpp b/include/gfx_device.hpp index 6fdda92..1da1856 100644 --- a/include/gfx_device.hpp +++ b/include/gfx_device.hpp @@ -37,7 +37,7 @@ namespace engine { gfx::Buffer* createBuffer(gfx::BufferType type, uint64_t size, const void* data); void destroyBuffer(const gfx::Buffer* buffer); - gfx::Texture* createTexture(const void* imageData, uint32_t w, uint32_t h); + gfx::Texture* createTexture(const void* imageData, uint32_t w, uint32_t h, gfx::TextureFilter minFilter, gfx::TextureFilter magFilter); void destroyTexture(const gfx::Texture* texture); // wait until all the active GPU queues have finished working diff --git a/include/resources/texture.hpp b/include/resources/texture.hpp index 40d8586..ad4536f 100644 --- a/include/resources/texture.hpp +++ b/include/resources/texture.hpp @@ -20,4 +20,4 @@ private: gfx::Texture* m_gpuTexture; }; -} \ No newline at end of file +} diff --git a/src/gfx_device_vulkan.cpp b/src/gfx_device_vulkan.cpp index 18fd570..a0b519b 100644 --- a/src/gfx_device_vulkan.cpp +++ b/src/gfx_device_vulkan.cpp @@ -154,6 +154,17 @@ namespace engine { throw std::runtime_error("Unknown buffer type"); } + static VkFilter getTextureFilter(gfx::TextureFilter filter) + { + switch(filter) { + case gfx::TextureFilter::LINEAR: + return VK_FILTER_LINEAR; + case gfx::TextureFilter::NEAREST: + return VK_FILTER_NEAREST; + } + throw std::runtime_error("Unknown texture filter"); + } + } // functions @@ -1922,7 +1933,7 @@ namespace engine { delete buffer; } - gfx::Texture* GFXDevice::createTexture(const void* imageData, uint32_t w, uint32_t h) + gfx::Texture* GFXDevice::createTexture(const void* imageData, uint32_t w, uint32_t h, gfx::TextureFilter minFilter, gfx::TextureFilter magFilter) { auto out = new gfx::Texture; @@ -2031,9 +2042,13 @@ namespace engine { // create texture sampler { + + VkFilter minFilterInternal = vkinternal::getTextureFilter(minFilter); + VkFilter magFilterInternal = vkinternal::getTextureFilter(magFilter); + VkSamplerCreateInfo samplerInfo{ VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO }; - samplerInfo.magFilter = VK_FILTER_LINEAR; - samplerInfo.minFilter = VK_FILTER_LINEAR; + samplerInfo.magFilter = VK_FILTER_NEAREST; + samplerInfo.minFilter = VK_FILTER_NEAREST; samplerInfo.addressModeU = VK_SAMPLER_ADDRESS_MODE_REPEAT; samplerInfo.addressModeV = VK_SAMPLER_ADDRESS_MODE_REPEAT; samplerInfo.addressModeW = VK_SAMPLER_ADDRESS_MODE_REPEAT; diff --git a/src/resources/texture.cpp b/src/resources/texture.cpp index ac99a1c..e6060b4 100644 --- a/src/resources/texture.cpp +++ b/src/resources/texture.cpp @@ -85,7 +85,12 @@ Texture::Texture(const std::filesystem::path& resPath) : Resource(resPath, "text throw std::runtime_error("Currently, only RGBA textures are supported. Size: " + std::to_string(texbuf->size())); } - m_gpuTexture = gfxdev->createTexture(texbuf->data(), (uint32_t)width, (uint32_t)height); + gfx::TextureFilter filter = gfx::TextureFilter::LINEAR; + if (width <= 8 || height <= 8) { + filter = gfx::TextureFilter::NEAREST; + } + + m_gpuTexture = gfxdev->createTexture(texbuf->data(), (uint32_t)width, (uint32_t)height, gfx::TextureFilter::LINEAR, filter); DEBUG("loaded texture {} width: {} height: {} size: {}", resPath.filename().string(), width, height, texbuf->size()); @@ -101,4 +106,4 @@ gfx::Texture* Texture::getHandle() return m_gpuTexture; } -} \ No newline at end of file +}