Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds background compositing field back in and fixes rendering pipeline. #95

Merged
merged 2 commits into from
Dec 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions cmake/ObsPluginHelpers.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,7 @@ if(OS_POSIX)
-Wswitch
-Wunused-parameter
-Wno-unused-function
-Wno-unused-command-line-argument
-Wno-missing-field-initializers
-fno-strict-aliasing
"$<$<COMPILE_LANGUAGE:C>:-Werror-implicit-function-declaration;-Wno-missing-braces>"
Expand Down
40 changes: 40 additions & 0 deletions data/shaders/composite.effect
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
uniform float4x4 ViewProj;
uniform texture2d image;
uniform texture2d background;

sampler_state textureSampler{
Filter = Linear;
AddressU = Clamp;
AddressV = Clamp;
MinLOD = 0;
MaxLOD = 0;
};

struct VertData {
float4 pos : POSITION;
float2 uv : TEXCOORD0;
};

VertData mainTransform(VertData v_in)
{
v_in.pos = mul(float4(v_in.pos.xyz, 1.0), ViewProj);
return v_in;
}

float4 mainImage(VertData v_in) : TARGET
{
float4 img_col = image.Sample(textureSampler, v_in.uv);
float4 background_col = background.Sample(textureSampler, v_in.uv);
float a = img_col.a;
float3 color = img_col.rgb * a + background_col.rgb * (1.0 - a);
return float4(color.rgb, a);
}

technique Draw
{
pass
{
vertex_shader = mainTransform(v_in);
pixel_shader = mainImage(v_in);
}
}
38 changes: 37 additions & 1 deletion data/shaders/gaussian_1d.effect
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

uniform float4x4 ViewProj;
uniform texture2d image;
uniform texture2d background;

uniform float2 uv_size;
uniform float2 texel_step;
Expand Down Expand Up @@ -55,11 +56,46 @@ float4 mainImage(VertData v_in) : TARGET
return col;
}

// An in-progress version of background compositing that should be more accurate,
// but is currently causing some artifacting along the edges of the source.
float4 mainImageComposite(VertData v_in) : TARGET
{
// DO THE BLUR
// 1. Sample incoming pixel, multiply by weight[0]
float4 c = image.Sample(textureSampler, v_in.uv);
float4 bg_col = background.Sample(textureSampler, v_in.uv);
float4 col = float4(c.rgb * c.a + bg_col.rgb * (1.0 - c.a), c.a) * weightLookup(0);
float total_weight = weightLookup(0);

// 2. March out from incoming pixel, multiply by corresponding weight.
for (uint i = 1; i < kernel_size; i++)
{
float weight = weightLookup(i);
float offset = offsetLookup(i);
total_weight += 2.0 * weight;
c = image.Sample(textureSampler, v_in.uv + (offset * texel_step));
col += float4(c.rgb * c.a + bg_col.rgb * (1.0 - c.a), c.a) * weight;
c = image.Sample(textureSampler, v_in.uv - (offset * texel_step));
col += float4(c.rgb * c.a + bg_col.rgb * (1.0 - c.a), c.a) * weight;
}
col /= total_weight;
return col;
}

technique Draw
{
pass
{
vertex_shader = mainTransform(v_in);
pixel_shader = mainImage(v_in);
}
}
}

technique DrawComposite
{
pass
{
vertex_shader = mainTransform(v_in);
pixel_shader = mainImageComposite(v_in);
}
}
2 changes: 1 addition & 1 deletion data/shaders/mix.effect
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,4 @@ technique Draw
vertex_shader = mainTransform(v_in);
pixel_shader = mainImage(v_in);
}
}
}
1 change: 1 addition & 0 deletions data/shaders/render_output.effect
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ float3 srgb_nonlinear_to_linear(float3 v)
float4 mainImage(VertData v_in) : TARGET
{
float4 px = output_image.Sample(textureSampler, v_in.uv);
//px.xyz = px.a > 0.0 ? px.xyz : float3(0.0f, 0.0f, 0.0f);
px.xyz = srgb_nonlinear_to_linear(px.xyz);
return px;
}
Expand Down
66 changes: 39 additions & 27 deletions src/blur/box.c
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ static void box_area_blur(composite_blur_filter_data_t *data)
return;
}

texture = blend_composite(texture, data);
const int passes = data->passes < 1 ? 1 : data->passes;
for (int i = 0; i < passes; i++) {
// 1. First pass- apply 1D blur kernel to horizontal dir.
Expand All @@ -88,14 +89,14 @@ static void box_area_blur(composite_blur_filter_data_t *data)
gs_effect_get_param_by_name(effect, "image");
gs_effect_set_texture(image, texture);

if(data->param_radius) {
if (data->param_radius) {
gs_effect_set_float(data->param_radius, data->radius);
}

struct vec2 texel_step;
texel_step.x = 1.0f / data->width;
texel_step.y = 0.0f;
if(data->param_texel_step) {
if (data->param_texel_step) {
gs_effect_set_vec2(data->param_texel_step, &texel_step);
}

Expand All @@ -120,7 +121,7 @@ static void box_area_blur(composite_blur_filter_data_t *data)

texel_step.x = 0.0f;
texel_step.y = 1.0f / data->height;
if(data->param_texel_step) {
if (data->param_texel_step) {
gs_effect_set_vec2(data->param_texel_step, &texel_step);
}

Expand Down Expand Up @@ -161,6 +162,8 @@ static void box_directional_blur(composite_blur_filter_data_t *data)
return;
}

texture = blend_composite(texture, data);

for (int i = 0; i < data->passes; i++) {
gs_texrender_t *tmp = data->render2;
data->render2 = data->output_texrender;
Expand All @@ -171,15 +174,15 @@ static void box_directional_blur(composite_blur_filter_data_t *data)
gs_effect_get_param_by_name(effect, "image");
gs_effect_set_texture(image, texture);

if(data->param_radius) {
if (data->param_radius) {
gs_effect_set_float(data->param_radius, data->radius);
}

struct vec2 texel_step;
float rads = -data->angle * ((float)M_PI / 180.0f);
texel_step.x = (float)cos(rads) / data->width;
texel_step.y = (float)sin(rads) / data->height;
if(data->param_texel_step) {
if (data->param_texel_step) {
gs_effect_set_vec2(data->param_texel_step, &texel_step);
}

Expand Down Expand Up @@ -223,6 +226,8 @@ static void box_zoom_blur(composite_blur_filter_data_t *data)
return;
}

texture = blend_composite(texture, data);

for (int i = 0; i < data->passes; i++) {
gs_texrender_t *tmp = data->render2;
data->render2 = data->output_texrender;
Expand All @@ -233,21 +238,22 @@ static void box_zoom_blur(composite_blur_filter_data_t *data)
gs_effect_get_param_by_name(effect, "image");
gs_effect_set_texture(image, texture);

if(data->param_radius) {
if (data->param_radius) {
gs_effect_set_float(data->param_radius, data->radius);
}

struct vec2 radial_center;
radial_center.x = data->center_x;
radial_center.y = data->center_y;
if(data->param_radial_center) {
gs_effect_set_vec2(data->param_radial_center, &radial_center);
if (data->param_radial_center) {
gs_effect_set_vec2(data->param_radial_center,
&radial_center);
}

struct vec2 uv_size;
uv_size.x = (float)data->width;
uv_size.y = (float)data->height;
if(data->param_uv_size) {
if (data->param_uv_size) {
gs_effect_set_vec2(data->param_uv_size, &uv_size);
}

Expand Down Expand Up @@ -290,46 +296,52 @@ static void box_tilt_shift_blur(composite_blur_filter_data_t *data)
return;
}

texture = blend_composite(texture, data);

for (int i = 0; i < data->passes; i++) {
// 1. First pass- apply 1D blur kernel to horizontal dir.
data->render2 = create_or_reset_texrender(data->render2);

gs_eparam_t *image =
gs_effect_get_param_by_name(effect, "image");
gs_effect_set_texture(image, texture);

if(data->param_radius) {
gs_effect_set_float(data->param_radius, (float)data->radius);

if (data->param_radius) {
gs_effect_set_float(data->param_radius,
(float)data->radius);
}

const float focus_center =
1.0f - (float)data->tilt_shift_center;
if(data->param_focus_center) {
gs_effect_set_float(data->param_focus_center, focus_center);
if (data->param_focus_center) {
gs_effect_set_float(data->param_focus_center,
focus_center);
}

const float focus_width = (float)data->tilt_shift_width / 2.0f;
if(data->param_focus_width) {
gs_effect_set_float(data->param_focus_width, focus_width);
if (data->param_focus_width) {
gs_effect_set_float(data->param_focus_width,
focus_width);
}

const float focus_angle =
(float)data->tilt_shift_angle * (M_PI / 180.0f);
if(data->param_focus_angle) {
gs_effect_set_float(data->param_focus_angle, focus_angle);
if (data->param_focus_angle) {
gs_effect_set_float(data->param_focus_angle,
focus_angle);
}

struct vec2 texel_step;
texel_step.x = 1.0f / data->width;
texel_step.y = 0.0f;
if(data->param_texel_step) {
if (data->param_texel_step) {
gs_effect_set_vec2(data->param_texel_step, &texel_step);
}

struct vec2 size;
size.x = (float)data->width;
size.y = (float)data->height;
if(data->param_uv_size) {
if (data->param_uv_size) {
gs_effect_set_vec2(data->param_uv_size, &size);
}

Expand All @@ -354,7 +366,7 @@ static void box_tilt_shift_blur(composite_blur_filter_data_t *data)

texel_step.x = 0.0f;
texel_step.y = 1.0f / data->height;
if(data->param_texel_step) {
if (data->param_texel_step) {
gs_effect_set_vec2(data->param_texel_step, &texel_step);
}

Expand Down Expand Up @@ -389,7 +401,7 @@ static void load_1d_box_effect(composite_blur_filter_data_t *filter)
gs_effect_get_param_info(param, &info);
if (strcmp(info.name, "texel_step") == 0) {
filter->param_texel_step = param;
} else if(strcmp(info.name, "radius") == 0) {
} else if (strcmp(info.name, "radius") == 0) {
filter->param_radius = param;
}
}
Expand All @@ -412,13 +424,13 @@ static void load_tiltshift_box_effect(composite_blur_filter_data_t *filter)
filter->param_uv_size = param;
} else if (strcmp(info.name, "texel_step") == 0) {
filter->param_texel_step = param;
} else if(strcmp(info.name, "radius") == 0) {
} else if (strcmp(info.name, "radius") == 0) {
filter->param_radius = param;
} else if (strcmp(info.name, "focus_center") == 0) {
filter->param_focus_center = param;
} else if(strcmp(info.name, "focus_width") == 0) {
} else if (strcmp(info.name, "focus_width") == 0) {
filter->param_focus_width = param;
} else if(strcmp(info.name, "focus_angle") == 0) {
} else if (strcmp(info.name, "focus_angle") == 0) {
filter->param_focus_angle = param;
}
}
Expand All @@ -439,9 +451,9 @@ static void load_radial_box_effect(composite_blur_filter_data_t *filter)
gs_effect_get_param_info(param, &info);
if (strcmp(info.name, "uv_size") == 0) {
filter->param_uv_size = param;
} else if(strcmp(info.name, "radius") == 0) {
} else if (strcmp(info.name, "radius") == 0) {
filter->param_radius = param;
} else if(strcmp(info.name, "radial_center") == 0) {
} else if (strcmp(info.name, "radial_center") == 0) {
filter->param_radial_center = param;
}
}
Expand Down
2 changes: 2 additions & 0 deletions src/blur/dual_kawase.c
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,8 @@ static void dual_kawase_blur(composite_blur_filter_data_t *data)
if (!effect_down || !effect_up || !texture) {
return;
}

texture = blend_composite(texture, data);
set_blending_parameters();

int last_pass = 1;
Expand Down
Loading