blob: d6e11df72b9fdee1bb79edf6f28f6f49c5889097 [file] [log] [blame]
Jamie Madill9e54b5a2016-05-25 12:57:39 -04001//
Shahbaz Youssefi563fbaa2018-10-02 11:22:01 -04002// Copyright 2016-2018 The ANGLE Project Authors. All rights reserved.
Jamie Madill9e54b5a2016-05-25 12:57:39 -04003// 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 Youssefi563fbaa2018-10-02 11:22:01 -040011#include "libANGLE/Context.h"
12#include "libANGLE/renderer/vulkan/ContextVk.h"
Shahbaz Youssefic4765aa2018-10-12 14:40:29 -040013#include "libANGLE/renderer/vulkan/RendererVk.h"
Jamie Madill9e54b5a2016-05-25 12:57:39 -040014
15#include "common/debug.h"
16
17namespace rx
18{
19
Shahbaz Youssefi563fbaa2018-10-02 11:22:01 -040020QueryVk::QueryVk(gl::QueryType type) : QueryImpl(type), mCachedResult(0), mCachedResultValid(false)
Jamie Madill9e54b5a2016-05-25 12:57:39 -040021{
22}
23
Shahbaz Youssefi563fbaa2018-10-02 11:22:01 -040024QueryVk::~QueryVk() = default;
25
26gl::Error QueryVk::onDestroy(const gl::Context *context)
Jamie Madill9e54b5a2016-05-25 12:57:39 -040027{
Shahbaz Youssefi563fbaa2018-10-02 11:22:01 -040028 ContextVk *contextVk = vk::GetImpl(context);
29 contextVk->getQueryPool(getType())->freeQuery(contextVk, &mQueryHelper);
30
31 return gl::NoError();
Jamie Madill9e54b5a2016-05-25 12:57:39 -040032}
33
Jamie Madill5188a272018-07-25 10:53:56 -040034gl::Error QueryVk::begin(const gl::Context *context)
Jamie Madill9e54b5a2016-05-25 12:57:39 -040035{
Shahbaz Youssefi563fbaa2018-10-02 11:22:01 -040036 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 Madill9e54b5a2016-05-25 12:57:39 -040045}
46
Jamie Madill5188a272018-07-25 10:53:56 -040047gl::Error QueryVk::end(const gl::Context *context)
Jamie Madill9e54b5a2016-05-25 12:57:39 -040048{
Shahbaz Youssefi563fbaa2018-10-02 11:22:01 -040049 ContextVk *contextVk = vk::GetImpl(context);
50
51 mQueryHelper.endQuery(contextVk, mQueryHelper.getQueryPool(), mQueryHelper.getQuery());
52
53 return gl::NoError();
Jamie Madill9e54b5a2016-05-25 12:57:39 -040054}
55
Jamie Madill5188a272018-07-25 10:53:56 -040056gl::Error QueryVk::queryCounter(const gl::Context *context)
Jamie Madill9e54b5a2016-05-25 12:57:39 -040057{
58 UNIMPLEMENTED();
Yuly Novikovc4d18aa2017-03-09 18:45:02 -050059 return gl::InternalError();
Jamie Madill9e54b5a2016-05-25 12:57:39 -040060}
61
Shahbaz Youssefi563fbaa2018-10-02 11:22:01 -040062angle::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 Youssefic4765aa2018-10-12 14:40:29 -040070 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 Youssefi563fbaa2018-10-02 11:22:01 -040092
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 Youssefic4765aa2018-10-12 14:40:29 -0400100 // If the results are not ready, do nothing. mCachedResultValid remains false.
101 if (result == angle::Result::Incomplete())
Shahbaz Youssefi563fbaa2018-10-02 11:22:01 -0400102 {
Shahbaz Youssefic4765aa2018-10-12 14:40:29 -0400103 // If VK_QUERY_RESULT_WAIT_BIT was given, Incomplete() cannot have been returned.
104 ASSERT(!wait);
105 return angle::Result::Continue();
Shahbaz Youssefi563fbaa2018-10-02 11:22:01 -0400106 }
107
Shahbaz Youssefic4765aa2018-10-12 14:40:29 -0400108 // 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 Youssefi563fbaa2018-10-02 11:22:01 -0400122 return angle::Result::Continue();
123}
124
Jamie Madill5188a272018-07-25 10:53:56 -0400125gl::Error QueryVk::getResult(const gl::Context *context, GLint *params)
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400126{
Shahbaz Youssefi563fbaa2018-10-02 11:22:01 -0400127 ANGLE_TRY(getResult(context, true));
128 *params = static_cast<GLint>(mCachedResult);
129 return gl::NoError();
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400130}
131
Jamie Madill5188a272018-07-25 10:53:56 -0400132gl::Error QueryVk::getResult(const gl::Context *context, GLuint *params)
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400133{
Shahbaz Youssefi563fbaa2018-10-02 11:22:01 -0400134 ANGLE_TRY(getResult(context, true));
135 *params = static_cast<GLuint>(mCachedResult);
136 return gl::NoError();
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400137}
138
Jamie Madill5188a272018-07-25 10:53:56 -0400139gl::Error QueryVk::getResult(const gl::Context *context, GLint64 *params)
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400140{
Shahbaz Youssefi563fbaa2018-10-02 11:22:01 -0400141 ANGLE_TRY(getResult(context, true));
142 *params = static_cast<GLint64>(mCachedResult);
143 return gl::NoError();
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400144}
145
Jamie Madill5188a272018-07-25 10:53:56 -0400146gl::Error QueryVk::getResult(const gl::Context *context, GLuint64 *params)
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400147{
Shahbaz Youssefi563fbaa2018-10-02 11:22:01 -0400148 ANGLE_TRY(getResult(context, true));
149 *params = mCachedResult;
150 return gl::NoError();
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400151}
152
Jamie Madill5188a272018-07-25 10:53:56 -0400153gl::Error QueryVk::isResultAvailable(const gl::Context *context, bool *available)
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400154{
Shahbaz Youssefi563fbaa2018-10-02 11:22:01 -0400155 ANGLE_TRY(getResult(context, false));
156 *available = mCachedResultValid;
157
158 return gl::NoError();
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400159}
160
161} // namespace rx