BindlessPrototype.azsl 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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_textureArray
  10. // PerSceneSrg::m_sampler
  11. #include "BindlessPrototypeSrg.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 = 1;
  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. Texture2D texture = ImageSrg::m_textureArray[bindlessMaterial1.m_diffuseTextureIndex % 8]; // % 8 for wrap-around texture index as specified in ImageSrg.m_textureArray
  113. OUT.m_color = texture.Sample(ImageSrg::m_sampler, psInput.m_uv);
  114. }
  115. else if(materialIndex.x == 2) // Shaded material
  116. {
  117. float4 color;
  118. BindlessMaterial2 bindlessMaterial2;
  119. ReadFromFloatBuffer(bindlessMaterial2.m_diffuseColor, HandleSrg::m_materialHandle, offset);
  120. ReadFromFloatBuffer(bindlessMaterial2.m_diffuseTextureIndex, HandleSrg::m_materialHandle, offset);
  121. ReadFromFloatBuffer(bindlessMaterial2.m_normalTextureIndex, HandleSrg::m_materialHandle, offset);
  122. ReadFromFloatBuffer(bindlessMaterial2.m_specularTextureIndex, HandleSrg::m_materialHandle, offset);
  123. Texture2D texture = ImageSrg::m_textureArray[bindlessMaterial2.m_diffuseTextureIndex % 8]; // % 8 for wrap-around texture index as specified in ImageSrg.m_textureArray
  124. color = texture.Sample(ImageSrg::m_sampler, psInput.m_uv);
  125. float3 lightDir;
  126. uint lightOffset = 0;
  127. ReadFromFloatBuffer(lightDir, HandleSrg::m_lightHandle, lightOffset);
  128. lightDir = normalize(-lightDir);
  129. color *= dot(lightDir, psInput.m_normal) * 8.0;
  130. OUT.m_color = color;
  131. }
  132. else
  133. {
  134. OUT.m_color = float4(1.0, 1.0, 1.0, 1.0);
  135. }
  136. return OUT;
  137. }