Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 1 | // |
Shahbaz Youssefi | 563fbaa | 2018-10-02 11:22:01 -0400 | [diff] [blame] | 2 | // Copyright 2016-2018 The ANGLE Project Authors. All rights reserved. |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 3 | // Use of this source code is governed by a BSD-style license that can be |
| 4 | // found in the LICENSE file. |
| 5 | // |
| 6 | // QueryVk.cpp: |
| 7 | // Implements the class methods for QueryVk. |
| 8 | // |
| 9 | |
| 10 | #include "libANGLE/renderer/vulkan/QueryVk.h" |
Shahbaz Youssefi | 563fbaa | 2018-10-02 11:22:01 -0400 | [diff] [blame] | 11 | #include "libANGLE/Context.h" |
| 12 | #include "libANGLE/renderer/vulkan/ContextVk.h" |
Shahbaz Youssefi | c4765aa | 2018-10-12 14:40:29 -0400 | [diff] [blame] | 13 | #include "libANGLE/renderer/vulkan/RendererVk.h" |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 14 | |
| 15 | #include "common/debug.h" |
| 16 | |
| 17 | namespace rx |
| 18 | { |
| 19 | |
Shahbaz Youssefi | 563fbaa | 2018-10-02 11:22:01 -0400 | [diff] [blame] | 20 | QueryVk::QueryVk(gl::QueryType type) : QueryImpl(type), mCachedResult(0), mCachedResultValid(false) |
Jamie Madill | b980c56 | 2018-11-27 11:34:27 -0500 | [diff] [blame] | 21 | {} |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 22 | |
Shahbaz Youssefi | 563fbaa | 2018-10-02 11:22:01 -0400 | [diff] [blame] | 23 | QueryVk::~QueryVk() = default; |
| 24 | |
Jamie Madill | f4a789f | 2018-10-18 16:56:20 -0400 | [diff] [blame] | 25 | void QueryVk::onDestroy(const gl::Context *context) |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 26 | { |
Jamie Madill | e4634a1 | 2018-11-14 09:54:35 -0500 | [diff] [blame] | 27 | ContextVk *contextVk = vk::GetImpl(context); |
Jamie Madill | f4a789f | 2018-10-18 16:56:20 -0400 | [diff] [blame] | 28 | vk::DynamicQueryPool *queryPool = contextVk->getQueryPool(getType()); |
| 29 | queryPool->freeQuery(contextVk, &mQueryHelper); |
| 30 | queryPool->freeQuery(contextVk, &mQueryHelperTimeElapsedBegin); |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 31 | } |
| 32 | |
Jamie Madill | f4a789f | 2018-10-18 16:56:20 -0400 | [diff] [blame] | 33 | angle::Result QueryVk::begin(const gl::Context *context) |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 34 | { |
Shahbaz Youssefi | 563fbaa | 2018-10-02 11:22:01 -0400 | [diff] [blame] | 35 | ContextVk *contextVk = vk::GetImpl(context); |
| 36 | |
Shahbaz Youssefi | 563fbaa | 2018-10-02 11:22:01 -0400 | [diff] [blame] | 37 | mCachedResultValid = false; |
| 38 | |
Shahbaz Youssefi | c2b576d | 2018-10-12 14:45:34 -0400 | [diff] [blame] | 39 | if (!mQueryHelper.getQueryPool()) |
| 40 | { |
| 41 | ANGLE_TRY(contextVk->getQueryPool(getType())->allocateQuery(contextVk, &mQueryHelper)); |
| 42 | } |
| 43 | |
| 44 | // Note: TimeElapsed is implemented by using two Timestamp queries and taking the diff. |
| 45 | if (getType() == gl::QueryType::TimeElapsed) |
| 46 | { |
| 47 | if (!mQueryHelperTimeElapsedBegin.getQueryPool()) |
| 48 | { |
| 49 | ANGLE_TRY(contextVk->getQueryPool(getType())->allocateQuery( |
| 50 | contextVk, &mQueryHelperTimeElapsedBegin)); |
| 51 | } |
| 52 | |
Shahbaz Youssefi | c81e7bf | 2019-01-18 15:35:55 -0500 | [diff] [blame^] | 53 | mQueryHelperTimeElapsedBegin.writeTimestamp(contextVk); |
Shahbaz Youssefi | c2b576d | 2018-10-12 14:45:34 -0400 | [diff] [blame] | 54 | } |
| 55 | else |
| 56 | { |
Shahbaz Youssefi | c81e7bf | 2019-01-18 15:35:55 -0500 | [diff] [blame^] | 57 | mQueryHelper.beginQuery(contextVk); |
Shahbaz Youssefi | c2b576d | 2018-10-12 14:45:34 -0400 | [diff] [blame] | 58 | } |
Shahbaz Youssefi | 563fbaa | 2018-10-02 11:22:01 -0400 | [diff] [blame] | 59 | |
Jamie Madill | 7c985f5 | 2018-11-29 18:16:17 -0500 | [diff] [blame] | 60 | return angle::Result::Continue; |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 61 | } |
| 62 | |
Jamie Madill | f4a789f | 2018-10-18 16:56:20 -0400 | [diff] [blame] | 63 | angle::Result QueryVk::end(const gl::Context *context) |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 64 | { |
Shahbaz Youssefi | 563fbaa | 2018-10-02 11:22:01 -0400 | [diff] [blame] | 65 | ContextVk *contextVk = vk::GetImpl(context); |
| 66 | |
Shahbaz Youssefi | c2b576d | 2018-10-12 14:45:34 -0400 | [diff] [blame] | 67 | if (getType() == gl::QueryType::TimeElapsed) |
| 68 | { |
Shahbaz Youssefi | c81e7bf | 2019-01-18 15:35:55 -0500 | [diff] [blame^] | 69 | mQueryHelper.writeTimestamp(contextVk); |
Shahbaz Youssefi | c2b576d | 2018-10-12 14:45:34 -0400 | [diff] [blame] | 70 | } |
| 71 | else |
| 72 | { |
Shahbaz Youssefi | c81e7bf | 2019-01-18 15:35:55 -0500 | [diff] [blame^] | 73 | mQueryHelper.endQuery(contextVk); |
Shahbaz Youssefi | c2b576d | 2018-10-12 14:45:34 -0400 | [diff] [blame] | 74 | } |
Shahbaz Youssefi | 563fbaa | 2018-10-02 11:22:01 -0400 | [diff] [blame] | 75 | |
Jamie Madill | 7c985f5 | 2018-11-29 18:16:17 -0500 | [diff] [blame] | 76 | return angle::Result::Continue; |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 77 | } |
| 78 | |
Jamie Madill | f4a789f | 2018-10-18 16:56:20 -0400 | [diff] [blame] | 79 | angle::Result QueryVk::queryCounter(const gl::Context *context) |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 80 | { |
Shahbaz Youssefi | c2b576d | 2018-10-12 14:45:34 -0400 | [diff] [blame] | 81 | ContextVk *contextVk = vk::GetImpl(context); |
| 82 | |
| 83 | mCachedResultValid = false; |
| 84 | |
| 85 | if (!mQueryHelper.getQueryPool()) |
| 86 | { |
| 87 | ANGLE_TRY(contextVk->getQueryPool(getType())->allocateQuery(contextVk, &mQueryHelper)); |
| 88 | } |
| 89 | |
| 90 | ASSERT(getType() == gl::QueryType::Timestamp); |
| 91 | |
Shahbaz Youssefi | c81e7bf | 2019-01-18 15:35:55 -0500 | [diff] [blame^] | 92 | mQueryHelper.writeTimestamp(contextVk); |
Shahbaz Youssefi | c2b576d | 2018-10-12 14:45:34 -0400 | [diff] [blame] | 93 | |
Jamie Madill | 7c985f5 | 2018-11-29 18:16:17 -0500 | [diff] [blame] | 94 | return angle::Result::Continue; |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 95 | } |
| 96 | |
Shahbaz Youssefi | 563fbaa | 2018-10-02 11:22:01 -0400 | [diff] [blame] | 97 | angle::Result QueryVk::getResult(const gl::Context *context, bool wait) |
| 98 | { |
| 99 | if (mCachedResultValid) |
| 100 | { |
Jamie Madill | 7c985f5 | 2018-11-29 18:16:17 -0500 | [diff] [blame] | 101 | return angle::Result::Continue; |
Shahbaz Youssefi | 563fbaa | 2018-10-02 11:22:01 -0400 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | ContextVk *contextVk = vk::GetImpl(context); |
Shahbaz Youssefi | c4765aa | 2018-10-12 14:40:29 -0400 | [diff] [blame] | 105 | RendererVk *renderer = contextVk->getRenderer(); |
| 106 | |
| 107 | // glGetQueryObject* requires an implicit flush of the command buffers to guarantee execution in |
| 108 | // finite time. |
Shahbaz Youssefi | c2b576d | 2018-10-12 14:45:34 -0400 | [diff] [blame] | 109 | // Note regarding time-elapsed: end should have been called after begin, so flushing when end |
| 110 | // has pending work should flush begin too. |
Shahbaz Youssefi | c4765aa | 2018-10-12 14:40:29 -0400 | [diff] [blame] | 111 | if (mQueryHelper.hasPendingWork(renderer)) |
| 112 | { |
Jamie Madill | 526392d | 2018-11-16 09:35:14 -0500 | [diff] [blame] | 113 | ANGLE_TRY(renderer->flush(contextVk)); |
Shahbaz Youssefi | c2b576d | 2018-10-12 14:45:34 -0400 | [diff] [blame] | 114 | |
| 115 | ASSERT(!mQueryHelperTimeElapsedBegin.hasPendingWork(renderer)); |
Shahbaz Youssefi | c4765aa | 2018-10-12 14:40:29 -0400 | [diff] [blame] | 116 | ASSERT(!mQueryHelper.hasPendingWork(renderer)); |
| 117 | } |
| 118 | |
| 119 | // If the command buffer this query is being written to is still in flight, its reset command |
| 120 | // may not have been performed by the GPU yet. To avoid a race condition in this case, wait |
| 121 | // for the batch to finish first before querying (or return not-ready if not waiting). |
| 122 | ANGLE_TRY(renderer->checkCompletedCommands(contextVk)); |
Shahbaz Youssefi | c81e7bf | 2019-01-18 15:35:55 -0500 | [diff] [blame^] | 123 | if (renderer->isSerialInUse(mQueryHelper.getStoredQueueSerial())) |
Shahbaz Youssefi | c4765aa | 2018-10-12 14:40:29 -0400 | [diff] [blame] | 124 | { |
| 125 | if (!wait) |
| 126 | { |
Jamie Madill | 7c985f5 | 2018-11-29 18:16:17 -0500 | [diff] [blame] | 127 | return angle::Result::Continue; |
Shahbaz Youssefi | c4765aa | 2018-10-12 14:40:29 -0400 | [diff] [blame] | 128 | } |
| 129 | ANGLE_TRY(renderer->finishToSerial(contextVk, mQueryHelper.getStoredQueueSerial())); |
| 130 | } |
Shahbaz Youssefi | 563fbaa | 2018-10-02 11:22:01 -0400 | [diff] [blame] | 131 | |
| 132 | VkQueryResultFlags flags = (wait ? VK_QUERY_RESULT_WAIT_BIT : 0) | VK_QUERY_RESULT_64_BIT; |
| 133 | |
Yuly Novikov | 2778029 | 2018-11-09 11:19:49 -0500 | [diff] [blame] | 134 | VkResult result = mQueryHelper.getQueryPool()->getResults( |
| 135 | contextVk->getDevice(), mQueryHelper.getQuery(), 1, sizeof(mCachedResult), &mCachedResult, |
Shahbaz Youssefi | 563fbaa | 2018-10-02 11:22:01 -0400 | [diff] [blame] | 136 | sizeof(mCachedResult), flags); |
Shahbaz Youssefi | c4765aa | 2018-10-12 14:40:29 -0400 | [diff] [blame] | 137 | // If the results are not ready, do nothing. mCachedResultValid remains false. |
Yuly Novikov | 2778029 | 2018-11-09 11:19:49 -0500 | [diff] [blame] | 138 | if (result == VK_NOT_READY) |
Shahbaz Youssefi | 563fbaa | 2018-10-02 11:22:01 -0400 | [diff] [blame] | 139 | { |
Yuly Novikov | 2778029 | 2018-11-09 11:19:49 -0500 | [diff] [blame] | 140 | // If VK_QUERY_RESULT_WAIT_BIT was given, VK_NOT_READY cannot have been returned. |
Shahbaz Youssefi | c4765aa | 2018-10-12 14:40:29 -0400 | [diff] [blame] | 141 | ASSERT(!wait); |
Jamie Madill | 7c985f5 | 2018-11-29 18:16:17 -0500 | [diff] [blame] | 142 | return angle::Result::Continue; |
Shahbaz Youssefi | 563fbaa | 2018-10-02 11:22:01 -0400 | [diff] [blame] | 143 | } |
Yuly Novikov | 2778029 | 2018-11-09 11:19:49 -0500 | [diff] [blame] | 144 | ANGLE_VK_TRY(contextVk, result); |
Shahbaz Youssefi | 563fbaa | 2018-10-02 11:22:01 -0400 | [diff] [blame] | 145 | |
Shahbaz Youssefi | c4765aa | 2018-10-12 14:40:29 -0400 | [diff] [blame] | 146 | // Fix up the results to what OpenGL expects. |
| 147 | switch (getType()) |
| 148 | { |
| 149 | case gl::QueryType::AnySamples: |
| 150 | case gl::QueryType::AnySamplesConservative: |
| 151 | // OpenGL query result in these cases is binary |
| 152 | mCachedResult = !!mCachedResult; |
| 153 | break; |
Shahbaz Youssefi | c2b576d | 2018-10-12 14:45:34 -0400 | [diff] [blame] | 154 | case gl::QueryType::Timestamp: |
| 155 | break; |
| 156 | case gl::QueryType::TimeElapsed: |
| 157 | { |
| 158 | uint64_t timeElapsedEnd = mCachedResult; |
| 159 | |
| 160 | result = mQueryHelperTimeElapsedBegin.getQueryPool()->getResults( |
Yuly Novikov | 2778029 | 2018-11-09 11:19:49 -0500 | [diff] [blame] | 161 | contextVk->getDevice(), mQueryHelperTimeElapsedBegin.getQuery(), 1, |
| 162 | sizeof(mCachedResult), &mCachedResult, sizeof(mCachedResult), flags); |
Shahbaz Youssefi | c2b576d | 2018-10-12 14:45:34 -0400 | [diff] [blame] | 163 | // Since the result of the end query of time-elapsed is already available, the |
| 164 | // result of begin query must be available too. |
Yuly Novikov | 2778029 | 2018-11-09 11:19:49 -0500 | [diff] [blame] | 165 | ASSERT(result != VK_NOT_READY); |
| 166 | ANGLE_VK_TRY(contextVk, result); |
Shahbaz Youssefi | c2b576d | 2018-10-12 14:45:34 -0400 | [diff] [blame] | 167 | |
| 168 | mCachedResult = timeElapsedEnd - mCachedResult; |
| 169 | break; |
| 170 | } |
Shahbaz Youssefi | c4765aa | 2018-10-12 14:40:29 -0400 | [diff] [blame] | 171 | default: |
| 172 | UNREACHABLE(); |
| 173 | break; |
| 174 | } |
| 175 | |
| 176 | mCachedResultValid = true; |
Jamie Madill | 7c985f5 | 2018-11-29 18:16:17 -0500 | [diff] [blame] | 177 | return angle::Result::Continue; |
Shahbaz Youssefi | 563fbaa | 2018-10-02 11:22:01 -0400 | [diff] [blame] | 178 | } |
Jamie Madill | f4a789f | 2018-10-18 16:56:20 -0400 | [diff] [blame] | 179 | angle::Result QueryVk::getResult(const gl::Context *context, GLint *params) |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 180 | { |
Shahbaz Youssefi | 563fbaa | 2018-10-02 11:22:01 -0400 | [diff] [blame] | 181 | ANGLE_TRY(getResult(context, true)); |
| 182 | *params = static_cast<GLint>(mCachedResult); |
Jamie Madill | 7c985f5 | 2018-11-29 18:16:17 -0500 | [diff] [blame] | 183 | return angle::Result::Continue; |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 184 | } |
| 185 | |
Jamie Madill | f4a789f | 2018-10-18 16:56:20 -0400 | [diff] [blame] | 186 | angle::Result QueryVk::getResult(const gl::Context *context, GLuint *params) |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 187 | { |
Shahbaz Youssefi | 563fbaa | 2018-10-02 11:22:01 -0400 | [diff] [blame] | 188 | ANGLE_TRY(getResult(context, true)); |
| 189 | *params = static_cast<GLuint>(mCachedResult); |
Jamie Madill | 7c985f5 | 2018-11-29 18:16:17 -0500 | [diff] [blame] | 190 | return angle::Result::Continue; |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 191 | } |
| 192 | |
Jamie Madill | f4a789f | 2018-10-18 16:56:20 -0400 | [diff] [blame] | 193 | angle::Result QueryVk::getResult(const gl::Context *context, GLint64 *params) |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 194 | { |
Shahbaz Youssefi | 563fbaa | 2018-10-02 11:22:01 -0400 | [diff] [blame] | 195 | ANGLE_TRY(getResult(context, true)); |
| 196 | *params = static_cast<GLint64>(mCachedResult); |
Jamie Madill | 7c985f5 | 2018-11-29 18:16:17 -0500 | [diff] [blame] | 197 | return angle::Result::Continue; |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 198 | } |
| 199 | |
Jamie Madill | f4a789f | 2018-10-18 16:56:20 -0400 | [diff] [blame] | 200 | angle::Result QueryVk::getResult(const gl::Context *context, GLuint64 *params) |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 201 | { |
Shahbaz Youssefi | 563fbaa | 2018-10-02 11:22:01 -0400 | [diff] [blame] | 202 | ANGLE_TRY(getResult(context, true)); |
| 203 | *params = mCachedResult; |
Jamie Madill | 7c985f5 | 2018-11-29 18:16:17 -0500 | [diff] [blame] | 204 | return angle::Result::Continue; |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 205 | } |
| 206 | |
Jamie Madill | f4a789f | 2018-10-18 16:56:20 -0400 | [diff] [blame] | 207 | angle::Result QueryVk::isResultAvailable(const gl::Context *context, bool *available) |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 208 | { |
Shahbaz Youssefi | 563fbaa | 2018-10-02 11:22:01 -0400 | [diff] [blame] | 209 | ANGLE_TRY(getResult(context, false)); |
| 210 | *available = mCachedResultValid; |
| 211 | |
Jamie Madill | 7c985f5 | 2018-11-29 18:16:17 -0500 | [diff] [blame] | 212 | return angle::Result::Continue; |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 213 | } |
| 214 | |
| 215 | } // namespace rx |