From 52f81f8239ec73d9a6b353685154c215fe7a0f63 Mon Sep 17 00:00:00 2001 From: Bradley Austin Davis Date: Sat, 24 Feb 2024 14:18:00 -0800 Subject: [PATCH] Use staging buffer convenience method where possible --- framework/CMakeLists.txt | 4 - framework/api_vulkan_sample.cpp | 23 +----- framework/core/acceleration_structure.cpp | 10 ++- framework/core/acceleration_structure.h | 9 +-- framework/core/scratch_buffer.cpp | 74 ----------------- framework/core/scratch_buffer.h | 71 ---------------- framework/core/shader_binding_table.cpp | 80 ------------------- framework/core/shader_binding_table.h | 69 ---------------- framework/gltf_loader.cpp | 39 ++------- framework/gui.cpp | 15 ++-- framework/hpp_api_vulkan_sample.cpp | 14 +--- framework/hpp_gui.cpp | 52 +++++++----- samples/api/compute_nbody/compute_nbody.cpp | 5 +- .../hpp_compute_nbody/hpp_compute_nbody.cpp | 11 ++- .../hpp_texture_mipmap_generation.cpp | 5 +- samples/api/instancing/instancing.cpp | 5 +- .../terrain_tessellation.cpp | 9 +-- .../buffer_device_address.cpp | 15 ++-- .../conservative_rasterization.cpp | 9 +-- .../descriptor_indexing.cpp | 15 ++-- .../extended_dynamic_state2.cpp | 13 +-- .../fragment_shading_rate.cpp | 12 +-- .../fragment_shading_rate_dynamic.cpp | 17 ++-- .../logic_op_dynamic_state.cpp | 13 +-- .../memory_budget/memory_budget.cpp | 5 +- .../shader_object/shader_object.cpp | 11 +-- .../extensions/sparse_image/sparse_image.cpp | 20 ++--- .../synchronization_2/synchronization_2.cpp | 5 +- .../vertex_dynamic_state.cpp | 9 +-- .../16bit_arithmetic/16bit_arithmetic.cpp | 8 +- .../multi_draw_indirect.cpp | 33 ++++---- samples/tooling/profiles/profiles.cpp | 14 ++-- 32 files changed, 156 insertions(+), 538 deletions(-) delete mode 100644 framework/core/scratch_buffer.cpp delete mode 100644 framework/core/scratch_buffer.h delete mode 100644 framework/core/shader_binding_table.cpp delete mode 100644 framework/core/shader_binding_table.h diff --git a/framework/CMakeLists.txt b/framework/CMakeLists.txt index 169cb5c24a..0e82f8f505 100644 --- a/framework/CMakeLists.txt +++ b/framework/CMakeLists.txt @@ -250,9 +250,7 @@ set(CORE_FILES core/framebuffer.h core/render_pass.h core/query_pool.h - core/scratch_buffer.h core/acceleration_structure.h - core/shader_binding_table.h core/hpp_allocated.h core/hpp_buffer.h core/hpp_command_buffer.h @@ -303,9 +301,7 @@ set(CORE_FILES core/framebuffer.cpp core/render_pass.cpp core/query_pool.cpp - core/scratch_buffer.cpp core/acceleration_structure.cpp - core/shader_binding_table.cpp core/vulkan_resource.cpp core/hpp_buffer.cpp core/hpp_command_buffer.cpp diff --git a/framework/api_vulkan_sample.cpp b/framework/api_vulkan_sample.cpp index 8c5a0ee30d..c44552299c 100644 --- a/framework/api_vulkan_sample.cpp +++ b/framework/api_vulkan_sample.cpp @@ -1,4 +1,4 @@ -/* Copyright (c) 2019-2023, Sascha Willems +/* Copyright (c) 2019-2024, Sascha Willems * * SPDX-License-Identifier: Apache-2.0 * @@ -1000,12 +1000,7 @@ Texture ApiVulkanSample::load_texture(const std::string &file, vkb::sg::Image::C VkCommandBuffer command_buffer = device->create_command_buffer(VK_COMMAND_BUFFER_LEVEL_PRIMARY, true); - vkb::core::Buffer stage_buffer{*device, - texture.image->get_data().size(), - VK_BUFFER_USAGE_TRANSFER_SRC_BIT, - VMA_MEMORY_USAGE_CPU_ONLY}; - - stage_buffer.update(texture.image->get_data()); + vkb::core::Buffer stage_buffer = vkb::core::Buffer::create_staging_buffer(*device, texture.image->get_data()); // Setup buffer copy regions for each mip level std::vector bufferCopyRegions; @@ -1096,12 +1091,7 @@ Texture ApiVulkanSample::load_texture_array(const std::string &file, vkb::sg::Im VkCommandBuffer command_buffer = device->create_command_buffer(VK_COMMAND_BUFFER_LEVEL_PRIMARY, true); - vkb::core::Buffer stage_buffer{*device, - texture.image->get_data().size(), - VK_BUFFER_USAGE_TRANSFER_SRC_BIT, - VMA_MEMORY_USAGE_CPU_ONLY}; - - stage_buffer.update(texture.image->get_data()); + vkb::core::Buffer stage_buffer = vkb::core::Buffer::create_staging_buffer(*device, texture.image->get_data()); // Setup buffer copy regions for each mip level std::vector buffer_copy_regions; @@ -1195,12 +1185,7 @@ Texture ApiVulkanSample::load_texture_cubemap(const std::string &file, vkb::sg:: VkCommandBuffer command_buffer = device->create_command_buffer(VK_COMMAND_BUFFER_LEVEL_PRIMARY, true); - vkb::core::Buffer stage_buffer{*device, - texture.image->get_data().size(), - VK_BUFFER_USAGE_TRANSFER_SRC_BIT, - VMA_MEMORY_USAGE_CPU_ONLY}; - - stage_buffer.update(texture.image->get_data()); + vkb::core::Buffer stage_buffer = vkb::core::Buffer::create_staging_buffer(*device, texture.image->get_data()); // Setup buffer copy regions for each mip level std::vector buffer_copy_regions; diff --git a/framework/core/acceleration_structure.cpp b/framework/core/acceleration_structure.cpp index 2911ba81c5..b1522ac599 100644 --- a/framework/core/acceleration_structure.cpp +++ b/framework/core/acceleration_structure.cpp @@ -1,4 +1,4 @@ -/* Copyright (c) 2021, Sascha Willems +/* Copyright (c) 2021-2024, Sascha Willems * * SPDX-License-Identifier: Apache-2.0 * @@ -23,7 +23,7 @@ namespace vkb { namespace core { -AccelerationStructure::AccelerationStructure(Device & device, +AccelerationStructure::AccelerationStructure(Device &device, VkAccelerationStructureTypeKHR type) : device{device}, type{type} @@ -202,7 +202,11 @@ void AccelerationStructure::build(VkQueue queue, VkBuildAccelerationStructureFla device_address = vkGetAccelerationStructureDeviceAddressKHR(device.get_handle(), &acceleration_device_address_info); // Create a scratch buffer as a temporary storage for the acceleration structure build - scratch_buffer = std::make_unique(device, build_sizes_info.buildScratchSize); + scratch_buffer = std::make_unique( + device, + build_sizes_info.buildScratchSize, + VK_BUFFER_USAGE_STORAGE_BUFFER_BIT | VK_BUFFER_USAGE_SHADER_DEVICE_ADDRESS_BIT, + VMA_MEMORY_USAGE_GPU_ONLY); build_geometry_info.scratchData.deviceAddress = scratch_buffer->get_device_address(); build_geometry_info.dstAccelerationStructure = handle; diff --git a/framework/core/acceleration_structure.h b/framework/core/acceleration_structure.h index 7d65b6f149..61fbcf8621 100644 --- a/framework/core/acceleration_structure.h +++ b/framework/core/acceleration_structure.h @@ -1,4 +1,4 @@ -/* Copyright (c) 2021, Sascha Willems +/* Copyright (c) 2021-2024, Sascha Willems * * SPDX-License-Identifier: Apache-2.0 * @@ -20,7 +20,6 @@ #include "common/helpers.h" #include "common/vk_common.h" #include "core/buffer.h" -#include "core/scratch_buffer.h" namespace vkb { @@ -39,7 +38,7 @@ class AccelerationStructure * @param device A valid Vulkan device * @param type The type of the acceleration structure (top- or bottom-level) */ - AccelerationStructure(Device & device, + AccelerationStructure(Device &device, VkAccelerationStructureTypeKHR type); ~AccelerationStructure(); @@ -54,7 +53,7 @@ class AccelerationStructure * @param max_vertex Index of the last vertex in the geometry * @param vertex_stride Stride of the vertex structure * @param transform_offset Offset of this geometry in the transform data buffer - * @param vertex_format Format of the vertex structure + * @param vertex_format Format of the vertex structure * @param flags Ray tracing geometry flags * @param vertex_buffer_data_address set this if don't want the vertex_buffer data_address * @param index_buffer_data_address set this if don't want the index_buffer data_address @@ -149,7 +148,7 @@ class AccelerationStructure bool updated = false; }; - std::unique_ptr scratch_buffer; + std::unique_ptr scratch_buffer; std::map geometries{}; diff --git a/framework/core/scratch_buffer.cpp b/framework/core/scratch_buffer.cpp deleted file mode 100644 index 84816933f7..0000000000 --- a/framework/core/scratch_buffer.cpp +++ /dev/null @@ -1,74 +0,0 @@ -/* Copyright (c) 2021, Sascha Willems - * - * SPDX-License-Identifier: Apache-2.0 - * - * 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. - */ - -#include "scratch_buffer.h" - -#include "device.h" - -namespace vkb -{ -namespace core -{ -ScratchBuffer::ScratchBuffer(Device &device, VkDeviceSize size) : - device{device}, - size{size} -{ - VkBufferCreateInfo buffer_info{VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO}; - buffer_info.usage = VK_BUFFER_USAGE_STORAGE_BUFFER_BIT | VK_BUFFER_USAGE_SHADER_DEVICE_ADDRESS_BIT; - buffer_info.size = size; - - VmaAllocationCreateInfo memory_info{}; - memory_info.usage = VMA_MEMORY_USAGE_GPU_ONLY; - - VmaAllocationInfo allocation_info{}; - auto result = vmaCreateBuffer(device.get_memory_allocator(), - &buffer_info, &memory_info, - &handle, &allocation, - &allocation_info); - - if (result != VK_SUCCESS) - { - throw VulkanException{result, "Could not create Scratchbuffer"}; - } - - memory = allocation_info.deviceMemory; - - VkBufferDeviceAddressInfoKHR buffer_device_address_info{}; - buffer_device_address_info.sType = VK_STRUCTURE_TYPE_BUFFER_DEVICE_ADDRESS_INFO; - buffer_device_address_info.buffer = handle; - device_address = vkGetBufferDeviceAddressKHR(device.get_handle(), &buffer_device_address_info); -} - -ScratchBuffer::~ScratchBuffer() -{ - if (handle != VK_NULL_HANDLE && allocation != VK_NULL_HANDLE) - { - vmaDestroyBuffer(device.get_memory_allocator(), handle, allocation); - } -} - -VkBuffer ScratchBuffer::get_handle() const -{ - return handle; -} - -uint64_t ScratchBuffer::get_device_address() const -{ - return device_address; -} -} // namespace core -} // namespace vkb diff --git a/framework/core/scratch_buffer.h b/framework/core/scratch_buffer.h deleted file mode 100644 index a0df565109..0000000000 --- a/framework/core/scratch_buffer.h +++ /dev/null @@ -1,71 +0,0 @@ -/* Copyright (c) 2023, Sascha Willems - * - * SPDX-License-Identifier: Apache-2.0 - * - * 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. - */ - -#pragma once - -#include "common/helpers.h" -#include "common/vk_common.h" - -namespace vkb -{ -class Device; - -namespace core -{ -/** - * @brief A simplified buffer class for creating temporary device local scratch buffers, used in e.g. ray tracing - */ -class ScratchBuffer -{ - public: - /** - * @brief Creates a scratch buffer using VMA with pre-defined usage flags - * @param device A valid Vulkan device - * @param size The size in bytes of the buffer - */ - ScratchBuffer(Device & device, - VkDeviceSize size); - - ~ScratchBuffer(); - - VkBuffer get_handle() const; - - uint64_t get_device_address() const; - - /** - * @return The size of the buffer - */ - VkDeviceSize get_size() const - { - return size; - } - - private: - Device &device; - - uint64_t device_address{0}; - - VkBuffer handle{VK_NULL_HANDLE}; - - VmaAllocation allocation{VK_NULL_HANDLE}; - - VkDeviceMemory memory{VK_NULL_HANDLE}; - - VkDeviceSize size{0}; -}; -} // namespace core -} // namespace vkb \ No newline at end of file diff --git a/framework/core/shader_binding_table.cpp b/framework/core/shader_binding_table.cpp deleted file mode 100644 index eb2fe55755..0000000000 --- a/framework/core/shader_binding_table.cpp +++ /dev/null @@ -1,80 +0,0 @@ -/* Copyright (c) 2021, Sascha Willems - * - * SPDX-License-Identifier: Apache-2.0 - * - * 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. - */ - -#include "shader_binding_table.h" - -#include "device.h" - -namespace vkb -{ -namespace core -{ -ShaderBindingTable::ShaderBindingTable(Device & device, - uint32_t handle_count, - VkDeviceSize handle_size_aligned, - VmaMemoryUsage memory_usage) : - device{device} -{ - VkBufferCreateInfo buffer_info{VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO}; - buffer_info.usage = VK_BUFFER_USAGE_SHADER_BINDING_TABLE_BIT_KHR | VK_BUFFER_USAGE_SHADER_DEVICE_ADDRESS_BIT; - buffer_info.size = handle_count * handle_size_aligned; - - VmaAllocationCreateInfo memory_info{}; - memory_info.flags = VMA_ALLOCATION_CREATE_MAPPED_BIT; - memory_info.usage = memory_usage; - - VmaAllocationInfo allocation_info{}; - auto result = vmaCreateBuffer(device.get_memory_allocator(), - &buffer_info, &memory_info, - &handle, &allocation, - &allocation_info); - - if (result != VK_SUCCESS) - { - throw VulkanException{result, "Could not create ShaderBindingTable"}; - } - - memory = allocation_info.deviceMemory; - - VkBufferDeviceAddressInfoKHR buffer_device_address_info{}; - buffer_device_address_info.sType = VK_STRUCTURE_TYPE_BUFFER_DEVICE_ADDRESS_INFO; - buffer_device_address_info.buffer = handle; - strided_device_address_region.deviceAddress = vkGetBufferDeviceAddressKHR(device.get_handle(), &buffer_device_address_info); - strided_device_address_region.stride = handle_size_aligned; - strided_device_address_region.size = handle_count * handle_size_aligned; - - mapped_data = static_cast(allocation_info.pMappedData); -} - -ShaderBindingTable::~ShaderBindingTable() -{ - if (handle != VK_NULL_HANDLE && allocation != VK_NULL_HANDLE) - { - vmaDestroyBuffer(device.get_memory_allocator(), handle, allocation); - } -} - -const VkStridedDeviceAddressRegionKHR *vkb::core::ShaderBindingTable::get_strided_device_address_region() const -{ - return &strided_device_address_region; -} -uint8_t *ShaderBindingTable::get_data() const -{ - return mapped_data; -} -} // namespace core -} // namespace vkb diff --git a/framework/core/shader_binding_table.h b/framework/core/shader_binding_table.h deleted file mode 100644 index c822d2e931..0000000000 --- a/framework/core/shader_binding_table.h +++ /dev/null @@ -1,69 +0,0 @@ -/* Copyright (c) 2021, Sascha Willems - * - * SPDX-License-Identifier: Apache-2.0 - * - * 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. - */ - -#pragma once - -#include "common/helpers.h" -#include "common/vk_common.h" - -namespace vkb -{ -class Device; - -namespace core -{ -/** - * @brief Extended buffer class to simplify ray tracing shader binding table usage - */ -class ShaderBindingTable -{ - public: - /** - * @brief Creates a shader binding table - * @param device A valid Vulkan device - * @param handle_count Shader group handle count - * @param handle_size_aligned Aligned shader group handle size - * @param memory_usage The memory usage of the shader binding table - */ - ShaderBindingTable(Device & device, - uint32_t handle_count, - VkDeviceSize handle_size_aligned, - VmaMemoryUsage memory_usage = VMA_MEMORY_USAGE_CPU_TO_GPU); - - ~ShaderBindingTable(); - - const VkStridedDeviceAddressRegionKHR *get_strided_device_address_region() const; - - uint8_t *get_data() const; - - private: - Device &device; - - VkStridedDeviceAddressRegionKHR strided_device_address_region{}; - - uint64_t device_address{0}; - - VkBuffer handle{VK_NULL_HANDLE}; - - VmaAllocation allocation{VK_NULL_HANDLE}; - - VkDeviceMemory memory{VK_NULL_HANDLE}; - - uint8_t *mapped_data{nullptr}; -}; -} // namespace core -} // namespace vkb \ No newline at end of file diff --git a/framework/gltf_loader.cpp b/framework/gltf_loader.cpp index 6e6eddc27d..d20633a9af 100644 --- a/framework/gltf_loader.cpp +++ b/framework/gltf_loader.cpp @@ -1,5 +1,5 @@ -/* Copyright (c) 2018-2023, Arm Limited and Contributors - * Copyright (c) 2019-2023, Sascha Willems +/* Copyright (c) 2018-2024, Arm Limited and Contributors + * Copyright (c) 2019-2024, Sascha Willems * * SPDX-License-Identifier: Apache-2.0 * @@ -587,15 +587,10 @@ sg::Scene GLTFLoader::load_scene(int scene_index) auto &image = image_components[image_index]; - core::Buffer stage_buffer{device, - image->get_data().size(), - VK_BUFFER_USAGE_TRANSFER_SRC_BIT, - VMA_MEMORY_USAGE_CPU_ONLY}; + core::Buffer stage_buffer = vkb::core::Buffer::create_staging_buffer(device, image->get_data()); batch_size += image->get_data().size(); - stage_buffer.update(image->get_data()); - upload_image_to_gpu(command_buffer, stage_buffer, *image); transient_buffers.push_back(std::move(stage_buffer)); @@ -1146,12 +1141,7 @@ std::unique_ptr GLTFLoader::load_model(uint32_t index, bool storage aligned_vertex_data.push_back(vert); } - core::Buffer stage_buffer{device, - aligned_vertex_data.size() * sizeof(AlignedVertex), - VK_BUFFER_USAGE_TRANSFER_SRC_BIT, - VMA_MEMORY_USAGE_CPU_ONLY}; - - stage_buffer.update(aligned_vertex_data.data(), aligned_vertex_data.size() * sizeof(AlignedVertex)); + core::Buffer stage_buffer = vkb::core::Buffer::create_staging_buffer(device, aligned_vertex_data); core::Buffer buffer{device, aligned_vertex_data.size() * sizeof(AlignedVertex), @@ -1179,12 +1169,7 @@ std::unique_ptr GLTFLoader::load_model(uint32_t index, bool storage vertex_data.push_back(vert); } - core::Buffer stage_buffer{device, - vertex_data.size() * sizeof(Vertex), - VK_BUFFER_USAGE_TRANSFER_SRC_BIT, - VMA_MEMORY_USAGE_CPU_ONLY}; - - stage_buffer.update(vertex_data.data(), vertex_data.size() * sizeof(Vertex)); + core::Buffer stage_buffer = vkb::core::Buffer::create_staging_buffer(device, vertex_data); core::Buffer buffer{device, vertex_data.size() * sizeof(Vertex), @@ -1241,12 +1226,7 @@ std::unique_ptr GLTFLoader::load_model(uint32_t index, bool storage // vertex_indices and index_buffer are used for meshlets now submesh->vertex_indices = (uint32_t) meshlets.size(); - core::Buffer stage_buffer{device, - meshlets.size() * sizeof(Meshlet), - VK_BUFFER_USAGE_TRANSFER_SRC_BIT, - VMA_MEMORY_USAGE_CPU_ONLY}; - - stage_buffer.update(meshlets.data(), meshlets.size() * sizeof(Meshlet)); + core::Buffer stage_buffer = vkb::core::Buffer::create_staging_buffer(device, meshlets); submesh->index_buffer = std::make_unique(device, meshlets.size() * sizeof(Meshlet), @@ -1259,12 +1239,7 @@ std::unique_ptr GLTFLoader::load_model(uint32_t index, bool storage } else { - core::Buffer stage_buffer{device, - index_data.size(), - VK_BUFFER_USAGE_TRANSFER_SRC_BIT, - VMA_MEMORY_USAGE_CPU_ONLY}; - - stage_buffer.update(index_data); + core::Buffer stage_buffer = vkb::core::Buffer::create_staging_buffer(device, index_data); submesh->index_buffer = std::make_unique(device, index_data.size(), diff --git a/framework/gui.cpp b/framework/gui.cpp index 1c9b687778..bce5e83786 100644 --- a/framework/gui.cpp +++ b/framework/gui.cpp @@ -1,5 +1,5 @@ -/* Copyright (c) 2018-2023, Arm Limited and Contributors - * Copyright (c) 2019-2023, Sascha Willems +/* Copyright (c) 2018-2024, Arm Limited and Contributors + * Copyright (c) 2019-2024, Sascha Willems * * SPDX-License-Identifier: Apache-2.0 * @@ -49,10 +49,10 @@ namespace vkb { namespace { -void upload_draw_data(ImDrawData *draw_data, const uint8_t *vertex_data, const uint8_t *index_data) +void upload_draw_data(const ImDrawData *draw_data, uint8_t *vertex_data, uint8_t *index_data) { - ImDrawVert *vtx_dst = (ImDrawVert *) vertex_data; - ImDrawIdx *idx_dst = (ImDrawIdx *) index_data; + ImDrawVert *vtx_dst = reinterpret_cast(vertex_data); + ImDrawIdx *idx_dst = reinterpret_cast(index_data); for (int n = 0; n < draw_data->CmdListsCount; n++) { @@ -177,8 +177,7 @@ Gui::Gui(VulkanSample &sample_, const Window &window, const Stats *stats, // Upload font data into the vulkan image memory { - core::Buffer stage_buffer{device, upload_size, VK_BUFFER_USAGE_TRANSFER_SRC_BIT, VMA_MEMORY_USAGE_CPU_ONLY, 0}; - stage_buffer.update({font_data, font_data + upload_size}); + core::Buffer stage_buffer = core::Buffer::create_staging_buffer(device, upload_size, font_data); auto &command_buffer = device.request_command_buffer(); @@ -1141,7 +1140,9 @@ bool Drawer::radio_button(const char *caption, int32_t *selectedOption, const in { bool res = ImGui::RadioButton(caption, selectedOption, elementOption); if (res) + { dirty = true; + } return res; } diff --git a/framework/hpp_api_vulkan_sample.cpp b/framework/hpp_api_vulkan_sample.cpp index a16212b1d1..cdd1b23528 100644 --- a/framework/hpp_api_vulkan_sample.cpp +++ b/framework/hpp_api_vulkan_sample.cpp @@ -1,4 +1,4 @@ -/* Copyright (c) 2021-2023, NVIDIA CORPORATION. All rights reserved. +/* Copyright (c) 2021-2024, NVIDIA CORPORATION. All rights reserved. * * SPDX-License-Identifier: Apache-2.0 * @@ -838,9 +838,7 @@ HPPTexture HPPApiVulkanSample::load_texture(const std::string &file, vkb::scene_ vk::CommandBuffer command_buffer = get_device()->create_command_buffer(vk::CommandBufferLevel::ePrimary, true); - vkb::core::HPPBuffer stage_buffer{*get_device(), texture.image->get_data().size(), vk::BufferUsageFlagBits::eTransferSrc, VMA_MEMORY_USAGE_CPU_ONLY}; - - stage_buffer.update(texture.image->get_data()); + vkb::core::HPPBuffer stage_buffer = vkb::core::HPPBuffer::create_staging_buffer(*get_device(), texture.image->get_data()); // Setup buffer copy regions for each mip level std::vector bufferCopyRegions; @@ -898,9 +896,7 @@ HPPTexture HPPApiVulkanSample::load_texture_array(const std::string &file, vkb:: vk::CommandBuffer command_buffer = get_device()->create_command_buffer(vk::CommandBufferLevel::ePrimary, true); - vkb::core::HPPBuffer stage_buffer{*get_device(), texture.image->get_data().size(), vk::BufferUsageFlagBits::eTransferSrc, VMA_MEMORY_USAGE_CPU_ONLY}; - - stage_buffer.update(texture.image->get_data()); + vkb::core::HPPBuffer stage_buffer = vkb::core::HPPBuffer::create_staging_buffer(*get_device(), texture.image->get_data()); // Setup buffer copy regions for each mip level std::vector buffer_copy_regions; @@ -964,9 +960,7 @@ HPPTexture HPPApiVulkanSample::load_texture_cubemap(const std::string &file, vkb vk::CommandBuffer command_buffer = get_device()->create_command_buffer(vk::CommandBufferLevel::ePrimary, true); - vkb::core::HPPBuffer stage_buffer{*get_device(), texture.image->get_data().size(), vk::BufferUsageFlagBits::eTransferSrc, VMA_MEMORY_USAGE_CPU_ONLY}; - - stage_buffer.update(texture.image->get_data()); + vkb::core::HPPBuffer stage_buffer = vkb::core::HPPBuffer::create_staging_buffer(*get_device(), texture.image->get_data()); // Setup buffer copy regions for each mip level std::vector buffer_copy_regions; diff --git a/framework/hpp_gui.cpp b/framework/hpp_gui.cpp index f518576626..23a8e0c74a 100644 --- a/framework/hpp_gui.cpp +++ b/framework/hpp_gui.cpp @@ -1,4 +1,4 @@ -/* Copyright (c) 2023, NVIDIA CORPORATION. All rights reserved. +/* Copyright (c) 2023-2024, NVIDIA CORPORATION. All rights reserved. * * SPDX-License-Identifier: Apache-2.0 * @@ -26,10 +26,10 @@ namespace vkb { namespace { -void upload_draw_data(const ImDrawData *draw_data, const uint8_t *vertex_data, const uint8_t *index_data) +void upload_draw_data(const ImDrawData *draw_data, uint8_t *vertex_data, uint8_t *index_data) { - ImDrawVert *vtx_dst = (ImDrawVert *) vertex_data; - ImDrawIdx *idx_dst = (ImDrawIdx *) index_data; + ImDrawVert *vtx_dst = reinterpret_cast(vertex_data); + ImDrawIdx *idx_dst = reinterpret_cast(index_data); for (int n = 0; n < draw_data->CmdListsCount; n++) { @@ -131,20 +131,19 @@ HPPGui::HPPGui(HPPVulkanSample &sample_, const vkb::Window &window, const vkb::s auto &device = sample.get_render_context().get_device(); // Create target image for copy - vk::Extent3D font_extent(to_u32(tex_width), to_u32(tex_height), 1u); - - font_image = std::make_unique(device, font_extent, vk::Format::eR8G8B8A8Unorm, - vk::ImageUsageFlagBits::eSampled | vk::ImageUsageFlagBits::eTransferDst, - VMA_MEMORY_USAGE_GPU_ONLY); - font_image->set_debug_name("GUI font image"); + font_image = + vkb::core::HPPImageBuilder(to_u32(tex_width), to_u32(tex_height)) + .with_format(vk::Format::eR8G8B8A8Unorm) + .with_usage(vk::ImageUsageFlagBits::eSampled | vk::ImageUsageFlagBits::eTransferDst) + .with_debug_name("GUI font image") + .build_unique(device); font_image_view = std::make_unique(*font_image, vk::ImageViewType::e2D); font_image_view->set_debug_name("View on GUI font image"); // Upload font data into the vulkan image memory { - vkb::core::HPPBuffer stage_buffer(device, upload_size, vk::BufferUsageFlagBits::eTransferSrc, VMA_MEMORY_USAGE_CPU_ONLY, 0); - stage_buffer.update({font_data, font_data + upload_size}); + vkb::core::HPPBuffer stage_buffer = vkb::core::HPPBuffer::create_staging_buffer(device, upload_size, font_data); auto &command_buffer = device.get_command_pool().request_command_buffer(); @@ -225,11 +224,21 @@ HPPGui::HPPGui(HPPVulkanSample &sample_, const vkb::Window &window, const vkb::s if (explicit_update) { - vertex_buffer = std::make_unique(sample.get_render_context().get_device(), 1, vk::BufferUsageFlagBits::eVertexBuffer, VMA_MEMORY_USAGE_GPU_TO_CPU); - vertex_buffer->set_debug_name("GUI vertex buffer"); - - index_buffer = std::make_unique(sample.get_render_context().get_device(), 1, vk::BufferUsageFlagBits::eIndexBuffer, VMA_MEMORY_USAGE_GPU_TO_CPU); - index_buffer->set_debug_name("GUI index buffer"); + auto &device = sample.get_render_context().get_device(); + + vertex_buffer = + vkb::core::HPPBufferBuilder(1) + .with_usage(vk::BufferUsageFlagBits::eVertexBuffer) + .with_vma_usage(VMA_MEMORY_USAGE_GPU_TO_CPU) + .with_debug_name("GUI vertex buffer") + .build_unique(device); + + index_buffer = + vkb::core::HPPBufferBuilder(1) + .with_usage(vk::BufferUsageFlagBits::eIndexBuffer) + .with_vma_usage(VMA_MEMORY_USAGE_GPU_TO_CPU) + .with_debug_name("GUI index buffer") + .build_unique(device); } } @@ -691,7 +700,9 @@ bool HPPGui::is_debug_view_active() const HPPGui::StatsView::StatsView(const vkb::stats::HPPStats *stats) { if (stats == nullptr) + { return; + } // Request graph data information for each stat and record it in graph_map const std::set &indices = stats->get_requested_stats(); @@ -1061,7 +1072,7 @@ bool HPPDrawer::checkbox(const std::string &caption, int32_t *value) if (res) { dirty = true; - }; + } return res; } @@ -1069,8 +1080,9 @@ bool HPPDrawer::radio_button(const char *caption, int32_t *selectedOption, const { bool res = ImGui::RadioButton(caption, selectedOption, elementOption); if (res) + { dirty = true; - + } return res; } @@ -1080,7 +1092,7 @@ bool HPPDrawer::input_float(const std::string &caption, float *value, float step if (res) { dirty = true; - }; + } return res; } diff --git a/samples/api/compute_nbody/compute_nbody.cpp b/samples/api/compute_nbody/compute_nbody.cpp index 659b901e82..734f634ba7 100644 --- a/samples/api/compute_nbody/compute_nbody.cpp +++ b/samples/api/compute_nbody/compute_nbody.cpp @@ -1,4 +1,4 @@ -/* Copyright (c) 2019-2023, Sascha Willems +/* Copyright (c) 2019-2024, Sascha Willems * * SPDX-License-Identifier: Apache-2.0 * @@ -322,8 +322,7 @@ void ComputeNBody::prepare_storage_buffers() // Staging // SSBO won't be changed on the host after upload so copy to device local memory - vkb::core::Buffer staging_buffer{get_device(), storage_buffer_size, VK_BUFFER_USAGE_TRANSFER_SRC_BIT, VMA_MEMORY_USAGE_CPU_ONLY}; - staging_buffer.update(particle_buffer.data(), storage_buffer_size); + vkb::core::Buffer staging_buffer = vkb::core::Buffer::create_staging_buffer(get_device(), particle_buffer); compute.storage_buffer = std::make_unique(get_device(), storage_buffer_size, diff --git a/samples/api/hpp_compute_nbody/hpp_compute_nbody.cpp b/samples/api/hpp_compute_nbody/hpp_compute_nbody.cpp index 6e1986afdb..6062635708 100644 --- a/samples/api/hpp_compute_nbody/hpp_compute_nbody.cpp +++ b/samples/api/hpp_compute_nbody/hpp_compute_nbody.cpp @@ -1,4 +1,4 @@ -/* Copyright (c) 2022-2023, NVIDIA CORPORATION. All rights reserved. +/* Copyright (c) 2022-2024, NVIDIA CORPORATION. All rights reserved. * * SPDX-License-Identifier: Apache-2.0 * @@ -371,7 +371,7 @@ void HPPComputeNBody::initializeCamera() camera.type = vkb::CameraType::LookAt; // Note: Using reversed depth-buffer for increased precision, so Z-Near and Z-Far are flipped - camera.set_perspective(60.0f, (float) extent.width / (float) extent.height, 512.0f, 0.1f); + camera.set_perspective(60.0f, static_cast(extent.width) / static_cast(extent.height), 512.0f, 0.1f); camera.set_rotation(glm::vec3(-26.0f, 75.0f, 0.0f)); camera.set_translation(glm::vec3(0.0f, 0.0f, -14.0f)); camera.translation_speed = 2.5f; @@ -513,7 +513,7 @@ void HPPComputeNBody::prepare_compute_storage_buffers() // Initial particle positions std::vector particle_buffer(compute.ubo.particle_count); - std::default_random_engine rnd_engine(lock_simulation_speed ? 0 : (unsigned) time(nullptr)); + std::default_random_engine rnd_engine(lock_simulation_speed ? 0 : static_cast(time(nullptr))); std::normal_distribution rnd_distribution(0.0f, 1.0f); for (uint32_t i = 0; i < static_cast(attractors.size()); i++) @@ -547,7 +547,7 @@ void HPPComputeNBody::prepare_compute_storage_buffers() } // Color gradient offset - particle.vel.w = (float) i * 1.0f / static_cast(attractors.size()); + particle.vel.w = static_cast(i) * 1.0f / static_cast(attractors.size()); } } @@ -555,8 +555,7 @@ void HPPComputeNBody::prepare_compute_storage_buffers() // Staging // SSBO won't be changed on the host after upload so copy to device local memory - vkb::core::HPPBuffer staging_buffer(*get_device(), storage_buffer_size, vk::BufferUsageFlagBits::eTransferSrc, VMA_MEMORY_USAGE_CPU_ONLY); - staging_buffer.update(particle_buffer.data(), storage_buffer_size); + vkb::core::HPPBuffer staging_buffer = vkb::core::HPPBuffer::create_staging_buffer(*get_device(), particle_buffer); compute.storage_buffer = std::make_unique(*get_device(), storage_buffer_size, diff --git a/samples/api/hpp_texture_mipmap_generation/hpp_texture_mipmap_generation.cpp b/samples/api/hpp_texture_mipmap_generation/hpp_texture_mipmap_generation.cpp index 9ba608edcd..3957dd1437 100644 --- a/samples/api/hpp_texture_mipmap_generation/hpp_texture_mipmap_generation.cpp +++ b/samples/api/hpp_texture_mipmap_generation/hpp_texture_mipmap_generation.cpp @@ -1,4 +1,4 @@ -/* Copyright (c) 2022-2023, NVIDIA CORPORATION. All rights reserved. +/* Copyright (c) 2022-2024, NVIDIA CORPORATION. All rights reserved. * * SPDX-License-Identifier: Apache-2.0 * @@ -267,8 +267,7 @@ void HPPTextureMipMapGeneration::load_assets() check_format_features(format); // Create a host-visible staging buffer that contains the raw image data - vkb::core::HPPBuffer staging_buffer(*device, ktx_texture->dataSize, vk::BufferUsageFlagBits::eTransferSrc, VMA_MEMORY_USAGE_CPU_TO_GPU); - staging_buffer.update(ktx_texture->pData, ktx_texture->dataSize); + vkb::core::HPPBuffer staging_buffer = vkb::core::HPPBuffer::create_staging_buffer(*device, ktx_texture->dataSize, ktx_texture->pData); // now, the ktx_texture can be destroyed ktxTexture_Destroy(ktx_texture); diff --git a/samples/api/instancing/instancing.cpp b/samples/api/instancing/instancing.cpp index 7535f114f8..9e9e9d4e45 100644 --- a/samples/api/instancing/instancing.cpp +++ b/samples/api/instancing/instancing.cpp @@ -1,4 +1,4 @@ -/* Copyright (c) 2019-2023, Sascha Willems +/* Copyright (c) 2019-2024, Sascha Willems * * SPDX-License-Identifier: Apache-2.0 * @@ -402,8 +402,7 @@ void Instancing::prepare_instance_data() // On devices with separate memory types for host visible and device local memory this will result in better performance // On devices with unified memory types (DEVICE_LOCAL_BIT and HOST_VISIBLE_BIT supported at once) this isn't necessary and you could skip the staging - vkb::core::Buffer staging_buffer(get_device(), instance_buffer.size, VK_BUFFER_USAGE_TRANSFER_SRC_BIT, VMA_MEMORY_USAGE_CPU_TO_GPU); - staging_buffer.update(instance_data); + vkb::core::Buffer staging_buffer = vkb::core::Buffer::create_staging_buffer(get_device(), instance_data); instance_buffer.buffer = std::make_unique(get_device(), instance_buffer.size, VK_BUFFER_USAGE_VERTEX_BUFFER_BIT | VK_BUFFER_USAGE_TRANSFER_DST_BIT, VMA_MEMORY_USAGE_GPU_ONLY); diff --git a/samples/api/terrain_tessellation/terrain_tessellation.cpp b/samples/api/terrain_tessellation/terrain_tessellation.cpp index dec427e111..7ce184eca9 100644 --- a/samples/api/terrain_tessellation/terrain_tessellation.cpp +++ b/samples/api/terrain_tessellation/terrain_tessellation.cpp @@ -1,4 +1,4 @@ -/* Copyright (c) 2019-2023, Sascha Willems +/* Copyright (c) 2019-2024, Sascha Willems * * SPDX-License-Identifier: Apache-2.0 * @@ -323,11 +323,8 @@ void TerrainTessellation::generate_terrain() // Create staging buffers - vkb::core::Buffer vertex_staging(get_device(), vertex_buffer_size, VK_BUFFER_USAGE_TRANSFER_SRC_BIT, VMA_MEMORY_USAGE_CPU_TO_GPU); - vertex_staging.update(vertices); - - vkb::core::Buffer index_staging(get_device(), index_buffer_size, VK_BUFFER_USAGE_TRANSFER_SRC_BIT, VMA_MEMORY_USAGE_CPU_TO_GPU); - index_staging.update(indices); + vkb::core::Buffer vertex_staging = vkb::core::Buffer::create_staging_buffer(get_device(), vertices); + vkb::core::Buffer index_staging = vkb::core::Buffer::create_staging_buffer(get_device(), indices); terrain.vertices = std::make_unique(get_device(), vertex_buffer_size, diff --git a/samples/extensions/buffer_device_address/buffer_device_address.cpp b/samples/extensions/buffer_device_address/buffer_device_address.cpp index 40b321643d..343238b76d 100644 --- a/samples/extensions/buffer_device_address/buffer_device_address.cpp +++ b/samples/extensions/buffer_device_address/buffer_device_address.cpp @@ -1,4 +1,4 @@ -/* Copyright (c) 2021-2023, Arm Limited and Contributors +/* Copyright (c) 2021-2024, Arm Limited and Contributors * * SPDX-License-Identifier: Apache-2.0 * @@ -185,12 +185,8 @@ std::unique_ptr BufferDeviceAddress::create_index_buffer() VK_BUFFER_USAGE_INDEX_BUFFER_BIT | VK_BUFFER_USAGE_TRANSFER_DST_BIT, VMA_MEMORY_USAGE_GPU_ONLY); - auto staging_buffer = std::make_unique(get_device(), - size, - VK_BUFFER_USAGE_TRANSFER_SRC_BIT, - VMA_MEMORY_USAGE_CPU_TO_GPU); - - auto *buffer = reinterpret_cast(staging_buffer->map()); + auto staging_buffer = vkb::core::Buffer::create_staging_buffer(get_device(), size, nullptr); + auto *buffer = reinterpret_cast(staging_buffer.map()); for (unsigned strip = 0; strip < mesh_strips; strip++) { for (unsigned x = 0; x < mesh_width; x++) @@ -200,11 +196,12 @@ std::unique_ptr BufferDeviceAddress::create_index_buffer() } *buffer++ = 0xffff; } - staging_buffer->unmap(); + staging_buffer.flush(); + staging_buffer.unmap(); auto &cmd = get_device().request_command_buffer(); cmd.begin(VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT); - cmd.copy_buffer(*staging_buffer, *index_buffer, size); + cmd.copy_buffer(staging_buffer, *index_buffer, size); vkb::BufferMemoryBarrier memory_barrier; memory_barrier.src_access_mask = VK_ACCESS_TRANSFER_WRITE_BIT; diff --git a/samples/extensions/conservative_rasterization/conservative_rasterization.cpp b/samples/extensions/conservative_rasterization/conservative_rasterization.cpp index 06d98e79a6..cfd757480f 100644 --- a/samples/extensions/conservative_rasterization/conservative_rasterization.cpp +++ b/samples/extensions/conservative_rasterization/conservative_rasterization.cpp @@ -1,4 +1,4 @@ -/* Copyright (c) 2019-2023, Sascha Willems +/* Copyright (c) 2019-2024, Sascha Willems * * SPDX-License-Identifier: Apache-2.0 * @@ -353,11 +353,8 @@ void ConservativeRasterization::load_assets() uint32_t index_buffer_size = triangle.index_count * sizeof(uint32_t); // Host visible source buffers (staging) - vkb::core::Buffer vertex_staging_buffer{get_device(), vertex_buffer_size, VK_BUFFER_USAGE_TRANSFER_SRC_BIT, VMA_MEMORY_USAGE_CPU_ONLY}; - vertex_staging_buffer.update(vertex_buffer.data(), vertex_buffer_size); - - vkb::core::Buffer index_staging_buffer{get_device(), index_buffer_size, VK_BUFFER_USAGE_TRANSFER_SRC_BIT, VMA_MEMORY_USAGE_CPU_ONLY}; - index_staging_buffer.update(index_buffer.data(), index_buffer_size); + vkb::core::Buffer vertex_staging_buffer = vkb::core::Buffer::create_staging_buffer(get_device(), vertex_buffer); + vkb::core::Buffer index_staging_buffer = vkb::core::Buffer::create_staging_buffer(get_device(), index_buffer); // Device local destination buffers triangle.vertices = std::make_unique(get_device(), diff --git a/samples/extensions/descriptor_indexing/descriptor_indexing.cpp b/samples/extensions/descriptor_indexing/descriptor_indexing.cpp index 3c36ec2ec4..0d67884fa9 100644 --- a/samples/extensions/descriptor_indexing/descriptor_indexing.cpp +++ b/samples/extensions/descriptor_indexing/descriptor_indexing.cpp @@ -1,4 +1,4 @@ -/* Copyright (c) 2021-2023, Arm Limited and Contributors +/* Copyright (c) 2021-2024, Arm Limited and Contributors * * SPDX-License-Identifier: Apache-2.0 * @@ -355,15 +355,11 @@ DescriptorIndexing::TestImage DescriptorIndexing::create_image(const float rgb[3 image_view.image = test_image.image; VK_CHECK(vkCreateImageView(get_device().get_handle(), &image_view, nullptr, &test_image.image_view)); - auto staging_buffer = std::make_unique(get_device(), - image_info.extent.width * image_info.extent.height * sizeof(uint32_t), - VK_BUFFER_USAGE_TRANSFER_SRC_BIT, - VMA_MEMORY_USAGE_CPU_TO_GPU); + auto staging_buffer = vkb::core::Buffer::create_staging_buffer(get_device(), image_info.extent.width * image_info.extent.height * sizeof(uint32_t), nullptr); // Generate a random texture. // Fairly simple, create different colors and some different patterns. - - uint8_t *buffer = staging_buffer->map(); + uint8_t *buffer = staging_buffer.map(); for (uint32_t y = 0; y < image_info.extent.height; y++) { for (uint32_t x = 0; x < image_info.extent.width; x++) @@ -429,7 +425,8 @@ DescriptorIndexing::TestImage DescriptorIndexing::create_image(const float rgb[3 rgba[3] = 0xff; } } - staging_buffer->unmap(); + staging_buffer.flush(); + staging_buffer.unmap(); auto &cmd = get_device().request_command_buffer(); cmd.begin(VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT); @@ -439,7 +436,7 @@ DescriptorIndexing::TestImage DescriptorIndexing::create_image(const float rgb[3 VkBufferImageCopy copy_info{}; copy_info.imageSubresource = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1}; copy_info.imageExtent = image_info.extent; - vkCmdCopyBufferToImage(cmd.get_handle(), staging_buffer->get_handle(), test_image.image, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, 1, ©_info); + vkCmdCopyBufferToImage(cmd.get_handle(), staging_buffer.get_handle(), test_image.image, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, 1, ©_info); vkb::image_layout_transition(cmd.get_handle(), test_image.image, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL); diff --git a/samples/extensions/extended_dynamic_state2/extended_dynamic_state2.cpp b/samples/extensions/extended_dynamic_state2/extended_dynamic_state2.cpp index adeeefcafb..928b3c0e50 100644 --- a/samples/extensions/extended_dynamic_state2/extended_dynamic_state2.cpp +++ b/samples/extensions/extended_dynamic_state2/extended_dynamic_state2.cpp @@ -1,4 +1,4 @@ -/* Copyright (c) 2023, Mobica Limited +/* Copyright (c) 2023-2024, Mobica Limited * * SPDX-License-Identifier: Apache-2.0 * @@ -1009,14 +1009,9 @@ void ExtendedDynamicState2::model_data_creation() UINT32_MAX, 2, 3, 6, 7}; - vkb::core::Buffer vertex_pos_staging(get_device(), vertex_buffer_size, VK_BUFFER_USAGE_TRANSFER_SRC_BIT, VMA_MEMORY_USAGE_CPU_TO_GPU); - vertex_pos_staging.update(vertices_pos); - - vkb::core::Buffer vertex_norm_staging(get_device(), vertex_buffer_size, VK_BUFFER_USAGE_TRANSFER_SRC_BIT, VMA_MEMORY_USAGE_CPU_TO_GPU); - vertex_norm_staging.update(vertices_norm); - - vkb::core::Buffer index_staging(get_device(), index_buffer_size, VK_BUFFER_USAGE_TRANSFER_SRC_BIT, VMA_MEMORY_USAGE_CPU_TO_GPU); - index_staging.update(indices); + vkb::core::Buffer vertex_pos_staging = vkb::core::Buffer::create_staging_buffer(get_device(), vertices_pos); + vkb::core::Buffer vertex_norm_staging = vkb::core::Buffer::create_staging_buffer(get_device(), vertices_norm); + vkb::core::Buffer index_staging = vkb::core::Buffer::create_staging_buffer(get_device(), indices); cube.vertices_pos = std::make_unique(get_device(), vertex_buffer_size, diff --git a/samples/extensions/fragment_shading_rate/fragment_shading_rate.cpp b/samples/extensions/fragment_shading_rate/fragment_shading_rate.cpp index c8d0b8473f..1d27a5ece7 100644 --- a/samples/extensions/fragment_shading_rate/fragment_shading_rate.cpp +++ b/samples/extensions/fragment_shading_rate/fragment_shading_rate.cpp @@ -1,4 +1,4 @@ -/* Copyright (c) 2020-2023, Sascha Willems +/* Copyright (c) 2020-2024, Sascha Willems * * SPDX-License-Identifier: Apache-2.0 * @@ -178,11 +178,7 @@ void FragmentShadingRate::create_shading_rate_attachment() } // Move shading rate pattern data to staging buffer - std::unique_ptr staging_buffer = std::make_unique(get_device(), - buffer_size, - VK_BUFFER_USAGE_TRANSFER_SRC_BIT, - VMA_MEMORY_USAGE_CPU_TO_GPU); - staging_buffer->update(shading_rate_pattern_data, buffer_size); + vkb::core::Buffer staging_buffer = vkb::core::Buffer::create_staging_buffer(get_device(), buffer_size, shading_rate_pattern_data); delete[] shading_rate_pattern_data; // Upload the buffer containing the shading rates to the image that'll be used as the shading rate attachment inside our renderpass @@ -196,15 +192,13 @@ void FragmentShadingRate::create_shading_rate_attachment() buffer_copy_region.imageExtent.width = image_extent.width; buffer_copy_region.imageExtent.height = image_extent.height; buffer_copy_region.imageExtent.depth = 1; - vkCmdCopyBufferToImage(copy_cmd, staging_buffer->get_handle(), shading_rate_image.image, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, 1, &buffer_copy_region); + vkCmdCopyBufferToImage(copy_cmd, staging_buffer.get_handle(), shading_rate_image.image, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, 1, &buffer_copy_region); // Transfer image layout to fragment shading rate attachment layout required to access this in the renderpass vkb::image_layout_transition( copy_cmd, shading_rate_image.image, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, VK_IMAGE_LAYOUT_FRAGMENT_SHADING_RATE_ATTACHMENT_OPTIMAL_KHR); device->flush_command_buffer(copy_cmd, queue, true); - - staging_buffer.reset(); } /* diff --git a/samples/extensions/fragment_shading_rate_dynamic/fragment_shading_rate_dynamic.cpp b/samples/extensions/fragment_shading_rate_dynamic/fragment_shading_rate_dynamic.cpp index 43c2676f81..26a9fa543f 100644 --- a/samples/extensions/fragment_shading_rate_dynamic/fragment_shading_rate_dynamic.cpp +++ b/samples/extensions/fragment_shading_rate_dynamic/fragment_shading_rate_dynamic.cpp @@ -1,4 +1,4 @@ -/* Copyright (c) 2021-2023, Holochip +/* Copyright (c) 2021-2024, Holochip * * SPDX-License-Identifier: Apache-2.0 * @@ -123,11 +123,11 @@ void FragmentShadingRateDynamic::create_shading_rate_attachment() VK_SAMPLE_COUNT_1_BIT); }; - shading_rate_image = create_shading_rate(VK_IMAGE_USAGE_FRAGMENT_SHADING_RATE_ATTACHMENT_BIT_KHR | - static_cast(VK_BUFFER_USAGE_TRANSFER_DST_BIT), - VK_FORMAT_R8_UINT); + shading_rate_image = create_shading_rate( + VK_IMAGE_USAGE_FRAGMENT_SHADING_RATE_ATTACHMENT_BIT_KHR | VK_IMAGE_USAGE_TRANSFER_DST_BIT, + VK_FORMAT_R8_UINT); shading_rate_image_compute = create_shading_rate( - VK_IMAGE_USAGE_STORAGE_BIT | static_cast(VK_BUFFER_USAGE_TRANSFER_SRC_BIT), + VK_IMAGE_USAGE_STORAGE_BIT | VK_IMAGE_USAGE_TRANSFER_SRC_BIT, VK_FORMAT_R8_UINT); uint32_t fragment_shading_rate_count = 0; @@ -148,10 +148,7 @@ void FragmentShadingRateDynamic::create_shading_rate_attachment() const auto min_shading_rate = fragment_shading_rates.front().fragmentSize; std::vector temp_buffer(frame_height * frame_width, (min_shading_rate.height >> 1) | ((min_shading_rate.width << 1) & 12)); - auto staging_buffer = std::make_unique(*device, temp_buffer.size() * sizeof(temp_buffer[0]), - VK_BUFFER_USAGE_TRANSFER_SRC_BIT, - VMA_MEMORY_USAGE_CPU_TO_GPU); - staging_buffer->update(temp_buffer); + auto staging_buffer = vkb::core::Buffer::create_staging_buffer(*device, temp_buffer); auto cmd = device->create_command_buffer(VK_COMMAND_BUFFER_LEVEL_PRIMARY, true); @@ -161,7 +158,7 @@ void FragmentShadingRateDynamic::create_shading_rate_attachment() buffer_copy_region.imageSubresource.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT; buffer_copy_region.imageSubresource.layerCount = 1; buffer_copy_region.imageExtent = image_extent; - vkCmdCopyBufferToImage(cmd, staging_buffer->get_handle(), shading_rate_image->get_handle(), + vkCmdCopyBufferToImage(cmd, staging_buffer.get_handle(), shading_rate_image->get_handle(), VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, 1, &buffer_copy_region); vkb::image_layout_transition( diff --git a/samples/extensions/logic_op_dynamic_state/logic_op_dynamic_state.cpp b/samples/extensions/logic_op_dynamic_state/logic_op_dynamic_state.cpp index 59ddada612..fba4c26b03 100644 --- a/samples/extensions/logic_op_dynamic_state/logic_op_dynamic_state.cpp +++ b/samples/extensions/logic_op_dynamic_state/logic_op_dynamic_state.cpp @@ -1,4 +1,4 @@ -/* Copyright (c) 2023, Mobica Limited +/* Copyright (c) 2023-2024, Mobica Limited * * SPDX-License-Identifier: Apache-2.0 * @@ -609,14 +609,9 @@ void LogicOpDynamicState::model_data_creation() UINT32_MAX, 2, 3, 6, 7}; - vkb::core::Buffer vertex_pos_staging(get_device(), vertex_buffer_size, VK_BUFFER_USAGE_TRANSFER_SRC_BIT, VMA_MEMORY_USAGE_CPU_TO_GPU); - vertex_pos_staging.update(vertices_pos); - - vkb::core::Buffer vertex_norm_staging(get_device(), vertex_buffer_size, VK_BUFFER_USAGE_TRANSFER_SRC_BIT, VMA_MEMORY_USAGE_CPU_TO_GPU); - vertex_norm_staging.update(vertices_norm); - - vkb::core::Buffer index_staging(get_device(), index_buffer_size, VK_BUFFER_USAGE_TRANSFER_SRC_BIT, VMA_MEMORY_USAGE_CPU_TO_GPU); - index_staging.update(indices); + vkb::core::Buffer vertex_pos_staging = vkb::core::Buffer::create_staging_buffer(get_device(), vertices_pos); + vkb::core::Buffer vertex_norm_staging = vkb::core::Buffer::create_staging_buffer(get_device(), vertices_norm); + vkb::core::Buffer index_staging = vkb::core::Buffer::create_staging_buffer(get_device(), indices); cube.vertices_pos = std::make_unique(get_device(), vertex_buffer_size, diff --git a/samples/extensions/memory_budget/memory_budget.cpp b/samples/extensions/memory_budget/memory_budget.cpp index d20d5fb709..2b97b2ef45 100644 --- a/samples/extensions/memory_budget/memory_budget.cpp +++ b/samples/extensions/memory_budget/memory_budget.cpp @@ -1,4 +1,4 @@ -/* Copyright (c) 2019-2023, Sascha Willems +/* Copyright (c) 2019-2024, Sascha Willems * Copyright (c) 2023, Holochip Corporation * * SPDX-License-Identifier: Apache-2.0 @@ -473,8 +473,7 @@ void MemoryBudget::prepare_instance_data() // On devices with separate memory types for host visible and device local memory this will result in better performance // On devices with unified memory types (DEVICE_LOCAL_BIT and HOST_VISIBLE_BIT supported at once) this isn't necessary, and you could skip the staging - vkb::core::Buffer staging_buffer(get_device(), instance_buffer.size, VK_BUFFER_USAGE_TRANSFER_SRC_BIT, VMA_MEMORY_USAGE_CPU_TO_GPU); - staging_buffer.update(instance_data); + vkb::core::Buffer staging_buffer = vkb::core::Buffer::create_staging_buffer(get_device(), instance_data); instance_buffer.buffer = std::make_unique(get_device(), instance_buffer.size, VK_BUFFER_USAGE_VERTEX_BUFFER_BIT | VK_BUFFER_USAGE_TRANSFER_DST_BIT, VMA_MEMORY_USAGE_GPU_ONLY); diff --git a/samples/extensions/shader_object/shader_object.cpp b/samples/extensions/shader_object/shader_object.cpp index 1144d1e59a..f036ede825 100644 --- a/samples/extensions/shader_object/shader_object.cpp +++ b/samples/extensions/shader_object/shader_object.cpp @@ -1,6 +1,6 @@ /* - * Copyright 2023 Nintendo - * Copyright 2023, Sascha Willems + * Copyright 2023-2024-2024 Nintendo + * Copyright 2023-2024-2024, Sascha Willems * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -900,11 +900,8 @@ void ShaderObject::generate_terrain() uint32_t index_buffer_size = index_count * sizeof(uint32_t); // Create staging buffers - vkb::core::Buffer vertex_staging(get_device(), vertex_buffer_size, VK_BUFFER_USAGE_TRANSFER_SRC_BIT, VMA_MEMORY_USAGE_CPU_TO_GPU); - vertex_staging.update(vertices); - - vkb::core::Buffer index_staging(get_device(), index_buffer_size, VK_BUFFER_USAGE_TRANSFER_SRC_BIT, VMA_MEMORY_USAGE_CPU_TO_GPU); - index_staging.update(indices); + vkb::core::Buffer vertex_staging = vkb::core::Buffer::create_staging_buffer(get_device(), vertices); + vkb::core::Buffer index_staging = vkb::core::Buffer::create_staging_buffer(get_device(), indices); terrain.vertices = std::make_unique(get_device(), vertex_buffer_size, diff --git a/samples/extensions/sparse_image/sparse_image.cpp b/samples/extensions/sparse_image/sparse_image.cpp index a3732caa9b..e3745472c7 100644 --- a/samples/extensions/sparse_image/sparse_image.cpp +++ b/samples/extensions/sparse_image/sparse_image.cpp @@ -1,4 +1,4 @@ -/* Copyright (c) 2023, Mobica Limited +/* Copyright (c) 2023-2024, Mobica Limited * * SPDX-License-Identifier: Apache-2.0 * @@ -1181,11 +1181,8 @@ void SparseImage::create_vertex_buffer() vertices[2].uv = {1.0f, 1.0f}; vertices[3].uv = {0.0f, 1.0f}; - std::unique_ptr staging_buffer; - staging_buffer = std::make_unique(get_device(), vertices_size, VK_BUFFER_USAGE_TRANSFER_SRC_BIT, VMA_MEMORY_USAGE_CPU_TO_GPU); - vertex_buffer = std::make_unique(get_device(), vertices_size, VK_BUFFER_USAGE_TRANSFER_DST_BIT | VK_BUFFER_USAGE_VERTEX_BUFFER_BIT, VMA_MEMORY_USAGE_GPU_ONLY); - - staging_buffer->update(vertices.data(), vertices_size); + auto staging_buffer = vkb::core::Buffer::create_staging_buffer(get_device(), vertices); + vertex_buffer = std::make_unique(get_device(), vertices_size, VK_BUFFER_USAGE_TRANSFER_DST_BIT | VK_BUFFER_USAGE_VERTEX_BUFFER_BIT, VMA_MEMORY_USAGE_GPU_ONLY); VkCommandBuffer command_buffer = get_device().create_command_buffer(VK_COMMAND_BUFFER_LEVEL_PRIMARY, true); @@ -1194,7 +1191,7 @@ void SparseImage::create_vertex_buffer() copy_buffer_info.dstOffset = 0U; copy_buffer_info.size = vertices_size; - vkCmdCopyBuffer(command_buffer, staging_buffer->get_handle(), vertex_buffer->get_handle(), 1U, ©_buffer_info); + vkCmdCopyBuffer(command_buffer, staging_buffer.get_handle(), vertex_buffer->get_handle(), 1U, ©_buffer_info); device->flush_command_buffer(command_buffer, queue, true); } @@ -1207,11 +1204,8 @@ void SparseImage::create_index_buffer() VkDeviceSize indices_size = sizeof(indices[0]) * indices.size(); index_count = indices.size(); - std::unique_ptr staging_buffer; - staging_buffer = std::make_unique(get_device(), indices_size, VK_BUFFER_USAGE_TRANSFER_SRC_BIT, VMA_MEMORY_USAGE_CPU_TO_GPU); - index_buffer = std::make_unique(get_device(), indices_size, VK_BUFFER_USAGE_TRANSFER_DST_BIT | VK_BUFFER_USAGE_INDEX_BUFFER_BIT, VMA_MEMORY_USAGE_GPU_ONLY); - - staging_buffer->update(indices.data(), indices_size); + auto staging_buffer = vkb::core::Buffer::create_staging_buffer(get_device(), indices); + index_buffer = std::make_unique(get_device(), indices_size, VK_BUFFER_USAGE_TRANSFER_DST_BIT | VK_BUFFER_USAGE_INDEX_BUFFER_BIT, VMA_MEMORY_USAGE_GPU_ONLY); VkCommandBuffer command_buffer = get_device().create_command_buffer(VK_COMMAND_BUFFER_LEVEL_PRIMARY, true); @@ -1220,7 +1214,7 @@ void SparseImage::create_index_buffer() copy_buffer_info.dstOffset = 0U; copy_buffer_info.size = indices_size; - vkCmdCopyBuffer(command_buffer, staging_buffer->get_handle(), index_buffer->get_handle(), 1U, ©_buffer_info); + vkCmdCopyBuffer(command_buffer, staging_buffer.get_handle(), index_buffer->get_handle(), 1U, ©_buffer_info); device->flush_command_buffer(command_buffer, queue, true); } diff --git a/samples/extensions/synchronization_2/synchronization_2.cpp b/samples/extensions/synchronization_2/synchronization_2.cpp index 3980c2cff4..5779e3c42d 100644 --- a/samples/extensions/synchronization_2/synchronization_2.cpp +++ b/samples/extensions/synchronization_2/synchronization_2.cpp @@ -1,4 +1,4 @@ -/* Copyright (c) 2021-2023, Sascha Willems +/* Copyright (c) 2021-2024, Sascha Willems * * SPDX-License-Identifier: Apache-2.0 * @@ -307,8 +307,7 @@ void Synchronization2::prepare_storage_buffers() // Staging // SSBO won't be changed on the host after upload so copy to device local memory - vkb::core::Buffer staging_buffer{get_device(), storage_buffer_size, VK_BUFFER_USAGE_TRANSFER_SRC_BIT, VMA_MEMORY_USAGE_CPU_ONLY}; - staging_buffer.update(particle_buffer.data(), static_cast(storage_buffer_size)); + vkb::core::Buffer staging_buffer = vkb::core::Buffer::create_staging_buffer(get_device(), particle_buffer); compute.storage_buffer = std::make_unique(get_device(), storage_buffer_size, diff --git a/samples/extensions/vertex_dynamic_state/vertex_dynamic_state.cpp b/samples/extensions/vertex_dynamic_state/vertex_dynamic_state.cpp index 9c0ed4e82c..927e00df2e 100644 --- a/samples/extensions/vertex_dynamic_state/vertex_dynamic_state.cpp +++ b/samples/extensions/vertex_dynamic_state/vertex_dynamic_state.cpp @@ -1,4 +1,4 @@ -/* Copyright (c) 2022-2023, Mobica Limited +/* Copyright (c) 2022-2024, Mobica Limited * * SPDX-License-Identifier: Apache-2.0 * @@ -531,11 +531,8 @@ void VertexDynamicState::model_data_creation() 3, 7, 6, 6, 2, 3}; - vkb::core::Buffer vertex_staging(get_device(), vertex_buffer_size, VK_BUFFER_USAGE_TRANSFER_SRC_BIT, VMA_MEMORY_USAGE_CPU_TO_GPU); - vertex_staging.update(vertices); - - vkb::core::Buffer index_staging(get_device(), index_buffer_size, VK_BUFFER_USAGE_TRANSFER_SRC_BIT, VMA_MEMORY_USAGE_CPU_TO_GPU); - index_staging.update(indices); + vkb::core::Buffer vertex_staging = vkb::core::Buffer::create_staging_buffer(get_device(), vertices); + vkb::core::Buffer index_staging = vkb::core::Buffer::create_staging_buffer(get_device(), indices); cube.vertices = std::make_unique(get_device(), vertex_buffer_size, diff --git a/samples/performance/16bit_arithmetic/16bit_arithmetic.cpp b/samples/performance/16bit_arithmetic/16bit_arithmetic.cpp index a7a76d22df..42b721adbf 100644 --- a/samples/performance/16bit_arithmetic/16bit_arithmetic.cpp +++ b/samples/performance/16bit_arithmetic/16bit_arithmetic.cpp @@ -1,4 +1,4 @@ -/* Copyright (c) 2020-2023, Arm Limited and Contributors +/* Copyright (c) 2020-2024, Arm Limited and Contributors * * SPDX-License-Identifier: Apache-2.0 * @@ -103,13 +103,11 @@ bool KHR16BitArithmeticSample::prepare(const vkb::ApplicationOptions &options) blob_buffer = std::make_unique(device, sizeof(initial_data_fp16), VK_BUFFER_USAGE_STORAGE_BUFFER_BIT | VK_BUFFER_USAGE_TRANSFER_DST_BIT, VMA_MEMORY_USAGE_GPU_ONLY); - auto staging_buffer = std::make_unique(device, sizeof(initial_data_fp16), VK_BUFFER_USAGE_TRANSFER_SRC_BIT, - VMA_MEMORY_USAGE_CPU_TO_GPU); - staging_buffer->update(initial_data_fp16, sizeof(initial_data_fp16)); + auto staging_buffer = vkb::core::Buffer::create_staging_buffer(device, initial_data_fp16); auto &cmd = device.request_command_buffer(); cmd.begin(VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT, VK_NULL_HANDLE); - cmd.copy_buffer(*staging_buffer, *blob_buffer, sizeof(initial_data_fp16)); + cmd.copy_buffer(staging_buffer, *blob_buffer, sizeof(initial_data_fp16)); vkb::BufferMemoryBarrier barrier; barrier.src_stage_mask = VK_PIPELINE_STAGE_TRANSFER_BIT; diff --git a/samples/performance/multi_draw_indirect/multi_draw_indirect.cpp b/samples/performance/multi_draw_indirect/multi_draw_indirect.cpp index 60d2b82eeb..48d70b262a 100644 --- a/samples/performance/multi_draw_indirect/multi_draw_indirect.cpp +++ b/samples/performance/multi_draw_indirect/multi_draw_indirect.cpp @@ -1,4 +1,4 @@ -/* Copyright (c) 2021-2023, Holochip Corporation +/* Copyright (c) 2021-2024, Holochip Corporation * * SPDX-License-Identifier: Apache-2.0 * @@ -331,10 +331,7 @@ void MultiDrawIndirect::load_scene() VK_IMAGE_TILING_OPTIMAL, 0); - const auto &data = image->get_data(); - auto data_buffer = std::make_unique(*device, data.size() * sizeof(data[0]), VK_BUFFER_USAGE_TRANSFER_SRC_BIT, VMA_MEMORY_USAGE_CPU_TO_GPU, VMA_ALLOCATION_CREATE_MAPPED_BIT, queue_families); - data_buffer->update(data.data(), data.size() * sizeof(data[0]), 0); - data_buffer->flush(); + auto data_buffer = vkb::core::Buffer::create_staging_buffer(*device, image->get_data()); auto &texture_cmd = device->get_command_pool().request_command_buffer(); texture_cmd.begin(VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT, VK_NULL_HANDLE); @@ -355,7 +352,7 @@ void MultiDrawIndirect::load_scene() region.imageExtent = image->get_extent(); region.bufferOffset = offsets[0][0]; - texture_cmd.copy_buffer_to_image(*data_buffer, *texture.image, {region}); + texture_cmd.copy_buffer_to_image(data_buffer, *texture.image, {region}); texture_cmd.end(); auto &queue = device->get_queue_by_flags(VK_QUEUE_GRAPHICS_BIT, 0); @@ -444,9 +441,9 @@ void MultiDrawIndirect::initialize_resources() } assert(vertex_buffer_size && index_buffer_size && model_buffer_size); - auto staging_vertex_buffer = std::make_unique(get_device(), vertex_buffer_size, VK_BUFFER_USAGE_TRANSFER_SRC_BIT, VMA_MEMORY_USAGE_CPU_TO_GPU); - auto staging_index_buffer = std::make_unique(get_device(), index_buffer_size, VK_BUFFER_USAGE_TRANSFER_SRC_BIT, VMA_MEMORY_USAGE_CPU_TO_GPU); - auto staging_model_buffer = std::make_unique(get_device(), model_buffer_size, VK_BUFFER_USAGE_TRANSFER_SRC_BIT, VMA_MEMORY_USAGE_CPU_TO_GPU); + auto staging_vertex_buffer = vkb::core::Buffer::create_staging_buffer(get_device(), vertex_buffer_size, nullptr); + auto staging_index_buffer = vkb::core::Buffer::create_staging_buffer(get_device(), index_buffer_size, nullptr); + auto staging_model_buffer = vkb::core::Buffer::create_staging_buffer(get_device(), model_buffer_size, nullptr); // We will store the GPU commands in the indirect call buffer constexpr auto default_indirect_flags = VK_BUFFER_USAGE_TRANSFER_SRC_BIT | VK_BUFFER_USAGE_STORAGE_BUFFER_BIT | VK_BUFFER_USAGE_INDIRECT_BUFFER_BIT | VK_BUFFER_USAGE_TRANSFER_DST_BIT; @@ -475,8 +472,8 @@ void MultiDrawIndirect::initialize_resources() for (size_t i = 0; i < models.size(); ++i) { auto &model = models[i]; - staging_vertex_buffer->update(model.vertices.data(), model.vertices.size() * sizeof(Vertex), model.vertex_buffer_offset); - staging_index_buffer->update(model.triangles.data(), model.triangles.size() * sizeof(model.triangles[0]), model.index_buffer_offset); + staging_vertex_buffer.update(model.vertices.data(), model.vertices.size() * sizeof(Vertex), model.vertex_buffer_offset); + staging_index_buffer.update(model.triangles.data(), model.triangles.size() * sizeof(model.triangles[0]), model.index_buffer_offset); GpuModelInformation model_information; model_information.bounding_sphere_center = model.bounding_sphere.center; @@ -484,12 +481,12 @@ void MultiDrawIndirect::initialize_resources() model_information.texture_index = model.texture_index; model_information.firstIndex = model.index_buffer_offset / (sizeof(model.triangles[0][0])); model_information.indexCount = static_cast(model.triangles.size()); - staging_model_buffer->update(&model_information, sizeof(GpuModelInformation), i * sizeof(GpuModelInformation)); + staging_model_buffer.update(&model_information, sizeof(GpuModelInformation), i * sizeof(GpuModelInformation)); } - staging_vertex_buffer->flush(); - staging_index_buffer->flush(); - staging_model_buffer->flush(); + staging_vertex_buffer.flush(); + staging_index_buffer.flush(); + staging_model_buffer.flush(); auto &cmd = device->request_command_buffer(); cmd.begin(VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT, VK_NULL_HANDLE); @@ -505,9 +502,9 @@ void MultiDrawIndirect::initialize_resources() cmd.buffer_memory_barrier(*output_buffer, 0, VK_WHOLE_SIZE, barrier); return output_buffer; }; - vertex_buffer = copy(*staging_vertex_buffer, VK_BUFFER_USAGE_VERTEX_BUFFER_BIT | VK_BUFFER_USAGE_STORAGE_BUFFER_BIT); - index_buffer = copy(*staging_index_buffer, VK_BUFFER_USAGE_INDEX_BUFFER_BIT); - model_information_buffer = copy(*staging_model_buffer, VK_BUFFER_USAGE_STORAGE_BUFFER_BIT | VK_BUFFER_USAGE_VERTEX_BUFFER_BIT); + vertex_buffer = copy(staging_vertex_buffer, VK_BUFFER_USAGE_VERTEX_BUFFER_BIT | VK_BUFFER_USAGE_STORAGE_BUFFER_BIT); + index_buffer = copy(staging_index_buffer, VK_BUFFER_USAGE_INDEX_BUFFER_BIT); + model_information_buffer = copy(staging_model_buffer, VK_BUFFER_USAGE_STORAGE_BUFFER_BIT | VK_BUFFER_USAGE_VERTEX_BUFFER_BIT); if (m_supports_buffer_device) { // In this sample, we use a staging buffer for the device address buffer (i.e. for device exclusive memory). diff --git a/samples/tooling/profiles/profiles.cpp b/samples/tooling/profiles/profiles.cpp index 725fe70b38..602af9703e 100644 --- a/samples/tooling/profiles/profiles.cpp +++ b/samples/tooling/profiles/profiles.cpp @@ -1,4 +1,4 @@ -/* Copyright (c) 2022-2023, Sascha Willems +/* Copyright (c) 2022-2024, Sascha Willems * * SPDX-License-Identifier: Apache-2.0 * @@ -201,10 +201,7 @@ void Profiles::generate_textures() image_view.subresourceRange.baseArrayLayer = 0; image_view.subresourceRange.layerCount = 1; - auto staging_buffer = std::make_unique(get_device(), - image_info.extent.width * image_info.extent.height * sizeof(uint32_t), - VK_BUFFER_USAGE_TRANSFER_SRC_BIT, - VMA_MEMORY_USAGE_CPU_TO_GPU); + auto staging_buffer = vkb::core::Buffer(get_device(), image_info.extent.width * image_info.extent.height * sizeof(uint32_t)); textures.resize(32); for (size_t i = 0; i < textures.size(); i++) @@ -225,7 +222,7 @@ void Profiles::generate_textures() std::default_random_engine rnd_engine(rnd_device()); std::uniform_int_distribution rnd_dist(0, 255); const size_t buffer_size = dim * dim * 4; - uint8_t *buffer = staging_buffer->map(); + uint8_t *buffer = staging_buffer.map(); for (size_t i = 0; i < dim * dim; i++) { buffer[i * 4] = static_cast(rnd_dist(rnd_engine)); @@ -234,6 +231,9 @@ void Profiles::generate_textures() buffer[i * 4 + 3] = 255; } + staging_buffer.unmap(); + staging_buffer.flush(); + auto &cmd = get_device().request_command_buffer(); cmd.begin(VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT); @@ -242,7 +242,7 @@ void Profiles::generate_textures() VkBufferImageCopy copy_info{}; copy_info.imageSubresource = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1}; copy_info.imageExtent = image_info.extent; - vkCmdCopyBufferToImage(cmd.get_handle(), staging_buffer->get_handle(), textures[i].image, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, 1, ©_info); + vkCmdCopyBufferToImage(cmd.get_handle(), staging_buffer.get_handle(), textures[i].image, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, 1, ©_info); vkb::image_layout_transition(cmd.get_handle(), textures[i].image, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL);