Add API usage sample to README.md.

This commit is contained in:
Hans-Kristian Arntzen 2016-04-11 14:22:51 +02:00
parent 5ac882765d
commit a1fd2403e9

View File

@ -32,6 +32,53 @@ MinGW-w64 based compilation works with `make`, and an MSVC 2013 solution is also
## Usage
### Using the C++ API
To perform reflection and convert to other shader languages you can use the SPIRV-Cross API.
For example:
```
#include "spirv_glsl.hpp"
#include <vector>
#include <utility>
extern std::vector<uint32_t> load_spirv_file();
int main()
{
// Read SPIR-V from disk or similar.
std::vector<uint32_t> spirv_binary = load_spirv_file();
spirv_cross::CompilerGLSL glsl(std::move(spirv_binary));
// The SPIR-V is now parsed, and we can perform reflection on it.
spirv_cross::ShaderResources resources = glsl.get_shader_resources();
// Get all sampled images in the shader.
for (auto &resource : resources.sampled_images)
{
unsigned set = glsl.get_decoration(resource.id, spv::DecorationDescriptorSet);
unsigned binding = glsl.get_decoration(resource.id, spv::DecorationBinding);
printf("Image %s at set = %u, binding = %u\n", resource.name.c_str(), set, binding);
// Modify the decoration to prepare it for GLSL.
glsl.unset_decoration(resource.id, spv::DecorationDescriptorSet);
// Some arbitrary remapping if we want.
glsl.set_decoration(resource.id, spv::DecorationBinding, set * 16 + binding);
}
// Set some options.
spirv_cross::CompilerGLSL::Options options;
options.version = 310;
options.es = true;
glsl.set_options(options);
// Compile to GLSL, ready to give to GL driver.
std::string source = glsl.compile();
}
```
### Creating a SPIR-V file from GLSL with glslang
```