SPIR-V shader causing validation errors at runtime

Viewed 1759

I have a compute shader:

#version 450
#extension GL_ARB_separate_shader_objects : enable
#extension GL_ARB_gpu_shader_int64 : enable

layout(local_size_x_id = 0) in;

layout(set = 0, binding = 0) buffer Foo {
    u64vec2[256] scratchpad;
} foo;

layout(set = 0, binding = 1) uniform Bar {
    u64vec2 a;
    u64vec2 b;
} bar;

void main() {
    int foobar = 0;
    int baz = 0;
}

I compiled this with glslangValidator from LunarG SDK 1.0.65.0 and checked it using spirv-val, which returned nothing. I enabled shaderInt64when creating the VkDevice. When loading this shader using vkCreateShaderModule I get this validation error:

SPIR-V module not valid: Invalid instruction word count: 0

The validation error goes away when I do any of the following:

  • Remove the 64-bit extension and change all types to int
  • Remove either Foo or Bar buffers
  • Remove either variables in main
  • Remove layout(local_size_x_id = 0) in

My question is, is this a bug in the compiler or validation layers, or am I using one of these features incorrectly?

2 Answers

If you are loading SPIR-V at runtime from file, you have to open the file in a binary mode (e.g. std::ifstream::binary in C++). Otherwise your binary may get changed when loading (i.e. usually the runtime tries to switch newline character(s) based on the platform it runs on).

Alternativelly you can load shaders statically (via #include) as a C++ inline file. You can create such file by glslc -mfmt=c or glslangValidator -V -x --vn variable_name.

Assuming your GLSL->Spir-V compiler threw no errors, make sure this checklist is met:

  1. You are opening your file in binary mode
  2. You are reading the correct number of bytes from the file

SPIR-V is an intermediate language, and incorrect instruction count may be getting thrown because the validation layer is expecting a number of assembly-like instructions specified by the SPIR-V spec (which you have not fully loaded from the file).

Related