• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2017 The Dawn Authors
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //     http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #ifndef DAWNNATIVE_UTILS_WGPUHELPERS_H_
16 #define DAWNNATIVE_UTILS_WGPUHELPERS_H_
17 
18 #include <dawn_native/dawn_platform.h>
19 
20 #include <array>
21 #include <initializer_list>
22 #include <vector>
23 
24 #include "common/RefCounted.h"
25 #include "dawn_native/Error.h"
26 
27 namespace dawn_native { namespace utils {
28 
29     ResultOrError<Ref<ShaderModuleBase>> CreateShaderModule(DeviceBase* device, const char* source);
30 
31     ResultOrError<Ref<BufferBase>> CreateBufferFromData(DeviceBase* device,
32                                                         wgpu::BufferUsage usage,
33                                                         const void* data,
34                                                         uint64_t size);
35 
36     template <typename T>
CreateBufferFromData(DeviceBase * device,wgpu::BufferUsage usage,std::initializer_list<T> data)37     ResultOrError<Ref<BufferBase>> CreateBufferFromData(DeviceBase* device,
38                                                         wgpu::BufferUsage usage,
39                                                         std::initializer_list<T> data) {
40         return CreateBufferFromData(device, usage, data.begin(), uint32_t(sizeof(T) * data.size()));
41     }
42 
43     ResultOrError<Ref<PipelineLayoutBase>> MakeBasicPipelineLayout(
44         DeviceBase* device,
45         const Ref<BindGroupLayoutBase>& bindGroupLayout);
46 
47     // Helpers to make creating bind group layouts look nicer:
48     //
49     //   utils::MakeBindGroupLayout(device, {
50     //       {0, wgpu::ShaderStage::Vertex, wgpu::BufferBindingType::Uniform},
51     //       {1, wgpu::ShaderStage::Fragment, wgpu::SamplerBindingType::Filtering},
52     //       {3, wgpu::ShaderStage::Fragment, wgpu::TextureSampleType::Float}
53     //   });
54 
55     struct BindingLayoutEntryInitializationHelper : BindGroupLayoutEntry {
56         BindingLayoutEntryInitializationHelper(uint32_t entryBinding,
57                                                wgpu::ShaderStage entryVisibility,
58                                                wgpu::BufferBindingType bufferType,
59                                                bool bufferHasDynamicOffset = false,
60                                                uint64_t bufferMinBindingSize = 0);
61         BindingLayoutEntryInitializationHelper(uint32_t entryBinding,
62                                                wgpu::ShaderStage entryVisibility,
63                                                wgpu::SamplerBindingType samplerType);
64         BindingLayoutEntryInitializationHelper(
65             uint32_t entryBinding,
66             wgpu::ShaderStage entryVisibility,
67             wgpu::TextureSampleType textureSampleType,
68             wgpu::TextureViewDimension viewDimension = wgpu::TextureViewDimension::e2D,
69             bool textureMultisampled = false);
70         BindingLayoutEntryInitializationHelper(
71             uint32_t entryBinding,
72             wgpu::ShaderStage entryVisibility,
73             wgpu::StorageTextureAccess storageTextureAccess,
74             wgpu::TextureFormat format,
75             wgpu::TextureViewDimension viewDimension = wgpu::TextureViewDimension::e2D);
76 
77         BindingLayoutEntryInitializationHelper(const BindGroupLayoutEntry& entry);
78     };
79 
80     ResultOrError<Ref<BindGroupLayoutBase>> MakeBindGroupLayout(
81         DeviceBase* device,
82         std::initializer_list<BindingLayoutEntryInitializationHelper> entriesInitializer,
83         bool allowInternalBinding = false);
84 
85     // Helpers to make creating bind groups look nicer:
86     //
87     //   utils::MakeBindGroup(device, layout, {
88     //       {0, mySampler},
89     //       {1, myBuffer, offset, size},
90     //       {3, myTextureView}
91     //   });
92 
93     // Structure with one constructor per-type of bindings, so that the initializer_list accepts
94     // bindings with the right type and no extra information.
95     struct BindingInitializationHelper {
96         BindingInitializationHelper(uint32_t binding, const Ref<SamplerBase>& sampler);
97         BindingInitializationHelper(uint32_t binding, const Ref<TextureViewBase>& textureView);
98         BindingInitializationHelper(uint32_t binding,
99                                     const Ref<BufferBase>& buffer,
100                                     uint64_t offset = 0,
101                                     uint64_t size = wgpu::kWholeSize);
102         ~BindingInitializationHelper();
103 
104         BindGroupEntry GetAsBinding() const;
105 
106         uint32_t binding;
107         Ref<SamplerBase> sampler;
108         Ref<TextureViewBase> textureView;
109         Ref<BufferBase> buffer;
110         uint64_t offset = 0;
111         uint64_t size = 0;
112     };
113 
114     ResultOrError<Ref<BindGroupBase>> MakeBindGroup(
115         DeviceBase* device,
116         const Ref<BindGroupLayoutBase>& layout,
117         std::initializer_list<BindingInitializationHelper> entriesInitializer);
118 
119     const char* GetLabelForTrace(const char* label);
120 
121 }}  // namespace dawn_native::utils
122 
123 #endif  // DAWNNATIVE_UTILS_WGPUHELPERS_H_