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 Apache License, Version 2.0 (the "Apache License")
5 // with the following modification; you may not use this file except in
6 // compliance with the Apache License and the following modification to it:
7 // Section 6. Trademarks. is deleted and replaced with:
8 //
9 // 6. Trademarks. This License does not grant permission to use the trade
10 // names, trademarks, service marks, or product names of the Licensor
11 // and its affiliates, except as required to comply with Section 4(c) of
12 // the License and to reproduce the content of the NOTICE file.
13 //
14 // You may obtain a copy of the Apache License at
15 //
16 // http://www.apache.org/licenses/LICENSE-2.0
17 //
18 // Unless required by applicable law or agreed to in writing, software
19 // distributed under the Apache License with the above modification is
20 // distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
21 // KIND, either express or implied. See the Apache License for the specific
22 // language governing permissions and limitations under the Apache License.
23 //
24 #ifndef PXR_IMAGING_HD_ST_TEXTURE_OBJECT_REGISTRY_H
25 #define PXR_IMAGING_HD_ST_TEXTURE_OBJECT_REGISTRY_H
26 
27 #include "pxr/pxr.h"
28 #include "pxr/imaging/hdSt/api.h"
29 #include "pxr/imaging/hd/enums.h"
31 
32 #include <tbb/concurrent_vector.h>
33 #include <vector>
34 #include <atomic>
35 
37 
39  std::shared_ptr<class HdStTextureObject>;
41  std::weak_ptr<class HdStTextureObject>;
43  std::vector<HdStTextureObjectPtr>;
46 
47 
48 /// \class HdSt_TextureObjectRegistry
49 ///
50 /// A central registry for texture GPU resources.
51 ///
53 {
54 public:
57 
58  /// Allocate texture.
59  ///
60  /// This just creates the HdStTextureObject, the actual GPU
61  /// resources won't be allocated until the Commit phase.
62  ///
63  HDST_API
65  const HdStTextureIdentifier &textureId,
66  HdTextureType textureType);
67 
68  /// Create GPU texture objects, load textures from files and
69  /// upload to GPU.
70  ///
71  HDST_API
72  std::set<HdStTextureObjectSharedPtr> Commit();
73 
74  /// Free GPU resources of textures not used by any client.
75  ///
76  HDST_API
77  void GarbageCollect();
78 
79  /// Mark texture file path as dirty. All textures whose identifier
80  /// contains the file path will be reloaded during the next Commit.
81  ///
82  HDST_API
83  void MarkTextureFilePathDirty(const TfToken &filePath);
84 
85  /// Mark that the GPU resource for a texture needs to be
86  /// (re-)loaded, e.g., because the memory request changed.
87  ///
88  HDST_API
90  HdStTextureObjectPtr const &textureObject);
91 
92  /// Get resource registry
93  ///
94  HDST_API
96  return _resourceRegistry;
97  }
98 
99  /// The total GPU memory consumed by all textures managed by this registry.
100  ///
101  int64_t GetTotalTextureMemory() const {
102  return _totalTextureMemory;
103  }
104 
105  /// Add signed number to total texture memory amount. Called from
106  /// texture objects when (de-)allocated GPU resources.
107  ///
108  HDST_API
109  void AdjustTotalTextureMemory(int64_t memDiff);
110 
111  /// The number of texture objects.
112  size_t GetNumberOfTextureObjects() const {
113  return _textureObjectRegistry.size();
114  }
115 
116 private:
117  HdStTextureObjectSharedPtr _MakeTextureObject(
118  const HdStTextureIdentifier &textureId,
119  HdTextureType textureType);
120 
121  std::atomic<int64_t> _totalTextureMemory;
122 
123  // Registry for texture and sampler objects.
125  _textureObjectRegistry;
126 
127  // Map file paths to texture objects for quick invalidation
128  // by file path.
129  std::unordered_map<TfToken, HdStTextureObjectPtrVector,
131  _filePathToTextureObjects;
132 
133  // File paths for which GPU resources need to be (re-)loaded
134  tbb::concurrent_vector<TfToken> _dirtyFilePaths;
135 
136  // Texture for which GPU resources need to be (re-)loaded
137  tbb::concurrent_vector<HdStTextureObjectPtr> _dirtyTextures;
138 
139  HdStResourceRegistry *_resourceRegistry;
140 };
141 
143 
144 #endif
std::vector< HdStTextureObjectPtr > HdStTextureObjectPtrVector
HDST_API void MarkTextureObjectDirty(HdStTextureObjectPtr const &textureObject)
HDST_API HdStTextureObjectSharedPtr AllocateTextureObject(const HdStTextureIdentifier &textureId, HdTextureType textureType)
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:166
Definition: token.h:87
HdTextureType
Definition: enums.h:221
HDST_API void MarkTextureFilePathDirty(const TfToken &filePath)
size_t size() const
std::shared_ptr< class HdStTextureObject > HdStTextureObjectSharedPtr
HdSt_TextureObjectRegistry(HdStResourceRegistry *registry)
PXR_NAMESPACE_CLOSE_SCOPE PXR_NAMESPACE_OPEN_SCOPE
Definition: path.h:1432
HDST_API HdStResourceRegistry * GetResourceRegistry() const
#define HDST_API
Definition: api.h:40
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:91
HDST_API std::set< HdStTextureObjectSharedPtr > Commit()
HDST_API void AdjustTotalTextureMemory(int64_t memDiff)
std::weak_ptr< class HdStTextureObject > HdStTextureObjectPtr