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>
22 #define BUFFER_OFFSET(i) ((char *)nullptr + (i))
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;
32 string read_file(const string &filename)
34 FILE *fp = fopen(filename.c_str(), "r");
36 perror(filename.c_str());
40 int ret = fseek(fp, 0, SEEK_END);
42 perror("fseek(SEEK_END)");
48 ret = fseek(fp, 0, SEEK_SET);
50 perror("fseek(SEEK_SET)");
56 ret = fread(&str[0], size, 1, fp);
62 fprintf(stderr, "Short read when trying to read %d bytes from %s\n",
63 size, filename.c_str());
72 GLuint compile_shader(const string &shader_src, GLenum type)
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);
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);
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 */ ";
94 for (char ch : shader_src) {
95 src_with_lines.push_back(ch);
98 snprintf(buf, sizeof(buf), "/* %3zu */ ", ++lineno);
99 src_with_lines += buf;
103 fprintf(stderr, "Failed to compile shader:\n%s\n", src_with_lines.c_str());
111 GLuint load_texture(const char *filename, unsigned width, unsigned height)
113 FILE *fp = fopen(filename, "rb");
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);
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]);
132 for (int w = width, h = height; w > 1 || h > 1; ) {
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);
147 GLuint link_program(GLuint vs_obj, GLuint fs_obj)
149 GLuint program = glCreateProgram();
150 glAttachShader(program, vs_obj);
151 glAttachShader(program, fs_obj);
152 glLinkProgram(program);
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);
164 GLuint generate_vbo(GLint size, GLsizeiptr data_size, const GLvoid *data)
167 glCreateBuffers(1, &vbo);
168 glBufferData(GL_ARRAY_BUFFER, data_size, data, GL_STATIC_DRAW);
169 glNamedBufferData(vbo, data_size, data, GL_STATIC_DRAW);
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)
175 int attrib = glGetAttribLocation(glsl_program_num, attribute_name.c_str());
180 GLuint vbo = generate_vbo(size, data_size, data);
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);
190 void bind_sampler(GLuint program, const char *uniform_name, GLuint texture_unit, GLuint tex, GLuint sampler)
192 GLint location = glGetUniformLocation(program, uniform_name);
193 if (location == -1) {
197 glBindTextureUnit(texture_unit, tex);
198 glBindSampler(texture_unit, sampler);
199 glProgramUniform1i(program, location, texture_unit);
204 if (SDL_Init(SDL_INIT_EVERYTHING) == -1) {
205 fprintf(stderr, "SDL_Init failed: %s\n", SDL_GetError());
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);
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,
222 SDL_GLContext context = SDL_GL_CreateContext(window);
223 assert(context != nullptr);
226 GLuint tex0 = load_texture("test1499.pgm", WIDTH, HEIGHT);
227 GLuint tex1 = load_texture("test1500.pgm", WIDTH, HEIGHT);
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);
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);
238 // Make some samplers.
239 GLuint nearest_sampler;
240 glCreateSamplers(1, &nearest_sampler);
241 glSamplerParameteri(nearest_sampler, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
242 glSamplerParameteri(nearest_sampler, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
243 glSamplerParameteri(nearest_sampler, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
244 glSamplerParameteri(nearest_sampler, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
246 GLuint linear_sampler;
247 glCreateSamplers(1, &linear_sampler);
248 glSamplerParameteri(linear_sampler, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
249 glSamplerParameteri(linear_sampler, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
250 glSamplerParameteri(linear_sampler, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
251 glSamplerParameteri(linear_sampler, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
253 GLuint mipmap_sampler;
254 glCreateSamplers(1, &mipmap_sampler);
255 glSamplerParameteri(mipmap_sampler, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST);
256 glSamplerParameteri(mipmap_sampler, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
257 glSamplerParameteri(mipmap_sampler, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
258 glSamplerParameteri(mipmap_sampler, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
261 int level = coarsest_level;
262 int level_width = WIDTH >> level;
263 int level_height = HEIGHT >> level;
264 float patch_spacing_pixels = patch_size_pixels * (1.0f - patch_overlap_ratio);
265 int width_patches = 1 + lrintf((level_width - patch_size_pixels) / patch_spacing_pixels);
266 int height_patches = 1 + lrintf((level_height - patch_size_pixels) / patch_spacing_pixels);
268 // Make sure we always read from the correct level; the chosen
269 // mipmapping could otherwise be rather unpredictable, especially
270 // during motion search.
271 GLuint tex0_view, tex1_view;
272 glGenTextures(1, &tex0_view);
273 glTextureView(tex0_view, GL_TEXTURE_2D, tex0, GL_R8, level, 1, 0, 1);
274 glGenTextures(1, &tex1_view);
275 glTextureView(tex1_view, GL_TEXTURE_2D, tex1, GL_R8, level, 1, 0, 1);
277 // Compute gradients in every point, used for the motion search.
278 // The DIS paper doesn't actually mention how these are computed,
279 // but seemingly, a 3x3 Sobel operator is used here (at least in
280 // later versions of the code), while a [1 -8 0 8 -1] kernel is
281 // used for all the derivatives in the variational refinement part
282 // (which borrows code from DeepFlow). This is inconsistent,
283 // but I guess we're better off with staying with the original
284 // decisions until we actually know having different ones would be better.
286 // Create a new texture; we could be fancy and render use a multi-level
289 glCreateTextures(GL_TEXTURE_2D, 1, &grad0_tex);
290 glTextureStorage2D(grad0_tex, 1, GL_RG16F, level_width, level_height);
293 glCreateFramebuffers(1, &grad0_fbo);
294 glNamedFramebufferTexture(grad0_fbo, GL_COLOR_ATTACHMENT0, grad0_tex, 0);
296 glUseProgram(sobel_program);
297 glBindTextureUnit(0, tex0_view);
298 glBindSampler(0, nearest_sampler);
299 glProgramUniform1i(sobel_program, glGetUniformLocation(sobel_program, "tex"), 0);
300 glProgramUniform1f(sobel_program, glGetUniformLocation(sobel_program, "inv_width"), 1.0f / level_width);
301 glProgramUniform1f(sobel_program, glGetUniformLocation(sobel_program, "inv_height"), 1.0f / level_height);
303 // Set up the VAO containing all the required position/texcoord data.
305 glCreateVertexArrays(1, &sobel_vao);
306 glBindVertexArray(sobel_vao);
314 glCreateBuffers(1, &vertex_vbo);
315 glNamedBufferData(vertex_vbo, sizeof(vertices), vertices, GL_STATIC_DRAW);
316 glBindBuffer(GL_ARRAY_BUFFER, vertex_vbo);
318 int position_attrib = glGetAttribLocation(sobel_program, "position");
319 glEnableVertexArrayAttrib(sobel_vao, position_attrib);
320 glVertexAttribPointer(position_attrib, 2, GL_FLOAT, GL_FALSE, 0, BUFFER_OFFSET(0));
322 int texcoord_attrib = glGetAttribLocation(sobel_program, "texcoord");
323 glEnableVertexArrayAttrib(sobel_vao, texcoord_attrib);
324 glVertexAttribPointer(texcoord_attrib, 2, GL_FLOAT, GL_FALSE, 0, BUFFER_OFFSET(0));
326 glBindBuffer(GL_ARRAY_BUFFER, 0);
329 glViewport(0, 0, level_width, level_height);
330 glBindFramebuffer(GL_FRAMEBUFFER, grad0_fbo);
331 glUseProgram(sobel_program);
332 glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
334 // Motion search to find the initial flow.
336 // Create a flow texture, initialized to zero.
338 glCreateTextures(GL_TEXTURE_2D, 1, &flow_tex);
339 glTextureStorage2D(flow_tex, 1, GL_RG16F, width_patches, height_patches);
341 // And an output flow texture. (Well, we could have used texture barriers,
342 // but I don't feel lucky today.)
344 glCreateTextures(GL_TEXTURE_2D, 1, &flow_out_tex);
345 glTextureStorage2D(flow_out_tex, 1, GL_RG16F, width_patches, height_patches);
348 glCreateFramebuffers(1, &flow_fbo);
349 glNamedFramebufferTexture(flow_fbo, GL_COLOR_ATTACHMENT0, flow_out_tex, 0);
351 glUseProgram(motion_search_program);
353 bind_sampler(motion_search_program, "image0_tex", 0, tex0_view, nearest_sampler);
354 bind_sampler(motion_search_program, "image1_tex", 1, tex1_view, linear_sampler);
355 bind_sampler(motion_search_program, "grad0_tex", 2, grad0_tex, nearest_sampler);
356 bind_sampler(motion_search_program, "flow_tex", 3, flow_tex, nearest_sampler);
358 glProgramUniform1f(motion_search_program, glGetUniformLocation(motion_search_program, "image_width"), level_width);
359 glProgramUniform1f(motion_search_program, glGetUniformLocation(motion_search_program, "image_height"), level_height);
360 glProgramUniform1f(motion_search_program, glGetUniformLocation(motion_search_program, "inv_image_width"), 1.0f / level_width);
361 glProgramUniform1f(motion_search_program, glGetUniformLocation(motion_search_program, "inv_image_height"), 1.0f / level_height);
363 // printf("%d x %d patches on this level\n", width_patches, height_patches);
365 // Set up the VAO containing all the required position/texcoord data.
366 GLuint motion_search_vao;
367 glCreateVertexArrays(1, &motion_search_vao);
368 glBindVertexArray(motion_search_vao);
369 glBindBuffer(GL_ARRAY_BUFFER, vertex_vbo);
371 position_attrib = glGetAttribLocation(motion_search_program, "position");
372 glEnableVertexArrayAttrib(motion_search_vao, position_attrib);
373 glVertexAttribPointer(position_attrib, 2, GL_FLOAT, GL_FALSE, 0, BUFFER_OFFSET(0));
375 texcoord_attrib = glGetAttribLocation(motion_search_program, "texcoord");
376 glEnableVertexArrayAttrib(motion_search_vao, texcoord_attrib);
377 glVertexAttribPointer(texcoord_attrib, 2, GL_FLOAT, GL_FALSE, 0, BUFFER_OFFSET(0));
379 glBindBuffer(GL_ARRAY_BUFFER, 0);
382 glViewport(0, 0, width_patches, height_patches);
383 glBindFramebuffer(GL_FRAMEBUFFER, flow_fbo);
384 glUseProgram(motion_search_program);
385 glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
387 fprintf(stderr, "err = %d\n", glGetError());