-
-
Notifications
You must be signed in to change notification settings - Fork 636
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
201 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
116 changes: 116 additions & 0 deletions
116
src/main/java/net/coderbot/iris/uisupport/GaussianBlurRenderer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |