Ben Clayton | abb5785 | 2019-03-01 14:33:35 +0000 | [diff] [blame] | 1 | // Copyright 2019 The SwiftShader Authors. All Rights Reserved. |
| 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 | #include <vulkan/vulkan_core.h> |
| 16 | |
Ben Clayton | 40f3d98 | 2019-03-21 18:26:46 +0000 | [diff] [blame] | 17 | #include <memory> |
Ben Clayton | abb5785 | 2019-03-01 14:33:35 +0000 | [diff] [blame] | 18 | #include <vector> |
| 19 | |
| 20 | class Driver; |
| 21 | |
| 22 | // Device provides a wrapper around a VkDevice with a number of helper functions |
| 23 | // for common test operations. |
| 24 | class Device |
| 25 | { |
| 26 | public: |
| 27 | Device(); |
Ben Clayton | 40f3d98 | 2019-03-21 18:26:46 +0000 | [diff] [blame] | 28 | ~Device(); |
Ben Clayton | abb5785 | 2019-03-01 14:33:35 +0000 | [diff] [blame] | 29 | |
| 30 | // CreateComputeDevice enumerates the physical devices, looking for a device |
| 31 | // that supports compute. |
| 32 | // If a compatible physical device is found, then a device is created and |
| 33 | // assigned to out. |
| 34 | // If a compatible physical device is not found, VK_SUCCESS will still be |
| 35 | // returned (as there was no Vulkan error), but calling Device::IsValid() |
| 36 | // on this device will return false. |
| 37 | static VkResult CreateComputeDevice( |
Ben Clayton | 40f3d98 | 2019-03-21 18:26:46 +0000 | [diff] [blame] | 38 | Driver const *driver, VkInstance instance, std::unique_ptr<Device>& out); |
Ben Clayton | abb5785 | 2019-03-01 14:33:35 +0000 | [diff] [blame] | 39 | |
| 40 | // IsValid returns true if the Device is initialized and can be used. |
| 41 | bool IsValid() const; |
| 42 | |
| 43 | // CreateBuffer creates a new buffer with the |
| 44 | // VK_BUFFER_USAGE_STORAGE_BUFFER_BIT usage, and |
| 45 | // VK_SHARING_MODE_EXCLUSIVE sharing mode. |
| 46 | VkResult CreateStorageBuffer(VkDeviceMemory memory, VkDeviceSize size, |
| 47 | VkDeviceSize offset, VkBuffer *out) const; |
| 48 | |
| 49 | // CreateShaderModule creates a new shader module with the given SPIR-V |
| 50 | // code. |
| 51 | VkResult CreateShaderModule(const std::vector<uint32_t> &spirv, |
| 52 | VkShaderModule *out) const; |
| 53 | |
| 54 | // CreateDescriptorSetLayout creates a new descriptor set layout with the |
| 55 | // given bindings. |
| 56 | VkResult CreateDescriptorSetLayout( |
| 57 | const std::vector<VkDescriptorSetLayoutBinding> &bindings, |
| 58 | VkDescriptorSetLayout *out) const; |
| 59 | |
| 60 | // CreatePipelineLayout creates a new single set descriptor set layout. |
| 61 | VkResult CreatePipelineLayout(VkDescriptorSetLayout layout, |
| 62 | VkPipelineLayout *out) const; |
| 63 | |
| 64 | // CreateComputePipeline creates a new compute pipeline with the entry point |
| 65 | // "main". |
| 66 | VkResult CreateComputePipeline(VkShaderModule module, |
| 67 | VkPipelineLayout pipelineLayout, |
| 68 | VkPipeline *out) const; |
| 69 | |
| 70 | // CreateStorageBufferDescriptorPool creates a new descriptor pool that can |
| 71 | // hold descriptorCount storage buffers. |
| 72 | VkResult CreateStorageBufferDescriptorPool(uint32_t descriptorCount, |
| 73 | VkDescriptorPool *out) const; |
| 74 | |
| 75 | // AllocateDescriptorSet allocates a single descriptor set with the given |
| 76 | // layout from pool. |
| 77 | VkResult AllocateDescriptorSet(VkDescriptorPool pool, |
| 78 | VkDescriptorSetLayout layout, |
| 79 | VkDescriptorSet *out) const; |
| 80 | |
| 81 | // UpdateStorageBufferDescriptorSets updates the storage buffers in |
| 82 | // descriptorSet with the given list of VkDescriptorBufferInfos. |
| 83 | void UpdateStorageBufferDescriptorSets(VkDescriptorSet descriptorSet, |
| 84 | const std::vector<VkDescriptorBufferInfo> &bufferInfos) const; |
| 85 | |
| 86 | // AllocateMemory allocates size bytes from a memory heap that has all the |
| 87 | // given flag bits set. |
| 88 | // If memory could not be allocated from any heap then |
| 89 | // VK_ERROR_OUT_OF_DEVICE_MEMORY is returned. |
| 90 | VkResult AllocateMemory(size_t size, VkMemoryPropertyFlags flags, VkDeviceMemory* out) const; |
| 91 | |
| 92 | // MapMemory wraps vkMapMemory, supplying the first VkDevice parameter. |
| 93 | VkResult MapMemory(VkDeviceMemory memory, VkDeviceSize offset, |
| 94 | VkDeviceSize size, VkMemoryMapFlags flags, void **ppData) const; |
| 95 | |
| 96 | // UnmapMemory wraps vkUnmapMemory, supplying the first VkDevice parameter. |
| 97 | void UnmapMemory(VkDeviceMemory memory) const; |
| 98 | |
| 99 | // CreateCommandPool creates a new command pool. |
| 100 | VkResult CreateCommandPool(VkCommandPool* out) const; |
| 101 | |
| 102 | // AllocateCommandBuffer creates a new command buffer with a primary level. |
| 103 | VkResult AllocateCommandBuffer(VkCommandPool pool, VkCommandBuffer* out) const; |
| 104 | |
| 105 | // BeginCommandBuffer begins writing to commandBuffer. |
Nicolas Capens | da1ed06 | 2019-03-12 11:23:28 -0400 | [diff] [blame] | 106 | VkResult BeginCommandBuffer(VkCommandBufferUsageFlags usage, VkCommandBuffer commandBuffer) const; |
Ben Clayton | abb5785 | 2019-03-01 14:33:35 +0000 | [diff] [blame] | 107 | |
| 108 | // QueueSubmitAndWait submits the given command buffer and waits for it to |
| 109 | // complete. |
| 110 | VkResult QueueSubmitAndWait(VkCommandBuffer commandBuffer) const; |
| 111 | |
| 112 | private: |
| 113 | Device(Driver const *driver, VkDevice device, VkPhysicalDevice physicalDevice, uint32_t queueFamilyIndex); |
| 114 | |
| 115 | static VkResult GetPhysicalDevices( |
| 116 | Driver const *driver, VkInstance instance, |
| 117 | std::vector<VkPhysicalDevice> &out); |
| 118 | |
| 119 | static int GetComputeQueueFamilyIndex( |
| 120 | Driver const *driver, VkPhysicalDevice device); |
| 121 | |
| 122 | static std::vector<VkQueueFamilyProperties> |
| 123 | GetPhysicalDeviceQueueFamilyProperties( |
| 124 | Driver const *driver, VkPhysicalDevice device); |
| 125 | |
| 126 | Driver const *driver; |
| 127 | VkDevice device; |
| 128 | VkPhysicalDevice physicalDevice; |
| 129 | uint32_t queueFamilyIndex; |
| 130 | }; |