From 14ee590e98f41be47cff3dc5fcac86cfe9b6b0bd Mon Sep 17 00:00:00 2001 From: Moritz Zwerger Date: Thu, 14 Dec 2023 23:32:49 +0100 Subject: [PATCH] opengl: abstract vao Thought I am smarter than opengl, but it totally wasn't good. Keeping the abstraction though --- .../system/dummy/buffer/DummyVertexBuffer.kt | 4 +- .../registries/registries/RegistriesLoader.kt | 1 - .../system/base/buffer/vertex/VertexBuffer.kt | 4 +- .../buffer/vertex/FloatOpenGLVertexBuffer.kt | 37 +++------- .../system/opengl/buffer/vertex/OpenGLVAO.kt | 67 +++++++++++++++++++ 5 files changed, 79 insertions(+), 34 deletions(-) create mode 100644 src/main/java/de/bixilon/minosoft/gui/rendering/system/opengl/buffer/vertex/OpenGLVAO.kt diff --git a/src/integration-test/kotlin/de/bixilon/minosoft/gui/rendering/system/dummy/buffer/DummyVertexBuffer.kt b/src/integration-test/kotlin/de/bixilon/minosoft/gui/rendering/system/dummy/buffer/DummyVertexBuffer.kt index 455c000fd..839eeaa38 100644 --- a/src/integration-test/kotlin/de/bixilon/minosoft/gui/rendering/system/dummy/buffer/DummyVertexBuffer.kt +++ b/src/integration-test/kotlin/de/bixilon/minosoft/gui/rendering/system/dummy/buffer/DummyVertexBuffer.kt @@ -1,6 +1,6 @@ /* * Minosoft - * Copyright (C) 2020-2022 Moritz Zwerger + * Copyright (C) 2020-2023 Moritz Zwerger * * This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. * @@ -21,7 +21,7 @@ import de.bixilon.minosoft.gui.rendering.util.mesh.MeshStruct import java.nio.FloatBuffer class DummyVertexBuffer( - override val structure: MeshStruct, + override val struct: MeshStruct, override var buffer: FloatBuffer, override val type: RenderableBufferTypes = RenderableBufferTypes.ARRAY_BUFFER, ) : FloatVertexBuffer { diff --git a/src/main/java/de/bixilon/minosoft/data/registries/registries/RegistriesLoader.kt b/src/main/java/de/bixilon/minosoft/data/registries/registries/RegistriesLoader.kt index 0c20de3d0..fffca8387 100644 --- a/src/main/java/de/bixilon/minosoft/data/registries/registries/RegistriesLoader.kt +++ b/src/main/java/de/bixilon/minosoft/data/registries/registries/RegistriesLoader.kt @@ -26,7 +26,6 @@ object RegistriesLoader { registries.setDefaultParents(version) - return registries } diff --git a/src/main/java/de/bixilon/minosoft/gui/rendering/system/base/buffer/vertex/VertexBuffer.kt b/src/main/java/de/bixilon/minosoft/gui/rendering/system/base/buffer/vertex/VertexBuffer.kt index 0e1d942b0..b14a69586 100644 --- a/src/main/java/de/bixilon/minosoft/gui/rendering/system/base/buffer/vertex/VertexBuffer.kt +++ b/src/main/java/de/bixilon/minosoft/gui/rendering/system/base/buffer/vertex/VertexBuffer.kt @@ -1,6 +1,6 @@ /* * Minosoft - * Copyright (C) 2021 Moritz Zwerger + * Copyright (C) 2020-2023 Moritz Zwerger * * This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. * @@ -18,7 +18,7 @@ import de.bixilon.minosoft.gui.rendering.util.mesh.MeshStruct interface VertexBuffer { val vertices: Int val primitiveType: PrimitiveTypes - val structure: MeshStruct + val struct: MeshStruct fun draw() } diff --git a/src/main/java/de/bixilon/minosoft/gui/rendering/system/opengl/buffer/vertex/FloatOpenGLVertexBuffer.kt b/src/main/java/de/bixilon/minosoft/gui/rendering/system/opengl/buffer/vertex/FloatOpenGLVertexBuffer.kt index 2cd6b05b7..799bed8f7 100644 --- a/src/main/java/de/bixilon/minosoft/gui/rendering/system/opengl/buffer/vertex/FloatOpenGLVertexBuffer.kt +++ b/src/main/java/de/bixilon/minosoft/gui/rendering/system/opengl/buffer/vertex/FloatOpenGLVertexBuffer.kt @@ -21,39 +21,29 @@ import de.bixilon.minosoft.gui.rendering.system.base.buffer.vertex.PrimitiveType import de.bixilon.minosoft.gui.rendering.system.opengl.OpenGLRenderSystem import de.bixilon.minosoft.gui.rendering.system.opengl.buffer.FloatOpenGLBuffer import de.bixilon.minosoft.gui.rendering.util.mesh.MeshStruct -import org.lwjgl.opengl.GL20.glEnableVertexAttribArray -import org.lwjgl.opengl.GL20.glVertexAttribPointer import org.lwjgl.opengl.GL30.* import java.nio.FloatBuffer class FloatOpenGLVertexBuffer( renderSystem: OpenGLRenderSystem, - override val structure: MeshStruct, + override val struct: MeshStruct, data: FloatBuffer, override val primitiveType: PrimitiveTypes, ) : FloatOpenGLBuffer(renderSystem, data), FloatVertexBuffer { + private val vao = OpenGLVAO(renderSystem, struct) override var vertices = -1 private set - private var vao = -1 override fun init() { - val floatsPerVertex = structure.BYTES_PER_VERTEX / Float.SIZE_BYTES + val floatsPerVertex = struct.BYTES_PER_VERTEX / Float.SIZE_BYTES vertices = buffer.position() / floatsPerVertex - vao = glGenVertexArrays() super.init() - glBindVertexArray(vao) - - super.initialUpload() bind() - + super.initialUpload() _data = null - - for (attribute in structure.attributes) { - glVertexAttribPointer(attribute.index, attribute.size, GL_FLOAT, false, structure.BYTES_PER_VERTEX, attribute.stride) - glEnableVertexAttribArray(attribute.index) - } + vao.init() unbind() } @@ -63,16 +53,12 @@ class FloatOpenGLVertexBuffer( return } super.unbind() - glBindVertexArray(0) + vao.unbind() } fun bindVao() { super.bind() - if (renderSystem.boundVao == vao) { - return - } - glBindVertexArray(vao) - renderSystem.boundVao = vao + vao.bind() } override fun draw() { @@ -82,13 +68,7 @@ class FloatOpenGLVertexBuffer( } override fun unload() { - if (state == RenderableBufferStates.UPLOADED) { - glDeleteVertexArrays(vao) - if (renderSystem.boundVao == vao) { - renderSystem.boundVao = -1 - } - vao = -1 - } + vao.unload() super.unload() } @@ -103,5 +83,4 @@ class FloatOpenGLVertexBuffer( } } } - } diff --git a/src/main/java/de/bixilon/minosoft/gui/rendering/system/opengl/buffer/vertex/OpenGLVAO.kt b/src/main/java/de/bixilon/minosoft/gui/rendering/system/opengl/buffer/vertex/OpenGLVAO.kt new file mode 100644 index 000000000..45349069b --- /dev/null +++ b/src/main/java/de/bixilon/minosoft/gui/rendering/system/opengl/buffer/vertex/OpenGLVAO.kt @@ -0,0 +1,67 @@ +/* + * Minosoft + * Copyright (C) 2020-2023 Moritz Zwerger + * + * This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along with this program. If not, see . + * + * This software is not affiliated with Mojang AB, the original developer of Minecraft. + */ + +package de.bixilon.minosoft.gui.rendering.system.opengl.buffer.vertex + +import de.bixilon.minosoft.gui.rendering.RenderConstants +import de.bixilon.minosoft.gui.rendering.system.opengl.OpenGLRenderSystem +import de.bixilon.minosoft.gui.rendering.util.mesh.MeshStruct +import org.lwjgl.opengl.GL20.glEnableVertexAttribArray +import org.lwjgl.opengl.GL20.glVertexAttribPointer +import org.lwjgl.opengl.GL30.* + +class OpenGLVAO( + val system: OpenGLRenderSystem, + val struct: MeshStruct +) { + private var handle = -1 + + fun init() { + if (handle >= 0) throw IllegalArgumentException("VAO already loaded!") + handle = glGenVertexArrays() + bind() + + for (attribute in struct.attributes) { + glVertexAttribPointer(attribute.index, attribute.size, GL_FLOAT, false, struct.BYTES_PER_VERTEX, attribute.stride) + glEnableVertexAttribArray(attribute.index) + } + unbind() + } + + fun bind() { + if (handle < 0) throw IllegalArgumentException("VAO not initialized!") + if (system.boundVao == handle) { + return + } + glBindVertexArray(handle) + system.boundVao = handle + } + + fun unbind() { + if (RenderConstants.DIRTY_BUFFER_UNBIND) { + return + } + if (handle < 0) throw IllegalArgumentException("VAO not initialized!") + glBindVertexArray(-1) + system.boundVao = -1 + } + + fun unload() { + if (handle < 0) throw IllegalArgumentException("VAO not initialized!") + glDeleteVertexArrays(handle) + if (system.boundVao == handle) { + system.boundVao = -1 + } + handle = -1 + } +}