Alexis Hetu | 86f8bdb | 2019-01-22 12:07:24 -0500 | [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_QUERY_POOL_HPP_ |
| 16 | #define VK_QUERY_POOL_HPP_ |
| 17 | |
Ben Clayton | b03ce83 | 2019-05-21 19:56:58 +0100 | [diff] [blame] | 18 | #include "System/Synchronization.hpp" |
| 19 | |
Alexis Hetu | 86f8bdb | 2019-01-22 12:07:24 -0500 | [diff] [blame] | 20 | #include "VkObject.hpp" |
Alexis Hetu | f0aa9d5 | 2019-04-01 17:06:47 -0400 | [diff] [blame] | 21 | #include <atomic> |
| 22 | #include <condition_variable> |
Ben Clayton | caf6031 | 2019-05-21 15:31:12 +0100 | [diff] [blame] | 23 | #include <mutex> |
Alexis Hetu | 86f8bdb | 2019-01-22 12:07:24 -0500 | [diff] [blame] | 24 | |
| 25 | namespace vk |
| 26 | { |
| 27 | |
Ben Clayton | b03ce83 | 2019-05-21 19:56:58 +0100 | [diff] [blame] | 28 | class Query |
Alexis Hetu | f0aa9d5 | 2019-04-01 17:06:47 -0400 | [diff] [blame] | 29 | { |
Ben Clayton | b03ce83 | 2019-05-21 19:56:58 +0100 | [diff] [blame] | 30 | public: |
| 31 | static auto constexpr INVALID_TYPE = VK_QUERY_TYPE_MAX_ENUM; |
| 32 | |
| 33 | Query(); |
| 34 | |
Alexis Hetu | f0aa9d5 | 2019-04-01 17:06:47 -0400 | [diff] [blame] | 35 | enum State |
| 36 | { |
| 37 | UNAVAILABLE, |
| 38 | ACTIVE, |
| 39 | FINISHED |
| 40 | }; |
| 41 | |
Ben Clayton | b03ce83 | 2019-05-21 19:56:58 +0100 | [diff] [blame] | 42 | struct Data |
| 43 | { |
| 44 | State state; // The current query state. |
| 45 | int64_t value; // The current query value. |
| 46 | }; |
| 47 | |
| 48 | // reset() sets the state of the Query to UNAVAILABLE, sets the type to |
| 49 | // INVALID_TYPE and clears the query value. |
| 50 | // reset() must not be called while the query is in the ACTIVE state. |
| 51 | void reset(); |
| 52 | |
| 53 | // prepare() sets the Query type to ty, and sets the state to ACTIVE. |
| 54 | // prepare() must not be called when the query is already ACTIVE. |
| 55 | void prepare(VkQueryType ty); |
| 56 | |
| 57 | // start() begins a query task which is closed with a call to finish(). |
| 58 | // Query tasks can be nested. |
| 59 | // start() must only be called when in the ACTIVE state. |
| 60 | void start(); |
| 61 | |
| 62 | // finish() ends a query task begun with a call to start(). |
| 63 | // Once all query tasks are complete the query will transition to the |
| 64 | // FINISHED state. |
| 65 | // finish() must only be called when in the ACTIVE state. |
| 66 | void finish(); |
| 67 | |
| 68 | // wait() blocks until the query reaches the FINISHED state. |
| 69 | void wait(); |
| 70 | |
| 71 | // getData() returns the current query state and value. |
| 72 | Data getData() const; |
| 73 | |
| 74 | // getType() returns the type of query. |
| 75 | VkQueryType getType() const; |
| 76 | |
| 77 | // set() replaces the current query value with val. |
| 78 | void set(int64_t val); |
| 79 | |
| 80 | // add() adds val to the current query value. |
| 81 | void add(int64_t val); |
| 82 | |
| 83 | private: |
| 84 | sw::WaitGroup wg; |
| 85 | sw::Event finished; |
| 86 | std::atomic<State> state; |
| 87 | std::atomic<VkQueryType> type; |
| 88 | std::atomic<int64_t> value; |
Alexis Hetu | f0aa9d5 | 2019-04-01 17:06:47 -0400 | [diff] [blame] | 89 | }; |
| 90 | |
Alexis Hetu | 86f8bdb | 2019-01-22 12:07:24 -0500 | [diff] [blame] | 91 | class QueryPool : public Object<QueryPool, VkQueryPool> |
| 92 | { |
| 93 | public: |
| 94 | QueryPool(const VkQueryPoolCreateInfo* pCreateInfo, void* mem); |
Alexis Hetu | f0aa9d5 | 2019-04-01 17:06:47 -0400 | [diff] [blame] | 95 | void destroy(const VkAllocationCallbacks* pAllocator); |
Alexis Hetu | 86f8bdb | 2019-01-22 12:07:24 -0500 | [diff] [blame] | 96 | |
| 97 | static size_t ComputeRequiredAllocationSize(const VkQueryPoolCreateInfo* pCreateInfo); |
| 98 | |
Alexis Hetu | f0aa9d5 | 2019-04-01 17:06:47 -0400 | [diff] [blame] | 99 | VkResult getResults(uint32_t firstQuery, uint32_t queryCount, size_t dataSize, |
| 100 | void* pData, VkDeviceSize stride, VkQueryResultFlags flags) const; |
| 101 | void begin(uint32_t query, VkQueryControlFlags flags); |
| 102 | void end(uint32_t query); |
| 103 | void reset(uint32_t firstQuery, uint32_t queryCount); |
Ben Clayton | caf6031 | 2019-05-21 15:31:12 +0100 | [diff] [blame] | 104 | |
Alexis Hetu | f0aa9d5 | 2019-04-01 17:06:47 -0400 | [diff] [blame] | 105 | void writeTimestamp(uint32_t query); |
| 106 | |
| 107 | inline Query* getQuery(uint32_t query) const { return &(pool[query]); } |
Alexis Hetu | 86f8bdb | 2019-01-22 12:07:24 -0500 | [diff] [blame] | 108 | |
| 109 | private: |
Alexis Hetu | f0aa9d5 | 2019-04-01 17:06:47 -0400 | [diff] [blame] | 110 | Query* pool; |
| 111 | VkQueryType type; |
| 112 | uint32_t count; |
Alexis Hetu | 86f8bdb | 2019-01-22 12:07:24 -0500 | [diff] [blame] | 113 | }; |
| 114 | |
| 115 | static inline QueryPool* Cast(VkQueryPool object) |
| 116 | { |
Alexis Hetu | bd4cf81 | 2019-06-14 15:14:07 -0400 | [diff] [blame] | 117 | return QueryPool::Cast(object); |
Alexis Hetu | 86f8bdb | 2019-01-22 12:07:24 -0500 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | } // namespace vk |
| 121 | |
| 122 | #endif // VK_QUERY_POOL_HPP_ |