diff --git a/libobs/data/premultiplied_alpha.effect b/libobs/data/premultiplied_alpha.effect new file mode 100644 index 000000000..6abe8256c --- /dev/null +++ b/libobs/data/premultiplied_alpha.effect @@ -0,0 +1,38 @@ +uniform float4x4 ViewProj; +uniform texture2d image; + +sampler_state def_sampler { + Filter = Linear; + AddressU = Clamp; + AddressV = Clamp; +}; + +struct VertInOut { + float4 pos : POSITION; + float2 uv : TEXCOORD0; +}; + +VertInOut VSDefault(VertInOut vert_in) +{ + VertInOut vert_out; + vert_out.pos = mul(float4(vert_in.pos.xyz, 1.0), ViewProj); + vert_out.uv = vert_in.uv; + return vert_out; +} + +float4 PSDraw(VertInOut vert_in) : TARGET +{ + float4 rgba = image.Sample(def_sampler, vert_in.uv); + if (rgba.a > 0.0) + rgba.rgb /= rgba.a; + return saturate(rgba); +} + +technique Draw +{ + pass + { + vertex_shader = VSDefault(vert_in); + pixel_shader = PSDraw(vert_in); + } +} diff --git a/libobs/obs-internal.h b/libobs/obs-internal.h index a825d239e..e9df2f235 100644 --- a/libobs/obs-internal.h +++ b/libobs/obs-internal.h @@ -237,6 +237,7 @@ struct obs_core_video { gs_effect_t *bicubic_effect; gs_effect_t *lanczos_effect; gs_effect_t *bilinear_lowres_effect; + gs_effect_t *premultiplied_alpha_effect; gs_stagesurf_t *mapped_surface; int cur_texture; diff --git a/libobs/obs.c b/libobs/obs.c index 712a4a0e8..d3e8607da 100644 --- a/libobs/obs.c +++ b/libobs/obs.c @@ -291,6 +291,11 @@ static int obs_init_graphics(struct obs_video_info *ovi) NULL); bfree(filename); + filename = find_libobs_data_file("premultiplied_alpha.effect"); + video->premultiplied_alpha_effect = gs_effect_create_from_file(filename, + NULL); + bfree(filename); + obs->video.transparent_texture = gs_texture_create(2, 2, GS_RGBA, 1, &transparent_tex, 0); @@ -306,6 +311,8 @@ static int obs_init_graphics(struct obs_video_info *ovi) success = false; if (!video->conversion_effect) success = false; + if (!video->premultiplied_alpha_effect) + success = false; if (!video->transparent_texture) success = false; @@ -1351,6 +1358,8 @@ gs_effect_t *obs_get_base_effect(enum obs_base_effect effect) return obs->video.lanczos_effect; case OBS_EFFECT_BILINEAR_LOWRES: return obs->video.bilinear_lowres_effect; + case OBS_EFFECT_PREMULTIPLIED_ALPHA: + return obs->video.premultiplied_alpha_effect; } return NULL; diff --git a/libobs/obs.h b/libobs/obs.h index a17ead9d8..82aee5d12 100644 --- a/libobs/obs.h +++ b/libobs/obs.h @@ -521,6 +521,7 @@ enum obs_base_effect { OBS_EFFECT_BICUBIC, /**< Bicubic downscale */ OBS_EFFECT_LANCZOS, /**< Lanczos downscale */ OBS_EFFECT_BILINEAR_LOWRES, /**< Bilinear low resolution downscale */ + OBS_EFFECT_PREMULTIPLIED_ALPHA,/**< Premultiplied alpha */ }; /** Returns a commonly used base effect */