aboutsummaryrefslogtreecommitdiff
path: root/src/vulkan/resource.cc
blob: e90e7c19dd32b0108f159389c371a5b577685ad4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
// Copyright 2018 The Amber Authors.
//
// 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 "src/vulkan/resource.h"

#include <limits>

#include "src/make_unique.h"
#include "src/vulkan/command_buffer.h"
#include "src/vulkan/device.h"

namespace amber {
namespace vulkan {
namespace {

VkMemoryBarrier kMemoryBarrierForAll = {
    VK_STRUCTURE_TYPE_MEMORY_BARRIER, nullptr,
    VK_ACCESS_INDIRECT_COMMAND_READ_BIT | VK_ACCESS_INDEX_READ_BIT |
        VK_ACCESS_VERTEX_ATTRIBUTE_READ_BIT | VK_ACCESS_UNIFORM_READ_BIT |
        VK_ACCESS_INPUT_ATTACHMENT_READ_BIT | VK_ACCESS_SHADER_READ_BIT |
        VK_ACCESS_SHADER_WRITE_BIT | VK_ACCESS_COLOR_ATTACHMENT_READ_BIT |
        VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT |
        VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_READ_BIT |
        VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT |
        VK_ACCESS_TRANSFER_READ_BIT | VK_ACCESS_TRANSFER_WRITE_BIT |
        VK_ACCESS_HOST_READ_BIT | VK_ACCESS_HOST_WRITE_BIT,
    VK_ACCESS_INDIRECT_COMMAND_READ_BIT | VK_ACCESS_INDEX_READ_BIT |
        VK_ACCESS_VERTEX_ATTRIBUTE_READ_BIT | VK_ACCESS_UNIFORM_READ_BIT |
        VK_ACCESS_INPUT_ATTACHMENT_READ_BIT | VK_ACCESS_SHADER_READ_BIT |
        VK_ACCESS_SHADER_WRITE_BIT | VK_ACCESS_COLOR_ATTACHMENT_READ_BIT |
        VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT |
        VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_READ_BIT |
        VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT |
        VK_ACCESS_TRANSFER_READ_BIT | VK_ACCESS_TRANSFER_WRITE_BIT |
        VK_ACCESS_HOST_READ_BIT | VK_ACCESS_HOST_WRITE_BIT};

// Fill the contents of |buffer| with |values|.
template <typename T>
void SetValuesForBuffer(void* buffer, const std::vector<Value>& values) {
  T* ptr = static_cast<T*>(buffer);
  for (const auto& v : values) {
    *ptr = v.IsInteger() ? static_cast<T>(v.AsUint64())
                         : static_cast<T>(v.AsDouble());
    ++ptr;
  }
}

}  // namespace

void BufferInput::UpdateBufferWithValues(void* buffer) const {
  uint8_t* ptr = static_cast<uint8_t*>(buffer) + offset;
  switch (type) {
    case DataType::kInt8:
      SetValuesForBuffer<int8_t>(ptr, values);
      break;
    case DataType::kUint8:
      SetValuesForBuffer<uint8_t>(ptr, values);
      break;
    case DataType::kInt16:
      SetValuesForBuffer<int16_t>(ptr, values);
      break;
    case DataType::kUint16:
      SetValuesForBuffer<uint16_t>(ptr, values);
      break;
    case DataType::kInt32:
      SetValuesForBuffer<int32_t>(ptr, values);
      break;
    case DataType::kUint32:
      SetValuesForBuffer<uint32_t>(ptr, values);
      break;
    case DataType::kInt64:
      SetValuesForBuffer<int64_t>(ptr, values);
      break;
    case DataType::kUint64:
      SetValuesForBuffer<uint64_t>(ptr, values);
      break;
    case DataType::kFloat:
      SetValuesForBuffer<float>(ptr, values);
      break;
    case DataType::kDouble:
      SetValuesForBuffer<double>(ptr, values);
      break;
  }
}

Resource::Resource(Device* device, uint32_t size_in_bytes)
    : device_(device), size_in_bytes_(size_in_bytes) {}

Resource::~Resource() = default;

Result Resource::CreateVkBuffer(VkBuffer* buffer, VkBufferUsageFlags usage) {
  if (!buffer)
    return Result("Vulkan::Given VkBuffer pointer is nullptr");

  VkBufferCreateInfo buffer_info = VkBufferCreateInfo();
  buffer_info.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
  buffer_info.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
  buffer_info.size = size_in_bytes_;
  buffer_info.usage = usage;

  if (device_->GetPtrs()->vkCreateBuffer(device_->GetVkDevice(), &buffer_info,
                                         nullptr, buffer) != VK_SUCCESS) {
    return Result("Vulkan::Calling vkCreateBuffer Fail");
  }

  return {};
}

uint32_t Resource::ChooseMemory(uint32_t memory_type_bits,
                                VkMemoryPropertyFlags flags,
                                bool force_flags) {
  // Based on Vulkan spec about VkMemoryRequirements, N th bit of
  // |memory_type_bits| is 1 where N can be the proper memory type index.
  // This code is looking for the first non-zero bit whose memory type
  // VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT property. If not exists,
  // it returns the first non-zero bit.
  uint32_t first_non_zero = std::numeric_limits<uint32_t>::max();
  uint32_t memory_type_index = 0;
  while (memory_type_bits) {
    if (memory_type_bits % 2) {
      if (first_non_zero == std::numeric_limits<uint32_t>::max())
        first_non_zero = memory_type_index;

      if (device_->HasMemoryFlags(memory_type_index, flags))
        return memory_type_index;
    }

    ++memory_type_index;
    memory_type_bits >>= 1;
  }

  if (force_flags)
    return std::numeric_limits<uint32_t>::max();

  return first_non_zero;
}

const VkMemoryRequirements Resource::GetVkBufferMemoryRequirements(
    VkBuffer buffer) const {
  VkMemoryRequirements requirement;
  device_->GetPtrs()->vkGetBufferMemoryRequirements(device_->GetVkDevice(),
                                                    buffer, &requirement);
  return requirement;
}

Result Resource::AllocateAndBindMemoryToVkBuffer(VkBuffer buffer,
                                                 VkDeviceMemory* memory,
                                                 VkMemoryPropertyFlags flags,
                                                 bool force_flags,
                                                 uint32_t* memory_type_index) {
  if (memory_type_index == nullptr) {
    return Result(
        "Vulkan: Resource::AllocateAndBindMemoryToVkBuffer memory_type_index "
        "is nullptr");
  }

  *memory_type_index = 0;

  if (buffer == VK_NULL_HANDLE)
    return Result("Vulkan::Given VkBuffer is VK_NULL_HANDLE");
  if (memory == nullptr)
    return Result("Vulkan::Given VkDeviceMemory pointer is nullptr");

  auto requirement = GetVkBufferMemoryRequirements(buffer);

  *memory_type_index =
      ChooseMemory(requirement.memoryTypeBits, flags, force_flags);
  if (*memory_type_index == std::numeric_limits<uint32_t>::max())
    return Result("Vulkan::Find Proper Memory Fail");

  Result r = AllocateMemory(memory, requirement.size, *memory_type_index);
  if (!r.IsSuccess())
    return r;

  if (device_->GetPtrs()->vkBindBufferMemory(device_->GetVkDevice(), buffer,
                                             *memory, 0) != VK_SUCCESS) {
    return Result("Vulkan::Calling vkBindBufferMemory Fail");
  }

  return {};
}

Result Resource::AllocateMemory(VkDeviceMemory* memory,
                                VkDeviceSize size,
                                uint32_t memory_type_index) {
  VkMemoryAllocateInfo alloc_info = VkMemoryAllocateInfo();
  alloc_info.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
  alloc_info.allocationSize = size;
  alloc_info.memoryTypeIndex = memory_type_index;
  if (device_->GetPtrs()->vkAllocateMemory(device_->GetVkDevice(), &alloc_info,
                                           nullptr, memory) != VK_SUCCESS) {
    return Result("Vulkan::Calling vkAllocateMemory Fail");
  }

  return {};
}

Result Resource::MapMemory(VkDeviceMemory memory) {
  if (device_->GetPtrs()->vkMapMemory(device_->GetVkDevice(), memory, 0,
                                      VK_WHOLE_SIZE, 0,
                                      &memory_ptr_) != VK_SUCCESS) {
    return Result("Vulkan::Calling vkMapMemory Fail");
  }

  return {};
}

void Resource::UnMapMemory(VkDeviceMemory memory) {
  device_->GetPtrs()->vkUnmapMemory(device_->GetVkDevice(), memory);
}

void Resource::MemoryBarrier(CommandBuffer* command) {
  // TODO(jaebaek): Current memory barrier is natively implemented.
  // Update it with the following access flags:
  // (r = read, w = write)
  //
  //                                 Host           Device
  // VertexBuffer                  host w         vertex r
  //                           transfer w       transfer r
  //
  // IndexBuffer                   host w          index r
  //                           transfer w       transfer r
  //
  // FrameBuffer                   host r          color w
  //                                       depth/stencil w
  //                           transfer r       transfer w
  //
  // ReadWrite Descriptors       host r/w       shader r/w
  //                         transfer r/w     transfer r/w
  //
  // ReadOnly Descriptors          host w         shader r
  //                           transfer w       transfer r
  device_->GetPtrs()->vkCmdPipelineBarrier(
      command->GetVkCommandBuffer(), VK_PIPELINE_STAGE_ALL_COMMANDS_BIT,
      VK_PIPELINE_STAGE_ALL_COMMANDS_BIT, 0, 1, &kMemoryBarrierForAll, 0,
      nullptr, 0, nullptr);
}

}  // namespace vulkan
}  // namespace amber