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.
This commit is contained in:
Lioncash 2023-12-09 14:54:17 -05:00
parent dd227fea5a
commit 5f6c76af51
15 changed files with 19 additions and 20 deletions

View File

@ -88,12 +88,12 @@ std::vector<BBoxType> D3DBoundingBox::Read(u32 index, u32 length)
return values; return values;
} }
void D3DBoundingBox::Write(u32 index, const std::vector<BBoxType>& values) void D3DBoundingBox::Write(u32 index, std::span<const BBoxType> values)
{ {
D3D11_BOX box{index * sizeof(BBoxType), D3D11_BOX box{index * sizeof(BBoxType),
0, 0,
0, 0,
static_cast<u32>(index + values.size()) * sizeof(BBoxType), static_cast<u32>((index + values.size()) * sizeof(BBoxType)),
1, 1,
1}; 1};
D3D::context->UpdateSubresource(m_buffer.Get(), 0, &box, values.data(), 0, 0); D3D::context->UpdateSubresource(m_buffer.Get(), 0, &box, values.data(), 0, 0);

View File

@ -19,7 +19,7 @@ public:
protected: protected:
std::vector<BBoxType> Read(u32 index, u32 length) override; std::vector<BBoxType> Read(u32 index, u32 length) override;
void Write(u32 index, const std::vector<BBoxType>& values) override; void Write(u32 index, std::span<const BBoxType> values) override;
private: private:
ComPtr<ID3D11Buffer> m_buffer; ComPtr<ID3D11Buffer> m_buffer;

View File

@ -56,9 +56,9 @@ std::vector<BBoxType> D3D12BoundingBox::Read(u32 index, u32 length)
return values; return values;
} }
void D3D12BoundingBox::Write(u32 index, const std::vector<BBoxType>& values) void D3D12BoundingBox::Write(u32 index, std::span<const BBoxType> values)
{ {
const u32 copy_size = static_cast<u32>(values.size()) * sizeof(BBoxType); const u32 copy_size = static_cast<u32>(values.size() * sizeof(BBoxType));
if (!m_upload_buffer.ReserveMemory(copy_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"); WARN_LOG_FMT(VIDEO, "Executing command list while waiting for space in bbox stream buffer");

View File

@ -21,7 +21,7 @@ public:
protected: protected:
std::vector<BBoxType> Read(u32 index, u32 length) override; std::vector<BBoxType> Read(u32 index, u32 length) override;
void Write(u32 index, const std::vector<BBoxType>& values) override; void Write(u32 index, std::span<const BBoxType> values) override;
private: private:
static constexpr u32 BUFFER_SIZE = sizeof(BBoxType) * NUM_BBOX_VALUES; static constexpr u32 BUFFER_SIZE = sizeof(BBoxType) * NUM_BBOX_VALUES;

View File

@ -18,7 +18,7 @@ public:
protected: protected:
std::vector<BBoxType> Read(u32 index, u32 length) override; std::vector<BBoxType> Read(u32 index, u32 length) override;
void Write(u32 index, const std::vector<BBoxType>& values) override; void Write(u32 index, std::span<const BBoxType> values) override;
private: private:
BBoxType* m_cpu_buffer_ptr; BBoxType* m_cpu_buffer_ptr;

View File

@ -42,7 +42,7 @@ std::vector<BBoxType> Metal::BoundingBox::Read(u32 index, u32 length)
} }
} }
void Metal::BoundingBox::Write(u32 index, const std::vector<BBoxType>& values) void Metal::BoundingBox::Write(u32 index, std::span<const BBoxType> values)
{ {
const u32 size = values.size() * sizeof(BBoxType); const u32 size = values.size() * sizeof(BBoxType);
if (!g_state_tracker->HasUnflushedData() && !g_state_tracker->GPUBusy()) if (!g_state_tracker->HasUnflushedData() && !g_state_tracker->GPUBusy())

View File

@ -19,7 +19,7 @@ protected:
{ {
return std::vector<BBoxType>(length); return std::vector<BBoxType>(length);
} }
void Write(u32 index, const std::vector<BBoxType>& values) override {} void Write(u32 index, std::span<const BBoxType> values) override {}
}; };
} // namespace Null } // namespace Null

View File

@ -66,7 +66,7 @@ std::vector<BBoxType> OGLBoundingBox::Read(u32 index, u32 length)
return values; return values;
} }
void OGLBoundingBox::Write(u32 index, const std::vector<BBoxType>& values) void OGLBoundingBox::Write(u32 index, std::span<const BBoxType> values)
{ {
glBindBuffer(GL_SHADER_STORAGE_BUFFER, m_buffer_id); glBindBuffer(GL_SHADER_STORAGE_BUFFER, m_buffer_id);
glBufferSubData(GL_SHADER_STORAGE_BUFFER, sizeof(BBoxType) * index, glBufferSubData(GL_SHADER_STORAGE_BUFFER, sizeof(BBoxType) * index,

View File

@ -19,7 +19,7 @@ public:
protected: protected:
std::vector<BBoxType> Read(u32 index, u32 length) override; std::vector<BBoxType> Read(u32 index, u32 length) override;
void Write(u32 index, const std::vector<BBoxType>& values) override; void Write(u32 index, std::span<const BBoxType> values) override;
private: private:
GLuint m_buffer_id = 0; GLuint m_buffer_id = 0;

View File

@ -55,7 +55,7 @@ std::vector<BBoxType> SWBoundingBox::Read(u32 index, u32 length)
return values; return values;
} }
void SWBoundingBox::Write(u32 index, const std::vector<BBoxType>& values) void SWBoundingBox::Write(u32 index, std::span<const BBoxType> values)
{ {
for (size_t i = 0; i < values.size(); i++) for (size_t i = 0; i < values.size(); i++)
{ {

View File

@ -37,7 +37,7 @@ public:
protected: protected:
std::vector<BBoxType> Read(u32 index, u32 length) override; std::vector<BBoxType> Read(u32 index, u32 length) override;
void Write(u32 index, const std::vector<BBoxType>& values) override; void Write(u32 index, std::span<const BBoxType> values) override;
}; };
} // namespace SW } // namespace SW

View File

@ -77,7 +77,7 @@ std::vector<BBoxType> VKBoundingBox::Read(u32 index, u32 length)
return values; return values;
} }
void VKBoundingBox::Write(u32 index, const std::vector<BBoxType>& values) void VKBoundingBox::Write(u32 index, std::span<const BBoxType> values)
{ {
// We can't issue vkCmdUpdateBuffer within a render pass. // 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. // 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<BBoxType>& values)
// Write the values to the GPU buffer // Write the values to the GPU buffer
vkCmdUpdateBuffer(g_command_buffer_mgr->GetCurrentCommandBuffer(), m_gpu_buffer, vkCmdUpdateBuffer(g_command_buffer_mgr->GetCurrentCommandBuffer(), m_gpu_buffer,
index * sizeof(BBoxType), values.size() * sizeof(BBoxType), index * sizeof(BBoxType), values.size() * sizeof(BBoxType), values.data());
reinterpret_cast<const BBoxType*>(values.data()));
// Restore fragment shader access to the buffer. // Restore fragment shader access to the buffer.
StagingBuffer::BufferMemoryBarrier( StagingBuffer::BufferMemoryBarrier(

View File

@ -26,7 +26,7 @@ public:
protected: protected:
std::vector<BBoxType> Read(u32 index, u32 length) override; std::vector<BBoxType> Read(u32 index, u32 length) override;
void Write(u32 index, const std::vector<BBoxType>& values) override; void Write(u32 index, std::span<const BBoxType> values) override;
private: private:
bool CreateGPUBuffer(); bool CreateGPUBuffer();

View File

@ -51,7 +51,7 @@ void BoundingBox::Flush()
for (u32 i = start; i < end; ++i) for (u32 i = start; i < end; ++i)
m_dirty[i] = false; m_dirty[i] = false;
Write(start, std::vector<BBoxType>(m_values.begin() + start, m_values.begin() + end)); Write(start, std::span(m_values.begin() + start, m_values.begin() + end));
} }
} }

View File

@ -5,6 +5,7 @@
#include <array> #include <array>
#include <memory> #include <memory>
#include <span>
#include <vector> #include <vector>
#include "Common/CommonTypes.h" #include "Common/CommonTypes.h"
@ -38,8 +39,7 @@ public:
protected: protected:
virtual std::vector<BBoxType> Read(u32 index, u32 length) = 0; virtual std::vector<BBoxType> Read(u32 index, u32 length) = 0;
// TODO: This can likely use std::span once we're on C++20 virtual void Write(u32 index, std::span<const BBoxType> values) = 0;
virtual void Write(u32 index, const std::vector<BBoxType>& values) = 0;
private: private:
void Readback(); void Readback();