aboutsummaryrefslogtreecommitdiff
path: root/BuddhaTest/src/Helpers.cpp
blob: 8bd9e4b382c2f656037a6e13048fbedde8cf1ed2 (plain) (blame)
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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
#include "Helpers.h"
#include <string>
#include <fstream>
#include <sstream>
#include <iostream>
#include <vector>
#include <png.h>
#include <cmath>
#include <unordered_map>
#include <string>

namespace Helpers
{
    GLuint LoadShaders(const std::string& vertex_file_path, const std::string& fragment_file_path) {

		// Create the shaders
		GLuint VertexShaderID = glCreateShader(GL_VERTEX_SHADER);
		GLuint FragmentShaderID = glCreateShader(GL_FRAGMENT_SHADER);

		// Read the Vertex Shader code from the file
		std::string VertexShaderCode;
		std::ifstream VertexShaderStream(vertex_file_path, std::ios::in);
		if (VertexShaderStream.is_open()) {
			std::stringstream sstr;
			sstr << VertexShaderStream.rdbuf();
			VertexShaderCode = sstr.str();
			VertexShaderStream.close();
		}
		else {
            std::cerr << "Failed to load vertex shader file from " << vertex_file_path << ". Is this installed correctly?" << std::endl;
			return 0;
		}

		// Read the Fragment Shader code from the file
		std::string FragmentShaderCode;
		std::ifstream FragmentShaderStream(fragment_file_path, std::ios::in);
		if (FragmentShaderStream.is_open()) {
			std::stringstream sstr;
			sstr << FragmentShaderStream.rdbuf();
			FragmentShaderCode = sstr.str();
			FragmentShaderStream.close();
		}
        else
        {
            std::cerr << "Failed to load fragment shader file from " << fragment_file_path << ". Is this installed correctly?" << std::endl;
            return 0;
        }

		GLint Result = GL_FALSE;
		int InfoLogLength;

		// Compile Vertex Shader
        //std::cout << "Compiling shader : " << vertex_file_path << std::endl;
		char const * VertexSourcePointer = VertexShaderCode.c_str();
		glShaderSource(VertexShaderID, 1, &VertexSourcePointer, NULL);
		glCompileShader(VertexShaderID);

		// Check Vertex Shader
		glGetShaderiv(VertexShaderID, GL_COMPILE_STATUS, &Result);
		glGetShaderiv(VertexShaderID, GL_INFO_LOG_LENGTH, &InfoLogLength);
		if (InfoLogLength > 0) {
			std::vector<char> VertexShaderErrorMessage(InfoLogLength + 1);
			glGetShaderInfoLog(VertexShaderID, InfoLogLength, NULL, &VertexShaderErrorMessage[0]);
			printf("%s\n", &VertexShaderErrorMessage[0]);
		}


		// Compile Fragment Shader
        //std::cout << "Compiling shader : " << fragment_file_path << std::endl;
		char const * FragmentSourcePointer = FragmentShaderCode.c_str();
		glShaderSource(FragmentShaderID, 1, &FragmentSourcePointer, NULL);
		glCompileShader(FragmentShaderID);

		// Check Fragment Shader
		glGetShaderiv(FragmentShaderID, GL_COMPILE_STATUS, &Result);
		glGetShaderiv(FragmentShaderID, GL_INFO_LOG_LENGTH, &InfoLogLength);
		if (InfoLogLength > 0) {
			std::vector<char> FragmentShaderErrorMessage(InfoLogLength + 1);
			glGetShaderInfoLog(FragmentShaderID, InfoLogLength, NULL, &FragmentShaderErrorMessage[0]);
			printf("%s\n", &FragmentShaderErrorMessage[0]);
		}

		// Link the program
        //printf("Linking program\n");
		GLuint ProgramID = glCreateProgram();
		glAttachShader(ProgramID, VertexShaderID);
		glAttachShader(ProgramID, FragmentShaderID);
		glLinkProgram(ProgramID);

		// Check the program
		glGetProgramiv(ProgramID, GL_LINK_STATUS, &Result);
		glGetProgramiv(ProgramID, GL_INFO_LOG_LENGTH, &InfoLogLength);
		if (InfoLogLength > 0) {
			std::vector<char> ProgramErrorMessage(InfoLogLength + 1);
			glGetProgramInfoLog(ProgramID, InfoLogLength, NULL, &ProgramErrorMessage[0]);
			printf("%s\n", &ProgramErrorMessage[0]);
		}

		glDetachShader(ProgramID, VertexShaderID);
		glDetachShader(ProgramID, FragmentShaderID);

		glDeleteShader(VertexShaderID);
		glDeleteShader(FragmentShaderID);

		return ProgramID;
	}

    GLuint LoadComputeShader(const std::string& compute_file_path, unsigned int localSizeX, unsigned int localSizeY, unsigned int localSizeZ)
	{
		GLuint ComputeShaderID = glCreateShader(GL_COMPUTE_SHADER);
		// Read the compute shader
		std::string ComputeShaderCode;
		{
			std::ifstream ComputeShaderCodeStream(compute_file_path, std::ios::in);
			if (ComputeShaderCodeStream.is_open()) {
				std::stringstream sstr;
                sstr << "#version 430" <<
                        std::endl <<
                        "layout (local_size_x = " << localSizeX <<
                        ", local_size_y = " << localSizeY <<
                        ", local_size_z = " << localSizeZ << ") in;" << std::endl;
				sstr << ComputeShaderCodeStream.rdbuf();
				ComputeShaderCode = sstr.str();
				ComputeShaderCodeStream.close();
			}
            else
            {
                std::cerr << "Failed to load compute shader file from " << compute_file_path << ". Is this installed correctly?" << std::endl;
                return 0;
            }
		}

		GLint Result = GL_FALSE;
		int InfoLogLength;

		{
			// Compile Compute Shader
            //std::cout << "Compiling shader : " << compute_file_path << std::endl;
			char const * ComputeSourcePointer = ComputeShaderCode.c_str();
			glShaderSource(ComputeShaderID, 1, &ComputeSourcePointer, NULL);
			glCompileShader(ComputeShaderID);

			// Check Compute Shader
			glGetShaderiv(ComputeShaderID, GL_COMPILE_STATUS, &Result);
			glGetShaderiv(ComputeShaderID, GL_INFO_LOG_LENGTH, &InfoLogLength);
			if (InfoLogLength > 0) {
				std::vector<char> ComputeShaderErrorMessage(InfoLogLength + 1);
				glGetShaderInfoLog(ComputeShaderID, InfoLogLength, NULL, &ComputeShaderErrorMessage[0]);
				printf("%s\n", &ComputeShaderErrorMessage[0]);
			}
		}
		GLuint ProgramID = glCreateProgram();
		glAttachShader(ProgramID, ComputeShaderID);
		glLinkProgram(ProgramID);

		// Check the program
		glGetProgramiv(ProgramID, GL_LINK_STATUS, &Result);
		glGetProgramiv(ProgramID, GL_INFO_LOG_LENGTH, &InfoLogLength);
		if (InfoLogLength > 0) {
			std::vector<char> ProgramErrorMessage(InfoLogLength + 1);
			glGetProgramInfoLog(ProgramID, InfoLogLength, NULL, &ProgramErrorMessage[0]);
			printf("%s\n", &ProgramErrorMessage[0]);
		}
		glDetachShader(ProgramID, ComputeShaderID);
		glDeleteShader(ComputeShaderID);
		return ProgramID;
	}

    void WriteOutputPNG(const std::string &path, const std::vector<uint32_t>& data, unsigned int width, unsigned int bufferHeight, double gamma, double colorScale)
    {
        std::vector<png_byte> pngData(3*width*2*bufferHeight);
        std::vector<png_byte *> rows{2*bufferHeight};
        for(int i = 0; i < 2*bufferHeight ; ++i)
        {
            rows[i] = pngData.data()+3*width*i;
        }

        uint32_t maxValue{UINT32_C(1)};
        for(unsigned int i = 0; i < data.size();++i)
        {
            maxValue = std::max(maxValue,data[i]);
        }
        for(unsigned int i = 0; i < data.size();++i)
        {
            if(fabs(gamma - 1.0) > 0.0001 || fabs(colorScale - 1.0) > 0.0001)
            {
                pngData[data.size() + i] = 255.0 * pow(std::min(1.0,colorScale*static_cast<double>(data[i])/static_cast<double>(maxValue)),gamma);
            }
            else
            {
                pngData[data.size() + i] = (255*data[i] + (maxValue/2))/maxValue;
            }
        }
        for(int i = 0; i < bufferHeight;++i)
        {
            for(int j = 0; j < width*3;++j)
            {
                rows[i][j] =rows[2*bufferHeight-i-1][j];
            }
        }

        ScopedCFileDescriptor fd(path.c_str(), "wb");
        if(!fd.IsValid())
        {
            std::cerr << "Failed to open image.png for writing." << std::endl;
            return;
        }
        png_structp png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, nullptr, nullptr, nullptr);
        if(!png_ptr)
        {
            return;
        }
        png_infop info_ptr = png_create_info_struct(png_ptr);
        if(!info_ptr)
        {
            png_destroy_write_struct(&png_ptr, (png_infopp)NULL);
            return;
        }
        if(setjmp(png_jmpbuf(png_ptr)))
        {
            png_destroy_write_struct(&png_ptr, &info_ptr);
            return;
        }
        png_init_io(png_ptr, fd.Get());
        png_set_IHDR(png_ptr, info_ptr, width, 2*bufferHeight, 8, PNG_COLOR_TYPE_RGB, PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);

        png_write_info(png_ptr, info_ptr);
        //header written.

        png_write_image(png_ptr, rows.data());

        png_write_end(png_ptr, info_ptr);
        png_destroy_write_struct(&png_ptr, &info_ptr);
    }

    ScopedCFileDescriptor::ScopedCFileDescriptor(const char *path, const char *mode)
    {
        Descriptor = fopen(path,mode);
    }

    ScopedCFileDescriptor::~ScopedCFileDescriptor()
    {
        if(IsValid())
            fclose(Descriptor);
    }

    FILE * ScopedCFileDescriptor::Get() const
    {
        return Descriptor;
    }

    bool ScopedCFileDescriptor::IsValid() const
    {
        return Descriptor != nullptr;
    }

    bool RenderSettings::CheckValidity()
    {
        if(imageHeight%2 != 0)
        {
            std::cerr << "Image height has to be an even number." << std::endl;
            return false;
        }
        const unsigned int bufferHeight = imageHeight/2;
        const unsigned int pixelCount{(imageWidth * imageHeight/2)*3}; //*3 -> RGB
        int maxSSBOSize;
        glGetIntegerv(GL_MAX_SHADER_STORAGE_BLOCK_SIZE,&maxSSBOSize);
        if(pixelCount * 4 > maxSSBOSize)
        {
            std::cerr << "Requested buffer size larger than maximum allowed by graphics driver. Max pixel number: " << maxSSBOSize/6 << std::endl;
            return false;
        }
        int WorkGroupSizeLimitX, WorkGroupSizeLimitY, WorkGroupSizeLimitZ;
        glGetIntegeri_v(GL_MAX_COMPUTE_WORK_GROUP_COUNT,0,&WorkGroupSizeLimitX);
        glGetIntegeri_v(GL_MAX_COMPUTE_WORK_GROUP_COUNT,1,&WorkGroupSizeLimitY);
        glGetIntegeri_v(GL_MAX_COMPUTE_WORK_GROUP_COUNT,2,&WorkGroupSizeLimitZ);
        if(globalWorkGroupSizeX > WorkGroupSizeLimitX ||
                globalWorkGroupSizeY > WorkGroupSizeLimitY ||
                globalWorkGroupSizeZ > WorkGroupSizeLimitZ)
        {
            std::cerr << "Requested global work group size exceeds maximum dimension. Limits: " << WorkGroupSizeLimitX << ", " << WorkGroupSizeLimitY << ", " << WorkGroupSizeLimitZ << std::endl;
            return false;
        }
        glGetIntegeri_v(GL_MAX_COMPUTE_WORK_GROUP_SIZE,0,&WorkGroupSizeLimitX);
        glGetIntegeri_v(GL_MAX_COMPUTE_WORK_GROUP_SIZE,1,&WorkGroupSizeLimitY);
        glGetIntegeri_v(GL_MAX_COMPUTE_WORK_GROUP_SIZE,2,&WorkGroupSizeLimitZ);
        if(localWorkgroupSizeX > WorkGroupSizeLimitX ||
                localWorkgroupSizeY > WorkGroupSizeLimitY ||
                localWorkgroupSizeZ > WorkGroupSizeLimitZ)
        {
            std::cerr << "Requested local work group size exceeds maximum dimension. Limits: " << WorkGroupSizeLimitX << ", " << WorkGroupSizeLimitY << ", " << WorkGroupSizeLimitZ << std::endl;
            return false;
        }
        int maxInvocations;
        glGetIntegerv(GL_MAX_COMPUTE_WORK_GROUP_INVOCATIONS,&maxInvocations);
        if(localWorkgroupSizeX*localWorkgroupSizeY*localWorkgroupSizeZ > maxInvocations)
        {
            std::cerr << "Requested local work group size exceeds maximum total count (Product of x*y*z dimensions). Limit: " << maxInvocations << std::endl;
            return false;
        }
        return true;
    }

    bool RenderSettings::ParseCommandLine(int argc, char *argv[])
    {
        struct SettingsPointer
        {
        public:
            SettingsPointer(unsigned int * ptr) : intPtr(ptr), dblPtr(nullptr), stringPtr(nullptr) {}
            SettingsPointer(double * ptr) : intPtr(nullptr), dblPtr(ptr), stringPtr(nullptr) {}
            SettingsPointer(std::string * ptr) : intPtr(nullptr), dblPtr(nullptr), stringPtr(ptr) {}
            unsigned int * GetIntPtr() const { return intPtr; }
            double * GetDblPtr() const { return dblPtr;}
            std::string * GetStringPtr() const { return stringPtr;}
        private:
            unsigned int * intPtr = nullptr;
            double * dblPtr = nullptr;
            std::string * stringPtr = nullptr;
        };
        std::unordered_map<std::string, SettingsPointer> commandMap{
            {"--imageWidth",&imageWidth},
            {"--imageHeight",&imageHeight},
            {"--windowWidth",&windowWidth},
            {"--windowHeight",&windowHeight},
            {"--orbitLengthRed",&orbitLengthRed},
            {"--orbitLengthGreen",&orbitLengthGreen},
            {"--orbitLengthBlue",&orbitLengthBlue},
            {"--localWorkgroupSizeX", &localWorkgroupSizeX},
            {"--localWorkgroupSizeY", &localWorkgroupSizeY},
            {"--localWorkgroupSizeZ", &localWorkgroupSizeZ},
            {"--globalWorkgroupSizeX", &globalWorkGroupSizeX},
            {"--globalWorkgroupSizeY", &globalWorkGroupSizeY},
            {"--globalWorkgroupSizeZ", &globalWorkGroupSizeZ},
            {"--imageGamma",&pngGamma},
            {"--imageColorScale",&pngColorScale},
            {"--output", &pngFilename}
        };

        for(int i=1; i < argc;++i)
        {
            std::string argAsString(argv[i]);
            if(argAsString == "--help") //help is a special case.
            {
                std::cout << "Draws a buddhabrot and iterates until the user closes the window. If a --output filename is given, a png file will afterwards be written there." <<std::endl <<
                             "Supported options are:" << std::endl << std::endl <<
                             "--output [path] : File to write output to. Empty by default, meaning no output is written." << std::endl <<
                             "--imageWidth [integer] : Width of the to be written image. 1024 by default. If no --output is given, this still detrmines the buffer size for rendering." << std::endl <<
                             "--imageHeight [integer] : Height of the to be written image. 576 by default. If no --output is given, this still detrmines the buffer size for rendering." << std::endl <<
                             "--imageGamma [float] : Gamma to use when writing the image. 1.0 by default. Ignored if no --output is given." << std::endl <<
                             "--imageColorScale [float] : Image brightness is scaled by the brightest pixel. The result is multiplied by this value. 2.0 by default, as 1.0 leaves very little dynamic range." << std::endl <<
                             "--windowWidth [integer] : Width of the preview window. 1024 by default." << std::endl <<
                             "--windowHeight [integer] : Height of the preview window. 576 by default." << std::endl <<
                             "--orbitLengthRed [integer] : Maximum number of iterations for escaping orbits to color red. 10 by default." << std::endl <<
                             "--orbitLengthGreen [integer] : Maximum number of iterations for escaping orbits to color green. 100 by default." << std::endl <<
                             "--orbitLengthBlue [integer] : Maximum number of iterations for escaping orbits to color blue. 1000 by default." << std::endl <<
                             "--localWorkgroupSizeX [integer] : How \"parallel\" the computation should be. Maximum possible value depends on GPU and drivers. The default is 1024. Values up to 1024 are guaranteed to work." << std::endl <<
                             "--localWorkgroupSizeY [integer] : How \"parallel\" the computation should be. Maximum possible value depends on GPU and drivers. The default is 1. Values up to 1024 are guaranteed to work." << std::endl <<
                             "--localWorkgroupSizeZ [integer] : How \"parallel\" the computation should be. Maximum possible value depends on GPU and drivers. The default of 1. Values up to 1024 are guaranteed to work." << std::endl <<
                             "\tNOTE: There's also a limit on the product of the three local workgroup sizes, for which a number smaller or equal to 1024 is guaranteed to work. Higher numbers might work and run faster. Feel free to experiment." << std::endl <<
                             "--globalWorkgroupSizeX [integer] : How often the local work group should be invoked per frame. Values up to 65535 are guaranteed to work. Default is 1024." << std::endl <<
                             "--globalWorkgroupSizeY [integer] : How often the local work group should be invoked per frame. Values up to 65535 are guaranteed to work. Default is 1." << std::endl <<
                             "--globalWorkgroupSizeZ [integer] : How often the local work group should be invoked per frame. Values up to 65535 are guaranteed to work. Default is 1." << std::endl;
                return false;
            }
        }
        //apart from --help all parameters consist of 2 entries.
        if(argc%2 != 1)
        {
            std::cerr << "Invalid number of arguments. See --help for usage." << std::endl;
            return false;
        }
        for(int i=1 ; i < argc; i += 2)
        {
            std::string argAsString(argv[i]);
            std::string valueAsString(argv[i+1]);
            auto setting = commandMap.find(argAsString);
            if(setting == commandMap.end())
            {
                std::cerr << "Unknown option: " << argAsString << std::endl;
                return false;
            }
            const SettingsPointer& ptr = setting->second;
            if(auto intProp = ptr.GetIntPtr())
            {
                *intProp = std::stoi(valueAsString);
            }
            else if(auto dblProp = ptr.GetDblPtr())
            {
                *dblProp = std::stod(valueAsString);
            }
            else if(auto strPtr = ptr.GetStringPtr())
            {
                *strPtr = valueAsString;
            }
            else
            {
                std::cerr << "Something went horribly wrong with command line parsing. This is a bug." << std::endl;
                return false;
            }
        }

        return true;
    }

    bool DoesFileExist(const std::string &path)
    {
        std::ifstream f(path);
            return f.good();
    }

}