aboutsummaryrefslogtreecommitdiff
path: root/src/vulkan/resource.h
blob: d3cc0deeb544736e78bed6a2c67d06549789e3c3 (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
// 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.

#ifndef SRC_VULKAN_RESOURCE_H_
#define SRC_VULKAN_RESOURCE_H_

#include <memory>
#include <vector>

#include "amber/result.h"
#include "amber/value.h"
#include "amber/vulkan_header.h"

namespace amber {
namespace vulkan {

class CommandBuffer;
class Device;
class TransferBuffer;
class TransferImage;

// Class for Vulkan resources. Its children are Vulkan Buffer and Vulkan Image.
class Resource {
 public:
  virtual ~Resource();

  /// Records a command on |command_buffer| to copy the buffer contents from the
  /// host to the device.
  virtual void CopyToDevice(CommandBuffer* command_buffer) = 0;
  /// Records a command on |command_buffer| to copy the buffer contents from the
  /// device to the host.
  virtual void CopyToHost(CommandBuffer* command_buffer) = 0;

  void* HostAccessibleMemoryPtr() const { return memory_ptr_; }

  uint32_t GetSizeInBytes() const { return size_in_bytes_; }
  void UpdateMemoryWithRawData(const std::vector<uint8_t>& raw_data);

  bool IsReadOnly() const { return is_read_only_; }
  void SetReadOnly(bool read_only) { is_read_only_ = read_only; }
  virtual Result Initialize() = 0;
  virtual TransferBuffer* AsTransferBuffer() { return nullptr; }
  virtual TransferImage* AsTransferImage() { return nullptr; }

 protected:
  Resource(Device* device, uint32_t size);
  Result CreateVkBuffer(VkBuffer* buffer, VkBufferUsageFlags usage);

  Result AllocateAndBindMemoryToVkBuffer(VkBuffer buffer,
                                         VkDeviceMemory* memory,
                                         VkMemoryPropertyFlags flags,
                                         bool force_flags,
                                         uint32_t* memory_type_index);

  Result MapMemory(VkDeviceMemory memory);
  void UnMapMemory(VkDeviceMemory memory);
  void SetMemoryPtr(void* ptr) { memory_ptr_ = ptr; }

  /// Records a memory barrier on |command_buffer|, to ensure prior writes to
  /// this buffer have completed and are available to subsequent commands.
  void MemoryBarrier(CommandBuffer* command_buffer);

  /// Returns a memory index for the given Vulkan device, for a memory type
  /// which has the given |flags| set. If no memory is found with the given
  /// |flags| set then the first non-zero memory index is returned. If
  /// |require_flags_found| is true then if no memory is found with the given
  /// |flags| then the maximum uint32_t value is returned instead of the
  /// first non-zero memory index.
  uint32_t ChooseMemory(uint32_t memory_type_bits,
                        VkMemoryPropertyFlags flags,
                        bool require_flags_found);
  Result AllocateMemory(VkDeviceMemory* memory,
                        VkDeviceSize size,
                        uint32_t memory_type_index);

  Device* device_ = nullptr;

 private:
  uint32_t size_in_bytes_ = 0;
  void* memory_ptr_ = nullptr;
  bool is_read_only_ = false;
};

}  // namespace vulkan
}  // namespace amber

#endif  // SRC_VULKAN_RESOURCE_H_