1 // Copyright 2019 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_VULKAN_UTILSVULKAN_H_ 16 #define DAWNNATIVE_VULKAN_UTILSVULKAN_H_ 17 18 #include "common/vulkan_platform.h" 19 #include "dawn_native/Commands.h" 20 #include "dawn_native/dawn_platform.h" 21 22 namespace dawn_native { 23 struct ProgrammableStage; 24 union OverridableConstantScalar; 25 } // namespace dawn_native 26 27 namespace dawn_native { namespace vulkan { 28 29 class Device; 30 31 // A Helper type used to build a pNext chain of extension structs. 32 // Usage is: 33 // 1) Create instance, passing the address of the first struct in the 34 // chain. This will parse the existing |pNext| chain in it to find 35 // its tail. 36 // 37 // 2) Call Add(&vk_struct) every time a new struct needs to be appended 38 // to the chain. 39 // 40 // 3) Alternatively, call Add(&vk_struct, VK_STRUCTURE_TYPE_XXX) to 41 // initialize the struct with a given VkStructureType value while 42 // appending it to the chain. 43 // 44 // Examples: 45 // VkPhysicalFeatures2 features2 = { 46 // .sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FEATURES_2, 47 // .pNext = nullptr, 48 // }; 49 // 50 // PNextChainBuilder featuresChain(&features2); 51 // 52 // featuresChain.Add(&featuresExtensions.subgroupSizeControl, 53 // VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SUBGROUP_SIZE_CONTROL_FEATURES_EXT); 54 // 55 struct PNextChainBuilder { 56 // Constructor takes the address of a Vulkan structure instance, and 57 // walks its pNext chain to record the current location of its tail. 58 // 59 // NOTE: Some VK_STRUCT_TYPEs define their pNext field as a const void* 60 // which is why the VkBaseOutStructure* casts below are necessary. 61 template <typename VK_STRUCT_TYPE> PNextChainBuilderPNextChainBuilder62 explicit PNextChainBuilder(VK_STRUCT_TYPE* head) 63 : mCurrent(reinterpret_cast<VkBaseOutStructure*>(head)) { 64 // Find the end of the current chain. 65 while (mCurrent->pNext != nullptr) { 66 mCurrent = mCurrent->pNext; 67 } 68 } 69 70 // Add one item to the chain. |vk_struct| must be a Vulkan structure 71 // that is already initialized. 72 template <typename VK_STRUCT_TYPE> AddPNextChainBuilder73 void Add(VK_STRUCT_TYPE* vkStruct) { 74 // Sanity checks to ensure proper type safety. 75 static_assert( 76 offsetof(VK_STRUCT_TYPE, sType) == offsetof(VkBaseOutStructure, sType) && 77 offsetof(VK_STRUCT_TYPE, pNext) == offsetof(VkBaseOutStructure, pNext), 78 "Argument type is not a proper Vulkan structure type"); 79 vkStruct->pNext = nullptr; 80 81 mCurrent->pNext = reinterpret_cast<VkBaseOutStructure*>(vkStruct); 82 mCurrent = mCurrent->pNext; 83 } 84 85 // A variant of Add() above that also initializes the |sType| field in |vk_struct|. 86 template <typename VK_STRUCT_TYPE> AddPNextChainBuilder87 void Add(VK_STRUCT_TYPE* vkStruct, VkStructureType sType) { 88 vkStruct->sType = sType; 89 Add(vkStruct); 90 } 91 92 private: 93 VkBaseOutStructure* mCurrent; 94 }; 95 96 VkCompareOp ToVulkanCompareOp(wgpu::CompareFunction op); 97 98 VkImageAspectFlags VulkanAspectMask(const Aspect& aspects); 99 100 Extent3D ComputeTextureCopyExtent(const TextureCopy& textureCopy, const Extent3D& copySize); 101 102 VkBufferImageCopy ComputeBufferImageCopyRegion(const BufferCopy& bufferCopy, 103 const TextureCopy& textureCopy, 104 const Extent3D& copySize); 105 VkBufferImageCopy ComputeBufferImageCopyRegion(const TextureDataLayout& dataLayout, 106 const TextureCopy& textureCopy, 107 const Extent3D& copySize); 108 109 void SetDebugName(Device* device, 110 VkObjectType objectType, 111 uint64_t objectHandle, 112 const char* prefix, 113 std::string label = ""); 114 115 // Returns nullptr or &specializationInfo 116 // specializationInfo, specializationDataEntries, specializationMapEntries needs to 117 // be alive at least until VkSpecializationInfo is passed into Vulkan Create*Pipelines 118 VkSpecializationInfo* GetVkSpecializationInfo( 119 const ProgrammableStage& programmableStage, 120 VkSpecializationInfo* specializationInfo, 121 std::vector<OverridableConstantScalar>* specializationDataEntries, 122 std::vector<VkSpecializationMapEntry>* specializationMapEntries); 123 124 }} // namespace dawn_native::vulkan 125 126 #endif // DAWNNATIVE_VULKAN_UTILSVULKAN_H_ 127