Skip to content

Commit 3960962

Browse files
authored
Add missing UniqueHandleTraits for handles without destroy function. (#2500)
1 parent 249794f commit 3960962

File tree

6 files changed

+153
-30
lines changed

6 files changed

+153
-30
lines changed

VulkanHppGenerator.cpp

Lines changed: 36 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -7356,17 +7356,27 @@ std::string VulkanHppGenerator::generateObjectDeleter( std::string const & comma
73567356
throw std::runtime_error( "Found " + commandName + " which requires special handling for the object deleter" );
73577357
}
73587358
}
7359-
else if ( commandName.find( "Allocate" ) != std::string::npos )
7360-
{
7361-
objectDeleter = "detail::ObjectFree";
7362-
allocator = "allocator, ";
7363-
}
73647359
else
73657360
{
7366-
assert( ( commandName.find( "Create" ) != std::string::npos ) || ( commandName.find( "Register" ) != std::string::npos ) );
7367-
objectDeleter = "detail::ObjectDestroy";
7368-
allocator = "allocator, ";
7361+
auto handleIt = m_handles.find( commandData.params[returnParam].type.type );
7362+
assert( handleIt != m_handles.end() );
7363+
7364+
if ( handleIt->second.deleteCommand.empty() )
7365+
{
7366+
objectDeleter = "detail::DummyDestroy";
7367+
}
7368+
else if ( commandName.find( "Allocate" ) != std::string::npos )
7369+
{
7370+
objectDeleter = "detail::ObjectFree";
7371+
}
7372+
else
7373+
{
7374+
assert( ( commandName.find( "Create" ) != std::string::npos ) || ( commandName.find( "Register" ) != std::string::npos ) );
7375+
objectDeleter = "detail::ObjectDestroy";
7376+
}
7377+
allocator = "allocator, ";
73697378
}
7379+
73707380
std::string className = initialSkipCount ? stripPrefix( commandData.params[initialSkipCount - 1].type.type, "Vk" ) : "";
73717381
std::string parentName = ( className.empty() || ( commandData.params[returnParam].type.type == "VkDevice" ) ) ? "detail::NoParent" : className;
73727382
return objectDeleter + "<" + parentName + ", Dispatch>( " + ( ( parentName == "detail::NoParent" ) ? "" : "*this, " ) + allocator + "d )";
@@ -11793,18 +11803,16 @@ std::string VulkanHppGenerator::generateUnion( std::pair<std::string, StructData
1179311803

1179411804
std::string VulkanHppGenerator::generateUniqueHandle( std::pair<std::string, HandleData> const & handleData ) const
1179511805
{
11796-
if ( !handleData.second.deleteCommand.empty() )
11806+
std::string type = stripPrefix( handleData.first, "Vk" );
11807+
std::string aliasHandle;
11808+
for ( auto const & alias : handleData.second.aliases )
1179711809
{
11798-
std::string type = stripPrefix( handleData.first, "Vk" );
11799-
std::string aliasHandle;
11800-
for ( auto const & alias : handleData.second.aliases )
11801-
{
11802-
static std::string const aliasHandleTemplate = R"( using Unique${aliasType} = UniqueHandle<${type}, VULKAN_HPP_DEFAULT_DISPATCHER_TYPE>;)";
11810+
static std::string const aliasHandleTemplate = R"( using Unique${aliasType} = UniqueHandle<${type}, VULKAN_HPP_DEFAULT_DISPATCHER_TYPE>;)";
1180311811

11804-
aliasHandle += replaceWithMap( aliasHandleTemplate, { { "aliasType", stripPrefix( alias.first, "Vk" ) }, { "type", type } } );
11805-
}
11812+
aliasHandle += replaceWithMap( aliasHandleTemplate, { { "aliasType", stripPrefix( alias.first, "Vk" ) }, { "type", type } } );
11813+
}
1180611814

11807-
static std::string const uniqueHandleTemplate = R"( template <typename Dispatch>
11815+
static std::string const uniqueHandleTemplate = R"( template <typename Dispatch>
1180811816
class UniqueHandleTraits<${type}, Dispatch>
1180911817
{
1181011818
public:
@@ -11813,16 +11821,17 @@ std::string VulkanHppGenerator::generateUniqueHandle( std::pair<std::string, Han
1181311821
using Unique${type} = UniqueHandle<${type}, VULKAN_HPP_DEFAULT_DISPATCHER_TYPE>;
1181411822
${aliasHandle})";
1181511823

11816-
return replaceWithMap(
11817-
uniqueHandleTemplate,
11818-
{ { "aliasHandle", aliasHandle },
11819-
{ "deleterAction", ( handleData.second.deleteCommand.substr( 2, 4 ) == "Free" ) ? "Free" : "Destroy" },
11820-
{ "deleterParent", handleData.second.destructorType.empty() ? "detail::NoParent" : stripPrefix( handleData.second.destructorType, "Vk" ) },
11821-
{ "deleterPool", handleData.second.deletePool.empty() ? "" : ", " + stripPrefix( handleData.second.deletePool, "Vk" ) },
11822-
{ "deleterType", handleData.second.deletePool.empty() ? "Object" : "Pool" },
11823-
{ "type", type } } );
11824-
}
11825-
return "";
11824+
assert( !handleData.second.constructorIts.empty() );
11825+
std::string deleterParent = ( handleData.first != "VkDevice" ) ? handleData.second.constructorIts.front()->second.handle : "";
11826+
11827+
return replaceWithMap(
11828+
uniqueHandleTemplate,
11829+
{ { "aliasHandle", aliasHandle },
11830+
{ "deleterAction", ( !handleData.second.deleteCommand.empty() && ( handleData.second.deleteCommand.substr( 2, 4 ) == "Free" ) ) ? "Free" : "Destroy" },
11831+
{ "deleterParent", deleterParent.empty() ? "detail::NoParent" : stripPrefix( deleterParent, "Vk" ) },
11832+
{ "deleterPool", handleData.second.deletePool.empty() ? "" : ", " + stripPrefix( handleData.second.deletePool, "Vk" ) },
11833+
{ "deleterType", handleData.second.deleteCommand.empty() ? "Dummy" : ( handleData.second.deletePool.empty() ? "Object" : "Pool" ) },
11834+
{ "type", type } } );
1182611835
}
1182711836

1182811837
std::string VulkanHppGenerator::generateUniqueHandle( std::vector<RequireData> const & requireData,

snippets/ObjectDestroy.hpp

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,3 +79,45 @@ class ObjectDestroy<NoParent, Dispatch>
7979
Optional<const AllocationCallbacks> m_allocationCallbacks = nullptr;
8080
Dispatch const * m_dispatch = nullptr;
8181
};
82+
83+
template <typename OwnerType, typename Dispatch>
84+
class DummyDestroy
85+
{
86+
public:
87+
DummyDestroy() = default;
88+
89+
DummyDestroy( OwnerType owner,
90+
Optional<const AllocationCallbacks> allocationCallbacks VULKAN_HPP_DEFAULT_ASSIGNMENT( nullptr ),
91+
Dispatch const & dispatch VULKAN_HPP_DEFAULT_DISPATCHER_ASSIGNMENT ) VULKAN_HPP_NOEXCEPT
92+
: m_owner( owner )
93+
, m_allocationCallbacks( allocationCallbacks )
94+
, m_dispatch( &dispatch )
95+
{
96+
}
97+
98+
OwnerType getOwner() const VULKAN_HPP_NOEXCEPT
99+
{
100+
return m_owner;
101+
}
102+
103+
Optional<const AllocationCallbacks> getAllocator() const VULKAN_HPP_NOEXCEPT
104+
{
105+
return m_allocationCallbacks;
106+
}
107+
108+
Dispatch const & getDispatch() const VULKAN_HPP_NOEXCEPT
109+
{
110+
return *m_dispatch;
111+
}
112+
113+
protected:
114+
template <typename T>
115+
void destroy( T /*t*/ ) VULKAN_HPP_NOEXCEPT
116+
{
117+
}
118+
119+
private:
120+
OwnerType m_owner = {};
121+
Optional<const AllocationCallbacks> m_allocationCallbacks = nullptr;
122+
Dispatch const * m_dispatch = nullptr;
123+
};

tests/UniqueHandleDefaultArguments/UniqueHandleDefaultArguments.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,21 @@
1616

1717
// Should be used on 64 bit only, as on 32 bit the test is ambiguous.
1818

19-
2019
#ifdef VULKAN_HPP_USE_CXX_MODULE
2120
# include <vulkan/vulkan.h>
2221
import vulkan;
2322
#else
2423
# include <vulkan/vulkan.hpp>
25-
VULKAN_HPP_DEFAULT_DISPATCH_LOADER_DYNAMIC_STORAGE
24+
VULKAN_HPP_DEFAULT_DISPATCH_LOADER_DYNAMIC_STORAGE
2625
#endif
2726

2827
int main( int /*argc*/, char ** /*argv*/ )
2928
{
3029
VkSurfaceKHR surface = 0;
3130
auto uniqueSurface = vk::UniqueSurfaceKHR( static_cast<vk::SurfaceKHR>( surface ), vk::Instance() );
31+
32+
vk::PhysicalDevice phys = {};
33+
auto uniqueDisplayMode = phys.createDisplayModeKHRUnique( {}, {} );
34+
3235
return 0;
3336
}

vulkan/vulkan.hpp

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7916,6 +7916,48 @@ VULKAN_HPP_EXPORT namespace VULKAN_HPP_NAMESPACE
79167916
Dispatch const * m_dispatch = nullptr;
79177917
};
79187918

7919+
template <typename OwnerType, typename Dispatch>
7920+
class DummyDestroy
7921+
{
7922+
public:
7923+
DummyDestroy() = default;
7924+
7925+
DummyDestroy( OwnerType owner,
7926+
Optional<AllocationCallbacks const> allocationCallbacks VULKAN_HPP_DEFAULT_ASSIGNMENT( nullptr ),
7927+
Dispatch const & dispatch VULKAN_HPP_DEFAULT_DISPATCHER_ASSIGNMENT ) VULKAN_HPP_NOEXCEPT
7928+
: m_owner( owner )
7929+
, m_allocationCallbacks( allocationCallbacks )
7930+
, m_dispatch( &dispatch )
7931+
{
7932+
}
7933+
7934+
OwnerType getOwner() const VULKAN_HPP_NOEXCEPT
7935+
{
7936+
return m_owner;
7937+
}
7938+
7939+
Optional<AllocationCallbacks const> getAllocator() const VULKAN_HPP_NOEXCEPT
7940+
{
7941+
return m_allocationCallbacks;
7942+
}
7943+
7944+
Dispatch const & getDispatch() const VULKAN_HPP_NOEXCEPT
7945+
{
7946+
return *m_dispatch;
7947+
}
7948+
7949+
protected:
7950+
template <typename T>
7951+
void destroy( T /*t*/ ) VULKAN_HPP_NOEXCEPT
7952+
{
7953+
}
7954+
7955+
private:
7956+
OwnerType m_owner = {};
7957+
Optional<AllocationCallbacks const> m_allocationCallbacks = nullptr;
7958+
Dispatch const * m_dispatch = nullptr;
7959+
};
7960+
79197961
template <typename OwnerType, typename Dispatch>
79207962
class ObjectFree
79217963
{

vulkan/vulkan_funcs.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10058,7 +10058,7 @@ VULKAN_HPP_EXPORT namespace VULKAN_HPP_NAMESPACE
1005810058
detail::resultCheck( result, VULKAN_HPP_NAMESPACE_STRING "::PhysicalDevice::createDisplayModeKHRUnique" );
1005910059

1006010060
return detail::createResultValueType(
10061-
result, UniqueHandle<DisplayModeKHR, Dispatch>( mode, detail::ObjectDestroy<PhysicalDevice, Dispatch>( *this, allocator, d ) ) );
10061+
result, UniqueHandle<DisplayModeKHR, Dispatch>( mode, detail::DummyDestroy<PhysicalDevice, Dispatch>( *this, allocator, d ) ) );
1006210062
}
1006310063
# endif /* VULKAN_HPP_NO_SMART_HANDLE */
1006410064
#endif /* VULKAN_HPP_DISABLE_ENHANCED_MODE */

vulkan/vulkan_handles.hpp

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2479,6 +2479,15 @@ VULKAN_HPP_EXPORT namespace VULKAN_HPP_NAMESPACE
24792479

24802480
using UniqueInstance = UniqueHandle<Instance, VULKAN_HPP_DEFAULT_DISPATCHER_TYPE>;
24812481

2482+
template <typename Dispatch>
2483+
class UniqueHandleTraits<PhysicalDevice, Dispatch>
2484+
{
2485+
public:
2486+
using deleter = detail::DummyDestroy<Instance, Dispatch>;
2487+
};
2488+
2489+
using UniquePhysicalDevice = UniqueHandle<PhysicalDevice, VULKAN_HPP_DEFAULT_DISPATCHER_TYPE>;
2490+
24822491
template <typename Dispatch>
24832492
class UniqueHandleTraits<Device, Dispatch>
24842493
{
@@ -2488,6 +2497,15 @@ VULKAN_HPP_EXPORT namespace VULKAN_HPP_NAMESPACE
24882497

24892498
using UniqueDevice = UniqueHandle<Device, VULKAN_HPP_DEFAULT_DISPATCHER_TYPE>;
24902499

2500+
template <typename Dispatch>
2501+
class UniqueHandleTraits<Queue, Dispatch>
2502+
{
2503+
public:
2504+
using deleter = detail::DummyDestroy<Device, Dispatch>;
2505+
};
2506+
2507+
using UniqueQueue = UniqueHandle<Queue, VULKAN_HPP_DEFAULT_DISPATCHER_TYPE>;
2508+
24912509
template <typename Dispatch>
24922510
class UniqueHandleTraits<DeviceMemory, Dispatch>
24932511
{
@@ -2739,6 +2757,15 @@ VULKAN_HPP_EXPORT namespace VULKAN_HPP_NAMESPACE
27392757

27402758
using UniqueDisplayKHR = UniqueHandle<DisplayKHR, VULKAN_HPP_DEFAULT_DISPATCHER_TYPE>;
27412759

2760+
template <typename Dispatch>
2761+
class UniqueHandleTraits<DisplayModeKHR, Dispatch>
2762+
{
2763+
public:
2764+
using deleter = detail::DummyDestroy<PhysicalDevice, Dispatch>;
2765+
};
2766+
2767+
using UniqueDisplayModeKHR = UniqueHandle<DisplayModeKHR, VULKAN_HPP_DEFAULT_DISPATCHER_TYPE>;
2768+
27422769
//=== VK_EXT_debug_report ===
27432770
template <typename Dispatch>
27442771
class UniqueHandleTraits<DebugReportCallbackEXT, Dispatch>

0 commit comments

Comments
 (0)