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/dawncpp.h> 19 20 #include <array> 21 #include <initializer_list> 22 23 #include "common/Constants.h" 24 25 namespace utils { 26 27 enum Expectation { Success, Failure }; 28 29 enum class ShaderStage { Vertex, Fragment, Compute }; 30 31 dawn::ShaderModule CreateShaderModule(const dawn::Device& device, 32 ShaderStage stage, 33 const char* source); 34 dawn::ShaderModule CreateShaderModuleFromASM(const dawn::Device& device, const char* source); 35 36 dawn::Buffer CreateBufferFromData(const dawn::Device& device, 37 const void* data, 38 uint64_t size, 39 dawn::BufferUsageBit usage); 40 41 template <typename T> CreateBufferFromData(const dawn::Device & device,dawn::BufferUsageBit usage,std::initializer_list<T> data)42 dawn::Buffer CreateBufferFromData(const dawn::Device& device, 43 dawn::BufferUsageBit usage, 44 std::initializer_list<T> data) { 45 return CreateBufferFromData(device, data.begin(), uint32_t(sizeof(T) * data.size()), usage); 46 } 47 48 dawn::BufferCopyView CreateBufferCopyView(dawn::Buffer buffer, 49 uint64_t offset, 50 uint32_t rowPitch, 51 uint32_t imageHeight); 52 dawn::TextureCopyView CreateTextureCopyView(dawn::Texture texture, 53 uint32_t level, 54 uint32_t slice, 55 dawn::Origin3D origin); 56 57 struct ComboRenderPassDescriptor : public dawn::RenderPassDescriptor { 58 public: 59 ComboRenderPassDescriptor(std::initializer_list<dawn::TextureView> colorAttachmentInfo, 60 dawn::TextureView depthStencil = dawn::TextureView()); 61 const ComboRenderPassDescriptor& operator=( 62 const ComboRenderPassDescriptor& otherRenderPass); 63 64 dawn::RenderPassColorAttachmentDescriptor* cColorAttachmentsInfoPtr[kMaxColorAttachments]; 65 dawn::RenderPassDepthStencilAttachmentDescriptor cDepthStencilAttachmentInfo; 66 67 private: 68 std::array<dawn::RenderPassColorAttachmentDescriptor, kMaxColorAttachments> 69 mColorAttachmentsInfo; 70 }; 71 72 struct BasicRenderPass { 73 public: 74 BasicRenderPass(); 75 BasicRenderPass(uint32_t width, 76 uint32_t height, 77 dawn::Texture color, 78 dawn::TextureFormat texture = kDefaultColorFormat); 79 80 static constexpr dawn::TextureFormat kDefaultColorFormat = dawn::TextureFormat::RGBA8Unorm; 81 82 uint32_t width; 83 uint32_t height; 84 dawn::Texture color; 85 dawn::TextureFormat colorFormat; 86 utils::ComboRenderPassDescriptor renderPassInfo; 87 }; 88 BasicRenderPass CreateBasicRenderPass(const dawn::Device& device, 89 uint32_t width, 90 uint32_t height); 91 92 dawn::SamplerDescriptor GetDefaultSamplerDescriptor(); 93 dawn::PipelineLayout MakeBasicPipelineLayout(const dawn::Device& device, 94 const dawn::BindGroupLayout* bindGroupLayout); 95 dawn::BindGroupLayout MakeBindGroupLayout( 96 const dawn::Device& device, 97 std::initializer_list<dawn::BindGroupLayoutBinding> bindingsInitializer); 98 99 // Helpers to make creating bind groups look nicer: 100 // 101 // utils::MakeBindGroup(device, layout, { 102 // {0, mySampler}, 103 // {1, myBuffer, offset, size}, 104 // {3, myTextureView} 105 // }); 106 107 // Structure with one constructor per-type of bindings, so that the initializer_list accepts 108 // bindings with the right type and no extra information. 109 struct BindingInitializationHelper { 110 BindingInitializationHelper(uint32_t binding, const dawn::Sampler& sampler); 111 BindingInitializationHelper(uint32_t binding, const dawn::TextureView& textureView); 112 BindingInitializationHelper(uint32_t binding, 113 const dawn::Buffer& buffer, 114 uint64_t offset, 115 uint64_t size); 116 117 dawn::BindGroupBinding GetAsBinding() const; 118 119 uint32_t binding; 120 dawn::Sampler sampler; 121 dawn::TextureView textureView; 122 dawn::Buffer buffer; 123 uint64_t offset = 0; 124 uint64_t size = 0; 125 }; 126 127 dawn::BindGroup MakeBindGroup( 128 const dawn::Device& device, 129 const dawn::BindGroupLayout& layout, 130 std::initializer_list<BindingInitializationHelper> bindingsInitializer); 131 132 } // namespace utils 133 134 #endif // UTILS_DAWNHELPERS_H_ 135