From a66c186e81ba88b8ce5a9ef041e3839454dd70ab Mon Sep 17 00:00:00 2001 From: Jannik Vogel Date: Fri, 11 Mar 2016 16:15:36 +0100 Subject: [PATCH] PICA: Align vertex attributes --- src/common/CMakeLists.txt | 1 + src/common/alignment.h | 22 ++++++++++++++++++++++ src/video_core/command_processor.cpp | 6 +++++- 3 files changed, 28 insertions(+), 1 deletion(-) create mode 100644 src/common/alignment.h diff --git a/src/common/CMakeLists.txt b/src/common/CMakeLists.txt index 959084cdf..1c9be718f 100644 --- a/src/common/CMakeLists.txt +++ b/src/common/CMakeLists.txt @@ -22,6 +22,7 @@ set(SRCS ) set(HEADERS + alignment.h assert.h bit_field.h bit_set.h diff --git a/src/common/alignment.h b/src/common/alignment.h new file mode 100644 index 000000000..b77da4a92 --- /dev/null +++ b/src/common/alignment.h @@ -0,0 +1,22 @@ +// This file is under the public domain. + +#pragma once + +#include +#include + +namespace Common { + +template +constexpr T AlignUp(T value, size_t size) { + static_assert(std::is_unsigned::value, "T must be an unsigned value."); + return static_cast(value + (size - value % size) % size); +} + +template +constexpr T AlignDown(T value, size_t size) { + static_assert(std::is_unsigned::value, "T must be an unsigned value."); + return static_cast(value - value % size); +} + +} // namespace Common diff --git a/src/video_core/command_processor.cpp b/src/video_core/command_processor.cpp index 2274dfa66..54721561e 100644 --- a/src/video_core/command_processor.cpp +++ b/src/video_core/command_processor.cpp @@ -5,6 +5,7 @@ #include #include +#include "common/alignment.h" #include "common/microprofile.h" #include "common/profiler.h" @@ -210,14 +211,17 @@ static void WritePicaReg(u32 id, u32 value, u32 mask) { u32 attribute_index = loader_config.GetComponent(component); if (attribute_index < 12) { + int element_size = attribute_config.GetElementSizeInBytes(attribute_index); + load_address = Common::AlignUp(load_address, element_size); vertex_attribute_sources[attribute_index] = load_address; vertex_attribute_strides[attribute_index] = static_cast(loader_config.byte_count); vertex_attribute_formats[attribute_index] = attribute_config.GetFormat(attribute_index); vertex_attribute_elements[attribute_index] = attribute_config.GetNumElements(attribute_index); - vertex_attribute_element_size[attribute_index] = attribute_config.GetElementSizeInBytes(attribute_index); + vertex_attribute_element_size[attribute_index] = element_size; load_address += attribute_config.GetStride(attribute_index); } else if (attribute_index < 16) { // Attribute ids 12, 13, 14 and 15 signify 4, 8, 12 and 16-byte paddings, respectively + load_address = Common::AlignUp(load_address, 4); load_address += (attribute_index - 11) * 4; } else { UNREACHABLE(); // This is truly unreachable due to the number of bits for each component