Skip to content

Commit

Permalink
better solution: use a shared VAO and bind at the start of render()
Browse files Browse the repository at this point in the history
  • Loading branch information
ZzzhHe committed Jan 1, 2025
1 parent 62c1510 commit ec8707d
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 17 deletions.
11 changes: 4 additions & 7 deletions libopenage/renderer/opengl/geometry.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2015-2024 the openage authors. See copying.md for legal info.
// Copyright 2015-2025 the openage authors. See copying.md for legal info.

#include "geometry.h"

Expand All @@ -14,10 +14,8 @@ namespace openage {
namespace renderer {
namespace opengl {

GlGeometry::GlGeometry(const std::shared_ptr<GlContext> &context) :
Geometry(geometry_t::bufferless_quad) {
this->quad_vao = GlVertexArray(context);
}
GlGeometry::GlGeometry() :
Geometry(geometry_t::bufferless_quad) {}

GlGeometry::GlGeometry(const std::shared_ptr<GlContext> &context, const resources::MeshData &mesh) :
Geometry(geometry_t::mesh) {
Expand Down Expand Up @@ -56,8 +54,7 @@ void GlGeometry::update_verts_offset(std::vector<uint8_t> const &verts, size_t o
void GlGeometry::draw() const {
switch (this->get_type()) {
case geometry_t::bufferless_quad:
// Bind the empty VAO before drawing the buffer-less quad.
this->quad_vao->bind();
// a VAO must be bound before this call. This is handled by GlRenderer.
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
break;

Expand Down
8 changes: 2 additions & 6 deletions libopenage/renderer/opengl/geometry.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2015-2024 the openage authors. See copying.md for legal info.
// Copyright 2015-2025 the openage authors. See copying.md for legal info.

#pragma once

Expand All @@ -19,7 +19,7 @@ namespace opengl {
class GlGeometry final : public Geometry {
public:
/// The default constructor makes a quad.
GlGeometry(const std::shared_ptr<GlContext> &context);
GlGeometry();

/// Initialize a meshed geometry. Relatively costly, has to initialize GL buffers and copy vertex data.
explicit GlGeometry(const std::shared_ptr<GlContext> &context, resources::MeshData const &);
Expand All @@ -44,10 +44,6 @@ class GlGeometry final : public Geometry {
/// Data managing GPU memory and interpretation of mesh data.
/// Only present if the type is a mesh.
std::optional<GlMesh> mesh;

/// Empty VAO for bufferless quad drawing.
/// Only present if the type is bufferless_quad.
std::optional<GlVertexArray> quad_vao;
};

} // namespace opengl
Expand Down
11 changes: 8 additions & 3 deletions libopenage/renderer/opengl/renderer.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2017-2024 the openage authors. See copying.md for legal info.
// Copyright 2017-2025 the openage authors. See copying.md for legal info.

#include "renderer.h"

Expand All @@ -15,6 +15,7 @@
#include "renderer/opengl/texture.h"
#include "renderer/opengl/uniform_buffer.h"
#include "renderer/opengl/uniform_input.h"
#include "renderer/opengl/vertex_array.h"
#include "renderer/opengl/window.h"
#include "renderer/resources/buffer_info.h"

Expand All @@ -26,7 +27,8 @@ GlRenderer::GlRenderer(const std::shared_ptr<GlContext> &ctx,
gl_context{ctx},
display{std::make_shared<GlRenderTarget>(ctx,
viewport_size[0],
viewport_size[1])} {
viewport_size[1])},
shared_quad_vao(std::make_shared<GlVertexArray>(ctx)) {
// color used to clear the color buffers
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);

Expand Down Expand Up @@ -63,7 +65,7 @@ std::shared_ptr<Geometry> GlRenderer::add_mesh_geometry(resources::MeshData cons
}

std::shared_ptr<Geometry> GlRenderer::add_bufferless_quad() {
return std::make_shared<GlGeometry>(this->gl_context);
return std::make_shared<GlGeometry>();
}

std::shared_ptr<RenderPass> GlRenderer::add_render_pass(std::vector<Renderable> renderables, const std::shared_ptr<RenderTarget> &target) {
Expand Down Expand Up @@ -169,6 +171,9 @@ void GlRenderer::render(const std::shared_ptr<RenderPass> &pass) {
auto gl_target = std::dynamic_pointer_cast<GlRenderTarget>(pass->get_target());
gl_target->bind_write();

// bind the shared quad vao for all bufferless quad geometries
shared_quad_vao->bind();

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

// TODO: Option for face culling
Expand Down
5 changes: 4 additions & 1 deletion libopenage/renderer/opengl/renderer.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2017-2024 the openage authors. See copying.md for legal info.
// Copyright 2017-2025 the openage authors. See copying.md for legal info.

#pragma once

Expand All @@ -17,6 +17,7 @@ namespace opengl {
class GlContext;
class GlRenderPass;
class GlRenderTarget;
class GlVertexArray;
class GlWindow;

/// The OpenGL specialization of the rendering interface.
Expand Down Expand Up @@ -67,6 +68,8 @@ class GlRenderer final : public Renderer {

/// The main screen surface as a render target.
std::shared_ptr<GlRenderTarget> display;

std::shared_ptr<GlVertexArray> shared_quad_vao;
};

} // namespace opengl
Expand Down

0 comments on commit ec8707d

Please sign in to comment.