package com.mojang.blaze3d.opengl; import com.mojang.logging.LogUtils; import java.util.Set; import java.util.function.Supplier; import net.fabricmc.api.EnvType; import net.fabricmc.api.Environment; import net.minecraft.util.StringUtil; import org.lwjgl.opengl.EXTDebugLabel; import org.lwjgl.opengl.GL11; import org.lwjgl.opengl.GLCapabilities; import org.lwjgl.opengl.KHRDebug; import org.slf4j.Logger; @Environment(EnvType.CLIENT) public abstract class GlDebugLabel { private static final Logger LOGGER = LogUtils.getLogger(); public void applyLabel(GlBuffer buffer) { } public void applyLabel(GlTexture texture) { } public void applyLabel(GlShaderModule shaderModule) { } public void applyLabel(GlProgram program) { } public void applyLabel(VertexArrayCache.VertexArray vertexArray) { } public static GlDebugLabel create(GLCapabilities capabilities, boolean renderDebugLabels, Set enabledExtensions) { if (renderDebugLabels) { if (capabilities.GL_KHR_debug && GlDevice.USE_GL_KHR_debug) { enabledExtensions.add("GL_KHR_debug"); return new GlDebugLabel.Core(); } if (capabilities.GL_EXT_debug_label && GlDevice.USE_GL_EXT_debug_label) { enabledExtensions.add("GL_EXT_debug_label"); return new GlDebugLabel.Ext(); } LOGGER.warn("Debug labels unavailable: neither KHR_debug nor EXT_debug_label are supported"); } return new GlDebugLabel.Empty(); } public boolean exists() { return false; } @Environment(EnvType.CLIENT) static class Core extends GlDebugLabel { private final int maxLabelLength = GL11.glGetInteger(33512); @Override public void applyLabel(GlBuffer buffer) { buffer.ensureBufferExists(); Supplier supplier = buffer.label; if (supplier != null) { KHRDebug.glObjectLabel(33504, buffer.handle, StringUtil.truncateStringIfNecessary((String)supplier.get(), this.maxLabelLength, true)); } } @Override public void applyLabel(GlTexture texture) { KHRDebug.glObjectLabel(5890, texture.id, StringUtil.truncateStringIfNecessary(texture.getLabel(), this.maxLabelLength, true)); } @Override public void applyLabel(GlShaderModule shaderModule) { KHRDebug.glObjectLabel(33505, shaderModule.getShaderId(), StringUtil.truncateStringIfNecessary(shaderModule.getDebugLabel(), this.maxLabelLength, true)); } @Override public void applyLabel(GlProgram program) { KHRDebug.glObjectLabel(33506, program.getProgramId(), StringUtil.truncateStringIfNecessary(program.getDebugLabel(), this.maxLabelLength, true)); } @Override public void applyLabel(VertexArrayCache.VertexArray vertexArray) { KHRDebug.glObjectLabel(32884, vertexArray.id, StringUtil.truncateStringIfNecessary(vertexArray.format.toString(), this.maxLabelLength, true)); } @Override public boolean exists() { return true; } } @Environment(EnvType.CLIENT) static class Empty extends GlDebugLabel { } @Environment(EnvType.CLIENT) static class Ext extends GlDebugLabel { @Override public void applyLabel(GlBuffer buffer) { buffer.ensureBufferExists(); Supplier supplier = buffer.label; if (supplier != null) { EXTDebugLabel.glLabelObjectEXT(37201, buffer.handle, StringUtil.truncateStringIfNecessary((String)supplier.get(), 256, true)); } } @Override public void applyLabel(GlTexture texture) { EXTDebugLabel.glLabelObjectEXT(5890, texture.id, StringUtil.truncateStringIfNecessary(texture.getLabel(), 256, true)); } @Override public void applyLabel(GlShaderModule shaderModule) { EXTDebugLabel.glLabelObjectEXT(35656, shaderModule.getShaderId(), StringUtil.truncateStringIfNecessary(shaderModule.getDebugLabel(), 256, true)); } @Override public void applyLabel(GlProgram program) { EXTDebugLabel.glLabelObjectEXT(35648, program.getProgramId(), StringUtil.truncateStringIfNecessary(program.getDebugLabel(), 256, true)); } @Override public void applyLabel(VertexArrayCache.VertexArray vertexArray) { EXTDebugLabel.glLabelObjectEXT(32884, vertexArray.id, StringUtil.truncateStringIfNecessary(vertexArray.format.toString(), 256, true)); } @Override public boolean exists() { return true; } } }