summaryrefslogtreecommitdiffstats
path: root/SharedFilteringLib.hlsl
diff options
context:
space:
mode:
authoryum <yum.food.vr@gmail.com>2025-06-13 17:59:00 -0700
committeryum <yum.food.vr@gmail.com>2025-06-13 17:59:00 -0700
commita9dbbfa1488a9ec3bd772fc9847888502f49a65e (patch)
tree2930432ee795faff97b81c6261c2160a37125048 /SharedFilteringLib.hlsl
parentf639117b1f743ab2941667b32b190dd1e715d4a5 (diff)
Fix some filamented inconsistencies
Diffstat (limited to 'SharedFilteringLib.hlsl')
-rw-r--r--SharedFilteringLib.hlsl47
1 files changed, 47 insertions, 0 deletions
diff --git a/SharedFilteringLib.hlsl b/SharedFilteringLib.hlsl
new file mode 100644
index 0000000..582adff
--- /dev/null
+++ b/SharedFilteringLib.hlsl
@@ -0,0 +1,47 @@
+#ifndef SERVICE_FILTERING_INCLUDED
+#define SERVICE_FILTERING_INCLUDED
+
+float4 cubic(float v)
+{
+ float4 n = float4(1.0, 2.0, 3.0, 4.0) - v;
+ float4 s = n * n * n;
+ float x = s.x;
+ float y = s.y - 4.0 * s.x;
+ float z = s.z - 4.0 * s.y + 6.0 * s.x;
+ float w = 6.0 - x - y - z;
+ return float4(x, y, z, w);
+}
+
+
+// Unity's SampleTexture2DBicubic doesn't exist in 2018, which is our target here.
+// So this is a similar function with tweaks to have similar semantics.
+
+float4 SampleTexture2DBicubicFilter(TEXTURE2D_PARAM(tex, smp), float2 coord, float4 texSize)
+{
+ coord = coord * texSize.xy - 0.5;
+ float fx = frac(coord.x);
+ float fy = frac(coord.y);
+ coord.x -= fx;
+ coord.y -= fy;
+
+ float4 xcubic = cubic(fx);
+ float4 ycubic = cubic(fy);
+
+ float4 c = float4(coord.x - 0.5, coord.x + 1.5, coord.y - 0.5, coord.y + 1.5);
+ float4 s = float4(xcubic.x + xcubic.y, xcubic.z + xcubic.w, ycubic.x + ycubic.y, ycubic.z + ycubic.w);
+ float4 offset = c + float4(xcubic.y, xcubic.w, ycubic.y, ycubic.w) / s;
+
+ float4 sample0 = SAMPLE_TEXTURE2D(tex, smp, float2(offset.x, offset.z) * texSize.zw);
+ float4 sample1 = SAMPLE_TEXTURE2D(tex, smp, float2(offset.y, offset.z) * texSize.zw);
+ float4 sample2 = SAMPLE_TEXTURE2D(tex, smp, float2(offset.x, offset.w) * texSize.zw);
+ float4 sample3 = SAMPLE_TEXTURE2D(tex, smp, float2(offset.y, offset.w) * texSize.zw);
+
+ float sx = s.x / (s.x + s.y);
+ float sy = s.z / (s.z + s.w);
+
+ return lerp(
+ lerp(sample3, sample2, sx),
+ lerp(sample1, sample0, sx), sy);
+}
+
+#endif // SERVICE_FILTERING_INCLUDED \ No newline at end of file