From 9860fec00ec9ea0ec12c21cdec79cadb42873bb2 Mon Sep 17 00:00:00 2001 From: Gary Talent Date: Thu, 18 Mar 2021 19:43:08 -0500 Subject: [PATCH] [nostalgia/core/userland] Delete GL shaders when done --- src/nostalgia/core/userland/gfx_opengl.cpp | 2 + src/nostalgia/core/userland/glutils.cpp | 53 ++++++++-------------- 2 files changed, 22 insertions(+), 33 deletions(-) diff --git a/src/nostalgia/core/userland/gfx_opengl.cpp b/src/nostalgia/core/userland/gfx_opengl.cpp index 738192be..2d00edc0 100644 --- a/src/nostalgia/core/userland/gfx_opengl.cpp +++ b/src/nostalgia/core/userland/gfx_opengl.cpp @@ -149,6 +149,8 @@ ox::Error init(Context *ctx) { ox::Error shutdown(Context *ctx) { const auto id = ctx->rendererData(); + glDeleteProgram(id->bgShader); + id->bgShader = 0; for (auto &bg : id->backgrounds) { destroy(bg); } diff --git a/src/nostalgia/core/userland/glutils.cpp b/src/nostalgia/core/userland/glutils.cpp index 4442d4fc..71635be0 100644 --- a/src/nostalgia/core/userland/glutils.cpp +++ b/src/nostalgia/core/userland/glutils.cpp @@ -6,6 +6,8 @@ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ +#include + #include #include @@ -14,54 +16,39 @@ namespace nostalgia::core::renderer { [[nodiscard]] -ox::Result buildShader(GLuint shaderType, const GLchar *src, const char *shaderName) { - auto shader = glCreateShader(shaderType); +static ox::Result buildShader(GLuint shaderType, const GLchar *src, const char *shaderName) { + const auto shader = glCreateShader(shaderType); glShaderSource(shader, 1, &src, nullptr); glCompileShader(shader); GLint status; glGetShaderiv(shader, GL_COMPILE_STATUS, &status); if (status != GL_TRUE) { - static const auto errMsgSize = 1000; - char errMsg[errMsgSize]; - glGetShaderInfoLog(shader, errMsgSize, nullptr, errMsg); - oxErrorf("shader compile error in {}: {}", shaderName, errMsg); + std::array errMsg; + glGetShaderInfoLog(shader, errMsg.size(), nullptr, errMsg.data()); + oxErrorf("shader compile error in {}: {}", shaderName, errMsg.data()); glDeleteShader(shader); return OxError(1, "shader compile error"); } return shader; } -[[nodiscard]] -ox::Result buildVertShader(const GLchar *src, const char *shaderName) { - return buildShader(GL_VERTEX_SHADER, src, shaderName); -} - -[[nodiscard]] -ox::Result buildFragShader(const GLchar *src, const char *shaderName) { - return buildShader(GL_FRAGMENT_SHADER, src, shaderName); -} - [[nodiscard]] ox::Result buildShaderProgram(const GLchar *vert, const GLchar *frag) { - GLuint prgm = 0; - oxRequire(vs, buildVertShader(vert, "vshad")); - if (!vs) { - glDeleteShader(vs); + ox::Result out; + oxRequire(vs, buildShader(GL_VERTEX_SHADER, vert, "vshad")); + auto [fs, fserr] = buildShader(GL_FRAGMENT_SHADER, frag, "fshad"); + if (!fserr) { + auto prgm = glCreateProgram(); + glAttachShader(prgm, vs); + glAttachShader(prgm, fs); + glLinkProgram(prgm); + out = prgm; } else { - oxRequire(fs, buildFragShader(frag, "fshad")); - if (!fs) { - // cleanup shaders that were created - glDeleteShader(fs); - } else { - prgm = glCreateProgram(); - if (prgm) { - glAttachShader(prgm, vs); - glAttachShader(prgm, fs); - glLinkProgram(prgm); - } - } + out = fserr; } - return prgm; + glDeleteShader(fs); + glDeleteShader(vs); + return out; } void destroy(const Bufferset &bufferset) {