From 878a76e25812cccae69a023ea6f65de024533c7d Mon Sep 17 00:00:00 2001 From: Connor McLaughlin Date: Thu, 14 Nov 2019 16:57:58 +1000 Subject: [PATCH] Common: Fix issues in utility classes for readbacks --- src/common/d3d11/staging_texture.cpp | 5 +++-- src/common/d3d11/staging_texture.h | 12 ++++++------ src/common/heap_array.h | 17 +++++++++++++---- src/common/rectangle.h | 2 +- 4 files changed, 23 insertions(+), 13 deletions(-) diff --git a/src/common/d3d11/staging_texture.cpp b/src/common/d3d11/staging_texture.cpp index 35a16192a..73f2b9be2 100644 --- a/src/common/d3d11/staging_texture.cpp +++ b/src/common/d3d11/staging_texture.cpp @@ -14,7 +14,7 @@ StagingTexture::~StagingTexture() bool StagingTexture::Create(ID3D11Device* device, u32 width, u32 height, DXGI_FORMAT format, bool for_uploading) { CD3D11_TEXTURE2D_DESC desc(format, width, height, 1, 1, 0, for_uploading ? D3D11_USAGE_DYNAMIC : D3D11_USAGE_STAGING, - 0, 1, 0, 0); + for_uploading ? D3D11_CPU_ACCESS_WRITE : D3D11_CPU_ACCESS_READ, 1, 0, 0); ComPtr texture; const HRESULT tex_hr = device->CreateTexture2D(&desc, nullptr, texture.GetAddressOf()); @@ -54,6 +54,7 @@ void StagingTexture::Unmap(ID3D11DeviceContext* context) { Assert(IsMapped()); context->Unmap(m_texture.Get(), 0); + m_map = {}; } void StagingTexture::CopyToTexture(ID3D11DeviceContext* context, u32 src_x, u32 src_y, ID3D11Texture2D* dst_texture, @@ -88,4 +89,4 @@ void StagingTexture::CopyFromTexture(ID3D11DeviceContext* context, ID3D11Texture context->CopySubresourceRegion(m_texture.Get(), 0, dst_x, dst_y, 0, src_texture, src_subresource, &box); } -} // namespace D3D \ No newline at end of file +} // namespace D3D11 \ No newline at end of file diff --git a/src/common/d3d11/staging_texture.h b/src/common/d3d11/staging_texture.h index 19d387051..d344945bd 100644 --- a/src/common/d3d11/staging_texture.h +++ b/src/common/d3d11/staging_texture.h @@ -54,18 +54,18 @@ public: { const u8* src_ptr = static_cast(m_map.pData) + (y * m_map.RowPitch) + (x * sizeof(T)); u8* dst_ptr = reinterpret_cast(data); - if (m_map.RowPitch != stride) + if (m_map.RowPitch != (sizeof(T) * stride)) { for (u32 row = 0; row < height; row++) { std::memcpy(dst_ptr, src_ptr, sizeof(T) * width); src_ptr += m_map.RowPitch; - dst_ptr += stride; + dst_ptr += sizeof(T) * stride; } } else { - std::memcpy(dst_ptr, src_ptr, stride * height); + std::memcpy(dst_ptr, src_ptr, (sizeof(T) * stride) * height); } } @@ -74,18 +74,18 @@ public: { const u8* src_ptr = reinterpret_cast(data); u8* dst_ptr = static_cast(m_map.pData) + (y * m_map.RowPitch) + (x * sizeof(T)); - if (m_map.RowPitch != stride) + if (m_map.RowPitch != (sizeof(T) * stride)) { for (u32 row = 0; row < height; row++) { std::memcpy(dst_ptr, src_ptr, sizeof(T) * width); - src_ptr += stride; + src_ptr += sizeof(T) * stride; dst_ptr += m_map.RowPitch; } } else { - std::memcpy(dst_ptr, src_ptr, stride * height); + std::memcpy(dst_ptr, src_ptr, (sizeof(T) * stride) * height); } } diff --git a/src/common/heap_array.h b/src/common/heap_array.h index ac3d8aa4e..d85c30249 100644 --- a/src/common/heap_array.h +++ b/src/common/heap_array.h @@ -1,5 +1,6 @@ #pragma once #include +#include #include template @@ -15,7 +16,7 @@ public: using const_pointer = const T*; using this_type = HeapArray; - HeapArray() { m_data = new T[size]; } + HeapArray() { m_data = new T[SIZE]; } HeapArray(const this_type& copy) { @@ -44,8 +45,16 @@ public: const_pointer cbegin() const { return m_data; } const_pointer cend() const { return m_data + SIZE; } - const_reference operator[](size_type index) const { return m_data[index]; } - reference operator[](size_type index) { return m_data[index]; } + const_reference operator[](size_type index) const + { + assert(index < SIZE); + return m_data[index]; + } + reference operator[](size_type index) + { + assert(index < SIZE); + return m_data[index]; + } const_reference front() const { return m_data[0]; } const_reference back() const { return m_data[SIZE - 1]; } @@ -71,7 +80,7 @@ public: } #define RELATIONAL_OPERATOR(op) \ - bool operator op (const this_type& rhs) const \ + bool operator op(const this_type& rhs) const \ { \ for (size_type i = 0; i < SIZE; i++) \ { \ diff --git a/src/common/rectangle.h b/src/common/rectangle.h index c84bbf68b..01c99ea2d 100644 --- a/src/common/rectangle.h +++ b/src/common/rectangle.h @@ -96,7 +96,7 @@ struct Rectangle right op## = amount; \ bottom op## = amount; \ } \ - constexpr Rectangle operator op(const T amount) \ + constexpr Rectangle operator op(const T amount) const \ { \ return Rectangle(left op amount, top op amount, right op amount, bottom op amount); \ }