LearnWebGPU-Code
LearnWebGPU-Code copied to clipboard
step030 Can not load SPIR-V shader
I have a function that loads spv file:
std::vector<uint32_t> loadSpv(std::string path) {
std::ifstream file(path, std::ios::ate | std::ios::binary);
if (!file.is_open()) {
throw std::runtime_error("Failed to load file");
}
size_t fileSize = (size_t)file.tellg();
std::vector<uint32_t> buffer(fileSize / sizeof(uint32_t));
file.seekg(0);
file.read((char*) buffer.data(), fileSize);
file.close();
return buffer;
}
I am trying to create the shader as follows:
auto vertexCode = loadSpv("Shaders/Triangle/v.spv");
wgpu::ShaderModuleSPIRVDescriptor vert;
vert.chain.next = nullptr;
vert.chain.sType = wgpu::SType::ShaderModuleSPIRVDescriptor;
vert.code = vertexCode.data();
vert.codeSize = vertexCode.size() * sizeof(uint32_t);
wgpu::ShaderModuleDescriptor vertDesc;
vertDesc.nextInChain = &vert.chain;
vertDesc.hintCount = 0;
vertDesc.hints = nullptr;
wgpu::ShaderModule vertex = device.createShaderModule(vertDesc);
But it crashes with error:
thread '<unnamed>' panicked at src/lib.rs:429:5:
wgpu uncaptured error:
Validation Error
Caused by:
In wgpuDeviceCreateShaderModule
invalid word count
I also tried to use this variant of loading:
std::vector<char> loadSpv(std::string path) {
std::ifstream file(path, std::ios::ate | std::ios::binary);
if (!file.is_open()) {
throw std::runtime_error("Failed to open file");
}
size_t fileSize = (size_t) file.tellg();
std::vector<char> buffer(fileSize);
file.seekg(0);
file.read(buffer.data(), fileSize);
file.close();
return buffer;
}
auto vertexCode = loadSpv("Shaders/Triangle/v.spv");
wgpu::ShaderModuleSPIRVDescriptor vert;
vert.chain.next = nullptr;
vert.chain.sType = wgpu::SType::ShaderModuleSPIRVDescriptor;
vert.code = reinterpret_cast<const uint32_t*>(vertexCode.data());
vert.codeSize = vertexCode.size();
wgpu::ShaderModuleDescriptor vertDesc;
vertDesc.nextInChain = &vert.chain;
vertDesc.hintCount = 0;
vertDesc.hints = nullptr;
wgpu::ShaderModule vertex = device.createShaderModule(vertDesc);
But it causes the same error.
Here is my shader code:
#version 460
layout (location = 0) in vec2 inPos;
void main() {
gl_Position = vec4(inPos, 1, 1);
}
Compiled with glslc v.vert -o v.spv
System: MacOS Ventura (Intel)
How can I correctly load SPIR-V shader?