1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
|
#include "OpenGLShader.h"
#include <glad/glad.h>
#include <glm/gtc/type_ptr.hpp>
#include <fstream>
#include <iostream>
namespace Donut
{
static uint32_t ShaderTypeFromString(const std::string& type)
{
if (type == "vertex")
return GL_VERTEX_SHADER;
if (type == "fragment" || type == "pixel")
return GL_FRAGMENT_SHADER;
if (type == "compute")
return GL_COMPUTE_SHADER;
return 0;
}
// Shaders are authored in Slang and compiled to Assets/Shaders/generated/
// <name>.glsl by Tools/compile-shaders.sh. Given a legacy ".../<name>.glsl"
// path, prefer that generated file when present; otherwise fall back to the
// hand-written GLSL (e.g. shaders not yet ported to Slang).
static std::string ResolveShaderPath(const std::string& filepath)
{
size_t slash = filepath.find_last_of("/\\");
std::string dir = (slash == std::string::npos) ? std::string() : filepath.substr(0, slash + 1);
std::string file = (slash == std::string::npos) ? filepath : filepath.substr(slash + 1);
size_t dot = file.rfind('.');
std::string base = (dot == std::string::npos) ? file : file.substr(0, dot);
std::string generated = dir + "generated/" + base + ".glsl";
std::ifstream test(generated);
if (test.good())
return generated;
return filepath;
}
OpenGLShader::OpenGLShader(const std::string& filepath)
{
std::string resolved = ResolveShaderPath(filepath);
m_IsSlang = (resolved != filepath);
std::string source = ReadFile(resolved);
auto shaderSources = PreProcess(source);
Compile(shaderSources);
auto lastSlash = filepath.find_last_of("/\\");
lastSlash = lastSlash == std::string::npos ? 0 : lastSlash + 1;
auto lastDot = filepath.rfind('.');
auto count = lastDot == std::string::npos ? filepath.size() - lastSlash : lastDot - lastSlash;
m_Name = filepath.substr(lastSlash, count);
}
OpenGLShader::OpenGLShader(const std::string& name, const std::string& vertexSrc, const std::string& fragmentSrc)
: m_Name(name)
{
std::unordered_map<uint32_t, std::string> sources;
sources[GL_VERTEX_SHADER] = vertexSrc;
sources[GL_FRAGMENT_SHADER] = fragmentSrc;
Compile(sources);
}
OpenGLShader::OpenGLShader(const std::string& name, const std::string& computeSrc)
: m_Name(name)
{
std::unordered_map<uint32_t, std::string> sources;
sources[GL_COMPUTE_SHADER] = computeSrc;
Compile(sources);
}
OpenGLShader::~OpenGLShader()
{
glDeleteProgram(m_RendererID);
}
std::string OpenGLShader::ReadFile(const std::string& filepath)
{
std::string result;
std::ifstream in(filepath, std::ios::in |
std::ios::binary);
if (in)
{
in.seekg(0, std::ios::end);
size_t size = in.tellg();
if (size != -1)
{
result.resize(size);
in.seekg(0, std::ios::beg);
in.read(&result[0], size);
}
}
return result;
}
std::unordered_map<uint32_t, std::string> OpenGLShader::PreProcess(const std::string& source)
{
std::unordered_map<uint32_t, std::string> shaderSources;
const char* typeToken = "#type";
size_t typeTokenLength = strlen(typeToken);
size_t pos = source.find(typeToken, 0);
while (pos != std::string::npos)
{
size_t eol = source.find_first_of("\r\n", pos);
size_t begin = pos + typeTokenLength + 1;
std::string type = source.substr(begin, eol - begin);
size_t nextLinePos = source.find_first_not_of("\r\n", eol);
pos = source.find(typeToken, nextLinePos);
shaderSources[ShaderTypeFromString(type)] = (pos == std::string::npos) ? source.substr(nextLinePos) :
source.substr(nextLinePos, pos - nextLinePos);
}
return shaderSources;
}
void OpenGLShader::Compile(const std::unordered_map<uint32_t, std::string>& shaderSources)
{
uint32_t program = glCreateProgram();
std::vector<uint32_t> glShaderIDs(shaderSources.size());
for (auto& kv : shaderSources)
{
uint32_t type = kv.first;
const std::string& source = kv.second;
uint32_t shader = glCreateShader(type);
const char* sourceCStr = source.c_str();
glShaderSource(shader, 1, &sourceCStr, 0);
glCompileShader(shader);
int isCompiled = 0;
glGetShaderiv(shader, GL_COMPILE_STATUS, &isCompiled);
if (isCompiled == GL_FALSE)
{
int maxLength = 0;
glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &maxLength);
std::vector<char> infoLog(maxLength);
glGetShaderInfoLog(shader, maxLength, &maxLength, &infoLog[0]);
glDeleteShader(shader);
for (auto id : glShaderIDs)
glDeleteShader(id);
glDeleteProgram(program);
m_RendererID = 0;
// infoLog.data() is null when the driver returns an empty log
// (e.g. macOS rejecting a compute shader); streaming a null
// char* into std::cout calls strlen(NULL) and crashes.
const char* log = infoLog.empty() ? "" : infoLog.data();
std::cout << "Shader compilation failure!" << std::endl << log << std::endl;
return;
}
glAttachShader(program, shader);
glShaderIDs.push_back(shader);
}
m_RendererID = program;
glLinkProgram(m_RendererID);
int isLinked = 0;
glGetProgramiv(m_RendererID, GL_LINK_STATUS, (int*)&isLinked);
if (isLinked == GL_FALSE)
{
int maxLength = 0;
glGetProgramiv(m_RendererID, GL_INFO_LOG_LENGTH, &maxLength);
std::vector<char> infoLog(maxLength);
glGetProgramInfoLog(m_RendererID, maxLength, &maxLength, &infoLog[0]);
glDeleteProgram(m_RendererID);
for (auto id : glShaderIDs)
glDeleteShader(id);
m_RendererID = 0;
const char* log = infoLog.empty() ? "" : infoLog.data();
std::cout << "Shader link failure!" << std::endl << log << std::endl;
return;
}
for (auto id : glShaderIDs)
{
glDetachShader(m_RendererID, id);
glDeleteShader(id);
}
}
void OpenGLShader::Bind() const
{
glUseProgram(m_RendererID);
}
void OpenGLShader::Unbind() const
{
glUseProgram(0);
}
void OpenGLShader::SetInt(const std::string& name, int value)
{
UploadUniformInt(name, value);
}
void OpenGLShader::SetIntArray(const std::string& name, int* values, uint32_t count)
{
UploadUniformIntArray(name, values, count);
}
void OpenGLShader::SetFloat(const std::string& name, float value)
{
UploadUniformFloat(name, value);
}
void OpenGLShader::SetFloat2(const std::string& name, const glm::vec2& value)
{
UploadUniformFloat2(name, value);
}
void OpenGLShader::SetFloat3(const std::string& name, const glm::vec3& value)
{
UploadUniformFloat3(name, value);
}
void OpenGLShader::SetFloat4(const std::string& name, const glm::vec4& value)
{
UploadUniformFloat4(name, value);
}
void OpenGLShader::SetMat4(const std::string& name, const glm::mat4& value)
{
UploadUniformMat4(name, value);
}
void OpenGLShader::UploadUniformInt(const std::string& name, int value)
{
int location = glGetUniformLocation(m_RendererID, name.c_str());
glUniform1i(location, value);
}
void OpenGLShader::UploadUniformIntArray(const std::string& name, int* values, uint32_t count)
{
int location = glGetUniformLocation(m_RendererID, name.c_str());
glUniform1iv(location, count, values);
}
void OpenGLShader::UploadUniformFloat(const std::string& name, float value)
{
int location = glGetUniformLocation(m_RendererID, name.c_str());
glUniform1f(location, value);
}
void OpenGLShader::UploadUniformFloat2(const std::string& name, const glm::vec2& value)
{
int location = glGetUniformLocation(m_RendererID, name.c_str());
glUniform2f(location, value.x, value.y);
}
void OpenGLShader::UploadUniformFloat3(const std::string& name, const glm::vec3& value)
{
int location = glGetUniformLocation(m_RendererID, name.c_str());
glUniform3f(location, value.x, value.y, value.z);
}
void OpenGLShader::UploadUniformFloat4(const std::string& name, const glm::vec4& value)
{
int location = glGetUniformLocation(m_RendererID, name.c_str());
glUniform4f(location, value.x, value.y, value.z, value.w);
}
void OpenGLShader::UploadUniformMat3(const std::string& name, const glm::mat3& matrix)
{
int location = glGetUniformLocation(m_RendererID, name.c_str());
glUniformMatrix3fv(location, 1, m_IsSlang ? GL_TRUE : GL_FALSE, glm::value_ptr(matrix));
}
void OpenGLShader::UploadUniformMat4(const std::string& name, const glm::mat4& matrix)
{
int location = glGetUniformLocation(m_RendererID, name.c_str());
glUniformMatrix4fv(location, 1, m_IsSlang ? GL_TRUE : GL_FALSE, glm::value_ptr(matrix));
}
void OpenGLShader::Dispatch(uint32_t x, uint32_t y, uint32_t z)
{
// Compute shaders require OpenGL 4.3+. On drivers that cap out earlier
// (e.g. macOS, which is frozen at 4.1) glDispatchCompute is never
// loaded and the pointer is null. Guard so we no-op instead of crash.
if (m_RendererID == 0 || glDispatchCompute == nullptr)
return;
glDispatchCompute(x, y, z);
}
void OpenGLShader::DispatchIndirect(uint32_t offset)
{
if (m_RendererID == 0 || glDispatchComputeIndirect == nullptr)
return;
glDispatchComputeIndirect(offset);
}
void OpenGLShader::MemoryBarrier(uint32_t barriers)
{
if (glMemoryBarrier == nullptr)
return;
glMemoryBarrier(barriers);
}
};
|