From c3fc5377742de743a27eae0d7d76c4d2d77772e1 Mon Sep 17 00:00:00 2001 From: Danny Robson Date: Thu, 14 Sep 2017 01:39:38 +1000 Subject: [PATCH] device_memory: add simple map objects --- device_memory.cpp | 83 +++++++++++++++++++++++++++++++++++++++-------- device_memory.hpp | 41 ++++++++++++++++++++--- object.hpp | 12 +++++-- tools/hello.cpp | 49 +++++++++++++++------------- 4 files changed, 143 insertions(+), 42 deletions(-) diff --git a/device_memory.cpp b/device_memory.cpp index 7ad0741..26dd871 100644 --- a/device_memory.cpp +++ b/device_memory.cpp @@ -23,29 +23,86 @@ using cruft::vk::device_memory; /////////////////////////////////////////////////////////////////////////////// -void* -device_memory::map (const device &dev, - VkDeviceSize offset, - VkDeviceSize size, - VkMemoryMapFlags flags) +cruft::vk::map_t::~map_t () { - void *res; - - auto err = vkMapMemory (dev.native (), native (), offset, size, flags, &res); - error::try_code (err); - - return res; + CHECK_ZERO (cbegin ()); + CHECK_ZERO (cend ()); } //----------------------------------------------------------------------------- void -device_memory::unmap (const device &dev) +cruft::vk::map_t::destroy (device &_device, device_memory &_memory) { - vkUnmapMemory (dev.native (), native ()); + CHECK_NEZ (cbegin ()); + CHECK_NEZ (cend ()); + + vkUnmapMemory (_device.native (), _memory.native ()); + + *this = {nullptr, nullptr}; } + +/////////////////////////////////////////////////////////////////////////////// +cruft::vk::map_t +cruft::vk::map (device &_device, device_memory &_memory, int len) +{ + void *ptr = nullptr; + auto err = vkMapMemory (_device.native (), _memory.native (), 0, len, 0, &ptr); + error::try_code (err); + + return { + reinterpret_cast (ptr), + reinterpret_cast (ptr) + len + }; +} + + + +/////////////////////////////////////////////////////////////////////////////// +cruft::vk::owned_map_t::owned_map_t (owned_ptr &_memory, + std::byte *first, + std::byte *last): + map_t (first, last), + m_memory (_memory) +{ ; } + + +//----------------------------------------------------------------------------- +cruft::vk::owned_map_t::~owned_map_t () +{ + destroy (); +} + + +//----------------------------------------------------------------------------- +void +cruft::vk::owned_map_t::destroy () +{ + if (cbegin ()) + map_t::destroy (m_memory.owner (), *m_memory); +} + + + +/////////////////////////////////////////////////////////////////////////////// +cruft::vk::owned_map_t +cruft::vk::map (owned_ptr &_memory, int len) +{ + void *ptr = nullptr; + auto err = vkMapMemory (_memory.owner ().native (), _memory->native (), 0, len, 0, &ptr); + error::try_code (err); + + return owned_map_t { + _memory, + reinterpret_cast (ptr), + reinterpret_cast (ptr) + len + }; +}; + + + /////////////////////////////////////////////////////////////////////////////// VkDeviceSize device_memory::commitment (const device &dev) const diff --git a/device_memory.hpp b/device_memory.hpp index 0e29f35..90ade3d 100644 --- a/device_memory.hpp +++ b/device_memory.hpp @@ -21,15 +21,46 @@ #include "./object.hpp" #include "./fwd.hpp" +#include + +#include + + namespace cruft::vk { + /////////////////////////////////////////////////////////////////////////// + struct map_t : public util::view { + public: + using view::view; + + ~map_t (); + + void destroy (device&, device_memory&); + }; + + + //------------------------------------------------------------------------- + map_t map (device&, device_memory&, int len); + + + ////////////////////////////////////////////////////////////////////////// + struct owned_map_t : public map_t { + owned_map_t (owned_ptr&, std::byte *first, std::byte *last); + ~owned_map_t (); + + void destroy (void); + + private: + owned_ptr &m_memory; + }; + + + owned_map_t map (owned_ptr&, int len); + + + /////////////////////////////////////////////////////////////////////////// struct device_memory : public owned { using owned::owned; - void* map (const device&, - VkDeviceSize offset, - VkDeviceSize size, - VkMemoryMapFlags flags); - void unmap (const device&); VkDeviceSize commitment (const device&) const; diff --git a/object.hpp b/object.hpp index 805c27d..07e948b 100644 --- a/object.hpp +++ b/object.hpp @@ -187,13 +187,21 @@ namespace cruft::vk { const SelfT& get (void) const& { return m_self; } SelfT& get (void)& { return m_self; } - SelfT& operator* ()& + SelfT& operator* ()& noexcept { return m_self; } SelfT* - operator-> ()& + operator-> ()& noexcept { return &m_self; } + auto& + owner (void)& noexcept + { return m_owner; } + + const auto& + owner (void) const& noexcept + { return m_owner; } + private: SelfT m_self; owner_t &m_owner; diff --git a/tools/hello.cpp b/tools/hello.cpp index 622b822..f5fcbb2 100644 --- a/tools/hello.cpp +++ b/tools/hello.cpp @@ -96,6 +96,7 @@ main (void) "VK_LAYER_LUNARG_standard_validation" }; + //------------------------------------------------------------------------- cruft::vk::instance instance ( util::make_view (layers), util::make_view (extensions.data (), extensions.data () + extensions.size ()) @@ -103,23 +104,30 @@ main (void) load_traits (instance); - auto callback = [&instance] () { - VkDebugReportCallbackCreateInfoEXT debug_info {}; - debug_info.sType = cruft::vk::structure_type_v; - debug_info.flags = //VK_DEBUG_REPORT_INFORMATION_BIT_EXT | - VK_DEBUG_REPORT_WARNING_BIT_EXT | - VK_DEBUG_REPORT_PERFORMANCE_WARNING_BIT_EXT | - VK_DEBUG_REPORT_ERROR_BIT_EXT - //VK_DEBUG_REPORT_DEBUG_BIT_EXT | - ; - debug_info.pfnCallback = vk_debug_callback; - return cruft::vk::make_owned (instance, &debug_info, nullptr); - } (); + //------------------------------------------------------------------------- + VkDebugReportCallbackCreateInfoEXT debug_info {}; + debug_info.sType = cruft::vk::structure_type_v; + debug_info.flags = + //VK_DEBUG_REPORT_INFORMATION_BIT_EXT | + VK_DEBUG_REPORT_WARNING_BIT_EXT | + VK_DEBUG_REPORT_PERFORMANCE_WARNING_BIT_EXT | + VK_DEBUG_REPORT_ERROR_BIT_EXT + //VK_DEBUG_REPORT_DEBUG_BIT_EXT | + ; + debug_info.pfnCallback = vk_debug_callback; + auto debug_callback = cruft::vk::make_owned ( + instance, &debug_info, nullptr + ); + + + //------------------------------------------------------------------------- auto pdevices = cruft::vk::physical_device::find (instance); auto &pdevice = pdevices[0]; + + //------------------------------------------------------------------------- auto surface = cruft::vk::make_owned ( cruft::vk::error::try_query ( glfwCreateWindowSurface, instance.native (), window, nullptr @@ -192,6 +200,8 @@ main (void) cruft::vk::device ldevice (pdevice, device_info); + + //------------------------------------------------------------------------- #if 0 VkSurfaceFormatKHR surface_format = (surface_formats.size () == 1 && surface_formats[0].format == VK_FORMAT_UNDEFINED) @@ -238,17 +248,13 @@ main (void) auto swapchain = cruft::vk::make_owned (ldevice, &swap_create_info, nullptr); - uint32_t swap_image_count = 0; - cruft::vk::error::try_code ( - vkGetSwapchainImagesKHR (ldevice.native (), swapchain->native (), &swap_image_count, nullptr) - ); std::vector swap_images = cruft::vk::error::try_values ( vkGetSwapchainImagesKHR, ldevice.native (), swapchain->native () ); using image_view_ptr = cruft::vk::owned_ptr; std::vector swap_image_views; - swap_image_views.reserve (swap_image_count); + swap_image_views.reserve (std::size (swap_images)); std::transform ( std::cbegin (swap_images), std::cend (swap_images), std::back_inserter (swap_image_views), @@ -317,11 +323,10 @@ main (void) vkBindBufferMemory (ldevice.native (), vertex_buffer->native (), vertex_memory->native (), 0) ); - auto data = cruft::vk::error::try_query ( - vkMapMemory, ldevice.native (), vertex_memory->native (), 0, buffer_info.size, 0 - ); - memcpy (data, std::data (VERTICES), sizeof (VERTICES)); - vkUnmapMemory (ldevice.native (), vertex_memory->native ()); + { + auto data = map (vertex_memory, sizeof (VERTICES)); + memcpy (std::data (data), std::data (VERTICES), sizeof (VERTICES)); + } VkPipelineShaderStageCreateInfo vert_stage_info {}; vert_stage_info.sType = cruft::vk::structure_type_v;