Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
640 changes: 194 additions & 446 deletions src/main/java/rs117/hd/HdPlugin.java

Large diffs are not rendered by default.

17 changes: 8 additions & 9 deletions src/main/java/rs117/hd/config/UIScalingMode.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,21 +26,20 @@

import lombok.Getter;
import lombok.RequiredArgsConstructor;

import static org.lwjgl.opengl.GL33C.*;
import rs117.hd.utils.opengl.texture.GLSamplerMode;

@Getter
@RequiredArgsConstructor
public enum UIScalingMode {
NEAREST("Nearest", GL_NEAREST),
LINEAR("Bilinear", GL_LINEAR),
MITCHELL("Mitchell", GL_NEAREST),
CATMULL_ROM("Catmull-Rom", GL_NEAREST),
XBR("xBR", GL_NEAREST),
PIXEL("Pixel", GL_LINEAR);
NEAREST("Nearest", GLSamplerMode.NEAREST_CLAMP),
LINEAR("Bilinear", GLSamplerMode.LINEAR_CLAMP),
MITCHELL("Mitchell", GLSamplerMode.NEAREST_CLAMP),
CATMULL_ROM("Catmull-Rom", GLSamplerMode.NEAREST_CLAMP),
XBR("xBR", GLSamplerMode.NEAREST_CLAMP),
PIXEL("Pixel", GLSamplerMode.LINEAR_CLAMP);

private final String name;
public final int glSamplingFunction;
public final GLSamplerMode samplerMode;

@Override
public String toString()
Expand Down
24 changes: 8 additions & 16 deletions src/main/java/rs117/hd/opengl/AsyncUICopy.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import rs117.hd.HdPlugin;
import rs117.hd.overlays.FrameTimer;
import rs117.hd.overlays.Timer;
import rs117.hd.utils.opengl.texture.GLTexture;

import static org.lwjgl.opengl.GL33C.*;

Expand All @@ -31,8 +32,7 @@ public class AsyncUICopy implements Runnable {

private IntBuffer mappedBuffer;
private int[] pixels;
private int interfacePbo;
private int interfaceTexture;
private GLTexture uiTex;
private int width;
private int height;

Expand All @@ -45,23 +45,20 @@ public void run() {
timer.add(Timer.COPY_UI, time);
}

public void prepare(int interfacePbo, int interfaceTex) {
public void prepare(GLTexture uiTex) {
// Ensure there isn't already another UI copy in progress
if (mappedBuffer != null)
return;

timer.begin(Timer.MAP_UI_BUFFER);
glBindBuffer(GL_PIXEL_UNPACK_BUFFER, interfacePbo);
ByteBuffer buffer = glMapBuffer(GL_PIXEL_UNPACK_BUFFER, GL_WRITE_ONLY);
glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0);
ByteBuffer buffer = uiTex.map(GL_WRITE_ONLY);
timer.end(Timer.MAP_UI_BUFFER);
if (buffer == null) {
log.error("Unable to map interface PBO. Skipping UI...");
return;
}

this.interfacePbo = interfacePbo;
this.interfaceTexture = interfaceTex;
this.uiTex = uiTex;
this.mappedBuffer = buffer.asIntBuffer();

var provider = client.getBufferProvider();
Expand All @@ -85,18 +82,13 @@ public boolean complete() {
throw new RuntimeException(e);
}

var uiResolution = plugin.getUiResolution();
if (uiResolution == null || width > uiResolution[0] || height > uiResolution[1]) {
log.error("UI texture resolution mismatch ({}x{} > {}). Skipping UI...", width, height, uiResolution);
if (width > uiTex.getWidth() || height > uiTex.getHeight()) {
log.error("UI texture resolution mismatch ({}x{} > {}x{}). Skipping UI...", width, height, uiTex.getWidth(), uiTex.getHeight());
return false;
}

timer.begin(Timer.UPLOAD_UI);
glBindBuffer(GL_PIXEL_UNPACK_BUFFER, interfacePbo);
glUnmapBuffer(GL_PIXEL_UNPACK_BUFFER);
glActiveTexture(HdPlugin.TEXTURE_UNIT_UI);
glBindTexture(GL_TEXTURE_2D, interfaceTexture);
glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, width, height, GL_BGRA, GL_UNSIGNED_INT_8_8_8_8_REV, 0);
uiTex.unmap(0, 0, width, height);
timer.end(Timer.UPLOAD_UI);

glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0);
Expand Down
73 changes: 73 additions & 0 deletions src/main/java/rs117/hd/opengl/GLRenderState.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package rs117.hd.opengl;

import java.util.ArrayDeque;
import lombok.RequiredArgsConstructor;
import rs117.hd.opengl.shader.ShaderProgram;
import rs117.hd.utils.opengl.texture.GLFrameBuffer;
import rs117.hd.utils.opengl.texture.GLTexture;

import static org.lwjgl.opengl.GL20C.glUseProgram;

public class GLRenderState {
public static final BindStack<GLFrameBuffer> frameBuffer = new BindStack<>(GLFrameBuffer::bind);
public static final BindStack<GLTexture> texture = new BindStack<>(GLTexture::bind);

public static ShaderProgram activeShaderProgram;

public static void clearActiveShader() {
if(activeShaderProgram != null) {
glUseProgram(0);
activeShaderProgram = null;
}
}

public static void clear() {
frameBuffer.clear();
texture.clear();

clearActiveShader();
}

@FunctionalInterface
public interface BinderInterface<T> {
void bind(T val);
}

@RequiredArgsConstructor
public static class BindStack<T> {
private T active;
private final ArrayDeque<T> previouslyActive = new ArrayDeque<>();
private final BinderInterface<T> binderFunction;

public boolean isActive(T val) { return active == val;}

public void push(T val){
if(val == null) {
active = null;
return;
}

if(active != null) {
previouslyActive.push(active);
}

previouslyActive.remove(val);
active = val;
}

public void pop() {
if(previouslyActive.isEmpty()) {
active = null;
return;
}

T newActive = previouslyActive.pop();
binderFunction.bind(newActive);
active = newActive;
}

public void clear() {
previouslyActive.clear();
}
}
}
2 changes: 1 addition & 1 deletion src/main/java/rs117/hd/opengl/compute/OpenCLManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
import rs117.hd.opengl.shader.ShaderException;
import rs117.hd.opengl.shader.ShaderIncludes;
import rs117.hd.opengl.uniforms.UBOCompute;
import rs117.hd.utils.buffer.SharedGLBuffer;
import rs117.hd.utils.opengl.buffer.SharedGLBuffer;

import static org.lwjgl.opencl.APPLEGLSharing.CL_CGL_DEVICE_FOR_CURRENT_VIRTUAL_SCREEN_APPLE;
import static org.lwjgl.opencl.APPLEGLSharing.clGetGLContextInfoAPPLE;
Expand Down
10 changes: 7 additions & 3 deletions src/main/java/rs117/hd/opengl/shader/ShaderProgram.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import rs117.hd.opengl.GLRenderState;
import rs117.hd.opengl.uniforms.UniformBuffer;

import static org.lwjgl.opengl.GL33C.*;
Expand Down Expand Up @@ -91,10 +92,13 @@ public <T extends UniformBuffer<?>> T getUniformBufferBlock(int UniformBlockInde

public void use() {
assert program != 0;
glUseProgram(program);
if(GLRenderState.activeShaderProgram != this) {
GLRenderState.activeShaderProgram = this;
glUseProgram(program);

for (UniformBufferBlockPair pair : uniformBlockMappings)
glUniformBlockBinding(program, pair.uboProgramIndex, pair.buffer.getBindingIndex());
for (UniformBufferBlockPair pair : uniformBlockMappings)
glUniformBlockBinding(program, pair.uboProgramIndex, pair.buffer.getBindingIndex());
}
}

public void destroy() {
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/rs117/hd/opengl/uniforms/UBOCompute.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import rs117.hd.utils.buffer.SharedGLBuffer;
import rs117.hd.utils.opengl.buffer.SharedGLBuffer;

import static org.lwjgl.opencl.CL10.*;
import static org.lwjgl.opengl.GL33C.*;
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/rs117/hd/opengl/uniforms/UBOGlobal.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package rs117.hd.opengl.uniforms;

import rs117.hd.utils.buffer.GLBuffer;
import rs117.hd.utils.opengl.buffer.GLBuffer;

import static org.lwjgl.opengl.GL33C.*;

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/rs117/hd/opengl/uniforms/UBOLights.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package rs117.hd.opengl.uniforms;

import rs117.hd.utils.buffer.GLBuffer;
import rs117.hd.utils.opengl.buffer.GLBuffer;

import static org.lwjgl.opengl.GL33C.*;

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/rs117/hd/opengl/uniforms/UBOMaterials.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import rs117.hd.HdPlugin;
import rs117.hd.model.ModelPusher;
import rs117.hd.scene.materials.Material;
import rs117.hd.utils.buffer.GLBuffer;
import rs117.hd.utils.opengl.buffer.GLBuffer;

import static org.lwjgl.opengl.GL33C.*;
import static rs117.hd.utils.MathUtils.*;
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/rs117/hd/opengl/uniforms/UBOUI.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package rs117.hd.opengl.uniforms;

import rs117.hd.utils.buffer.GLBuffer;
import rs117.hd.utils.opengl.buffer.GLBuffer;

import static org.lwjgl.opengl.GL33C.*;

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/rs117/hd/opengl/uniforms/UBOWaterTypes.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import rs117.hd.HdPlugin;
import rs117.hd.scene.water_types.WaterType;
import rs117.hd.utils.buffer.GLBuffer;
import rs117.hd.utils.opengl.buffer.GLBuffer;

import static org.lwjgl.opengl.GL33C.*;

Expand Down
4 changes: 2 additions & 2 deletions src/main/java/rs117/hd/opengl/uniforms/UniformBuffer.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.lwjgl.BufferUtils;
import rs117.hd.utils.buffer.GLBuffer;
import rs117.hd.utils.buffer.SharedGLBuffer;
import rs117.hd.utils.opengl.buffer.GLBuffer;
import rs117.hd.utils.opengl.buffer.SharedGLBuffer;

import static org.lwjgl.opengl.GL33C.*;
import static rs117.hd.utils.MathUtils.*;
Expand Down
9 changes: 5 additions & 4 deletions src/main/java/rs117/hd/overlays/ShaderOverlay.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
import rs117.hd.opengl.shader.ShaderProgram;
import rs117.hd.opengl.shader.ShaderTemplate;
import rs117.hd.utils.ShaderRecompile;
import rs117.hd.utils.opengl.texture.GLTexture;

import static org.lwjgl.opengl.GL33C.*;
import static rs117.hd.utils.MathUtils.*;
Expand Down Expand Up @@ -377,17 +378,17 @@ private void updateTransform() {
if (isFullscreen()) {
shader.uniTransform.set(0, 0, 1, 1);
} else {
int[] resolution = plugin.getUiResolution();
if (resolution == null)
GLTexture uiTex = plugin.getUiTex();
if (uiTex == null)
return;
var bounds = getBounds();
// Calculate translation and scale in NDC
float[] rect = { bounds.x + 1, bounds.y + 1, bounds.width - 1, bounds.height - 1 };
rect[0] += rect[0] + rect[2];
rect[1] += rect[1] + rect[3];
for (int i = 0; i < 2; i++) {
rect[i * 2] /= resolution[0];
rect[i * 2 + 1] /= resolution[1];
rect[i * 2] /= uiTex.getWidth();
rect[i * 2 + 1] /= uiTex.getHeight();
rect[i] -= 1;
}
rect[1] *= -1;
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/rs117/hd/overlays/Timer.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public enum Timer {
UPLOAD_GEOMETRY(true),
UPLOAD_UI(true, "Upload UI"),
COMPUTE(true),
CLEAR_SCENE(true),
CLEAR_SCENE(true, false),
RENDER_SHADOWS(true),
RENDER_SCENE(true),
RENDER_UI(true, "Render UI"),
Expand Down
Loading