blob: 7a96b445f61a446137b85dd02a549382c855974a [file] [log] [blame]
Alexis Hetuc8176632019-01-22 17:01:28 -05001// 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 Capens7d867272019-04-08 22:51:08 -040020#include "Vulkan/VkSampler.hpp"
21#include "Vulkan/VkImageView.hpp"
22#include "Device/Sampler.hpp"
23
Alexis Hetuc8176632019-01-22 17:01:28 -050024namespace vk
25{
26
Ben Clayton225a1302019-04-02 12:28:22 +010027class DescriptorSet;
Nicolas Capensa94a95c2019-03-29 06:22:17 -040028
Nicolas Capens7d867272019-04-08 22:51:08 -040029// TODO(b/129523279): Move to the Device or Pipeline layer.
Nicolas Capensbd54e072019-04-25 23:28:28 -040030struct alignas(16) SampledImageDescriptor
Nicolas Capens7d867272019-04-08 22:51:08 -040031{
Nicolas Capensa94ca182019-04-24 13:42:53 -040032 void updateSampler(const vk::Sampler *sampler);
33
Nicolas Capens7d867272019-04-08 22:51:08 -040034 // TODO(b/129523279): Minimize to the data actually needed.
Nicolas Capens97da7822019-04-30 17:33:26 -040035 vk::Sampler sampler;
Nicolas Capens7d867272019-04-08 22:51:08 -040036
Chris Forbes45f9a932019-05-08 13:30:38 -070037 uint32_t imageViewId;
38 VkImageViewType type;
39 VkFormat format;
40 VkComponentMapping swizzle;
Nicolas Capensbd54e072019-04-25 23:28:28 -040041 alignas(16) sw::Texture texture;
Ben Clayton0264d8e2019-05-08 15:39:40 +010042 VkExtent3D extent; // Of base mip-level.
43 int arrayLayers;
Nicolas Capens7d867272019-04-08 22:51:08 -040044};
45
Nicolas Capensbd54e072019-04-25 23:28:28 -040046struct alignas(16) StorageImageDescriptor
Chris Forbes58228822019-04-17 12:51:29 -070047{
48 void *ptr;
49 VkExtent3D extent;
50 int rowPitchBytes;
51 int slicePitchBytes;
Chris Forbes52a3bba2019-05-03 15:11:41 -070052 int samplePitchBytes;
Chris Forbes58228822019-04-17 12:51:29 -070053 int arrayLayers;
Ben Clayton9e4bc1b2019-04-16 16:52:02 -040054 int sizeInBytes;
Chris Forbes011744e2019-05-06 14:21:45 -070055
56 void *stencilPtr;
57 int stencilRowPitchBytes;
58 int stencilSlicePitchBytes;
59 int stencilSamplePitchBytes;
Chris Forbes58228822019-04-17 12:51:29 -070060};
61
Chris Forbesbfbdd892019-04-27 12:11:29 -070062struct 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 Hetuc8176632019-01-22 17:01:28 -050069class DescriptorSetLayout : public Object<DescriptorSetLayout, VkDescriptorSetLayout>
70{
71public:
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 Hetu048974f2019-02-15 15:28:37 -050079 static void WriteDescriptorSet(const VkWriteDescriptorSet& descriptorWrites);
80 static void CopyDescriptorSet(const VkCopyDescriptorSet& descriptorCopies);
Alexis Hetuc8176632019-01-22 17:01:28 -050081
Chris Forbesbc694e22019-04-19 15:12:49 -070082 static void WriteDescriptorSet(DescriptorSet *dstSet, VkDescriptorUpdateTemplateEntry const &entry, char const *src);
83
Alexis Hetu048974f2019-02-15 15:28:37 -050084 void initialize(VkDescriptorSet descriptorSet);
Ben Clayton225a1302019-04-02 12:28:22 +010085
86 // Returns the total size of the descriptor set in bytes.
Nicolas Capensa94a95c2019-03-29 06:22:17 -040087 size_t getDescriptorSetAllocationSize() const;
88
Ben Clayton225a1302019-04-02 12:28:22 +010089 // Returns the number of bindings in the descriptor set.
90 size_t getBindingCount() const;
91
Ben Clayton8c56e8d2019-04-25 08:24:01 +010092 // Returns true iff the given binding exists.
93 bool hasBinding(uint32_t binding) const;
94
Ben Clayton225a1302019-04-02 12:28:22 +010095 // Returns the byte offset from the base address of the descriptor set for
96 // the given binding and array element within that binding.
Alexis Hetu5078d482019-04-10 15:00:25 -040097 size_t getBindingOffset(uint32_t binding, size_t arrayElement) const;
Ben Clayton225a1302019-04-02 12:28:22 +010098
Chris Forbes0b092cd2019-04-19 09:02:14 -070099 // Returns the stride of an array of descriptors
100 size_t getBindingStride(uint32_t binding) const;
101
Ben Clayton225a1302019-04-02 12:28:22 +0100102 // Returns the number of descriptors across all bindings that are dynamic
103 // (see isBindingDynamic).
Alexis Hetu5078d482019-04-10 15:00:25 -0400104 uint32_t getDynamicDescriptorCount() const;
Ben Clayton225a1302019-04-02 12:28:22 +0100105
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 Hetu5078d482019-04-10 15:00:25 -0400110 uint32_t getDynamicDescriptorOffset(uint32_t binding) const;
Ben Clayton225a1302019-04-02 12:28:22 +0100111
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 Clayton72438f12019-04-04 12:30:46 +0100117 // Returns the VkDescriptorSetLayoutBinding for the given binding.
Ben Clayton225a1302019-04-02 12:28:22 +0100118 VkDescriptorSetLayoutBinding const & getBindingLayout(uint32_t binding) const;
119
Nicolas Capensa94a95c2019-03-29 06:22:17 -0400120 uint8_t* getOffsetPointer(DescriptorSet *descriptorSet, uint32_t binding, uint32_t arrayElement, uint32_t count, size_t* typeSize) const;
Alexis Hetuc8176632019-01-22 17:01:28 -0500121
122private:
Nicolas Capensa94a95c2019-03-29 06:22:17 -0400123 size_t getDescriptorSetDataSize() const;
Alexis Hetu048974f2019-02-15 15:28:37 -0500124 uint32_t getBindingIndex(uint32_t binding) const;
Ben Clayton225a1302019-04-02 12:28:22 +0100125 static bool isDynamic(VkDescriptorType type);
Alexis Hetu048974f2019-02-15 15:28:37 -0500126
Alexis Hetuc8176632019-01-22 17:01:28 -0500127 VkDescriptorSetLayoutCreateFlags flags;
128 uint32_t bindingCount;
129 VkDescriptorSetLayoutBinding* bindings;
Alexis Hetu048974f2019-02-15 15:28:37 -0500130 size_t* bindingOffsets;
Alexis Hetuc8176632019-01-22 17:01:28 -0500131};
132
133static 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_