Line data Source code
1 : /**
2 : Copyright (c) 2021-2022 Roman Katuntsev <sbkarr@stappler.org>
3 : Copyright (c) 2023 Stappler LLC <admin@stappler.dev>
4 :
5 : Permission is hereby granted, free of charge, to any person obtaining a copy
6 : of this software and associated documentation files (the "Software"), to deal
7 : in the Software without restriction, including without limitation the rights
8 : to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 : copies of the Software, and to permit persons to whom the Software is
10 : furnished to do so, subject to the following conditions:
11 :
12 : The above copyright notice and this permission notice shall be included in
13 : all copies or substantial portions of the Software.
14 :
15 : THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 : IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 : FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 : AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 : LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 : OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21 : THE SOFTWARE.
22 : **/
23 :
24 : #ifndef XENOLITH_BACKEND_VK_XLVKTEXTURESET_H_
25 : #define XENOLITH_BACKEND_VK_XLVKTEXTURESET_H_
26 :
27 : #include "XLVkDevice.h"
28 : #include "XLVkObject.h"
29 :
30 : namespace STAPPLER_VERSIONIZED stappler::xenolith::vk {
31 :
32 : class TextureSet;
33 : class Loop;
34 :
35 : // persistent object, part of Device
36 : class TextureSetLayout : public Ref {
37 : public:
38 : using AttachmentLayout = core::AttachmentLayout;
39 :
40 84 : virtual ~TextureSetLayout() { }
41 :
42 : bool init(Device &dev, uint32_t imageCount, uint32_t bufferLimit);
43 : void invalidate(Device &dev);
44 :
45 : bool compile(Device &dev, const Vector<VkSampler> &);
46 :
47 1277 : const uint32_t &getImageCount() const { return _imageCount; }
48 1214 : const uint32_t &getSamplersCount() const { return _samplersCount; }
49 1277 : const uint32_t &getBuffersCount() const { return _bufferCount; }
50 1235 : VkDescriptorSetLayout getLayout() const { return _layout; }
51 0 : const Rc<ImageView> &getEmptyImageView() const { return _emptyImageView; }
52 : const Rc<ImageView> &getSolidImageView() const { return _solidImageView; }
53 0 : const Rc<Buffer> &getEmptyBuffer() const { return _emptyBuffer; }
54 :
55 : Rc<TextureSet> acquireSet(Device &dev);
56 : void releaseSet(Rc<TextureSet> &&);
57 :
58 : void initDefault(Device &dev, Loop &, Function<void(bool)> &&);
59 :
60 1214 : bool isPartiallyBound() const { return _partiallyBound; }
61 :
62 : Rc<Image> getEmptyImageObject() const;
63 : Rc<Image> getSolidImageObject() const;
64 :
65 : void readImage(Device &dev, Loop &loop, const Rc<Image> &, AttachmentLayout, Function<void(const ImageInfoData &, BytesView)> &&);
66 : void readBuffer(Device &dev, Loop &loop, const Rc<Buffer> &, Function<void(const BufferInfo &, BytesView)> &&);
67 :
68 : protected:
69 : void writeDefaults(CommandBuffer &buf);
70 : void writeImageTransfer(Device &dev, CommandBuffer &buf, uint32_t, const Rc<Buffer> &, const Rc<Image> &);
71 : void writeImageRead(Device &dev, CommandBuffer &buf, uint32_t qidx, const Rc<Image> &,
72 : AttachmentLayout, const Rc<Buffer> &);
73 : void writeBufferRead(Device &dev, CommandBuffer &buf, uint32_t qidx, const Rc<Buffer> &,
74 : const Rc<Buffer> &);
75 :
76 : bool _partiallyBound = false;
77 : uint32_t _imageCount = 0;
78 : uint32_t _bufferCount = 0;
79 : uint32_t _samplersCount = 0;
80 : VkDescriptorSetLayout _layout = VK_NULL_HANDLE;
81 :
82 : Rc<Image> _emptyImage;
83 : Rc<ImageView> _emptyImageView;
84 : Rc<Image> _solidImage;
85 : Rc<ImageView> _solidImageView;
86 : Rc<Buffer> _emptyBuffer;
87 :
88 : mutable Mutex _mutex;
89 : Vector<Rc<TextureSet>> _sets;
90 : };
91 :
92 : class TextureSet : public core::TextureSet {
93 : public:
94 2428 : virtual ~TextureSet() { }
95 :
96 : bool init(Device &dev, const TextureSetLayout &);
97 :
98 4640 : VkDescriptorSet getSet() const { return _set; }
99 :
100 : virtual void write(const core::MaterialLayout &) override;
101 :
102 9517 : const Vector<ImageMemoryBarrier> &getPendingImageBarriers() const { return _pendingImageBarriers; }
103 9517 : const Vector<BufferMemoryBarrier> &getPendingBufferBarriers() const { return _pendingBufferBarriers; }
104 :
105 : void dropPendingBarriers();
106 :
107 : Device *getDevice() const;
108 :
109 : protected:
110 : using core::TextureSet::init;
111 :
112 : void writeImages(Vector<VkWriteDescriptorSet> &writes, const core::MaterialLayout &set,
113 : std::forward_list<Vector<VkDescriptorImageInfo>> &imagesList);
114 : void writeBuffers(Vector<VkWriteDescriptorSet> &writes, const core::MaterialLayout &set,
115 : std::forward_list<Vector<VkDescriptorBufferInfo>> &bufferList);
116 :
117 : bool _partiallyBound = false;
118 : const TextureSetLayout *_layout = nullptr;
119 : uint32_t _imageCount = 0;
120 : uint32_t _bufferCount = 0;
121 : VkDescriptorSet _set = VK_NULL_HANDLE;
122 : VkDescriptorPool _pool = VK_NULL_HANDLE;
123 : Vector<ImageMemoryBarrier> _pendingImageBarriers;
124 : Vector<BufferMemoryBarrier> _pendingBufferBarriers;
125 : };
126 :
127 : }
128 :
129 : #endif /* XENOLITH_BACKEND_VK_XLVKTEXTURESET_H_ */
|