Skip to content

Commit

Permalink
drivers/glrend/shader: refactor error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
vs49688 committed Oct 12, 2024
1 parent 5a83652 commit 85a0e54
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 73 deletions.
41 changes: 13 additions & 28 deletions drivers/glrend/shader_brender.c
Original file line number Diff line number Diff line change
Expand Up @@ -71,46 +71,31 @@ br_boolean VIDEOI_CompileBRenderShader(HVIDEO hVideo, const char *vertPath, cons

vert = VIDEOI_LoadAndCompileShader(GL_VERTEX_SHADER, vertPath, g_DefaultVertexShader, sizeof(g_DefaultVertexShader));
if(!vert)
return BR_FALSE;
goto vert_failed;

frag = VIDEOI_LoadAndCompileShader(GL_FRAGMENT_SHADER, fragPath, g_DefaultFragmentShader, sizeof(g_DefaultFragmentShader));
if(!frag) {
glDeleteShader(vert);
return BR_FALSE;
}
if(!frag)
goto frag_failed;

if(!(hVideo->brenderProgram.program = VIDEOI_CreateAndCompileProgram(vert, frag))) {
glDeleteShader(vert);
glDeleteShader(frag);
return BR_FALSE;
}
if(!(hVideo->brenderProgram.program = VIDEOI_CreateAndCompileProgram(vert, frag)))
goto prog_failed;

DeviceGLObjectLabel(GL_SHADER, vert, BR_GLREND_DEBUG_INTERNAL_PREFIX "brender:shader:vertex");
DeviceGLObjectLabel(GL_SHADER, frag, BR_GLREND_DEBUG_INTERNAL_PREFIX "brender:shader:fragment");
DeviceGLObjectLabel(GL_PROGRAM, hVideo->brenderProgram.program, BR_GLREND_DEBUG_INTERNAL_PREFIX "brender:shader:program");

glDeleteShader(vert);
glDeleteShader(frag);
hVideo->brenderProgram.blockIndexScene = glGetUniformBlockIndex(hVideo->brenderProgram.program, "br_scene_state");
hVideo->brenderProgram.blockIndexModel = glGetUniformBlockIndex(hVideo->brenderProgram.program, "br_model_state");

if(hVideo->brenderProgram.program) {
hVideo->brenderProgram.blockIndexScene = glGetUniformBlockIndex(hVideo->brenderProgram.program,
"br_scene_state");
if(hVideo->brenderProgram.blockIndexScene == GL_INVALID_INDEX) {
BrLogError("VIDEO", "Unable to retrieve block index for uniform block 'br_scene_state'.");
return BR_FALSE;
}

hVideo->brenderProgram.blockIndexModel = glGetUniformBlockIndex(hVideo->brenderProgram.program,
"br_model_state");
if(hVideo->brenderProgram.blockIndexModel == GL_INVALID_INDEX) {
BrLogError("VIDEO", "Unable to retrieve block index for uniform block 'br_model_state'.");
return BR_FALSE;
}
VIDEOI_GetShaderVariables(hVideo);

VIDEOI_GetShaderVariables(hVideo);
}
prog_failed:
glDeleteShader(frag);

frag_failed:
glDeleteShader(vert);

vert_failed:
DeviceGLCheckErrors();

return hVideo->brenderProgram.program != 0;
Expand Down
33 changes: 15 additions & 18 deletions drivers/glrend/shader_rect.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,31 +5,21 @@

br_boolean VIDEOI_CompileRectShader(HVIDEO hVideo)
{
GLuint vert = VIDEOI_CreateAndCompileShader(GL_VERTEX_SHADER, g_RectVertexShader, sizeof(g_RectVertexShader));
if(!vert)
return BR_FALSE;
GLuint vert, frag;

GLuint frag = VIDEOI_CreateAndCompileShader(GL_FRAGMENT_SHADER, g_RectFragmentShader, sizeof(g_RectFragmentShader));
if(!frag) {
glDeleteShader(vert);
return BR_FALSE;
}
if((vert = VIDEOI_CreateAndCompileShader(GL_VERTEX_SHADER, g_RectVertexShader, sizeof(g_RectVertexShader))) == 0)
goto vert_failed;

if((hVideo->rectProgram.program = VIDEOI_CreateAndCompileProgram(vert, frag)) == 0) {
DeviceGLCheckErrors();
if((frag = VIDEOI_CreateAndCompileShader(GL_FRAGMENT_SHADER, g_RectFragmentShader, sizeof(g_RectFragmentShader))) == 0)
goto frag_failed;

glDeleteShader(vert);
glDeleteShader(frag);
return BR_FALSE;
}
if((hVideo->rectProgram.program = VIDEOI_CreateAndCompileProgram(vert, frag)) == 0)
goto prog_failed;

DeviceGLObjectLabel(GL_SHADER, vert, BR_GLREND_DEBUG_INTERNAL_PREFIX "rect:shader:vertex");
DeviceGLObjectLabel(GL_SHADER, frag, BR_GLREND_DEBUG_INTERNAL_PREFIX "rect:shader:fragment");
DeviceGLObjectLabel(GL_PROGRAM, hVideo->rectProgram.program, BR_GLREND_DEBUG_INTERNAL_PREFIX "rect:program");

glDeleteShader(vert);
glDeleteShader(frag);

glGenVertexArrays(1, &hVideo->rectProgram.vao);
DeviceGLObjectLabel(GL_VERTEX_ARRAY, hVideo->rectProgram.vao, BR_GLREND_DEBUG_INTERNAL_PREFIX "rect:vao");

Expand All @@ -50,7 +40,14 @@ br_boolean VIDEOI_CompileRectShader(HVIDEO hVideo)

glBindFragDataLocation(hVideo->rectProgram.program, 0, "main_colour");

prog_failed:
glDeleteShader(frag);

frag_failed:
glDeleteShader(vert);

vert_failed:
DeviceGLCheckErrors();

return BR_TRUE;
return hVideo->rectProgram.program != 0;
}
55 changes: 28 additions & 27 deletions drivers/glrend/shader_text.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,51 +8,52 @@

br_boolean VIDEOI_CompileTextShader(HVIDEO hVideo)
{
GLuint vert = VIDEOI_CreateAndCompileShader(GL_VERTEX_SHADER, g_TextVertexShader, sizeof(g_TextVertexShader));
if(!vert)
return BR_FALSE;
GLuint vert, frag;

GLuint frag = VIDEOI_CreateAndCompileShader(GL_FRAGMENT_SHADER, g_TextFragmentShader, sizeof(g_TextFragmentShader));
if(!frag) {
glDeleteShader(vert);
return BR_FALSE;
}
if((vert = VIDEOI_CreateAndCompileShader(GL_VERTEX_SHADER, g_TextVertexShader, sizeof(g_TextVertexShader))) == 0)
goto vert_failed;

hVideo->textProgram.program = VIDEOI_CreateAndCompileProgram(vert, frag);
if((frag = VIDEOI_CreateAndCompileShader(GL_FRAGMENT_SHADER, g_TextFragmentShader, sizeof(g_TextFragmentShader))) == 0)
goto frag_failed;

if((hVideo->textProgram.program = VIDEOI_CreateAndCompileProgram(vert, frag)) == 0)
goto prog_failed;

DeviceGLObjectLabel(GL_SHADER, vert, BR_GLREND_DEBUG_INTERNAL_PREFIX "text:shader:vertex");
DeviceGLObjectLabel(GL_SHADER, frag, BR_GLREND_DEBUG_INTERNAL_PREFIX "text:shader:fragment");
DeviceGLObjectLabel(GL_PROGRAM, hVideo->textProgram.program, BR_GLREND_DEBUG_INTERNAL_PREFIX "text:program");

glDeleteShader(vert);
glDeleteShader(frag);

glGenVertexArrays(1, &hVideo->textProgram.vao_glyphs);
DeviceGLObjectLabel(GL_VERTEX_ARRAY, hVideo->textProgram.vao_glyphs, BR_GLREND_DEBUG_INTERNAL_PREFIX "text:vao");

glGenBuffers(1, &hVideo->textProgram.ubo_glyphs);
DeviceGLObjectLabel(GL_BUFFER, hVideo->textProgram.ubo_glyphs, BR_GLREND_DEBUG_INTERNAL_PREFIX "text:ubo");

if(hVideo->textProgram.program) {
hVideo->textProgram.uSampler = glGetUniformLocation(hVideo->textProgram.program, "uSampler");
hVideo->textProgram.uSampler = glGetUniformLocation(hVideo->textProgram.program, "uSampler");

hVideo->textProgram.block_index_font_data = glGetUniformBlockIndex(hVideo->textProgram.program, "FontData");
hVideo->textProgram.block_binding_font_data = 2;
glUniformBlockBinding(hVideo->textProgram.program, hVideo->textProgram.block_index_font_data,
hVideo->textProgram.block_binding_font_data);
hVideo->textProgram.block_index_font_data = glGetUniformBlockIndex(hVideo->textProgram.program, "FontData");
hVideo->textProgram.block_binding_font_data = 2;
glUniformBlockBinding(hVideo->textProgram.program, hVideo->textProgram.block_index_font_data,
hVideo->textProgram.block_binding_font_data);

hVideo->textProgram.block_index_glyphs = glGetUniformBlockIndex(hVideo->textProgram.program, "TextData");
hVideo->textProgram.block_binding_glyphs = 3;
glUniformBlockBinding(hVideo->textProgram.program, hVideo->textProgram.block_index_glyphs,
hVideo->textProgram.block_binding_glyphs);
hVideo->textProgram.block_index_glyphs = glGetUniformBlockIndex(hVideo->textProgram.program, "TextData");
hVideo->textProgram.block_binding_glyphs = 3;
glUniformBlockBinding(hVideo->textProgram.program, hVideo->textProgram.block_index_glyphs,
hVideo->textProgram.block_binding_glyphs);

glBindBuffer(GL_UNIFORM_BUFFER, hVideo->textProgram.ubo_glyphs);
glBindBufferBase(GL_UNIFORM_BUFFER, hVideo->textProgram.block_binding_glyphs, hVideo->textProgram.ubo_glyphs);
glBindBuffer(GL_UNIFORM_BUFFER, 0);
glBindBuffer(GL_UNIFORM_BUFFER, hVideo->textProgram.ubo_glyphs);
glBindBufferBase(GL_UNIFORM_BUFFER, hVideo->textProgram.block_binding_glyphs, hVideo->textProgram.ubo_glyphs);
glBindBuffer(GL_UNIFORM_BUFFER, 0);

glBindFragDataLocation(hVideo->textProgram.program, 0, "main_colour");
}
glBindFragDataLocation(hVideo->textProgram.program, 0, "main_colour");

prog_failed:
glDeleteShader(frag);

frag_failed:
glDeleteShader(vert);

vert_failed:
DeviceGLCheckErrors();

return hVideo->textProgram.program != 0;
Expand Down
2 changes: 2 additions & 0 deletions drivers/glrend/video.c
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,8 @@ HVIDEO VIDEO_Open(HVIDEO hVideo, const char *vertShader, const char *fragShader)
return NULL;
}

BrMemSet(hVideo, 0, sizeof(VIDEO));

glGetIntegerv(GL_MAX_UNIFORM_BLOCK_SIZE, &hVideo->maxUniformBlockSize);
glGetIntegerv(GL_MAX_UNIFORM_BUFFER_BINDINGS, &hVideo->maxUniformBufferBindings);
glGetIntegerv(GL_MAX_VERTEX_UNIFORM_BLOCKS, &hVideo->maxVertexUniformBlocks);
Expand Down

0 comments on commit 85a0e54

Please sign in to comment.