-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.cpp
347 lines (264 loc) · 7.99 KB
/
test.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
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
#include <iostream>
#include<glad/glad.h>
#include <gl/GL.h>
#include <cmath>
#include <GLFW/glfw3.h>
#include "helper.h"
void runWinProc(GLFWwindow* window) {
while (!glfwWindowShouldClose(window)) {
glfwPollEvents();
}
}
void CreateOpenGLContext() {
glfwInit();
// Tell GLFW what version of OpenGL we are using
// In this case we are using OpenGL 3.3
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 6);
// Tell GLFW we are using the CORE profile
// So that means we only have the modern functions
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
glfwWindowHint(GLFW_VISIBLE, GLFW_FALSE);
GLFWwindow* window = glfwCreateWindow(1, 1, "YoutubeOpenGL", NULL, NULL);
// Error check if the window fails to create
if (window == NULL)
{
std::cout << "Failed to create GLFW window" << std::endl;
glfwTerminate();
return;
}
// Introduce the window into the current context
glfwMakeContextCurrent(window);
if (gladLoadGL() == GL_FALSE) {
std::cout << "Can't load GL " << std::endl;
return;
}
}
void DestroyOpenGLContext() {
glfwTerminate();
}
struct shader_exception {
const GLuint ID;
const std::string what;
shader_exception(GLuint ID, std::string what):ID(ID),what(what) {
}
};
struct ComputeShader {
GLuint ID;
ComputeShader(std::string source) {
ID=glCreateShader(GL_COMPUTE_SHADER);
const char* shaderSource = source.c_str();
glShaderSource(ID, 1, &shaderSource, NULL);
glCompileShader(ID);
if (EnCounterShaderError(ID)) {
ID = 0;
return;
}
}
bool IsValidShader() {
return ID != 0;
}
};
/*
* Barrier for all the things below except barrier
* memoryBarrier
* memoryBarrierAtomicCounter
* memoryBarrierImage
* memoryBarrierBuffer
* memoryBarrierShared
*
* Create visibility for all unsynchronized memory operations
* groupMemoryBarrier
* Barrier for exclusion synchronization of invocations
* barrier
*/
template<typename T,size_t NELE=1>
struct LayoutBuffer {
unsigned int index;
unsigned int buffer;
const size_t size= sizeof(T)*NELE;
~LayoutBuffer() {
glDeleteBuffers(1, &buffer);
}
};
template<typename T,size_t NELE=1>
struct UniformBuffer {
void* clientBuffer;
unsigned int location;
const size_t size = sizeof(T) * NELE;
};
struct GPUProgram {
GLuint ID;
bool isSafeRunning;
bool isRunning;
std::vector<ComputeShader> shaders;
GPUProgram() {
ID = glCreateProgram();
isSafeRunning = false;
isRunning = false;
}
~GPUProgram() {
Delete();
}
void Link() {
glLinkProgram(ID);
CheckForProgramError(ID);
}
int GetWorkGroupCount(int index) {
int wgcnt;
glGetIntegeri_v(GL_MAX_COMPUTE_WORK_GROUP_COUNT, index, &wgcnt);
return wgcnt;
}
int GetWorkGroupSize(int index) {
int wgsz;
glGetIntegeri_v(GL_MAX_COMPUTE_WORK_GROUP_SIZE, index, &wgsz);
return wgsz;
}
int GetWorkGroupInv() {
int wgi;
glGetIntegerv(GL_MAX_COMPUTE_WORK_GROUP_INVOCATIONS, &wgi);
return wgi;
}
void Run(unsigned int xGroup, unsigned int yGroup, unsigned int zGroup) {
std::cout << xGroup << "," << yGroup << "," << zGroup << std::endl;
isRunning = true;
glUseProgram(ID);
glDispatchCompute(xGroup, yGroup, zGroup);
glMemoryBarrier(GL_ALL_BARRIER_BITS);
isRunning = false;
}
void RunSafe() {
isSafeRunning = true;
glUseProgram(ID);
}
template<typename T,size_t NELE>
void GetUniformLocation(const char* unfiromName,UniformBuffer<T,NELE>& outBuffer,void* dataToUpload) {
if (shaders.empty())throw std::runtime_error("you haven't attached any shader");
if (!isSafeRunning)throw std::runtime_error("gpu program isn't running in safe mode");
GLuint location=glGetUniformLocation(ID, unfiromName);
}
void AttachShader(ComputeShader& shader) {
if (!shader.IsValidShader())throw shader_exception(shader.ID,"Invalid shader!");
shaders.push_back(shader);
glAttachShader(ID, shader.ID);
glDeleteShader(shader.ID);
}
template<typename T,size_t NELE=1>
static void CreateAndBindBuffer(int index, T* data,LayoutBuffer<T,NELE>& outBuffer) {
GLuint buffer;
glGenBuffers(1, &buffer);
glBindBuffer(GL_SHADER_STORAGE_BUFFER, buffer);
glBufferData(GL_SHADER_STORAGE_BUFFER, sizeof(T)*NELE, data, GL_STATIC_DRAW);
glBindBufferBase(GL_SHADER_STORAGE_BUFFER, index, buffer);
glBindBuffer(GL_SHADER_STORAGE_BUFFER, 0);
outBuffer.buffer = buffer;
outBuffer.index = index;
}
template<typename T,size_t NELE=1>
static void GetOutputBuffer(LayoutBuffer<T,NELE>& layout,T* outBuffer) {
glBindBuffer(GL_SHADER_STORAGE_BUFFER, layout.buffer);
glGetBufferSubData(GL_SHADER_STORAGE_BUFFER, 0, layout.size, outBuffer);
glBindBuffer(GL_SHADER_STORAGE_BUFFER, 0);
}
template<typename T,size_t NELE=1>
static void AssignDataToBuffer(LayoutBuffer<T, NELE>& layout, int offset, T* data,size_t customSize=sizeof(T)*NELE) {
glBindBuffer(GL_SHADER_STORAGE_BUFFER, layout.buffer);
glBufferSubData(GL_SHADER_STORAGE_BUFFER, offset, customSize, data);
glBindBuffer(GL_SHADER_STORAGE_BUFFER, 0);
}
private:
void Delete() {
for (ComputeShader& shader : shaders) {
glDetachShader(ID,shader.ID);
}
shaders.clear();
glDeleteProgram(ID);
}
};
void test1() {
GPUProgram program;
const char* source = "#version 430\n\
layout(local_size_x = 1, local_size_y = 1, local_size_z = 1) in;\n\
layout(binding = 0) buffer InData { float a; float b; };\n\
layout(binding = 1) buffer OutData { float c; };\n\
void main() {\n\
c = a + b;\n\
}";
float a = 1.0f;
float b = 2.0f;
float c = 0.0f;
LayoutBuffer<float, 2> inputLayoutBuffer;
LayoutBuffer<float> outputLayoutBuffer;
GPUProgram::CreateAndBindBuffer(0, (float*)nullptr, inputLayoutBuffer);
GPUProgram::CreateAndBindBuffer(1, (float*)nullptr, outputLayoutBuffer);
GPUProgram::AssignDataToBuffer(inputLayoutBuffer, 0, &a, sizeof(float));
GPUProgram::AssignDataToBuffer(inputLayoutBuffer, sizeof(float), &b, sizeof(float));
ComputeShader shader(source);
program.AttachShader(shader);
program.Link();
program.Run(1, 1, 1);
GPUProgram::GetOutputBuffer(outputLayoutBuffer, &c);
std::cout << c << std::endl;
}
void test2() {
const char* computeShaderSource = R"(
#version 430
layout(local_size_x = 4, local_size_y = 4) in;
layout(std430, binding = 0) buffer BufferA {
float A[];
};
layout(std430, binding = 1) buffer BufferB {
float B[];
};
layout(std430, binding = 2) buffer BufferC {
float C[];
};
layout (std430, binding =3 ) buffer Information{
uint numColsA;
uint numRowsA;
uint numColsB;
};
void main() {
uint row = gl_GlobalInvocationID.x;
uint col = gl_GlobalInvocationID.y;
float sum = 0.0;
for (uint i = 0; i < numColsA; i++) {
sum += A[row * numColsA + i] * B[i * numColsB + col];
}
C[row * numColsB + col] = sum;
}
)";
GPUProgram program;
ComputeShader computeShader(computeShaderSource);
float a[] = {1,1,1,1,1,1,1,1, 1,1,1,1, 1,1,1,0};
float b[] = { 1,1,1,1,1,1,1,1, 1,1,1,1, 1,1,1,1 };
int colA=4;
int rowA = 4;
int colB=4;
LayoutBuffer<float, 16> matrixALayout;
LayoutBuffer<float, 16> matrixBLayout;
LayoutBuffer<float, 16> outputCLayout;
GPUProgram::CreateAndBindBuffer(0, a, matrixALayout);
GPUProgram::CreateAndBindBuffer(1, b, matrixBLayout);
GPUProgram::CreateAndBindBuffer(2, (float*)nullptr, outputCLayout);
LayoutBuffer<int, 3> informationLayout;
GPUProgram::CreateAndBindBuffer(3, (int*)nullptr, informationLayout);
GPUProgram::AssignDataToBuffer(informationLayout, 0, &colA, sizeof(int));
GPUProgram::AssignDataToBuffer(informationLayout, sizeof(float), &rowA, sizeof(int));
GPUProgram::AssignDataToBuffer(informationLayout, 2*sizeof(float), &colB, sizeof(int));
program.AttachShader(computeShader);
program.Link();
program.Run(4,4, 1);
float* result = new float[16];
GPUProgram::GetOutputBuffer(outputCLayout, result);
for (int i = 0; i < 16; i++) {
std::cout << result[i] << std::endl;
}
}
int main(const int argc,const char** argv){
CreateOpenGLContext();
test1();
test2();
DestroyOpenGLContext();
return 0;
}