Fix #114260: Compositor sometimes produces straight alpha #114305

Merged
Omar Emara merged 6 commits from OmarEmaraDev/blender:fix-114260 into main 2023-11-06 15:15:33 +01:00
2 changed files with 24 additions and 1 deletions
Showing only changes of commit 0bae72783d - Show all commits

View File

@ -24,8 +24,10 @@ GPU_SHADER_CREATE_INFO(compositor_read_input_vector)
GPU_SHADER_CREATE_INFO(compositor_read_input_color)
.additional_info("compositor_read_input_shared")
.push_constant(Type::BOOL, "premultiply_alpha")
.image(0, GPU_RGBA16F, Qualifier::WRITE, ImageType::FLOAT_2D, "output_img")
.define("READ_EXPRESSION(input_color)", "input_color")
.define("READ_EXPRESSION(input_color)",
"input_color * vec4(vec3(premultiply_alpha ? input_color.a : 1.0), 1.0)")
.do_static_compilation(true);
GPU_SHADER_CREATE_INFO(compositor_read_input_alpha)

View File

@ -519,6 +519,10 @@ class ImageOperation : public NodeOperation {
const int2 lower_bound = int2(0);
GPU_shader_uniform_2iv(shader, "lower_bound", lower_bound);
if (result.type() == ResultType::Color) {
GPU_shader_uniform_1b(shader, "premultiply_alpha", should_premultiply_alpha(image_user));
}
const int input_unit = GPU_shader_get_sampler_binding(shader, "input_tx");
GPU_texture_bind(image_texture, input_unit);
@ -570,6 +574,23 @@ class ImageOperation : public NodeOperation {
}
}
/* Compositor image inputs are expected to be always premultiplied, so identify if the GPU
* texture returned by the image module is straight and needs to be premultiplied. */
bool should_premultiply_alpha(ImageUser &image_user)
{
Image *image = get_image();
ImBuf *image_buffer = BKE_image_acquire_ibuf(image, &image_user, nullptr);
if (!image_buffer) {
return false;
}
const bool has_premultiplied_alpha = BKE_image_has_gpu_texture_premultiplied_alpha(
image, image_buffer);
BKE_image_release_ibuf(image, image_buffer, nullptr);
return !has_premultiplied_alpha;
}
Image *get_image()
{
return (Image *)bnode().id;