SubpassInputComposition.azsl 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. float3 position = SubpassInputsSrg::m_position.SubpassLoad().xyz;
  43. float3 normal = SubpassInputsSrg::m_normal.SubpassLoad().xyz;
  44. float4 albedo = SubpassInputsSrg::m_albedo.SubpassLoad();
  45. // Ambient part
  46. const float3 lightDir = normalize(float3(-1, -1, -1));
  47. float3 fragcolor = albedo.rgb * saturate(dot(lightDir, normal)) + ambient;
  48. for(int i = 0; i < NUM_LIGHTS; ++i)
  49. {
  50. // Vector to light
  51. float3 L = SubpassInputSceneSrg::m_lights[i].m_position.xyz - position;
  52. // Distance from light to fragment position
  53. float dist = length(L);
  54. // Viewer to fragment
  55. float3 V = position - ViewSrg::m_worldPosition;
  56. V = normalize(V);
  57. // Light to fragment
  58. L = normalize(L);
  59. // Attenuation
  60. float atten = SubpassInputSceneSrg::m_lights[i].m_radius / (pow(dist, 2.0) + 1.0);
  61. // Diffuse part
  62. float3 N = normalize(normal);
  63. float NdotL = max(0.0, dot(N, L));
  64. float3 diff = SubpassInputSceneSrg::m_lights[i].m_color * albedo.rgb * NdotL * atten;
  65. fragcolor += diff;
  66. }
  67. OUT.m_outScene = float4(fragcolor, 1.0);
  68. return OUT;
  69. }