简体   繁体   中英

Vulkan tutorial with glsl shaders

I copy pasted the code in this vulkan tutorial to try to take it apart and understand the Vulkan API. However I am having trouble getting it to run without errors.

The code does run as expected if I disable validation. However if validation is enabled I get:

validation layer: SPIR-V module not valid: Codesize must be a multiple of 4 but is 421. The Vulkan spec states: If pCode points to SPIR-V code, codeSize must be a multiple of 4 (https://www.khronos.org/registry/vulkan/specs/1.1-extensions/html/vkspec.html#VUID-VkShaderModuleCreateInfo-pCode-01376

Which if I understrand things correctly is one of the LunarSDK validation checks.

So I think that I must ask for a vulkan extension to handle glsl shaders instead of Spir-v but I am not sure how to do that as this is my first time handling the API and I don't know what code I am looking for. I tried reading the tutorial and the documentation but I cannot find how to fix it.

Vulkan expects SPIR-V shaders (Standard Portable Intermediate Representation) . Only through extensions do vulkan implementations allow other inputs for shaders. Khronos group has provided a GLSL -> SPIR-V compiler which you can either embed into your application and compile at runtime, or precompile on build and ship with your application. This compiler ships with the LunarGSDK. Use of the glslang compiler is explained in the Vulkan Tutorial here . You can compile via the command:

/path/to/glsllanvalidator/glslangValidator.exe -V shader.frag

I believe you must use the proper file extension (.frag, .vert. .comp etc.. which was not used in OpenGL) for the compiler to recognize the shader. More information on the standalone compiler can be found here . Using this method you would run the glslangValidator as part of your build step.

To embed the compiler into your application you can use the resources found in this answer and use helper libraries like shaderc , or you can embed it yourself by using glslang directly .

SPIR-V is a low level assembly like language that is represented by a binary file provided to the vulkan driver. The driver can then JIT compile the shader , allowing you to have the same shader code no matter the vulkan driver. What's different here is that the driver isn't forced to carry out all the compilation steps required if they were given a human readable text representation of the code The driver only needs to understand this low level assembly representation. This allows the developer to use what ever language front end that compiles to SPIR-V (which now includes HLSL ). As an added benefit, this removes a huge overhead from the driver that reduces a major source of bugs for the maintainer of the driver, and means that you don't need to store programs as plain text that can easily extracted when you publish your application.

To use GLSL shaders in Vulkan, you need to enable and use a specific extension (I don't remember its name, will check that later). But as far as I know, not too many vendors support it. It was originally created by Nvidia, but I think even they stopped supporting it. The core Vulkan API only supports SPIR-V shaders. But it's not hard to convert GLSL to SPIR-V.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM