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