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