5#ifdef VISUTWIN_HAS_VULKAN
10#include "spdlog/spdlog.h"
15 const std::string& sourceCode)
16 :
Shader(device, definition)
18 auto* vkDevice =
static_cast<VulkanGraphicsDevice*
>(device);
19 _vkDevice = vkDevice->device();
24 VulkanShader::VulkanShader(GraphicsDevice* device,
const ShaderDefinition& definition,
25 const uint32_t* vertSpirv,
size_t vertWordCount,
26 const uint32_t* fragSpirv,
size_t fragWordCount)
27 : Shader(device, definition)
29 auto* vkDevice =
static_cast<VulkanGraphicsDevice*
>(device);
30 _vkDevice = vkDevice->device();
32 if (vertSpirv && vertWordCount > 0)
33 _vertexModule = createModule(vertSpirv, vertWordCount);
34 if (fragSpirv && fragWordCount > 0)
35 _fragmentModule = createModule(fragSpirv, fragWordCount);
37 spdlog::debug(
"VulkanShader created: {} (vert={} frag={})",
39 _vertexModule != VK_NULL_HANDLE ?
"ok" :
"none",
40 _fragmentModule != VK_NULL_HANDLE ?
"ok" :
"none");
43 VulkanShader::~VulkanShader()
45 if (_vkDevice != VK_NULL_HANDLE) {
46 if (_vertexModule != VK_NULL_HANDLE)
47 vkDestroyShaderModule(_vkDevice, _vertexModule,
nullptr);
48 if (_fragmentModule != VK_NULL_HANDLE)
49 vkDestroyShaderModule(_vkDevice, _fragmentModule,
nullptr);
50 if (_computeModule != VK_NULL_HANDLE)
51 vkDestroyShaderModule(_vkDevice, _computeModule,
nullptr);
55 VkShaderModule VulkanShader::createModule(
const uint32_t* spirv,
size_t wordCount)
57 VkShaderModuleCreateInfo createInfo{VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO};
58 createInfo.codeSize = wordCount *
sizeof(uint32_t);
59 createInfo.pCode = spirv;
61 VkShaderModule module = VK_NULL_HANDLE;
62 if (vkCreateShaderModule(_vkDevice, &createInfo,
nullptr, &module) != VK_SUCCESS) {
63 spdlog::error(
"Failed to create Vulkan shader module");
64 return VK_NULL_HANDLE;
Abstract GPU interface for resource creation, state management, and draw submission.