From 224a5449ed76507cd4d8d2d27cb80c1b49683b37 Mon Sep 17 00:00:00 2001 From: James Seibel Date: Sat, 28 Feb 2026 11:35:09 -0600 Subject: [PATCH] re-Add vanilla fade --- .../common/renderTest/McApplyRenderer.java | 248 ++++++++++++++++++ .../common/renderTest/McCopyRenderer.java | 223 ++++++++++++++++ .../common/renderTest/McFadeRenderer.java | 187 +++++++++++-- .../common/renderTest/McLodRenderer.java | 105 +++----- .../common/renderTest/UniformHandler.java | 26 ++ coreSubProjects | 2 +- 6 files changed, 707 insertions(+), 84 deletions(-) create mode 100644 common/src/main/java/com/seibel/distanthorizons/common/renderTest/McApplyRenderer.java create mode 100644 common/src/main/java/com/seibel/distanthorizons/common/renderTest/McCopyRenderer.java create mode 100644 common/src/main/java/com/seibel/distanthorizons/common/renderTest/UniformHandler.java diff --git a/common/src/main/java/com/seibel/distanthorizons/common/renderTest/McApplyRenderer.java b/common/src/main/java/com/seibel/distanthorizons/common/renderTest/McApplyRenderer.java new file mode 100644 index 000000000..77dc8defe --- /dev/null +++ b/common/src/main/java/com/seibel/distanthorizons/common/renderTest/McApplyRenderer.java @@ -0,0 +1,248 @@ +/* + * This file is part of the Distant Horizons mod + * licensed under the GNU LGPL v3 License. + * + * Copyright (C) 2020 James Seibel + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, version 3. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program. If not, see . + */ + +package com.seibel.distanthorizons.common.renderTest; + +import com.mojang.blaze3d.buffers.GpuBuffer; +import com.mojang.blaze3d.buffers.GpuBufferSlice; +import com.mojang.blaze3d.pipeline.RenderPipeline; +import com.mojang.blaze3d.platform.DepthTestFunction; +import com.mojang.blaze3d.platform.PolygonMode; +import com.mojang.blaze3d.shaders.UniformType; +import com.mojang.blaze3d.systems.CommandEncoder; +import com.mojang.blaze3d.systems.GpuDevice; +import com.mojang.blaze3d.systems.RenderPass; +import com.mojang.blaze3d.systems.RenderSystem; +import com.mojang.blaze3d.textures.*; +import com.mojang.blaze3d.vertex.VertexFormat; +import com.seibel.distanthorizons.core.dependencyInjection.SingletonInjector; +import com.seibel.distanthorizons.core.logging.DhLogger; +import com.seibel.distanthorizons.core.logging.DhLoggerBuilder; +import com.seibel.distanthorizons.core.wrapperInterfaces.minecraft.IMinecraftGLWrapper; +import com.seibel.distanthorizons.core.wrapperInterfaces.minecraft.IMinecraftRenderWrapper; +import com.seibel.distanthorizons.core.wrapperInterfaces.render.IMcTestRenderer; +import net.minecraft.client.Minecraft; +import net.minecraft.resources.Identifier; + +import java.nio.ByteBuffer; +import java.nio.ByteOrder; +import java.util.OptionalDouble; +import java.util.OptionalInt; +import java.util.function.Supplier; + +/** + * TODO ??? + */ +public class McApplyRenderer +{ + public static final DhLogger LOGGER = new DhLoggerBuilder().build(); + + private static final IMinecraftRenderWrapper MC_RENDER = SingletonInjector.INSTANCE.get(IMinecraftRenderWrapper.class); + private static final IMinecraftGLWrapper GLMC = SingletonInjector.INSTANCE.get(IMinecraftGLWrapper.class); + + public static final McApplyRenderer INSTANCE = new McApplyRenderer(); + + private VertexFormat vertexFormat; + private RenderPipeline pipeline; + private boolean init = false; + + private GpuBuffer vboGpuBuffer; + + + //=============// + // constructor // + //=============// + //region + + private McApplyRenderer() + { + + } + + private void tryInit() + { + if (this.init) + { + return; + } + this.init = true; + + + + GpuDevice gpuDevice = RenderSystem.getDevice(); + CommandEncoder commandEncoder = gpuDevice.createCommandEncoder(); + + + this.vertexFormat = VertexFormat.builder() + .add("vPosition", DhVertexFormat.SCREEN_POS) + .build(); + + + RenderPipeline.Builder pipelineBuilder = RenderPipeline.builder(); + { + pipelineBuilder.withCull(false); + pipelineBuilder.withDepthWrite(false); + pipelineBuilder.withDepthTestFunction(DepthTestFunction.NO_DEPTH_TEST); + pipelineBuilder.withColorWrite(true); + pipelineBuilder.withoutBlend(); + pipelineBuilder.withPolygonMode(PolygonMode.FILL); + pipelineBuilder.withLocation(Identifier.parse("distanthorizons:apply_render")); + + pipelineBuilder.withVertexShader(Identifier.fromNamespaceAndPath("distanthorizons", "apply/vert")); + pipelineBuilder.withFragmentShader(Identifier.fromNamespaceAndPath("distanthorizons", "apply/frag")); + + pipelineBuilder.withSampler("uDhColorTexture"); + pipelineBuilder.withSampler("uDhDepthTexture"); + + pipelineBuilder.withVertexFormat(this.vertexFormat, VertexFormat.Mode.TRIANGLE_FAN); + } + this.pipeline = pipelineBuilder.build(); + + + // upload vertex data + { + // vertices for a full-screen quad + float[] vertices = new float[] + { + // PosX,Y, + -1f, -1f, + 1f, -1f, + 1f, 1f, + -1f, 1f, + }; + + + Supplier labelSupplier = () -> "distantHorizons:McApplyRenderer"; + int usage = 8 | 32; // is this just using OpenGL VBO flags?, if so I can't find it, supposedly GlDevice on Mojang's side + int size = vertices.length * Float.BYTES; + this.vboGpuBuffer = gpuDevice.createBuffer(labelSupplier, usage, size); + + { + int offset = 0; + int length = vertices.length * Float.BYTES; + GpuBufferSlice bufferSlice = new GpuBufferSlice(this.vboGpuBuffer, offset, length); + + ByteBuffer byteBuffer = ByteBuffer.allocateDirect(vertices.length * Float.BYTES); + // Fill buffer with vertices. + byteBuffer.order(ByteOrder.nativeOrder()); + byteBuffer.asFloatBuffer().put(vertices); + byteBuffer.rewind(); + + commandEncoder.writeToBuffer(bufferSlice, byteBuffer); + } + } + + } + + //endregion + + + + //========// + // render // + //========// + //region + + public void render() + { + this.tryInit(); + + + if (McLodRenderer.INSTANCE.dhColorTexture == null) + { + return; + } + + if (McLodRenderer.INSTANCE.dhDepthTexture == null) + { + return; + } + + + GpuDevice gpuDevice = RenderSystem.getDevice(); + CommandEncoder commandEncoder = gpuDevice.createCommandEncoder(); + + + + // create a render pass + { + Supplier debugLabelSupplier = () -> "distantHorizons:McApplyRenderer"; + GpuTextureView colorTexture = gpuDevice.createTextureView(Minecraft.getInstance().getMainRenderTarget().getColorTexture()); + OptionalInt optionalClearColorAsInt = OptionalInt.empty(); + GpuTextureView depthTexture = null; + OptionalDouble optionalDepthValueAsDouble = OptionalDouble.empty(); + + try (RenderPass renderPass = commandEncoder.createRenderPass( + debugLabelSupplier, + colorTexture, + optionalClearColorAsInt, + depthTexture, optionalDepthValueAsDouble)) + { + //renderPass.pushDebugGroup(); + //renderPass.popDebugGroup(); + + + // render pass setup + { + // bind color texture + { + GpuTextureView textureView = gpuDevice.createTextureView(McLodRenderer.INSTANCE.dhColorTexture); + GpuSampler gpuSampler = gpuDevice.createSampler( + AddressMode.CLAMP_TO_EDGE, AddressMode.CLAMP_TO_EDGE, // U,V + FilterMode.NEAREST, FilterMode.NEAREST, // minFilter, magFilter + 1, // maxAnisotropy + OptionalDouble.empty() // maxLod + ); + renderPass.bindTexture("uDhColorTexture", textureView, gpuSampler); + } + + // bind depth texture + { + GpuTextureView textureView = gpuDevice.createTextureView(McLodRenderer.INSTANCE.dhDepthTexture); + GpuSampler gpuSampler = gpuDevice.createSampler( + AddressMode.CLAMP_TO_EDGE, AddressMode.CLAMP_TO_EDGE, // U,V + FilterMode.NEAREST, FilterMode.NEAREST, // minFilter, magFilter + 1, // maxAnisotropy + OptionalDouble.empty() // maxLod + ); + renderPass.bindTexture("uDhDepthTexture", textureView, gpuSampler); + } + + // bind VBO + renderPass.setVertexBuffer(0, this.vboGpuBuffer); // vertex buffer can only be "0" lol + + // set pipeline + renderPass.setPipeline(this.pipeline); + } + + // draw render pass + { + int indexStart = 0; + int indexCount = 4; + renderPass.draw(indexStart, indexCount); + } + } + } + } + + //endregion + + + +} diff --git a/common/src/main/java/com/seibel/distanthorizons/common/renderTest/McCopyRenderer.java b/common/src/main/java/com/seibel/distanthorizons/common/renderTest/McCopyRenderer.java new file mode 100644 index 000000000..1dd4242a4 --- /dev/null +++ b/common/src/main/java/com/seibel/distanthorizons/common/renderTest/McCopyRenderer.java @@ -0,0 +1,223 @@ +/* + * This file is part of the Distant Horizons mod + * licensed under the GNU LGPL v3 License. + * + * Copyright (C) 2020 James Seibel + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, version 3. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program. If not, see . + */ + +package com.seibel.distanthorizons.common.renderTest; + +import com.mojang.blaze3d.buffers.GpuBuffer; +import com.mojang.blaze3d.buffers.GpuBufferSlice; +import com.mojang.blaze3d.pipeline.RenderPipeline; +import com.mojang.blaze3d.platform.DepthTestFunction; +import com.mojang.blaze3d.platform.PolygonMode; +import com.mojang.blaze3d.systems.CommandEncoder; +import com.mojang.blaze3d.systems.GpuDevice; +import com.mojang.blaze3d.systems.RenderPass; +import com.mojang.blaze3d.systems.RenderSystem; +import com.mojang.blaze3d.textures.*; +import com.mojang.blaze3d.vertex.VertexFormat; +import com.seibel.distanthorizons.core.dependencyInjection.SingletonInjector; +import com.seibel.distanthorizons.core.logging.DhLogger; +import com.seibel.distanthorizons.core.logging.DhLoggerBuilder; +import com.seibel.distanthorizons.core.wrapperInterfaces.minecraft.IMinecraftGLWrapper; +import com.seibel.distanthorizons.core.wrapperInterfaces.minecraft.IMinecraftRenderWrapper; +import net.minecraft.client.Minecraft; +import net.minecraft.resources.Identifier; + +import java.nio.ByteBuffer; +import java.nio.ByteOrder; +import java.util.OptionalDouble; +import java.util.OptionalInt; +import java.util.function.Supplier; + +/** + * TODO ??? + */ +public class McCopyRenderer +{ + public static final DhLogger LOGGER = new DhLoggerBuilder().build(); + + private static final IMinecraftRenderWrapper MC_RENDER = SingletonInjector.INSTANCE.get(IMinecraftRenderWrapper.class); + private static final IMinecraftGLWrapper GLMC = SingletonInjector.INSTANCE.get(IMinecraftGLWrapper.class); + + public static final McCopyRenderer INSTANCE = new McCopyRenderer(); + + private VertexFormat vertexFormat; + private RenderPipeline pipeline; + private boolean init = false; + + private GpuBuffer vboGpuBuffer; + + + //=============// + // constructor // + //=============// + //region + + private McCopyRenderer() + { + + } + + private void tryInit() + { + if (this.init) + { + return; + } + this.init = true; + + + + GpuDevice gpuDevice = RenderSystem.getDevice(); + CommandEncoder commandEncoder = gpuDevice.createCommandEncoder(); + + + this.vertexFormat = VertexFormat.builder() + .add("vPosition", DhVertexFormat.SCREEN_POS) + .build(); + + + RenderPipeline.Builder pipelineBuilder = RenderPipeline.builder(); + { + pipelineBuilder.withCull(false); + pipelineBuilder.withDepthWrite(false); + pipelineBuilder.withDepthTestFunction(DepthTestFunction.NO_DEPTH_TEST); + pipelineBuilder.withColorWrite(true); + pipelineBuilder.withoutBlend(); + pipelineBuilder.withPolygonMode(PolygonMode.FILL); + pipelineBuilder.withLocation(Identifier.parse("distanthorizons:copy_render")); + + pipelineBuilder.withVertexShader(Identifier.fromNamespaceAndPath("distanthorizons", "copy/vert")); + pipelineBuilder.withFragmentShader(Identifier.fromNamespaceAndPath("distanthorizons", "copy/frag")); + + pipelineBuilder.withSampler("uCopyTexture"); + + pipelineBuilder.withVertexFormat(this.vertexFormat, VertexFormat.Mode.TRIANGLE_FAN); + } + this.pipeline = pipelineBuilder.build(); + + + // upload vertex data + { + // vertices for a full-screen quad + float[] vertices = new float[] + { + // PosX,Y, + -1f, -1f, + 1f, -1f, + 1f, 1f, + -1f, 1f, + }; + + + Supplier labelSupplier = () -> "distantHorizons:McCopyRenderer"; + int usage = 8 | 32; // is this just using OpenGL VBO flags?, if so I can't find it, supposedly GlDevice on Mojang's side + int size = vertices.length * Float.BYTES; + this.vboGpuBuffer = gpuDevice.createBuffer(labelSupplier, usage, size); + + { + int offset = 0; + int length = vertices.length * Float.BYTES; + GpuBufferSlice bufferSlice = new GpuBufferSlice(this.vboGpuBuffer, offset, length); + + ByteBuffer byteBuffer = ByteBuffer.allocateDirect(vertices.length * Float.BYTES); + // Fill buffer with vertices. + byteBuffer.order(ByteOrder.nativeOrder()); + byteBuffer.asFloatBuffer().put(vertices); + byteBuffer.rewind(); + + commandEncoder.writeToBuffer(bufferSlice, byteBuffer); + } + } + + } + + //endregion + + + + //========// + // render // + //========// + //region + + public void render(GpuTexture sourceTexture, GpuTexture destinationTexture) + { + this.tryInit(); + + + + GpuDevice gpuDevice = RenderSystem.getDevice(); + CommandEncoder commandEncoder = gpuDevice.createCommandEncoder(); + + + + // create a render pass + { + Supplier debugLabelSupplier = () -> "distantHorizons:McCopyRenderer"; + GpuTextureView colorTexture = gpuDevice.createTextureView(destinationTexture); + OptionalInt optionalClearColorAsInt = OptionalInt.empty(); + GpuTextureView depthTexture = null; + OptionalDouble optionalDepthValueAsDouble = OptionalDouble.empty(); + + try (RenderPass renderPass = commandEncoder.createRenderPass( + debugLabelSupplier, + colorTexture, + optionalClearColorAsInt, + depthTexture, optionalDepthValueAsDouble)) + { + //renderPass.pushDebugGroup(); + //renderPass.popDebugGroup(); + + + // render pass setup + { + // bind color texture + { + GpuTextureView textureView = gpuDevice.createTextureView(sourceTexture); + GpuSampler gpuSampler = gpuDevice.createSampler( + AddressMode.CLAMP_TO_EDGE, AddressMode.CLAMP_TO_EDGE, // U,V + FilterMode.NEAREST, FilterMode.NEAREST, // minFilter, magFilter + 1, // maxAnisotropy + OptionalDouble.empty() // maxLod + ); + renderPass.bindTexture("uCopyTexture", textureView, gpuSampler); + } + + // bind VBO + renderPass.setVertexBuffer(0, this.vboGpuBuffer); // vertex buffer can only be "0" lol + + // set pipeline + renderPass.setPipeline(this.pipeline); + } + + // draw render pass + { + int indexStart = 0; + int indexCount = 4; + renderPass.draw(indexStart, indexCount); + } + } + } + } + + //endregion + + + +} diff --git a/common/src/main/java/com/seibel/distanthorizons/common/renderTest/McFadeRenderer.java b/common/src/main/java/com/seibel/distanthorizons/common/renderTest/McFadeRenderer.java index 1a1c6d07e..119b99cb3 100644 --- a/common/src/main/java/com/seibel/distanthorizons/common/renderTest/McFadeRenderer.java +++ b/common/src/main/java/com/seibel/distanthorizons/common/renderTest/McFadeRenderer.java @@ -21,23 +21,29 @@ package com.seibel.distanthorizons.common.renderTest; import com.mojang.blaze3d.buffers.GpuBuffer; import com.mojang.blaze3d.buffers.GpuBufferSlice; +import com.mojang.blaze3d.buffers.Std140Builder; +import com.mojang.blaze3d.buffers.Std140SizeCalculator; import com.mojang.blaze3d.pipeline.RenderPipeline; import com.mojang.blaze3d.platform.DepthTestFunction; import com.mojang.blaze3d.platform.PolygonMode; +import com.mojang.blaze3d.shaders.UniformType; import com.mojang.blaze3d.systems.CommandEncoder; import com.mojang.blaze3d.systems.GpuDevice; import com.mojang.blaze3d.systems.RenderPass; import com.mojang.blaze3d.systems.RenderSystem; import com.mojang.blaze3d.textures.*; import com.mojang.blaze3d.vertex.VertexFormat; -import com.mojang.blaze3d.vertex.VertexFormatElement; +import com.seibel.distanthorizons.api.methods.events.sharedParameterObjects.DhApiRenderParam; +import com.seibel.distanthorizons.core.config.Config; import com.seibel.distanthorizons.core.dependencyInjection.SingletonInjector; import com.seibel.distanthorizons.core.logging.DhLogger; import com.seibel.distanthorizons.core.logging.DhLoggerBuilder; +import com.seibel.distanthorizons.core.util.RenderUtil; +import com.seibel.distanthorizons.core.util.math.Mat4f; import com.seibel.distanthorizons.core.wrapperInterfaces.minecraft.IMinecraftGLWrapper; import com.seibel.distanthorizons.core.wrapperInterfaces.minecraft.IMinecraftRenderWrapper; import com.seibel.distanthorizons.core.wrapperInterfaces.render.IMcFadeRenderer; -import com.seibel.distanthorizons.core.wrapperInterfaces.render.IMcTestRenderer; +import com.seibel.distanthorizons.core.wrapperInterfaces.world.IClientLevelWrapper; import net.minecraft.client.Minecraft; import net.minecraft.resources.Identifier; @@ -63,8 +69,12 @@ public class McFadeRenderer implements IMcFadeRenderer private RenderPipeline pipeline; private boolean init = false; + private GpuBuffer fragUniformBuffer; + private GpuBuffer vboGpuBuffer; + public GpuTexture fadeColorTexture; + //=============// @@ -74,7 +84,9 @@ public class McFadeRenderer implements IMcFadeRenderer private McFadeRenderer() { - + this.vertexFormat = VertexFormat.builder() + .add("vPosition", DhVertexFormat.SCREEN_POS) + .build(); } private void tryInit() @@ -91,10 +103,6 @@ public class McFadeRenderer implements IMcFadeRenderer CommandEncoder commandEncoder = gpuDevice.createCommandEncoder(); - this.vertexFormat = VertexFormat.builder() - .add("vPosition", DhVertexFormat.SCREEN_POS) - .build(); - RenderPipeline.Builder pipelineBuilder = RenderPipeline.builder(); { @@ -107,9 +115,15 @@ public class McFadeRenderer implements IMcFadeRenderer pipelineBuilder.withLocation(Identifier.parse("distanthorizons:test_render")); pipelineBuilder.withVertexShader(Identifier.fromNamespaceAndPath("distanthorizons", "fade/vert")); - pipelineBuilder.withFragmentShader(Identifier.fromNamespaceAndPath("distanthorizons", "fade/frag")); + pipelineBuilder.withFragmentShader(Identifier.fromNamespaceAndPath("distanthorizons", "fade/vanilla_fade")); + + pipelineBuilder.withSampler("uMcDepthTexture"); + pipelineBuilder.withSampler("uCombinedMcDhColorTexture"); pipelineBuilder.withSampler("uDhDepthTexture"); + pipelineBuilder.withSampler("uDhColorTexture"); + + pipelineBuilder.withUniform("fragUniformBlock", UniformType.UNIFORM_BUFFER); pipelineBuilder.withVertexFormat(this.vertexFormat, VertexFormat.Mode.TRIANGLE_FAN); } @@ -160,22 +174,121 @@ public class McFadeRenderer implements IMcFadeRenderer //========// //region - public void render() + @Override + public void render(Mat4f mcModelViewMatrix, Mat4f mcProjectionMatrix, IClientLevelWrapper level) { this.tryInit(); + if (McLodRenderer.INSTANCE.dhDepthTexture == null + || McLodRenderer.INSTANCE.dhColorTexture == null) + { + return; + } + GpuDevice gpuDevice = RenderSystem.getDevice(); CommandEncoder commandEncoder = gpuDevice.createCommandEncoder(); + // textures + if (this.fadeColorTexture == null + || this.fadeColorTexture.getWidth(0) != MC_RENDER.getTargetFramebufferViewportWidth() + || this.fadeColorTexture.getHeight(0) != MC_RENDER.getTargetFramebufferViewportHeight()) + { + if (this.fadeColorTexture != null) + { + this.fadeColorTexture.close(); + } + + // TODO USAGE_TEXTURE_BINDING = 4 + int usage = 4 | 8 | 32 | 128; + this.fadeColorTexture = gpuDevice.createTexture("FadeColorTexture", + usage, + TextureFormat.RGBA8, + MC_RENDER.getTargetFramebufferViewportWidth(), MC_RENDER.getTargetFramebufferViewportHeight(), + 1, 1 + ); + } + + + { + int uniformBufferSize = new Std140SizeCalculator() + .putInt() // uOnlyRenderLods + .putFloat() // uStartFadeBlockDistance + .putFloat() // uEndFadeBlockDistance + .putFloat() // uMaxLevelHeight + .putMat4f() // uDhInvMvmProj + .putMat4f() // uMcInvMvmProj + .get(); + + + // create data // + + float dhNearClipDistance = RenderUtil.getNearClipPlaneInBlocks(); + // this added value prevents the near clip plane and discard circle from touching, which looks bad + dhNearClipDistance += 16f; + + // measured in blocks + // these multipliers in James' tests should provide a fairly smooth transition + // without having underdraw issues + float fadeStartDistance = dhNearClipDistance * 1.5f; + float fadeEndDistance = dhNearClipDistance * 1.9f; + + + Mat4f inverseMcModelViewProjectionMatrix = new Mat4f(mcProjectionMatrix); + inverseMcModelViewProjectionMatrix.multiply(mcModelViewMatrix); + inverseMcModelViewProjectionMatrix.invert(); + Mat4f inverseMcMvmProjMatrix = inverseMcModelViewProjectionMatrix; + + + Mat4f dhProjectionMatrix = RenderUtil.createLodProjectionMatrix(mcProjectionMatrix); + Mat4f dhModelViewMatrix = RenderUtil.createLodModelViewMatrix(mcModelViewMatrix); + + Mat4f inverseDhModelViewProjectionMatrix = new Mat4f(dhProjectionMatrix); + inverseDhModelViewProjectionMatrix.multiply(dhModelViewMatrix); + inverseDhModelViewProjectionMatrix.invert(); + Mat4f inverseDhMvmProjMatrix = inverseDhModelViewProjectionMatrix; + + + + // upload data // + + ByteBuffer buffer = ByteBuffer.allocateDirect(uniformBufferSize); + buffer.order(ByteOrder.LITTLE_ENDIAN); + buffer = Std140Builder.intoBuffer(buffer) + .putInt(Config.Client.Advanced.Debugging.lodOnlyMode.get() ? 1 : 0) // uOnlyRenderLods + .putFloat(fadeStartDistance) // uStartFadeBlockDistance + .putFloat(fadeEndDistance) // uEndFadeBlockDistance + .putFloat(level.getMaxHeight()) // uMaxLevelHeight + .putMat4f(inverseDhMvmProjMatrix.createJomlMatrix()) // uDhInvMvmProj + .putMat4f(inverseMcMvmProjMatrix.createJomlMatrix()) // uMcInvMvmProj + .get() + ; + + this.fragUniformBuffer = UniformHandler.createBuffer("fragUniformBlock", uniformBufferSize, this.fragUniformBuffer); + GpuBufferSlice bufferSlice = new GpuBufferSlice(this.fragUniformBuffer, 0, uniformBufferSize); + + commandEncoder.writeToBuffer(bufferSlice, buffer); + } + + + this.renderFadeToTexture(); + McCopyRenderer.INSTANCE.render(this.fadeColorTexture, Minecraft.getInstance().getMainRenderTarget().getColorTexture()); + + } + + private void renderFadeToTexture() + { + GpuDevice gpuDevice = RenderSystem.getDevice(); + CommandEncoder commandEncoder = gpuDevice.createCommandEncoder(); + // create a render pass Supplier debugLabelSupplier = () -> "distantHorizons:McFadeRenderer"; - GpuTextureView colorTexture = gpuDevice.createTextureView(Minecraft.getInstance().getMainRenderTarget().getColorTexture()); + GpuTextureView colorTexture = gpuDevice.createTextureView(this.fadeColorTexture); OptionalInt optionalClearColorAsInt = OptionalInt.empty(); - GpuTextureView depthTexture = null;//gpuDevice.createTextureView(Minecraft.getInstance().getMainRenderTarget().getDepthTexture()); + GpuTextureView depthTexture = null; OptionalDouble optionalDepthValueAsDouble = OptionalDouble.empty(); try (RenderPass renderPass = commandEncoder.createRenderPass( @@ -192,9 +305,32 @@ public class McFadeRenderer implements IMcFadeRenderer { // bind MC depth texture { - GpuTexture bindDepthTexture = Minecraft.getInstance().getMainRenderTarget().getDepthTexture(); - - GpuTextureView textureView = gpuDevice.createTextureView(bindDepthTexture); + GpuTextureView textureView = gpuDevice.createTextureView(Minecraft.getInstance().getMainRenderTarget().getDepthTexture()); + GpuSampler gpuSampler = gpuDevice.createSampler( + AddressMode.CLAMP_TO_EDGE, AddressMode.CLAMP_TO_EDGE, // U,V + FilterMode.NEAREST, FilterMode.NEAREST, // minFilter, magFilter + 1, // maxAnisotropy + OptionalDouble.empty() // maxLod + ); + renderPass.bindTexture("uMcDepthTexture", textureView, gpuSampler); + } + + // bind MC color texture + { + GpuTextureView textureView = gpuDevice.createTextureView(Minecraft.getInstance().getMainRenderTarget().getColorTexture()); + GpuSampler gpuSampler = gpuDevice.createSampler( + AddressMode.CLAMP_TO_EDGE, AddressMode.CLAMP_TO_EDGE, // U,V + FilterMode.NEAREST, FilterMode.NEAREST, // minFilter, magFilter + 1, // maxAnisotropy + OptionalDouble.empty() // maxLod + ); + renderPass.bindTexture("uCombinedMcDhColorTexture", textureView, gpuSampler); + } + + + // bind DH depth texture + { + GpuTextureView textureView = gpuDevice.createTextureView(McLodRenderer.INSTANCE.dhDepthTexture); GpuSampler gpuSampler = gpuDevice.createSampler( AddressMode.CLAMP_TO_EDGE, AddressMode.CLAMP_TO_EDGE, // U,V FilterMode.NEAREST, FilterMode.NEAREST, // minFilter, magFilter @@ -204,15 +340,27 @@ public class McFadeRenderer implements IMcFadeRenderer renderPass.bindTexture("uDhDepthTexture", textureView, gpuSampler); } - // bind VBO + // bind DH color texture { - renderPass.setVertexBuffer(0, this.vboGpuBuffer); // vertex buffer can only be "0" lol + GpuTextureView textureView = gpuDevice.createTextureView(McLodRenderer.INSTANCE.dhColorTexture); + GpuSampler gpuSampler = gpuDevice.createSampler( + AddressMode.CLAMP_TO_EDGE, AddressMode.CLAMP_TO_EDGE, // U,V + FilterMode.NEAREST, FilterMode.NEAREST, // minFilter, magFilter + 1, // maxAnisotropy + OptionalDouble.empty() // maxLod + ); + renderPass.bindTexture("uDhColorTexture", textureView, gpuSampler); } + + + renderPass.setUniform("fragUniformBlock", this.fragUniformBuffer); + + // bind VBO + renderPass.setVertexBuffer(0, this.vboGpuBuffer); // vertex buffer can only be "0" lol + // set pipeline - { - renderPass.setPipeline(this.pipeline); - } + renderPass.setPipeline(this.pipeline); } // draw render pass @@ -224,6 +372,7 @@ public class McFadeRenderer implements IMcFadeRenderer } } + //endregion diff --git a/common/src/main/java/com/seibel/distanthorizons/common/renderTest/McLodRenderer.java b/common/src/main/java/com/seibel/distanthorizons/common/renderTest/McLodRenderer.java index 414a27cc8..7aecbcb2b 100644 --- a/common/src/main/java/com/seibel/distanthorizons/common/renderTest/McLodRenderer.java +++ b/common/src/main/java/com/seibel/distanthorizons/common/renderTest/McLodRenderer.java @@ -16,7 +16,6 @@ import com.mojang.blaze3d.systems.RenderPass; import com.mojang.blaze3d.systems.RenderSystem; import com.mojang.blaze3d.textures.*; import com.mojang.blaze3d.vertex.VertexFormat; -import com.mojang.blaze3d.vertex.VertexFormatElement; import com.seibel.distanthorizons.api.methods.events.sharedParameterObjects.DhApiRenderParam; import com.seibel.distanthorizons.api.objects.math.DhApiVec3f; import com.seibel.distanthorizons.core.config.Config; @@ -26,6 +25,7 @@ import com.seibel.distanthorizons.core.logging.DhLogger; import com.seibel.distanthorizons.core.logging.DhLoggerBuilder; import com.seibel.distanthorizons.core.render.glObject.GLEnums; import com.seibel.distanthorizons.core.render.glObject.buffer.QuadElementBuffer; +import com.seibel.distanthorizons.core.util.ColorUtil; import com.seibel.distanthorizons.core.util.RenderUtil; import com.seibel.distanthorizons.core.util.math.Mat4f; import com.seibel.distanthorizons.core.wrapperInterfaces.minecraft.IMinecraftGLWrapper; @@ -40,11 +40,8 @@ import org.lwjgl.system.MemoryUtil; import java.nio.ByteBuffer; import java.nio.ByteOrder; -import java.nio.FloatBuffer; -import java.nio.IntBuffer; import java.util.OptionalDouble; import java.util.OptionalInt; -import java.util.function.Consumer; import java.util.function.Supplier; /** @@ -69,7 +66,8 @@ public class McLodRenderer implements IMcLodRenderer private GpuBuffer fragUniformBuffer; private GpuBuffer vertUniformBuffer; - private GpuTexture depthTexture; + public GpuTexture dhDepthTexture; + public GpuTexture dhColorTexture; @@ -102,11 +100,6 @@ public class McLodRenderer implements IMcLodRenderer - //GLMC.glBlendFunc(GL32.GL_SRC_ALPHA, GL32.GL_ONE_MINUS_SRC_ALPHA); - //GLMC.glBlendFuncSeparate(GL32.GL_SRC_ALPHA, GL32.GL_ONE_MINUS_SRC_ALPHA, GL32.GL_ONE, GL32.GL_ZERO); - - - GpuDevice gpuDevice = RenderSystem.getDevice(); CommandEncoder commandEncoder = gpuDevice.createCommandEncoder(); @@ -147,22 +140,6 @@ public class McLodRenderer implements IMcLodRenderer } - private static GpuBuffer createUniformBuffer(String uniformName, int size, GpuBuffer vboGpuBuffer) - { - GpuDevice gpuDevice = RenderSystem.getDevice(); - - // create VBO if needed - if (vboGpuBuffer == null - || vboGpuBuffer.size() < size) - { - // GpuBuffer.USAGE_UNIFORM = 128 - int usage = 8 | 32 | 128; // is this just using OpenGL VBO flags?, if so I can't find it, supposedly GlDevice on Mojang's side - vboGpuBuffer = gpuDevice.createBuffer(() -> uniformName, usage, size); - } - - return vboGpuBuffer; - } - //endregion @Override @@ -239,7 +216,7 @@ public class McLodRenderer implements IMcLodRenderer .putMat4f(combinedMatrix.createJomlMatrix()) // uCombinedMatrix .get(); - this.vertUniformBuffer = createUniformBuffer("vertUniformBlock", uniformBufferSize, this.vertUniformBuffer); + this.vertUniformBuffer = UniformHandler.createBuffer("vertUniformBlock", uniformBufferSize, this.vertUniformBuffer); GpuBufferSlice bufferSlice = new GpuBufferSlice(this.vertUniformBuffer, 0, uniformBufferSize); commandEncoder.writeToBuffer(bufferSlice, buffer); @@ -283,7 +260,7 @@ public class McLodRenderer implements IMcLodRenderer .get() ; - this.fragUniformBuffer = createUniformBuffer("fragUniformBlock", uniformBufferSize, this.fragUniformBuffer); + this.fragUniformBuffer = UniformHandler.createBuffer("fragUniformBlock", uniformBufferSize, this.fragUniformBuffer); GpuBufferSlice bufferSlice = new GpuBufferSlice(this.fragUniformBuffer, 0, uniformBufferSize); commandEncoder.writeToBuffer(bufferSlice, buffer); @@ -313,23 +290,31 @@ public class McLodRenderer implements IMcLodRenderer } } - // depth texture - if (this.depthTexture == null - || this.depthTexture.getWidth(0) != MC_RENDER.getTargetFramebufferViewportWidth() - || this.depthTexture.getHeight(0) != MC_RENDER.getTargetFramebufferViewportHeight()) + // textures + if (this.dhDepthTexture == null + || this.dhDepthTexture.getWidth(0) != MC_RENDER.getTargetFramebufferViewportWidth() + || this.dhDepthTexture.getHeight(0) != MC_RENDER.getTargetFramebufferViewportHeight()) { - if (this.depthTexture != null) + if (this.dhDepthTexture != null) { - this.depthTexture.close(); + this.dhDepthTexture.close(); + this.dhColorTexture.close(); } - int usage = 8 | 32 | 128; - this.depthTexture = gpuDevice.createTexture("DhDepthTexture", + // TODO USAGE_TEXTURE_BINDING = 4 + int usage = 4 | 8 | 32 | 128; + this.dhDepthTexture = gpuDevice.createTexture("DhDepthTexture", usage, TextureFormat.DEPTH32, MC_RENDER.getTargetFramebufferViewportWidth(), MC_RENDER.getTargetFramebufferViewportHeight(), 1, 1 ); + this.dhColorTexture = gpuDevice.createTexture("DhColorTexture", + usage, + TextureFormat.RGBA8, + MC_RENDER.getTargetFramebufferViewportWidth(), MC_RENDER.getTargetFramebufferViewportHeight(), + 1, 1 + ); } @@ -341,8 +326,8 @@ public class McLodRenderer implements IMcLodRenderer OptionalDouble optionalDepthValueAsDouble = OptionalDouble.empty(); try ( - GpuTextureView colorTextureView = gpuDevice.createTextureView(Minecraft.getInstance().getMainRenderTarget().getColorTexture()); - GpuTextureView depthTextureView = gpuDevice.createTextureView(this.depthTexture); + GpuTextureView colorTextureView = gpuDevice.createTextureView(this.dhColorTexture); + GpuTextureView depthTextureView = gpuDevice.createTextureView(this.dhDepthTexture); RenderPass renderPass = commandEncoder.createRenderPass( debugLabelSupplier, colorTextureView, @@ -361,30 +346,6 @@ public class McLodRenderer implements IMcLodRenderer renderPass.setUniform("fragUniformBlock", this.fragUniformBuffer); - //boolean renderWireframe = Config.Client.Advanced.Debugging.renderWireframe.get(); - //if (renderWireframe) - //{ - // GL32.glPolygonMode(GL32.GL_FRONT_AND_BACK, GL32.GL_LINE); - // GLMC.disableFaceCulling(); - //} - //else - //{ - // GL32.glPolygonMode(GL32.GL_FRONT_AND_BACK, GL32.GL_FILL); - // GLMC.enableFaceCulling(); - //} - // - //if (!opaquePass) - //{ - // GLMC.enableBlend(); - // GLMC.enableDepthTest(); - // GL32.glBlendEquation(GL32.GL_FUNC_ADD); - // GLMC.glBlendFuncSeparate(GL32.GL_SRC_ALPHA, GL32.GL_ONE_MINUS_SRC_ALPHA, GL32.GL_ONE, GL32.GL_ONE_MINUS_SRC_ALPHA); - //} - //else - //{ - // GLMC.disableBlend(); - //} - profiler.popPush("set pipeline"); @@ -431,14 +392,30 @@ public class McLodRenderer implements IMcLodRenderer profiler.pop(); } + @Override + public void applyToMcTexture() { McApplyRenderer.INSTANCE.render(); } + + @Override public void clearDepth() { GpuDevice gpuDevice = RenderSystem.getDevice(); CommandEncoder commandEncoder = gpuDevice.createCommandEncoder(); - if (this.depthTexture != null) + if (this.dhDepthTexture != null) { - commandEncoder.clearDepthTexture(this.depthTexture, 1.0f); + commandEncoder.clearDepthTexture(this.dhDepthTexture, 1.0f); + } + } + + @Override + public void clearColor() + { + GpuDevice gpuDevice = RenderSystem.getDevice(); + CommandEncoder commandEncoder = gpuDevice.createCommandEncoder(); + + if (this.dhColorTexture != null) + { + commandEncoder.clearColorTexture(this.dhColorTexture, ColorUtil.argbToInt(1, 1, 1, 1)); } } diff --git a/common/src/main/java/com/seibel/distanthorizons/common/renderTest/UniformHandler.java b/common/src/main/java/com/seibel/distanthorizons/common/renderTest/UniformHandler.java new file mode 100644 index 000000000..dd4c73a00 --- /dev/null +++ b/common/src/main/java/com/seibel/distanthorizons/common/renderTest/UniformHandler.java @@ -0,0 +1,26 @@ +package com.seibel.distanthorizons.common.renderTest; + +import com.mojang.blaze3d.buffers.GpuBuffer; +import com.mojang.blaze3d.systems.GpuDevice; +import com.mojang.blaze3d.systems.RenderSystem; + +public class UniformHandler +{ + + public static GpuBuffer createBuffer(String uniformName, int size, GpuBuffer vboGpuBuffer) + { + GpuDevice gpuDevice = RenderSystem.getDevice(); + + // create VBO if needed + if (vboGpuBuffer == null + || vboGpuBuffer.size() < size) + { + // GpuBuffer.USAGE_UNIFORM = 128 + int usage = 8 | 32 | 128; // is this just using OpenGL VBO flags?, if so I can't find it, supposedly GlDevice on Mojang's side + vboGpuBuffer = gpuDevice.createBuffer(() -> uniformName, usage, size); + } + + return vboGpuBuffer; + } + +} diff --git a/coreSubProjects b/coreSubProjects index c6a435571..3f509be19 160000 --- a/coreSubProjects +++ b/coreSubProjects @@ -1 +1 @@ -Subproject commit c6a43557185d068ef0c0f35c60235d275dbe4960 +Subproject commit 3f509be195bad4dc3988c8805f5bfb56d375a9dc