2018-11-30 22:52:05 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
2013-08-07 01:23:46 +04:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
|
|
|
#include "WebGLQuery.h"
|
2014-11-03 08:35:04 +03:00
|
|
|
|
|
|
|
#include "GLContext.h"
|
2013-08-07 01:23:46 +04:00
|
|
|
#include "mozilla/dom/WebGL2RenderingContextBinding.h"
|
2020-01-09 01:19:16 +03:00
|
|
|
#include "mozilla/StaticPrefs_webgl.h"
|
2013-08-07 01:23:46 +04:00
|
|
|
#include "nsContentUtils.h"
|
2014-11-14 07:03:50 +03:00
|
|
|
#include "WebGLContext.h"
|
2013-08-07 01:23:46 +04:00
|
|
|
|
2014-11-14 07:03:50 +03:00
|
|
|
namespace mozilla {
|
2013-08-07 01:23:46 +04:00
|
|
|
|
2016-10-04 04:33:52 +03:00
|
|
|
////
|
|
|
|
|
|
|
|
static GLuint GenQuery(gl::GLContext* gl) {
|
|
|
|
GLuint ret = 0;
|
|
|
|
gl->fGenQueries(1, &ret);
|
|
|
|
return ret;
|
2013-08-07 01:23:46 +04:00
|
|
|
}
|
|
|
|
|
2014-11-14 07:03:50 +03:00
|
|
|
WebGLQuery::WebGLQuery(WebGLContext* webgl)
|
2020-01-09 01:19:16 +03:00
|
|
|
: WebGLContextBoundObject(webgl),
|
2016-10-04 04:33:52 +03:00
|
|
|
mGLName(GenQuery(mContext->gl)),
|
|
|
|
mTarget(0),
|
2020-01-09 01:19:16 +03:00
|
|
|
mActiveSlot(nullptr) {}
|
2013-08-07 01:23:46 +04:00
|
|
|
|
2020-01-09 01:19:16 +03:00
|
|
|
WebGLQuery::~WebGLQuery() {
|
|
|
|
if (!mContext) return;
|
2013-08-07 01:23:46 +04:00
|
|
|
mContext->gl->fDeleteQueries(1, &mGLName);
|
|
|
|
}
|
|
|
|
|
2016-10-04 04:33:52 +03:00
|
|
|
////
|
|
|
|
|
|
|
|
static GLenum TargetForDriver(const gl::GLContext* gl, GLenum target) {
|
|
|
|
switch (target) {
|
|
|
|
case LOCAL_GL_ANY_SAMPLES_PASSED:
|
|
|
|
case LOCAL_GL_ANY_SAMPLES_PASSED_CONSERVATIVE:
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
return target;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (gl->IsSupported(gl::GLFeature::occlusion_query_boolean)) return target;
|
|
|
|
|
|
|
|
if (gl->IsSupported(gl::GLFeature::occlusion_query2))
|
|
|
|
return LOCAL_GL_ANY_SAMPLES_PASSED;
|
|
|
|
|
|
|
|
return LOCAL_GL_SAMPLES_PASSED;
|
|
|
|
}
|
|
|
|
|
2020-01-09 01:19:16 +03:00
|
|
|
void WebGLQuery::BeginQuery(GLenum target, RefPtr<WebGLQuery>& slot) {
|
2016-10-04 04:33:52 +03:00
|
|
|
mTarget = target;
|
2016-11-08 06:38:48 +03:00
|
|
|
mActiveSlot = &slot;
|
|
|
|
*mActiveSlot = this;
|
2016-10-04 04:33:52 +03:00
|
|
|
|
|
|
|
////
|
|
|
|
|
|
|
|
const auto& gl = mContext->gl;
|
|
|
|
|
|
|
|
const auto driverTarget = TargetForDriver(gl, mTarget);
|
|
|
|
gl->fBeginQuery(driverTarget, mGLName);
|
|
|
|
}
|
|
|
|
|
|
|
|
void WebGLQuery::EndQuery() {
|
2016-11-08 06:38:48 +03:00
|
|
|
*mActiveSlot = nullptr;
|
|
|
|
mActiveSlot = nullptr;
|
2016-10-04 04:33:52 +03:00
|
|
|
mCanBeAvailable = false;
|
|
|
|
|
|
|
|
////
|
|
|
|
|
|
|
|
const auto& gl = mContext->gl;
|
|
|
|
|
|
|
|
const auto driverTarget = TargetForDriver(gl, mTarget);
|
|
|
|
gl->fEndQuery(driverTarget);
|
|
|
|
}
|
|
|
|
|
2020-01-09 01:19:16 +03:00
|
|
|
Maybe<double> WebGLQuery::GetQueryParameter(GLenum pname) const {
|
2016-10-04 04:33:52 +03:00
|
|
|
switch (pname) {
|
|
|
|
case LOCAL_GL_QUERY_RESULT_AVAILABLE:
|
|
|
|
case LOCAL_GL_QUERY_RESULT:
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2018-07-27 07:46:33 +03:00
|
|
|
mContext->ErrorInvalidEnumInfo("pname", pname);
|
2020-01-09 01:19:14 +03:00
|
|
|
return Nothing();
|
2016-10-04 04:33:52 +03:00
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2016-10-04 04:33:52 +03:00
|
|
|
if (!mTarget) {
|
2018-07-27 07:46:33 +03:00
|
|
|
mContext->ErrorInvalidOperation("Query has never been active.");
|
2020-01-09 01:19:14 +03:00
|
|
|
return Nothing();
|
2016-10-04 04:33:52 +03:00
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2020-01-09 01:19:14 +03:00
|
|
|
if (mActiveSlot) {
|
|
|
|
mContext->ErrorInvalidOperation("Query is still active.");
|
|
|
|
return Nothing();
|
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2016-10-04 04:33:52 +03:00
|
|
|
// End of validation
|
|
|
|
////
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2016-10-04 04:33:52 +03:00
|
|
|
const auto& gl = mContext->gl;
|
|
|
|
|
|
|
|
uint64_t val = 0;
|
|
|
|
switch (pname) {
|
|
|
|
case LOCAL_GL_QUERY_RESULT_AVAILABLE:
|
|
|
|
gl->fGetQueryObjectuiv(mGLName, pname, (GLuint*)&val);
|
2020-01-09 01:19:16 +03:00
|
|
|
return Some(static_cast<bool>(val));
|
2016-10-04 04:33:52 +03:00
|
|
|
|
|
|
|
case LOCAL_GL_QUERY_RESULT:
|
|
|
|
switch (mTarget) {
|
|
|
|
case LOCAL_GL_TIME_ELAPSED_EXT:
|
|
|
|
case LOCAL_GL_TIMESTAMP_EXT:
|
|
|
|
if (mContext->Has64BitTimestamps()) {
|
|
|
|
gl->fGetQueryObjectui64v(mGLName, pname, &val);
|
|
|
|
break;
|
|
|
|
}
|
2019-12-20 10:16:43 +03:00
|
|
|
[[fallthrough]];
|
2016-10-04 04:33:52 +03:00
|
|
|
|
|
|
|
default:
|
|
|
|
gl->fGetQueryObjectuiv(mGLName, LOCAL_GL_QUERY_RESULT, (GLuint*)&val);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (mTarget) {
|
|
|
|
case LOCAL_GL_ANY_SAMPLES_PASSED:
|
|
|
|
case LOCAL_GL_ANY_SAMPLES_PASSED_CONSERVATIVE:
|
|
|
|
case LOCAL_GL_TRANSFORM_FEEDBACK_PRIMITIVES_WRITTEN:
|
|
|
|
case LOCAL_GL_TIME_ELAPSED_EXT:
|
|
|
|
case LOCAL_GL_TIMESTAMP_EXT:
|
2020-01-09 01:19:16 +03:00
|
|
|
return Some(val);
|
2016-10-04 04:33:52 +03:00
|
|
|
}
|
2020-01-09 01:19:14 +03:00
|
|
|
MOZ_CRASH("Bad `mTarget`.");
|
2016-10-04 04:33:52 +03:00
|
|
|
|
|
|
|
default:
|
|
|
|
MOZ_CRASH("Bad `pname`.");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-09 01:19:16 +03:00
|
|
|
void WebGLQuery::QueryCounter() {
|
|
|
|
const GLenum target = LOCAL_GL_TIMESTAMP_EXT;
|
2016-10-04 04:33:52 +03:00
|
|
|
if (mTarget && target != mTarget) {
|
2018-07-27 07:46:33 +03:00
|
|
|
mContext->ErrorInvalidOperation("Queries cannot change targets.");
|
2016-10-04 04:33:52 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
mTarget = target;
|
|
|
|
mCanBeAvailable = false;
|
|
|
|
|
|
|
|
const auto& gl = mContext->gl;
|
|
|
|
gl->fQueryCounter(mGLName, mTarget);
|
|
|
|
}
|
|
|
|
|
2014-11-14 07:03:50 +03:00
|
|
|
} // namespace mozilla
|