X-Git-Url: https://git.sesse.net/?a=blobdiff_plain;f=flow.cpp;h=e672fe6f6d8fefb8ecc97935b73af79ee3495f8e;hb=7709f1f068c9ab97b134ffda2f4c28c7b5aeae42;hp=882a0b6f235857a3577bfa93b41a45d2ebfcce5a;hpb=9ac85ad3923474830e946a52d4bfae8b0d819f6b;p=nageru diff --git a/flow.cpp b/flow.cpp index 882a0b6..e672fe6 100644 --- a/flow.cpp +++ b/flow.cpp @@ -1,21 +1,20 @@ #define NO_SDL_GLEXT 1 -#include - -#include -#include -#include -#include - #include "flow.h" + #include "gpu_timers.h" #include "util.h" #include +#include #include -#include +#include #include +#include #include +#include +#include +#include #include #define BUFFER_OFFSET(i) ((char *)nullptr + (i)) @@ -80,7 +79,7 @@ string read_file(const string &filename) } if (ret == 0) { fprintf(stderr, "Short read when trying to read %d bytes from %s\n", - size, filename.c_str()); + size, filename.c_str()); exit(1); } fclose(fp); @@ -88,11 +87,10 @@ string read_file(const string &filename) return str; } - GLuint compile_shader(const string &shader_src, GLenum type) { GLuint obj = glCreateShader(type); - const GLchar* source[] = { shader_src.data() }; + const GLchar *source[] = { shader_src.data() }; const GLint length[] = { (GLint)shader_src.size() }; glShaderSource(obj, 1, source, length); glCompileShader(obj); @@ -778,16 +776,16 @@ Splat::Splat(const OperatingPoint &op) uniform_splat_size = glGetUniformLocation(splat_program, "splat_size"); uniform_alpha = glGetUniformLocation(splat_program, "alpha"); - uniform_image_tex = glGetUniformLocation(splat_program, "image_tex"); + uniform_gray_tex = glGetUniformLocation(splat_program, "gray_tex"); uniform_flow_tex = glGetUniformLocation(splat_program, "flow_tex"); uniform_inv_flow_size = glGetUniformLocation(splat_program, "inv_flow_size"); } -void Splat::exec(GLuint image_tex, GLuint bidirectional_flow_tex, GLuint flow_tex, GLuint depth_rb, int width, int height, float alpha) +void Splat::exec(GLuint gray_tex, GLuint bidirectional_flow_tex, GLuint flow_tex, GLuint depth_rb, int width, int height, float alpha) { glUseProgram(splat_program); - bind_sampler(splat_program, uniform_image_tex, 0, image_tex, linear_sampler); + bind_sampler(splat_program, uniform_gray_tex, 0, gray_tex, linear_sampler); bind_sampler(splat_program, uniform_flow_tex, 1, bidirectional_flow_tex, nearest_sampler); glProgramUniform2f(splat_program, uniform_splat_size, op.splat_size / width, op.splat_size / height); @@ -797,6 +795,7 @@ void Splat::exec(GLuint image_tex, GLuint bidirectional_flow_tex, GLuint flow_te glViewport(0, 0, width, height); glDisable(GL_BLEND); glEnable(GL_DEPTH_TEST); + glDepthMask(GL_TRUE); glDepthFunc(GL_LESS); // We store the difference between I_0 and I_1, where less difference is good. (Default 1.0 is effectively +inf, which always loses.) fbos.render_to(depth_rb, flow_tex); @@ -914,10 +913,19 @@ void HoleBlend::exec(GLuint flow_tex, GLuint depth_rb, GLuint temp_tex[3], int w glDisable(GL_DEPTH_TEST); } -Blend::Blend() +Blend::Blend(bool split_ycbcr_output) + : split_ycbcr_output(split_ycbcr_output) { + string frag_shader = read_file("blend.frag"); + if (split_ycbcr_output) { + // Insert after the first #version line. + size_t offset = frag_shader.find('\n'); + assert(offset != string::npos); + frag_shader = frag_shader.substr(0, offset + 1) + "#define SPLIT_YCBCR_OUTPUT 1\n" + frag_shader.substr(offset + 1); + } + blend_vs_obj = compile_shader(read_file("vs.vert"), GL_VERTEX_SHADER); - blend_fs_obj = compile_shader(read_file("blend.frag"), GL_FRAGMENT_SHADER); + blend_fs_obj = compile_shader(frag_shader, GL_FRAGMENT_SHADER); blend_program = link_program(blend_vs_obj, blend_fs_obj); uniform_image_tex = glGetUniformLocation(blend_program, "image_tex"); @@ -926,7 +934,7 @@ Blend::Blend() uniform_flow_consistency_tolerance = glGetUniformLocation(blend_program, "flow_consistency_tolerance"); } -void Blend::exec(GLuint image_tex, GLuint flow_tex, GLuint output_tex, int level_width, int level_height, float alpha) +void Blend::exec(GLuint image_tex, GLuint flow_tex, GLuint output_tex, GLuint output2_tex, int level_width, int level_height, float alpha) { glUseProgram(blend_program); bind_sampler(blend_program, uniform_image_tex, 0, image_tex, linear_sampler); @@ -934,13 +942,20 @@ void Blend::exec(GLuint image_tex, GLuint flow_tex, GLuint output_tex, int level glProgramUniform1f(blend_program, uniform_alpha, alpha); glViewport(0, 0, level_width, level_height); - fbos.render_to(output_tex); + if (split_ycbcr_output) { + fbos_split.render_to(output_tex, output2_tex); + } else { + fbos.render_to(output_tex); + } glDisable(GL_BLEND); // A bit ironic, perhaps. glDrawArrays(GL_TRIANGLE_STRIP, 0, 4); } -Interpolate::Interpolate(int width, int height, const OperatingPoint &op) - : width(width), height(height), flow_level(op.finest_level), op(op), splat(op) { +Interpolate::Interpolate(const OperatingPoint &op, bool split_ycbcr_output) + : flow_level(op.finest_level), + split_ycbcr_output(split_ycbcr_output), + splat(op), + blend(split_ycbcr_output) { // Set up the vertex data that will be shared between all passes. float vertices[] = { 0.0f, 1.0f, @@ -960,7 +975,7 @@ Interpolate::Interpolate(int width, int height, const OperatingPoint &op) glVertexAttribPointer(position_attrib, 2, GL_FLOAT, GL_FALSE, 0, BUFFER_OFFSET(0)); } -GLuint Interpolate::exec(GLuint image_tex, GLuint bidirectional_flow_tex, GLuint width, GLuint height, float alpha) +pair Interpolate::exec(GLuint image_tex, GLuint gray_tex, GLuint bidirectional_flow_tex, GLuint width, GLuint height, float alpha) { GPUTimers timers; @@ -972,7 +987,7 @@ GLuint Interpolate::exec(GLuint image_tex, GLuint bidirectional_flow_tex, GLuint // Pick out the right level to test splatting results on. GLuint tex_view; glGenTextures(1, &tex_view); - glTextureView(tex_view, GL_TEXTURE_2D_ARRAY, image_tex, GL_RGBA8, flow_level, 1, 0, 2); + glTextureView(tex_view, GL_TEXTURE_2D_ARRAY, gray_tex, GL_R8, flow_level, 1, 0, 2); int flow_width = width >> flow_level; int flow_height = height >> flow_level; @@ -1002,10 +1017,20 @@ GLuint Interpolate::exec(GLuint image_tex, GLuint bidirectional_flow_tex, GLuint pool.release_texture(temp_tex[2]); pool.release_renderbuffer(depth_rb); - GLuint output_tex = pool.get_texture(GL_RGBA8, width, height); - { - ScopedTimer timer("Blend", &total_timer); - blend.exec(image_tex, flow_tex, output_tex, width, height, alpha); + GLuint output_tex, output2_tex = 0; + if (split_ycbcr_output) { + output_tex = pool.get_texture(GL_R8, width, height); + output2_tex = pool.get_texture(GL_RG8, width, height); + { + ScopedTimer timer("Blend", &total_timer); + blend.exec(image_tex, flow_tex, output_tex, output2_tex, width, height, alpha); + } + } else { + output_tex = pool.get_texture(GL_RGBA8, width, height); + { + ScopedTimer timer("Blend", &total_timer); + blend.exec(image_tex, flow_tex, output_tex, 0, width, height, alpha); + } } pool.release_texture(flow_tex); total_timer.end(); @@ -1013,16 +1038,19 @@ GLuint Interpolate::exec(GLuint image_tex, GLuint bidirectional_flow_tex, GLuint timers.print(); } - return output_tex; + return make_pair(output_tex, output2_tex); } GLuint TexturePool::get_texture(GLenum format, GLuint width, GLuint height, GLuint num_layers) { - for (Texture &tex : textures) { - if (!tex.in_use && !tex.is_renderbuffer && tex.format == format && - tex.width == width && tex.height == height && tex.num_layers == num_layers) { - tex.in_use = true; - return tex.tex_num; + { + lock_guard lock(mu); + for (Texture &tex : textures) { + if (!tex.in_use && !tex.is_renderbuffer && tex.format == format && + tex.width == width && tex.height == height && tex.num_layers == num_layers) { + tex.in_use = true; + return tex.tex_num; + } } } @@ -1040,17 +1068,23 @@ GLuint TexturePool::get_texture(GLenum format, GLuint width, GLuint height, GLui tex.num_layers = num_layers; tex.in_use = true; tex.is_renderbuffer = false; - textures.push_back(tex); + { + lock_guard lock(mu); + textures.push_back(tex); + } return tex.tex_num; } GLuint TexturePool::get_renderbuffer(GLenum format, GLuint width, GLuint height) { - for (Texture &tex : textures) { - if (!tex.in_use && tex.is_renderbuffer && tex.format == format && - tex.width == width && tex.height == height) { - tex.in_use = true; - return tex.tex_num; + { + lock_guard lock(mu); + for (Texture &tex : textures) { + if (!tex.in_use && tex.is_renderbuffer && tex.format == format && + tex.width == width && tex.height == height) { + tex.in_use = true; + return tex.tex_num; + } } } @@ -1063,12 +1097,16 @@ GLuint TexturePool::get_renderbuffer(GLenum format, GLuint width, GLuint height) tex.height = height; tex.in_use = true; tex.is_renderbuffer = true; - textures.push_back(tex); + { + lock_guard lock(mu); + textures.push_back(tex); + } return tex.tex_num; } void TexturePool::release_texture(GLuint tex_num) { + lock_guard lock(mu); for (Texture &tex : textures) { if (!tex.is_renderbuffer && tex.tex_num == tex_num) { assert(tex.in_use); @@ -1081,6 +1119,7 @@ void TexturePool::release_texture(GLuint tex_num) void TexturePool::release_renderbuffer(GLuint tex_num) { + lock_guard lock(mu); for (Texture &tex : textures) { if (tex.is_renderbuffer && tex.tex_num == tex_num) { assert(tex.in_use);