BindlessPrototype.azsl 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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. // GlobalSrg::m_floatBuffer
  9. // PerSceneSrg::m_sampler
  10. #include "BindlessPrototypeSrg.azsli"
  11. #include <Atom/Features/Bindless.azsli>
  12. struct BindlessMaterial0
  13. {
  14. uint4 materialIndex;
  15. float4 m_diffuseColor;
  16. };
  17. struct BindlessMaterial1
  18. {
  19. uint4 materialIndex;
  20. float4 m_diffuseColor;
  21. uint m_diffuseTextureIndex;
  22. };
  23. struct BindlessMaterial2
  24. {
  25. uint4 materialIndex;
  26. float4 m_diffuseColor;
  27. uint m_diffuseTextureIndex;
  28. uint m_normalTextureIndex;
  29. uint m_specularTextureIndex;
  30. };
  31. struct PerObject
  32. {
  33. float4x4 m_localToWorldMatrix;
  34. float4 rotation;
  35. };
  36. ShaderResourceGroupSemantic PerSubMesh
  37. {
  38. FrequencyId = 4;
  39. };
  40. // [TODO ATOM-2769] When the inline feature is complete, use InlineConstant instead of sending the data
  41. // via ConstantBuffer.
  42. ShaderResourceGroup HandleSrg : PerSubMesh
  43. {
  44. uint m_perViewHandle;
  45. uint m_perObjectHandle;
  46. uint m_materialHandle;
  47. uint m_lightHandle;
  48. };
  49. struct VertexInput
  50. {
  51. float3 m_position : POSITION;
  52. float3 m_normal : NORMAL;
  53. float3 m_tangent : TANGENT;
  54. float3 m_bitangent : BITANGENT;
  55. float2 m_uv : UV0;
  56. };
  57. struct VertexOutput
  58. {
  59. float4 m_position : SV_Position;
  60. float3 m_normal : NORMAL;
  61. float3 m_tangent : TANGENT;
  62. float3 m_bitangent : BITANGENT;
  63. float2 m_uv : UV0;
  64. };
  65. VertexOutput MainVS(VertexInput vsInput)
  66. {
  67. VertexOutput OUT;
  68. // Read the PerObject data from the FloatBuffer
  69. uint offset = 0;
  70. PerObject perObject;
  71. {
  72. ReadFromFloatBuffer(perObject.m_localToWorldMatrix, HandleSrg::m_perObjectHandle, offset);
  73. ReadFromFloatBuffer(perObject.rotation, HandleSrg::m_perObjectHandle, offset);
  74. }
  75. // Read the world matrix from the FloatBuffer
  76. float4x4 worldToClipMatrix;
  77. offset = 0;
  78. {
  79. ReadFromFloatBuffer(worldToClipMatrix, HandleSrg::m_perViewHandle, offset);
  80. }
  81. const float4 worldPosition = mul(perObject.m_localToWorldMatrix, float4(vsInput.m_position, 1.0));
  82. OUT.m_position = mul(worldToClipMatrix, worldPosition);
  83. OUT.m_uv = vsInput.m_uv;
  84. OUT.m_normal = normalize(vsInput.m_normal);
  85. return OUT;
  86. }
  87. struct PixelOutput
  88. {
  89. float4 m_color : SV_Target0;
  90. };
  91. PixelOutput MainPS(VertexOutput psInput)
  92. {
  93. PixelOutput OUT;
  94. uint offset = 0;
  95. // Read the material index to identify the material type
  96. uint4 materialIndex;
  97. {
  98. ReadFromFloatBuffer(materialIndex, HandleSrg::m_materialHandle, offset);
  99. }
  100. // Read the material data from the FloatBuffer depending ont he material index
  101. if(materialIndex.x == 0) // Albedo material
  102. {
  103. BindlessMaterial0 bindlessMaterial0;
  104. ReadFromFloatBuffer(bindlessMaterial0.m_diffuseColor, HandleSrg::m_materialHandle, offset);
  105. OUT.m_color = float4(bindlessMaterial0.m_diffuseColor.xyz, 1.0);
  106. }
  107. else if(materialIndex.x == 1) // Texture sample material
  108. {
  109. BindlessMaterial1 bindlessMaterial1;
  110. ReadFromFloatBuffer(bindlessMaterial1.m_diffuseColor, HandleSrg::m_materialHandle, offset);
  111. ReadFromFloatBuffer(bindlessMaterial1.m_diffuseTextureIndex, HandleSrg::m_materialHandle, offset);
  112. #ifdef UB_DIRECTBINDING_NOTSUPPPORTED
  113. Texture2D texture = Bindless::GetTexture2D(
  114. IndirectionBufferSrg::m_indirectionBuffer.Load(bindlessMaterial1.m_diffuseTextureIndex * 4));
  115. #else
  116. // % 8 for wrap-around texture index as specified in ImageSrg.m_textureArray
  117. Texture2D texture = ImageSrg::m_textureArray[bindlessMaterial1.m_diffuseTextureIndex % 8];
  118. #endif
  119. OUT.m_color = texture.Sample(SamplerSrg::m_sampler, psInput.m_uv);
  120. }
  121. else if(materialIndex.x == 2) // Shaded material
  122. {
  123. float4 color;
  124. BindlessMaterial2 bindlessMaterial2;
  125. ReadFromFloatBuffer(bindlessMaterial2.m_diffuseColor, HandleSrg::m_materialHandle, offset);
  126. ReadFromFloatBuffer(bindlessMaterial2.m_diffuseTextureIndex, HandleSrg::m_materialHandle, offset);
  127. ReadFromFloatBuffer(bindlessMaterial2.m_normalTextureIndex, HandleSrg::m_materialHandle, offset);
  128. ReadFromFloatBuffer(bindlessMaterial2.m_specularTextureIndex, HandleSrg::m_materialHandle, offset);
  129. Texture2D texture = Bindless::GetTexture2D(
  130. IndirectionBufferSrg::m_indirectionBuffer.Load(bindlessMaterial2.m_diffuseTextureIndex * 4));
  131. color = texture.Sample(SamplerSrg::m_sampler, psInput.m_uv);
  132. float3 lightDir;
  133. uint lightOffset = 0;
  134. ReadFromFloatBuffer(lightDir, HandleSrg::m_lightHandle, lightOffset);
  135. lightDir = normalize(-lightDir);
  136. color *= dot(lightDir, psInput.m_normal) * 8.0;
  137. OUT.m_color = color;
  138. }
  139. else
  140. {
  141. OUT.m_color = float4(1.0, 1.0, 1.0, 1.0);
  142. }
  143. return OUT;
  144. }