| 1 | package net.minecraft.client.renderer.oit;
|
| 2 |
|
| 3 | import com.mojang.blaze3d.systems.RenderSystem;
|
| 4 | import com.mojang.renderpearl.api.commands.RenderPass;
|
| 5 | import com.mojang.renderpearl.api.commands.RenderPassDescriptor;
|
| 6 | import com.mojang.renderpearl.api.textures.FilterMode;
|
| 7 | import com.mojang.renderpearl.api.textures.GpuSampler;
|
| 8 | import com.mojang.renderpearl.api.textures.GpuTextureView;
|
| 9 | import java.util.Optional;
|
| 10 | import java.util.OptionalDouble;
|
| 11 | import java.util.function.Supplier;
|
| 12 | import net.fabricmc.api.EnvType;
|
| 13 | import net.fabricmc.api.Environment;
|
| 14 | import net.minecraft.client.renderer.LevelRenderer;
|
| 15 |
|
| 16 | @Environment(EnvType.CLIENT)
|
| 17 | public class OitRenderPassProvider {
|
| 18 | public static RenderPass createRenderPass(final OitStage stage, final Supplier<String> label, final OitRenderPassProvider.Parameters params) {
|
| 19 | return switch (stage) {
|
| 20 | case DEPTH_BOUNDS -> createDepthBoundsPass(label, params);
|
| 21 | case TRANSMITTANCE -> createTransmittancePass(label, params);
|
| 22 | case ACCUMULATE -> createAccumulatePass(label, params);
|
| 23 | default -> throw new IllegalArgumentException("Invalid OIT stage.");
|
| 24 | };
|
| 25 | }
|
| 26 |
|
| 27 | private static RenderPass createDepthBoundsPass(final Supplier<String> label, final OitRenderPassProvider.Parameters params) {
|
| 28 | RenderPassDescriptor descriptor = RenderPassDescriptor.builder(() -> "OIT Depth Bounds for " + label.get())
|
| 29 | .withColorAttachment(params.depthBoundsTargetView)
|
| 30 | .withDepthAttachment(params.depthTextureView)
|
| 31 | .build();
|
| 32 | RenderPass renderPass = RenderSystem.getDevice().createCommandEncoder().createRenderPass(descriptor);
|
| 33 | RenderSystem.bindDefaultUniforms(renderPass);
|
| 34 | return renderPass;
|
| 35 | }
|
| 36 |
|
| 37 | private static RenderPass createTransmittancePass(final Supplier<String> label, final OitRenderPassProvider.Parameters params) {
|
| 38 | RenderPassDescriptor.Builder descriptor = RenderPassDescriptor.builder(() -> "OIT Transmittance for " + label.get())
|
| 39 | .withDepthAttachment(params.depthTextureView, OptionalDouble.empty());
|
| 40 |
|
| 41 | for (int i = 0; i < LevelRenderer.OIT_TRANSMITTANCE_TARGET_COUNT; i++) {
|
| 42 | descriptor.withColorAttachment(params.transmittanceTargetViews[i], Optional.empty());
|
| 43 | }
|
| 44 |
|
| 45 | GpuSampler nearestSampler = RenderSystem.getSamplerCache().getClampToEdge(FilterMode.NEAREST);
|
| 46 | RenderPass renderPass = RenderSystem.getDevice().createCommandEncoder().createRenderPass(descriptor.build());
|
| 47 | RenderSystem.bindDefaultUniforms(renderPass);
|
| 48 | renderPass.setUniform("DepthBoundsSampler", params.depthBoundsTargetView, nearestSampler);
|
| 49 | return renderPass;
|
| 50 | }
|
| 51 |
|
| 52 | private static RenderPass createAccumulatePass(final Supplier<String> label, final OitRenderPassProvider.Parameters params) {
|
| 53 | RenderPassDescriptor descriptor = RenderPassDescriptor.builder(() -> "OIT Accumulate for " + label.get())
|
| 54 | .withColorAttachment(params.accumulateTargetView)
|
| 55 | .withDepthAttachment(params.depthTextureView)
|
| 56 | .build();
|
| 57 | RenderPass renderPass = RenderSystem.getDevice().createCommandEncoder().createRenderPass(descriptor);
|
| 58 | GpuSampler nearestSampler = RenderSystem.getSamplerCache().getClampToEdge(FilterMode.NEAREST);
|
| 59 | RenderSystem.bindDefaultUniforms(renderPass);
|
| 60 |
|
| 61 | for (int i = 0; i < LevelRenderer.OIT_TRANSMITTANCE_TARGET_COUNT; i++) {
|
| 62 | renderPass.setUniform("Coeff" + i, params.transmittanceTargetViews[i], nearestSampler);
|
| 63 | }
|
| 64 |
|
| 65 | renderPass.setUniform("DepthBoundsSampler", params.depthBoundsTargetView, nearestSampler);
|
| 66 | return renderPass;
|
| 67 | }
|
| 68 |
|
| 69 | @Environment(EnvType.CLIENT)
|
| 70 | public static final class Parameters {
|
| 71 | private GpuTextureView depthBoundsTargetView;
|
| 72 | private final GpuTextureView[] transmittanceTargetViews;
|
| 73 | private final GpuTextureView accumulateTargetView;
|
| 74 | private final GpuTextureView depthTextureView;
|
| 75 |
|
| 76 | public Parameters(
|
| 77 | final GpuTextureView depthBoundsTargetView,
|
| 78 | final GpuTextureView[] transmittanceTargetViews,
|
| 79 | final GpuTextureView accumulateTargetView,
|
| 80 | final GpuTextureView depthTextureView
|
| 81 | ) {
|
| 82 | this.depthBoundsTargetView = depthBoundsTargetView;
|
| 83 | this.transmittanceTargetViews = transmittanceTargetViews;
|
| 84 | this.accumulateTargetView = accumulateTargetView;
|
| 85 | this.depthTextureView = depthTextureView;
|
| 86 | }
|
| 87 |
|
| 88 | public void setDepthBoundsTargetView(final GpuTextureView depthBoundsTargetView) {
|
| 89 | this.depthBoundsTargetView = depthBoundsTargetView;
|
| 90 | }
|
| 91 | }
|
| 92 | }
|
| 93 |
|