From 607fc9cfc8813419454dda0d978fbe731d87cafe Mon Sep 17 00:00:00 2001 From: assiduous Date: Mon, 28 Oct 2024 14:32:14 -0700 Subject: [PATCH] IDataBlob: added Offset parameter to GetDataPtr() and GetConstDataPtr() methods --- Common/interface/DataBlobImpl.hpp | 12 ++++++------ Common/interface/ProxyDataBlob.hpp | 10 ++++++---- Common/interface/StringDataBlobImpl.hpp | 8 ++++---- Common/src/DataBlobImpl.cpp | 8 ++++---- Common/src/MemoryFileStream.cpp | 14 +++++++------- Primitives/interface/DataBlob.h | 16 +++++++++------- .../DiligentGraphicsEngineTests.cs | 2 +- 7 files changed, 37 insertions(+), 33 deletions(-) diff --git a/Common/interface/DataBlobImpl.hpp b/Common/interface/DataBlobImpl.hpp index 00f9f33b2..4566c3c72 100644 --- a/Common/interface/DataBlobImpl.hpp +++ b/Common/interface/DataBlobImpl.hpp @@ -60,21 +60,21 @@ class DataBlobImpl final : public Diligent::ObjectBase virtual size_t DILIGENT_CALL_TYPE GetSize() const override; /// Returns the pointer to the internal data buffer - virtual void* DILIGENT_CALL_TYPE GetDataPtr() override; + virtual void* DILIGENT_CALL_TYPE GetDataPtr(size_t Offset = 0) override; /// Returns const pointer to the internal data buffer - virtual const void* DILIGENT_CALL_TYPE GetConstDataPtr() const override; + virtual const void* DILIGENT_CALL_TYPE GetConstDataPtr(size_t Offset = 0) const override; template - T* GetDataPtr() + T* GetDataPtr(size_t Offset = 0) { - return reinterpret_cast(GetDataPtr()); + return reinterpret_cast(GetDataPtr(Offset)); } template - const T* GetConstDataPtr() const + const T* GetConstDataPtr(size_t Offset = 0) const { - return reinterpret_cast(GetConstDataPtr()); + return reinterpret_cast(GetConstDataPtr(Offset)); } private: diff --git a/Common/interface/ProxyDataBlob.hpp b/Common/interface/ProxyDataBlob.hpp index 71756128c..8f24b32bd 100644 --- a/Common/interface/ProxyDataBlob.hpp +++ b/Common/interface/ProxyDataBlob.hpp @@ -78,15 +78,17 @@ class ProxyDataBlob : public ObjectBase } /// Returns the pointer to the internal data buffer - virtual void* DILIGENT_CALL_TYPE GetDataPtr() override + virtual void* DILIGENT_CALL_TYPE GetDataPtr(size_t Offset = 0) override { - return m_pData; + VERIFY(Offset < m_Size, "Offset (", Offset, ") exceeds the data size (", m_Size, ")"); + return static_cast(m_pData) + Offset; } /// Returns the pointer to the internal data buffer - virtual const void* DILIGENT_CALL_TYPE GetConstDataPtr() const override + virtual const void* DILIGENT_CALL_TYPE GetConstDataPtr(size_t Offset = 0) const override { - return m_pConstData; + VERIFY(Offset < m_Size, "Offset (", Offset, ") exceeds the data size (", m_Size, ")"); + return static_cast(m_pConstData) + Offset; } private: diff --git a/Common/interface/StringDataBlobImpl.hpp b/Common/interface/StringDataBlobImpl.hpp index f7b20f364..92e473975 100644 --- a/Common/interface/StringDataBlobImpl.hpp +++ b/Common/interface/StringDataBlobImpl.hpp @@ -73,15 +73,15 @@ class StringDataBlobImpl : public ObjectBase } /// Returns the pointer to the internal data buffer - virtual void* DILIGENT_CALL_TYPE GetDataPtr() override + virtual void* DILIGENT_CALL_TYPE GetDataPtr(size_t Offset = 0) override { - return &m_String[0]; + return &m_String[Offset]; } /// Returns the pointer to the internal data buffer - virtual const void* DILIGENT_CALL_TYPE GetConstDataPtr() const override + virtual const void* DILIGENT_CALL_TYPE GetConstDataPtr(size_t Offset = 0) const override { - return &m_String[0]; + return &m_String[Offset]; } private: diff --git a/Common/src/DataBlobImpl.cpp b/Common/src/DataBlobImpl.cpp index 9cf2d03be..21ae38c19 100644 --- a/Common/src/DataBlobImpl.cpp +++ b/Common/src/DataBlobImpl.cpp @@ -72,15 +72,15 @@ size_t DataBlobImpl::GetSize() const } /// Returns the pointer to the internal data buffer -void* DataBlobImpl::GetDataPtr() +void* DataBlobImpl::GetDataPtr(size_t Offset) { - return m_DataBuff.data(); + return &m_DataBuff[Offset]; } /// Returns const pointer to the internal data buffer -const void* DataBlobImpl::GetConstDataPtr() const +const void* DataBlobImpl::GetConstDataPtr(size_t Offset) const { - return m_DataBuff.data(); + return &m_DataBuff[Offset]; } IMPLEMENT_QUERY_INTERFACE(DataBlobImpl, IID_DataBlob, TBase) diff --git a/Common/src/MemoryFileStream.cpp b/Common/src/MemoryFileStream.cpp index efdae8009..ce1cd7a3f 100644 --- a/Common/src/MemoryFileStream.cpp +++ b/Common/src/MemoryFileStream.cpp @@ -1,5 +1,5 @@ /* - * Copyright 2019-2022 Diligent Graphics LLC + * Copyright 2019-2024 Diligent Graphics LLC * Copyright 2015-2019 Egor Yusov * * Licensed under the Apache License, Version 2.0 (the "License"); @@ -50,9 +50,9 @@ IMPLEMENT_QUERY_INTERFACE(MemoryFileStream, IID_FileStream, TBase) bool MemoryFileStream::Read(void* Data, size_t Size) { VERIFY_EXPR(m_CurrentOffset <= m_DataBlob->GetSize()); - auto BytesLeft = m_DataBlob->GetSize() - m_CurrentOffset; - auto BytesToRead = std::min(BytesLeft, Size); - auto* pSrcData = reinterpret_cast(m_DataBlob->GetDataPtr()) + m_CurrentOffset; + size_t BytesLeft = m_DataBlob->GetSize() - m_CurrentOffset; + size_t BytesToRead = std::min(BytesLeft, Size); + const void* pSrcData = m_DataBlob->GetConstDataPtr(m_CurrentOffset); memcpy(Data, pSrcData, BytesToRead); m_CurrentOffset += BytesToRead; return Size == BytesToRead; @@ -60,9 +60,9 @@ bool MemoryFileStream::Read(void* Data, size_t Size) void MemoryFileStream::ReadBlob(IDataBlob* pData) { - auto BytesLeft = m_DataBlob->GetSize() - m_CurrentOffset; + size_t BytesLeft = m_DataBlob->GetSize() - m_CurrentOffset; pData->Resize(BytesLeft); - auto res = Read(pData->GetDataPtr(), pData->GetSize()); + bool res = Read(pData->GetDataPtr(), pData->GetSize()); VERIFY_EXPR(res); (void)res; } @@ -73,7 +73,7 @@ bool MemoryFileStream::Write(const void* Data, size_t Size) { m_DataBlob->Resize(m_CurrentOffset + Size); } - auto* DstData = reinterpret_cast(m_DataBlob->GetDataPtr()) + m_CurrentOffset; + void* DstData = m_DataBlob->GetDataPtr(m_CurrentOffset); memcpy(DstData, Data, Size); m_CurrentOffset += Size; return true; diff --git a/Primitives/interface/DataBlob.h b/Primitives/interface/DataBlob.h index faf7b2fa7..0ddbb2feb 100644 --- a/Primitives/interface/DataBlob.h +++ b/Primitives/interface/DataBlob.h @@ -1,5 +1,5 @@ /* - * Copyright 2019-2023 Diligent Graphics LLC + * Copyright 2019-2024 Diligent Graphics LLC * Copyright 2015-2019 Egor Yusov * * Licensed under the Apache License, Version 2.0 (the "License"); @@ -59,10 +59,12 @@ DILIGENT_BEGIN_INTERFACE(IDataBlob, IObject) VIRTUAL size_t METHOD(GetSize)(THIS) CONST PURE; /// Returns the pointer to the internal data buffer - VIRTUAL void* METHOD(GetDataPtr)(THIS) PURE; + VIRTUAL void* METHOD(GetDataPtr)(THIS_ + size_t Offset DEFAULT_VALUE(0)) PURE; /// Returns const pointer to the internal data buffer - VIRTUAL const void* METHOD(GetConstDataPtr)(THIS) CONST PURE; + VIRTUAL const void* METHOD(GetConstDataPtr)(THIS_ + size_t Offset DEFAULT_VALUE(0)) CONST PURE; }; DILIGENT_END_INTERFACE @@ -72,10 +74,10 @@ DILIGENT_END_INTERFACE // clang-format off -# define IDataBlob_Resize(This, ...) CALL_IFACE_METHOD(DataBlob, Resize, This, __VA_ARGS__) -# define IDataBlob_GetSize(This) CALL_IFACE_METHOD(DataBlob, GetSize, This) -# define IDataBlob_GetDataPtr(This) CALL_IFACE_METHOD(DataBlob, GetDataPtr, This) -# define IDataBlob_GetConstDataPtr(This) CALL_IFACE_METHOD(DataBlob, GetConstDataPtr, This) +# define IDataBlob_Resize(This, ...) CALL_IFACE_METHOD(DataBlob, Resize, This, __VA_ARGS__) +# define IDataBlob_GetSize(This) CALL_IFACE_METHOD(DataBlob, GetSize, This) +# define IDataBlob_GetDataPtr(This, ...) CALL_IFACE_METHOD(DataBlob, GetDataPtr, This, __VA_ARGS__) +# define IDataBlob_GetConstDataPtr(This, ...) CALL_IFACE_METHOD(DataBlob, GetConstDataPtr, This, __VA_ARGS__) // clang-format on diff --git a/Tests/DiligentCoreTest.NET/DiligentGraphicsEngineTests.cs b/Tests/DiligentCoreTest.NET/DiligentGraphicsEngineTests.cs index e1cc6150e..750a570df 100644 --- a/Tests/DiligentCoreTest.NET/DiligentGraphicsEngineTests.cs +++ b/Tests/DiligentCoreTest.NET/DiligentGraphicsEngineTests.cs @@ -628,7 +628,7 @@ PSOutput main(in PSInput PSIn) Assert.Null(shader); Assert.NotNull(compilerError); - var compilerOutputStr = Marshal.PtrToStringAnsi(compilerError.GetDataPtr(), (int)compilerError.GetSize()); + var compilerOutputStr = Marshal.PtrToStringAnsi(compilerError.GetDataPtr(0), (int)compilerError.GetSize()); Assert.Contains("error", compilerOutputStr); m_Output.WriteLine(compilerOutputStr); }