summaryrefslogtreecommitdiff
path: root/src/video_core/vertex_loader.cpp
diff options
context:
space:
mode:
authorGravatar Henrik Rydgard2016-04-28 19:01:47 +0200
committerGravatar Henrik Rydgard2016-04-28 19:05:55 +0200
commit47ff00881703eeab03d32e60289ac34b7f4a7994 (patch)
treeb3260ea48aba0cf7feee3cd338c6676dd4a3d604 /src/video_core/vertex_loader.cpp
parentRemove late accesses to attribute_config (diff)
downloadyuzu-47ff00881703eeab03d32e60289ac34b7f4a7994.tar.gz
yuzu-47ff00881703eeab03d32e60289ac34b7f4a7994.tar.xz
yuzu-47ff00881703eeab03d32e60289ac34b7f4a7994.zip
Refactor: Extract VertexLoader from command_processor.cpp.
Preparation for a similar concept to Dolphin or PPSSPP. These can be JIT-ed and cached.
Diffstat (limited to 'src/video_core/vertex_loader.cpp')
-rw-r--r--src/video_core/vertex_loader.cpp119
1 files changed, 119 insertions, 0 deletions
diff --git a/src/video_core/vertex_loader.cpp b/src/video_core/vertex_loader.cpp
new file mode 100644
index 000000000..258002b07
--- /dev/null
+++ b/src/video_core/vertex_loader.cpp
@@ -0,0 +1,119 @@
1#include <cmath>
2#include <string>
3
4#include "boost/range/algorithm/fill.hpp"
5
6#include "common/assert.h"
7#include "common/alignment.h"
8#include "common/bit_field.h"
9#include "common/common_funcs.h"
10#include "common/common_types.h"
11#include "common/logging/log.h"
12
13#include "core/memory.h"
14
15#include "debug_utils/debug_utils.h"
16
17#include "pica.h"
18#include "pica_state.h"
19#include "pica_types.h"
20#include "vertex_loader.h"
21
22namespace Pica {
23
24void VertexLoader::Setup(const Pica::Regs &regs) {
25 const auto& attribute_config = regs.vertex_attributes;
26 base_address = attribute_config.GetPhysicalBaseAddress();
27 num_total_attributes = attribute_config.GetNumTotalAttributes();
28
29 boost::fill(vertex_attribute_sources, 0xdeadbeef);
30
31 for (int i = 0; i < 16; i++) {
32 vertex_attribute_is_default[i] = attribute_config.IsDefaultAttribute(i);
33 }
34
35 // Setup attribute data from loaders
36 for (int loader = 0; loader < 12; ++loader) {
37 const auto& loader_config = attribute_config.attribute_loaders[loader];
38
39 u32 offset = 0;
40
41 // TODO: What happens if a loader overwrites a previous one's data?
42 for (unsigned component = 0; component < loader_config.component_count; ++component) {
43 if (component >= 12) {
44 LOG_ERROR(HW_GPU, "Overflow in the vertex attribute loader %u trying to load component %u", loader, component);
45 continue;
46 }
47
48 u32 attribute_index = loader_config.GetComponent(component);
49 if (attribute_index < 12) {
50 int element_size = attribute_config.GetElementSizeInBytes(attribute_index);
51 offset = Common::AlignUp(offset, element_size);
52 vertex_attribute_sources[attribute_index] = base_address + loader_config.data_offset + offset;
53 vertex_attribute_strides[attribute_index] = static_cast<u32>(loader_config.byte_count);
54 vertex_attribute_formats[attribute_index] = attribute_config.GetFormat(attribute_index);
55 vertex_attribute_elements[attribute_index] = attribute_config.GetNumElements(attribute_index);
56 vertex_attribute_element_size[attribute_index] = element_size;
57 offset += attribute_config.GetStride(attribute_index);
58 } else if (attribute_index < 16) {
59 // Attribute ids 12, 13, 14 and 15 signify 4, 8, 12 and 16-byte paddings, respectively
60 offset = Common::AlignUp(offset, 4);
61 offset += (attribute_index - 11) * 4;
62 } else {
63 UNREACHABLE(); // This is truly unreachable due to the number of bits for each component
64 }
65 }
66 }
67}
68
69void VertexLoader::LoadVertex(int index, int vertex, Shader::InputVertex &input, MemoryAccesses &memory_accesses) {
70 for (int i = 0; i < num_total_attributes; ++i) {
71 if (vertex_attribute_elements[i] != 0) {
72 // Default attribute values set if array elements have < 4 components. This
73 // is *not* carried over from the default attribute settings even if they're
74 // enabled for this attribute.
75 static const float24 zero = float24::FromFloat32(0.0f);
76 static const float24 one = float24::FromFloat32(1.0f);
77 input.attr[i] = Math::Vec4<float24>(zero, zero, zero, one);
78
79 // Load per-vertex data from the loader arrays
80 for (unsigned int comp = 0; comp < vertex_attribute_elements[i]; ++comp) {
81 u32 source_addr = vertex_attribute_sources[i] + vertex_attribute_strides[i] * vertex + comp * vertex_attribute_element_size[i];
82 const u8* srcdata = Memory::GetPhysicalPointer(source_addr);
83
84 if (g_debug_context && Pica::g_debug_context->recorder) {
85 memory_accesses.AddAccess(source_addr,
86 (vertex_attribute_formats[i] == Regs::VertexAttributeFormat::FLOAT) ? 4
87 : (vertex_attribute_formats[i] == Regs::VertexAttributeFormat::SHORT) ? 2 : 1);
88 }
89
90 const float srcval =
91 (vertex_attribute_formats[i] == Regs::VertexAttributeFormat::BYTE) ? *reinterpret_cast<const s8*>(srcdata) :
92 (vertex_attribute_formats[i] == Regs::VertexAttributeFormat::UBYTE) ? *reinterpret_cast<const u8*>(srcdata) :
93 (vertex_attribute_formats[i] == Regs::VertexAttributeFormat::SHORT) ? *reinterpret_cast<const s16*>(srcdata) :
94 *reinterpret_cast<const float*>(srcdata);
95
96 input.attr[i][comp] = float24::FromFloat32(srcval);
97 LOG_TRACE(HW_GPU, "Loaded component %x of attribute %x for vertex %x (index %x) from 0x%08x + 0x%08x + 0x%04x: %f",
98 comp, i, vertex, index,
99 base_address,
100 vertex_attribute_sources[i] - base_address,
101 vertex_attribute_strides[i] * vertex + comp * vertex_attribute_element_size[i],
102 input.attr[i][comp].ToFloat32());
103 }
104 } else if (vertex_attribute_is_default[i]) {
105 // Load the default attribute if we're configured to do so
106 input.attr[i] = g_state.vs.default_attributes[i];
107 LOG_TRACE(HW_GPU, "Loaded default attribute %x for vertex %x (index %x): (%f, %f, %f, %f)",
108 i, vertex, index,
109 input.attr[i][0].ToFloat32(), input.attr[i][1].ToFloat32(),
110 input.attr[i][2].ToFloat32(), input.attr[i][3].ToFloat32());
111 } else {
112 // TODO(yuriks): In this case, no data gets loaded and the vertex
113 // remains with the last value it had. This isn't currently maintained
114 // as global state, however, and so won't work in Citra yet.
115 }
116 }
117}
118
119} // namespace Pica \ No newline at end of file