vertex_loader.cpp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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_state.h"
  12. #include "video_core/pica_types.h"
  13. #include "video_core/regs_pipeline.h"
  14. #include "video_core/shader/shader.h"
  15. #include "video_core/vertex_loader.h"
  16. namespace Pica {
  17. void VertexLoader::Setup(const PipelineRegs& 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,
  63. Shader::AttributeBuffer& input,
  64. DebugUtils::MemoryAccessTracker& memory_accesses) {
  65. ASSERT_MSG(is_setup, "A VertexLoader needs to be setup before loading vertices.");
  66. for (int i = 0; i < num_total_attributes; ++i) {
  67. if (vertex_attribute_elements[i] != 0) {
  68. // Load per-vertex data from the loader arrays
  69. u32 source_addr =
  70. base_address + vertex_attribute_sources[i] + vertex_attribute_strides[i] * vertex;
  71. if (g_debug_context && Pica::g_debug_context->recorder) {
  72. memory_accesses.AddAccess(
  73. source_addr,
  74. vertex_attribute_elements[i] *
  75. ((vertex_attribute_formats[i] == PipelineRegs::VertexAttributeFormat::FLOAT)
  76. ? 4
  77. : (vertex_attribute_formats[i] ==
  78. PipelineRegs::VertexAttributeFormat::SHORT)
  79. ? 2
  80. : 1));
  81. }
  82. switch (vertex_attribute_formats[i]) {
  83. case PipelineRegs::VertexAttributeFormat::BYTE: {
  84. const s8* srcdata =
  85. reinterpret_cast<const s8*>(Memory::GetPhysicalPointer(source_addr));
  86. for (unsigned int comp = 0; comp < vertex_attribute_elements[i]; ++comp) {
  87. input.attr[i][comp] = float24::FromFloat32(srcdata[comp]);
  88. }
  89. break;
  90. }
  91. case PipelineRegs::VertexAttributeFormat::UBYTE: {
  92. const u8* srcdata =
  93. reinterpret_cast<const u8*>(Memory::GetPhysicalPointer(source_addr));
  94. for (unsigned int comp = 0; comp < vertex_attribute_elements[i]; ++comp) {
  95. input.attr[i][comp] = float24::FromFloat32(srcdata[comp]);
  96. }
  97. break;
  98. }
  99. case PipelineRegs::VertexAttributeFormat::SHORT: {
  100. const s16* srcdata =
  101. reinterpret_cast<const s16*>(Memory::GetPhysicalPointer(source_addr));
  102. for (unsigned int comp = 0; comp < vertex_attribute_elements[i]; ++comp) {
  103. input.attr[i][comp] = float24::FromFloat32(srcdata[comp]);
  104. }
  105. break;
  106. }
  107. case PipelineRegs::VertexAttributeFormat::FLOAT: {
  108. const float* srcdata =
  109. reinterpret_cast<const float*>(Memory::GetPhysicalPointer(source_addr));
  110. for (unsigned int comp = 0; comp < vertex_attribute_elements[i]; ++comp) {
  111. input.attr[i][comp] = float24::FromFloat32(srcdata[comp]);
  112. }
  113. break;
  114. }
  115. }
  116. // Default attribute values set if array elements have < 4 components. This
  117. // is *not* carried over from the default attribute settings even if they're
  118. // enabled for this attribute.
  119. for (unsigned int comp = vertex_attribute_elements[i]; comp < 4; ++comp) {
  120. input.attr[i][comp] =
  121. comp == 3 ? float24::FromFloat32(1.0f) : float24::FromFloat32(0.0f);
  122. }
  123. LOG_TRACE(HW_GPU, "Loaded %d components of attribute %x for vertex %x (index %x) from "
  124. "0x%08x + 0x%08x + 0x%04x: %f %f %f %f",
  125. vertex_attribute_elements[i], i, vertex, index, base_address,
  126. vertex_attribute_sources[i], vertex_attribute_strides[i] * vertex,
  127. input.attr[i][0].ToFloat32(), input.attr[i][1].ToFloat32(),
  128. input.attr[i][2].ToFloat32(), input.attr[i][3].ToFloat32());
  129. } else if (vertex_attribute_is_default[i]) {
  130. // Load the default attribute if we're configured to do so
  131. input.attr[i] = g_state.input_default_attributes.attr[i];
  132. LOG_TRACE(HW_GPU,
  133. "Loaded default attribute %x for vertex %x (index %x): (%f, %f, %f, %f)", i,
  134. vertex, index, input.attr[i][0].ToFloat32(), input.attr[i][1].ToFloat32(),
  135. input.attr[i][2].ToFloat32(), input.attr[i][3].ToFloat32());
  136. } else {
  137. // TODO(yuriks): In this case, no data gets loaded and the vertex
  138. // remains with the last value it had. This isn't currently maintained
  139. // as global state, however, and so won't work in Citra yet.
  140. }
  141. }
  142. }
  143. } // namespace Pica