bdcd1abf0726b06d7526b6b3533545cca2573399
[platform/core/uifw/dali-core.git] / dali / graphics / vulkan / api / vulkan-api-buffer.cpp
1 /*
2  * Copyright (c) 2018 Samsung Electronics Co., Ltd.
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  *
16  */
17
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>
24
25 namespace Dali
26 {
27 namespace Graphics
28 {
29 namespace VulkanAPI
30 {
31
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 )
34 {
35
36 }
37
38 bool Buffer::Initialise()
39 {
40   Vulkan::Buffer::Type type;
41   if( mUsage == vk::BufferUsageFlagBits::eUniformBuffer )
42   {
43     type = Vulkan::Buffer::Type::UNIFORM;
44   }
45   else if( mUsage == vk::BufferUsageFlagBits::eIndexBuffer )
46   {
47     type = Vulkan::Buffer::Type::INDEX;
48   }
49   else if( mUsage == vk::BufferUsageFlagBits::eVertexBuffer )
50   {
51     type = Vulkan::Buffer::Type::VERTEX;
52   }
53   else
54   {
55     // unsupported usage
56     return false;
57   }
58
59   // create buffer
60   mBufferRef = Vulkan::Buffer::New( mGraphics, mSize, type );
61
62   // allocate memory
63   auto memory =
64     mGraphics.GetDeviceMemoryManager().GetDefaultAllocator().Allocate( mBufferRef,
65                       vk::MemoryPropertyFlagBits::eHostVisible ); // todo: host visible should be only for dynamic buffers
66   mBufferRef->BindMemory( memory );
67
68   return true;
69 }
70
71 void* Buffer::Map()
72 {
73   return mBufferRef->GetMemoryHandle()->Map();
74 }
75
76 void Buffer::Unmap()
77 {
78   mBufferRef->GetMemoryHandle()->Unmap();
79 }
80
81 void Buffer::Write( void* src, uint32_t srcSize, uint32_t dstOffset )
82 {
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) );
93 }
94
95 Vulkan::RefCountedBuffer Buffer::GetBufferRef() const
96 {
97   return mBufferRef;
98 }
99
100 }
101 }
102 }