SpiecsEngine
 
Loading...
Searching...
No Matches
VulkanShaderModule.cpp
Go to the documentation of this file.
1/**
2* @file VulkanShaderModule.cpp.
3* @brief The VulkanShaderModule Class Implementation.
4* @author Spices.
5*/
6
7#include "Pchheader.h"
9
10#include "Core/Library/FileLibrary.h"
11
12namespace Spices {
13
15 VulkanState& vulkanState ,
16 const std::string& shaderName ,
17 const std::string& shaderStage
18 )
19 : VulkanObject(vulkanState)
21 {
23
24 /**
25 * @brief Get Shader File Path.
26 */
27 const std::string filePath = GetShaderPath(shaderName, shaderStage);
28
29 /**
30 * @brief Confirm file existence.
31 */
32 FileHandle fileHandle = {};
33 fileHandle.is_valid = FileLibrary::FileLibrary_Exists(filePath.c_str());
34
35 if (!fileHandle.is_valid)
36 {
37 SPICES_CORE_ERROR("File is not exist.");
38 }
39
40 /**
41 * @brief Open Shader File.
42 */
43 FileLibrary::FileLibrary_Open(filePath.c_str(), FILE_MODE_READ, 1, &fileHandle);
44
45 uint64_t fileSize = 0;
46 FileLibrary::FileLibrary_Size(&fileHandle, &fileSize);
47
48 /**
49 * @brief Read Shader File.
50 */
51 std::vector<char> shaderChar;
52 shaderChar.resize(fileSize);
53 FileLibrary::FileLibrary_Read_all_bytes(&fileHandle, shaderChar.data(), &fileSize);
54
55 /**
56 * @brief Instance a VkShaderModuleCreateInfo.
57 */
58 VkShaderModuleCreateInfo createInfo{};
59 createInfo.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO;
60 createInfo.codeSize = fileSize;
61 createInfo.pCode = reinterpret_cast<const uint32_t*>(shaderChar.data());
62
63 /**
64 * @brief Create Shader Module.
65 */
66 VK_CHECK(vkCreateShaderModule(vulkanState.m_Device, &createInfo, nullptr, &m_ShaderModule))
67 DEBUGUTILS_SETOBJECTNAME(VK_OBJECT_TYPE_SHADER_MODULE, reinterpret_cast<uint64_t>(m_ShaderModule), m_VulkanState.m_Device, shaderName)
68
69 FileLibrary::FileLibrary_Close(&fileHandle);
70
71 /**
72 * @brief Add to Aftermath.
73 */
74 NSIGHTAFTERMATH_GPUCRASHTRACKER_ADDSHADERBINARY(filePath.c_str())
75 NSIGHTAFTERMATH_GPUCRASHTRACKER_ADDSHADERBINARY_WITHDEBUGINFO(filePath.c_str(), filePath.c_str())
76 }
77
79 VulkanState& vulkanState ,
80 const std::string& shaderName ,
81 const ShaderStage& shaderStage ,
82 const std::vector<uint8_t>& spirv ,
83 const std::string& fullPath
84 )
85 : VulkanObject(vulkanState)
86 , m_ShaderStage(shaderStage)
87 {
89
90 /**
91 * @brief Instance a VkShaderModuleCreateInfo.
92 */
93 VkShaderModuleCreateInfo createInfo{};
94 createInfo.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO;
95 createInfo.codeSize = spirv.size();
96 createInfo.pCode = reinterpret_cast<const uint32_t*>(spirv.data());
97
98 /**
99 * @brief Create Shader Module.
100 */
101 VK_CHECK(vkCreateShaderModule(vulkanState.m_Device, &createInfo, nullptr, &m_ShaderModule))
102 DEBUGUTILS_SETOBJECTNAME(VK_OBJECT_TYPE_SHADER_MODULE, reinterpret_cast<uint64_t>(m_ShaderModule), m_VulkanState.m_Device, shaderName)
103
104 /**
105 * @brief Add to Aftermath.
106 */
107 NSIGHTAFTERMATH_GPUCRASHTRACKER_ADDSHADERSOURCE(spirv)
108 NSIGHTAFTERMATH_GPUCRASHTRACKER_ADDSHADERSOURCE_WITHDEBUGINFO(spirv, spirv)
109 }
110
112 {
114
115 /**
116 * @brief Destroy Shader Module.
117 */
118 vkDestroyShaderModule(m_VulkanState.m_Device, m_ShaderModule, nullptr);
119 }
120
122 {
123 VkPipelineShaderStageCreateInfo shaderStages{};
124
125 shaderStages.sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
126 shaderStages.module = m_ShaderModule;
127 shaderStages.pName = "main";
128 shaderStages.flags = 0;
129 shaderStages.pNext = nullptr;
130 shaderStages.pSpecializationInfo = nullptr;
131 shaderStages.stage = ShaderHelper::ToFlagBits(m_ShaderStage);
132
133 return shaderStages;
134 }
135
136 std::string VulkanShaderModule::GetShaderPath(const std::string& name, const std::string& shaderType)
137 {
139
140 /**
141 * @brief Get full path of shader file.
142 */
143 std::stringstream ss;
144 ss << SPICES_ENGINE_ASSETS_PATH << "Shaders/spv/Shader." << name << "." << shaderType << ".spv";
145
146 return ss.str();
147 }
148}
#define SPICES_PROFILE_ZONE
#define VK_CHECK(expr)
Vulkan Check macro. Verify Vulkan API Effectiveness.
Definition VulkanUtils.h:68
static bool FileLibrary_Size(const FileHandle *handle, uint64_t *out_size)
Calculate The file size.
static bool FileLibrary_Open(const char *path, FileModes mode, bool binary, FileHandle *out_handle)
Open the file using given string.
static bool FileLibrary_Exists(const char *path)
Determine whether the given string is existing a file.
File Static Function Library.
Definition FileLibrary.h:49
static ShaderStage ToStage(std::string stage)
Convert String to ShaderStage.
ShaderHelper Class.
VulkanObject(VulkanState &vulkanState)
Constructor Function. Init member variables.
VulkanObject Class. This class defines the basic behaves of VulkanObject. When we create an new Vulka...
VkPipelineShaderStageCreateInfo GetShaderStageCreateInfo() const
virtual ~VulkanShaderModule() override
Destructor Function.
std::string GetShaderPath(const std::string &name, const std::string &shaderType)
Get shader path string.
VulkanShaderModule(VulkanState &vulkanState, const std::string &shaderName, const std::string &shaderStage)
Constructor Function. Create VkShaderModule.
VulkanShaderModule(VulkanState &vulkanState, const std::string &shaderName, const ShaderStage &shaderStage, const std::vector< uint8_t > &spirv, const std::string &fullPath)
Constructor Function. Create VkShaderModule.
This Class is a Wrapper of VkShaderModule.
ShaderStage
enum of shader stage.
@ FILE_MODE_READ
model : read
Definition FileLibrary.h:37
bool is_valid
Is this handle Valid.
Definition FileLibrary.h:26
This Struct is FILE* handle pointer Wrapper.
Definition FileLibrary.h:15
This struct contains all Vulkan object in used global.
Definition VulkanUtils.h:74