NativeVertexFormat.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. // Copyright 2008 Dolphin Emulator Project
  2. // SPDX-License-Identifier: GPL-2.0-or-later
  3. #pragma once
  4. #include <cstring>
  5. #include <functional> // for hash
  6. #include "Common/CommonTypes.h"
  7. #include "VideoCommon/CPMemory.h"
  8. // m_components
  9. enum : u32
  10. {
  11. VB_HAS_POSMTXIDX = (1 << 1),
  12. VB_HAS_TEXMTXIDX0 = (1 << 2),
  13. VB_HAS_TEXMTXIDX1 = (1 << 3),
  14. VB_HAS_TEXMTXIDX2 = (1 << 4),
  15. VB_HAS_TEXMTXIDX3 = (1 << 5),
  16. VB_HAS_TEXMTXIDX4 = (1 << 6),
  17. VB_HAS_TEXMTXIDX5 = (1 << 7),
  18. VB_HAS_TEXMTXIDX6 = (1 << 8),
  19. VB_HAS_TEXMTXIDX7 = (1 << 9),
  20. VB_HAS_TEXMTXIDXALL = (0xff << 2),
  21. // VB_HAS_POS=0, // Implied, it always has pos! don't bother testing
  22. VB_HAS_NORMAL = (1 << 10),
  23. VB_HAS_TANGENT = (1 << 11),
  24. VB_HAS_BINORMAL = (1 << 12),
  25. VB_COL_SHIFT = 13,
  26. VB_HAS_COL0 = (1 << 13),
  27. VB_HAS_COL1 = (1 << 14),
  28. VB_HAS_UV0 = (1 << 15),
  29. VB_HAS_UV1 = (1 << 16),
  30. VB_HAS_UV2 = (1 << 17),
  31. VB_HAS_UV3 = (1 << 18),
  32. VB_HAS_UV4 = (1 << 19),
  33. VB_HAS_UV5 = (1 << 20),
  34. VB_HAS_UV6 = (1 << 21),
  35. VB_HAS_UV7 = (1 << 22),
  36. VB_HAS_UVALL = (0xff << 15),
  37. VB_HAS_UVTEXMTXSHIFT = 13,
  38. };
  39. struct AttributeFormat
  40. {
  41. ComponentFormat type;
  42. int components;
  43. int offset;
  44. bool enable;
  45. bool integer;
  46. };
  47. struct PortableVertexDeclaration
  48. {
  49. int stride;
  50. AttributeFormat position;
  51. std::array<AttributeFormat, 3> normals;
  52. std::array<AttributeFormat, 2> colors;
  53. std::array<AttributeFormat, 8> texcoords;
  54. AttributeFormat posmtx;
  55. // Make sure we initialize padding to 0 since padding is included in the == memcmp
  56. PortableVertexDeclaration() { memset(this, 0, sizeof(*this)); }
  57. inline bool operator<(const PortableVertexDeclaration& b) const
  58. {
  59. return memcmp(this, &b, sizeof(PortableVertexDeclaration)) < 0;
  60. }
  61. inline bool operator==(const PortableVertexDeclaration& b) const
  62. {
  63. return memcmp(this, &b, sizeof(PortableVertexDeclaration)) == 0;
  64. }
  65. };
  66. static_assert(std::is_trivially_copyable_v<PortableVertexDeclaration>,
  67. "Make sure we can memset-initialize");
  68. template <>
  69. struct std::hash<PortableVertexDeclaration>
  70. {
  71. // Implementation from Wikipedia.
  72. template <typename T>
  73. static u32 Fletcher32(const T& data)
  74. {
  75. static_assert(sizeof(T) % sizeof(u16) == 0);
  76. auto buf = reinterpret_cast<const u16*>(&data);
  77. size_t len = sizeof(T) / sizeof(u16);
  78. u32 sum1 = 0xffff, sum2 = 0xffff;
  79. while (len)
  80. {
  81. size_t tlen = len > 360 ? 360 : len;
  82. len -= tlen;
  83. do
  84. {
  85. sum1 += *buf++;
  86. sum2 += sum1;
  87. } while (--tlen);
  88. sum1 = (sum1 & 0xffff) + (sum1 >> 16);
  89. sum2 = (sum2 & 0xffff) + (sum2 >> 16);
  90. }
  91. // Second reduction step to reduce sums to 16 bits
  92. sum1 = (sum1 & 0xffff) + (sum1 >> 16);
  93. sum2 = (sum2 & 0xffff) + (sum2 >> 16);
  94. return (sum2 << 16 | sum1);
  95. }
  96. size_t operator()(const PortableVertexDeclaration& decl) const noexcept
  97. {
  98. return Fletcher32(decl);
  99. }
  100. };
  101. // The implementation of this class is specific for GL/DX, so NativeVertexFormat.cpp
  102. // is in the respective backend, not here in VideoCommon.
  103. // Note that this class can't just invent arbitrary vertex formats out of its input -
  104. // all the data loading code must always be made compatible.
  105. class NativeVertexFormat
  106. {
  107. public:
  108. NativeVertexFormat(const PortableVertexDeclaration& vtx_decl) : m_decl(vtx_decl) {}
  109. virtual ~NativeVertexFormat() {}
  110. NativeVertexFormat(const NativeVertexFormat&) = delete;
  111. NativeVertexFormat& operator=(const NativeVertexFormat&) = delete;
  112. NativeVertexFormat(NativeVertexFormat&&) = default;
  113. NativeVertexFormat& operator=(NativeVertexFormat&&) = default;
  114. u32 GetVertexStride() const { return m_decl.stride; }
  115. const PortableVertexDeclaration& GetVertexDeclaration() const { return m_decl; }
  116. protected:
  117. PortableVertexDeclaration m_decl;
  118. };