convert_glsl_to_spirv: fail early, reduce copies, remove input buffer allocation
This commit is contained in:
parent
b16a4bc45e
commit
a761284f70
@ -155,115 +155,95 @@ const TBuiltInResource DefaultTBuiltInResource = {
|
|||||||
/* .generalConstantMatrixVectorIndexing = */ 1,
|
/* .generalConstantMatrixVectorIndexing = */ 1,
|
||||||
}};
|
}};
|
||||||
|
|
||||||
|
static bool initialized = false;
|
||||||
|
|
||||||
extern "C" {
|
extern "C" {
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Takes in a GLSL shader as a string and converts it to SPIR-V in binary form.
|
* Takes in a GLSL shader as a string and converts it to SPIR-V in binary form.
|
||||||
*
|
*
|
||||||
* |glsl| Char array created using create_input_buffer and populated
|
* |glsl| Null-terminated string containing the shader to be converted.
|
||||||
* with the shader to be converted.
|
* |stage_int| Magic number indicating the type of shader being processed.
|
||||||
* This buffer must be destroyed using destroy_input_buffer.
|
* Legal values are as follows:
|
||||||
* |shader_type| Magic number indicating the type of shader being processed.
|
|
||||||
* Legal values are as follows:
|
|
||||||
* Vertex = 0
|
* Vertex = 0
|
||||||
* Fragment = 4
|
* Fragment = 4
|
||||||
* Compute = 5
|
* Compute = 5
|
||||||
* |spirv| Pointer to an output buffer that will be updated with the
|
|
||||||
* resulting SPIR-V shader.
|
|
||||||
* This buffer must be destroyed using destroy_output_buffer.
|
|
||||||
*
|
|
||||||
* |spirv_len| Length of the output binary buffer.
|
|
||||||
* |gen_debug| Flag to indicate if debug information should be generated.
|
* |gen_debug| Flag to indicate if debug information should be generated.
|
||||||
|
* |spirv| Output parameter for a pointer to the resulting SPIR-V data.
|
||||||
|
* |spirv_len| Output parameter for the length of the output binary buffer.
|
||||||
*
|
*
|
||||||
* Return 0 on success, non-0 on failure.
|
* Returns a void* pointer which, if not null, must be destroyed by
|
||||||
|
* destroy_output_buffer.o. (This is not the same pointer returned in |spirv|.)
|
||||||
|
* If null, the compilation failed.
|
||||||
*/
|
*/
|
||||||
EMSCRIPTEN_KEEPALIVE
|
EMSCRIPTEN_KEEPALIVE
|
||||||
int convert_glsl_to_spirv(const char* glsl, int shader_type, uint32_t** spirv, size_t* spirv_len, bool gen_debug)
|
void* convert_glsl_to_spirv(const char* glsl, int stage_int, bool gen_debug, uint32_t** spirv, size_t* spirv_len)
|
||||||
{
|
{
|
||||||
int ret_val = 0;
|
if (glsl == nullptr) {
|
||||||
if (glsl == nullptr || spirv == nullptr) {
|
fprintf(stderr, "Input pointer null\n");
|
||||||
return 1;
|
return nullptr;
|
||||||
|
}
|
||||||
|
if (spirv == nullptr || spirv_len == nullptr) {
|
||||||
|
fprintf(stderr, "Output pointer null\n");
|
||||||
|
return nullptr;
|
||||||
}
|
}
|
||||||
*spirv = nullptr;
|
*spirv = nullptr;
|
||||||
|
*spirv_len = 0;
|
||||||
|
|
||||||
if (shader_type != 0 && shader_type != 4 && shader_type != 5) {
|
if (stage_int != 0 && stage_int != 4 && stage_int != 5) {
|
||||||
return 2;
|
fprintf(stderr, "Invalid shader stage\n");
|
||||||
|
return nullptr;
|
||||||
|
}
|
||||||
|
EShLanguage stage = static_cast<EShLanguage>(stage_int);
|
||||||
|
|
||||||
|
if (!initialized) {
|
||||||
|
glslang::InitializeProcess();
|
||||||
|
initialized = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
EShLanguage shader_stage = static_cast<EShLanguage>(shader_type);
|
glslang::TShader shader(stage);
|
||||||
|
shader.setStrings(&glsl, 1);
|
||||||
glslang::InitializeProcess();
|
shader.setEnvInput(glslang::EShSourceGlsl, stage, glslang::EShClientVulkan, 100);
|
||||||
{
|
shader.setEnvClient(glslang::EShClientVulkan, glslang::EShTargetVulkan_1_1);
|
||||||
glslang::TShader shader(shader_stage);
|
shader.setEnvTarget(glslang::EShTargetSpv, glslang::EShTargetSpv_1_3);
|
||||||
shader.setStrings(&glsl, 1);
|
if (!shader.parse(&DefaultTBuiltInResource, 100, true, EShMsgDefault)) {
|
||||||
shader.setEnvInput(glslang::EShSourceGlsl, shader_stage, glslang::EShClientVulkan, 100);
|
fprintf(stderr, "Parse failed\n");
|
||||||
shader.setEnvClient(glslang::EShClientVulkan, glslang::EShTargetVulkan_1_1);
|
fprintf(stderr, "%s\n", shader.getInfoLog());
|
||||||
shader.setEnvTarget(glslang::EShTargetSpv, glslang::EShTargetSpv_1_3);
|
return nullptr;
|
||||||
if (!shader.parse(&DefaultTBuiltInResource, 100, true, EShMsgDefault)) {
|
|
||||||
fprintf(stderr, "Parse failed\n");
|
|
||||||
fprintf(stderr, "%s\n", shader.getInfoLog());
|
|
||||||
fprintf(stderr, "%s\n", shader.getInfoDebugLog());
|
|
||||||
}
|
|
||||||
|
|
||||||
glslang::TProgram program;
|
|
||||||
program.addShader(&shader);
|
|
||||||
if (!program.link(EShMsgDefault)) {
|
|
||||||
fprintf(stderr, "Link failed\n");
|
|
||||||
fprintf(stderr, "%s\n", program.getInfoLog());
|
|
||||||
fprintf(stderr, "%s\n", program.getInfoDebugLog());
|
|
||||||
}
|
|
||||||
|
|
||||||
std::vector<uint32_t> output;
|
|
||||||
glslang::SpvOptions spvOptions;
|
|
||||||
spvOptions.generateDebugInfo = gen_debug;
|
|
||||||
spvOptions.disableOptimizer = false;
|
|
||||||
spvOptions.optimizeSize = false;
|
|
||||||
spvOptions.disassemble = false;
|
|
||||||
spvOptions.validate = false;
|
|
||||||
|
|
||||||
glslang::GlslangToSpv(*program.getIntermediate(shader_stage), output, nullptr, &spvOptions);
|
|
||||||
|
|
||||||
*spirv_len = output.size();
|
|
||||||
*spirv = static_cast<uint32_t*>(malloc(*spirv_len * sizeof(uint32_t)));
|
|
||||||
if (*spirv != nullptr) {
|
|
||||||
memcpy(*spirv, output.data(), *spirv_len * sizeof(uint32_t));
|
|
||||||
} else {
|
|
||||||
ret_val = 3;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
glslang::FinalizeProcess();
|
|
||||||
return ret_val;
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
glslang::TProgram program;
|
||||||
* Created a valid input buffer.
|
program.addShader(&shader);
|
||||||
*
|
if (!program.link(EShMsgDefault)) {
|
||||||
* Must be destroyed later using destroy_input_buffer.
|
fprintf(stderr, "Link failed\n");
|
||||||
*/
|
fprintf(stderr, "%s\n", program.getInfoLog());
|
||||||
EMSCRIPTEN_KEEPALIVE
|
return nullptr;
|
||||||
char* create_input_buffer(int count) { return static_cast<char*>(malloc(count * sizeof(char))); }
|
}
|
||||||
|
|
||||||
/*
|
glslang::SpvOptions spvOptions;
|
||||||
* Destroys a buffer created by create_input_buffer
|
spvOptions.generateDebugInfo = gen_debug;
|
||||||
*/
|
spvOptions.optimizeSize = false;
|
||||||
EMSCRIPTEN_KEEPALIVE
|
spvOptions.disassemble = false;
|
||||||
void destroy_input_buffer(char* p)
|
spvOptions.validate = false;
|
||||||
{
|
|
||||||
if (p != nullptr)
|
std::vector<uint32_t>* output = new std::vector<uint32_t>;
|
||||||
free(p);
|
glslang::GlslangToSpv(*program.getIntermediate(stage), *output, nullptr, &spvOptions);
|
||||||
|
|
||||||
|
*spirv_len = output->size();
|
||||||
|
*spirv = output->data();
|
||||||
|
return output;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Destroys a buffer created by convert_glsl_to_spirv
|
* Destroys a buffer created by convert_glsl_to_spirv
|
||||||
*/
|
*/
|
||||||
EMSCRIPTEN_KEEPALIVE
|
EMSCRIPTEN_KEEPALIVE
|
||||||
void destroy_output_buffer(uint32_t* p)
|
void destroy_output_buffer(void* p)
|
||||||
{
|
{
|
||||||
if (p != nullptr)
|
delete static_cast<std::vector<uint32_t>*>(p);
|
||||||
free(p);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
} // extern "C"
|
} // extern "C"
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* For non-Emscripten builds we supply a generic main, so that the glslang.js
|
* For non-Emscripten builds we supply a generic main, so that the glslang.js
|
||||||
@ -274,21 +254,17 @@ void destroy_output_buffer(uint32_t* p)
|
|||||||
*/
|
*/
|
||||||
#ifndef __EMSCRIPTEN__
|
#ifndef __EMSCRIPTEN__
|
||||||
int main() {
|
int main() {
|
||||||
const char* input_text = R"(#version 310 es
|
const char* input = R"(#version 310 es
|
||||||
|
|
||||||
void main() { })";
|
void main() { })";
|
||||||
|
|
||||||
char* input;
|
|
||||||
uint32_t* output;
|
uint32_t* output;
|
||||||
size_t output_len;
|
size_t output_len;
|
||||||
|
|
||||||
input = create_input_buffer(sizeof(input_text));
|
void* id = convert_glsl_to_spirv(input, 4, false, &output, &output_len);
|
||||||
assert(input != nullptr);
|
assert(output != nullptr);
|
||||||
memcpy(input, input_text, sizeof(input_text));
|
assert(output_len != 0);
|
||||||
|
destroy_output_buffer(id);
|
||||||
convert_glsl_to_spirv(input, 4, &output, &output_len, false);
|
|
||||||
destroy_output_buffer(output);
|
|
||||||
destroy_input_buffer(input);
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
#endif // !__EMSCRIPTEN__
|
#endif // ifndef __EMSCRIPTEN__
|
||||||
|
@ -14,23 +14,24 @@ Module['compileGLSLZeroCopy'] = function(glsl, shader_stage, gen_debug) {
|
|||||||
|
|
||||||
var p_output = Module['_malloc'](4);
|
var p_output = Module['_malloc'](4);
|
||||||
var p_output_len = Module['_malloc'](4);
|
var p_output_len = Module['_malloc'](4);
|
||||||
var err = ccall('convert_glsl_to_spirv',
|
var id = ccall('convert_glsl_to_spirv',
|
||||||
'number',
|
'number',
|
||||||
['string', 'number', 'number', 'number', 'boolean'],
|
['string', 'number', 'boolean', 'number', 'number'],
|
||||||
[glsl, shader_stage_int, p_output, p_output_len, gen_debug]);
|
[glsl, shader_stage_int, gen_debug, p_output, p_output_len]);
|
||||||
var output = getValue(p_output, 'i32');
|
var output = getValue(p_output, 'i32');
|
||||||
var output_len = getValue(p_output_len, 'i32');
|
var output_len = getValue(p_output_len, 'i32');
|
||||||
Module['_free'](p_output);
|
Module['_free'](p_output);
|
||||||
Module['_free'](p_output_len);
|
Module['_free'](p_output_len);
|
||||||
|
|
||||||
if (err !== 0 || output_len === 0) {
|
if (id === 0) {
|
||||||
throw new Error('GLSL compilation failed');
|
throw new Error('GLSL compilation failed');
|
||||||
}
|
}
|
||||||
|
|
||||||
var ret = {};
|
var ret = {};
|
||||||
ret.data = Module['HEAPU32'].subarray(output / 4, output / 4 + output_len);
|
var outputIndexU32 = output / 4;
|
||||||
|
ret.data = Module['HEAPU32'].subarray(outputIndexU32, outputIndexU32 + output_len);
|
||||||
ret.free = function() {
|
ret.free = function() {
|
||||||
Module['_destroy_output_buffer'](output);
|
Module['_destroy_output_buffer'](id);
|
||||||
};
|
};
|
||||||
|
|
||||||
return ret;
|
return ret;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user