fix shader memory leaks
This commit is contained in:
parent
0cfd2e577b
commit
397b67fa1a
|
@ -6,7 +6,7 @@
|
||||||
/* By: gbrochar <gbrochar@student.42.fr> +#+ +:+ +#+ */
|
/* By: gbrochar <gbrochar@student.42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2020/12/22 12:08:22 by gbrochar #+# #+# */
|
/* Created: 2020/12/22 12:08:22 by gbrochar #+# #+# */
|
||||||
/* Updated: 2020/12/25 14:54:37 by gbrochar ### ########.fr */
|
/* Updated: 2020/12/25 15:06:43 by gbrochar ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
@ -93,6 +93,9 @@ struct s_env
|
||||||
t_cam camera;
|
t_cam camera;
|
||||||
t_window window;
|
t_window window;
|
||||||
t_obj object;
|
t_obj object;
|
||||||
|
GLuint shader_program;
|
||||||
|
GLuint vertex_shader;
|
||||||
|
GLuint fragment_shader;
|
||||||
};
|
};
|
||||||
|
|
||||||
int parse(t_env *e, int argc, char **argv);
|
int parse(t_env *e, int argc, char **argv);
|
||||||
|
|
69
src/run.c
69
src/run.c
|
@ -6,7 +6,7 @@
|
||||||
/* By: gbrochar <gbrochar@student.42.fr> +#+ +:+ +#+ */
|
/* By: gbrochar <gbrochar@student.42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2020/12/23 19:38:05 by gbrochar #+# #+# */
|
/* Created: 2020/12/23 19:38:05 by gbrochar #+# #+# */
|
||||||
/* Updated: 2020/12/25 14:58:32 by gbrochar ### ########.fr */
|
/* Updated: 2020/12/25 15:22:27 by gbrochar ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
@ -81,11 +81,11 @@ static void CreateVertexBuffer(t_env *e)
|
||||||
glBufferData(GL_ELEMENT_ARRAY_BUFFER, e->object.indices.ptr * sizeof(int), e->object.indices.data, GL_STATIC_DRAW);
|
glBufferData(GL_ELEMENT_ARRAY_BUFFER, e->object.indices.ptr * sizeof(int), e->object.indices.data, GL_STATIC_DRAW);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void AddShader(GLuint ShaderProgram, const char* pShaderText, GLenum ShaderType)
|
static void AddShader(GLuint ShaderProgram, const char* pShaderText, GLenum ShaderType, GLuint *shader)
|
||||||
{
|
{
|
||||||
GLuint ShaderObj = glCreateShader(ShaderType);
|
*shader = glCreateShader(ShaderType);
|
||||||
|
|
||||||
if (ShaderObj == 0)
|
if (*shader == 0)
|
||||||
{
|
{
|
||||||
fprintf(stderr, "Error creating shader type %d\n", ShaderType);
|
fprintf(stderr, "Error creating shader type %d\n", ShaderType);
|
||||||
exit(0);
|
exit(0);
|
||||||
|
@ -95,26 +95,26 @@ static void AddShader(GLuint ShaderProgram, const char* pShaderText, GLenum Shad
|
||||||
p[0] = pShaderText;
|
p[0] = pShaderText;
|
||||||
GLint Lengths[1];
|
GLint Lengths[1];
|
||||||
Lengths[0] = strlen(pShaderText);
|
Lengths[0] = strlen(pShaderText);
|
||||||
glShaderSource(ShaderObj, 1, p, Lengths);
|
glShaderSource(*shader, 1, p, Lengths);
|
||||||
glCompileShader(ShaderObj);
|
glCompileShader(*shader);
|
||||||
GLint success;
|
GLint success;
|
||||||
glGetShaderiv(ShaderObj, GL_COMPILE_STATUS, &success);
|
glGetShaderiv(*shader, GL_COMPILE_STATUS, &success);
|
||||||
if (!success)
|
if (!success)
|
||||||
{
|
{
|
||||||
GLchar InfoLog[1024];
|
GLchar InfoLog[1024];
|
||||||
glGetShaderInfoLog(ShaderObj, 1024, NULL, InfoLog);
|
glGetShaderInfoLog(*shader, 1024, NULL, InfoLog);
|
||||||
fprintf(stderr, "Error compiling shader type %d: '%s'\n", ShaderType, InfoLog);
|
fprintf(stderr, "Error compiling shader type %d: '%s'\n", ShaderType, InfoLog);
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
glAttachShader(ShaderProgram, ShaderObj);
|
glAttachShader(ShaderProgram, *shader);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void CompileShaders()
|
static void CompileShaders(t_env *e)
|
||||||
{
|
{
|
||||||
GLuint ShaderProgram = glCreateProgram();
|
e->shader_program = glCreateProgram();
|
||||||
|
|
||||||
if (ShaderProgram == 0)
|
if (e->shader_program == 0)
|
||||||
{
|
{
|
||||||
fprintf(stderr, "Error creating shader program\n");
|
fprintf(stderr, "Error creating shader program\n");
|
||||||
exit(1);
|
exit(1);
|
||||||
|
@ -141,42 +141,55 @@ static void CompileShaders()
|
||||||
strcat(fs, buffer);
|
strcat(fs, buffer);
|
||||||
}
|
}
|
||||||
fclose(fp);
|
fclose(fp);
|
||||||
AddShader(ShaderProgram, vs, GL_VERTEX_SHADER);
|
AddShader(e->shader_program, vs, GL_VERTEX_SHADER, &(e->vertex_shader));
|
||||||
AddShader(ShaderProgram, fs, GL_FRAGMENT_SHADER);
|
AddShader(e->shader_program, fs, GL_FRAGMENT_SHADER, &(e->fragment_shader));
|
||||||
|
|
||||||
GLint Success = 0;
|
GLint Success = 0;
|
||||||
GLchar ErrorLog[1024] = { 0 };
|
GLchar ErrorLog[1024] = { 0 };
|
||||||
|
|
||||||
glLinkProgram(ShaderProgram);
|
glLinkProgram(e->shader_program);
|
||||||
glGetProgramiv(ShaderProgram, GL_LINK_STATUS, &Success);
|
glGetProgramiv(e->shader_program, GL_LINK_STATUS, &Success);
|
||||||
if (Success == 0)
|
if (Success == 0)
|
||||||
{
|
{
|
||||||
glGetProgramInfoLog(ShaderProgram, sizeof(ErrorLog), NULL, ErrorLog);
|
glGetProgramInfoLog(e->shader_program, sizeof(ErrorLog), NULL, ErrorLog);
|
||||||
fprintf(stderr, "Error linking shader program : '%s'\n", ErrorLog);
|
fprintf(stderr, "Error linking shader program : '%s'\n", ErrorLog);
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
glValidateProgram(ShaderProgram);
|
glValidateProgram(e->shader_program);
|
||||||
glGetProgramiv(ShaderProgram, GL_VALIDATE_STATUS, &Success);
|
glGetProgramiv(e->shader_program, GL_VALIDATE_STATUS, &Success);
|
||||||
if (!Success)
|
if (!Success)
|
||||||
{
|
{
|
||||||
glGetProgramInfoLog(ShaderProgram, sizeof(ErrorLog), NULL, ErrorLog);
|
glGetProgramInfoLog(e->shader_program, sizeof(ErrorLog), NULL, ErrorLog);
|
||||||
fprintf(stderr, "Invalid shader program '%s'\n", ErrorLog);
|
fprintf(stderr, "Invalid shader program '%s'\n", ErrorLog);
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
glUseProgram(ShaderProgram);
|
glUseProgram(e->shader_program);
|
||||||
|
|
||||||
// gScaleLocation = glGetUniformLocation(ShaderProgram, "gScale");
|
// gScaleLocation = glGetUniformLocation(e->shader_program, "gScale");
|
||||||
gScaleIntLocation = glGetUniformLocation(ShaderProgram, "time");
|
gScaleIntLocation = glGetUniformLocation(e->shader_program, "time");
|
||||||
gProjLocation = glGetUniformLocation(ShaderProgram, "proj");
|
gProjLocation = glGetUniformLocation(e->shader_program, "proj");
|
||||||
gViewLocation = glGetUniformLocation(ShaderProgram, "view");
|
gViewLocation = glGetUniformLocation(e->shader_program, "view");
|
||||||
gModelLocation = glGetUniformLocation(ShaderProgram, "model");
|
gModelLocation = glGetUniformLocation(e->shader_program, "model");
|
||||||
// assert(gScaleLocation != 0xFFFFFFFF);
|
// assert(gScaleLocation != 0xFFFFFFFF);
|
||||||
assert(gScaleIntLocation != 0xFFFFFFFF);
|
assert(gScaleIntLocation != 0xFFFFFFFF);
|
||||||
assert(gProjLocation != 0xFFFFFFFF);
|
assert(gProjLocation != 0xFFFFFFFF);
|
||||||
assert(gViewLocation != 0xFFFFFFFF);
|
assert(gViewLocation != 0xFFFFFFFF);
|
||||||
assert(gModelLocation != 0xFFFFFFFF);
|
assert(gModelLocation != 0xFFFFFFFF);
|
||||||
|
free(buffer);
|
||||||
|
free(vs);
|
||||||
|
free(fs);
|
||||||
|
}
|
||||||
|
|
||||||
|
void free_shaders(t_env *e)
|
||||||
|
{
|
||||||
|
glDetachShader(e->shader_program, e->vertex_shader);
|
||||||
|
glDetachShader(e->shader_program, e->fragment_shader);
|
||||||
|
glDeleteShader(e->vertex_shader);
|
||||||
|
glDeleteShader(e->fragment_shader);
|
||||||
|
glUseProgram(0);
|
||||||
|
glDeleteProgram(e->shader_program);
|
||||||
}
|
}
|
||||||
|
|
||||||
int run(t_env *e)
|
int run(t_env *e)
|
||||||
|
@ -209,7 +222,7 @@ int run(t_env *e)
|
||||||
printf("GL version: %s\n", glGetString(GL_VERSION));
|
printf("GL version: %s\n", glGetString(GL_VERSION));
|
||||||
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
|
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
|
||||||
CreateVertexBuffer(e);
|
CreateVertexBuffer(e);
|
||||||
CompileShaders();
|
CompileShaders(e);
|
||||||
/* Loop until the user closes the window */
|
/* Loop until the user closes the window */
|
||||||
while (!glfwWindowShouldClose(window))
|
while (!glfwWindowShouldClose(window))
|
||||||
{
|
{
|
||||||
|
@ -223,7 +236,7 @@ int run(t_env *e)
|
||||||
/* Poll for and process events */
|
/* Poll for and process events */
|
||||||
glfwPollEvents();
|
glfwPollEvents();
|
||||||
}
|
}
|
||||||
|
free_shaders(e);
|
||||||
glfwTerminate();
|
glfwTerminate();
|
||||||
return (SUCCESS);
|
return (SUCCESS);
|
||||||
}
|
}
|
Loading…
Reference in New Issue