From 6ec993c6f8b57698bc7c7488901c209e7142d8b2 Mon Sep 17 00:00:00 2001 From: Roz K Date: Sat, 31 Dec 2022 11:52:26 +0100 Subject: [PATCH] Some errors handling. --- cpp/render/render_opengles.cpp | 115 ++++++++++++++++++++------------- cpp/types.hpp | 2 + 2 files changed, 71 insertions(+), 46 deletions(-) diff --git a/cpp/render/render_opengles.cpp b/cpp/render/render_opengles.cpp index 91680c6..ed03189 100644 --- a/cpp/render/render_opengles.cpp +++ b/cpp/render/render_opengles.cpp @@ -32,8 +32,6 @@ static void rk_printf(char const * message) { printf("[RK] %s\n", message); } -#define rk_gl_error(_message) { if (glGetError() != GL_NO_ERROR) { printf("[GL] %s\n", (_message)); } } - static void rk_debug_message_callback( GLenum source, GLenum type, @@ -96,60 +94,79 @@ void rk_render_initialize( void rk_render_terminate() { } +static char rk_infolog[1024]; + static void rk_print_shader_infolog(GLuint shader) { GLsizei length; - char infolog[1024]; - glGetShaderInfoLog(shader, sizeof(infolog), &length, infolog); + glGetShaderInfoLog(shader, sizeof(rk_infolog) - 1, &length, rk_infolog); if (length > 0) { - rk_printf(infolog); + rk_gl_printf(rk_infolog); } } static void rk_print_program_infolog(GLuint program) { GLsizei length; - char infolog[1024]; - glGetProgramInfoLog(program, sizeof(infolog), &length, infolog); + glGetProgramInfoLog(program, sizeof(rk_infolog) - 1, &length, rk_infolog); if (length > 0) { - rk_printf(infolog); + rk_gl_printf(rk_infolog); } } -//TODO: error handling rk_shader_t rk_load_shader( 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(); - if (!vert_nlines || !vert_lines) { - rk_printf("Missing vertex shader."); - return nullptr; - } - if (!frag_nlines || !frag_lines) { - rk_printf("Missing fragment shader."); - return nullptr; + if (!vert_nlines || !vert_lines || !frag_nlines || !frag_lines) { + rk_printf("rk_load_shader(): invalid params."); + return RK_INVALID_HANDLE; } rk_printf("Compiling vertex shader..."); - glShaderSource(shader->vertex, vert_nlines, vert_lines, nullptr); - glCompileShader(shader->vertex); - rk_gl_error("glCompileShader() failed."); - rk_print_shader_infolog(shader->vertex); + while (glGetError() != GL_NO_ERROR); + GLuint const vert = glCreateShader(GL_VERTEX_SHADER); + glShaderSource(vert, vert_nlines, vert_lines, nullptr); + glCompileShader(vert); + GLint vert_success = 0; + glGetShaderiv(vert, GL_COMPILE_STATUS, &vert_success); + if (!vert_success || glGetError() != GL_NO_ERROR) { + rk_print_shader_infolog(vert); + glDeleteShader(vert); + return RK_INVALID_HANDLE; + } rk_printf("Compiling fragment shader..."); - glShaderSource(shader->fragment, frag_nlines, frag_lines, nullptr); - glCompileShader(shader->fragment); - rk_gl_error("glCompileShader() failed."); - rk_print_shader_infolog(shader->fragment); + while (glGetError() != GL_NO_ERROR); + GLuint const frag = glCreateShader(GL_FRAGMENT_SHADER); + glShaderSource(frag, frag_nlines, frag_lines, nullptr); + glCompileShader(frag); + GLint frag_success = 0; + glGetShaderiv(frag, GL_COMPILE_STATUS, &frag_success); + if (!frag_success || glGetError() != GL_NO_ERROR) { + rk_print_shader_infolog(frag); + glDeleteShader(vert); + glDeleteShader(frag); + return RK_INVALID_HANDLE; + } rk_printf("Linking program..."); - glAttachShader(shader->program, shader->vertex); - glAttachShader(shader->program, shader->fragment); - glLinkProgram(shader->program); - rk_gl_error("glLinkProgram() failed."); - rk_print_program_infolog(shader->program); + while (glGetError() != GL_NO_ERROR); + GLuint prog = glCreateProgram(); + glAttachShader(prog, vert); + glAttachShader(prog, frag); + glLinkProgram(prog); + GLint prog_success = 0; + glGetProgramiv(prog, GL_LINK_STATUS, &prog_success); + if (!prog_success || glGetError() != GL_NO_ERROR) { + rk_print_program_infolog(prog); + glDeleteShader(vert); + glDeleteShader(frag); + glDeleteProgram(prog); + return RK_INVALID_HANDLE; + } rk_printf("Done."); glReleaseShaderCompiler(); + rk_shader * const shader = new rk_shader; + shader->vertex = vert; + shader->fragment = frag; + shader->program = prog; return reinterpret_cast(shader); } @@ -158,7 +175,8 @@ rk_input_t rk_resolve_input( char const * name) { rk_shader const * const shader = reinterpret_cast(_shader); if (!shader || !name) { - return nullptr; + rk_printf("rk_resolve_input(): invalid params."); + return RK_INVALID_HANDLE; } GLint const uniform = glGetUniformLocation(shader->program, name); return reinterpret_cast(uniform + 1); @@ -169,7 +187,8 @@ rk_param_t rk_resolve_param( char const * name) { rk_shader const * const shader = reinterpret_cast(_shader); if (!shader || !name) { - return nullptr; + rk_printf("rk_resolve_param(): invalid params."); + return RK_INVALID_HANDLE; } GLint const location = glGetAttribLocation(shader->program, name); return reinterpret_cast(location + 1); @@ -183,7 +202,8 @@ rk_texture_t rk_create_texture( rk_texture_flags flags, rk_ubyte const * pixels) { if (!width || !height || !pixels) { - return nullptr; + rk_printf("rk_create_texture(): invalid params."); + return RK_INVALID_HANDLE; } GLint internal_format; GLenum source_format; @@ -210,7 +230,8 @@ rk_texture_t rk_create_texture( source_type = GL_FLOAT; break; default: - return nullptr; + rk_printf("rk_create_texture(): invalid texture format."); + return RK_INVALID_HANDLE; break; } rk_texture * const texture = new rk_texture; @@ -264,7 +285,8 @@ rk_triangles_t rk_create_triangles( rk_uint nvertices, rk_vec3 const * vertices) { if (!nvertices || !vertices) { - return nullptr; + rk_printf("rk_create_triangles(): invalid params."); + return RK_INVALID_HANDLE; } rk_triangles * const triangles = new rk_triangles; triangles->size = nvertices; @@ -287,7 +309,8 @@ rk_vertices_t rk_create_vertices( rk_uint nindices, rk_ushort const * indices) { if (!format || !nvertices || !_vertices || !nindices || !indices) { - return nullptr; + rk_printf("rk_create_vertices(): invalid params."); + return RK_INVALID_HANDLE; } unsigned format_size = 0; unsigned vertex_size = 0; @@ -303,14 +326,14 @@ rk_vertices_t rk_create_vertices( vertex_size += sizeof(rk_vec3_uint10); break; default: - rk_printf("rk_create_vertices(): invalid format."); - return nullptr; + rk_printf("rk_create_vertices(): invalid vertex format."); + return RK_INVALID_HANDLE; break; } } if (!format_size) { - rk_printf("rk_create_vertices(): empty format."); - return nullptr; + rk_printf("rk_create_vertices(): empty vertex format."); + return RK_INVALID_HANDLE; } rk_vertices * const vertices = new rk_vertices; vertices->nvertices = nvertices; @@ -462,8 +485,8 @@ rk_batch_t rk_create_batch( rk_param_format const * params_format) { rk_vertices const * const vertices = reinterpret_cast(_vertices); if (!vertices || !max_size || max_size > RK_BATCH_MAX_SIZE) { - rk_printf("rk_create_batch(): invalid parameters."); - return nullptr; + rk_printf("rk_create_batch(): invalid params."); + return RK_INVALID_HANDLE; } unsigned vertex_size = 0; for (rk_vertex_format const * f = vertices->format; *f; ++f) { @@ -501,7 +524,7 @@ rk_batch_t rk_create_batch( break; default: rk_printf("rk_create_batch(): invalid param format."); - return nullptr; + return RK_INVALID_HANDLE; break; } } diff --git a/cpp/types.hpp b/cpp/types.hpp index edac367..05cbc44 100644 --- a/cpp/types.hpp +++ b/cpp/types.hpp @@ -36,4 +36,6 @@ typedef uint64_t rk_ulong; typedef void * rk_handle_t; +#define RK_INVALID_HANDLE nullptr + #endif // _RK_ENGINE_TYPES_H