From 4e1f8a36b0a53e6afe6c48aff1d188f668336add Mon Sep 17 00:00:00 2001 From: Lioncash Date: Sat, 9 Dec 2023 14:54:17 -0500 Subject: [PATCH] VideoCommon: Use std::span for BoundingBox::Write() Crosses off a lingering TODO. Also amends a few nearby cases where a u32 cast was being repromoted to size_t. --- Source/Core/VideoBackends/D3D/D3DBoundingBox.cpp | 4 ++-- Source/Core/VideoBackends/D3D/D3DBoundingBox.h | 2 +- Source/Core/VideoBackends/D3D12/D3D12BoundingBox.cpp | 4 ++-- Source/Core/VideoBackends/D3D12/D3D12BoundingBox.h | 2 +- Source/Core/VideoBackends/Metal/MTLBoundingBox.h | 2 +- Source/Core/VideoBackends/Metal/MTLBoundingBox.mm | 2 +- Source/Core/VideoBackends/Null/NullBoundingBox.h | 2 +- Source/Core/VideoBackends/OGL/OGLBoundingBox.cpp | 2 +- Source/Core/VideoBackends/OGL/OGLBoundingBox.h | 2 +- Source/Core/VideoBackends/Software/SWBoundingBox.cpp | 2 +- Source/Core/VideoBackends/Software/SWBoundingBox.h | 2 +- Source/Core/VideoBackends/Vulkan/VKBoundingBox.cpp | 5 ++--- Source/Core/VideoBackends/Vulkan/VKBoundingBox.h | 2 +- Source/Core/VideoCommon/BoundingBox.cpp | 2 +- Source/Core/VideoCommon/BoundingBox.h | 4 ++-- 15 files changed, 19 insertions(+), 20 deletions(-) diff --git a/Source/Core/VideoBackends/D3D/D3DBoundingBox.cpp b/Source/Core/VideoBackends/D3D/D3DBoundingBox.cpp index 7a6fe64d80fb..5931165c1ecc 100644 --- a/Source/Core/VideoBackends/D3D/D3DBoundingBox.cpp +++ b/Source/Core/VideoBackends/D3D/D3DBoundingBox.cpp @@ -88,12 +88,12 @@ std::vector D3DBoundingBox::Read(u32 index, u32 length) return values; } -void D3DBoundingBox::Write(u32 index, const std::vector& values) +void D3DBoundingBox::Write(u32 index, std::span values) { D3D11_BOX box{index * sizeof(BBoxType), 0, 0, - static_cast(index + values.size()) * sizeof(BBoxType), + static_cast(index + values.size() * sizeof(BBoxType)), 1, 1}; D3D::context->UpdateSubresource(m_buffer.Get(), 0, &box, values.data(), 0, 0); diff --git a/Source/Core/VideoBackends/D3D/D3DBoundingBox.h b/Source/Core/VideoBackends/D3D/D3DBoundingBox.h index 4a9cc9878591..6c6f14e02428 100644 --- a/Source/Core/VideoBackends/D3D/D3DBoundingBox.h +++ b/Source/Core/VideoBackends/D3D/D3DBoundingBox.h @@ -19,7 +19,7 @@ class D3DBoundingBox final : public BoundingBox protected: std::vector Read(u32 index, u32 length) override; - void Write(u32 index, const std::vector& values) override; + void Write(u32 index, std::span values) override; private: ComPtr m_buffer; diff --git a/Source/Core/VideoBackends/D3D12/D3D12BoundingBox.cpp b/Source/Core/VideoBackends/D3D12/D3D12BoundingBox.cpp index 1627e39343a3..a26c4e8160ce 100644 --- a/Source/Core/VideoBackends/D3D12/D3D12BoundingBox.cpp +++ b/Source/Core/VideoBackends/D3D12/D3D12BoundingBox.cpp @@ -56,9 +56,9 @@ std::vector D3D12BoundingBox::Read(u32 index, u32 length) return values; } -void D3D12BoundingBox::Write(u32 index, const std::vector& values) +void D3D12BoundingBox::Write(u32 index, std::span values) { - const u32 copy_size = static_cast(values.size()) * sizeof(BBoxType); + const u32 copy_size = static_cast(values.size() * sizeof(BBoxType)); if (!m_upload_buffer.ReserveMemory(copy_size, sizeof(BBoxType))) { WARN_LOG_FMT(VIDEO, "Executing command list while waiting for space in bbox stream buffer"); diff --git a/Source/Core/VideoBackends/D3D12/D3D12BoundingBox.h b/Source/Core/VideoBackends/D3D12/D3D12BoundingBox.h index d2d95c5c8301..1fb188f873c0 100644 --- a/Source/Core/VideoBackends/D3D12/D3D12BoundingBox.h +++ b/Source/Core/VideoBackends/D3D12/D3D12BoundingBox.h @@ -21,7 +21,7 @@ class D3D12BoundingBox final : public BoundingBox protected: std::vector Read(u32 index, u32 length) override; - void Write(u32 index, const std::vector& values) override; + void Write(u32 index, std::span values) override; private: static constexpr u32 BUFFER_SIZE = sizeof(BBoxType) * NUM_BBOX_VALUES; diff --git a/Source/Core/VideoBackends/Metal/MTLBoundingBox.h b/Source/Core/VideoBackends/Metal/MTLBoundingBox.h index d4a67389a24a..f73d0a4a7645 100644 --- a/Source/Core/VideoBackends/Metal/MTLBoundingBox.h +++ b/Source/Core/VideoBackends/Metal/MTLBoundingBox.h @@ -18,7 +18,7 @@ class BoundingBox final : public ::BoundingBox protected: std::vector Read(u32 index, u32 length) override; - void Write(u32 index, const std::vector& values) override; + void Write(u32 index, std::span values) override; private: BBoxType* m_cpu_buffer_ptr; diff --git a/Source/Core/VideoBackends/Metal/MTLBoundingBox.mm b/Source/Core/VideoBackends/Metal/MTLBoundingBox.mm index 0bdcc9ef62e2..da36e6a0b212 100644 --- a/Source/Core/VideoBackends/Metal/MTLBoundingBox.mm +++ b/Source/Core/VideoBackends/Metal/MTLBoundingBox.mm @@ -42,7 +42,7 @@ } } -void Metal::BoundingBox::Write(u32 index, const std::vector& values) +void Metal::BoundingBox::Write(u32 index, std::span values) { const u32 size = values.size() * sizeof(BBoxType); if (!g_state_tracker->HasUnflushedData() && !g_state_tracker->GPUBusy()) diff --git a/Source/Core/VideoBackends/Null/NullBoundingBox.h b/Source/Core/VideoBackends/Null/NullBoundingBox.h index 74cf04e06d68..f33ca2f67b5f 100644 --- a/Source/Core/VideoBackends/Null/NullBoundingBox.h +++ b/Source/Core/VideoBackends/Null/NullBoundingBox.h @@ -19,7 +19,7 @@ class NullBoundingBox final : public BoundingBox { return std::vector(length); } - void Write(u32 index, const std::vector& values) override {} + void Write(u32 index, std::span values) override {} }; } // namespace Null diff --git a/Source/Core/VideoBackends/OGL/OGLBoundingBox.cpp b/Source/Core/VideoBackends/OGL/OGLBoundingBox.cpp index d21326c1f5b7..6936163f3efa 100644 --- a/Source/Core/VideoBackends/OGL/OGLBoundingBox.cpp +++ b/Source/Core/VideoBackends/OGL/OGLBoundingBox.cpp @@ -66,7 +66,7 @@ std::vector OGLBoundingBox::Read(u32 index, u32 length) return values; } -void OGLBoundingBox::Write(u32 index, const std::vector& values) +void OGLBoundingBox::Write(u32 index, std::span values) { glBindBuffer(GL_SHADER_STORAGE_BUFFER, m_buffer_id); glBufferSubData(GL_SHADER_STORAGE_BUFFER, sizeof(BBoxType) * index, diff --git a/Source/Core/VideoBackends/OGL/OGLBoundingBox.h b/Source/Core/VideoBackends/OGL/OGLBoundingBox.h index 50d063d7b70e..70de8197d9e8 100644 --- a/Source/Core/VideoBackends/OGL/OGLBoundingBox.h +++ b/Source/Core/VideoBackends/OGL/OGLBoundingBox.h @@ -19,7 +19,7 @@ class OGLBoundingBox final : public BoundingBox protected: std::vector Read(u32 index, u32 length) override; - void Write(u32 index, const std::vector& values) override; + void Write(u32 index, std::span values) override; private: GLuint m_buffer_id = 0; diff --git a/Source/Core/VideoBackends/Software/SWBoundingBox.cpp b/Source/Core/VideoBackends/Software/SWBoundingBox.cpp index 96d38517b8ef..b9b3d21ca8bc 100644 --- a/Source/Core/VideoBackends/Software/SWBoundingBox.cpp +++ b/Source/Core/VideoBackends/Software/SWBoundingBox.cpp @@ -55,7 +55,7 @@ std::vector SWBoundingBox::Read(u32 index, u32 length) return values; } -void SWBoundingBox::Write(u32 index, const std::vector& values) +void SWBoundingBox::Write(u32 index, std::span values) { for (size_t i = 0; i < values.size(); i++) { diff --git a/Source/Core/VideoBackends/Software/SWBoundingBox.h b/Source/Core/VideoBackends/Software/SWBoundingBox.h index 116d85bdf165..e84f74f5269f 100644 --- a/Source/Core/VideoBackends/Software/SWBoundingBox.h +++ b/Source/Core/VideoBackends/Software/SWBoundingBox.h @@ -37,7 +37,7 @@ class SWBoundingBox final : public BoundingBox protected: std::vector Read(u32 index, u32 length) override; - void Write(u32 index, const std::vector& values) override; + void Write(u32 index, std::span values) override; }; } // namespace SW diff --git a/Source/Core/VideoBackends/Vulkan/VKBoundingBox.cpp b/Source/Core/VideoBackends/Vulkan/VKBoundingBox.cpp index 7765e97a2534..af512935007a 100644 --- a/Source/Core/VideoBackends/Vulkan/VKBoundingBox.cpp +++ b/Source/Core/VideoBackends/Vulkan/VKBoundingBox.cpp @@ -77,7 +77,7 @@ std::vector VKBoundingBox::Read(u32 index, u32 length) return values; } -void VKBoundingBox::Write(u32 index, const std::vector& values) +void VKBoundingBox::Write(u32 index, std::span values) { // We can't issue vkCmdUpdateBuffer within a render pass. // However, the writes must be serialized, so we can't put it in the init buffer. @@ -91,8 +91,7 @@ void VKBoundingBox::Write(u32 index, const std::vector& values) // Write the values to the GPU buffer vkCmdUpdateBuffer(g_command_buffer_mgr->GetCurrentCommandBuffer(), m_gpu_buffer, - index * sizeof(BBoxType), values.size() * sizeof(BBoxType), - reinterpret_cast(values.data())); + index * sizeof(BBoxType), values.size() * sizeof(BBoxType), values.data()); // Restore fragment shader access to the buffer. StagingBuffer::BufferMemoryBarrier( diff --git a/Source/Core/VideoBackends/Vulkan/VKBoundingBox.h b/Source/Core/VideoBackends/Vulkan/VKBoundingBox.h index 107abc75517a..3520b8892c10 100644 --- a/Source/Core/VideoBackends/Vulkan/VKBoundingBox.h +++ b/Source/Core/VideoBackends/Vulkan/VKBoundingBox.h @@ -26,7 +26,7 @@ class VKBoundingBox final : public BoundingBox protected: std::vector Read(u32 index, u32 length) override; - void Write(u32 index, const std::vector& values) override; + void Write(u32 index, std::span values) override; private: bool CreateGPUBuffer(); diff --git a/Source/Core/VideoCommon/BoundingBox.cpp b/Source/Core/VideoCommon/BoundingBox.cpp index ffc85d824c81..33174012d74f 100644 --- a/Source/Core/VideoCommon/BoundingBox.cpp +++ b/Source/Core/VideoCommon/BoundingBox.cpp @@ -51,7 +51,7 @@ void BoundingBox::Flush() for (u32 i = start; i < end; ++i) m_dirty[i] = false; - Write(start, std::vector(m_values.begin() + start, m_values.begin() + end)); + Write(start, std::span(m_values.begin() + start, m_values.begin() + end)); } } diff --git a/Source/Core/VideoCommon/BoundingBox.h b/Source/Core/VideoCommon/BoundingBox.h index ed4861d6ad41..4c3064ce5340 100644 --- a/Source/Core/VideoCommon/BoundingBox.h +++ b/Source/Core/VideoCommon/BoundingBox.h @@ -5,6 +5,7 @@ #include #include +#include #include #include "Common/CommonTypes.h" @@ -38,8 +39,7 @@ class BoundingBox protected: virtual std::vector Read(u32 index, u32 length) = 0; - // TODO: This can likely use std::span once we're on C++20 - virtual void Write(u32 index, const std::vector& values) = 0; + virtual void Write(u32 index, std::span values) = 0; private: void Readback();