1 // Copyright 2019 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #ifndef THIRD_PARTY_BLINK_RENDERER_MODULES_WEBGPU_DAWN_CONVERSIONS_H_
6 #define THIRD_PARTY_BLINK_RENDERER_MODULES_WEBGPU_DAWN_CONVERSIONS_H_
7 
8 #include <dawn/webgpu.h>
9 
10 #include <memory>
11 
12 #include "base/check.h"
13 #include "third_party/blink/renderer/modules/webgpu/dawn_object.h"
14 #include "third_party/blink/renderer/platform/heap/heap_allocator.h"
15 #include "third_party/blink/renderer/platform/heap/member.h"
16 
17 // This file provides helpers for converting WebGPU objects, descriptors,
18 // and enums from Blink to Dawn types.
19 
20 namespace blink {
21 
22 class DoubleSequenceOrGPUColorDict;
23 class GPUColorDict;
24 class GPUProgrammableStageDescriptor;
25 class GPUTextureCopyView;
26 class GPUTextureDataLayout;
27 class UnsignedLongEnforceRangeSequenceOrGPUExtent3DDict;
28 class UnsignedLongEnforceRangeSequenceOrGPUOrigin3DDict;
29 
30 // Convert WebGPU bitfield values to Dawn enums. These have the same value.
31 template <typename DawnEnum>
AsDawnEnum(uint32_t webgpu_enum)32 DawnEnum AsDawnEnum(uint32_t webgpu_enum) {
33   return static_cast<DawnEnum>(webgpu_enum);
34 }
35 
36 // Convert WebGPU string enums to Dawn enums.
37 template <typename DawnEnum>
38 DawnEnum AsDawnEnum(const WTF::String& webgpu_enum);
39 
40 // These conversions are used multiple times and are declared here. Conversions
41 // used only once, for example for object construction, are defined
42 // individually.
43 WGPUColor AsDawnColor(const Vector<double>&);
44 WGPUColor AsDawnType(const GPUColorDict*);
45 WGPUColor AsDawnType(const DoubleSequenceOrGPUColorDict*);
46 WGPUExtent3D AsDawnType(
47     const UnsignedLongEnforceRangeSequenceOrGPUExtent3DDict*);
48 WGPUOrigin3D AsDawnType(
49     const UnsignedLongEnforceRangeSequenceOrGPUOrigin3DDict*);
50 WGPUTextureCopyView AsDawnType(const GPUTextureCopyView* webgpu_view,
51                                GPUDevice* device);
52 const char* ValidateTextureDataLayout(const GPUTextureDataLayout* webgpu_layout,
53                                       WGPUTextureDataLayout* layout);
54 using OwnedProgrammableStageDescriptor =
55     std::tuple<WGPUProgrammableStageDescriptor, std::unique_ptr<char[]>>;
56 OwnedProgrammableStageDescriptor AsDawnType(
57     const GPUProgrammableStageDescriptor*);
58 
59 // WebGPU objects are converted to Dawn objects by getting the opaque handle
60 // which can be passed to Dawn.
61 template <typename Handle>
AsDawnType(const DawnObject<Handle> * object)62 Handle AsDawnType(const DawnObject<Handle>* object) {
63   DCHECK(object);
64   return object->GetHandle();
65 }
66 
67 template <typename WebGPUType>
68 using TypeOfDawnType = decltype(AsDawnType(std::declval<const WebGPUType*>()));
69 
70 // Helper for converting a list of objects to Dawn structs or handles
71 template <typename WebGPUType>
AsDawnType(const HeapVector<Member<WebGPUType>> & webgpu_objects)72 std::unique_ptr<TypeOfDawnType<WebGPUType>[]> AsDawnType(
73     const HeapVector<Member<WebGPUType>>& webgpu_objects) {
74   using DawnType = TypeOfDawnType<WebGPUType>;
75 
76   wtf_size_t count = webgpu_objects.size();
77   // TODO(enga): Pass in temporary memory or an allocator so we don't make a
78   // separate memory allocation here.
79   std::unique_ptr<DawnType[]> dawn_objects(new DawnType[count]);
80   for (wtf_size_t i = 0; i < count; ++i) {
81     dawn_objects[i] = AsDawnType(webgpu_objects[i].Get());
82   }
83   return dawn_objects;
84 }
85 
86 template <typename DawnEnum, typename WebGPUEnum>
AsDawnEnum(const Vector<WebGPUEnum> & webgpu_enums)87 std::unique_ptr<DawnEnum[]> AsDawnEnum(const Vector<WebGPUEnum>& webgpu_enums) {
88   wtf_size_t count = webgpu_enums.size();
89   // TODO(enga): Pass in temporary memory or an allocator so we don't make a
90   // separate memory allocation here.
91   std::unique_ptr<DawnEnum[]> dawn_enums(new DawnEnum[count]);
92   for (wtf_size_t i = 0; i < count; ++i) {
93     dawn_enums[i] = AsDawnEnum<DawnEnum>(webgpu_enums[i]);
94   }
95   return dawn_enums;
96 }
97 
98 }  // namespace blink
99 
100 #endif  // THIRD_PARTY_BLINK_RENDERER_MODULES_WEBGPU_DAWN_CONVERSIONS_H_
101