BicubicUpsamplePS.hlsl 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. // RUN: %dxc -E main -T ps_6_0 -O0 %s | FileCheck %s
  2. // CHECK: Frc
  3. // CHECK: IMax
  4. // CHECK: UMin
  5. // CHECK: textureLoad
  6. // CHECK: Log
  7. // CHECK: Exp
  8. //
  9. // Copyright (c) Microsoft. All rights reserved.
  10. // This code is licensed under the MIT License (MIT).
  11. // THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF
  12. // ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY
  13. // IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR
  14. // PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.
  15. //
  16. // Developed by Minigraph
  17. //
  18. // Author: James Stanard
  19. //
  20. //--------------------------------------------------------------------------------------
  21. // Simple bicubic filter
  22. //
  23. // http://en.wikipedia.org/wiki/Bicubic_interpolation
  24. // http://http.developer.nvidia.com/GPUGems/gpugems_ch24.html
  25. //
  26. //--------------------------------------------------------------------------------------
  27. #include "ShaderUtility.hlsli"
  28. #include "PresentRS.hlsli"
  29. Texture2D<float3> ColorTex : register(t0);
  30. SamplerState BilinearClamp : register(s0);
  31. cbuffer Constants : register(b0)
  32. {
  33. uint2 TextureSize;
  34. float A;
  35. }
  36. float W1(float x)
  37. {
  38. return x * x * ((A + 2) * x - (A + 3)) + 1.0;
  39. }
  40. float W2(float x)
  41. {
  42. return A * (x * (x * (x - 5) + 8) - 4);
  43. }
  44. float4 GetWeights(float d1)
  45. {
  46. return float4(W2(1.0 + d1), W1(d1), W1(1.0 - d1), W2(2.0 - d1));
  47. }
  48. float3 Cubic(float4 w, float3 c0, float3 c1, float3 c2, float3 c3)
  49. {
  50. return c0 * w.x + c1 * w.y + c2 * w.z + c3 * w.w;
  51. }
  52. float3 GetColor(uint s, uint t)
  53. {
  54. #ifdef GAMMA_SPACE
  55. return ApplyColorProfile(ColorTex[uint2(s, t)], DISPLAY_PLANE_FORMAT);
  56. #else
  57. return ColorTex[uint2(s, t)];
  58. #endif
  59. }
  60. [RootSignature(Present_RootSig)]
  61. float3 main(float4 position : SV_Position, float2 uv : TexCoord0) : SV_Target0
  62. {
  63. float2 t = uv * TextureSize + 0.5;
  64. float2 f = frac(t);
  65. int2 st = int2(t);
  66. uint MaxWidth = TextureSize.x - 1;
  67. uint MaxHeight = TextureSize.y - 1;
  68. uint s0 = max(st.x - 2, 0);
  69. uint s1 = max(st.x - 1, 0);
  70. uint s2 = min(st.x + 0, MaxWidth);
  71. uint s3 = min(st.x + 1, MaxWidth);
  72. uint t0 = max(st.y - 2, 0);
  73. uint t1 = max(st.y - 1, 0);
  74. uint t2 = min(st.y + 0, MaxHeight);
  75. uint t3 = min(st.y + 1, MaxHeight);
  76. float4 Weights = GetWeights(f.x);
  77. float3 c0 = Cubic(Weights, GetColor(s0, t0), GetColor(s1, t0), GetColor(s2, t0), GetColor(s3, t0));
  78. float3 c1 = Cubic(Weights, GetColor(s0, t1), GetColor(s1, t1), GetColor(s2, t1), GetColor(s3, t1));
  79. float3 c2 = Cubic(Weights, GetColor(s0, t2), GetColor(s1, t2), GetColor(s2, t2), GetColor(s3, t2));
  80. float3 c3 = Cubic(Weights, GetColor(s0, t3), GetColor(s1, t3), GetColor(s2, t3), GetColor(s3, t3));
  81. float3 Color = Cubic(GetWeights(f.y), c0, c1, c2, c3);
  82. #ifdef GAMMA_SPACE
  83. return Color;
  84. #else
  85. return ApplyColorProfile(Color, DISPLAY_PLANE_FORMAT);
  86. #endif
  87. }