Skip to content

Commit

Permalink
Switch over to new rendering pipeline that does proper color space, c…
Browse files Browse the repository at this point in the history
…orrects pre-multiplied alpha, and handles blending properly.
  • Loading branch information
FiniteSingularity committed Dec 16, 2023
1 parent 0536324 commit 91704f3
Show file tree
Hide file tree
Showing 13 changed files with 243 additions and 56 deletions.
12 changes: 3 additions & 9 deletions data/shaders/circle-mask.effect
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ float4 alphaImage(VertData v_in) : TARGET
float d = SDF(dist, radius);
d = feather_amount > 0.0f ? smoothstep(0.0f, feather_amount, -d + feather_amount) : saturate(-d);

float4 color = pmrgba_to_rgba(image.Sample(textureSampler, (mask_position + dist / zoom / global_scale) / uv_size));
float4 color = image.Sample(textureSampler, (mask_position + dist / zoom / global_scale) / uv_size);
return float4(color.rgb, color.a * d);
}

Expand All @@ -71,24 +71,18 @@ float4 alphaFrameCheckImage(VertData v_in) : TARGET
sample_uv.y >= 0.0f && sample_uv.y <= 1.0f ? 1.0f : 0.0f;
d = feather_amount > 0.0f ? smoothstep(alpha_zero * alpha_scale, feather_amount, -d + feather_amount) : clamp(-d, alpha_zero * alpha_scale, 1.0f);

float4 color = pmrgba_to_rgba(image.Sample(textureSampler, (mask_position + dist / zoom / global_scale) / uv_size));
float4 color = image.Sample(textureSampler, (mask_position + dist / zoom / global_scale) / uv_size);
return float4(color.rgb, color.a * d);
}

float4 adjustmentsImage(VertData v_in) : TARGET
{
//float2 coord = v_in.uv * uv_size;
//float2 dist = coord - mask_position;
//float2 sample_dist = float2(dist.x * cos_theta - dist.y * sin_theta, dist.x * sin_theta + dist.y * cos_theta);
//float d = SDF(sample_dist, float2(width, height), corner_radius) + feather_shift;
//d = feather_amount > 0.0f ? smoothstep(0.0f, feather_amount, -d) : saturate(-d);

float2 coord = v_in.uv * uv_size;
float2 dist = coord - mask_position;
float d = SDF(dist, radius);
d = feather_amount > 0.0f ? smoothstep(0.0f, feather_amount, -d + feather_amount) : saturate(-d);

float4 color = pmrgba_to_rgba(image.Sample(textureSampler, v_in.uv));
float4 color = image.Sample(textureSampler, v_in.uv);
float4 color_adj_min = adjustments(
color,
min_brightness,
Expand Down
6 changes: 3 additions & 3 deletions data/shaders/ellipse-mask.effect
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ float4 alphaImage(VertData v_in) : TARGET
float d = SDF(sample_dist, ellipse);
d = feather_amount > 0.0f ? smoothstep(0.0f, feather_amount, -d + feather_amount) : saturate(-d);

float4 color = pmrgba_to_rgba(image.Sample(textureSampler, (mask_position + dist / zoom / global_scale) / uv_size));
float4 color = image.Sample(textureSampler, (mask_position + dist / zoom / global_scale) / uv_size);
return float4(color.rgb, color.a * d);
}

Expand All @@ -112,7 +112,7 @@ float4 alphaFrameCheckImage(VertData v_in) : TARGET
sample_uv.y >= 0.0f && sample_uv.y <= 1.0f ? 1.0f : 0.0f;
d = feather_amount > 0.0f ? smoothstep(alpha_zero * alpha_scale, feather_amount, -d + feather_amount) : clamp(-d, alpha_zero * alpha_scale, 1.0f);

float4 color = pmrgba_to_rgba(image.Sample(textureSampler, (mask_position + dist / zoom / global_scale) / uv_size));
float4 color = image.Sample(textureSampler, (mask_position + dist / zoom / global_scale) / uv_size);
return float4(color.rgb, color.a * d);
}

Expand All @@ -125,7 +125,7 @@ float4 adjustmentsImage(VertData v_in) : TARGET
float d = SDF(sample_dist, ellipse);
d = feather_amount > 0.0f ? smoothstep(0.0f, feather_amount, -d + feather_amount) : saturate(-d);

float4 color = pmrgba_to_rgba(image.Sample(textureSampler, v_in.uv));
float4 color = image.Sample(textureSampler, v_in.uv);
float4 color_adj_min = adjustments(
color,
min_brightness,
Expand Down
8 changes: 4 additions & 4 deletions data/shaders/gradient-mask.effect
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ float4 mainAlphaImage(VertData v_in) : TARGET
);

float alpha = saturate((coord_p.x - position + width / 2.0f) / width);
float4 col = pmrgba_to_rgba(image.Sample(textureSampler, v_in.uv));
float4 col = image.Sample(textureSampler, v_in.uv);
return float4(col.rgb, col.a * (invert ? 1.0f - alpha : alpha));
}

Expand All @@ -70,7 +70,7 @@ float4 mainAdjustmentsImage(VertData v_in) : TARGET

float scale = saturate((coord_p.x - position + width / 2.0f) / width);
scale = invert ? 1.0f - scale : scale;
float4 color = pmrgba_to_rgba(image.Sample(textureSampler, v_in.uv));
float4 color = image.Sample(textureSampler, v_in.uv);

color = adjustments(
color,
Expand Down Expand Up @@ -98,7 +98,7 @@ float4 debugAlphaImage(VertData v_in) : TARGET

float alpha = saturate((coord_p.x - position + width / 2.0f) / width);

float4 color = pmrgba_to_rgba(image.Sample(textureSampler, v_in.uv));
float4 color = image.Sample(textureSampler, v_in.uv);
if (coord_p.x - position > -(width / 2.0f + 2.0f) && coord_p.x - position < -(width / 2.0f))
{
return float4(0.0f, 1.0f, 0.0f, 1.0f);
Expand Down Expand Up @@ -131,7 +131,7 @@ float4 debugAdjustmentsImage(VertData v_in) : TARGET

float scale = saturate((coord_p.x - position + width / 2.0f) / width);
scale = invert ? 1.0f - scale : scale;
float4 color = pmrgba_to_rgba(image.Sample(textureSampler, v_in.uv));
float4 color = image.Sample(textureSampler, v_in.uv);
if (coord_p.x - position > -(width / 2.0f + 2.0f) && coord_p.x - position < -(width / 2.0f))
{
return float4(0.0f, 1.0f, 0.0f, 1.0f);
Expand Down
6 changes: 3 additions & 3 deletions data/shaders/heart-mask.effect
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ float4 alphaImage(VertData v_in) : TARGET
float d = SDF(sample_dist) - corner_radius;
d = feather_amount > 0.0f ? smoothstep(0.0f, feather_amount, -d + feather_amount) : saturate(-d);

float4 color = pmrgba_to_rgba(image.Sample(textureSampler, (mask_position + dist / zoom / global_scale) / uv_size));
float4 color = image.Sample(textureSampler, (mask_position + dist / zoom / global_scale) / uv_size);
return float4(color.rgb, color.a * d);
}

Expand All @@ -90,7 +90,7 @@ float4 alphaFrameCheckImage(VertData v_in) : TARGET
sample_uv.y >= 0.0f && sample_uv.y <= 1.0f ? 1.0f : 0.0f;
d = feather_amount > 0.0f ? smoothstep(alpha_zero * alpha_scale, feather_amount, -d + feather_amount) : clamp(-d, alpha_zero * alpha_scale, 1.0f);

float4 color = pmrgba_to_rgba(image.Sample(textureSampler, (mask_position + dist / zoom / global_scale) / uv_size));
float4 color = image.Sample(textureSampler, (mask_position + dist / zoom / global_scale) / uv_size);
return float4(color.rgb, color.a * d);
}

Expand All @@ -102,7 +102,7 @@ float4 adjustmentsImage(VertData v_in) : TARGET
float d = SDF(sample_dist) - corner_radius;
d = feather_amount > 0.0f ? smoothstep(0.0f, feather_amount, -d + feather_amount) : saturate(-d);

float4 color = pmrgba_to_rgba(image.Sample(textureSampler, v_in.uv));
float4 color = image.Sample(textureSampler, v_in.uv);
float4 color_adj_min = adjustments(
color,
min_brightness,
Expand Down
6 changes: 3 additions & 3 deletions data/shaders/polygon-mask.effect
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ float4 alphaImage(VertData v_in) : TARGET
float d = SDF(sample_dist, num_sides, radius) - corner_radius;
d = feather_amount > 0.0f ? smoothstep(0.0f, feather_amount, -d + feather_amount) : saturate(-d);

float4 color = pmrgba_to_rgba(image.Sample(textureSampler, (mask_position + dist / zoom / global_scale) / uv_size));
float4 color = image.Sample(textureSampler, (mask_position + dist / zoom / global_scale) / uv_size);
return float4(color.rgb, color.a * d);
}

Expand All @@ -85,7 +85,7 @@ float4 alphaFrameCheckImage(VertData v_in) : TARGET
sample_uv.y >= 0.0f && sample_uv.y <= 1.0f ? 1.0f : 0.0f;
d = feather_amount > 0.0f ? smoothstep(alpha_zero * alpha_scale, feather_amount, -d + feather_amount) : clamp(-d, alpha_zero * alpha_scale, 1.0f);

float4 color = pmrgba_to_rgba(image.Sample(textureSampler, (mask_position + dist / zoom / global_scale) / uv_size));
float4 color = image.Sample(textureSampler, (mask_position + dist / zoom / global_scale) / uv_size);
return float4(color.rgb, color.a * d);
}

Expand All @@ -96,7 +96,7 @@ float4 adjustmentsImage(VertData v_in) : TARGET
float2 sample_dist = float2(dist.x * cos_rot - dist.y * sin_rot, dist.x * sin_rot + dist.y * cos_rot);
float d = SDF(sample_dist, num_sides, radius) - corner_radius;
d = feather_amount > 0.0f ? smoothstep(0.0f, feather_amount, -d + feather_amount) : saturate(-d);
float4 color = pmrgba_to_rgba(image.Sample(textureSampler, v_in.uv));
float4 color = image.Sample(textureSampler, v_in.uv);
float4 color_adj_min = adjustments(
color,
min_brightness,
Expand Down
6 changes: 3 additions & 3 deletions data/shaders/rectangular-mask.effect
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ float4 alphaImage(VertData v_in) : TARGET
float d = SDF(sample_dist, float2(width, height), corner_radius);
d = feather_amount > 0.0f ? smoothstep(0.0f, feather_amount, -d + feather_amount) : saturate(-d);

float4 color = pmrgba_to_rgba(image.Sample(textureSampler, (mask_position + dist / zoom / global_scale) / uv_size));
float4 color = image.Sample(textureSampler, (mask_position + dist / zoom / global_scale) / uv_size);
return float4(color.rgb, color.a * d);
}

Expand All @@ -92,7 +92,7 @@ float4 alphaFrameCheckImage(VertData v_in) : TARGET
sample_uv.y >= 0.0f && sample_uv.y <= 1.0f ? 1.0f : 0.0f;
d = feather_amount > 0.0f ? smoothstep(alpha_zero * alpha_scale, feather_amount, -d + feather_amount) : clamp(-d, alpha_zero * alpha_scale, 1.0f);

float4 color = pmrgba_to_rgba(image.Sample(textureSampler, (mask_position + dist / zoom / global_scale) / uv_size));
float4 color = image.Sample(textureSampler, (mask_position + dist / zoom / global_scale) / uv_size);
return float4(color.rgb, color.a * d);
}

Expand All @@ -103,7 +103,7 @@ float4 adjustmentsImage(VertData v_in) : TARGET
float2 sample_dist = float2(dist.x * cos_theta - dist.y * sin_theta, dist.x * sin_theta + dist.y * cos_theta);
float d = SDF(sample_dist, float2(width, height), corner_radius) + feather_shift;
d = feather_amount > 0.0f ? smoothstep(0.0f, feather_amount, -d) : saturate(-d);
float4 color = pmrgba_to_rgba(image.Sample(textureSampler, v_in.uv));
float4 color = image.Sample(textureSampler, v_in.uv);
float4 color_adj_min = adjustments(
color,
min_brightness,
Expand Down
49 changes: 49 additions & 0 deletions data/shaders/render_output.effect
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
uniform float4x4 ViewProj;
uniform texture2d image;
uniform texture2d output_image;

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;
}

float srgb_nonlinear_to_linear_channel(float u)
{
return (u <= 0.04045) ? (u / 12.92) : pow((u + 0.055) / 1.055, 2.4);
}

float3 srgb_nonlinear_to_linear(float3 v)
{
return float3(srgb_nonlinear_to_linear_channel(v.r), srgb_nonlinear_to_linear_channel(v.g), srgb_nonlinear_to_linear_channel(v.b));
}

float4 mainImage(VertData v_in) : TARGET
{
float4 px = output_image.Sample(textureSampler, v_in.uv);
px.xyz = srgb_nonlinear_to_linear(px.xyz);
return px;
}

technique Draw
{
pass
{
vertex_shader = mainTransform(v_in);
pixel_shader = mainImage(v_in);
}
}
24 changes: 12 additions & 12 deletions data/shaders/source-mask.effect
Original file line number Diff line number Diff line change
Expand Up @@ -43,18 +43,18 @@ VertData mainTransform(VertData v_in)

float4 alphaImage(VertData v_in) : TARGET
{
float4 mask = pmrgba_to_rgba(source_image.Sample(textureSampler, v_in.uv));
float4 mask = source_image.Sample(textureSampler, v_in.uv);
float alpha = multiplier * (mask.r * channel_multipliers.r + mask.g * channel_multipliers.g + mask.b * channel_multipliers.b + mask.a * channel_multipliers.a);
float4 color = pmrgba_to_rgba(image.Sample(textureSampler, v_in.uv));
float4 color = image.Sample(textureSampler, v_in.uv);
return float4(color.rgb, color.a * (invert ? 1.0 - alpha : alpha));
}

float4 adjustmentsImage(VertData v_in) : TARGET
{
float4 mask = pmrgba_to_rgba(source_image.Sample(textureSampler, v_in.uv));
float4 mask = source_image.Sample(textureSampler, v_in.uv);
float scale = multiplier * (mask.r * channel_multipliers.r + mask.g * channel_multipliers.g + mask.b * channel_multipliers.b + mask.a * channel_multipliers.a);
scale = invert ? 1.0 - scale : scale;
float4 color = pmrgba_to_rgba(image.Sample(textureSampler, v_in.uv));
float4 color = image.Sample(textureSampler, v_in.uv);
color = adjustments(
color,
lerp(min_brightness, max_brightness, scale),
Expand All @@ -67,20 +67,20 @@ float4 adjustmentsImage(VertData v_in) : TARGET

float4 alphaThresholdImage(VertData v_in) : TARGET
{
float4 mask = pmrgba_to_rgba(source_image.Sample(textureSampler, v_in.uv));
float4 mask = source_image.Sample(textureSampler, v_in.uv);
float alpha = multiplier * (mask.r * channel_multipliers.r + mask.g * channel_multipliers.g + mask.b * channel_multipliers.b + mask.a * channel_multipliers.a);
alpha = step(threshold_value, alpha);
float4 color = pmrgba_to_rgba(image.Sample(textureSampler, v_in.uv));
float4 color = image.Sample(textureSampler, v_in.uv);
return float4(color.rgb, color.a * (invert ? 1.0 - alpha : alpha));
}

float4 adjustmentsThresholdImage(VertData v_in) : TARGET
{
float4 mask = pmrgba_to_rgba(source_image.Sample(textureSampler, v_in.uv));
float4 mask = source_image.Sample(textureSampler, v_in.uv);
float scale = multiplier * (mask.r * channel_multipliers.r + mask.g * channel_multipliers.g + mask.b * channel_multipliers.b + mask.a * channel_multipliers.a);
scale = step(threshold_value, scale);
scale = invert ? 1.0 - scale : scale;
float4 color = pmrgba_to_rgba(image.Sample(textureSampler, v_in.uv));
float4 color = image.Sample(textureSampler, v_in.uv);
color = adjustments(
color,
lerp(min_brightness, max_brightness, scale),
Expand All @@ -93,20 +93,20 @@ float4 adjustmentsThresholdImage(VertData v_in) : TARGET

float4 alphaRangeImage(VertData v_in) : TARGET
{
float4 mask = pmrgba_to_rgba(source_image.Sample(textureSampler, v_in.uv));
float4 mask = source_image.Sample(textureSampler, v_in.uv);
float alpha = multiplier * (mask.r * channel_multipliers.r + mask.g * channel_multipliers.g + mask.b * channel_multipliers.b + mask.a * channel_multipliers.a);
alpha = smoothstep(range_min, range_max, alpha);
float4 color = pmrgba_to_rgba(image.Sample(textureSampler, v_in.uv));
float4 color = image.Sample(textureSampler, v_in.uv);
return float4(color.rgb, color.a * (invert ? 1.0 - alpha : alpha));
}

float4 adjustmentsRangeImage(VertData v_in) : TARGET
{
float4 mask = pmrgba_to_rgba(source_image.Sample(textureSampler, v_in.uv));
float4 mask = source_image.Sample(textureSampler, v_in.uv);
float scale = multiplier * (mask.r * channel_multipliers.r + mask.g * channel_multipliers.g + mask.b * channel_multipliers.b + mask.a * channel_multipliers.a);
scale = smoothstep(range_min, range_max, scale);
scale = invert ? 1.0 - scale : scale;
float4 color = pmrgba_to_rgba(image.Sample(textureSampler, v_in.uv));
float4 color = image.Sample(textureSampler, v_in.uv);
color = adjustments(
color,
lerp(min_brightness, max_brightness, scale),
Expand Down
6 changes: 3 additions & 3 deletions data/shaders/star-mask.effect
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ float4 alphaImage(VertData v_in) : TARGET
float d = SDF(sample_dist, radius) - corner_radius;
d = feather_amount > 0.0f ? smoothstep(0.0f, feather_amount, -d + feather_amount) : saturate(-d);

float4 color = pmrgba_to_rgba(image.Sample(textureSampler, (mask_position + dist / zoom / global_scale) / uv_size));
float4 color = image.Sample(textureSampler, (mask_position + dist / zoom / global_scale) / uv_size);
return float4(color.rgb, color.a * d);
}

Expand All @@ -91,7 +91,7 @@ float4 alphaFrameCheckImage(VertData v_in) : TARGET
sample_uv.y >= 0.0f && sample_uv.y <= 1.0f ? 1.0f : 0.0f;
d = feather_amount > 0.0f ? smoothstep(alpha_zero * alpha_scale, feather_amount, -d + feather_amount) : clamp(-d, alpha_zero * alpha_scale, 1.0f);

float4 color = pmrgba_to_rgba(image.Sample(textureSampler, (mask_position + dist / zoom / global_scale) / uv_size));
float4 color = image.Sample(textureSampler, (mask_position + dist / zoom / global_scale) / uv_size);
return float4(color.rgb, color.a * d);
}

Expand All @@ -103,7 +103,7 @@ float4 adjustmentsImage(VertData v_in) : TARGET
float d = SDF(dist, radius) - corner_radius;
d = feather_amount > 0.0f ? smoothstep(0.0f, feather_amount, -d + feather_amount) : saturate(-d);

float4 color = pmrgba_to_rgba(image.Sample(textureSampler, v_in.uv));
float4 color = image.Sample(textureSampler, v_in.uv);
float4 color_adj_min = adjustments(
color,
min_brightness,
Expand Down
Loading

0 comments on commit 91704f3

Please sign in to comment.