vertex_loader.cpp 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. #include <memory>
  2. #include <boost/range/algorithm/fill.hpp>
  3. #include "common/alignment.h"
  4. #include "common/assert.h"
  5. #include "common/bit_field.h"
  6. #include "common/common_types.h"
  7. #include "common/logging/log.h"
  8. #include "common/vector_math.h"
  9. #include "core/memory.h"
  10. #include "video_core/debug_utils/debug_utils.h"
  11. #include "video_core/pica.h"
  12. #include "video_core/pica_state.h"
  13. #include "video_core/pica_types.h"
  14. #include "video_core/shader/shader.h"
  15. #include "video_core/vertex_loader.h"
  16. namespace Pica {
  17. void VertexLoader::Setup(const Pica::Regs& regs) {
  18. ASSERT_MSG(!is_setup, "VertexLoader is not intended to be setup more than once.");
  19. const auto& attribute_config = regs.vertex_attributes;
  20. num_total_attributes = attribute_config.GetNumTotalAttributes();
  21. boost::fill(vertex_attribute_sources, 0xdeadbeef);
  22. for (int i = 0; i < 16; i++) {
  23. vertex_attribute_is_default[i] = attribute_config.IsDefaultAttribute(i);
  24. }
  25. // Setup attribute data from loaders
  26. for (int loader = 0; loader < 12; ++loader) {
  27. const auto& loader_config = attribute_config.attribute_loaders[loader];
  28. u32 offset = 0;
  29. // TODO: What happens if a loader overwrites a previous one's data?
  30. for (unsigned component = 0; component < loader_config.component_count; ++component) {
  31. if (component >= 12) {
  32. LOG_ERROR(HW_GPU,
  33. "Overflow in the vertex attribute loader %u trying to load component %u",
  34. loader, component);
  35. continue;
  36. }
  37. u32 attribute_index = loader_config.GetComponent(component);
  38. if (attribute_index < 12) {
  39. offset = Common::AlignUp(offset,
  40. attribute_config.GetElementSizeInBytes(attribute_index));
  41. vertex_attribute_sources[attribute_index] = loader_config.data_offset + offset;
  42. vertex_attribute_strides[attribute_index] =
  43. static_cast<u32>(loader_config.byte_count);
  44. vertex_attribute_formats[attribute_index] =
  45. attribute_config.GetFormat(attribute_index);
  46. vertex_attribute_elements[attribute_index] =
  47. attribute_config.GetNumElements(attribute_index);
  48. offset += attribute_config.GetStride(attribute_index);
  49. } else if (attribute_index < 16) {
  50. // Attribute ids 12, 13, 14 and 15 signify 4, 8, 12 and 16-byte paddings,
  51. // respectively
  52. offset = Common::AlignUp(offset, 4);
  53. offset += (attribute_index - 11) * 4;
  54. } else {
  55. UNREACHABLE(); // This is truly unreachable due to the number of bits for each
  56. // component
  57. }
  58. }
  59. }
  60. is_setup = true;
  61. }
  62. void VertexLoader::LoadVertex(u32 base_address, int index, int vertex, Shader::InputVertex& input,
  63. DebugUtils::MemoryAccessTracker& memory_accesses) {
  64. ASSERT_MSG(is_setup, "A VertexLoader needs to be setup before loading vertices.");
  65. for (int i = 0; i < num_total_attributes; ++i) {
  66. if (vertex_attribute_elements[i] != 0) {
  67. // Load per-vertex data from the loader arrays
  68. u32 source_addr =
  69. base_address + vertex_attribute_sources[i] + vertex_attribute_strides[i] * vertex;
  70. if (g_debug_context && Pica::g_debug_context->recorder) {
  71. memory_accesses.AddAccess(
  72. source_addr,
  73. vertex_attribute_elements[i] *
  74. ((vertex_attribute_formats[i] == Regs::VertexAttributeFormat::FLOAT)
  75. ? 4
  76. : (vertex_attribute_formats[i] == Regs::VertexAttributeFormat::SHORT)
  77. ? 2
  78. : 1));
  79. }
  80. switch (vertex_attribute_formats[i]) {
  81. case Regs::VertexAttributeFormat::BYTE: {
  82. const s8* srcdata =
  83. reinterpret_cast<const s8*>(Memory::GetPhysicalPointer(source_addr));
  84. for (unsigned int comp = 0; comp < vertex_attribute_elements[i]; ++comp) {
  85. input.attr[i][comp] = float24::FromFloat32(srcdata[comp]);
  86. }
  87. break;
  88. }
  89. case Regs::VertexAttributeFormat::UBYTE: {
  90. const u8* srcdata =
  91. reinterpret_cast<const u8*>(Memory::GetPhysicalPointer(source_addr));
  92. for (unsigned int comp = 0; comp < vertex_attribute_elements[i]; ++comp) {
  93. input.attr[i][comp] = float24::FromFloat32(srcdata[comp]);
  94. }
  95. break;
  96. }
  97. case Regs::VertexAttributeFormat::SHORT: {
  98. const s16* srcdata =
  99. reinterpret_cast<const s16*>(Memory::GetPhysicalPointer(source_addr));
  100. for (unsigned int comp = 0; comp < vertex_attribute_elements[i]; ++comp) {
  101. input.attr[i][comp] = float24::FromFloat32(srcdata[comp]);
  102. }
  103. break;
  104. }
  105. case Regs::VertexAttributeFormat::FLOAT: {
  106. const float* srcdata =
  107. reinterpret_cast<const float*>(Memory::GetPhysicalPointer(source_addr));
  108. for (unsigned int comp = 0; comp < vertex_attribute_elements[i]; ++comp) {
  109. input.attr[i][comp] = float24::FromFloat32(srcdata[comp]);
  110. }
  111. break;
  112. }
  113. }
  114. // Default attribute values set if array elements have < 4 components. This
  115. // is *not* carried over from the default attribute settings even if they're
  116. // enabled for this attribute.
  117. for (unsigned int comp = vertex_attribute_elements[i]; comp < 4; ++comp) {
  118. input.attr[i][comp] =
  119. comp == 3 ? float24::FromFloat32(1.0f) : float24::FromFloat32(0.0f);
  120. }
  121. LOG_TRACE(HW_GPU, "Loaded %d components of attribute %x for vertex %x (index %x) from "
  122. "0x%08x + 0x%08x + 0x%04x: %f %f %f %f",
  123. vertex_attribute_elements[i], i, vertex, index, base_address,
  124. vertex_attribute_sources[i], vertex_attribute_strides[i] * vertex,
  125. input.attr[i][0].ToFloat32(), input.attr[i][1].ToFloat32(),
  126. input.attr[i][2].ToFloat32(), input.attr[i][3].ToFloat32());
  127. } else if (vertex_attribute_is_default[i]) {
  128. // Load the default attribute if we're configured to do so
  129. input.attr[i] = g_state.vs_default_attributes[i];
  130. LOG_TRACE(HW_GPU,
  131. "Loaded default attribute %x for vertex %x (index %x): (%f, %f, %f, %f)", i,
  132. vertex, index, input.attr[i][0].ToFloat32(), input.attr[i][1].ToFloat32(),
  133. input.attr[i][2].ToFloat32(), input.attr[i][3].ToFloat32());
  134. } else {
  135. // TODO(yuriks): In this case, no data gets loaded and the vertex
  136. // remains with the last value it had. This isn't currently maintained
  137. // as global state, however, and so won't work in Citra yet.
  138. }
  139. }
  140. }
  141. } // namespace Pica