2016-04-30 17:34:51 +02:00
|
|
|
#include <memory>
|
|
|
|
#include <boost/range/algorithm/fill.hpp>
|
2016-04-28 19:01:47 +02:00
|
|
|
#include "common/alignment.h"
|
2016-04-29 16:57:06 +02:00
|
|
|
#include "common/assert.h"
|
2016-04-28 19:01:47 +02:00
|
|
|
#include "common/bit_field.h"
|
|
|
|
#include "common/common_types.h"
|
|
|
|
#include "common/logging/log.h"
|
2016-04-30 17:34:51 +02:00
|
|
|
#include "common/vector_math.h"
|
2016-04-28 19:01:47 +02:00
|
|
|
#include "core/memory.h"
|
2016-04-29 08:50:21 +02:00
|
|
|
#include "video_core/debug_utils/debug_utils.h"
|
2016-04-28 19:40:11 +02:00
|
|
|
#include "video_core/pica_state.h"
|
|
|
|
#include "video_core/pica_types.h"
|
2017-01-28 22:27:24 +01:00
|
|
|
#include "video_core/regs_pipeline.h"
|
2016-04-30 17:34:51 +02:00
|
|
|
#include "video_core/shader/shader.h"
|
2016-09-21 08:52:38 +02:00
|
|
|
#include "video_core/vertex_loader.h"
|
2016-04-28 19:01:47 +02:00
|
|
|
|
|
|
|
namespace Pica {
|
|
|
|
|
2017-01-28 21:34:31 +01:00
|
|
|
void VertexLoader::Setup(const PipelineRegs& regs) {
|
2016-04-29 17:27:15 +02:00
|
|
|
ASSERT_MSG(!is_setup, "VertexLoader is not intended to be setup more than once.");
|
|
|
|
|
2016-04-28 19:01:47 +02:00
|
|
|
const auto& attribute_config = regs.vertex_attributes;
|
|
|
|
num_total_attributes = attribute_config.GetNumTotalAttributes();
|
|
|
|
|
|
|
|
boost::fill(vertex_attribute_sources, 0xdeadbeef);
|
|
|
|
|
|
|
|
for (int i = 0; i < 16; i++) {
|
|
|
|
vertex_attribute_is_default[i] = attribute_config.IsDefaultAttribute(i);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Setup attribute data from loaders
|
|
|
|
for (int loader = 0; loader < 12; ++loader) {
|
|
|
|
const auto& loader_config = attribute_config.attribute_loaders[loader];
|
|
|
|
|
|
|
|
u32 offset = 0;
|
|
|
|
|
|
|
|
// TODO: What happens if a loader overwrites a previous one's data?
|
|
|
|
for (unsigned component = 0; component < loader_config.component_count; ++component) {
|
|
|
|
if (component >= 12) {
|
2018-06-29 15:56:12 +02:00
|
|
|
LOG_ERROR(HW_GPU,
|
|
|
|
"Overflow in the vertex attribute loader {} trying to load component {}",
|
|
|
|
loader, component);
|
2016-04-28 19:01:47 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
u32 attribute_index = loader_config.GetComponent(component);
|
|
|
|
if (attribute_index < 12) {
|
2016-09-18 02:38:01 +02:00
|
|
|
offset = Common::AlignUp(offset,
|
|
|
|
attribute_config.GetElementSizeInBytes(attribute_index));
|
2016-04-28 20:17:35 +02:00
|
|
|
vertex_attribute_sources[attribute_index] = loader_config.data_offset + offset;
|
2016-09-18 02:38:01 +02:00
|
|
|
vertex_attribute_strides[attribute_index] =
|
|
|
|
static_cast<u32>(loader_config.byte_count);
|
|
|
|
vertex_attribute_formats[attribute_index] =
|
|
|
|
attribute_config.GetFormat(attribute_index);
|
|
|
|
vertex_attribute_elements[attribute_index] =
|
|
|
|
attribute_config.GetNumElements(attribute_index);
|
2016-04-28 19:01:47 +02:00
|
|
|
offset += attribute_config.GetStride(attribute_index);
|
|
|
|
} else if (attribute_index < 16) {
|
2016-09-18 02:38:01 +02:00
|
|
|
// Attribute ids 12, 13, 14 and 15 signify 4, 8, 12 and 16-byte paddings,
|
|
|
|
// respectively
|
2016-04-28 19:01:47 +02:00
|
|
|
offset = Common::AlignUp(offset, 4);
|
|
|
|
offset += (attribute_index - 11) * 4;
|
|
|
|
} else {
|
2016-09-18 02:38:01 +02:00
|
|
|
UNREACHABLE(); // This is truly unreachable due to the number of bits for each
|
|
|
|
// component
|
2016-04-28 19:01:47 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-04-29 17:27:15 +02:00
|
|
|
|
|
|
|
is_setup = true;
|
2016-04-28 19:01:47 +02:00
|
|
|
}
|
|
|
|
|
2016-12-19 01:42:19 +01:00
|
|
|
void VertexLoader::LoadVertex(u32 base_address, int index, int vertex,
|
|
|
|
Shader::AttributeBuffer& input,
|
2016-09-18 02:38:01 +02:00
|
|
|
DebugUtils::MemoryAccessTracker& memory_accesses) {
|
2016-04-29 17:27:15 +02:00
|
|
|
ASSERT_MSG(is_setup, "A VertexLoader needs to be setup before loading vertices.");
|
|
|
|
|
2016-04-28 19:01:47 +02:00
|
|
|
for (int i = 0; i < num_total_attributes; ++i) {
|
|
|
|
if (vertex_attribute_elements[i] != 0) {
|
|
|
|
// Load per-vertex data from the loader arrays
|
2016-09-18 02:38:01 +02:00
|
|
|
u32 source_addr =
|
|
|
|
base_address + vertex_attribute_sources[i] + vertex_attribute_strides[i] * vertex;
|
2016-04-28 21:43:49 +02:00
|
|
|
|
|
|
|
if (g_debug_context && Pica::g_debug_context->recorder) {
|
2016-09-18 02:38:01 +02:00
|
|
|
memory_accesses.AddAccess(
|
|
|
|
source_addr,
|
|
|
|
vertex_attribute_elements[i] *
|
2017-01-28 21:34:31 +01:00
|
|
|
((vertex_attribute_formats[i] == PipelineRegs::VertexAttributeFormat::FLOAT)
|
2016-09-18 02:38:01 +02:00
|
|
|
? 4
|
2017-01-28 21:34:31 +01:00
|
|
|
: (vertex_attribute_formats[i] ==
|
|
|
|
PipelineRegs::VertexAttributeFormat::SHORT)
|
2016-09-18 02:38:01 +02:00
|
|
|
? 2
|
|
|
|
: 1));
|
2016-04-28 21:43:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
switch (vertex_attribute_formats[i]) {
|
2017-01-28 21:34:31 +01:00
|
|
|
case PipelineRegs::VertexAttributeFormat::BYTE: {
|
2016-09-18 02:38:01 +02:00
|
|
|
const s8* srcdata =
|
|
|
|
reinterpret_cast<const s8*>(Memory::GetPhysicalPointer(source_addr));
|
2016-04-28 21:43:49 +02:00
|
|
|
for (unsigned int comp = 0; comp < vertex_attribute_elements[i]; ++comp) {
|
|
|
|
input.attr[i][comp] = float24::FromFloat32(srcdata[comp]);
|
2016-04-28 19:01:47 +02:00
|
|
|
}
|
2016-04-28 21:43:49 +02:00
|
|
|
break;
|
|
|
|
}
|
2017-01-28 21:34:31 +01:00
|
|
|
case PipelineRegs::VertexAttributeFormat::UBYTE: {
|
2016-09-18 02:38:01 +02:00
|
|
|
const u8* srcdata =
|
|
|
|
reinterpret_cast<const u8*>(Memory::GetPhysicalPointer(source_addr));
|
2016-04-28 21:43:49 +02:00
|
|
|
for (unsigned int comp = 0; comp < vertex_attribute_elements[i]; ++comp) {
|
|
|
|
input.attr[i][comp] = float24::FromFloat32(srcdata[comp]);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2017-01-28 21:34:31 +01:00
|
|
|
case PipelineRegs::VertexAttributeFormat::SHORT: {
|
2016-09-18 02:38:01 +02:00
|
|
|
const s16* srcdata =
|
|
|
|
reinterpret_cast<const s16*>(Memory::GetPhysicalPointer(source_addr));
|
2016-04-28 21:43:49 +02:00
|
|
|
for (unsigned int comp = 0; comp < vertex_attribute_elements[i]; ++comp) {
|
|
|
|
input.attr[i][comp] = float24::FromFloat32(srcdata[comp]);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2017-01-28 21:34:31 +01:00
|
|
|
case PipelineRegs::VertexAttributeFormat::FLOAT: {
|
2016-09-18 02:38:01 +02:00
|
|
|
const float* srcdata =
|
|
|
|
reinterpret_cast<const float*>(Memory::GetPhysicalPointer(source_addr));
|
2016-04-28 21:43:49 +02:00
|
|
|
for (unsigned int comp = 0; comp < vertex_attribute_elements[i]; ++comp) {
|
|
|
|
input.attr[i][comp] = float24::FromFloat32(srcdata[comp]);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2016-04-28 19:01:47 +02:00
|
|
|
|
2016-04-28 21:43:49 +02:00
|
|
|
// Default attribute values set if array elements have < 4 components. This
|
|
|
|
// is *not* carried over from the default attribute settings even if they're
|
|
|
|
// enabled for this attribute.
|
|
|
|
for (unsigned int comp = vertex_attribute_elements[i]; comp < 4; ++comp) {
|
2016-09-18 02:38:01 +02:00
|
|
|
input.attr[i][comp] =
|
|
|
|
comp == 3 ? float24::FromFloat32(1.0f) : float24::FromFloat32(0.0f);
|
2016-04-28 19:01:47 +02:00
|
|
|
}
|
2016-04-28 21:43:49 +02:00
|
|
|
|
2018-06-29 13:18:07 +02:00
|
|
|
LOG_TRACE(HW_GPU,
|
2018-06-29 15:56:12 +02:00
|
|
|
"Loaded {} components of attribute {:x} for vertex {:x} (index {:x}) from "
|
|
|
|
"0x{:08x} + 0x{:08x} + 0x{:04x}: {} {} {} {}",
|
|
|
|
vertex_attribute_elements[i], i, vertex, index, base_address,
|
|
|
|
vertex_attribute_sources[i], vertex_attribute_strides[i] * vertex,
|
|
|
|
input.attr[i][0].ToFloat32(), input.attr[i][1].ToFloat32(),
|
|
|
|
input.attr[i][2].ToFloat32(), input.attr[i][3].ToFloat32());
|
2016-04-28 19:01:47 +02:00
|
|
|
} else if (vertex_attribute_is_default[i]) {
|
|
|
|
// Load the default attribute if we're configured to do so
|
2016-12-19 01:42:19 +01:00
|
|
|
input.attr[i] = g_state.input_default_attributes.attr[i];
|
2018-06-29 13:18:07 +02:00
|
|
|
LOG_TRACE(
|
2018-06-28 23:13:30 +02:00
|
|
|
HW_GPU,
|
|
|
|
"Loaded default attribute {:x} for vertex {:x} (index {:x}): ({}, {}, {}, {})", i,
|
|
|
|
vertex, index, input.attr[i][0].ToFloat32(), input.attr[i][1].ToFloat32(),
|
|
|
|
input.attr[i][2].ToFloat32(), input.attr[i][3].ToFloat32());
|
2016-04-28 19:01:47 +02:00
|
|
|
} else {
|
|
|
|
// TODO(yuriks): In this case, no data gets loaded and the vertex
|
|
|
|
// remains with the last value it had. This isn't currently maintained
|
|
|
|
// as global state, however, and so won't work in Citra yet.
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-18 02:38:01 +02:00
|
|
|
} // namespace Pica
|