Advertisement
Krythic

Gaussian Blur Shader

Aug 16th, 2020
1,339
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.71 KB | None | 0 0
  1. struct Vertex
  2. {
  3.     float4 pos : POSITION;
  4.     float2 tex : TEXTURE;
  5.     float3 norm : NORMAL;
  6. };
  7.  
  8. struct PixelShaderArgs
  9. {
  10.     float4 pos : SV_POSITION;
  11.     float2 col : TEXTURE;
  12.     float3 norm : NORMAL;
  13. };
  14.  
  15. struct GaussianBlurShaderData
  16. {
  17.     float2 Resolution;
  18.     float Intensity;
  19.     float Padding;
  20. };
  21.  
  22. Texture2D ShaderTexture : register(t0);
  23. SamplerState Sampler : register(s0);
  24. float4x4 localMatrix : register(b0);
  25.  
  26. cbuffer GaussianBlurShaderDataBuffer : register(b1)
  27. {
  28.     GaussianBlurShaderData GaussianData;
  29. };
  30.  
  31. PixelShaderArgs VertexShaderMain(Vertex vertex)
  32. {
  33.     PixelShaderArgs output;
  34.     output.pos = vertex.pos;
  35.     output.col = vertex.tex;
  36.     return output;
  37. }
  38.  
  39. float Curve(float x)
  40. {
  41.     x = x * 2.0 - 1.0;
  42.     return -x * abs(x) * 0.5 + x + 0.5;
  43. }
  44.  
  45. float4 GaussianBlur(float2 size, float2 uv, float radius)
  46. {
  47.     if (radius >= 1.0)
  48.     {
  49.         float4 a = float4(0,0,0,0);
  50.         float4 c = float4(0,0,0,0);
  51.         float width = 1.0 / size.x;
  52.         float divisor = 0.0;
  53.         float weight = 0.0;
  54.         float multiplier = 1.0 / radius;
  55.         for (float x = -radius; x <= radius; x++)
  56.         {
  57.             a = ShaderTexture.Sample(Sampler, uv + float2(x * width, 0.0));
  58.             weight = Curve(1.0 - (abs(x) * multiplier));
  59.             c += a * weight;
  60.             divisor += weight;
  61.         }
  62.         return float4(c.r / divisor, c.g / divisor, c.b / divisor, 1.0);
  63.     }
  64.     return ShaderTexture.Sample(Sampler, uv);
  65. }
  66.  
  67. float4 PixelShaderMain(PixelShaderArgs pixelShaderArgs) : SV_Target
  68. {
  69.     float2 uv = pixelShaderArgs.pos.xy / GaussianData.Resolution.xy;
  70.     return GaussianBlur( GaussianData.Resolution.xy, uv, GaussianData.Intensity);
  71. }
  72.  
  73.  
  74.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement