File: gpu_supported_limits.cc

package info (click to toggle)
chromium 140.0.7339.127-1~deb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm-proposed-updates
  • size: 6,201,772 kB
  • sloc: cpp: 35,093,800; ansic: 7,161,670; javascript: 4,199,694; python: 1,441,798; asm: 949,904; xml: 747,503; pascal: 187,748; perl: 88,691; sh: 88,248; objc: 79,953; sql: 52,714; cs: 44,599; fortran: 24,137; makefile: 22,119; tcl: 15,277; php: 13,980; yacc: 9,000; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (154 lines) | stat: -rw-r--r-- 6,548 bytes parent folder | download | duplicates (4)
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
// Copyright 2021 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "third_party/blink/renderer/modules/webgpu/gpu_supported_limits.h"

#include <algorithm>

#include "base/numerics/checked_math.h"
#include "third_party/blink/renderer/bindings/core/v8/v8_union_undefined_unsignedlonglongenforcerange.h"
#include "third_party/blink/renderer/bindings/modules/v8/v8_gpu_extent_3d_dict.h"
#include "third_party/blink/renderer/core/dom/dom_exception.h"
#include "third_party/blink/renderer/core/execution_context/execution_context.h"
#include "third_party/blink/renderer/core/inspector/console_message.h"

#define SUPPORTED_LIMITS(X)                    \
  X(maxTextureDimension1D)                     \
  X(maxTextureDimension2D)                     \
  X(maxTextureDimension3D)                     \
  X(maxTextureArrayLayers)                     \
  X(maxBindGroups)                             \
  X(maxBindGroupsPlusVertexBuffers)            \
  X(maxBindingsPerBindGroup)                   \
  X(maxDynamicUniformBuffersPerPipelineLayout) \
  X(maxDynamicStorageBuffersPerPipelineLayout) \
  X(maxSampledTexturesPerShaderStage)          \
  X(maxSamplersPerShaderStage)                 \
  X(maxStorageBuffersPerShaderStage)           \
  X(maxStorageTexturesPerShaderStage)          \
  X(maxUniformBuffersPerShaderStage)           \
  X(maxUniformBufferBindingSize)               \
  X(maxStorageBufferBindingSize)               \
  X(minUniformBufferOffsetAlignment)           \
  X(minStorageBufferOffsetAlignment)           \
  X(maxVertexBuffers)                          \
  X(maxBufferSize)                             \
  X(maxVertexAttributes)                       \
  X(maxVertexBufferArrayStride)                \
  X(maxInterStageShaderVariables)              \
  X(maxColorAttachments)                       \
  X(maxColorAttachmentBytesPerSample)          \
  X(maxComputeWorkgroupStorageSize)            \
  X(maxComputeInvocationsPerWorkgroup)         \
  X(maxComputeWorkgroupSizeX)                  \
  X(maxComputeWorkgroupSizeY)                  \
  X(maxComputeWorkgroupSizeZ)                  \
  X(maxComputeWorkgroupsPerDimension)          \
  X(maxStorageBuffersInFragmentStage)          \
  X(maxStorageTexturesInFragmentStage)         \
  X(maxStorageBuffersInVertexStage)            \
  X(maxStorageTexturesInVertexStage)

namespace blink {

namespace {
template <typename T>
constexpr T UndefinedLimitValue();

template <>
constexpr uint32_t UndefinedLimitValue<uint32_t>() {
  return wgpu::kLimitU32Undefined;
}

template <>
constexpr uint64_t UndefinedLimitValue<uint64_t>() {
  return wgpu::kLimitU64Undefined;
}
}  // namespace

// GPUSupportedLimits

GPUSupportedLimits::GPUSupportedLimits(const ComboLimits& limits) {
  limits.UnlinkedCopyTo(&limits_);
}

// static
bool GPUSupportedLimits::Populate(
    ComboLimits* out,
    const HeapVector<
        std::pair<String,
                  Member<V8UnionUndefinedOrUnsignedLongLongEnforceRange>>>& in,
    ScriptPromiseResolverBase* resolver) {
  auto* context = resolver->GetExecutionContext();
  // TODO(crbug.com/dawn/685): This loop is O(n^2) if the developer
  // passes all of the limits. It could be O(n) with a mapping of
  // String -> wgpu::Limits::*member.
  for (const auto& [limitName, limitRawValue] : in) {
#define X(name)                                                               \
  if (limitName == #name) {                                                   \
    using T = decltype(GPUSupportedLimits::ComboLimits::name);                \
    if (limitRawValue->IsUndefined()) {                                       \
      continue;                                                               \
    }                                                                         \
    uint64_t limitRawIntegerValue =                                           \
        limitRawValue->GetAsUnsignedLongLongEnforceRange();                   \
    base::CheckedNumeric<T> value{limitRawIntegerValue};                      \
    if (!value.IsValid() || value.ValueOrDie() == UndefinedLimitValue<T>()) { \
      resolver->RejectWithDOMException(                                       \
          DOMExceptionCode::kOperationError,                                  \
          "Required " #name " limit (" +                                      \
              String::Number(limitRawIntegerValue) +                          \
              ") exceeds the maximum representable value for its type.");     \
      return false;                                                           \
    }                                                                         \
    out->name = value.ValueOrDie();                                           \
    continue;                                                                 \
  }
    SUPPORTED_LIMITS(X)
#undef X
    if (limitRawValue->IsUndefined()) {
      auto* console_message = MakeGarbageCollected<ConsoleMessage>(
          mojom::blink::ConsoleMessageSource::kRendering,
          mojom::blink::ConsoleMessageLevel::kWarning,
          "The limit \"" + limitName + "\" is not recognized.");
      context->AddConsoleMessage(console_message);
    } else {
      resolver->RejectWithDOMException(
          DOMExceptionCode::kOperationError,
          "The limit \"" + limitName +
              "\" with a non-undefined value is not recognized.");
      return false;
    }
  }
  return true;
}

#define X(name)                                                              \
  decltype(GPUSupportedLimits::ComboLimits::name) GPUSupportedLimits::name() \
      const {                                                                \
    return limits_.name;                                                     \
  }
SUPPORTED_LIMITS(X)
#undef X

// GPUSupportedLimits::ComboLimits

GPUSupportedLimits::ComboLimits::ComboLimits() = default;

void GPUSupportedLimits::ComboLimits::UnlinkedCopyTo(
    GPUSupportedLimits::ComboLimits* o) const {
  *static_cast<wgpu::Limits*>(o) = *this;
  o->wgpu::Limits::nextInChain = nullptr;
  *static_cast<wgpu::CompatibilityModeLimits*>(o) = *this;
  o->wgpu::CompatibilityModeLimits::nextInChain = nullptr;
}

wgpu::Limits* GPUSupportedLimits::ComboLimits::GetLinked() {
  this->wgpu::Limits::nextInChain =
      static_cast<wgpu::CompatibilityModeLimits*>(this);
  this->wgpu::CompatibilityModeLimits::nextInChain = nullptr;
  return this;
}

}  // namespace blink