HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
garbageCollector.h
Go to the documentation of this file.
1 //
2 // Copyright 2020 Pixar
3 //
4 // Licensed under the terms set forth in the LICENSE.txt file available at
5 // https://openusd.org/license.
6 //
7 #ifndef PXR_IMAGING_HGI_GL_GARBAGE_COLLECTOR_H
8 #define PXR_IMAGING_HGI_GL_GARBAGE_COLLECTOR_H
9 
10 #include "pxr/pxr.h"
11 #include "pxr/base/tf/diagnostic.h"
12 
13 #include "pxr/imaging/hgi/hgi.h"
14 #include "pxr/imaging/hgiGL/api.h"
15 
16 #include <mutex>
17 #include <vector>
18 
20 
21 class HgiGL;
22 
23 /// \class HgiGLGarbageCollector
24 ///
25 /// Handles garbage collection of opengl objects by delaying their destruction
26 /// until those objects are no longer used.
27 ///
29 {
30 public:
31  HGIGL_API
33 
34  HGIGL_API
36 
37  /// Destroys the objects inside the garbage collector.
38  /// Thread safety: This call is not thread safe and must be called from
39  /// the thread that has the opengl context bound while no other threads are
40  /// destroying objects (e.g. during EndFrame).
41  HGIGL_API
43 
44  /// Returns a garbage collection vector for a type of handle.
45  /// Thread safety: The returned vector is a thread_local vector so this call
46  /// is thread safe as long as the vector is only used by the calling thread.
55 
56 private:
57  HgiGLGarbageCollector & operator =
58  (const HgiGLGarbageCollector&) = delete;
59  HgiGLGarbageCollector(const HgiGLGarbageCollector&) = delete;
60 
61  /// Returns a thread_local vector in which to store a object handle.
62  /// Thread safety: The returned vector is a thread_local vector so this call
63  /// is thread safe as long as the vector is only used by the calling thread.
64  template<class T>
65  T* _GetThreadLocalStorageList(std::vector<T*>* collector);
66 
67  // List of all the per-thread-vectors of objects that need to be destroyed.
68  // The vectors are static (shared across HGIs), because we use thread_local
69  // in _GetThreadLocalStorageList which makes us share the garbage collector
70  // vectors across Hgi instances.
71  static std::vector<HgiBufferHandleVector*> _bufferList;
72  static std::vector<HgiTextureHandleVector*> _textureList;
73  static std::vector<HgiSamplerHandleVector*> _samplerList;
74  static std::vector<HgiShaderFunctionHandleVector*> _shaderFunctionList;
75  static std::vector<HgiShaderProgramHandleVector*> _shaderProgramList;
76  static std::vector<HgiResourceBindingsHandleVector*> _resourceBindingsList;
77  static std::vector<HgiGraphicsPipelineHandleVector*> _graphicsPipelineList;
78  static std::vector<HgiComputePipelineHandleVector*> _computePipelineList;
79 
80  bool _isDestroying;
81 };
82 
83 
85 
86 #endif
HgiBufferHandleVector * GetBufferList()
HGIGL_API void PerformGarbageCollection()
std::vector< HgiShaderProgramHandle > HgiShaderProgramHandleVector
std::vector< HgiTextureHandle > HgiTextureHandleVector
Definition: texture.h:196
std::vector< HgiShaderFunctionHandle > HgiShaderFunctionHandleVector
HgiGraphicsPipelineHandleVector * GetGraphicsPipelineList()
std::vector< HgiGraphicsPipelineHandle > HgiGraphicsPipelineHandleVector
HGIGL_API HgiGLGarbageCollector()
Definition: hgi.h:54
std::vector< HgiBufferHandle > HgiBufferHandleVector
Definition: buffer.h:135
HgiTextureHandleVector * GetTextureList()
std::vector< HgiResourceBindingsHandle > HgiResourceBindingsHandleVector
#define HGIGL_API
Definition: api.h:23
HGIGL_API ~HgiGLGarbageCollector()
HgiComputePipelineHandleVector * GetComputePipelineList()
PXR_NAMESPACE_CLOSE_SCOPE PXR_NAMESPACE_OPEN_SCOPE
Definition: path.h:1425
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:74
std::vector< HgiComputePipelineHandle > HgiComputePipelineHandleVector
HgiSamplerHandleVector * GetSamplerList()
std::vector< HgiSamplerHandle > HgiSamplerHandleVector
Definition: sampler.h:143
HgiResourceBindingsHandleVector * GetResourceBindingsList()
HgiShaderProgramHandleVector * GetShaderProgramList()
HgiShaderFunctionHandleVector * GetShaderFunctionList()