Skip to content

Commit fe60b2b

Browse files
authored
Merge pull request #1734 from ZzzhHe/opengl-bufferless-quad-vao
Bind empty VAO for bufferless quad rendering
2 parents 247ce09 + 21fbaa9 commit fe60b2b

3 files changed

Lines changed: 22 additions & 5 deletions

File tree

libopenage/renderer/opengl/geometry.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2015-2024 the openage authors. See copying.md for legal info.
1+
// Copyright 2015-2025 the openage authors. See copying.md for legal info.
22

33
#include "geometry.h"
44

@@ -54,6 +54,7 @@ void GlGeometry::update_verts_offset(std::vector<uint8_t> const &verts, size_t o
5454
void GlGeometry::draw() const {
5555
switch (this->get_type()) {
5656
case geometry_t::bufferless_quad:
57+
// any VAO must be bound before this call
5758
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
5859
break;
5960

libopenage/renderer/opengl/renderer.cpp

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2017-2024 the openage authors. See copying.md for legal info.
1+
// Copyright 2017-2025 the openage authors. See copying.md for legal info.
22

33
#include "renderer.h"
44

@@ -15,6 +15,7 @@
1515
#include "renderer/opengl/texture.h"
1616
#include "renderer/opengl/uniform_buffer.h"
1717
#include "renderer/opengl/uniform_input.h"
18+
#include "renderer/opengl/vertex_array.h"
1819
#include "renderer/opengl/window.h"
1920
#include "renderer/resources/buffer_info.h"
2021

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

@@ -100,7 +102,7 @@ std::shared_ptr<UniformBuffer> GlRenderer::add_uniform_buffer(resources::Uniform
100102
resources::UniformBufferInfo::get_size(input, info.get_layout()),
101103
resources::UniformBufferInfo::get_stride_size(input.type, info.get_layout()),
102104
input.count,
103-
input.name});
105+
input.name});
104106

105107
offset += size;
106108
}
@@ -169,6 +171,11 @@ void GlRenderer::render(const std::shared_ptr<RenderPass> &pass) {
169171
auto gl_target = std::dynamic_pointer_cast<GlRenderTarget>(pass->get_target());
170172
gl_target->bind_write();
171173

174+
// ensure that an (empty) VAO is bound before rendering geometry
175+
// a bound VAO is required to render bufferless quad geometries by OpenGL
176+
// see https://www.khronos.org/opengl/wiki/Vertex_Rendering#Causes_of_rendering_failure
177+
shared_quad_vao->bind();
178+
172179
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
173180

174181
// TODO: Option for face culling

libopenage/renderer/opengl/renderer.h

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2017-2024 the openage authors. See copying.md for legal info.
1+
// Copyright 2017-2025 the openage authors. See copying.md for legal info.
22

33
#pragma once
44

@@ -17,6 +17,7 @@ namespace opengl {
1717
class GlContext;
1818
class GlRenderPass;
1919
class GlRenderTarget;
20+
class GlVertexArray;
2021
class GlWindow;
2122

2223
/// The OpenGL specialization of the rendering interface.
@@ -67,6 +68,14 @@ class GlRenderer final : public Renderer {
6768

6869
/// The main screen surface as a render target.
6970
std::shared_ptr<GlRenderTarget> display;
71+
72+
/// An empty vertex array object (VAO).
73+
///
74+
/// This VAO has to be bound at the start of a render pass to ensure
75+
/// that bufferless quad geometry can be drawn without errors. Drawing a
76+
/// bufferless quad requires any VAO to be bound
77+
/// see https://www.khronos.org/opengl/wiki/Vertex_Rendering#Causes_of_rendering_failure
78+
std::shared_ptr<GlVertexArray> shared_quad_vao;
7079
};
7180

7281
} // namespace opengl

0 commit comments

Comments
 (0)