Advertisement
Guest User

RetroArch - Bilateral Filter

a guest
Aug 22nd, 2014
723
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.12 KB | None | 0 0
  1. /*
  2. Bilateral v1.0
  3. by Sp00kyFox, 2014
  4.  
  5. Bilateral Filter, calculates a weighted mean of surrounding pixels based on color and spatial distance.
  6. This can be used to smooth color transitions or blend dithering to some extent while preserving sharp edges.
  7. Increasing the radius leads to more pixel lookups and therefore to a lower shader performance.
  8.  
  9. */
  10.  
  11.  
  12. #pragma parameter RAD "BILATERAL Radius" 2.00 0.00 12.0 0.25
  13. #pragma parameter CLR "BILATERAL Color Thresh" 0.15 0.01 1.0 0.01
  14. #pragma parameter CWGHT "BILATERAL Central Wght" 0.25 0.00 2.0 0.05
  15.  
  16. #ifdef PARAMETER_UNIFORM
  17. uniform float RAD, CLR, CWGHT;
  18. #else
  19. #define RAD 2.00
  20. #define CLR 0.15
  21. #define CWGHT 0.25
  22. #endif
  23.  
  24.  
  25. #define TEX(dx,dy) tex2D(decal, VAR.texCoord+float2((dx),(dy))*VAR.t1)
  26.  
  27. static float4 unit4 = float4(1.0);
  28.  
  29. static int steps = ceil(RAD);
  30. static float clr = -CLR * CLR;
  31. static float sigma = RAD * RAD / 2.0;
  32. static float cwght = 1.0 + CWGHT * max(1.0, 2.87029746*sigma + 0.43165242*RAD - 0.25219746);
  33.  
  34. static float domain[13] = {1.0, exp( -1.0/sigma), exp( -4.0/sigma), exp( -9.0/sigma), exp( -16.0/sigma), exp( -25.0/sigma), exp( -36.0/sigma),
  35. exp(-49.0/sigma), exp(-64.0/sigma), exp(-81.0/sigma), exp(-100.0/sigma), exp(-121.0/sigma), exp(-144.0/sigma)};
  36.  
  37. float dist2(float3 pt1, float3 pt2)
  38. {
  39. float3 v = pt1 - pt2;
  40. return dot(v,v);
  41. }
  42.  
  43. float4 weight(int i, int j, float3 org, float4x3 A)
  44. {
  45. return domain[i] * domain[j] * exp(float4(dist2(org,A[0]), dist2(org,A[1]), dist2(org,A[2]), dist2(org,A[3]))/clr);
  46. }
  47.  
  48.  
  49. struct input
  50. {
  51. float2 video_size;
  52. float2 texture_size;
  53. float2 output_size;
  54. };
  55.  
  56. struct out_vertex {
  57. float4 position : POSITION;
  58. float4 color : COLOR;
  59. float2 texCoord : TEXCOORD0;
  60. float2 t1;
  61. };
  62.  
  63. /* VERTEX_SHADER */
  64. out_vertex main_vertex
  65. (
  66. float4 position : POSITION,
  67. float4 color : COLOR,
  68. float2 texCoord : TEXCOORD0,
  69.  
  70. uniform float4x4 modelViewProj,
  71. uniform input IN
  72. )
  73. {
  74. out_vertex OUT;
  75.  
  76. OUT.position = mul(modelViewProj, position);
  77. OUT.color = color;
  78.  
  79. OUT.texCoord = texCoord;
  80. OUT.t1 = 1.0/IN.texture_size;
  81.  
  82. return OUT;
  83. }
  84.  
  85.  
  86. /* FRAGMENT SHADER */
  87. float3 main_fragment(in out_vertex VAR, uniform sampler2D decal : TEXUNIT0, uniform input IN) : COLOR
  88. {
  89. float4x3 A, B;
  90. float4 wghtA, wghtB;
  91. float3 org = TEX(0,0).rgb, result = cwght*org;
  92. float norm = cwght;
  93.  
  94.  
  95. for(int x=1; x<=steps; x++){
  96.  
  97. A = float4x3(TEX( x, 0).rgb, TEX(-x, 0).rgb, TEX( 0, x).rgb, TEX( 0,-x).rgb);
  98. B = float4x3(TEX( x, x).rgb, TEX( x,-x).rgb, TEX(-x, x).rgb, TEX(-x,-x).rgb);
  99.  
  100. wghtA = weight(x, 0, org, A); wghtB = weight(x, x, org, B);
  101.  
  102. result += mul(wghtA, A) + mul(wghtB, B);
  103. norm += dot(wghtA, unit4) + dot(wghtB, unit4);
  104.  
  105. for(int y=1; y<x; y++){
  106.  
  107. A = float4x3(TEX( x, y).rgb, TEX( x,-y).rgb, TEX(-x, y).rgb, TEX(-x,-y).rgb);
  108. B = float4x3(TEX( y, x).rgb, TEX( y,-x).rgb, TEX(-y, x).rgb, TEX(-y,-x).rgb);
  109.  
  110. wghtA = weight(x, y, org, A); wghtB = weight(y, x, org, B);
  111.  
  112. result += mul(wghtA, A) + mul(wghtB, B);
  113. norm += dot(wghtA, unit4) + dot(wghtB, unit4);
  114. }
  115. }
  116.  
  117. return result/norm;
  118. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement