GlDebug .java

com.mojang.renderpearl.backend.opengl.GlDebug (class, 177 lines): members: getLastOpenGlDebugMessages, enableDebugCallback, CIRCULAR_LOG_SIZE, lastEntry.

com.mojang.renderpearl.backend.openglclass177 linesclient5.4 KB

API Surface (5)

String sourceToString(final int source)
String typeToString(final int type)
String severityToString(final int severity)
List<String> getLastOpenGlDebugMessages()
String toString()

Fields & Constants (6)

LOGGER
CIRCULAR_LOG_SIZE
DEBUG_LEVELS
DEBUG_LEVELS_ARB
MESSAGE_BUFFER
lastEntry

String literals (20)

"Unknown (0x"
"WINDOW SYSTEM"
"SHADER COMPILER"
"THIRD PARTY"
"APPLICATION"
"OTHER"
"DEPRECATED BEHAVIOR"
"UNDEFINED BEHAVIOR"
"PORTABILITY"
"PERFORMANCE"
"MARKER"
"NOTIFICATION"
"HIGH"
"MEDIUM"
"GL_KHR_debug"
"GL_ARB_debug_output"
", source="
", type="
", severity="
", message='"

Full Source

GlDebug.java177 lines · Minecraft 26.3 · verbatim
1
package com.mojang.renderpearl.backend.opengl;
2
3
import com.google.common.collect.EvictingQueue;
4
import com.google.common.collect.ImmutableList;
5
import com.google.common.collect.Lists;
6
import com.mojang.logging.LogUtils;
7
import java.util.HexFormat;
8
import java.util.List;
9
import java.util.Queue;
10
import java.util.Set;
11
import net.fabricmc.api.EnvType;
12
import net.fabricmc.api.Environment;
13
import org.jspecify.annotations.Nullable;
14
import org.lwjgl.opengl.ARBDebugOutput;
15
import org.lwjgl.opengl.GL;
16
import org.lwjgl.opengl.GL33C;
17
import org.lwjgl.opengl.GLCapabilities;
18
import org.lwjgl.opengl.GLDebugMessageARBCallback;
19
import org.lwjgl.opengl.GLDebugMessageCallback;
20
import org.lwjgl.opengl.KHRDebug;
21
import org.slf4j.Logger;
22
23
@Environment(EnvType.CLIENT)
24
public class GlDebug {
25
	private static final Logger LOGGER = LogUtils.getLogger();
26
	private static final int CIRCULAR_LOG_SIZE = 10;
27
	private final Queue<GlDebug.LogEntry> MESSAGE_BUFFER = EvictingQueue.create(10);
28
	private volatile GlDebug.@Nullable LogEntry lastEntry;
29
	private static final List<Integer> DEBUG_LEVELS = ImmutableList.of(37190, 37191, 37192, 33387);
30
	private static final List<Integer> DEBUG_LEVELS_ARB = ImmutableList.of(37190, 37191, 37192);
31
32
	private static String printUnknownToken(final int token) {
33
		return "Unknown (0x" + HexFormat.of().withUpperCase().toHexDigits(token) + ")";
34
	}
35
36
	public static String sourceToString(final int source) {
37
		return switch (source) {
38
			case 33350 -> "API";
39
			case 33351 -> "WINDOW SYSTEM";
40
			case 33352 -> "SHADER COMPILER";
41
			case 33353 -> "THIRD PARTY";
42
			case 33354 -> "APPLICATION";
43
			case 33355 -> "OTHER";
44
			default -> printUnknownToken(source);
45
		};
46
	}
47
48
	public static String typeToString(final int type) {
49
		return switch (type) {
50
			case 33356 -> "ERROR";
51
			case 33357 -> "DEPRECATED BEHAVIOR";
52
			case 33358 -> "UNDEFINED BEHAVIOR";
53
			case 33359 -> "PORTABILITY";
54
			case 33360 -> "PERFORMANCE";
55
			case 33361 -> "OTHER";
56
			case 33384 -> "MARKER";
57
			default -> printUnknownToken(type);
58
		};
59
	}
60
61
	public static String severityToString(final int severity) {
62
		return switch (severity) {
63
			case 33387 -> "NOTIFICATION";
64
			case 37190 -> "HIGH";
65
			case 37191 -> "MEDIUM";
66
			case 37192 -> "LOW";
67
			default -> printUnknownToken(severity);
68
		};
69
	}
70
71
	private void printDebugLog(final int source, final int type, final int id, final int severity, final int length, final long message, final long userParam) {
72
		String msg = GLDebugMessageCallback.getMessage(length, message);
73
		GlDebug.LogEntry entry;
74
		synchronized (this.MESSAGE_BUFFER) {
75
			entry = this.lastEntry;
76
			if (entry != null && entry.isSame(source, type, id, severity, msg)) {
77
				entry.count++;
78
			} else {
79
				entry = new GlDebug.LogEntry(source, type, id, severity, msg);
80
				this.MESSAGE_BUFFER.add(entry);
81
				this.lastEntry = entry;
82
			}
83
		}
84
85
		LOGGER.info("OpenGL debug message: {}", entry);
86
	}
87
88
	public List<String> getLastOpenGlDebugMessages() {
89
		synchronized (this.MESSAGE_BUFFER) {
90
			List<String> result = Lists.newArrayListWithCapacity(this.MESSAGE_BUFFER.size());
91
92
			for (GlDebug.LogEntry e : this.MESSAGE_BUFFER) {
93
				result.add(e + " x " + e.count);
94
			}
95
96
			return result;
97
		}
98
	}
99
100
	public static @Nullable GlDebug enableDebugCallback(final int verbosity, final boolean debugSynchronousGlLogs, final Set<String> enabledExtensions) {
101
		if (verbosity <= 0) {
102
			return null;
103
		}
104
105
		GLCapabilities caps = GL.getCapabilities();
106
		if (caps.GL_KHR_debug && GlDevice.USE_GL_KHR_debug) {
107
			GlDebug debug = new GlDebug();
108
			enabledExtensions.add("GL_KHR_debug");
109
			GL33C.glEnable(37600);
110
			if (debugSynchronousGlLogs) {
111
				GL33C.glEnable(33346);
112
			}
113
114
			for (int i = 0; i < DEBUG_LEVELS.size(); i++) {
115
				boolean isEnabled = i < verbosity;
116
				KHRDebug.glDebugMessageControl(4352, 4352, DEBUG_LEVELS.get(i), (int[])null, isEnabled);
117
			}
118
119
			KHRDebug.glDebugMessageCallback(GLDebugMessageCallback.create(debug::printDebugLog), 0L);
120
			return debug;
121
		} else if (caps.GL_ARB_debug_output && GlDevice.USE_GL_ARB_debug_output) {
122
			GlDebug debug = new GlDebug();
123
			enabledExtensions.add("GL_ARB_debug_output");
124
			if (debugSynchronousGlLogs) {
125
				GL33C.glEnable(33346);
126
			}
127
128
			for (int i = 0; i < DEBUG_LEVELS_ARB.size(); i++) {
129
				boolean isEnabled = i < verbosity;
130
				ARBDebugOutput.glDebugMessageControlARB(4352, 4352, DEBUG_LEVELS_ARB.get(i), (int[])null, isEnabled);
131
			}
132
133
			ARBDebugOutput.glDebugMessageCallbackARB(GLDebugMessageARBCallback.create(debug::printDebugLog), 0L);
134
			return debug;
135
		} else {
136
			return null;
137
		}
138
	}
139
140
	@Environment(EnvType.CLIENT)
141
	private static class LogEntry {
142
		private final int id;
143
		private final int source;
144
		private final int type;
145
		private final int severity;
146
		private final String message;
147
		private int count = 1;
148
149
		private LogEntry(final int source, final int type, final int id, final int severity, final String message) {
150
			this.id = id;
151
			this.source = source;
152
			this.type = type;
153
			this.severity = severity;
154
			this.message = message;
155
		}
156
157
		private boolean isSame(final int source, final int type, final int id, final int severity, final String message) {
158
			return type == this.type && source == this.source && id == this.id && severity == this.severity && message.equals(this.message);
159
		}
160
161
		@Override
162
		public String toString() {
163
			return "id="
164
				+ this.id
165
				+ ", source="
166
				+ GlDebug.sourceToString(this.source)
167
				+ ", type="
168
				+ GlDebug.typeToString(this.type)
169
				+ ", severity="
170
				+ GlDebug.severityToString(this.severity)
171
				+ ", message='"
172
				+ this.message
173
				+ "'";
174
		}
175
	}
176
}
177

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