From aea43306a40e895c9c2263c66bbfdd2888c11c49 Mon Sep 17 00:00:00 2001 From: Gary Talent Date: Tue, 23 Mar 2021 00:02:17 -0500 Subject: [PATCH] [nostalgia/core/gl] Cleanup VBO, EBO, and VAO initialization --- src/nostalgia/core/userland/gfx_opengl.cpp | 55 ++++++++++++++-------- src/nostalgia/core/userland/glutils.cpp | 4 +- src/nostalgia/core/userland/glutils.hpp | 11 +---- 3 files changed, 40 insertions(+), 30 deletions(-) diff --git a/src/nostalgia/core/userland/gfx_opengl.cpp b/src/nostalgia/core/userland/gfx_opengl.cpp index ae3eee96..cfba5092 100644 --- a/src/nostalgia/core/userland/gfx_opengl.cpp +++ b/src/nostalgia/core/userland/gfx_opengl.cpp @@ -30,11 +30,15 @@ constexpr auto BgVertexVboRowLength = 4; constexpr auto BgVertexVboLength = BgVertexVboRows * BgVertexVboRowLength; constexpr auto BgVertexEboLength = 6; -struct Background: public Bufferset { +struct Background { + VertexArray vao; + Buffer vbo; + Buffer ebo; + Texture tex; bool enabled = false; bool updated = false; std::array bgVertices; - std::array bgEbos; + std::array bgElements; }; struct GlImplData { @@ -102,7 +106,7 @@ static void sendVbo(const Background &bg) noexcept { static void sendEbo(const Background &bg) noexcept { // ebo glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, bg.ebo); - glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(bg.bgEbos), &bg.bgEbos, GL_STATIC_DRAW); + glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(bg.bgElements), &bg.bgElements, GL_STATIC_DRAW); } static void initBackgroundBufferObjects(Context *ctx, Background *bg) noexcept { @@ -110,19 +114,31 @@ static void initBackgroundBufferObjects(Context *ctx, Background *bg) noexcept { for (auto y = 0u; y < TileRows; ++y) { const auto i = bgVertexRow(x, y); auto vbo = &bg->bgVertices[i * BgVertexVboLength]; - auto ebo = &bg->bgEbos[i * BgVertexEboLength]; + auto ebo = &bg->bgElements[i * BgVertexEboLength]; setTileBufferObject(ctx, i * BgVertexVboRows, x, y, 0, vbo, ebo); } } } +static VertexArray genVertexArrayObject() noexcept { + GLuint vao = 0; + glGenVertexArrays(1, &vao); + return VertexArray(vao); +} + +static Buffer genBuffer() noexcept { + GLuint buff = 0; + glGenBuffers(1, &buff); + return Buffer(buff); +} + static void initBackgroundBufferset(Context *ctx, GLuint shader, Background *bg) { // vao - glGenVertexArrays(1, &bg->vao.id); + bg->vao = genVertexArrayObject(); glBindVertexArray(bg->vao); // vbo & ebo - glGenBuffers(1, &bg->vbo.id); - glGenBuffers(1, &bg->ebo.id); + bg->vbo = genBuffer(); + bg->ebo = genBuffer(); initBackgroundBufferObjects(ctx, bg); sendVbo(*bg); sendEbo(*bg); @@ -136,18 +152,20 @@ static void initBackgroundBufferset(Context *ctx, GLuint shader, Background *bg) ox::bit_cast(2 * sizeof(float))); } -static ox::Error loadTexture(Texture *tex, void *pixels) { - if (tex->id == 0) { - glGenTextures(1, &tex->id); - } +static Texture loadTexture(GLsizei w, GLsizei h, void *pixels) { + GLuint texId = 0; + glGenTextures(1, &texId); + Texture tex(texId); + tex.width = w; + tex.height = h; glActiveTexture(GL_TEXTURE0); - glBindTexture(GL_TEXTURE_2D, tex->id); - glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, tex->width, tex->height, 0, GL_RGBA, GL_UNSIGNED_BYTE, pixels); + glBindTexture(GL_TEXTURE_2D, tex.id); + 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_MAG_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); - return OxError(0); + return ox::move(tex); } static void tickFps(GlImplData *id) { @@ -174,7 +192,7 @@ static void drawBackground(Background *bg) { renderer::sendVbo(*bg); } glBindTexture(GL_TEXTURE_2D, bg->tex); - glDrawElements(GL_TRIANGLES, bg->bgEbos.size(), GL_UNSIGNED_INT, 0); + glDrawElements(GL_TRIANGLES, bg->bgElements.size(), GL_UNSIGNED_INT, 0); } } @@ -209,9 +227,8 @@ ox::Error loadBgTexture(Context *ctx, int section, void *pixels, int w, int h) { oxTracef("nostalgia::core::gfx::gl", "loadBgTexture: { section: {}, w: {}, h: {} }", section, w, h); const auto &id = ctx->rendererData(); auto &tex = id->backgrounds[static_cast(section)].tex; - tex.width = w; - tex.height = h; - return loadTexture(&tex, pixels); + tex = loadTexture(w, h, pixels); + return OxError(0); } } @@ -281,7 +298,7 @@ void setTile(Context *ctx, int layer, int column, int row, uint8_t tile) { const auto i = renderer::bgVertexRow(x, y); auto &bg = id->backgrounds[z]; auto vbo = &bg.bgVertices[i * renderer::BgVertexVboLength]; - auto ebo = &bg.bgEbos[i * renderer::BgVertexEboLength]; + auto ebo = &bg.bgElements[i * renderer::BgVertexEboLength]; renderer::setTileBufferObject(ctx, i * renderer::BgVertexVboRows, x, y, tile, vbo, ebo); bg.updated = true; } diff --git a/src/nostalgia/core/userland/glutils.cpp b/src/nostalgia/core/userland/glutils.cpp index 3c3b8ba7..c9be7739 100644 --- a/src/nostalgia/core/userland/glutils.cpp +++ b/src/nostalgia/core/userland/glutils.cpp @@ -34,7 +34,7 @@ template struct GLobject; template struct GLobject; [[nodiscard]] -static ox::Result buildShader(GLuint shaderType, const GLchar *src, const char *shaderName) { +static ox::Result buildShader(GLuint shaderType, const GLchar *src, const char *shaderName) noexcept { Shader shader(glCreateShader(shaderType)); glShaderSource(shader, 1, &src, nullptr); glCompileShader(shader); @@ -50,7 +50,7 @@ static ox::Result buildShader(GLuint shaderType, const GLchar *src, cons } [[nodiscard]] -ox::Result buildShaderProgram(const GLchar *vert, const GLchar *frag) { +ox::Result buildShaderProgram(const GLchar *vert, const GLchar *frag) noexcept { oxRequire(vs, buildShader(GL_VERTEX_SHADER, vert, "vshad")); oxRequire(fs, buildShader(GL_FRAGMENT_SHADER, frag, "fshad")); Program prgm(glCreateProgram()); diff --git a/src/nostalgia/core/userland/glutils.hpp b/src/nostalgia/core/userland/glutils.hpp index c8706aac..3412c618 100644 --- a/src/nostalgia/core/userland/glutils.hpp +++ b/src/nostalgia/core/userland/glutils.hpp @@ -57,7 +57,7 @@ struct GLobject: public Base { this->id = id; } - constexpr GLobject(GLobject &&o) { + constexpr GLobject(GLobject &&o): Base(ox::move(o)) { id = o.id; o.id = 0; } @@ -106,14 +106,7 @@ using Program = GLobject; using Texture = GLobject; using VertexArray = GLobject; -struct Bufferset { - VertexArray vao; - Buffer vbo; - Buffer ebo; - Texture tex; -}; - [[nodiscard]] -ox::Result buildShaderProgram(const GLchar *vert, const GLchar *frag); +ox::Result buildShaderProgram(const GLchar *vert, const GLchar *frag) noexcept; }