REVIEWED: rlLoadShaderCode()
This commit is contained in:
parent
e621efe79f
commit
f57727995a
41
src/rlgl.h
41
src/rlgl.h
@ -3544,41 +3544,44 @@ unsigned int rlLoadShaderCode(const char *vsCode, const char *fsCode)
|
|||||||
unsigned int vertexShaderId = 0;
|
unsigned int vertexShaderId = 0;
|
||||||
unsigned int fragmentShaderId = 0;
|
unsigned int fragmentShaderId = 0;
|
||||||
|
|
||||||
if (vsCode != NULL) {
|
// Compile vertex shader (if provided)
|
||||||
vertexShaderId = rlCompileShader(vsCode, GL_VERTEX_SHADER);
|
if (vsCode != NULL) vertexShaderId = rlCompileShader(vsCode, GL_VERTEX_SHADER);
|
||||||
} else {
|
// In case no vertex shader was provided or compilation failed, we use default vertex shader
|
||||||
vertexShaderId = RLGL.State.defaultVShaderId;
|
if (vertexShaderId == 0) vertexShaderId = RLGL.State.defaultVShaderId;
|
||||||
}
|
|
||||||
if (fsCode != NULL) {
|
|
||||||
fragmentShaderId = rlCompileShader(fsCode, GL_FRAGMENT_SHADER);
|
|
||||||
} else {
|
|
||||||
fragmentShaderId = RLGL.State.defaultFShaderId;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Load shader program if provided vertex/fragment shaders compile successfully
|
// Compile fragment shader (if provided)
|
||||||
if ((vertexShaderId != 0) && (fragmentShaderId != 0)) id = rlLoadShaderProgram(vertexShaderId, fragmentShaderId);
|
if (fsCode != NULL) fragmentShaderId = rlCompileShader(fsCode, GL_FRAGMENT_SHADER);
|
||||||
|
// In case no fragment shader was provided or compilation failed, we use default fragment shader
|
||||||
|
if (fragmentShaderId == 0) fragmentShaderId = RLGL.State.defaultFShaderId;
|
||||||
|
|
||||||
// Once shader program is compiled, we can detach and delete vertex/fragment shaders
|
// In case vertex and fragment shader are the default ones, no need to recompile, we can just assign the default shader program id
|
||||||
// NOTE: Vertex
|
if ((vertexShaderId == RLGL.State.defaultVShaderId) && (fragmentShaderId == RLGL.State.defaultFShaderId)) id = RLGL.State.defaultShaderId;
|
||||||
if (vertexShaderId != 0)
|
else
|
||||||
|
{
|
||||||
|
// One of or both shader are new, we need to compile a new shader program
|
||||||
|
id = rlLoadShaderProgram(vertexShaderId, fragmentShaderId);
|
||||||
|
|
||||||
|
// We can detach and delete vertex/fragment shaders (if not default ones)
|
||||||
|
// NOTE: We detach shader before deletion to make sure memory is freed
|
||||||
|
if (vertexShaderId != RLGL.State.defaultVShaderId)
|
||||||
{
|
{
|
||||||
// Detach shader before deletion to make sure memory is freed
|
|
||||||
glDetachShader(id, vertexShaderId);
|
glDetachShader(id, vertexShaderId);
|
||||||
glDeleteShader(vertexShaderId);
|
glDeleteShader(vertexShaderId);
|
||||||
}
|
}
|
||||||
if (fragmentShaderId != 0)
|
if (fragmentShaderId != RLGL.State.defaultFShaderId)
|
||||||
{
|
{
|
||||||
// Detach shader before deletion to make sure memory is freed
|
|
||||||
glDetachShader(id, fragmentShaderId);
|
glDetachShader(id, fragmentShaderId);
|
||||||
glDeleteShader(fragmentShaderId);
|
glDeleteShader(fragmentShaderId);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// In case shader program loading failed, we assign default shader
|
||||||
if (id == 0)
|
if (id == 0)
|
||||||
{
|
{
|
||||||
// In case shader loading fails, we return the default shader
|
// In case shader loading fails, we return the default shader
|
||||||
TRACELOG(RL_LOG_WARNING, "SHADER: Failed to load custom shader code, using default shader");
|
TRACELOG(RL_LOG_WARNING, "SHADER: Failed to load custom shader code, using default shader");
|
||||||
id = RLGL.State.defaultShaderId;
|
id = RLGL.State.defaultShaderId;
|
||||||
}
|
}
|
||||||
|
/*
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
// Get available shader uniforms
|
// Get available shader uniforms
|
||||||
@ -3600,6 +3603,8 @@ unsigned int rlLoadShaderCode(const char *vsCode, const char *fsCode)
|
|||||||
TRACELOGD("SHADER: [ID %i] Active uniform (%s) set at location: %i", id, name, glGetUniformLocation(id, name));
|
TRACELOGD("SHADER: [ID %i] Active uniform (%s) set at location: %i", id, name, glGetUniformLocation(id, name));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
*/
|
||||||
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
return id;
|
return id;
|
||||||
|
Loading…
Reference in New Issue
Block a user