Remove the need for full screen texture copy for ssao

This commit is contained in:
mcrcortex
2024-02-23 01:50:03 +10:00
parent 1e793630e7
commit b961705ea3
3 changed files with 43 additions and 44 deletions

View File

@@ -1,6 +1,5 @@
package me.cortex.voxy.client.core.rendering.post;
import com.mojang.blaze3d.systems.RenderSystem;
import me.cortex.voxy.client.core.gl.GlFramebuffer;
import me.cortex.voxy.client.core.gl.GlTexture;
import me.cortex.voxy.client.core.gl.shader.Shader;
@@ -11,30 +10,26 @@ import org.joml.Matrix4f;
import org.lwjgl.opengl.GL11C;
import static org.lwjgl.opengl.ARBComputeShader.glDispatchCompute;
import static org.lwjgl.opengl.ARBCopyImage.glCopyImageSubData;
import static org.lwjgl.opengl.ARBFramebufferObject.*;
import static org.lwjgl.opengl.ARBShaderImageLoadStore.glBindImageTexture;
import static org.lwjgl.opengl.GL11.*;
import static org.lwjgl.opengl.GL13.*;
import static org.lwjgl.opengl.GL15.GL_READ_WRITE;
import static org.lwjgl.opengl.GL15C.GL_READ_ONLY;
import static org.lwjgl.opengl.GL20.glUniformMatrix4fv;
import static org.lwjgl.opengl.GL20C.glGetUniformLocation;
import static org.lwjgl.opengl.GL20C.glGetUniformfv;
import static org.lwjgl.opengl.GL30C.GL_R32F;
import static org.lwjgl.opengl.GL43.GL_DEPTH_STENCIL_TEXTURE_MODE;
import static org.lwjgl.opengl.GL44C.glBindImageTextures;
import static org.lwjgl.opengl.GL45C.glBlitNamedFramebuffer;
import static org.lwjgl.opengl.GL45C.glCopyTextureSubImage2D;
public class PostProcessing {
private final GlFramebuffer framebuffer;
private int width;
private int height;
private GlTexture colour;
private GlTexture colourCopy;
private GlTexture colourSSAO;
private GlTexture depthStencil;
private boolean didSSAO;
private final FullscreenBlit emptyBlit = new FullscreenBlit("voxy:post/noop.frag");
//private final FullscreenBlit blitTexture = new FullscreenBlit("voxy:post/blit_texture_cutout.frag");
private final FullscreenBlit blitTexture = new FullscreenBlit("voxy:post/blit_texture_depth_cutout.frag");
@@ -58,15 +53,15 @@ public class PostProcessing {
this.width = width;
this.height = height;
if (this.colour != null) {
if (this.colourCopy != null) {
this.colourCopy.free();
if (this.colourSSAO != null) {
this.colourSSAO.free();
}
this.colour.free();
this.depthStencil.free();
}
this.colour = new GlTexture().store(GL_RGBA8, 1, width, height);
this.colourCopy = new GlTexture().store(GL_RGBA8, 1, width, height);
this.colourSSAO = new GlTexture().store(GL_RGBA8, 1, width, height);
this.depthStencil = new GlTexture().store(GL_DEPTH24_STENCIL8, 1, width, height);
this.framebuffer.bind(GL_COLOR_ATTACHMENT0, this.colour);
@@ -79,7 +74,7 @@ public class PostProcessing {
public void shutdown() {
this.framebuffer.free();
if (this.colourCopy != null) this.colourCopy.free();
if (this.colourSSAO != null) this.colourSSAO.free();
if (this.colour != null) this.colour.free();
if (this.depthStencil != null) this.depthStencil.free();
this.emptyBlit.delete();
@@ -88,6 +83,7 @@ public class PostProcessing {
}
public void setup(int width, int height, int sourceFB) {
this.didSSAO = false;
this.glStateCapture.capture();
this.setSize(width, height);
@@ -124,9 +120,7 @@ public class PostProcessing {
//Computes ssao on the current framebuffer data and updates it
// this means that translucency wont be effected etc
public void computeSSAO(Matrix4f projection, MatrixStack stack) {
glCopyImageSubData(this.colour.id, GL_TEXTURE_2D, 0, 0, 0, 0,
this.colourCopy.id, GL_TEXTURE_2D, 0, 0, 0, 0,
this.width, this.height, 1);
this.didSSAO = true;
this.ssaoComp.bind();
float[] data = new float[4*4];
@@ -137,13 +131,12 @@ public class PostProcessing {
mat.get(data);
glUniformMatrix4fv(4, false, data);//invMVP
glBindImageTexture(0, this.colour.id, 0, false,0, GL_READ_WRITE, GL_RGBA8);
//glBindImageTexture(1, this.depthStencil.id, 0, false,0, GL_READ_ONLY, GL_R32F);
glBindImageTexture(0, this.colourSSAO.id, 0, false,0, GL_READ_WRITE, GL_RGBA8);
glActiveTexture(GL_TEXTURE1);
GL11C.glBindTexture(GL_TEXTURE_2D, this.depthStencil.id);
glTexParameteri (GL_TEXTURE_2D, GL_DEPTH_STENCIL_TEXTURE_MODE, GL_DEPTH_COMPONENT);
glActiveTexture(GL_TEXTURE2);
GL11C.glBindTexture(GL_TEXTURE_2D, this.colourCopy.id);
GL11C.glBindTexture(GL_TEXTURE_2D, this.colour.id);
glDispatchCompute((this.width+31)/32, (this.height+31)/32, 1);
}
@@ -175,7 +168,7 @@ public class PostProcessing {
GL11C.glBindTexture(GL_TEXTURE_2D, this.depthStencil.id);
glTexParameteri (GL_TEXTURE_2D, GL_DEPTH_STENCIL_TEXTURE_MODE, GL_DEPTH_COMPONENT);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, this.colour.id);
glBindTexture(GL_TEXTURE_2D, this.didSSAO?this.colourSSAO.id:this.colour.id);
glEnable(GL_DEPTH_TEST);
glDepthMask(true);
this.blitTexture.blit();

View File

@@ -106,6 +106,9 @@ public class Serialization {
if (clzName.contains("mixin")) {
continue;//Dont want to load mixins
}
if (clzName.contains("VoxyConfigScreenFactory")) {
continue;//Dont want to modmenu incase it doesnt exist
}
if (clzName.equals(Serialization.class.getName())) {
continue;//Dont want to load ourselves

View File

@@ -2,9 +2,9 @@
layout(local_size_x = 32, local_size_y = 32) in;
layout(binding = 0, rgba8) uniform restrict image2D colourTex;
layout(binding = 0, rgba8) uniform writeonly restrict image2D colourTexOut;
layout(binding = 1) uniform sampler2D depthTex;
layout(binding = 2) uniform sampler2D colourTexCpy;
layout(binding = 2) uniform sampler2D colourTex;
layout(location = 3) uniform mat4 MVP;
layout(location = 4) uniform mat4 invMVP;
@@ -23,7 +23,7 @@ vec4 reDeProject(vec3 pos) {
if (depth == 1.0f) {
return vec4(-1.0f);
}
uint meta = uint(255.0f*texture(colourTexCpy, UV).w);
uint meta = uint(255.0f*texture(colourTex, UV).w);
if ((meta>>6)==0) {
return vec4(-1.0f);
}
@@ -44,35 +44,38 @@ float computeAOAngle(vec3 pos, float testHeight, vec3 normal) {
}
void main() {
ivec2 size = imageSize(colourTex);//TODO: dont use imageSize as it is slow, swap for uniform
vec2 point = vec2(gl_GlobalInvocationID.xy)/size;
float depth = texture(depthTex, point).r;
if (depth == 1.0f || any(lessThanEqual(size, gl_GlobalInvocationID.xy))) {
ivec2 size = imageSize(colourTexOut);//TODO: dont use imageSize as it is slow, swap for uniform
if (any(lessThanEqual(size, gl_GlobalInvocationID.xy))) {
return;
}
vec4 colour = imageLoad(colourTex, ivec2(gl_GlobalInvocationID.xy));
uint metadata = uint(colour.w*255.0f);
uint face = metadata&7u;
uint lod = (metadata>>3)&7u;
bool hasAO = (metadata>>6)!=0;
vec3 pos = rev3d(vec3(point, depth));
vec2 point = vec2(gl_GlobalInvocationID.xy)/size;
float depth = texture(depthTex, point).r;
vec4 ocolour = vec4(0);
if (depth < 1.0f) {
vec4 colour = texture(colourTex, point);
uint metadata = uint(colour.w*255.0f);
uint face = metadata&7u;
uint lod = (metadata>>3)&7u;
bool hasAO = (metadata>>6)!=0;
vec3 pos = rev3d(vec3(point, depth));
//TODO: TODO: only encode the axis, then use then it as as a mask along with pos and multiply by the -sign of everything
vec3 viewNormal = vec3(uint((face>>1)==2), uint((face>>1)==0), uint((face>>1)==1)) * (float(int(face)&1)*2-1);
//vec3 viewNormal = vec3(uint((face>>1)==2), uint((face>>1)==0), uint((face>>1)==1)) * (-sign(pos));
//TODO: TODO: only encode the axis, then use then it as as a mask along with pos and multiply by the -sign of everything
vec3 viewNormal = vec3(uint((face>>1)==2), uint((face>>1)==0), uint((face>>1)==1)) * (float(int(face)&1)*2-1);
//vec3 viewNormal = vec3(uint((face>>1)==2), uint((face>>1)==0), uint((face>>1)==1)) * (-sign(pos));
float d = 0.0;
if (hasAO) {
d = computeAOAngle(pos, 1.0*(1<<lod), viewNormal);//1
if (d<0.1) {
d = 0.0f;
float d = 0.0;
if (hasAO) {
d = computeAOAngle(pos, 1.0*(1<<lod), viewNormal);//1
if (d<0.1) {
d = 0.0f;
}
}
}
vec4 ocolour = colour;
ocolour.xyz *= ((1.0f-d)/3.0f+(2.0f/3.0f));
ocolour.w = 1.0f;
imageStore(colourTex, ivec2(gl_GlobalInvocationID.xy), ocolour);
ocolour = colour;
ocolour.xyz *= ((1.0f-d)/3.0f+(2.0f/3.0f));
ocolour.w = 1.0f;
}
imageStore(colourTexOut, ivec2(gl_GlobalInvocationID.xy), ocolour);
}
//vec4 ocolour = vec4(max(0, d), abs(min(0,d)), 0, 1);
//vec4 ocolour = vec4(repro-(viewNormal/2), 1);