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
127 changes: 81 additions & 46 deletions src/main/java/rs117/hd/HdPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@
import org.lwjgl.opengl.*;
import org.lwjgl.system.Callback;
import org.lwjgl.system.Configuration;
import rs117.hd.config.AntiAliasingMode;
import rs117.hd.config.ColorFilter;
import rs117.hd.config.DynamicLights;
import rs117.hd.config.SeasonalHemisphere;
Expand All @@ -95,7 +96,6 @@
import rs117.hd.overlays.FrameTimer;
import rs117.hd.overlays.GammaCalibrationOverlay;
import rs117.hd.overlays.ShadowMapOverlay;
import rs117.hd.overlays.TiledLightingOverlay;
import rs117.hd.overlays.Timer;
import rs117.hd.renderer.Renderer;
import rs117.hd.renderer.legacy.LegacyRenderer;
Expand Down Expand Up @@ -165,6 +165,7 @@ public class HdPlugin extends Plugin {

public static int MAX_TEXTURE_UNITS;
public static int TEXTURE_UNIT_COUNT = 0;
public static final int TEXTURE_UNIT_SCENE_COLOR = GL_TEXTURE0 + TEXTURE_UNIT_COUNT++;
public static final int TEXTURE_UNIT_UI = GL_TEXTURE0 + TEXTURE_UNIT_COUNT++;
public static final int TEXTURE_UNIT_GAME = GL_TEXTURE0 + TEXTURE_UNIT_COUNT++;
public static final int TEXTURE_UNIT_SHADOW_MAP = GL_TEXTURE0 + TEXTURE_UNIT_COUNT++;
Expand Down Expand Up @@ -316,9 +317,6 @@ public class HdPlugin extends Plugin {
@Inject
private ShadowMapOverlay shadowMapOverlay;

@Inject
private TiledLightingOverlay tiledLightingOverlay;

@Inject
public HDVariables vars;

Expand Down Expand Up @@ -374,9 +372,9 @@ public class HdPlugin extends Plugin {
public int[] sceneResolution;
public int fboScene;
private int rboSceneColor;
private int texSceneColor;
private int rboSceneDepth;
public int fboSceneResolve;
private int rboSceneResolveColor;
private int fboSceneResolve;

public int shadowMapResolution;
public int fboShadowMap;
Expand Down Expand Up @@ -537,7 +535,6 @@ protected void startUp() {
rboSceneColor = 0;
rboSceneDepth = 0;
fboSceneResolve = 0;
rboSceneResolveColor = 0;
fboShadowMap = 0;
frame = 0;
elapsedTime = 0;
Expand Down Expand Up @@ -915,6 +912,7 @@ public ShaderIncludes getShaderIncludes() {
.define("MAX_LIGHT_COUNT", configTiledLighting ? UBOLights.MAX_LIGHTS : configDynamicLights.getMaxSceneLights())
.define("NORMAL_MAPPING", config.normalMapping())
.define("PARALLAX_OCCLUSION_MAPPING", config.parallaxOcclusionMapping())
.define("FXAA_ENABLED", config.antiAliasingMode() == AntiAliasingMode.FXAA)
.define("SHADOW_MODE", configShadowMode)
.define("SHADOW_TRANSPARENCY", config.shadowTransparency())
.define("SHADOW_FILTERING", config.shadowFiltering())
Expand Down Expand Up @@ -1317,33 +1315,62 @@ public void updateSceneFbo() {
uboGlobal.sceneResolution.set(sceneResolution);
uboGlobal.upload(); // Ensure this is up to date with rendering


// Create and bind the FBO
fboScene = glGenFramebuffers();
glBindFramebuffer(GL_FRAMEBUFFER, fboScene);

// Create color render buffer
rboSceneColor = glGenRenderbuffers();
glBindRenderbuffer(GL_RENDERBUFFER, rboSceneColor);

// Flush out all pending errors, so we can check whether the next step succeeds
clearGLErrors();

int format = 0;
for (int desiredFormat : desiredFormats) {
glRenderbufferStorageMultisample(GL_RENDERBUFFER, msaaSamples, desiredFormat, sceneResolution[0], sceneResolution[1]);
if (msaaSamples > 1) {
rboSceneColor = glGenRenderbuffers();
glBindRenderbuffer(GL_RENDERBUFFER, rboSceneColor);

for (int desiredFormat : desiredFormats) {
glRenderbufferStorageMultisample(GL_RENDERBUFFER, msaaSamples, desiredFormat, sceneResolution[0], sceneResolution[1]);
if (glGetError() == GL_NO_ERROR) {
format = desiredFormat;
break;
}
}

if (glGetError() == GL_NO_ERROR) {
format = desiredFormat;
break;
if (format == 0)
throw new RuntimeException("No supported " + (sRGB ? "sRGB" : "linear") + " formats");

// Found a usable format. Bind the RBO to the scene FBO
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, rboSceneColor);
checkGLErrors();
} else {
for (int desiredFormat : desiredFormats) {
final int probe = glGenRenderbuffers();
glBindRenderbuffer(GL_RENDERBUFFER, probe);
glRenderbufferStorage(GL_RENDERBUFFER, desiredFormat, sceneResolution[0], sceneResolution[1]);

final boolean supported = glGetError() == GL_NO_ERROR;

glDeleteRenderbuffers(probe);
glBindRenderbuffer(GL_RENDERBUFFER, 0);

if (supported) {
format = desiredFormat;
break;
}
}
}

if (format == 0)
throw new RuntimeException("No supported " + (sRGB ? "sRGB" : "linear") + " formats");
if (format == 0)
throw new RuntimeException("No supported " + (sRGB ? "sRGB" : "linear") + " formats");

// Found a usable format. Bind the RBO to the scene FBO
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, rboSceneColor);
checkGLErrors();
texSceneColor = glGenTextures();
glActiveTexture(TEXTURE_UNIT_SCENE_COLOR);
glBindTexture(GL_TEXTURE_2D, texSceneColor);
glTexImage2D(GL_TEXTURE_2D, 0, format, sceneResolution[0], sceneResolution[1], 0, GL_RGBA, GL_UNSIGNED_BYTE, 0);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, texSceneColor, 0);
checkGLErrors();
}

// Create depth render buffer
rboSceneDepth = glGenRenderbuffers();
Expand All @@ -1353,17 +1380,23 @@ public void updateSceneFbo() {
checkGLErrors();

// If necessary, create an FBO for resolving multisampling
if (msaaSamples > 1 && resolutionScale != 1) {
if (msaaSamples > 1) {
fboSceneResolve = glGenFramebuffers();
glBindFramebuffer(GL_FRAMEBUFFER, fboSceneResolve);
rboSceneResolveColor = glGenRenderbuffers();
glBindRenderbuffer(GL_RENDERBUFFER, rboSceneResolveColor);
glRenderbufferStorageMultisample(GL_RENDERBUFFER, 0, format, sceneResolution[0], sceneResolution[1]);
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, rboSceneResolveColor);

texSceneColor = glGenTextures();
glActiveTexture(TEXTURE_UNIT_SCENE_COLOR);
glBindTexture(GL_TEXTURE_2D, texSceneColor);
glTexImage2D(GL_TEXTURE_2D, 0, format, sceneResolution[0], sceneResolution[1], 0, GL_RGBA, GL_UNSIGNED_BYTE, 0);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, texSceneColor, 0);
checkGLErrors();
}

// Reset
glActiveTexture(TEXTURE_UNIT_UI);
glBindTexture(GL_TEXTURE_2D, 0);
glBindFramebuffer(GL_FRAMEBUFFER, awtContext.getFramebuffer(false));
glBindRenderbuffer(GL_RENDERBUFFER, 0);
}
Expand All @@ -1375,9 +1408,9 @@ private void destroySceneFbo() {
glDeleteFramebuffers(fboScene);
fboScene = 0;

if (rboSceneColor != 0)
glDeleteRenderbuffers(rboSceneColor);
rboSceneColor = 0;
if (texSceneColor != 0)
glDeleteTextures(texSceneColor);
texSceneColor = 0;

if (rboSceneDepth != 0)
glDeleteRenderbuffers(rboSceneDepth);
Expand All @@ -1386,10 +1419,6 @@ private void destroySceneFbo() {
if (fboSceneResolve != 0)
glDeleteFramebuffers(fboSceneResolve);
fboSceneResolve = 0;

if (rboSceneResolveColor != 0)
glDeleteRenderbuffers(rboSceneResolveColor);
rboSceneResolveColor = 0;
}

private void initializeShadowMapFbo() {
Expand Down Expand Up @@ -1537,15 +1566,27 @@ public void drawUi(int overlayColor) {

frameTimer.begin(Timer.RENDER_UI);

glBindFramebuffer(GL_FRAMEBUFFER, awtContext.getFramebuffer(false));
// Disable alpha writes, just in case the default FBO has an alpha channel
glColorMask(true, true, true, false);
if(fboSceneResolve != 0) {
glBindFramebuffer(GL_READ_FRAMEBUFFER, fboScene);
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, fboSceneResolve);
glBlitFramebuffer(
0, 0, sceneResolution[0], sceneResolution[1],
0, 0, sceneResolution[0], sceneResolution[1],
GL_COLOR_BUFFER_BIT, GL_NEAREST
);

glViewport(0, 0, actualUiResolution[0], actualUiResolution[1]);
glBindFramebuffer(GL_READ_FRAMEBUFFER, 0);
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0);
}

tiledLightingOverlay.render();
glBindFramebuffer(GL_FRAMEBUFFER, awtContext.getFramebuffer(false));
glViewport(0, 0, actualUiResolution[0], actualUiResolution[1]);

uiProgram.use();
if(sceneViewport != null)
uboUI.sceneViewport.set(sceneViewport);
else
uboUI.sceneViewport.set(0, 0, 0, 0);
uboUI.sourceDimensions.set(uiResolution);
uboUI.targetDimensions.set(actualUiResolution);
uboUI.alphaOverlay.set(ColorUtils.srgba(overlayColor));
Expand Down Expand Up @@ -1578,19 +1619,12 @@ public void drawUi(int overlayColor) {
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, function);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, function);

glEnable(GL_BLEND);
glBlendFuncSeparate(GL_ONE, GL_ONE_MINUS_SRC_ALPHA, GL_ZERO, GL_ONE);
glBindVertexArray(vaoTri);
glDrawArrays(GL_TRIANGLES, 0, 3);

shadowMapOverlay.render();
gammaCalibrationOverlay.render();

// Reset
glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_ZERO, GL_ONE);
glDisable(GL_BLEND);
glColorMask(true, true, true, true);

frameTimer.end(Timer.RENDER_UI);
}

Expand Down Expand Up @@ -1820,6 +1854,7 @@ public void processPendingConfigChanges() {
recompilePrograms = true;
break;
case KEY_ANTI_ALIASING_MODE:
recompilePrograms = true;
case KEY_SCENE_RESOLUTION_SCALE:
recreateSceneFbo = true;
break;
Expand Down
1 change: 1 addition & 0 deletions src/main/java/rs117/hd/config/AntiAliasingMode.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
public enum AntiAliasingMode
{
DISABLED("Disabled", 0),
FXAA("FXAA", 0),
MSAA_2("MSAA x2", 2),
MSAA_4("MSAA x4", 4),
MSAA_8("MSAA x8", 8),
Expand Down
3 changes: 3 additions & 0 deletions src/main/java/rs117/hd/opengl/shader/UIShaderProgram.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package rs117.hd.opengl.shader;

import static org.lwjgl.opengl.GL33C.*;
import static rs117.hd.HdPlugin.TEXTURE_UNIT_SCENE_COLOR;
import static rs117.hd.HdPlugin.TEXTURE_UNIT_UI;

public class UIShaderProgram extends ShaderProgram {
private final UniformTexture uniUiTexture = addUniformTexture("uiTexture");
private final UniformTexture uniSceneTexture = addUniformTexture("sceneTexture");

public UIShaderProgram() {
super(t -> t
Expand All @@ -15,5 +17,6 @@ public UIShaderProgram() {
@Override
protected void initialize() {
uniUiTexture.set(TEXTURE_UNIT_UI);
uniSceneTexture.set(TEXTURE_UNIT_SCENE_COLOR);
}
}
1 change: 1 addition & 0 deletions src/main/java/rs117/hd/opengl/uniforms/UBOUI.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ public UBOUI() {
super(GL_DYNAMIC_DRAW);
}

public Property sceneViewport = addProperty(PropertyType.IVec4, "sceneViewport");
public Property sourceDimensions = addProperty(PropertyType.IVec2, "sourceDimensions");
public Property targetDimensions = addProperty(PropertyType.IVec2, "targetDimensions");
public Property alphaOverlay = addProperty(PropertyType.FVec4, "alphaOverlay");
Expand Down
36 changes: 6 additions & 30 deletions src/main/java/rs117/hd/renderer/legacy/LegacyRenderer.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
import rs117.hd.opengl.uniforms.UBOCompute;
import rs117.hd.opengl.uniforms.UBOLights;
import rs117.hd.overlays.FrameTimer;
import rs117.hd.overlays.TiledLightingOverlay;
import rs117.hd.overlays.Timer;
import rs117.hd.renderer.Renderer;
import rs117.hd.scene.AreaManager;
Expand Down Expand Up @@ -136,6 +137,9 @@ public class LegacyRenderer implements Renderer {
@Inject
private ShadowShaderProgram.Legacy shadowProgram;

@Inject
private TiledLightingOverlay tiledLightingOverlay;

@Inject
private JobSystem jobSystem;

Expand Down Expand Up @@ -1209,42 +1213,14 @@ public void draw(int overlayColor) {

frameTimer.end(Timer.RENDER_SCENE);

tiledLightingOverlay.render();

glDisable(GL_BLEND);
glDisable(GL_CULL_FACE);
glDisable(GL_MULTISAMPLE);
glDisable(GL_DEPTH_TEST);
glDepthMask(true);
glUseProgram(0);

glBindFramebuffer(GL_READ_FRAMEBUFFER, plugin.fboScene);
if (plugin.fboSceneResolve != 0) {
// Blit from the scene FBO to the multisample resolve FBO
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, plugin.fboSceneResolve);
glBlitFramebuffer(
0, 0, plugin.sceneResolution[0], plugin.sceneResolution[1],
0, 0, plugin.sceneResolution[0], plugin.sceneResolution[1],
GL_COLOR_BUFFER_BIT, GL_NEAREST
);
glBindFramebuffer(GL_READ_FRAMEBUFFER, plugin.fboSceneResolve);
}

// Blit from the resolved FBO to the default FBO
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, plugin.awtContext.getFramebuffer(false));
glBlitFramebuffer(
0,
0,
plugin.sceneResolution[0],
plugin.sceneResolution[1],
plugin.sceneViewport[0],
plugin.sceneViewport[1],
plugin.sceneViewport[0] + plugin.sceneViewport[2],
plugin.sceneViewport[1] + plugin.sceneViewport[3],
GL_COLOR_BUFFER_BIT,
config.sceneScalingMode().glFilter
);
} else {
glClearColor(0, 0, 0, 1f);
glClear(GL_COLOR_BUFFER_BIT);
}

plugin.drawUi(overlayColor);
Expand Down
Loading