File: gpu_device.h

package info (click to toggle)
chromium 138.0.7204.183-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,908 kB
  • sloc: cpp: 34,937,088; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (266 lines) | stat: -rw-r--r-- 10,067 bytes parent folder | download | duplicates (5)
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
// Copyright 2018 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef THIRD_PARTY_BLINK_RENDERER_MODULES_WEBGPU_GPU_DEVICE_H_
#define THIRD_PARTY_BLINK_RENDERER_MODULES_WEBGPU_GPU_DEVICE_H_

#include <bitset>

#include "base/memory/scoped_refptr.h"
#include "third_party/blink/renderer/bindings/core/v8/script_promise.h"
#include "third_party/blink/renderer/bindings/core/v8/script_promise_property.h"
#include "third_party/blink/renderer/bindings/modules/v8/v8_gpu_blend_factor.h"
#include "third_party/blink/renderer/bindings/modules/v8/v8_gpu_texture_format.h"
#include "third_party/blink/renderer/core/dom/events/event_target.h"
#include "third_party/blink/renderer/core/execution_context/execution_context_lifecycle_observer.h"
#include "third_party/blink/renderer/modules/webgpu/dawn_object.h"
#include "third_party/blink/renderer/platform/graphics/gpu/webgpu_callback.h"
#include "third_party/blink/renderer/platform/heap/collection_support/heap_hash_set.h"
#include "third_party/blink/renderer/platform/heap/garbage_collected.h"

namespace blink {

class ExecutionContext;
class ExternalTextureCache;
class GPUAdapter;
class GPUAdapterInfo;
class GPUBuffer;
class GPUBufferDescriptor;
class GPUCommandEncoder;
class GPUCommandEncoderDescriptor;
class GPUBindGroup;
class GPUBindGroupDescriptor;
class GPUBindGroupLayout;
class GPUBindGroupLayoutDescriptor;
class GPUComputePipeline;
class GPUComputePipelineDescriptor;
class GPUDeviceDescriptor;
class GPUDeviceLostInfo;
class GPUError;
class GPUExternalTexture;
class GPUExternalTextureDescriptor;
class GPUPipelineLayout;
class GPUPipelineLayoutDescriptor;
class GPUQuerySet;
class GPUQuerySetDescriptor;
class GPUQueue;
class GPURenderBundleEncoder;
class GPURenderBundleEncoderDescriptor;
class GPURenderPipeline;
class GPURenderPipelineDescriptor;
class GPUSampler;
class GPUSamplerDescriptor;
class GPUShaderModule;
class GPUShaderModuleDescriptor;
class GPUSupportedFeatures;
class GPUSupportedLimits;
class GPUTexture;
class GPUTextureDescriptor;
class ScriptState;
class V8GPUErrorFilter;

// Singleton warnings are messages that can only be raised once per device. They
// should be used for warnings of behavior that is not invalid but may have
// performance issues or side effects that the developer may overlook since a
// regular warning is not raised.
enum class GPUSingletonWarning {
  kNonPreferredFormat,
  kDepthKey,
  kCount,  // Must be last
};

class GPUDevice final : public EventTarget,
                        public ExecutionContextClient,
                        public DawnObject<wgpu::Device> {
  DEFINE_WRAPPERTYPEINFO();
  USING_PRE_FINALIZER(GPUDevice, Dispose);

 public:
  // Creates the device without a handle so that some callbacks can be set up
  // in the wgpu::DeviceDescriptor that will be used to create this device.
  // Initialize is where the handle is given, and the rest of the initialization
  // happens.
  GPUDevice(ExecutionContext* execution_context,
            scoped_refptr<DawnControlClientHolder> dawn_control_client,
            GPUAdapter* adapter,
            const String& label);

  // The second step of the initialization once we have the result from
  // wgpu::Adapter::RequestDevice. The lost_info if non-null will be used to
  // resolve the lost property.
  void Initialize(wgpu::Device handle,
                  const GPUDeviceDescriptor* descriptor,
                  GPUDeviceLostInfo* lost_info);

  GPUDevice(const GPUDevice&) = delete;
  GPUDevice& operator=(const GPUDevice&) = delete;

  ~GPUDevice() override;

  void Trace(Visitor* visitor) const override;

  // gpu_device.idl {{{
  GPUAdapter* adapter() const;
  GPUSupportedFeatures* features() const;
  GPUSupportedLimits* limits() const { return limits_.Get(); }
  GPUAdapterInfo* adapterInfo() const;
  ScriptPromise<GPUDeviceLostInfo> lost(ScriptState* script_state);
  // }}} End of WebIDL binding implementation.

  GPUQueue* queue();

  void destroy(v8::Isolate* isolate);

  GPUBuffer* createBuffer(const GPUBufferDescriptor* descriptor,
                          ExceptionState& exception_state);
  GPUTexture* createTexture(const GPUTextureDescriptor* descriptor,
                            ExceptionState& exception_state);
  GPUSampler* createSampler(const GPUSamplerDescriptor* descriptor);

  GPUExternalTexture* importExternalTexture(
      const GPUExternalTextureDescriptor* descriptor,
      ExceptionState& exception_state);

  GPUBindGroup* createBindGroup(const GPUBindGroupDescriptor* descriptor,
                                ExceptionState& exception_state);
  GPUBindGroupLayout* createBindGroupLayout(
      const GPUBindGroupLayoutDescriptor* descriptor,
      ExceptionState& exception_state);
  GPUPipelineLayout* createPipelineLayout(
      const GPUPipelineLayoutDescriptor* descriptor);

  GPUShaderModule* createShaderModule(
      const GPUShaderModuleDescriptor* descriptor);
  GPURenderPipeline* createRenderPipeline(
      ScriptState* script_state,
      const GPURenderPipelineDescriptor* descriptor);
  GPUComputePipeline* createComputePipeline(
      const GPUComputePipelineDescriptor* descriptor,
      ExceptionState& exception_state);
  ScriptPromise<GPURenderPipeline> createRenderPipelineAsync(
      ScriptState* script_state,
      const GPURenderPipelineDescriptor* descriptor,
      ExceptionState&);
  ScriptPromise<GPUComputePipeline> createComputePipelineAsync(
      ScriptState* script_state,
      const GPUComputePipelineDescriptor* descriptor);

  GPUCommandEncoder* createCommandEncoder(
      const GPUCommandEncoderDescriptor* descriptor);
  GPURenderBundleEncoder* createRenderBundleEncoder(
      const GPURenderBundleEncoderDescriptor* descriptor,
      ExceptionState& exception_state);

  GPUQuerySet* createQuerySet(const GPUQuerySetDescriptor* descriptor,
                              ExceptionState& exception_state);

  void pushErrorScope(const V8GPUErrorFilter& filter);
  ScriptPromise<IDLNullable<GPUError>> popErrorScope(ScriptState* script_state);

  DEFINE_ATTRIBUTE_EVENT_LISTENER(uncapturederror, kUncapturederror)

  // EventTarget overrides.
  const AtomicString& InterfaceName() const override;
  ExecutionContext* GetExecutionContext() const override;

  bool IsDestroyed() const;
  std::string GetFormattedLabel() const;
  void InjectError(wgpu::ErrorType type, const char* message);
  void AddConsoleWarning(const String& message);
  void AddConsoleWarning(const char* message);
  void AddConsoleWarning(wgpu::StringView message);
  void AddSingletonWarning(GPUSingletonWarning type);

  void TrackTextureWithMailbox(GPUTexture* texture);
  void UntrackTextureWithMailbox(GPUTexture* texture);

  bool ValidateTextureFormatUsage(V8GPUTextureFormat format,
                                  ExceptionState& exception_state);
  bool ValidateBlendFactor(V8GPUBlendFactor blend_factor,
                           ExceptionState& exception_state);

  // Store the buffer in a weak hash set so we can unmap it when the
  // device is destroyed.
  void TrackMappableBuffer(GPUBuffer* buffer);
  // Untrack the GPUBuffer. This is called eagerly when the buffer is
  // destroyed.
  void UntrackMappableBuffer(GPUBuffer* buffer);

  // Helper used to set the wgpu::DeviceDescriptor callbacks during the first
  // steps of GPUDevice creation. Note that this helper should only ever be
  // called once per GPUDevice.
  void SetDescriptorCallbacks(wgpu::DeviceDescriptor& dawn_desc);

 private:
  using LostProperty = ScriptPromiseProperty<GPUDeviceLostInfo, IDLUndefined>;

  // Used by USING_PRE_FINALIZER.
  void Dispose();
  void DissociateMailboxes();
  void UnmapAllMappableBuffers(v8::Isolate* isolate);

  void OnUncapturedError(const wgpu::Device& device,
                         wgpu::ErrorType errorType,
                         wgpu::StringView message);
  void OnLogging(wgpu::LoggingType loggingType, wgpu::StringView message);
  void OnDeviceLost(
      std::unique_ptr<
          WGPURepeatingCallback<wgpu::UncapturedErrorCallback<void>>>,
      const wgpu::Device& device,
      wgpu::DeviceLostReason reason,
      wgpu::StringView message);

  void OnPopErrorScopeCallback(
      ScriptPromiseResolver<IDLNullable<GPUError>>* resolver,
      wgpu::PopErrorScopeStatus status,
      wgpu::ErrorType type,
      wgpu::StringView message);

  void OnCreateRenderPipelineAsyncCallback(
      const String& label,
      ScriptPromiseResolver<GPURenderPipeline>* resolver,
      wgpu::CreatePipelineAsyncStatus status,
      wgpu::RenderPipeline render_pipeline,
      wgpu::StringView message);
  void OnCreateComputePipelineAsyncCallback(
      const String& label,
      ScriptPromiseResolver<GPUComputePipeline>* resolver,
      wgpu::CreatePipelineAsyncStatus status,
      wgpu::ComputePipeline compute_pipeline,
      wgpu::StringView message);

  void SetLabelImpl(const String& value) override {
    std::string utf8_label = value.Utf8();
    GetHandle().SetLabel(utf8_label.c_str());
  }

  Member<GPUAdapter> adapter_;
  Member<GPUSupportedFeatures> features_;
  Member<GPUSupportedLimits> limits_;
  Member<GPUAdapterInfo> adapter_info_;
  Member<GPUQueue> queue_;
  Member<LostProperty> lost_property_;
  std::unique_ptr<WGPURepeatingCallback<wgpu::LoggingCallback<void>>>
      logging_callback_;

  static constexpr int kMaxAllowedConsoleWarnings = 500;
  int allowed_console_warnings_remaining_ = kMaxAllowedConsoleWarnings;

  // Textures with mailboxes that should be dissociated before device.destroy().
  HeapHashSet<WeakMember<GPUTexture>> textures_with_mailbox_;

  HeapHashSet<WeakMember<GPUBuffer>> mappable_buffers_;

  Member<ExternalTextureCache> external_texture_cache_;

  std::bitset<static_cast<size_t>(GPUSingletonWarning::kCount)>
      singleton_warning_fired_;

  // This attribute records that whether GPUDevice is destroyed (via destroy()).
  bool destroyed_ = false;
};

}  // namespace blink

#endif  // THIRD_PARTY_BLINK_RENDERER_MODULES_WEBGPU_GPU_DEVICE_H_