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 #ifndef SRC_VULKAN_DESCRIPTOR_H_ 16 #define SRC_VULKAN_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/resource.h" 27 28 namespace amber { 29 namespace vulkan { 30 31 class CommandBuffer; 32 class Device; 33 class BufferDescriptor; 34 class ImageDescriptor; 35 class BufferBackedDescriptor; 36 class SamplerDescriptor; 37 38 enum class DescriptorType : uint8_t { 39 kStorageBuffer = 0, 40 kStorageBufferDynamic, 41 kUniformBuffer, 42 kUniformBufferDynamic, 43 kStorageImage, 44 kSampledImage, 45 kCombinedImageSampler, 46 kUniformTexelBuffer, 47 kStorageTexelBuffer, 48 kSampler 49 }; 50 51 class Descriptor { 52 public: 53 Descriptor(DescriptorType type, 54 Device* device, 55 uint32_t desc_set, 56 uint32_t binding); 57 virtual ~Descriptor(); 58 59 virtual void UpdateDescriptorSetIfNeeded(VkDescriptorSet descriptor_set) = 0; 60 virtual Result CreateResourceIfNeeded() = 0; GetDescriptorCount()61 virtual uint32_t GetDescriptorCount() { return 1; } GetDynamicOffsets()62 virtual std::vector<uint32_t> GetDynamicOffsets() { return {}; } GetDescriptorOffsets()63 virtual std::vector<VkDeviceSize> GetDescriptorOffsets() { return {}; } GetDescriptorRanges()64 virtual std::vector<VkDeviceSize> GetDescriptorRanges() { return {}; } AsBufferDescriptor()65 virtual BufferDescriptor* AsBufferDescriptor() { return nullptr; } AsImageDescriptor()66 virtual ImageDescriptor* AsImageDescriptor() { return nullptr; } AsBufferBackedDescriptor()67 virtual BufferBackedDescriptor* AsBufferBackedDescriptor() { return nullptr; } AsSamplerDescriptor()68 virtual SamplerDescriptor* AsSamplerDescriptor() { return nullptr; } GetDescriptorSet()69 uint32_t GetDescriptorSet() const { return descriptor_set_; } GetBinding()70 uint32_t GetBinding() const { return binding_; } 71 VkDescriptorType GetVkDescriptorType() const; GetDescriptorType()72 DescriptorType GetDescriptorType() const { return type_; } 73 IsStorageBuffer()74 bool IsStorageBuffer() const { 75 return type_ == DescriptorType::kStorageBuffer; 76 } IsStorageBufferDynamic()77 bool IsStorageBufferDynamic() const { 78 return type_ == DescriptorType::kStorageBufferDynamic; 79 } IsUniformBuffer()80 bool IsUniformBuffer() const { 81 return type_ == DescriptorType::kUniformBuffer; 82 } IsUniformBufferDynamic()83 bool IsUniformBufferDynamic() const { 84 return type_ == DescriptorType::kUniformBufferDynamic; 85 } IsUniformTexelBuffer()86 bool IsUniformTexelBuffer() const { 87 return type_ == DescriptorType::kUniformTexelBuffer; 88 } IsStorageTexelBuffer()89 bool IsStorageTexelBuffer() const { 90 return type_ == DescriptorType::kStorageTexelBuffer; 91 } 92 93 protected: 94 Device* device_ = nullptr; 95 DescriptorType type_ = DescriptorType::kStorageBuffer; 96 uint32_t descriptor_set_ = 0; 97 uint32_t binding_ = 0; 98 bool is_descriptor_set_update_needed_ = false; 99 }; 100 101 } // namespace vulkan 102 } // namespace amber 103 104 #endif // SRC_VULKAN_DESCRIPTOR_H_ 105