Fix C example in README
Remove the usage of C++ STL types in the C-only interface example. Fixes #3239.
This commit is contained in:
parent
3ebb72cc74
commit
eaa7057768
34
README.md
34
README.md
@ -425,9 +425,18 @@ warning/error and other options for controlling compilation.
|
|||||||
|
|
||||||
This interface is located `glslang_c_interface.h` and exposes functionality similar to the C++ interface. The following snippet is a complete example showing how to compile GLSL into SPIR-V 1.5 for Vulkan 1.2.
|
This interface is located `glslang_c_interface.h` and exposes functionality similar to the C++ interface. The following snippet is a complete example showing how to compile GLSL into SPIR-V 1.5 for Vulkan 1.2.
|
||||||
|
|
||||||
```cxx
|
```c
|
||||||
std::vector<uint32_t> compileShaderToSPIRV_Vulkan(glslang_stage_t stage, const char* shaderSource, const char* fileName)
|
#include <glslang/Include/glslang_c_interface.h>
|
||||||
{
|
|
||||||
|
// Required for use of glslang_default_resource
|
||||||
|
#include <glslang/Public/resource_limits_c.h>
|
||||||
|
|
||||||
|
typedef struct SpirVBinary {
|
||||||
|
uint32_t *words; // SPIR-V words
|
||||||
|
int size; // number of words in SPIR-V binary
|
||||||
|
} SpirVBinary;
|
||||||
|
|
||||||
|
SpirVBinary compileShaderToSPIRV_Vulkan(glslang_stage_t stage, const char* shaderSource, const char* fileName) {
|
||||||
const glslang_input_t input = {
|
const glslang_input_t input = {
|
||||||
.language = GLSLANG_SOURCE_GLSL,
|
.language = GLSLANG_SOURCE_GLSL,
|
||||||
.stage = stage,
|
.stage = stage,
|
||||||
@ -441,18 +450,22 @@ std::vector<uint32_t> compileShaderToSPIRV_Vulkan(glslang_stage_t stage, const c
|
|||||||
.force_default_version_and_profile = false,
|
.force_default_version_and_profile = false,
|
||||||
.forward_compatible = false,
|
.forward_compatible = false,
|
||||||
.messages = GLSLANG_MSG_DEFAULT_BIT,
|
.messages = GLSLANG_MSG_DEFAULT_BIT,
|
||||||
.resource = reinterpret_cast<const glslang_resource_t*>(&glslang::DefaultTBuiltInResource),
|
.resource = glslang_default_resource(),
|
||||||
};
|
};
|
||||||
|
|
||||||
glslang_shader_t* shader = glslang_shader_create(&input);
|
glslang_shader_t* shader = glslang_shader_create(&input);
|
||||||
|
|
||||||
|
SpirVBinary bin = {
|
||||||
|
.words = NULL,
|
||||||
|
.size = 0,
|
||||||
|
};
|
||||||
if (!glslang_shader_preprocess(shader, &input)) {
|
if (!glslang_shader_preprocess(shader, &input)) {
|
||||||
printf("GLSL preprocessing failed %s\n", fileName);
|
printf("GLSL preprocessing failed %s\n", fileName);
|
||||||
printf("%s\n", glslang_shader_get_info_log(shader));
|
printf("%s\n", glslang_shader_get_info_log(shader));
|
||||||
printf("%s\n", glslang_shader_get_info_debug_log(shader));
|
printf("%s\n", glslang_shader_get_info_debug_log(shader));
|
||||||
printf("%s\n", input.code);
|
printf("%s\n", input.code);
|
||||||
glslang_shader_delete(shader);
|
glslang_shader_delete(shader);
|
||||||
return std::vector<uint32_t>();
|
return bin;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!glslang_shader_parse(shader, &input)) {
|
if (!glslang_shader_parse(shader, &input)) {
|
||||||
@ -461,7 +474,7 @@ std::vector<uint32_t> compileShaderToSPIRV_Vulkan(glslang_stage_t stage, const c
|
|||||||
printf("%s\n", glslang_shader_get_info_debug_log(shader));
|
printf("%s\n", glslang_shader_get_info_debug_log(shader));
|
||||||
printf("%s\n", glslang_shader_get_preprocessed_code(shader));
|
printf("%s\n", glslang_shader_get_preprocessed_code(shader));
|
||||||
glslang_shader_delete(shader);
|
glslang_shader_delete(shader);
|
||||||
return std::vector<uint32_t>();
|
return bin;
|
||||||
}
|
}
|
||||||
|
|
||||||
glslang_program_t* program = glslang_program_create();
|
glslang_program_t* program = glslang_program_create();
|
||||||
@ -473,13 +486,14 @@ std::vector<uint32_t> compileShaderToSPIRV_Vulkan(glslang_stage_t stage, const c
|
|||||||
printf("%s\n", glslang_program_get_info_debug_log(program));
|
printf("%s\n", glslang_program_get_info_debug_log(program));
|
||||||
glslang_program_delete(program);
|
glslang_program_delete(program);
|
||||||
glslang_shader_delete(shader);
|
glslang_shader_delete(shader);
|
||||||
return std::vector<uint32_t>();
|
return bin;
|
||||||
}
|
}
|
||||||
|
|
||||||
glslang_program_SPIRV_generate(program, stage);
|
glslang_program_SPIRV_generate(program, stage);
|
||||||
|
|
||||||
std::vector<uint32_t> outShaderModule(glslang_program_SPIRV_get_size(program));
|
bin.size = glslang_program_SPIRV_get_size(program);
|
||||||
glslang_program_SPIRV_get(program, outShaderModule.data());
|
bin.words = malloc(bin.size * sizeof(uint32_t));
|
||||||
|
glslang_program_SPIRV_get(program, bin.words);
|
||||||
|
|
||||||
const char* spirv_messages = glslang_program_SPIRV_get_messages(program);
|
const char* spirv_messages = glslang_program_SPIRV_get_messages(program);
|
||||||
if (spirv_messages)
|
if (spirv_messages)
|
||||||
@ -488,7 +502,7 @@ std::vector<uint32_t> compileShaderToSPIRV_Vulkan(glslang_stage_t stage, const c
|
|||||||
glslang_program_delete(program);
|
glslang_program_delete(program);
|
||||||
glslang_shader_delete(shader);
|
glslang_shader_delete(shader);
|
||||||
|
|
||||||
return outShaderModule;
|
return bin;
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user