[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) {
const auto id = ctx->rendererData<GlImplData>();
glDeleteProgram(id->bgShader);
id->bgShader = 0;
for (auto &bg : id->backgrounds) {
destroy(bg);
}

View File

@ -6,6 +6,8 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
#include <array>
#include <ox/std/assert.hpp>
#include <ox/std/trace.hpp>
@ -14,54 +16,39 @@
namespace nostalgia::core::renderer {
[[nodiscard]]
ox::Result<GLuint> buildShader(GLuint shaderType, const GLchar *src, const char *shaderName) {
auto shader = glCreateShader(shaderType);
static ox::Result<GLuint> 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<char, 1000> 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<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]]
ox::Result<GLuint> buildShaderProgram(const GLchar *vert, const GLchar *frag) {
GLuint prgm = 0;
oxRequire(vs, buildVertShader(vert, "vshad"));
if (!vs) {
glDeleteShader(vs);
ox::Result<GLuint> 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) {