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