Skip to content

Commit

Permalink
IDataBlob: added template versions of GetDataPtr and GetConstDataPtr …
Browse files Browse the repository at this point in the history
…methods
  • Loading branch information
TheMostDiligent committed Oct 29, 2024
1 parent 656c769 commit 858546c
Show file tree
Hide file tree
Showing 8 changed files with 23 additions and 9 deletions.
2 changes: 1 addition & 1 deletion Graphics/GraphicsEngineOpenGL/src/ShaderGLImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,7 @@ bool ShaderGLImpl::GetCompileStatus(IDataBlob** ppCompilerOutput, bool ThrowOnEr
// InfoLogLen accounts for null terminator
auto pOutputDataBlob = DataBlobImpl::Create(InfoLogLen + m_GLSLSourceString.length() + 1);

char* DataPtr = static_cast<char*>(pOutputDataBlob->GetDataPtr());
char* DataPtr = pOutputDataBlob->GetDataPtr<char>();
if (!InfoLog.empty())
{
// Copy info log including null terminator
Expand Down
4 changes: 2 additions & 2 deletions Graphics/HLSL2GLSLConverterLib/src/HLSL2GLSLConverterImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -798,8 +798,8 @@ void HLSL2GLSLConverterImpl::ConversionStream::InsertIncludes(String& GLSLSource
pIncludeDataStream->ReadBlob(pIncludeData);

// Get include text
auto IncludeText = reinterpret_cast<const Char*>(pIncludeData->GetDataPtr());
size_t NumSymbols = pIncludeData->GetSize();
const Char* IncludeText = pIncludeData->GetConstDataPtr<Char>();
size_t NumSymbols = pIncludeData->GetSize();

// Insert the text into source
GLSLSource.insert(IncludeStartPos - GLSLSource.begin(), IncludeText, NumSymbols);
Expand Down
2 changes: 1 addition & 1 deletion Graphics/ShaderTools/include/HLSLUtils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ void HandleHLSLCompilerResult(bool CompilationSucceeded,
const auto ShaderSourceLen = ShaderSource.length();
auto pOutputLogBlob = DataBlobImpl::Create(ShaderSourceLen + 1 + CompilerMsgLen + 1);

auto* log = static_cast<char*>(pOutputLogBlob->GetDataPtr());
char* log = pOutputLogBlob->GetDataPtr<char>();

if (CompilerMsg != nullptr)
memcpy(log, CompilerMsg, CompilerMsgLen);
Expand Down
4 changes: 2 additions & 2 deletions Graphics/ShaderTools/src/GLSLangUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ void LogCompilerError(const char* DebugOutputMessage,
if (ppCompilerOutput != nullptr)
{
auto pOutputDataBlob = DataBlobImpl::Create(SourceCodeLen + 1 + ErrorLog.length() + 1);
char* DataPtr = reinterpret_cast<char*>(pOutputDataBlob->GetDataPtr());
char* DataPtr = pOutputDataBlob->GetDataPtr<char>();
memcpy(DataPtr, ErrorLog.data(), ErrorLog.length() + 1);
memcpy(DataPtr + ErrorLog.length() + 1, ShaderSource, SourceCodeLen + 1);
pOutputDataBlob->QueryInterface(IID_DataBlob, reinterpret_cast<IObject**>(ppCompilerOutput));
Expand Down Expand Up @@ -321,7 +321,7 @@ class IncluderImpl : public ::glslang::TShader::Includer
auto* pNewInclude =
new IncludeResult{
headerName,
reinterpret_cast<const char*>(pFileData->GetDataPtr()),
pFileData->GetConstDataPtr<char>(),
pFileData->GetSize(),
nullptr};

Expand Down
2 changes: 1 addition & 1 deletion Graphics/ShaderTools/src/ShaderToolsCommon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ ShaderSourceFileData ReadShaderSourceFile(const char* Sourc

SourceData.pFileData = DataBlobImpl::Create();
pSourceStream->ReadBlob(SourceData.pFileData);
SourceData.Source = reinterpret_cast<char*>(SourceData.pFileData->GetDataPtr());
SourceData.Source = SourceData.pFileData->GetConstDataPtr<char>();
SourceData.SourceLength = StaticCast<Uint32>(SourceData.pFileData->GetSize());
}
else
Expand Down
2 changes: 1 addition & 1 deletion Graphics/ShaderTools/src/WGSLShaderResources.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -761,7 +761,7 @@ WGSLShaderResources::WGSLShaderResources(IMemoryAllocator& Allocator,
{
RefCntAutoPtr<DataBlobImpl> pOutputDataBlob = DataBlobImpl::Create(WGSL.length() + 1 + Diagnostics.length() + 1);

char* DataPtr = reinterpret_cast<char*>(pOutputDataBlob->GetDataPtr());
char* DataPtr = pOutputDataBlob->GetDataPtr<char>();
memcpy(DataPtr, Diagnostics.data(), Diagnostics.length() + 1);
memcpy(DataPtr + Diagnostics.length() + 1, WGSL.data(), WGSL.length() + 1);
pOutputDataBlob->QueryInterface(IID_DataBlob, reinterpret_cast<IObject**>(ppTintOutput));
Expand Down
14 changes: 14 additions & 0 deletions Primitives/interface/DataBlob.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,20 @@ DILIGENT_BEGIN_INTERFACE(IDataBlob, IObject)
/// Returns const pointer to the internal data buffer
VIRTUAL const void* METHOD(GetConstDataPtr)(THIS_
size_t Offset DEFAULT_VALUE(0)) CONST PURE;

#if DILIGENT_CPP_INTERFACE
template <typename T>
T* GetDataPtr(size_t Offset = 0)
{
return static_cast<T*>(GetDataPtr(Offset));
}

template <typename T>
const T* GetConstDataPtr(size_t Offset = 0) const
{
return static_cast<const T*>(GetConstDataPtr(Offset));
}
#endif
};
DILIGENT_END_INTERFACE

Expand Down
2 changes: 1 addition & 1 deletion Tests/DiligentCoreAPITest/src/BrokenShaderTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ void TestBrokenShader(const char* Source,
ASSERT_NE(pErrors, nullptr);
}
ASSERT_NE(pErrors, nullptr);
const char* Msg = reinterpret_cast<const char*>(pErrors->GetDataPtr());
const char* Msg = pErrors->GetConstDataPtr<char>();
LOG_INFO_MESSAGE("Compiler output:\n", Msg);
}

Expand Down

0 comments on commit 858546c

Please sign in to comment.