SubpassInputGBuffer.azsl 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. /*
  2. * 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 <viewsrg.srgi>
  9. #include "SubpassInputModelSrg.azsli"
  10. struct VSInput
  11. {
  12. float3 m_position : POSITION;
  13. float3 m_normal : NORMAL;
  14. };
  15. struct VSOutput
  16. {
  17. float4 m_position : SV_Position;
  18. float4 m_worldPos : POSITION;
  19. float3 m_normal : NORMAL;
  20. };
  21. VSOutput MainVS(VSInput vsInput)
  22. {
  23. VSOutput OUT;
  24. OUT.m_worldPos = mul(SubpassInputModelSrg::m_modelMatrix, float4(vsInput.m_position, 1.0));
  25. OUT.m_position = mul(ViewSrg::m_viewProjectionMatrix, OUT.m_worldPos);
  26. OUT.m_normal = vsInput.m_normal;
  27. return OUT;
  28. }
  29. struct PSOutput
  30. {
  31. float4 m_position : SV_Target0;
  32. float4 m_normal : SV_Target1;
  33. float4 m_albedo : SV_Target2;
  34. float4 m_outScene : SV_Target3;
  35. };
  36. float linearDepth(float depth)
  37. {
  38. const float NEAR_PLANE = 0.1f;
  39. const float FAR_PLANE = 256.0f;
  40. float z = depth * 2.0f - 1.0f;
  41. return (2.0f * NEAR_PLANE * FAR_PLANE) / (FAR_PLANE + NEAR_PLANE - z * (FAR_PLANE - NEAR_PLANE));
  42. }
  43. PSOutput MainPS(VSOutput vsOutput)
  44. {
  45. PSOutput OUT;
  46. OUT.m_albedo = SubpassInputModelSrg::m_color;
  47. OUT.m_normal = float4(normalize(vsOutput.m_normal), 1.0);
  48. OUT.m_position = vsOutput.m_worldPos;
  49. OUT.m_position.a = linearDepth(OUT.m_position.z);
  50. OUT.m_outScene = OUT.m_albedo;
  51. return OUT;
  52. }