aboutsummaryrefslogtreecommitdiff
path: root/src/client/render/program.cc
diff options
context:
space:
mode:
authorNicolas James <Eele1Ephe7uZahRie@tutanota.com>2025-02-12 18:05:18 +1100
committerNicolas James <Eele1Ephe7uZahRie@tutanota.com>2025-02-12 18:05:18 +1100
commit1cc08c51eb4b0f95c30c0a98ad1fc5ad3459b2df (patch)
tree222dfcd07a1e40716127a347bbfd7119ce3d0984 /src/client/render/program.cc
initial commit
Diffstat (limited to 'src/client/render/program.cc')
-rw-r--r--src/client/render/program.cc55
1 files changed, 55 insertions, 0 deletions
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