VertexLoader_Position.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. // Copyright 2008 Dolphin Emulator Project
  2. // Licensed under GPLv2+
  3. // Refer to the license.txt file included.
  4. #include <type_traits>
  5. #include "Common/CommonTypes.h"
  6. #include "VideoCommon/VertexLoader.h"
  7. #include "VideoCommon/VertexLoader_Position.h"
  8. #include "VideoCommon/VertexLoaderManager.h"
  9. #include "VideoCommon/VertexManagerBase.h"
  10. #include "VideoCommon/VideoCommon.h"
  11. template <typename T>
  12. float PosScale(T val, float scale)
  13. {
  14. return val * scale;
  15. }
  16. template <>
  17. float PosScale(float val, float scale)
  18. {
  19. return val;
  20. }
  21. template <typename T, int N>
  22. void LOADERDECL Pos_ReadDirect(VertexLoader* loader)
  23. {
  24. static_assert(N <= 3, "N > 3 is not sane!");
  25. auto const scale = loader->m_posScale;
  26. DataReader dst(g_vertex_manager_write_ptr, nullptr);
  27. DataReader src(g_video_buffer_read_ptr, nullptr);
  28. for (int i = 0; i < N; ++i)
  29. {
  30. float value = PosScale(src.Read<T>(), scale);
  31. if (loader->m_counter < 3)
  32. VertexLoaderManager::position_cache[loader->m_counter][i] = value;
  33. dst.Write(value);
  34. }
  35. g_vertex_manager_write_ptr = dst.GetPointer();
  36. g_video_buffer_read_ptr = src.GetPointer();
  37. LOG_VTX();
  38. }
  39. template <typename I, typename T, int N>
  40. void LOADERDECL Pos_ReadIndex(VertexLoader* loader)
  41. {
  42. static_assert(std::is_unsigned<I>::value, "Only unsigned I is sane!");
  43. static_assert(N <= 3, "N > 3 is not sane!");
  44. auto const index = DataRead<I>();
  45. loader->m_vertexSkip = index == std::numeric_limits<I>::max();
  46. auto const data = reinterpret_cast<const T*>(VertexLoaderManager::cached_arraybases[ARRAY_POSITION] + (index * g_main_cp_state.array_strides[ARRAY_POSITION]));
  47. auto const scale = loader->m_posScale;
  48. DataReader dst(g_vertex_manager_write_ptr, nullptr);
  49. for (int i = 0; i < N; ++i)
  50. {
  51. float value = PosScale(Common::FromBigEndian(data[i]), scale);
  52. if (loader->m_counter < 3)
  53. VertexLoaderManager::position_cache[loader->m_counter][i] = value;
  54. dst.Write(value);
  55. }
  56. g_vertex_manager_write_ptr = dst.GetPointer();
  57. LOG_VTX();
  58. }
  59. static TPipelineFunction tableReadPosition[4][8][2] = {
  60. {
  61. {nullptr, nullptr,},
  62. {nullptr, nullptr,},
  63. {nullptr, nullptr,},
  64. {nullptr, nullptr,},
  65. {nullptr, nullptr,},
  66. },
  67. {
  68. {Pos_ReadDirect<u8, 2>, Pos_ReadDirect<u8, 3>,},
  69. {Pos_ReadDirect<s8, 2>, Pos_ReadDirect<s8, 3>,},
  70. {Pos_ReadDirect<u16, 2>, Pos_ReadDirect<u16, 3>,},
  71. {Pos_ReadDirect<s16, 2>, Pos_ReadDirect<s16, 3>,},
  72. {Pos_ReadDirect<float, 2>, Pos_ReadDirect<float, 3>,},
  73. },
  74. {
  75. {Pos_ReadIndex<u8, u8, 2>, Pos_ReadIndex<u8, u8, 3>,},
  76. {Pos_ReadIndex<u8, s8, 2>, Pos_ReadIndex<u8, s8, 3>,},
  77. {Pos_ReadIndex<u8, u16, 2>, Pos_ReadIndex<u8, u16, 3>,},
  78. {Pos_ReadIndex<u8, s16, 2>, Pos_ReadIndex<u8, s16, 3>,},
  79. {Pos_ReadIndex<u8, float, 2>, Pos_ReadIndex<u8, float, 3>,},
  80. },
  81. {
  82. {Pos_ReadIndex<u16, u8, 2>, Pos_ReadIndex<u16, u8, 3>,},
  83. {Pos_ReadIndex<u16, s8, 2>, Pos_ReadIndex<u16, s8, 3>,},
  84. {Pos_ReadIndex<u16, u16, 2>, Pos_ReadIndex<u16, u16, 3>,},
  85. {Pos_ReadIndex<u16, s16, 2>, Pos_ReadIndex<u16, s16, 3>,},
  86. {Pos_ReadIndex<u16, float, 2>, Pos_ReadIndex<u16, float, 3>,},
  87. },
  88. };
  89. static int tableReadPositionVertexSize[4][8][2] = {
  90. {
  91. {0, 0,}, {0, 0,}, {0, 0,}, {0, 0,}, {0, 0,},
  92. },
  93. {
  94. {2, 3,}, {2, 3,}, {4, 6,}, {4, 6,}, {8, 12,},
  95. },
  96. {
  97. {1, 1,}, {1, 1,}, {1, 1,}, {1, 1,}, {1, 1,},
  98. },
  99. {
  100. {2, 2,}, {2, 2,}, {2, 2,}, {2, 2,}, {2, 2,},
  101. },
  102. };
  103. unsigned int VertexLoader_Position::GetSize(u64 _type, unsigned int _format, unsigned int _elements)
  104. {
  105. return tableReadPositionVertexSize[_type][_format][_elements];
  106. }
  107. TPipelineFunction VertexLoader_Position::GetFunction(u64 _type, unsigned int _format, unsigned int _elements)
  108. {
  109. return tableReadPosition[_type][_format][_elements];
  110. }