• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2018 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 #ifndef SRC_VULKAN_BUFFER_DESCRIPTOR_H_
16 #define SRC_VULKAN_BUFFER_DESCRIPTOR_H_
17 
18 #include <memory>
19 #include <vector>
20 
21 #include "amber/result.h"
22 #include "amber/value.h"
23 #include "amber/vulkan_header.h"
24 #include "src/buffer.h"
25 #include "src/engine.h"
26 #include "src/vulkan/transfer_buffer.h"
27 
28 namespace amber {
29 namespace vulkan {
30 
31 class CommandBuffer;
32 class Device;
33 
34 enum class DescriptorType : uint8_t {
35   kStorageBuffer = 0,
36   kUniformBuffer,
37 };
38 
39 /// Stores descriptor set and binding information for storage and uniform
40 /// buffers.
41 class BufferDescriptor {
42  public:
43   BufferDescriptor(Buffer* buffer,
44                    DescriptorType type,
45                    Device* device,
46                    uint32_t desc_set,
47                    uint32_t binding);
48   ~BufferDescriptor();
49 
GetDescriptorSet()50   uint32_t GetDescriptorSet() const { return descriptor_set_; }
GetBinding()51   uint32_t GetBinding() const { return binding_; }
52 
53   VkDescriptorType GetVkDescriptorType() const;
54 
IsStorageBuffer()55   bool IsStorageBuffer() const {
56     return type_ == DescriptorType::kStorageBuffer;
57   }
IsUniformBuffer()58   bool IsUniformBuffer() const {
59     return type_ == DescriptorType::kUniformBuffer;
60   }
61 
62   Result CreateResourceIfNeeded();
63   void RecordCopyDataToResourceIfNeeded(CommandBuffer* command);
64   Result RecordCopyDataToHost(CommandBuffer* command);
65   Result MoveResourceToBufferOutput();
66   void UpdateDescriptorSetIfNeeded(VkDescriptorSet descriptor_set);
67 
68   Result SetSizeInElements(uint32_t element_count);
69   Result AddToBuffer(const std::vector<Value>& values, uint32_t offset);
70 
71  private:
72   Device* device_ = nullptr;
73   Buffer* amber_buffer_ = nullptr;
74   std::unique_ptr<TransferBuffer> transfer_buffer_;
75 
76   DescriptorType type_ = DescriptorType::kStorageBuffer;
77 
78   bool is_descriptor_set_update_needed_ = false;
79   uint32_t descriptor_set_ = 0;
80   uint32_t binding_ = 0;
81 };
82 
83 }  // namespace vulkan
84 }  // namespace amber
85 
86 #endif  // SRC_VULKAN_BUFFER_DESCRIPTOR_H_
87