#include "pch.h"
#include "app.h"
#include "shader.h"

void App::initShaders()
{
#ifdef _DEBUG
    if (!check_uniform_uniqueness())
        std::logic_error("check_uniform_uniqueness() failed");
#endif // _DEBUG

    GLint n_exts;
    glGetIntegerv(GL_NUM_EXTENSIONS, &n_exts);
    for (int i = 0; i < n_exts; i++)
    {
        std::string ext = (const char*)glGetStringi(GL_EXTENSIONS, i);
        if (ext.find("shader_framebuffer_fetch") != std::string::npos)
        {
            ShaderManager::ext_framebuffer_fetch = true;
        }
    }

    LOG("Shader Extension shader_framebuffer_fetch: %s", ShaderManager::ext_framebuffer_fetch ? "enabled" : "disabled");

    LOG("initializing shaders");
    if (!ShaderManager::load(kShader::Texture, "data/shaders/texture.glsl"))
        LOG("Failed to create shader Texture");
    if (!ShaderManager::load(kShader::TextureAlpha, "data/shaders/texture-alpha.glsl"))
        LOG("Failed to create shader TextureAlpha");
    if (!ShaderManager::load(kShader::TextureAlphaSep, "data/shaders/texture-alpha-sep.glsl"))
        LOG("Failed to create shader TextureAlphaSep");
    if (!ShaderManager::load(kShader::TextureBlend, "data/shaders/texture-blend.glsl"))
        LOG("Failed to create shader TextureBlend");
    if (!ShaderManager::load(kShader::StrokePreview, "data/shaders/stroke-preview.glsl"))
        LOG("Failed to create shader StrokePreview");
    if (!ShaderManager::load(kShader::CompErase, "data/shaders/comp-erase.glsl"))
        LOG("Failed to create shader CompErase");
    if (!ShaderManager::load(kShader::CompDraw, "data/shaders/comp-draw.glsl"))
        LOG("Failed to create shader CompDraw");
    if (!ShaderManager::load(kShader::Color, "data/shaders/color.glsl"))
        LOG("Failed to create shader Color");
    if (!ShaderManager::load(kShader::ColorQuad, "data/shaders/color-quad.glsl"))
        LOG("Failed to create shader ColorQuad");
    if (!ShaderManager::load(kShader::ColorTri, "data/shaders/color-tri.glsl"))
        LOG("Failed to create shader ColorTri");
    if (!ShaderManager::load(kShader::ColorHue, "data/shaders/color-hue.glsl"))
        LOG("Failed to create shader ColorHue");
    if (!ShaderManager::load(kShader::UVs, "data/shaders/uvs.glsl"))
        LOG("Failed to create shader UVs");
    if (!ShaderManager::load(kShader::Font, "data/shaders/font.glsl"))
        LOG("Failed to create shader Font");
    if (!ShaderManager::load(kShader::Atlas, "data/shaders/atlas.glsl"))
        LOG("Failed to create shader Atlas");
    if (!ShaderManager::load(kShader::Stroke, "data/shaders/stroke.glsl"))
        LOG("Failed to create shader Stroke");
    if (!ShaderManager::load(kShader::Checkerboard, "data/shaders/checkerboard.glsl"))
        LOG("Failed to create shader Checkerboard");
    if (!ShaderManager::load(kShader::Equirect, "data/shaders/equirect.glsl"))
        LOG("Failed to create shader Equirect");
    if (!ShaderManager::load(kShader::BrushStroke, "data/shaders/stroke-instanced.glsl"))
        LOG("Failed to create shader BrushStroke");
    if (!ShaderManager::load(kShader::VertexColor, "data/shaders/vertex-color.glsl"))
        LOG("Failed to create shader VertexColor");
    if (!ShaderManager::load(kShader::Lambert, "data/shaders/lambert.glsl"))
        LOG("Failed to create shader Lambert");
    if (!ShaderManager::load(kShader::LambertLightmap, "data/shaders/lightmap.glsl"))
        LOG("Failed to create shader LambertLightmap");
    if (!ShaderManager::load(kShader::BakeUV, "data/shaders/bake-uv.glsl"))
        LOG("Failed to create shader BakeUV");
    LOG("shaders initialized");
}


