Browse Source
Merge pull request #9453 from ameerj/scratch-vector
Merge pull request #9453 from ameerj/scratch-vector
common: Add ScratchBuffer Classpull/15/merge
committed by
GitHub
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
14 changed files with 370 additions and 56 deletions
-
2src/common/CMakeLists.txt
-
25src/common/make_unique_for_overwrite.h
-
95src/common/scratch_buffer.h
-
1src/tests/CMakeLists.txt
-
199src/tests/common/scratch_buffer.cpp
-
11src/video_core/buffer_cache/buffer_cache.h
-
19src/video_core/dma_pusher.cpp
-
8src/video_core/dma_pusher.h
-
4src/video_core/engines/engine_upload.cpp
-
7src/video_core/engines/engine_upload.h
-
34src/video_core/engines/maxwell_dma.cpp
-
8src/video_core/engines/maxwell_dma.h
-
6src/video_core/host1x/vic.cpp
-
7src/video_core/host1x/vic.h
@ -0,0 +1,25 @@ |
|||
// SPDX-FileCopyrightText: Copyright 2022 yuzu Emulator Project |
|||
// SPDX-License-Identifier: GPL-2.0-or-later |
|||
|
|||
#pragma once |
|||
|
|||
#include <memory> |
|||
#include <type_traits> |
|||
|
|||
namespace Common { |
|||
|
|||
template <class T> |
|||
requires(!std::is_array_v<T>) std::unique_ptr<T> make_unique_for_overwrite() { |
|||
return std::unique_ptr<T>(new T); |
|||
} |
|||
|
|||
template <class T> |
|||
requires std::is_unbounded_array_v<T> std::unique_ptr<T> make_unique_for_overwrite(std::size_t n) { |
|||
return std::unique_ptr<T>(new std::remove_extent_t<T>[n]); |
|||
} |
|||
|
|||
template <class T, class... Args> |
|||
requires std::is_bounded_array_v<T> |
|||
void make_unique_for_overwrite(Args&&...) = delete; |
|||
|
|||
} // namespace Common |
|||
@ -0,0 +1,95 @@ |
|||
// SPDX-FileCopyrightText: Copyright 2022 yuzu Emulator Project |
|||
// SPDX-License-Identifier: GPL-2.0-or-later |
|||
|
|||
#pragma once |
|||
|
|||
#include "common/make_unique_for_overwrite.h" |
|||
|
|||
namespace Common { |
|||
|
|||
/** |
|||
* ScratchBuffer class |
|||
* This class creates a default initialized heap allocated buffer for cases such as intermediate |
|||
* buffers being copied into entirely, where value initializing members during allocation or resize |
|||
* is redundant. |
|||
*/ |
|||
template <typename T> |
|||
class ScratchBuffer { |
|||
public: |
|||
ScratchBuffer() = default; |
|||
|
|||
explicit ScratchBuffer(size_t initial_capacity) |
|||
: last_requested_size{initial_capacity}, buffer_capacity{initial_capacity}, |
|||
buffer{Common::make_unique_for_overwrite<T[]>(initial_capacity)} {} |
|||
|
|||
~ScratchBuffer() = default; |
|||
|
|||
/// This will only grow the buffer's capacity if size is greater than the current capacity. |
|||
/// The previously held data will remain intact. |
|||
void resize(size_t size) { |
|||
if (size > buffer_capacity) { |
|||
auto new_buffer = Common::make_unique_for_overwrite<T[]>(size); |
|||
std::move(buffer.get(), buffer.get() + buffer_capacity, new_buffer.get()); |
|||
buffer = std::move(new_buffer); |
|||
buffer_capacity = size; |
|||
} |
|||
last_requested_size = size; |
|||
} |
|||
|
|||
/// This will only grow the buffer's capacity if size is greater than the current capacity. |
|||
/// The previously held data will be destroyed if a reallocation occurs. |
|||
void resize_destructive(size_t size) { |
|||
if (size > buffer_capacity) { |
|||
buffer_capacity = size; |
|||
buffer = Common::make_unique_for_overwrite<T[]>(buffer_capacity); |
|||
} |
|||
last_requested_size = size; |
|||
} |
|||
|
|||
[[nodiscard]] T* data() noexcept { |
|||
return buffer.get(); |
|||
} |
|||
|
|||
[[nodiscard]] const T* data() const noexcept { |
|||
return buffer.get(); |
|||
} |
|||
|
|||
[[nodiscard]] T* begin() noexcept { |
|||
return data(); |
|||
} |
|||
|
|||
[[nodiscard]] const T* begin() const noexcept { |
|||
return data(); |
|||
} |
|||
|
|||
[[nodiscard]] T* end() noexcept { |
|||
return data() + last_requested_size; |
|||
} |
|||
|
|||
[[nodiscard]] const T* end() const noexcept { |
|||
return data() + last_requested_size; |
|||
} |
|||
|
|||
[[nodiscard]] T& operator[](size_t i) { |
|||
return buffer[i]; |
|||
} |
|||
|
|||
[[nodiscard]] const T& operator[](size_t i) const { |
|||
return buffer[i]; |
|||
} |
|||
|
|||
[[nodiscard]] size_t size() const noexcept { |
|||
return last_requested_size; |
|||
} |
|||
|
|||
[[nodiscard]] size_t capacity() const noexcept { |
|||
return buffer_capacity; |
|||
} |
|||
|
|||
private: |
|||
size_t last_requested_size{}; |
|||
size_t buffer_capacity{}; |
|||
std::unique_ptr<T[]> buffer{}; |
|||
}; |
|||
|
|||
} // namespace Common |
|||
@ -0,0 +1,199 @@ |
|||
// SPDX-FileCopyrightText: Copyright 2022 yuzu Emulator Project
|
|||
// SPDX-License-Identifier: GPL-2.0-or-later
|
|||
|
|||
#include <algorithm>
|
|||
#include <array>
|
|||
#include <span>
|
|||
#include <catch2/catch.hpp>
|
|||
#include "common/common_types.h"
|
|||
#include "common/scratch_buffer.h"
|
|||
|
|||
namespace Common { |
|||
|
|||
TEST_CASE("ScratchBuffer: Basic Test", "[common]") { |
|||
ScratchBuffer<u8> buf; |
|||
|
|||
REQUIRE(buf.size() == 0U); |
|||
REQUIRE(buf.capacity() == 0U); |
|||
|
|||
std::array<u8, 10> payload; |
|||
payload.fill(66); |
|||
|
|||
buf.resize(payload.size()); |
|||
REQUIRE(buf.size() == payload.size()); |
|||
REQUIRE(buf.capacity() == payload.size()); |
|||
|
|||
std::memcpy(buf.data(), payload.data(), payload.size()); |
|||
for (size_t i = 0; i < payload.size(); ++i) { |
|||
REQUIRE(buf[i] == payload[i]); |
|||
} |
|||
} |
|||
|
|||
TEST_CASE("ScratchBuffer: resize_destructive Grow", "[common]") { |
|||
std::array<u8, 10> payload; |
|||
payload.fill(66); |
|||
|
|||
ScratchBuffer<u8> buf(payload.size()); |
|||
REQUIRE(buf.size() == payload.size()); |
|||
REQUIRE(buf.capacity() == payload.size()); |
|||
|
|||
// Increasing the size should reallocate the buffer
|
|||
buf.resize_destructive(payload.size() * 2); |
|||
REQUIRE(buf.size() == payload.size() * 2); |
|||
REQUIRE(buf.capacity() == payload.size() * 2); |
|||
|
|||
// Since the buffer is not value initialized, reading its data will be garbage
|
|||
} |
|||
|
|||
TEST_CASE("ScratchBuffer: resize_destructive Shrink", "[common]") { |
|||
std::array<u8, 10> payload; |
|||
payload.fill(66); |
|||
|
|||
ScratchBuffer<u8> buf(payload.size()); |
|||
REQUIRE(buf.size() == payload.size()); |
|||
REQUIRE(buf.capacity() == payload.size()); |
|||
|
|||
std::memcpy(buf.data(), payload.data(), payload.size()); |
|||
for (size_t i = 0; i < payload.size(); ++i) { |
|||
REQUIRE(buf[i] == payload[i]); |
|||
} |
|||
|
|||
// Decreasing the size should not cause a buffer reallocation
|
|||
// This can be tested by ensuring the buffer capacity and data has not changed,
|
|||
buf.resize_destructive(1U); |
|||
REQUIRE(buf.size() == 1U); |
|||
REQUIRE(buf.capacity() == payload.size()); |
|||
|
|||
for (size_t i = 0; i < payload.size(); ++i) { |
|||
REQUIRE(buf[i] == payload[i]); |
|||
} |
|||
} |
|||
|
|||
TEST_CASE("ScratchBuffer: resize Grow u8", "[common]") { |
|||
std::array<u8, 10> payload; |
|||
payload.fill(66); |
|||
|
|||
ScratchBuffer<u8> buf(payload.size()); |
|||
REQUIRE(buf.size() == payload.size()); |
|||
REQUIRE(buf.capacity() == payload.size()); |
|||
|
|||
std::memcpy(buf.data(), payload.data(), payload.size()); |
|||
for (size_t i = 0; i < payload.size(); ++i) { |
|||
REQUIRE(buf[i] == payload[i]); |
|||
} |
|||
|
|||
// Increasing the size should reallocate the buffer
|
|||
buf.resize(payload.size() * 2); |
|||
REQUIRE(buf.size() == payload.size() * 2); |
|||
REQUIRE(buf.capacity() == payload.size() * 2); |
|||
|
|||
// resize() keeps the previous data intact
|
|||
for (size_t i = 0; i < payload.size(); ++i) { |
|||
REQUIRE(buf[i] == payload[i]); |
|||
} |
|||
} |
|||
|
|||
TEST_CASE("ScratchBuffer: resize Grow u64", "[common]") { |
|||
std::array<u64, 10> payload; |
|||
payload.fill(6666); |
|||
|
|||
ScratchBuffer<u64> buf(payload.size()); |
|||
REQUIRE(buf.size() == payload.size()); |
|||
REQUIRE(buf.capacity() == payload.size()); |
|||
|
|||
std::memcpy(buf.data(), payload.data(), payload.size() * sizeof(u64)); |
|||
for (size_t i = 0; i < payload.size(); ++i) { |
|||
REQUIRE(buf[i] == payload[i]); |
|||
} |
|||
|
|||
// Increasing the size should reallocate the buffer
|
|||
buf.resize(payload.size() * 2); |
|||
REQUIRE(buf.size() == payload.size() * 2); |
|||
REQUIRE(buf.capacity() == payload.size() * 2); |
|||
|
|||
// resize() keeps the previous data intact
|
|||
for (size_t i = 0; i < payload.size(); ++i) { |
|||
REQUIRE(buf[i] == payload[i]); |
|||
} |
|||
} |
|||
|
|||
TEST_CASE("ScratchBuffer: resize Shrink", "[common]") { |
|||
std::array<u8, 10> payload; |
|||
payload.fill(66); |
|||
|
|||
ScratchBuffer<u8> buf(payload.size()); |
|||
REQUIRE(buf.size() == payload.size()); |
|||
REQUIRE(buf.capacity() == payload.size()); |
|||
|
|||
std::memcpy(buf.data(), payload.data(), payload.size()); |
|||
for (size_t i = 0; i < payload.size(); ++i) { |
|||
REQUIRE(buf[i] == payload[i]); |
|||
} |
|||
|
|||
// Decreasing the size should not cause a buffer reallocation
|
|||
// This can be tested by ensuring the buffer capacity and data has not changed,
|
|||
buf.resize(1U); |
|||
REQUIRE(buf.size() == 1U); |
|||
REQUIRE(buf.capacity() == payload.size()); |
|||
|
|||
for (size_t i = 0; i < payload.size(); ++i) { |
|||
REQUIRE(buf[i] == payload[i]); |
|||
} |
|||
} |
|||
|
|||
TEST_CASE("ScratchBuffer: Span Size", "[common]") { |
|||
std::array<u8, 10> payload; |
|||
payload.fill(66); |
|||
|
|||
ScratchBuffer<u8> buf(payload.size()); |
|||
REQUIRE(buf.size() == payload.size()); |
|||
REQUIRE(buf.capacity() == payload.size()); |
|||
|
|||
std::memcpy(buf.data(), payload.data(), payload.size()); |
|||
for (size_t i = 0; i < payload.size(); ++i) { |
|||
REQUIRE(buf[i] == payload[i]); |
|||
} |
|||
|
|||
buf.resize(3U); |
|||
REQUIRE(buf.size() == 3U); |
|||
REQUIRE(buf.capacity() == payload.size()); |
|||
|
|||
const auto buf_span = std::span<u8>(buf); |
|||
// The span size is the last requested size of the buffer, not its capacity
|
|||
REQUIRE(buf_span.size() == buf.size()); |
|||
|
|||
for (size_t i = 0; i < buf_span.size(); ++i) { |
|||
REQUIRE(buf_span[i] == buf[i]); |
|||
REQUIRE(buf_span[i] == payload[i]); |
|||
} |
|||
} |
|||
|
|||
TEST_CASE("ScratchBuffer: Span Writes", "[common]") { |
|||
std::array<u8, 10> payload; |
|||
payload.fill(66); |
|||
|
|||
ScratchBuffer<u8> buf(payload.size()); |
|||
REQUIRE(buf.size() == payload.size()); |
|||
REQUIRE(buf.capacity() == payload.size()); |
|||
|
|||
std::memcpy(buf.data(), payload.data(), payload.size()); |
|||
for (size_t i = 0; i < payload.size(); ++i) { |
|||
REQUIRE(buf[i] == payload[i]); |
|||
} |
|||
|
|||
buf.resize(3U); |
|||
REQUIRE(buf.size() == 3U); |
|||
REQUIRE(buf.capacity() == payload.size()); |
|||
|
|||
const auto buf_span = std::span<u8>(buf); |
|||
REQUIRE(buf_span.size() == buf.size()); |
|||
|
|||
for (size_t i = 0; i < buf_span.size(); ++i) { |
|||
const auto new_value = static_cast<u8>(i + 1U); |
|||
// Writes to a span of the scratch buffer will propogate to the buffer itself
|
|||
buf_span[i] = new_value; |
|||
REQUIRE(buf[i] == new_value); |
|||
} |
|||
} |
|||
|
|||
} // namespace Common
|
|||
Write
Preview
Loading…
Cancel
Save
Reference in new issue