2 * Copyright (c) 2018 Samsung Electronics Co., Ltd.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
18 #include <dali/graphics/vulkan/api/vulkan-api-buffer.h>
19 #include <dali/graphics/vulkan/vulkan-graphics.h>
20 #include <dali/graphics/vulkan/vulkan-buffer.h>
21 #include <dali/graphics/vulkan/gpu-memory/vulkan-gpu-memory-allocator.h>
22 #include <dali/graphics/vulkan/gpu-memory/vulkan-gpu-memory-manager.h>
23 #include <dali/graphics/vulkan/api/vulkan-api-controller.h>
32 Buffer::Buffer( Controller& controller, vk::BufferUsageFlagBits usage, API::Buffer::UsageHint usageHints, uint32_t size )
33 : mController( controller ), mGraphics( controller.GetGraphics() ), mUsageHints( usageHints ), mSize( size )
38 bool Buffer::Initialise()
40 Vulkan::Buffer::Type type;
41 if( mUsage == vk::BufferUsageFlagBits::eUniformBuffer )
43 type = Vulkan::Buffer::Type::UNIFORM;
45 else if( mUsage == vk::BufferUsageFlagBits::eIndexBuffer )
47 type = Vulkan::Buffer::Type::INDEX;
49 else if( mUsage == vk::BufferUsageFlagBits::eVertexBuffer )
51 type = Vulkan::Buffer::Type::VERTEX;
60 mBufferRef = Vulkan::Buffer::New( mGraphics, mSize, type );
64 mGraphics.GetDeviceMemoryManager().GetDefaultAllocator().Allocate( mBufferRef,
65 vk::MemoryPropertyFlagBits::eHostVisible ); // todo: host visible should be only for dynamic buffers
66 mBufferRef->BindMemory( memory );
73 return mBufferRef->GetMemoryHandle()->Map();
78 mBufferRef->GetMemoryHandle()->Unmap();
81 void Buffer::Write( void* src, uint32_t srcSize, uint32_t dstOffset )
83 // depends whether the buffer is host visible or device local
84 // TODO: implement in-GPU copying, for now all buffers are host-visible
85 auto transfer = std::make_unique<VulkanAPI::BufferMemoryTransfer>();
86 auto tmp = new char[srcSize];
87 memcpy( tmp, src, srcSize );
88 transfer->srcPtr.reset( tmp );
89 transfer->dstBuffer = mBufferRef;
90 transfer->dstOffset = dstOffset;
91 transfer->srcSize = srcSize;
92 mController.ScheduleBufferMemoryTransfer( std::move(transfer) );
95 Vulkan::RefCountedBuffer Buffer::GetBufferRef() const