diff --git a/src/main/java/me/cortex/voxy/client/core/VoxelCore.java b/src/main/java/me/cortex/voxy/client/core/VoxelCore.java index 20fd4aae..7d16f937 100644 --- a/src/main/java/me/cortex/voxy/client/core/VoxelCore.java +++ b/src/main/java/me/cortex/voxy/client/core/VoxelCore.java @@ -180,7 +180,7 @@ public class VoxelCore { this.postProcessing.computeSSAO(projection, matrices); //We can render the translucent directly after as it is the furthest translucent objects - this.renderer.renderFarAwayTranslucent(); + this.renderer.renderFarAwayTranslucent(viewport); this.postProcessing.renderPost(projection, RenderSystem.getProjectionMatrix(), boundFB); diff --git a/src/main/java/me/cortex/voxy/client/core/gl/GlPersistentMappedBuffer.java b/src/main/java/me/cortex/voxy/client/core/gl/GlPersistentMappedBuffer.java index c67093f9..a5f76ee0 100644 --- a/src/main/java/me/cortex/voxy/client/core/gl/GlPersistentMappedBuffer.java +++ b/src/main/java/me/cortex/voxy/client/core/gl/GlPersistentMappedBuffer.java @@ -13,7 +13,7 @@ public class GlPersistentMappedBuffer extends TrackedObject { public GlPersistentMappedBuffer(long size, int flags) { this.id = glCreateBuffers(); this.size = size; - glNamedBufferStorage(this.id, size, GL_CLIENT_STORAGE_BIT|GL_MAP_PERSISTENT_BIT|(flags&(GL_MAP_WRITE_BIT|GL_MAP_READ_BIT))); + glNamedBufferStorage(this.id, size, GL_CLIENT_STORAGE_BIT|GL_MAP_PERSISTENT_BIT|(flags&(GL_MAP_COHERENT_BIT|GL_MAP_WRITE_BIT|GL_MAP_READ_BIT))); this.addr = nglMapNamedBufferRange(this.id, 0, size, flags|GL_MAP_PERSISTENT_BIT); } diff --git a/src/main/java/me/cortex/voxy/client/core/rendering/AbstractFarWorldRenderer.java b/src/main/java/me/cortex/voxy/client/core/rendering/AbstractFarWorldRenderer.java index e949b49d..34ecc679 100644 --- a/src/main/java/me/cortex/voxy/client/core/rendering/AbstractFarWorldRenderer.java +++ b/src/main/java/me/cortex/voxy/client/core/rendering/AbstractFarWorldRenderer.java @@ -39,7 +39,7 @@ import static org.lwjgl.opengl.GL30.*; //Todo: tinker with having the compute shader where each thread is a position to render? maybe idk public abstract class AbstractFarWorldRenderer { - protected final int vao = glGenVertexArrays(); + public static final int STATIC_VAO = glGenVertexArrays(); protected final GlBuffer uniformBuffer; protected final GeometryManager geometry; @@ -65,8 +65,6 @@ public abstract class AbstractFarWorldRenderer { this.models = new ModelManager(16); } - protected abstract void setupVao(); - public void setupRender(Frustum frustum, Camera camera) { this.frustum = frustum.frustumIntersection; @@ -126,7 +124,7 @@ public abstract class AbstractFarWorldRenderer { public abstract void renderFarAwayOpaque(T viewport); - public abstract void renderFarAwayTranslucent(); + public abstract void renderFarAwayTranslucent(T viewport); public void enqueueResult(BuiltSection result) { this.geometry.enqueueResult(result); @@ -145,7 +143,6 @@ public abstract class AbstractFarWorldRenderer { } public void shutdown() { - glDeleteVertexArrays(this.vao); this.models.free(); this.geometry.free(); this.uniformBuffer.free(); diff --git a/src/main/java/me/cortex/voxy/client/core/rendering/Gl46FarWorldRenderer.java b/src/main/java/me/cortex/voxy/client/core/rendering/Gl46FarWorldRenderer.java index e45bc6c0..b585dde9 100644 --- a/src/main/java/me/cortex/voxy/client/core/rendering/Gl46FarWorldRenderer.java +++ b/src/main/java/me/cortex/voxy/client/core/rendering/Gl46FarWorldRenderer.java @@ -29,6 +29,7 @@ import static org.lwjgl.opengl.GL42.*; import static org.lwjgl.opengl.GL42.GL_FRAMEBUFFER_BARRIER_BIT; import static org.lwjgl.opengl.GL43.*; import static org.lwjgl.opengl.GL43.GL_SHADER_STORAGE_BUFFER; +import static org.lwjgl.opengl.GL45C.glBindTextureUnit; import static org.lwjgl.opengl.GL45C.glClearNamedBufferData; public class Gl46FarWorldRenderer extends AbstractFarWorldRenderer { @@ -56,12 +57,9 @@ public class Gl46FarWorldRenderer extends AbstractFarWorldRenderer this.glCommandBuffer = new GlBuffer(maxSections*5L*4 * 6); this.glCommandCountBuffer = new GlBuffer(4*2); glClearNamedBufferData(this.glCommandBuffer.id, GL_R8UI, GL_RED_INTEGER, GL_UNSIGNED_BYTE, new int[1]); - setupVao(); } - @Override - protected void setupVao() { - glBindVertexArray(this.vao); + protected void bindResources(Gl46Viewport viewport) { glBindBuffer(GL_DRAW_INDIRECT_BUFFER, this.glCommandBuffer.id); glBindBuffer(GL_PARAMETER_BUFFER_ARB, this.glCommandCountBuffer.id); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, SharedIndexBuffer.INSTANCE.id()); @@ -70,10 +68,10 @@ public class Gl46FarWorldRenderer extends AbstractFarWorldRenderer glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 2, this.glCommandBuffer.id); glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 3, this.glCommandCountBuffer.id); glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 4, this.geometry.metaId()); + glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 5, viewport.visibilityBuffer.id); glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 6, this.models.getBufferId()); glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 7, this.models.getColourBufferId()); glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 8, this.lightDataBuffer.id);//Lighting LUT - glBindVertexArray(0); } //FIXME: dont do something like this as it breaks multiviewport mods @@ -115,30 +113,26 @@ public class Gl46FarWorldRenderer extends AbstractFarWorldRenderer RenderLayer.getCutoutMipped().startDrawing(); - int oldActiveTexture = glGetInteger(GL_ACTIVE_TEXTURE); //RenderSystem.enableBlend(); //RenderSystem.defaultBlendFunc(); this.updateUniformBuffer(viewport); - UploadStream.INSTANCE.commit(); - - glBindVertexArray(this.vao); - glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 5, viewport.visibilityBuffer.id); + glBindVertexArray(AbstractFarWorldRenderer.STATIC_VAO); //Bind the texture atlas - glActiveTexture(GL_TEXTURE0); - int oldBoundTexture = glGetInteger(GL_TEXTURE_BINDING_2D); glBindSampler(0, this.models.getSamplerId()); - glBindTexture(GL_TEXTURE_2D, this.models.getTextureId()); + glBindTextureUnit(0, this.models.getTextureId()); glClearNamedBufferData(this.glCommandCountBuffer.id, GL_R32UI, GL_RED_INTEGER, GL_UNSIGNED_INT, new int[1]); this.commandGen.bind(); + this.bindResources(viewport); glDispatchCompute((this.geometry.getSectionCount()+127)/128, 1, 1); glMemoryBarrier(GL_COMMAND_BARRIER_BIT | GL_SHADER_STORAGE_BARRIER_BIT | GL_UNIFORM_BARRIER_BIT); this.lodShader.bind(); + this.bindResources(viewport); glDisable(GL_CULL_FACE); //glPointSize(10); glMultiDrawElementsIndirectCountARB(GL_TRIANGLES, GL_UNSIGNED_SHORT, 0, 0, (int) (this.geometry.getSectionCount()*4.4), 0); @@ -159,6 +153,7 @@ public class Gl46FarWorldRenderer extends AbstractFarWorldRenderer glMemoryBarrier(GL_PIXEL_BUFFER_BARRIER_BIT | GL_FRAMEBUFFER_BARRIER_BIT); this.cullShader.bind(); + this.bindResources(viewport); glColorMask(false, false, false, false); glDepthMask(false); @@ -174,15 +169,14 @@ public class Gl46FarWorldRenderer extends AbstractFarWorldRenderer glBindVertexArray(0); glBindSampler(0, 0); - GL11C.glBindTexture(GL_TEXTURE_2D, oldBoundTexture); - glActiveTexture(oldActiveTexture); + glBindTextureUnit(0, 0); RenderLayer.getCutoutMipped().endDrawing(); } @Override - public void renderFarAwayTranslucent() { + public void renderFarAwayTranslucent(Gl46Viewport viewport) { RenderLayer.getTranslucent().startDrawing(); - glBindVertexArray(this.vao); + glBindVertexArray(AbstractFarWorldRenderer.STATIC_VAO); glDisable(GL_CULL_FACE); glEnable(GL_BLEND); @@ -192,11 +186,11 @@ public class Gl46FarWorldRenderer extends AbstractFarWorldRenderer glBindSampler(0, this.models.getSamplerId()); - glActiveTexture(GL_TEXTURE0); - glBindTexture(GL_TEXTURE_2D, this.models.getTextureId()); + glBindTextureUnit(0, this.models.getTextureId()); //RenderSystem.blendFunc(GlStateManager.SrcFactor.ONE, GlStateManager.DstFactor.ONE); this.lodShader.bind(); + this.bindResources(viewport); glDepthMask(false); glMultiDrawElementsIndirectCountARB(GL_TRIANGLES, GL_UNSIGNED_SHORT, 400_000 * 4 * 5, 4, this.geometry.getSectionCount(), 0); @@ -207,6 +201,7 @@ public class Gl46FarWorldRenderer extends AbstractFarWorldRenderer glBindSampler(0, 0); + glBindTextureUnit(0, 0); glDisable(GL_BLEND); RenderLayer.getTranslucent().endDrawing(); diff --git a/src/main/java/me/cortex/voxy/client/core/rendering/util/DownloadStream.java b/src/main/java/me/cortex/voxy/client/core/rendering/util/DownloadStream.java index 61dbd0f1..52d40d75 100644 --- a/src/main/java/me/cortex/voxy/client/core/rendering/util/DownloadStream.java +++ b/src/main/java/me/cortex/voxy/client/core/rendering/util/DownloadStream.java @@ -21,6 +21,7 @@ import static org.lwjgl.opengl.GL42.glMemoryBarrier; import static org.lwjgl.opengl.GL42C.GL_BUFFER_UPDATE_BARRIER_BIT; import static org.lwjgl.opengl.GL43.GL_SHADER_STORAGE_BARRIER_BIT; import static org.lwjgl.opengl.GL44.GL_CLIENT_MAPPED_BUFFER_BARRIER_BIT; +import static org.lwjgl.opengl.GL44.GL_MAP_COHERENT_BIT; public class DownloadStream { public interface DownloadResultConsumer { @@ -36,7 +37,7 @@ public class DownloadStream { private final ArrayList thisFrameDownloadList = new ArrayList<>(); public DownloadStream(long size) { - this.downloadBuffer = new GlPersistentMappedBuffer(size, GL_MAP_READ_BIT); + this.downloadBuffer = new GlPersistentMappedBuffer(size, GL_MAP_READ_BIT|GL_MAP_COHERENT_BIT); this.allocationArena.setLimit(size); } diff --git a/src/main/java/me/cortex/voxy/client/core/rendering/util/UploadStream.java b/src/main/java/me/cortex/voxy/client/core/rendering/util/UploadStream.java index 5c6eaffc..eea96ed8 100644 --- a/src/main/java/me/cortex/voxy/client/core/rendering/util/UploadStream.java +++ b/src/main/java/me/cortex/voxy/client/core/rendering/util/UploadStream.java @@ -19,6 +19,7 @@ import static org.lwjgl.opengl.GL42.glMemoryBarrier; import static org.lwjgl.opengl.GL42C.GL_BUFFER_UPDATE_BARRIER_BIT; import static org.lwjgl.opengl.GL43.GL_SHADER_STORAGE_BARRIER_BIT; import static org.lwjgl.opengl.GL44.GL_CLIENT_MAPPED_BUFFER_BARRIER_BIT; +import static org.lwjgl.opengl.GL44.GL_MAP_COHERENT_BIT; public class UploadStream { private final AllocationArena allocationArena = new AllocationArena(); @@ -27,10 +28,9 @@ public class UploadStream { private final Deque frames = new ArrayDeque<>(); private final LongArrayList thisFrameAllocations = new LongArrayList(); private final Deque uploadList = new ArrayDeque<>(); - private final LongArrayList flushList = new LongArrayList(); public UploadStream(long size) { - this.uploadBuffer = new GlPersistentMappedBuffer(size,GL_MAP_WRITE_BIT|GL_MAP_UNSYNCHRONIZED_BIT|GL_MAP_FLUSH_EXPLICIT_BIT); + this.uploadBuffer = new GlPersistentMappedBuffer(size,GL_MAP_WRITE_BIT|GL_MAP_UNSYNCHRONIZED_BIT|GL_MAP_COHERENT_BIT); this.allocationArena.setLimit(size); } @@ -59,7 +59,7 @@ public class UploadStream { throw new IllegalStateException("Could not allocate memory segment big enough for upload even after force flush"); } } - this.flushList.add(this.caddr); + this.thisFrameAllocations.add(this.caddr); this.offset = size; addr = this.caddr; } else {//Could expand the allocation so just update it @@ -78,17 +78,6 @@ public class UploadStream { public void commit() { - //First flush all the allocations and enqueue them to be freed - { - for (long alloc : flushList) { - glFlushMappedNamedBufferRange(this.uploadBuffer.id, alloc, this.allocationArena.getSize(alloc)); - this.thisFrameAllocations.add(alloc); - } - this.flushList.clear(); - } - glMemoryBarrier(GL_CLIENT_MAPPED_BUFFER_BARRIER_BIT | GL_BUFFER_UPDATE_BARRIER_BIT); - glMemoryBarrier(GL_ALL_BARRIER_BITS); - //Execute all the copies for (var entry : this.uploadList) { glCopyNamedBufferSubData(this.uploadBuffer.id, entry.target.id, entry.uploadOffset, entry.targetOffset, entry.size);