HairCommon.h 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. // Jolt Physics Library (https://github.com/jrouwe/JoltPhysics)
  2. // SPDX-FileCopyrightText: 2026 Jorrit Rouwe
  3. // SPDX-License-Identifier: MIT
  4. #include "ShaderMath.h"
  5. #include "ShaderMat44.h"
  6. #include "ShaderQuat.h"
  7. #include "ShaderPlane.h"
  8. #include "ShaderVec3.h"
  9. // The density and velocity fields are stored in fixed point while accumulating, this constant converts from float to fixed point.
  10. JPH_SHADER_CONSTANT(int, cFloatToFixed, 1 << 10)
  11. JPH_SHADER_CONSTANT(float, cFixedToFloat, 1.0f / float(cFloatToFixed))
  12. bool IsVertexFixed(JPH_SHADER_BUFFER(JPH_uint) inVertexFixed, uint inVertexIndex)
  13. {
  14. return (inVertexFixed[inVertexIndex >> 5] & (1u << (inVertexIndex & 31))) != 0;
  15. }
  16. float GetVertexInvMass(JPH_SHADER_BUFFER(JPH_uint) inVertexFixed, uint inVertexIndex)
  17. {
  18. return IsVertexFixed(inVertexFixed, inVertexIndex)? 0.0f : 1.0f;
  19. }
  20. float GetVertexStrandFraction(JPH_SHADER_BUFFER(JPH_uint) inStrandFractions, uint inVertexIndex)
  21. {
  22. return ((inStrandFractions[inVertexIndex >> 2] >> ((inVertexIndex & 3) << 3)) & 0xff) * (1.0f / 255.0f);
  23. }
  24. uint GetStrandVertexCount(JPH_SHADER_BUFFER(JPH_uint) inStrandVertexCounts, uint inStrandIndex)
  25. {
  26. return (inStrandVertexCounts[inStrandIndex >> 2] >> ((inStrandIndex & 3) << 3)) & 0xff;
  27. }
  28. uint GetStrandMaterialIndex(JPH_SHADER_BUFFER(JPH_uint) inStrandMaterialIndex, uint inStrandIndex)
  29. {
  30. return (inStrandMaterialIndex[inStrandIndex >> 2] >> ((inStrandIndex & 3) << 3)) & 0xff;
  31. }
  32. float GradientSamplerSample(float4 inSampler, float inStrandFraction)
  33. {
  34. return min(inSampler.w, max(inSampler.z, inSampler.y + inStrandFraction * inSampler.x));
  35. }
  36. void GridPositionToIndexAndFraction(float3 inPosition, JPH_OUT(uint3) outIndex, JPH_OUT(float3) outFraction)
  37. {
  38. // Get position in grid space
  39. float3 grid_pos = min(max(inPosition - cGridOffset, float3(0, 0, 0)) * cGridScale, cGridSizeMin1);
  40. outIndex = min(uint3(grid_pos), cGridSizeMin2);
  41. outFraction = grid_pos - float3(outIndex);
  42. }
  43. uint GridIndexToBufferIndex(uint3 inIndex)
  44. {
  45. return inIndex.x + inIndex.y * cGridStride.y + inIndex.z * cGridStride.z;
  46. }