standardpbr_forwardpass.azsl 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. ShaderResourceGroupSemantic BindingPerMaterial
  2. {
  3. FrequencyId = 1;
  4. };
  5. ShaderResourceGroupSemantic BindingPerObject
  6. {
  7. FrequencyId = 0;
  8. };
  9. ShaderResourceGroupSemantic BindingPerPass
  10. {
  11. FrequencyId = 3;
  12. };
  13. ShaderResourceGroupSemantic BindingPerSubPass
  14. {
  15. FrequencyId = 2;
  16. };
  17. ShaderResourceGroupSemantic BindingPerView
  18. {
  19. FrequencyId = 4;
  20. };
  21. ShaderResourceGroup ViewSrg: BindingPerView
  22. {
  23. row_major float4x4 m_viewProjectionMatrix;
  24. float4 m_worldPosition;
  25. float4x4 m_worldToClipMatrix;
  26. }
  27. float3 GetView_WorldPosition()
  28. {
  29. return ViewSrg::m_worldPosition.xyz;
  30. }
  31. float4x4 GetView_ViewProjectionMatrix()
  32. {
  33. return ViewSrg::m_viewProjectionMatrix;
  34. }ShaderResourceGroupSemantic BindingPerScene
  35. {
  36. FrequencyId = 5;
  37. };
  38. ShaderResourceGroup SceneSrg: BindingPerScene
  39. {
  40. float m_time;
  41. }
  42. float GetScene_Time()
  43. {
  44. return SceneSrg::m_time;
  45. }#line 1 "C:/Lumberyard/lyengine/branches/Atom/dev/Gems/Atom/Feature/Common/Assets/Materials/StandardPBR/StandardPBR_ForwardPass.azsl"
  46. #line 1 "<built-in>"
  47. #line 1 "<built-in>"
  48. #line 373 "<built-in>"
  49. #line 1 "<command line>"
  50. #line 1 "<built-in>"
  51. #line 1 "C:/Lumberyard/lyengine/branches/Atom/dev/Gems/Atom/Feature/Common/Assets/Materials/StandardPBR/StandardPBR_ForwardPass.azsl"
  52. /*
  53. * Copyright (c) Contributors to the Open 3D Engine Project.
  54. * For complete copyright and license terms please see the LICENSE at the root of this distribution.
  55. *
  56. * SPDX-License-Identifier: Apache-2.0 OR MIT
  57. *
  58. */
  59. struct VSInput
  60. {
  61. // Base fields (required by the template azsli file)...
  62. float3 m_position : POSITION;
  63. float3 m_normal : NORMAL;
  64. float4 m_tangent : TANGENT;
  65. float4 m_bitangent : BITANGENT;
  66. // Extended fields (only referenced in this azsl file)...
  67. float2 m_uv : UV0;
  68. };
  69. struct VSOutput
  70. {
  71. // Base fields (required by the template azsli file)...
  72. float4 m_position : SV_Position;
  73. float3 m_normal: NORMAL;
  74. float4 m_tangent : TANGENT;
  75. float4 m_bitangent : BITANGENT;
  76. float3 m_worldPosition : UV0;
  77. // Extended fields (only referenced in this azsl file)...
  78. float2 m_uv : UV1;
  79. };
  80. #line 1 "C:/Lumberyard/lyengine/branches/Atom/dev/Gems/Atom/Feature/Common/Assets/Materials/StandardPBR/./MaterialTemplate/ForwardPass.azsli"
  81. /*
  82. * Copyright (c) Contributors to the Open 3D Engine Project.
  83. * For complete copyright and license terms please see the LICENSE at the root of this distribution.
  84. *
  85. * SPDX-License-Identifier: Apache-2.0 OR MIT
  86. *
  87. */
  88. // VSInput, VSOutput, ObjectSrg must be defined before including this file.
  89. // [GFX TODO][ATOM-1402] Remove this and use GetObject_WorldMatrix() instead after AZSLc to HLSL code generation bug is fixed.
  90. float4x4 GetObject_WorldMatrix_TEMP()
  91. {
  92. float4x4 modelToWorld = float4x4(
  93. float4(1, 0, 0, 0),
  94. float4(0, 1, 0, 0),
  95. float4(0, 0, 1, 0),
  96. float4(0, 0, 0, 1));
  97. modelToWorld[0] = PerObject::m_modelToWorld[0];
  98. modelToWorld[1] = PerObject::m_modelToWorld[1];
  99. modelToWorld[2] = PerObject::m_modelToWorld[2];
  100. return modelToWorld;
  101. }
  102. void StandardPbrTemplate_ForwardPass_VS(in VSInput IN, out VSOutput OUT, float3 worldPosition)
  103. {
  104. OUT.m_worldPosition = worldPosition;
  105. OUT.m_position = mul(ViewSrg::m_viewProjectionMatrix, float4(OUT.m_worldPosition, 1.0));
  106. //[GFX TODO] Make this support non-uniform scale. Needs an inverse-transpose matrix.
  107. OUT.m_normal = mul(GetObject_WorldMatrix_TEMP(), float4(IN.m_normal, 0.0)).xyz;
  108. OUT.m_tangent = mul(GetObject_WorldMatrix_TEMP(), IN.m_tangent);
  109. OUT.m_bitangent = mul(GetObject_WorldMatrix_TEMP(), IN.m_bitangent);
  110. }
  111. struct PSOutput
  112. {
  113. float4 m_color : SV_Target0;
  114. };
  115. float4 VisNormal(float3 n)
  116. {
  117. return float4(n * 0.5 + 0.5,1);
  118. }
  119. void StandardPbrTemplate_ForwardPass_PS(in VSOutput IN, out PSOutput OUT, float3 baseColor, float metallic, float roughness, float3 normal, float alpha)
  120. {
  121. // This is just a dummy placeholder that will be replaced later...
  122. static const float3 lightDir = normalize(float3(1,-1,1));
  123. float3 viewDir = normalize(GetView_WorldPosition() - IN.m_worldPosition);
  124. float3 H = normalize(lightDir + viewDir);
  125. float NdotH = dot(normal, H);
  126. float NdotL = dot(normal, lightDir);
  127. float3 diffuse = saturate( NdotL ) * baseColor;
  128. float3 specular = pow( saturate( NdotH ), 50.0) * saturate(0.5-roughness);
  129. OUT.m_color = float4(diffuse + specular, alpha);
  130. }
  131. #line 39 "C:/Lumberyard/lyengine/branches/Atom/dev/Gems/Atom/Feature/Common/Assets/Materials/StandardPBR/StandardPBR_ForwardPass.azsl"
  132. #line 1 "C:/Lumberyard/lyengine/branches/Atom/dev/Gems/Atom/Feature/Common/Assets/Materials/StandardPBR/./StandardPBR_Common.azsli"
  133. /*
  134. * Copyright (c) Contributors to the Open 3D Engine Project.
  135. * For complete copyright and license terms please see the LICENSE at the root of this distribution.
  136. *
  137. * SPDX-License-Identifier: Apache-2.0 OR MIT
  138. *
  139. */
  140. ShaderResourceGroup MaterialSrg : BindingPerMaterial
  141. {
  142. float3 m_baseColor;
  143. float m_baseColorFactor;
  144. Texture2D m_baseColorMap;
  145. Texture2D m_roughnessMap;
  146. Texture2D m_normalMap;
  147. Sampler m_sampler
  148. {
  149. AddressU = Clamp;
  150. AddressV = Clamp;
  151. MinFilter = Linear;
  152. MagFilter = Linear;
  153. MipFilter = Linear;
  154. };
  155. }
  156. #line 41 "C:/Lumberyard/lyengine/branches/Atom/dev/Gems/Atom/Feature/Common/Assets/Materials/StandardPBR/StandardPBR_ForwardPass.azsl"
  157. #line 1 "C:/Lumberyard/lyengine/branches/Atom/dev/Gems/Atom/Feature/Common/Assets/Materials/StandardPBR/../../../../../RPI/Assets/ShaderLib/ShaderResourceGroups/DefaultObjectSrg.azsli"
  158. /*
  159. * Copyright (c) Contributors to the Open 3D Engine Project.
  160. * For complete copyright and license terms please see the LICENSE at the root of this distribution.
  161. *
  162. * SPDX-License-Identifier: Apache-2.0 OR MIT
  163. *
  164. */
  165. ShaderResourceGroup PerObject : BindingPerObject
  166. {
  167. row_major float3x4 m_modelToWorld;
  168. }
  169. float4x4 GetObject_WorldMatrix()
  170. {
  171. float4x4 modelToWorld = float4x4(
  172. float4(1, 0, 0, 0),
  173. float4(0, 1, 0, 0),
  174. float4(0, 0, 1, 0),
  175. float4(0, 0, 0, 1));
  176. modelToWorld[0] = PerObject::m_modelToWorld[0];
  177. modelToWorld[1] = PerObject::m_modelToWorld[1];
  178. modelToWorld[2] = PerObject::m_modelToWorld[2];
  179. return modelToWorld;
  180. }
  181. #line 43 "C:/Lumberyard/lyengine/branches/Atom/dev/Gems/Atom/Feature/Common/Assets/Materials/StandardPBR/StandardPBR_ForwardPass.azsl"
  182. VSOutput StandardPbr_ForwardPassVS(VSInput IN)
  183. {
  184. VSOutput OUT;
  185. float3 worldPosition = mul(GetObject_WorldMatrix(), float4(IN.m_position, 1.0)).xyz;
  186. OUT.m_uv = IN.m_uv;
  187. StandardPbrTemplate_ForwardPass_VS(IN, OUT, worldPosition);
  188. return OUT;
  189. }
  190. PSOutput StandardPbr_ForwardPassPS(VSOutput IN)
  191. {
  192. PSOutput OUT;
  193. // TODO: Figure out how we want our base material to expect channels to be encoded.
  194. float4 baseColorMapSample = MaterialSrg::m_baseColorMap.Sample(MaterialSrg::m_sampler, IN.m_uv);
  195. float3 baseColor = baseColorMapSample.rgb * MaterialSrg::m_baseColor.rgb * MaterialSrg::m_baseColorFactor;
  196. float alpha = baseColorMapSample.a;
  197. // TODO: Figure out how we want our base material to expect channels to be encoded.
  198. float4 roughnessMapSample = MaterialSrg::m_roughnessMap.Sample(MaterialSrg::m_sampler, IN.m_uv);
  199. float roughness = roughnessMapSample.r;
  200. float4 normalMapSample = MaterialSrg::m_normalMap.Sample(MaterialSrg::m_sampler, IN.m_uv);
  201. float3 surfaceNormal;
  202. surfaceNormal.xy = normalMapSample.xy;
  203. surfaceNormal.z = sqrt(1-dot(surfaceNormal.xy, surfaceNormal.xy));
  204. float3 vertexNormal = normalize(IN.m_normal);
  205. float3 vertexTangent = IN.m_tangent.xyz / IN.m_tangent.w;
  206. float3 vertexBitangent = IN.m_bitangent.xyz / IN.m_bitangent.w;
  207. float3x3 tbn = float3x3(vertexTangent, vertexBitangent, vertexNormal);
  208. float3 normal = mul(surfaceNormal, tbn);
  209. // TODO: Get this from a map
  210. float metallic = 0;
  211. StandardPbrTemplate_ForwardPass_PS(IN, OUT, baseColor, metallic, roughness, normal, alpha);
  212. return OUT;
  213. }