Hash :
6328caf3
Author :
Date :
2020-03-24T19:31:02
Vulkan: Avoid renderpass break for occlusion query This ensures beginQuery and endQuery only get inserted into renderpasses and will not close renderpass because of query call Bug: angleproject:4381 Change-Id: I690f096b9e8e4b7ea9a67045d1be0fd7a319c98c Reviewed-on: https://chromium-review.googlesource.com/c/angle/angle/+/2119246 Reviewed-by: Jamie Madill <jmadill@chromium.org> Reviewed-by: Courtney Goeltzenleuchter <courtneygo@google.com> Commit-Queue: Charlie Lao <cclao@google.com>
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 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303
//
// Copyright 2016 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/TransformFeedback.h"
#include "libANGLE/renderer/vulkan/ContextVk.h"
#include "libANGLE/renderer/vulkan/RendererVk.h"
#include "libANGLE/renderer/vulkan/TransformFeedbackVk.h"
#include "common/debug.h"
namespace rx
{
QueryVk::QueryVk(gl::QueryType type)
: QueryImpl(type),
mTransformFeedbackPrimitivesDrawn(0),
mCachedResult(0),
mCachedResultValid(false)
{}
QueryVk::~QueryVk() = default;
void QueryVk::onDestroy(const gl::Context *context)
{
ContextVk *contextVk = vk::GetImpl(context);
if (getType() != gl::QueryType::TransformFeedbackPrimitivesWritten)
{
vk::DynamicQueryPool *queryPool = contextVk->getQueryPool(getType());
queryPool->freeQuery(contextVk, &mQueryHelper);
queryPool->freeQuery(contextVk, &mQueryHelperTimeElapsedBegin);
}
}
angle::Result QueryVk::stashQueryHelper(ContextVk *contextVk)
{
ASSERT(isOcclusionQuery());
mStashedQueryHelpers.emplace_back(mQueryHelper);
mQueryHelper.deinit();
ANGLE_TRY(contextVk->getQueryPool(getType())->allocateQuery(contextVk, &mQueryHelper));
return angle::Result::Continue;
}
angle::Result QueryVk::retrieveStashedQueryResult(ContextVk *contextVk, uint64_t *result)
{
uint64_t total = 0;
for (vk::QueryHelper &query : mStashedQueryHelpers)
{
uint64_t v;
ANGLE_TRY(query.getUint64Result(contextVk, &v));
total += v;
}
mStashedQueryHelpers.clear();
*result = total;
return angle::Result::Continue;
}
angle::Result QueryVk::begin(const gl::Context *context)
{
ContextVk *contextVk = vk::GetImpl(context);
mCachedResultValid = false;
// Transform feedback query is a handled by a CPU-calculated value when emulated.
if (getType() == gl::QueryType::TransformFeedbackPrimitivesWritten)
{
mTransformFeedbackPrimitivesDrawn = 0;
// We could consider using VK_QUERY_TYPE_TRANSFORM_FEEDBACK_STREAM_EXT.
return angle::Result::Continue;
}
if (!mQueryHelper.valid())
{
ANGLE_TRY(contextVk->getQueryPool(getType())->allocateQuery(contextVk, &mQueryHelper));
}
if (isOcclusionQuery())
{
// For pathological usage case where begin/end is called back to back without flush and get
// result, we have to force flush so that the same mQueryHelper will not encoded in the same
// renderpass twice without resetting it.
if (mQueryHelper.hasPendingWork(contextVk))
{
ANGLE_TRY(contextVk->flushImpl(nullptr));
// As soon as beginQuery is called, previous query's result will not retrievable by API.
// We must clear it so that it will not count against current beginQuery call.
mStashedQueryHelpers.clear();
mQueryHelper.deinit();
ANGLE_TRY(contextVk->getQueryPool(getType())->allocateQuery(contextVk, &mQueryHelper));
}
contextVk->beginOcclusionQuery(this);
}
else if (getType() == gl::QueryType::TimeElapsed)
{
// Note: TimeElapsed is implemented by using two Timestamp queries and taking the diff.
if (!mQueryHelperTimeElapsedBegin.valid())
{
ANGLE_TRY(contextVk->getQueryPool(getType())->allocateQuery(
contextVk, &mQueryHelperTimeElapsedBegin));
}
ANGLE_TRY(mQueryHelperTimeElapsedBegin.flushAndWriteTimestamp(contextVk));
}
else
{
ANGLE_TRY(mQueryHelper.beginQuery(contextVk));
}
return angle::Result::Continue;
}
angle::Result QueryVk::end(const gl::Context *context)
{
ContextVk *contextVk = vk::GetImpl(context);
if (getType() == gl::QueryType::TransformFeedbackPrimitivesWritten)
{
mCachedResult = mTransformFeedbackPrimitivesDrawn;
// There could be transform feedback in progress, so add the primitives drawn so far from
// the current transform feedback object.
gl::TransformFeedback *transformFeedback =
context->getState().getCurrentTransformFeedback();
if (transformFeedback)
{
mCachedResult += transformFeedback->getPrimitivesDrawn();
}
mCachedResultValid = true;
// We could consider using VK_QUERY_TYPE_TRANSFORM_FEEDBACK_STREAM_EXT.
}
else if (isOcclusionQuery())
{
contextVk->endOcclusionQuery(this);
}
else if (getType() == gl::QueryType::TimeElapsed)
{
ANGLE_TRY(mQueryHelper.flushAndWriteTimestamp(contextVk));
}
else
{
ANGLE_TRY(mQueryHelper.endQuery(contextVk));
}
return angle::Result::Continue;
}
angle::Result QueryVk::queryCounter(const gl::Context *context)
{
ASSERT(getType() == gl::QueryType::Timestamp);
ContextVk *contextVk = vk::GetImpl(context);
mCachedResultValid = false;
if (!mQueryHelper.valid())
{
ANGLE_TRY(contextVk->getQueryPool(getType())->allocateQuery(contextVk, &mQueryHelper));
}
return mQueryHelper.flushAndWriteTimestamp(contextVk);
}
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(contextVk))
{
ANGLE_TRY(contextVk->flushImpl(nullptr));
ASSERT(!mQueryHelperTimeElapsedBegin.hasPendingWork(contextVk));
ASSERT(!mQueryHelper.hasPendingWork(contextVk));
}
// 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(contextVk->checkCompletedCommands());
if (contextVk->isSerialInUse(mQueryHelper.getStoredQueueSerial()))
{
if (!wait)
{
return angle::Result::Continue;
}
ANGLE_TRY(contextVk->finishToSerial(mQueryHelper.getStoredQueueSerial()));
}
if (wait)
{
ANGLE_TRY(mQueryHelper.getUint64Result(contextVk, &mCachedResult));
uint64_t v;
ANGLE_TRY(retrieveStashedQueryResult(contextVk, &v));
mCachedResult += v;
}
else
{
bool available = false;
ANGLE_TRY(mQueryHelper.getUint64ResultNonBlocking(contextVk, &mCachedResult, &available));
if (!available)
{
// If the results are not ready, do nothing. mCachedResultValid remains false.
return angle::Result::Continue;
}
uint64_t v;
ANGLE_TRY(retrieveStashedQueryResult(contextVk, &v));
mCachedResult += v;
}
double timestampPeriod = renderer->getPhysicalDeviceProperties().limits.timestampPeriod;
// 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:
mCachedResult = static_cast<uint64_t>(mCachedResult * timestampPeriod);
break;
case gl::QueryType::TimeElapsed:
{
uint64_t timeElapsedEnd = mCachedResult;
// Since the result of the end query of time-elapsed is already available, the
// result of begin query must be available too.
ANGLE_TRY(mQueryHelperTimeElapsedBegin.getUint64Result(contextVk, &mCachedResult));
mCachedResult = timeElapsedEnd - mCachedResult;
mCachedResult = static_cast<uint64_t>(mCachedResult * timestampPeriod);
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;
}
void QueryVk::onTransformFeedbackEnd(const gl::Context *context)
{
gl::TransformFeedback *transformFeedback = context->getState().getCurrentTransformFeedback();
ASSERT(transformFeedback);
mTransformFeedbackPrimitivesDrawn += transformFeedback->getPrimitivesDrawn();
}
} // namespace rx