Alexis Hetu | c817663 | 2019-01-22 17:01:28 -0500 | [diff] [blame] | 1 | // Copyright 2018 The SwiftShader Authors. All Rights Reserved. |
| 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 VK_DESCRIPTOR_SET_LAYOUT_HPP_ |
| 16 | #define VK_DESCRIPTOR_SET_LAYOUT_HPP_ |
| 17 | |
| 18 | #include "VkObject.hpp" |
| 19 | |
Nicolas Capens | 7d86727 | 2019-04-08 22:51:08 -0400 | [diff] [blame] | 20 | #include "Vulkan/VkSampler.hpp" |
| 21 | #include "Vulkan/VkImageView.hpp" |
| 22 | #include "Device/Sampler.hpp" |
| 23 | |
Alexis Hetu | c817663 | 2019-01-22 17:01:28 -0500 | [diff] [blame] | 24 | namespace vk |
| 25 | { |
| 26 | |
Ben Clayton | 225a130 | 2019-04-02 12:28:22 +0100 | [diff] [blame] | 27 | class DescriptorSet; |
Nicolas Capens | a94a95c | 2019-03-29 06:22:17 -0400 | [diff] [blame] | 28 | |
Nicolas Capens | 7d86727 | 2019-04-08 22:51:08 -0400 | [diff] [blame] | 29 | // TODO(b/129523279): Move to the Device or Pipeline layer. |
Nicolas Capens | bd54e07 | 2019-04-25 23:28:28 -0400 | [diff] [blame] | 30 | struct alignas(16) SampledImageDescriptor |
Nicolas Capens | 7d86727 | 2019-04-08 22:51:08 -0400 | [diff] [blame] | 31 | { |
Nicolas Capens | a94ca18 | 2019-04-24 13:42:53 -0400 | [diff] [blame] | 32 | void updateSampler(const vk::Sampler *sampler); |
| 33 | |
Nicolas Capens | 7d86727 | 2019-04-08 22:51:08 -0400 | [diff] [blame] | 34 | // TODO(b/129523279): Minimize to the data actually needed. |
Nicolas Capens | 97da782 | 2019-04-30 17:33:26 -0400 | [diff] [blame] | 35 | vk::Sampler sampler; |
Nicolas Capens | 7d86727 | 2019-04-08 22:51:08 -0400 | [diff] [blame] | 36 | |
Chris Forbes | 45f9a93 | 2019-05-08 13:30:38 -0700 | [diff] [blame] | 37 | uint32_t imageViewId; |
| 38 | VkImageViewType type; |
| 39 | VkFormat format; |
| 40 | VkComponentMapping swizzle; |
Nicolas Capens | bd54e07 | 2019-04-25 23:28:28 -0400 | [diff] [blame] | 41 | alignas(16) sw::Texture texture; |
Ben Clayton | 0264d8e | 2019-05-08 15:39:40 +0100 | [diff] [blame] | 42 | VkExtent3D extent; // Of base mip-level. |
| 43 | int arrayLayers; |
Nicolas Capens | 7d86727 | 2019-04-08 22:51:08 -0400 | [diff] [blame] | 44 | }; |
| 45 | |
Nicolas Capens | bd54e07 | 2019-04-25 23:28:28 -0400 | [diff] [blame] | 46 | struct alignas(16) StorageImageDescriptor |
Chris Forbes | 5822882 | 2019-04-17 12:51:29 -0700 | [diff] [blame] | 47 | { |
| 48 | void *ptr; |
| 49 | VkExtent3D extent; |
| 50 | int rowPitchBytes; |
| 51 | int slicePitchBytes; |
Chris Forbes | 52a3bba | 2019-05-03 15:11:41 -0700 | [diff] [blame] | 52 | int samplePitchBytes; |
Chris Forbes | 5822882 | 2019-04-17 12:51:29 -0700 | [diff] [blame] | 53 | int arrayLayers; |
Ben Clayton | 9e4bc1b | 2019-04-16 16:52:02 -0400 | [diff] [blame] | 54 | int sizeInBytes; |
Chris Forbes | 011744e | 2019-05-06 14:21:45 -0700 | [diff] [blame] | 55 | |
| 56 | void *stencilPtr; |
| 57 | int stencilRowPitchBytes; |
| 58 | int stencilSlicePitchBytes; |
| 59 | int stencilSamplePitchBytes; |
Chris Forbes | 5822882 | 2019-04-17 12:51:29 -0700 | [diff] [blame] | 60 | }; |
| 61 | |
Chris Forbes | bfbdd89 | 2019-04-27 12:11:29 -0700 | [diff] [blame] | 62 | struct alignas(16) BufferDescriptor |
| 63 | { |
| 64 | void *ptr; |
| 65 | int sizeInBytes; // intended size of the bound region -- slides along with dynamic offsets |
| 66 | int robustnessSize; // total accessible size from static offset -- does not move with dynamic offset |
| 67 | }; |
| 68 | |
Alexis Hetu | c817663 | 2019-01-22 17:01:28 -0500 | [diff] [blame] | 69 | class DescriptorSetLayout : public Object<DescriptorSetLayout, VkDescriptorSetLayout> |
| 70 | { |
| 71 | public: |
| 72 | DescriptorSetLayout(const VkDescriptorSetLayoutCreateInfo* pCreateInfo, void* mem); |
| 73 | ~DescriptorSetLayout() = delete; |
| 74 | void destroy(const VkAllocationCallbacks* pAllocator); |
| 75 | |
| 76 | static size_t ComputeRequiredAllocationSize(const VkDescriptorSetLayoutCreateInfo* pCreateInfo); |
| 77 | |
| 78 | static size_t GetDescriptorSize(VkDescriptorType type); |
Alexis Hetu | 048974f | 2019-02-15 15:28:37 -0500 | [diff] [blame] | 79 | static void WriteDescriptorSet(const VkWriteDescriptorSet& descriptorWrites); |
| 80 | static void CopyDescriptorSet(const VkCopyDescriptorSet& descriptorCopies); |
Alexis Hetu | c817663 | 2019-01-22 17:01:28 -0500 | [diff] [blame] | 81 | |
Chris Forbes | bc694e2 | 2019-04-19 15:12:49 -0700 | [diff] [blame] | 82 | static void WriteDescriptorSet(DescriptorSet *dstSet, VkDescriptorUpdateTemplateEntry const &entry, char const *src); |
| 83 | |
Alexis Hetu | 048974f | 2019-02-15 15:28:37 -0500 | [diff] [blame] | 84 | void initialize(VkDescriptorSet descriptorSet); |
Ben Clayton | 225a130 | 2019-04-02 12:28:22 +0100 | [diff] [blame] | 85 | |
| 86 | // Returns the total size of the descriptor set in bytes. |
Nicolas Capens | a94a95c | 2019-03-29 06:22:17 -0400 | [diff] [blame] | 87 | size_t getDescriptorSetAllocationSize() const; |
| 88 | |
Ben Clayton | 225a130 | 2019-04-02 12:28:22 +0100 | [diff] [blame] | 89 | // Returns the number of bindings in the descriptor set. |
| 90 | size_t getBindingCount() const; |
| 91 | |
Ben Clayton | 8c56e8d | 2019-04-25 08:24:01 +0100 | [diff] [blame] | 92 | // Returns true iff the given binding exists. |
| 93 | bool hasBinding(uint32_t binding) const; |
| 94 | |
Ben Clayton | 225a130 | 2019-04-02 12:28:22 +0100 | [diff] [blame] | 95 | // Returns the byte offset from the base address of the descriptor set for |
| 96 | // the given binding and array element within that binding. |
Alexis Hetu | 5078d48 | 2019-04-10 15:00:25 -0400 | [diff] [blame] | 97 | size_t getBindingOffset(uint32_t binding, size_t arrayElement) const; |
Ben Clayton | 225a130 | 2019-04-02 12:28:22 +0100 | [diff] [blame] | 98 | |
Chris Forbes | 0b092cd | 2019-04-19 09:02:14 -0700 | [diff] [blame] | 99 | // Returns the stride of an array of descriptors |
| 100 | size_t getBindingStride(uint32_t binding) const; |
| 101 | |
Ben Clayton | 225a130 | 2019-04-02 12:28:22 +0100 | [diff] [blame] | 102 | // Returns the number of descriptors across all bindings that are dynamic |
| 103 | // (see isBindingDynamic). |
Alexis Hetu | 5078d48 | 2019-04-10 15:00:25 -0400 | [diff] [blame] | 104 | uint32_t getDynamicDescriptorCount() const; |
Ben Clayton | 225a130 | 2019-04-02 12:28:22 +0100 | [diff] [blame] | 105 | |
| 106 | // Returns the relative offset into the pipeline's dynamic offsets array for |
| 107 | // the given binding. This offset should be added to the base offset |
| 108 | // returned by PipelineLayout::getDynamicOffsetBase() to produce the |
| 109 | // starting index for dynamic descriptors. |
Alexis Hetu | 5078d48 | 2019-04-10 15:00:25 -0400 | [diff] [blame] | 110 | uint32_t getDynamicDescriptorOffset(uint32_t binding) const; |
Ben Clayton | 225a130 | 2019-04-02 12:28:22 +0100 | [diff] [blame] | 111 | |
| 112 | // Returns true if the given binding is of type: |
| 113 | // VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC or |
| 114 | // VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC |
| 115 | bool isBindingDynamic(uint32_t binding) const; |
| 116 | |
Ben Clayton | 72438f1 | 2019-04-04 12:30:46 +0100 | [diff] [blame] | 117 | // Returns the VkDescriptorSetLayoutBinding for the given binding. |
Ben Clayton | 225a130 | 2019-04-02 12:28:22 +0100 | [diff] [blame] | 118 | VkDescriptorSetLayoutBinding const & getBindingLayout(uint32_t binding) const; |
| 119 | |
Nicolas Capens | a94a95c | 2019-03-29 06:22:17 -0400 | [diff] [blame] | 120 | uint8_t* getOffsetPointer(DescriptorSet *descriptorSet, uint32_t binding, uint32_t arrayElement, uint32_t count, size_t* typeSize) const; |
Alexis Hetu | c817663 | 2019-01-22 17:01:28 -0500 | [diff] [blame] | 121 | |
| 122 | private: |
Nicolas Capens | a94a95c | 2019-03-29 06:22:17 -0400 | [diff] [blame] | 123 | size_t getDescriptorSetDataSize() const; |
Alexis Hetu | 048974f | 2019-02-15 15:28:37 -0500 | [diff] [blame] | 124 | uint32_t getBindingIndex(uint32_t binding) const; |
Ben Clayton | 225a130 | 2019-04-02 12:28:22 +0100 | [diff] [blame] | 125 | static bool isDynamic(VkDescriptorType type); |
Alexis Hetu | 048974f | 2019-02-15 15:28:37 -0500 | [diff] [blame] | 126 | |
Alexis Hetu | c817663 | 2019-01-22 17:01:28 -0500 | [diff] [blame] | 127 | VkDescriptorSetLayoutCreateFlags flags; |
| 128 | uint32_t bindingCount; |
| 129 | VkDescriptorSetLayoutBinding* bindings; |
Alexis Hetu | 048974f | 2019-02-15 15:28:37 -0500 | [diff] [blame] | 130 | size_t* bindingOffsets; |
Alexis Hetu | c817663 | 2019-01-22 17:01:28 -0500 | [diff] [blame] | 131 | }; |
| 132 | |
| 133 | static inline DescriptorSetLayout* Cast(VkDescriptorSetLayout object) |
| 134 | { |
| 135 | return reinterpret_cast<DescriptorSetLayout*>(object); |
| 136 | } |
| 137 | |
| 138 | } // namespace vk |
| 139 | |
| 140 | #endif // VK_DESCRIPTOR_SET_LAYOUT_HPP_ |