+
+ if (do_phase_timing) {
+ // Get back the timer queries.
+ for (unsigned phase_num = 0; phase_num < phases.size(); ++phase_num) {
+ Phase *phase = phases[phase_num];
+ for (std::list<GLuint>::iterator timer_it = phase->timer_query_objects_running.begin();
+ timer_it != phase->timer_query_objects_running.end(); ) {
+ GLint timer_query_object = *timer_it;
+ GLint available;
+ glGetQueryObjectiv(timer_query_object, GL_QUERY_RESULT_AVAILABLE, &available);
+ if (available) {
+ GLuint64 time_elapsed;
+ glGetQueryObjectui64v(timer_query_object, GL_QUERY_RESULT, &time_elapsed);
+ phase->time_elapsed_ns += time_elapsed;
+ ++phase->num_measured_iterations;
+ phase->timer_query_objects_free.push_back(timer_query_object);
+ phase->timer_query_objects_running.erase(timer_it++);
+ } else {
+ ++timer_it;
+ }
+ }
+ }
+ }
+}
+
+void EffectChain::enable_phase_timing(bool enable)
+{
+ if (enable) {
+ assert(movit_timer_queries_supported);
+ }
+ this->do_phase_timing = enable;
+}
+
+void EffectChain::reset_phase_timing()
+{
+ for (unsigned phase_num = 0; phase_num < phases.size(); ++phase_num) {
+ Phase *phase = phases[phase_num];
+ phase->time_elapsed_ns = 0;
+ phase->num_measured_iterations = 0;
+ }
+}
+
+void EffectChain::print_phase_timing()
+{
+ double total_time_ms = 0.0;
+ for (unsigned phase_num = 0; phase_num < phases.size(); ++phase_num) {
+ Phase *phase = phases[phase_num];
+ double avg_time_ms = phase->time_elapsed_ns * 1e-6 / phase->num_measured_iterations;
+ printf("Phase %d: %5.1f ms [", phase_num, avg_time_ms);
+ for (unsigned effect_num = 0; effect_num < phase->effects.size(); ++effect_num) {
+ if (effect_num != 0) {
+ printf(", ");
+ }
+ printf("%s", phase->effects[effect_num]->effect->effect_type_id().c_str());
+ }
+ printf("]\n");
+ total_time_ms += avg_time_ms;
+ }
+ printf("Total: %5.1f ms\n", total_time_ms);
+}
+
+void EffectChain::execute_phase(Phase *phase, bool last_phase,
+ map<Phase *, GLuint> *output_textures,
+ set<Phase *> *generated_mipmaps)
+{
+ GLuint fbo = 0;
+
+ // Find a texture for this phase.
+ inform_input_sizes(phase);
+ if (!last_phase) {
+ find_output_size(phase);
+
+ GLuint tex_num = resource_pool->create_2d_texture(intermediate_format, phase->output_width, phase->output_height);
+ output_textures->insert(make_pair(phase, tex_num));
+ }
+
+ // Set up RTT inputs for this phase.
+ for (unsigned sampler = 0; sampler < phase->inputs.size(); ++sampler) {
+ glActiveTexture(GL_TEXTURE0 + sampler);
+ Phase *input = phase->inputs[sampler];
+ input->output_node->bound_sampler_num = sampler;
+ glBindTexture(GL_TEXTURE_2D, (*output_textures)[input]);
+ check_error();
+ if (phase->input_needs_mipmaps && generated_mipmaps->count(input) == 0) {
+ glGenerateMipmap(GL_TEXTURE_2D);
+ check_error();
+ generated_mipmaps->insert(input);
+ }
+ setup_rtt_sampler(sampler, phase->input_needs_mipmaps);
+ phase->input_samplers[sampler] = sampler; // Bind the sampler to the right uniform.
+ }
+
+ GLuint instance_program_num = resource_pool->use_glsl_program(phase->glsl_program_num);
+ check_error();
+
+ // And now the output.
+ if (phase->is_compute_shader) {
+ // This is currently the only place where we use image units,
+ // so we can always use 0.
+ phase->outbuf_image_unit = 0;
+ 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;
+ phase->inv_output_size.y = 1.0f / phase->output_height;
+ 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) {
+ fbo = resource_pool->create_fbo((*output_textures)[phase]);
+ glBindFramebuffer(GL_FRAMEBUFFER, fbo);
+ glViewport(0, 0, phase->output_width, phase->output_height);
+ }
+ }
+
+ // Give the required parameters to all the effects.
+ unsigned sampler_num = phase->inputs.size();
+ for (unsigned i = 0; i < phase->effects.size(); ++i) {
+ Node *node = phase->effects[i];
+ unsigned old_sampler_num = sampler_num;
+ node->effect->set_gl_state(instance_program_num, phase->effect_ids[node], &sampler_num);
+ check_error();
+
+ if (node->effect->is_single_texture()) {
+ assert(sampler_num - old_sampler_num == 1);
+ node->bound_sampler_num = old_sampler_num;
+ } else {
+ node->bound_sampler_num = -1;
+ }
+ }
+
+
+ if (phase->is_compute_shader) {
+ unsigned x, y, z;
+ phase->output_node->effect->get_compute_dimensions(phase->output_width, phase->output_height, &x, &y, &z);
+
+ // Uniforms need to come after set_gl_state() _and_ get_compute_dimensions(),
+ // since they can be updated from there.
+ setup_uniforms(phase);
+ glDispatchCompute(x, y, z);
+ } else {
+ // Uniforms need to come after set_gl_state(), since they can be updated
+ // from there.
+ setup_uniforms(phase);
+
+ // Bind the vertex data.
+ GLuint vao = resource_pool->create_vec2_vao(phase->attribute_indexes, vbo);
+ glBindVertexArray(vao);
+
+ glDrawArrays(GL_TRIANGLES, 0, 3);
+ check_error();
+
+ resource_pool->release_vec2_vao(vao);
+ }
+
+ for (unsigned i = 0; i < phase->effects.size(); ++i) {
+ Node *node = phase->effects[i];
+ node->effect->clear_gl_state();
+ }
+
+ resource_pool->unuse_glsl_program(instance_program_num);
+
+ if (!last_phase && !phase->is_compute_shader) {
+ resource_pool->release_fbo(fbo);
+ }
+}
+
+void EffectChain::setup_uniforms(Phase *phase)
+{
+ // TODO: Use UBO blocks.
+ for (size_t i = 0; i < phase->uniforms_image2d.size(); ++i) {
+ const Uniform<int> &uniform = phase->uniforms_image2d[i];
+ if (uniform.location != -1) {
+ glUniform1iv(uniform.location, uniform.num_values, uniform.value);
+ }
+ }
+ for (size_t i = 0; i < phase->uniforms_sampler2d.size(); ++i) {
+ const Uniform<int> &uniform = phase->uniforms_sampler2d[i];
+ if (uniform.location != -1) {
+ glUniform1iv(uniform.location, uniform.num_values, uniform.value);
+ }
+ }
+ for (size_t i = 0; i < phase->uniforms_bool.size(); ++i) {
+ const Uniform<bool> &uniform = phase->uniforms_bool[i];
+ assert(uniform.num_values == 1);
+ if (uniform.location != -1) {
+ glUniform1i(uniform.location, *uniform.value);
+ }
+ }
+ for (size_t i = 0; i < phase->uniforms_int.size(); ++i) {
+ const Uniform<int> &uniform = phase->uniforms_int[i];
+ if (uniform.location != -1) {
+ glUniform1iv(uniform.location, uniform.num_values, uniform.value);
+ }
+ }
+ for (size_t i = 0; i < phase->uniforms_float.size(); ++i) {
+ const Uniform<float> &uniform = phase->uniforms_float[i];
+ if (uniform.location != -1) {
+ glUniform1fv(uniform.location, uniform.num_values, uniform.value);
+ }
+ }
+ for (size_t i = 0; i < phase->uniforms_vec2.size(); ++i) {
+ const Uniform<float> &uniform = phase->uniforms_vec2[i];
+ if (uniform.location != -1) {
+ glUniform2fv(uniform.location, uniform.num_values, uniform.value);
+ }
+ }
+ for (size_t i = 0; i < phase->uniforms_vec3.size(); ++i) {
+ const Uniform<float> &uniform = phase->uniforms_vec3[i];
+ if (uniform.location != -1) {
+ glUniform3fv(uniform.location, uniform.num_values, uniform.value);
+ }
+ }
+ for (size_t i = 0; i < phase->uniforms_vec4.size(); ++i) {
+ const Uniform<float> &uniform = phase->uniforms_vec4[i];
+ if (uniform.location != -1) {
+ glUniform4fv(uniform.location, uniform.num_values, uniform.value);
+ }
+ }
+ for (size_t i = 0; i < phase->uniforms_mat3.size(); ++i) {
+ const Uniform<Matrix3d> &uniform = phase->uniforms_mat3[i];
+ assert(uniform.num_values == 1);
+ if (uniform.location != -1) {
+ // Convert to float (GLSL has no double matrices).
+ float matrixf[9];
+ for (unsigned y = 0; y < 3; ++y) {
+ for (unsigned x = 0; x < 3; ++x) {
+ matrixf[y + x * 3] = (*uniform.value)(y, x);
+ }
+ }
+ glUniformMatrix3fv(uniform.location, 1, GL_FALSE, matrixf);
+ }
+ }
+}
+
+void EffectChain::setup_rtt_sampler(int sampler_num, bool use_mipmaps)
+{
+ glActiveTexture(GL_TEXTURE0 + sampler_num);
+ check_error();
+ if (use_mipmaps) {
+ glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST);
+ check_error();
+ } else {
+ glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
+ check_error();
+ }
+ glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
+ check_error();
+ glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);