From cc452e5edafd331224c0d50c5dbecd542750cee3 Mon Sep 17 00:00:00 2001 From: jpark37 Date: Tue, 10 Sep 2019 20:07:17 -0700 Subject: [PATCH] image-source: Support linear SRGB Both image and color sources have been updated. Also added alpha support to the color source. Useful for users, and serves as an easy alpha blending test case. --- plugins/image-source/color-source.c | 42 ++++++++++++++++++++--------- plugins/image-source/image-source.c | 15 +++++++++-- 2 files changed, 43 insertions(+), 14 deletions(-) diff --git a/plugins/image-source/color-source.c b/plugins/image-source/color-source.c index b2e087470..26bb477ef 100644 --- a/plugins/image-source/color-source.c +++ b/plugins/image-source/color-source.c @@ -1,7 +1,8 @@ #include struct color_source { - uint32_t color; + struct vec4 color; + struct vec4 color_srgb; uint32_t width; uint32_t height; @@ -22,7 +23,8 @@ static void color_source_update(void *data, obs_data_t *settings) uint32_t width = (uint32_t)obs_data_get_int(settings, "width"); uint32_t height = (uint32_t)obs_data_get_int(settings, "height"); - context->color = color; + vec4_from_rgba(&context->color, color); + vec4_from_rgba_srgb(&context->color_srgb, color); context->width = width; context->height = height; } @@ -50,8 +52,8 @@ static obs_properties_t *color_source_properties(void *unused) obs_properties_t *props = obs_properties_create(); - obs_properties_add_color(props, "color", - obs_module_text("ColorSource.Color")); + obs_properties_add_color_alpha(props, "color", + obs_module_text("ColorSource.Color")); obs_properties_add_int(props, "width", obs_module_text("ColorSource.Width"), 0, 4096, @@ -64,19 +66,14 @@ static obs_properties_t *color_source_properties(void *unused) return props; } -static void color_source_render(void *data, gs_effect_t *effect) +static void color_source_render_helper(struct color_source *context, + struct vec4 *colorVal) { - UNUSED_PARAMETER(effect); - - struct color_source *context = data; - gs_effect_t *solid = obs_get_base_effect(OBS_EFFECT_SOLID); gs_eparam_t *color = gs_effect_get_param_by_name(solid, "color"); gs_technique_t *tech = gs_effect_get_technique(solid, "Solid"); - struct vec4 colorVal; - vec4_from_rgba(&colorVal, context->color); - gs_effect_set_vec4(color, &colorVal); + gs_effect_set_vec4(color, colorVal); gs_technique_begin(tech); gs_technique_begin_pass(tech, 0); @@ -87,6 +84,27 @@ static void color_source_render(void *data, gs_effect_t *effect) gs_technique_end(tech); } +static void color_source_render(void *data, gs_effect_t *effect) +{ + UNUSED_PARAMETER(effect); + + struct color_source *context = data; + + /* need linear path for correct alpha blending */ + const bool linear_srgb = gs_get_linear_srgb() || + (context->color.w < 1.0f); + + const bool previous = gs_framebuffer_srgb_enabled(); + gs_enable_framebuffer_srgb(linear_srgb); + + if (linear_srgb) + color_source_render_helper(context, &context->color_srgb); + else + color_source_render_helper(context, &context->color); + + gs_enable_framebuffer_srgb(previous); +} + static uint32_t color_source_getwidth(void *data) { struct color_source *context = data; diff --git a/plugins/image-source/image-source.c b/plugins/image-source/image-source.c index 425554ed5..03787d271 100644 --- a/plugins/image-source/image-source.c +++ b/plugins/image-source/image-source.c @@ -147,10 +147,21 @@ static void image_source_render(void *data, gs_effect_t *effect) if (!context->if2.image.texture) return; - gs_effect_set_texture(gs_effect_get_param_by_name(effect, "image"), - context->if2.image.texture); + const bool linear_srgb = gs_get_linear_srgb(); + + const bool previous = gs_framebuffer_srgb_enabled(); + gs_enable_framebuffer_srgb(linear_srgb); + + gs_eparam_t *const param = gs_effect_get_param_by_name(effect, "image"); + if (linear_srgb) + gs_effect_set_texture_srgb(param, context->if2.image.texture); + else + gs_effect_set_texture(param, context->if2.image.texture); + gs_draw_sprite(context->if2.image.texture, 0, context->if2.image.cx, context->if2.image.cy); + + gs_enable_framebuffer_srgb(previous); } static void image_source_tick(void *data, float seconds)