Hacked in translucency rendering without sorting

This commit is contained in:
mcrcortex
2024-01-29 15:39:48 +10:00
parent 4d28a5a8c9
commit c2c622226b
4 changed files with 55 additions and 20 deletions

View File

@@ -133,6 +133,9 @@ public class VoxelCore {
//glBindFramebuffer(GL_FRAMEBUFFER, boundFB); //glBindFramebuffer(GL_FRAMEBUFFER, boundFB);
//this.postProcessing.renderPost(boundFB); //this.postProcessing.renderPost(boundFB);
//We can render the translucent directly after as it is the furthest translucent objects
this.renderer.renderFarAwayTranslucent();
} }
public void addDebugInfo(List<String> debug) { public void addDebugInfo(List<String> debug) {

View File

@@ -84,6 +84,8 @@ public abstract class AbstractFarWorldRenderer {
public abstract void renderFarAwayOpaque(MatrixStack stack, double cx, double cy, double cz); public abstract void renderFarAwayOpaque(MatrixStack stack, double cx, double cy, double cz);
public abstract void renderFarAwayTranslucent();
public void enqueueResult(BuiltSection result) { public void enqueueResult(BuiltSection result) {
this.geometry.enqueueResult(result); this.geometry.enqueueResult(result);
} }

View File

@@ -59,7 +59,7 @@ public class Gl46FarWorldRenderer extends AbstractFarWorldRenderer {
public Gl46FarWorldRenderer(int geometryBuffer, int maxSections) { public Gl46FarWorldRenderer(int geometryBuffer, int maxSections) {
super(geometryBuffer, maxSections); super(geometryBuffer, maxSections);
this.glCommandBuffer = new GlBuffer(maxSections*5L*4); this.glCommandBuffer = new GlBuffer(maxSections*5L*4 * 6);
this.glCommandCountBuffer = new GlBuffer(4*2); this.glCommandCountBuffer = new GlBuffer(4*2);
this.glVisibilityBuffer = new GlBuffer(maxSections*4L); this.glVisibilityBuffer = new GlBuffer(maxSections*4L);
glClearNamedBufferData(this.glCommandBuffer.id, GL_R8UI, GL_RED_INTEGER, GL_UNSIGNED_BYTE, new int[1]); glClearNamedBufferData(this.glCommandBuffer.id, GL_R8UI, GL_RED_INTEGER, GL_UNSIGNED_BYTE, new int[1]);
@@ -84,6 +84,26 @@ public class Gl46FarWorldRenderer extends AbstractFarWorldRenderer {
glBindVertexArray(0); glBindVertexArray(0);
} }
//FIXME: dont do something like this as it breaks multiviewport mods
private int frameId = 0;
private void updateUniformBuffer(MatrixStack stack, double cx, double cy, double cz) {
long ptr = UploadStream.INSTANCE.upload(this.uniformBuffer, 0, this.uniformBuffer.size());
var mat = new Matrix4f(RenderSystem.getProjectionMatrix()).mul(stack.peek().getPositionMatrix());
var innerTranslation = new Vector3f((float) (cx-(this.sx<<5)), (float) (cy-(this.sy<<5)), (float) (cz-(this.sz<<5)));
mat.translate(-innerTranslation.x, -innerTranslation.y, -innerTranslation.z);
mat.getToAddress(ptr); ptr += 4*4*4;
MemoryUtil.memPutInt(ptr, this.sx); ptr += 4;
MemoryUtil.memPutInt(ptr, this.sy); ptr += 4;
MemoryUtil.memPutInt(ptr, this.sz); ptr += 4;
MemoryUtil.memPutInt(ptr, this.geometry.getSectionCount()); ptr += 4;
var planes = ((AccessFrustumIntersection)this.frustum).getPlanes();
for (var plane : planes) {
plane.getToAddress(ptr); ptr += 4*4;
}
innerTranslation.getToAddress(ptr); ptr += 4*3;
MemoryUtil.memPutInt(ptr, this.frameId++); ptr += 4;
}
public void renderFarAwayOpaque(MatrixStack stack, double cx, double cy, double cz) { public void renderFarAwayOpaque(MatrixStack stack, double cx, double cy, double cz) {
if (this.geometry.getSectionCount() == 0) { if (this.geometry.getSectionCount() == 0) {
return; return;
@@ -144,26 +164,33 @@ public class Gl46FarWorldRenderer extends AbstractFarWorldRenderer {
RenderLayer.getCutoutMipped().endDrawing(); RenderLayer.getCutoutMipped().endDrawing();
} }
//FIXME: dont do something like this as it breaks multiviewport mods @Override
private int frameId = 0; public void renderFarAwayTranslucent() {
private void updateUniformBuffer(MatrixStack stack, double cx, double cy, double cz) { RenderLayer.getTranslucent().startDrawing();
long ptr = UploadStream.INSTANCE.upload(this.uniformBuffer, 0, this.uniformBuffer.size()); glBindVertexArray(this.vao);
var mat = new Matrix4f(RenderSystem.getProjectionMatrix()).mul(stack.peek().getPositionMatrix()); glDisable(GL_CULL_FACE);
var innerTranslation = new Vector3f((float) (cx-(this.sx<<5)), (float) (cy-(this.sy<<5)), (float) (cz-(this.sz<<5)));
mat.translate(-innerTranslation.x, -innerTranslation.y, -innerTranslation.z); int oldActiveTexture = glGetInteger(GL_ACTIVE_TEXTURE);
mat.getToAddress(ptr); ptr += 4*4*4; int oldBoundTexture = glGetInteger(GL_TEXTURE_BINDING_2D);
MemoryUtil.memPutInt(ptr, this.sx); ptr += 4;
MemoryUtil.memPutInt(ptr, this.sy); ptr += 4; glBindSampler(0, this.models.getSamplerId());
MemoryUtil.memPutInt(ptr, this.sz); ptr += 4; glActiveTexture(GL_TEXTURE0);
MemoryUtil.memPutInt(ptr, this.geometry.getSectionCount()); ptr += 4; glBindTexture(GL_TEXTURE_2D, this.models.getTextureId());
var planes = ((AccessFrustumIntersection)this.frustum).getPlanes(); this.lodShader.bind();
for (var plane : planes) {
plane.getToAddress(ptr); ptr += 4*4; glMultiDrawElementsIndirectCountARB(GL_TRIANGLES, GL_UNSIGNED_SHORT, 400_000 * 4 * 5, 4, this.geometry.getSectionCount(), 0);
}
innerTranslation.getToAddress(ptr); ptr += 4*3; glEnable(GL_CULL_FACE);
MemoryUtil.memPutInt(ptr, this.frameId++); ptr += 4; glBindVertexArray(0);
glBindSampler(0, 0);
GL11C.glBindTexture(GL_TEXTURE_2D, oldBoundTexture);
glActiveTexture(oldActiveTexture);
RenderLayer.getTranslucent().endDrawing();
} }
@Override @Override
public void shutdown() { public void shutdown() {
super.shutdown(); super.shutdown();

View File

@@ -87,7 +87,10 @@ void main() {
uint count = 0; uint count = 0;
//Translucency //Translucency
count = meta.cntA&0xFFFF; count = meta.cntA&0xFFFF;
if (count != 0) {
uint translucentCommandPtr = atomicAdd(translucentDrawCount, 1) + 400000;//FIXME: dont hardcode this offset
writeCmd(translucentCommandPtr, encodedPos, ptr, count);
}
ptr += count; ptr += count;
//Double sided quads //Double sided quads