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