[nostalgia] Move much of the OpenGL code to glutils

This commit is contained in:
Gary Talent 2021-10-29 00:57:59 -05:00
parent 51f3c01c4e
commit 45d79e99e8
6 changed files with 99 additions and 66 deletions

View File

@ -10,6 +10,7 @@ endif()
target_link_libraries( target_link_libraries(
NostalgiaCore PUBLIC NostalgiaCore PUBLIC
OxClaw
OxFS OxFS
) )

View File

@ -8,10 +8,19 @@
#pragma once #pragma once
#include <ox/claw/claw.hpp>
#include <ox/fs/fs.hpp> #include <ox/fs/fs.hpp>
#include "context.hpp"
namespace nostalgia::core { namespace nostalgia::core {
template<typename T>
ox::Result<T> readObj(Context *ctx, const ox::FileAddress &file) noexcept {
oxRequire(buff, ctx->rom->read(file));
return ox::readClaw<T>(buff);
}
ox::Result<ox::UniquePtr<ox::FileSystem>> loadRomFs(const char *path) noexcept; ox::Result<ox::UniquePtr<ox::FileSystem>> loadRomFs(const char *path) noexcept;
ox::Result<char*> loadRom(const char *path = "") noexcept; ox::Result<char*> loadRom(const char *path = "") noexcept;

View File

@ -6,19 +6,13 @@
* 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 <ox/claw/claw.hpp>
#include <nostalgia/core/gfx.hpp> #include <nostalgia/core/gfx.hpp>
#include <nostalgia/core/media.hpp>
#include "gfx.hpp" #include "gfx.hpp"
namespace nostalgia::core { namespace nostalgia::core {
template<typename T>
static ox::Result<T> readObj(Context *ctx, const ox::FileAddress &file) noexcept {
oxRequire(buff, ctx->rom->read(file));
return ox::readClaw<T>(buff);
}
ox::Error initConsole(Context *ctx) noexcept { ox::Error initConsole(Context *ctx) noexcept {
constexpr auto TilesheetAddr = "/TileSheets/Charset.ng"; constexpr auto TilesheetAddr = "/TileSheets/Charset.ng";
constexpr auto PaletteAddr = "/Palettes/Charset.npal"; constexpr auto PaletteAddr = "/Palettes/Charset.npal";

View File

@ -33,15 +33,14 @@ constexpr auto BgVertexVboRowLength = 4;
constexpr auto BgVertexVboLength = BgVertexVboRows * BgVertexVboRowLength; constexpr auto BgVertexVboLength = BgVertexVboRows * BgVertexVboRowLength;
constexpr auto BgVertexEboLength = 6; constexpr auto BgVertexEboLength = 6;
struct Background { struct Background: public glutils::BufferSet {
glutils::GLVertexArray vao;
glutils::GLBuffer vbo;
glutils::GLBuffer ebo;
glutils::GLTexture tex;
bool enabled = false; bool enabled = false;
bool updated = false; bool updated = false;
std::array<float, TileCount * BgVertexVboLength> bgVertices = {};
std::array<GLuint, TileCount * BgVertexEboLength> bgElements = {}; inline Background() noexcept {
vertices.resize(TileCount * BgVertexVboLength);
elements.resize(TileCount * BgVertexEboLength);
}
}; };
struct GlImplData { struct GlImplData {
@ -54,11 +53,11 @@ struct GlImplData {
constexpr const GLchar *bgvshad = R"( constexpr const GLchar *bgvshad = R"(
{} {}
in vec2 vTexCoord; in vec2 vTexCoord;
in vec2 position; in vec2 vPosition;
out vec2 fTexCoord; out vec2 fTexCoord;
uniform float vTileHeight; uniform float vTileHeight;
void main() { void main() {
gl_Position = vec4(position, 0.0, 1.0); gl_Position = vec4(vPosition, 0.0, 1.0);
fTexCoord = vTexCoord * vec2(1, vTileHeight); fTexCoord = vTexCoord * vec2(1, vTileHeight);
})"; })";
@ -77,7 +76,8 @@ static constexpr auto bgVertexRow(unsigned x, unsigned y) noexcept {
return y * TileRows + x; return y * TileRows + x;
} }
static void setTileBufferObject(Context *ctx, unsigned vi, float x, float y, int textureRow, float *vbo, GLuint *ebo) { static void
setTileBufferObject(Context *ctx, unsigned vi, float x, float y, int textureRow, float *vbo, GLuint *ebo) noexcept {
// don't worry, this memcpy gets optimized to something much more ideal // don't worry, this memcpy gets optimized to something much more ideal
const auto [sw, sh] = getScreenSize(ctx); const auto [sw, sh] = getScreenSize(ctx);
constexpr float ymod = 2.0f / 20.0f; constexpr float ymod = 2.0f / 20.0f;
@ -100,59 +100,35 @@ static void setTileBufferObject(Context *ctx, unsigned vi, float x, float y, int
memcpy(ebo, elms, sizeof(elms)); memcpy(ebo, elms, sizeof(elms));
} }
static void sendVbo(const Background &bg) noexcept { static void initBackgroundBufferObjects(Context *ctx, glutils::BufferSet *bg) noexcept {
// vbo
glBindBuffer(GL_ARRAY_BUFFER, bg.vbo);
glBufferData(GL_ARRAY_BUFFER, sizeof(bg.bgVertices), &bg.bgVertices, GL_DYNAMIC_DRAW);
}
static void sendEbo(const Background &bg) noexcept {
// ebo
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, bg.ebo);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(bg.bgElements), &bg.bgElements, GL_STATIC_DRAW);
}
static void initBackgroundBufferObjects(Context *ctx, Background *bg) noexcept {
for (auto x = 0u; x < TileColumns; ++x) { for (auto x = 0u; x < TileColumns; ++x) {
for (auto y = 0u; y < TileRows; ++y) { for (auto y = 0u; y < TileRows; ++y) {
const auto i = bgVertexRow(x, y); const auto i = bgVertexRow(x, y);
auto vbo = &bg->bgVertices[i * BgVertexVboLength]; auto vbo = &bg->vertices[i * BgVertexVboLength];
auto ebo = &bg->bgElements[i * BgVertexEboLength]; auto ebo = &bg->elements[i * BgVertexEboLength];
setTileBufferObject(ctx, i * BgVertexVboRows, static_cast<float>(x), static_cast<float>(y), 0, vbo, ebo); setTileBufferObject(ctx, i * BgVertexVboRows, static_cast<float>(x), static_cast<float>(y), 0, vbo, ebo);
} }
} }
} }
static glutils::GLVertexArray genVertexArrayObject() noexcept { static void initBackgroundBufferset(Context *ctx, GLuint shader, glutils::BufferSet *bg) noexcept {
GLuint vao = 0;
glGenVertexArrays(1, &vao);
return glutils::GLVertexArray(vao);
}
static glutils::GLBuffer genBuffer() noexcept {
GLuint buff = 0;
glGenBuffers(1, &buff);
return glutils::GLBuffer(buff);
}
static void initBackgroundBufferset(Context *ctx, GLuint shader, Background *bg) noexcept {
// vao // vao
bg->vao = genVertexArrayObject(); bg->vao = glutils::generateVertexArrayObject();
glBindVertexArray(bg->vao); glBindVertexArray(bg->vao);
// vbo & ebo // vbo & ebo
bg->vbo = genBuffer(); bg->vbo = glutils::generateBuffer();
bg->ebo = genBuffer(); bg->ebo = glutils::generateBuffer();
initBackgroundBufferObjects(ctx, bg); initBackgroundBufferObjects(ctx, bg);
sendVbo(*bg); glutils::sendVbo(*bg);
sendEbo(*bg); glutils::sendEbo(*bg);
// vbo layout // vbo layout
auto posAttr = static_cast<GLuint>(glGetAttribLocation(shader, "position")); auto posAttr = static_cast<GLuint>(glGetAttribLocation(shader, "vPosition"));
glEnableVertexAttribArray(posAttr); glEnableVertexAttribArray(posAttr);
glVertexAttribPointer(posAttr, 2, GL_FLOAT, GL_FALSE, BgVertexVboRowLength * sizeof(float), nullptr); glVertexAttribPointer(posAttr, 2, GL_FLOAT, GL_FALSE, BgVertexVboRowLength * sizeof(float), nullptr);
auto texCoordAttr = static_cast<GLuint>(glGetAttribLocation(shader, "vTexCoord")); auto texCoordAttr = static_cast<GLuint>(glGetAttribLocation(shader, "vTexCoord"));
glEnableVertexAttribArray(texCoordAttr); glEnableVertexAttribArray(texCoordAttr);
glVertexAttribPointer(texCoordAttr, 2, GL_FLOAT, GL_FALSE, BgVertexVboRowLength * sizeof(float), glVertexAttribPointer(texCoordAttr, 2, GL_FLOAT, GL_FALSE, BgVertexVboRowLength * sizeof(float),
ox::bit_cast<void*>(2 * sizeof(float))); std::bit_cast<void*>(2 * sizeof(float)));
} }
static glutils::GLTexture loadTexture(GLsizei w, GLsizei h, void *pixels) noexcept { static glutils::GLTexture loadTexture(GLsizei w, GLsizei h, void *pixels) noexcept {
@ -192,10 +168,10 @@ static void drawBackground(Background *bg) noexcept {
glBindVertexArray(bg->vao); glBindVertexArray(bg->vao);
if (bg->updated) { if (bg->updated) {
bg->updated = false; bg->updated = false;
renderer::sendVbo(*bg); glutils::sendVbo(*bg);
} }
glBindTexture(GL_TEXTURE_2D, bg->tex); glBindTexture(GL_TEXTURE_2D, bg->tex);
glDrawElements(GL_TRIANGLES, static_cast<GLsizei>(bg->bgElements.size()), GL_UNSIGNED_INT, nullptr); glDrawElements(GL_TRIANGLES, static_cast<GLsizei>(bg->elements.size()), GL_UNSIGNED_INT, nullptr);
} }
} }
@ -210,16 +186,15 @@ static void drawBackgrounds(GlImplData *id) noexcept {
} }
ox::Error init(Context *ctx) noexcept { ox::Error init(Context *ctx) noexcept {
constexpr auto GlslVersion = "#version 150";
const auto id = new GlImplData; const auto id = new GlImplData;
ctx->setRendererData(id); ctx->setRendererData(id);
const auto vshad = ox::sfmt(bgvshad, GlslVersion); const auto vshad = ox::sfmt(bgvshad, glutils::GlslVersion);
const auto fshad = ox::sfmt(bgfshad, GlslVersion); const auto fshad = ox::sfmt(bgfshad, glutils::GlslVersion);
oxReturnError(glutils::buildShaderProgram(vshad.c_str(), fshad.c_str()).moveTo(&id->bgShader)); oxReturnError(glutils::buildShaderProgram(vshad.c_str(), fshad.c_str()).moveTo(&id->bgShader));
for (auto &bg : id->backgrounds) { for (auto &bg : id->backgrounds) {
initBackgroundBufferset(ctx, id->bgShader, &bg); initBackgroundBufferset(ctx, id->bgShader, &bg);
} }
ImGui_ImplOpenGL3_Init(GlslVersion); ImGui_ImplOpenGL3_Init(glutils::GlslVersion);
return OxError(0); return OxError(0);
} }
@ -280,8 +255,6 @@ void draw(Context *ctx) noexcept {
glClear(GL_COLOR_BUFFER_BIT); glClear(GL_COLOR_BUFFER_BIT);
// render // render
renderer::drawBackgrounds(id); renderer::drawBackgrounds(id);
//bool showDemo = true;
//ImGui::ShowDemoWindow(&showDemo);
for (const auto cd : ctx->drawers) { for (const auto cd : ctx->drawers) {
cd->draw(ctx); cd->draw(ctx);
} }
@ -318,9 +291,10 @@ void setTile(Context *ctx, int layer, int column, int row, uint8_t tile) noexcep
const auto x = static_cast<unsigned>(column); const auto x = static_cast<unsigned>(column);
const auto i = renderer::bgVertexRow(x, y); const auto i = renderer::bgVertexRow(x, y);
auto &bg = id->backgrounds[z]; auto &bg = id->backgrounds[z];
auto vbo = &bg.bgVertices[i * renderer::BgVertexVboLength]; auto vbo = &bg.vertices[i * renderer::BgVertexVboLength];
auto ebo = &bg.bgElements[i * renderer::BgVertexEboLength]; auto ebo = &bg.elements[i * renderer::BgVertexEboLength];
renderer::setTileBufferObject(ctx, i * renderer::BgVertexVboRows, static_cast<float>(x), static_cast<float>(y), tile, vbo, ebo); renderer::setTileBufferObject(ctx, i * renderer::BgVertexVboRows,
static_cast<float>(x), static_cast<float>(y), tile, vbo, ebo);
bg.updated = true; bg.updated = true;
} }

View File

@ -67,8 +67,26 @@ ox::Result<GLProgram> buildShaderProgram(const GLchar *vert, const GLchar *frag)
return prgm; return prgm;
} }
ox::Result<GLProgram> buildShaderProgram(const ox::String &vert, const ox::String &frag) noexcept {
return buildShaderProgram(vert.c_str(), frag.c_str());
}
GLVertexArray generateVertexArrayObject() noexcept {
GLVertexArray vao;
glGenVertexArrays(1, &vao.id);
return vao;
}
GLBuffer generateBuffer() noexcept {
GLBuffer buff;
glGenBuffers(1, &buff.id);
return buff;
}
FrameBuffer generateFrameBuffer(int width, int height) noexcept { FrameBuffer generateFrameBuffer(int width, int height) noexcept {
FrameBuffer fb; FrameBuffer fb;
fb.width = width;
fb.height = height;
glGenFramebuffers(1, &fb.fbo.id); glGenFramebuffers(1, &fb.fbo.id);
glBindFramebuffer(GL_FRAMEBUFFER, fb); glBindFramebuffer(GL_FRAMEBUFFER, fb);
// color texture // color texture
@ -82,12 +100,25 @@ FrameBuffer generateFrameBuffer(int width, int height) noexcept {
glGenRenderbuffers(1, &fb.depth.id); glGenRenderbuffers(1, &fb.depth.id);
glBindRenderbuffer(GL_RENDERBUFFER, fb.depth); glBindRenderbuffer(GL_RENDERBUFFER, fb.depth);
glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH24_STENCIL8, width, height); glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH24_STENCIL8, width, height);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT, GL_RENDERBUFFER, fb.depth, 0); glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT, GL_RENDERBUFFER, fb.depth);
// verify FBO // verify FBO
oxAssert(glCheckFramebufferStatus(GL_FRAMEBUFFER) == GL_FRAMEBUFFER_COMPLETE, "Frame Buffer is incomplete"); oxAssert(glCheckFramebufferStatus(GL_FRAMEBUFFER) == GL_FRAMEBUFFER_COMPLETE, "Frame Buffer is incomplete");
// restore primary FB // restore primary FB
glBindFramebuffer(GL_FRAMEBUFFER, 0); glBindFramebuffer(GL_FRAMEBUFFER, 0);
oxAssert(glGetError() == 0, "Framebuffer creation failed");
return fb; return fb;
} }
void sendVbo(const BufferSet &bg) noexcept {
const auto bufferSize = static_cast<GLsizeiptr>(sizeof(decltype(bg.vertices)::value_type) * bg.vertices.size());
glBindBuffer(GL_ARRAY_BUFFER, bg.vbo);
glBufferData(GL_ARRAY_BUFFER, bufferSize, bg.vertices.data(), GL_DYNAMIC_DRAW);
}
void sendEbo(const BufferSet &bg) noexcept {
const auto bufferSize = static_cast<GLsizeiptr>(sizeof(decltype(bg.elements)::value_type) * bg.elements.size());
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, bg.ebo);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, bufferSize, bg.elements.data(), GL_STATIC_DRAW);
}
} }

View File

@ -9,6 +9,7 @@
#pragma once #pragma once
#include <ox/std/defines.hpp> #include <ox/std/defines.hpp>
#include <ox/std/string.hpp>
#define GL_GLEXT_PROTOTYPES 1 #define GL_GLEXT_PROTOTYPES 1
#ifdef OX_OS_Darwin #ifdef OX_OS_Darwin
@ -21,9 +22,12 @@
#endif #endif
#include <ox/std/error.hpp> #include <ox/std/error.hpp>
#include <ox/std/vector.hpp>
namespace nostalgia::glutils { namespace nostalgia::glutils {
constexpr auto GlslVersion = "#version 150";
struct Empty {}; struct Empty {};
struct TextureBase { struct TextureBase {
@ -125,6 +129,8 @@ using GLVertexArray = GLObject<deleteVertexArray>;
* and not its dependencies. * and not its dependencies.
*/ */
struct FrameBuffer { struct FrameBuffer {
int width = 0;
int height = 0;
GLFrameBuffer fbo; GLFrameBuffer fbo;
GLTexture color; GLTexture color;
GLRenderBuffer depth; GLRenderBuffer depth;
@ -139,10 +145,28 @@ struct FrameBuffer {
}; };
[[nodiscard]]
ox::Result<GLProgram> buildShaderProgram(const GLchar *vert, const GLchar *frag) noexcept; ox::Result<GLProgram> buildShaderProgram(const GLchar *vert, const GLchar *frag) noexcept;
ox::Result<GLProgram> buildShaderProgram(const ox::String &vert, const ox::String &frag) noexcept;
glutils::GLVertexArray generateVertexArrayObject() noexcept;
glutils::GLBuffer generateBuffer() noexcept;
[[nodiscard]] [[nodiscard]]
FrameBuffer generateFrameBuffer(int width, int height) noexcept; FrameBuffer generateFrameBuffer(int width, int height) noexcept;
struct BufferSet {
glutils::GLVertexArray vao;
glutils::GLBuffer vbo;
glutils::GLBuffer ebo;
glutils::GLTexture tex;
ox::Vector<float> vertices;
ox::Vector<GLuint> elements;
};
void sendVbo(const BufferSet &bg) noexcept;
void sendEbo(const BufferSet &bg) noexcept;
} }