Alexis Hetu | 1f23d8c | 2018-10-16 14:40:19 -0400 | [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_FENCE_HPP_ |
| 16 | #define VK_FENCE_HPP_ |
| 17 | |
| 18 | #include "VkObject.hpp" |
Ben Clayton | 6779e5e | 2019-05-20 11:07:58 +0100 | [diff] [blame] | 19 | #include "System/Synchronization.hpp" |
Alexis Hetu | 1f23d8c | 2018-10-16 14:40:19 -0400 | [diff] [blame] | 20 | |
Ben Clayton | 59f2d5e | 2019-11-13 21:46:11 +0000 | [diff] [blame] | 21 | #include "marl/containers.h" |
| 22 | #include "marl/event.h" |
| 23 | #include "marl/waitgroup.h" |
| 24 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 25 | namespace vk { |
Alexis Hetu | 1f23d8c | 2018-10-16 14:40:19 -0400 | [diff] [blame] | 26 | |
Ben Clayton | 6d33e8c | 2019-05-20 11:15:03 +0100 | [diff] [blame] | 27 | class Fence : public Object<Fence, VkFence>, public sw::TaskEvents |
Alexis Hetu | 1f23d8c | 2018-10-16 14:40:19 -0400 | [diff] [blame] | 28 | { |
| 29 | public: |
Ben Clayton | 2ed93ab | 2019-12-17 20:38:03 +0000 | [diff] [blame] | 30 | Fence(const VkFenceCreateInfo *pCreateInfo, void *mem) |
| 31 | : event(marl::Event::Mode::Manual, (pCreateInfo->flags & VK_FENCE_CREATE_SIGNALED_BIT) != 0) |
| 32 | {} |
Alexis Hetu | 1f23d8c | 2018-10-16 14:40:19 -0400 | [diff] [blame] | 33 | |
Ben Clayton | 2ed93ab | 2019-12-17 20:38:03 +0000 | [diff] [blame] | 34 | static size_t ComputeRequiredAllocationSize(const VkFenceCreateInfo *pCreateInfo) |
Alexis Hetu | 1f23d8c | 2018-10-16 14:40:19 -0400 | [diff] [blame] | 35 | { |
| 36 | return 0; |
| 37 | } |
| 38 | |
Alexis Hetu | 1f23d8c | 2018-10-16 14:40:19 -0400 | [diff] [blame] | 39 | void reset() |
| 40 | { |
Ben Clayton | 59f2d5e | 2019-11-13 21:46:11 +0000 | [diff] [blame] | 41 | event.clear(); |
Alexis Hetu | 1f23d8c | 2018-10-16 14:40:19 -0400 | [diff] [blame] | 42 | } |
| 43 | |
Alexis Hetu | e1f51b9 | 2019-04-23 15:34:34 -0400 | [diff] [blame] | 44 | VkResult getStatus() |
Alexis Hetu | 1f23d8c | 2018-10-16 14:40:19 -0400 | [diff] [blame] | 45 | { |
Ben Clayton | 59f2d5e | 2019-11-13 21:46:11 +0000 | [diff] [blame] | 46 | return event.isSignalled() ? VK_SUCCESS : VK_NOT_READY; |
Alexis Hetu | e1f51b9 | 2019-04-23 15:34:34 -0400 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | VkResult wait() |
| 50 | { |
Ben Clayton | 59f2d5e | 2019-11-13 21:46:11 +0000 | [diff] [blame] | 51 | event.wait(); |
Ben Clayton | 6779e5e | 2019-05-20 11:07:58 +0100 | [diff] [blame] | 52 | return VK_SUCCESS; |
Alexis Hetu | e1f51b9 | 2019-04-23 15:34:34 -0400 | [diff] [blame] | 53 | } |
| 54 | |
Ben Clayton | 2ed93ab | 2019-12-17 20:38:03 +0000 | [diff] [blame] | 55 | template<class CLOCK, class DURATION> |
| 56 | VkResult wait(const std::chrono::time_point<CLOCK, DURATION> &timeout) |
Alexis Hetu | e1f51b9 | 2019-04-23 15:34:34 -0400 | [diff] [blame] | 57 | { |
Ben Clayton | 59f2d5e | 2019-11-13 21:46:11 +0000 | [diff] [blame] | 58 | return event.wait_until(timeout) ? VK_SUCCESS : VK_TIMEOUT; |
Ben Clayton | 6779e5e | 2019-05-20 11:07:58 +0100 | [diff] [blame] | 59 | } |
| 60 | |
Ben Clayton | 2ed93ab | 2019-12-17 20:38:03 +0000 | [diff] [blame] | 61 | const marl::Event &getEvent() const { return event; } |
Ben Clayton | 951d48d | 2019-11-13 21:46:59 +0000 | [diff] [blame] | 62 | |
Ben Clayton | 6d33e8c | 2019-05-20 11:15:03 +0100 | [diff] [blame] | 63 | // TaskEvents compliance |
| 64 | void start() override |
Ben Clayton | 6779e5e | 2019-05-20 11:07:58 +0100 | [diff] [blame] | 65 | { |
Ben Clayton | 59f2d5e | 2019-11-13 21:46:11 +0000 | [diff] [blame] | 66 | ASSERT(!event.isSignalled()); |
Ben Clayton | 6779e5e | 2019-05-20 11:07:58 +0100 | [diff] [blame] | 67 | wg.add(); |
| 68 | } |
| 69 | |
Ben Clayton | 6d33e8c | 2019-05-20 11:15:03 +0100 | [diff] [blame] | 70 | void finish() override |
Ben Clayton | 6779e5e | 2019-05-20 11:07:58 +0100 | [diff] [blame] | 71 | { |
Ben Clayton | 59f2d5e | 2019-11-13 21:46:11 +0000 | [diff] [blame] | 72 | ASSERT(!event.isSignalled()); |
Nicolas Capens | 81bc9d9 | 2019-12-16 15:05:57 -0500 | [diff] [blame] | 73 | if(wg.done()) |
Ben Clayton | 6779e5e | 2019-05-20 11:07:58 +0100 | [diff] [blame] | 74 | { |
Ben Clayton | 59f2d5e | 2019-11-13 21:46:11 +0000 | [diff] [blame] | 75 | event.signal(); |
Ben Clayton | 6779e5e | 2019-05-20 11:07:58 +0100 | [diff] [blame] | 76 | } |
Alexis Hetu | 1f23d8c | 2018-10-16 14:40:19 -0400 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | private: |
Ben Clayton | 2ed93ab | 2019-12-17 20:38:03 +0000 | [diff] [blame] | 80 | Fence(const Fence &) = delete; |
Ben Clayton | 6779e5e | 2019-05-20 11:07:58 +0100 | [diff] [blame] | 81 | |
Ben Clayton | 59f2d5e | 2019-11-13 21:46:11 +0000 | [diff] [blame] | 82 | marl::WaitGroup wg; |
| 83 | const marl::Event event; |
Alexis Hetu | 1f23d8c | 2018-10-16 14:40:19 -0400 | [diff] [blame] | 84 | }; |
| 85 | |
Ben Clayton | 2ed93ab | 2019-12-17 20:38:03 +0000 | [diff] [blame] | 86 | static inline Fence *Cast(VkFence object) |
Alexis Hetu | 1f23d8c | 2018-10-16 14:40:19 -0400 | [diff] [blame] | 87 | { |
Alexis Hetu | bd4cf81 | 2019-06-14 15:14:07 -0400 | [diff] [blame] | 88 | return Fence::Cast(object); |
Alexis Hetu | 1f23d8c | 2018-10-16 14:40:19 -0400 | [diff] [blame] | 89 | } |
| 90 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 91 | } // namespace vk |
Alexis Hetu | 1f23d8c | 2018-10-16 14:40:19 -0400 | [diff] [blame] | 92 | |
Ben Clayton | 2ed93ab | 2019-12-17 20:38:03 +0000 | [diff] [blame] | 93 | #endif // VK_FENCE_HPP_ |