SleakEngine 0.1.0
C++23 multi-backend game engine
Loading...
Searching...
No Matches
OpenGLShader.cpp
Go to the documentation of this file.
2
3#include <Core/Logger.hpp>
4#include <filesystem>
5#include <fstream>
6#include <sstream>
7#include <string_view>
8
9namespace Sleak {
10namespace RenderEngine {
11
12namespace {
13
14constexpr std::string_view GL_STEM_SUFFIX = "_gl";
15
16/// Returns the file name up to its first '.', minus a trailing _gl.
17std::string StemFromPath(const std::string& path) {
18 std::string stem = std::filesystem::path(path).filename().string();
19 stem = stem.substr(0, stem.find('.'));
20 if (stem.ends_with(GL_STEM_SUFFIX)) {
21 stem.resize(stem.size() - GL_STEM_SUFFIX.size());
22 }
23 return stem;
24}
25
26/// Returns the info log of a shader or program object.
27std::string ReadInfoLog(GLuint object, PFNGLGETSHADERIVPROC getParam,
28 PFNGLGETSHADERINFOLOGPROC getLog) {
29 GLint length = 0;
30 getParam(object, GL_INFO_LOG_LENGTH, &length);
31 if (length <= 0) return {};
32 std::string log(static_cast<size_t>(length), '\0');
33 GLsizei written = 0;
34 getLog(object, length, &written, log.data());
35 log.resize(static_cast<size_t>(written));
36 while (!log.empty() && (log.back() == '\n' || log.back() == '\r')) {
37 log.pop_back();
38 }
39 return log;
40}
41
42#ifdef SLEAK_HOT_RELOAD
43/// Returns the live shader registry, leaked to survive static teardown.
44std::unordered_set<OpenGLShader*>& LiveShaders() {
45 static auto* shaders = new std::unordered_set<OpenGLShader*>();
46 return *shaders;
47}
48#endif
49
50} // namespace
51
53#ifdef SLEAK_HOT_RELOAD
54 LiveShaders().insert(this);
55#endif
56}
57
59#ifdef SLEAK_HOT_RELOAD
60 LiveShaders().erase(this);
61#endif
62 if (m_program != 0) {
63 glDeleteProgram(m_program);
64 m_program = 0;
65 }
66}
67
68#ifdef SLEAK_HOT_RELOAD
69const std::unordered_set<OpenGLShader*>& OpenGLShader::GetLiveShaders() {
70 return LiveShaders();
71}
72#endif
73
74bool OpenGLShader::compile(const std::string& shaderPath) {
75 // Strip .hlsl extension if present, then use _gl.vert/_gl.frag
76 std::string basePath = shaderPath;
77 auto hlslPos = basePath.rfind(".hlsl");
78 if (hlslPos != std::string::npos) {
79 basePath = basePath.substr(0, hlslPos);
80 }
81 return compile(basePath + "_gl.vert", basePath + "_gl.frag");
82}
83
84bool OpenGLShader::compile(const std::string& vertPath,
85 const std::string& fragPath) {
86 m_vertPath = vertPath;
87 m_fragPath = fragPath;
88 m_vertStem = StemFromPath(vertPath);
89 m_fragStem = StemFromPath(fragPath);
90 return BuildProgram();
91}
92
94 if (m_vertPath.empty() || m_fragPath.empty()) return false;
95 return BuildProgram();
96}
97
98bool OpenGLShader::BuildProgram() {
99 const std::string vertSource = ReadFile(m_vertPath);
100 const std::string fragSource = ReadFile(m_fragPath);
101
102 if (vertSource.empty() || fragSource.empty()) {
103 SLEAK_ERROR("Failed to read shader files: {} and {}", m_vertPath,
104 m_fragPath);
105 return false;
106 }
107
108 GLuint vertShader = 0;
109 GLuint fragShader = 0;
110 if (!CompileShaderSource(vertSource, GL_VERTEX_SHADER, m_vertPath,
111 vertShader)) {
112 return false;
113 }
114 if (!CompileShaderSource(fragSource, GL_FRAGMENT_SHADER, m_fragPath,
115 fragShader)) {
116 glDeleteShader(vertShader);
117 return false;
118 }
119
120 GLuint program = glCreateProgram();
121 glAttachShader(program, vertShader);
122 glAttachShader(program, fragShader);
123 glLinkProgram(program);
124 glDetachShader(program, vertShader);
125 glDetachShader(program, fragShader);
126 glDeleteShader(vertShader);
127 glDeleteShader(fragShader);
128
129 GLint linked = GL_FALSE;
130 glGetProgramiv(program, GL_LINK_STATUS, &linked);
131 if (linked != GL_TRUE) {
132 SLEAK_ERROR("Shader program linking failed ({} + {}): {}", m_vertPath,
133 m_fragPath,
134 ReadInfoLog(program, glGetProgramiv, glGetProgramInfoLog));
135 glDeleteProgram(program);
136 return false;
137 }
138
139 if (m_program != 0) glDeleteProgram(m_program);
140 m_program = program;
141 return true;
142}
143
145 if (m_program != 0) {
146 glUseProgram(m_program);
147 }
148}
149
150bool OpenGLShader::CompileShaderSource(const std::string& source, GLenum type,
151 const std::string& path,
152 GLuint& shader) {
153 shader = glCreateShader(type);
154 const char* src = source.c_str();
155 glShaderSource(shader, 1, &src, nullptr);
156 glCompileShader(shader);
157
158 GLint success = GL_FALSE;
159 glGetShaderiv(shader, GL_COMPILE_STATUS, &success);
160 if (success != GL_TRUE) {
161 SLEAK_ERROR("Shader compilation failed ({}): {}", path,
162 ReadInfoLog(shader, glGetShaderiv, glGetShaderInfoLog));
163 glDeleteShader(shader);
164 shader = 0;
165 return false;
166 }
167 return true;
168}
169
170std::string OpenGLShader::ReadFile(const std::string& path) {
171 std::ifstream file(path);
172 if (!file.is_open()) {
173 SLEAK_ERROR("Cannot open shader file: {}", path);
174 return "";
175 }
176 std::stringstream ss;
177 ss << file.rdbuf();
178 return ss.str();
179}
180
181} // namespace RenderEngine
182} // namespace Sleak
#define SLEAK_ERROR(...)
Definition Logger.hpp:22
bool Reload()
Rebuilds the program from its files, keeping the old one on failure.
void bind() override
Binds this program as the active shader for subsequent draws.
bool compile(const std::string &shaderPath) override
Splits a combined shader file into vertex/fragment stages and links them.
Backend-facing rendering layer shared by the four graphics backends.
Root namespace for everything the engine exposes.
Definition Camera.hpp:10