summaryrefslogtreecommitdiffstats
path: root/dom/canvas/WebGL2ContextQueries.cpp
blob: 17a1a462f0adcb93f9acfd6bddf90429a0e7735d (plain)
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
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* 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 "WebGL2Context.h"
#include "GLContext.h"
#include "WebGLQuery.h"
#include "nsThreadUtils.h"

namespace mozilla {

/*
 * We fake ANY_SAMPLES_PASSED and ANY_SAMPLES_PASSED_CONSERVATIVE with
 * SAMPLES_PASSED on desktop.
 *
 * OpenGL ES 3.0 spec 4.1.6:
 *     If the target of the query is ANY_SAMPLES_PASSED_CONSERVATIVE, an
 *     implementation may choose to use a less precise version of the test which
 *     can additionally set the samples-boolean state to TRUE in some other
 *     implementation-dependent cases.
 */

RefPtr<WebGLQuery>* WebGLContext::ValidateQuerySlotByTarget(GLenum target) {
  if (IsWebGL2()) {
    switch (target) {
      case LOCAL_GL_ANY_SAMPLES_PASSED:
      case LOCAL_GL_ANY_SAMPLES_PASSED_CONSERVATIVE:
        return &mQuerySlot_SamplesPassed;

      case LOCAL_GL_TRANSFORM_FEEDBACK_PRIMITIVES_WRITTEN:
        return &mQuerySlot_TFPrimsWritten;

      default:
        break;
    }
  }

  if (IsExtensionEnabled(WebGLExtensionID::EXT_disjoint_timer_query)) {
    switch (target) {
      case LOCAL_GL_TIME_ELAPSED_EXT:
        return &mQuerySlot_TimeElapsed;

      default:
        break;
    }
  }

  ErrorInvalidEnumInfo("target", target);
  return nullptr;
}

// -------------------------------------------------------------------------
// Query Objects

RefPtr<WebGLQuery> WebGLContext::CreateQuery() {
  const FuncScope funcScope(*this, "createQuery");
  if (IsContextLost()) return nullptr;

  return new WebGLQuery(this);
}

void WebGLContext::BeginQuery(GLenum target, WebGLQuery& query) {
  FuncScope funcScope(*this, "beginQuery");
  if (IsContextLost()) return;
  funcScope.mBindFailureGuard = true;

  const auto& slot = ValidateQuerySlotByTarget(target);
  if (!slot) return;

  if (*slot) return ErrorInvalidOperation("Query target already active.");

  const auto& curTarget = query.Target();
  if (curTarget && target != curTarget) {
    ErrorInvalidOperation("Queries cannot change targets.");
    return;
  }

  ////

  query.BeginQuery(target, *slot);

  funcScope.mBindFailureGuard = false;
}

void WebGLContext::EndQuery(GLenum target) {
  FuncScope funcScope(*this, "endQuery");
  if (IsContextLost()) return;
  funcScope.mBindFailureGuard = true;

  const auto& slot = ValidateQuerySlotByTarget(target);
  if (!slot) return;

  const auto query = *slot;  // Grab a strong reference.
  if (!query) return ErrorInvalidOperation("Query target not active.");

  query->EndQuery();

  funcScope.mBindFailureGuard = false;
}

Maybe<double> WebGLContext::GetQueryParameter(const WebGLQuery& query,
                                              GLenum pname) const {
  const FuncScope funcScope(*this, "getQueryParameter");
  if (IsContextLost()) return Nothing();

  return query.GetQueryParameter(pname);
}

// disjoint_timer_queries

void WebGLContext::QueryCounter(WebGLQuery& query) const {
  const WebGLContext::FuncScope funcScope(*this, "queryCounterEXT");
  if (IsContextLost()) return;

  query.QueryCounter();
}

}  // namespace mozilla