Diligent Engine API Reference
DeviceObjectBase.h
1 /* Copyright 2015-2018 Egor Yusov
2  *
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
10  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
11  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF ANY PROPRIETARY RIGHTS.
12  *
13  * In no event and under no legal theory, whether in tort (including negligence),
14  * contract, or otherwise, unless required by applicable law (such as deliberate
15  * and grossly negligent acts) or agreed to in writing, shall any Contributor be
16  * liable for any damages, including any direct, indirect, special, incidental,
17  * or consequential damages of any character arising as a result of this License or
18  * out of the use or inability to use the software (including but not limited to damages
19  * for loss of goodwill, work stoppage, computer failure or malfunction, or any and
20  * all other commercial damages or losses), even if such Contributor has been advised
21  * of the possibility of such damages.
22  */
23 
24 #pragma once
25 
28 
29 #include "ObjectBase.h"
30 #include "UniqueIdentifier.h"
31 
32 namespace Diligent
33 {
34 
35 template<typename BaseInterface>
37 
38 class IRenderDevice;
39 
41 template<class BaseInterface, typename ObjectDescType>
42 class DeviceObjectBase : public ObjectBase<BaseInterface>
43 {
44 public:
46 
52  DeviceObjectBase( IReferenceCounters *pRefCounters,
53  IRenderDevice *pDevice,
54  const ObjectDescType &ObjDesc,
55  bool bIsDeviceInternal = false) :
56  TBase(pRefCounters),
57  m_pDevice( pDevice ),
58  // Do not keep strong reference to the device if the object is an internal device object
59  m_spDevice( bIsDeviceInternal ? nullptr : pDevice ),
60  m_ObjectNameCopy(ObjDesc.Name ? ObjDesc.Name : ""),
61  m_Desc( ObjDesc )
62  {
63  m_Desc.Name = m_ObjectNameCopy.c_str();
64 
65  // !!!WARNING!!!
66  // We cannot add resource to the hash table from here, because the object
67  // has not been completely created yet and the reference counters object
68  // is not initialized!
69  //m_pDevice->AddResourceToHash( this ); - ERROR!
70  }
71 
72  DeviceObjectBase( const DeviceObjectBase& ) = delete;
73  DeviceObjectBase( DeviceObjectBase&& ) = delete;
74  DeviceObjectBase& operator = ( const DeviceObjectBase& ) = delete;
75  DeviceObjectBase& operator = ( DeviceObjectBase&& ) = delete;
76 
77  virtual ~DeviceObjectBase()
78  {
79  }
80 
81  inline virtual Atomics::Long Release()override final
82  {
83  // Render device owns allocators for all types of device objects,
84  // so it must be destroyed after all device objects are released.
85  // Consider the following scenario: an object A ownes last strong
86  // reference to the device:
87  //
88  // 1. A::~A() completes
89  // 2. A::~DeviceObjectBase() completes
90  // 3. A::m_spDevice is released
91  // Render device is destroyed, all allocators are invalid
92  // 4. RefCountersImpl::ObjectWrapperBase::DestroyObject() calls
93  // m_pAllocator->Free(m_pObject) - crash!
94 
95  // We must keep the device alive while the object is being destroyed
96  RefCntAutoPtr<IRenderDevice> pDevice(m_spDevice);
97  // Note that internal device object do not keep strong reference to the device
98  return TBase::Release();
99  }
100 
101  IMPLEMENT_QUERY_INTERFACE_IN_PLACE( IID_DeviceObject, TBase )
102 
103  virtual const ObjectDescType& GetDesc()const override final
104  {
105  return m_Desc;
106  }
107 
109  UniqueIdentifier GetUniqueID()const
110  {
116  return m_UniqueID.GetID();
117  }
118 
119  IRenderDevice *GetDevice()const{return m_pDevice;}
120 
121 private:
123  RefCntAutoPtr<IRenderDevice> m_spDevice;
124 
126  IRenderDevice *m_pDevice;
127 
128 protected:
129 
131 
135  const String m_ObjectNameCopy;
136 
138  ObjectDescType m_Desc;
139 
140  // Template argument is only used to separate counters for
141  // different groups of objects
142  UniqueIdHelper<BaseInterface> m_UniqueID;
143 };
144 
145 }
Render device interface.
Definition: RenderDevice.h:55
Template class implementing base functionality for an object.
Definition: ObjectBase.h:62
Namespace for the OpenGL implementation of the graphics engine.
Definition: BufferD3D11Impl.h:34
DeviceObjectBase(IReferenceCounters *pRefCounters, IRenderDevice *pDevice, const ObjectDescType &ObjDesc, bool bIsDeviceInternal=false)
Definition: DeviceObjectBase.h:52
const String m_ObjectNameCopy
Copy of a device object name.
Definition: DeviceObjectBase.h:135
UniqueIdentifier GetUniqueID() const
Returns unique identifier.
Definition: DeviceObjectBase.h:109
Base implementation of a render device.
Definition: DeviceObjectBase.h:36
ObjectDescType m_Desc
Object description.
Definition: DeviceObjectBase.h:138
Template class implementing base functionality for a device object.
Definition: DeviceObjectBase.h:42