xref: /aosp_15_r20/external/swiftshader/src/Vulkan/VkQueryPool.hpp (revision 03ce13f70fcc45d86ee91b7ee4cab1936a95046e)
1*03ce13f7SAndroid Build Coastguard Worker // Copyright 2018 The SwiftShader Authors. All Rights Reserved.
2*03ce13f7SAndroid Build Coastguard Worker //
3*03ce13f7SAndroid Build Coastguard Worker // Licensed under the Apache License, Version 2.0 (the "License");
4*03ce13f7SAndroid Build Coastguard Worker // you may not use this file except in compliance with the License.
5*03ce13f7SAndroid Build Coastguard Worker // You may obtain a copy of the License at
6*03ce13f7SAndroid Build Coastguard Worker //
7*03ce13f7SAndroid Build Coastguard Worker //    http://www.apache.org/licenses/LICENSE-2.0
8*03ce13f7SAndroid Build Coastguard Worker //
9*03ce13f7SAndroid Build Coastguard Worker // Unless required by applicable law or agreed to in writing, software
10*03ce13f7SAndroid Build Coastguard Worker // distributed under the License is distributed on an "AS IS" BASIS,
11*03ce13f7SAndroid Build Coastguard Worker // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12*03ce13f7SAndroid Build Coastguard Worker // See the License for the specific language governing permissions and
13*03ce13f7SAndroid Build Coastguard Worker // limitations under the License.
14*03ce13f7SAndroid Build Coastguard Worker 
15*03ce13f7SAndroid Build Coastguard Worker #ifndef VK_QUERY_POOL_HPP_
16*03ce13f7SAndroid Build Coastguard Worker #define VK_QUERY_POOL_HPP_
17*03ce13f7SAndroid Build Coastguard Worker 
18*03ce13f7SAndroid Build Coastguard Worker #include "VkObject.hpp"
19*03ce13f7SAndroid Build Coastguard Worker 
20*03ce13f7SAndroid Build Coastguard Worker #include "marl/event.h"
21*03ce13f7SAndroid Build Coastguard Worker #include "marl/waitgroup.h"
22*03ce13f7SAndroid Build Coastguard Worker 
23*03ce13f7SAndroid Build Coastguard Worker #include <atomic>
24*03ce13f7SAndroid Build Coastguard Worker #include <condition_variable>
25*03ce13f7SAndroid Build Coastguard Worker #include <mutex>
26*03ce13f7SAndroid Build Coastguard Worker 
27*03ce13f7SAndroid Build Coastguard Worker namespace vk {
28*03ce13f7SAndroid Build Coastguard Worker 
29*03ce13f7SAndroid Build Coastguard Worker class Query
30*03ce13f7SAndroid Build Coastguard Worker {
31*03ce13f7SAndroid Build Coastguard Worker public:
32*03ce13f7SAndroid Build Coastguard Worker 	Query(VkQueryType type);
33*03ce13f7SAndroid Build Coastguard Worker 
34*03ce13f7SAndroid Build Coastguard Worker 	enum State
35*03ce13f7SAndroid Build Coastguard Worker 	{
36*03ce13f7SAndroid Build Coastguard Worker 		UNAVAILABLE,
37*03ce13f7SAndroid Build Coastguard Worker 		ACTIVE,
38*03ce13f7SAndroid Build Coastguard Worker 		FINISHED
39*03ce13f7SAndroid Build Coastguard Worker 	};
40*03ce13f7SAndroid Build Coastguard Worker 
41*03ce13f7SAndroid Build Coastguard Worker 	struct Data
42*03ce13f7SAndroid Build Coastguard Worker 	{
43*03ce13f7SAndroid Build Coastguard Worker 		State state;    // The current query state.
44*03ce13f7SAndroid Build Coastguard Worker 		int64_t value;  // The current query value.
45*03ce13f7SAndroid Build Coastguard Worker 	};
46*03ce13f7SAndroid Build Coastguard Worker 
47*03ce13f7SAndroid Build Coastguard Worker 	// reset() sets the state of the Query to UNAVAILABLE, sets the type to
48*03ce13f7SAndroid Build Coastguard Worker 	// INVALID_TYPE and clears the query value.
49*03ce13f7SAndroid Build Coastguard Worker 	// reset() must not be called while the query is in the ACTIVE state.
50*03ce13f7SAndroid Build Coastguard Worker 	void reset();
51*03ce13f7SAndroid Build Coastguard Worker 
52*03ce13f7SAndroid Build Coastguard Worker 	// start() begins a query task which is closed with a call to finish().
53*03ce13f7SAndroid Build Coastguard Worker 	// Query tasks can be nested.
54*03ce13f7SAndroid Build Coastguard Worker 	// start() sets the state to ACTIVE.
55*03ce13f7SAndroid Build Coastguard Worker 	void start();
56*03ce13f7SAndroid Build Coastguard Worker 
57*03ce13f7SAndroid Build Coastguard Worker 	// finish() ends a query task begun with a call to start().
58*03ce13f7SAndroid Build Coastguard Worker 	// Once all query tasks are complete the query will transition to the
59*03ce13f7SAndroid Build Coastguard Worker 	// FINISHED state.
60*03ce13f7SAndroid Build Coastguard Worker 	// finish() must only be called when in the ACTIVE state.
61*03ce13f7SAndroid Build Coastguard Worker 	void finish();
62*03ce13f7SAndroid Build Coastguard Worker 
63*03ce13f7SAndroid Build Coastguard Worker 	// wait() blocks until the query reaches the FINISHED state.
64*03ce13f7SAndroid Build Coastguard Worker 	void wait();
65*03ce13f7SAndroid Build Coastguard Worker 
66*03ce13f7SAndroid Build Coastguard Worker 	// getData() returns the current query state and value.
67*03ce13f7SAndroid Build Coastguard Worker 	Data getData() const;
68*03ce13f7SAndroid Build Coastguard Worker 
69*03ce13f7SAndroid Build Coastguard Worker 	// getType() returns the type of query.
70*03ce13f7SAndroid Build Coastguard Worker 	VkQueryType getType() const;
71*03ce13f7SAndroid Build Coastguard Worker 
72*03ce13f7SAndroid Build Coastguard Worker 	// set() replaces the current query value with val.
73*03ce13f7SAndroid Build Coastguard Worker 	void set(int64_t val);
74*03ce13f7SAndroid Build Coastguard Worker 
75*03ce13f7SAndroid Build Coastguard Worker 	// add() adds val to the current query value.
76*03ce13f7SAndroid Build Coastguard Worker 	void add(int64_t val);
77*03ce13f7SAndroid Build Coastguard Worker 
78*03ce13f7SAndroid Build Coastguard Worker private:
79*03ce13f7SAndroid Build Coastguard Worker 	marl::WaitGroup wg;
80*03ce13f7SAndroid Build Coastguard Worker 	marl::Event finished;
81*03ce13f7SAndroid Build Coastguard Worker 	std::atomic<State> state;
82*03ce13f7SAndroid Build Coastguard Worker 	std::atomic<VkQueryType> type;
83*03ce13f7SAndroid Build Coastguard Worker 	std::atomic<int64_t> value;
84*03ce13f7SAndroid Build Coastguard Worker };
85*03ce13f7SAndroid Build Coastguard Worker 
86*03ce13f7SAndroid Build Coastguard Worker class QueryPool : public Object<QueryPool, VkQueryPool>
87*03ce13f7SAndroid Build Coastguard Worker {
88*03ce13f7SAndroid Build Coastguard Worker public:
89*03ce13f7SAndroid Build Coastguard Worker 	QueryPool(const VkQueryPoolCreateInfo *pCreateInfo, void *mem);
90*03ce13f7SAndroid Build Coastguard Worker 	void destroy(const VkAllocationCallbacks *pAllocator);
91*03ce13f7SAndroid Build Coastguard Worker 
92*03ce13f7SAndroid Build Coastguard Worker 	static size_t ComputeRequiredAllocationSize(const VkQueryPoolCreateInfo *pCreateInfo);
93*03ce13f7SAndroid Build Coastguard Worker 
94*03ce13f7SAndroid Build Coastguard Worker 	VkResult getResults(uint32_t firstQuery, uint32_t queryCount, size_t dataSize,
95*03ce13f7SAndroid Build Coastguard Worker 	                    void *pData, VkDeviceSize stride, VkQueryResultFlags flags) const;
96*03ce13f7SAndroid Build Coastguard Worker 	void begin(uint32_t query, VkQueryControlFlags flags);
97*03ce13f7SAndroid Build Coastguard Worker 	void end(uint32_t query);
98*03ce13f7SAndroid Build Coastguard Worker 	void reset(uint32_t firstQuery, uint32_t queryCount);
99*03ce13f7SAndroid Build Coastguard Worker 
100*03ce13f7SAndroid Build Coastguard Worker 	void writeTimestamp(uint32_t query);
101*03ce13f7SAndroid Build Coastguard Worker 
getQuery(uint32_t query) const102*03ce13f7SAndroid Build Coastguard Worker 	inline Query *getQuery(uint32_t query) const { return &pool[query]; }
getType() const103*03ce13f7SAndroid Build Coastguard Worker 	inline VkQueryType getType() const { return type; }
104*03ce13f7SAndroid Build Coastguard Worker 
105*03ce13f7SAndroid Build Coastguard Worker private:
106*03ce13f7SAndroid Build Coastguard Worker 	Query *const pool;
107*03ce13f7SAndroid Build Coastguard Worker 	const VkQueryType type;
108*03ce13f7SAndroid Build Coastguard Worker 	const uint32_t count;
109*03ce13f7SAndroid Build Coastguard Worker };
110*03ce13f7SAndroid Build Coastguard Worker 
Cast(VkQueryPool object)111*03ce13f7SAndroid Build Coastguard Worker static inline QueryPool *Cast(VkQueryPool object)
112*03ce13f7SAndroid Build Coastguard Worker {
113*03ce13f7SAndroid Build Coastguard Worker 	return QueryPool::Cast(object);
114*03ce13f7SAndroid Build Coastguard Worker }
115*03ce13f7SAndroid Build Coastguard Worker 
116*03ce13f7SAndroid Build Coastguard Worker }  // namespace vk
117*03ce13f7SAndroid Build Coastguard Worker 
118*03ce13f7SAndroid Build Coastguard Worker #endif  // VK_QUERY_POOL_HPP_
119