[nostalgia] Move glutils into separate library

This commit is contained in:
2021-05-15 02:27:13 -05:00
parent 282bdc0fd7
commit 63e46d7bb4
7 changed files with 48 additions and 24 deletions
+1
View File
@@ -9,6 +9,7 @@ find_package(glfw3 REQUIRED)
target_link_libraries(
NostalgiaCore-GLFW PUBLIC
glfw::glfw
NostalgiaGlUtils
NostalgiaCore-Userspace
)
+1 -8
View File
@@ -2,7 +2,6 @@ add_library(
NostalgiaCore-Userspace OBJECT
gfx.cpp
gfx_opengl.cpp
glutils.cpp
media.cpp
)
@@ -10,19 +9,13 @@ if(NOT MSVC)
target_compile_options(NostalgiaCore-Userspace PRIVATE -Wsign-conversion)
endif()
if(APPLE)
find_package(OpenGL REQUIRED)
else()
set(OPENGL_gl_LIBRARY GL)
endif()
target_link_libraries(
NostalgiaCore-Userspace PUBLIC
OxClaw
OxFS
OxStd
NostalgiaCore
${OPENGL_gl_LIBRARY}
NostalgiaGlUtils
)
install(
+14 -14
View File
@@ -11,11 +11,11 @@
#include <ox/std/bit.hpp>
#include <ox/std/fmt.hpp>
#include <nostalgia/glutils/glutils.hpp>
#include <nostalgia/core/config.hpp>
#include <nostalgia/core/gfx.hpp>
#include "glutils.hpp"
namespace nostalgia::core {
@@ -30,10 +30,10 @@ constexpr auto BgVertexVboLength = BgVertexVboRows * BgVertexVboRowLength;
constexpr auto BgVertexEboLength = 6;
struct Background {
GLVertexArray vao;
GLBuffer vbo;
GLBuffer ebo;
GLTexture tex;
glutils::GLVertexArray vao;
glutils::GLBuffer vbo;
glutils::GLBuffer ebo;
glutils::GLTexture tex;
bool enabled = false;
bool updated = false;
std::array<float, TileCount * BgVertexVboLength> bgVertices = {};
@@ -41,7 +41,7 @@ struct Background {
};
struct GlImplData {
GLProgram bgShader;
glutils::GLProgram bgShader;
int64_t prevFpsCheckTime = 0;
uint64_t draws = 0;
std::array<Background, 4> backgrounds;
@@ -119,16 +119,16 @@ static void initBackgroundBufferObjects(Context *ctx, Background *bg) noexcept {
}
}
static GLVertexArray genVertexArrayObject() noexcept {
static glutils::GLVertexArray genVertexArrayObject() noexcept {
GLuint vao = 0;
glGenVertexArrays(1, &vao);
return GLVertexArray(vao);
return glutils::GLVertexArray(vao);
}
static GLBuffer genBuffer() noexcept {
static glutils::GLBuffer genBuffer() noexcept {
GLuint buff = 0;
glGenBuffers(1, &buff);
return GLBuffer(buff);
return glutils::GLBuffer(buff);
}
static void initBackgroundBufferset(Context *ctx, GLuint shader, Background *bg) noexcept {
@@ -151,10 +151,10 @@ static void initBackgroundBufferset(Context *ctx, GLuint shader, Background *bg)
ox::bit_cast<void*>(2 * sizeof(float)));
}
static GLTexture loadTexture(GLsizei w, GLsizei h, void *pixels) noexcept {
static glutils::GLTexture loadTexture(GLsizei w, GLsizei h, void *pixels) noexcept {
GLuint texId = 0;
glGenTextures(1, &texId);
GLTexture tex(texId);
glutils::GLTexture tex(texId);
tex.width = w;
tex.height = h;
glActiveTexture(GL_TEXTURE0);
@@ -208,7 +208,7 @@ static void drawBackgrounds(GlImplData *id) noexcept {
ox::Error init(Context *ctx) noexcept {
const auto id = new GlImplData;
ctx->setRendererData(id);
oxReturnError(buildShaderProgram(bgvshad, bgfshad).moveTo(&id->bgShader));
oxReturnError(glutils::buildShaderProgram(bgvshad, bgfshad).moveTo(&id->bgShader));
for (auto &bg : id->backgrounds) {
initBackgroundBufferset(ctx, id->bgShader, &bg);
}
-61
View File
@@ -1,61 +0,0 @@
/*
* Copyright 2016 - 2021 gary@drinkingtea.net
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
#include <ox/std/bstring.hpp>
#include <ox/std/trace.hpp>
#include "glutils.hpp"
namespace nostalgia::core::renderer {
void deleteBuffer(GLuint b) noexcept {
glDeleteBuffers(1, &b);
}
void deleteTexture(GLuint t) noexcept {
glDeleteTextures(1, &t);
}
void deleteVertexArray(GLuint v) noexcept {
glDeleteVertexArrays(1, &v);
}
template struct GLObject<deleteBuffer>;
template struct GLObject<deleteTexture, TextureBase>;
template struct GLObject<deleteVertexArray>;
template struct GLObject<glDeleteProgram>;
template struct GLObject<glDeleteShader>;
[[nodiscard]]
static ox::Result<GLShader> buildShader(GLuint shaderType, const GLchar *src, const char *shaderName) noexcept {
GLShader shader(glCreateShader(shaderType));
glShaderSource(shader, 1, &src, nullptr);
glCompileShader(shader);
GLint status;
glGetShaderiv(shader, GL_COMPILE_STATUS, &status);
if (status != GL_TRUE) {
ox::BString<1000> errMsg;
glGetShaderInfoLog(shader, errMsg.cap(), nullptr, errMsg.data());
oxErrorf("shader compile error in {}: {}", shaderName, errMsg);
return OxError(1, "shader compile error");
}
return ox::move(shader);
}
[[nodiscard]]
ox::Result<GLProgram> buildShaderProgram(const GLchar *vert, const GLchar *frag) noexcept {
oxRequire(vs, buildShader(GL_VERTEX_SHADER, vert, "vshad"));
oxRequire(fs, buildShader(GL_FRAGMENT_SHADER, frag, "fshad"));
GLProgram prgm(glCreateProgram());
glAttachShader(prgm, vs);
glAttachShader(prgm, fs);
glLinkProgram(prgm);
return ox::move(prgm);
}
}
-111
View File
@@ -1,111 +0,0 @@
/*
* Copyright 2016 - 2021 gary@drinkingtea.net
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
#define GL_GLEXT_PROTOTYPES 1
#ifdef OX_OS_Darwin
#define GL_SILENCE_DEPRECATION
#include <OpenGL/gl3.h>
#else
#include <GLES3/gl3.h>
#endif
namespace nostalgia::core::renderer {
struct Empty {};
struct TextureBase {
GLsizei width = 0;
GLsizei height = 0;
constexpr TextureBase() noexcept = default;
constexpr TextureBase(TextureBase &&tb) noexcept {
width = tb.width;
height = tb.height;
tb.width = 0;
tb.height = 0;
}
constexpr TextureBase &operator=(TextureBase &&tb) noexcept {
width = tb.width;
height = tb.height;
tb.width = 0;
tb.height = 0;
return *this;
}
};
template<auto del, typename Base = Empty>
struct GLObject: public Base {
GLuint id = 0;
constexpr GLObject() noexcept = default;
explicit constexpr GLObject(GLuint id) noexcept {
this->id = id;
}
constexpr GLObject(GLObject &&o) noexcept: Base(ox::move(o)) {
id = o.id;
o.id = 0;
}
~GLObject() {
del(id);
}
GLObject &operator=(GLObject &&o) noexcept {
if (this != &o) {
del(id);
Base::operator=(ox::move(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;
}
};
void deleteBuffer(GLuint b) noexcept;
void deleteTexture(GLuint t) noexcept;
void deleteVertexArray(GLuint v) noexcept;
extern template struct GLObject<deleteBuffer>;
extern template struct GLObject<deleteTexture, TextureBase>;
extern template struct GLObject<deleteVertexArray>;
extern template struct GLObject<glDeleteProgram>;
extern template struct GLObject<glDeleteShader>;
using GLBuffer = GLObject<deleteBuffer>;
using GLShader = GLObject<glDeleteShader>;
using GLProgram = GLObject<glDeleteProgram>;
using GLTexture = GLObject<deleteTexture, TextureBase>;
using GLVertexArray = GLObject<deleteVertexArray>;
[[nodiscard]]
ox::Result<GLProgram> buildShaderProgram(const GLchar *vert, const GLchar *frag) noexcept;
}