From 52d7eb5eab6180cf45fb4a3b5a77b9fbe6770c6c Mon Sep 17 00:00:00 2001 From: Josh Holtrop Date: Mon, 7 Dec 2020 15:40:11 -0500 Subject: [PATCH] Allow passing shaders and attribute locations to Program constructor. --- src/gltk/program.d | 29 +++++++++++++++++++++++++---- src/jes/gui/package.d | 6 +----- 2 files changed, 26 insertions(+), 9 deletions(-) diff --git a/src/gltk/program.d b/src/gltk/program.d index 14d9879..4c096d5 100644 --- a/src/gltk/program.d +++ b/src/gltk/program.d @@ -7,13 +7,17 @@ class Program { private GLuint m_id; - this() + this(Args...)(Args args) { m_id = glCreateProgram(); if (m_id == 0u) { throw new Exception("Failed to allocate an OpenGL program"); } + static if (args.length != 0u) + { + build(args); + } } ~this() @@ -26,7 +30,7 @@ class Program glAttachShader(m_id, shader.id); } - void bind_attrib_location(uint index, string name) + void bind_attrib_location(string name, uint index) const { glBindAttribLocation(m_id, index, name.toStringz()); } @@ -54,12 +58,12 @@ class Program } } - GLint get_uniform_location(string uniform_name) + GLint get_uniform_location(string uniform_name) const { return glGetUniformLocation(m_id, uniform_name.toStringz()); } - @property GLuint id() + @property GLuint id() const { return m_id; } @@ -68,4 +72,21 @@ class Program { glUseProgram(m_id); } + + private void build(Args...)(Shader s, Args args) const + { + attach_shader(s); + build(args); + } + + private void build(Args...)(string attrib_name, uint index, Args args) const + { + bind_attrib_location(attrib_name, index); + build(args); + } + + private void build() const + { + link(); + } } diff --git a/src/jes/gui/package.d b/src/jes/gui/package.d index 3ddd848..b777cfe 100644 --- a/src/jes/gui/package.d +++ b/src/jes/gui/package.d @@ -47,11 +47,7 @@ class Gui v_shader.set_source_from_file("share/jes/shaders/text.v.glsl"); auto f_shader = new gltk.Shader(GL_FRAGMENT_SHADER); f_shader.set_source_from_file("share/jes/shaders/text.f.glsl"); - m_text_program = new gltk.Program(); - m_text_program.attach_shader(v_shader); - m_text_program.attach_shader(f_shader); - m_text_program.bind_attrib_location(0u, "coords"); - m_text_program.link(); + m_text_program = new gltk.Program(v_shader, f_shader, "coords", 0); m_viewport_size = m_text_program.get_uniform_location("viewport_size"); m_texture = m_text_program.get_uniform_location("texture"); m_color = m_text_program.get_uniform_location("color");