Skip to content

Commit

Permalink
IDataBlob: added Offset parameter to GetDataPtr() and GetConstDataPtr…
Browse files Browse the repository at this point in the history
…() methods
  • Loading branch information
TheMostDiligent committed Oct 28, 2024
1 parent cb699ef commit 607fc9c
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 33 deletions.
12 changes: 6 additions & 6 deletions Common/interface/DataBlobImpl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,21 +60,21 @@ class DataBlobImpl final : public Diligent::ObjectBase<IDataBlob>
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 <typename T>
T* GetDataPtr()
T* GetDataPtr(size_t Offset = 0)
{
return reinterpret_cast<T*>(GetDataPtr());
return reinterpret_cast<T*>(GetDataPtr(Offset));
}

template <typename T>
const T* GetConstDataPtr() const
const T* GetConstDataPtr(size_t Offset = 0) const
{
return reinterpret_cast<const T*>(GetConstDataPtr());
return reinterpret_cast<const T*>(GetConstDataPtr(Offset));
}

private:
Expand Down
10 changes: 6 additions & 4 deletions Common/interface/ProxyDataBlob.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,15 +78,17 @@ class ProxyDataBlob : public ObjectBase<IDataBlob>
}

/// 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<Uint8*>(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<const Uint8*>(m_pConstData) + Offset;
}

private:
Expand Down
8 changes: 4 additions & 4 deletions Common/interface/StringDataBlobImpl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,15 +73,15 @@ class StringDataBlobImpl : public ObjectBase<IDataBlob>
}

/// 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:
Expand Down
8 changes: 4 additions & 4 deletions Common/src/DataBlobImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
14 changes: 7 additions & 7 deletions Common/src/MemoryFileStream.cpp
Original file line number Diff line number Diff line change
@@ -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");
Expand Down Expand Up @@ -50,19 +50,19 @@ 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<const Uint8*>(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;
}

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;
}
Expand All @@ -73,7 +73,7 @@ bool MemoryFileStream::Write(const void* Data, size_t Size)
{
m_DataBlob->Resize(m_CurrentOffset + Size);
}
auto* DstData = reinterpret_cast<Uint8*>(m_DataBlob->GetDataPtr()) + m_CurrentOffset;
void* DstData = m_DataBlob->GetDataPtr(m_CurrentOffset);
memcpy(DstData, Data, Size);
m_CurrentOffset += Size;
return true;
Expand Down
16 changes: 9 additions & 7 deletions Primitives/interface/DataBlob.h
Original file line number Diff line number Diff line change
@@ -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");
Expand Down Expand Up @@ -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

Expand All @@ -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

Expand Down
2 changes: 1 addition & 1 deletion Tests/DiligentCoreTest.NET/DiligentGraphicsEngineTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down

0 comments on commit 607fc9c

Please sign in to comment.