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 cyb) {
        RenderSystem.assertThread(RenderSystem::isOnRenderThread);
        cyb.getFragmentProgram().close();
        cyb.getVertexProgram().close();
        GlStateManager.glDeleteProgram(cyb.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 cyb) throws IOException {
        RenderSystem.assertThread(RenderSystem::isOnRenderThread);
        cyb.getFragmentProgram().attachToEffect(cyb);
        cyb.getVertexProgram().attachToEffect(cyb);
        GlStateManager.glLinkProgram(cyb.getId());
        final int integer2 = GlStateManager.glGetProgrami(cyb.getId(), 35714);
        if (integer2 == 0) {
            ProgramManager.LOGGER.warn("Error encountered when linking program containing VS {} and FS {}. Log output:", cyb.getVertexProgram().getName(), cyb.getFragmentProgram().getName());
            ProgramManager.LOGGER.warn(GlStateManager.glGetProgramInfoLog(cyb.getId(), 32768));
        }
    }
    
    static {
        LOGGER = LogManager.getLogger();
    }
}
