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