package com.mojang.blaze3d.shaders;

import org.apache.logging.log4j.LogManager;
import java.io.IOException;
import com.mojang.blaze3d.platform.GlStateManager;
import com.mojang.blaze3d.systems.RenderSystem;
import org.apache.logging.log4j.Logger;

public class ProgramManager {
    private static final Logger LOGGER;
    
    public static void glUseProgram(final int integer) {
        RenderSystem.assertThread(RenderSystem::isOnRenderThread);
        GlStateManager._glUseProgram(integer);
    }
    
    public static void releaseProgram(final Effect dhc) {
        RenderSystem.assertThread(RenderSystem::isOnRenderThread);
        dhc.getFragmentProgram().close();
        dhc.getVertexProgram().close();
        GlStateManager.glDeleteProgram(dhc.getId());
    }
    
    public static int createProgram() throws IOException {
        RenderSystem.assertThread(RenderSystem::isOnRenderThread);
        final int integer1 = GlStateManager.glCreateProgram();
        if (integer1 <= 0) {
            throw new IOException("Could not create shader program (returned program ID " + integer1 + ")");
        }
        return integer1;
    }
    
    public static void linkProgram(final Effect dhc) throws IOException {
        RenderSystem.assertThread(RenderSystem::isOnRenderThread);
        dhc.getFragmentProgram().attachToEffect(dhc);
        dhc.getVertexProgram().attachToEffect(dhc);
        GlStateManager.glLinkProgram(dhc.getId());
        final int integer2 = GlStateManager.glGetProgrami(dhc.getId(), 35714);
        if (integer2 == 0) {
            ProgramManager.LOGGER.warn("Error encountered when linking program containing VS {} and FS {}. Log output:", dhc.getVertexProgram().getName(), dhc.getFragmentProgram().getName());
            ProgramManager.LOGGER.warn(GlStateManager.glGetProgramInfoLog(dhc.getId(), 32768));
        }
    }
    
    static {
        LOGGER = LogManager.getLogger();
    }
}
