HairUtilities.azsli 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /*
  2. * Modifications 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. //---------------------------------------------------------------------------------------
  9. // Shader code utilities for TressFX
  10. //-------------------------------------------------------------------------------------
  11. //
  12. // Copyright (c) 2019 Advanced Micro Devices, Inc. All rights reserved.
  13. //
  14. // Permission is hereby granted, free of charge, to any person obtaining a copy
  15. // of this software and associated documentation files (the "Software"), to deal
  16. // in the Software without restriction, including without limitation the rights
  17. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  18. // copies of the Software, and to permit persons to whom the Software is
  19. // furnished to do so, subject to the following conditions:
  20. //
  21. // The above copyright notice and this permission notice shall be included in
  22. // all copies or substantial portions of the Software.
  23. //
  24. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  25. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  26. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  27. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  28. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  29. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  30. // THE SOFTWARE.
  31. //
  32. // Cutoff to not render hair
  33. #pragma once
  34. #define SHORTCUT_MIN_ALPHA 0.02
  35. #define TRESSFX_FLOAT_EPSILON 1e-7
  36. // Needed for DirectionalLightShadow.azsli - o_opacity_mode exists in most shaders but not this one.
  37. option enum class OpacityMode
  38. {
  39. Opaque,
  40. Cutout,
  41. Blended,
  42. TintedTransparent
  43. } o_opacity_mode = OpacityMode::Blended;
  44. //--------------------------------------------------------------------------------------
  45. //
  46. // Controls whether you do mul(M,v) or mul(v,M)
  47. // i.e., row major vs column major
  48. //
  49. //--------------------------------------------------------------------------------------
  50. float4 MatrixMult(float4x4 m, float4 v)
  51. {
  52. return mul(m, v);
  53. }
  54. // Pack a float4 into an uint
  55. uint PackFloat4IntoUint(float4 vValue)
  56. {
  57. return (((uint)(vValue.x * 255)) << 24) | (((uint)(vValue.y * 255)) << 16) | (((uint)(vValue.z * 255)) << 8) | (uint)(vValue.w * 255);
  58. }
  59. // Unpack a uint into a float4 value
  60. float4 UnpackUintIntoFloat4(uint uValue)
  61. {
  62. return float4(((uValue & 0xFF000000) >> 24) / 255.0, ((uValue & 0x00FF0000) >> 16) / 255.0, ((uValue & 0x0000FF00) >> 8) / 255.0, ((uValue & 0x000000FF)) / 255.0);
  63. }
  64. // Pack a float3 and a uint8 into an uint
  65. uint PackFloat3ByteIntoUint(float3 vValue, uint uByteValue)
  66. {
  67. return (((uint)(vValue.x * 255)) << 24) | (((uint)(vValue.y * 255)) << 16) | (((uint)(vValue.z * 255)) << 8) | uByteValue;
  68. }
  69. // Unpack a uint into a float3 and a uint8 value
  70. float3 UnpackUintIntoFloat3Byte(uint uValue, out uint uByteValue)
  71. {
  72. uByteValue = uValue & 0x000000FF;
  73. return float3(((uValue & 0xFF000000) >> 24) / 255.0, ((uValue & 0x00FF0000) >> 16) / 255.0, ((uValue & 0x0000FF00) >> 8) / 255.0);
  74. }
  75. //--------------------------------------------------------------------------------------
  76. //
  77. // Safe_normalize-float2
  78. //
  79. //--------------------------------------------------------------------------------------
  80. float2 Safe_normalize(float2 vec)
  81. {
  82. float len = length(vec);
  83. return len >= TRESSFX_FLOAT_EPSILON ? (vec * rcp(len)) : float2(0, 0);
  84. }
  85. //--------------------------------------------------------------------------------------
  86. //
  87. // Safe_normalize-float3
  88. //
  89. //--------------------------------------------------------------------------------------
  90. float3 Safe_normalize(float3 vec)
  91. {
  92. float len = length(vec);
  93. return len >= TRESSFX_FLOAT_EPSILON ? (vec * rcp(len)) : float3(0, 0, 0);
  94. }
  95. //--------------------------------------------------------------------------------------
  96. // ComputeCoverage
  97. //
  98. // Calculate the pixel coverage of a hair strand by computing the hair width
  99. //--------------------------------------------------------------------------------------
  100. float ComputeCoverage(float2 p0, float2 p1, float2 pixelLoc, float2 winSize)
  101. {
  102. // p0, p1, pixelLoc are in d3d clip space (-1 to 1)x(-1 to 1)
  103. // Scale positions so 1.f = half pixel width
  104. p0 *= winSize;
  105. p1 *= winSize;
  106. pixelLoc *= winSize;
  107. float p0dist = length(p0 - pixelLoc);
  108. float p1dist = length(p1 - pixelLoc);
  109. float hairWidth = length(p0 - p1);
  110. // will be 1.f if pixel outside hair, 0.f if pixel inside hair
  111. float outside = any(float2(step(hairWidth, p0dist), step(hairWidth, p1dist)));
  112. // if outside, set sign to -1, else set sign to 1
  113. float sign = outside > 0.f ? -1.f : 1.f;
  114. // signed distance (positive if inside hair, negative if outside hair)
  115. float relDist = sign * saturate(min(p0dist, p1dist));
  116. // returns coverage based on the relative distance
  117. // 0, if completely outside hair edge
  118. // 1, if completely inside hair edge
  119. return (relDist + 1.f) * 0.5f;
  120. }