diff --git a/NothinFancy/CMakeLists.txt b/NothinFancy/CMakeLists.txt index 60c1d98..ef6762d 100644 --- a/NothinFancy/CMakeLists.txt +++ b/NothinFancy/CMakeLists.txt @@ -1,5 +1,5 @@ # NF library CMakeLists.txt -add_library(NothinFancy STATIC "src/Engine.cpp" "src/include/nf.h" "src/pch.h" "src/util.h" "src/util/log.h" "src/util/log.cpp" "src/include/nf/config.h" "src/util/util.cpp" "src/util/file.h" "src/util/file.cpp" "src/client/Client.h" "src/client/Client.cpp" "src/client/Window.h" "src/client/Window.cpp" "src/client/render/RenderEngine.h" "src/client/render/RenderEngine.cpp") +add_library(NothinFancy STATIC "src/Engine.cpp" "src/include/nf.h" "src/pch.h" "src/util.h" "src/util/log.h" "src/util/log.cpp" "src/include/nf/config.h" "src/util/util.cpp" "src/util/file.h" "src/util/file.cpp" "src/client/Client.h" "src/client/Client.cpp" "src/client/Window.h" "src/client/Window.cpp" "src/client/render/RenderEngine.h" "src/client/render/RenderEngine.cpp" "src/client/render/ShaderModule.h" "src/client/render/ShaderModule.cpp") # Use C++20 set_property(TARGET NothinFancy PROPERTY CXX_STANDARD 20) diff --git a/NothinFancy/src/client/render/RenderEngine.cpp b/NothinFancy/src/client/render/RenderEngine.cpp index 29f1ce2..5386997 100644 --- a/NothinFancy/src/client/render/RenderEngine.cpp +++ b/NothinFancy/src/client/render/RenderEngine.cpp @@ -3,6 +3,7 @@ #include "RenderEngine.h" #include "util.h" +#include "ShaderModule.h" namespace nf::client::render { RenderEngine::RenderEngine(std::shared_ptr window, DisplayConfig display) @@ -287,20 +288,10 @@ namespace nf::client::render { if (!util::readFile("shaders/output.vert.glsl.spv", outputShaderVertex) || !util::readFile("shaders/output.frag.glsl.spv", outputShaderFragment)) NFError("Could not read output shader binaries."); - VkShaderModule outputShaderVertexModule = VK_NULL_HANDLE, outputShaderFragmentModule = VK_NULL_HANDLE; - VkShaderModuleCreateInfo outputShaderModulesCI = { VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO }; - outputShaderModulesCI.codeSize = outputShaderVertex.size(); - outputShaderModulesCI.pCode = reinterpret_cast(outputShaderVertex.data()); - vkCreateShaderModule(m_device, &outputShaderModulesCI, nullptr, &outputShaderVertexModule); - outputShaderModulesCI.codeSize = outputShaderFragment.size(); - outputShaderModulesCI.pCode = reinterpret_cast(outputShaderFragment.data()); - vkCreateShaderModule(m_device, &outputShaderModulesCI, nullptr, &outputShaderFragmentModule); - if (!outputShaderVertexModule || !outputShaderFragmentModule) - NFError("Could not create output shader modules."); + ShaderModule outputShaderVertexModule(m_device, outputShaderVertex); + ShaderModule outputShaderFragmentModule(m_device, outputShaderFragment); // And cleanup shader modules - vkDestroyShaderModule(m_device, outputShaderVertexModule, nullptr); - vkDestroyShaderModule(m_device, outputShaderFragmentModule, nullptr); } RenderEngine::~RenderEngine() { diff --git a/NothinFancy/src/client/render/ShaderModule.cpp b/NothinFancy/src/client/render/ShaderModule.cpp new file mode 100644 index 0000000..3c5a110 --- /dev/null +++ b/NothinFancy/src/client/render/ShaderModule.cpp @@ -0,0 +1,26 @@ +// ShaderModule class implementation +#include "pch.h" + +#include "ShaderModule.h" +#include "util.h" + +namespace nf::client::render { + ShaderModule::ShaderModule(const VkDevice& device, const std::string& shaderBinary) + : m_device(device) + , m_shaderModule() + { + VkShaderModuleCreateInfo shaderModuleCI = { VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO }; + shaderModuleCI.codeSize = shaderBinary.size(); + shaderModuleCI.pCode = reinterpret_cast(shaderBinary.data()); + if (vkCreateShaderModule(device, &shaderModuleCI, nullptr, &m_shaderModule) != VK_SUCCESS) + NFError("Could not create shader module."); + } + + VkShaderModule& ShaderModule::getHandle() { + return m_shaderModule; + } + + ShaderModule::~ShaderModule() { + vkDestroyShaderModule(m_device, m_shaderModule, nullptr); + } +} diff --git a/NothinFancy/src/client/render/ShaderModule.h b/NothinFancy/src/client/render/ShaderModule.h new file mode 100644 index 0000000..61f4c53 --- /dev/null +++ b/NothinFancy/src/client/render/ShaderModule.h @@ -0,0 +1,17 @@ +// ShaderModule class header +#pragma once + +namespace nf::client::render { + class ShaderModule { + public: + ShaderModule(const VkDevice& device, const std::string& shaderBinary); + + VkShaderModule& getHandle(); + + ~ShaderModule(); + private: + const VkDevice& m_device; + + VkShaderModule m_shaderModule; + }; +}