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 UTILS_DAWNHELPERS_H_ 16 #define UTILS_DAWNHELPERS_H_ 17 18 #include <dawn/webgpu_cpp.h> 19 20 #include <array> 21 #include <initializer_list> 22 #include <vector> 23 24 #include "common/Constants.h" 25 #include "utils/TextureUtils.h" 26 27 namespace utils { 28 29 enum Expectation { Success, Failure }; 30 31 wgpu::ShaderModule CreateShaderModuleFromASM(const wgpu::Device& device, const char* source); 32 wgpu::ShaderModule CreateShaderModule(const wgpu::Device& device, const char* source); 33 34 wgpu::Buffer CreateBufferFromData(const wgpu::Device& device, 35 const void* data, 36 uint64_t size, 37 wgpu::BufferUsage usage); 38 39 template <typename T> CreateBufferFromData(const wgpu::Device & device,wgpu::BufferUsage usage,std::initializer_list<T> data)40 wgpu::Buffer CreateBufferFromData(const wgpu::Device& device, 41 wgpu::BufferUsage usage, 42 std::initializer_list<T> data) { 43 return CreateBufferFromData(device, data.begin(), uint32_t(sizeof(T) * data.size()), usage); 44 } 45 46 wgpu::ImageCopyBuffer CreateImageCopyBuffer(wgpu::Buffer buffer, 47 uint64_t offset, 48 uint32_t bytesPerRow, 49 uint32_t rowsPerImage = wgpu::kCopyStrideUndefined); 50 wgpu::ImageCopyTexture CreateImageCopyTexture( 51 wgpu::Texture texture, 52 uint32_t level, 53 wgpu::Origin3D origin, 54 wgpu::TextureAspect aspect = wgpu::TextureAspect::All); 55 wgpu::TextureDataLayout CreateTextureDataLayout( 56 uint64_t offset, 57 uint32_t bytesPerRow, 58 uint32_t rowsPerImage = wgpu::kCopyStrideUndefined); 59 60 struct ComboRenderPassDescriptor : public wgpu::RenderPassDescriptor { 61 public: 62 ComboRenderPassDescriptor(std::initializer_list<wgpu::TextureView> colorAttachmentInfo, 63 wgpu::TextureView depthStencil = wgpu::TextureView()); 64 65 ComboRenderPassDescriptor(const ComboRenderPassDescriptor& otherRenderPass); 66 const ComboRenderPassDescriptor& operator=( 67 const ComboRenderPassDescriptor& otherRenderPass); 68 69 std::array<wgpu::RenderPassColorAttachment, kMaxColorAttachments> cColorAttachments; 70 wgpu::RenderPassDepthStencilAttachment cDepthStencilAttachmentInfo = {}; 71 }; 72 73 struct BasicRenderPass { 74 public: 75 BasicRenderPass(); 76 BasicRenderPass(uint32_t width, 77 uint32_t height, 78 wgpu::Texture color, 79 wgpu::TextureFormat texture = kDefaultColorFormat); 80 81 static constexpr wgpu::TextureFormat kDefaultColorFormat = wgpu::TextureFormat::RGBA8Unorm; 82 83 uint32_t width; 84 uint32_t height; 85 wgpu::Texture color; 86 wgpu::TextureFormat colorFormat; 87 utils::ComboRenderPassDescriptor renderPassInfo; 88 }; 89 BasicRenderPass CreateBasicRenderPass( 90 const wgpu::Device& device, 91 uint32_t width, 92 uint32_t height, 93 wgpu::TextureFormat format = BasicRenderPass::kDefaultColorFormat); 94 95 wgpu::PipelineLayout MakeBasicPipelineLayout(const wgpu::Device& device, 96 const wgpu::BindGroupLayout* bindGroupLayout); 97 98 wgpu::PipelineLayout MakePipelineLayout(const wgpu::Device& device, 99 std::vector<wgpu::BindGroupLayout> bgls); 100 101 extern wgpu::ExternalTextureBindingLayout kExternalTextureBindingLayout; 102 103 // Helpers to make creating bind group layouts look nicer: 104 // 105 // utils::MakeBindGroupLayout(device, { 106 // {0, wgpu::ShaderStage::Vertex, wgpu::BufferBindingType::Uniform}, 107 // {1, wgpu::ShaderStage::Fragment, wgpu::SamplerBindingType::Filtering}, 108 // {3, wgpu::ShaderStage::Fragment, wgpu::TextureSampleType::Float} 109 // }); 110 111 struct BindingLayoutEntryInitializationHelper : wgpu::BindGroupLayoutEntry { 112 BindingLayoutEntryInitializationHelper(uint32_t entryBinding, 113 wgpu::ShaderStage entryVisibility, 114 wgpu::BufferBindingType bufferType, 115 bool bufferHasDynamicOffset = false, 116 uint64_t bufferMinBindingSize = 0); 117 BindingLayoutEntryInitializationHelper(uint32_t entryBinding, 118 wgpu::ShaderStage entryVisibility, 119 wgpu::SamplerBindingType samplerType); 120 BindingLayoutEntryInitializationHelper( 121 uint32_t entryBinding, 122 wgpu::ShaderStage entryVisibility, 123 wgpu::TextureSampleType textureSampleType, 124 wgpu::TextureViewDimension viewDimension = wgpu::TextureViewDimension::e2D, 125 bool textureMultisampled = false); 126 BindingLayoutEntryInitializationHelper( 127 uint32_t entryBinding, 128 wgpu::ShaderStage entryVisibility, 129 wgpu::StorageTextureAccess storageTextureAccess, 130 wgpu::TextureFormat format, 131 wgpu::TextureViewDimension viewDimension = wgpu::TextureViewDimension::e2D); 132 BindingLayoutEntryInitializationHelper(uint32_t entryBinding, 133 wgpu::ShaderStage entryVisibility, 134 wgpu::ExternalTextureBindingLayout* bindingLayout); 135 136 BindingLayoutEntryInitializationHelper(const wgpu::BindGroupLayoutEntry& entry); 137 }; 138 139 wgpu::BindGroupLayout MakeBindGroupLayout( 140 const wgpu::Device& device, 141 std::initializer_list<BindingLayoutEntryInitializationHelper> entriesInitializer); 142 143 // Helpers to make creating bind groups look nicer: 144 // 145 // utils::MakeBindGroup(device, layout, { 146 // {0, mySampler}, 147 // {1, myBuffer, offset, size}, 148 // {3, myTextureView} 149 // }); 150 151 // Structure with one constructor per-type of bindings, so that the initializer_list accepts 152 // bindings with the right type and no extra information. 153 struct BindingInitializationHelper { 154 BindingInitializationHelper(uint32_t binding, const wgpu::Sampler& sampler); 155 BindingInitializationHelper(uint32_t binding, const wgpu::TextureView& textureView); 156 BindingInitializationHelper(uint32_t binding, const wgpu::ExternalTexture& externalTexture); 157 BindingInitializationHelper(uint32_t binding, 158 const wgpu::Buffer& buffer, 159 uint64_t offset = 0, 160 uint64_t size = wgpu::kWholeSize); 161 162 wgpu::BindGroupEntry GetAsBinding() const; 163 164 uint32_t binding; 165 wgpu::Sampler sampler; 166 wgpu::TextureView textureView; 167 wgpu::Buffer buffer; 168 wgpu::ExternalTextureBindingEntry externalTextureBindingEntry; 169 uint64_t offset = 0; 170 uint64_t size = 0; 171 }; 172 173 wgpu::BindGroup MakeBindGroup( 174 const wgpu::Device& device, 175 const wgpu::BindGroupLayout& layout, 176 std::initializer_list<BindingInitializationHelper> entriesInitializer); 177 178 } // namespace utils 179 180 #endif // UTILS_DAWNHELPERS_H_ 181