You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

305 lines
9.4 KiB

5 years ago
  1. // Upgrade NOTE: replaced 'mul(UNITY_MATRIX_MVP,*)' with 'UnityObjectToClipPos(*)'
  2. // Copyright(c) 2016, Michal Skalsky
  3. // All rights reserved.
  4. //
  5. // Redistribution and use in source and binary forms, with or without modification,
  6. // are permitted provided that the following conditions are met:
  7. //
  8. // 1. Redistributions of source code must retain the above copyright notice,
  9. // this list of conditions and the following disclaimer.
  10. //
  11. // 2. Redistributions in binary form must reproduce the above copyright notice,
  12. // this list of conditions and the following disclaimer in the documentation
  13. // and/or other materials provided with the distribution.
  14. //
  15. // 3. Neither the name of the copyright holder nor the names of its contributors
  16. // may be used to endorse or promote products derived from this software without
  17. // specific prior written permission.
  18. //
  19. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
  20. // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  21. // OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.IN NO EVENT
  22. // SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  23. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
  24. // OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  25. // HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
  26. // TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
  27. // EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. //Modified by Carlos Macarr�n & David Miranda
  29. Shader "Hidden/Upsample"
  30. {
  31. Properties
  32. {
  33. _UpsampleDepthThreshold("Upsample Depth Threshold", Range(0, 2)) = .05
  34. [HideInInspector] _LowResColor("", 2D) = "white"{}
  35. }
  36. SubShader
  37. {
  38. // No culling or depth
  39. Cull Off ZWrite Off ZTest Always
  40. CGINCLUDE
  41. // #define UPSAMPLE_DEPTH_THRESHOLD 0.05f
  42. uniform half _UpsampleDepthThreshold = .05;
  43. int RightSide;
  44. #define PI 3.1415927f
  45. #include "UnityCG.cginc"
  46. float4 _TexelSize = 1;
  47. sampler2D _HiResDepthBufferR;
  48. sampler2D _HiResDepthBuffer;
  49. sampler2D _LowResDepthBuffer;
  50. sampler2D _LowResDepthBufferR;
  51. sampler2D _LowResColor;
  52. sampler2D _LowResColorR;
  53. struct appdata
  54. {
  55. float4 vertex : POSITION;
  56. float2 uv : TEXCOORD0;
  57. };
  58. struct v2fDownsample
  59. {
  60. float4 vertex : SV_POSITION0;
  61. float2 uv : TEXCOORD0;
  62. float2 uv00 : TEXCOORD1;
  63. float2 uv01 : TEXCOORD2;
  64. float2 uv10 : TEXCOORD3;
  65. float2 uv11 : TEXCOORD4;
  66. };
  67. struct v2fUpsample
  68. {
  69. float4 vertex : SV_POSITION;
  70. float2 uv : TEXCOORD0;
  71. float2 uv00 : TEXCOORD1;
  72. float2 uv01 : TEXCOORD2;
  73. float2 uv10 : TEXCOORD3;
  74. float2 uv11 : TEXCOORD4;
  75. };
  76. //-----------------------------------------------------------------------------------------
  77. // vertDownsampleDepth
  78. //-----------------------------------------------------------------------------------------
  79. v2fDownsample vertDownsampleDepth(appdata v, float2 texelSize)
  80. {
  81. v2fDownsample o;
  82. UNITY_INITIALIZE_OUTPUT(v2fDownsample, o);
  83. o.vertex = UnityObjectToClipPos(v.vertex);
  84. #if UNITY_SINGLE_PASS_STEREO
  85. o.uv = UnityStereoTransformScreenSpaceTex(v.uv);
  86. #else
  87. o.uv = v.uv;
  88. #endif
  89. o.uv00 = v.uv - 0.5 * texelSize.xy;
  90. o.uv10 = o.uv00 + float2(texelSize.x, 0);
  91. o.uv01 = o.uv00 + float2(0, texelSize.y);
  92. o.uv11 = o.uv00 + texelSize.xy;
  93. return o;
  94. }
  95. //-----------------------------------------------------------------------------------------
  96. // vertUpsample
  97. //-----------------------------------------------------------------------------------------
  98. v2fUpsample vertUpsample(appdata v, float2 texelSize)
  99. {
  100. v2fUpsample o;
  101. UNITY_INITIALIZE_OUTPUT(v2fUpsample, o);
  102. o.vertex = UnityObjectToClipPos(v.vertex);
  103. #if UNITY_SINGLE_PASS_STEREO
  104. o.uv = UnityStereoTransformScreenSpaceTex(v.uv);
  105. #else
  106. o.uv = v.uv;
  107. #endif
  108. o.uv00 = v.uv - 0.5 * texelSize.xy;
  109. o.uv10 = o.uv00 + float2(texelSize.x, 0);
  110. o.uv01 = o.uv00 + float2(0, texelSize.y);
  111. o.uv11 = o.uv00 + texelSize.xy;
  112. return o;
  113. }
  114. #undef SAMPLE_DEPTH_TEXTURE
  115. #define SAMPLE_DEPTH_TEXTURE(sampler, uv) ( tex2D(sampler, uv).r )
  116. //-----------------------------------------------------------------------------------------
  117. // BilateralUpsample
  118. //-----------------------------------------------------------------------------------------
  119. float4 BilateralUpsample(v2fUpsample input, sampler2D hiDepth, sampler2D loDepth, sampler2D loColor)
  120. {
  121. float4 highResDepth = (SAMPLE_DEPTH_TEXTURE(hiDepth, input.uv)).xxxx;
  122. float4 lowResDepth=0;
  123. #if UNITY_SINGLE_PASS_STEREO
  124. float4 scaleOffset = unity_StereoScaleOffset[unity_StereoEyeIndex];
  125. input.uv00 = (input.uv00 - scaleOffset.zw) / scaleOffset.xy;
  126. input.uv10 = (input.uv10 - scaleOffset.zw) / scaleOffset.xy;
  127. input.uv01 = (input.uv01 - scaleOffset.zw) / scaleOffset.xy;
  128. input.uv11 = (input.uv11 - scaleOffset.zw) / scaleOffset.xy;
  129. #endif
  130. lowResDepth[0] = (SAMPLE_DEPTH_TEXTURE(loDepth, input.uv00));
  131. lowResDepth[1] = (SAMPLE_DEPTH_TEXTURE(loDepth, input.uv10));
  132. lowResDepth[2] = (SAMPLE_DEPTH_TEXTURE(loDepth, input.uv01));
  133. lowResDepth[3] = (SAMPLE_DEPTH_TEXTURE(loDepth, input.uv11));
  134. float4 depthDiff = abs(lowResDepth - highResDepth);
  135. float accumDiff = dot(depthDiff, .1);//3.2.1 changed 1 to .1
  136. //UNITY_BRANCH
  137. if (accumDiff < _UpsampleDepthThreshold) // small error, not an edge -> use bilinear filter
  138. {
  139. // should be bilinear sampler, dont know how to use two different samplers for one texture in Unity
  140. //return float4(1, 0, 0, 1);
  141. return tex2D(loColor, input.uv);
  142. }
  143. #if VISUALIZE_EDGE
  144. return float4(1, .2, 0, 1);
  145. #endif
  146. // find nearest sample
  147. float minDepthDiff = depthDiff[0];
  148. float2 nearestUv = input.uv00;
  149. if (depthDiff[1] < minDepthDiff)
  150. {
  151. nearestUv = input.uv10;
  152. minDepthDiff = depthDiff[1];
  153. }
  154. if (depthDiff[2] < minDepthDiff)
  155. {
  156. nearestUv = input.uv01;
  157. minDepthDiff = depthDiff[2];
  158. }
  159. if (depthDiff[3] < minDepthDiff)
  160. {
  161. nearestUv = input.uv11;
  162. minDepthDiff = depthDiff[3];
  163. }
  164. return tex2D(loColor, nearestUv);
  165. }
  166. //-----------------------------------------------------------------------------------------
  167. // DownsampleDepth
  168. //-----------------------------------------------------------------------------------------
  169. float4 DownsampleDepth(v2fDownsample input, sampler2D depthTexture)
  170. {
  171. float final;
  172. // Normal
  173. final = tex2D(depthTexture, input.uv).x;
  174. #if DOWNSAMPLE_DEPTH_MODE_MIN // min depth
  175. float depth = tex2D(depthTexture, input.uv00) ;
  176. depth = min(depth, tex2D(depthTexture, input.uv01) );
  177. depth = min(depth, tex2D(depthTexture, input.uv10) );
  178. depth = min(depth, tex2D(depthTexture, input.uv11) );
  179. final = (depth);
  180. #elif DOWNSAMPLE_DEPTH_MODE_MAX // max depth
  181. float depth = tex2D(depthTexture, input.uv00) ;
  182. depth = max(depth, tex2D(depthTexture, input.uv01) );
  183. depth = max(depth, tex2D(depthTexture, input.uv10) );
  184. depth = max(depth, tex2D(depthTexture, input.uv11) );
  185. final = (depth);
  186. #else // DOWNSAMPLE_DEPTH_MODE_CHESSBOARD
  187. float4 depth;
  188. depth.x = tex2D(depthTexture, input.uv00) ;
  189. depth.y = tex2D(depthTexture, input.uv01) ;
  190. depth.z = tex2D(depthTexture, input.uv10) ;
  191. depth.w = tex2D(depthTexture, input.uv11) ;
  192. float minDepth = min(min(depth.x, depth.y), min(depth.z, depth.w));
  193. float maxDepth = max(max(depth.x, depth.y), max(depth.z, depth.w));
  194. // chessboard pattern
  195. int2 position = input.vertex.xy % 2;
  196. int index = position.x + position.y;
  197. final = (index == 1 ? minDepth : maxDepth);
  198. #endif
  199. return final;
  200. }
  201. ENDCG
  202. // pass 0 - downsample depth
  203. Pass
  204. {
  205. // Name "downsample depth"
  206. CGPROGRAM
  207. #pragma vertex vertDepth
  208. #pragma fragment frag
  209. #pragma exclude_renderers d3d9
  210. #pragma target 3.0
  211. #pragma multi_compile DOWNSAMPLE_DEPTH_MODE_MIN DOWNSAMPLE_DEPTH_MODE_MAX DOWNSAMPLE_DEPTH_MODE_CHESSBOARD
  212. #pragma multi_compile _ FOG_VOLUME_STEREO_ON
  213. v2fDownsample vertDepth(appdata v)
  214. {
  215. return vertDownsampleDepth(v, _TexelSize);
  216. }
  217. float4 frag(v2fDownsample input) : SV_Target
  218. {
  219. #ifdef FOG_VOLUME_STEREO_ON
  220. if (RightSide == 1)
  221. return DownsampleDepth(input, _HiResDepthBufferR);
  222. else
  223. return DownsampleDepth(input, _HiResDepthBuffer);
  224. #else
  225. return DownsampleDepth(input, _HiResDepthBuffer);
  226. #endif
  227. }
  228. ENDCG
  229. }
  230. // pass 1 - bilateral upsample
  231. Pass
  232. {
  233. // Name "bilateral upsample"
  234. Blend One Zero
  235. CGPROGRAM
  236. #pragma vertex vertUpsampleToFull
  237. #pragma fragment frag
  238. #pragma exclude_renderers d3d9
  239. #pragma target 3.0
  240. #pragma shader_feature VISUALIZE_EDGE
  241. #pragma multi_compile _ FOG_VOLUME_STEREO_ON
  242. v2fUpsample vertUpsampleToFull(appdata v)
  243. {
  244. return vertUpsample(v, _TexelSize);
  245. }
  246. float4 frag(v2fUpsample input) : SV_Target
  247. {
  248. float4 c = 0;
  249. //#ifdef FOG_VOLUME_STEREO_ON
  250. if (RightSide==0)
  251. c = BilateralUpsample(input, _HiResDepthBuffer, _LowResDepthBuffer, _LowResColor);
  252. //c = float4(1, 0, 0, 1);
  253. else
  254. c = BilateralUpsample(input, _HiResDepthBufferR, _LowResDepthBufferR, _LowResColorR);
  255. //c = float4(0, 1, 0, 1);
  256. //#else
  257. //c = BilateralUpsample(input, _HiResDepthBuffer, _LowResDepthBuffer, _LowResColor);
  258. //#endif
  259. return c;
  260. }
  261. ENDCG
  262. }
  263. }
  264. }