From 21fbaa91849ddb5dcac177ade4c64c38c8f67de7 Mon Sep 17 00:00:00 2001 From: Alex Zhuohao He Date: Thu, 2 Jan 2025 15:05:53 -0500 Subject: [PATCH] bind empty VAO to bufferless quad rendering --- libopenage/renderer/opengl/geometry.cpp | 3 ++- libopenage/renderer/opengl/renderer.cpp | 13 ++++++++++--- libopenage/renderer/opengl/renderer.h | 11 ++++++++++- 3 files changed, 22 insertions(+), 5 deletions(-) diff --git a/libopenage/renderer/opengl/geometry.cpp b/libopenage/renderer/opengl/geometry.cpp index 0e2c930a2a..e0898edbe8 100644 --- a/libopenage/renderer/opengl/geometry.cpp +++ b/libopenage/renderer/opengl/geometry.cpp @@ -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" @@ -54,6 +54,7 @@ void GlGeometry::update_verts_offset(std::vector const &verts, size_t o void GlGeometry::draw() const { switch (this->get_type()) { case geometry_t::bufferless_quad: + // any VAO must be bound before this call glDrawArrays(GL_TRIANGLE_STRIP, 0, 4); break; diff --git a/libopenage/renderer/opengl/renderer.cpp b/libopenage/renderer/opengl/renderer.cpp index 33b734790f..6d8d909a5b 100644 --- a/libopenage/renderer/opengl/renderer.cpp +++ b/libopenage/renderer/opengl/renderer.cpp @@ -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" @@ -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" @@ -26,7 +27,8 @@ GlRenderer::GlRenderer(const std::shared_ptr &ctx, gl_context{ctx}, display{std::make_shared(ctx, viewport_size[0], - viewport_size[1])} { + viewport_size[1])}, + shared_quad_vao{std::make_shared(ctx)} { // color used to clear the color buffers glClearColor(0.0f, 0.0f, 0.0f, 0.0f); @@ -100,7 +102,7 @@ std::shared_ptr GlRenderer::add_uniform_buffer(resources::Uniform resources::UniformBufferInfo::get_size(input, info.get_layout()), resources::UniformBufferInfo::get_stride_size(input.type, info.get_layout()), input.count, - input.name}); + input.name}); offset += size; } @@ -169,6 +171,11 @@ void GlRenderer::render(const std::shared_ptr &pass) { auto gl_target = std::dynamic_pointer_cast(pass->get_target()); gl_target->bind_write(); + // ensure that an (empty) VAO is bound before rendering geometry + // a bound VAO is required to render bufferless quad geometries by OpenGL + // see https://www.khronos.org/opengl/wiki/Vertex_Rendering#Causes_of_rendering_failure + shared_quad_vao->bind(); + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // TODO: Option for face culling diff --git a/libopenage/renderer/opengl/renderer.h b/libopenage/renderer/opengl/renderer.h index 458af24358..751af7cacc 100644 --- a/libopenage/renderer/opengl/renderer.h +++ b/libopenage/renderer/opengl/renderer.h @@ -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 @@ -17,6 +17,7 @@ namespace opengl { class GlContext; class GlRenderPass; class GlRenderTarget; +class GlVertexArray; class GlWindow; /// The OpenGL specialization of the rendering interface. @@ -67,6 +68,14 @@ class GlRenderer final : public Renderer { /// The main screen surface as a render target. std::shared_ptr display; + + /// An empty vertex array object (VAO). + /// + /// This VAO has to be bound at the start of a render pass to ensure + /// that bufferless quad geometry can be drawn without errors. Drawing a + /// bufferless quad requires any VAO to be bound + /// see https://www.khronos.org/opengl/wiki/Vertex_Rendering#Causes_of_rendering_failure + std::shared_ptr shared_quad_vao; }; } // namespace opengl