SubpassInputComposition.azsl 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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 "SubpassInputSceneSrg.azsli"
  10. ShaderResourceGroup SubpassInputsSrg : SRG_PerSubPass
  11. {
  12. [[input_attachment_index(0)]]
  13. SubpassInput m_position;
  14. [[input_attachment_index(1)]]
  15. SubpassInput m_normal;
  16. [[input_attachment_index(2)]]
  17. SubpassInput m_albedo;
  18. }
  19. struct VSInput
  20. {
  21. uint m_vertexIndex : SV_VertexID;
  22. };
  23. struct VSOutput
  24. {
  25. float4 m_position : SV_Position;
  26. };
  27. VSOutput MainVS(VSInput input)
  28. {
  29. VSOutput OUT;
  30. float2 uv = float2((input.m_vertexIndex << 1) & 2, input.m_vertexIndex & 2);
  31. OUT.m_position = float4(uv * 2.0f - 1.0f, 0.0f, 1.0f);
  32. return OUT;
  33. }
  34. struct PSOutput
  35. {
  36. float4 m_outScene : SV_Target0;
  37. };
  38. PSOutput MainPS(float4 screenPos : SV_POSITION)
  39. {
  40. const float3 ambient = float3(0.15, 0.15, 0.15);
  41. PSOutput OUT;
  42. int3 loadPos = int3(screenPos.x, screenPos.y, 0);
  43. float3 position = SubpassInputsSrg::m_position.SubpassLoad(loadPos).xyz;
  44. float3 normal = SubpassInputsSrg::m_normal.SubpassLoad(loadPos).xyz;
  45. float4 albedo = SubpassInputsSrg::m_albedo.SubpassLoad(loadPos);
  46. // Ambient part
  47. const float3 lightDir = normalize(float3(-1, -1, -1));
  48. float3 fragcolor = albedo.rgb * saturate(dot(lightDir, normal)) + ambient;
  49. for(int i = 0; i < NUM_LIGHTS; ++i)
  50. {
  51. // Vector to light
  52. float3 L = SubpassInputSceneSrg::m_lights[i].m_position.xyz - position;
  53. // Distance from light to fragment position
  54. float dist = length(L);
  55. // Viewer to fragment
  56. float3 V = position - ViewSrg::m_worldPosition;
  57. V = normalize(V);
  58. // Light to fragment
  59. L = normalize(L);
  60. // Attenuation
  61. float atten = SubpassInputSceneSrg::m_lights[i].m_radius / (pow(dist, 2.0) + 1.0);
  62. // Diffuse part
  63. float3 N = normalize(normal);
  64. float NdotL = max(0.0, dot(N, L));
  65. float3 diff = SubpassInputSceneSrg::m_lights[i].m_color * albedo.rgb * NdotL * atten;
  66. fragcolor += diff;
  67. }
  68. OUT.m_outScene = float4(fragcolor, 1.0);
  69. return OUT;
  70. }