[nostalgia/core/userland] Add texture deletion

This commit is contained in:
Gary Talent 2021-03-20 15:47:32 -05:00
parent 37ec5eccd0
commit 6bdee3fc1c
3 changed files with 27 additions and 25 deletions

View File

@ -137,11 +137,11 @@ static void initBackgroundBufferset(Context *ctx, GLuint shader, Background *bg)
} }
static ox::Error loadTexture(Texture *tex, void *pixels) { static ox::Error loadTexture(Texture *tex, void *pixels) {
if (tex->texId == 0) { if (tex->id == 0) {
glGenTextures(1, &tex->texId); glGenTextures(1, &tex->id);
} }
glActiveTexture(GL_TEXTURE0); glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, tex->texId); glBindTexture(GL_TEXTURE_2D, tex->id);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, tex->width, tex->height, 0, GL_RGBA, GL_UNSIGNED_BYTE, pixels); glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, tex->width, tex->height, 0, GL_RGBA, GL_UNSIGNED_BYTE, pixels);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
@ -258,7 +258,7 @@ void draw(Context *ctx) {
void clearTileLayer(Context *ctx, int layer) { void clearTileLayer(Context *ctx, int layer) {
const auto id = ctx->rendererData<renderer::GlImplData>(); const auto id = ctx->rendererData<renderer::GlImplData>();
auto bg = id->backgrounds[static_cast<std::size_t>(layer)]; auto &bg = id->backgrounds[static_cast<std::size_t>(layer)];
initBackgroundBufferObjects(ctx, &bg); initBackgroundBufferObjects(ctx, &bg);
bg.updated = true; bg.updated = true;
} }

View File

@ -15,12 +15,17 @@
namespace nostalgia::core::renderer { namespace nostalgia::core::renderer {
void deleteTexture(GLuint t) {
glDeleteTextures(1, &t);
}
template struct GLobject<glDeleteProgram>; template struct GLobject<glDeleteProgram>;
template struct GLobject<glDeleteShader>; template struct GLobject<glDeleteShader>;
template struct GLobject<deleteTexture, TextureBase>;
[[nodiscard]] [[nodiscard]]
static ox::Result<Shader> 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); Shader shader(glCreateShader(shaderType));
glShaderSource(shader, 1, &src, nullptr); glShaderSource(shader, 1, &src, nullptr);
glCompileShader(shader); glCompileShader(shader);
GLint status; GLint status;
@ -29,10 +34,9 @@ static ox::Result<Shader> buildShader(GLuint shaderType, const GLchar *src, cons
std::array<char, 1000> errMsg; std::array<char, 1000> errMsg;
glGetShaderInfoLog(shader, errMsg.size(), nullptr, errMsg.data()); glGetShaderInfoLog(shader, errMsg.size(), nullptr, errMsg.data());
oxErrorf("shader compile error in {}: {}", shaderName, errMsg.data()); oxErrorf("shader compile error in {}: {}", shaderName, errMsg.data());
glDeleteShader(shader);
return OxError(1, "shader compile error"); return OxError(1, "shader compile error");
} }
return Shader(shader); return ox::move(shader);
} }
[[nodiscard]] [[nodiscard]]

View File

@ -19,8 +19,18 @@
namespace nostalgia::core::renderer { namespace nostalgia::core::renderer {
template<auto del> struct Empty {};
struct GLobject {
struct TextureBase {
GLsizei width = 0;
GLsizei height = 0;
};
template<auto del, typename Base = Empty>
struct GLobject: public Base {
GLuint id = 0; GLuint id = 0;
@ -61,26 +71,14 @@ struct GLobject {
}; };
void deleteTexture(GLuint t);
extern template struct GLobject<glDeleteProgram>; extern template struct GLobject<glDeleteProgram>;
extern template struct GLobject<glDeleteShader>; extern template struct GLobject<glDeleteShader>;
extern template struct GLobject<deleteTexture, TextureBase>;
using Shader = GLobject<glDeleteShader>; using Shader = GLobject<glDeleteShader>;
using Program = GLobject<glDeleteProgram>; using Program = GLobject<glDeleteProgram>;
using Texture = GLobject<deleteTexture, TextureBase>;
struct Texture {
GLuint texId = 0;
GLsizei width = 0;
GLsizei height = 0;
constexpr operator GLuint&() noexcept {
return texId;
}
constexpr operator const GLuint&() const noexcept {
return texId;
}
};
struct Bufferset { struct Bufferset {
GLuint vao = 0; GLuint vao = 0;