Minimizing bugs in SPIR-V

When debugging problems with SPIR-V generated by rust-gpu, you occasionally need to reduce the SPIR-V in order to make it easily shareable with others. We've created a short guide on how to do that.

Prerequisites

In order to build and validate SPIR-V you're going to install SPIR-V tools.

SPIR-V Template

SPIR-V has some amount of required boilerplate in order to be considered valid, we've created a small template to help to get started. This file creates a single empty vertex entry-point with a single floating-point constant.

; bug.spvasm
OpCapability Shader
OpCapability VulkanMemoryModel
OpMemoryModel Logical Vulkan

; == Entry-points ==
OpEntryPoint Vertex %vert_fn "vert"

; == Types ==
%void = OpTypeVoid
%f32 = OpTypeFloat 32

; Function Types
%void_fn = OpTypeFunction %void

; == Constants ==
%f32_1 = OpConstant %f32 1

; == Functions ==
%vert_fn = OpFunction %void None %void_fn
  %block = OpLabel
    OpReturn
OpFunctionEnd

Steps

  1. Assemble your spirv with spirv-as ./bug.spvasm, this will produce a out.spv file containing the assembled code.

  2. The assembled code also needs to be validated with spirv-val out.spv

  3. Once the code has been validated as having no issues, you can use spirv-cross to compile the code to various outputs.

    • GLSL spirv-cross out.spv
    • HLSL spirv-cross --hlsl out.spv
    • MSL spirv-cross --msl out.spv
    • Vulkan GLSL spirv-cross -V out.spv