2016-02-26 13:39:01 +11:00
|
|
|
/*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*
|
|
|
|
* Copyright:
|
2017-09-01 12:33:41 +10:00
|
|
|
* 2016-2017, Danny Robson <danny@nerdcruft.net>
|
2016-02-26 13:39:01 +11:00
|
|
|
*/
|
|
|
|
|
2017-05-26 16:30:48 +10:00
|
|
|
#ifndef CRUFT_VK_OBJECT_HPP
|
|
|
|
#define CRUFT_VK_OBJECT_HPP
|
2016-02-24 11:11:41 +11:00
|
|
|
|
|
|
|
#include "./traits.hpp"
|
|
|
|
#include "./except.hpp"
|
|
|
|
|
|
|
|
#include <utility>
|
2017-05-26 16:32:31 +10:00
|
|
|
#include <vector>
|
2016-02-24 11:11:41 +11:00
|
|
|
|
2017-05-26 16:30:48 +10:00
|
|
|
namespace cruft::vk {
|
2017-09-01 12:33:41 +10:00
|
|
|
/// the base class for all non-trivial vulkan objects requiring lifetime
|
|
|
|
/// management.
|
2017-09-08 17:58:13 +10:00
|
|
|
template <typename SelfT>
|
2016-02-24 11:11:41 +11:00
|
|
|
struct object {
|
2017-09-10 13:54:27 +10:00
|
|
|
using native_type = native_t<SelfT>;
|
|
|
|
|
2017-09-13 16:51:17 +10:00
|
|
|
object (const native_t<SelfT> &_native):
|
2017-09-08 17:35:24 +10:00
|
|
|
m_native (_native)
|
|
|
|
{
|
2018-05-10 12:58:15 +10:00
|
|
|
CHECK_NEQ (m_native, static_cast<native_type> (VK_NULL_HANDLE));
|
2017-09-08 17:35:24 +10:00
|
|
|
}
|
2016-02-24 11:11:41 +11:00
|
|
|
|
2017-09-08 17:35:24 +10:00
|
|
|
object (object &&rhs):
|
|
|
|
m_native (VK_NULL_HANDLE)
|
|
|
|
{ std::swap (m_native, rhs.m_native); }
|
2017-09-08 17:11:35 +10:00
|
|
|
|
2017-05-26 16:32:31 +10:00
|
|
|
object (const object&) = delete;
|
2016-02-24 11:11:41 +11:00
|
|
|
|
2017-09-08 17:35:24 +10:00
|
|
|
const auto& native (void) const& { return m_native; }
|
|
|
|
auto& native (void)& { return m_native; }
|
2016-02-24 11:11:41 +11:00
|
|
|
|
2017-09-07 15:52:36 +10:00
|
|
|
protected:
|
2017-09-08 17:58:13 +10:00
|
|
|
native_t<SelfT> m_native;
|
2016-02-24 11:11:41 +11:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2017-09-08 18:10:57 +10:00
|
|
|
///////////////////////////////////////////////////////////////////////////
|
2017-09-05 17:20:17 +10:00
|
|
|
template <typename SelfT>
|
|
|
|
struct root : public object<SelfT> {
|
|
|
|
template <typename ...Args>
|
|
|
|
root (Args &&...args):
|
2017-09-08 17:35:24 +10:00
|
|
|
object<SelfT> (
|
|
|
|
cruft::vk::error::try_query (
|
2017-09-13 16:50:23 +10:00
|
|
|
wrapper_traits<SelfT>::create,
|
2017-09-08 17:35:24 +10:00
|
|
|
std::forward<Args> (args)...
|
|
|
|
)
|
|
|
|
)
|
2017-09-05 17:20:17 +10:00
|
|
|
{ ; }
|
|
|
|
|
|
|
|
~root ()
|
|
|
|
{
|
2017-09-13 16:50:23 +10:00
|
|
|
wrapper_traits<SelfT>::destroy (this->native (), nullptr);
|
2017-09-05 17:20:17 +10:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2017-09-08 18:10:57 +10:00
|
|
|
///////////////////////////////////////////////////////////////////////////
|
2017-09-01 12:33:41 +10:00
|
|
|
/// a vulkan object that must be directly instantiated through
|
|
|
|
/// constructor arguments, rather than being enumerated and owned by
|
|
|
|
/// another object.
|
2017-09-08 17:58:13 +10:00
|
|
|
template <typename SelfT>
|
|
|
|
struct descendant : public object<SelfT> {
|
2017-09-08 17:35:24 +10:00
|
|
|
template <typename ParentT, typename ...Args>
|
|
|
|
descendant (ParentT &parent, Args &&...args):
|
2017-09-08 17:58:13 +10:00
|
|
|
object<SelfT> (
|
2017-09-08 17:35:24 +10:00
|
|
|
cruft::vk::error::try_query(
|
2017-09-13 16:50:23 +10:00
|
|
|
wrapper_traits<SelfT>::create,
|
2017-09-08 17:35:24 +10:00
|
|
|
parent.native (),
|
|
|
|
std::forward<Args> (args)...
|
|
|
|
)
|
|
|
|
)
|
2016-02-24 11:11:41 +11:00
|
|
|
{ ; }
|
|
|
|
|
2017-09-08 17:35:24 +10:00
|
|
|
|
2017-09-08 18:03:10 +10:00
|
|
|
//---------------------------------------------------------------------
|
2017-09-05 17:20:17 +10:00
|
|
|
~descendant ()
|
2016-02-24 11:11:41 +11:00
|
|
|
{
|
2017-09-13 16:50:23 +10:00
|
|
|
wrapper_traits<SelfT>::destroy (this->native (), nullptr);
|
2016-02-24 11:11:41 +11:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-09-01 12:33:41 +10:00
|
|
|
|
2017-09-08 18:10:57 +10:00
|
|
|
///////////////////////////////////////////////////////////////////////////
|
2017-09-01 12:33:41 +10:00
|
|
|
/// a vulkan object that is obtained by listings from a parent object.
|
2017-09-09 15:34:06 +10:00
|
|
|
template <typename SelfT, typename ParentT>
|
2017-09-08 17:58:13 +10:00
|
|
|
struct enumerated : public object<SelfT> {
|
|
|
|
using object<SelfT>::object;
|
2017-05-26 16:32:31 +10:00
|
|
|
|
2017-09-08 17:58:13 +10:00
|
|
|
static std::vector<SelfT>
|
2017-09-09 15:34:06 +10:00
|
|
|
find (const ParentT &parent)
|
|
|
|
{
|
|
|
|
return error::try_handles<SelfT> (
|
|
|
|
enum_traits<native_t<SelfT>>::enumerate, parent.native ()
|
|
|
|
);
|
|
|
|
}
|
2017-05-26 16:32:31 +10:00
|
|
|
};
|
2016-02-24 11:11:41 +11:00
|
|
|
|
2017-09-01 12:33:41 +10:00
|
|
|
|
2017-09-08 18:10:57 +10:00
|
|
|
///////////////////////////////////////////////////////////////////////////
|
2017-09-01 12:33:41 +10:00
|
|
|
/// a vulkan object that is directly owned by a parent object.
|
|
|
|
///
|
|
|
|
/// typically implies that this object must be freed using a reference to
|
|
|
|
/// the parent object.
|
2017-09-05 17:20:17 +10:00
|
|
|
template <typename SelfT, typename OwnerT>
|
|
|
|
struct owned : public object<SelfT> {
|
2017-09-07 15:52:36 +10:00
|
|
|
using owner_t = OwnerT;
|
|
|
|
|
2017-09-05 17:20:17 +10:00
|
|
|
using object<SelfT>::object;
|
|
|
|
|
2017-09-08 17:11:35 +10:00
|
|
|
///////////////////////////////////////////////////////////////////////
|
2017-09-05 17:20:17 +10:00
|
|
|
template <typename ...Args>
|
2017-09-08 17:11:35 +10:00
|
|
|
owned (OwnerT &owner, Args &&...args):
|
|
|
|
object<SelfT> (
|
|
|
|
error::try_query (
|
2017-09-13 16:50:23 +10:00
|
|
|
wrapper_traits<SelfT>::create,
|
2017-09-08 17:11:35 +10:00
|
|
|
owner.native (),
|
|
|
|
std::forward<Args> (args)...
|
|
|
|
)
|
|
|
|
)
|
2017-09-05 17:20:17 +10:00
|
|
|
{ ; }
|
|
|
|
|
2017-09-07 15:52:36 +10:00
|
|
|
|
2017-09-08 17:11:35 +10:00
|
|
|
//---------------------------------------------------------------------
|
|
|
|
owned (owned &&rhs):
|
|
|
|
object<SelfT> (std::move (rhs))
|
|
|
|
{ ; }
|
|
|
|
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------
|
2017-09-07 15:52:36 +10:00
|
|
|
~owned ()
|
|
|
|
{
|
2018-05-10 12:58:15 +10:00
|
|
|
CHECK_EQ (this->native (), static_cast<typename SelfT::native_type> (VK_NULL_HANDLE));
|
2017-09-07 15:52:36 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-09-08 17:11:35 +10:00
|
|
|
//---------------------------------------------------------------------
|
2017-09-07 15:52:36 +10:00
|
|
|
void
|
2017-09-08 17:11:35 +10:00
|
|
|
destroy (OwnerT &owner)
|
2017-09-07 15:52:36 +10:00
|
|
|
{
|
2017-09-13 16:50:23 +10:00
|
|
|
wrapper_traits<SelfT>::destroy (owner.native (), this->native (), nullptr);
|
2017-09-08 17:11:35 +10:00
|
|
|
this->m_native = VK_NULL_HANDLE;
|
2017-09-07 15:52:36 +10:00
|
|
|
}
|
2017-09-08 17:11:35 +10:00
|
|
|
};
|
2017-09-07 15:52:36 +10:00
|
|
|
|
|
|
|
|
2017-09-08 18:10:57 +10:00
|
|
|
///////////////////////////////////////////////////////////////////////////
|
2017-09-13 16:51:52 +10:00
|
|
|
template <typename SelfT>
|
2017-09-10 13:54:27 +10:00
|
|
|
class owned_ptr {
|
|
|
|
public:
|
2017-09-13 16:51:52 +10:00
|
|
|
owned_ptr (SelfT &&_self, owner_t<SelfT> &_owner):
|
2017-09-10 13:54:27 +10:00
|
|
|
m_self (std::move (_self)),
|
|
|
|
m_owner (_owner)
|
|
|
|
{ ; }
|
|
|
|
|
|
|
|
owned_ptr (owned_ptr &&rhs):
|
|
|
|
m_self (std::move (rhs.m_self)),
|
|
|
|
m_owner (rhs.m_owner)
|
|
|
|
{ ; }
|
|
|
|
|
|
|
|
~owned_ptr ()
|
|
|
|
{
|
|
|
|
m_self.destroy (m_owner);
|
|
|
|
}
|
|
|
|
|
|
|
|
// it's unclear whether we want to work around reseating m_owner,
|
|
|
|
// or if this operation just isn't sufficiently prevalent to
|
|
|
|
// justify the work.
|
|
|
|
owned_ptr& operator= (owned_ptr &&rhs) = delete;
|
|
|
|
|
2017-09-13 16:51:52 +10:00
|
|
|
const SelfT& get (void) const& { return m_self; }
|
|
|
|
SelfT& get (void)& { return m_self; }
|
|
|
|
|
2017-09-14 01:39:38 +10:00
|
|
|
SelfT& operator* ()& noexcept
|
2017-09-10 13:54:27 +10:00
|
|
|
{ return m_self; }
|
|
|
|
|
2017-09-19 18:22:40 +10:00
|
|
|
const SelfT*
|
|
|
|
operator-> () const& noexcept
|
|
|
|
{ return &m_self; }
|
|
|
|
|
2017-09-13 16:51:52 +10:00
|
|
|
SelfT*
|
2017-09-14 01:39:38 +10:00
|
|
|
operator-> ()& noexcept
|
2017-09-10 13:54:27 +10:00
|
|
|
{ return &m_self; }
|
|
|
|
|
2017-09-14 01:39:38 +10:00
|
|
|
auto&
|
|
|
|
owner (void)& noexcept
|
|
|
|
{ return m_owner; }
|
|
|
|
|
|
|
|
const auto&
|
|
|
|
owner (void) const& noexcept
|
|
|
|
{ return m_owner; }
|
|
|
|
|
2017-09-10 13:54:27 +10:00
|
|
|
private:
|
2017-09-13 16:51:52 +10:00
|
|
|
SelfT m_self;
|
|
|
|
owner_t<SelfT> &m_owner;
|
2017-09-10 13:54:27 +10:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2017-09-13 16:52:16 +10:00
|
|
|
//-------------------------------------------------------------------------
|
|
|
|
template <typename SelfT, typename OwnerT>
|
|
|
|
auto
|
|
|
|
make_owned (native_t<SelfT> _native, OwnerT &_owner)
|
|
|
|
{
|
|
|
|
return owned_ptr<SelfT> {
|
|
|
|
SelfT (_native), _owner
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2017-09-10 13:54:27 +10:00
|
|
|
|
|
|
|
//-------------------------------------------------------------------------
|
2017-09-08 17:12:16 +10:00
|
|
|
template <typename SelfT, typename OwnerT, typename ...Args>
|
|
|
|
auto
|
|
|
|
make_owned (OwnerT &owner, Args &&...args)
|
|
|
|
{
|
2017-09-10 13:54:27 +10:00
|
|
|
return owned_ptr<SelfT> {
|
2017-09-08 17:12:16 +10:00
|
|
|
SelfT { owner, std::forward<Args> (args)... },
|
|
|
|
owner
|
|
|
|
};
|
2017-09-05 17:20:17 +10:00
|
|
|
};
|
2017-09-19 18:22:40 +10:00
|
|
|
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
template <typename ReturnT, typename ...Args>
|
|
|
|
struct function_wrapper {
|
|
|
|
using value_type = ReturnT(*)(Args...) noexcept;
|
|
|
|
|
|
|
|
constexpr function_wrapper (value_type _value): m_function {_value} { ; }
|
|
|
|
constexpr function_wrapper (): m_function { nullptr } { ; }
|
|
|
|
|
|
|
|
|
|
|
|
value_type
|
|
|
|
reset (value_type _value)
|
|
|
|
{
|
|
|
|
value_type old = std::move (m_function);
|
|
|
|
m_function = _value;
|
|
|
|
return old;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
template <typename SelfT, typename ...Tail>
|
|
|
|
auto
|
|
|
|
operator () (const owned_ptr<SelfT> &owned, Tail &&...tail) const
|
|
|
|
{
|
|
|
|
return dispatch (owned.owner (), owned.get (), std::forward<Tail> (tail)...);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
template <typename ...Tail>
|
|
|
|
auto operator () (Tail &&...tail) const
|
|
|
|
{
|
|
|
|
return dispatch (std::forward<Tail> (tail)...);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
template <typename ...Tail>
|
|
|
|
auto dispatch (Tail &&...tail) const
|
|
|
|
{
|
|
|
|
if constexpr (is_values_function_v<value_type>)
|
|
|
|
return error::try_values (m_function, std::forward<Tail> (tail)...);
|
|
|
|
else if constexpr (is_query_function_v<value_type>)
|
|
|
|
return error::try_query (m_function, std::forward<Tail> (tail)...);
|
|
|
|
else
|
|
|
|
return error::try_func (m_function, std::forward<Tail> (tail)...);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
value_type m_function;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
#define WRAP_FUNCTION(NAME,FUNC) \
|
|
|
|
namespace detail { const function_wrapper __##NAME {FUNC}; } \
|
|
|
|
template <typename ...Args> \
|
|
|
|
auto NAME (Args &&...args) \
|
|
|
|
{ \
|
|
|
|
return detail::__##NAME (std::forward<Args> (args)... ); \
|
|
|
|
};
|
2017-09-08 17:11:35 +10:00
|
|
|
};
|
2016-02-24 11:11:41 +11:00
|
|
|
|
|
|
|
#endif
|