CreateShaderModule Fail

hi guys, i am new in vulkan.

I have written a vertex shader, and compile it to SPIR-V code. Then I load it and create shader module, but it failed. I don’t know why. Can you help me?

This is my shader code:

#version 460

#extension GL_ARB_separate_shader_objects : enable

layout(location = 0) in vec2 inPosition;
layout(location = 1) in vec3 inColor;

layout(location = 0) out vec3 fragColor;

void main() {
    gl_Position = vec4(inPosition, 0.0, 1.0);
    fragColor = inColor;
}

Compile cmd:

glslangValidator.exe -V .\triangle.vert.glsl

Create Shader Modul code:

VkShaderModuleCreateInfo info;
info.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO;
info.codeSize = code.size();
info.pCode = (const uint32_t*)code.data();
info.flags = 0;
info.pNext = nullptr;

vkCreateShaderModule(mpDevice->GetVkDevice(), &info, nullptr, &mShaderModule);

It will be hang in vkCreateShaderModule function.

The validation layer output is :

Validation Error: [ UNASSIGNED-CoreValidation-Shader-InconsistentSpirv ] Object 0: handle = 0x26b1f2df968, type = VK_OBJECT_TYPE_DEVICE; | MessageID = 0x6bbb14 | SPIR-V module not valid: End of input reached while decoding OpAccessChain starting at word 244: missing result ID operand at word offset 2.

How is you code data look like?
info.codeSize must take size in bytes, maybe here is a problem.

code is a std::string object. I have two shaders, one is frag, one is vert, It is no problem when i load frag shader.

my frag shader code:

#version 460
#extension GL_ARB_separate_shader_objects: enable

layout(location = 0) out vec4 outColor;

void main() {
    outColor = vec4(1.0, 0.0, 0.0, 1.0);
}

Then you already failed.

SPIR-V code is not a string. It is a sequence of uint32_t objects. That’s what you should load from the file.

If you have to do explicit casts to make the code work, there’s a very good chance that your code isn’t working right.

std::string sounds weird) Do you load text of shader instead of bytecode from glslc?

I compile the shader text to SPIR-V bytecode, then read it to a string buffer which name is code.

Compile command is:

glslangValidator.exe -V .\triangle.vert.glsl

It seems that the size I read from file is less than the total size.

Please do not post pictures of code. Also, this is not Twitter; you don’t have character limits. It’s OK to take your time to post a single, complete post. You don’t need to post repeatedly to get all of the information out there.

Lastly, you read the file as text. But it’s a binary file; you should open and read it as binary. And it should be read into a uint32_t array.

1 Like