finish cross compiliation to GLSL

This commit is contained in:
Gregory Wells
2025-08-12 22:40:06 -04:00
parent 23f46385fe
commit 3f7b0737d7
5 changed files with 46 additions and 13 deletions

View File

@@ -5,15 +5,45 @@ typedef struct glCompiler_t {
spirv_cross::CompilerGLSL* glsl;
} glInternalCompiler;
void handle_resources(spirv_cross::CompilerGLSL& compiler, spirv_cross::SmallVector<spirv_cross::Resource>& 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, &current_binding, shader.sets);
handle_resources(*compiler->glsl, arg_buffers.storage_buffers, &current_binding, shader.sets);
handle_resources(*compiler->glsl, arg_buffers.sampled_images, &current_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);
}

View File

@@ -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);

View File

@@ -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) {

View File

@@ -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);