Add create/destroy_shader functions to RenderDriver

This commit is contained in:
2025-04-18 22:27:15 +10:00
parent 47a07ea6a8
commit c7249eeb62
4 changed files with 42 additions and 0 deletions

View File

@@ -11,6 +11,7 @@
#include <nova/render/render_fwd.h> #include <nova/render/render_fwd.h>
#include <nova/types.h> #include <nova/types.h>
#include <span>
#include <string> #include <string>
namespace Nova { namespace Nova {
@@ -41,5 +42,8 @@ namespace Nova {
[[nodiscard]] virtual SwapchainID create_swapchain(SurfaceID surface) = 0; [[nodiscard]] virtual SwapchainID create_swapchain(SurfaceID surface) = 0;
virtual void resize_swapchain(SwapchainID swapchain) = 0; virtual void resize_swapchain(SwapchainID swapchain) = 0;
virtual void destroy_swapchain(SwapchainID swapchain) = 0; virtual void destroy_swapchain(SwapchainID swapchain) = 0;
[[nodiscard]] virtual ShaderID create_shader(const std::span<u8> bytes) = 0;
virtual void destroy_shader(ShaderID shader) = 0;
}; };
} // namespace Nova } // namespace Nova

View File

@@ -12,9 +12,11 @@ namespace Nova {
class RenderDriver; class RenderDriver;
struct RenderDevice; struct RenderDevice;
struct Shader;
struct Surface; struct Surface;
struct Swapchain; struct Swapchain;
using ShaderID = Shader*;
using SurfaceID = Surface*; using SurfaceID = Surface*;
using SwapchainID = Swapchain*; using SwapchainID = Swapchain*;
} // namespace Nova } // namespace Nova

View File

@@ -291,6 +291,35 @@ void VulkanRenderDriver::destroy_swapchain(SwapchainID p_swapchain) {
delete p_swapchain; delete p_swapchain;
} }
ShaderID VulkanRenderDriver::create_shader(const std::span<u8> p_bytes) {
NOVA_AUTO_TRACE();
NOVA_ASSERT(!p_bytes.empty());
Shader* shader = new Shader();
VkShaderModuleCreateInfo create {};
create.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO;
create.codeSize = p_bytes.size();
create.pCode = reinterpret_cast<const u32*>(p_bytes.data());
if (vkCreateShaderModule(m_device, &create, get_allocator(VK_OBJECT_TYPE_SHADER_MODULE), &shader->handle)
!= VK_SUCCESS) {
throw std::runtime_error("Failed to create shader module");
}
NOVA_LOG("VkShaderModule created");
return shader;
}
void VulkanRenderDriver::destroy_shader(ShaderID p_shader) {
NOVA_AUTO_TRACE();
NOVA_ASSERT(p_shader);
if (p_shader->handle) {
vkDestroyShaderModule(m_device, p_shader->handle, get_allocator(VK_OBJECT_TYPE_SHADER_MODULE));
}
delete p_shader;
}
VkInstance VulkanRenderDriver::get_instance() const { VkInstance VulkanRenderDriver::get_instance() const {
return m_instance; return m_instance;
} }

View File

@@ -14,6 +14,10 @@
#include <vector> #include <vector>
namespace Nova { namespace Nova {
struct Shader {
VkShaderModule handle = VK_NULL_HANDLE;
};
struct Surface { struct Surface {
VkSurfaceKHR handle = VK_NULL_HANDLE; VkSurfaceKHR handle = VK_NULL_HANDLE;
u32 width = 0; u32 width = 0;
@@ -53,6 +57,9 @@ namespace Nova {
void resize_swapchain(SwapchainID swapchain) override; void resize_swapchain(SwapchainID swapchain) override;
void destroy_swapchain(SwapchainID swapchain) override; void destroy_swapchain(SwapchainID swapchain) override;
[[nodiscard]] ShaderID create_shader(const std::span<u8> bytes) override;
void destroy_shader(ShaderID shader) override;
[[nodiscard]] VkInstance get_instance() const; [[nodiscard]] VkInstance get_instance() const;
[[nodiscard]] VkAllocationCallbacks* get_allocator(VkObjectType type) const; [[nodiscard]] VkAllocationCallbacks* get_allocator(VkObjectType type) const;