DynamicGpuDataStorageNonMapped .java

net.minecraft.client.renderer.DynamicGpuDataStorageNonMapped (class, 180 lines): members: cpuBuffer, gpuBuffer, oldBuffers, nextBlock, writeDataBatched.

net.minecraft.client.rendererclass180 linesclientextends DynamicGpuDataStorage.DynamicGpuData>6.3 KB

Implements

API Surface (6)

void endFrame()
GpuBufferSlice writeData(final T gpuData)
GpuBufferSlice[] writeData(final T[] dataArray)
GpuBufferSlice writeDataBatched(final List<T> dataList)
GpuBufferSlice[] writeDataBatchedMultiple(final List<List<T>> dataLists)
void close()

Fields & Constants (9)

LOGGER
oldBuffers
blockSize
gpuBuffer
cpuBuffer
nextBlock
capacity
label
usage

String literals (1)

"writeData"

Code Graph: Dependencies (6)

Edges (calls / imports / inherits / references) resolved by graphify from the AST. Every chip opens the class source.

Full Source

DynamicGpuDataStorageNonMapped.java180 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.client.renderer;
2
3
import com.mojang.blaze3d.systems.RenderSystem;
4
import com.mojang.logging.LogUtils;
5
import com.mojang.renderpearl.api.buffers.GpuBuffer;
6
import com.mojang.renderpearl.api.buffers.GpuBufferSlice;
7
import com.mojang.renderpearl.api.device.GpuDevice;
8
import java.nio.ByteBuffer;
9
import java.util.ArrayList;
10
import java.util.List;
11
import net.fabricmc.api.EnvType;
12
import net.fabricmc.api.Environment;
13
import net.minecraft.util.Mth;
14
import org.apache.commons.lang3.NotImplementedException;
15
import org.lwjgl.system.MemoryUtil;
16
import org.slf4j.Logger;
17
18
@Environment(EnvType.CLIENT)
19
public class DynamicGpuDataStorageNonMapped<T extends DynamicGpuDataStorage.DynamicGpuData> implements DynamicGpuDataStorage<T> {
20
	private static final Logger LOGGER = LogUtils.getLogger();
21
	private final List<GpuBuffer> oldBuffers = new ArrayList<>();
22
	private final int blockSize;
23
	private GpuBuffer gpuBuffer;
24
	private ByteBuffer cpuBuffer;
25
	private int nextBlock;
26
	private int capacity;
27
	private final String label;
28
	private final @GpuBuffer.Usage int usage;
29
30
	public DynamicGpuDataStorageNonMapped(final String label, final int dataSize, final @GpuBuffer.Usage int usage, final int initialCapacity) {
31
		GpuDevice device = RenderSystem.getDevice();
32
		this.usage = usage;
33
		this.blockSize = usage == 128 ? Mth.roundToward(dataSize, device.getDeviceInfo().limits().minUniformOffsetAlignment()) : dataSize;
34
		this.capacity = Mth.smallestEncompassingPowerOfTwo(initialCapacity);
35
		this.nextBlock = 0;
36
		this.gpuBuffer = RenderSystem.getDevice().createBuffer(() -> label + " x" + this.blockSize, this.usage | 8, this.blockSize * this.capacity);
37
		this.cpuBuffer = MemoryUtil.memAlloc(this.blockSize * this.capacity);
38
		this.label = label;
39
	}
40
41
	@Override
42
	public void endFrame() {
43
		this.nextBlock = 0;
44
		this.cpuBuffer.clear();
45
46
		for (GpuBuffer oldBuffer : this.oldBuffers) {
47
			oldBuffer.close();
48
		}
49
50
		this.oldBuffers.clear();
51
	}
52
53
	@Override
54
	public GpuBufferSlice writeData(final T gpuData) {
55
		throw new NotImplementedException("writeData");
56
	}
57
58
	@Override
59
	public GpuBufferSlice[] writeData(final T[] dataArray) {
60
		if (dataArray.length == 0) {
61
			return new GpuBufferSlice[0];
62
		}
63
64
		if (this.nextBlock + dataArray.length > this.capacity) {
65
			int newCapacity = Mth.smallestEncompassingPowerOfTwo(Math.max(this.capacity + 1, dataArray.length));
66
			LOGGER.info("Resizing {}, capacity limit of {} reached during a single frame. New capacity will be {}.", this.label, this.capacity, newCapacity);
67
			this.resizeBuffers(newCapacity);
68
		}
69
70
		GpuBufferSlice[] result = new GpuBufferSlice[dataArray.length];
71
		int firstOffset = this.nextBlock * this.blockSize;
72
73
		for (int i = 0; i < dataArray.length; i++) {
74
			T data = dataArray[i];
75
			result[i] = this.gpuBuffer.slice(firstOffset + i * this.blockSize, this.blockSize);
76
			this.cpuBuffer.position(firstOffset + i * this.blockSize);
77
			data.write(this.cpuBuffer);
78
		}
79
80
		ByteBuffer cpuSlice = this.cpuBuffer.slice(firstOffset, dataArray.length * this.blockSize);
81
		GpuBufferSlice writeSlice = this.gpuBuffer.slice(firstOffset, dataArray.length * this.blockSize);
82
		RenderSystem.getDevice().createCommandEncoder().writeToBuffer(writeSlice, cpuSlice);
83
		this.nextBlock += dataArray.length;
84
		return result;
85
	}
86
87
	private void resizeBuffers(final int newCapacity) {
88
		this.capacity = newCapacity;
89
		this.nextBlock = 0;
90
		this.oldBuffers.add(this.gpuBuffer);
91
		this.gpuBuffer = RenderSystem.getDevice().createBuffer(() -> this.label + " x" + this.blockSize, this.usage | 8, this.blockSize * this.capacity);
92
		this.cpuBuffer = MemoryUtil.memRealloc(this.cpuBuffer, this.blockSize * this.capacity);
93
	}
94
95
	@Override
96
	public GpuBufferSlice writeDataBatched(final List<T> dataList) {
97
		if (dataList.isEmpty()) {
98
			return new GpuBufferSlice(null, 0L, 0L);
99
		}
100
101
		if (this.nextBlock + dataList.size() > this.capacity) {
102
			int newCapacity = Mth.smallestEncompassingPowerOfTwo(Math.max(this.capacity + 1, dataList.size()));
103
			LOGGER.info("Resizing {}, capacity limit of {} reached during a single frame. New capacity will be {}.", this.label, this.capacity, newCapacity);
104
			this.resizeBuffers(newCapacity);
105
		}
106
107
		int firstOffset = this.nextBlock * this.blockSize;
108
109
		for (int i = 0; i < dataList.size(); i++) {
110
			T data = dataList.get(i);
111
			this.cpuBuffer.position(firstOffset + i * this.blockSize);
112
			data.write(this.cpuBuffer);
113
		}
114
115
		ByteBuffer cpuSlice = this.cpuBuffer.slice(firstOffset, dataList.size() * this.blockSize);
116
		GpuBufferSlice result = this.gpuBuffer.slice(firstOffset, dataList.size() * this.blockSize);
117
		RenderSystem.getDevice().createCommandEncoder().writeToBuffer(result, cpuSlice);
118
		this.nextBlock = this.nextBlock + dataList.size();
119
		return result;
120
	}
121
122
	@Override
123
	public GpuBufferSlice[] writeDataBatchedMultiple(final List<List<T>> dataLists) {
124
		if (dataLists.isEmpty()) {
125
			return new GpuBufferSlice[0];
126
		}
127
128
		int totalCount = 0;
129
130
		for (List<T> data : dataLists) {
131
			totalCount += data.size();
132
		}
133
134
		if (this.nextBlock + totalCount > this.capacity) {
135
			int newCapacity = Mth.smallestEncompassingPowerOfTwo(Math.max(this.capacity + 1, totalCount));
136
			LOGGER.info("Resizing {}, capacity limit of {} reached during a single frame. New capacity will be {}.", this.label, this.capacity, newCapacity);
137
			this.resizeBuffers(newCapacity);
138
		}
139
140
		int firstOffset = this.nextBlock * this.blockSize;
141
		int offset = firstOffset;
142
		GpuBufferSlice[] result = new GpuBufferSlice[dataLists.size()];
143
		int bufferPositionIndex = 0;
144
145
		for (int i = 0; i < dataLists.size(); i++) {
146
			List<T> dataList = dataLists.get(i);
147
			result[i] = this.gpuBuffer.slice(offset, dataList.size() * this.blockSize);
148
149
			for (T data : dataList) {
150
				this.cpuBuffer.position(firstOffset + bufferPositionIndex++ * this.blockSize);
151
				data.write(this.cpuBuffer);
152
			}
153
154
			offset += dataList.size() * this.blockSize;
155
		}
156
157
		ByteBuffer cpuSlice = this.cpuBuffer.slice(firstOffset, totalCount * this.blockSize);
158
		GpuBufferSlice writeSlice = this.gpuBuffer.slice(firstOffset, totalCount * this.blockSize);
159
		RenderSystem.getDevice().createCommandEncoder().writeToBuffer(writeSlice, cpuSlice);
160
		this.nextBlock += totalCount;
161
		return result;
162
	}
163
164
	@Override
165
	public @GpuBuffer.Usage int usage() {
166
		return this.usage;
167
	}
168
169
	@Override
170
	public void close() {
171
		for (GpuBuffer oldBuffer : this.oldBuffers) {
172
			oldBuffer.close();
173
		}
174
175
		this.oldBuffers.clear();
176
		this.gpuBuffer.close();
177
		MemoryUtil.memFree(this.cpuBuffer);
178
	}
179
}
180

Decompiled Minecraft 26.3 source of DynamicGpuDataStorageNonMapped (net/minecraft/client/renderer/DynamicGpuDataStorageNonMapped.java) with Mojang official mappings, stored verbatim on the site.