vertex_loader.cpp 6.5 KB

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