HairFullScreenUtils.azsli 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. /*
  2. * Modifications Copyright (c) Contributors to the Open 3D Engine Project.
  3. * For complete copyright and license terms please see the LICENSE at the root of this distribution.
  4. *
  5. * SPDX-License-Identifier: Apache-2.0 OR MIT
  6. *
  7. */
  8. #include <Atom/Features/PostProcessing/FullscreenVertexInfo.azsli>
  9. #include <Atom/Features/PostProcessing/FullscreenVertexUtil.azsli>
  10. #include <viewsrg_all.srgi>
  11. //==============================================================================
  12. // Generate a fullscreen triangle from pipeline provided vertex id
  13. VSOutput FullScreenVS(VSInput input)
  14. {
  15. VSOutput OUT;
  16. float4 posTex = GetVertexPositionAndTexCoords(input.m_vertexID);
  17. OUT.m_texCoord = float2(posTex.z, posTex.w); // [To Do] - test sign of Y based on original code
  18. OUT.m_position = float4(posTex.xy, 0.0, 1.0);
  19. return OUT;
  20. }
  21. //==============================================================================
  22. // Given the depth buffer depth of the current pixel and the fragment XY position,
  23. // reconstruct the NDC.
  24. // screenCoords - from 0.. dimension of the screen of the current pixel
  25. // screenTexture - screen buffer texture representing the same resolution we work in
  26. // sDepth - the depth buffer depth at the fragment location
  27. // NDC - Normalized Device Coordinates = warped screen space ( -1.1, -1..1, 0..1 )
  28. float3 ScreenPosToNDC( Texture2D<float> screenTexture, float2 screenCoords, float depth )
  29. {
  30. uint2 dimensions;
  31. screenTexture.GetDimensions(dimensions.x, dimensions.y);
  32. float2 UV = saturate(screenCoords / dimensions.xy);
  33. float x = UV.x * 2.0f - 1.0f;
  34. float y = (1.0f - UV.y) * 2.0f - 1.0f;
  35. float3 NDC = float3(x, y, depth);
  36. return NDC;
  37. }
  38. // Given the depth buffer depth of the current pixel and the fragment XY position,
  39. // reconstruct the world space position
  40. float3 ScreenPosToWorldPos(
  41. Texture2D<float> screenTexture, float2 screenCoords, float depth,
  42. inout float3 screenPosNDC )
  43. {
  44. screenPosNDC = ScreenPosToNDC(screenTexture, screenCoords, depth);
  45. float4 projectedPos = float4(screenPosNDC, 1.0f); // warped projected space [0..1]
  46. float4 positionVS = mul(ViewSrg::m_projectionMatrixInverse, projectedPos);
  47. positionVS /= positionVS.w; // notice the normalization factor - crucial!
  48. float4 positionWS = mul(ViewSrg::m_viewMatrixInverse, positionVS);
  49. return positionWS.xyz;
  50. }