This repository was archived by the owner on Oct 25, 2023. It is now read-only.
forked from Drewol/unnamed-sdvx-clone
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathShader.cpp
303 lines (277 loc) · 7.14 KB
/
Shader.cpp
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
303
#include "stdafx.h"
#include "Shader.hpp"
#include <Graphics/ResourceManagers.hpp>
#include "OpenGL.hpp"
namespace Graphics
{
#ifdef EMBEDDED
const uint32 typeMap[] =
{
GL_VERTEX_SHADER,
GL_FRAGMENT_SHADER,
};
#else
const uint32 typeMap[] =
{
GL_VERTEX_SHADER,
GL_FRAGMENT_SHADER,
GL_GEOMETRY_SHADER,
};
const uint32 shaderStageMap[] =
{
GL_VERTEX_SHADER_BIT,
GL_FRAGMENT_SHADER_BIT,
GL_GEOMETRY_SHADER_BIT,
};
#endif
class Shader_Impl : public ShaderRes
{
ShaderType m_type;
uint32 m_prog;
OpenGL* m_gl;
String m_sourcePath;
// Hot Reload detection on windows
#ifdef _WIN32
HANDLE m_changeNotification = INVALID_HANDLE_VALUE;
uint64 m_lwt = -1;
#endif
public:
Shader_Impl(OpenGL* gl) : m_gl(gl)
{
}
~Shader_Impl()
{
// Cleanup OpenGL resource
if(glIsProgram(m_prog))
{
glDeleteProgram(m_prog);
}
#ifdef _WIN32
// Close change notification handle
if(m_changeNotification != INVALID_HANDLE_VALUE)
{
CloseHandle(m_changeNotification);
}
#endif
}
void SetupChangeHandler()
{
#ifdef _WIN32
if(m_changeNotification != INVALID_HANDLE_VALUE)
{
CloseHandle(m_changeNotification);
m_changeNotification = INVALID_HANDLE_VALUE;
}
WString rootFolder = Utility::ConvertToWString(Path::RemoveLast(m_sourcePath));
m_changeNotification = FindFirstChangeNotificationW(*rootFolder, false, FILE_NOTIFY_CHANGE_LAST_WRITE);
#endif
}
#ifdef EMBEDDED
bool LoadProgram(uint32& programOut)
{
File in;
if(!in.OpenRead(m_sourcePath))
return false;
String sourceStr;
sourceStr.resize(in.GetSize());
if(sourceStr.size() == 0)
return false;
in.Read(&sourceStr.front(), sourceStr.size());
sourceStr = "#version 100\n#define EMBEDDED\n#define target gl_FragColor\n#define texture texture2D\nprecision mediump float;\n" + sourceStr;
const GLint programsize = sourceStr.size();
const char* pChars = *sourceStr;
glShaderSource(programOut, 1, &pChars, &programsize);
glCompileShader(programOut);
int nStatus = 0;
glGetShaderiv(programOut, GL_COMPILE_STATUS, &nStatus);
if(nStatus == GL_FALSE)
{
static char infoLogBuffer[2048];
int s = 0;
glGetShaderInfoLog(programOut, sizeof(infoLogBuffer), &s, infoLogBuffer);
Logf("Shader program compile log for %s: %s", Logger::Severity::Error, m_sourcePath, infoLogBuffer);
return false;
}
// Shader hot-reload in debug mode
#if defined(_DEBUG) && defined(_WIN32)
// Store last write time
m_lwt = in.GetLastWriteTime();
SetupChangeHandler();
#endif
return true;
}
#else
bool LoadProgram(uint32& programOut)
{
File in;
if(!in.OpenRead(m_sourcePath))
return false;
String sourceStr;
sourceStr.resize(in.GetSize());
if(sourceStr.size() == 0)
return false;
in.Read(&sourceStr.front(), sourceStr.size());
String firstLine;
sourceStr.Split("\n", &firstLine, nullptr);
firstLine.Trim('\r');
firstLine.ToLower();
if (firstLine.compare("#version 330") != 0)
{
sourceStr = "#version 330\n" + sourceStr;
}
const char* pChars = *sourceStr;
programOut = glCreateShaderProgramv(typeMap[(size_t)m_type], 1, &pChars);
if(programOut == 0)
return false;
int nStatus = 0;
glGetProgramiv(programOut, GL_LINK_STATUS, &nStatus);
if(nStatus == 0)
{
static char infoLogBuffer[2048];
int s = 0;
glGetProgramInfoLog(programOut, sizeof(infoLogBuffer), &s, infoLogBuffer);
Logf("Shader program compile log for %s: %s", Logger::Severity::Error, m_sourcePath, infoLogBuffer);
return false;
}
// Shader hot-reload in debug mode
#if defined(_DEBUG) && defined(_WIN32)
// Store last write time
m_lwt = in.GetLastWriteTime();
SetupChangeHandler();
#endif
return true;
}
#endif
bool UpdateHotReload() override
{
#ifdef _WIN32
if(m_changeNotification != INVALID_HANDLE_VALUE)
{
if(WaitForSingleObject(m_changeNotification, 0) == WAIT_OBJECT_0)
{
uint64 newLwt = File::GetLastWriteTime(m_sourcePath);
if(newLwt != -1 && newLwt > m_lwt)
{
uint32 newProgram = 0;
if(LoadProgram(newProgram))
{
// Successfully reloaded
m_prog = newProgram;
return true;
}
}
// Watch for new change
SetupChangeHandler();
}
}
#endif
return false;
}
bool Init(ShaderType type, const String& name)
{
m_sourcePath = Path::Normalize(name);
m_type = type;
#ifdef EMBEDDED
m_prog = glCreateShader(typeMap[(size_t)type]);
#endif
return LoadProgram(m_prog);
}
#ifndef EMBEDDED
void Bind() override
{
if(m_gl->m_activeShaders[(size_t)m_type] != this)
{
glUseProgramStages(m_gl->m_mainProgramPipeline, shaderStageMap[(size_t)m_type], m_prog);
m_gl->m_activeShaders[(size_t)m_type] = this;
}
}
bool IsBound() const override
{
return m_gl->m_activeShaders[(size_t)m_type] == this;
}
uint32 GetLocation(const String& name) const override
{
return glGetUniformLocation(m_prog, name.c_str());
}
virtual void BindUniform(uint32 loc, const Transform& mat)
{
glProgramUniformMatrix4fv(m_prog, loc, 1, false, mat.mat);
}
virtual void BindUniformVec2(uint32 loc, const Vector2& v)
{
glProgramUniform2fv(m_prog, loc, 1, &v.x);
}
virtual void BindUniformVec3(uint32 loc, const Vector3& v)
{
glProgramUniform3fv(m_prog, loc, 1, &v.x);
}
virtual void BindUniformVec4(uint32 loc, const Vector4& v)
{
glProgramUniform4fv(m_prog, loc, 1, &v.x);
}
virtual void BindUniform(uint32 loc, int i)
{
glProgramUniform1i(m_prog, loc, i);
}
virtual void BindUniform(uint32 loc, float i)
{
glProgramUniform1f(m_prog, loc, i);
}
virtual void BindUniformArray(uint32 loc, const Transform* mat, size_t count)
{
glProgramUniformMatrix4fv(m_prog, loc, (int)count, false, (float*)mat);
}
virtual void BindUniformArray(uint32 loc, const Vector2* v2, size_t count)
{
glProgramUniform2fv(m_prog, loc, (int)count, (float*)v2);
}
virtual void BindUniformArray(uint32 loc, const Vector3* v3, size_t count)
{
glProgramUniform3fv(m_prog, loc, (int)count, (float*)v3);
}
virtual void BindUniformArray(uint32 loc, const Vector4* v4, size_t count)
{
glProgramUniform4fv(m_prog, loc, (int)count, (float*)v4);
}
virtual void BindUniformArray(uint32 loc, const float* i, size_t count)
{
glProgramUniform1fv(m_prog, loc, (int)count, i);
}
virtual void BindUniformArray(uint32 loc, const int* i, size_t count)
{
glProgramUniform1iv(m_prog, loc, (int)count, i);
}
#endif
virtual uint32 Handle() override
{
return m_prog;
}
String GetOriginalName() const override
{
return m_sourcePath;
}
};
Shader ShaderRes::Create(class OpenGL* gl, ShaderType type, const String& assetPath)
{
Shader_Impl* pImpl = new Shader_Impl(gl);
if(!pImpl->Init(type, assetPath))
{
delete pImpl;
return Shader();
}
else
{
return GetResourceManager<ResourceType::Shader>().Register(pImpl);
}
}
void ShaderRes::Unbind(class OpenGL* gl, ShaderType type)
{
#ifndef EMBEDDED
if(gl->m_activeShaders[(size_t)type] != 0)
{
glUseProgramStages(gl->m_mainProgramPipeline, shaderStageMap[(size_t)type], 0);
gl->m_activeShaders[(size_t)type] = 0;
}
#endif
}
}