VertexLoader.cpp 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. // Copyright 2008 Dolphin Emulator Project
  2. // SPDX-License-Identifier: GPL-2.0-or-later
  3. #include "VideoCommon/VertexLoader.h"
  4. #include "Common/Assert.h"
  5. #include "Common/CommonTypes.h"
  6. #include "VideoCommon/VertexLoaderManager.h"
  7. #include "VideoCommon/VertexLoaderUtils.h"
  8. #include "VideoCommon/VertexLoader_Color.h"
  9. #include "VideoCommon/VertexLoader_Normal.h"
  10. #include "VideoCommon/VertexLoader_Position.h"
  11. #include "VideoCommon/VertexLoader_TextCoord.h"
  12. #include "VideoCommon/VideoCommon.h"
  13. // This pointer is used as the source/dst for all fixed function loader calls
  14. const u8* g_video_buffer_read_ptr;
  15. u8* g_vertex_manager_write_ptr;
  16. static void PosMtx_ReadDirect_UByte(VertexLoader* loader)
  17. {
  18. u32 posmtx = DataRead<u8>() & 0x3f;
  19. if (loader->m_remaining < 3)
  20. VertexLoaderManager::position_matrix_index_cache[loader->m_remaining] = posmtx;
  21. DataWrite<u32>(posmtx);
  22. PRIM_LOG("posmtx: {}, ", posmtx);
  23. }
  24. static void TexMtx_ReadDirect_UByte(VertexLoader* loader)
  25. {
  26. loader->m_curtexmtx[loader->m_texmtxread] = DataRead<u8>() & 0x3f;
  27. PRIM_LOG("texmtx{}: {}, ", loader->m_texmtxread, loader->m_curtexmtx[loader->m_texmtxread]);
  28. loader->m_texmtxread++;
  29. }
  30. static void TexMtx_Write_Float(VertexLoader* loader)
  31. {
  32. DataWrite(float(loader->m_curtexmtx[loader->m_texmtxwrite++]));
  33. }
  34. static void TexMtx_Write_Float2(VertexLoader* loader)
  35. {
  36. DataWrite(0.f);
  37. DataWrite(float(loader->m_curtexmtx[loader->m_texmtxwrite++]));
  38. }
  39. static void TexMtx_Write_Float3(VertexLoader* loader)
  40. {
  41. DataWrite(0.f);
  42. DataWrite(0.f);
  43. DataWrite(float(loader->m_curtexmtx[loader->m_texmtxwrite++]));
  44. }
  45. static void SkipVertex(VertexLoader* loader)
  46. {
  47. if (loader->m_vertexSkip)
  48. {
  49. // reset the output buffer
  50. g_vertex_manager_write_ptr -= loader->m_native_vtx_decl.stride;
  51. loader->m_skippedVertices++;
  52. }
  53. }
  54. VertexLoader::VertexLoader(const TVtxDesc& vtx_desc, const VAT& vtx_attr)
  55. : VertexLoaderBase(vtx_desc, vtx_attr)
  56. {
  57. CompileVertexTranslator();
  58. // generate frac factors
  59. m_posScale = 1.0f / (1U << m_VtxAttr.g0.PosFrac);
  60. for (u32 i = 0; i < 8; i++)
  61. m_tcScale[i] = 1.0f / (1U << m_VtxAttr.GetTexFrac(i));
  62. }
  63. void VertexLoader::CompileVertexTranslator()
  64. {
  65. // Position in pc vertex format.
  66. int nat_offset = 0;
  67. // Position Matrix Index
  68. if (m_VtxDesc.low.PosMatIdx)
  69. {
  70. WriteCall(PosMtx_ReadDirect_UByte);
  71. m_native_vtx_decl.posmtx.components = 4;
  72. m_native_vtx_decl.posmtx.enable = true;
  73. m_native_vtx_decl.posmtx.offset = nat_offset;
  74. m_native_vtx_decl.posmtx.type = ComponentFormat::UByte;
  75. m_native_vtx_decl.posmtx.integer = true;
  76. nat_offset += 4;
  77. }
  78. for (auto texmtxidx : m_VtxDesc.low.TexMatIdx)
  79. {
  80. if (texmtxidx)
  81. WriteCall(TexMtx_ReadDirect_UByte);
  82. }
  83. // Write vertex position loader
  84. WriteCall(VertexLoader_Position::GetFunction(m_VtxDesc.low.Position, m_VtxAttr.g0.PosFormat,
  85. m_VtxAttr.g0.PosElements));
  86. int pos_elements = m_VtxAttr.g0.PosElements == CoordComponentCount::XY ? 2 : 3;
  87. m_native_vtx_decl.position.components = pos_elements;
  88. m_native_vtx_decl.position.enable = true;
  89. m_native_vtx_decl.position.offset = nat_offset;
  90. m_native_vtx_decl.position.type = ComponentFormat::Float;
  91. m_native_vtx_decl.position.integer = false;
  92. nat_offset += pos_elements * sizeof(float);
  93. // Normals
  94. if (m_VtxDesc.low.Normal != VertexComponentFormat::NotPresent)
  95. {
  96. TPipelineFunction pFunc =
  97. VertexLoader_Normal::GetFunction(m_VtxDesc.low.Normal, m_VtxAttr.g0.NormalFormat,
  98. m_VtxAttr.g0.NormalElements, m_VtxAttr.g0.NormalIndex3);
  99. if (pFunc == nullptr)
  100. {
  101. PanicAlertFmt("VertexLoader_Normal::GetFunction({} {} {} {}) returned zero!",
  102. m_VtxDesc.low.Normal, m_VtxAttr.g0.NormalFormat, m_VtxAttr.g0.NormalElements,
  103. m_VtxAttr.g0.NormalIndex3);
  104. }
  105. WriteCall(pFunc);
  106. for (int i = 0; i < (m_VtxAttr.g0.NormalElements == NormalComponentCount::NTB ? 3 : 1); i++)
  107. {
  108. m_native_vtx_decl.normals[i].components = 3;
  109. m_native_vtx_decl.normals[i].enable = true;
  110. m_native_vtx_decl.normals[i].offset = nat_offset;
  111. m_native_vtx_decl.normals[i].type = ComponentFormat::Float;
  112. m_native_vtx_decl.normals[i].integer = false;
  113. nat_offset += 12;
  114. }
  115. }
  116. for (size_t i = 0; i < m_VtxDesc.low.Color.Size(); i++)
  117. {
  118. m_native_vtx_decl.colors[i].components = 4;
  119. m_native_vtx_decl.colors[i].type = ComponentFormat::UByte;
  120. m_native_vtx_decl.colors[i].integer = false;
  121. TPipelineFunction pFunc =
  122. VertexLoader_Color::GetFunction(m_VtxDesc.low.Color[i], m_VtxAttr.GetColorFormat(i));
  123. if (pFunc != nullptr)
  124. {
  125. WriteCall(pFunc);
  126. }
  127. else
  128. {
  129. ASSERT(m_VtxDesc.low.Color[i] == VertexComponentFormat::NotPresent);
  130. // Keep colIndex in sync if color 0 is absent but color 1 is present
  131. if (i == 0 && m_VtxDesc.low.Color[1] != VertexComponentFormat::NotPresent)
  132. WriteCall(VertexLoader_Color::GetDummyFunction());
  133. }
  134. if (m_VtxDesc.low.Color[i] != VertexComponentFormat::NotPresent)
  135. {
  136. m_native_vtx_decl.colors[i].offset = nat_offset;
  137. m_native_vtx_decl.colors[i].enable = true;
  138. nat_offset += 4;
  139. }
  140. }
  141. // Texture matrix indices (remove if corresponding texture coordinate isn't enabled)
  142. for (size_t i = 0; i < m_VtxDesc.high.TexCoord.Size(); i++)
  143. {
  144. m_native_vtx_decl.texcoords[i].offset = nat_offset;
  145. m_native_vtx_decl.texcoords[i].type = ComponentFormat::Float;
  146. m_native_vtx_decl.texcoords[i].integer = false;
  147. const auto tc = m_VtxDesc.high.TexCoord[i].Value();
  148. const auto format = m_VtxAttr.GetTexFormat(i);
  149. const auto elements = m_VtxAttr.GetTexElements(i);
  150. if (tc != VertexComponentFormat::NotPresent)
  151. {
  152. ASSERT_MSG(VIDEO, VertexComponentFormat::Direct <= tc && tc <= VertexComponentFormat::Index16,
  153. "Invalid texture coordinates!\n(tc = {})", tc);
  154. ASSERT_MSG(VIDEO, ComponentFormat::UByte <= format && format <= ComponentFormat::Float,
  155. "Invalid texture coordinates format!\n(format = {})", format);
  156. ASSERT_MSG(VIDEO, elements == TexComponentCount::S || elements == TexComponentCount::ST,
  157. "Invalid number of texture coordinates elements!\n(elements = {})", elements);
  158. WriteCall(VertexLoader_TextCoord::GetFunction(tc, format, elements));
  159. }
  160. if (m_VtxDesc.low.TexMatIdx[i])
  161. {
  162. m_native_vtx_decl.texcoords[i].enable = true;
  163. m_native_vtx_decl.texcoords[i].components = 3;
  164. nat_offset += 12;
  165. if (tc != VertexComponentFormat::NotPresent)
  166. {
  167. // if texmtx is included, texcoord will always be 3 floats, z will be the texmtx index
  168. WriteCall(elements == TexComponentCount::ST ? TexMtx_Write_Float : TexMtx_Write_Float2);
  169. }
  170. else
  171. {
  172. WriteCall(TexMtx_Write_Float3);
  173. }
  174. }
  175. else
  176. {
  177. if (tc != VertexComponentFormat::NotPresent)
  178. {
  179. m_native_vtx_decl.texcoords[i].enable = true;
  180. m_native_vtx_decl.texcoords[i].components = elements == TexComponentCount::ST ? 2 : 1;
  181. nat_offset += 4 * (elements == TexComponentCount::ST ? 2 : 1);
  182. }
  183. }
  184. if (tc == VertexComponentFormat::NotPresent)
  185. {
  186. // if there's more tex coords later, have to write a dummy call
  187. bool has_more = false;
  188. for (size_t j = i + 1; j < m_VtxDesc.high.TexCoord.Size(); ++j)
  189. {
  190. if (m_VtxDesc.high.TexCoord[j] != VertexComponentFormat::NotPresent)
  191. {
  192. has_more = true;
  193. // Keep tcIndex in sync so that the correct array is used later
  194. WriteCall(VertexLoader_TextCoord::GetDummyFunction());
  195. break;
  196. }
  197. else if (m_VtxDesc.low.TexMatIdx[j])
  198. {
  199. has_more = true;
  200. }
  201. }
  202. if (!has_more)
  203. {
  204. // no more tex coords and tex matrices, so exit loop
  205. break;
  206. }
  207. }
  208. }
  209. // indexed position formats may skip the vertex
  210. if (IsIndexed(m_VtxDesc.low.Position))
  211. {
  212. WriteCall(SkipVertex);
  213. }
  214. m_native_vtx_decl.stride = nat_offset;
  215. }
  216. void VertexLoader::WriteCall(TPipelineFunction func)
  217. {
  218. m_PipelineStages.push_back(func);
  219. }
  220. int VertexLoader::RunVertices(const u8* src, u8* dst, int count)
  221. {
  222. g_vertex_manager_write_ptr = dst;
  223. g_video_buffer_read_ptr = src;
  224. m_numLoadedVertices += count;
  225. m_skippedVertices = 0;
  226. for (m_remaining = count - 1; m_remaining >= 0; m_remaining--)
  227. {
  228. m_tcIndex = 0;
  229. m_colIndex = 0;
  230. m_texmtxwrite = m_texmtxread = 0;
  231. for (TPipelineFunction& func : m_PipelineStages)
  232. func(this);
  233. PRIM_LOG("\n");
  234. }
  235. return count - m_skippedVertices;
  236. }