From 3f7b0737d7b4afdff0c251021c7dafa65d0f8eed Mon Sep 17 00:00:00 2001 From: Gregory Wells Date: Tue, 12 Aug 2025 22:40:06 -0400 Subject: [PATCH] finish cross compiliation to GLSL --- .../src/shader_module/metal_shader_compiler.h | 4 +- .../src/shaders/opengl_shader_compiler.cpp | 40 ++++++++++++++++--- .../src/shaders/opengl_shader_compiler.h | 9 +++-- .../opengl/src/shaders/opengl_shader_module.c | 4 +- .../opengl/src/shaders/opengl_shader_module.h | 2 +- 5 files changed, 46 insertions(+), 13 deletions(-) diff --git a/projects/apis/metal/src/shader_module/metal_shader_compiler.h b/projects/apis/metal/src/shader_module/metal_shader_compiler.h index b23f45b..0f680a9 100644 --- a/projects/apis/metal/src/shader_module/metal_shader_compiler.h +++ b/projects/apis/metal/src/shader_module/metal_shader_compiler.h @@ -4,8 +4,8 @@ #include "utils/gryphn_cpp_function.h" #include -#define MAX_METAL_SETS 32 -#define MAX_METAL_BINDINGS 16 +#define MAX_METAL_SETS 16 +#define MAX_METAL_BINDINGS 32 typedef struct mtlCompiler_t* mtlCompiler; diff --git a/projects/apis/opengl/src/shaders/opengl_shader_compiler.cpp b/projects/apis/opengl/src/shaders/opengl_shader_compiler.cpp index 6781a98..336e47e 100644 --- a/projects/apis/opengl/src/shaders/opengl_shader_compiler.cpp +++ b/projects/apis/opengl/src/shaders/opengl_shader_compiler.cpp @@ -5,15 +5,45 @@ typedef struct glCompiler_t { spirv_cross::CompilerGLSL* glsl; } glInternalCompiler; +void handle_resources(spirv_cross::CompilerGLSL& compiler, spirv_cross::SmallVector& resources, int* currentBinding, glSet* setMap) { + for (size_t i = 0; i < resources.size(); i++) { + uint32_t + set = compiler.get_decoration(resources[i].id, spv::DecorationDescriptorSet), + binding = compiler.get_decoration(resources[i].id, spv::DecorationBinding); + setMap[set].bindings[binding] = *currentBinding; + + compiler.unset_decoration(resources[i].id, spv::DecorationBinding); + compiler.set_decoration(resources[i].id, spv::DecorationBinding, *currentBinding); + *currentBinding = (*currentBinding) + 1; + } +} + GN_CPP_FUNCTION glCompiler glCreateCompiler(glCompilerInfo* info) { glInternalCompiler* compiler = (glInternalCompiler*)malloc(sizeof(glInternalCompiler)); compiler->glsl = new spirv_cross::CompilerGLSL(info->code, info->wordCount); + // spirv_cross::CompilerGLSL::Options options = compiler->glsl->get_common_options(); + // compiler->glsl->set_common_options(options); return compiler; } -GN_CPP_FUNCTION const char* glCompilerCompilerShader(glCompiler compiler) { +GN_CPP_FUNCTION glShader glCompilerCompilerShader(glCompiler compiler) { + int current_binding = 0; + + glShader shader = {}; + auto arg_buffers = compiler->glsl->get_shader_resources(); + handle_resources(*compiler->glsl, arg_buffers.uniform_buffers, ¤t_binding, shader.sets); + handle_resources(*compiler->glsl, arg_buffers.storage_buffers, ¤t_binding, shader.sets); + handle_resources(*compiler->glsl, arg_buffers.sampled_images, ¤t_binding, shader.sets); + + shader.sets[0].bindings[3] = 69; + std::string output = compiler->glsl->compile(); - char* copied_output = (char*)malloc(sizeof(char*) * (output.size() + 1)); - strcpy(copied_output, output.c_str()); - copied_output[output.size()] = '\0'; - return copied_output; + shader.source = (char*)malloc(sizeof(char*) * (output.size() + 1)); + strcpy(shader.source, output.c_str()); + shader.source[output.size()] = '\0'; + return shader; +} + +GN_CPP_FUNCTION void glDestroyCompiler(glCompiler compiler) { + delete compiler->glsl; + free(compiler); } diff --git a/projects/apis/opengl/src/shaders/opengl_shader_compiler.h b/projects/apis/opengl/src/shaders/opengl_shader_compiler.h index bb5ec2e..b7e186f 100644 --- a/projects/apis/opengl/src/shaders/opengl_shader_compiler.h +++ b/projects/apis/opengl/src/shaders/opengl_shader_compiler.h @@ -3,8 +3,8 @@ #include "glad/glad.h" #include "utils/gryphn_cpp_function.h" -#define MAX_OPENGL_SETS 32 -#define MAX_OPENGL_BINDINGS 16 +#define MAX_OPENGL_SETS 16 +#define MAX_OPENGL_BINDINGS 32 typedef enum glShaderModuleStage { glVertex = GL_VERTEX_SHADER, @@ -22,10 +22,11 @@ typedef struct glCompilerInfo { typedef struct glSet { uint32_t bindings[MAX_OPENGL_BINDINGS]; } glSet; typedef struct glShader { - const char* source; + char* source; glSet sets[MAX_OPENGL_SETS]; } glShader; typedef struct glCompiler_t* glCompiler; GN_CPP_FUNCTION glCompiler glCreateCompiler(glCompilerInfo* info); -GN_CPP_FUNCTION const char* glCompilerCompilerShader(glCompiler compiler); +GN_CPP_FUNCTION glShader glCompilerCompilerShader(glCompiler compiler); +GN_CPP_FUNCTION void glDestroyCompiler(glCompiler compiler); diff --git a/projects/apis/opengl/src/shaders/opengl_shader_module.c b/projects/apis/opengl/src/shaders/opengl_shader_module.c index 4a425e0..f3253ba 100644 --- a/projects/apis/opengl/src/shaders/opengl_shader_module.c +++ b/projects/apis/opengl/src/shaders/opengl_shader_module.c @@ -9,7 +9,9 @@ gnReturnCode openglCreateShaderModule(gnShaderModule module, gnDevice device, gn .wordCount = shaderModuleInfo.size / 4, .entryPoint = gnToCString(shaderModuleInfo.entryPoint), }; - module->shaderModule->compiler = glCreateCompiler(&info); + glCompiler compiler = glCreateCompiler(&info); + module->shaderModule->shader = glCompilerCompilerShader(compiler); + glDestroyCompiler(compiler); return GN_SUCCESS; } void openglDestroyShaderModule(gnShaderModule module) { diff --git a/projects/apis/opengl/src/shaders/opengl_shader_module.h b/projects/apis/opengl/src/shaders/opengl_shader_module.h index 758424f..4b2c54b 100644 --- a/projects/apis/opengl/src/shaders/opengl_shader_module.h +++ b/projects/apis/opengl/src/shaders/opengl_shader_module.h @@ -3,7 +3,7 @@ #include "opengl_shader_compiler.h" typedef struct gnPlatformShaderModule_t { - glCompiler compiler; + glShader shader; } gnPlatformShaderModule; gnReturnCode openglCreateShaderModule(gnShaderModule module, gnDevice device, gnShaderModuleInfo shaderModuleInfo);