GlBuffer .java

com.mojang.renderpearl.backend.opengl.GlBuffer (class, 157 lines): members: canPersistentMap, mappingRefCount, checkCanBeUsed, MEMORY_POOL, Direct, handle.

com.mojang.renderpearl.backend.openglclass157 linesclientextends BaseGpuBuffer4.5 KB

API Surface (5)

int handle()
void checkCanBeUsed()
boolean isClosed()
void close()
GpuBufferSlice.MappedView map(final long offset, final long length, final boolean read, final boolean write)

Fields & Constants (4)

MEMORY_POOL
handle
canPersistentMap
mappingRefCount

String literals (12)

"GPU Buffers"
"Attempt to close a mapped buffer"
"Buffer already closed"
"At least read or write must be true"
"Buffer is not readable"
"Buffer is not writable"
" bytes at offset "
" from "
" size buffer)"
"Mapping buffers larger than 2GB is not supported"
"Failed to map buffer"
"Offset or length must be positive integer values"

Code Graph: Dependencies (6)

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

Full Source

GlBuffer.java157 lines · Minecraft 26.3 · verbatim
1
package com.mojang.renderpearl.backend.opengl;
2
3
import com.mojang.jtracy.MemoryPool;
4
import com.mojang.jtracy.TracyClient;
5
import com.mojang.renderpearl.api.buffers.GpuBuffer;
6
import com.mojang.renderpearl.api.buffers.GpuBufferSlice;
7
import com.mojang.renderpearl.backend.common.BaseGpuBuffer;
8
import com.mojang.renderpearl.backend.opengl.GlBuffer.Direct.1;
9
import java.nio.ByteBuffer;
10
import net.fabricmc.api.EnvType;
11
import net.fabricmc.api.Environment;
12
import org.jspecify.annotations.Nullable;
13
import org.lwjgl.system.MemoryUtil;
14
15
@Environment(EnvType.CLIENT)
16
public abstract class GlBuffer extends BaseGpuBuffer {
17
	protected static final MemoryPool MEMORY_POOL = TracyClient.createMemoryPool("GPU Buffers");
18
	private final int handle;
19
	protected final boolean canPersistentMap;
20
	protected int mappingRefCount = 0;
21
22
	protected GlBuffer(final @GpuBuffer.Usage int usage, final long size, final int handle, final boolean canPersistentMap) {
23
		super(usage, size);
24
		this.handle = handle;
25
		this.canPersistentMap = canPersistentMap;
26
	}
27
28
	public int handle() {
29
		return this.handle;
30
	}
31
32
	@Override
33
	public void checkCanBeUsed() {
34
		if (!this.canPersistentMap) {
35
			if (this.mappingRefCount != 0) {
36
				throw new IllegalStateException("Attempt to use buffer while mapped without persistent mapping capability");
37
			}
38
		}
39
	}
40
41
	@Environment(EnvType.CLIENT)
42
	public static class Direct extends GlBuffer {
43
		private boolean closed;
44
		private final DirectStateAccess dsa;
45
		protected final int mappingFlags;
46
		protected @Nullable ByteBuffer mappedBuffer;
47
48
		protected Direct(
49
			final GlHeuristics heuristics,
50
			final DirectStateAccess dsa,
51
			final @GpuBuffer.Usage int usage,
52
			final long size,
53
			final int handle,
54
			final boolean canPersistentMap
55
		) {
56
			this.dsa = dsa;
57
			int clampedSize = (int)Math.min(size, 2147483647L);
58
			MEMORY_POOL.malloc(handle, clampedSize);
59
			int mappingFlags = 0;
60
			if ((usage & 1) != 0) {
61
				mappingFlags |= 1;
62
			}
63
64
			if ((usage & 2) != 0) {
65
				mappingFlags |= 2;
66
				if (!heuristics.isGlOnDx12()) {
67
					mappingFlags |= 32;
68
				}
69
			}
70
71
			if (canPersistentMap) {
72
				mappingFlags |= 192;
73
			}
74
75
			this.mappingFlags = mappingFlags;
76
			super(usage, size, handle, canPersistentMap);
77
			if (canPersistentMap && (usage & 3) != 0) {
78
				this.map(0L, size, (usage & 1) != 0, (usage & 2) != 0);
79
			}
80
		}
81
82
		@Override
83
		public boolean isClosed() {
84
			return this.closed;
85
		}
86
87
		@Override
88
		public void close() {
89
			if (!this.closed) {
90
				this.closed = true;
91
				if (this.canPersistentMap && (this.usage() & 3) != 0) {
92
					this.unmap();
93
				}
94
95
				if (this.mappingRefCount != 0) {
96
					throw new IllegalStateException("Attempt to close a mapped buffer");
97
				}
98
99
				GlStateManager._glDeleteBuffers(this.handle());
100
				MEMORY_POOL.free(this.handle());
101
			}
102
		}
103
104
		@Override
105
		public GpuBufferSlice.MappedView map(final long offset, final long length, final boolean read, final boolean write) {
106
			if (this.isClosed()) {
107
				throw new IllegalStateException("Buffer already closed");
108
			}
109
110
			if (!read && !write) {
111
				throw new IllegalArgumentException("At least read or write must be true");
112
			}
113
114
			if (read && (this.usage() & 1) == 0) {
115
				throw new IllegalStateException("Buffer is not readable");
116
			}
117
118
			if (write && (this.usage() & 2) == 0) {
119
				throw new IllegalStateException("Buffer is not writable");
120
			}
121
122
			if (offset + length > this.size()) {
123
				throw new IllegalArgumentException(
124
					"Cannot map more data than this buffer can hold (attempting to map " + length + " bytes at offset " + offset + " from " + this.size() + " size buffer)"
125
				);
126
			}
127
128
			if (offset > 2147483647L || length > 2147483647L) {
129
				throw new IllegalArgumentException("Mapping buffers larger than 2GB is not supported");
130
			}
131
132
			if (offset >= 0L && length >= 0L) {
133
				this.mappingRefCount++;
134
				if (this.mappedBuffer == null) {
135
					GlStateManager.clearGlErrors();
136
					this.mappedBuffer = this.dsa.mapBufferRange(this.handle(), 0L, this.size(), this.mappingFlags, this.usage());
137
					if (this.mappedBuffer == null) {
138
						throw new IllegalStateException("Failed to map buffer");
139
					}
140
				}
141
142
				return new GpuBufferSlice.MappedView(this.slice(offset, length), MemoryUtil.memSlice(this.mappedBuffer, (int)offset, (int)length), new 1(this));
143
			} else {
144
				throw new IllegalArgumentException("Offset or length must be positive integer values");
145
			}
146
		}
147
148
		private void unmap() {
149
			this.mappingRefCount--;
150
			if (this.mappingRefCount == 0) {
151
				this.dsa.unmapBuffer(this.handle(), this.usage());
152
				this.mappedBuffer = null;
153
			}
154
		}
155
	}
156
}
157

Decompiled Minecraft 26.3 source of GlBuffer (com/mojang/renderpearl/backend/opengl/GlBuffer.java) with Mojang official mappings, stored verbatim on the site.