Skip to content

Commit

Permalink
changes?
Browse files Browse the repository at this point in the history
  • Loading branch information
BasiqueEvangelist committed Aug 23, 2023
1 parent 755e0d2 commit f926fc6
Show file tree
Hide file tree
Showing 6 changed files with 139 additions and 57 deletions.
41 changes: 41 additions & 0 deletions src/main/java/io/wispforest/owo/ui/util/GlDebugUtils.java
Original file line number Diff line number Diff line change
@@ -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();
}
}
}
}
60 changes: 60 additions & 0 deletions src/main/java/io/wispforest/owo/ui/util/OwoGlUtil.java
Original file line number Diff line number Diff line change
@@ -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);
}
}
}
19 changes: 12 additions & 7 deletions src/main/java/io/wispforest/owo/ui/window/FramebufferWindow.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand All @@ -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);
}

Expand Down Expand Up @@ -179,7 +181,8 @@ public EventSource<CharTyped> 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);
Expand All @@ -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);
Expand All @@ -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();
Expand All @@ -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);
}

Expand Down
38 changes: 21 additions & 17 deletions src/main/java/io/wispforest/owo/ui/window/OwoWindow.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand All @@ -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();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,9 @@ public double scaleFactor() {
public long handle() {
return window.getHandle();
}

@Override
public String toString() {
return "VanillaWindowContext[" + (this == MAIN ? "MAIN" : window) + "]";
}
}
33 changes: 0 additions & 33 deletions src/main/java/io/wispforest/owo/util/OwoGlfwUtil.java

This file was deleted.

0 comments on commit f926fc6

Please sign in to comment.