From 1cc08c51eb4b0f95c30c0a98ad1fc5ad3459b2df Mon Sep 17 00:00:00 2001 From: Nicolas James Date: Wed, 12 Feb 2025 18:05:18 +1100 Subject: initial commit --- src/client/render/program.cc | 55 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 src/client/render/program.cc (limited to 'src/client/render/program.cc') diff --git a/src/client/render/program.cc b/src/client/render/program.cc new file mode 100644 index 0000000..d658b68 --- /dev/null +++ b/src/client/render/program.cc @@ -0,0 +1,55 @@ +#include "client/render/program.hh" + +namespace client { +namespace render { + +static void check_shader(const GLuint shader) { + int status = 0; + char info[512]; + glGetShaderiv(shader, GL_COMPILE_STATUS, &status); + + if (!status) { + glGetShaderInfoLog(shader, 512, nullptr, info); + throw std::runtime_error(std::string("failed to compile shader: ") + + info); + } +} + +static void check_program(const GLuint program) { + int status = 0; + char info[512]; + glGetProgramiv(program, GL_LINK_STATUS, &status); + + if (!status) { + glGetProgramInfoLog(program, 512, nullptr, info); + throw std::runtime_error(std::string("failed to link program: ") + + info); + } +} + +program::program(const std::string_view vpath, const std::string_view fpath) { + const auto make_shader = [](const auto& path, const auto type) -> GLuint { + const std::string source = shared::read_file(path.data()); + const char* const src_ptr = source.c_str(); + GLuint shader = glCreateShader(type); + glShaderSource(shader, 1, &src_ptr, nullptr); + glCompileShader(shader); + check_shader(shader); + return shader; + }; + + GLuint vertex = make_shader(vpath, GL_VERTEX_SHADER); + GLuint fragment = make_shader(fpath, GL_FRAGMENT_SHADER); + + this->index = glCreateProgram(); + glAttachShader(this->index, vertex); + glAttachShader(this->index, fragment); + glLinkProgram(this->index); + check_program(this->index); + + glDeleteShader(vertex); + glDeleteShader(fragment); +} + +} // namespace render +} // namespace client -- cgit v1.2.3