blob: 9c7d246caf020da64d2a2b1a6d552a24711cb057 [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
18#include "VkObject.hpp"
Ben Clayton0fde10b2019-11-14 00:21:59 +000019
20#include "marl/event.h"
21#include "marl/waitgroup.h"
22
Alexis Hetuf0aa9d52019-04-01 17:06:47 -040023#include <atomic>
24#include <condition_variable>
Ben Claytoncaf60312019-05-21 15:31:12 +010025#include <mutex>
Alexis Hetu86f8bdb2019-01-22 12:07:24 -050026
Nicolas Capens157ba262019-12-10 17:49:14 -050027namespace vk {
Alexis Hetu86f8bdb2019-01-22 12:07:24 -050028
Ben Claytonb03ce832019-05-21 19:56:58 +010029class Query
Alexis Hetuf0aa9d52019-04-01 17:06:47 -040030{
Ben Claytonb03ce832019-05-21 19:56:58 +010031public:
32 static auto constexpr INVALID_TYPE = VK_QUERY_TYPE_MAX_ENUM;
33
34 Query();
35
Alexis Hetuf0aa9d52019-04-01 17:06:47 -040036 enum State
37 {
38 UNAVAILABLE,
39 ACTIVE,
40 FINISHED
41 };
42
Ben Claytonb03ce832019-05-21 19:56:58 +010043 struct Data
44 {
Ben Clayton2ed93ab2019-12-17 20:38:03 +000045 State state; // The current query state.
46 int64_t value; // The current query value.
Ben Claytonb03ce832019-05-21 19:56:58 +010047 };
48
49 // reset() sets the state of the Query to UNAVAILABLE, sets the type to
50 // INVALID_TYPE and clears the query value.
51 // reset() must not be called while the query is in the ACTIVE state.
52 void reset();
53
54 // prepare() sets the Query type to ty, and sets the state to ACTIVE.
55 // prepare() must not be called when the query is already ACTIVE.
56 void prepare(VkQueryType ty);
57
58 // start() begins a query task which is closed with a call to finish().
59 // Query tasks can be nested.
60 // start() must only be called when in the ACTIVE state.
61 void start();
62
63 // finish() ends a query task begun with a call to start().
64 // Once all query tasks are complete the query will transition to the
65 // FINISHED state.
66 // finish() must only be called when in the ACTIVE state.
67 void finish();
68
69 // wait() blocks until the query reaches the FINISHED state.
70 void wait();
71
72 // getData() returns the current query state and value.
73 Data getData() const;
74
75 // getType() returns the type of query.
76 VkQueryType getType() const;
77
78 // set() replaces the current query value with val.
79 void set(int64_t val);
80
81 // add() adds val to the current query value.
82 void add(int64_t val);
83
84private:
Ben Clayton0fde10b2019-11-14 00:21:59 +000085 marl::WaitGroup wg;
86 marl::Event finished;
Ben Claytonb03ce832019-05-21 19:56:58 +010087 std::atomic<State> state;
88 std::atomic<VkQueryType> type;
89 std::atomic<int64_t> value;
Alexis Hetuf0aa9d52019-04-01 17:06:47 -040090};
91
Alexis Hetu86f8bdb2019-01-22 12:07:24 -050092class QueryPool : public Object<QueryPool, VkQueryPool>
93{
94public:
Ben Clayton2ed93ab2019-12-17 20:38:03 +000095 QueryPool(const VkQueryPoolCreateInfo *pCreateInfo, void *mem);
96 void destroy(const VkAllocationCallbacks *pAllocator);
Alexis Hetu86f8bdb2019-01-22 12:07:24 -050097
Ben Clayton2ed93ab2019-12-17 20:38:03 +000098 static size_t ComputeRequiredAllocationSize(const VkQueryPoolCreateInfo *pCreateInfo);
Alexis Hetu86f8bdb2019-01-22 12:07:24 -050099
Alexis Hetuf0aa9d52019-04-01 17:06:47 -0400100 VkResult getResults(uint32_t firstQuery, uint32_t queryCount, size_t dataSize,
Ben Clayton2ed93ab2019-12-17 20:38:03 +0000101 void *pData, VkDeviceSize stride, VkQueryResultFlags flags) const;
Alexis Hetuf0aa9d52019-04-01 17:06:47 -0400102 void begin(uint32_t query, VkQueryControlFlags flags);
103 void end(uint32_t query);
104 void reset(uint32_t firstQuery, uint32_t queryCount);
Ben Claytoncaf60312019-05-21 15:31:12 +0100105
Alexis Hetuf0aa9d52019-04-01 17:06:47 -0400106 void writeTimestamp(uint32_t query);
107
Ben Clayton2ed93ab2019-12-17 20:38:03 +0000108 inline Query *getQuery(uint32_t query) const { return &(pool[query]); }
Alexis Hetu86f8bdb2019-01-22 12:07:24 -0500109
110private:
Ben Clayton2ed93ab2019-12-17 20:38:03 +0000111 Query *pool;
Alexis Hetuf0aa9d52019-04-01 17:06:47 -0400112 VkQueryType type;
113 uint32_t count;
Alexis Hetu86f8bdb2019-01-22 12:07:24 -0500114};
115
Ben Clayton2ed93ab2019-12-17 20:38:03 +0000116static inline QueryPool *Cast(VkQueryPool object)
Alexis Hetu86f8bdb2019-01-22 12:07:24 -0500117{
Alexis Hetubd4cf812019-06-14 15:14:07 -0400118 return QueryPool::Cast(object);
Alexis Hetu86f8bdb2019-01-22 12:07:24 -0500119}
120
Nicolas Capens157ba262019-12-10 17:49:14 -0500121} // namespace vk
Alexis Hetu86f8bdb2019-01-22 12:07:24 -0500122
Ben Clayton2ed93ab2019-12-17 20:38:03 +0000123#endif // VK_QUERY_POOL_HPP_