3
0

MorphTargetDelta.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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/RPI.Reflect/Model/MorphTargetDelta.h>
  9. namespace AZ::RPI
  10. {
  11. static_assert(sizeof(PackedCompressedMorphTargetDelta) == 32, "The morph target compute shader expects a structured buffer that is exactly 32 bytes per element. If you change MorphTargetDelta, be sure to update MorphTargetSRG.azsli");
  12. PackedCompressedMorphTargetDelta PackMorphTargetDelta(const CompressedMorphTargetDelta& compressedDelta)
  13. {
  14. PackedCompressedMorphTargetDelta packedDelta{ 0,0,0,0,0, {0,0,0} };
  15. packedDelta.m_morphedVertexIndex = compressedDelta.m_morphedVertexIndex;
  16. // Position x is in the most significant 16 bits, y is in the least significant 16 bits
  17. packedDelta.m_positionXY |= (static_cast<uint32_t>(compressedDelta.m_positionX) << 16);
  18. packedDelta.m_positionXY |= static_cast<uint32_t>(compressedDelta.m_positionY);
  19. // Position z is in the most significant 16 bits
  20. packedDelta.m_positionZNormalXY |= static_cast<uint32_t>(compressedDelta.m_positionZ) << 16;
  21. // Normal x and y are in the least significant 16 bits (8 bits per channel)
  22. packedDelta.m_positionZNormalXY |= static_cast<uint32_t>(compressedDelta.m_normalX) << 8;
  23. packedDelta.m_positionZNormalXY |= static_cast<uint32_t>(compressedDelta.m_normalY);
  24. // Normal z is in the most significant 8 bits
  25. packedDelta.m_normalZTangentXYZ |= static_cast<uint32_t>(compressedDelta.m_normalZ) << 24;
  26. // Tangent x, y, and z are in the least significant 24 bits (8 bits per channel)
  27. packedDelta.m_normalZTangentXYZ |= static_cast<uint32_t>(compressedDelta.m_tangentX) << 16;
  28. packedDelta.m_normalZTangentXYZ |= static_cast<uint32_t>(compressedDelta.m_tangentY) << 8;
  29. packedDelta.m_normalZTangentXYZ |= static_cast<uint32_t>(compressedDelta.m_tangentZ);
  30. // Bitangents are in the least significant 24 bits (8 bits per channel)
  31. packedDelta.m_padBitangentXYZ |= static_cast<uint32_t>(compressedDelta.m_bitangentX) << 16;
  32. packedDelta.m_padBitangentXYZ |= static_cast<uint32_t>(compressedDelta.m_bitangentY) << 8;
  33. packedDelta.m_padBitangentXYZ |= static_cast<uint32_t>(compressedDelta.m_bitangentZ);
  34. return packedDelta;
  35. }
  36. CompressedMorphTargetDelta UnpackMorphTargetDelta(const PackedCompressedMorphTargetDelta& packedDelta)
  37. {
  38. CompressedMorphTargetDelta compressedDelta;
  39. compressedDelta.m_morphedVertexIndex = packedDelta.m_morphedVertexIndex;
  40. // Position x is in the most significant 16 bits, y is in the least significant 16 bits
  41. compressedDelta.m_positionX = packedDelta.m_positionXY >> 16;
  42. compressedDelta.m_positionY = packedDelta.m_positionXY & 0x0000FFFF;
  43. // Position z is in the most significant 16 bits
  44. compressedDelta.m_positionZ = packedDelta.m_positionZNormalXY >> 16;
  45. // Normal x and y are in the least significant 16 bits (8 bits per channel)
  46. compressedDelta.m_normalX = (packedDelta.m_positionZNormalXY >> 8) & 0x000000FF;
  47. compressedDelta.m_normalY = packedDelta.m_positionZNormalXY & 0x000000FF;
  48. // Normal z is in the most significant 8 bits
  49. compressedDelta.m_normalZ = packedDelta.m_normalZTangentXYZ >> 24;
  50. // Tangent x, y, and z are in the least significant 24 bits (8 bits per channel)
  51. compressedDelta.m_tangentX = (packedDelta.m_normalZTangentXYZ >> 16) & 0x000000FF;
  52. compressedDelta.m_tangentY = (packedDelta.m_normalZTangentXYZ >> 8 ) & 0x000000FF;
  53. compressedDelta.m_tangentZ = packedDelta.m_normalZTangentXYZ & 0x000000FF;
  54. // Bitangents are in the least significant 24 bits (8 bits per channel)
  55. compressedDelta.m_bitangentX = (packedDelta.m_padBitangentXYZ >> 16) & 0x000000FF;
  56. compressedDelta.m_bitangentY = (packedDelta.m_padBitangentXYZ >> 8 ) & 0x000000FF;
  57. compressedDelta.m_bitangentZ = packedDelta.m_padBitangentXYZ & 0x000000FF;
  58. return compressedDelta;
  59. }
  60. } // namespace AZ::RPI