From 7e015fda86aaf66c81752d83b4383678918e2364 Mon Sep 17 00:00:00 2001 From: Roz K Date: Sun, 4 Dec 2022 01:43:35 +0100 Subject: [PATCH] Cleanup: remove loading of shader sources. --- cpp/opengl/render_context.hpp | 8 ------ cpp/opengl/render_context_glx.cpp | 40 --------------------------- cpp/opengl/render_opengles.cpp | 45 +++++++++++-------------------- cpp/render.hpp | 5 +++- 4 files changed, 19 insertions(+), 79 deletions(-) diff --git a/cpp/opengl/render_context.hpp b/cpp/opengl/render_context.hpp index 5d44619..6c95218 100644 --- a/cpp/opengl/render_context.hpp +++ b/cpp/opengl/render_context.hpp @@ -27,14 +27,6 @@ RK_EXPORT rk_window_t rk_create_context( rk_uint width, rk_uint height); -RK_EXPORT char ** rk_load_shader_source( - char const * filename, - rk_uint * length); - -RK_EXPORT void rk_free_shader_source( - char ** shader, - rk_uint length); - RK_EXPORT void rk_swap_buffers(); RK_EXPORT void rk_destroy_context(); diff --git a/cpp/opengl/render_context_glx.cpp b/cpp/opengl/render_context_glx.cpp index 78cfb4f..3a85ac1 100644 --- a/cpp/opengl/render_context_glx.cpp +++ b/cpp/opengl/render_context_glx.cpp @@ -215,46 +215,6 @@ void rk_swap_buffers() { } } -char ** rk_load_shader_source( - char const * filename, - rk_uint * length) { - char ** shader = nullptr; - char buffer[1024]; - FILE * const file = fopen(filename, "rt"); - if (file) { - int nlines = 0; - while (fgets(buffer, sizeof(buffer), file)) { - ++nlines; - } - if (nlines) { - rewind(file); - shader = new char*[nlines]; - for (int line = 0; line < nlines; ++line) { - shader[line] = new char[sizeof(buffer)]; - fgets(shader[line], sizeof(buffer), file); - } - } else { - printf("Shader %s is empty.\n", filename); - } - fclose(file); - *length = nlines; - } else { - printf("Cannot open shader %s.\n", filename); - } - return shader; -} - -void rk_free_shader_source( - char ** shader, - rk_uint length) { - if (shader) { - for (rk_uint line = 0; line < length; ++line) { - delete[] shader[line]; - } - } - delete[] shader; -} - void rk_destroy_context() { if (rk_display) { glXMakeCurrent(rk_display, 0, nullptr); diff --git a/cpp/opengl/render_opengles.cpp b/cpp/opengl/render_opengles.cpp index c4c28fa..2a6d4dc 100644 --- a/cpp/opengl/render_opengles.cpp +++ b/cpp/opengl/render_opengles.cpp @@ -96,55 +96,40 @@ static void rk_print_program_infolog(GLuint program) { //TODO: external loading of shader sources //TODO: error handling rk_shader_t rk_load_shader( - char const * name) { + rk_uint const vert_nlines, + char const ** const vert_lines, + rk_uint const frag_nlines, + char const ** const frag_lines) { rk_shader * const shader = new rk_shader; shader->vertex = glCreateShader(GL_VERTEX_SHADER); shader->fragment = glCreateShader(GL_FRAGMENT_SHADER); shader->program = glCreateProgram(); - - char vertex_name[256]; - snprintf(vertex_name, sizeof(vertex_name), "%s_opengles.vert", name); - printf("[RK_ENGINE] Loading vertex shader %s...\n", vertex_name); - rk_uint vertex_length = 0; - char ** vertex_source = rk_load_shader_source(vertex_name, &vertex_length); - if (vertex_source) { - rk_printf("Compiling vertex shader..."); - glShaderSource(shader->vertex, vertex_length, vertex_source, nullptr); + if (vert_nlines == 0 || vert_lines == nullptr) { + rk_printf("Missing vertex shader."); + return nullptr; } + if (frag_nlines == 0 || frag_lines == nullptr) { + rk_printf("Missing fragment shader."); + return nullptr; + } + rk_printf("Compiling vertex shader..."); + glShaderSource(shader->vertex, vert_nlines, vert_lines, nullptr); glCompileShader(shader->vertex); rk_error("glCompileShader() failed."); rk_print_shader_infolog(shader->vertex); - - char fragment_name[256]; - snprintf(fragment_name, sizeof(fragment_name), "%s_opengles.frag", name); - printf("[RK_ENGINE] Loading fragment shader %s...\n", fragment_name); - rk_uint fragment_length = 0; - char ** fragment_source = rk_load_shader_source(fragment_name, &fragment_length); - if (fragment_source) { - rk_printf("Compiling fragment shader..."); - glShaderSource(shader->fragment, fragment_length, fragment_source, nullptr); - } + rk_printf("Compiling fragment shader..."); + glShaderSource(shader->fragment, frag_nlines, frag_lines, nullptr); glCompileShader(shader->fragment); rk_error("glCompileShader() failed."); rk_print_shader_infolog(shader->fragment); - rk_printf("Linking program..."); glAttachShader(shader->program, shader->vertex); glAttachShader(shader->program, shader->fragment); glLinkProgram(shader->program); rk_error("glLinkProgram() failed."); rk_print_program_infolog(shader->program); - rk_printf("Done."); - glReleaseShaderCompiler(); - if (vertex_source != nullptr) { - rk_free_shader_source(vertex_source, vertex_length); - } - if (fragment_source != nullptr) { - rk_free_shader_source(fragment_source, fragment_length); - } - return shader; } diff --git a/cpp/render.hpp b/cpp/render.hpp index c0d637c..d2c8df1 100644 --- a/cpp/render.hpp +++ b/cpp/render.hpp @@ -87,7 +87,10 @@ RK_EXPORT rk_window_t rk_initialize( rk_uint height); RK_EXPORT rk_shader_t rk_load_shader( - char const * name); + rk_uint const vert_nlines, + char const ** const vert_lines, + rk_uint const frag_nlines, + char const ** const frag_lines); RK_EXPORT void rk_select_shader( rk_shader_t _shader);