MeshletsDebugRenderShader.azsl 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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. // To use the object Id and VertexUtility the ObjectSrg must be created
  9. // and passed with the Object Id here.
  10. // This will require more than several changes also on the CPU side, utilizing the
  11. // transform (m_transformService->ReserveObjectId()) and setting the Id in the
  12. // PerObjectSrg (objectSrg->SetConstant(objectIdIndex, m_objectId.GetIndex())).
  13. // Look at ModelDataInstance::Init(Data::Instance<RPI::Model> model) to see how
  14. // to set the model Id - this will lead to MeshDrawPacket::DoUpdate that will demonstrate
  15. // how to set the Srgs.
  16. // There is no need to set all the Srgs in the DrawPacket - only the ones that the pass
  17. // does not add manually in the command list (check the inheritance of the commit method).
  18. //#include <Atom/Features/PBR/DefaultObjectSrg.azsli>
  19. #include <Atom/Features/SrgSemantics.azsli>
  20. //------------------------------------------------------------------------------
  21. //ShaderResourceGroup PassSrg : SRG_PerPass_WithFallback
  22. ShaderResourceGroup PassSrg : SRG_PerPass
  23. { // No need for StructuredBuffer - change to buffer across all
  24. StructuredBuffer<int> m_meshletsSharedBuffer;
  25. }
  26. // Vertex Assembly streams will NOT be used - it is shown below as reference
  27. // Instead we use the buffers directly with index offset into the data.
  28. struct VSInput
  29. {
  30. uint m_vertexIndex : SV_VertexID;
  31. /* Use the following if ever passed as vertex assembly streams
  32. float3 m_position : POSITION;
  33. float3 m_normal : NORMAL;
  34. float4 m_tangent : TANGENT;
  35. float3 m_bitangent : BITANGENT;
  36. float2 m_uv : UV0;
  37. */
  38. };
  39. struct VSOutput
  40. {
  41. float4 m_position : SV_Position;
  42. float3 m_normal: NORMAL;
  43. float3 m_tangent : TANGENT;
  44. float3 m_bitangent : BITANGENT;
  45. float3 m_worldPosition : UV0;
  46. float2 m_uv : UV1;
  47. };
  48. struct MeshletsPassOutput
  49. {
  50. float4 m_color : SV_Target0;
  51. };
  52. struct MeshletsPassOutputWithDepth
  53. {
  54. float4 m_color : SV_Target0;
  55. float m_depth : SV_Depth;
  56. };
  57. // Shader Resource Groups
  58. #include <viewsrg.srgi>
  59. #include <MeshletsPerObjectRenderSrg.azsli>
  60. #include <Atom/RPI/Math.azsli>
  61. //! Utility function for vertex shaders to transform vertex tangent, bitangent, and normal vectors into world space.
  62. void ConstructTBN(float3 vertexNormal, float4 vertexTangent, float3 vertexBitangent,
  63. float4x4 localToWorld, float3x3 localToWorldInverseTranspose,
  64. out float3 normalWS, out float3 tangentWS, out float3 bitangentWS)
  65. {
  66. normalWS = normalize(mul(localToWorldInverseTranspose, vertexNormal));
  67. tangentWS = normalize(mul(localToWorld, float4(vertexTangent.xyz, 0)).xyz);
  68. bitangentWS = normalize(mul(localToWorld, float4(vertexBitangent, 0)).xyz);
  69. }
  70. //! @param skipShadowCoords can be useful for example when PixelDepthOffset is enable, because the pixel shader will have to run before the final world position is known
  71. void VertexHelper(uint vertexIndex, inout VSOutput OUT, float3 worldPosition)
  72. {
  73. OUT.m_worldPosition = worldPosition;
  74. // This paragraph is temporary to force the shader to include the Srg.
  75. float dummyVariableToForceSrg = 0.000001 * (float)PassSrg::m_meshletsSharedBuffer[vertexIndex];
  76. OUT.m_worldPosition.z += (abs(dummyVariableToForceSrg) < 0.001) ? dummyVariableToForceSrg : 0;
  77. OUT.m_position = mul(ViewSrg::m_viewProjectionMatrix, float4(OUT.m_worldPosition, 1.0));
  78. float4x4 objectToWorld = MeshletsObjectRenderSrg::GetWorldMatrix();
  79. float3x3 objectToWorldIT = MeshletsObjectRenderSrg::GetWorldMatrixInverseTranspose();
  80. ConstructTBN(
  81. MeshletsObjectRenderSrg::GetNormal(vertexIndex),
  82. MeshletsObjectRenderSrg::GetTangent(vertexIndex),
  83. MeshletsObjectRenderSrg::GetBiTangent(vertexIndex),
  84. objectToWorld, objectToWorldIT,
  85. OUT.m_normal, OUT.m_tangent, OUT.m_bitangent);
  86. }
  87. VSOutput MeshletsDebugRender_MainPassVS(VSInput IN)
  88. {
  89. VSOutput OUT;
  90. // Idea - test vertexIndex by sending to render as a position.
  91. float3 localPosition = MeshletsObjectRenderSrg::GetPosition(IN.m_vertexIndex);
  92. float3 worldPosition = mul(MeshletsObjectRenderSrg::GetWorldMatrix(), float4(localPosition, 1.0)).xyz;
  93. VertexHelper(IN.m_vertexIndex, OUT, worldPosition);
  94. OUT.m_uv = MeshletsObjectRenderSrg::GetUV(IN.m_vertexIndex);
  95. return OUT;
  96. }
  97. MeshletsPassOutput MeshletsDebugRender_MainPassPS(VSOutput IN)
  98. {
  99. // ------- Output -------
  100. MeshletsPassOutput OUT;
  101. // Debug purposes - uv will be colored by the Compute that
  102. // will generate the meshlets indices.
  103. OUT.m_color = float4(IN.m_uv.rg, 0, 1);
  104. return OUT;
  105. }