diff --git a/src/main/java/me/cortex/voxelmon/core/DistanceTracker.java b/src/main/java/me/cortex/voxelmon/core/DistanceTracker.java index 3a91968b..bb0fb0c6 100644 --- a/src/main/java/me/cortex/voxelmon/core/DistanceTracker.java +++ b/src/main/java/me/cortex/voxelmon/core/DistanceTracker.java @@ -25,7 +25,7 @@ public class DistanceTracker { this.tracker = tracker; //NOTE: This is in our render distance units, to convert to chunks at lvl 0 multiply by 2 - int DIST = 24; + int DIST = 16;//24; this.rings[0] = new TransitionRing2D(5, DIST, (x,z)->{ if (true) { diff --git a/src/main/java/me/cortex/voxelmon/core/VoxelCore.java b/src/main/java/me/cortex/voxelmon/core/VoxelCore.java index 3c631ef8..0a291e71 100644 --- a/src/main/java/me/cortex/voxelmon/core/VoxelCore.java +++ b/src/main/java/me/cortex/voxelmon/core/VoxelCore.java @@ -1,9 +1,7 @@ package me.cortex.voxelmon.core; -import me.cortex.voxelmon.core.rendering.AbstractFarWorldRenderer; -import me.cortex.voxelmon.core.rendering.Gl46FarWorldRenderer; -import me.cortex.voxelmon.core.rendering.RenderTracker; -import me.cortex.voxelmon.core.rendering.SharedIndexBuffer; +import com.mojang.blaze3d.platform.GlStateManager; +import me.cortex.voxelmon.core.rendering.*; import me.cortex.voxelmon.core.rendering.building.BuiltSectionGeometry; import me.cortex.voxelmon.core.rendering.building.RenderGenerationService; import me.cortex.voxelmon.core.util.DebugUtil; @@ -29,6 +27,9 @@ import org.lwjgl.system.MemoryUtil; import java.io.File; import java.util.*; +import static org.lwjgl.opengl.ARBFramebufferObject.GL_FRAMEBUFFER; +import static org.lwjgl.opengl.ARBFramebufferObject.glBindFramebuffer; + //Core class that ingests new data from sources and updates the required systems //3 primary services: @@ -50,7 +51,9 @@ public class VoxelCore { private final DistanceTracker distanceTracker; private final RenderGenerationService renderGen; private final RenderTracker renderTracker; + private final AbstractFarWorldRenderer renderer; + private final PostProcessing postProcessing; public VoxelCore() { @@ -66,6 +69,8 @@ public class VoxelCore { this.distanceTracker = new DistanceTracker(this.renderTracker, 5); + this.postProcessing = new PostProcessing(); + Runtime.getRuntime().addShutdownHook(new Thread(this::shutdown)); @@ -140,66 +145,21 @@ public class VoxelCore { DebugUtil.setPositionMatrix(matrices); matrices.pop(); - /* - for (int i = 0; i < 5; i++) { - for (int y = 0; y < Math.max(1, 10>>i); y++) { - for (int x = -32; x < 32; x++) { - for (int z = -32; z < 32; z++) { - if (-16 < x && x < 16 && -16 < z && z < 16) { - continue; - } - var sec = this.world.getOrLoadAcquire(i, x, y, z); - this.renderGen.enqueueTask(sec); - sec.release(); - } - } - } - }*/ + int boundFB = GlStateManager.getBoundFramebuffer(); + this.postProcessing.setSize(MinecraftClient.getInstance().getFramebuffer().textureWidth, MinecraftClient.getInstance().getFramebuffer().textureHeight); + this.postProcessing.bindClearFramebuffer(); + + //TODO: FIXME: since we just bound the post processing FB the depth information isnt + // copied over, we must do this manually and also copy it with respect to the + // near/far planes - //DebugRenderUtil.renderAABB(new Box(0,100,0,1,101,1), 0,1,0,0.1f); - //DebugRenderUtil.renderAABB(new Box(1,100,1,2,101,2), 1,0,0,0.1f); - - - /* - int LEVEL = 4; - int SEC_Y = 1>>LEVEL; - int X = 47>>LEVEL; - int Z = 32>>LEVEL; - var section = world.getOrLoadAcquire(LEVEL,X,SEC_Y,Z); - var data = section.copyData(); - int SCALE = 1<>>= 27; - DebugUtil.renderAABB(new Box(x*SCALE + X_OFFSET,y*SCALE+Y_OFFSET,z*SCALE+Z_OFFSET,x*SCALE+SCALE + X_OFFSET,y*SCALE+SCALE+Y_OFFSET,z*SCALE+SCALE+Z_OFFSET), (float) (point&7)/7,(float) ((point>>3)&7)/7,(float) ((point>>6)&7)/7,1f); - } - } - } - } - section.release(); - */ - - - /* - var points = RingUtil.generatingBoundingCorner2D(4); - for (var point : points) { - int x = point>>>16; - int y = point&0xFFFF; - DebugUtil.renderAABB(new Box(x,150,y,x+1,151,y+1), 1,1,0,1); - } - */ - + //TODO: have the renderer also render a bounding full face just like black boarders around lvl 0 + // this is cause the terrain might not exist and so all the caves are visible causing hell for the + // occlusion culler this.renderer.renderFarAwayOpaque(matrices, cameraX, cameraY, cameraZ); + glBindFramebuffer(GL_FRAMEBUFFER, boundFB); + this.postProcessing.renderPost(boundFB); } public void addDebugInfo(List debug) { @@ -220,6 +180,7 @@ public class VoxelCore { public void shutdown() { try {this.renderGen.shutdown();} catch (Exception e) {System.err.println(e);} try {this.renderer.shutdown();} catch (Exception e) {System.err.println(e);} + try {this.postProcessing.shutdown();} catch (Exception e) {System.err.println(e);} try {this.world.shutdown();} catch (Exception e) {System.err.println(e);} } } diff --git a/src/main/java/me/cortex/voxelmon/core/gl/GlFramebuffer.java b/src/main/java/me/cortex/voxelmon/core/gl/GlFramebuffer.java index e956f81d..6caa53b9 100644 --- a/src/main/java/me/cortex/voxelmon/core/gl/GlFramebuffer.java +++ b/src/main/java/me/cortex/voxelmon/core/gl/GlFramebuffer.java @@ -23,6 +23,9 @@ public class GlFramebuffer extends TrackedObject { } public void verify() { - glCheckNamedFramebufferStatus(this.id, GL_FRAMEBUFFER); + int code; + if ((code = glCheckNamedFramebufferStatus(this.id, GL_FRAMEBUFFER)) != GL_FRAMEBUFFER_COMPLETE) { + throw new IllegalStateException("Framebuffer incomplete with error code: " + code); + } } } diff --git a/src/main/java/me/cortex/voxelmon/core/gl/GlTexture.java b/src/main/java/me/cortex/voxelmon/core/gl/GlTexture.java index 5d5b4c6b..177c1688 100644 --- a/src/main/java/me/cortex/voxelmon/core/gl/GlTexture.java +++ b/src/main/java/me/cortex/voxelmon/core/gl/GlTexture.java @@ -10,7 +10,7 @@ import static org.lwjgl.opengl.GL45C.glCreateTextures; import static org.lwjgl.opengl.GL45C.glTextureStorage2D; public class GlTexture extends TrackedObject { - final int id; + public final int id; private final int type; public GlTexture() { this(GL_TEXTURE_2D); @@ -23,7 +23,7 @@ public class GlTexture extends TrackedObject { public GlTexture store(int format, int levels, int width, int height) { if (this.type == GL_TEXTURE_2D) { - glTextureStorage2D(this.id, format, levels, width, height); + glTextureStorage2D(this.id, levels, format, width, height); } else { throw new IllegalStateException("Unknown texture type"); } diff --git a/src/main/java/me/cortex/voxelmon/core/rendering/PostProcessing.java b/src/main/java/me/cortex/voxelmon/core/rendering/PostProcessing.java index a5ed09fd..7ac53723 100644 --- a/src/main/java/me/cortex/voxelmon/core/rendering/PostProcessing.java +++ b/src/main/java/me/cortex/voxelmon/core/rendering/PostProcessing.java @@ -2,9 +2,22 @@ package me.cortex.voxelmon.core.rendering; import me.cortex.voxelmon.core.gl.GlFramebuffer; import me.cortex.voxelmon.core.gl.GlTexture; +import me.cortex.voxelmon.core.gl.shader.Shader; +import me.cortex.voxelmon.core.gl.shader.ShaderType; import static org.lwjgl.opengl.ARBFramebufferObject.*; -import static org.lwjgl.opengl.GL11.GL_RGBA8; +import static org.lwjgl.opengl.ARBShaderImageLoadStore.glBindImageTexture; +import static org.lwjgl.opengl.GL11.*; +import static org.lwjgl.opengl.GL13.*; +import static org.lwjgl.opengl.GL15C.GL_READ_ONLY; +import static org.lwjgl.opengl.GL15C.GL_READ_WRITE; +import static org.lwjgl.opengl.GL30C.GL_R32F; +import static org.lwjgl.opengl.GL42C.GL_SHADER_IMAGE_ACCESS_BARRIER_BIT; +import static org.lwjgl.opengl.GL42C.glMemoryBarrier; +import static org.lwjgl.opengl.GL43C.glDispatchCompute; +import static org.lwjgl.opengl.GL44C.glBindImageTextures; +import static org.lwjgl.opengl.GL45C.glBlitNamedFramebuffer; +import static org.lwjgl.opengl.GL45C.glTextureBarrier; public class PostProcessing { private final GlFramebuffer framebuffer; @@ -13,6 +26,10 @@ public class PostProcessing { private GlTexture colour; private GlTexture depthStencil; + private final Shader ssao = Shader.make() + .add(ShaderType.COMPUTE, "voxelmon:lod/ssao/ssao.comp") + .compile(); + public PostProcessing() { this.framebuffer = new GlFramebuffer(); } @@ -37,12 +54,25 @@ public class PostProcessing { //Bind and clears the post processing frame buffer public void bindClearFramebuffer() { - + glBindFramebuffer(GL_FRAMEBUFFER, this.framebuffer.id); + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT); } //Executes the post processing and emits to whatever framebuffer is currently bound via a blit - public void renderPost() { - + public void renderPost(int outputFb) { + this.ssao.bind(); + glBindImageTexture(0, this.colour.id, 0, false,0, GL_READ_WRITE, GL_RGBA8); + glActiveTexture(GL_TEXTURE1); + glBindTexture(GL_TEXTURE_2D, this.depthStencil.id); + //glDispatchCompute(this.width/32, this.height/32, 1); + glTextureBarrier(); + glBlitNamedFramebuffer(this.framebuffer.id, outputFb, 0,0, this.width, this.height, 0, 0, this.width, this.height, GL_COLOR_BUFFER_BIT, GL_NEAREST); } + public void shutdown() { + this.framebuffer.free(); + if (this.colour != null) this.colour.free(); + if (this.depthStencil != null) this.depthStencil.free(); + this.ssao.free(); + } } diff --git a/src/main/java/me/cortex/voxelmon/core/rendering/RenderTracker.java b/src/main/java/me/cortex/voxelmon/core/rendering/RenderTracker.java index fee78ac1..289f2bb4 100644 --- a/src/main/java/me/cortex/voxelmon/core/rendering/RenderTracker.java +++ b/src/main/java/me/cortex/voxelmon/core/rendering/RenderTracker.java @@ -187,7 +187,7 @@ public class RenderTracker { buildMask |= 1<>6)&1) == 1) { colour *= vec4(0.247, 0.463, 0.894, 1); } + + //Apply face tint + if (face == 0) { + colour.xyz *= vec3(0.75, 0.75, 0.75); + } else if (face != 1) { + colour.xyz *= vec3((float(face-2)/4)*0.25 + 0.75); + } + + //gl_Position = MVP * vec4(vec3(((cornerIdx)&1)+10,10,((cornerIdx>>1)&1)+10),1); //uint i = uint(quad>>32); - uint i = uint(gl_BaseInstance); - i ^= i>>16; - i *= 124128573; - i ^= i>>16; - i *= 4211346123; - i ^= i>>16; - i *= 462312435; - i ^= i>>16; - i *= 542354341; - i ^= i>>16; + //uint i = uint(gl_BaseInstance); + //i ^= i>>16; + //i *= 124128573; + //i ^= i>>16; + //i *= 4211346123; + //i ^= i>>16; + //i *= 462312435; + //i ^= i>>16; + //i *= 542354341; + //i ^= i>>16; //uint i = uint(lodLevel+12)*215387625; //colour *= vec4(vec3(float((uint(i)>>2)&7)/7,float((uint(i)>>5)&7)/7,float((uint(i)>>8)&7)/7)*0.7+0.3,1); diff --git a/src/main/resources/assets/voxelmon/shaders/lod/ssao/ssao.comp b/src/main/resources/assets/voxelmon/shaders/lod/ssao/ssao.comp new file mode 100644 index 00000000..19e758ef --- /dev/null +++ b/src/main/resources/assets/voxelmon/shaders/lod/ssao/ssao.comp @@ -0,0 +1,11 @@ +#version 460 +layout(local_size_x = 32, local_size_y = 32, local_size_x = 1) in; + +layout(binding=0,rgba8) restrict uniform image2D colourTexture; +layout(binding=1) uniform sampler2D depthTexture; +void main() { + float d = texture2D(depthTexture, vec2(gl_GlobalInvocationID.xy)/vec2(1024)).x; + d = 1/d; + d = d/2; + imageStore(colourTexture, ivec2(gl_GlobalInvocationID.xy), vec4(d,d,d,1)); +} \ No newline at end of file