package com.mojang.blaze3d.opengl; import com.mojang.blaze3d.shaders.ShaderType; import com.mojang.blaze3d.systems.RenderSystem; import net.fabricmc.api.EnvType; import net.fabricmc.api.Environment; import net.minecraft.client.renderer.ShaderManager; import net.minecraft.resources.ResourceLocation; import org.apache.commons.lang3.StringUtils; @Environment(EnvType.CLIENT) public class GlShaderModule implements AutoCloseable { private static final int NOT_ALLOCATED = -1; public static final GlShaderModule INVALID_SHADER = new GlShaderModule(-1, ResourceLocation.withDefaultNamespace("invalid"), ShaderType.VERTEX); private final ResourceLocation id; private int shaderId; private final ShaderType type; public GlShaderModule(int shaderId, ResourceLocation id, ShaderType type) { this.id = id; this.shaderId = shaderId; this.type = type; } public static GlShaderModule compile(ResourceLocation id, ShaderType type, String source) throws ShaderManager.CompilationException { RenderSystem.assertOnRenderThread(); int i = GlStateManager.glCreateShader(GlConst.toGl(type)); GlStateManager.glShaderSource(i, source); GlStateManager.glCompileShader(i); if (GlStateManager.glGetShaderi(i, 35713) == 0) { String string = StringUtils.trim(GlStateManager.glGetShaderInfoLog(i, 32768)); throw new ShaderManager.CompilationException("Couldn't compile " + type.getName() + " shader (" + id + ") : " + string); } else { return new GlShaderModule(i, id, type); } } public void close() { if (this.shaderId == -1) { throw new IllegalStateException("Already closed"); } else { RenderSystem.assertOnRenderThread(); GlStateManager.glDeleteShader(this.shaderId); this.shaderId = -1; } } public ResourceLocation getId() { return this.id; } public int getShaderId() { return this.shaderId; } public String getDebugLabel() { return this.type.idConverter().idToFile(this.id).toString(); } }