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" 30 #include "pxr/imaging/hd/instanceRegistry.h" 31 32 #include <tbb/concurrent_vector.h> 33 #include <vector> 34 #include <atomic> 35 36 PXR_NAMESPACE_OPEN_SCOPE 37 38 using HdStTextureObjectSharedPtr = 39 std::shared_ptr<class HdStTextureObject>; 40 using HdStTextureObjectPtr = 41 std::weak_ptr<class HdStTextureObject>; 42 using HdStTextureObjectPtrVector = 43 std::vector<HdStTextureObjectPtr>; 44 class HdStResourceRegistry; 45 class HdStTextureIdentifier; 46 47 48 /// \class HdSt_TextureObjectRegistry 49 /// 50 /// A central registry for texture GPU resources. 51 /// 52 class HdSt_TextureObjectRegistry final 53 { 54 public: 55 explicit HdSt_TextureObjectRegistry(HdStResourceRegistry * registry); 56 ~HdSt_TextureObjectRegistry(); 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 64 HdStTextureObjectSharedPtr AllocateTextureObject( 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 89 void MarkTextureObjectDirty( 90 HdStTextureObjectPtr const &textureObject); 91 92 /// Get resource registry 93 /// 94 HDST_API GetResourceRegistry()95 HdStResourceRegistry * GetResourceRegistry() const { 96 return _resourceRegistry; 97 } 98 99 /// The total GPU memory consumed by all textures managed by this registry. 100 /// GetTotalTextureMemory()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. GetNumberOfTextureObjects()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. 124 HdInstanceRegistry<HdStTextureObjectSharedPtr> 125 _textureObjectRegistry; 126 127 // Map file paths to texture objects for quick invalidation 128 // by file path. 129 std::unordered_map<TfToken, HdStTextureObjectPtrVector, 130 TfToken::HashFunctor> 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 142 PXR_NAMESPACE_CLOSE_SCOPE 143 144 #endif 145