Commit: 5ddddee07b174d1ef1b52f7622908aeb45ef2b7b Author: Omar Emara Date: Tue Nov 2 18:12:47 2021 +0200 Branches: viewport-compositor https://developer.blender.org/rB5ddddee07b174d1ef1b52f7622908aeb45ef2b7b
Viewport Compositor: Port Color Balance node This patch ports the Color Balance node to the viewport compositor. The shader is a straightforward port of the compositor code. A few utilities were added to ease implementation. =================================================================== M source/blender/gpu/CMakeLists.txt M source/blender/gpu/intern/gpu_material_library.c M source/blender/gpu/shaders/common/gpu_shader_common_color_util.glsl A source/blender/gpu/shaders/composite/gpu_shader_composite_color_balance.glsl M source/blender/nodes/composite/nodes/node_composite_colorbalance.cc =================================================================== diff --git a/source/blender/gpu/CMakeLists.txt b/source/blender/gpu/CMakeLists.txt index 0a3a14db15b..cdd8fb466ac 100644 --- a/source/blender/gpu/CMakeLists.txt +++ b/source/blender/gpu/CMakeLists.txt @@ -281,6 +281,7 @@ data_to_c_simple(shaders/common/gpu_shader_common_hash.glsl SRC) data_to_c_simple(shaders/composite/gpu_shader_composite_alpha_over.glsl SRC) data_to_c_simple(shaders/composite/gpu_shader_composite_bright_contrast.glsl SRC) +data_to_c_simple(shaders/composite/gpu_shader_composite_color_balance.glsl SRC) data_to_c_simple(shaders/composite/gpu_shader_composite_composite.glsl SRC) data_to_c_simple(shaders/composite/gpu_shader_composite_image.glsl SRC) data_to_c_simple(shaders/composite/gpu_shader_composite_invert.glsl SRC) diff --git a/source/blender/gpu/intern/gpu_material_library.c b/source/blender/gpu/intern/gpu_material_library.c index e8e6e7699fb..f9cd64801ed 100644 --- a/source/blender/gpu/intern/gpu_material_library.c +++ b/source/blender/gpu/intern/gpu_material_library.c @@ -47,6 +47,7 @@ extern char datatoc_gpu_shader_common_hash_glsl[]; extern char datatoc_gpu_shader_composite_alpha_over_glsl[]; extern char datatoc_gpu_shader_composite_bright_contrast_glsl[]; +extern char datatoc_gpu_shader_composite_color_balance_glsl[]; extern char datatoc_gpu_shader_composite_composite_glsl[]; extern char datatoc_gpu_shader_composite_image_glsl[]; extern char datatoc_gpu_shader_composite_invert_glsl[]; @@ -166,6 +167,13 @@ static GPUMaterialLibrary gpu_shader_composite_bright_contrast_library = { NULL}, }; +static GPUMaterialLibrary gpu_shader_composite_color_balance_library = { + .code = datatoc_gpu_shader_composite_color_balance_glsl, + .dependencies = {&gpu_shader_common_math_util_library, + &gpu_shader_common_color_util_library, + NULL}, +}; + static GPUMaterialLibrary gpu_shader_composite_composite_library = { .code = datatoc_gpu_shader_composite_composite_glsl, .dependencies = {NULL}, @@ -709,6 +717,7 @@ static GPUMaterialLibrary *gpu_material_libraries[] = { &gpu_shader_material_world_normals_library, &gpu_shader_composite_alpha_over_library, &gpu_shader_composite_bright_contrast_library, + &gpu_shader_composite_color_balance_library, &gpu_shader_composite_composite_library, &gpu_shader_composite_image_library, &gpu_shader_composite_invert_library, diff --git a/source/blender/gpu/shaders/common/gpu_shader_common_color_util.glsl b/source/blender/gpu/shaders/common/gpu_shader_common_color_util.glsl index f1a2f41cca4..ff39668b914 100644 --- a/source/blender/gpu/shaders/common/gpu_shader_common_color_util.glsl +++ b/source/blender/gpu/shaders/common/gpu_shader_common_color_util.glsl @@ -109,3 +109,33 @@ void color_alpha_unpremultiply(vec4 color, out vec4 result) result = vec4(color.rgb / color.a, color.a); } } + +float linear_rgb_to_srgb(float color) +{ + if (color < 0.0031308) { + return (color < 0.0) ? 0.0 : color * 12.92; + } + + return 1.055 * pow(color, 1.0 / 2.4) - 0.055; +} + +vec3 linear_rgb_to_srgb(vec3 color) +{ + return vec3( + linear_rgb_to_srgb(color.r), linear_rgb_to_srgb(color.g), linear_rgb_to_srgb(color.b)); +} + +float srgb_to_linear_rgb(float color) +{ + if (color < 0.04045) { + return (color < 0.0) ? 0.0 : color * (1.0 / 12.92); + } + + return pow((color + 0.055) * (1.0 / 1.055), 2.4); +} + +vec3 srgb_to_linear_rgb(vec3 color) +{ + return vec3( + srgb_to_linear_rgb(color.r), srgb_to_linear_rgb(color.g), srgb_to_linear_rgb(color.b)); +} diff --git a/source/blender/gpu/shaders/composite/gpu_shader_composite_color_balance.glsl b/source/blender/gpu/shaders/composite/gpu_shader_composite_color_balance.glsl new file mode 100644 index 00000000000..835e8a495d8 --- /dev/null +++ b/source/blender/gpu/shaders/composite/gpu_shader_composite_color_balance.glsl @@ -0,0 +1,32 @@ +void node_composite_color_balance_lgg( + float factor, vec4 color, vec3 lift, vec3 gamma, vec3 gain, out vec4 result) +{ + lift = 2.0 - lift; + vec3 srgb_color = linear_rgb_to_srgb(color.rgb); + vec3 lift_balanced = ((srgb_color - 1.0) * lift) + 1.0; + + vec3 gain_balanced = lift_balanced * gain; + gain_balanced = max(gain_balanced, vec3(0.0)); + + vec3 linear_color = srgb_to_linear_rgb(gain_balanced); + gamma = mix(gamma, vec3(1e-6), equal(gamma, vec3(0.0))); + vec3 gamma_balanced = pow(linear_color, 1.0 / gamma); + + result.rgb = mix(color.rgb, gamma_balanced, min(factor, 1.0)); + result.a = color.a; +} + +void node_composite_color_balance_asc_cdl(float factor, + vec4 color, + vec3 offset, + vec3 power, + vec3 slope, + float offset_basis, + out vec4 result) +{ + offset += offset_basis; + vec3 balanced = color.rgb * slope + offset; + balanced = pow(max(balanced, vec3(0.0)), power); + result.rgb = mix(color.rgb, balanced, min(factor, 1.0)); + result.a = color.a; +} diff --git a/source/blender/nodes/composite/nodes/node_composite_colorbalance.cc b/source/blender/nodes/composite/nodes/node_composite_colorbalance.cc index 440e37fe741..d5bf6e6da8c 100644 --- a/source/blender/nodes/composite/nodes/node_composite_colorbalance.cc +++ b/source/blender/nodes/composite/nodes/node_composite_colorbalance.cc @@ -80,6 +80,36 @@ static void node_composit_init_colorbalance(bNodeTree *UNUSED(ntree), bNode *nod node->storage = n; } +static int node_composite_gpu_colorbalance(GPUMaterial *mat, + bNode *node, + bNodeExecData *UNUSED(execdata), + GPUNodeStack *in, + GPUNodeStack *out) +{ + NodeColorBalance *n = (NodeColorBalance *)node->storage; + + if (node->custom1 == 0) { + return GPU_stack_link(mat, + node, + "node_composite_color_balance_lgg", + in, + out, + GPU_uniform(n->lift), + GPU_uniform(n->gamma), + GPU_uniform(n->gain)); + } + + return GPU_stack_link(mat, + node, + "node_composite_color_balance_asc_cdl", + in, + out, + GPU_uniform(n->offset), + GPU_uniform(n->power), + GPU_uniform(n->slope), + GPU_uniform(&n->offset_basis)); +} + void register_node_type_cmp_colorbalance(void) { static bNodeType ntype; @@ -90,6 +120,7 @@ void register_node_type_cmp_colorbalance(void) node_type_init(&ntype, node_composit_init_colorbalance); node_type_storage( &ntype, "NodeColorBalance", node_free_standard_storage, node_copy_standard_storage); + node_type_gpu(&ntype, node_composite_gpu_colorbalance); nodeRegisterType(&ntype); } _______________________________________________ Bf-blender-cvs mailing list Bf-blender-cvs@blender.org List details, subscription details or unsubscribe: https://lists.blender.org/mailman/listinfo/bf-blender-cvs