Line data Source code
1 : /** 2 : Copyright (c) 2021 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_XLVKSYNC_H_ 25 : #define XENOLITH_BACKEND_VK_XLVKSYNC_H_ 26 : 27 : #include "XLVk.h" 28 : #include "XLCoreObject.h" 29 : 30 : namespace STAPPLER_VERSIONIZED stappler::xenolith::vk { 31 : 32 : class Device; 33 : class DeviceQueue; 34 : class Loop; 35 : 36 : /* VkSemaphore wrapper 37 : * 38 : * usage pattern: 39 : * - store handles in common storage 40 : * - pop one before running signal function 41 : * - run function, that signals VkSemaphore, handle should be acquired with getSemaphore() 42 : * - run function, that waits on VkSemaphore 43 : * - push Semaphore back into storage 44 : */ 45 : 46 : class Semaphore : public core::Semaphore { 47 : public: 48 : virtual ~Semaphore(); 49 : 50 : bool init(Device &); 51 : 52 166409 : VkSemaphore getSemaphore() const { return _sem; } 53 : 54 : protected: 55 : using core::Semaphore::init; 56 : 57 : VkSemaphore _sem = VK_NULL_HANDLE; 58 : }; 59 : 60 : 61 : /* VkFence wrapper 62 : * 63 : * usage pattern: 64 : * - store handles in common storage 65 : * - pop one before running signal function 66 : * - associate resources with Fence 67 : * - run function, that signals VkFence 68 : * - schedule spinner on check() 69 : * - release resources when VkFence is signaled 70 : * - push Fence back into storage when VkFence is signaled 71 : * - storage should reset() Fence on push 72 : */ 73 : 74 : class Fence : public core::Object { 75 : public: 76 : enum State { 77 : Disabled, 78 : Armed, 79 : Signaled 80 : }; 81 : 82 : virtual ~Fence(); 83 : 84 : bool init(Device &); 85 : void clear(); 86 : 87 99113 : VkFence getFence() const { return _fence; } 88 : 89 : void setFrame(Function<bool()> &&schedule, Function<void()> &&release, uint64_t f); 90 0 : uint64_t getFrame() const { return _frame; } 91 : 92 : void setScheduleCallback(Function<bool()> &&schedule); 93 : void setReleaseCallback(Function<bool()> &&release); 94 : 95 172573 : uint64_t getArmedTime() const { return _armedTime; } 96 : 97 : bool isArmed() const { return _state == Armed; } 98 : void setArmed(DeviceQueue &); 99 : void setArmed(); 100 : 101 : void setTag(StringView); 102 : StringView getTag() const { return _tag; } 103 : 104 : // function will be called and ref will be released on fence's signal 105 : void addRelease(Function<void(bool)> &&, Ref *, StringView tag); 106 : 107 : bool schedule(Loop &); 108 : 109 : bool check(Loop &, bool lockfree = true); 110 : // void reset(Loop &, Function<void(Rc<Fence> &&)> &&); 111 : 112 : void autorelease(Rc<Ref> &&); 113 : 114 : protected: 115 : using core::Object::init; 116 : 117 : void scheduleReset(Loop &); 118 : void scheduleReleaseReset(Loop &, bool s); 119 : void doRelease(bool success); 120 : 121 : struct ReleaseHandle { 122 : Function<void(bool)> callback; 123 : Rc<Ref> ref; 124 : StringView tag; 125 : }; 126 : 127 : uint64_t _frame = 0; 128 : State _state = Disabled; 129 : VkFence _fence = VK_NULL_HANDLE; 130 : Vector<ReleaseHandle> _release; 131 : Mutex _mutex; 132 : DeviceQueue *_queue = nullptr; 133 : uint64_t _armedTime = 0; 134 : StringView _tag; 135 : 136 : Function<bool()> _scheduleFn; 137 : Function<void()> _releaseFn; 138 : Vector<Rc<Ref>> _autorelease; 139 : }; 140 : 141 : } 142 : 143 : #endif /* XENOLITH_BACKEND_VK_XLVKSYNC_H_ */