From 2edf5a958484f8dc8fb6d3be14606852b696e379 Mon Sep 17 00:00:00 2001 From: Ray Date: Tue, 29 Nov 2022 10:45:10 +0100 Subject: [PATCH] REVIEWED: Issue with shader linkage --- src/rcore.c | 11 +++++++---- src/rlgl.h | 6 ++++-- 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/src/rcore.c b/src/rcore.c index 8f5b63b15..118a5736b 100644 --- a/src/rcore.c +++ b/src/rcore.c @@ -2446,10 +2446,6 @@ Shader LoadShader(const char *vsFileName, const char *fsFileName) Shader LoadShaderFromMemory(const char *vsCode, const char *fsCode) { Shader shader = { 0 }; - shader.locs = (int *)RL_CALLOC(RL_MAX_SHADER_LOCATIONS, sizeof(int)); - - // NOTE: All locations must be reseted to -1 (no location) - for (int i = 0; i < RL_MAX_SHADER_LOCATIONS; i++) shader.locs[i] = -1; shader.id = rlLoadShaderCode(vsCode, fsCode); @@ -2465,6 +2461,11 @@ Shader LoadShaderFromMemory(const char *vsCode, const char *fsCode) // vertex texcoord2 location = 5 // NOTE: If any location is not found, loc point becomes -1 + + shader.locs = (int *)RL_CALLOC(RL_MAX_SHADER_LOCATIONS, sizeof(int)); + + // All locations reseted to -1 (no location) + for (int i = 0; i < RL_MAX_SHADER_LOCATIONS; i++) shader.locs[i] = -1; // Get handles to GLSL input attibute locations shader.locs[SHADER_LOC_VERTEX_POSITION] = rlGetLocationAttrib(shader.id, RL_DEFAULT_SHADER_ATTRIB_NAME_POSITION); @@ -2497,6 +2498,8 @@ void UnloadShader(Shader shader) if (shader.id != rlGetShaderIdDefault()) { rlUnloadShaderProgram(shader.id); + + // NOTE: If shader loading failed, it should be 0 RL_FREE(shader.locs); } } diff --git a/src/rlgl.h b/src/rlgl.h index 04fcf8f91..e4f65d69a 100644 --- a/src/rlgl.h +++ b/src/rlgl.h @@ -3643,12 +3643,14 @@ unsigned int rlLoadShaderCode(const char *vsCode, const char *fsCode) // NOTE: We detach shader before deletion to make sure memory is freed if (vertexShaderId != RLGL.State.defaultVShaderId) { - glDetachShader(id, vertexShaderId); + // WARNING: Shader program linkage could fail and returned id is 0 + if (id > 0) glDetachShader(id, vertexShaderId); glDeleteShader(vertexShaderId); } if (fragmentShaderId != RLGL.State.defaultFShaderId) { - glDetachShader(id, fragmentShaderId); + // WARNING: Shader program linkage could fail and returned id is 0 + if (id > 0) glDetachShader(id, fragmentShaderId); glDeleteShader(fragmentShaderId); }