SPIRV-Reflect is a lightweight library that provides a C/C++ reflection API for SPIR-V shader bytecode in Vulkan applications.
Перейти к файлу
Hai Nguyen bd64d605bc
Merge pull request #106 from jeremy-lunarg/hayes-fix-werrors
Fix Werror(s)
2020-12-08 01:24:40 -05:00
common Fix Werror(s) 2020-12-07 18:10:39 -07:00
examples Made C++ noncopyable and upated tests 2020-12-07 11:22:21 -05:00
include/spirv/unified1 Updated spirv.h 2020-12-03 12:58:55 -05:00
tests Made C++ noncopyable and upated tests 2020-12-07 11:22:21 -05:00
third_party Simplified to used unified1/spirv.h out of the box 2018-08-01 22:41:05 -04:00
util/stripper Strip source, line, and tool debug info in stripper 2018-08-31 19:15:34 -04:00
.gitignore Add .gitignore with build output files 2018-02-07 14:56:27 -08:00
.gitmodules Simplified to used unified1/spirv.h out of the box 2018-08-01 22:41:05 -04:00
BUILD.bazel ADDED: drop unused symbols 2019-12-12 09:30:40 -05:00
CMakeLists.txt Made C++ noncopyable and upated tests 2020-12-07 11:22:21 -05:00
CODE_OF_CONDUCT.adoc Create CODE_OF_CONDUCT.adoc 2020-07-13 12:10:14 -04:00
LICENSE Create LICENSE 2020-07-13 12:08:51 -04:00
README.md Merge pull request #82 from jaebaek/bazel 2020-11-13 11:30:13 -05:00
WORKSPACE Add Bazel build script 2019-12-10 14:41:15 -05:00
main.cpp Fix compilation type warnings 2020-09-22 23:00:38 -06:00
spirv_reflect.c Added missing shader stage processing for raytracing 2020-12-06 18:30:24 -05:00
spirv_reflect.h Made C++ noncopyable and upated tests 2020-12-07 11:22:21 -05:00

README.md

SPIRV-Reflect

SPIRV-Reflect is a lightweight library that provides a C/C++ reflection API for SPIR-V shader bytecode in Vulkan applications.

SPIRV-Reflect has been tested on Linux and Windows.

Features

  • Extract descriptor bindings from SPIR-V bytecode, to assist in the generation of Vulkan descriptor set and pipeline layouts.
  • Extract push constant block size from SPIR-V bytecode to assist in the generation of pipeline layouts.
  • Extract full layout data for uniform buffer and push constant blocks from SPIR-V bytecode, to assist in application updates of these structures.
  • Extract input/output variables from SPIR-V bytecode (including semantic decorations for HLSL shaders), to assist in validation of pipeline input/output settings.
  • Remap descriptor bindings at runtime, and update the source SPIR-V bytecode accordingly.
  • Log all reflection data as human-readable text.

Integration

SPIRV-Reflect is designed to make integration as easy as possible. The only external dependency is the Vulkan SDK.

To integrate SPIRV-Reflect into a project, simply add spirv_reflect.h and spirv_reflect.c in the project's build, and include spirv_reflect.h from the necessary source files.

Building Samples

This step is only necessary when building/running SPIRV-Reflect's example applications.

SPIRV-Reflect includes a collection of sample programs in the examples/ directory which demonstrate various use cases:

  • descriptors: This sample demonstrates the retrieval of descriptor bindings, including the population of VkDescriptorSetLayoutCreateInfo structures from reflection data.
  • hlsl_resource_types: This sample shows how various HLSL resource types are represented in SPIR-V.
  • io_variables: This sample demonstrates the retrieval of input/output variables, including the population of VkPipelineVertexInputStateCreateInfo structures from reflection data.

To build the included sample applications, use CMake to generate the appropriate project files for your platform, then build them as usual.

Note that you can set VulkanSDK directory as your preference. For example, on Linux:

VULKAN_SDK=$HOME/VulkanSDK/1.1.70.1/x86_64 cmake -G Ninja  ..

Usage

SPIRV-Reflect's core C API should be familiar to Vulkan developers:

#include "spirv_reflect.h"

int SpirvReflectExample(const void* spirv_code, size_t spirv_nbytes)
{
  // Generate reflection data for a shader
  SpvReflectShaderModule module;
  SpvReflectResult result = spvReflectCreateShaderModule(spirv_nbytes, spirv_code, &module);
  assert(result == SPV_REFLECT_RESULT_SUCCESS);

  // Enumerate and extract shader's input variables
  uint32_t var_count = 0;
  result = spvReflectEnumerateInputVariables(&module, &var_count, NULL);
  assert(result == SPV_REFLECT_RESULT_SUCCESS);
  SpvReflectInterfaceVariable** input_vars =
    (SpvReflectInterfaceVariable**)malloc(var_count * sizeof(SpvReflectInterfaceVariable*));
  result = spvReflectEnumerateInputVariables(&module, &var_count, input_vars);
  assert(result == SPV_REFLECT_RESULT_SUCCESS);

  // Output variables, descriptor bindings, descriptor sets, and push constants
  // can be enumerated and extracted using a similar mechanism.

  // Destroy the reflection data when no longer required.
  spvReflectDestroyShaderModule(&module);
}

A C++ wrapper is also provided.

Building Self-Test Suite

SPIRV-Reflect uses googletest for self-testing. This component is optional, and generally only of interest to developers modifying SPIRV-Reflect. To run the self-tests:

  • git submodule init
  • git submodule update
  • Enable SPIRV_REFLECT_BUILD_TESTS in CMake
  • Build and run the test-spirv-reflect project.

Bazel build

We tested the following bazel build command using Linux Bazel 1.2.1.

bazel build :all

License

Copyright 2017-2018 Google Inc.

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.