[nostalgia/core/userland] Delete GL shaders when done

This commit is contained in:
Gary Talent 2021-03-18 19:43:08 -05:00
parent ff0a959793
commit 9860fec00e
2 changed files with 22 additions and 33 deletions

View File

@ -149,6 +149,8 @@ ox::Error init(Context *ctx) {
ox::Error shutdown(Context *ctx) { ox::Error shutdown(Context *ctx) {
const auto id = ctx->rendererData<GlImplData>(); const auto id = ctx->rendererData<GlImplData>();
glDeleteProgram(id->bgShader);
id->bgShader = 0;
for (auto &bg : id->backgrounds) { for (auto &bg : id->backgrounds) {
destroy(bg); destroy(bg);
} }

View File

@ -6,6 +6,8 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/. * file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/ */
#include <array>
#include <ox/std/assert.hpp> #include <ox/std/assert.hpp>
#include <ox/std/trace.hpp> #include <ox/std/trace.hpp>
@ -14,54 +16,39 @@
namespace nostalgia::core::renderer { namespace nostalgia::core::renderer {
[[nodiscard]] [[nodiscard]]
ox::Result<GLuint> buildShader(GLuint shaderType, const GLchar *src, const char *shaderName) { static ox::Result<GLuint> buildShader(GLuint shaderType, const GLchar *src, const char *shaderName) {
auto shader = glCreateShader(shaderType); const auto shader = glCreateShader(shaderType);
glShaderSource(shader, 1, &src, nullptr); glShaderSource(shader, 1, &src, nullptr);
glCompileShader(shader); glCompileShader(shader);
GLint status; GLint status;
glGetShaderiv(shader, GL_COMPILE_STATUS, &status); glGetShaderiv(shader, GL_COMPILE_STATUS, &status);
if (status != GL_TRUE) { if (status != GL_TRUE) {
static const auto errMsgSize = 1000; std::array<char, 1000> errMsg;
char errMsg[errMsgSize]; glGetShaderInfoLog(shader, errMsg.size(), nullptr, errMsg.data());
glGetShaderInfoLog(shader, errMsgSize, nullptr, errMsg); oxErrorf("shader compile error in {}: {}", shaderName, errMsg.data());
oxErrorf("shader compile error in {}: {}", shaderName, errMsg);
glDeleteShader(shader); glDeleteShader(shader);
return OxError(1, "shader compile error"); return OxError(1, "shader compile error");
} }
return shader; return shader;
} }
[[nodiscard]]
ox::Result<GLuint> buildVertShader(const GLchar *src, const char *shaderName) {
return buildShader(GL_VERTEX_SHADER, src, shaderName);
}
[[nodiscard]]
ox::Result<GLuint> buildFragShader(const GLchar *src, const char *shaderName) {
return buildShader(GL_FRAGMENT_SHADER, src, shaderName);
}
[[nodiscard]] [[nodiscard]]
ox::Result<GLuint> buildShaderProgram(const GLchar *vert, const GLchar *frag) { ox::Result<GLuint> buildShaderProgram(const GLchar *vert, const GLchar *frag) {
GLuint prgm = 0; ox::Result<GLuint> out;
oxRequire(vs, buildVertShader(vert, "vshad")); oxRequire(vs, buildShader(GL_VERTEX_SHADER, vert, "vshad"));
if (!vs) { auto [fs, fserr] = buildShader(GL_FRAGMENT_SHADER, frag, "fshad");
glDeleteShader(vs); if (!fserr) {
} else { auto prgm = glCreateProgram();
oxRequire(fs, buildFragShader(frag, "fshad"));
if (!fs) {
// cleanup shaders that were created
glDeleteShader(fs);
} else {
prgm = glCreateProgram();
if (prgm) {
glAttachShader(prgm, vs); glAttachShader(prgm, vs);
glAttachShader(prgm, fs); glAttachShader(prgm, fs);
glLinkProgram(prgm); glLinkProgram(prgm);
out = prgm;
} else {
out = fserr;
} }
} glDeleteShader(fs);
} glDeleteShader(vs);
return prgm; return out;
} }
void destroy(const Bufferset &bufferset) { void destroy(const Bufferset &bufferset) {