module GL
  @programs = {}

  class << self

    def load_shaders
      program_sources = {
        text: ["text.v.glsl", "text.f.glsl"],
        basic: ["basic.v.glsl", "basic.f.glsl"],
        rect: ["rect.v.glsl", "basic.f.glsl"],
      }

      program_sources.each do |program, (v_src_fname, f_src_fname)|
        v_src = Runtime.read(:shader, v_src_fname)
        v_shader = GLShader.new(:vertex, v_src)
        v_shader or raise "Could not find shader #{v_src_fname}"

        f_src = Runtime.read(:shader, f_src_fname)
        f_shader = GLShader.new(:fragment, f_src)
        f_shader or raise "Could not find shader #{f_src_fname}"

        uniforms = (v_src + f_src).lines.map do |line|
          if line =~ /^\s*uniform\s.*\s(\S+);/
            $1
          end
        end.compact

        @programs[program] = GLProgram.new(v_shader, f_shader,
                                           uniforms: uniforms)
      end
    end

  end
end
