Skip to content

Commit

Permalink
Frost - Part 1
Browse files Browse the repository at this point in the history
  • Loading branch information
IMS212 committed Sep 8, 2023
1 parent b8de6a5 commit 1ae51b3
Show file tree
Hide file tree
Showing 8 changed files with 201 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,8 @@
import com.mojang.blaze3d.platform.GlStateManager;
import net.coderbot.iris.gl.IrisRenderSystem;
import net.coderbot.iris.gl.framebuffer.GlFramebuffer;
import net.coderbot.iris.gl.program.ComputeProgram;
import net.coderbot.iris.gl.program.Program;
import net.coderbot.iris.gl.program.ProgramBuilder;
import net.coderbot.iris.gl.texture.InternalTextureFormat;
import net.coderbot.iris.gl.uniform.UniformUpdateFrequency;
import net.coderbot.iris.postprocess.FullScreenQuadRenderer;
import net.coderbot.iris.shaderpack.StringPair;
Expand All @@ -16,7 +14,6 @@
import org.apache.commons.io.IOUtils;
import org.lwjgl.opengl.GL11C;
import org.lwjgl.opengl.GL30C;
import org.lwjgl.opengl.GL43C;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
Expand Down Expand Up @@ -54,7 +51,7 @@ public void rebuildProgram(int width, int height, ColorSpace colorSpace) {
String vertexSource;
String source;
try {
vertexSource = new String(IOUtils.toByteArray(Objects.requireNonNull(getClass().getResourceAsStream("/colorSpace.vsh"))), StandardCharsets.UTF_8);
vertexSource = new String(IOUtils.toByteArray(Objects.requireNonNull(getClass().getResourceAsStream("/defaultComposite.vsh"))), StandardCharsets.UTF_8);
source = new String(IOUtils.toByteArray(Objects.requireNonNull(getClass().getResourceAsStream("/colorSpace.csh"))), StandardCharsets.UTF_8);
} catch (IOException e) {
throw new RuntimeException(e);
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/net/coderbot/iris/gl/sampler/GlSampler.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@
import org.lwjgl.opengl.GL30C;

public class GlSampler extends GlResource {
public GlSampler(boolean linear, boolean mipmapped, boolean shadow, boolean hardwareShadow) {
public GlSampler(boolean linear, boolean wrap, boolean mipmapped, boolean shadow, boolean hardwareShadow) {
super(IrisRenderSystem.genSampler());

IrisRenderSystem.samplerParameteri(getId(), GL11C.GL_TEXTURE_MIN_FILTER, linear ? GL11C.GL_LINEAR : GL11C.GL_NEAREST);
IrisRenderSystem.samplerParameteri(getId(), GL11C.GL_TEXTURE_MAG_FILTER, linear ? GL11C.GL_LINEAR : GL11C.GL_NEAREST);
IrisRenderSystem.samplerParameteri(getId(), GL11C.GL_TEXTURE_WRAP_S, GL13C.GL_CLAMP_TO_EDGE);
IrisRenderSystem.samplerParameteri(getId(), GL11C.GL_TEXTURE_WRAP_T, GL13C.GL_CLAMP_TO_EDGE);
IrisRenderSystem.samplerParameteri(getId(), GL11C.GL_TEXTURE_WRAP_S, wrap ? GL13C.GL_REPEAT : GL13C.GL_CLAMP_TO_EDGE);
IrisRenderSystem.samplerParameteri(getId(), GL11C.GL_TEXTURE_WRAP_T, wrap ? GL13C.GL_REPEAT : GL13C.GL_CLAMP_TO_EDGE);

if (mipmapped) {
IrisRenderSystem.samplerParameteri(getId(), GL11C.GL_TEXTURE_MIN_FILTER, linear ? GL11C.GL_LINEAR_MIPMAP_LINEAR : GL11C.GL_NEAREST_MIPMAP_NEAREST);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public class ShaderPackScreen extends Screen implements HudHideable {
private MutableComponent developmentComponent;
private MutableComponent updateComponent;

private boolean guiHidden = false;
public boolean guiHidden = false;
private float guiButtonHoverTimer = 0.0f;

public ShaderPackScreen(Screen parent) {
Expand Down Expand Up @@ -220,8 +220,10 @@ protected void init() {

if (inWorld) {
this.shaderPackList.setRenderBackground(false);
this.shaderPackList.setRenderTopAndBottom(false);
if (shaderOptionList != null) {
this.shaderOptionList.setRenderBackground(false);
this.shaderOptionList.setRenderTopAndBottom(false);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import net.coderbot.iris.gl.texture.DepthBufferFormat;
import net.coderbot.iris.gl.texture.TextureType;
import net.coderbot.iris.gui.option.IrisVideoSettings;
import net.coderbot.iris.gui.screen.ShaderPackScreen;
import net.coderbot.iris.helpers.Tri;
import net.coderbot.iris.mixin.GlStateManagerAccessor;
import net.coderbot.iris.mixin.LevelRendererAccessor;
Expand Down Expand Up @@ -80,6 +81,7 @@
import net.coderbot.iris.texture.pbr.PBRTextureHolder;
import net.coderbot.iris.texture.pbr.PBRTextureManager;
import net.coderbot.iris.texture.pbr.PBRType;
import net.coderbot.iris.uisupport.GaussianBlurRenderer;
import net.coderbot.iris.uniforms.CapturedRenderingState;
import net.coderbot.iris.uniforms.CommonUniforms;
import net.coderbot.iris.uniforms.FrameUpdateNotifier;
Expand Down Expand Up @@ -147,6 +149,7 @@ public class NewWorldRenderingPipeline implements WorldRenderingPipeline, CoreWo
private final CenterDepthSampler centerDepthSampler;
private final SodiumTerrainPipeline sodiumTerrainPipeline;
private final ColorSpaceConverter colorSpaceConverter;
private final GaussianBlurRenderer gaussianBlurRenderer;

private final ImmutableSet<Integer> flippedBeforeShadow;
private final ImmutableSet<Integer> flippedAfterPrepare;
Expand Down Expand Up @@ -540,6 +543,8 @@ public void process(int target) {
}
}

gaussianBlurRenderer = new GaussianBlurRenderer(main.width, main.height);

currentColorSpace = IrisVideoSettings.colorSpace;
}

Expand Down Expand Up @@ -948,6 +953,8 @@ public void beginLevelRendering() {
packDirectives.getRenderTargetDirectives());
this.clearPasses = ClearPassCreator.createClearPasses(renderTargets, false,
packDirectives.getRenderTargetDirectives());
gaussianBlurRenderer.rebuildProgram(main.width, main.height);

}

if (changed || IrisVideoSettings.colorSpace != currentColorSpace) {
Expand Down Expand Up @@ -1090,6 +1097,9 @@ public void finalizeLevelRendering() {
centerDepthSampler.sampleCenterDepth();
compositeRenderer.renderAll();
finalPassRenderer.renderFinalPass();
if (Minecraft.getInstance().screen instanceof ShaderPackScreen screen && !screen.guiHidden) {
gaussianBlurRenderer.process(Minecraft.getInstance().getMainRenderTarget().getColorTextureId());
}
colorSpaceConverter.process(Minecraft.getInstance().getMainRenderTarget().getColorTextureId());
}

Expand Down
8 changes: 4 additions & 4 deletions src/main/java/net/coderbot/iris/samplers/IrisSamplers.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,10 @@ private IrisSamplers() {
}

public static void initRenderer() {
SHADOW_SAMPLER_NEAREST = new GlSampler(false, false, true, true);
SHADOW_SAMPLER_LINEAR = new GlSampler(true, false, true, true);
LINEAR_MIPMAP = new GlSampler(true, true, false, false);
NEAREST_MIPMAP = new GlSampler(false, true, false, false);
SHADOW_SAMPLER_NEAREST = new GlSampler(false, false,false, true, true);
SHADOW_SAMPLER_LINEAR = new GlSampler(true, false, false, true, true);
LINEAR_MIPMAP = new GlSampler(true, false,true, false, false);
NEAREST_MIPMAP = new GlSampler(false, false, true, false, false);
}

public static void addRenderTargetSamplers(SamplerHolder samplers, Supplier<ImmutableSet<Integer>> flipped,
Expand Down
116 changes: 116 additions & 0 deletions src/main/java/net/coderbot/iris/uisupport/GaussianBlurRenderer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
package net.coderbot.iris.uisupport;

import com.google.common.collect.ImmutableSet;
import com.mojang.blaze3d.platform.GlStateManager;
import net.coderbot.iris.gl.IrisRenderSystem;
import net.coderbot.iris.gl.framebuffer.GlFramebuffer;
import net.coderbot.iris.gl.program.Program;
import net.coderbot.iris.gl.program.ProgramBuilder;
import net.coderbot.iris.gl.sampler.GlSampler;
import net.coderbot.iris.gl.uniform.UniformUpdateFrequency;
import net.coderbot.iris.postprocess.FullScreenQuadRenderer;
import net.coderbot.iris.shaderpack.StringPair;
import net.coderbot.iris.shaderpack.preprocessor.JcppProcessor;
import net.coderbot.iris.vendored.joml.Matrix4f;
import org.apache.commons.io.IOUtils;
import org.lwjgl.opengl.GL11C;
import org.lwjgl.opengl.GL20C;
import org.lwjgl.opengl.GL30C;
import org.lwjgl.opengl.GL43C;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

public class GaussianBlurRenderer {
private int width;
private int height;
private Program program;
private GlFramebuffer firstFB;
private GlFramebuffer secondFB;
private int swapTexture;

private int target;
private int directionLocation;
private int sizeLocation;
private static GlSampler sampler;

static {
sampler = new GlSampler(true, false, false, false, false);
}

public GaussianBlurRenderer(int width, int height) {
rebuildProgram(width, height);
}

public void rebuildProgram(int width, int height) {
if (program != null) {
program.destroy();
program = null;
firstFB.destroy();
firstFB = null;
secondFB.destroy();
secondFB = null;
GlStateManager._deleteTexture(swapTexture);
swapTexture = 0;
}

this.width = width;
this.height = height;

String vertexSource;
String source;
try {
vertexSource = new String(IOUtils.toByteArray(Objects.requireNonNull(getClass().getResourceAsStream("/defaultComposite.vsh"))), StandardCharsets.UTF_8);
source = new String(IOUtils.toByteArray(Objects.requireNonNull(getClass().getResourceAsStream("/gaussianBlur.fsh"))), StandardCharsets.UTF_8);
} catch (IOException e) {
throw new RuntimeException(e);
}

List<StringPair> defineList = new ArrayList<>();
source = JcppProcessor.glslPreprocessSource(source, defineList);

ProgramBuilder builder = ProgramBuilder.begin("gaussianBlur", vertexSource, null, source, ImmutableSet.of(0));

builder.uniformJomlMatrix(UniformUpdateFrequency.ONCE, "projection", () -> new Matrix4f(2, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, -1, -1, 0, 1));
builder.addExternalSampler(0, "readImage");

swapTexture = GlStateManager._genTexture();
IrisRenderSystem.texImage2D(swapTexture, GL30C.GL_TEXTURE_2D, 0, GL30C.GL_RGBA8, width, height, 0, GL30C.GL_RGBA, GL30C.GL_UNSIGNED_BYTE, null);
IrisRenderSystem.texParameteri(swapTexture, GL20C.GL_TEXTURE_2D, GL20C.GL_TEXTURE_MIN_FILTER, GL20C.GL_LINEAR);
IrisRenderSystem.texParameteri(swapTexture, GL20C.GL_TEXTURE_2D, GL20C.GL_TEXTURE_MAG_FILTER, GL20C.GL_LINEAR);

this.firstFB = new GlFramebuffer();
firstFB.addColorAttachment(0, swapTexture);
this.secondFB = new GlFramebuffer();
secondFB.addColorAttachment(0, target);
this.program = builder.build();

this.directionLocation = GlStateManager._glGetUniformLocation(this.program.getProgramId(), "direction");
this.sizeLocation = GlStateManager._glGetUniformLocation(this.program.getProgramId(), "texSize");
program.use();
IrisRenderSystem.uniform2f(sizeLocation, width, height);
}

public void process(int targetImage) {
if (this.target != targetImage) {
secondFB.addColorAttachment(0, targetImage);
this.target = targetImage;
}
IrisRenderSystem.bindSamplerToUnit(0, sampler.getId());
IrisRenderSystem.bindTextureToUnit(GL43C.GL_TEXTURE_2D, 0, targetImage);
program.use();
firstFB.bind();
IrisRenderSystem.uniform2f(directionLocation, 1.0f, 0.0f);
FullScreenQuadRenderer.INSTANCE.render();
IrisRenderSystem.bindTextureToUnit(GL43C.GL_TEXTURE_2D, 0, swapTexture);

secondFB.bind();
IrisRenderSystem.uniform2f(directionLocation, 0.0f, 1.0f);
FullScreenQuadRenderer.INSTANCE.render();
Program.unbind();
IrisRenderSystem.bindSamplerToUnit(0, 0);
}
}
File renamed without changes.
64 changes: 64 additions & 0 deletions src/main/resources/gaussianBlur.fsh
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
#version 330 core

const int SAMPLE_COUNT = 10;

const float OFFSETS[10] = float[10](
-8.456814493133681,
-6.466941272204228,
-4.477095577950016,
-2.4872697699036146,
-0.4974522695575193,
1.4923608703786542,
3.4821807463742394,
5.472015437203308,
7.461873982996055,
9
);

const float WEIGHTS[10] = float[10](
0.06618541985300355,
0.08970336395182268,
0.11210975911041324,
0.12920061912152314,
0.13730098788873435,
0.13454575120851442,
0.12157824474454274,
0.10130423138377932,
0.07783716371467683,
0.03023445902298974
);


uniform vec2 texSize;

// blurDirection is:
// vec2(1,0) for horizontal pass
// vec2(0,1) for vertical pass
// The sourceTexture to be blurred MUST use linear filtering!
// pixelCoord is in [0..1]
vec4 blur(in sampler2D sourceTexture, vec2 blurDirection, vec2 pixelCoord)
{
vec4 result = vec4(0.0);
for (int i = 0; i < SAMPLE_COUNT; ++i)
{
vec2 offset = blurDirection * OFFSETS[i] / texSize;
float weight = WEIGHTS[i];
result += texture(sourceTexture, pixelCoord + offset) * weight;
}
return result;
}
uniform sampler2D readImage;
uniform vec2 direction;

layout (location = 0) out vec4 color;

void main() {
vec2 size = gl_FragCoord.xy / texSize;
/*if (size.x > 0.5) {
color = texture(readImage, gl_FragCoord.xy / texSize);
return;
}*/

float distance = (size.y > 0.5 ? -distance(1.0, size.y) : -distance(0, size.y)) * 3;
color = blur(readImage, direction, gl_FragCoord.xy / texSize) * mix(vec4(0.6, 0.6, 0.6, 1.0), vec4(0.45, 0.45, 0.45, 1.0), distance);
}

0 comments on commit 1ae51b3

Please sign in to comment.