home *** CD-ROM | disk | FTP | other *** search
/ Xentax forum attachments archive / xentax.7z / 18759 / ROE.7z / QSSHADER_QSScreenSpaceDepth.h < prev    next >
Encoding:
C/C++ Source or Header  |  2020-09-17  |  3.3 KB  |  122 lines

  1. #ifndef QSSHADER_QSScreenSpaceDepth_H
  2. #define QSSHADER_QSScreenSpaceDepth_H
  3. #include "DX12Defines.fxh"
  4. #include "QSConstant.fxh"
  5. #include "FunctionLib.fxh"
  6.  
  7. Texture2D DepthSampler;
  8. float4 DepthParam;//x--near*far, y--far, z--far-near  w--near
  9. float4 ProjParam;//x--w==tan(fovw/2), y--tan(fovh/2)
  10. float4 FrustumParam; //x = near, y = far
  11. float4x4 ScreenToPartialWorldTransform;
  12. float4x4 invPartialViewProj;
  13. float4x4 invView;
  14.  
  15. #define NearPlane            FrustumParam.x
  16. #define FarPlane            FrustumParam.y
  17. #define NearPlaneDeviceZ    FrustumParam.z
  18. #define FarPlaneDeviceZ        FrustumParam.w
  19.  
  20. Texture2D<int2> StencilTexture;
  21. float4 StencilResolution;
  22. uint GetStencil(float2 uv)
  23. {
  24.     return StencilTexture.Load(int3(uv * StencilResolution.xy, 0)).g;
  25. }
  26. //calc viewspace z from screen space depth
  27. float CalcViewZFromDeviceZ(float depth)
  28. {
  29.     return DepthParam.x / (DepthParam.y - depth*DepthParam.z);
  30. }
  31.  
  32. float CalcViewZFromDeviceZ(float depth, float4 depthParam)
  33. {
  34.     return depthParam.x / (depthParam.y - depth * depthParam.z);
  35. }
  36.  
  37. float CalcHomoZFromViewSpace(float depth)
  38. {
  39.     return DepthParam.y / DepthParam.z - DepthParam.x / (depth * DepthParam.z);
  40. }
  41.  
  42. float3 GetViewPosFromViewZ(float2 uv, float viewSpaceZ)
  43. {
  44.     float3 viewPos;
  45.     viewPos.y = viewSpaceZ;
  46.     viewPos.x = (uv.x * 2 - 1) * viewSpaceZ * ProjParam.x;
  47.     viewPos.z = -(uv.y * 2 - 1) * viewSpaceZ * ProjParam.y;
  48.     return viewPos;
  49. }
  50.  
  51. float3 GetWorldPosFromViewZ(float2 uv, float viewSpaceZ)
  52. {
  53.     float4 viewPos;
  54.     viewPos.xyz = GetViewPosFromViewZ(uv, viewSpaceZ);
  55.     viewPos.w = 1;
  56.     return mul(viewPos, invView);
  57. }
  58.  
  59. float3 GetPartialWorldPosFromScreenPos(float4 screenPos)
  60. {
  61.     float4 partialPos = mul(screenPos, invPartialViewProj);
  62.     return partialPos.xyz / partialPos.w;
  63. }
  64.  
  65. float3 GetPartialWorldPosFromDeviceZ(float2 uv, float deviceZ)
  66. {
  67.     float4 partialWorldPos = mul(float4(uv, deviceZ, 1.0f), ScreenToPartialWorldTransform);
  68.     return partialWorldPos.xyz / partialWorldPos.w;
  69. }
  70.  
  71.  
  72. //-------------------------------------------
  73.  
  74. float GetDeviceZ(float2 uv)
  75. {
  76.     return DepthSampler.SampleLevel(NoMipMapPointClamp, uv, 0).r;    
  77. }
  78.  
  79. float GetDeviceZ(float2 uv, Texture2D depthSampler)
  80. {
  81.     return depthSampler.SampleLevel(NoMipMapPointClamp, uv, 0).r;
  82. }
  83.  
  84. float GetViewZFromScreenUv(float2 uv)
  85. {
  86.     float screenSpaceZ = GetDeviceZ(uv);
  87.     return CalcViewZFromDeviceZ(screenSpaceZ);
  88. }
  89.  
  90. float GetViewZFromScreenUv(float2 uv, Texture2D depthSampler)
  91. {
  92.     float screenSpaceZ = GetDeviceZ(uv, depthSampler);
  93.     return CalcViewZFromDeviceZ(screenSpaceZ);
  94. }
  95.  
  96. float3 GetViewPosFromScreenUv(float2 uv)
  97. {
  98.     return GetViewPosFromViewZ(uv, GetViewZFromScreenUv(uv));
  99. }
  100.  
  101. float3 GetWorldPosFromScreenUv(float2 uv)
  102. {
  103.     float deviceZ = GetDeviceZ(uv);
  104.     float4 partialWorldPos = mul(float4(uv, deviceZ, 1.0f), ScreenToPartialWorldTransform);
  105.     return partialWorldPos.xyz / partialWorldPos.w + cameraPos;
  106. }
  107.  
  108. float3 GetWorldPosFromScreenUv(float2 uv, Texture2D depthSampler)
  109. {
  110.     float deviceZ = GetDeviceZ(uv, depthSampler);
  111.     float4 partialWorldPos = mul(float4(uv, deviceZ, 1.0f), ScreenToPartialWorldTransform);
  112.     return partialWorldPos.xyz / partialWorldPos.w + cameraPos;
  113. }
  114.  
  115. float DepthSoftenEdge(float2 uv, float viewDepth, float edge)
  116. {
  117.     float sceneViewDepth = GetViewZFromScreenUv(uv);
  118.     return saturate(abs(viewDepth-sceneViewDepth)*edge*0.01f);
  119. }
  120.  
  121. #endif //QSSHADER_QSScreenSpaceDepth_H
  122.