FrameBufferCache .java

com.mojang.renderpearl.backend.opengl.FrameBufferCache (class, 125 lines): members: colorTextures, destroyFbo, getFbo, depthAttachment, dsa, CacheKey, key.

com.mojang.renderpearl.backend.openglclass125 linesclient4.1 KB

API Surface (5)

int getFbo(final DirectStateAccess dsa, final List<@Nullable FrameBufferAttachment> colorTextures, final @Nullable FrameBufferAttachment depthTexture)
int getFbo(final DirectStateAccess dsa, final List<@Nullable FrameBufferAttachment> colorTextures, final @Nullable FrameBufferAttachment depthTexture, final int mipOffset)
void destroyFbo(final FrameBufferCache.CacheKey key)
int hashCode()
boolean equals(final Object obj)

Fields & Constants (7)

cache
dsa
colorTextures
depthTexture
key
colorAttachments
depthAttachment

Code Graph: Dependencies (3)

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

Full Source

FrameBufferCache.java125 lines · Minecraft 26.3 · verbatim
1
package com.mojang.renderpearl.backend.opengl;
2
3
import it.unimi.dsi.fastutil.objects.Object2IntMap;
4
import it.unimi.dsi.fastutil.objects.Object2IntOpenHashMap;
5
import java.util.ArrayList;
6
import java.util.Arrays;
7
import java.util.List;
8
import net.fabricmc.api.EnvType;
9
import net.fabricmc.api.Environment;
10
import org.jspecify.annotations.Nullable;
11
12
@Environment(EnvType.CLIENT)
13
public class FrameBufferCache {
14
	private final Object2IntMap<FrameBufferCache.CacheKey> cache = new Object2IntOpenHashMap<>();
15
16
	public int getFbo(final DirectStateAccess dsa, final List<@Nullable FrameBufferAttachment> colorTextures, final @Nullable FrameBufferAttachment depthTexture) {
17
		return this.getFbo(dsa, colorTextures, depthTexture, 0);
18
	}
19
20
	public int getFbo(
21
		final DirectStateAccess dsa,
22
		final List<@Nullable FrameBufferAttachment> colorTextures,
23
		final @Nullable FrameBufferAttachment depthTexture,
24
		final int mipOffset
25
	) {
26
		FrameBufferCache.CacheKey cacheKey = new FrameBufferCache.CacheKey(colorTextures, depthTexture, mipOffset);
27
		return this.cache.computeIfAbsent(cacheKey, var6 -> this.createFbo(cacheKey, dsa, colorTextures, depthTexture, mipOffset));
28
	}
29
30
	private int createFbo(
31
		final FrameBufferCache.CacheKey key,
32
		final DirectStateAccess dsa,
33
		final List<@Nullable FrameBufferAttachment> colorAttachments,
34
		final @Nullable FrameBufferAttachment depthAttachment,
35
		final int mipOffset
36
	) {
37
		int fbo = dsa.createFrameBufferObject();
38
		int colorAttachmentCount = colorAttachments.size();
39
		int[] colorIds = new int[colorAttachmentCount];
40
		int[] mipLevels = new int[colorAttachmentCount];
41
42
		for (int i = 0; i < colorAttachmentCount; i++) {
43
			FrameBufferAttachment attachment = colorAttachments.get(i);
44
			if (attachment != null) {
45
				colorIds[i] = attachment.glId();
46
				mipLevels[i] = attachment.fboMipLevel() + mipOffset;
47
				attachment.addAssociatedFbo(key);
48
			} else {
49
				colorIds[i] = 0;
50
				mipLevels[i] = 0;
51
			}
52
		}
53
54
		if (depthAttachment != null) {
55
			depthAttachment.addAssociatedFbo(key);
56
		}
57
58
		dsa.bindFrameBufferTextures(
59
			fbo, colorIds, mipLevels, depthAttachment == null ? 0 : depthAttachment.glId(), depthAttachment == null ? 0 : depthAttachment.fboMipLevel(), 0
60
		);
61
		return fbo;
62
	}
63
64
	public void destroyFbo(final FrameBufferCache.CacheKey key) {
65
		if (this.cache.containsKey(key)) {
66
			for (FrameBufferAttachment associatedAttachment : key.associatedAttachments) {
67
				if (associatedAttachment != null) {
68
					associatedAttachment.removeAssociatedFbo(key);
69
				}
70
			}
71
72
			int fboId = this.cache.removeInt(key);
73
			GlStateManager._glDeleteFramebuffers(fboId);
74
		}
75
	}
76
77
	@Environment(EnvType.CLIENT)
78
	public static class CacheKey {
79
		private final int[] data;
80
		private final int hash;
81
		public final List<@Nullable FrameBufferAttachment> associatedAttachments;
82
83
		public CacheKey(final List<@Nullable FrameBufferAttachment> colorAttachments, final @Nullable FrameBufferAttachment depthAttachment, final int mipOffset) {
84
			int colorAttachmentCount = colorAttachments.size();
85
			this.data = new int[(colorAttachmentCount + (depthAttachment != null ? 1 : 0)) * 2];
86
87
			for (int i = 0; i < colorAttachmentCount; i++) {
88
				FrameBufferAttachment attachment = colorAttachments.get(i);
89
				if (attachment != null) {
90
					this.data[i * 2] = attachment.glId();
91
					this.data[i * 2 + 1] = attachment.fboMipLevel() + mipOffset;
92
				} else {
93
					this.data[i * 2] = 0;
94
					this.data[i * 2 + 1] = 0;
95
				}
96
			}
97
98
			this.associatedAttachments = new ArrayList<>(colorAttachments);
99
			if (depthAttachment != null) {
100
				this.data[colorAttachmentCount * 2] = depthAttachment.glId();
101
				this.data[colorAttachmentCount * 2 + 1] = depthAttachment.fboMipLevel() + mipOffset;
102
				this.associatedAttachments.add(depthAttachment);
103
			}
104
105
			this.hash = Arrays.hashCode(this.data);
106
		}
107
108
		@Override
109
		public int hashCode() {
110
			return this.hash;
111
		}
112
113
		@Override
114
		public boolean equals(final Object obj) {
115
			if (this == obj) {
116
				return true;
117
			} else if (obj instanceof FrameBufferCache.CacheKey other) {
118
				return this.hash != other.hash ? false : Arrays.equals(this.data, other.data);
119
			} else {
120
				return false;
121
			}
122
		}
123
	}
124
}
125

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