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 7f0e4067f..5744823e8 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,7 @@ 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.common.renderTest.apply.McApplyRenderer; +import com.seibel.distanthorizons.common.renderTest.apply.DhApplyRenderer; import com.seibel.distanthorizons.common.renderTest.helpers.DhVertexFormat; import com.seibel.distanthorizons.common.renderTest.helpers.LodContainerUniformBufferWrapper; import com.seibel.distanthorizons.common.renderTest.helpers.UniformHandler; @@ -40,6 +40,7 @@ import com.seibel.distanthorizons.core.wrapperInterfaces.minecraft.IMinecraftRen import com.seibel.distanthorizons.core.wrapperInterfaces.minecraft.IProfilerWrapper; import com.seibel.distanthorizons.core.wrapperInterfaces.render.IMcLodRenderer; import com.seibel.distanthorizons.core.wrapperInterfaces.render.IVertexBufferWrapper; +import net.minecraft.client.Minecraft; import net.minecraft.resources.Identifier; import org.lwjgl.opengl.GL32; import org.lwjgl.system.MemoryUtil; @@ -62,6 +63,9 @@ public class McLodRenderer implements IMcLodRenderer public static final McLodRenderer INSTANCE = new McLodRenderer(); + + private DhApplyRenderer applyRenderer; + private VertexFormat vertexFormat; private RenderPipeline opaquePipeline; private RenderPipeline transparentPipeline; @@ -111,6 +115,11 @@ public class McLodRenderer implements IMcLodRenderer this.init = true; // todo only set when succeeded (in case of exception) + this.applyRenderer = new DhApplyRenderer( + "dh_apply_to_mc", + null, + "apply/vert", "apply/frag" + ); GpuDevice gpuDevice = RenderSystem.getDevice(); CommandEncoder commandEncoder = gpuDevice.createCommandEncoder(); @@ -447,7 +456,13 @@ public class McLodRenderer implements IMcLodRenderer } @Override - public void applyToMcTexture() { McApplyRenderer.INSTANCE.render(); } + public void applyToMcTexture() + { + //McApplyRenderer.INSTANCE.render(); + + GpuTexture mcColorTexture = Minecraft.getInstance().getMainRenderTarget().getColorTexture(); + this.applyRenderer.render(this.dhColorTexture, this.dhDepthTexture, mcColorTexture); + } @Override public void clearDepth() diff --git a/common/src/main/java/com/seibel/distanthorizons/common/renderTest/apply/DhApplyRenderer.java b/common/src/main/java/com/seibel/distanthorizons/common/renderTest/apply/DhApplyRenderer.java new file mode 100644 index 000000000..4f0bb334b --- /dev/null +++ b/common/src/main/java/com/seibel/distanthorizons/common/renderTest/apply/DhApplyRenderer.java @@ -0,0 +1,268 @@ +/* + * 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.apply; + +import com.mojang.blaze3d.buffers.GpuBuffer; +import com.mojang.blaze3d.buffers.GpuBufferSlice; +import com.mojang.blaze3d.pipeline.BlendFunction; +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.common.renderTest.helpers.DhVertexFormat; +import com.seibel.distanthorizons.core.logging.DhLogger; +import com.seibel.distanthorizons.core.logging.DhLoggerBuilder; +import net.minecraft.resources.Identifier; +import org.jetbrains.annotations.Nullable; + +import java.nio.ByteBuffer; +import java.nio.ByteOrder; +import java.util.OptionalDouble; +import java.util.OptionalInt; +import java.util.function.Supplier; + +/** + * TODO ??? + */ +public class DhApplyRenderer +{ + public static final DhLogger LOGGER = new DhLoggerBuilder().build(); + + private static final GpuDevice GPU_DEVICE = RenderSystem.getDevice(); + private static final CommandEncoder COMMAND_ENCODER = GPU_DEVICE.createCommandEncoder(); + + private RenderPipeline pipeline; + + protected GpuBuffer vboGpuBuffer; + + protected final String identifierName; + public String getIdentifierName() { return this.identifierName; } + + @Nullable + private final BlendFunction blendFunction; + private final String vertexShaderPath; + private final String fragmentShaderPath; + + private GpuTextureView sourceColorTextureView; + private GpuSampler sourceColorSampler; + + private GpuTextureView sourceDepthTextureView; + private GpuSampler sourceDepthSampler; + + private GpuTextureView destinationColorTextureView; + + + + //=============// + // constructor // + //=============// + //region + + public DhApplyRenderer( + String name, + @Nullable BlendFunction blendFunction, + String vertexShaderPath, String fragmentShaderPath + ) + { + this.identifierName = "distanthorizons:"+name; + this.blendFunction = blendFunction; + + this.vertexShaderPath = vertexShaderPath; + this.fragmentShaderPath = fragmentShaderPath; + } + + private void tryInit( + GpuTexture sourceColorTexture, + GpuTexture sourceDepthTexture, + GpuTexture destinationColorTexture) + { + this.createPipeline(); + this.uploadVertexData(); + this.createTextureViews(sourceColorTexture, sourceDepthTexture, destinationColorTexture); + } + private void createPipeline() + { + if (this.pipeline != null) + { + return; + } + + VertexFormat 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); + + if (this.blendFunction != null) + { + pipelineBuilder.withBlend(this.blendFunction); + } + else + { + pipelineBuilder.withoutBlend(); + } + + pipelineBuilder.withPolygonMode(PolygonMode.FILL); + pipelineBuilder.withLocation(Identifier.parse(this.identifierName)); // TODO will complain if capital letters are included + + // TODO manually validate paths to confirm they exist and end with ".fsh" or ".vsh", MC silently fails if the files are missing/improperly named + pipelineBuilder.withVertexShader(Identifier.fromNamespaceAndPath("distanthorizons", this.vertexShaderPath)); + pipelineBuilder.withFragmentShader(Identifier.fromNamespaceAndPath("distanthorizons", this.fragmentShaderPath)); + + pipelineBuilder.withSampler("uSourceColorTexture"); + pipelineBuilder.withSampler("uSourceDepthTexture"); + + pipelineBuilder.withVertexFormat(vertexFormat, VertexFormat.Mode.TRIANGLE_FAN); + } + this.pipeline = pipelineBuilder.build(); + } + private void uploadVertexData() + { + // vertices for a full-screen quad + float[] vertices = new float[] + { + // PosX,Y, + -1f, -1f, + 1f, -1f, + 1f, 1f, + -1f, 1f, + }; + + + Supplier labelSupplier = () -> "distantHorizons:"+this.identifierName; + 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 = GPU_DEVICE.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(); + + COMMAND_ENCODER.writeToBuffer(bufferSlice, byteBuffer); + } + } + private void createTextureViews( + GpuTexture sourceColorTexture, + GpuTexture sourceDepthTexture, + GpuTexture destinationColorTexture) + { + + // source color + if (this.sourceColorTextureView == null + || this.sourceColorTextureView.texture() != sourceColorTexture) + { + if (this.sourceColorTextureView != null) + { + this.sourceColorTextureView.close(); + } + + this.sourceColorTextureView = GPU_DEVICE.createTextureView(sourceColorTexture); + this.sourceColorSampler = GPU_DEVICE.createSampler( + AddressMode.CLAMP_TO_EDGE, AddressMode.CLAMP_TO_EDGE, // U,V + FilterMode.NEAREST, FilterMode.NEAREST, // minFilter, magFilter + 1, // maxAnisotropy + OptionalDouble.empty() // maxLod + ); + } + + // source depth + if (this.sourceDepthTextureView == null + || this.sourceDepthTextureView.texture() != sourceDepthTexture) + { + if (this.sourceDepthTextureView != null) + { + this.sourceDepthTextureView.close(); + } + + this.sourceDepthTextureView = GPU_DEVICE.createTextureView(sourceDepthTexture); + this.sourceDepthSampler = GPU_DEVICE.createSampler( + AddressMode.CLAMP_TO_EDGE, AddressMode.CLAMP_TO_EDGE, // U,V + FilterMode.NEAREST, FilterMode.NEAREST, // minFilter, magFilter + 1, // maxAnisotropy + OptionalDouble.empty() // maxLod + ); + } + + // destination color + if (this.destinationColorTextureView == null + || this.destinationColorTextureView.texture() != destinationColorTexture) + { + if (this.destinationColorTextureView != null) + { + this.destinationColorTextureView.close(); + } + + this.destinationColorTextureView = GPU_DEVICE.createTextureView(destinationColorTexture); + } + } + + //endregion + + + + //========// + // render // + //========// + //region + + public void render( + GpuTexture sourceColorTexture, + GpuTexture sourceDepthTexture, + GpuTexture destinationColorTexture) + { + this.tryInit(sourceColorTexture, sourceDepthTexture, destinationColorTexture); + + try (RenderPass renderPass = COMMAND_ENCODER.createRenderPass( + this::getIdentifierName, + this.destinationColorTextureView, /*optionalClearColorAsInt*/ OptionalInt.empty(), + /*depthTexture*/ null, /*optionalDepthValueAsDouble*/ OptionalDouble.empty())) + { + renderPass.bindTexture("uSourceColorTexture", this.sourceColorTextureView, this.sourceColorSampler); + renderPass.bindTexture("uSourceDepthTexture", this.sourceDepthTextureView, this.sourceDepthSampler); + + renderPass.setVertexBuffer(0, this.vboGpuBuffer); + renderPass.setPipeline(this.pipeline); + + renderPass.draw(/*indexStart*/ 0, /*indexCount*/ 4); + } + } + + //endregion + + + +} diff --git a/common/src/main/java/com/seibel/distanthorizons/common/renderTest/apply/McApplyRenderer.java b/common/src/main/java/com/seibel/distanthorizons/common/renderTest/apply/McApplyRenderer.java deleted file mode 100644 index 7e0794014..000000000 --- a/common/src/main/java/com/seibel/distanthorizons/common/renderTest/apply/McApplyRenderer.java +++ /dev/null @@ -1,262 +0,0 @@ -/* - * 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.apply; - -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.common.renderTest.McLodRenderer; -import com.seibel.distanthorizons.common.renderTest.helpers.DhVertexFormat; -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 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; - - private GpuTextureView dhColorTextureView; - private GpuSampler dhColorTextureSampler; - - private GpuTextureView dhDepthTextureView; - private GpuSampler dhDepthTextureSampler; - - private GpuTextureView mcColorTextureView; - - - //=============// - // 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(); - - - { - GpuTexture mcColorTexture = Minecraft.getInstance().getMainRenderTarget().getColorTexture(); - if (this.mcColorTextureView == null - || this.mcColorTextureView.texture() != mcColorTexture) - { - this.mcColorTextureView = gpuDevice.createTextureView(mcColorTexture); - } - - - if (this.dhColorTextureSampler == null) - { - this.dhColorTextureSampler = gpuDevice.createSampler( - AddressMode.CLAMP_TO_EDGE, AddressMode.CLAMP_TO_EDGE, // U,V - FilterMode.NEAREST, FilterMode.NEAREST, // minFilter, magFilter - 1, // maxAnisotropy - OptionalDouble.empty() // maxLod - ); - - this.dhDepthTextureSampler = gpuDevice.createSampler( - AddressMode.CLAMP_TO_EDGE, AddressMode.CLAMP_TO_EDGE, // U,V - FilterMode.NEAREST, FilterMode.NEAREST, // minFilter, magFilter - 1, // maxAnisotropy - OptionalDouble.empty() // maxLod - ); - } - - } - - - // create a render pass - { - Supplier debugLabelSupplier = () -> "distantHorizons:McApplyRenderer"; - OptionalInt optionalClearColorAsInt = OptionalInt.empty(); - GpuTextureView depthTexture = null; - OptionalDouble optionalDepthValueAsDouble = OptionalDouble.empty(); - - try (RenderPass renderPass = commandEncoder.createRenderPass( - debugLabelSupplier, - this.mcColorTextureView, - optionalClearColorAsInt, - depthTexture, optionalDepthValueAsDouble)) - { - //renderPass.pushDebugGroup(); - //renderPass.popDebugGroup(); - - - // render pass setup - { - renderPass.bindTexture("uDhDepthTexture", McLodRenderer.INSTANCE.dhDepthTextureView, this.dhColorTextureSampler); - renderPass.bindTexture("uDhColorTexture", McLodRenderer.INSTANCE.dhColorTextureView, this.dhDepthTextureSampler); - - // 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/apply/McCopyRenderer.java b/common/src/main/java/com/seibel/distanthorizons/common/renderTest/apply/McCopyRenderer.java index 4cdfc55a0..d70c06e05 100644 --- a/common/src/main/java/com/seibel/distanthorizons/common/renderTest/apply/McCopyRenderer.java +++ b/common/src/main/java/com/seibel/distanthorizons/common/renderTest/apply/McCopyRenderer.java @@ -63,15 +63,13 @@ public class McCopyRenderer private GpuBuffer vboGpuBuffer; + //=============// // constructor // //=============// //region - private McCopyRenderer() - { - - } + private McCopyRenderer() { } private void tryInit() { diff --git a/common/src/main/java/com/seibel/distanthorizons/common/renderTest/postProcessing/McFogApplyRenderer.java b/common/src/main/java/com/seibel/distanthorizons/common/renderTest/postProcessing/McFogApplyRenderer.java deleted file mode 100644 index 904236fef..000000000 --- a/common/src/main/java/com/seibel/distanthorizons/common/renderTest/postProcessing/McFogApplyRenderer.java +++ /dev/null @@ -1,253 +0,0 @@ -/* - * 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.postProcessing; - -import com.mojang.blaze3d.buffers.GpuBuffer; -import com.mojang.blaze3d.buffers.GpuBufferSlice; -import com.mojang.blaze3d.pipeline.BlendFunction; -import com.mojang.blaze3d.pipeline.RenderPipeline; -import com.mojang.blaze3d.platform.DepthTestFunction; -import com.mojang.blaze3d.platform.DestFactor; -import com.mojang.blaze3d.platform.PolygonMode; -import com.mojang.blaze3d.platform.SourceFactor; -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.AddressMode; -import com.mojang.blaze3d.textures.FilterMode; -import com.mojang.blaze3d.textures.GpuSampler; -import com.mojang.blaze3d.textures.GpuTextureView; -import com.mojang.blaze3d.vertex.VertexFormat; -import com.seibel.distanthorizons.common.renderTest.helpers.DhVertexFormat; -import com.seibel.distanthorizons.common.renderTest.McLodRenderer; -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.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 McFogApplyRenderer -{ - 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 McFogApplyRenderer INSTANCE = new McFogApplyRenderer(); - - private VertexFormat vertexFormat; - private RenderPipeline pipeline; - private boolean init = false; - - private GpuBuffer vboGpuBuffer; - - - //=============// - // constructor // - //=============// - //region - - private McFogApplyRenderer() - { - - } - - 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.withBlend(new BlendFunction(SourceFactor.SRC_ALPHA, DestFactor.ONE_MINUS_SRC_ALPHA, SourceFactor.ONE, DestFactor.ONE_MINUS_SRC_ALPHA)); - pipelineBuilder.withPolygonMode(PolygonMode.FILL); - pipelineBuilder.withLocation(Identifier.parse("distanthorizons:fog_apply_render")); - - pipelineBuilder.withVertexShader(Identifier.fromNamespaceAndPath("distanthorizons", "fog/quad_apply")); - pipelineBuilder.withFragmentShader(Identifier.fromNamespaceAndPath("distanthorizons", "fog/apply")); - - pipelineBuilder.withSampler("uColorTexture"); - 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:McFogApplyRenderer"; - 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:McApplyFogRenderer"; - GpuTextureView colorTexture = gpuDevice.createTextureView(McLodRenderer.INSTANCE.dhColorTexture); - 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 fog color texture - { - GpuTextureView textureView = gpuDevice.createTextureView(McFogRenderer.INSTANCE.fogColorTexture); - 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("uColorTexture", textureView, gpuSampler); - } - - // bind DH LOD 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/postProcessing/McFogRenderer.java b/common/src/main/java/com/seibel/distanthorizons/common/renderTest/postProcessing/McFogRenderer.java index f77ecad40..5998f078f 100644 --- a/common/src/main/java/com/seibel/distanthorizons/common/renderTest/postProcessing/McFogRenderer.java +++ b/common/src/main/java/com/seibel/distanthorizons/common/renderTest/postProcessing/McFogRenderer.java @@ -23,9 +23,12 @@ 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.BlendFunction; import com.mojang.blaze3d.pipeline.RenderPipeline; import com.mojang.blaze3d.platform.DepthTestFunction; +import com.mojang.blaze3d.platform.DestFactor; import com.mojang.blaze3d.platform.PolygonMode; +import com.mojang.blaze3d.platform.SourceFactor; import com.mojang.blaze3d.shaders.UniformType; import com.mojang.blaze3d.systems.CommandEncoder; import com.mojang.blaze3d.systems.GpuDevice; @@ -37,6 +40,7 @@ import com.seibel.distanthorizons.api.enums.rendering.EDhApiFogColorMode; import com.seibel.distanthorizons.api.enums.rendering.EDhApiHeightFogDirection; import com.seibel.distanthorizons.api.enums.rendering.EDhApiHeightFogMixMode; import com.seibel.distanthorizons.api.objects.math.DhApiMat4f; +import com.seibel.distanthorizons.common.renderTest.apply.DhApplyRenderer; import com.seibel.distanthorizons.common.renderTest.helpers.DhVertexFormat; import com.seibel.distanthorizons.common.renderTest.McLodRenderer; import com.seibel.distanthorizons.common.renderTest.helpers.UniformHandler; @@ -72,6 +76,9 @@ public class McFogRenderer implements IMcFogRenderer public static final McFogRenderer INSTANCE = new McFogRenderer(); + + private DhApplyRenderer applyRenderer; + private VertexFormat vertexFormat; private RenderPipeline pipeline; private boolean init = false; @@ -106,6 +113,14 @@ public class McFogRenderer implements IMcFogRenderer + + this.applyRenderer = new DhApplyRenderer( + "fog_apply_to_dh", + new BlendFunction(SourceFactor.SRC_ALPHA, DestFactor.ONE_MINUS_SRC_ALPHA, SourceFactor.ONE, DestFactor.ONE_MINUS_SRC_ALPHA), + "apply/vert", "apply/frag" + ); + + GpuDevice gpuDevice = RenderSystem.getDevice(); CommandEncoder commandEncoder = gpuDevice.createCommandEncoder(); @@ -362,7 +377,7 @@ public class McFogRenderer implements IMcFogRenderer this.renderFogToTexture(); - McFogApplyRenderer.INSTANCE.render(); + this.applyRenderer.render(this.fogColorTexture, McLodRenderer.INSTANCE.dhDepthTexture, McLodRenderer.INSTANCE.dhColorTexture); } diff --git a/common/src/main/java/com/seibel/distanthorizons/common/renderTest/postProcessing/McSsaoApplyRenderer.java b/common/src/main/java/com/seibel/distanthorizons/common/renderTest/postProcessing/McSsaoApplyRenderer.java deleted file mode 100644 index 5d6c5ae75..000000000 --- a/common/src/main/java/com/seibel/distanthorizons/common/renderTest/postProcessing/McSsaoApplyRenderer.java +++ /dev/null @@ -1,253 +0,0 @@ -/* - * 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.postProcessing; - -import com.mojang.blaze3d.buffers.GpuBuffer; -import com.mojang.blaze3d.buffers.GpuBufferSlice; -import com.mojang.blaze3d.pipeline.BlendFunction; -import com.mojang.blaze3d.pipeline.RenderPipeline; -import com.mojang.blaze3d.platform.DepthTestFunction; -import com.mojang.blaze3d.platform.DestFactor; -import com.mojang.blaze3d.platform.PolygonMode; -import com.mojang.blaze3d.platform.SourceFactor; -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.AddressMode; -import com.mojang.blaze3d.textures.FilterMode; -import com.mojang.blaze3d.textures.GpuSampler; -import com.mojang.blaze3d.textures.GpuTextureView; -import com.mojang.blaze3d.vertex.VertexFormat; -import com.seibel.distanthorizons.common.renderTest.helpers.DhVertexFormat; -import com.seibel.distanthorizons.common.renderTest.McLodRenderer; -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.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 McSsaoApplyRenderer -{ - 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 McSsaoApplyRenderer INSTANCE = new McSsaoApplyRenderer(); - - private VertexFormat vertexFormat; - private RenderPipeline pipeline; - private boolean init = false; - - private GpuBuffer vboGpuBuffer; - - - //=============// - // constructor // - //=============// - //region - - private McSsaoApplyRenderer() - { - - } - - 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.withBlend(new BlendFunction(SourceFactor.ZERO, DestFactor.SRC_ALPHA, SourceFactor.ZERO, DestFactor.ONE)); - pipelineBuilder.withPolygonMode(PolygonMode.FILL); - pipelineBuilder.withLocation(Identifier.parse("distanthorizons:ssao_apply_render")); - - pipelineBuilder.withVertexShader(Identifier.fromNamespaceAndPath("distanthorizons", "ssao/quad_apply")); - pipelineBuilder.withFragmentShader(Identifier.fromNamespaceAndPath("distanthorizons", "ssao/apply")); - - pipelineBuilder.withSampler("uSsaoColorTexture"); - 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:McApplySsaoRenderer"; - GpuTextureView colorTexture = gpuDevice.createTextureView(McLodRenderer.INSTANCE.dhColorTexture); - 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 SSAO color texture - { - GpuTextureView textureView = gpuDevice.createTextureView(McSsaoRenderer.INSTANCE.ssaoColorTexture); - 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("uSsaoColorTexture", textureView, gpuSampler); - } - - // bind DH LOD 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/postProcessing/McSsaoRenderer.java b/common/src/main/java/com/seibel/distanthorizons/common/renderTest/postProcessing/McSsaoRenderer.java index 0baa4ef43..bea03b91e 100644 --- a/common/src/main/java/com/seibel/distanthorizons/common/renderTest/postProcessing/McSsaoRenderer.java +++ b/common/src/main/java/com/seibel/distanthorizons/common/renderTest/postProcessing/McSsaoRenderer.java @@ -23,9 +23,12 @@ 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.BlendFunction; import com.mojang.blaze3d.pipeline.RenderPipeline; import com.mojang.blaze3d.platform.DepthTestFunction; +import com.mojang.blaze3d.platform.DestFactor; import com.mojang.blaze3d.platform.PolygonMode; +import com.mojang.blaze3d.platform.SourceFactor; import com.mojang.blaze3d.shaders.UniformType; import com.mojang.blaze3d.systems.CommandEncoder; import com.mojang.blaze3d.systems.GpuDevice; @@ -34,6 +37,7 @@ import com.mojang.blaze3d.systems.RenderSystem; import com.mojang.blaze3d.textures.*; import com.mojang.blaze3d.vertex.VertexFormat; import com.seibel.distanthorizons.api.objects.math.DhApiMat4f; +import com.seibel.distanthorizons.common.renderTest.apply.DhApplyRenderer; import com.seibel.distanthorizons.common.renderTest.helpers.DhVertexFormat; import com.seibel.distanthorizons.common.renderTest.McLodRenderer; import com.seibel.distanthorizons.common.renderTest.helpers.UniformHandler; @@ -64,6 +68,9 @@ public class McSsaoRenderer implements IMcSsaoRenderer public static final McSsaoRenderer INSTANCE = new McSsaoRenderer(); + + private DhApplyRenderer applyRenderer; + private VertexFormat vertexFormat; private RenderPipeline pipeline; private boolean init = false; @@ -97,6 +104,11 @@ public class McSsaoRenderer implements IMcSsaoRenderer this.init = true; + this.applyRenderer = new DhApplyRenderer( + "ssao_apply_to_dh", + new BlendFunction(SourceFactor.ZERO, DestFactor.SRC_ALPHA, SourceFactor.ZERO, DestFactor.ONE), + "apply/vert", "ssao/apply" + ); GpuDevice gpuDevice = RenderSystem.getDevice(); CommandEncoder commandEncoder = gpuDevice.createCommandEncoder(); @@ -111,7 +123,7 @@ public class McSsaoRenderer implements IMcSsaoRenderer pipelineBuilder.withColorWrite(true); pipelineBuilder.withoutBlend(); pipelineBuilder.withPolygonMode(PolygonMode.FILL); - pipelineBuilder.withLocation(Identifier.parse("distanthorizons:test_render")); + pipelineBuilder.withLocation(Identifier.parse("distanthorizons:ssao_render")); pipelineBuilder.withVertexShader(Identifier.fromNamespaceAndPath("distanthorizons", "ssao/quad_apply")); pipelineBuilder.withFragmentShader(Identifier.fromNamespaceAndPath("distanthorizons", "ssao/ao")); @@ -259,7 +271,7 @@ public class McSsaoRenderer implements IMcSsaoRenderer this.renderSsaoToTexture(); - McSsaoApplyRenderer.INSTANCE.render(); + this.applyRenderer.render(this.ssaoColorTexture, McLodRenderer.INSTANCE.dhDepthTexture, McLodRenderer.INSTANCE.dhColorTexture); } diff --git a/common/src/main/java/com/seibel/distanthorizons/common/renderTest/postProcessing/McVanillaFadeRenderer.java b/common/src/main/java/com/seibel/distanthorizons/common/renderTest/postProcessing/McVanillaFadeRenderer.java index 08f93e3f1..52f5fc9b5 100644 --- a/common/src/main/java/com/seibel/distanthorizons/common/renderTest/postProcessing/McVanillaFadeRenderer.java +++ b/common/src/main/java/com/seibel/distanthorizons/common/renderTest/postProcessing/McVanillaFadeRenderer.java @@ -115,7 +115,7 @@ public class McVanillaFadeRenderer implements IMcVanillaFadeRenderer pipelineBuilder.withColorWrite(true); pipelineBuilder.withoutBlend(); pipelineBuilder.withPolygonMode(PolygonMode.FILL); - pipelineBuilder.withLocation(Identifier.parse("distanthorizons:test_render")); + pipelineBuilder.withLocation(Identifier.parse("distanthorizons:mc_vanilla_fade_render")); pipelineBuilder.withVertexShader(Identifier.fromNamespaceAndPath("distanthorizons", "fade/vert")); pipelineBuilder.withFragmentShader(Identifier.fromNamespaceAndPath("distanthorizons", "fade/vanilla_fade"));