device_memory: add simple map objects

This commit is contained in:
Danny Robson 2017-09-14 01:39:38 +10:00
parent 300ac66d16
commit c3fc537774
4 changed files with 143 additions and 42 deletions

View File

@ -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<std::byte*> (ptr),
reinterpret_cast<std::byte*> (ptr) + len
};
}
///////////////////////////////////////////////////////////////////////////////
cruft::vk::owned_map_t::owned_map_t (owned_ptr<device_memory> &_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<device_memory> &_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<std::byte*> (ptr),
reinterpret_cast<std::byte*> (ptr) + len
};
};
///////////////////////////////////////////////////////////////////////////////
VkDeviceSize
device_memory::commitment (const device &dev) const

View File

@ -21,15 +21,46 @@
#include "./object.hpp"
#include "./fwd.hpp"
#include <cruft/util/view.hpp>
#include <cstddef>
namespace cruft::vk {
///////////////////////////////////////////////////////////////////////////
struct map_t : public util::view<std::byte*> {
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<device_memory>&, std::byte *first, std::byte *last);
~owned_map_t ();
void destroy (void);
private:
owned_ptr<device_memory> &m_memory;
};
owned_map_t map (owned_ptr<device_memory>&, int len);
///////////////////////////////////////////////////////////////////////////
struct device_memory : public owned<device_memory,device> {
using owned::owned;
void* map (const device&,
VkDeviceSize offset,
VkDeviceSize size,
VkMemoryMapFlags flags);
void unmap (const device&);
VkDeviceSize commitment (const device&) const;

View File

@ -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<SelfT> &m_owner;

View File

@ -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<VkDebugReportCallbackCreateInfoEXT>;
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<cruft::vk::debug_report> (instance, &debug_info, nullptr);
} ();
//-------------------------------------------------------------------------
VkDebugReportCallbackCreateInfoEXT debug_info {};
debug_info.sType = cruft::vk::structure_type_v<VkDebugReportCallbackCreateInfoEXT>;
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<cruft::vk::debug_report> (
instance, &debug_info, nullptr
);
//-------------------------------------------------------------------------
auto pdevices = cruft::vk::physical_device::find (instance);
auto &pdevice = pdevices[0];
//-------------------------------------------------------------------------
auto surface = cruft::vk::make_owned<cruft::vk::surface> (
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<cruft::vk::swapchain> (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<VkImage> swap_images = cruft::vk::error::try_values (
vkGetSwapchainImagesKHR, ldevice.native (), swapchain->native ()
);
using image_view_ptr = cruft::vk::owned_ptr<cruft::vk::image_view>;
std::vector<image_view_ptr> 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<VkPipelineShaderStageCreateInfo>;