2#include "vulkan/vulkan_core.h"
12 if(device && vertShader)
13 vkDestroyShaderModule(device, vertShader,
nullptr);
14 if(device && fragShader)
15 vkDestroyShaderModule(device, fragShader,
nullptr);
20 std::string basePath = path;
21 auto hlslPos = basePath.rfind(
".hlsl");
22 if (hlslPos != std::string::npos) {
23 basePath = basePath.substr(0, hlslPos);
25 return compile(basePath +
".vert.spv", basePath +
".frag.spv");
33 auto vertCode = ReadFile(vert);
34 auto fragCode = ReadFile(frag);
36 if(vertCode.empty() || fragCode.empty())
39 vertShader = createShaderModule(vertCode);
43 fragShader = createShaderModule(fragCode);
47 vertexInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
48 vertexInfo.stage = VK_SHADER_STAGE_VERTEX_BIT;
49 vertexInfo.module = vertShader;
50 vertexInfo.pSpecializationInfo =
nullptr;
51 vertexInfo.pName =
"main";
53 fragmentInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
54 fragmentInfo.stage = VK_SHADER_STAGE_FRAGMENT_BIT;
55 fragmentInfo.module = fragShader;
56 fragmentInfo.pSpecializationInfo =
nullptr;
57 fragmentInfo.pName =
"main";
66 auto vertCode = ReadFile(vertPath);
70 vertShader = createShaderModule(vertCode);
74 vertexInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
75 vertexInfo.stage = VK_SHADER_STAGE_VERTEX_BIT;
76 vertexInfo.module = vertShader;
77 vertexInfo.pSpecializationInfo =
nullptr;
78 vertexInfo.pName =
"main";
84 auto code = ReadFile(path);
85 if (code.empty())
return VK_NULL_HANDLE;
86 return createShaderModule(code);
93VkShaderModule VulkanShader::createShaderModule(
const std::vector<char>& code) {
94 VkShaderModule module;
96 VkShaderModuleCreateInfo ShaderInfo{};
97 ShaderInfo.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO;
98 ShaderInfo.codeSize = code.size();
99 ShaderInfo.pCode =
reinterpret_cast<const uint32_t*
>(code.data());
101 VkResult result = vkCreateShaderModule(device, &ShaderInfo,
nullptr, &module);
103 if(result != VK_SUCCESS)
109std::vector<char> VulkanShader::ReadFile(
const std::string& path) {
110 std::ifstream file(path, std::ios::ate | std::ios::binary);
115 return std::vector<char>(0);
118 size_t size = (size_t) file.tellg();
119 std::vector<char> buffer(size);
122 file.read(buffer.data(), size);
#define SLEAK_RETURN_ERR(...)
virtual void bind() override
No-op: Vulkan binds shader stages via pipeline creation, not a runtime bind call.
VkShaderModule LoadSPIRV(const char *path)
Reads a .spv file and creates a VkShaderModule from it.
bool compileVertexOnly(const std::string &vertPath)
Loads only the vertex stage (used for depth-only/shadow pipelines with no fragment shader).
virtual bool compile(const std::string &shaderPath) override
Loads the combined-path convention's .vert.spv/.frag.spv pair.
Backend-facing rendering layer shared by the four graphics backends.
Root namespace for everything the engine exposes.