]> git.sesse.net Git - nageru/blob - flow.cpp
Add the densification step, in theory now providing us with flow. But still too buggy...
[nageru] / flow.cpp
1 #define NO_SDL_GLEXT 1
2
3 #define WIDTH 1280
4 #define HEIGHT 720
5
6 #include <epoxy/gl.h>
7
8 #include <SDL2/SDL.h>
9 #include <SDL2/SDL_error.h>
10 #include <SDL2/SDL_events.h>
11 #include <SDL2/SDL_image.h>
12 #include <SDL2/SDL_keyboard.h>
13 #include <SDL2/SDL_mouse.h>
14 #include <SDL2/SDL_video.h>
15
16 #include <assert.h>
17 #include <stdio.h>
18
19 #include <algorithm>
20 #include <memory>
21
22 #define BUFFER_OFFSET(i) ((char *)nullptr + (i))
23
24 using namespace std;
25
26 // Operating point 3 (10 Hz on CPU, excluding preprocessing).
27 constexpr float patch_overlap_ratio = 0.75f;
28 constexpr unsigned coarsest_level = 0;
29 constexpr unsigned finest_level = 0;
30 constexpr unsigned patch_size_pixels = 12;
31
32 string read_file(const string &filename)
33 {
34         FILE *fp = fopen(filename.c_str(), "r");
35         if (fp == nullptr) {
36                 perror(filename.c_str());
37                 exit(1);
38         }
39
40         int ret = fseek(fp, 0, SEEK_END);
41         if (ret == -1) {
42                 perror("fseek(SEEK_END)");
43                 exit(1);
44         }
45
46         int size = ftell(fp);
47
48         ret = fseek(fp, 0, SEEK_SET);
49         if (ret == -1) {
50                 perror("fseek(SEEK_SET)");
51                 exit(1);
52         }
53
54         string str;
55         str.resize(size);
56         ret = fread(&str[0], size, 1, fp);
57         if (ret == -1) {
58                 perror("fread");
59                 exit(1);
60         }
61         if (ret == 0) {
62                 fprintf(stderr, "Short read when trying to read %d bytes from %s\n",
63                                 size, filename.c_str());
64                 exit(1);
65         }
66         fclose(fp);
67
68         return str;
69 }
70
71
72 GLuint compile_shader(const string &shader_src, GLenum type)
73 {
74         GLuint obj = glCreateShader(type);
75         const GLchar* source[] = { shader_src.data() };
76         const GLint length[] = { (GLint)shader_src.size() };
77         glShaderSource(obj, 1, source, length);
78         glCompileShader(obj);
79
80         GLchar info_log[4096];
81         GLsizei log_length = sizeof(info_log) - 1;
82         glGetShaderInfoLog(obj, log_length, &log_length, info_log);
83         info_log[log_length] = 0;
84         if (strlen(info_log) > 0) {
85                 fprintf(stderr, "Shader compile log: %s\n", info_log);
86         }
87
88         GLint status;
89         glGetShaderiv(obj, GL_COMPILE_STATUS, &status);
90         if (status == GL_FALSE) {
91                 // Add some line numbers to easier identify compile errors.
92                 string src_with_lines = "/*   1 */ ";
93                 size_t lineno = 1;
94                 for (char ch : shader_src) {
95                         src_with_lines.push_back(ch);
96                         if (ch == '\n') {
97                                 char buf[32];
98                                 snprintf(buf, sizeof(buf), "/* %3zu */ ", ++lineno);
99                                 src_with_lines += buf;
100                         }
101                 }
102
103                 fprintf(stderr, "Failed to compile shader:\n%s\n", src_with_lines.c_str());
104                 exit(1);
105         }
106
107         return obj;
108 }
109
110
111 GLuint load_texture(const char *filename, unsigned width, unsigned height)
112 {
113         FILE *fp = fopen(filename, "rb");
114         if (fp == nullptr) {
115                 perror(filename);
116                 exit(1);
117         }
118         unique_ptr<uint8_t[]> pix(new uint8_t[width * height]);
119         if (fread(pix.get(), width * height, 1, fp) != 1) {
120                 fprintf(stderr, "Short read from %s\n", filename);
121                 exit(1);
122         }
123         fclose(fp);
124
125         // Convert to bottom-left origin.
126         for (unsigned y = 0; y < height / 2; ++y) {
127                 unsigned y2 = height - 1 - y;
128                 swap_ranges(&pix[y * width], &pix[y * width + width], &pix[y2 * width]);
129         }
130
131         int levels = 1;
132         for (int w = width, h = height; w > 1 || h > 1; ) {
133                 w >>= 1;
134                 h >>= 1;
135                 ++levels;
136         }
137
138         GLuint tex;
139         glCreateTextures(GL_TEXTURE_2D, 1, &tex);
140         glTextureStorage2D(tex, levels, GL_R8, width, height);
141         glTextureSubImage2D(tex, 0, 0, 0, width, height, GL_RED, GL_UNSIGNED_BYTE, pix.get());
142         glGenerateTextureMipmap(tex);
143
144         return tex;
145 }
146
147 GLuint link_program(GLuint vs_obj, GLuint fs_obj)
148 {
149         GLuint program = glCreateProgram();
150         glAttachShader(program, vs_obj);
151         glAttachShader(program, fs_obj);
152         glLinkProgram(program);
153         GLint success;
154         glGetProgramiv(program, GL_LINK_STATUS, &success);
155         if (success == GL_FALSE) {
156                 GLchar error_log[1024] = {0};
157                 glGetProgramInfoLog(program, 1024, nullptr, error_log);
158                 fprintf(stderr, "Error linking program: %s\n", error_log);
159                 exit(1);
160         }
161         return program;
162 }
163
164 GLuint generate_vbo(GLint size, GLsizeiptr data_size, const GLvoid *data)
165 {
166         GLuint vbo;
167         glCreateBuffers(1, &vbo);
168         glBufferData(GL_ARRAY_BUFFER, data_size, data, GL_STATIC_DRAW);
169         glNamedBufferData(vbo, data_size, data, GL_STATIC_DRAW);
170         return vbo;
171 }
172
173 GLuint fill_vertex_attribute(GLuint vao, GLuint glsl_program_num, const string &attribute_name, GLint size, GLenum type, GLsizeiptr data_size, const GLvoid *data)
174 {
175         int attrib = glGetAttribLocation(glsl_program_num, attribute_name.c_str());
176         if (attrib == -1) {
177                 return -1;
178         }
179
180         GLuint vbo = generate_vbo(size, data_size, data);
181
182         glBindBuffer(GL_ARRAY_BUFFER, vbo);
183         glEnableVertexArrayAttrib(vao, attrib);
184         glVertexAttribPointer(attrib, size, type, GL_FALSE, 0, BUFFER_OFFSET(0));
185         glBindBuffer(GL_ARRAY_BUFFER, 0);
186
187         return vbo;
188 }
189
190 void bind_sampler(GLuint program, const char *uniform_name, GLuint texture_unit, GLuint tex, GLuint sampler)
191 {
192         GLint location = glGetUniformLocation(program, uniform_name);
193         if (location == -1) {
194                 return;
195         }
196
197         glBindTextureUnit(texture_unit, tex);
198         glBindSampler(texture_unit, sampler);
199         glProgramUniform1i(program, location, texture_unit);
200 }
201
202 int main(void)
203 {
204         if (SDL_Init(SDL_INIT_EVERYTHING) == -1) {
205                 fprintf(stderr, "SDL_Init failed: %s\n", SDL_GetError());
206                 exit(1);
207         }
208         SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE, 8);
209         SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 0);
210         SDL_GL_SetAttribute(SDL_GL_STENCIL_SIZE, 0);
211         SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
212
213         SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
214         SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 4);
215         SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 5);
216         // SDL_GL_SetAttribute(SDL_GL_CONTEXT_FLAGS, SDL_GL_CONTEXT_DEBUG_FLAG);
217         SDL_Window *window = SDL_CreateWindow("OpenGL window",
218                         SDL_WINDOWPOS_UNDEFINED,
219                         SDL_WINDOWPOS_UNDEFINED,
220                         64, 64,
221                         SDL_WINDOW_OPENGL);
222         SDL_GLContext context = SDL_GL_CreateContext(window);
223         assert(context != nullptr);
224
225         // Load pictures.
226         GLuint tex0 = load_texture("test1499.pgm", WIDTH, HEIGHT);
227         GLuint tex1 = load_texture("test1500.pgm", WIDTH, HEIGHT);
228
229         // Load shaders.
230         GLuint motion_vs_obj = compile_shader(read_file("motion_search.vert"), GL_VERTEX_SHADER);
231         GLuint motion_fs_obj = compile_shader(read_file("motion_search.frag"), GL_FRAGMENT_SHADER);
232         GLuint motion_search_program = link_program(motion_vs_obj, motion_fs_obj);
233
234         GLuint sobel_vs_obj = compile_shader(read_file("vs.vert"), GL_VERTEX_SHADER);
235         GLuint sobel_fs_obj = compile_shader(read_file("sobel.frag"), GL_FRAGMENT_SHADER);
236         GLuint sobel_program = link_program(sobel_vs_obj, sobel_fs_obj);
237
238         GLuint densify_vs_obj = compile_shader(read_file("densify.vert"), GL_VERTEX_SHADER);
239         GLuint densify_fs_obj = compile_shader(read_file("densify.frag"), GL_FRAGMENT_SHADER);
240         GLuint densify_program = link_program(densify_vs_obj, densify_fs_obj);
241
242         // Make some samplers.
243         GLuint nearest_sampler;
244         glCreateSamplers(1, &nearest_sampler);
245         glSamplerParameteri(nearest_sampler, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
246         glSamplerParameteri(nearest_sampler, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
247         glSamplerParameteri(nearest_sampler, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
248         glSamplerParameteri(nearest_sampler, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
249
250         GLuint linear_sampler;
251         glCreateSamplers(1, &linear_sampler);
252         glSamplerParameteri(linear_sampler, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
253         glSamplerParameteri(linear_sampler, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
254         glSamplerParameteri(linear_sampler, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
255         glSamplerParameteri(linear_sampler, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
256
257         GLuint mipmap_sampler;
258         glCreateSamplers(1, &mipmap_sampler);
259         glSamplerParameteri(mipmap_sampler, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST);
260         glSamplerParameteri(mipmap_sampler, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
261         glSamplerParameteri(mipmap_sampler, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
262         glSamplerParameteri(mipmap_sampler, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
263
264         // Coarsest level.
265         int level = coarsest_level;
266         int level_width = WIDTH >> level;
267         int level_height = HEIGHT >> level;
268         float patch_spacing_pixels = patch_size_pixels * (1.0f - patch_overlap_ratio);
269         int width_patches = 1 + lrintf((level_width - patch_size_pixels) / patch_spacing_pixels);
270         int height_patches = 1 + lrintf((level_height - patch_size_pixels) / patch_spacing_pixels);
271
272         // Make sure we always read from the correct level; the chosen
273         // mipmapping could otherwise be rather unpredictable, especially
274         // during motion search.
275         GLuint tex0_view, tex1_view;
276         glGenTextures(1, &tex0_view);
277         glTextureView(tex0_view, GL_TEXTURE_2D, tex0, GL_R8, level, 1, 0, 1);
278         glGenTextures(1, &tex1_view);
279         glTextureView(tex1_view, GL_TEXTURE_2D, tex1, GL_R8, level, 1, 0, 1);
280
281         // Compute gradients in every point, used for the motion search.
282         // The DIS paper doesn't actually mention how these are computed,
283         // but seemingly, a 3x3 Sobel operator is used here (at least in
284         // later versions of the code), while a [1 -8 0 8 -1] kernel is
285         // used for all the derivatives in the variational refinement part
286         // (which borrows code from DeepFlow). This is inconsistent,
287         // but I guess we're better off with staying with the original
288         // decisions until we actually know having different ones would be better.
289
290         // Create a new texture; we could be fancy and render use a multi-level
291         // texture, but meh.
292         GLuint grad0_tex;
293         glCreateTextures(GL_TEXTURE_2D, 1, &grad0_tex);
294         glTextureStorage2D(grad0_tex, 1, GL_RG16F, level_width, level_height);
295
296         GLuint grad0_fbo;
297         glCreateFramebuffers(1, &grad0_fbo);
298         glNamedFramebufferTexture(grad0_fbo, GL_COLOR_ATTACHMENT0, grad0_tex, 0);
299
300         glUseProgram(sobel_program);
301         glBindTextureUnit(0, tex0_view);
302         glBindSampler(0, nearest_sampler);
303         glProgramUniform1i(sobel_program, glGetUniformLocation(sobel_program, "tex"), 0);
304         glProgramUniform1f(sobel_program, glGetUniformLocation(sobel_program, "inv_width"), 1.0f / level_width);
305         glProgramUniform1f(sobel_program, glGetUniformLocation(sobel_program, "inv_height"), 1.0f / level_height);
306
307         // Set up the VAO containing all the required position/texcoord data.
308         GLuint sobel_vao;
309         glCreateVertexArrays(1, &sobel_vao);
310         glBindVertexArray(sobel_vao);
311         float vertices[] = {
312                 0.0f, 1.0f,
313                 0.0f, 0.0f,
314                 1.0f, 1.0f,
315                 1.0f, 0.0f,
316         };
317         GLuint vertex_vbo;
318         glCreateBuffers(1, &vertex_vbo);
319         glNamedBufferData(vertex_vbo, sizeof(vertices), vertices, GL_STATIC_DRAW);
320         glBindBuffer(GL_ARRAY_BUFFER, vertex_vbo);
321
322         int position_attrib = glGetAttribLocation(sobel_program, "position");
323         glEnableVertexArrayAttrib(sobel_vao, position_attrib);
324         glVertexAttribPointer(position_attrib, 2, GL_FLOAT, GL_FALSE, 0, BUFFER_OFFSET(0));
325
326         int texcoord_attrib = glGetAttribLocation(sobel_program, "texcoord");
327         glEnableVertexArrayAttrib(sobel_vao, texcoord_attrib);
328         glVertexAttribPointer(texcoord_attrib, 2, GL_FLOAT, GL_FALSE, 0, BUFFER_OFFSET(0));
329
330         glBindBuffer(GL_ARRAY_BUFFER, 0);
331
332         // Now finally draw.
333         glViewport(0, 0, level_width, level_height);
334         glBindFramebuffer(GL_FRAMEBUFFER, grad0_fbo);
335         glUseProgram(sobel_program);
336         glDisable(GL_BLEND);
337         glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
338
339         // Motion search to find the initial flow.
340
341         // Create a flow texture, initialized to zero.
342         GLuint flow_tex;
343         glCreateTextures(GL_TEXTURE_2D, 1, &flow_tex);
344         glTextureStorage2D(flow_tex, 1, GL_RG16F, width_patches, height_patches);
345
346         // And an output flow texture. (Well, we could have used texture barriers,
347         // but I don't feel lucky today.)
348         GLuint flow_out_tex;
349         glCreateTextures(GL_TEXTURE_2D, 1, &flow_out_tex);
350         glTextureStorage2D(flow_out_tex, 1, GL_RG16F, width_patches, height_patches);
351
352         GLuint flow_fbo;
353         glCreateFramebuffers(1, &flow_fbo);
354         glNamedFramebufferTexture(flow_fbo, GL_COLOR_ATTACHMENT0, flow_out_tex, 0);
355
356         glUseProgram(motion_search_program);
357
358         bind_sampler(motion_search_program, "image0_tex", 0, tex0_view, nearest_sampler);
359         bind_sampler(motion_search_program, "image1_tex", 1, tex1_view, linear_sampler);
360         bind_sampler(motion_search_program, "grad0_tex", 2, grad0_tex, nearest_sampler);
361         bind_sampler(motion_search_program, "flow_tex", 3, flow_tex, nearest_sampler);
362
363         glProgramUniform1f(motion_search_program, glGetUniformLocation(motion_search_program, "image_width"), level_width);
364         glProgramUniform1f(motion_search_program, glGetUniformLocation(motion_search_program, "image_height"), level_height);
365         glProgramUniform1f(motion_search_program, glGetUniformLocation(motion_search_program, "inv_image_width"), 1.0f / level_width);
366         glProgramUniform1f(motion_search_program, glGetUniformLocation(motion_search_program, "inv_image_height"), 1.0f / level_height);
367
368 //      printf("%d x %d patches on this level\n", width_patches, height_patches);
369
370         // Set up the VAO containing all the required position/texcoord data.
371         GLuint motion_search_vao;
372         glCreateVertexArrays(1, &motion_search_vao);
373         glBindVertexArray(motion_search_vao);
374         glBindBuffer(GL_ARRAY_BUFFER, vertex_vbo);
375
376         position_attrib = glGetAttribLocation(motion_search_program, "position");
377         glEnableVertexArrayAttrib(motion_search_vao, position_attrib);
378         glVertexAttribPointer(position_attrib, 2, GL_FLOAT, GL_FALSE, 0, BUFFER_OFFSET(0));
379
380         texcoord_attrib = glGetAttribLocation(motion_search_program, "texcoord");
381         glEnableVertexArrayAttrib(motion_search_vao, texcoord_attrib);
382         glVertexAttribPointer(texcoord_attrib, 2, GL_FLOAT, GL_FALSE, 0, BUFFER_OFFSET(0));
383
384         glBindBuffer(GL_ARRAY_BUFFER, 0);
385
386         // And draw.
387         glViewport(0, 0, width_patches, height_patches);
388         glBindFramebuffer(GL_FRAMEBUFFER, flow_fbo);
389         glUseProgram(motion_search_program);
390         glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
391
392         // Do “densification”, ie., upsampling of the flow patches to the flow field
393         // (the same size as the image at this level). We draw one quad per patch
394         // over its entire covered area (using instancing in the vertex shader),
395         // and then weight the contributions in the pixel shader by post-warp difference.
396         // This is equation (3) in the paper.
397         //
398         // We accumulate the flow vectors in the R/G channels (for u/v) and the total
399         // weight in the B channel. Dividing R and G by B gives the normalized values.
400
401         // Set up an output texture.
402         GLuint dense_flow_tex;
403         glCreateTextures(GL_TEXTURE_2D, 1, &dense_flow_tex);
404         //glTextureStorage2D(dense_flow_tex, 1, GL_RGB16F, level_width, level_height);
405         glTextureStorage2D(dense_flow_tex, 1, GL_RGBA32F, level_width, level_height);
406
407         GLuint dense_flow_fbo;
408         glCreateFramebuffers(1, &dense_flow_fbo);
409         glNamedFramebufferTexture(dense_flow_fbo, GL_COLOR_ATTACHMENT0, dense_flow_tex, 0);
410
411         glUseProgram(densify_program);
412
413         bind_sampler(densify_program, "image0_tex", 0, tex0_view, nearest_sampler);
414         bind_sampler(densify_program, "image1_tex", 1, tex1_view, linear_sampler);
415         bind_sampler(densify_program, "flow_tex", 2, flow_out_tex, nearest_sampler);
416
417         glProgramUniform1i(densify_program, glGetUniformLocation(densify_program, "width_patches"), width_patches);
418         glProgramUniform2f(densify_program, glGetUniformLocation(densify_program, "patch_size"),
419                 float(patch_size_pixels) / level_width,
420                 float(patch_size_pixels) / level_height);
421
422         float patch_spacing_x = float(level_width - patch_size_pixels) / (width_patches - 1);
423         float patch_spacing_y = float(level_height - patch_size_pixels) / (height_patches - 1);
424         glProgramUniform2f(densify_program, glGetUniformLocation(densify_program, "patch_spacing"),
425                 patch_spacing_x / level_width,
426                 patch_spacing_y / level_height);
427
428         // Set up the VAO containing all the required position/texcoord data.
429         GLuint densify_vao;
430         glCreateVertexArrays(1, &densify_vao);
431         glBindVertexArray(densify_vao);
432         glBindBuffer(GL_ARRAY_BUFFER, vertex_vbo);
433
434         position_attrib = glGetAttribLocation(densify_program, "position");
435         glEnableVertexArrayAttrib(densify_vao, position_attrib);
436         glVertexAttribPointer(position_attrib, 2, GL_FLOAT, GL_FALSE, 0, BUFFER_OFFSET(0));
437
438         glBindBuffer(GL_ARRAY_BUFFER, 0);
439
440         // And draw.
441         glViewport(0, 0, level_width, level_height);
442         glEnable(GL_BLEND);
443         glBlendFunc(GL_ONE, GL_ONE);
444         glBindFramebuffer(GL_FRAMEBUFFER, dense_flow_fbo);
445         glDrawArraysInstanced(GL_TRIANGLE_STRIP, 0, 4, width_patches * height_patches);
446
447         // TODO: Variational refinement.
448
449         unique_ptr<float[]> dense_flow(new float[level_width * level_height * 3]);
450         glGetTextureImage(dense_flow_tex, 0, GL_RGB, GL_FLOAT, level_width * level_height * 3 * sizeof(float), dense_flow.get());
451
452         FILE *fp = fopen("flow.ppm", "wb");
453         fprintf(fp, "P6\n%d %d\n255\n", level_width, level_height);
454         for (unsigned y = 0; y < level_height; ++y) {
455                 for (unsigned x = 0; x < level_width; ++x) {
456                         float du = dense_flow[(y * level_width + x) * 3 + 0];
457                         float dv = dense_flow[(y * level_width + x) * 3 + 1];
458                         float w = dense_flow[(y * level_width + x) * 3 + 2];
459
460                         du /= w;
461                         dv /= w;
462
463                         float angle = atan2(dv, du);
464                         float magnitude = min(hypot(du * level_width, dv * level_height) / 20.0, 1.0);
465                         
466                         // HSV to RGB (from Wikipedia). Saturation is 1.
467                         float c = magnitude;
468                         float h = angle * 6.0 / (2.0 * M_PI);
469                         float X = c * (1.0 - (fmod(h, 2.0) - 1.0));
470                         float r = 0.0f, g = 0.0f, b = 0.0f;
471                         if (h < 1.0f) {
472                                 r = c; g = X;
473                         } else if (h < 2.0f) {
474                                 r = X; g = c;
475                         } else if (h < 3.0f) {
476                                 g = c; b = X;
477                         } else if (h < 4.0f) {
478                                 g = X; b = c;
479                         } else if (h < 5.0f) {
480                                 r = X; b = c;
481                         } else if (h < 6.0f) {
482                                 r = c; b = X;
483                         } else {
484                                 // h is NaN, so black is fine.
485                         }
486                         float m = magnitude - c;
487                         r += m, g += m, b += m;
488                         r = max(min(r, 1.0f), 0.0f);
489                         g = max(min(g, 1.0f), 0.0f);
490                         b = max(min(b, 1.0f), 0.0f);
491                         putc(lrintf(r * 255.0f), fp);
492                         putc(lrintf(g * 255.0f), fp);
493                         putc(lrintf(b * 255.0f), fp);
494                 }
495         }
496         fclose(fp);
497
498         fprintf(stderr, "err = %d\n", glGetError());
499 }