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 | |
| 26 | gl::Error 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); |
| 29 | contextVk->getQueryPool(getType())->freeQuery(contextVk, &mQueryHelper); |
| 30 | |
| 31 | return gl::NoError(); |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 32 | } |
| 33 | |
Jamie Madill | 5188a27 | 2018-07-25 10:53:56 -0400 | [diff] [blame] | 34 | gl::Error 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 | |
| 38 | ANGLE_TRY(contextVk->getQueryPool(getType())->allocateQuery(contextVk, &mQueryHelper)); |
| 39 | |
| 40 | mCachedResultValid = false; |
| 41 | |
| 42 | mQueryHelper.beginQuery(contextVk, mQueryHelper.getQueryPool(), mQueryHelper.getQuery()); |
| 43 | |
| 44 | return gl::NoError(); |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 45 | } |
| 46 | |
Jamie Madill | 5188a27 | 2018-07-25 10:53:56 -0400 | [diff] [blame] | 47 | gl::Error QueryVk::end(const gl::Context *context) |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 48 | { |
Shahbaz Youssefi | 563fbaa | 2018-10-02 11:22:01 -0400 | [diff] [blame] | 49 | ContextVk *contextVk = vk::GetImpl(context); |
| 50 | |
| 51 | mQueryHelper.endQuery(contextVk, mQueryHelper.getQueryPool(), mQueryHelper.getQuery()); |
| 52 | |
| 53 | return gl::NoError(); |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 54 | } |
| 55 | |
Jamie Madill | 5188a27 | 2018-07-25 10:53:56 -0400 | [diff] [blame] | 56 | gl::Error QueryVk::queryCounter(const gl::Context *context) |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 57 | { |
| 58 | UNIMPLEMENTED(); |
Yuly Novikov | c4d18aa | 2017-03-09 18:45:02 -0500 | [diff] [blame] | 59 | return gl::InternalError(); |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 60 | } |
| 61 | |
Shahbaz Youssefi | 563fbaa | 2018-10-02 11:22:01 -0400 | [diff] [blame] | 62 | angle::Result QueryVk::getResult(const gl::Context *context, bool wait) |
| 63 | { |
| 64 | if (mCachedResultValid) |
| 65 | { |
| 66 | return angle::Result::Continue(); |
| 67 | } |
| 68 | |
| 69 | ContextVk *contextVk = vk::GetImpl(context); |
Shahbaz Youssefi | c4765aa | 2018-10-12 14:40:29 -0400 | [diff] [blame^] | 70 | RendererVk *renderer = contextVk->getRenderer(); |
| 71 | |
| 72 | // glGetQueryObject* requires an implicit flush of the command buffers to guarantee execution in |
| 73 | // finite time. |
| 74 | if (mQueryHelper.hasPendingWork(renderer)) |
| 75 | { |
| 76 | ANGLE_TRY_HANDLE(context, renderer->flush(contextVk)); |
| 77 | ASSERT(!mQueryHelper.hasPendingWork(renderer)); |
| 78 | } |
| 79 | |
| 80 | // If the command buffer this query is being written to is still in flight, its reset command |
| 81 | // may not have been performed by the GPU yet. To avoid a race condition in this case, wait |
| 82 | // for the batch to finish first before querying (or return not-ready if not waiting). |
| 83 | ANGLE_TRY(renderer->checkCompletedCommands(contextVk)); |
| 84 | if (mQueryHelper.isResourceInUse(renderer)) |
| 85 | { |
| 86 | if (!wait) |
| 87 | { |
| 88 | return angle::Result::Continue(); |
| 89 | } |
| 90 | ANGLE_TRY(renderer->finishToSerial(contextVk, mQueryHelper.getStoredQueueSerial())); |
| 91 | } |
Shahbaz Youssefi | 563fbaa | 2018-10-02 11:22:01 -0400 | [diff] [blame] | 92 | |
| 93 | VkQueryResultFlags flags = (wait ? VK_QUERY_RESULT_WAIT_BIT : 0) | VK_QUERY_RESULT_64_BIT; |
| 94 | |
| 95 | angle::Result result = mQueryHelper.getQueryPool()->getResults( |
| 96 | contextVk, mQueryHelper.getQuery(), 1, sizeof(mCachedResult), &mCachedResult, |
| 97 | sizeof(mCachedResult), flags); |
| 98 | ANGLE_TRY(result); |
| 99 | |
Shahbaz Youssefi | c4765aa | 2018-10-12 14:40:29 -0400 | [diff] [blame^] | 100 | // If the results are not ready, do nothing. mCachedResultValid remains false. |
| 101 | if (result == angle::Result::Incomplete()) |
Shahbaz Youssefi | 563fbaa | 2018-10-02 11:22:01 -0400 | [diff] [blame] | 102 | { |
Shahbaz Youssefi | c4765aa | 2018-10-12 14:40:29 -0400 | [diff] [blame^] | 103 | // If VK_QUERY_RESULT_WAIT_BIT was given, Incomplete() cannot have been returned. |
| 104 | ASSERT(!wait); |
| 105 | return angle::Result::Continue(); |
Shahbaz Youssefi | 563fbaa | 2018-10-02 11:22:01 -0400 | [diff] [blame] | 106 | } |
| 107 | |
Shahbaz Youssefi | c4765aa | 2018-10-12 14:40:29 -0400 | [diff] [blame^] | 108 | // Fix up the results to what OpenGL expects. |
| 109 | switch (getType()) |
| 110 | { |
| 111 | case gl::QueryType::AnySamples: |
| 112 | case gl::QueryType::AnySamplesConservative: |
| 113 | // OpenGL query result in these cases is binary |
| 114 | mCachedResult = !!mCachedResult; |
| 115 | break; |
| 116 | default: |
| 117 | UNREACHABLE(); |
| 118 | break; |
| 119 | } |
| 120 | |
| 121 | mCachedResultValid = true; |
Shahbaz Youssefi | 563fbaa | 2018-10-02 11:22:01 -0400 | [diff] [blame] | 122 | return angle::Result::Continue(); |
| 123 | } |
| 124 | |
Jamie Madill | 5188a27 | 2018-07-25 10:53:56 -0400 | [diff] [blame] | 125 | gl::Error QueryVk::getResult(const gl::Context *context, GLint *params) |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 126 | { |
Shahbaz Youssefi | 563fbaa | 2018-10-02 11:22:01 -0400 | [diff] [blame] | 127 | ANGLE_TRY(getResult(context, true)); |
| 128 | *params = static_cast<GLint>(mCachedResult); |
| 129 | return gl::NoError(); |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 130 | } |
| 131 | |
Jamie Madill | 5188a27 | 2018-07-25 10:53:56 -0400 | [diff] [blame] | 132 | gl::Error QueryVk::getResult(const gl::Context *context, GLuint *params) |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 133 | { |
Shahbaz Youssefi | 563fbaa | 2018-10-02 11:22:01 -0400 | [diff] [blame] | 134 | ANGLE_TRY(getResult(context, true)); |
| 135 | *params = static_cast<GLuint>(mCachedResult); |
| 136 | return gl::NoError(); |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 137 | } |
| 138 | |
Jamie Madill | 5188a27 | 2018-07-25 10:53:56 -0400 | [diff] [blame] | 139 | gl::Error QueryVk::getResult(const gl::Context *context, GLint64 *params) |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 140 | { |
Shahbaz Youssefi | 563fbaa | 2018-10-02 11:22:01 -0400 | [diff] [blame] | 141 | ANGLE_TRY(getResult(context, true)); |
| 142 | *params = static_cast<GLint64>(mCachedResult); |
| 143 | return gl::NoError(); |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 144 | } |
| 145 | |
Jamie Madill | 5188a27 | 2018-07-25 10:53:56 -0400 | [diff] [blame] | 146 | gl::Error QueryVk::getResult(const gl::Context *context, GLuint64 *params) |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 147 | { |
Shahbaz Youssefi | 563fbaa | 2018-10-02 11:22:01 -0400 | [diff] [blame] | 148 | ANGLE_TRY(getResult(context, true)); |
| 149 | *params = mCachedResult; |
| 150 | return gl::NoError(); |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 151 | } |
| 152 | |
Jamie Madill | 5188a27 | 2018-07-25 10:53:56 -0400 | [diff] [blame] | 153 | gl::Error QueryVk::isResultAvailable(const gl::Context *context, bool *available) |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 154 | { |
Shahbaz Youssefi | 563fbaa | 2018-10-02 11:22:01 -0400 | [diff] [blame] | 155 | ANGLE_TRY(getResult(context, false)); |
| 156 | *available = mCachedResultValid; |
| 157 | |
| 158 | return gl::NoError(); |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 159 | } |
| 160 | |
| 161 | } // namespace rx |