Cleanup: remove loading of shader sources.

This commit is contained in:
Roz K 2022-12-04 01:43:35 +01:00
parent d5abbb0415
commit 7e015fda86
Signed by: roz
GPG Key ID: 51FBF4E483E1C822
4 changed files with 19 additions and 79 deletions

View File

@ -27,14 +27,6 @@ RK_EXPORT rk_window_t rk_create_context(
rk_uint width, rk_uint width,
rk_uint height); 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_swap_buffers();
RK_EXPORT void rk_destroy_context(); RK_EXPORT void rk_destroy_context();

View File

@ -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() { void rk_destroy_context() {
if (rk_display) { if (rk_display) {
glXMakeCurrent(rk_display, 0, nullptr); glXMakeCurrent(rk_display, 0, nullptr);

View File

@ -96,55 +96,40 @@ static void rk_print_program_infolog(GLuint program) {
//TODO: external loading of shader sources //TODO: external loading of shader sources
//TODO: error handling //TODO: error handling
rk_shader_t rk_load_shader( 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; rk_shader * const shader = new rk_shader;
shader->vertex = glCreateShader(GL_VERTEX_SHADER); shader->vertex = glCreateShader(GL_VERTEX_SHADER);
shader->fragment = glCreateShader(GL_FRAGMENT_SHADER); shader->fragment = glCreateShader(GL_FRAGMENT_SHADER);
shader->program = glCreateProgram(); shader->program = glCreateProgram();
if (vert_nlines == 0 || vert_lines == nullptr) {
char vertex_name[256]; rk_printf("Missing vertex shader.");
snprintf(vertex_name, sizeof(vertex_name), "%s_opengles.vert", name); return nullptr;
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 (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); glCompileShader(shader->vertex);
rk_error("glCompileShader() failed."); rk_error("glCompileShader() failed.");
rk_print_shader_infolog(shader->vertex); 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..."); rk_printf("Compiling fragment shader...");
glShaderSource(shader->fragment, fragment_length, fragment_source, nullptr); glShaderSource(shader->fragment, frag_nlines, frag_lines, nullptr);
}
glCompileShader(shader->fragment); glCompileShader(shader->fragment);
rk_error("glCompileShader() failed."); rk_error("glCompileShader() failed.");
rk_print_shader_infolog(shader->fragment); rk_print_shader_infolog(shader->fragment);
rk_printf("Linking program..."); rk_printf("Linking program...");
glAttachShader(shader->program, shader->vertex); glAttachShader(shader->program, shader->vertex);
glAttachShader(shader->program, shader->fragment); glAttachShader(shader->program, shader->fragment);
glLinkProgram(shader->program); glLinkProgram(shader->program);
rk_error("glLinkProgram() failed."); rk_error("glLinkProgram() failed.");
rk_print_program_infolog(shader->program); rk_print_program_infolog(shader->program);
rk_printf("Done."); rk_printf("Done.");
glReleaseShaderCompiler(); 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; return shader;
} }

View File

@ -87,7 +87,10 @@ RK_EXPORT rk_window_t rk_initialize(
rk_uint height); rk_uint height);
RK_EXPORT rk_shader_t rk_load_shader( 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_EXPORT void rk_select_shader(
rk_shader_t _shader); rk_shader_t _shader);