From fc1d7b23036972c38b7d7fe4d30f19b51e2e7d6a Mon Sep 17 00:00:00 2001 From: Ven Date: Mon, 7 Jul 2025 15:31:47 +0100 Subject: [PATCH] Allow DefaultChunkRenderer to be more extensible --- .../render/chunk/DefaultChunkRenderer.java | 52 +++++++++++++++---- .../render/chunk/ShaderChunkRenderer.java | 2 +- 2 files changed, 42 insertions(+), 12 deletions(-) diff --git a/common/src/main/java/org/embeddedt/embeddium/impl/render/chunk/DefaultChunkRenderer.java b/common/src/main/java/org/embeddedt/embeddium/impl/render/chunk/DefaultChunkRenderer.java index fc1c8a29e..19effa66c 100644 --- a/common/src/main/java/org/embeddedt/embeddium/impl/render/chunk/DefaultChunkRenderer.java +++ b/common/src/main/java/org/embeddedt/embeddium/impl/render/chunk/DefaultChunkRenderer.java @@ -56,8 +56,35 @@ public abstract class DefaultChunkRenderer extends ShaderChunkRenderer { return buffer; } + protected void configureShaderInterface() { + if (this.activeProgram == null) { + throw new IllegalStateException("No active program"); + } + configureShaderInterface(this.activeProgram.getInterface()); + } + protected abstract void configureShaderInterface(ChunkShaderInterface shader); + protected boolean canRender() { + return this.activeProgram != null; + } + + protected void setMatrices(ChunkRenderMatrices matrices) { + if (this.activeProgram == null) { + throw new IllegalStateException("No active program"); + } + ChunkShaderInterface shader = this.activeProgram.getInterface(); + shader.setProjectionMatrix(matrices.projection()); + shader.setModelViewMatrix(matrices.modelView()); + } + + protected GlPrimitiveType getPrimitiveType() { + if (this.activeProgram == null) { + throw new IllegalStateException("No active program"); + } + return this.activeProgram.getInterface().getPrimitiveType(); + } + @Override public void render(ChunkRenderMatrices matrices, CommandList commandList, @@ -69,26 +96,25 @@ public abstract class DefaultChunkRenderer extends ShaderChunkRenderer { return; } - super.begin(renderPass); + this.begin(renderPass); + // Default implementation will check if an active program exists // If there is no active program, shader compilation probably failed, and we can't render anything. - if (this.activeProgram != null) { + if (this.canRender()) { boolean useBlockFaceCulling = this.useBlockFaceCulling(); GLDebug.pushGroup(770, renderPass.name() + " terrain pass"); - ChunkShaderInterface shader = this.activeProgram.getInterface(); - shader.setProjectionMatrix(matrices.projection()); - shader.setModelViewMatrix(matrices.modelView()); + this.setMatrices(matrices); - var primitiveType = shader.getPrimitiveType(); + var primitiveType = this.getPrimitiveType(); Iterator iterator = renderLists.iterator(renderPass.isReverseOrder()); this.currentRenderPass = renderPass; this.currentVertexFormat = this.renderPassConfiguration.getVertexTypeForPass(this.currentRenderPass).getVertexFormat(); - this.configureShaderInterface(shader); + this.configureShaderInterface(); while (iterator.hasNext()) { ChunkRenderList renderList = iterator.next(); @@ -112,7 +138,7 @@ public abstract class DefaultChunkRenderer extends ShaderChunkRenderer { var tessellation = this.prepareTessellation(commandList, region); - setModelMatrixUniforms(shader, region, camera); + this.setModelMatrixUniforms(region, camera); executeDrawBatch(commandList, tessellation, primitiveType, this.batch); } @@ -122,7 +148,7 @@ public abstract class DefaultChunkRenderer extends ShaderChunkRenderer { GLDebug.popGroup(); } - super.end(renderPass); + this.end(renderPass); } private static void fillCommandBuffer(MultiDrawBatch batch, @@ -258,12 +284,16 @@ public abstract class DefaultChunkRenderer extends ShaderChunkRenderer { return planes; } - private static void setModelMatrixUniforms(ChunkShaderInterface shader, RenderRegion region, CameraTransform camera) { + protected void setModelMatrixUniforms(RenderRegion region, CameraTransform camera) { + if (this.activeProgram == null) { + throw new IllegalStateException("No active program"); + } + float x = getCameraTranslation(region.getOriginX(), camera.intX, camera.fracX); float y = getCameraTranslation(region.getOriginY(), camera.intY, camera.fracY); float z = getCameraTranslation(region.getOriginZ(), camera.intZ, camera.fracZ); - shader.setRegionOffset(x, y, z); + this.activeProgram.getInterface().setRegionOffset(x, y, z); } private static float getCameraTranslation(int chunkBlockPos, int cameraBlockPos, float cameraPos) { diff --git a/common/src/main/java/org/embeddedt/embeddium/impl/render/chunk/ShaderChunkRenderer.java b/common/src/main/java/org/embeddedt/embeddium/impl/render/chunk/ShaderChunkRenderer.java index 4644e8745..d0f37fbcb 100644 --- a/common/src/main/java/org/embeddedt/embeddium/impl/render/chunk/ShaderChunkRenderer.java +++ b/common/src/main/java/org/embeddedt/embeddium/impl/render/chunk/ShaderChunkRenderer.java @@ -27,7 +27,7 @@ public abstract class ShaderChunkRenderer implements ChunkRenderer { protected final RenderDevice device; - protected GlProgram activeProgram; + protected @Nullable GlProgram activeProgram; public ShaderChunkRenderer(RenderDevice device, RenderPassConfiguration renderPassConfiguration) { this.device = device; -- 2.54.1