libobs: Filter using premultiplied alpha

Convert incoming straight alpha color to premultiplied at filter begin,
and process premultiplied value at filter end.

If direct rendering is allowed, we assume the input source outputs
premultiiplied alpha. If not, that source will need to be updated.
This commit is contained in:
jpark37 2021-04-24 17:32:38 -07:00
parent 840e2b3d43
commit 50dfa566e6

View File

@ -3701,10 +3701,11 @@ bool obs_source_process_filter_begin(obs_source_t *filter,
filter->filter_texrender = filter->filter_texrender =
gs_texrender_create(format, GS_ZS_NONE); gs_texrender_create(format, GS_ZS_NONE);
gs_blend_state_push();
gs_blend_function(GS_BLEND_ONE, GS_BLEND_ZERO);
if (gs_texrender_begin(filter->filter_texrender, cx, cy)) { if (gs_texrender_begin(filter->filter_texrender, cx, cy)) {
gs_blend_state_push();
gs_blend_function_separate(GS_BLEND_SRCALPHA, GS_BLEND_ZERO,
GS_BLEND_ONE, GS_BLEND_ZERO);
bool custom_draw = (parent_flags & OBS_SOURCE_CUSTOM_DRAW) != 0; bool custom_draw = (parent_flags & OBS_SOURCE_CUSTOM_DRAW) != 0;
bool async = (parent_flags & OBS_SOURCE_ASYNC) != 0; bool async = (parent_flags & OBS_SOURCE_ASYNC) != 0;
struct vec4 clear_color; struct vec4 clear_color;
@ -3718,10 +3719,10 @@ bool obs_source_process_filter_begin(obs_source_t *filter,
else else
obs_source_video_render(target); obs_source_video_render(target);
gs_blend_state_pop();
gs_texrender_end(filter->filter_texrender); gs_texrender_end(filter->filter_texrender);
} }
gs_blend_state_pop();
return true; return true;
} }
@ -3748,6 +3749,9 @@ static void obs_source_process_filter_tech_end_internal(
const char *tech = tech_name ? tech_name : "Draw"; const char *tech = tech_name ? tech_name : "Draw";
gs_blend_state_push();
gs_blend_function(GS_BLEND_ONE, GS_BLEND_INVSRCALPHA);
if (can_bypass(target, parent, parent_flags, filter->allow_direct)) { if (can_bypass(target, parent, parent_flags, filter->allow_direct)) {
render_filter_bypass(target, effect, tech); render_filter_bypass(target, effect, tech);
} else { } else {
@ -3757,6 +3761,8 @@ static void obs_source_process_filter_tech_end_internal(
} }
} }
gs_blend_state_pop();
gs_set_linear_srgb(previous); gs_set_linear_srgb(previous);
} }