HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
textureObjectRegistry.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_HD_ST_TEXTURE_OBJECT_REGISTRY_H
8 #define PXR_IMAGING_HD_ST_TEXTURE_OBJECT_REGISTRY_H
9 
10 #include "pxr/pxr.h"
11 #include "pxr/imaging/hdSt/api.h"
12 #include "pxr/imaging/hdSt/enums.h"
14 
15 #include <tbb/concurrent_vector.h>
16 #include <vector>
17 #include <atomic>
18 
20 
22  std::shared_ptr<class HdStTextureObject>;
24  std::weak_ptr<class HdStTextureObject>;
26  std::vector<HdStTextureObjectPtr>;
29 
30 
31 /// \class HdSt_TextureObjectRegistry
32 ///
33 /// A central registry for texture GPU resources.
34 ///
36 {
37 public:
38  HDST_API
40  HDST_API
42 
43  /// Allocate texture.
44  ///
45  /// This just creates the HdStTextureObject, the actual GPU
46  /// resources won't be allocated until the Commit phase.
47  ///
48  HDST_API
50  const HdStTextureIdentifier &textureId,
51  HdStTextureType textureType);
52 
53  /// Create GPU texture objects, load textures from files and
54  /// upload to GPU.
55  ///
56  HDST_API
57  std::set<HdStTextureObjectSharedPtr> Commit();
58 
59  /// Free GPU resources of textures not used by any client.
60  ///
61  HDST_API
62  void GarbageCollect();
63 
64  /// Mark texture file path as dirty. All textures whose identifier
65  /// contains the file path will be reloaded during the next Commit.
66  ///
67  HDST_API
68  void MarkTextureFilePathDirty(const TfToken &filePath);
69 
70  /// Mark that the GPU resource for a texture needs to be
71  /// (re-)loaded, e.g., because the memory request changed.
72  ///
73  HDST_API
75  HdStTextureObjectPtr const &textureObject);
76 
77  /// Get resource registry
78  ///
79  HDST_API
81  return _resourceRegistry;
82  }
83 
84  /// The total GPU memory consumed by all textures managed by this registry.
85  ///
86  int64_t GetTotalTextureMemory() const {
87  return _totalTextureMemory;
88  }
89 
90  /// Add signed number to total texture memory amount. Called from
91  /// texture objects when (de-)allocated GPU resources.
92  ///
93  HDST_API
94  void AdjustTotalTextureMemory(int64_t memDiff);
95 
96  /// The number of texture objects.
97  size_t GetNumberOfTextureObjects() const {
98  return _textureObjectRegistry.size();
99  }
100 
101 private:
102  HdStTextureObjectSharedPtr _MakeTextureObject(
103  const HdStTextureIdentifier &textureId,
104  HdStTextureType textureType);
105 
106  std::atomic<int64_t> _totalTextureMemory;
107 
108  // Registry for texture and sampler objects.
110  _textureObjectRegistry;
111 
112  // Map file paths to texture objects for quick invalidation
113  // by file path.
114  std::unordered_map<TfToken, HdStTextureObjectPtrVector,
116  _filePathToTextureObjects;
117 
118  // File paths for which GPU resources need to be (re-)loaded
119  tbb::concurrent_vector<TfToken> _dirtyFilePaths;
120 
121  // Texture for which GPU resources need to be (re-)loaded
122  tbb::concurrent_vector<HdStTextureObjectPtr> _dirtyTextures;
123 
124  HdStResourceRegistry *_resourceRegistry;
125 };
126 
128 
129 #endif
std::vector< HdStTextureObjectPtr > HdStTextureObjectPtrVector
HDST_API void MarkTextureObjectDirty(HdStTextureObjectPtr const &textureObject)
size_t GetNumberOfTextureObjects() const
The number of texture objects.
HDST_API void GarbageCollect()
Functor to use for hash maps from tokens to other things.
Definition: token.h:149
Definition: token.h:70
HDST_API HdSt_TextureObjectRegistry(HdStResourceRegistry *registry)
int64_t GetTotalTextureMemory() const
HDST_API void MarkTextureFilePathDirty(const TfToken &filePath)
size_t size() const
std::shared_ptr< class HdStTextureObject > HdStTextureObjectSharedPtr
PXR_NAMESPACE_CLOSE_SCOPE PXR_NAMESPACE_OPEN_SCOPE
Definition: path.h:1425
HDST_API HdStResourceRegistry * GetResourceRegistry() const
#define HDST_API
Definition: api.h:23
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:74
HdStTextureType
Definition: enums.h:30
HDST_API std::set< HdStTextureObjectSharedPtr > Commit()
HDST_API void AdjustTotalTextureMemory(int64_t memDiff)
HDST_API ~HdSt_TextureObjectRegistry()
std::weak_ptr< class HdStTextureObject > HdStTextureObjectPtr
HDST_API HdStTextureObjectSharedPtr AllocateTextureObject(const HdStTextureIdentifier &textureId, HdStTextureType textureType)