Skip to content

Commit

Permalink
reduce gpu/cpu mem copy
Browse files Browse the repository at this point in the history
  • Loading branch information
archibate committed May 11, 2024
1 parent 52acbee commit 28e8ab3
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 48 deletions.
8 changes: 4 additions & 4 deletions include/OBJ.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
#include <string>

struct DrawableOBJ {
check_gl::GLVertexArray vao;
check_gl::GLBuffer vbo;
check_gl::GLBuffer ebo;
GLVertexArray vao;
GLBuffer vbo;
GLBuffer ebo;
std::size_t numElements;

void draw();
Expand All @@ -24,5 +24,5 @@ struct OBJ {

void load_obj(std::string path);
void auto_normal();
[[nodiscard]] DrawableOBJ draw_obj();
void draw_obj(DrawableOBJ &drawable, bool dynamic);
};
40 changes: 22 additions & 18 deletions include/check_gl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,16 @@ void opengl_link_program(unsigned int program);
::check_gl::opengl_check_error(__FILE__, __LINE__, #x); \
} while (0)

namespace check_gl {

struct GLHandle {
struct GLHandleBase {
unsigned int handle;

GLHandle() noexcept : handle(0) {
GLHandleBase() noexcept : handle(0) {
}

explicit GLHandle(std::nullptr_t) noexcept : handle(0) {
explicit GLHandleBase(std::nullptr_t) noexcept : handle(0) {
}

explicit GLHandle(unsigned int handle) noexcept : handle(handle) {
explicit GLHandleBase(unsigned int handle) noexcept : handle(handle) {
}

operator unsigned int() const noexcept {
Expand All @@ -48,26 +46,26 @@ struct GLHandle {
return ret;
}

GLHandle(GLHandle &&that) noexcept : handle(that.handle) {
GLHandleBase(GLHandleBase &&that) noexcept : handle(that.handle) {
that.handle = 0;
}

GLHandle &operator=(GLHandle &&that) noexcept {
GLHandleBase &operator=(GLHandleBase &&that) noexcept {
std::swap(handle, that.handle);
return *this;
}
};

template <class Derived>
struct GLHandleImpl : GLHandle {
using GLHandle::GLHandle;
struct GLHandleImpl : GLHandleBase {
using GLHandleBase::GLHandleBase;

GLHandleImpl(GLHandleImpl &&) = default;
GLHandleImpl &operator=(GLHandleImpl &&) = default;

struct [[nodiscard]] BindGuard : GLHandle {
struct [[nodiscard]] BindGuard : GLHandleBase {
private:
BindGuard(unsigned int handle) noexcept : GLHandle(handle) {
BindGuard(unsigned int handle) noexcept : GLHandleBase(handle) {
}

friend GLHandleImpl;
Expand All @@ -82,11 +80,11 @@ struct GLHandleImpl : GLHandle {
}
};

struct [[nodiscard]] BindTargetGuard : GLHandle {
struct [[nodiscard]] BindTargetGuard : GLHandleBase {
private:
unsigned int target;

BindTargetGuard(unsigned int target, unsigned int handle) noexcept : GLHandle(handle), target(target) {
BindTargetGuard(unsigned int target, unsigned int handle) noexcept : GLHandleBase(handle), target(target) {
}

friend GLHandleImpl;
Expand Down Expand Up @@ -121,12 +119,20 @@ struct GLHandleImpl : GLHandle {
/* } */

template <class ...Args>
Derived &make(Args ...args) {
Derived &&remake(Args ...args) {
if (this->handle) {
Derived::on_delete(1, &this->handle);
}
Derived::on_gen(1, &this->handle, args...);
return static_cast<Derived &>(*this);
return std::move(static_cast<Derived &>(*this));
}

template <class ...Args>
Derived &&make(Args ...args) {
if (!this->handle) {
Derived::on_gen(1, &this->handle, args...);
}
return std::move(static_cast<Derived &>(*this));
}

void reset(unsigned int handle) {
Expand Down Expand Up @@ -245,5 +251,3 @@ struct GLTexture : GLHandleImpl<GLRenderbuffer> {
CHECK_GL(glBindTexture(target, handle));
}
};

}
22 changes: 11 additions & 11 deletions src/Game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ struct Game::Private { // P-IMPL pattern

OBJ obj;
DrawableOBJ drawable;
unsigned int program;
GLProgram program;
};

Game::Game() : m_private(std::make_unique<Private>()) {}
Expand All @@ -29,7 +29,6 @@ void Game::set_window(GLFWwindow *window) {
#endif

void Game::initialize() {
m_private->obj.load_obj(OPENGLTUTOR_HOME "assets/monkey.obj");
CHECK_GL(glEnable(GL_DEPTH_TEST));
CHECK_GL(glEnable(GL_MULTISAMPLE));
CHECK_GL(glEnable(GL_BLEND));
Expand All @@ -40,19 +39,21 @@ void Game::initialize() {

// glm::vec4 lightPos(-1, 1, 1, 0); // 等会用更现代的方式指定光源方向

unsigned int program = glCreateProgram();
unsigned int vertShader = glCreateShader(GL_VERTEX_SHADER);
auto program = GLProgram().make();

auto vertShader = GLShader().make(GL_VERTEX_SHADER);
check_gl::opengl_shader_source(vertShader, get_file_content(OPENGLTUTOR_HOME "assets/orange.vert"));
CHECK_GL(glAttachShader(program, vertShader));
unsigned int fragShader = glCreateShader(GL_FRAGMENT_SHADER);

auto fragShader = GLShader().make(GL_FRAGMENT_SHADER);
check_gl::opengl_shader_source(fragShader, get_file_content(OPENGLTUTOR_HOME "assets/orange.frag"));
CHECK_GL(glAttachShader(program, fragShader));

CHECK_GL(glLinkProgram(program));
/* unsigned int vao; */
/* CHECK_GL(glGenVertexArrays(1, &vao)); */
/* CHECK_GL(glBindVertexArray(vao)); */
/* m_private->vao = vao; */
m_private->program = program;
m_private->program = std::move(program);

m_private->obj.load_obj(OPENGLTUTOR_HOME "assets/monkey.obj");
m_private->obj.draw_obj(m_private->drawable, /*dynamic=*/false);
}

void Game::render() {
Expand Down Expand Up @@ -85,6 +86,5 @@ void Game::render() {
glm::vec3 lightDir = glm::normalize(glm::vec3(mousePos.x, mousePos.y, 1));
int location = glGetUniformLocation(m_private->program, "uniLightDir");
CHECK_GL(glUniform3fv(location, 1, glm::value_ptr(lightDir)));
m_private->drawable = m_private->obj.draw_obj();
m_private->drawable.draw();
}
24 changes: 9 additions & 15 deletions src/OBJ.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,24 +94,20 @@ void DrawableOBJ::draw() {
CHECK_GL(glDrawElements(GL_TRIANGLES, numElements, GL_UNSIGNED_INT, (void *)0));
}

DrawableOBJ OBJ::draw_obj() {
DrawableOBJ drawable;
drawable.vao.make();
drawable.vbo.make();
drawable.ebo.make();
auto vboBind = drawable.vbo.bind(GL_ARRAY_BUFFER);
CHECK_GL(glBufferData(GL_ARRAY_BUFFER, sizeof(Vertex) * vertices.size(), vertices.data(), GL_STATIC_DRAW));
auto eboBind = drawable.ebo.bind(GL_ELEMENT_ARRAY_BUFFER);
CHECK_GL(glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(glm::uvec3) * faces.size(), faces.data(), GL_STATIC_DRAW));
auto vaoBind = drawable.vao.bind();
void OBJ::draw_obj(DrawableOBJ &drawable, bool dynamic) {
auto vboBind = drawable.vbo.make().bind(GL_ARRAY_BUFFER);
CHECK_GL(glBufferData(GL_ARRAY_BUFFER, sizeof(Vertex) * vertices.size(), vertices.data(), dynamic ? GL_DYNAMIC_DRAW : GL_STATIC_DRAW));
auto eboBind = drawable.ebo.make().bind(GL_ELEMENT_ARRAY_BUFFER);
CHECK_GL(glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(glm::uvec3) * faces.size(), faces.data(), dynamic ? GL_DYNAMIC_DRAW : GL_STATIC_DRAW));
auto vaoBind = drawable.vao.make().bind();
CHECK_GL(glEnableVertexAttribArray(0));
CHECK_GL(glEnableVertexAttribArray(1));
CHECK_GL(glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void *)offsetof(Vertex, position)));
CHECK_GL(glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void *)offsetof(Vertex, normal)));
drawable.numElements = faces.size() * 3;
/* CHECK_GL(glDrawElements(GL_TRIANGLES, drawable.numElements, GL_UNSIGNED_INT, (void *)0)); */
return drawable;
}

/* void OBJ::legacy_draw_obj() { */
/* glBegin(GL_TRIANGLES); */
/* for (auto const &face: faces) { */
/* auto const &a = vertices[face[0]]; */
Expand All @@ -125,6 +121,4 @@ DrawableOBJ OBJ::draw_obj() {
/* glVertexAttrib3fv(0, glm::value_ptr(c.position)); */
/* } */
/* CHECK_GL(glEnd()); */

return {};
}
/* } */

0 comments on commit 28e8ab3

Please sign in to comment.