From 23b27a27e2e0514e6d1efc610b3f30df059aa855 Mon Sep 17 00:00:00 2001 From: Corentin Wallez Date: Fri, 24 Nov 2017 14:18:09 -0500 Subject: [PATCH] Format: src/backend/vulkan --- src/backend/vulkan/BufferUploader.cpp | 36 +++-- src/backend/vulkan/BufferUploader.h | 27 ++-- src/backend/vulkan/BufferVk.cpp | 27 ++-- src/backend/vulkan/BufferVk.h | 59 ++++--- src/backend/vulkan/MemoryAllocator.cpp | 35 ++-- src/backend/vulkan/MemoryAllocator.h | 48 +++--- src/backend/vulkan/VulkanBackend.cpp | 55 ++++--- src/backend/vulkan/VulkanBackend.h | 211 ++++++++++++------------- src/backend/vulkan/VulkanFunctions.cpp | 42 ++--- src/backend/vulkan/VulkanFunctions.h | 23 +-- src/backend/vulkan/VulkanInfo.cpp | 39 +++-- src/backend/vulkan/VulkanInfo.h | 12 +- src/backend/vulkan/vulkan_platform.h | 2 +- 13 files changed, 317 insertions(+), 299 deletions(-) diff --git a/src/backend/vulkan/BufferUploader.cpp b/src/backend/vulkan/BufferUploader.cpp index ef798acf65..cf92dca957 100644 --- a/src/backend/vulkan/BufferUploader.cpp +++ b/src/backend/vulkan/BufferUploader.cpp @@ -19,19 +19,21 @@ #include -namespace backend { -namespace vulkan { +namespace backend { namespace vulkan { - BufferUploader::BufferUploader(Device* device) - : mDevice(device) { + BufferUploader::BufferUploader(Device* device) : mDevice(device) { } BufferUploader::~BufferUploader() { ASSERT(mBuffersToDelete.Empty()); } - void BufferUploader::BufferSubData(VkBuffer buffer, VkDeviceSize offset, VkDeviceSize size, const void* data) { - // TODO(cwallez@chromium.org): this is soooooo bad. We should use some sort of ring buffer for this. + void BufferUploader::BufferSubData(VkBuffer buffer, + VkDeviceSize offset, + VkDeviceSize size, + const void* data) { + // TODO(cwallez@chromium.org): this is soooooo bad. We should use some sort of ring buffer + // for this. // Create a staging buffer VkBufferCreateInfo createInfo; @@ -45,20 +47,23 @@ namespace vulkan { createInfo.pQueueFamilyIndices = 0; VkBuffer stagingBuffer = VK_NULL_HANDLE; - if (mDevice->fn.CreateBuffer(mDevice->GetVkDevice(), &createInfo, nullptr, &stagingBuffer) != VK_SUCCESS) { + if (mDevice->fn.CreateBuffer(mDevice->GetVkDevice(), &createInfo, nullptr, + &stagingBuffer) != VK_SUCCESS) { ASSERT(false); } VkMemoryRequirements requirements; - mDevice->fn.GetBufferMemoryRequirements(mDevice->GetVkDevice(), stagingBuffer, &requirements); + mDevice->fn.GetBufferMemoryRequirements(mDevice->GetVkDevice(), stagingBuffer, + &requirements); DeviceMemoryAllocation allocation; if (!mDevice->GetMemoryAllocator()->Allocate(requirements, true, &allocation)) { ASSERT(false); } - if (mDevice->fn.BindBufferMemory(mDevice->GetVkDevice(), stagingBuffer, allocation.GetMemory(), - allocation.GetMemoryOffset()) != VK_SUCCESS) { + if (mDevice->fn.BindBufferMemory(mDevice->GetVkDevice(), stagingBuffer, + allocation.GetMemory(), + allocation.GetMemoryOffset()) != VK_SUCCESS) { ASSERT(false); } @@ -75,11 +80,9 @@ namespace vulkan { barrier.srcAccessMask = VK_ACCESS_HOST_WRITE_BIT; barrier.dstAccessMask = VK_ACCESS_TRANSFER_READ_BIT; - mDevice->fn.CmdPipelineBarrier(commands, - VK_PIPELINE_STAGE_HOST_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT, 0, - 1, &barrier, - 0, nullptr, - 0, nullptr); + mDevice->fn.CmdPipelineBarrier(commands, VK_PIPELINE_STAGE_HOST_BIT, + VK_PIPELINE_STAGE_TRANSFER_BIT, 0, 1, &barrier, 0, nullptr, + 0, nullptr); VkBufferCopy copy; copy.srcOffset = 0; @@ -100,5 +103,4 @@ namespace vulkan { mBuffersToDelete.ClearUpTo(completedSerial); } -} -} +}} // namespace backend::vulkan diff --git a/src/backend/vulkan/BufferUploader.h b/src/backend/vulkan/BufferUploader.h index 7adc9499cc..94b83a3e2a 100644 --- a/src/backend/vulkan/BufferUploader.h +++ b/src/backend/vulkan/BufferUploader.h @@ -18,26 +18,27 @@ #include "backend/vulkan/vulkan_platform.h" #include "common/SerialQueue.h" -namespace backend { -namespace vulkan { +namespace backend { namespace vulkan { class Device; class BufferUploader { - public: - BufferUploader(Device* device); - ~BufferUploader(); + public: + BufferUploader(Device* device); + ~BufferUploader(); - void BufferSubData(VkBuffer buffer, VkDeviceSize offset, VkDeviceSize size, const void* data); + void BufferSubData(VkBuffer buffer, + VkDeviceSize offset, + VkDeviceSize size, + const void* data); - void Tick(Serial completedSerial); + void Tick(Serial completedSerial); - private: - Device* mDevice = nullptr; - SerialQueue mBuffersToDelete; + private: + Device* mDevice = nullptr; + SerialQueue mBuffersToDelete; }; -} -} +}} // namespace backend::vulkan -#endif // BACKEND_VULKAN_BUFFERUPLOADER_H_ +#endif // BACKEND_VULKAN_BUFFERUPLOADER_H_ diff --git a/src/backend/vulkan/BufferVk.cpp b/src/backend/vulkan/BufferVk.cpp index f777ca71e8..d7f11c6cbf 100644 --- a/src/backend/vulkan/BufferVk.cpp +++ b/src/backend/vulkan/BufferVk.cpp @@ -19,8 +19,7 @@ #include -namespace backend { -namespace vulkan { +namespace backend { namespace vulkan { namespace { @@ -49,10 +48,9 @@ namespace vulkan { return flags; } - } + } // namespace - Buffer::Buffer(BufferBuilder* builder) - : BufferBase(builder) { + Buffer::Buffer(BufferBuilder* builder) : BufferBase(builder) { Device* device = ToBackend(GetDevice()); VkBufferCreateInfo createInfo; @@ -65,19 +63,24 @@ namespace vulkan { createInfo.queueFamilyIndexCount = 0; createInfo.pQueueFamilyIndices = 0; - if (device->fn.CreateBuffer(device->GetVkDevice(), &createInfo, nullptr, &mHandle) != VK_SUCCESS) { + if (device->fn.CreateBuffer(device->GetVkDevice(), &createInfo, nullptr, &mHandle) != + VK_SUCCESS) { ASSERT(false); } VkMemoryRequirements requirements; device->fn.GetBufferMemoryRequirements(device->GetVkDevice(), mHandle, &requirements); - bool requestMappable = (GetAllowedUsage() & (nxt::BufferUsageBit::MapRead | nxt::BufferUsageBit::MapWrite)) != 0; - if (!device->GetMemoryAllocator()->Allocate(requirements, requestMappable, &mMemoryAllocation)) { + bool requestMappable = + (GetAllowedUsage() & (nxt::BufferUsageBit::MapRead | nxt::BufferUsageBit::MapWrite)) != + 0; + if (!device->GetMemoryAllocator()->Allocate(requirements, requestMappable, + &mMemoryAllocation)) { ASSERT(false); } - if (device->fn.BindBufferMemory(device->GetVkDevice(), mHandle, mMemoryAllocation.GetMemory(), + if (device->fn.BindBufferMemory(device->GetVkDevice(), mHandle, + mMemoryAllocation.GetMemory(), mMemoryAllocation.GetMemoryOffset()) != VK_SUCCESS) { ASSERT(false); } @@ -118,8 +121,7 @@ namespace vulkan { void Buffer::TransitionUsageImpl(nxt::BufferUsageBit, nxt::BufferUsageBit) { } - MapReadRequestTracker::MapReadRequestTracker(Device* device) - : mDevice(device) { + MapReadRequestTracker::MapReadRequestTracker(Device* device) : mDevice(device) { } MapReadRequestTracker::~MapReadRequestTracker() { @@ -142,5 +144,4 @@ namespace vulkan { mInflightRequests.ClearUpTo(finishedSerial); } -} -} +}} // namespace backend::vulkan diff --git a/src/backend/vulkan/BufferVk.h b/src/backend/vulkan/BufferVk.h index f92d7f2491..288a576784 100644 --- a/src/backend/vulkan/BufferVk.h +++ b/src/backend/vulkan/BufferVk.h @@ -17,52 +17,51 @@ #include "backend/Buffer.h" -#include "backend/vulkan/vulkan_platform.h" #include "backend/vulkan/MemoryAllocator.h" +#include "backend/vulkan/vulkan_platform.h" #include "common/SerialQueue.h" -namespace backend { -namespace vulkan { +namespace backend { namespace vulkan { class Device; class Buffer : public BufferBase { - public: - Buffer(BufferBuilder* builder); - ~Buffer(); + public: + Buffer(BufferBuilder* builder); + ~Buffer(); - void OnMapReadCommandSerialFinished(uint32_t mapSerial, const void* data); + void OnMapReadCommandSerialFinished(uint32_t mapSerial, const void* data); - private: - void SetSubDataImpl(uint32_t start, uint32_t count, const uint32_t* data) override; - void MapReadAsyncImpl(uint32_t serial, uint32_t start, uint32_t count) override; - void UnmapImpl() override; - void TransitionUsageImpl(nxt::BufferUsageBit currentUsage, nxt::BufferUsageBit targetUsage) override; + private: + void SetSubDataImpl(uint32_t start, uint32_t count, const uint32_t* data) override; + void MapReadAsyncImpl(uint32_t serial, uint32_t start, uint32_t count) override; + void UnmapImpl() override; + void TransitionUsageImpl(nxt::BufferUsageBit currentUsage, + nxt::BufferUsageBit targetUsage) override; - VkBuffer mHandle = VK_NULL_HANDLE; - DeviceMemoryAllocation mMemoryAllocation; + VkBuffer mHandle = VK_NULL_HANDLE; + DeviceMemoryAllocation mMemoryAllocation; }; class MapReadRequestTracker { - public: - MapReadRequestTracker(Device* device); - ~MapReadRequestTracker(); + public: + MapReadRequestTracker(Device* device); + ~MapReadRequestTracker(); - void Track(Buffer* buffer, uint32_t mapSerial, const void* data); - void Tick(Serial finishedSerial); + void Track(Buffer* buffer, uint32_t mapSerial, const void* data); + void Tick(Serial finishedSerial); - private: - Device* mDevice; + private: + Device* mDevice; - struct Request { - Ref buffer; - uint32_t mapSerial; - const void* data; - }; - SerialQueue mInflightRequests; + struct Request { + Ref buffer; + uint32_t mapSerial; + const void* data; + }; + SerialQueue mInflightRequests; }; -} -} +}} // namespace backend::vulkan -#endif // BACKEND_VULKAN_BUFFERVK_H_ +#endif // BACKEND_VULKAN_BUFFERVK_H_ diff --git a/src/backend/vulkan/MemoryAllocator.cpp b/src/backend/vulkan/MemoryAllocator.cpp index 48107bbaa2..1762666b45 100644 --- a/src/backend/vulkan/MemoryAllocator.cpp +++ b/src/backend/vulkan/MemoryAllocator.cpp @@ -15,8 +15,7 @@ #include "backend/vulkan/MemoryAllocator.h" #include "backend/vulkan/VulkanBackend.h" -namespace backend { -namespace vulkan { +namespace backend { namespace vulkan { DeviceMemoryAllocation::~DeviceMemoryAllocation() { ASSERT(mMemory == VK_NULL_HANDLE); @@ -34,15 +33,16 @@ namespace vulkan { return mMappedPointer; } - MemoryAllocator::MemoryAllocator(Device* device) - :mDevice(device) { + MemoryAllocator::MemoryAllocator(Device* device) : mDevice(device) { } MemoryAllocator::~MemoryAllocator() { ASSERT(mReleasedMemory.Empty()); } - bool MemoryAllocator::Allocate(VkMemoryRequirements requirements, bool mappable, DeviceMemoryAllocation* allocation) { + bool MemoryAllocator::Allocate(VkMemoryRequirements requirements, + bool mappable, + DeviceMemoryAllocation* allocation) { const VulkanDeviceInfo& info = mDevice->GetDeviceInfo(); // Find a suitable memory type for this allocation @@ -54,7 +54,8 @@ namespace vulkan { } // Mappable resource must be host visible - if (mappable && (info.memoryTypes[i].propertyFlags & VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT) == 0) { + if (mappable && + (info.memoryTypes[i].propertyFlags & VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT) == 0) { continue; } @@ -66,16 +67,20 @@ namespace vulkan { // For non-mappable resources, favor device local memory. if (!mappable) { - if ((info.memoryTypes[bestType].propertyFlags & VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT) == 0 && - (info.memoryTypes[i].propertyFlags & VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT) != 0) { + if ((info.memoryTypes[bestType].propertyFlags & + VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT) == 0 && + (info.memoryTypes[i].propertyFlags & VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT) != + 0) { bestType = static_cast(i); continue; } } // All things equal favor the memory in the biggest heap - VkDeviceSize bestTypeHeapSize = info.memoryHeaps[info.memoryTypes[bestType].heapIndex].size; - VkDeviceSize candidateHeapSize = info.memoryHeaps[info.memoryTypes[bestType].heapIndex].size; + VkDeviceSize bestTypeHeapSize = + info.memoryHeaps[info.memoryTypes[bestType].heapIndex].size; + VkDeviceSize candidateHeapSize = + info.memoryHeaps[info.memoryTypes[bestType].heapIndex].size; if (candidateHeapSize > bestTypeHeapSize) { bestType = static_cast(i); continue; @@ -95,14 +100,15 @@ namespace vulkan { allocateInfo.memoryTypeIndex = static_cast(bestType); VkDeviceMemory allocatedMemory = VK_NULL_HANDLE; - if (mDevice->fn.AllocateMemory(mDevice->GetVkDevice(), &allocateInfo, nullptr, &allocatedMemory) != VK_SUCCESS) { + if (mDevice->fn.AllocateMemory(mDevice->GetVkDevice(), &allocateInfo, nullptr, + &allocatedMemory) != VK_SUCCESS) { return false; } void* mappedPointer = nullptr; if (mappable) { - if (mDevice->fn.MapMemory(mDevice->GetVkDevice(), allocatedMemory, 0, requirements.size, 0, - &mappedPointer) != VK_SUCCESS) { + if (mDevice->fn.MapMemory(mDevice->GetVkDevice(), allocatedMemory, 0, requirements.size, + 0, &mappedPointer) != VK_SUCCESS) { return false; } } @@ -127,5 +133,4 @@ namespace vulkan { } mReleasedMemory.ClearUpTo(finishedSerial); } -} -} +}} // namespace backend::vulkan diff --git a/src/backend/vulkan/MemoryAllocator.h b/src/backend/vulkan/MemoryAllocator.h index 6c82d8b805..03e5f43c70 100644 --- a/src/backend/vulkan/MemoryAllocator.h +++ b/src/backend/vulkan/MemoryAllocator.h @@ -18,42 +18,42 @@ #include "backend/vulkan/vulkan_platform.h" #include "common/SerialQueue.h" -namespace backend { -namespace vulkan { +namespace backend { namespace vulkan { class Device; class MemoryAllocator; class DeviceMemoryAllocation { - public: - ~DeviceMemoryAllocation(); - VkDeviceMemory GetMemory() const; - size_t GetMemoryOffset() const; - uint8_t* GetMappedPointer() const; + public: + ~DeviceMemoryAllocation(); + VkDeviceMemory GetMemory() const; + size_t GetMemoryOffset() const; + uint8_t* GetMappedPointer() const; - private: - friend class MemoryAllocator; - VkDeviceMemory mMemory = VK_NULL_HANDLE; - size_t mOffset = 0; - uint8_t* mMappedPointer = nullptr; + private: + friend class MemoryAllocator; + VkDeviceMemory mMemory = VK_NULL_HANDLE; + size_t mOffset = 0; + uint8_t* mMappedPointer = nullptr; }; class MemoryAllocator { - public: - MemoryAllocator(Device* device); - ~MemoryAllocator(); + public: + MemoryAllocator(Device* device); + ~MemoryAllocator(); - bool Allocate(VkMemoryRequirements requirements, bool mappable, DeviceMemoryAllocation* allocation); - void Free(DeviceMemoryAllocation* allocation); + bool Allocate(VkMemoryRequirements requirements, + bool mappable, + DeviceMemoryAllocation* allocation); + void Free(DeviceMemoryAllocation* allocation); - void Tick(Serial finishedSerial); + void Tick(Serial finishedSerial); - private: - Device* mDevice = nullptr; - SerialQueue mReleasedMemory; + private: + Device* mDevice = nullptr; + SerialQueue mReleasedMemory; }; -} -} +}} // namespace backend::vulkan -#endif // BACKEND_VULKAN_MEMORYALLOCATOR_H_ +#endif // BACKEND_VULKAN_MEMORYALLOCATOR_H_ diff --git a/src/backend/vulkan/VulkanBackend.cpp b/src/backend/vulkan/VulkanBackend.cpp index 7bed353c1f..c9a92527fc 100644 --- a/src/backend/vulkan/VulkanBackend.cpp +++ b/src/backend/vulkan/VulkanBackend.cpp @@ -15,8 +15,8 @@ #include "backend/vulkan/VulkanBackend.h" #include "backend/Commands.h" -#include "backend/vulkan/BufferVk.h" #include "backend/vulkan/BufferUploader.h" +#include "backend/vulkan/BufferVk.h" #include "common/Platform.h" #include @@ -24,15 +24,14 @@ #include #if NXT_PLATFORM_LINUX - const char kVulkanLibName[] = "libvulkan.so.1"; +const char kVulkanLibName[] = "libvulkan.so.1"; #elif NXT_PLATFORM_WINDOWS - const char kVulkanLibName[] = "vulkan-1.dll"; +const char kVulkanLibName[] = "vulkan-1.dll"; #else - #error "Unimplemented Vulkan backend platform" +# error "Unimplemented Vulkan backend platform" #endif -namespace backend { -namespace vulkan { +namespace backend { namespace vulkan { nxtProcTable GetNonValidatingProcs(); nxtProcTable GetValidatingProcs(); @@ -330,16 +329,16 @@ namespace vulkan { std::vector layersToRequest; std::vector extensionsToRequest; - #if defined(NXT_ENABLE_ASSERTS) - if (mGlobalInfo.standardValidation) { - layersToRequest.push_back(kLayerNameLunargStandardValidation); - usedKnobs->standardValidation = true; - } - if (mGlobalInfo.debugReport) { - extensionsToRequest.push_back(kExtensionNameExtDebugReport); - usedKnobs->debugReport = true; - } - #endif +#if defined(NXT_ENABLE_ASSERTS) + if (mGlobalInfo.standardValidation) { + layersToRequest.push_back(kLayerNameLunargStandardValidation); + usedKnobs->standardValidation = true; + } + if (mGlobalInfo.debugReport) { + extensionsToRequest.push_back(kExtensionNameExtDebugReport); + usedKnobs->debugReport = true; + } +#endif VkApplicationInfo appInfo; appInfo.sType = VK_STRUCTURE_TYPE_APPLICATION_INFO; @@ -380,10 +379,12 @@ namespace vulkan { // Find a universal queue family { - constexpr uint32_t kUniversalFlags = VK_QUEUE_GRAPHICS_BIT | VK_QUEUE_COMPUTE_BIT | VK_QUEUE_TRANSFER_BIT; + constexpr uint32_t kUniversalFlags = + VK_QUEUE_GRAPHICS_BIT | VK_QUEUE_COMPUTE_BIT | VK_QUEUE_TRANSFER_BIT; int universalQueueFamily = -1; for (unsigned int i = 0; i < mDeviceInfo.queueFamilies.size(); ++i) { - if ((mDeviceInfo.queueFamilies[i].queueFlags & kUniversalFlags) == kUniversalFlags) { + if ((mDeviceInfo.queueFamilies[i].queueFlags & kUniversalFlags) == + kUniversalFlags) { universalQueueFamily = i; break; } @@ -439,7 +440,8 @@ namespace vulkan { createInfo.pfnCallback = Device::OnDebugReportCallback; createInfo.pUserData = this; - if (fn.CreateDebugReportCallbackEXT(mInstance, &createInfo, nullptr, &mDebugReportCallback) != VK_SUCCESS) { + if (fn.CreateDebugReportCallbackEXT(mInstance, &createInfo, nullptr, + &mDebugReportCallback) != VK_SUCCESS) { return false; } @@ -536,7 +538,8 @@ namespace vulkan { allocateInfo.level = VK_COMMAND_BUFFER_LEVEL_PRIMARY; allocateInfo.commandBufferCount = 1; - if (fn.AllocateCommandBuffers(mVkDevice, &allocateInfo, &commands.commandBuffer) != VK_SUCCESS) { + if (fn.AllocateCommandBuffers(mVkDevice, &allocateInfo, &commands.commandBuffer) != + VK_SUCCESS) { ASSERT(false); } @@ -565,8 +568,7 @@ namespace vulkan { // Queue - Queue::Queue(QueueBuilder* builder) - : QueueBase(builder) { + Queue::Queue(QueueBuilder* builder) : QueueBase(builder) { } Queue::~Queue() { @@ -577,8 +579,7 @@ namespace vulkan { // Texture - Texture::Texture(TextureBuilder* builder) - : TextureBase(builder) { + Texture::Texture(TextureBuilder* builder) : TextureBase(builder) { } Texture::~Texture() { @@ -589,8 +590,7 @@ namespace vulkan { // SwapChain - SwapChain::SwapChain(SwapChainBuilder* builder) - : SwapChainBase(builder) { + SwapChain::SwapChain(SwapChainBuilder* builder) : SwapChainBase(builder) { const auto& im = GetImplementation(); im.Init(im.userData, nullptr); } @@ -601,5 +601,4 @@ namespace vulkan { TextureBase* SwapChain::GetNextTextureImpl(TextureBuilder* builder) { return GetDevice()->CreateTexture(builder); } -} -} +}} // namespace backend::vulkan diff --git a/src/backend/vulkan/VulkanBackend.h b/src/backend/vulkan/VulkanBackend.h index 8345e35a84..73731bf59c 100644 --- a/src/backend/vulkan/VulkanBackend.h +++ b/src/backend/vulkan/VulkanBackend.h @@ -17,15 +17,13 @@ #include "nxt/nxtcpp.h" -#include "backend/vulkan/VulkanFunctions.h" -#include "backend/vulkan/VulkanInfo.h" #include "backend/BindGroup.h" #include "backend/BindGroupLayout.h" #include "backend/BlendState.h" -#include "backend/Device.h" #include "backend/CommandBuffer.h" #include "backend/ComputePipeline.h" #include "backend/DepthStencilState.h" +#include "backend/Device.h" #include "backend/Framebuffer.h" #include "backend/InputState.h" #include "backend/PipelineLayout.h" @@ -37,14 +35,15 @@ #include "backend/SwapChain.h" #include "backend/Texture.h" #include "backend/ToBackend.h" +#include "backend/vulkan/VulkanFunctions.h" +#include "backend/vulkan/VulkanInfo.h" #include "common/DynamicLib.h" #include "common/Serial.h" #include "common/SerialQueue.h" #include -namespace backend { -namespace vulkan { +namespace backend { namespace vulkan { using BindGroup = BindGroupBase; using BindGroupLayout = BindGroupLayoutBase; @@ -94,143 +93,143 @@ namespace vulkan { using TextureViewType = TextureView; }; - template + template auto ToBackend(T&& common) -> decltype(ToBackendBase(common)) { return ToBackendBase(common); } class Device : public DeviceBase { - public: - Device(); - ~Device(); + public: + Device(); + ~Device(); - BindGroupBase* CreateBindGroup(BindGroupBuilder* builder) override; - BindGroupLayoutBase* CreateBindGroupLayout(BindGroupLayoutBuilder* builder) override; - BlendStateBase* CreateBlendState(BlendStateBuilder* builder) override; - BufferBase* CreateBuffer(BufferBuilder* builder) override; - BufferViewBase* CreateBufferView(BufferViewBuilder* builder) override; - CommandBufferBase* CreateCommandBuffer(CommandBufferBuilder* builder) override; - ComputePipelineBase* CreateComputePipeline(ComputePipelineBuilder* builder) override; - DepthStencilStateBase* CreateDepthStencilState(DepthStencilStateBuilder* builder) override; - FramebufferBase* CreateFramebuffer(FramebufferBuilder* builder) override; - InputStateBase* CreateInputState(InputStateBuilder* builder) override; - PipelineLayoutBase* CreatePipelineLayout(PipelineLayoutBuilder* builder) override; - QueueBase* CreateQueue(QueueBuilder* builder) override; - RenderPassBase* CreateRenderPass(RenderPassBuilder* builder) override; - RenderPipelineBase* CreateRenderPipeline(RenderPipelineBuilder* builder) override; - SamplerBase* CreateSampler(SamplerBuilder* builder) override; - ShaderModuleBase* CreateShaderModule(ShaderModuleBuilder* builder) override; - SwapChainBase* CreateSwapChain(SwapChainBuilder* builder) override; - TextureBase* CreateTexture(TextureBuilder* builder) override; - TextureViewBase* CreateTextureView(TextureViewBuilder* builder) override; + BindGroupBase* CreateBindGroup(BindGroupBuilder* builder) override; + BindGroupLayoutBase* CreateBindGroupLayout(BindGroupLayoutBuilder* builder) override; + BlendStateBase* CreateBlendState(BlendStateBuilder* builder) override; + BufferBase* CreateBuffer(BufferBuilder* builder) override; + BufferViewBase* CreateBufferView(BufferViewBuilder* builder) override; + CommandBufferBase* CreateCommandBuffer(CommandBufferBuilder* builder) override; + ComputePipelineBase* CreateComputePipeline(ComputePipelineBuilder* builder) override; + DepthStencilStateBase* CreateDepthStencilState(DepthStencilStateBuilder* builder) override; + FramebufferBase* CreateFramebuffer(FramebufferBuilder* builder) override; + InputStateBase* CreateInputState(InputStateBuilder* builder) override; + PipelineLayoutBase* CreatePipelineLayout(PipelineLayoutBuilder* builder) override; + QueueBase* CreateQueue(QueueBuilder* builder) override; + RenderPassBase* CreateRenderPass(RenderPassBuilder* builder) override; + RenderPipelineBase* CreateRenderPipeline(RenderPipelineBuilder* builder) override; + SamplerBase* CreateSampler(SamplerBuilder* builder) override; + ShaderModuleBase* CreateShaderModule(ShaderModuleBuilder* builder) override; + SwapChainBase* CreateSwapChain(SwapChainBuilder* builder) override; + TextureBase* CreateTexture(TextureBuilder* builder) override; + TextureViewBase* CreateTextureView(TextureViewBuilder* builder) override; - void TickImpl() override; + void TickImpl() override; - const VulkanDeviceInfo& GetDeviceInfo() const; - MapReadRequestTracker* GetMapReadRequestTracker() const; - MemoryAllocator* GetMemoryAllocator() const; - BufferUploader* GetBufferUploader() const; + const VulkanDeviceInfo& GetDeviceInfo() const; + MapReadRequestTracker* GetMapReadRequestTracker() const; + MemoryAllocator* GetMemoryAllocator() const; + BufferUploader* GetBufferUploader() const; - Serial GetSerial() const; + Serial GetSerial() const; - VkCommandBuffer GetPendingCommandBuffer(); - void SubmitPendingCommands(); + VkCommandBuffer GetPendingCommandBuffer(); + void SubmitPendingCommands(); - // Contains all the Vulkan entry points, vkDoFoo is called via device->fn.DoFoo. - const VulkanFunctions fn; + // Contains all the Vulkan entry points, vkDoFoo is called via device->fn.DoFoo. + const VulkanFunctions fn; - VkInstance GetInstance() const; - VkDevice GetVkDevice() const; + VkInstance GetInstance() const; + VkDevice GetVkDevice() const; - private: - bool CreateInstance(VulkanGlobalKnobs* usedKnobs); - bool CreateDevice(VulkanDeviceKnobs* usedKnobs); - void GatherQueueFromDevice(); + private: + bool CreateInstance(VulkanGlobalKnobs* usedKnobs); + bool CreateDevice(VulkanDeviceKnobs* usedKnobs); + void GatherQueueFromDevice(); - bool RegisterDebugReport(); - static VkBool32 OnDebugReportCallback(VkDebugReportFlagsEXT flags, - VkDebugReportObjectTypeEXT objectType, - uint64_t object, - size_t location, - int32_t messageCode, - const char* pLayerPrefix, - const char* pMessage, - void* pUserdata); + bool RegisterDebugReport(); + static VkBool32 OnDebugReportCallback(VkDebugReportFlagsEXT flags, + VkDebugReportObjectTypeEXT objectType, + uint64_t object, + size_t location, + int32_t messageCode, + const char* pLayerPrefix, + const char* pMessage, + void* pUserdata); - // To make it easier to use fn it is a public const member. However - // the Device is allowed to mutate them through these private methods. - VulkanFunctions* GetMutableFunctions(); + // To make it easier to use fn it is a public const member. However + // the Device is allowed to mutate them through these private methods. + VulkanFunctions* GetMutableFunctions(); - VulkanGlobalInfo mGlobalInfo; - VulkanDeviceInfo mDeviceInfo; + VulkanGlobalInfo mGlobalInfo; + VulkanDeviceInfo mDeviceInfo; - DynamicLib mVulkanLib; + DynamicLib mVulkanLib; - VkInstance mInstance = VK_NULL_HANDLE; - VkPhysicalDevice mPhysicalDevice = VK_NULL_HANDLE; - VkDevice mVkDevice = VK_NULL_HANDLE; - uint32_t mQueueFamily = 0; - VkQueue mQueue = VK_NULL_HANDLE; - VkDebugReportCallbackEXT mDebugReportCallback = VK_NULL_HANDLE; + VkInstance mInstance = VK_NULL_HANDLE; + VkPhysicalDevice mPhysicalDevice = VK_NULL_HANDLE; + VkDevice mVkDevice = VK_NULL_HANDLE; + uint32_t mQueueFamily = 0; + VkQueue mQueue = VK_NULL_HANDLE; + VkDebugReportCallbackEXT mDebugReportCallback = VK_NULL_HANDLE; - MapReadRequestTracker* mMapReadRequestTracker = nullptr; - MemoryAllocator* mMemoryAllocator = nullptr; - BufferUploader* mBufferUploader = nullptr; + MapReadRequestTracker* mMapReadRequestTracker = nullptr; + MemoryAllocator* mMemoryAllocator = nullptr; + BufferUploader* mBufferUploader = nullptr; - VkFence GetUnusedFence(); - void CheckPassedFences(); + VkFence GetUnusedFence(); + void CheckPassedFences(); - // We track which operations are in flight on the GPU with an increasing serial. - // This works only because we have a single queue. Each submit to a queue is associated - // to a serial and a fence, such that when the fence is "ready" we know the operations - // have finished. - std::queue> mFencesInFlight; - std::vector mUnusedFences; - Serial mNextSerial = 1; - Serial mCompletedSerial = 0; + // We track which operations are in flight on the GPU with an increasing serial. + // This works only because we have a single queue. Each submit to a queue is associated + // to a serial and a fence, such that when the fence is "ready" we know the operations + // have finished. + std::queue> mFencesInFlight; + std::vector mUnusedFences; + Serial mNextSerial = 1; + Serial mCompletedSerial = 0; - struct CommandPoolAndBuffer { - VkCommandPool pool = VK_NULL_HANDLE; - VkCommandBuffer commandBuffer = VK_NULL_HANDLE; - }; + struct CommandPoolAndBuffer { + VkCommandPool pool = VK_NULL_HANDLE; + VkCommandBuffer commandBuffer = VK_NULL_HANDLE; + }; - CommandPoolAndBuffer GetUnusedCommands(); - void RecycleCompletedCommands(); - void FreeCommands(CommandPoolAndBuffer* commands); + CommandPoolAndBuffer GetUnusedCommands(); + void RecycleCompletedCommands(); + void FreeCommands(CommandPoolAndBuffer* commands); - SerialQueue mCommandsInFlight; - std::vector mUnusedCommands; - CommandPoolAndBuffer mPendingCommands; + SerialQueue mCommandsInFlight; + std::vector mUnusedCommands; + CommandPoolAndBuffer mPendingCommands; }; class Queue : public QueueBase { - public: - Queue(QueueBuilder* builder); - ~Queue(); + public: + Queue(QueueBuilder* builder); + ~Queue(); - // NXT API - void Submit(uint32_t numCommands, CommandBuffer* const * commands); + // NXT API + void Submit(uint32_t numCommands, CommandBuffer* const* commands); }; class Texture : public TextureBase { - public: - Texture(TextureBuilder* builder); - ~Texture(); + public: + Texture(TextureBuilder* builder); + ~Texture(); - private: - void TransitionUsageImpl(nxt::TextureUsageBit currentUsage, nxt::TextureUsageBit targetUsage) override; + private: + void TransitionUsageImpl(nxt::TextureUsageBit currentUsage, + nxt::TextureUsageBit targetUsage) override; }; class SwapChain : public SwapChainBase { - public: - SwapChain(SwapChainBuilder* builder); - ~SwapChain(); + public: + SwapChain(SwapChainBuilder* builder); + ~SwapChain(); - protected: - TextureBase* GetNextTextureImpl(TextureBuilder* builder) override; + protected: + TextureBase* GetNextTextureImpl(TextureBuilder* builder) override; }; -} -} +}} // namespace backend::vulkan -#endif // BACKEND_VULKAN_VULKANBACKEND_H_ +#endif // BACKEND_VULKAN_VULKANBACKEND_H_ diff --git a/src/backend/vulkan/VulkanFunctions.cpp b/src/backend/vulkan/VulkanFunctions.cpp index be63afcef5..886b8d155e 100644 --- a/src/backend/vulkan/VulkanFunctions.cpp +++ b/src/backend/vulkan/VulkanFunctions.cpp @@ -17,14 +17,13 @@ #include "backend/vulkan/VulkanInfo.h" #include "common/DynamicLib.h" -namespace backend { -namespace vulkan { +namespace backend { namespace vulkan { - #define GET_GLOBAL_PROC(name) \ - name = reinterpret_cast(GetInstanceProcAddr(nullptr, "vk" #name)); \ - if (name == nullptr) { \ - return false; \ - } +#define GET_GLOBAL_PROC(name) \ + name = reinterpret_cast(GetInstanceProcAddr(nullptr, "vk" #name)); \ + if (name == nullptr) { \ + return false; \ + } bool VulkanFunctions::LoadGlobalProcs(const DynamicLib& vulkanLib) { if (!vulkanLib.GetProc(&GetInstanceProcAddr, "vkGetInstanceProcAddr")) { @@ -38,14 +37,16 @@ namespace vulkan { return true; } - #define GET_INSTANCE_PROC(name) \ - name = reinterpret_cast(GetInstanceProcAddr(instance, "vk" #name)); \ - if (name == nullptr) { \ - return false; \ - } +#define GET_INSTANCE_PROC(name) \ + name = reinterpret_cast(GetInstanceProcAddr(instance, "vk" #name)); \ + if (name == nullptr) { \ + return false; \ + } - bool VulkanFunctions::LoadInstanceProcs(VkInstance instance, const VulkanGlobalKnobs& usedKnobs) { - // Load this proc first so that we can destroy the instance even if some other GET_INSTANCE_PROC fails + bool VulkanFunctions::LoadInstanceProcs(VkInstance instance, + const VulkanGlobalKnobs& usedKnobs) { + // Load this proc first so that we can destroy the instance even if some other + // GET_INSTANCE_PROC fails GET_INSTANCE_PROC(DestroyInstance); GET_INSTANCE_PROC(CreateDevice); @@ -79,11 +80,11 @@ namespace vulkan { return true; } - #define GET_DEVICE_PROC(name) \ - name = reinterpret_cast(GetDeviceProcAddr(device, "vk" #name)); \ - if (name == nullptr) { \ - return false; \ - } +#define GET_DEVICE_PROC(name) \ + name = reinterpret_cast(GetDeviceProcAddr(device, "vk" #name)); \ + if (name == nullptr) { \ + return false; \ + } bool VulkanFunctions::LoadDeviceProcs(VkDevice device, const VulkanDeviceKnobs& usedKnobs) { GET_DEVICE_PROC(AllocateCommandBuffers); @@ -217,5 +218,4 @@ namespace vulkan { return true; } -} -} +}} // namespace backend::vulkan diff --git a/src/backend/vulkan/VulkanFunctions.h b/src/backend/vulkan/VulkanFunctions.h index 15aff7f86f..f7dd214965 100644 --- a/src/backend/vulkan/VulkanFunctions.h +++ b/src/backend/vulkan/VulkanFunctions.h @@ -19,8 +19,7 @@ class DynamicLib; -namespace backend { -namespace vulkan { +namespace backend { namespace vulkan { struct VulkanGlobalKnobs; struct VulkanDeviceKnobs; @@ -54,11 +53,14 @@ namespace vulkan { PFN_vkGetDeviceProcAddr GetDeviceProcAddr = nullptr; PFN_vkGetPhysicalDeviceFeatures GetPhysicalDeviceFeatures = nullptr; PFN_vkGetPhysicalDeviceFormatProperties GetPhysicalDeviceFormatProperties = nullptr; - PFN_vkGetPhysicalDeviceImageFormatProperties GetPhysicalDeviceImageFormatProperties = nullptr; + PFN_vkGetPhysicalDeviceImageFormatProperties GetPhysicalDeviceImageFormatProperties = + nullptr; PFN_vkGetPhysicalDeviceMemoryProperties GetPhysicalDeviceMemoryProperties = nullptr; PFN_vkGetPhysicalDeviceProperties GetPhysicalDeviceProperties = nullptr; - PFN_vkGetPhysicalDeviceQueueFamilyProperties GetPhysicalDeviceQueueFamilyProperties = nullptr; - PFN_vkGetPhysicalDeviceSparseImageFormatProperties GetPhysicalDeviceSparseImageFormatProperties = nullptr; + PFN_vkGetPhysicalDeviceQueueFamilyProperties GetPhysicalDeviceQueueFamilyProperties = + nullptr; + PFN_vkGetPhysicalDeviceSparseImageFormatProperties + GetPhysicalDeviceSparseImageFormatProperties = nullptr; // Not technically an instance proc but we want to be able to use it as soon as the // device is created. PFN_vkDestroyDevice DestroyDevice = nullptr; @@ -71,9 +73,11 @@ namespace vulkan { // VK_KHR_surface PFN_vkDestroySurfaceKHR DestroySurfaceKHR = nullptr; PFN_vkGetPhysicalDeviceSurfaceSupportKHR GetPhysicalDeviceSurfaceSupportKHR = nullptr; - PFN_vkGetPhysicalDeviceSurfaceCapabilitiesKHR GetPhysicalDeviceSurfaceCapabilitiesKHR = nullptr; + PFN_vkGetPhysicalDeviceSurfaceCapabilitiesKHR GetPhysicalDeviceSurfaceCapabilitiesKHR = + nullptr; PFN_vkGetPhysicalDeviceSurfaceFormatsKHR GetPhysicalDeviceSurfaceFormatsKHR = nullptr; - PFN_vkGetPhysicalDeviceSurfacePresentModesKHR GetPhysicalDeviceSurfacePresentModesKHR = nullptr; + PFN_vkGetPhysicalDeviceSurfacePresentModesKHR GetPhysicalDeviceSurfacePresentModesKHR = + nullptr; // ---------- Device procs @@ -206,7 +210,6 @@ namespace vulkan { PFN_vkQueuePresentKHR QueuePresentKHR = nullptr; }; -} -} +}} // namespace backend::vulkan -#endif // BACKEND_VULKAN_VULKANFUNCTIONS_H_ +#endif // BACKEND_VULKAN_VULKANFUNCTIONS_H_ diff --git a/src/backend/vulkan/VulkanInfo.cpp b/src/backend/vulkan/VulkanInfo.cpp index fce5b15d79..a496e4b18e 100644 --- a/src/backend/vulkan/VulkanInfo.cpp +++ b/src/backend/vulkan/VulkanInfo.cpp @@ -26,10 +26,9 @@ namespace { bool IsExtensionName(const VkExtensionProperties& extension, const char* name) { return strncmp(extension.extensionName, name, VK_MAX_EXTENSION_NAME_SIZE) == 0; } -} +} // namespace -namespace backend { -namespace vulkan { +namespace backend { namespace vulkan { const char kLayerNameLunargStandardValidation[] = "VK_LAYER_LUNARG_standard_validation"; @@ -65,13 +64,15 @@ namespace vulkan { // Gather the info about the instance extensions { uint32_t count = 0; - VkResult result = device.fn.EnumerateInstanceExtensionProperties(nullptr, &count, nullptr); + VkResult result = + device.fn.EnumerateInstanceExtensionProperties(nullptr, &count, nullptr); if (result != VK_SUCCESS && result != VK_INCOMPLETE) { return false; } info->extensions.resize(count); - result = device.fn.EnumerateInstanceExtensionProperties(nullptr, &count, info->extensions.data()); + result = device.fn.EnumerateInstanceExtensionProperties(nullptr, &count, + info->extensions.data()); if (result != VK_SUCCESS) { return false; } @@ -109,7 +110,9 @@ namespace vulkan { return true; } - bool GatherDeviceInfo(const Device& device, VkPhysicalDevice physicalDevice, VulkanDeviceInfo* info) { + bool GatherDeviceInfo(const Device& device, + VkPhysicalDevice physicalDevice, + VulkanDeviceInfo* info) { // Gather general info about the device device.fn.GetPhysicalDeviceProperties(physicalDevice, &info->properties); device.fn.GetPhysicalDeviceFeatures(physicalDevice, &info->features); @@ -119,8 +122,10 @@ namespace vulkan { VkPhysicalDeviceMemoryProperties memory; device.fn.GetPhysicalDeviceMemoryProperties(physicalDevice, &memory); - info->memoryTypes.assign(memory.memoryTypes, memory.memoryTypes + memory.memoryTypeCount); - info->memoryHeaps.assign(memory.memoryHeaps, memory.memoryHeaps + memory.memoryHeapCount); + info->memoryTypes.assign(memory.memoryTypes, + memory.memoryTypes + memory.memoryTypeCount); + info->memoryHeaps.assign(memory.memoryHeaps, + memory.memoryHeaps + memory.memoryHeapCount); } // Gather info about device queue families @@ -129,19 +134,22 @@ namespace vulkan { device.fn.GetPhysicalDeviceQueueFamilyProperties(physicalDevice, &count, nullptr); info->queueFamilies.resize(count); - device.fn.GetPhysicalDeviceQueueFamilyProperties(physicalDevice, &count, info->queueFamilies.data()); + device.fn.GetPhysicalDeviceQueueFamilyProperties(physicalDevice, &count, + info->queueFamilies.data()); } // Gather the info about the device layers { uint32_t count = 0; - VkResult result = device.fn.EnumerateDeviceLayerProperties(physicalDevice, &count, nullptr); + VkResult result = + device.fn.EnumerateDeviceLayerProperties(physicalDevice, &count, nullptr); if (result != VK_SUCCESS && result != VK_INCOMPLETE) { return false; } info->layers.resize(count); - result = device.fn.EnumerateDeviceLayerProperties(physicalDevice, &count, info->layers.data()); + result = device.fn.EnumerateDeviceLayerProperties(physicalDevice, &count, + info->layers.data()); if (result != VK_SUCCESS) { return false; } @@ -150,13 +158,15 @@ namespace vulkan { // Gather the info about the device extensions { uint32_t count = 0; - VkResult result = device.fn.EnumerateDeviceExtensionProperties(physicalDevice, nullptr, &count, nullptr); + VkResult result = device.fn.EnumerateDeviceExtensionProperties(physicalDevice, nullptr, + &count, nullptr); if (result != VK_SUCCESS && result != VK_INCOMPLETE) { return false; } info->extensions.resize(count); - result = device.fn.EnumerateDeviceExtensionProperties(physicalDevice, nullptr, &count, info->extensions.data()); + result = device.fn.EnumerateDeviceExtensionProperties(physicalDevice, nullptr, &count, + info->extensions.data()); if (result != VK_SUCCESS) { return false; } @@ -173,5 +183,4 @@ namespace vulkan { return true; } -} -} +}} // namespace backend::vulkan diff --git a/src/backend/vulkan/VulkanInfo.h b/src/backend/vulkan/VulkanInfo.h index 45a8c3cfa0..f3889cc062 100644 --- a/src/backend/vulkan/VulkanInfo.h +++ b/src/backend/vulkan/VulkanInfo.h @@ -19,8 +19,7 @@ #include -namespace backend { -namespace vulkan { +namespace backend { namespace vulkan { class Device; @@ -68,8 +67,9 @@ namespace vulkan { bool GatherGlobalInfo(const Device& device, VulkanGlobalInfo* info); bool GetPhysicalDevices(const Device& device, std::vector* physicalDevices); - bool GatherDeviceInfo(const Device& device, VkPhysicalDevice physicalDevice, VulkanDeviceInfo* info); -} -} + bool GatherDeviceInfo(const Device& device, + VkPhysicalDevice physicalDevice, + VulkanDeviceInfo* info); +}} // namespace backend::vulkan -#endif // BACKEND_VULKAN_VULKANINFO_H_ +#endif // BACKEND_VULKAN_VULKANINFO_H_ diff --git a/src/backend/vulkan/vulkan_platform.h b/src/backend/vulkan/vulkan_platform.h index 726097fe06..3354a38ba5 100644 --- a/src/backend/vulkan/vulkan_platform.h +++ b/src/backend/vulkan/vulkan_platform.h @@ -17,4 +17,4 @@ #include -#endif // BACKEND_VULKAN_VULKANPLATFORM_H_ +#endif // BACKEND_VULKAN_VULKANPLATFORM_H_