Skip to content

Instantly share code, notes, and snippets.

@kkhomyakov3d
Forked from TheRealMJP/Tex2DCatmullRom.hlsl
Created September 17, 2021 20:52
Show Gist options
  • Select an option

  • Save kkhomyakov3d/a1bb301039720a7b62da6c257bda9fe3 to your computer and use it in GitHub Desktop.

Select an option

Save kkhomyakov3d/a1bb301039720a7b62da6c257bda9fe3 to your computer and use it in GitHub Desktop.

Revisions

  1. @TheRealMJP TheRealMJP revised this gist Nov 8, 2019. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions Tex2DCatmullRom.hlsl
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,5 @@
    // The following code is licensed under the MIT license: https://gist.github.com/TheRealMJP/bc503b0b87b643d3505d41eab8b332ae

    // Samples a texture with Catmull-Rom filtering, using 9 texture fetches instead of 16.
    // See http://vec3.ca/bicubic-filtering-in-fewer-taps/ for more details
    float4 SampleTextureCatmullRom(in Texture2D<float4> tex, in SamplerState linearSampler, in float2 uv, in float2 texSize)
  2. @TheRealMJP TheRealMJP revised this gist Sep 22, 2016. 1 changed file with 4 additions and 6 deletions.
    10 changes: 4 additions & 6 deletions Tex2DCatmullRom.hlsl
    Original file line number Diff line number Diff line change
    @@ -11,16 +11,14 @@ float4 SampleTextureCatmullRom(in Texture2D<float4> tex, in SamplerState linearS
    // Compute the fractional offset from our starting texel to our original sample location, which we'll
    // feed into the Catmull-Rom spline function to get our filter weights.
    float2 f = samplePos - texPos1;
    float2 f2 = f * f;
    float2 f3 = f2 * f;

    // Compute the Catmull-Rom weights using the fractional offset that we calculated earlier.
    // These equations are pre-expanded based on our knowledge of where the texels will be located,
    // which lets us avoid having to evaluate a piece-wise function.
    float2 w0 = (1.0f / 6.0f) * (-3.0f * f3 + 6.0f * f2 - 3.0f * f);
    float2 w1 = (1.0f / 6.0f) * (9.0f * f3 - 15.0f * f2 + 6.0f);
    float2 w2 = (1.0f / 6.0f) * (-9.0f * f3 + 12.0f * f2 + 3.0f * f);
    float2 w3 = (1.0f / 6.0f) * (3.0f * f3 - 3.0f * f2);
    float2 w0 = f * (-0.5f + f * (1.0f - 0.5f * f));
    float2 w1 = 1.0f + f * f * (-2.5f + 1.5f * f);
    float2 w2 = f * (0.5f + f * (2.0f - 1.5f * f));
    float2 w3 = f * f * (-0.5f + 0.5f * f);

    // Work out weighting factors and sampling offsets that will let us use bilinear filtering to
    // simultaneously evaluate the middle 2 samples from the 4x4 grid.
  3. @TheRealMJP TheRealMJP revised this gist Sep 19, 2016. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion Tex2DCatmullRom.hlsl
    Original file line number Diff line number Diff line change
    @@ -49,5 +49,5 @@ float4 SampleTextureCatmullRom(in Texture2D<float4> tex, in SamplerState linearS
    result += tex.SampleLevel(linearSampler, float2(texPos12.x, texPos3.y), 0.0f) * w12.x * w3.y;
    result += tex.SampleLevel(linearSampler, float2(texPos3.x, texPos3.y), 0.0f) * w3.x * w3.y;

    return max(result, 0.0f);
    return result;
    }
  4. @TheRealMJP TheRealMJP created this gist Sep 19, 2016.
    53 changes: 53 additions & 0 deletions Tex2DCatmullRom.hlsl
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,53 @@
    // Samples a texture with Catmull-Rom filtering, using 9 texture fetches instead of 16.
    // See http://vec3.ca/bicubic-filtering-in-fewer-taps/ for more details
    float4 SampleTextureCatmullRom(in Texture2D<float4> tex, in SamplerState linearSampler, in float2 uv, in float2 texSize)
    {
    // We're going to sample a a 4x4 grid of texels surrounding the target UV coordinate. We'll do this by rounding
    // down the sample location to get the exact center of our "starting" texel. The starting texel will be at
    // location [1, 1] in the grid, where [0, 0] is the top left corner.
    float2 samplePos = uv * texSize;
    float2 texPos1 = floor(samplePos - 0.5f) + 0.5f;

    // Compute the fractional offset from our starting texel to our original sample location, which we'll
    // feed into the Catmull-Rom spline function to get our filter weights.
    float2 f = samplePos - texPos1;
    float2 f2 = f * f;
    float2 f3 = f2 * f;

    // Compute the Catmull-Rom weights using the fractional offset that we calculated earlier.
    // These equations are pre-expanded based on our knowledge of where the texels will be located,
    // which lets us avoid having to evaluate a piece-wise function.
    float2 w0 = (1.0f / 6.0f) * (-3.0f * f3 + 6.0f * f2 - 3.0f * f);
    float2 w1 = (1.0f / 6.0f) * (9.0f * f3 - 15.0f * f2 + 6.0f);
    float2 w2 = (1.0f / 6.0f) * (-9.0f * f3 + 12.0f * f2 + 3.0f * f);
    float2 w3 = (1.0f / 6.0f) * (3.0f * f3 - 3.0f * f2);

    // Work out weighting factors and sampling offsets that will let us use bilinear filtering to
    // simultaneously evaluate the middle 2 samples from the 4x4 grid.
    float2 w12 = w1 + w2;
    float2 offset12 = w2 / (w1 + w2);

    // Compute the final UV coordinates we'll use for sampling the texture
    float2 texPos0 = texPos1 - 1;
    float2 texPos3 = texPos1 + 2;
    float2 texPos12 = texPos1 + offset12;

    texPos0 /= texSize;
    texPos3 /= texSize;
    texPos12 /= texSize;

    float4 result = 0.0f;
    result += tex.SampleLevel(linearSampler, float2(texPos0.x, texPos0.y), 0.0f) * w0.x * w0.y;
    result += tex.SampleLevel(linearSampler, float2(texPos12.x, texPos0.y), 0.0f) * w12.x * w0.y;
    result += tex.SampleLevel(linearSampler, float2(texPos3.x, texPos0.y), 0.0f) * w3.x * w0.y;

    result += tex.SampleLevel(linearSampler, float2(texPos0.x, texPos12.y), 0.0f) * w0.x * w12.y;
    result += tex.SampleLevel(linearSampler, float2(texPos12.x, texPos12.y), 0.0f) * w12.x * w12.y;
    result += tex.SampleLevel(linearSampler, float2(texPos3.x, texPos12.y), 0.0f) * w3.x * w12.y;

    result += tex.SampleLevel(linearSampler, float2(texPos0.x, texPos3.y), 0.0f) * w0.x * w3.y;
    result += tex.SampleLevel(linearSampler, float2(texPos12.x, texPos3.y), 0.0f) * w12.x * w3.y;
    result += tex.SampleLevel(linearSampler, float2(texPos3.x, texPos3.y), 0.0f) * w3.x * w3.y;

    return max(result, 0.0f);
    }