VisuTwin Canvas
C++ 3D Engine — Metal Backend
Loading...
Searching...
No Matches
vulkanShader.cpp
Go to the documentation of this file.
1// SPDX-License-Identifier: Apache-2.0
2// Copyright 2025-2026 Arnis Lektauers
3//
4
5#ifdef VISUTWIN_HAS_VULKAN
6
7#include "vulkanShader.h"
9
10#include "spdlog/spdlog.h"
11
12namespace visutwin::canvas
13{
14 VulkanShader::VulkanShader(GraphicsDevice* device, const ShaderDefinition& definition,
15 const std::string& sourceCode)
16 : Shader(device, definition)
17 {
18 auto* vkDevice = static_cast<VulkanGraphicsDevice*>(device);
19 _vkDevice = vkDevice->device();
20 (void)sourceCode;
21 // Runtime GLSL compilation not supported — use the SPIR-V constructor.
22 }
23
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)
28 {
29 auto* vkDevice = static_cast<VulkanGraphicsDevice*>(device);
30 _vkDevice = vkDevice->device();
31
32 if (vertSpirv && vertWordCount > 0)
33 _vertexModule = createModule(vertSpirv, vertWordCount);
34 if (fragSpirv && fragWordCount > 0)
35 _fragmentModule = createModule(fragSpirv, fragWordCount);
36
37 spdlog::debug("VulkanShader created: {} (vert={} frag={})",
38 definition.name,
39 _vertexModule != VK_NULL_HANDLE ? "ok" : "none",
40 _fragmentModule != VK_NULL_HANDLE ? "ok" : "none");
41 }
42
43 VulkanShader::~VulkanShader()
44 {
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);
52 }
53 }
54
55 VkShaderModule VulkanShader::createModule(const uint32_t* spirv, size_t wordCount)
56 {
57 VkShaderModuleCreateInfo createInfo{VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO};
58 createInfo.codeSize = wordCount * sizeof(uint32_t);
59 createInfo.pCode = spirv;
60
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;
65 }
66 return module;
67 }
68}
69
70#endif // VISUTWIN_HAS_VULKAN
Abstract GPU interface for resource creation, state management, and draw submission.