Skip to content

Commit 450244c

Browse files
DeviceObjectBase: reworked unique identifiers
1 parent 70e1a46 commit 450244c

File tree

2 files changed

+29
-12
lines changed

2 files changed

+29
-12
lines changed

Graphics/GraphicsEngine/include/DeviceObjectBase.hpp

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2019-2023 Diligent Graphics LLC
2+
* Copyright 2019-2024 Diligent Graphics LLC
33
* Copyright 2015-2019 Egor Yusov
44
*
55
* Licensed under the Apache License, Version 2.0 (the "License");
@@ -60,6 +60,7 @@ class DeviceObjectBase : public ObjectBase<BaseInterface>
6060
TBase {pRefCounters},
6161
m_pDevice {pDevice },
6262
m_Desc {ObjDesc },
63+
m_UniqueID {pDevice != nullptr ? pDevice->GenerateUniqueId() : 0},
6364
m_bIsDeviceInternal{bIsDeviceInternal}
6465
//clang-format on
6566
{
@@ -145,14 +146,20 @@ class DeviceObjectBase : public ObjectBase<BaseInterface>
145146
}
146147

147148
/// Returns unique identifier
149+
150+
/// \remarks
151+
/// This unique ID is used to unambiguously identify device object for
152+
/// tracking purposes within the device.
153+
/// Neither GL handle nor pointer could be safely used for this purpose
154+
/// as both GL reuses released handles and we pool device objects and reuse
155+
/// released ones.
156+
///
157+
/// \note
158+
/// Objects created from different devices may have the same unique ID.
148159
virtual Int32 DILIGENT_CALL_TYPE GetUniqueID() const override final
149160
{
150-
/// \note
151-
/// This unique ID is used to unambiguously identify device object for
152-
/// tracking purposes.
153-
/// Neither GL handle nor pointer could be safely used for this purpose
154-
/// as both GL reuses released handles and the OS reuses released pointers.
155-
return m_UniqueID.GetID();
161+
VERIFY(m_UniqueID != 0, "Unique ID is not initialized. This indicates that this device object has been created without a device");
162+
return m_UniqueID;
156163
}
157164

158165
/// Implementation of IDeviceObject::SetUserData.
@@ -189,10 +196,11 @@ class DeviceObjectBase : public ObjectBase<BaseInterface>
189196
/// Object description
190197
ObjectDescType m_Desc;
191198

192-
// Template argument is only used to separate counters for
193-
// different groups of objects
194-
UniqueIdHelper<BaseInterface> m_UniqueID;
195-
const bool m_bIsDeviceInternal;
199+
// WARNING: using static counter for unique ID is not safe
200+
// as there may be different counter instances in different
201+
// executable units (e.g. in different DLLs).
202+
const UniqueIdentifier m_UniqueID;
203+
const bool m_bIsDeviceInternal;
196204

197205
RefCntAutoPtr<IObject> m_pUserData;
198206
};

Graphics/GraphicsEngine/include/RenderDeviceBase.hpp

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2019-2023 Diligent Graphics LLC
2+
* Copyright 2019-2024 Diligent Graphics LLC
33
* Copyright 2015-2019 Egor Yusov
44
*
55
* Licensed under the Apache License, Version 2.0 (the "License");
@@ -30,6 +30,8 @@
3030
/// \file
3131
/// Implementation of the Diligent::RenderDeviceBase template class and related structures
3232

33+
#include <atomic>
34+
3335
#include "RenderDevice.h"
3436
#include "DeviceObjectBase.hpp"
3537
#include "Defines.h"
@@ -329,6 +331,11 @@ class RenderDeviceBase : public ObjectBase<typename EngineImplTraits::RenderDevi
329331
return m_DeviceInfo.Features;
330332
}
331333

334+
UniqueIdentifier GenerateUniqueId()
335+
{
336+
return m_UniqueId.fetch_add(1) + 1;
337+
}
338+
332339
protected:
333340
virtual void TestTextureFormat(TEXTURE_FORMAT TexFormat) = 0;
334341

@@ -597,6 +604,8 @@ class RenderDeviceBase : public ObjectBase<typename EngineImplTraits::RenderDevi
597604
FixedBlockMemoryAllocator m_PipeResSignAllocator; ///< Allocator for pipeline resource signature objects
598605
FixedBlockMemoryAllocator m_MemObjAllocator; ///< Allocator for device memory objects
599606
FixedBlockMemoryAllocator m_PSOCacheAllocator; ///< Allocator for pipeline state cache objects
607+
608+
std::atomic<UniqueIdentifier> m_UniqueId{0};
600609
};
601610

602611
} // namespace Diligent

0 commit comments

Comments
 (0)