diff --git a/projects/apis/opengl/CMakeLists.txt b/projects/apis/opengl/CMakeLists.txt index 40b2131..3876518 100644 --- a/projects/apis/opengl/CMakeLists.txt +++ b/projects/apis/opengl/CMakeLists.txt @@ -3,7 +3,7 @@ set(CMAKE_CXX_STANDARD 17) project(GryphnOpenGLImpl) file(GLOB_RECURSE SOURCE_FILES CONFIGURE_DEPENDS - "src/*.c" + "src/*.c" "src/*.cpp" ) file(GLOB_RECURSE LOADER_FILES CONFIGURE_DEPENDS "loader/*.c" @@ -19,8 +19,9 @@ target_include_directories(GryphnOpenGLImpl PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/../../ ${CMAKE_CURRENT_SOURCE_DIR}/../../../include/ ) +add_subdirectory(depends/SPIRV-Cross) target_include_directories(GryphnOpenGLImpl PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/depends/glad/include/ ) add_compile_definitions(GN_REVEAL_IMPL) -target_link_libraries(GryphnOpenGLImpl GL "X11") +target_link_libraries(GryphnOpenGLImpl GL "X11" spirv-cross-core spirv-cross-glsl spirv-cross-cpp) diff --git a/projects/apis/opengl/src/shaders/opengl_shader_compiler.cpp b/projects/apis/opengl/src/shaders/opengl_shader_compiler.cpp index e69de29..6781a98 100644 --- a/projects/apis/opengl/src/shaders/opengl_shader_compiler.cpp +++ b/projects/apis/opengl/src/shaders/opengl_shader_compiler.cpp @@ -0,0 +1,19 @@ +#include "opengl_shader_compiler.h" +#include "spirv_glsl.hpp" + +typedef struct glCompiler_t { + spirv_cross::CompilerGLSL* glsl; +} glInternalCompiler; + +GN_CPP_FUNCTION glCompiler glCreateCompiler(glCompilerInfo* info) { + glInternalCompiler* compiler = (glInternalCompiler*)malloc(sizeof(glInternalCompiler)); + compiler->glsl = new spirv_cross::CompilerGLSL(info->code, info->wordCount); + return compiler; +} +GN_CPP_FUNCTION const char* glCompilerCompilerShader(glCompiler compiler) { + 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; +} diff --git a/projects/apis/opengl/src/shaders/opengl_shader_compiler.h b/projects/apis/opengl/src/shaders/opengl_shader_compiler.h index e69de29..bb5ec2e 100644 --- a/projects/apis/opengl/src/shaders/opengl_shader_compiler.h +++ b/projects/apis/opengl/src/shaders/opengl_shader_compiler.h @@ -0,0 +1,31 @@ +#pragma once +#include "stdint.h" +#include "glad/glad.h" +#include "utils/gryphn_cpp_function.h" + +#define MAX_OPENGL_SETS 32 +#define MAX_OPENGL_BINDINGS 16 + +typedef enum glShaderModuleStage { + glVertex = GL_VERTEX_SHADER, + mtlFragment = GL_FRAGMENT_SHADER, + mtlMaxStage +} glShaderModuleStage; + +typedef struct glCompilerInfo { + uint32_t* code; + int wordCount; + const char* entryPoint; + // glShaderModuleStage stage; +} glCompilerInfo; + +typedef struct glSet { uint32_t bindings[MAX_OPENGL_BINDINGS]; } glSet; + +typedef struct glShader { + const 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); diff --git a/projects/apis/opengl/src/shaders/opengl_shader_module.c b/projects/apis/opengl/src/shaders/opengl_shader_module.c index 4e4e487..4a425e0 100644 --- a/projects/apis/opengl/src/shaders/opengl_shader_module.c +++ b/projects/apis/opengl/src/shaders/opengl_shader_module.c @@ -1,9 +1,17 @@ #include "opengl_shader_module.h" -// #include "stdio.h" +#include "opengl_shader_compiler.h" +#include "stdlib.h" gnReturnCode openglCreateShaderModule(gnShaderModule module, gnDevice device, gnShaderModuleInfo shaderModuleInfo) { + module->shaderModule = malloc(sizeof(gnPlatformShaderModule)); + glCompilerInfo info = { + .code = shaderModuleInfo.code, + .wordCount = shaderModuleInfo.size / 4, + .entryPoint = gnToCString(shaderModuleInfo.entryPoint), + }; + module->shaderModule->compiler = glCreateCompiler(&info); return GN_SUCCESS; } void openglDestroyShaderModule(gnShaderModule module) { - + free(module->shaderModule); } diff --git a/projects/apis/opengl/src/shaders/opengl_shader_module.h b/projects/apis/opengl/src/shaders/opengl_shader_module.h index ae29d18..758424f 100644 --- a/projects/apis/opengl/src/shaders/opengl_shader_module.h +++ b/projects/apis/opengl/src/shaders/opengl_shader_module.h @@ -1,7 +1,9 @@ +#pragma once #include "core/src/shader_module/gryphn_shader_module.h" +#include "opengl_shader_compiler.h" typedef struct gnPlatformShaderModule_t { - const char* shaderSource; + glCompiler compiler; } gnPlatformShaderModule; gnReturnCode openglCreateShaderModule(gnShaderModule module, gnDevice device, gnShaderModuleInfo shaderModuleInfo);