3
0

SimpleTextured.azsl 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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 <Atom/Features/SrgSemantics.azsli>
  9. // Indicates whether the sampler should use Wrap or Clamp
  10. option bool o_clamp;
  11. // Indicates whether to use color channels from the texture or only the alpha channel
  12. option bool o_useColorChannels;
  13. ShaderResourceGroup InstanceSrg : SRG_PerDraw
  14. {
  15. row_major float4x4 m_worldToProj;
  16. Texture2D m_texture;
  17. Sampler m_wrapSampler
  18. {
  19. MaxAnisotropy = 16;
  20. AddressU = Wrap;
  21. AddressV = Wrap;
  22. AddressW = Wrap;
  23. };
  24. Sampler m_clampSampler
  25. {
  26. MaxAnisotropy = 16;
  27. AddressU = Clamp;
  28. AddressV = Clamp;
  29. AddressW = Clamp;
  30. };
  31. };
  32. struct VSInput
  33. {
  34. float3 m_position : POSITION;
  35. float4 m_color : COLOR0;
  36. float2 m_uv : TEXCOORD0;
  37. };
  38. struct VSOutput
  39. {
  40. float4 m_position : SV_Position;
  41. float4 m_color : COLOR0;
  42. float2 m_uv : TEXCOORD0;
  43. };
  44. VSOutput MainVS(VSInput IN)
  45. {
  46. float4x4 worldToProj = InstanceSrg::m_worldToProj;
  47. float4 posPS = mul(worldToProj, float4(IN.m_position, 1.0f));
  48. VSOutput OUT;
  49. OUT.m_position = posPS;
  50. OUT.m_color = IN.m_color;
  51. OUT.m_uv = IN.m_uv;
  52. return OUT;
  53. };
  54. struct PSOutput
  55. {
  56. float4 m_color : SV_Target0;
  57. };
  58. PSOutput MainPS(VSOutput IN)
  59. {
  60. PSOutput OUT;
  61. float4 tex;
  62. if (o_clamp)
  63. {
  64. tex = InstanceSrg::m_texture.Sample(InstanceSrg::m_clampSampler, IN.m_uv);
  65. }
  66. else
  67. {
  68. tex = InstanceSrg::m_texture.Sample(InstanceSrg::m_wrapSampler, IN.m_uv);
  69. }
  70. if (!o_useColorChannels)
  71. {
  72. // When getting rgba from an R8 the "r" channel will be the value from the texture.
  73. // We want to put the r in the alpha (to use as opacity) and set the rgb to 1.
  74. // We do this rather than using an A8 texture because A8 is not supported on Vulkan.
  75. tex.a = tex.r;
  76. tex.rgb = 1.0f;
  77. }
  78. float opacity = IN.m_color.a * tex.a;
  79. // We use pre-multiplied alpha here since it is more flexible. For example, it enables alpha-blended rendering to
  80. // a render target and then alpha blending that render target into another render target
  81. OUT.m_color.rgb = IN.m_color.rgb * tex.rgb * opacity;
  82. OUT.m_color.a = opacity;
  83. return OUT;
  84. };