1 // Copyright 2018 The Dawn Authors 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 #include "dawn_native/vulkan/StagingBufferVk.h" 16 #include "dawn_native/vulkan/DeviceVk.h" 17 #include "dawn_native/vulkan/FencedDeleter.h" 18 #include "dawn_native/vulkan/ResourceHeapVk.h" 19 #include "dawn_native/vulkan/ResourceMemoryAllocatorVk.h" 20 #include "dawn_native/vulkan/UtilsVulkan.h" 21 #include "dawn_native/vulkan/VulkanError.h" 22 23 namespace dawn_native { namespace vulkan { 24 StagingBuffer(size_t size,Device * device)25 StagingBuffer::StagingBuffer(size_t size, Device* device) 26 : StagingBufferBase(size), mDevice(device) { 27 } 28 Initialize()29 MaybeError StagingBuffer::Initialize() { 30 VkBufferCreateInfo createInfo; 31 createInfo.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO; 32 createInfo.pNext = nullptr; 33 createInfo.flags = 0; 34 createInfo.size = GetSize(); 35 createInfo.usage = VK_BUFFER_USAGE_TRANSFER_SRC_BIT; 36 createInfo.sharingMode = VK_SHARING_MODE_EXCLUSIVE; 37 createInfo.queueFamilyIndexCount = 0; 38 createInfo.pQueueFamilyIndices = 0; 39 40 DAWN_TRY(CheckVkSuccess( 41 mDevice->fn.CreateBuffer(mDevice->GetVkDevice(), &createInfo, nullptr, &*mBuffer), 42 "vkCreateBuffer")); 43 44 VkMemoryRequirements requirements; 45 mDevice->fn.GetBufferMemoryRequirements(mDevice->GetVkDevice(), mBuffer, &requirements); 46 47 DAWN_TRY_ASSIGN(mAllocation, mDevice->GetResourceMemoryAllocator()->Allocate( 48 requirements, MemoryKind::LinearMappable)); 49 50 DAWN_TRY(CheckVkSuccess( 51 mDevice->fn.BindBufferMemory(mDevice->GetVkDevice(), mBuffer, 52 ToBackend(mAllocation.GetResourceHeap())->GetMemory(), 53 mAllocation.GetOffset()), 54 "vkBindBufferMemory")); 55 56 mMappedPointer = mAllocation.GetMappedPointer(); 57 if (mMappedPointer == nullptr) { 58 return DAWN_INTERNAL_ERROR("Unable to map staging buffer."); 59 } 60 61 SetDebugName(mDevice, VK_OBJECT_TYPE_BUFFER, reinterpret_cast<uint64_t&>(mBuffer), 62 "Dawn_StagingBuffer"); 63 64 return {}; 65 } 66 ~StagingBuffer()67 StagingBuffer::~StagingBuffer() { 68 mMappedPointer = nullptr; 69 mDevice->GetFencedDeleter()->DeleteWhenUnused(mBuffer); 70 mDevice->GetResourceMemoryAllocator()->Deallocate(&mAllocation); 71 } 72 GetBufferHandle() const73 VkBuffer StagingBuffer::GetBufferHandle() const { 74 return mBuffer; 75 } 76 77 }} // namespace dawn_native::vulkan 78