From f926fc6c4ae7944401ff17952150aac06101b631 Mon Sep 17 00:00:00 2001 From: Basique Date: Thu, 24 Aug 2023 02:04:19 +0300 Subject: [PATCH] changes? --- .../wispforest/owo/ui/util/GlDebugUtils.java | 41 +++++++++++++ .../io/wispforest/owo/ui/util/OwoGlUtil.java | 60 +++++++++++++++++++ .../owo/ui/window/FramebufferWindow.java | 19 +++--- .../wispforest/owo/ui/window/OwoWindow.java | 38 ++++++------ .../window/context/VanillaWindowContext.java | 5 ++ .../io/wispforest/owo/util/OwoGlfwUtil.java | 33 ---------- 6 files changed, 139 insertions(+), 57 deletions(-) create mode 100644 src/main/java/io/wispforest/owo/ui/util/GlDebugUtils.java create mode 100644 src/main/java/io/wispforest/owo/ui/util/OwoGlUtil.java delete mode 100644 src/main/java/io/wispforest/owo/util/OwoGlfwUtil.java diff --git a/src/main/java/io/wispforest/owo/ui/util/GlDebugUtils.java b/src/main/java/io/wispforest/owo/ui/util/GlDebugUtils.java new file mode 100644 index 00000000..ab02b80c --- /dev/null +++ b/src/main/java/io/wispforest/owo/ui/util/GlDebugUtils.java @@ -0,0 +1,41 @@ +package io.wispforest.owo.ui.util; + + +import org.lwjgl.opengl.ARBDebugOutput; +import org.lwjgl.opengl.GL; +import org.lwjgl.opengl.KHRDebug; + +public final class GlDebugUtils { + public static final boolean GL_KHR_debug = GL.getCapabilities().GL_KHR_debug; + + private GlDebugUtils() { + + } + + public static void labelObject(int type, int id, String name) { + if (GL_KHR_debug) { + KHRDebug.glObjectLabel(type, id, name); + } + } + + public static DebugGroup pushGroup(String name) { + if (GL_KHR_debug) { + KHRDebug.glPushDebugGroup(KHRDebug.GL_DEBUG_SOURCE_APPLICATION, 42, name); + } + + return new DebugGroup(); + } + + public static class DebugGroup implements AutoCloseable { + private DebugGroup() { + + } + + @Override + public void close() { + if (GL_KHR_debug) { + KHRDebug.glPopDebugGroup(); + } + } + } +} diff --git a/src/main/java/io/wispforest/owo/ui/util/OwoGlUtil.java b/src/main/java/io/wispforest/owo/ui/util/OwoGlUtil.java new file mode 100644 index 00000000..886f5b80 --- /dev/null +++ b/src/main/java/io/wispforest/owo/ui/util/OwoGlUtil.java @@ -0,0 +1,60 @@ +package io.wispforest.owo.ui.util; + +import com.mojang.blaze3d.systems.RenderSystem; +import com.mojang.blaze3d.systems.VertexSorter; +import org.joml.Matrix4f; +import org.lwjgl.glfw.GLFW; + +public final class OwoGlUtil { + private OwoGlUtil() { + + } + + public static ContextRestorer setContext(long handle) { + long old = GLFW.glfwGetCurrentContext(); + if (old == handle) return new ContextRestorer(-1); + + GLFW.glfwMakeContextCurrent(handle); + + return new ContextRestorer(old); + } + + public static ProjectionRestorer setProjectionMatrix(Matrix4f projectionMatrix, VertexSorter sorter) { + Matrix4f oldMatrix = RenderSystem.getProjectionMatrix(); + VertexSorter oldSorter = RenderSystem.getVertexSorting(); + + RenderSystem.setProjectionMatrix(projectionMatrix, sorter); + + return new ProjectionRestorer(oldMatrix, oldSorter); + } + + public static class ContextRestorer implements AutoCloseable { + private final long oldContext; + + private ContextRestorer(long old) { + this.oldContext = old; + } + + @Override + public void close() { + if (oldContext == -1) return; + + GLFW.glfwMakeContextCurrent(oldContext); + } + } + + public static class ProjectionRestorer implements AutoCloseable { + private final Matrix4f matrix; + private final VertexSorter sorter; + + private ProjectionRestorer(Matrix4f matrix, VertexSorter sorter) { + this.matrix = matrix; + this.sorter = sorter; + } + + @Override + public void close() { + RenderSystem.setProjectionMatrix(matrix, sorter); + } + } +} diff --git a/src/main/java/io/wispforest/owo/ui/window/FramebufferWindow.java b/src/main/java/io/wispforest/owo/ui/window/FramebufferWindow.java index 3d9c558a..b4a95bd3 100644 --- a/src/main/java/io/wispforest/owo/ui/window/FramebufferWindow.java +++ b/src/main/java/io/wispforest/owo/ui/window/FramebufferWindow.java @@ -2,11 +2,12 @@ import com.mojang.blaze3d.systems.RenderSystem; import io.wispforest.owo.ui.event.CharTyped; +import io.wispforest.owo.ui.util.GlDebugUtils; import io.wispforest.owo.ui.window.context.CurrentWindowContext; import io.wispforest.owo.ui.window.context.WindowContext; import io.wispforest.owo.util.EventSource; import io.wispforest.owo.util.EventStream; -import io.wispforest.owo.util.OwoGlfwUtil; +import io.wispforest.owo.ui.util.OwoGlUtil; import io.wispforest.owo.util.SupportsFeaturesImpl; import net.minecraft.client.MinecraftClient; import net.minecraft.client.gl.Framebuffer; @@ -48,7 +49,7 @@ public FramebufferWindow(int width, int height, String name, long parentContext) this.width = width; this.height = height; - try (var ignored = OwoGlfwUtil.setContext(0)) { + try (var ignored = OwoGlUtil.setContext(0)) { glfwDefaultWindowHints(); glfwWindowHint(GLFW_CLIENT_API, GLFW_OPENGL_API); glfwWindowHint(GLFW_CONTEXT_CREATION_API, GLFW_NATIVE_CONTEXT_API); @@ -67,8 +68,9 @@ public FramebufferWindow(int width, int height, String name, long parentContext) } this.framebuffer = new SimpleFramebuffer(width, height, true, MinecraftClient.IS_SYSTEM_MAC); + GlDebugUtils.labelObject(GL32.GL_FRAMEBUFFER, this.framebuffer.fbo, "Main context framebuffer for " + this); - try (var ignored = OwoGlfwUtil.setContext(this.handle)) { + try (var ignored = OwoGlUtil.setContext(this.handle)) { GlDebug.enableDebug(client.options.glDebugVerbosity, true); } @@ -179,7 +181,8 @@ public EventSource charTyped() { public void present() { if (closed()) return; - try (var ignored = OwoGlfwUtil.setContext(handle)) { + try (var ignored = OwoGlUtil.setContext(handle); + var ignored1 = GlDebugUtils.pushGroup("Presenting framebuffer of " + this)) { // This code intentionally doesn't use Minecraft's RenderSystem // class, as it caches GL state that is invalid on this context. GL32.glBindFramebuffer(GL32.GL_READ_FRAMEBUFFER, localFramebuffer); @@ -197,13 +200,14 @@ public void present() { } private void initLocalFramebuffer() { - try (var ignored = OwoGlfwUtil.setContext(this.handle)) { + try (var ignored = OwoGlUtil.setContext(this.handle)) { if (localFramebuffer != 0) { GL32.glDeleteFramebuffers(localFramebuffer); } this.localFramebuffer = GL32.glGenFramebuffers(); GL32.glBindFramebuffer(GL32.GL_FRAMEBUFFER, this.localFramebuffer); + GlDebugUtils.labelObject(GL32.GL_FRAMEBUFFER, this.localFramebuffer, "Local context framebuffer for " + this); GL32.glFramebufferTexture2D(GL32.GL_FRAMEBUFFER, GL32.GL_COLOR_ATTACHMENT0, GL32.GL_TEXTURE_2D, this.framebuffer.getColorAttachment(), 0); int status = GL32.glCheckFramebufferStatus(GL32.GL_FRAMEBUFFER); @@ -218,10 +222,11 @@ protected void sizeChanged(long handle, int width, int height) { this.width = width; this.height = height; - try (var ignored = OwoGlfwUtil.setContext(client.getWindow().getHandle())) { + try (var ignored = OwoGlUtil.setContext(client.getWindow().getHandle())) { framebuffer.delete(); this.framebuffer = new SimpleFramebuffer(width, height, true, MinecraftClient.IS_SYSTEM_MAC); + GlDebugUtils.labelObject(GL32.GL_FRAMEBUFFER, this.framebuffer.fbo, "Main context framebuffer for " + this); } initLocalFramebuffer(); @@ -237,7 +242,7 @@ public boolean closed() { public void close() { super.close(); - try (var ignored = OwoGlfwUtil.setContext(this.handle)) { + try (var ignored = OwoGlUtil.setContext(this.handle)) { GL32.glDeleteFramebuffers(this.localFramebuffer); } diff --git a/src/main/java/io/wispforest/owo/ui/window/OwoWindow.java b/src/main/java/io/wispforest/owo/ui/window/OwoWindow.java index 1aaeba5f..9dd77c3b 100644 --- a/src/main/java/io/wispforest/owo/ui/window/OwoWindow.java +++ b/src/main/java/io/wispforest/owo/ui/window/OwoWindow.java @@ -4,6 +4,8 @@ import com.mojang.blaze3d.systems.VertexSorter; import io.wispforest.owo.ui.core.OwoUIAdapter; import io.wispforest.owo.ui.core.ParentComponent; +import io.wispforest.owo.ui.util.GlDebugUtils; +import io.wispforest.owo.ui.util.OwoGlUtil; import io.wispforest.owo.ui.window.context.CurrentWindowContext; import net.minecraft.client.MinecraftClient; import net.minecraft.client.gui.DrawContext; @@ -97,7 +99,8 @@ public void recalculateScale() { public void render() { if (closed()) return; - try (var ignored = CurrentWindowContext.setCurrent(this)) { + try (var ignored = CurrentWindowContext.setCurrent(this); + var ignored1 = GlDebugUtils.pushGroup("Rendering " + this)) { framebuffer().beginWrite(true); RenderSystem.clearColor(0, 0, 0, 1); @@ -112,22 +115,23 @@ public void render() { 1000.0F, 21000.0F ); - RenderSystem.backupProjectionMatrix(); - RenderSystem.setProjectionMatrix(matrix4f, VertexSorter.BY_Z); - MatrixStack matrixStack = RenderSystem.getModelViewStack(); - matrixStack.push(); - matrixStack.loadIdentity(); - matrixStack.translate(0.0F, 0.0F, -11000.0F); - RenderSystem.applyModelViewMatrix(); - DiffuseLighting.enableGuiDepthLighting(); - - var consumers = client.getBufferBuilders().getEntityVertexConsumers(); - adapter.render(new DrawContext(client, consumers), mouseX, mouseY, client.getTickDelta()); - consumers.draw(); - - RenderSystem.getModelViewStack().pop(); - RenderSystem.applyModelViewMatrix(); - RenderSystem.restoreProjectionMatrix(); + + try (var ignored2 = OwoGlUtil.setProjectionMatrix(matrix4f, VertexSorter.BY_Z)) { + MatrixStack matrixStack = RenderSystem.getModelViewStack(); + matrixStack.push(); + matrixStack.loadIdentity(); + matrixStack.translate(0.0F, 0.0F, -11000.0F); + RenderSystem.applyModelViewMatrix(); + DiffuseLighting.enableGuiDepthLighting(); + + var consumers = client.getBufferBuilders().getEntityVertexConsumers(); + adapter.render(new DrawContext(client, consumers), mouseX, mouseY, client.getTickDelta()); + consumers.draw(); + + RenderSystem.getModelViewStack().pop(); + RenderSystem.applyModelViewMatrix(); + } + framebuffer().endWrite(); } diff --git a/src/main/java/io/wispforest/owo/ui/window/context/VanillaWindowContext.java b/src/main/java/io/wispforest/owo/ui/window/context/VanillaWindowContext.java index f88578e0..ea9a25eb 100644 --- a/src/main/java/io/wispforest/owo/ui/window/context/VanillaWindowContext.java +++ b/src/main/java/io/wispforest/owo/ui/window/context/VanillaWindowContext.java @@ -59,4 +59,9 @@ public double scaleFactor() { public long handle() { return window.getHandle(); } + + @Override + public String toString() { + return "VanillaWindowContext[" + (this == MAIN ? "MAIN" : window) + "]"; + } } diff --git a/src/main/java/io/wispforest/owo/util/OwoGlfwUtil.java b/src/main/java/io/wispforest/owo/util/OwoGlfwUtil.java deleted file mode 100644 index e2ac652f..00000000 --- a/src/main/java/io/wispforest/owo/util/OwoGlfwUtil.java +++ /dev/null @@ -1,33 +0,0 @@ -package io.wispforest.owo.util; - -import org.lwjgl.glfw.GLFW; - -public final class OwoGlfwUtil { - private OwoGlfwUtil() { - - } - - public static ContextRestorer setContext(long handle) { - long old = GLFW.glfwGetCurrentContext(); - if (old == handle) return new ContextRestorer(-1); - - GLFW.glfwMakeContextCurrent(handle); - - return new ContextRestorer(old); - } - - public static class ContextRestorer implements AutoCloseable { - private final long oldContext; - - private ContextRestorer(long old) { - this.oldContext = old; - } - - @Override - public void close() { - if (oldContext == -1) return; - - GLFW.glfwMakeContextCurrent(oldContext); - } - } -}