Skip to content

Commit c0903bf

Browse files
StringDataBlobImpl: use raw allocator for string memory (#703)
1 parent f3201da commit c0903bf

File tree

6 files changed

+41
-31
lines changed

6 files changed

+41
-31
lines changed

Common/interface/DataBlobImpl.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ namespace Diligent
4242
{
4343

4444
/// Base interface for a data blob
45-
class DataBlobImpl final : public Diligent::ObjectBase<IDataBlob>
45+
class DataBlobImpl final : public ObjectBase<IDataBlob>
4646
{
4747
public:
4848
using TBase = ObjectBase<IDataBlob>;

Common/interface/STDAllocator.hpp

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2019-2024 Diligent Graphics LLC
2+
* Copyright 2019-2025 Diligent Graphics LLC
33
* Copyright 2015-2019 Egor Yusov
44
*
55
* Licensed under the Apache License, Version 2.0 (the "License");
@@ -171,7 +171,7 @@ bool operator==(const STDAllocator<T, A>& left, const STDAllocator<U, A>& right)
171171
}
172172

173173
template <class T, class U, class A>
174-
bool operator!=(const STDAllocator<T, A>& left, const STDAllocator<U, A>& right)
174+
bool operator!=(const STDAllocator<T, A>& left, const STDAllocator<U, A>& right) noexcept
175175
{
176176
return !(left == right);
177177
}
@@ -216,4 +216,11 @@ struct STDDeleter
216216
};
217217
template <class T> using STDDeleterRawMem = STDDeleter<T, IMemoryAllocator>;
218218

219+
/// String using custom STDAllocator with IMemoryAllocator
220+
using StringAlloc =
221+
std::basic_string<
222+
Char,
223+
std::char_traits<Char>,
224+
STDAllocator<Char, IMemoryAllocator>>;
225+
219226
} // namespace Diligent

Common/interface/StringDataBlobImpl.hpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2019-2024 Diligent Graphics LLC
2+
* Copyright 2019-2025 Diligent Graphics LLC
33
* Copyright 2015-2019 Egor Yusov
44
*
55
* Licensed under the Apache License, Version 2.0 (the "License");
@@ -36,6 +36,8 @@
3636
#include "../../Primitives/interface/DataBlob.h"
3737
#include "ObjectBase.hpp"
3838
#include "RefCntAutoPtr.hpp"
39+
#include "STDAllocator.hpp"
40+
#include "EngineMemory.h"
3941

4042
namespace Diligent
4143
{
@@ -49,7 +51,7 @@ class StringDataBlobImpl : public ObjectBase<IDataBlob>
4951
template <typename... ArgsType>
5052
StringDataBlobImpl(IReferenceCounters* pRefCounters, ArgsType&&... Args) :
5153
TBase{pRefCounters},
52-
m_String{std::forward<ArgsType>(Args)...}
54+
m_String{std::forward<ArgsType>(Args)..., STD_ALLOCATOR_RAW_MEM(Char, GetRawAllocator(), "Allocator for String")}
5355
{}
5456

5557
template <typename... ArgsType>
@@ -85,7 +87,7 @@ class StringDataBlobImpl : public ObjectBase<IDataBlob>
8587
}
8688

8789
private:
88-
String m_String;
90+
StringAlloc m_String;
8991
};
9092

9193
} // namespace Diligent

Graphics/HLSL2GLSLConverterLib/include/HLSL2GLSLConverterImpl.hpp

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2019-2024 Diligent Graphics LLC
2+
* Copyright 2019-2025 Diligent Graphics LLC
33
* Copyright 2015-2019 Egor Yusov
44
*
55
* Licensed under the Apache License, Version 2.0 (the "License");
@@ -39,6 +39,7 @@
3939
#include "HashUtils.hpp"
4040
#include "Constants.h"
4141
#include "HLSLTokenizer.hpp"
42+
#include "STDAllocator.hpp"
4243

4344
namespace Diligent
4445
{
@@ -145,7 +146,7 @@ class HLSL2GLSLConverterImpl
145146

146147
/// \param [in] Attribs - Conversion attributes.
147148
/// \return Converted GLSL source code.
148-
String Convert(ConversionAttribs& Attribs) const;
149+
StringAlloc Convert(ConversionAttribs& Attribs) const;
149150

150151
/// Creates a conversion stream
151152

@@ -250,12 +251,12 @@ class HLSL2GLSLConverterImpl
250251
size_t NumSymbols,
251252
bool bPreserveTokens);
252253

253-
String Convert(const Char* EntryPoint,
254-
SHADER_TYPE ShaderType,
255-
bool IncludeDefintions,
256-
const char* SamplerSuffix,
257-
bool UseInOutLocationQualifiers,
258-
bool UseRowMajorMatrices);
254+
StringAlloc Convert(const Char* EntryPoint,
255+
SHADER_TYPE ShaderType,
256+
bool IncludeDefintions,
257+
const char* SamplerSuffix,
258+
bool UseInOutLocationQualifiers,
259+
bool UseRowMajorMatrices);
259260

260261
virtual void DILIGENT_CALL_TYPE Convert(const Char* EntryPoint,
261262
SHADER_TYPE ShaderType,
@@ -457,7 +458,7 @@ class HLSL2GLSLConverterImpl
457458
const String& OutStreamName,
458459
const char* EntryPoint);
459460

460-
String BuildGLSLSource();
461+
StringAlloc BuildGLSLSource();
461462

462463
// Tokenized source code
463464
TokenListType m_Tokens;

Graphics/HLSL2GLSLConverterLib/src/HLSL2GLSLConverterImpl.cpp

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4396,9 +4396,9 @@ void HLSL2GLSLConverterImpl::ConversionStream::RemoveSpecialShaderAttributes()
43964396
);
43974397
}
43984398

4399-
String HLSL2GLSLConverterImpl::ConversionStream::BuildGLSLSource()
4399+
StringAlloc HLSL2GLSLConverterImpl::ConversionStream::BuildGLSLSource()
44004400
{
4401-
String Output;
4401+
StringAlloc Output{STD_ALLOCATOR_RAW_MEM(Char, GetRawAllocator(), "Allocator for String")};
44024402
for (const auto& Token : m_Tokens)
44034403
{
44044404
if ((Token.Type == TokenType::kw_linear ||
@@ -4412,8 +4412,8 @@ String HLSL2GLSLConverterImpl::ConversionStream::BuildGLSLSource()
44124412
continue;
44134413
}
44144414

4415-
Output.append(Token.Delimiter);
4416-
Output.append(Token.Literal);
4415+
Output.append(Token.Delimiter.begin(), Token.Delimiter.end());
4416+
Output.append(Token.Literal.begin(), Token.Literal.end());
44174417
}
44184418
return Output;
44194419
}
@@ -4460,7 +4460,7 @@ HLSL2GLSLConverterImpl::ConversionStream::ConversionStream(IReferenceCounters*
44604460
}
44614461

44624462

4463-
String HLSL2GLSLConverterImpl::Convert(ConversionAttribs& Attribs) const
4463+
StringAlloc HLSL2GLSLConverterImpl::Convert(ConversionAttribs& Attribs) const
44644464
{
44654465
if (Attribs.ppConversionStream == nullptr)
44664466
{
@@ -4473,7 +4473,7 @@ String HLSL2GLSLConverterImpl::Convert(ConversionAttribs& Attribs) const
44734473
}
44744474
catch (std::runtime_error&)
44754475
{
4476-
return "";
4476+
return StringAlloc{"", STD_ALLOCATOR_RAW_MEM(Char, GetRawAllocator(), "Allocator for String")};
44774477
}
44784478
}
44794479
else
@@ -4531,7 +4531,7 @@ void HLSL2GLSLConverterImpl::ConversionStream::Convert(const Char* EntryPoint,
45314531
{
45324532
try
45334533
{
4534-
auto GLSLSource = Convert(EntryPoint, ShaderType, IncludeDefintions, SamplerSuffix, UseInOutLocationQualifiers, UseRowMajorMatrices);
4534+
StringAlloc GLSLSource = Convert(EntryPoint, ShaderType, IncludeDefintions, SamplerSuffix, UseInOutLocationQualifiers, UseRowMajorMatrices);
45354535
StringDataBlobImpl* pDataBlob = MakeNewRCObj<StringDataBlobImpl>()(std::move(GLSLSource));
45364536
pDataBlob->QueryInterface(IID_DataBlob, reinterpret_cast<IObject**>(ppGLSLSource));
45374537
}
@@ -4541,12 +4541,12 @@ void HLSL2GLSLConverterImpl::ConversionStream::Convert(const Char* EntryPoint,
45414541
}
45424542
}
45434543

4544-
String HLSL2GLSLConverterImpl::ConversionStream::Convert(const Char* EntryPoint,
4545-
SHADER_TYPE ShaderType,
4546-
bool IncludeDefintions,
4547-
const char* SamplerSuffix,
4548-
bool UseInOutLocationQualifiers,
4549-
bool UseRowMajorMatrices)
4544+
StringAlloc HLSL2GLSLConverterImpl::ConversionStream::Convert(const Char* EntryPoint,
4545+
SHADER_TYPE ShaderType,
4546+
bool IncludeDefintions,
4547+
const char* SamplerSuffix,
4548+
bool UseInOutLocationQualifiers,
4549+
bool UseRowMajorMatrices)
45504550
{
45514551
m_bUseInOutLocationQualifiers = UseInOutLocationQualifiers;
45524552
m_bUseRowMajorMatrices = UseRowMajorMatrices;
@@ -4788,7 +4788,7 @@ String HLSL2GLSLConverterImpl::ConversionStream::Convert(const Char* EntryPoint,
47884788

47894789
RemoveSpecialShaderAttributes();
47904790

4791-
auto GLSLSource = BuildGLSLSource();
4791+
StringAlloc GLSLSource = BuildGLSLSource();
47924792

47934793
if (m_bPreserveTokens)
47944794
{

Graphics/ShaderTools/src/GLSLUtils.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -389,12 +389,12 @@ String BuildGLSLSourceString(const BuildGLSLSourceStringAttribs& Attribs) noexce
389389
// (search for "Input Layout Qualifiers" and "Output Layout Qualifiers").
390390
ConvertAttribs.UseInOutLocationQualifiers = Attribs.Features.SeparablePrograms;
391391
ConvertAttribs.UseRowMajorMatrices = (ShaderCI.CompileFlags & SHADER_COMPILE_FLAG_PACK_MATRIX_ROW_MAJOR) != 0;
392-
String ConvertedSource = Converter.Convert(ConvertAttribs);
392+
StringAlloc ConvertedSource = Converter.Convert(ConvertAttribs);
393393
if (ConvertedSource.empty())
394394
{
395395
LOG_ERROR_AND_THROW("Failed to convert HLSL source to GLSL");
396396
}
397-
GLSLSource.append(ConvertedSource);
397+
GLSLSource.append(ConvertedSource.begin(), ConvertedSource.end());
398398
#endif
399399
}
400400
else

0 commit comments

Comments
 (0)