Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improving handling game shaders #302

Merged
merged 6 commits into from Oct 29, 2017
Merged

Improving handling game shaders #302

merged 6 commits into from Oct 29, 2017

Conversation

ghost
Copy link

@ghost ghost commented Aug 7, 2017

Ok, I've edited pull request, because now happens much more. ;)

I've found some problems. First OpenGL programs have never been removed/closed. So I've added code doing it (in commit 75b188f), but now destructor have never been called, because memory allocated by OpenGLShaderPrograms are handled by raw pointers and never is freed. So I converted them unique pointers(commit 30a12ea).

I also improved the management of shaders in function compileProgram(commits 86d9122, 49f38e3, c858454). It should reduce memory usage.

Template I used:
glCreateProgram
glCreateShader
glShaderSource
glCompileShader
glAttachShader
glLinkProgram <- after this line opengl got everything it needs, you can free shader resources
glDetachShader
glDeleteShader

for{//render loop
glUseProgram
//...drawing operations
glUseProgram(0);
}

glDeleteProgram

Removes this type of memory leak:
==21845== 1,075 bytes in 1 blocks are possibly lost in loss record 2,921 of 3,413
==21845== at 0x4C2BE7F: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==21845== by 0xDE31CA2: ??? (in /usr/lib/xorg/modules/dri/radeonsi_dri.so)
==21845== by 0x577114: compileShader(unsigned int, char const*) (OpenGLRenderer.cpp:10)
==21845== by 0x5772F6: compileProgram(char const*, char const*) (OpenGLRenderer.cpp:47)
==21845== by 0x577BE0: OpenGLRenderer::createShader(std::__cxx11::basic_string<char, std::char_traits, std::allocator > const&, std::__cxx11::basic_string<char, std::char_traits, std::allocator > const&) (OpenGLRenderer.cpp:220)
==21845== by 0x57F7B0: WaterRenderer::WaterRenderer(GameRenderer*) (WaterRenderer.cpp:12)
==21845== by 0x5611D2: GameRenderer::GameRenderer(Logger*, GameData*) (GameRenderer.cpp:77)
==21845== by 0x46B9A4: RWGame::RWGame(Logger&, int, char**) (RWGame.cpp:35)
==21845== by 0x45B0F9: main (main.cpp:13)
References:
#239

@ghost ghost changed the title Open gl detach delete shaders OpenGL detach and delete shaders Aug 7, 2017
@ghost ghost changed the title OpenGL detach and delete shaders OpenGL detach and delete shaders + addition forgotten glDeleteProgram Aug 9, 2017
@MrSapps
Copy link

MrSapps commented Aug 18, 2017

Its a bigger change but I find its nice to create RAII wrappers for the oGL stuff.

@@ -212,6 +212,7 @@ GameRenderer::GameRenderer(Logger* log, GameData* _data)

GameRenderer::~GameRenderer() {
glDeleteFramebuffers(1, &framebufferName);
glDeleteProgram(ssRectProgram);
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or maybe should we convert ssRectProgram to ShaderProgram?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It should be doable. But isn't required for this PR.

@ghost ghost changed the title OpenGL detach and delete shaders + addition forgotten glDeleteProgram Improving handling game shaders Aug 31, 2017
@madebr madebr mentioned this pull request Sep 13, 2017
const std::string& frag) override;
void setProgramBlockBinding(ShaderProgram* p, const std::string& name,
void setProgramBlockBinding(std::unique_ptr<ShaderProgram>& p, const std::string& name,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it would make more sense for the setProgramBlockBinding, setUniform and useProgram to take either a ShaderProgram* or a ShaderProgram&, the interface shouldn't care if they're in a unique_ptr or not, and they shouldn't be null anyway.

@@ -212,6 +212,7 @@ GameRenderer::GameRenderer(Logger* log, GameData* _data)

GameRenderer::~GameRenderer() {
glDeleteFramebuffers(1, &framebufferName);
glDeleteProgram(ssRectProgram);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It should be doable. But isn't required for this PR.

Filip Gawin added 6 commits October 26, 2017 23:02
Each shader program should be taken care by glDeleteProgram.
To make it works/usefull, each shader's raw ptr will convertet to unique_ptr
in next commit.
Actually deleting isn't handled,
so this commit removes memory leak.
@danhedron danhedron merged commit b9d306a into rwengine:master Oct 29, 2017
@ghost ghost deleted the openGL_detach_delete_shaders branch October 30, 2017 17:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants