MXVK Vulkan Framework 0.24.0
C++20 Vulkan rendering framework for practical 2D and 3D application development with SDL3.
Loading...
Searching...
No Matches
mxvk_shader_module.cpp
Go to the documentation of this file.
2
4
5#include <fstream>
6#include <iterator>
7
8namespace mxvk {
9 std::vector<char> load_spv(const std::string &path) {
10 if (path.empty()) {
11 throw mxvk::Exception("SPIR-V path is empty");
12 }
13
14 std::ifstream file(path, std::ios::binary);
15 if (!file.is_open()) {
16 throw mxvk::Exception("Failed to open SPIR-V file: " + path);
17 }
18
19 const std::vector<char> bytes((std::istreambuf_iterator<char>(file)), std::istreambuf_iterator<char>());
20 if (bytes.empty()) {
21 throw mxvk::Exception("SPIR-V file is empty: " + path);
22 }
23 if ((bytes.size() % 4U) != 0U) {
24 throw mxvk::Exception("SPIR-V file size is not 4-byte aligned: " + path);
25 }
26
27 return bytes;
28 }
29
30 VkShaderModule create_shader_module(VkDevice device, const std::vector<char> &spv_bytes) {
31 if (device == VK_NULL_HANDLE) {
32 throw mxvk::Exception("Cannot create shader module with a null device");
33 }
34 if (spv_bytes.empty() || (spv_bytes.size() % 4U) != 0U) {
35 throw mxvk::Exception("Invalid SPIR-V shader data");
36 }
37
38 VkShaderModuleCreateInfo create_info{};
39 create_info.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO;
40 create_info.codeSize = spv_bytes.size();
41 create_info.pCode = reinterpret_cast<const uint32_t *>(spv_bytes.data());
42
43 VkShaderModule module = VK_NULL_HANDLE;
44 if (vkCreateShaderModule(device, &create_info, nullptr, &module) != VK_SUCCESS) {
45 throw mxvk::Exception("Failed to create shader module");
46 }
47
48 return module;
49 }
50} // namespace mxvk
Utilities for loading and saving PNG images.
Definition mxvk.hpp:30
VkShaderModule create_shader_module(VkDevice device, const std::vector< char > &spv_bytes)
Create a shader module from SPIR-V bytecode.
std::vector< char > load_spv(const std::string &path)
Load a SPIR-V file from disk.