• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2019 The Amber 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 "src/vulkan/index_buffer.h"
16 
17 #include <cstring>
18 
19 #include "src/make_unique.h"
20 #include "src/vulkan/command_buffer.h"
21 #include "src/vulkan/device.h"
22 
23 namespace amber {
24 namespace vulkan {
25 
IndexBuffer(Device * device)26 IndexBuffer::IndexBuffer(Device* device) : device_(device) {}
27 
28 IndexBuffer::~IndexBuffer() = default;
29 
SendIndexData(CommandBuffer * command,Buffer * buffer)30 Result IndexBuffer::SendIndexData(CommandBuffer* command, Buffer* buffer) {
31   if (transfer_buffer_) {
32     return Result(
33         "IndexBuffer::SendIndexData must be called once when it is created");
34   }
35 
36   if (buffer->ElementCount() == 0)
37     return Result("IndexBuffer::SendIndexData |buffer| is empty");
38 
39   transfer_buffer_ =
40       MakeUnique<TransferBuffer>(device_, buffer->GetSizeInBytes(), nullptr);
41   Result r = transfer_buffer_->Initialize(VK_BUFFER_USAGE_INDEX_BUFFER_BIT |
42                                           VK_BUFFER_USAGE_TRANSFER_DST_BIT);
43   if (!r.IsSuccess())
44     return r;
45 
46   std::memcpy(transfer_buffer_->HostAccessibleMemoryPtr(),
47               buffer->ValuePtr()->data(), buffer->GetSizeInBytes());
48 
49   transfer_buffer_->CopyToDevice(command);
50   return {};
51 }
52 
BindToCommandBuffer(CommandBuffer * command)53 Result IndexBuffer::BindToCommandBuffer(CommandBuffer* command) {
54   if (!transfer_buffer_) {
55     return Result(
56         "IndexBuffer::BindToCommandBuffer |transfer_buffer_| is nullptr");
57   }
58 
59   device_->GetPtrs()->vkCmdBindIndexBuffer(command->GetVkCommandBuffer(),
60                                            transfer_buffer_->GetVkBuffer(), 0,
61                                            VK_INDEX_TYPE_UINT32);
62   return {};
63 }
64 
65 }  // namespace vulkan
66 }  // namespace amber
67