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
|
#include <glad/glad.h>
#include <GLFW/glfw3.h>
#include <Helpers.h>
#include <iostream>
#include <vector>
#include <chrono>
#include <iomanip>
#include <algorithm>
void error_callback(int error, const char* description)
{
std::cerr << "Error: " << description << std::endl;
}
void framebuffer_size_callback(GLFWwindow* window, int width, int height)
{
glViewport(0, 0, width, height);
}
int main(int argc, char * argv[])
{
Helpers::RenderSettings settings;
if(!settings.ParseCommandLine(argc,argv))
{
return 2;
}
unsigned int bufferHeight = settings.imageHeight/2;
GLFWwindow* window;
/* Initialize the library */
if (!glfwInit())
return -1;
glfwSetErrorCallback(error_callback);
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
/* Create a windowed mode window and its OpenGL context */
window = glfwCreateWindow(settings.windowWidth, settings.windowHeight, "Buddhabrot", NULL, NULL);
if (!window)
{
std::cerr << "Failed to create OpenGL 4.3 core context. We do not support compatibility contexts." << std::endl;
glfwTerminate();
return -1;
}
//register callback on window resize:
glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
/* Make the window's context current */
glfwMakeContextCurrent(window);
gladLoadGLLoader((GLADloadproc)glfwGetProcAddress);
//disable vsync
glfwSwapInterval(0);
//we have a context. Let's check if input is sane.
//calcualte buffer size, and make sure it's allowed by the driver.
const unsigned int pixelCount{(settings.imageWidth * bufferHeight)};
if(!settings.CheckValidity())
{
glfwTerminate();
return 1;
}
// Create and compile our GLSL program from the shaders
std::string vertexPath("Shaders/BuddhaVertex.glsl");
std::string fragmentPath("Shaders/BuddhaFragment.glsl");
std::string computePath("Shaders/BuddhaCompute.glsl");
if(!Helpers::DoesFileExist(vertexPath))
{
#if defined _WIN32 || defined __CYGWIN__
std::string separator("\\");
#else
std::string separator("/");
#endif
vertexPath = INSTALL_PREFIX + separator + "share" + separator + "BuddhaShader" + separator + vertexPath;
fragmentPath = INSTALL_PREFIX + separator + "share" + separator + "BuddhaShader" + separator + fragmentPath;
computePath = INSTALL_PREFIX + separator + "share" + separator + "BuddhaShader" + separator + computePath;
}
GLuint VertexAndFragmentShaders = Helpers::LoadShaders(vertexPath, fragmentPath);
//Do the same for the compute shader:
GLuint ComputeShader = Helpers::LoadComputeShader(computePath, settings.localWorkgroupSizeX, settings.localWorkgroupSizeY, settings.localWorkgroupSizeZ);
if(VertexAndFragmentShaders == 0 || ComputeShader == 0)
{
std::cerr << "Something went wrong with loading the shaders. Abort." << std::endl;
glfwTerminate();
return 1;
}
GLuint VertexArrayID;
glGenVertexArrays(1, &VertexArrayID);
glBindVertexArray(VertexArrayID);
const GLfloat g_vertex_buffer_data[] = {
-1.0f, -1.0f, 0.0f,
1.0f, -1.0f, 0.0f,
-1.0f, 1.0f, 0.0f,
1.0f, 1.0f, 0.0f
};
GLuint vertexbuffer;
glGenBuffers(1, &vertexbuffer);
glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);
glBufferData(GL_ARRAY_BUFFER, sizeof(g_vertex_buffer_data), g_vertex_buffer_data, GL_STATIC_DRAW);
GLuint drawBuffer;
glGenBuffers(1, &drawBuffer);
glBindBuffer(GL_SHADER_STORAGE_BUFFER, drawBuffer);
glBufferData(GL_SHADER_STORAGE_BUFFER, 4 *3* pixelCount, nullptr, GL_DYNAMIC_COPY);
glClearBufferData(GL_SHADER_STORAGE_BUFFER,GL_R8,GL_RED,GL_UNSIGNED_INT,nullptr);
glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 2, drawBuffer);
glBindBuffer(GL_SHADER_STORAGE_BUFFER, 0);
//the state buffer is special. While making each entry 8 bytes large and using std430 layout, the data is never read back,
//so we can get away with being more lenient and allowing the compiler to choose layout without much extra work.
//We need to query the size for allocation though.
GLuint stateBufferIndex = glGetProgramResourceIndex(ComputeShader,GL_SHADER_STORAGE_BLOCK,"statusBuffer");
GLint bufferAlignment;
GLenum t = GL_BUFFER_DATA_SIZE;
glGetProgramResourceiv(ComputeShader,GL_SHADER_STORAGE_BLOCK,stateBufferIndex,1,&t,1,nullptr,&bufferAlignment);
GLuint stateStructIndex = glGetProgramResourceIndex(ComputeShader,GL_BUFFER_VARIABLE,"stateArray[0].phase");
GLint requiredStateBufferSizePerWorker;
t = GL_TOP_LEVEL_ARRAY_STRIDE;
glGetProgramResourceiv(ComputeShader,GL_BUFFER_VARIABLE,stateStructIndex,1,&t,1,nullptr,&requiredStateBufferSizePerWorker);
const uint32_t workersPerFrame = settings.globalWorkGroupSizeX*settings.globalWorkGroupSizeY*settings.globalWorkGroupSizeZ*settings.localWorkgroupSizeX*settings.localWorkgroupSizeY*settings.localWorkgroupSizeZ;
auto requiredStateMemory = ((requiredStateBufferSizePerWorker*workersPerFrame + bufferAlignment -1)/bufferAlignment) * bufferAlignment;
GLuint stateBuffer;
glGenBuffers(1,&stateBuffer);
glBindBuffer(GL_SHADER_STORAGE_BUFFER,stateBuffer);
glBufferData(GL_SHADER_STORAGE_BUFFER, (requiredStateMemory),nullptr,GL_DYNAMIC_COPY);
glClearBufferData(GL_SHADER_STORAGE_BUFFER,GL_R8,GL_RED,GL_UNSIGNED_INT,nullptr);
glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 5, stateBuffer);
glUseProgram(ComputeShader);
GLint orbitLengthUniformHandle = glGetUniformLocation(ComputeShader, "orbitLength");
GLint totalIterationsUniformHandle = glGetUniformLocation(ComputeShader, "totalIterations");
GLint widthUniformComputeHandle = glGetUniformLocation(ComputeShader, "width");
GLint heightUniformComputeHandle = glGetUniformLocation(ComputeShader, "height");
GLint iterationsPerDispatchHandle = glGetUniformLocation(ComputeShader, "iterationsPerDispatch");
glUniform4ui(orbitLengthUniformHandle,settings.orbitLengthRed,settings.orbitLengthGreen,settings.orbitLengthBlue,settings.orbitLengthSkip);
glUniform1ui(widthUniformComputeHandle, settings.imageWidth);
glUniform1ui(heightUniformComputeHandle, bufferHeight);
const uint32_t maxOrbitlength = std::max(std::max(settings.orbitLengthBlue,settings.orbitLengthGreen),settings.orbitLengthRed);
glUniform1ui(totalIterationsUniformHandle, maxOrbitlength);
glUseProgram(VertexAndFragmentShaders);
GLint widthUniformFragmentHandle = glGetUniformLocation(VertexAndFragmentShaders, "width");
GLint heightUniformFragmentHandle = glGetUniformLocation(VertexAndFragmentShaders, "height");
glUniform1ui(widthUniformFragmentHandle, settings.imageWidth);
glUniform1ui(heightUniformFragmentHandle, bufferHeight);
glClearColor(0.0f, 0.0f, 0.4f, 0.0f);
uint32_t iterationsPerFrame = 1;
Helpers::PIDController<float, std::chrono::high_resolution_clock::time_point::rep> pid{0.0f,0.0f,1e-4f};
const uint32_t targetFrameDuration{1000000/settings.targetFrameRate};
uint64_t totalIterationCount{0};
uint64_t lastMessage{0};
/* Loop until the user closes the window */
while (!glfwWindowShouldClose(window))
{
auto frameStart{std::chrono::high_resolution_clock::now()};
totalIterationCount += iterationsPerFrame;
//let the compute shader do something
glUseProgram(ComputeShader);
glUniform1ui(iterationsPerDispatchHandle, iterationsPerFrame);
glDispatchCompute(settings.globalWorkGroupSizeX, settings.globalWorkGroupSizeY, settings.globalWorkGroupSizeZ);
//before reading the values in the ssbo, we need a memory barrier:
glMemoryBarrier(GL_SHADER_STORAGE_BARRIER_BIT); //I hope this is the correct (and only required) bit
/* Render here */
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glUseProgram(VertexAndFragmentShaders);
glEnableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);
glVertexAttribPointer(
0, // attribute 0. No particular reason for 0, but must match the layout in the shader.
3, // size
GL_FLOAT, // type
GL_FALSE, // normalized?
0, // stride
(void*)0 // array buffer offset
);
// Draw the triangle strip!
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4); // Triangle strip with 4 vertices -> quad.
glDisableVertexAttribArray(0);
/* Swap front and back buffers */
glfwSwapBuffers(window);
/* Poll for and process events */
glfwPollEvents();
auto frameStop{std::chrono::high_resolution_clock::now()};
const auto dur{std::chrono::duration_cast<std::chrono::microseconds>(frameStop-frameStart)};
auto frameDuration{dur.count()};
if(frameDuration > 0)
{
const auto error{targetFrameDuration - frameDuration};
const auto pidOutput{pid.Update(1,float(error))};
iterationsPerFrame = std::max(1,static_cast<int>(pidOutput));
//std::cout << iterationsPerFrame << " " << pidOutput << std::endl;
}
if(settings.printDebugOutput != 0 && totalIterationCount/maxOrbitlength > lastMessage)
{
lastMessage = totalIterationCount/maxOrbitlength;
const auto ctime = std::chrono::system_clock::to_time_t(std::chrono::system_clock::now());
std::cout << "Iteration count next frame: " << iterationsPerFrame << std::endl;
std::cout << std::put_time(std::localtime(&ctime),"%X") << ": Iteration count per worker higher than: " << lastMessage*maxOrbitlength << std::endl;
std::cout << std::put_time(std::localtime(&ctime),"%X") << ": Total iteration count higher than: " << lastMessage*maxOrbitlength*workersPerFrame << std::endl;
}
}
//settings.pngFilename = "Don'tForgetToRemoveThisLine.png";
if(!settings.pngFilename.empty())
{
glMemoryBarrier(GL_ALL_BARRIER_BITS);
std::vector<uint32_t> readBackBuffer(pixelCount*3);
glBindBuffer(GL_SHADER_STORAGE_BUFFER, drawBuffer);
glGetBufferSubData(GL_SHADER_STORAGE_BUFFER,0,4 *3* pixelCount,readBackBuffer.data());
Helpers::WriteOutputPNG(settings.pngFilename,readBackBuffer,settings.imageWidth,bufferHeight, settings.pngGamma, settings.pngColorScale);
}
//a bit of cleanup
glDeleteBuffers(1,&vertexbuffer);
glDeleteBuffers(1,&drawBuffer);
glDeleteBuffers(1,&stateBuffer);
glfwTerminate();
return 0;
}
|