From 7813eedf61dc3873f1903c806f4220e63bcff752 Mon Sep 17 00:00:00 2001 From: Jozufozu Date: Mon, 6 Dec 2021 21:11:51 -0800 Subject: [PATCH] Miscellaneous gl changes while debugging - Only unbind at the end of MaterialManagerImpl#render - Add GlBufferType#bind and #unbind - Make GlVertexArray#unbind static --- .../flywheel/backend/gl/GlVertexArray.java | 2 +- .../flywheel/backend/gl/buffer/GlBuffer.java | 4 ++-- .../backend/gl/buffer/GlBufferType.java | 10 ++++++++ .../backend/instancing/GPUInstancer.java | 6 +---- .../backend/material/MaterialManagerImpl.java | 6 +++++ .../backend/model/ArrayModelRenderer.java | 4 +--- .../flywheel/core/FullscreenQuad.java | 4 ++-- .../core/crumbling/CrumblingGroup.java | 24 +++++++++++-------- 8 files changed, 37 insertions(+), 23 deletions(-) diff --git a/src/main/java/com/jozufozu/flywheel/backend/gl/GlVertexArray.java b/src/main/java/com/jozufozu/flywheel/backend/gl/GlVertexArray.java index 64544c54b..0802d231f 100644 --- a/src/main/java/com/jozufozu/flywheel/backend/gl/GlVertexArray.java +++ b/src/main/java/com/jozufozu/flywheel/backend/gl/GlVertexArray.java @@ -11,7 +11,7 @@ public class GlVertexArray extends GlObject { Backend.getInstance().compat.vao.bindVertexArray(handle()); } - public void unbind() { + public static void unbind() { Backend.getInstance().compat.vao.bindVertexArray(0); } diff --git a/src/main/java/com/jozufozu/flywheel/backend/gl/buffer/GlBuffer.java b/src/main/java/com/jozufozu/flywheel/backend/gl/buffer/GlBuffer.java index 54063a15d..f764f1dec 100644 --- a/src/main/java/com/jozufozu/flywheel/backend/gl/buffer/GlBuffer.java +++ b/src/main/java/com/jozufozu/flywheel/backend/gl/buffer/GlBuffer.java @@ -40,11 +40,11 @@ public abstract class GlBuffer extends GlObject { } public void bind() { - GL20.glBindBuffer(type.glEnum, handle()); + type.bind(handle()); } public void unbind() { - GL20.glBindBuffer(type.glEnum, 0); + type.unbind(); } public void doneForThisFrame() { diff --git a/src/main/java/com/jozufozu/flywheel/backend/gl/buffer/GlBufferType.java b/src/main/java/com/jozufozu/flywheel/backend/gl/buffer/GlBufferType.java index 7ba9d0073..b2371c440 100644 --- a/src/main/java/com/jozufozu/flywheel/backend/gl/buffer/GlBufferType.java +++ b/src/main/java/com/jozufozu/flywheel/backend/gl/buffer/GlBufferType.java @@ -8,6 +8,8 @@ import org.lwjgl.opengl.GL40; import org.lwjgl.opengl.GL42; import org.lwjgl.opengl.GL43; +import com.mojang.blaze3d.platform.GlStateManager; + public enum GlBufferType { ARRAY_BUFFER(GL15C.GL_ARRAY_BUFFER), ELEMENT_ARRAY_BUFFER(GL15C.GL_ELEMENT_ARRAY_BUFFER), @@ -29,4 +31,12 @@ public enum GlBufferType { GlBufferType(int glEnum) { this.glEnum = glEnum; } + + public void bind(int buffer) { + GlStateManager._glBindBuffer(glEnum, buffer); + } + + public void unbind() { + GlStateManager._glBindBuffer(glEnum, 0); + } } diff --git a/src/main/java/com/jozufozu/flywheel/backend/instancing/GPUInstancer.java b/src/main/java/com/jozufozu/flywheel/backend/instancing/GPUInstancer.java index d80982c38..a3ea14ceb 100644 --- a/src/main/java/com/jozufozu/flywheel/backend/instancing/GPUInstancer.java +++ b/src/main/java/com/jozufozu/flywheel/backend/instancing/GPUInstancer.java @@ -52,7 +52,7 @@ public class GPUInstancer extends AbstractInstancer { // persistent mapping sync point instanceVBO.doneForThisFrame(); - vao.unbind(); + GlVertexArray.unbind(); GlError.pollAndThrow(() -> modelData.name() + "_unbind"); } @@ -72,16 +72,12 @@ public class GPUInstancer extends AbstractInstancer { vao.bind(); model.setupState(); - - vao.unbind(); }); vao.bind(); instanceVBO = GlBuffer.requestPersistent(GlBufferType.ARRAY_BUFFER); AttribUtil.enableArrays(model.getAttributeCount() + instanceFormat.getAttributeCount()); - - vao.unbind(); } public boolean isInitialized() { diff --git a/src/main/java/com/jozufozu/flywheel/backend/material/MaterialManagerImpl.java b/src/main/java/com/jozufozu/flywheel/backend/material/MaterialManagerImpl.java index 3c49410fd..7e5cca536 100644 --- a/src/main/java/com/jozufozu/flywheel/backend/material/MaterialManagerImpl.java +++ b/src/main/java/com/jozufozu/flywheel/backend/material/MaterialManagerImpl.java @@ -5,6 +5,8 @@ import java.util.HashMap; import java.util.Map; import java.util.function.Supplier; +import com.jozufozu.flywheel.backend.gl.GlVertexArray; +import com.jozufozu.flywheel.backend.gl.buffer.GlBufferType; import com.jozufozu.flywheel.backend.state.RenderLayer; import com.jozufozu.flywheel.core.WorldContext; import com.jozufozu.flywheel.core.shader.WorldProgram; @@ -89,6 +91,10 @@ public class MaterialManagerImpl

implements MaterialMana group.render(state, viewProjection, camX, camY, camZ); } + + GlBufferType.ELEMENT_ARRAY_BUFFER.unbind(); + GlBufferType.ARRAY_BUFFER.unbind(); + GlVertexArray.unbind(); } public void delete() { diff --git a/src/main/java/com/jozufozu/flywheel/backend/model/ArrayModelRenderer.java b/src/main/java/com/jozufozu/flywheel/backend/model/ArrayModelRenderer.java index 48359821c..8ac9bc906 100644 --- a/src/main/java/com/jozufozu/flywheel/backend/model/ArrayModelRenderer.java +++ b/src/main/java/com/jozufozu/flywheel/backend/model/ArrayModelRenderer.java @@ -22,8 +22,6 @@ public class ArrayModelRenderer extends ModelRenderer { vao.bind(); model.drawCall(); - - vao.unbind(); } @Override @@ -44,7 +42,7 @@ public class ArrayModelRenderer extends ModelRenderer { AttribUtil.enableArrays(this.model.getAttributeCount()); - vao.unbind(); + GlVertexArray.unbind(); this.model.clearState(); } diff --git a/src/main/java/com/jozufozu/flywheel/core/FullscreenQuad.java b/src/main/java/com/jozufozu/flywheel/core/FullscreenQuad.java index 19245df4f..d3635718d 100644 --- a/src/main/java/com/jozufozu/flywheel/core/FullscreenQuad.java +++ b/src/main/java/com/jozufozu/flywheel/core/FullscreenQuad.java @@ -39,14 +39,14 @@ public class FullscreenQuad { GL20.glVertexAttribPointer(0, 4, GlNumericType.FLOAT.getGlEnum(), false, 4 * 4, 0); - vao.unbind(); + GlVertexArray.unbind(); vbo.unbind(); } public void draw() { vao.bind(); GL20.glDrawArrays(GL20.GL_TRIANGLES, 0, 6); - vao.unbind(); + GlVertexArray.unbind(); } public void delete() { diff --git a/src/main/java/com/jozufozu/flywheel/core/crumbling/CrumblingGroup.java b/src/main/java/com/jozufozu/flywheel/core/crumbling/CrumblingGroup.java index 3c403ebcd..cf096be15 100644 --- a/src/main/java/com/jozufozu/flywheel/core/crumbling/CrumblingGroup.java +++ b/src/main/java/com/jozufozu/flywheel/core/crumbling/CrumblingGroup.java @@ -28,16 +28,7 @@ public class CrumblingGroup

extends MaterialGroupImp int renderTex = RenderSystem.getShaderTexture(0); - ResourceLocation texture = RenderTextures.getShaderTexture(0); - - if (texture != null) { - SheetData atlasData = AtlasInfo.getAtlasData(texture); - - width = atlasData.width; - height = atlasData.height; - } else { - width = height = 256; - } + updateAtlasSize(); type.clearRenderState(); @@ -56,6 +47,19 @@ public class CrumblingGroup

extends MaterialGroupImp CrumblingRenderer._currentLayer.clearRenderState(); } + private void updateAtlasSize() { + ResourceLocation texture = RenderTextures.getShaderTexture(0); + + if (texture != null) { + SheetData atlasData = AtlasInfo.getAtlasData(texture); + + width = atlasData.width; + height = atlasData.height; + } else { + width = height = 256; + } + } + @Override public void setup(P p) { p.setAtlasSize(width, height);