ImageMips.azsl 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. #include <Atom/Features/ColorManagement/TransformColor.azsli>
  10. ShaderResourceGroup ImageMipsSrg : SRG_PerDraw
  11. {
  12. Texture2D m_texture;
  13. int m_residentMip;
  14. float2 m_position;
  15. float2 m_size;
  16. Sampler m_sampler
  17. {
  18. MaxAnisotropy = 16;
  19. MinFilter = Linear;
  20. MagFilter = Linear;
  21. AddressU = Wrap;
  22. AddressV = Wrap;
  23. AddressW = Wrap;
  24. };
  25. }
  26. struct VSInput
  27. {
  28. uint m_vertexIndex :SV_VertexID;
  29. uint m_instanceIndex :SV_InstanceID;
  30. };
  31. struct VSOutput
  32. {
  33. float4 m_position : SV_Position;
  34. float3 m_uvlod : UV0;
  35. };
  36. struct PSOutput
  37. {
  38. float4 m_color : SV_Target0;
  39. };
  40. VSOutput MainVS(VSInput vsInput)
  41. {
  42. VSOutput OUT;
  43. float2 uvs[4] = { {0.0, 0.0}, {1.0, 0.0}, {0.0, 1.0}, {1.0, 1.0} };
  44. int index = vsInput.m_vertexIndex%4;
  45. int mipLevel = vsInput.m_instanceIndex;
  46. float2 mipSize = ImageMipsSrg::m_size;
  47. float2 offset = {0.0, 0.0};
  48. bool offsetX = true;
  49. for (int mip = 0; mip<mipLevel; mip++)
  50. {
  51. if (offsetX)
  52. {
  53. offset.x += mipSize.x;
  54. }
  55. else
  56. {
  57. offset.y += mipSize.y;
  58. }
  59. offsetX = !offsetX;
  60. mipSize /= 2;
  61. }
  62. float2 startPosition = ImageMipsSrg::m_position + offset;
  63. OUT.m_position = float4(startPosition + uvs[index]*mipSize, 0, 1.0);
  64. OUT.m_uvlod.xy = uvs[index];
  65. OUT.m_uvlod.z = mipLevel-ImageMipsSrg::m_residentMip;
  66. return OUT;
  67. }
  68. PSOutput MainPS(VSOutput psInput)
  69. {
  70. PSOutput OUT;
  71. if (psInput.m_uvlod.z < 0)
  72. {
  73. OUT.m_color = float4(1.0, 1.0, 1.0, 1.0);
  74. }
  75. else
  76. {
  77. float3 texColor = ImageMipsSrg::m_texture.SampleLevel(ImageMipsSrg::m_sampler, psInput.m_uvlod.xy, psInput.m_uvlod.z).xyz;
  78. OUT.m_color = float4(TransformColor(texColor, ColorSpaceId::LinearSRGB, ColorSpaceId::SRGB), 1.0);
  79. }
  80. return OUT;
  81. }