[nostalgia/core/userland] Cleanup shader cleanup with unique_ptr-like GLobject type

This commit is contained in:
Gary Talent 2021-03-20 15:31:13 -05:00
parent 896a7a0a96
commit 47a43fecb1
3 changed files with 64 additions and 22 deletions

View File

@ -38,7 +38,7 @@ struct Background: public Bufferset {
};
struct GlImplData {
GLuint bgShader = 0;
Program bgShader;
int64_t prevFpsCheckTime = 0;
uint64_t draws = 0;
std::array<Background, 4> backgrounds;
@ -191,7 +191,7 @@ static void drawBackgrounds(GlImplData *id) {
ox::Error init(Context *ctx) {
const auto id = new GlImplData;
ctx->setRendererData(id);
oxReturnError(buildShaderProgram(bgvshad, bgfshad).get(&id->bgShader));
oxReturnError(buildShaderProgram(bgvshad, bgfshad).moveTo(&id->bgShader));
for (auto &bg : id->backgrounds) {
initBackgroundBufferset(ctx, id->bgShader, &bg);
}
@ -200,8 +200,6 @@ 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

@ -15,8 +15,11 @@
namespace nostalgia::core::renderer {
template struct GLobject<glDeleteProgram>;
template struct GLobject<glDeleteShader>;
[[nodiscard]]
static ox::Result<GLuint> buildShader(GLuint shaderType, const GLchar *src, const char *shaderName) {
static ox::Result<Shader> buildShader(GLuint shaderType, const GLchar *src, const char *shaderName) {
const auto shader = glCreateShader(shaderType);
glShaderSource(shader, 1, &src, nullptr);
glCompileShader(shader);
@ -29,26 +32,18 @@ static ox::Result<GLuint> buildShader(GLuint shaderType, const GLchar *src, cons
glDeleteShader(shader);
return OxError(1, "shader compile error");
}
return shader;
return Shader(shader);
}
[[nodiscard]]
ox::Result<GLuint> buildShaderProgram(const GLchar *vert, const GLchar *frag) {
ox::Result<GLuint> out;
ox::Result<Program> buildShaderProgram(const GLchar *vert, const GLchar *frag) {
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 {
out = fserr;
}
glDeleteShader(fs);
glDeleteShader(vs);
return out;
oxRequire(fs, buildShader(GL_FRAGMENT_SHADER, frag, "fshad"));
Program prgm(glCreateProgram());
glAttachShader(prgm, vs);
glAttachShader(prgm, fs);
glLinkProgram(prgm);
return ox::move(prgm);
}
void destroy(const Bufferset &bufferset) {

View File

@ -15,9 +15,58 @@
#endif
#include <ox/std/error.hpp>
#include <ox/std/trace.hpp>
namespace nostalgia::core::renderer {
template<auto del>
struct GLobject {
GLuint id = 0;
constexpr GLobject() = default;
explicit constexpr GLobject(GLuint id) {
this->id = id;
}
constexpr GLobject(GLobject &&o) {
id = o.id;
o.id = 0;
}
~GLobject() {
oxDebugf("Deleting {}", id);
del(id);
}
GLobject &operator=(GLobject &&o) {
id = o.id;
o.id = 0;
return *this;
}
constexpr GLuint release() noexcept {
auto out = id;
id = 0;
return out;
}
constexpr operator GLuint&() noexcept {
return id;
}
constexpr operator const GLuint&() const noexcept {
return id;
}
};
extern template struct GLobject<glDeleteProgram>;
extern template struct GLobject<glDeleteShader>;
using Shader = GLobject<glDeleteShader>;
using Program = GLobject<glDeleteProgram>;
struct Texture {
GLuint texId = 0;
@ -43,7 +92,7 @@ struct Bufferset {
};
[[nodiscard]]
ox::Result<GLuint> buildShaderProgram(const GLchar *vert, const GLchar *frag);
ox::Result<Program> buildShaderProgram(const GLchar *vert, const GLchar *frag);
void destroy(const Bufferset &bufferset);