pose_palette_cache.h 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. #pragma once
  2. #include "gl/humanoid/humanoid_types.h"
  3. #include "template_cache.h"
  4. #include <array>
  5. #include <cstdint>
  6. #include <mutex>
  7. #include <unordered_map>
  8. #include <vector>
  9. namespace Render::GL {
  10. struct PosePaletteKey {
  11. AnimState state{AnimState::Idle};
  12. std::uint8_t frame{0};
  13. bool is_moving{false};
  14. bool operator==(const PosePaletteKey &o) const {
  15. return state == o.state && frame == o.frame && is_moving == o.is_moving;
  16. }
  17. };
  18. struct PosePaletteKeyHash {
  19. std::size_t operator()(const PosePaletteKey &k) const noexcept {
  20. std::size_t h = static_cast<std::size_t>(k.state);
  21. h ^= static_cast<std::size_t>(k.frame) + 0x9e3779b9 + (h << 6) + (h >> 2);
  22. h ^= static_cast<std::size_t>(k.is_moving) + 0x9e3779b9 + (h << 6) +
  23. (h >> 2);
  24. return h;
  25. }
  26. };
  27. struct PosePaletteEntry {
  28. HumanoidPose pose;
  29. float time{0.0F};
  30. };
  31. class PosePaletteCache {
  32. public:
  33. static auto instance() noexcept -> PosePaletteCache & {
  34. static PosePaletteCache inst;
  35. return inst;
  36. }
  37. void generate();
  38. auto get(const PosePaletteKey &key) const -> const PosePaletteEntry *;
  39. void clear();
  40. [[nodiscard]] auto size() const -> std::size_t;
  41. [[nodiscard]] auto is_generated() const -> bool { return m_generated; }
  42. private:
  43. PosePaletteCache() = default;
  44. std::unordered_map<PosePaletteKey, PosePaletteEntry, PosePaletteKeyHash>
  45. m_palette;
  46. mutable std::mutex m_mutex;
  47. bool m_generated{false};
  48. };
  49. } // namespace Render::GL