-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathShader.cpp
213 lines (160 loc) · 6.03 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
// Shadersave - A screensaver that can run Shadertoy shaders locally.
// Copyright (C) 2024 Analog Feelings
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
#include <Globals.h>
#include <Classes/Shader.h>
#include <GL/glew.h>
#include <sstream>
auto Shader::LoadShader(const std::string& vertexText) -> bool
{
const char* constVertex = vertexText.c_str();
unsigned int createdVertex = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(createdVertex, 1, &constVertex, nullptr);
glCompileShader(createdVertex);
bool vertexResult = this->CheckCompileErrors(createdVertex, "VERTEX");
this->VertexShader = createdVertex;
return vertexResult;
}
auto Shader::LoadShadertoyShader(std::string& fragmentText, const std::string& commonText) -> bool
{
if (fragmentText.empty())
{
Globals::LastError = "Shader file was empty.";
return false;
}
std::stringstream stream;
stream << "#version 430 core" << "\n";
stream << "uniform vec3 iResolution;" << "\n";
stream << "uniform float iTime;" << "\n";
stream << "uniform float iTimeDelta;" << "\n";
stream << "uniform float iFrameRate;" << "\n";
stream << "uniform int iFrame;" << "\n";
stream << "uniform sampler2D iChannel0;" << "\n";
stream << "uniform sampler2D iChannel1;" << "\n";
stream << "uniform sampler2D iChannel2;" << "\n";
stream << "uniform sampler2D iChannel3;" << "\n";
stream << "uniform float iChannelTime[4];" << "\n"; // This gets ignored but still needs a definition.
stream << "uniform vec3 iChannelResolution[4];" << "\n";
stream << "uniform vec4 iDate;" << "\n";
stream << "uniform vec4 iMouse;" << "\n"; // This gets ignored but still needs a definition.
stream << commonText << "\n";
stream << fragmentText << "\n";
stream << "out vec4 analogFeelings_Output;" << "\n";
stream << "void main() {" << "\n";
stream << " mainImage(analogFeelings_Output, gl_FragCoord.xy);" << "\n";
stream << "}" << "\n";
fragmentText = stream.str();
const char* constFragment = fragmentText.c_str();
unsigned int createdFragment = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(createdFragment, 1, &constFragment, nullptr);
glCompileShader(createdFragment);
bool fragmentResult = this->CheckCompileErrors(createdFragment, "FRAGMENT");
this->FragmentShader = createdFragment;
return fragmentResult;
}
auto Shader::CreateProgram() -> bool
{
this->ProgramId = glCreateProgram();
glAttachShader(this->ProgramId, this->VertexShader);
glAttachShader(this->ProgramId, this->FragmentShader);
glLinkProgram(this->ProgramId);
bool programResult = this->CheckCompileErrors(this->ProgramId, "PROGRAM");
if (!programResult)
return false;
glDeleteShader(this->VertexShader);
glDeleteShader(this->FragmentShader);
int uniformCount;
glGetProgramiv(this->ProgramId, GL_ACTIVE_UNIFORMS, &uniformCount);
for (int i = 0; i < uniformCount; i++)
{
int length, size;
unsigned int type;
char buffer[UNIFORM_BUFFER_SIZE] = {};
glGetActiveUniform(this->ProgramId, i, UNIFORM_BUFFER_SIZE, &length, &size, &type, buffer);
std::string bufferString(buffer);
int location = glGetUniformLocation(this->ProgramId, buffer);
if (bufferString.ends_with("[0]"))
bufferString.resize(bufferString.size() - 3);
this->UniformMap.insert(std::pair(bufferString, location));
}
return true;
}
auto Shader::UseShader() -> void
{
glUseProgram(this->ProgramId);
}
Shader::~Shader()
{
glDeleteProgram(this->ProgramId);
}
auto Shader::SetIntUniform(const std::string& name, int value) -> void
{
if(!this->UniformMap.contains(name)) return;
int uniformLocation = this->UniformMap.at(name);
glProgramUniform1i(this->ProgramId, uniformLocation, value);
}
auto Shader::SetFloatUniform(const std::string& name, float value) -> void
{
if(!this->UniformMap.contains(name)) return;
int uniformLocation = this->UniformMap.at(name);
glProgramUniform1f(this->ProgramId, uniformLocation, value);
}
auto Shader::SetVector2Uniform(const std::string& name, float x, float y) -> void
{
if(!this->UniformMap.contains(name)) return;
int uniformLocation = this->UniformMap.at(name);
glProgramUniform2f(this->ProgramId, uniformLocation, x, y);
}
auto Shader::SetVector3Uniform(const std::string& name, float x, float y, float z) -> void
{
if(!this->UniformMap.contains(name)) return;
int uniformLocation = this->UniformMap.at(name);
glProgramUniform3f(this->ProgramId, uniformLocation, x, y, z);
}
auto Shader::SetVector4Uniform(const std::string& name, float x, float y, float z, float w) -> void
{
if(!this->UniformMap.contains(name)) return;
int uniformLocation = this->UniformMap.at(name);
glProgramUniform4f(this->ProgramId, uniformLocation, x, y, z, w);
}
auto Shader::SetVector3ArrayUniform(const std::string& name, Vector3* value, int size) -> void
{
if (!this->UniformMap.contains(name)) return;
int uniformLocation = this->UniformMap.at(name);
glProgramUniform3fv(this->ProgramId, uniformLocation, size, reinterpret_cast<float*>(value));
}
auto Shader::CheckCompileErrors(unsigned int shaderId, const std::string& type) -> bool
{
int isSuccess;
if (type != "PROGRAM")
{
glGetShaderiv(shaderId, GL_COMPILE_STATUS, &isSuccess);
if (!isSuccess)
{
glGetShaderInfoLog(shaderId, SHADER_LOG_SIZE, nullptr, this->ShaderLog);
Globals::LastError = this->ShaderLog;
}
}
else
{
glGetProgramiv(shaderId, GL_LINK_STATUS, &isSuccess);
if (!isSuccess)
{
glGetProgramInfoLog(shaderId, SHADER_LOG_SIZE, nullptr, this->ShaderLog);
Globals::LastError = this->ShaderLog;
}
}
return isSuccess;
}