| 1 | package net.minecraft.client.renderer.chunk;
|
| 2 |
|
| 3 | import com.mojang.blaze3d.systems.RenderSystem;
|
| 4 | import com.mojang.renderpearl.api.buffers.GpuBuffer;
|
| 5 | import com.mojang.renderpearl.api.buffers.GpuBufferSlice;
|
| 6 | import com.mojang.renderpearl.api.commands.RenderPass;
|
| 7 | import com.mojang.renderpearl.api.device.DeviceLimits;
|
| 8 | import com.mojang.renderpearl.api.pipeline.IndexType;
|
| 9 | import com.mojang.renderpearl.api.pipeline.PrimitiveTopology;
|
| 10 | import com.mojang.renderpearl.api.pipeline.RenderPipeline;
|
| 11 | import com.mojang.renderpearl.api.textures.FilterMode;
|
| 12 | import com.mojang.renderpearl.api.textures.GpuSampler;
|
| 13 | import com.mojang.renderpearl.api.textures.GpuTextureView;
|
| 14 | import java.util.EnumMap;
|
| 15 | import java.util.List;
|
| 16 | import java.util.Map;
|
| 17 | import net.fabricmc.api.EnvType;
|
| 18 | import net.fabricmc.api.Environment;
|
| 19 | import net.minecraft.client.Minecraft;
|
| 20 | import net.minecraft.client.renderer.GameRenderer;
|
| 21 | import net.minecraft.client.renderer.RenderPipelines;
|
| 22 | import net.minecraft.client.renderer.oit.OitRenderPassProvider;
|
| 23 | import net.minecraft.client.renderer.oit.OitStage;
|
| 24 | import org.jspecify.annotations.Nullable;
|
| 25 |
|
| 26 | @Environment(EnvType.CLIENT)
|
| 27 | public abstract class ChunkSectionsToRender {
|
| 28 | private final int maxIndicesRequired;
|
| 29 | private final GpuBufferSlice terrainTransformUBO;
|
| 30 |
|
| 31 | private ChunkSectionsToRender(final GpuBufferSlice terrainTransformUBO, final int maxIndicesRequired) {
|
| 32 | this.maxIndicesRequired = maxIndicesRequired;
|
| 33 | this.terrainTransformUBO = terrainTransformUBO;
|
| 34 | }
|
| 35 |
|
| 36 | protected abstract void render(
|
| 37 | final ChunkSectionLayer layer,
|
| 38 | final RenderPass renderPass,
|
| 39 | @Nullable GpuBuffer defaultIndexBuffer,
|
| 40 | @Nullable IndexType defaultIndexType,
|
| 41 | final @Nullable RenderPipeline renderPipelineOverride,
|
| 42 | final @Nullable RenderPipeline renderPipelineOverrideMultidraw
|
| 43 | );
|
| 44 |
|
| 45 | public void renderGroup(
|
| 46 | final ChunkSectionLayerGroup group, final RenderPass renderPass, final GpuSampler sampler, final GpuTextureView atlas, final boolean renderWireframeTerrain
|
| 47 | ) {
|
| 48 | GameRenderer gameRenderer = Minecraft.getInstance().gameRenderer;
|
| 49 | GpuTextureView lightmap = gameRenderer.lightmap();
|
| 50 | this.renderLayers(
|
| 51 | group.layers(),
|
| 52 | sampler,
|
| 53 | renderPass,
|
| 54 | atlas,
|
| 55 | lightmap,
|
| 56 | renderWireframeTerrain ? RenderPipelines.WIREFRAME : null,
|
| 57 | renderWireframeTerrain ? RenderPipelines.WIREFRAME_MULTIDRAW : null
|
| 58 | );
|
| 59 | }
|
| 60 |
|
| 61 | public void renderOit(
|
| 62 | final GpuSampler sampler, final OitStage stage, final OitRenderPassProvider.Parameters params, final GpuTextureView atlas, final GpuTextureView lightmap
|
| 63 | ) {
|
| 64 | try (RenderPass renderPass = OitRenderPassProvider.createRenderPass(stage, () -> "Terrain", params)) {
|
| 65 | this.renderLayers(
|
| 66 | ChunkSectionLayerGroup.TRANSLUCENT.layers(),
|
| 67 | sampler,
|
| 68 | renderPass,
|
| 69 | atlas,
|
| 70 | lightmap,
|
| 71 | RenderPipelines.OIT_TERRAIN.getPipeline(stage),
|
| 72 | RenderPipelines.OIT_TERRAIN_MULTIDRAW.getPipeline(stage)
|
| 73 | );
|
| 74 | }
|
| 75 | }
|
| 76 |
|
| 77 | private void renderLayers(
|
| 78 | final ChunkSectionLayer[] layers,
|
| 79 | final GpuSampler sampler,
|
| 80 | final RenderPass renderPass,
|
| 81 | final GpuTextureView atlas,
|
| 82 | final GpuTextureView lightmap,
|
| 83 | final @Nullable RenderPipeline renderPipelineOverride,
|
| 84 | final @Nullable RenderPipeline renderPipelineOverrideMultidraw
|
| 85 | ) {
|
| 86 | RenderSystem.AutoStorageIndexBuffer autoIndices = RenderSystem.getSequentialBuffer(PrimitiveTopology.QUADS);
|
| 87 | GpuBuffer defaultIndexBuffer = this.maxIndicesRequired == 0 ? null : autoIndices.getBuffer();
|
| 88 | IndexType defaultIndexType = this.maxIndicesRequired == 0 ? null : autoIndices.type();
|
| 89 | renderPass.setUniform("TerrainUniform", this.terrainTransformUBO);
|
| 90 | renderPass.setUniform("Sampler0", atlas, sampler);
|
| 91 | renderPass.setUniform("Sampler2", lightmap, RenderSystem.getSamplerCache().getClampToEdge(FilterMode.LINEAR));
|
| 92 |
|
| 93 | for (ChunkSectionLayer layer : layers) {
|
| 94 | renderPass.pushDebugGroup(() -> "Terrain layer: " + layer.label());
|
| 95 | this.render(layer, renderPass, defaultIndexBuffer, defaultIndexType, renderPipelineOverride, renderPipelineOverrideMultidraw);
|
| 96 | renderPass.popDebugGroup();
|
| 97 | }
|
| 98 | }
|
| 99 |
|
| 100 | @Environment(EnvType.CLIENT)
|
| 101 | public static final class DrawIndirect extends ChunkSectionsToRender {
|
| 102 | private final EnumMap<ChunkSectionLayer, List<ChunkSectionsToRender.GpuMultiDrawIndexedIndirect>> drawGroupsPerLayer;
|
| 103 | private final GpuBufferSlice chunkSectionInfos;
|
| 104 |
|
| 105 | public DrawIndirect(
|
| 106 | final GpuBufferSlice terrainTransformUBO,
|
| 107 | final EnumMap<ChunkSectionLayer, List<ChunkSectionsToRender.GpuMultiDrawIndexedIndirect>> drawGroupsPerLayer,
|
| 108 | final int maxIndicesRequired,
|
| 109 | final GpuBufferSlice chunkSectionInfos
|
| 110 | ) {
|
| 111 | super(terrainTransformUBO, maxIndicesRequired);
|
| 112 | this.drawGroupsPerLayer = drawGroupsPerLayer;
|
| 113 | this.chunkSectionInfos = chunkSectionInfos;
|
| 114 | }
|
| 115 |
|
| 116 | @Override
|
| 117 | protected void render(
|
| 118 | final ChunkSectionLayer layer,
|
| 119 | final RenderPass renderPass,
|
| 120 | final @Nullable GpuBuffer defaultIndexBuffer,
|
| 121 | final @Nullable IndexType defaultIndexType,
|
| 122 | final @Nullable RenderPipeline renderPipelineOverride,
|
| 123 | final @Nullable RenderPipeline renderPipelineOverrideMultidraw
|
| 124 | ) {
|
| 125 | renderPass.setPipeline(RenderSystem.getCompiledPipeline(renderPipelineOverrideMultidraw != null ? renderPipelineOverrideMultidraw : layer.pipeline(true)));
|
| 126 | DeviceLimits deviceLimits = RenderSystem.getDevice().getDeviceInfo().limits();
|
| 127 | int maxDrawIndirectDrawCount = deviceLimits.maxDrawIndirectDrawCount();
|
| 128 | List<ChunkSectionsToRender.GpuMultiDrawIndexedIndirect> drawGroups = this.drawGroupsPerLayer.get(layer);
|
| 129 | if (!drawGroups.isEmpty()) {
|
| 130 | renderPass.setVertexBuffer(1, this.chunkSectionInfos);
|
| 131 | }
|
| 132 |
|
| 133 | for (ChunkSectionsToRender.GpuMultiDrawIndexedIndirect indirectDraw : drawGroups) {
|
| 134 | if (indirectDraw.drawCount() > 0) {
|
| 135 | renderPass.setVertexBuffer(0, indirectDraw.vertexBuffer);
|
| 136 | IndexType indexType = indirectDraw.indexType() == null ? defaultIndexType : indirectDraw.indexType();
|
| 137 | renderPass.setIndexBuffer(indirectDraw.indexBuffer() == null ? defaultIndexBuffer : indirectDraw.indexBuffer().buffer(), indexType);
|
| 138 | GpuBuffer buffer = indirectDraw.indirectCommandBuffer.buffer();
|
| 139 | long startOffset = indirectDraw.indirectCommandBuffer().offset();
|
| 140 | int remainingDrawCount = indirectDraw.drawCount();
|
| 141 |
|
| 142 | while (remainingDrawCount > 0) {
|
| 143 | int passDrawCount = Integer.min(remainingDrawCount, maxDrawIndirectDrawCount);
|
| 144 | long length = passDrawCount * 20L;
|
| 145 | GpuBufferSlice passSlice = buffer.slice(startOffset, length);
|
| 146 | renderPass.drawIndexedIndirect(passSlice, passDrawCount);
|
| 147 | remainingDrawCount -= passDrawCount;
|
| 148 | startOffset += length;
|
| 149 | }
|
| 150 | }
|
| 151 | }
|
| 152 | }
|
| 153 | }
|
| 154 |
|
| 155 | @Environment(EnvType.CLIENT)
|
| 156 | public static final class DrawSeparate extends ChunkSectionsToRender {
|
| 157 | private final Map<ChunkSectionLayer, List<RenderPass.Draw<GpuBufferSlice[]>>> drawsPerLayer;
|
| 158 | private final GpuBufferSlice[] chunkSectionInfos;
|
| 159 |
|
| 160 | public DrawSeparate(
|
| 161 | final GpuBufferSlice terrainTransformUBO,
|
| 162 | final Map<ChunkSectionLayer, List<RenderPass.Draw<GpuBufferSlice[]>>> drawsPerLayer,
|
| 163 | final int maxIndicesRequired,
|
| 164 | final GpuBufferSlice[] chunkSectionInfos
|
| 165 | ) {
|
| 166 | super(terrainTransformUBO, maxIndicesRequired);
|
| 167 | this.drawsPerLayer = drawsPerLayer;
|
| 168 | this.chunkSectionInfos = chunkSectionInfos;
|
| 169 | }
|
| 170 |
|
| 171 | @Override
|
| 172 | protected void render(
|
| 173 | final ChunkSectionLayer layer,
|
| 174 | final RenderPass renderPass,
|
| 175 | final @Nullable GpuBuffer defaultIndexBuffer,
|
| 176 | final @Nullable IndexType defaultIndexType,
|
| 177 | final @Nullable RenderPipeline renderPipelineOverride,
|
| 178 | final @Nullable RenderPipeline renderPipelineOverrideMultidraw
|
| 179 | ) {
|
| 180 | renderPass.setPipeline(RenderSystem.getCompiledPipeline(renderPipelineOverride != null ? renderPipelineOverride : layer.pipeline(false)));
|
| 181 | List<RenderPass.Draw<GpuBufferSlice[]>> draws = this.drawsPerLayer.get(layer);
|
| 182 | if (!draws.isEmpty()) {
|
| 183 | renderPass.drawMultipleIndexed(draws, defaultIndexBuffer, defaultIndexType, List.of("ChunkSection"), this.chunkSectionInfos);
|
| 184 | }
|
| 185 | }
|
| 186 | }
|
| 187 |
|
| 188 | @Environment(EnvType.CLIENT)
|
| 189 | public record GpuMultiDrawIndexedIndirect(
|
| 190 | GpuBufferSlice vertexBuffer, @Nullable GpuBufferSlice indexBuffer, @Nullable IndexType indexType, GpuBufferSlice indirectCommandBuffer, int drawCount
|
| 191 | ) {
|
| 192 | }
|
| 193 | }
|
| 194 |
|