X-Git-Url: https://git.sesse.net/?a=blobdiff_plain;f=effect_chain.cpp;h=a0eef117c0fe972cf6dc4df7d21cdcf586996a90;hb=907cb1eff7308026f467ee9cc659cd8a725774da;hp=34a396aa607ab65db3e64f4f9f57a1273e45f021;hpb=b89deb99c8e5d5e96d502a7c90b2bbc7daaac822;p=movit diff --git a/effect_chain.cpp b/effect_chain.cpp index 34a396a..a0eef11 100644 --- a/effect_chain.cpp +++ b/effect_chain.cpp @@ -1873,6 +1873,7 @@ void EffectChain::render_to_fbo(GLuint dest_fbo, unsigned width, unsigned height glBeginQuery(GL_TIME_ELAPSED, timer_query_object); phase->timer_query_objects_running.push_back(timer_query_object); } + bool render_to_texture = true; if (phase_num == phases.size() - 1) { // Last phase goes to the output the user specified. glBindFramebuffer(GL_FRAMEBUFFER, dest_fbo); @@ -1880,16 +1881,16 @@ void EffectChain::render_to_fbo(GLuint dest_fbo, unsigned width, unsigned height GLenum status = glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT); assert(status == GL_FRAMEBUFFER_COMPLETE); glViewport(x, y, width, height); + render_to_texture = false; if (dither_effect != nullptr) { CHECK(dither_effect->set_int("output_width", width)); CHECK(dither_effect->set_int("output_height", height)); } } - bool last_phase = (phase_num == phases.size() - 1); // Enable sRGB rendering for intermediates in case we are // rendering to an sRGB format. - bool needs_srgb = last_phase ? final_srgb : true; + bool needs_srgb = render_to_texture ? true : final_srgb; if (needs_srgb && !current_srgb) { glEnable(GL_FRAMEBUFFER_SRGB); check_error(); @@ -1900,16 +1901,14 @@ void EffectChain::render_to_fbo(GLuint dest_fbo, unsigned width, unsigned height current_srgb = true; } - execute_phase(phase, last_phase, &output_textures, &generated_mipmaps); + execute_phase(phase, render_to_texture, &output_textures, &generated_mipmaps); if (do_phase_timing) { glEndQuery(GL_TIME_ELAPSED); } } - for (map::const_iterator texture_it = output_textures.begin(); - texture_it != output_textures.end(); - ++texture_it) { - resource_pool->release_2d_texture(texture_it->second); + for (const auto &phase_and_texnum : output_textures) { + resource_pool->release_2d_texture(phase_and_texnum.second); } glBindFramebuffer(GL_FRAMEBUFFER, 0); @@ -1926,8 +1925,8 @@ void EffectChain::render_to_fbo(GLuint dest_fbo, unsigned width, unsigned height // Get back the timer queries. for (unsigned phase_num = 0; phase_num < phases.size(); ++phase_num) { Phase *phase = phases[phase_num]; - for (std::list::iterator timer_it = phase->timer_query_objects_running.begin(); - timer_it != phase->timer_query_objects_running.end(); ) { + for (auto timer_it = phase->timer_query_objects_running.cbegin(); + timer_it != phase->timer_query_objects_running.cend(); ) { GLint timer_query_object = *timer_it; GLint available; glGetQueryObjectiv(timer_query_object, GL_QUERY_RESULT_AVAILABLE, &available); @@ -1982,7 +1981,7 @@ void EffectChain::print_phase_timing() printf("Total: %5.1f ms\n", total_time_ms); } -void EffectChain::execute_phase(Phase *phase, bool last_phase, +void EffectChain::execute_phase(Phase *phase, bool render_to_texture, map *output_textures, set *generated_mipmaps) { @@ -1990,11 +1989,22 @@ void EffectChain::execute_phase(Phase *phase, bool last_phase, // Find a texture for this phase. inform_input_sizes(phase); - if (!last_phase) { + if (render_to_texture) { find_output_size(phase); GLuint tex_num = resource_pool->create_2d_texture(intermediate_format, phase->output_width, phase->output_height); + assert(tex_num != 0); output_textures->insert(make_pair(phase, tex_num)); + + // The output texture needs to have valid state to be written to by a compute shader. + if (phase->is_compute_shader) { + glActiveTexture(GL_TEXTURE0); + check_error(); + glBindTexture(GL_TEXTURE_2D, (*output_textures)[phase]); + check_error(); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); + check_error(); + } } // Set up RTT inputs for this phase. @@ -2002,6 +2012,7 @@ void EffectChain::execute_phase(Phase *phase, bool last_phase, glActiveTexture(GL_TEXTURE0 + sampler); Phase *input = phase->inputs[sampler]; input->output_node->bound_sampler_num = sampler; + assert(output_textures->count(input)); glBindTexture(GL_TEXTURE_2D, (*output_textures)[input]); check_error(); if (phase->input_needs_mipmaps && generated_mipmaps->count(input) == 0) { @@ -2021,6 +2032,7 @@ void EffectChain::execute_phase(Phase *phase, bool last_phase, // This is currently the only place where we use image units, // so we can always use 0. phase->outbuf_image_unit = 0; + assert(output_textures->count(phase)); glBindImageTexture(phase->outbuf_image_unit, (*output_textures)[phase], 0, GL_FALSE, 0, GL_WRITE_ONLY, GL_RGBA16F); check_error(); phase->inv_output_size.x = 1.0f / phase->output_width; @@ -2028,8 +2040,8 @@ void EffectChain::execute_phase(Phase *phase, bool last_phase, phase->output_texcoord_adjust.x = 0.5f / phase->output_width; phase->output_texcoord_adjust.y = 0.5f / phase->output_height; } else { - // (Already set up for us if it is the last phase.) - if (!last_phase) { + // (Already set up for us if we are outputting to the user's FBO.) + if (render_to_texture) { fbo = resource_pool->create_fbo((*output_textures)[phase]); glBindFramebuffer(GL_FRAMEBUFFER, fbo); glViewport(0, 0, phase->output_width, phase->output_height); @@ -2061,6 +2073,9 @@ void EffectChain::execute_phase(Phase *phase, bool last_phase, // since they can be updated from there. setup_uniforms(phase); glDispatchCompute(x, y, z); + check_error(); + glMemoryBarrier(GL_TEXTURE_FETCH_BARRIER_BIT | GL_TEXTURE_UPDATE_BARRIER_BIT); + check_error(); } else { // Uniforms need to come after set_gl_state(), since they can be updated // from there. @@ -2083,7 +2098,7 @@ void EffectChain::execute_phase(Phase *phase, bool last_phase, resource_pool->unuse_glsl_program(instance_program_num); - if (!last_phase && !phase->is_compute_shader) { + if (render_to_texture && !phase->is_compute_shader) { resource_pool->release_fbo(fbo); } }