| 1 | package com.mojang.renderpearl.backend.opengl;
|
| 2 |
|
| 3 | import com.mojang.logging.LogUtils;
|
| 4 | import java.util.Set;
|
| 5 | import java.util.function.Supplier;
|
| 6 | import net.fabricmc.api.EnvType;
|
| 7 | import net.fabricmc.api.Environment;
|
| 8 | import net.minecraft.util.StringUtil;
|
| 9 | import org.jspecify.annotations.Nullable;
|
| 10 | import org.lwjgl.opengl.EXTDebugLabel;
|
| 11 | import org.lwjgl.opengl.GL33C;
|
| 12 | import org.lwjgl.opengl.GLCapabilities;
|
| 13 | import org.lwjgl.opengl.KHRDebug;
|
| 14 | import org.slf4j.Logger;
|
| 15 |
|
| 16 | @Environment(EnvType.CLIENT)
|
| 17 | public abstract class GlDebugLabel {
|
| 18 | private static final Logger LOGGER = LogUtils.getLogger();
|
| 19 |
|
| 20 | public void applyLabel(final GlBuffer buffer, final @Nullable Supplier<String> label) {
|
| 21 | }
|
| 22 |
|
| 23 | public void applyLabel(final GlTexture texture) {
|
| 24 | }
|
| 25 |
|
| 26 | public void applyLabel(final GlShaderModule shaderModule) {
|
| 27 | }
|
| 28 |
|
| 29 | public void applyLabel(final GlProgram program) {
|
| 30 | }
|
| 31 |
|
| 32 | public void pushDebugGroup(final Supplier<String> label) {
|
| 33 | }
|
| 34 |
|
| 35 | public void popDebugGroup() {
|
| 36 | }
|
| 37 |
|
| 38 | public static GlDebugLabel create(final GLCapabilities caps, final boolean wantsLabels, final Set<String> enabledExtensions) {
|
| 39 | if (wantsLabels) {
|
| 40 | if (caps.GL_KHR_debug && GlDevice.USE_GL_KHR_debug) {
|
| 41 | enabledExtensions.add("GL_KHR_debug");
|
| 42 | return new GlDebugLabel.Core();
|
| 43 | }
|
| 44 |
|
| 45 | if (caps.GL_EXT_debug_label && GlDevice.USE_GL_EXT_debug_label) {
|
| 46 | enabledExtensions.add("GL_EXT_debug_label");
|
| 47 | return new GlDebugLabel.Ext();
|
| 48 | }
|
| 49 |
|
| 50 | LOGGER.warn("Debug labels unavailable: neither KHR_debug nor EXT_debug_label are supported");
|
| 51 | }
|
| 52 |
|
| 53 | return new GlDebugLabel.Empty();
|
| 54 | }
|
| 55 |
|
| 56 | public boolean exists() {
|
| 57 | return false;
|
| 58 | }
|
| 59 |
|
| 60 | @Environment(EnvType.CLIENT)
|
| 61 | private static class Core extends GlDebugLabel {
|
| 62 | private final int maxLabelLength = GL33C.glGetInteger(33512);
|
| 63 |
|
| 64 | @Override
|
| 65 | public void applyLabel(final GlBuffer buffer, final @Nullable Supplier<String> label) {
|
| 66 | if (label != null) {
|
| 67 | KHRDebug.glObjectLabel(33504, buffer.handle(), StringUtil.truncateStringIfNecessary(label.get(), this.maxLabelLength, true));
|
| 68 | }
|
| 69 | }
|
| 70 |
|
| 71 | @Override
|
| 72 | public void applyLabel(final GlTexture texture) {
|
| 73 | KHRDebug.glObjectLabel(5890, texture.id, StringUtil.truncateStringIfNecessary(texture.getLabel(), this.maxLabelLength, true));
|
| 74 | }
|
| 75 |
|
| 76 | @Override
|
| 77 | public void applyLabel(final GlShaderModule shaderModule) {
|
| 78 | KHRDebug.glObjectLabel(33505, shaderModule.getShaderId(), StringUtil.truncateStringIfNecessary(shaderModule.getLabel(), this.maxLabelLength, true));
|
| 79 | }
|
| 80 |
|
| 81 | @Override
|
| 82 | public void applyLabel(final GlProgram program) {
|
| 83 | KHRDebug.glObjectLabel(33506, program.getProgramId(), StringUtil.truncateStringIfNecessary(program.getDebugLabel(), this.maxLabelLength, true));
|
| 84 | }
|
| 85 |
|
| 86 | @Override
|
| 87 | public void pushDebugGroup(final Supplier<String> label) {
|
| 88 | KHRDebug.glPushDebugGroup(33354, 0, label.get());
|
| 89 | }
|
| 90 |
|
| 91 | @Override
|
| 92 | public void popDebugGroup() {
|
| 93 | KHRDebug.glPopDebugGroup();
|
| 94 | }
|
| 95 |
|
| 96 | @Override
|
| 97 | public boolean exists() {
|
| 98 | return true;
|
| 99 | }
|
| 100 | }
|
| 101 |
|
| 102 | @Environment(EnvType.CLIENT)
|
| 103 | private static class Empty extends GlDebugLabel {
|
| 104 | }
|
| 105 |
|
| 106 | @Environment(EnvType.CLIENT)
|
| 107 | private static class Ext extends GlDebugLabel {
|
| 108 | @Override
|
| 109 | public void applyLabel(final GlBuffer buffer, final @Nullable Supplier<String> label) {
|
| 110 | if (label != null) {
|
| 111 | EXTDebugLabel.glLabelObjectEXT(37201, buffer.handle(), StringUtil.truncateStringIfNecessary(label.get(), 256, true));
|
| 112 | }
|
| 113 | }
|
| 114 |
|
| 115 | @Override
|
| 116 | public void applyLabel(final GlTexture texture) {
|
| 117 | EXTDebugLabel.glLabelObjectEXT(5890, texture.id, StringUtil.truncateStringIfNecessary(texture.getLabel(), 256, true));
|
| 118 | }
|
| 119 |
|
| 120 | @Override
|
| 121 | public void applyLabel(final GlShaderModule shaderModule) {
|
| 122 | EXTDebugLabel.glLabelObjectEXT(35656, shaderModule.getShaderId(), StringUtil.truncateStringIfNecessary(shaderModule.getLabel(), 256, true));
|
| 123 | }
|
| 124 |
|
| 125 | @Override
|
| 126 | public void applyLabel(final GlProgram program) {
|
| 127 | EXTDebugLabel.glLabelObjectEXT(35648, program.getProgramId(), StringUtil.truncateStringIfNecessary(program.getDebugLabel(), 256, true));
|
| 128 | }
|
| 129 |
|
| 130 | @Override
|
| 131 | public boolean exists() {
|
| 132 | return true;
|
| 133 | }
|
| 134 | }
|
| 135 | }
|
| 136 |
|