diff options
| author | Tony-LunarG <tony@lunarg.com> | 2019-12-19 12:04:19 -0700 |
|---|---|---|
| committer | Tony Barbour <tony@lunarg.com> | 2019-12-19 14:17:45 -0700 |
| commit | 37af49fa7b55692ca59fd3539553ec47f6b2fa3d (patch) | |
| tree | aa1bd6bea305abae39944e55ddd09b3c4ce515a8 | |
| parent | 246503e9c085ba003d8f7259539128383e9e56e3 (diff) | |
| download | usermoji-37af49fa7b55692ca59fd3539553ec47f6b2fa3d.tar.xz | |
cube: Leave uniform memory mapped
Best practice is to leave memory mapped for the life of the app
rather than map-update-unmap
Change-Id: Iab7ff62a4ebdf47f4916f7d769d0cf3647a52d31
| -rw-r--r-- | cube/cube.c | 21 | ||||
| -rw-r--r-- | cube/cube.cpp | 19 |
2 files changed, 15 insertions, 25 deletions
diff --git a/cube/cube.c b/cube/cube.c index a7b097be..83c53180 100644 --- a/cube/cube.c +++ b/cube/cube.c @@ -295,6 +295,7 @@ typedef struct { VkImageView view; VkBuffer uniform_buffer; VkDeviceMemory uniform_memory; + void *uniform_memory_ptr; VkFramebuffer framebuffer; VkDescriptorSet descriptor_set; } SwapchainImageResources; @@ -869,8 +870,6 @@ void demo_build_image_ownership_cmd(struct demo *demo, int i) { void demo_update_data_buffer(struct demo *demo) { mat4x4 MVP, Model, VP; int matrixSize = sizeof(MVP); - uint8_t *pData; - VkResult U_ASSERT_ONLY err; mat4x4_mul(VP, demo->projection_matrix, demo->view_matrix); @@ -879,13 +878,7 @@ void demo_update_data_buffer(struct demo *demo) { mat4x4_rotate(demo->model_matrix, Model, 0.0f, 1.0f, 0.0f, (float)degreesToRadians(demo->spin_angle)); mat4x4_mul(MVP, VP, demo->model_matrix); - err = vkMapMemory(demo->device, demo->swapchain_image_resources[demo->current_buffer].uniform_memory, 0, VK_WHOLE_SIZE, 0, - (void **)&pData); - assert(!err); - - memcpy(pData, (const void *)&MVP[0][0], matrixSize); - - vkUnmapMemory(demo->device, demo->swapchain_image_resources[demo->current_buffer].uniform_memory); + memcpy(demo->swapchain_image_resources[demo->current_buffer].uniform_memory_ptr, (const void *)&MVP[0][0], matrixSize); } void DemoUpdateTargetIPD(struct demo *demo) { @@ -1753,7 +1746,6 @@ void demo_prepare_cube_data_buffers(struct demo *demo) { VkBufferCreateInfo buf_info; VkMemoryRequirements mem_reqs; VkMemoryAllocateInfo mem_alloc; - uint8_t *pData; mat4x4 MVP, VP; VkResult U_ASSERT_ONLY err; bool U_ASSERT_ONLY pass; @@ -1799,12 +1791,11 @@ void demo_prepare_cube_data_buffers(struct demo *demo) { err = vkAllocateMemory(demo->device, &mem_alloc, NULL, &demo->swapchain_image_resources[i].uniform_memory); assert(!err); - err = vkMapMemory(demo->device, demo->swapchain_image_resources[i].uniform_memory, 0, VK_WHOLE_SIZE, 0, (void **)&pData); + err = vkMapMemory(demo->device, demo->swapchain_image_resources[i].uniform_memory, 0, VK_WHOLE_SIZE, 0, + &demo->swapchain_image_resources[i].uniform_memory_ptr); assert(!err); - memcpy(pData, &data, sizeof data); - - vkUnmapMemory(demo->device, demo->swapchain_image_resources[i].uniform_memory); + memcpy(demo->swapchain_image_resources[i].uniform_memory_ptr, &data, sizeof data); err = vkBindBufferMemory(demo->device, demo->swapchain_image_resources[i].uniform_buffer, demo->swapchain_image_resources[i].uniform_memory, 0); @@ -2338,6 +2329,7 @@ static void demo_cleanup(struct demo *demo) { vkDestroyImageView(demo->device, demo->swapchain_image_resources[i].view, NULL); vkFreeCommandBuffers(demo->device, demo->cmd_pool, 1, &demo->swapchain_image_resources[i].cmd); vkDestroyBuffer(demo->device, demo->swapchain_image_resources[i].uniform_buffer, NULL); + vkUnmapMemory(demo->device, demo->swapchain_image_resources[i].uniform_memory); vkFreeMemory(demo->device, demo->swapchain_image_resources[i].uniform_memory, NULL); } free(demo->swapchain_image_resources); @@ -2420,6 +2412,7 @@ static void demo_resize(struct demo *demo) { vkDestroyImageView(demo->device, demo->swapchain_image_resources[i].view, NULL); vkFreeCommandBuffers(demo->device, demo->cmd_pool, 1, &demo->swapchain_image_resources[i].cmd); vkDestroyBuffer(demo->device, demo->swapchain_image_resources[i].uniform_buffer, NULL); + vkUnmapMemory(demo->device, demo->swapchain_image_resources[i].uniform_memory); vkFreeMemory(demo->device, demo->swapchain_image_resources[i].uniform_memory, NULL); } vkDestroyCommandPool(demo->device, demo->cmd_pool, NULL); diff --git a/cube/cube.cpp b/cube/cube.cpp index ddaa5a5b..216440ab 100644 --- a/cube/cube.cpp +++ b/cube/cube.cpp @@ -206,6 +206,7 @@ typedef struct { vk::ImageView view; vk::Buffer uniform_buffer; vk::DeviceMemory uniform_memory; + void *uniform_memory_ptr; vk::Framebuffer framebuffer; vk::DescriptorSet descriptor_set; } SwapchainImageResources; @@ -646,6 +647,7 @@ void Demo::cleanup() { device.destroyImageView(swapchain_image_resources[i].view, nullptr); device.freeCommandBuffers(cmd_pool, 1, &swapchain_image_resources[i].cmd); device.destroyBuffer(swapchain_image_resources[i].uniform_buffer, nullptr); + device.unmapMemory(swapchain_image_resources[i].uniform_memory); device.freeMemory(swapchain_image_resources[i].uniform_memory, nullptr); } @@ -1707,12 +1709,11 @@ void Demo::prepare_cube_data_buffers() { result = device.allocateMemory(&mem_alloc, nullptr, &swapchain_image_resources[i].uniform_memory); VERIFY(result == vk::Result::eSuccess); - auto pData = device.mapMemory(swapchain_image_resources[i].uniform_memory, 0, VK_WHOLE_SIZE, vk::MemoryMapFlags()); - VERIFY(pData.result == vk::Result::eSuccess); - - memcpy(pData.value, &data, sizeof data); + result = device.mapMemory(swapchain_image_resources[i].uniform_memory, 0, VK_WHOLE_SIZE, vk::MemoryMapFlags(), + &swapchain_image_resources[i].uniform_memory_ptr); + VERIFY(result == vk::Result::eSuccess); - device.unmapMemory(swapchain_image_resources[i].uniform_memory); + memcpy(swapchain_image_resources[i].uniform_memory_ptr, &data, sizeof data); result = device.bindBufferMemory(swapchain_image_resources[i].uniform_buffer, swapchain_image_resources[i].uniform_memory, 0); @@ -2274,6 +2275,7 @@ void Demo::resize() { device.destroyImageView(swapchain_image_resources[i].view, nullptr); device.freeCommandBuffers(cmd_pool, 1, &swapchain_image_resources[i].cmd); device.destroyBuffer(swapchain_image_resources[i].uniform_buffer, nullptr); + device.unmapMemory(swapchain_image_resources[i].uniform_memory); device.freeMemory(swapchain_image_resources[i].uniform_memory, nullptr); } @@ -2348,12 +2350,7 @@ void Demo::update_data_buffer() { mat4x4 MVP; mat4x4_mul(MVP, VP, model_matrix); - auto data = device.mapMemory(swapchain_image_resources[current_buffer].uniform_memory, 0, VK_WHOLE_SIZE, vk::MemoryMapFlags()); - VERIFY(data.result == vk::Result::eSuccess); - - memcpy(data.value, (const void *)&MVP[0][0], sizeof(MVP)); - - device.unmapMemory(swapchain_image_resources[current_buffer].uniform_memory); + memcpy(swapchain_image_resources[current_buffer].uniform_memory_ptr, (const void *)&MVP[0][0], sizeof(MVP)); } /* Convert ppm image data from header file into RGBA texture image */ |
