blob: 45891b2e744e0d1e32f0f96c3ffbcac8aa29f07b [file] [log] [blame]
Alexis Hetu86f8bdb2019-01-22 12:07:24 -05001// 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 Claytonb03ce832019-05-21 19:56:58 +010018#include "System/Synchronization.hpp"
19
Alexis Hetu86f8bdb2019-01-22 12:07:24 -050020#include "VkObject.hpp"
Alexis Hetuf0aa9d52019-04-01 17:06:47 -040021#include <atomic>
22#include <condition_variable>
Ben Claytoncaf60312019-05-21 15:31:12 +010023#include <mutex>
Alexis Hetu86f8bdb2019-01-22 12:07:24 -050024
25namespace vk
26{
27
Ben Claytonb03ce832019-05-21 19:56:58 +010028class Query
Alexis Hetuf0aa9d52019-04-01 17:06:47 -040029{
Ben Claytonb03ce832019-05-21 19:56:58 +010030public:
31 static auto constexpr INVALID_TYPE = VK_QUERY_TYPE_MAX_ENUM;
32
33 Query();
34
Alexis Hetuf0aa9d52019-04-01 17:06:47 -040035 enum State
36 {
37 UNAVAILABLE,
38 ACTIVE,
39 FINISHED
40 };
41
Ben Claytonb03ce832019-05-21 19:56:58 +010042 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
83private:
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 Hetuf0aa9d52019-04-01 17:06:47 -040089};
90
Alexis Hetu86f8bdb2019-01-22 12:07:24 -050091class QueryPool : public Object<QueryPool, VkQueryPool>
92{
93public:
94 QueryPool(const VkQueryPoolCreateInfo* pCreateInfo, void* mem);
Alexis Hetuf0aa9d52019-04-01 17:06:47 -040095 void destroy(const VkAllocationCallbacks* pAllocator);
Alexis Hetu86f8bdb2019-01-22 12:07:24 -050096
97 static size_t ComputeRequiredAllocationSize(const VkQueryPoolCreateInfo* pCreateInfo);
98
Alexis Hetuf0aa9d52019-04-01 17:06:47 -040099 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 Claytoncaf60312019-05-21 15:31:12 +0100104
Alexis Hetuf0aa9d52019-04-01 17:06:47 -0400105 void writeTimestamp(uint32_t query);
106
107 inline Query* getQuery(uint32_t query) const { return &(pool[query]); }
Alexis Hetu86f8bdb2019-01-22 12:07:24 -0500108
109private:
Alexis Hetuf0aa9d52019-04-01 17:06:47 -0400110 Query* pool;
111 VkQueryType type;
112 uint32_t count;
Alexis Hetu86f8bdb2019-01-22 12:07:24 -0500113};
114
115static inline QueryPool* Cast(VkQueryPool object)
116{
Alexis Hetubd4cf812019-06-14 15:14:07 -0400117 return QueryPool::Cast(object);
Alexis Hetu86f8bdb2019-01-22 12:07:24 -0500118}
119
120} // namespace vk
121
122#endif // VK_QUERY_POOL_HPP_