Advertisement
Guest User

horner

a guest
Sep 21st, 2016
385
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.94 KB | None | 0 0
  1. // Samples a texture with Catmull-Rom filtering, using 9 texture fetches instead of 16.
  2. // See http://vec3.ca/bicubic-filtering-in-fewer-taps/ for more details
  3. float4 SampleTextureCatmullRom(in Texture2D<float4> tex, in SamplerState linearSampler, in float2 uv, in float2 texSize)
  4. {
  5.     // We're going to sample a a 4x4 grid of texels surrounding the target UV coordinate. We'll do this by rounding
  6.     // down the sample location to get the exact center of our "starting" texel. The starting texel will be at
  7.     // location [1, 1] in the grid, where [0, 0] is the top left corner.
  8.     float2 samplePos = uv * texSize;
  9.     float2 texPos1 = floor(samplePos - 0.5f) + 0.5f;
  10.  
  11.     // Compute the fractional offset from our starting texel to our original sample location, which we'll
  12.     // feed into the Catmull-Rom spline function to get our filter weights.
  13.     float2 f = samplePos - texPos1;
  14.     //float2 f2 = f * f;
  15.  
  16.     // Compute the Catmull-Rom weights using the fractional offset that we calculated earlier.
  17.     // These equations are pre-expanded based on our knowledge of where the texels will be located,
  18.     // which lets us avoid having to evaluate a piece-wise function.
  19.     float2 w0 = f * ( -0.5 + f * (1.0 - 0.5*f));
  20.     float2 w1 = 1.0 + f * f * (1.5*f - 2.5);
  21.     float2 w2 = f * ( 0.5 + f * (2.0 - 1.5*f) );
  22.     float2 w3 = f * f * (-0.5 + 0.5 * f);
  23.  
  24.     // Work out weighting factors and sampling offsets that will let us use bilinear filtering to
  25.     // simultaneously evaluate the middle 2 samples from the 4x4 grid.
  26.     float2 w12 = w1 + w2;
  27.     float2 offset12 = w2 / (w1 + w2);
  28.  
  29.     // Compute the final UV coordinates we'll use for sampling the texture
  30.     float2 texPos0 = texPos1 - 1;
  31.     float2 texPos3 = texPos1 + 2;
  32.     float2 texPos12 = texPos1 + offset12;
  33.  
  34.     texPos0 /= texSize;
  35.     texPos3 /= texSize;
  36.     texPos12 /= texSize;
  37.  
  38.     float4 result = 0.0f;
  39.     result += tex.SampleLevel(linearSampler, float2(texPos0.x, texPos0.y), 0.0f) * w0.x * w0.y;
  40.     result += tex.SampleLevel(linearSampler, float2(texPos12.x, texPos0.y), 0.0f) * w12.x * w0.y;
  41.     result += tex.SampleLevel(linearSampler, float2(texPos3.x, texPos0.y), 0.0f) * w3.x * w0.y;
  42.  
  43.     result += tex.SampleLevel(linearSampler, float2(texPos0.x, texPos12.y), 0.0f) * w0.x * w12.y;
  44.     result += tex.SampleLevel(linearSampler, float2(texPos12.x, texPos12.y), 0.0f) * w12.x * w12.y;
  45.     result += tex.SampleLevel(linearSampler, float2(texPos3.x, texPos12.y), 0.0f) * w3.x * w12.y;
  46.  
  47.     result += tex.SampleLevel(linearSampler, float2(texPos0.x, texPos3.y), 0.0f) * w0.x * w3.y;
  48.     result += tex.SampleLevel(linearSampler, float2(texPos12.x, texPos3.y), 0.0f) * w12.x * w3.y;
  49.     result += tex.SampleLevel(linearSampler, float2(texPos3.x, texPos3.y), 0.0f) * w3.x * w3.y;
  50.  
  51.     return result;
  52. }
  53.  
  54.  
  55.  
  56. Texture2D<float4> g_Tx;
  57. sampler g_S;
  58.  
  59. float4 main( float2 uv : TEXCOORD0 ) : SV_Target
  60. {
  61.     return SampleTextureCatmullRom( g_Tx, g_S, uv, float2(512,512) );
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement