-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
263 lines (201 loc) · 6.59 KB
/
main.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
#include <GL/glew.h>
#include <GLFW/glfw3.h>
#include <stdlib.h>
#include <stdio.h>
#include <iostream>
#include <thread>
#include <mutex>
#include <vector>
#define STB_IMAGE_IMPLEMENTATION
#include "stb_image.h"
#define print(x) std::cout << #x << std::flush
#define Width 640
#define Height 480
GLuint tex = 0;
void test_shader(GLuint shader) {
GLint result;
glGetShaderiv(shader, GL_COMPILE_STATUS, &result);
if(result == GL_FALSE) {
int length;
glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &length);
char* message = (char*) alloca(length * sizeof(char));
glGetShaderInfoLog(shader, length, &length, message);
std::cout
<< "\nWARNING: Failed to compile \n"
<< message
<< std::endl;
}
}
void window_setup(GLFWwindow *window) {
glfwMakeContextCurrent(window);
glfwSwapInterval(1);
glewExperimental = GL_TRUE;
if (glewInit() != GLEW_OK) {
glfwTerminate();
return;
}
}
void draw_setup() {
int w, h, channels;
stbi_set_flip_vertically_on_load(true);
unsigned char *data = stbi_load("test.jpg", &w, &h, &channels, 0);
GLuint tex_output;
glGenTextures(1, &tex_output);
tex = tex_output;
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, tex_output);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_NONE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_NONE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA32F, w, h, 0, GL_RGB, GL_UNSIGNED_BYTE, data);
glBindImageTexture(0, tex_output, 0, GL_FALSE, 0, GL_WRITE_ONLY, GL_RGBA32F);
static const float vertex_data[] = {
-1.0f, -1.0f, 0.0f, 0.0f, 0.0f, // lb
1.0f, -1.0f, 0.0f, 1.0f, 0.0f, // rb
1.0f, 1.0f, 0.0f, 1.0f, 1.0f, // rt
-1.0f, 1.0f, 0.0f, 0.0f, 1.0f // lt
};
unsigned int indices[] = {
0, 1, 2,
2, 3, 0};
const char* vertex_shader_code =
"#version 460\n"
"layout(location = 0) in vec3 pos;\n"
"layout(location = 1) in vec2 in_tex_coord;\n"
"out vec2 tex_coord;\n"
"void main()\n"
"{\n"
" tex_coord = in_tex_coord;\n"
" gl_Position.xyz = pos;\n"
" gl_Position.w = 1.0;\n"
"}\n";
const char* fragment_shader_code =
"#version 460\n"
"in vec2 tex_coord;\n"
"out vec4 color;\n"
"uniform int w;\n"
"uniform int h;\n"
"uniform sampler2D out_tex;\n"
"layout(rgba32f, binding = 0) uniform image2D img_output;\n"
"void main()\n"
"{\n"
" color = texture(out_tex, tex_coord);\n"
"}\n";
GLuint vao;
glGenVertexArrays(1, &vao);
glBindVertexArray(vao);
GLuint vbo;
glGenBuffers(1, &vbo);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glBufferData(GL_ARRAY_BUFFER, 4 * 5 * sizeof(float), vertex_data, GL_STATIC_DRAW);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(float) * 5, 0);
glEnableVertexAttribArray(1);
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, sizeof(float) * 5, (const void *)(3 * sizeof(float)));
GLuint ibo;
glGenBuffers(1, &ibo);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, 6 * sizeof(unsigned int), indices, GL_STATIC_DRAW);
GLuint vertex_shader = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vertex_shader, 1, &vertex_shader_code, nullptr);
glCompileShader(vertex_shader);
GLuint fragment_shader = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(fragment_shader, 1, &fragment_shader_code, nullptr);
glCompileShader(fragment_shader);
test_shader(fragment_shader);
test_shader(vertex_shader);
GLuint program = glCreateProgram();
glAttachShader(program, vertex_shader);
glAttachShader(program, fragment_shader);
glLinkProgram(program);
glValidateProgram(program);
glUseProgram(program);
GLint location_w = glGetUniformLocation(program, "w");
glUniform1i(location_w, Width);
GLint location_h = glGetUniformLocation(program, "h");
glUniform1i(location_h, Height);
}
void compute_setup() {
const char *cs_shader_code =
"#version 460\n"
"layout(local_size_x = 1, local_size_y = 1) in;\n"
"layout(rgba32f, binding = 0) uniform image2D img_output;\n"
"uniform int w;\n"
"uniform int h;\n"
"void main()\n"
"{\n"
" ivec2 pos = ivec2(gl_GlobalInvocationID.xy);\n"
" ivec2 new_pos = pos - ivec2(1,0);\n"
" if(new_pos.x < 0) new_pos.x = w - 1;\n"
" vec4 pixel = imageLoad(img_output, new_pos);\n"
" imageStore(img_output, pos, pixel);\n"
"}\n";
while(!tex) {
}
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, tex);
glBindImageTexture(0, tex, 0, GL_FALSE, 0, GL_WRITE_ONLY, GL_RGBA32F);
GLuint cs_shader = glCreateShader(GL_COMPUTE_SHADER);
glShaderSource(cs_shader, 1, &cs_shader_code, NULL);
glCompileShader(cs_shader);
test_shader(cs_shader);
GLuint program = glCreateProgram();
glAttachShader(program, cs_shader);
glLinkProgram(program);
glUseProgram(program);
GLint location_w = glGetUniformLocation(program, "w");
glUniform1i(location_w, Width);
GLint location_h = glGetUniformLocation(program, "h");
glUniform1i(location_h, Height);
}
void start_draw_thread(GLFWwindow *window, bool *finished) {
window_setup(window);
draw_setup();
while (!glfwWindowShouldClose(window) && !*finished)
{
glClear(GL_COLOR_BUFFER_BIT);
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, nullptr);
glfwSwapBuffers(window);
}
*finished = true;
}
void start_compute_thread(GLFWwindow *window, bool *finished) {
window_setup(window);
compute_setup();
GLuint own_tex = 0;
while (!glfwWindowShouldClose(window) && !*finished)
{
glDispatchCompute((GLuint) Width, (GLuint) Height, 1);
glfwSwapBuffers(window);
}
*finished = true;
}
int main(void)
{
if (!glfwInit())
exit(EXIT_FAILURE);
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 5);
GLFWwindow *draw_window = glfwCreateWindow(Width, Height, "Draw", nullptr, nullptr);
glfwWindowHint(GLFW_VISIBLE, GLFW_FALSE);
GLFWwindow *compute_window = glfwCreateWindow(Width, Height, "Compute", nullptr, draw_window);
if (!draw_window || !compute_window)
{
glfwTerminate();
exit(EXIT_FAILURE);
}
bool draw_finished = false, compute_finished = false;
std::thread draw_thread(start_draw_thread, draw_window, &draw_finished);
std::thread compute_thread(start_compute_thread, compute_window, &compute_finished);
while(!draw_finished && !compute_finished) {
glfwPollEvents();
}
draw_finished = true;
compute_finished = true;
glfwDestroyWindow(compute_window);
glfwDestroyWindow(draw_window);
compute_thread.join();
draw_thread.join();
glfwTerminate();
}