From da922a8c69853224d6ab5c4f01f28bb929492b6c Mon Sep 17 00:00:00 2001 From: James Seibel Date: Sun, 8 Mar 2026 19:18:17 -0500 Subject: [PATCH] wrap GPU textures --- .../renderTest/McDebugObjectRenderer.java | 29 ++-- .../renderTest/McGenericObjectRenderer.java | 60 +++---- .../common/renderTest/McLodRenderer.java | 141 ++++------------- .../renderTest/apply/DhApplyRenderer.java | 84 +++------- .../renderTest/apply/McCopyRenderer.java | 94 +++++------ .../helpers/AbstractUniformBufferWrapper.java | 14 +- .../helpers/McTextureViewWrapper.java | 59 +++++++ .../renderTest/helpers/McTextureWrapper.java | 113 +++++++++++++ .../postProcessing/McFarFadeRenderer.java | 125 ++++----------- .../postProcessing/McFogRenderer.java | 100 +++--------- .../postProcessing/McSsaoRenderer.java | 101 +++--------- .../postProcessing/McVanillaFadeRenderer.java | 149 ++++-------------- .../renderTest/test/DhTestRenderer.java | 43 ++--- .../common/wrappers/misc/LightMapWrapper.java | 9 +- 14 files changed, 431 insertions(+), 690 deletions(-) create mode 100644 common/src/main/java/com/seibel/distanthorizons/common/renderTest/helpers/McTextureViewWrapper.java create mode 100644 common/src/main/java/com/seibel/distanthorizons/common/renderTest/helpers/McTextureWrapper.java diff --git a/common/src/main/java/com/seibel/distanthorizons/common/renderTest/McDebugObjectRenderer.java b/common/src/main/java/com/seibel/distanthorizons/common/renderTest/McDebugObjectRenderer.java index f4a6d0044..17a562cee 100644 --- a/common/src/main/java/com/seibel/distanthorizons/common/renderTest/McDebugObjectRenderer.java +++ b/common/src/main/java/com/seibel/distanthorizons/common/renderTest/McDebugObjectRenderer.java @@ -64,6 +64,9 @@ public class McDebugObjectRenderer implements IMcDebugRenderer private static final IMinecraftRenderWrapper MC_RENDER = SingletonInjector.INSTANCE.get(IMinecraftRenderWrapper.class); + private static final GpuDevice GPU_DEVICE = RenderSystem.getDevice(); + private static final CommandEncoder COMMAND_ENCODER = GPU_DEVICE.createCommandEncoder(); + public static McDebugObjectRenderer INSTANCE = new McDebugObjectRenderer(); @@ -250,8 +253,8 @@ public class McDebugObjectRenderer implements IMcDebugRenderer { this.init(); - if (McLodRenderer.INSTANCE.dhColorTexture == null - || McLodRenderer.INSTANCE.dhDepthTexture == null) + if (McLodRenderer.INSTANCE.dhColorTextureWrapper.isEmpty() + || McLodRenderer.INSTANCE.dhDepthTextureWrapper.isEmpty()) { return; } @@ -270,8 +273,6 @@ public class McDebugObjectRenderer implements IMcDebugRenderer //===========// //#region - GpuDevice gpuDevice = RenderSystem.getDevice(); - CommandEncoder commandEncoder = gpuDevice.createCommandEncoder(); // validation // @@ -318,25 +319,18 @@ public class McDebugObjectRenderer implements IMcDebugRenderer this.uniformBuffer = UniformHandler.createBuffer("uniformBlock", uniformBufferSize, this.uniformBuffer); GpuBufferSlice bufferSlice = new GpuBufferSlice(this.uniformBuffer, 0, uniformBufferSize); - commandEncoder.writeToBuffer(bufferSlice, buffer); + COMMAND_ENCODER.writeToBuffer(bufferSlice, buffer); } - // render // - Supplier debugLabelSupplier = () -> "distantHorizons:McDebugRenderer"; - GpuTextureView colorTexture = gpuDevice.createTextureView(McLodRenderer.INSTANCE.dhColorTexture); - OptionalInt optionalClearColorAsInt = OptionalInt.empty(); - GpuTextureView depthTexture = gpuDevice.createTextureView(McLodRenderer.INSTANCE.dhDepthTexture); - OptionalDouble optionalDepthValueAsDouble = OptionalDouble.empty(); - - try (RenderPass renderPass = commandEncoder.createRenderPass( - debugLabelSupplier, - colorTexture, - optionalClearColorAsInt, - depthTexture, optionalDepthValueAsDouble)) + try (RenderPass renderPass = COMMAND_ENCODER.createRenderPass( + this::getName, + McLodRenderer.INSTANCE.dhColorTextureWrapper.textureView, + /*optionalClearColorAsInt*/ OptionalInt.empty(), + McLodRenderer.INSTANCE.dhDepthTextureWrapper.textureView, /*optionalDepthValueAsDouble*/ OptionalDouble.empty())) { // Bind instance data // renderPass.setUniform("uniformBlock", this.uniformBuffer); @@ -356,6 +350,7 @@ public class McDebugObjectRenderer implements IMcDebugRenderer //#endregion } + private String getName() { return "distantHorizons:McDebugRenderer"; } //endregion diff --git a/common/src/main/java/com/seibel/distanthorizons/common/renderTest/McGenericObjectRenderer.java b/common/src/main/java/com/seibel/distanthorizons/common/renderTest/McGenericObjectRenderer.java index b9c58706e..db03e9489 100644 --- a/common/src/main/java/com/seibel/distanthorizons/common/renderTest/McGenericObjectRenderer.java +++ b/common/src/main/java/com/seibel/distanthorizons/common/renderTest/McGenericObjectRenderer.java @@ -45,6 +45,7 @@ import com.seibel.distanthorizons.api.objects.render.DhApiRenderableBox; import com.seibel.distanthorizons.api.objects.render.DhApiRenderableBoxGroupShading; import com.seibel.distanthorizons.common.renderTest.helpers.DhVertexFormat; import com.seibel.distanthorizons.common.renderTest.helpers.McInstancedVboContainer; +import com.seibel.distanthorizons.common.renderTest.helpers.McTextureViewWrapper; import com.seibel.distanthorizons.common.renderTest.helpers.UniformHandler; import com.seibel.distanthorizons.common.wrappers.misc.LightMapWrapper; import com.seibel.distanthorizons.core.dependencyInjection.SingletonInjector; @@ -87,7 +88,9 @@ public class McGenericObjectRenderer implements IMcGenericRenderer private 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); + + private static final GpuDevice GPU_DEVICE = RenderSystem.getDevice(); + private static final CommandEncoder COMMAND_ENCODER = GPU_DEVICE.createCommandEncoder(); private static final DhApiRenderableBoxGroupShading DEFAULT_SHADING = DhApiRenderableBoxGroupShading.getUnshaded(); @@ -108,12 +111,10 @@ public class McGenericObjectRenderer implements IMcGenericRenderer private RenderPipeline opaquePipeline; private RenderPipeline transparentPipeline; - //private GpuBuffer boxVertexBuffer; - //private GpuBuffer boxIndexBuffer; - private GpuBuffer vertUniformBuffer; + //=============// // constructor // //=============// @@ -136,7 +137,6 @@ public class McGenericObjectRenderer implements IMcGenericRenderer .build(); this.createPipelines(); - //this.createBuffers(); if (RENDER_DEBUG_OBJECTS) { @@ -145,10 +145,6 @@ public class McGenericObjectRenderer implements IMcGenericRenderer } private void createPipelines() { - GpuDevice gpuDevice = RenderSystem.getDevice(); - CommandEncoder commandEncoder = gpuDevice.createCommandEncoder(); - - RenderPipeline.Builder pipelineBuilder = RenderPipeline.builder(); { pipelineBuilder.withCull(true); @@ -353,8 +349,8 @@ public class McGenericObjectRenderer implements IMcGenericRenderer //#endregion - if (McLodRenderer.INSTANCE.dhColorTexture == null - || McLodRenderer.INSTANCE.dhDepthTexture == null) + if (McLodRenderer.INSTANCE.dhColorTextureWrapper.isEmpty() + || McLodRenderer.INSTANCE.dhDepthTextureWrapper.isEmpty()) { return; } @@ -366,9 +362,6 @@ public class McGenericObjectRenderer implements IMcGenericRenderer //===========// //#region - GpuDevice gpuDevice = RenderSystem.getDevice(); - CommandEncoder commandEncoder = gpuDevice.createCommandEncoder(); - Collection boxList = this.boxGroupById.values(); for (RenderableBoxGroup boxGroup : boxList) { @@ -493,7 +486,7 @@ public class McGenericObjectRenderer implements IMcGenericRenderer this.vertUniformBuffer = UniformHandler.createBuffer("vertUniformBlock", uniformBufferSize, this.vertUniformBuffer); GpuBufferSlice bufferSlice = new GpuBufferSlice(this.vertUniformBuffer, 0, uniformBufferSize); - commandEncoder.writeToBuffer(bufferSlice, buffer); + COMMAND_ENCODER.writeToBuffer(bufferSlice, buffer); } @@ -505,17 +498,12 @@ public class McGenericObjectRenderer implements IMcGenericRenderer profiler.push(boxGroup.getResourceLocationNamespace()); profiler.push(boxGroup.getResourceLocationPath()); - Supplier debugLabelSupplier = () -> "distantHorizons:McTestRenderer"; - GpuTextureView colorTexture = gpuDevice.createTextureView(McLodRenderer.INSTANCE.dhColorTexture); - OptionalInt optionalClearColorAsInt = OptionalInt.empty(); - GpuTextureView depthTexture = gpuDevice.createTextureView(McLodRenderer.INSTANCE.dhDepthTexture); - OptionalDouble optionalDepthValueAsDouble = OptionalDouble.empty(); - - try (RenderPass renderPass = commandEncoder.createRenderPass( - debugLabelSupplier, - colorTexture, - optionalClearColorAsInt, - depthTexture, optionalDepthValueAsDouble)) + try (RenderPass renderPass = COMMAND_ENCODER.createRenderPass( + this::getName, + McLodRenderer.INSTANCE.dhColorTextureWrapper.textureView, + /*optionalClearColorAsInt*/ OptionalInt.empty(), + McLodRenderer.INSTANCE.dhDepthTextureWrapper.textureView, + /*optionalDepthValueAsDouble*/ OptionalDouble.empty())) { this.renderBoxGroupInstanced(renderPass, renderEventParam, boxGroup, camPos, profiler); } @@ -543,6 +531,7 @@ public class McGenericObjectRenderer implements IMcGenericRenderer //endregion } + private String getName() { return "distantHorizons:McTestRenderer"; } //endregion @@ -562,24 +551,11 @@ public class McGenericObjectRenderer implements IMcGenericRenderer profiler.push("vertex setup"); - GpuDevice gpuDevice = RenderSystem.getDevice(); - McInstancedVboContainer container = (McInstancedVboContainer) boxGroup.instancedVbos; - - // bind MC Lightmap - { - LightMapWrapper lightMapWrapper = (LightMapWrapper) renderEventParam.lightmap; - - GpuTextureView textureView = gpuDevice.createTextureView(lightMapWrapper.gpuTexture); - 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("uLightMap", textureView, gpuSampler); - } + LightMapWrapper lightMapWrapper = (LightMapWrapper) renderEventParam.lightmap; + McTextureViewWrapper lightmapTextureViewWrapper = lightMapWrapper.getTextureViewWrapper(); + renderPass.bindTexture("uLightMap", lightmapTextureViewWrapper.textureView, lightmapTextureViewWrapper.textureSampler); 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 5744823e8..a1366e526 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 @@ -17,10 +17,7 @@ import com.mojang.blaze3d.systems.RenderSystem; import com.mojang.blaze3d.textures.*; import com.mojang.blaze3d.vertex.VertexFormat; 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; -import com.seibel.distanthorizons.common.renderTest.helpers.VertexBufferWrapper; +import com.seibel.distanthorizons.common.renderTest.helpers.*; import com.seibel.distanthorizons.common.wrappers.misc.LightMapWrapper; import com.seibel.distanthorizons.core.config.Config; import com.seibel.distanthorizons.core.dataObjects.render.bufferBuilding.LodBufferContainer; @@ -35,7 +32,6 @@ 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.util.objects.SortedArraySet; -import com.seibel.distanthorizons.core.wrapperInterfaces.minecraft.IMinecraftGLWrapper; import com.seibel.distanthorizons.core.wrapperInterfaces.minecraft.IMinecraftRenderWrapper; import com.seibel.distanthorizons.core.wrapperInterfaces.minecraft.IProfilerWrapper; import com.seibel.distanthorizons.core.wrapperInterfaces.render.IMcLodRenderer; @@ -59,7 +55,9 @@ public class McLodRenderer implements IMcLodRenderer 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); + + private static final GpuDevice GPU_DEVICE = RenderSystem.getDevice(); + private static final CommandEncoder COMMAND_ENCODER = GPU_DEVICE.createCommandEncoder(); public static final McLodRenderer INSTANCE = new McLodRenderer(); @@ -76,15 +74,8 @@ public class McLodRenderer implements IMcLodRenderer private GpuBuffer fragUniformBuffer; private GpuBuffer vertSharedUniformBuffer; - public GpuTexture dhDepthTexture; - public GpuTextureView dhDepthTextureView; - - public GpuTexture dhColorTexture; - public GpuTextureView dhColorTextureView; - - private GpuTexture mcLightTexture; - private GpuTextureView mcLightTextureView; - private GpuSampler mcLightGpuSampler; + public final McTextureWrapper dhDepthTextureWrapper = McTextureWrapper.createDepth("DhDepthTexture"); + public final McTextureWrapper dhColorTextureWrapper = McTextureWrapper.createColor("DhColorTexture"); @@ -121,9 +112,6 @@ public class McLodRenderer implements IMcLodRenderer "apply/vert", "apply/frag" ); - GpuDevice gpuDevice = RenderSystem.getDevice(); - CommandEncoder commandEncoder = gpuDevice.createCommandEncoder(); - RenderPipeline.Builder pipelineBuilder = RenderPipeline.builder(); { @@ -181,12 +169,6 @@ public class McLodRenderer implements IMcLodRenderer this.tryInit(); - - GpuDevice gpuDevice = RenderSystem.getDevice(); - CommandEncoder commandEncoder = gpuDevice.createCommandEncoder(); - - - profiler.push("vert unique uniforms"); { // create data // @@ -243,7 +225,7 @@ public class McLodRenderer implements IMcLodRenderer this.vertSharedUniformBuffer = UniformHandler.createBuffer("vertSharedUniformBlock", uniformBufferSize, this.vertSharedUniformBuffer); GpuBufferSlice bufferSlice = new GpuBufferSlice(this.vertSharedUniformBuffer, 0, uniformBufferSize); - commandEncoder.writeToBuffer(bufferSlice, buffer); + COMMAND_ENCODER.writeToBuffer(bufferSlice, buffer); } profiler.popPush("set frag uniforms"); @@ -285,7 +267,7 @@ public class McLodRenderer implements IMcLodRenderer this.fragUniformBuffer = UniformHandler.createBuffer("fragUniformBlock", uniformBufferSize, this.fragUniformBuffer); GpuBufferSlice bufferSlice = new GpuBufferSlice(this.fragUniformBuffer, 0, uniformBufferSize); - commandEncoder.writeToBuffer(bufferSlice, buffer); + COMMAND_ENCODER.writeToBuffer(bufferSlice, buffer); } // create index buffer @@ -303,89 +285,47 @@ public class McLodRenderer implements IMcLodRenderer // GpuBuffer.USAGE_UNIFORM = 128 // GpuBuffer.USAGE_INDEX = 64 int usage = 8 | 32 | 64 | 128; // is this just using OpenGL VBO flags?, if so I can't find it, supposedly GlDevice on Mojang's side - this.indexBuffer = gpuDevice.createBuffer(() -> "DH Index Buffer", usage, buffer.capacity()); + this.indexBuffer = GPU_DEVICE.createBuffer(() -> "DH Index Buffer", usage, buffer.capacity()); } int offset = 0; GpuBufferSlice bufferSlice = new GpuBufferSlice(this.indexBuffer, offset, buffer.capacity()); - commandEncoder.writeToBuffer(bufferSlice, buffer); + COMMAND_ENCODER.writeToBuffer(bufferSlice, buffer); } } // textures - if (this.dhDepthTexture == null - || this.dhDepthTexture.getWidth(0) != MC_RENDER.getTargetFramebufferViewportWidth() - || this.dhDepthTexture.getHeight(0) != MC_RENDER.getTargetFramebufferViewportHeight()) - { - if (this.dhDepthTexture != null) - { - this.dhDepthTexture.close(); - this.dhDepthTextureView.close(); - - this.dhColorTexture.close(); - this.dhDepthTextureView.close(); - } - - // 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.dhDepthTextureView = gpuDevice.createTextureView(this.dhDepthTexture); - - this.dhColorTexture = gpuDevice.createTexture("DhColorTexture", - usage, - TextureFormat.RGBA8, - MC_RENDER.getTargetFramebufferViewportWidth(), MC_RENDER.getTargetFramebufferViewportHeight(), - 1, 1 - ); - this.dhColorTextureView = gpuDevice.createTextureView(this.dhColorTexture); - } + this.dhDepthTextureWrapper.trySetup(); + this.dhColorTextureWrapper.trySetup(); + + //LightMapWrapper lightMapWrapper = (LightMapWrapper) renderEventParam.lightmap; + //this.mcLightTextureViewWrapper.trySetup(lightMapWrapper.gpuTexture); - LightMapWrapper lightMapWrapper = (LightMapWrapper) renderEventParam.lightmap; - if (this.mcLightTexture != lightMapWrapper.gpuTexture) - { - this.mcLightTexture = lightMapWrapper.gpuTexture; - if (this.mcLightTextureView != null) - { - this.mcLightTextureView.close(); - } - - - this.mcLightTextureView = gpuDevice.createTextureView(this.mcLightTexture); - this.mcLightGpuSampler = gpuDevice.createSampler( - AddressMode.CLAMP_TO_EDGE, AddressMode.CLAMP_TO_EDGE, // U,V - FilterMode.NEAREST, FilterMode.NEAREST, // minFilter, magFilter - 1, // maxAnisotropy - OptionalDouble.empty() // maxLod - ); - } + //LightMapWrapper lightMapWrapper = (LightMapWrapper) renderEventParam.lightmap; + //McTextureViewWrapper lightmapTextureViewWrapper = lightMapWrapper.getTextureViewWrapper(); + //renderPass.bindTexture("uLightMap", lightmapTextureViewWrapper.textureView, lightmapTextureViewWrapper.textureSampler); { profiler.popPush("setup"); // create a render pass - Supplier debugLabelSupplier = () -> "distantHorizons:McLodRenderer"; OptionalInt optionalClearColorAsInt = OptionalInt.empty(); OptionalDouble optionalDepthValueAsDouble = OptionalDouble.empty(); - try(RenderPass renderPass = commandEncoder.createRenderPass( - debugLabelSupplier, - this.dhColorTextureView, + try(RenderPass renderPass = COMMAND_ENCODER.createRenderPass( + this::getName, + this.dhColorTextureWrapper.textureView, optionalClearColorAsInt, - this.dhDepthTextureView, optionalDepthValueAsDouble) + this.dhDepthTextureWrapper.textureView, optionalDepthValueAsDouble) ) { - //renderPass.pushDebugGroup(); - //renderPass.popDebugGroup(); - // bind MC Lightmap - renderPass.bindTexture("uLightMap", this.mcLightTextureView, this.mcLightGpuSampler); + //renderPass.bindTexture("uLightMap", this.mcLightTextureViewWrapper.textureView, this.mcLightTextureViewWrapper.textureSampler); + LightMapWrapper lightMapWrapper = (LightMapWrapper) renderEventParam.lightmap; + McTextureViewWrapper lightmapTextureViewWrapper = lightMapWrapper.getTextureViewWrapper(); + renderPass.bindTexture("uLightMap", lightmapTextureViewWrapper.textureView, lightmapTextureViewWrapper.textureSampler); // set pipeline renderPass.setPipeline(opaquePass ? this.opaquePipeline : this.transparentPipeline); @@ -397,7 +337,6 @@ public class McLodRenderer implements IMcLodRenderer - for (int lodIndex = 0; lodIndex < bufferContainers.size(); lodIndex++) { profiler.popPush("binding"); @@ -454,39 +393,19 @@ public class McLodRenderer implements IMcLodRenderer profiler.pop(); } + private String getName() { return "distantHorizons:McLodRenderer"; } @Override public void applyToMcTexture() { - //McApplyRenderer.INSTANCE.render(); - GpuTexture mcColorTexture = Minecraft.getInstance().getMainRenderTarget().getColorTexture(); - this.applyRenderer.render(this.dhColorTexture, this.dhDepthTexture, mcColorTexture); + this.applyRenderer.render(this.dhColorTextureWrapper.texture, this.dhDepthTextureWrapper.texture, mcColorTexture); } @Override - public void clearDepth() - { - GpuDevice gpuDevice = RenderSystem.getDevice(); - CommandEncoder commandEncoder = gpuDevice.createCommandEncoder(); - - if (this.dhDepthTexture != null) - { - commandEncoder.clearDepthTexture(this.dhDepthTexture, 1.0f); - } - } - + public void clearDepth() { this.dhDepthTextureWrapper.clearDepth(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)); - } - } + public void clearColor() { this.dhColorTextureWrapper.clearColor(ColorUtil.argbToInt(1, 1, 1, 1)); } //endregion 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 index 4f0bb334b..3d09de693 100644 --- 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 @@ -32,6 +32,8 @@ 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.common.renderTest.helpers.McTextureViewWrapper; +import com.seibel.distanthorizons.common.renderTest.helpers.McTextureWrapper; import com.seibel.distanthorizons.core.logging.DhLogger; import com.seibel.distanthorizons.core.logging.DhLoggerBuilder; import net.minecraft.resources.Identifier; @@ -65,13 +67,10 @@ public class DhApplyRenderer private final String vertexShaderPath; private final String fragmentShaderPath; - private GpuTextureView sourceColorTextureView; - private GpuSampler sourceColorSampler; + private final McTextureViewWrapper sourceColorTextureViewWrapper = new McTextureViewWrapper(); + private final McTextureViewWrapper sourceDepthTextureViewWrapper = new McTextureViewWrapper(); - private GpuTextureView sourceDepthTextureView; - private GpuSampler sourceDepthSampler; - - private GpuTextureView destinationColorTextureView; + private final McTextureViewWrapper destinationColorTextureViewWrapper = new McTextureViewWrapper(); @@ -100,7 +99,12 @@ public class DhApplyRenderer { this.createPipeline(); this.uploadVertexData(); - this.createTextureViews(sourceColorTexture, sourceDepthTexture, destinationColorTexture); + + this.sourceColorTextureViewWrapper.trySetup(sourceColorTexture); + this.sourceDepthTextureViewWrapper.trySetup(sourceDepthTexture); + + this.destinationColorTextureViewWrapper.trySetup(destinationColorTexture); + } private void createPipeline() { @@ -175,60 +179,6 @@ public class DhApplyRenderer 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 @@ -247,12 +197,14 @@ public class DhApplyRenderer this.tryInit(sourceColorTexture, sourceDepthTexture, destinationColorTexture); try (RenderPass renderPass = COMMAND_ENCODER.createRenderPass( - this::getIdentifierName, - this.destinationColorTextureView, /*optionalClearColorAsInt*/ OptionalInt.empty(), - /*depthTexture*/ null, /*optionalDepthValueAsDouble*/ OptionalDouble.empty())) + this::getIdentifierName, + this.destinationColorTextureViewWrapper.textureView, + /*optionalClearColorAsInt*/ OptionalInt.empty(), + /*depthTexture*/ null, + /*optionalDepthValueAsDouble*/ OptionalDouble.empty())) { - renderPass.bindTexture("uSourceColorTexture", this.sourceColorTextureView, this.sourceColorSampler); - renderPass.bindTexture("uSourceDepthTexture", this.sourceDepthTextureView, this.sourceDepthSampler); + renderPass.bindTexture("uSourceColorTexture", this.sourceColorTextureViewWrapper.textureView, this.sourceColorTextureViewWrapper.textureSampler); + renderPass.bindTexture("uSourceDepthTexture", this.sourceDepthTextureViewWrapper.textureView, this.sourceDepthTextureViewWrapper.textureSampler); renderPass.setVertexBuffer(0, this.vboGpuBuffer); renderPass.setPipeline(this.pipeline); 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 d70c06e05..d791a93ef 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 @@ -31,6 +31,8 @@ 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.common.renderTest.helpers.McTextureViewWrapper; +import com.seibel.distanthorizons.common.renderTest.helpers.McTextureWrapper; import com.seibel.distanthorizons.core.dependencyInjection.SingletonInjector; import com.seibel.distanthorizons.core.logging.DhLogger; import com.seibel.distanthorizons.core.logging.DhLoggerBuilder; @@ -49,10 +51,10 @@ import java.util.function.Supplier; */ public class McCopyRenderer { - public static final DhLogger LOGGER = new DhLoggerBuilder().build(); + 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); + private static final GpuDevice GPU_DEVICE = RenderSystem.getDevice(); + private static final CommandEncoder COMMAND_ENCODER = GPU_DEVICE.createCommandEncoder(); public static final McCopyRenderer INSTANCE = new McCopyRenderer(); @@ -154,66 +156,48 @@ public class McCopyRenderer //========// //region - public void render(GpuTexture sourceTexture, GpuTexture destinationTexture) + public void render( + McTextureWrapper sourceColorTextureWrapper, + McTextureViewWrapper destinationColorTextureWrapper) + { + this.render( + sourceColorTextureWrapper.textureView, sourceColorTextureWrapper.textureSampler, + destinationColorTextureWrapper.textureView); + } + public void render( + McTextureWrapper sourceColorTextureWrapper, + McTextureWrapper destinationColorTextureWrapper) + { + this.render( + sourceColorTextureWrapper.textureView, sourceColorTextureWrapper.textureSampler, + destinationColorTextureWrapper.textureView); + } + + private void render( + GpuTextureView sourceTextureView, + GpuSampler sourceTextureSampler, + GpuTextureView destinationTextureView) { this.tryInit(); - - - GpuDevice gpuDevice = RenderSystem.getDevice(); - CommandEncoder commandEncoder = gpuDevice.createCommandEncoder(); - - - - // create a render pass + try (RenderPass renderPass = COMMAND_ENCODER.createRenderPass( + this::getName, + destinationTextureView, + /*optionalClearColorAsInt*/ OptionalInt.empty(), + /*depthTexture*/ null, + /*optionalDepthValueAsDouble*/ OptionalDouble.empty())) { - Supplier debugLabelSupplier = () -> "distantHorizons:McCopyRenderer"; - GpuTextureView colorTexture = gpuDevice.createTextureView(destinationTexture); - OptionalInt optionalClearColorAsInt = OptionalInt.empty(); - GpuTextureView depthTexture = null; - OptionalDouble optionalDepthValueAsDouble = OptionalDouble.empty(); + renderPass.bindTexture("uCopyTexture", sourceTextureView, sourceTextureSampler); - 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); - } - } + renderPass.setVertexBuffer(0, this.vboGpuBuffer); // vertex buffer can only be "0" lol + + renderPass.setPipeline(this.pipeline); + renderPass.draw(/*indexStart*/ 0, /*indexCount*/ 4); } } + private String getName() { return "distantHorizons:McCopyRenderer"; } + //endregion diff --git a/common/src/main/java/com/seibel/distanthorizons/common/renderTest/helpers/AbstractUniformBufferWrapper.java b/common/src/main/java/com/seibel/distanthorizons/common/renderTest/helpers/AbstractUniformBufferWrapper.java index 3c78a2bd8..e2703eef8 100644 --- a/common/src/main/java/com/seibel/distanthorizons/common/renderTest/helpers/AbstractUniformBufferWrapper.java +++ b/common/src/main/java/com/seibel/distanthorizons/common/renderTest/helpers/AbstractUniformBufferWrapper.java @@ -5,6 +5,8 @@ import com.mojang.blaze3d.buffers.GpuBufferSlice; import com.mojang.blaze3d.systems.CommandEncoder; import com.mojang.blaze3d.systems.GpuDevice; import com.mojang.blaze3d.systems.RenderSystem; +import com.seibel.distanthorizons.core.logging.DhLogger; +import com.seibel.distanthorizons.core.logging.DhLoggerBuilder; import com.seibel.distanthorizons.core.wrapperInterfaces.render.IUniformBufferWrapper; import java.nio.ByteBuffer; @@ -12,6 +14,9 @@ import java.nio.ByteOrder; public abstract class AbstractUniformBufferWrapper implements IUniformBufferWrapper { + private static final DhLogger LOGGER = new DhLoggerBuilder().build(); + + private final String name; private ByteBuffer buffer = null; @@ -62,7 +67,14 @@ public abstract class AbstractUniformBufferWrapper implements IUniformBufferWrap int byteSize = (this.buffer.limit() - this.buffer.position()); GpuBufferSlice bufferSlice = new GpuBufferSlice(this.gpuBuffer, /*offset*/0, byteSize); - commandEncoder.writeToBuffer(bufferSlice, this.buffer); + if (!bufferSlice.buffer().isClosed()) + { + commandEncoder.writeToBuffer(bufferSlice, this.buffer); + } + else + { + LOGGER.warn("Uploading to buffer ["+this.name+"] failed due to already being closed"); + } } private String getName() { return this.name; } diff --git a/common/src/main/java/com/seibel/distanthorizons/common/renderTest/helpers/McTextureViewWrapper.java b/common/src/main/java/com/seibel/distanthorizons/common/renderTest/helpers/McTextureViewWrapper.java new file mode 100644 index 000000000..93e93b334 --- /dev/null +++ b/common/src/main/java/com/seibel/distanthorizons/common/renderTest/helpers/McTextureViewWrapper.java @@ -0,0 +1,59 @@ +package com.seibel.distanthorizons.common.renderTest.helpers; + +import com.mojang.blaze3d.systems.CommandEncoder; +import com.mojang.blaze3d.systems.GpuDevice; +import com.mojang.blaze3d.systems.RenderSystem; +import com.mojang.blaze3d.textures.*; +import com.seibel.distanthorizons.core.logging.DhLogger; +import com.seibel.distanthorizons.core.logging.DhLoggerBuilder; + +import java.util.OptionalDouble; + +public class McTextureViewWrapper +{ + 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(); + + + + public GpuTextureView textureView = null; + public GpuSampler textureSampler = null; + + + + public void trySetup(GpuTexture texture) + { + this.tryRecreateTextureView(texture); + this.tryCreateSampler(); + } + private void tryRecreateTextureView(GpuTexture texture) + { + if (this.textureView == null + || this.textureView.texture() != texture) + { + if (this.textureView != null) + { + this.textureView.close(); + } + + this.textureView = GPU_DEVICE.createTextureView(texture); + } + } + private void tryCreateSampler() + { + if (this.textureSampler == null) + { + this.textureSampler = GPU_DEVICE.createSampler( + AddressMode.CLAMP_TO_EDGE, AddressMode.CLAMP_TO_EDGE, // U,V + FilterMode.NEAREST, FilterMode.NEAREST, // minFilter, magFilter + 1, // maxAnisotropy + OptionalDouble.empty() // maxLod + ); + } + } + + + +} diff --git a/common/src/main/java/com/seibel/distanthorizons/common/renderTest/helpers/McTextureWrapper.java b/common/src/main/java/com/seibel/distanthorizons/common/renderTest/helpers/McTextureWrapper.java new file mode 100644 index 000000000..cc5338bfc --- /dev/null +++ b/common/src/main/java/com/seibel/distanthorizons/common/renderTest/helpers/McTextureWrapper.java @@ -0,0 +1,113 @@ +package com.seibel.distanthorizons.common.renderTest.helpers; + +import com.mojang.blaze3d.systems.CommandEncoder; +import com.mojang.blaze3d.systems.GpuDevice; +import com.mojang.blaze3d.systems.RenderSystem; +import com.mojang.blaze3d.textures.*; +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.ColorUtil; +import com.seibel.distanthorizons.core.wrapperInterfaces.minecraft.IMinecraftRenderWrapper; + +import java.util.OptionalDouble; + +public class McTextureWrapper +{ + public static final DhLogger LOGGER = new DhLoggerBuilder().build(); + + private static final IMinecraftRenderWrapper MC_RENDER = SingletonInjector.INSTANCE.get(IMinecraftRenderWrapper.class); + + private static final GpuDevice GPU_DEVICE = RenderSystem.getDevice(); + private static final CommandEncoder COMMAND_ENCODER = GPU_DEVICE.createCommandEncoder(); + + + public final String name; + public final TextureFormat textureFormat; + + public GpuTexture texture = null; + public GpuTextureView textureView = null; + public GpuSampler textureSampler = null; + + + + public static McTextureWrapper createDepth(String name) { return new McTextureWrapper(name, TextureFormat.DEPTH32); } + public static McTextureWrapper createColor(String name) { return new McTextureWrapper(name, TextureFormat.RGBA8); } + + private McTextureWrapper(String name, TextureFormat textureFormat) + { + this.name = name; + this.textureFormat = textureFormat; + } + + + + public boolean isEmpty() { return this.texture == null; } + + public void trySetup() + { + this.tryCreateTexture(); + this.tryCreateSampler(); + } + private void tryCreateTexture() + { + int viewWidth = MC_RENDER.getTargetFramebufferViewportWidth(); + int textureWidth = (this.texture != null) ? this.texture.getWidth(0) : -1; + + int viewHeight = MC_RENDER.getTargetFramebufferViewportHeight(); + int textureHeight = (this.texture != null) ? this.texture.getHeight(0) : -1; + + if (this.texture == null + || textureWidth != viewWidth + || textureHeight != viewHeight) + { + if (this.texture != null) + { + this.texture.close(); + this.textureView.close(); + } + + // TODO USAGE_TEXTURE_BINDING = 4 + int usage = 4 | 8 | 32 | 128; + this.texture = GPU_DEVICE.createTexture(this.name, + usage, + this.textureFormat, + viewWidth, viewHeight, + /*depthOrLayers*/ 1, /*mipLevels*/ 1 + ); + this.textureView = GPU_DEVICE.createTextureView(this.texture); + } + } + private void tryCreateSampler() + { + if (this.textureSampler == null) + { + this.textureSampler = GPU_DEVICE.createSampler( + AddressMode.CLAMP_TO_EDGE, AddressMode.CLAMP_TO_EDGE, // U,V + FilterMode.NEAREST, FilterMode.NEAREST, // minFilter, magFilter + 1, // maxAnisotropy + OptionalDouble.empty() // maxLod + ); + } + } + + + /** @see ColorUtil#argbToInt */ + public void clearColor(int clearArgbColor) + { + if (this.texture != null) + { + COMMAND_ENCODER.clearColorTexture(this.texture, clearArgbColor); + } + } + public void clearDepth(float depth) + { + if (this.texture != null) + { + COMMAND_ENCODER.clearDepthTexture(this.texture, depth); + } + } + + + +} diff --git a/common/src/main/java/com/seibel/distanthorizons/common/renderTest/postProcessing/McFarFadeRenderer.java b/common/src/main/java/com/seibel/distanthorizons/common/renderTest/postProcessing/McFarFadeRenderer.java index 2e0a85cbb..a99d313e0 100644 --- a/common/src/main/java/com/seibel/distanthorizons/common/renderTest/postProcessing/McFarFadeRenderer.java +++ b/common/src/main/java/com/seibel/distanthorizons/common/renderTest/postProcessing/McFarFadeRenderer.java @@ -36,6 +36,8 @@ import com.mojang.blaze3d.vertex.VertexFormat; import com.seibel.distanthorizons.common.renderTest.helpers.DhVertexFormat; import com.seibel.distanthorizons.common.renderTest.apply.McCopyRenderer; import com.seibel.distanthorizons.common.renderTest.McLodRenderer; +import com.seibel.distanthorizons.common.renderTest.helpers.McTextureViewWrapper; +import com.seibel.distanthorizons.common.renderTest.helpers.McTextureWrapper; import com.seibel.distanthorizons.common.renderTest.helpers.UniformHandler; import com.seibel.distanthorizons.core.dependencyInjection.SingletonInjector; import com.seibel.distanthorizons.core.logging.DhLogger; @@ -64,6 +66,9 @@ public class McFarFadeRenderer implements IMcFarFadeRenderer private static final IMinecraftRenderWrapper MC_RENDER = SingletonInjector.INSTANCE.get(IMinecraftRenderWrapper.class); private static final IMinecraftGLWrapper GLMC = SingletonInjector.INSTANCE.get(IMinecraftGLWrapper.class); + private static final GpuDevice GPU_DEVICE = RenderSystem.getDevice(); + private static final CommandEncoder COMMAND_ENCODER = GPU_DEVICE.createCommandEncoder(); + public static final McFarFadeRenderer INSTANCE = new McFarFadeRenderer(); private VertexFormat vertexFormat; @@ -74,7 +79,8 @@ public class McFarFadeRenderer implements IMcFarFadeRenderer private GpuBuffer vboGpuBuffer; - public GpuTexture fadeColorTexture; + public final McTextureWrapper dhFadeColorTextureWrapper = McTextureWrapper.createColor("DhFadeColorTexture"); + public final McTextureViewWrapper mcColorTextureViewWrapper = new McTextureViewWrapper(); @@ -180,38 +186,17 @@ public class McFarFadeRenderer implements IMcFarFadeRenderer this.tryInit(); - if (McLodRenderer.INSTANCE.dhDepthTexture == null - || McLodRenderer.INSTANCE.dhColorTexture == null) + if (McLodRenderer.INSTANCE.dhDepthTextureWrapper.isEmpty() + || McLodRenderer.INSTANCE.dhColorTextureWrapper.isEmpty()) { 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 - ); - } - + this.dhFadeColorTextureWrapper.trySetup(); + this.mcColorTextureViewWrapper.trySetup(Minecraft.getInstance().getMainRenderTarget().getColorTexture()); { int uniformBufferSize = new Std140SizeCalculator() @@ -252,12 +237,12 @@ public class McFarFadeRenderer implements IMcFarFadeRenderer this.fragUniformBuffer = UniformHandler.createBuffer("fragUniformBlock", uniformBufferSize, this.fragUniformBuffer); GpuBufferSlice bufferSlice = new GpuBufferSlice(this.fragUniformBuffer, 0, uniformBufferSize); - commandEncoder.writeToBuffer(bufferSlice, buffer); + COMMAND_ENCODER.writeToBuffer(bufferSlice, buffer); } this.renderFadeToTexture(); - McCopyRenderer.INSTANCE.render(this.fadeColorTexture, McLodRenderer.INSTANCE.dhColorTexture); + McCopyRenderer.INSTANCE.render(this.dhFadeColorTextureWrapper, McLodRenderer.INSTANCE.dhColorTextureWrapper); } @@ -266,81 +251,29 @@ public class McFarFadeRenderer implements IMcFarFadeRenderer GpuDevice gpuDevice = RenderSystem.getDevice(); CommandEncoder commandEncoder = gpuDevice.createCommandEncoder(); - // create a render pass - Supplier debugLabelSupplier = () -> "distantHorizons:McFadeRenderer"; - GpuTextureView colorTexture = gpuDevice.createTextureView(this.fadeColorTexture); - OptionalInt optionalClearColorAsInt = OptionalInt.empty(); - GpuTextureView depthTexture = null; - OptionalDouble optionalDepthValueAsDouble = OptionalDouble.empty(); - try (RenderPass renderPass = commandEncoder.createRenderPass( - debugLabelSupplier, - colorTexture, - optionalClearColorAsInt, - depthTexture, optionalDepthValueAsDouble)) + this::getName, + this.dhFadeColorTextureWrapper.textureView, + /*optionalClearColorAsInt*/ OptionalInt.empty(), + /*depthTexture*/ null, + /*optionalDepthValueAsDouble*/ OptionalDouble.empty())) { - //renderPass.pushDebugGroup(); - //renderPass.popDebugGroup(); + // MC texture + renderPass.bindTexture("uMcColorTexture", this.mcColorTextureViewWrapper.textureView, this.mcColorTextureViewWrapper.textureSampler); + // DH textures + renderPass.bindTexture("uDhDepthTexture", McLodRenderer.INSTANCE.dhDepthTextureWrapper.textureView, McLodRenderer.INSTANCE.dhDepthTextureWrapper.textureSampler); + renderPass.bindTexture("uDhColorTexture", McLodRenderer.INSTANCE.dhColorTextureWrapper.textureView, McLodRenderer.INSTANCE.dhColorTextureWrapper.textureSampler); - // render pass setup - { - // 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("uMcColorTexture", 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 - 1, // maxAnisotropy - OptionalDouble.empty() // maxLod - ); - renderPass.bindTexture("uDhDepthTexture", textureView, gpuSampler); - } - - // bind DH 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); - } - - - - 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.setUniform("fragUniformBlock", this.fragUniformBuffer); - // draw render pass - { - int indexStart = 0; - int indexCount = 4; - renderPass.draw(indexStart, indexCount); - } + renderPass.setVertexBuffer(0, this.vboGpuBuffer); // vertex buffer can only be "0" lol + renderPass.setPipeline(this.pipeline); + + renderPass.draw(/*indexStart*/ 0, /*indexCount*/ 4); } } + private String getName() { return "distantHorizons:McFadeRenderer"; } //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 5998f078f..e0e251937 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 @@ -43,6 +43,7 @@ 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.McTextureWrapper; import com.seibel.distanthorizons.common.renderTest.helpers.UniformHandler; import com.seibel.distanthorizons.core.config.Config; import com.seibel.distanthorizons.core.dependencyInjection.SingletonInjector; @@ -72,7 +73,9 @@ public class McFogRenderer implements IMcFogRenderer private static final IMinecraftClientWrapper MC = SingletonInjector.INSTANCE.get(IMinecraftClientWrapper.class); private static final IMinecraftRenderWrapper MC_RENDER = SingletonInjector.INSTANCE.get(IMinecraftRenderWrapper.class); - private static final IMinecraftGLWrapper GLMC = SingletonInjector.INSTANCE.get(IMinecraftGLWrapper.class); + + private static final GpuDevice GPU_DEVICE = RenderSystem.getDevice(); + private static final CommandEncoder COMMAND_ENCODER = GPU_DEVICE.createCommandEncoder(); public static final McFogRenderer INSTANCE = new McFogRenderer(); @@ -87,7 +90,7 @@ public class McFogRenderer implements IMcFogRenderer private GpuBuffer vboGpuBuffer; - public GpuTexture fogColorTexture; + public McTextureWrapper fogColorTextureWrapper = McTextureWrapper.createColor("DhFogColorTexture"); @@ -139,9 +142,6 @@ public class McFogRenderer implements IMcFogRenderer pipelineBuilder.withVertexShader(Identifier.fromNamespaceAndPath("distanthorizons", "fog/quad_apply")); pipelineBuilder.withFragmentShader(Identifier.fromNamespaceAndPath("distanthorizons", "fog/fog")); - pipelineBuilder.withSampler("uMcDepthTexture"); - pipelineBuilder.withSampler("uCombinedMcDhColorTexture"); - pipelineBuilder.withSampler("uDhDepthTexture"); pipelineBuilder.withUniform("fragUniformBlock", UniformType.UNIFORM_BUFFER); @@ -201,37 +201,15 @@ public class McFogRenderer implements IMcFogRenderer this.tryInit(); - if (McLodRenderer.INSTANCE.dhDepthTexture == null - || McLodRenderer.INSTANCE.dhColorTexture == null) + if (McLodRenderer.INSTANCE.dhDepthTextureWrapper.isEmpty() + || McLodRenderer.INSTANCE.dhColorTextureWrapper.isEmpty()) { return; } - GpuDevice gpuDevice = RenderSystem.getDevice(); - CommandEncoder commandEncoder = gpuDevice.createCommandEncoder(); - - - // textures - if (this.fogColorTexture == null - || this.fogColorTexture.getWidth(0) != MC_RENDER.getTargetFramebufferViewportWidth() - || this.fogColorTexture.getHeight(0) != MC_RENDER.getTargetFramebufferViewportHeight()) - { - if (this.fogColorTexture != null) - { - this.fogColorTexture.close(); - } - - // TODO USAGE_TEXTURE_BINDING = 4 - int usage = 4 | 8 | 32 | 128; - this.fogColorTexture = gpuDevice.createTexture("FogColorTexture", - usage, - TextureFormat.RGBA8, - MC_RENDER.getTargetFramebufferViewportWidth(), MC_RENDER.getTargetFramebufferViewportHeight(), - 1, 1 - ); - } + this.fogColorTextureWrapper.trySetup(); { @@ -372,12 +350,12 @@ public class McFogRenderer implements IMcFogRenderer this.fragUniformBuffer = UniformHandler.createBuffer("fragUniformBlock", uniformBufferSize, this.fragUniformBuffer); GpuBufferSlice bufferSlice = new GpuBufferSlice(this.fragUniformBuffer, 0, uniformBufferSize); - commandEncoder.writeToBuffer(bufferSlice, buffer); + COMMAND_ENCODER.writeToBuffer(bufferSlice, buffer); } this.renderFogToTexture(); - this.applyRenderer.render(this.fogColorTexture, McLodRenderer.INSTANCE.dhDepthTexture, McLodRenderer.INSTANCE.dhColorTexture); + this.applyRenderer.render(this.fogColorTextureWrapper.texture, McLodRenderer.INSTANCE.dhDepthTextureWrapper.texture, McLodRenderer.INSTANCE.dhColorTextureWrapper.texture); } @@ -399,58 +377,24 @@ public class McFogRenderer implements IMcFogRenderer private void renderFogToTexture() { - GpuDevice gpuDevice = RenderSystem.getDevice(); - CommandEncoder commandEncoder = gpuDevice.createCommandEncoder(); - - // create a render pass - Supplier debugLabelSupplier = () -> "distantHorizons:McFogRenderer"; - GpuTextureView colorTexture = gpuDevice.createTextureView(this.fogColorTexture); - OptionalInt optionalClearColorAsInt = OptionalInt.empty(); - GpuTextureView depthTexture = null; - OptionalDouble optionalDepthValueAsDouble = OptionalDouble.empty(); - - try (RenderPass renderPass = commandEncoder.createRenderPass( - debugLabelSupplier, - colorTexture, - optionalClearColorAsInt, - depthTexture, optionalDepthValueAsDouble)) + try (RenderPass renderPass = COMMAND_ENCODER.createRenderPass( + this::getName, + this.fogColorTextureWrapper.textureView, + /*optionalClearColorAsInt*/ OptionalInt.empty(), + /*depthTexture*/ null, + /*optionalDepthValueAsDouble*/ OptionalDouble.empty())) { - //renderPass.pushDebugGroup(); - //renderPass.popDebugGroup(); + renderPass.bindTexture("uDhDepthTexture", this.fogColorTextureWrapper.textureView, this.fogColorTextureWrapper.textureSampler); + renderPass.setUniform("fragUniformBlock", this.fragUniformBuffer); - // render pass setup - { - // 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 - 1, // maxAnisotropy - OptionalDouble.empty() // maxLod - ); - renderPass.bindTexture("uDhDepthTexture", 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.setVertexBuffer(0, this.vboGpuBuffer); // vertex buffer can only be "0" lol + renderPass.setPipeline(this.pipeline); - // draw render pass - { - int indexStart = 0; - int indexCount = 4; - renderPass.draw(indexStart, indexCount); - } + renderPass.draw(/*indexStart*/ 0, /*indexCount*/ 4); } } + private String getName() { return "distantHorizons:McFogRenderer"; } //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 bea03b91e..25e9db043 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 @@ -40,6 +40,8 @@ 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.McTextureViewWrapper; +import com.seibel.distanthorizons.common.renderTest.helpers.McTextureWrapper; import com.seibel.distanthorizons.common.renderTest.helpers.UniformHandler; import com.seibel.distanthorizons.core.dependencyInjection.SingletonInjector; import com.seibel.distanthorizons.core.logging.DhLogger; @@ -64,7 +66,9 @@ public class McSsaoRenderer implements IMcSsaoRenderer 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); + + private static final GpuDevice GPU_DEVICE = RenderSystem.getDevice(); + private static final CommandEncoder COMMAND_ENCODER = GPU_DEVICE.createCommandEncoder(); public static final McSsaoRenderer INSTANCE = new McSsaoRenderer(); @@ -79,7 +83,7 @@ public class McSsaoRenderer implements IMcSsaoRenderer private GpuBuffer vboGpuBuffer; - public GpuTexture ssaoColorTexture; + public McTextureWrapper ssaoColorTextureWrapper = McTextureWrapper.createColor("DhSsaoTexture"); @@ -128,9 +132,6 @@ public class McSsaoRenderer implements IMcSsaoRenderer pipelineBuilder.withVertexShader(Identifier.fromNamespaceAndPath("distanthorizons", "ssao/quad_apply")); pipelineBuilder.withFragmentShader(Identifier.fromNamespaceAndPath("distanthorizons", "ssao/ao")); - pipelineBuilder.withSampler("uMcDepthTexture"); - pipelineBuilder.withSampler("uCombinedMcDhColorTexture"); - pipelineBuilder.withSampler("uDhDepthTexture"); pipelineBuilder.withUniform("fragUniformBlock", UniformType.UNIFORM_BUFFER); @@ -190,38 +191,16 @@ public class McSsaoRenderer implements IMcSsaoRenderer this.tryInit(); - if (McLodRenderer.INSTANCE.dhDepthTexture == null - || McLodRenderer.INSTANCE.dhColorTexture == null) + if (McLodRenderer.INSTANCE.dhDepthTextureWrapper.isEmpty() + || McLodRenderer.INSTANCE.dhColorTextureWrapper.isEmpty()) { return; } - GpuDevice gpuDevice = RenderSystem.getDevice(); - CommandEncoder commandEncoder = gpuDevice.createCommandEncoder(); - - // textures - if (this.ssaoColorTexture == null - || this.ssaoColorTexture.getWidth(0) != MC_RENDER.getTargetFramebufferViewportWidth() - || this.ssaoColorTexture.getHeight(0) != MC_RENDER.getTargetFramebufferViewportHeight()) - { - if (this.ssaoColorTexture != null) - { - this.ssaoColorTexture.close(); - } - - // TODO USAGE_TEXTURE_BINDING = 4 - int usage = 4 | 8 | 32 | 128; - this.ssaoColorTexture = gpuDevice.createTexture("SsaoColorTexture", - usage, - TextureFormat.RGBA8, - MC_RENDER.getTargetFramebufferViewportWidth(), MC_RENDER.getTargetFramebufferViewportHeight(), - 1, 1 - ); - } - + this.ssaoColorTextureWrapper.trySetup(); { int uniformBufferSize = new Std140SizeCalculator() @@ -266,69 +245,35 @@ public class McSsaoRenderer implements IMcSsaoRenderer this.fragUniformBuffer = UniformHandler.createBuffer("fragUniformBlock", uniformBufferSize, this.fragUniformBuffer); GpuBufferSlice bufferSlice = new GpuBufferSlice(this.fragUniformBuffer, 0, uniformBufferSize); - commandEncoder.writeToBuffer(bufferSlice, buffer); + COMMAND_ENCODER.writeToBuffer(bufferSlice, buffer); } this.renderSsaoToTexture(); - this.applyRenderer.render(this.ssaoColorTexture, McLodRenderer.INSTANCE.dhDepthTexture, McLodRenderer.INSTANCE.dhColorTexture); + this.applyRenderer.render(this.ssaoColorTextureWrapper.texture, McLodRenderer.INSTANCE.dhDepthTextureWrapper.texture, McLodRenderer.INSTANCE.dhColorTextureWrapper.texture); } private void renderSsaoToTexture() { - GpuDevice gpuDevice = RenderSystem.getDevice(); - CommandEncoder commandEncoder = gpuDevice.createCommandEncoder(); - - // create a render pass - Supplier debugLabelSupplier = () -> "distantHorizons:McSsaoRenderer"; - GpuTextureView colorTexture = gpuDevice.createTextureView(this.ssaoColorTexture); - OptionalInt optionalClearColorAsInt = OptionalInt.empty(); - GpuTextureView depthTexture = null; - OptionalDouble optionalDepthValueAsDouble = OptionalDouble.empty(); - - try (RenderPass renderPass = commandEncoder.createRenderPass( - debugLabelSupplier, - colorTexture, - optionalClearColorAsInt, - depthTexture, optionalDepthValueAsDouble)) + try (RenderPass renderPass = COMMAND_ENCODER.createRenderPass( + this::getName, + this.ssaoColorTextureWrapper.textureView, + /*optionalClearColorAsInt*/ OptionalInt.empty(), + /*depthTexture*/ null, + /*optionalDepthValueAsDouble*/ OptionalDouble.empty())) { - //renderPass.pushDebugGroup(); - //renderPass.popDebugGroup(); + renderPass.bindTexture("uDhDepthTexture", McLodRenderer.INSTANCE.dhDepthTextureWrapper.textureView, McLodRenderer.INSTANCE.dhDepthTextureWrapper.textureSampler); + renderPass.setUniform("fragUniformBlock", this.fragUniformBuffer); - // render pass setup - { - // 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 - 1, // maxAnisotropy - OptionalDouble.empty() // maxLod - ); - renderPass.bindTexture("uDhDepthTexture", 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.setVertexBuffer(0, this.vboGpuBuffer); // vertex buffer can only be "0" lol - // draw render pass - { - int indexStart = 0; - int indexCount = 4; - renderPass.draw(indexStart, indexCount); - } + renderPass.setPipeline(this.pipeline); + renderPass.draw(/*indexStart*/ 0, /*indexCount*/ 4); } } + private String getName() { return "distantHorizons:McSsaoRenderer"; } //endregion 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 52f5fc9b5..ec91d7e08 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 @@ -36,6 +36,8 @@ import com.mojang.blaze3d.vertex.VertexFormat; import com.seibel.distanthorizons.common.renderTest.McLodRenderer; import com.seibel.distanthorizons.common.renderTest.apply.McCopyRenderer; import com.seibel.distanthorizons.common.renderTest.helpers.DhVertexFormat; +import com.seibel.distanthorizons.common.renderTest.helpers.McTextureViewWrapper; +import com.seibel.distanthorizons.common.renderTest.helpers.McTextureWrapper; import com.seibel.distanthorizons.common.renderTest.helpers.UniformHandler; import com.seibel.distanthorizons.core.config.Config; import com.seibel.distanthorizons.core.dependencyInjection.SingletonInjector; @@ -43,7 +45,6 @@ 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.IMcVanillaFadeRenderer; import com.seibel.distanthorizons.core.wrapperInterfaces.world.IClientLevelWrapper; @@ -64,7 +65,9 @@ public class McVanillaFadeRenderer implements IMcVanillaFadeRenderer 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); + + private static final GpuDevice GPU_DEVICE = RenderSystem.getDevice(); + private static final CommandEncoder COMMAND_ENCODER = GPU_DEVICE.createCommandEncoder(); public static final McVanillaFadeRenderer INSTANCE = new McVanillaFadeRenderer(); @@ -76,7 +79,10 @@ public class McVanillaFadeRenderer implements IMcVanillaFadeRenderer private GpuBuffer vboGpuBuffer; - public GpuTexture fadeColorTexture; + public final McTextureWrapper fadeColorTextureWrapper = McTextureWrapper.createColor("DhVanillaFadeTexture"); + + public final McTextureViewWrapper mcDepthTextureWrapper = new McTextureViewWrapper(); + public final McTextureViewWrapper mcColorTextureWrapper = new McTextureViewWrapper(); @@ -182,38 +188,18 @@ public class McVanillaFadeRenderer implements IMcVanillaFadeRenderer { this.tryInit(); - - if (McLodRenderer.INSTANCE.dhDepthTexture == null - || McLodRenderer.INSTANCE.dhColorTexture == null) + if (McLodRenderer.INSTANCE.dhDepthTextureWrapper.isEmpty() + || McLodRenderer.INSTANCE.dhColorTextureWrapper.isEmpty()) { 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 - ); - } + this.fadeColorTextureWrapper.trySetup(); + + this.mcDepthTextureWrapper.trySetup(Minecraft.getInstance().getMainRenderTarget().getDepthTexture()); + this.mcColorTextureWrapper.trySetup(Minecraft.getInstance().getMainRenderTarget().getColorTexture()); { @@ -273,108 +259,39 @@ public class McVanillaFadeRenderer implements IMcVanillaFadeRenderer this.fragUniformBuffer = UniformHandler.createBuffer("fragUniformBlock", uniformBufferSize, this.fragUniformBuffer); GpuBufferSlice bufferSlice = new GpuBufferSlice(this.fragUniformBuffer, 0, uniformBufferSize); - commandEncoder.writeToBuffer(bufferSlice, buffer); + COMMAND_ENCODER.writeToBuffer(bufferSlice, buffer); } this.renderFadeToTexture(); - McCopyRenderer.INSTANCE.render(this.fadeColorTexture, Minecraft.getInstance().getMainRenderTarget().getColorTexture()); + McCopyRenderer.INSTANCE.render(this.fadeColorTextureWrapper, this.mcColorTextureWrapper); } private void renderFadeToTexture() { - GpuDevice gpuDevice = RenderSystem.getDevice(); - CommandEncoder commandEncoder = gpuDevice.createCommandEncoder(); - - // create a render pass - Supplier debugLabelSupplier = () -> "distantHorizons:McFadeRenderer"; - GpuTextureView colorTexture = gpuDevice.createTextureView(this.fadeColorTexture); - OptionalInt optionalClearColorAsInt = OptionalInt.empty(); - GpuTextureView depthTexture = null; - OptionalDouble optionalDepthValueAsDouble = OptionalDouble.empty(); - - try (RenderPass renderPass = commandEncoder.createRenderPass( - debugLabelSupplier, - colorTexture, - optionalClearColorAsInt, - depthTexture, optionalDepthValueAsDouble)) + try (RenderPass renderPass = COMMAND_ENCODER.createRenderPass( + this::getRenderPassName, + this.fadeColorTextureWrapper.textureView, + /*optionalClearColorAsInt*/ OptionalInt.empty(), + /*depthTexture*/ null, + /*optionalDepthValueAsDouble*/ OptionalDouble.empty())) { - //renderPass.pushDebugGroup(); - //renderPass.popDebugGroup(); + renderPass.bindTexture("uMcDepthTexture", this.mcDepthTextureWrapper.textureView, this.mcDepthTextureWrapper.textureSampler); + renderPass.bindTexture("uCombinedMcDhColorTexture", this.mcColorTextureWrapper.textureView, this.mcColorTextureWrapper.textureSampler); + renderPass.bindTexture("uDhDepthTexture", McLodRenderer.INSTANCE.dhDepthTextureWrapper.textureView, McLodRenderer.INSTANCE.dhDepthTextureWrapper.textureSampler); + renderPass.bindTexture("uDhColorTexture", McLodRenderer.INSTANCE.dhColorTextureWrapper.textureView, McLodRenderer.INSTANCE.dhColorTextureWrapper.textureSampler); - // render pass setup - { - // bind MC depth texture - { - 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 - 1, // maxAnisotropy - OptionalDouble.empty() // maxLod - ); - renderPass.bindTexture("uDhDepthTexture", textureView, gpuSampler); - } - - // bind DH 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); - } - - - - 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.setUniform("fragUniformBlock", this.fragUniformBuffer); - // draw render pass - { - int indexStart = 0; - int indexCount = 4; - renderPass.draw(indexStart, indexCount); - } + renderPass.setVertexBuffer(0, this.vboGpuBuffer); // vertex buffer can only be "0" lol + + renderPass.setPipeline(this.pipeline); + renderPass.draw(/*indexStart*/ 0, /*indexCount*/ 4); } } - + private String getRenderPassName() { return "distantHorizons:McFadeRenderer"; } //endregion diff --git a/common/src/main/java/com/seibel/distanthorizons/common/renderTest/test/DhTestRenderer.java b/common/src/main/java/com/seibel/distanthorizons/common/renderTest/test/DhTestRenderer.java index dde624fd3..988d5f16b 100644 --- a/common/src/main/java/com/seibel/distanthorizons/common/renderTest/test/DhTestRenderer.java +++ b/common/src/main/java/com/seibel/distanthorizons/common/renderTest/test/DhTestRenderer.java @@ -50,7 +50,10 @@ import java.util.function.Supplier; */ public class DhTestRenderer implements IMcTestRenderer { - public static final DhLogger LOGGER = new DhLoggerBuilder().build(); + 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(); public static final DhTestRenderer INSTANCE = new DhTestRenderer(); @@ -80,10 +83,6 @@ public class DhTestRenderer implements IMcTestRenderer - GpuDevice gpuDevice = RenderSystem.getDevice(); - CommandEncoder commandEncoder = gpuDevice.createCommandEncoder(); - - VertexFormat vertexFormat = VertexFormat.builder() .add("vPosition", DhVertexFormat.SCREEN_POS) .add("vColor", DhVertexFormat.RGBA_FLOAT_COLOR) @@ -115,7 +114,7 @@ public class DhTestRenderer implements IMcTestRenderer this.pipeline = pipelineBuilder.build(); - this.mcColorTextureView = gpuDevice.createTextureView(Minecraft.getInstance().getMainRenderTarget().getColorTexture()); + this.mcColorTextureView = GPU_DEVICE.createTextureView(Minecraft.getInstance().getMainRenderTarget().getColorTexture()); this.uploadVertexData(); @@ -170,31 +169,19 @@ public class DhTestRenderer implements IMcTestRenderer { this.tryInit(); - - - GpuDevice gpuDevice = RenderSystem.getDevice(); - CommandEncoder commandEncoder = gpuDevice.createCommandEncoder(); - - - - // create a render pass + try (RenderPass renderPass = COMMAND_ENCODER.createRenderPass( + this::getName, + this.mcColorTextureView, + /*optionalClearColorAsInt*/ OptionalInt.empty(), + /*mcDepthTextureView*/ null, + /*optionalDepthValueAsDouble*/ OptionalDouble.empty())) { - OptionalInt optionalClearColorAsInt = OptionalInt.empty(); - GpuTextureView mcDepthTextureView = null; - OptionalDouble optionalDepthValueAsDouble = OptionalDouble.empty(); - - try (RenderPass renderPass = commandEncoder.createRenderPass( - () -> "distantHorizons:DhTestRenderer", - this.mcColorTextureView, - optionalClearColorAsInt, - mcDepthTextureView, optionalDepthValueAsDouble)) - { - renderPass.setVertexBuffer(0, this.vboGpuBuffer); - renderPass.setPipeline(this.pipeline); - renderPass.draw(/*indexStart*/ 0, /*indexCount*/ 3); - } + renderPass.setVertexBuffer(0, this.vboGpuBuffer); + renderPass.setPipeline(this.pipeline); + renderPass.draw(/*indexStart*/ 0, /*indexCount*/ 3); } } + private String getName() { return "distantHorizons:DhTestRenderer"; } //endregion diff --git a/common/src/main/java/com/seibel/distanthorizons/common/wrappers/misc/LightMapWrapper.java b/common/src/main/java/com/seibel/distanthorizons/common/wrappers/misc/LightMapWrapper.java index 9d3f57f59..3835720e1 100644 --- a/common/src/main/java/com/seibel/distanthorizons/common/wrappers/misc/LightMapWrapper.java +++ b/common/src/main/java/com/seibel/distanthorizons/common/wrappers/misc/LightMapWrapper.java @@ -21,6 +21,7 @@ package com.seibel.distanthorizons.common.wrappers.misc; import com.mojang.blaze3d.platform.NativeImage; import com.mojang.blaze3d.textures.GpuTexture; +import com.seibel.distanthorizons.common.renderTest.helpers.McTextureViewWrapper; import com.seibel.distanthorizons.core.dependencyInjection.SingletonInjector; import com.seibel.distanthorizons.core.logging.DhLoggerBuilder; import com.seibel.distanthorizons.core.wrapperInterfaces.minecraft.IMinecraftGLWrapper; @@ -39,7 +40,9 @@ public class LightMapWrapper implements ILightMapWrapper private static final DhLogger LOGGER = new DhLoggerBuilder().build(); private int textureId = 0; - public GpuTexture gpuTexture = null; + + private GpuTexture gpuTexture = null; + private final McTextureViewWrapper lightmapTextureWrapper = new McTextureViewWrapper(); @@ -103,8 +106,8 @@ public class LightMapWrapper implements ILightMapWrapper public void setLightmapGpuTexture(GpuTexture gpuTexture) { - // just use the MC texture ID this.gpuTexture = gpuTexture; + this.lightmapTextureWrapper.trySetup(this.gpuTexture); } //endregion @@ -116,6 +119,8 @@ public class LightMapWrapper implements ILightMapWrapper //==============// //region + public McTextureViewWrapper getTextureViewWrapper() { return this.lightmapTextureWrapper; } + @Override public void bind() {