diff --git a/common/src/main/java/com/seibel/distanthorizons/common/renderTest/DhVertexFormat.java b/common/src/main/java/com/seibel/distanthorizons/common/renderTest/DhVertexFormat.java new file mode 100644 index 000000000..fd4192f0a --- /dev/null +++ b/common/src/main/java/com/seibel/distanthorizons/common/renderTest/DhVertexFormat.java @@ -0,0 +1,11 @@ +package com.seibel.distanthorizons.common.renderTest; + +import com.mojang.blaze3d.vertex.VertexFormatElement; + +public class DhVertexFormat +{ + + public static final VertexFormatElement SCREEN_POS = VertexFormatElement.register(/*id*/7, /*index*/0, VertexFormatElement.Type.FLOAT, VertexFormatElement.Usage.POSITION, /*count*/ 2); + public static final VertexFormatElement RGBA_COLOR = VertexFormatElement.register(/*id*/8, /*index*/0, VertexFormatElement.Type.FLOAT, VertexFormatElement.Usage.COLOR, /*count*/ 4); + +} 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 new file mode 100644 index 000000000..422ce841a --- /dev/null +++ b/common/src/main/java/com/seibel/distanthorizons/common/renderTest/McFadeRenderer.java @@ -0,0 +1,233 @@ +/* + * 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.mojang.blaze3d.vertex.VertexFormatElement; +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.IMcFadeRenderer; +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; + +/** + * Renders a UV colored quad + * to the center of the screen to confirm DH's + * apply shader is running correctly + */ +public class McFadeRenderer implements IMcFadeRenderer +{ + 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 McFadeRenderer INSTANCE = new McFadeRenderer(); + + private VertexFormat vertexFormat; + private RenderPipeline pipeline; + private boolean init = false; + + private GpuBuffer vboGpuBuffer; + + + //=============// + // constructor // + //=============// + //region + + private McFadeRenderer() + { + + } + + 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); + //builder.withBlend(BlendFunction.OVERLAY); + pipelineBuilder.withPolygonMode(PolygonMode.FILL); + pipelineBuilder.withLocation(Identifier.parse("distanthorizons:test_render")); + + pipelineBuilder.withVertexShader(Identifier.fromNamespaceAndPath("distanthorizons", "fade/vert")); + pipelineBuilder.withFragmentShader(Identifier.fromNamespaceAndPath("distanthorizons", "fade/frag")); + + 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:McFadeRenderer"; + 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(); + + + + GpuDevice gpuDevice = RenderSystem.getDevice(); + CommandEncoder commandEncoder = gpuDevice.createCommandEncoder(); + + + + // create a render pass + Supplier debugLabelSupplier = () -> "distantHorizons:McFadeRenderer"; + GpuTextureView colorTexture = gpuDevice.createTextureView(Minecraft.getInstance().getMainRenderTarget().getColorTexture()); + OptionalInt optionalClearColorAsInt = OptionalInt.empty(); + GpuTextureView depthTexture = null;//gpuDevice.createTextureView(Minecraft.getInstance().getMainRenderTarget().getDepthTexture()); + OptionalDouble optionalDepthValueAsDouble = OptionalDouble.empty(); + + try (RenderPass renderPass = commandEncoder.createRenderPass( + debugLabelSupplier, + colorTexture, + optionalClearColorAsInt, + depthTexture, optionalDepthValueAsDouble)) + { + //renderPass.pushDebugGroup(); + //renderPass.popDebugGroup(); + + + // render pass setup + { + // bind MC depth texture + { + IMinecraftRenderWrapper mcRender = SingletonInjector.INSTANCE.get(IMinecraftRenderWrapper.class); + int id = mcRender.getDepthTextureId(); + + GpuTexture bindDepthTexture = Minecraft.getInstance().getMainRenderTarget().getDepthTexture(); + + GpuTextureView textureView = gpuDevice.createTextureView(bindDepthTexture); + 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/McTestRenderer.java b/common/src/main/java/com/seibel/distanthorizons/common/renderTest/McTestRenderer.java index 3803caf3e..b693e424f 100644 --- a/common/src/main/java/com/seibel/distanthorizons/common/renderTest/McTestRenderer.java +++ b/common/src/main/java/com/seibel/distanthorizons/common/renderTest/McTestRenderer.java @@ -91,9 +91,6 @@ public class McTestRenderer implements IMcTestRenderer private RenderPipeline pipeline; private boolean init = false; - private VertexFormatElement posForm; - private VertexFormatElement colForm; - private GpuBuffer vboGpuBuffer; @@ -104,8 +101,7 @@ public class McTestRenderer implements IMcTestRenderer private McTestRenderer() { - this.posForm = VertexFormatElement.register(/*id*/7, /*index*/0, VertexFormatElement.Type.FLOAT, VertexFormatElement.Usage.POSITION, /*count*/ 2); - this.colForm = VertexFormatElement.register(/*id*/8, /*index*/0, VertexFormatElement.Type.FLOAT, VertexFormatElement.Usage.COLOR, /*count*/ 4); + } private void tryInit() @@ -123,16 +119,16 @@ public class McTestRenderer implements IMcTestRenderer this.vertexFormat = VertexFormat.builder() - .add("vPosition", this.posForm) - .add("vColor", this.colForm) + .add("vPosition", DhVertexFormat.SCREEN_POS) + .add("vColor", DhVertexFormat.RGBA_COLOR) .build(); - int breakpointOne = 0; + //int breakpointOne = 0; // needs to manually be set if the VertexFormatElement isn't registered - this.vertexFormat.getOffsetsByElement()[this.posForm.id()] = 0; - this.vertexFormat.getOffsetsByElement()[this.colForm.id()] = Float.BYTES * 2; - - int breakpointTwo = 0; + //this.vertexFormat.getOffsetsByElement()[this.posForm.id()] = 0; + //this.vertexFormat.getOffsetsByElement()[this.colForm.id()] = Float.BYTES * 2; + // + //int breakpointTwo = 0; RenderPipeline.Builder pipelineBuilder = RenderPipeline.builder(); @@ -148,7 +144,7 @@ public class McTestRenderer implements IMcTestRenderer pipelineBuilder.withVertexShader(Identifier.fromNamespaceAndPath("distanthorizons", "test/vert")); pipelineBuilder.withFragmentShader(Identifier.fromNamespaceAndPath("distanthorizons", "test/frag")); - pipelineBuilder.withVertexFormat(this.vertexFormat, VertexFormat.Mode.TRIANGLE_FAN); + pipelineBuilder.withVertexFormat(this.vertexFormat, VertexFormat.Mode.TRIANGLES); } this.pipeline = pipelineBuilder.build(); diff --git a/common/src/main/java/com/seibel/distanthorizons/common/wrappers/DependencySetup.java b/common/src/main/java/com/seibel/distanthorizons/common/wrappers/DependencySetup.java index 0eb59f2b8..9f4a935d3 100644 --- a/common/src/main/java/com/seibel/distanthorizons/common/wrappers/DependencySetup.java +++ b/common/src/main/java/com/seibel/distanthorizons/common/wrappers/DependencySetup.java @@ -19,6 +19,7 @@ package com.seibel.distanthorizons.common.wrappers; +import com.seibel.distanthorizons.common.renderTest.McFadeRenderer; import com.seibel.distanthorizons.common.renderTest.McTestRenderer; import com.seibel.distanthorizons.common.wrappers.gui.ClassicConfigGUI; import com.seibel.distanthorizons.common.wrappers.gui.LangWrapper; @@ -37,6 +38,7 @@ import com.seibel.distanthorizons.core.wrapperInterfaces.minecraft.IMinecraftCli import com.seibel.distanthorizons.core.wrapperInterfaces.minecraft.IMinecraftGLWrapper; import com.seibel.distanthorizons.core.wrapperInterfaces.minecraft.IMinecraftRenderWrapper; import com.seibel.distanthorizons.core.wrapperInterfaces.minecraft.IMinecraftSharedWrapper; +import com.seibel.distanthorizons.core.wrapperInterfaces.render.IMcFadeRenderer; import com.seibel.distanthorizons.core.wrapperInterfaces.render.IMcTestRenderer; /** @@ -75,6 +77,7 @@ public class DependencySetup public static void createRenderBindings() { SingletonInjector.INSTANCE.bind(IMcTestRenderer.class, McTestRenderer.INSTANCE); + SingletonInjector.INSTANCE.bind(IMcFadeRenderer.class, McFadeRenderer.INSTANCE); } } diff --git a/coreSubProjects b/coreSubProjects index 87cead607..d1ba402f4 160000 --- a/coreSubProjects +++ b/coreSubProjects @@ -1 +1 @@ -Subproject commit 87cead607fddddcd5c93770fc81fc4aa6bd3eebc +Subproject commit d1ba402f4d1db1f8cd98ef880984de089429153b