How Are Shared GraphicsPipelineConfigurators and DescriptorSets Reused in vsgXchange? #1550
-
Hi, I've noticed that when loading models in vsgXchange, both GraphicsPipelineConfigurator and DescriptorSet objects are shared via Any clarification on how these shared objects are reused would be greatly appreciated! |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
The beauty of open source is the answer is right there in the code that we can share and review, in this instance the star of the show is the SharedObjects::share(ref_ptr& object)https://github.com/vsg-dev/VulkanSceneGraph/blob/master/include/vsg/utils/SharedObjects.h#L154 method that leverages a std::map/set combo in SharedObjects, along with vsg::Object::compare(): The ShaderObjects members that store the shared objects: mutable std::recursive_mutex _mutex;
std::map<std::type_index, ref_ptr<Object>> _defaults;
std::map<std::type_index, std::set<ref_ptr<Object>, DereferenceLess>> _sharedObjects; The SharedObjects::share(ref_ptr& object) implementation: template<class T>
void SharedObjects::share(ref_ptr<T>& object)
{
std::scoped_lock<std::recursive_mutex> lock(_mutex);
if (suitableForSharing && !suitableForSharing->suitable(object.get())) return;
auto id = std::type_index(typeid(T));
auto& shared_objects = _sharedObjects[id];
if (auto itr = shared_objects.find(object); itr != shared_objects.end())
{
object = ref_ptr<T>(static_cast<T*>(itr->get()));
return;
}
shared_objects.insert(object);
} And the DereferenceLess functor that provides the comparison test:
The vsg::Object subclasses then implement the compare(..) method to enable duplicates to be found. You'll find compare(..) implementations throughout the VSG. |
Beta Was this translation helpful? Give feedback.
-
Thanks for the reply. I understand. |
Beta Was this translation helpful? Give feedback.
The beauty of open source is the answer is right there in the code that we can share and review, in this instance the star of the show is the SharedObjects::share(ref_ptr& object)https://github.com/vsg-dev/VulkanSceneGraph/blob/master/include/vsg/utils/SharedObjects.h#L154 method that leverages a std::map/set combo in SharedObjects, along with vsg::Object::compare():
The ShaderObjects members that store the shared objects:
mutable std::recursive_mutex _mutex; std::map<std::type_index, ref_ptr<Object>> _defaults; std::map<std::type_index, std::set<ref_ptr<Object>, DereferenceLess>> _sharedObjects;
The SharedObjects::share(ref_ptr& object) implementation: