+ // If we're the last phase, add the right #defines for Y'CbCr multi-output as needed.
+ vector<string> frag_shader_outputs; // In order.
+ if (phase->output_node->outgoing_links.empty() && num_output_color_ycbcr > 0) {
+ switch (output_ycbcr_splitting[0]) {
+ case YCBCR_OUTPUT_INTERLEAVED:
+ // No #defines set.
+ frag_shader_outputs.push_back("FragColor");
+ break;
+ case YCBCR_OUTPUT_SPLIT_Y_AND_CBCR:
+ frag_shader += "#define YCBCR_OUTPUT_SPLIT_Y_AND_CBCR 1\n";
+ frag_shader_outputs.push_back("Y");
+ frag_shader_outputs.push_back("Chroma");
+ break;
+ case YCBCR_OUTPUT_PLANAR:
+ frag_shader += "#define YCBCR_OUTPUT_PLANAR 1\n";
+ frag_shader_outputs.push_back("Y");
+ frag_shader_outputs.push_back("Cb");
+ frag_shader_outputs.push_back("Cr");
+ break;
+ default:
+ assert(false);
+ }
+
+ if (num_output_color_ycbcr > 1) {
+ switch (output_ycbcr_splitting[1]) {
+ case YCBCR_OUTPUT_INTERLEAVED:
+ frag_shader += "#define SECOND_YCBCR_OUTPUT_INTERLEAVED 1\n";
+ frag_shader_outputs.push_back("YCbCr2");
+ break;
+ case YCBCR_OUTPUT_SPLIT_Y_AND_CBCR:
+ frag_shader += "#define SECOND_YCBCR_OUTPUT_SPLIT_Y_AND_CBCR 1\n";
+ frag_shader_outputs.push_back("Y2");
+ frag_shader_outputs.push_back("Chroma2");
+ break;
+ case YCBCR_OUTPUT_PLANAR:
+ frag_shader += "#define SECOND_YCBCR_OUTPUT_PLANAR 1\n";
+ frag_shader_outputs.push_back("Y2");
+ frag_shader_outputs.push_back("Cb2");
+ frag_shader_outputs.push_back("Cr2");
+ break;
+ default:
+ assert(false);
+ }
+ }
+
+ if (output_color_rgba) {
+ // Note: Needs to come in the header, because not only the
+ // output needs to see it (YCbCrConversionEffect and DitherEffect
+ // do, too).
+ frag_shader_header += "#define YCBCR_ALSO_OUTPUT_RGBA 1\n";
+ frag_shader_outputs.push_back("RGBA");
+ }
+ }
+
+ // If we're bouncing to a temporary texture, signal transformation if desired.
+ if (!phase->output_node->outgoing_links.empty()) {
+ if (intermediate_transformation == SQUARE_ROOT_FRAMEBUFFER_TRANSFORMATION &&
+ phase->output_node->output_gamma_curve == GAMMA_LINEAR) {
+ frag_shader += "#define SQUARE_ROOT_TRANSFORMATION 1\n";
+ }
+ }
+
+ if (phase->is_compute_shader) {
+ frag_shader.append(read_file("footer.comp"));
+ phase->output_node->effect->register_uniform_vec2("inv_output_size", (float *)&phase->inv_output_size);
+ phase->output_node->effect->register_uniform_vec2("output_texcoord_adjust", (float *)&phase->output_texcoord_adjust);
+ } else {
+ frag_shader.append(read_file("footer.frag"));
+ }
+
+ // Collect uniforms from all effects and output them. Note that this needs
+ // to happen after output_fragment_shader(), even though the uniforms come
+ // before in the output source, since output_fragment_shader() is allowed
+ // to register new uniforms (e.g. arrays that are of unknown length until
+ // finalization time).
+ // TODO: Make a uniform block for platforms that support it.
+ string frag_shader_uniforms = "";
+ for (unsigned i = 0; i < phase->effects.size(); ++i) {
+ Node *node = phase->effects[i];
+ Effect *effect = node->effect;
+ const string effect_id = phase->effect_ids[node];
+ extract_uniform_declarations(effect->uniforms_image2d, "image2D", effect_id, &phase->uniforms_image2d, &frag_shader_uniforms);
+ extract_uniform_declarations(effect->uniforms_sampler2d, "sampler2D", effect_id, &phase->uniforms_sampler2d, &frag_shader_uniforms);
+ extract_uniform_declarations(effect->uniforms_bool, "bool", effect_id, &phase->uniforms_bool, &frag_shader_uniforms);
+ extract_uniform_declarations(effect->uniforms_int, "int", effect_id, &phase->uniforms_int, &frag_shader_uniforms);
+ extract_uniform_declarations(effect->uniforms_float, "float", effect_id, &phase->uniforms_float, &frag_shader_uniforms);
+ extract_uniform_declarations(effect->uniforms_vec2, "vec2", effect_id, &phase->uniforms_vec2, &frag_shader_uniforms);
+ extract_uniform_declarations(effect->uniforms_vec3, "vec3", effect_id, &phase->uniforms_vec3, &frag_shader_uniforms);
+ extract_uniform_declarations(effect->uniforms_vec4, "vec4", effect_id, &phase->uniforms_vec4, &frag_shader_uniforms);
+ extract_uniform_array_declarations(effect->uniforms_float_array, "float", effect_id, &phase->uniforms_float, &frag_shader_uniforms);
+ extract_uniform_array_declarations(effect->uniforms_vec2_array, "vec2", effect_id, &phase->uniforms_vec2, &frag_shader_uniforms);
+ extract_uniform_array_declarations(effect->uniforms_vec3_array, "vec3", effect_id, &phase->uniforms_vec3, &frag_shader_uniforms);
+ extract_uniform_array_declarations(effect->uniforms_vec4_array, "vec4", effect_id, &phase->uniforms_vec4, &frag_shader_uniforms);
+ extract_uniform_declarations(effect->uniforms_mat3, "mat3", effect_id, &phase->uniforms_mat3, &frag_shader_uniforms);
+ }
+
+ frag_shader = frag_shader_header + frag_shader_uniforms + frag_shader;
+
+ string vert_shader = read_version_dependent_file("vs", "vert");
+
+ // If we're the last phase and need to flip the picture to compensate for
+ // the origin, tell the vertex shader so.
+ if (phase->output_node->outgoing_links.empty() && output_origin == OUTPUT_ORIGIN_TOP_LEFT) {
+ const string needle = "#define FLIP_ORIGIN 0";
+ size_t pos = vert_shader.find(needle);
+ assert(pos != string::npos);
+
+ vert_shader[pos + needle.size() - 1] = '1';
+ }
+
+ if (phase->is_compute_shader) {
+ phase->glsl_program_num = resource_pool->compile_glsl_compute_program(frag_shader);
+
+ Uniform<int> uniform;
+ uniform.name = "outbuf";
+ uniform.value = &phase->outbuf_image_unit;
+ uniform.prefix = "tex";
+ uniform.num_values = 1;
+ uniform.location = -1;
+ phase->uniforms_image2d.push_back(uniform);
+ } else {
+ phase->glsl_program_num = resource_pool->compile_glsl_program(vert_shader, frag_shader, frag_shader_outputs);
+ }
+ GLint position_attribute_index = glGetAttribLocation(phase->glsl_program_num, "position");
+ GLint texcoord_attribute_index = glGetAttribLocation(phase->glsl_program_num, "texcoord");
+ if (position_attribute_index != -1) {
+ phase->attribute_indexes.insert(position_attribute_index);
+ }
+ if (texcoord_attribute_index != -1) {
+ phase->attribute_indexes.insert(texcoord_attribute_index);
+ }
+
+ // Collect the resulting location numbers for each uniform.
+ collect_uniform_locations(phase->glsl_program_num, &phase->uniforms_image2d);
+ collect_uniform_locations(phase->glsl_program_num, &phase->uniforms_sampler2d);
+ collect_uniform_locations(phase->glsl_program_num, &phase->uniforms_bool);
+ collect_uniform_locations(phase->glsl_program_num, &phase->uniforms_int);
+ collect_uniform_locations(phase->glsl_program_num, &phase->uniforms_float);
+ collect_uniform_locations(phase->glsl_program_num, &phase->uniforms_vec2);
+ collect_uniform_locations(phase->glsl_program_num, &phase->uniforms_vec3);
+ collect_uniform_locations(phase->glsl_program_num, &phase->uniforms_vec4);
+ collect_uniform_locations(phase->glsl_program_num, &phase->uniforms_mat3);