Line data Source code
1 : /**
2 : Copyright (c) 2023 Stappler LLC <admin@stappler.dev>
3 :
4 : Permission is hereby granted, free of charge, to any person obtaining a copy
5 : of this software and associated documentation files (the "Software"), to deal
6 : in the Software without restriction, including without limitation the rights
7 : to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 : copies of the Software, and to permit persons to whom the Software is
9 : furnished to do so, subject to the following conditions:
10 :
11 : The above copyright notice and this permission notice shall be included in
12 : all copies or substantial portions of the Software.
13 :
14 : THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 : IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 : FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 : AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 : LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 : OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20 : THE SOFTWARE.
21 : **/
22 :
23 : #ifndef XENOLITH_CORE_XLCOREOBJECT_H_
24 : #define XENOLITH_CORE_XLCOREOBJECT_H_
25 :
26 : #include "XLCoreInfo.h"
27 :
28 : // check if 64-bit pointer is available for Vulkan
29 : #ifndef XL_USE_64_BIT_PTR_DEFINES
30 : #if defined(__LP64__) || defined(_WIN64) || (defined(__x86_64__) && !defined(__ILP32__) ) || defined(_M_X64) || defined(__ia64) || defined (_M_IA64) || defined(__aarch64__) || defined(__powerpc64__)
31 : #define XL_USE_64_BIT_PTR_DEFINES 1
32 : #else
33 : #define XL_USE_64_BIT_PTR_DEFINES 0
34 : #endif
35 : #endif
36 :
37 : namespace STAPPLER_VERSIONIZED stappler::xenolith::core {
38 :
39 : #if (XL_USE_64_BIT_PTR_DEFINES == 1)
40 : using ObjectHandle = ValueWrapper<void *, class ObjectHandleFlag>;
41 : #else
42 : using ObjectHandle = ValueWrapper<uint64_t, class ObjectHandleFlag>;
43 : #endif
44 :
45 : class TextureSet;
46 : class ImageView;
47 : class BufferObject;
48 : class Loop;
49 : class Device;
50 : class Attachment;
51 : class Queue;
52 :
53 : struct GraphicPipelineInfo;
54 : struct GraphicPipelineData;
55 : struct ComputePipelineInfo;
56 : struct ComputePipelineData;
57 : struct ProgramData;
58 : struct QueuePassData;
59 : struct SubpassData;
60 : struct PipelineDescriptor;
61 :
62 : struct ObjectData {
63 : using ClearCallback = void (*) (Device *, ObjectType, ObjectHandle, void *);
64 :
65 : ObjectType type;
66 : Device *device = nullptr;
67 : ClearCallback callback = nullptr;
68 : ObjectHandle handle;
69 : void *ptr = nullptr;
70 : };
71 :
72 : class Object : public NamedRef {
73 : public:
74 : using ObjectHandle = core::ObjectHandle;
75 : using ClearCallback = void (*) (Device *, ObjectType, ObjectHandle, void *);
76 :
77 : virtual ~Object();
78 :
79 : virtual bool init(Device &, ClearCallback, ObjectType, ObjectHandle ptr, void * = nullptr);
80 : void invalidate();
81 :
82 32 : const ObjectData &getObjectData() const { return _object; }
83 :
84 0 : virtual void setName(StringView str) { _name = str.str<Interface>(); }
85 1540 : virtual StringView getName() const override { return _name; }
86 :
87 : protected:
88 : ObjectData _object;
89 : String _name;
90 : };
91 :
92 :
93 : class GraphicPipeline : public Object {
94 : public:
95 : using PipelineInfo = core::GraphicPipelineInfo;
96 : using PipelineData = core::GraphicPipelineData;
97 : using SubpassData = core::SubpassData;
98 : using Queue = core::Queue;
99 :
100 84 : virtual ~GraphicPipeline() { }
101 : };
102 :
103 :
104 : class ComputePipeline : public Object {
105 : public:
106 : using PipelineInfo = core::ComputePipelineInfo;
107 : using PipelineData = core::ComputePipelineData;
108 : using SubpassData = core::SubpassData;
109 : using Queue = core::Queue;
110 :
111 1512 : virtual ~ComputePipeline() { }
112 : };
113 :
114 :
115 : class Shader : public Object {
116 : public:
117 : using ProgramData = core::ProgramData;
118 : using DescriptorType = core::DescriptorType;
119 :
120 : static String inspectShader(SpanView<uint32_t>);
121 :
122 1596 : virtual ~Shader() { }
123 :
124 0 : virtual ProgramStage getStage() const { return _stage; }
125 :
126 : protected:
127 : String inspect(SpanView<uint32_t>);
128 :
129 : ProgramStage _stage = ProgramStage::None;
130 : };
131 :
132 :
133 : class RenderPass : public Object {
134 : public:
135 : using QueuePassData = core::QueuePassData;
136 : using Attachment = core::Attachment;
137 : using PipelineDescriptor = core::PipelineDescriptor;
138 : using DescriptorType = core::DescriptorType;
139 :
140 357 : virtual ~RenderPass() { }
141 :
142 : virtual bool init(Device &, ClearCallback, ObjectType, ObjectHandle, void *) override;
143 :
144 19620 : uint64_t getIndex() const { return _index; }
145 250504 : PassType getType() const { return _type; }
146 :
147 : protected:
148 : uint64_t _index = 1; // 0 stays as special value
149 : PassType _type = PassType::Generic;
150 : };
151 :
152 : class Framebuffer : public Object {
153 : public:
154 : static uint64_t getViewHash(SpanView<Rc<ImageView>>);
155 : static uint64_t getViewHash(SpanView<uint64_t>);
156 :
157 117 : virtual ~Framebuffer() { }
158 :
159 28551 : const Extent2 &getExtent() const { return _extent; }
160 : uint32_t getLayerCount() const { return _layerCount; }
161 7322 : Extent3 getFramebufferExtent() const { return Extent3(_extent, _layerCount); }
162 14644 : const Vector<uint64_t> &getViewIds() const { return _viewIds; }
163 7322 : const Rc<RenderPass> &getRenderPass() const { return _renderPass; }
164 :
165 : uint64_t getHash() const;
166 :
167 : protected:
168 : Extent2 _extent;
169 : uint32_t _layerCount;
170 : Vector<uint64_t> _viewIds;
171 : Rc<RenderPass> _renderPass;
172 : Vector<Rc<ImageView>> _imageViews;
173 : };
174 :
175 : class DataAtlas : public Ref {
176 : public:
177 : enum Type {
178 : ImageAtlas,
179 : MeshAtlas,
180 : Custom,
181 : };
182 :
183 1064 : virtual ~DataAtlas() { }
184 :
185 : bool init(Type, uint32_t count, uint32_t objectSize, Extent2 = Extent2(0, 0));
186 : void compile();
187 :
188 : const uint8_t *getObjectByName(uint32_t) const;
189 : const uint8_t *getObjectByName(StringView) const;
190 : const uint8_t *getObjectByOrder(uint32_t) const;
191 :
192 : void addObject(uint32_t, void *);
193 : void addObject(StringView, void *);
194 :
195 : Type getType() const { return _type; }
196 : uint32_t getObjectSize() const { return _objectSize; }
197 37623 : Extent2 getImageExtent() const { return _imageExtent; }
198 :
199 : uint32_t getObjectsCount() const { return _intNames.size() + _stringNames.size(); }
200 :
201 1836 : BytesView getData() const { return _data; }
202 2509 : BytesView getIndexData() const { return _dataIndex; }
203 :
204 : void setIndexBuffer(Rc<BufferObject> &&);
205 2501 : const Rc<BufferObject> &getIndexBuffer() const { return _indexBuffer; }
206 :
207 : void setDataBuffer(Rc<BufferObject> &&);
208 2501 : const Rc<BufferObject> &getDataBuffer() const { return _dataBuffer; }
209 :
210 : protected:
211 : void makeHashIndex();
212 :
213 : Type _type = Type::Custom;
214 : uint32_t _objectSize;
215 : Extent2 _imageExtent;
216 : HashMap<uint32_t, uint32_t> _intNames;
217 : HashMap<String, uint32_t> _stringNames;
218 : Bytes _data;
219 : Bytes _dataIndex;
220 : Rc<BufferObject> _indexBuffer;
221 : Rc<BufferObject> _dataBuffer;
222 : };
223 :
224 : class ImageObject : public Object {
225 : public:
226 : virtual ~ImageObject();
227 :
228 : virtual bool init(Device &, ClearCallback, ObjectType, ObjectHandle ptr, void *p) override;
229 : virtual bool init(Device &, ClearCallback, ObjectType, ObjectHandle ptr, void *p, uint64_t idx);
230 :
231 344911 : const ImageInfoData &getInfo() const { return _info; }
232 24860 : uint64_t getIndex() const { return _index; }
233 : const Rc<DataAtlas> &getAtlas() const { return _atlas; }
234 :
235 : ImageViewInfo getViewInfo(const ImageViewInfo &) const;
236 :
237 : protected:
238 : ImageInfoData _info;
239 : Rc<DataAtlas> _atlas;
240 :
241 : uint64_t _index = 1; // 0 stays as special value
242 : };
243 :
244 : class ImageView : public Object {
245 : public:
246 : virtual ~ImageView();
247 :
248 : virtual bool init(Device &, ClearCallback, ObjectType, ObjectHandle ptr, void *p = nullptr) override;
249 :
250 : void setReleaseCallback(Function<void()> &&);
251 : void runReleaseCallback();
252 :
253 13303 : const Rc<ImageObject> &getImage() const { return _image; }
254 86 : const ImageViewInfo &getInfo() const { return _info; }
255 :
256 940 : void setLocation(uint32_t set, uint32_t desc) {
257 940 : _set = set;
258 940 : _descriptor = desc;
259 940 : }
260 :
261 : uint32_t getSet() const { return _set; }
262 : uint32_t getDescriptor() const { return _descriptor; }
263 21287 : uint64_t getIndex() const { return _index; }
264 :
265 : Extent3 getExtent() const;
266 : uint32_t getLayerCount() const;
267 :
268 : Extent3 getFramebufferExtent() const;
269 :
270 : protected:
271 : ImageViewInfo _info;
272 : Rc<ImageObject> _image;
273 :
274 : uint32_t _set = 0;
275 : uint32_t _descriptor = 0;
276 :
277 : // all ImageViews are atomically indexed for descriptor caching purpose
278 : uint64_t _index = 1; // 0 stays as special value
279 : Function<void()> _releaseCallback;
280 : };
281 :
282 : class BufferObject : public Object {
283 : public:
284 699656 : virtual ~BufferObject() { }
285 :
286 126000 : const BufferInfo &getInfo() const { return _info; }
287 2314086 : uint64_t getSize() const { return _info.size; }
288 :
289 1624 : void setLocation(uint32_t set, uint32_t desc) {
290 1624 : _set = set;
291 1624 : _descriptor = desc;
292 1624 : }
293 :
294 : uint32_t getSet() const { return _set; }
295 3378 : uint32_t getDescriptor() const { return _descriptor; }
296 :
297 : protected:
298 : BufferInfo _info;
299 :
300 : uint32_t _set = 0;
301 : uint32_t _descriptor = 0;
302 : };
303 :
304 :
305 : class Sampler : public Object {
306 : public:
307 84 : virtual ~Sampler() { }
308 :
309 : const SamplerInfo &getInfo() const { return _info; }
310 :
311 : void setIndex(uint32_t idx) { _index = idx; }
312 : uint32_t getIndex() const { return _index; }
313 :
314 : protected:
315 : uint32_t _index = 0;
316 : SamplerInfo _info;
317 : };
318 :
319 : class CommandBuffer : public Ref {
320 : public:
321 197903 : virtual ~CommandBuffer() = default;
322 :
323 : virtual void bindImage(ImageObject *);
324 : virtual void bindBuffer(BufferObject *);
325 : virtual void bindFramebuffer(Framebuffer *);
326 :
327 : protected:
328 : uint32_t _currentSubpass = 0;
329 : uint32_t _boundLayoutIndex = 0;
330 : bool _withinRenderpass = false;
331 :
332 : Set<Rc<ImageObject>> _images;
333 : Set<Rc<BufferObject>> _buffers;
334 : Set<Rc<Framebuffer>> _framebuffers;
335 : };
336 :
337 : struct MaterialImageSlot {
338 : Rc<ImageView> image;
339 : uint32_t refCount = 0;
340 : };
341 :
342 : struct MaterialBufferSlot {
343 : Rc<BufferObject> buffer;
344 : uint32_t refCount = 0;
345 : };
346 :
347 : struct MaterialLayout {
348 : Vector<MaterialImageSlot> imageSlots;
349 : uint32_t usedImageSlots = 0;
350 :
351 : Vector<MaterialBufferSlot> bufferSlots;
352 : uint32_t usedBufferSlots = 0;
353 :
354 : Rc<TextureSet> set;
355 : };
356 :
357 : class TextureSet : public Object {
358 : public:
359 1214 : virtual ~TextureSet() { }
360 :
361 : virtual void write(const MaterialLayout &);
362 :
363 : protected:
364 : uint32_t _count = 0;
365 : Vector<uint64_t> _layoutIndexes;
366 : Vector<BufferObject *> _layoutBuffers;
367 : };
368 :
369 : class Semaphore : public Object {
370 : public:
371 134374 : virtual ~Semaphore() { }
372 :
373 : void setSignaled(bool value);
374 21934 : bool isSignaled() const { return _signaled; }
375 :
376 : void setWaited(bool value);
377 220960 : bool isWaited() const { return _waited; }
378 :
379 : void setInUse(bool value, uint64_t timeline);
380 : bool isInUse() const { return _inUse; }
381 :
382 328136 : uint64_t getTimeline() const { return _timeline; }
383 :
384 : virtual bool reset();
385 :
386 : protected:
387 : uint64_t _timeline = 0;
388 : bool _signaled = false;
389 : bool _waited = false;
390 : bool _inUse = false;
391 : };
392 :
393 : }
394 :
395 : #endif /* XENOLITH_CORE_XLCOREOBJECT_H_ */
|