vertex_loader.cpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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, "Overflow in the vertex attribute loader %u trying to load component %u", loader, component);
  33. continue;
  34. }
  35. u32 attribute_index = loader_config.GetComponent(component);
  36. if (attribute_index < 12) {
  37. offset = Common::AlignUp(offset, attribute_config.GetElementSizeInBytes(attribute_index));
  38. vertex_attribute_sources[attribute_index] = loader_config.data_offset + offset;
  39. vertex_attribute_strides[attribute_index] = static_cast<u32>(loader_config.byte_count);
  40. vertex_attribute_formats[attribute_index] = attribute_config.GetFormat(attribute_index);
  41. vertex_attribute_elements[attribute_index] = attribute_config.GetNumElements(attribute_index);
  42. offset += attribute_config.GetStride(attribute_index);
  43. } else if (attribute_index < 16) {
  44. // Attribute ids 12, 13, 14 and 15 signify 4, 8, 12 and 16-byte paddings, respectively
  45. offset = Common::AlignUp(offset, 4);
  46. offset += (attribute_index - 11) * 4;
  47. } else {
  48. UNREACHABLE(); // This is truly unreachable due to the number of bits for each component
  49. }
  50. }
  51. }
  52. is_setup = true;
  53. }
  54. void VertexLoader::LoadVertex(u32 base_address, int index, int vertex, Shader::InputVertex& input, DebugUtils::MemoryAccessTracker& memory_accesses) {
  55. ASSERT_MSG(is_setup, "A VertexLoader needs to be setup before loading vertices.");
  56. for (int i = 0; i < num_total_attributes; ++i) {
  57. if (vertex_attribute_elements[i] != 0) {
  58. // Load per-vertex data from the loader arrays
  59. u32 source_addr = base_address + vertex_attribute_sources[i] + vertex_attribute_strides[i] * vertex;
  60. if (g_debug_context && Pica::g_debug_context->recorder) {
  61. memory_accesses.AddAccess(source_addr, vertex_attribute_elements[i] * (
  62. (vertex_attribute_formats[i] == Regs::VertexAttributeFormat::FLOAT) ? 4
  63. : (vertex_attribute_formats[i] == Regs::VertexAttributeFormat::SHORT) ? 2 : 1));
  64. }
  65. switch (vertex_attribute_formats[i]) {
  66. case Regs::VertexAttributeFormat::BYTE:
  67. {
  68. const s8* srcdata = reinterpret_cast<const s8*>(Memory::GetPhysicalPointer(source_addr));
  69. for (unsigned int comp = 0; comp < vertex_attribute_elements[i]; ++comp) {
  70. input.attr[i][comp] = float24::FromFloat32(srcdata[comp]);
  71. }
  72. break;
  73. }
  74. case Regs::VertexAttributeFormat::UBYTE:
  75. {
  76. const u8* srcdata = reinterpret_cast<const u8*>(Memory::GetPhysicalPointer(source_addr));
  77. for (unsigned int comp = 0; comp < vertex_attribute_elements[i]; ++comp) {
  78. input.attr[i][comp] = float24::FromFloat32(srcdata[comp]);
  79. }
  80. break;
  81. }
  82. case Regs::VertexAttributeFormat::SHORT:
  83. {
  84. const s16* srcdata = reinterpret_cast<const s16*>(Memory::GetPhysicalPointer(source_addr));
  85. for (unsigned int comp = 0; comp < vertex_attribute_elements[i]; ++comp) {
  86. input.attr[i][comp] = float24::FromFloat32(srcdata[comp]);
  87. }
  88. break;
  89. }
  90. case Regs::VertexAttributeFormat::FLOAT:
  91. {
  92. const float* srcdata = reinterpret_cast<const float*>(Memory::GetPhysicalPointer(source_addr));
  93. for (unsigned int comp = 0; comp < vertex_attribute_elements[i]; ++comp) {
  94. input.attr[i][comp] = float24::FromFloat32(srcdata[comp]);
  95. }
  96. break;
  97. }
  98. }
  99. // Default attribute values set if array elements have < 4 components. This
  100. // is *not* carried over from the default attribute settings even if they're
  101. // enabled for this attribute.
  102. for (unsigned int comp = vertex_attribute_elements[i]; comp < 4; ++comp) {
  103. input.attr[i][comp] = comp == 3 ? float24::FromFloat32(1.0f) : float24::FromFloat32(0.0f);
  104. }
  105. LOG_TRACE(HW_GPU, "Loaded %d components of attribute %x for vertex %x (index %x) from 0x%08x + 0x%08x + 0x%04x: %f %f %f %f",
  106. vertex_attribute_elements[i], i, vertex, index,
  107. base_address,
  108. vertex_attribute_sources[i],
  109. vertex_attribute_strides[i] * vertex,
  110. input.attr[i][0].ToFloat32(), input.attr[i][1].ToFloat32(), input.attr[i][2].ToFloat32(), input.attr[i][3].ToFloat32());
  111. } else if (vertex_attribute_is_default[i]) {
  112. // Load the default attribute if we're configured to do so
  113. input.attr[i] = g_state.vs.default_attributes[i];
  114. LOG_TRACE(HW_GPU, "Loaded default attribute %x for vertex %x (index %x): (%f, %f, %f, %f)",
  115. i, vertex, index,
  116. input.attr[i][0].ToFloat32(), input.attr[i][1].ToFloat32(),
  117. input.attr[i][2].ToFloat32(), input.attr[i][3].ToFloat32());
  118. } else {
  119. // TODO(yuriks): In this case, no data gets loaded and the vertex
  120. // remains with the last value it had. This isn't currently maintained
  121. // as global state, however, and so won't work in Citra yet.
  122. }
  123. }
  124. }
  125. } // namespace Pica