// Copyright 2019-2022 The Khronos Group, Inc. // SPDX-License-Identifier: CC-BY-4.0 ifndef::chapters[:chapters:] [[what-is-spirv]] = What is SPIR-V [NOTE] ==== Please read the link:https://github.com/KhronosGroup/SPIRV-Guide[SPIRV-Guide] for more in detail information about SPIR-V ==== link:https://www.khronos.org/registry/SPIR-V/[SPIR-V] is a binary intermediate representation for graphical-shader stages and compute kernels. With Vulkan, an application can still write their shaders in a high-level shading language such as GLSL or xref:{chapters}hlsl.adoc[HLSL], but a SPIR-V binary is needed when using link:https://www.khronos.org/registry/vulkan/specs/1.3/html/vkspec.html#vkCreateShaderModule[vkCreateShaderModule]. Khronos has a very nice link:https://www.khronos.org/registry/SPIR-V/papers/WhitePaper.pdf[white paper] about SPIR-V and its advantages, and a high-level description of the representation. There are also two great Khronos presentations from Vulkan DevDay 2016 link:https://www.khronos.org/assets/uploads/developers/library/2016-vulkan-devday-uk/3-Intro-to-spir-v-shaders.pdf[here] and link:https://www.khronos.org/assets/uploads/developers/library/2016-vulkan-devday-uk/4-Using-spir-v-with-spirv-cross.pdf[here] (link:https://www.youtube.com/watch?v=XRpVwdduzgU[video of both]). == SPIR-V Interface and Capabilities Vulkan has an entire section that defines how link:https://www.khronos.org/registry/vulkan/specs/1.3/html/vkspec.html#interfaces[Vulkan interfaces with SPIR-V shaders]. Most valid usages of interfacing with SPIR-V occur during pipeline creation when shaders are compiled together. SPIR-V has many capabilities as it has other targets than just Vulkan. To see the supported capabilities Vulkan requires, one can reference the link:https://www.khronos.org/registry/vulkan/specs/1.3/html/vkspec.html#spirvenv-capabilities[Appendix]. Some extensions and features in Vulkan are just designed to check if some SPIR-V capabilities are supported or not. == Compilers === glslang link:https://github.com/KhronosGroup/glslang[glslang] is the Khronos reference front-end for GLSL, HLSL and ESSL, and sample SPIR-V generator. There is a standalone `glslangValidator` tool that is included that can be used to create SPIR-V from GLSL, HLSL and ESSL. === Shaderc A collection of tools, libraries, and tests for Vulkan shader compilation hosted by Google. It contains `glslc` which wraps around core functionality in link:https://github.com/KhronosGroup/glslang[glslang] and link:https://github.com/KhronosGroup/SPIRV-Tools[SPIRV-Tools]. Shaderc also contains `spvc` which wraps around core functionality in link:https://github.com/KhronosGroup/SPIRV-Cross[SPIRV-Cross] and link:https://github.com/KhronosGroup/SPIRV-Tools[SPIRV-Tools]. Shaderc builds both tools as a standalone command line tool (link:https://github.com/google/shaderc/tree/main/glslc[glslc]) as well as a library to link to (link:https://github.com/google/shaderc/tree/main/libshaderc[libshaderc]). === DXC link:https://github.com/microsoft/DirectXShaderCompiler[DirectXShaderCompiler] also supports link:https://github.com/Microsoft/DirectXShaderCompiler/wiki/SPIR%E2%80%90V-CodeGen[translating HLSL into the SPIR-V]. image::images/what_is_spirv_dxc.png[what_is_spirv_dxc.png] === Clspv link:https://github.com/google/clspv[Clspv] is a prototype compiler for a subset of OpenCL C to SPIR-V to be used as Vulkan compute shaders. == Tools and Ecosystem There is a rich ecosystem of tools to take advantage of SPIR-V. The link:https://vulkan.lunarg.com/doc/sdk/latest/windows/spirv_toolchain.html[Vulkan SDK gives an overview] of all the SPIR-V tools that are built and packaged for developers. === SPIRV-Tools The Khronos link:https://github.com/KhronosGroup/SPIRV-Tools[SPIRV-Tools] project provides C and C++ APIs and a command line interface to work with SPIR-V modules. More information in the link:https://github.com/KhronosGroup/SPIRV-Guide/blob/master/chapters/khronos_tooling.md#spir-v-tools[SPIRV-Guide]. === SPIRV-Cross The Khronos link:https://github.com/KhronosGroup/SPIRV-Cross[SPIRV-Cross] project is a practical tool and library for performing reflection on SPIR-V and disassembling SPIR-V back to a desired high-level shading language. For more details, link:https://github.com/Themaister[Hans Kristian], the main developer of SPIR-V Cross, has given two great presentations about what it takes to create a tool such as SPIR-V Cross from link:https://www.khronos.org/assets/uploads/developers/library/2018-vulkanised/04-SPIRVCross_Vulkanised2018.pdf[2018 Vulkanised] (link:https://www.youtube.com/watch?v=T5Va6hSGx44[video]) and link:https://www.khronos.org/assets/uploads/developers/library/2019-vulkanised/04-SPIRV-Cross-May19.pdf[2019 Vulkanised] (link:https://www.youtube.com/watch?v=lv-fh_oFJUc[video]) image::images/what_is_spirv_spriv_cross.png[what_is_spirv_spriv_cross.png] === SPIRV-LLVM The Khronos link:https://github.com/KhronosGroup/SPIRV-LLVM[SPIRV-LLVM] project is a LLVM framework with SPIR-V support. It's intended to contain a bi-directional converter between LLVM and SPIR-V. It also serves as a foundation for LLVM-based front-end compilers targeting SPIR-V.