| 1 | package net.minecraft.client.renderer;
|
| 2 |
|
| 3 | import com.mojang.blaze3d.buffers.Std140Builder;
|
| 4 | import com.mojang.blaze3d.buffers.Std140SizeCalculator;
|
| 5 | import com.mojang.renderpearl.api.buffers.GpuBufferSlice;
|
| 6 | import java.nio.ByteBuffer;
|
| 7 | import java.util.List;
|
| 8 | import net.fabricmc.api.EnvType;
|
| 9 | import net.fabricmc.api.Environment;
|
| 10 | import org.joml.Matrix4f;
|
| 11 | import org.joml.Matrix4fc;
|
| 12 | import org.joml.Vector3f;
|
| 13 | import org.joml.Vector3fc;
|
| 14 | import org.joml.Vector4f;
|
| 15 | import org.joml.Vector4fc;
|
| 16 | import org.jspecify.annotations.Nullable;
|
| 17 |
|
| 18 | @Environment(EnvType.CLIENT)
|
| 19 | public class DynamicGpuData implements AutoCloseable {
|
| 20 | private static final Vector4fc WHITE = new Vector4f(1.0F, 1.0F, 1.0F, 1.0F);
|
| 21 | private static final Vector3fc NO_OFFSET = new Vector3f();
|
| 22 | private static final Matrix4fc IDENTITY_TEXTURE_TRANSFORM = new Matrix4f();
|
| 23 | public static final int TRANSFORM_UBO_SIZE = new Std140SizeCalculator().putMat4f().putMat4f().putVec4().putVec3().get();
|
| 24 | public static final int TERRAIN_TRANSFORM_UBO_SIZE = new Std140SizeCalculator().putMat4f().putIVec2().get();
|
| 25 | public static final int CHUNK_SECTION_UBO_SIZE = new Std140SizeCalculator().putIVec3().putFloat().get();
|
| 26 | private static final int INITIAL_CAPACITY = 2;
|
| 27 | private final DynamicGpuDataStorageMapped<DynamicGpuData.Transform> transforms;
|
| 28 | private final DynamicGpuDataStorageMapped<DynamicGpuData.TerrainTransform> terrain;
|
| 29 | private @Nullable DynamicGpuDataStorage<DynamicGpuData.ChunkSectionInfo> chunkSections = null;
|
| 30 | private @Nullable DynamicGpuDataStorageMapped<DynamicGpuData.IndexedDraw> chunkSectionsCommandBuffer = null;
|
| 31 |
|
| 32 | public DynamicGpuData() {
|
| 33 | this.transforms = new DynamicGpuDataStorageMapped<>("Dynamic Transforms UBO", TRANSFORM_UBO_SIZE, 128, 2);
|
| 34 | this.terrain = new DynamicGpuDataStorageMapped<>("Terrain UBO", TERRAIN_TRANSFORM_UBO_SIZE, 128, 1);
|
| 35 | }
|
| 36 |
|
| 37 | public void reset() {
|
| 38 | this.transforms.endFrame();
|
| 39 | this.terrain.endFrame();
|
| 40 | if (this.chunkSections != null) {
|
| 41 | this.chunkSections.endFrame();
|
| 42 | }
|
| 43 |
|
| 44 | if (this.chunkSectionsCommandBuffer != null) {
|
| 45 | this.chunkSectionsCommandBuffer.endFrame();
|
| 46 | }
|
| 47 | }
|
| 48 |
|
| 49 | @Override
|
| 50 | public void close() {
|
| 51 | this.transforms.close();
|
| 52 | this.terrain.close();
|
| 53 | if (this.chunkSections != null) {
|
| 54 | this.chunkSections.close();
|
| 55 | }
|
| 56 |
|
| 57 | if (this.chunkSectionsCommandBuffer != null) {
|
| 58 | this.chunkSectionsCommandBuffer.close();
|
| 59 | }
|
| 60 | }
|
| 61 |
|
| 62 | public GpuBufferSlice writeTransform(final Matrix4f modelView) {
|
| 63 | return this.writeTransform(new DynamicGpuData.Transform(modelView, WHITE, NO_OFFSET, IDENTITY_TEXTURE_TRANSFORM));
|
| 64 | }
|
| 65 |
|
| 66 | public GpuBufferSlice writeTransform(final Matrix4f modelView, final Vector4f colorModulator) {
|
| 67 | return this.writeTransform(new DynamicGpuData.Transform(modelView, colorModulator, NO_OFFSET, IDENTITY_TEXTURE_TRANSFORM));
|
| 68 | }
|
| 69 |
|
| 70 | public GpuBufferSlice writeTransform(final Matrix4f modelView, final Matrix4f textureMatrix) {
|
| 71 | return this.writeTransform(new DynamicGpuData.Transform(modelView, WHITE, NO_OFFSET, textureMatrix));
|
| 72 | }
|
| 73 |
|
| 74 | public GpuBufferSlice writeTransform(final Matrix4f modelView, final Vector4f colorModulator, final Vector3f modelOffset, final Matrix4f textureMatrix) {
|
| 75 | return this.writeTransform(new DynamicGpuData.Transform(modelView, colorModulator, modelOffset, textureMatrix));
|
| 76 | }
|
| 77 |
|
| 78 | public GpuBufferSlice writeTransform(final DynamicGpuData.Transform transform) {
|
| 79 | return this.transforms.writeData(transform);
|
| 80 | }
|
| 81 |
|
| 82 | public GpuBufferSlice[] writeTransforms(final DynamicGpuData.Transform... transforms) {
|
| 83 | return this.transforms.writeData(transforms);
|
| 84 | }
|
| 85 |
|
| 86 | public GpuBufferSlice writeTerrainTransform(final Matrix4fc modelView, final int textureAtlasWidth, final int textureAtlasHeight) {
|
| 87 | return this.terrain.writeData(new DynamicGpuData.TerrainTransform(modelView, textureAtlasWidth, textureAtlasHeight));
|
| 88 | }
|
| 89 |
|
| 90 | public GpuBufferSlice[] writeChunkSections(final DynamicGpuData.ChunkSectionInfo... infos) {
|
| 91 | if (this.chunkSections != null && (this.chunkSections.usage() & 128) == 0) {
|
| 92 | this.chunkSections.close();
|
| 93 | this.chunkSections = null;
|
| 94 | }
|
| 95 |
|
| 96 | if (this.chunkSections == null) {
|
| 97 | this.chunkSections = new DynamicGpuDataStorageMapped<>("Chunk Sections UBO", CHUNK_SECTION_UBO_SIZE, 128, 2);
|
| 98 | }
|
| 99 |
|
| 100 | return this.chunkSections.writeData(infos);
|
| 101 | }
|
| 102 |
|
| 103 | public GpuBufferSlice writeChunkSectionsInstanced(final List<DynamicGpuData.ChunkSectionInfo> infos) {
|
| 104 | if (this.chunkSections != null && (this.chunkSections.usage() & 32) == 0) {
|
| 105 | this.chunkSections.close();
|
| 106 | this.chunkSections = null;
|
| 107 | }
|
| 108 |
|
| 109 | if (this.chunkSections == null) {
|
| 110 | this.chunkSections = new DynamicGpuDataStorageNonMapped<>("Chunk Sections Instanced", CHUNK_SECTION_UBO_SIZE, 32, 2);
|
| 111 | }
|
| 112 |
|
| 113 | return this.chunkSections.writeDataBatched(infos);
|
| 114 | }
|
| 115 |
|
| 116 | public GpuBufferSlice[] writeChunkSectionCommands(final List<List<DynamicGpuData.IndexedDraw>> draws) {
|
| 117 | if (this.chunkSectionsCommandBuffer == null) {
|
| 118 | this.chunkSectionsCommandBuffer = new DynamicGpuDataStorageMapped<>("Chunk Sections Command Buffer", 20, 512, 2);
|
| 119 | }
|
| 120 |
|
| 121 | return this.chunkSectionsCommandBuffer.writeDataBatchedMultiple(draws);
|
| 122 | }
|
| 123 |
|
| 124 | @Environment(EnvType.CLIENT)
|
| 125 | public record ChunkSectionInfo(int x, int y, int z, float visibility) implements DynamicGpuDataStorage.DynamicGpuData {
|
| 126 | @Override
|
| 127 | public void write(final ByteBuffer buffer) {
|
| 128 | Std140Builder.intoBuffer(buffer).putIVec3(this.x, this.y, this.z).putFloat(this.visibility);
|
| 129 | }
|
| 130 | }
|
| 131 |
|
| 132 | @Environment(EnvType.CLIENT)
|
| 133 | public record IndexedDraw(int indexCount, int instanceCount, int firstIndex, int baseVertex, int baseInstance) implements DynamicGpuDataStorage.DynamicGpuData {
|
| 134 | @Override
|
| 135 | public void write(final ByteBuffer buffer) {
|
| 136 | buffer.putInt(this.indexCount).putInt(this.instanceCount).putInt(this.firstIndex).putInt(this.baseVertex).putInt(this.baseInstance);
|
| 137 | }
|
| 138 | }
|
| 139 |
|
| 140 | @Environment(EnvType.CLIENT)
|
| 141 | public record TerrainTransform(Matrix4fc modelView, int textureAtlasWidth, int textureAtlasHeight) implements DynamicGpuDataStorage.DynamicGpuData {
|
| 142 | @Override
|
| 143 | public void write(final ByteBuffer buffer) {
|
| 144 | Std140Builder.intoBuffer(buffer).putMat4f(this.modelView).putIVec2(this.textureAtlasWidth, this.textureAtlasHeight);
|
| 145 | }
|
| 146 | }
|
| 147 |
|
| 148 | @Environment(EnvType.CLIENT)
|
| 149 | public record Transform(Matrix4fc modelView, Vector4fc colorModulator, Vector3fc modelOffset, Matrix4fc textureMatrix)
|
| 150 | implements DynamicGpuDataStorage.DynamicGpuData {
|
| 151 | @Override
|
| 152 | public void write(final ByteBuffer buffer) {
|
| 153 | Std140Builder.intoBuffer(buffer).putMat4f(this.modelView).putMat4f(this.textureMatrix).putVec4(this.colorModulator).putVec3(this.modelOffset);
|
| 154 | }
|
| 155 | }
|
| 156 | }
|
| 157 |
|