From 92e7dec2fa919a70253c987f8333face2c31c017 Mon Sep 17 00:00:00 2001 From: Kearwood Gilbert Date: Mon, 16 Aug 2021 15:49:17 -0700 Subject: [PATCH] Refactored SurfaceInfo to KRSurface and DeviceInfo to KRDevice. Created new files for KRSurface and KRDevice. --- kraken/CMakeLists.txt | 2 ++ kraken/KRContext.cpp | 34 +++++++++++++------------- kraken/KRContext.h | 41 +++++--------------------------- kraken/KRDevice.cpp | 33 ++++++++++++++++++++++++++ kraken/KRDevice.h | 55 +++++++++++++++++++++++++++++++++++++++++++ kraken/KRPipeline.cpp | 4 ++-- 6 files changed, 115 insertions(+), 54 deletions(-) create mode 100644 kraken/KRDevice.cpp create mode 100644 kraken/KRDevice.h diff --git a/kraken/CMakeLists.txt b/kraken/CMakeLists.txt index 4a42c5f..da4a9bd 100644 --- a/kraken/CMakeLists.txt +++ b/kraken/CMakeLists.txt @@ -22,6 +22,8 @@ add_sources(KRBundleManager.cpp) add_sources(KRCamera.cpp) add_sources(KRCollider.cpp) add_sources(KRContext.cpp) +add_sources(KRDevice.cpp) +add_sources(KRSurface.cpp) add_sources(KRStreamer.cpp) IF(APPLE) add_sources(KREngine.mm) diff --git a/kraken/KRContext.cpp b/kraken/KRContext.cpp index 2a705a0..62c8956 100755 --- a/kraken/KRContext.cpp +++ b/kraken/KRContext.cpp @@ -787,7 +787,7 @@ KRContext::destroyDeviceContexts() { const std::lock_guard lock(KRContext::g_DeviceInfoMutex); for (auto itr = m_devices.begin(); itr != m_devices.end(); itr++) { - DeviceInfo* deviceInfo = &(*itr).second; + KRDevice* deviceInfo = &(*itr).second; vkDestroyCommandPool(deviceInfo->logicalDevice, deviceInfo->graphicsCommandPool, nullptr); vkDestroyCommandPool(deviceInfo->logicalDevice, deviceInfo->computeCommandPool, nullptr); vkDestroyDevice(deviceInfo->logicalDevice, nullptr); @@ -808,8 +808,8 @@ KRContext::destroySurfaces() } const std::lock_guard surfaceLock(KRContext::g_SurfaceInfoMutex); for (auto itr = m_surfaces.begin(); itr != m_surfaces.end(); itr++) { - SurfaceInfo& surfaceInfo = (*itr).second; - DeviceInfo& deviceInfo = GetDeviceInfo(surfaceInfo.deviceHandle); + KRSurface& surfaceInfo = (*itr).second; + KRDevice& deviceInfo = GetDeviceInfo(surfaceInfo.deviceHandle); for (auto framebuffer : surfaceInfo.swapChainFramebuffers) { vkDestroyFramebuffer(deviceInfo.logicalDevice, framebuffer, nullptr); } @@ -926,12 +926,12 @@ KrResult KRContext::createWindowSurface(const KrCreateWindowSurfaceInfo* createW const std::lock_guard surfaceLock(KRContext::g_SurfaceInfoMutex); const std::lock_guard deviceLock(KRContext::g_DeviceInfoMutex); - DeviceInfo* deviceInfo = nullptr; + KRDevice* deviceInfo = nullptr; #ifdef WIN32 HWND hWnd = static_cast(createWindowSurfaceInfo->hWnd); - SurfaceInfo info{}; + KRSurface info{}; info.hWnd = hWnd; VkWin32SurfaceCreateInfoKHR createInfo{}; @@ -943,7 +943,7 @@ KrResult KRContext::createWindowSurface(const KrCreateWindowSurfaceInfo* createW } for (auto itr = m_devices.begin(); itr != m_devices.end(); itr++) { - DeviceInfo* device = &(*itr).second; + KRDevice* device = &(*itr).second; VkBool32 canPresent = false; vkGetPhysicalDeviceSurfaceSupportKHR(device->device, device->graphicsFamilyQueueIndex, info.surface, &canPresent); if (canPresent) { @@ -1098,7 +1098,7 @@ KrResult KRContext::createWindowSurface(const KrCreateWindowSurfaceInfo* createW } KrSurfaceHandle surfaceHandle = ++m_topSurfaceHandle; - m_surfaces.insert(std::pair(surfaceHandle, info)); + m_surfaces.insert(std::pair(surfaceHandle, info)); m_surfaceHandleMap.insert(std::pair(createWindowSurfaceInfo->surfaceHandle, surfaceHandle)); @@ -1107,7 +1107,7 @@ KrResult KRContext::createWindowSurface(const KrCreateWindowSurfaceInfo* createW { KRPipeline* testPipeline = m_pPipelineManager->get("vulkan_test"); - SurfaceInfo& surface = m_surfaces[surfaceHandle]; + KRSurface& surface = m_surfaces[surfaceHandle]; surface.swapChainFramebuffers.resize(surface.swapChainImageViews.size()); for (size_t i = 0; i < surface.swapChainImageViews.size(); i++) { @@ -1157,8 +1157,8 @@ KrResult KRContext::deleteWindowSurface(const KrDeleteWindowSurfaceInfo* deleteW if (itr == m_surfaces.end()) { return KR_ERROR_NOT_FOUND; } - SurfaceInfo* surfaceInfo = &(*itr).second; - DeviceInfo& deviceInfo = GetDeviceInfo(surfaceInfo->deviceHandle); + KRSurface* surfaceInfo = &(*itr).second; + KRDevice& deviceInfo = GetDeviceInfo(surfaceInfo->deviceHandle); for (auto imageView : surfaceInfo->swapChainImageViews) { vkDestroyImageView(deviceInfo.logicalDevice, imageView, nullptr); } @@ -1198,8 +1198,8 @@ void KRContext::renderFrame() const std::lock_guard surfaceLock(KRContext::g_SurfaceInfoMutex); for (auto surfaceItr = m_surfaces.begin(); surfaceItr != m_surfaces.end(); surfaceItr++) { - SurfaceInfo& surface = (*surfaceItr).second; - DeviceInfo& device = GetDeviceInfo(surface.deviceHandle); + KRSurface& surface = (*surfaceItr).second; + KRDevice& device = GetDeviceInfo(surface.deviceHandle); uint32_t imageIndex = 0; vkAcquireNextImageKHR(device.logicalDevice, surface.swapChain, UINT64_MAX, surface.imageAvailableSemaphore, VK_NULL_HANDLE, &imageIndex); @@ -1269,7 +1269,7 @@ void KRContext::renderFrame() frameIndex++; } -KRContext::SurfaceInfo& KRContext::GetSurfaceInfo(KrSurfaceHandle handle) +KRSurface& KRContext::GetSurfaceInfo(KrSurfaceHandle handle) { auto itr = m_surfaces.find(handle); if (itr == m_surfaces.end()) { @@ -1278,7 +1278,7 @@ KRContext::SurfaceInfo& KRContext::GetSurfaceInfo(KrSurfaceHandle handle) return m_surfaces[handle]; } -KRContext::DeviceInfo& KRContext::GetDeviceInfo(KrDeviceHandle handle) +KRDevice& KRContext::GetDeviceInfo(KrDeviceHandle handle) { return m_devices[handle]; } @@ -1302,7 +1302,7 @@ void KRContext::createDevices() VK_KHR_SWAPCHAIN_EXTENSION_NAME }; - std::vector deviceInfos; + std::vector deviceInfos; for (const VkPhysicalDevice& device : devices) { VkPhysicalDeviceProperties deviceProperties; @@ -1377,7 +1377,7 @@ void KRContext::createDevices() } } if (addDevice) { - DeviceInfo& info = deviceInfos.emplace_back(DeviceInfo{}); + KRDevice& info = deviceInfos.emplace_back(KRDevice{}); info.device = device; info.deviceProperties = deviceProperties; info.deviceFeatures = deviceFeatures; @@ -1386,7 +1386,7 @@ void KRContext::createDevices() } } - for (DeviceInfo& info: deviceInfos) { + for (KRDevice& info: deviceInfos) { VkDeviceQueueCreateInfo queueCreateInfo[2]{}; float queuePriority = 1.0f; queueCreateInfo[0].sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO; diff --git a/kraken/KRContext.h b/kraken/KRContext.h index 362620c..52d75a5 100755 --- a/kraken/KRContext.h +++ b/kraken/KRContext.h @@ -22,6 +22,8 @@ #include "KRShaderManager.h" #include "KRSourceManager.h" #include "KRStreamer.h" +#include "KRDevice.h" +#include "KRSurface.h" class KRAudioManager; @@ -128,43 +130,12 @@ public: static void activateStreamerContext(); static void activateRenderContext(); - - typedef struct { - VkPhysicalDevice device; - VkDevice logicalDevice; - VkPhysicalDeviceProperties deviceProperties; - VkPhysicalDeviceFeatures deviceFeatures; - uint32_t graphicsFamilyQueueIndex; - VkQueue graphicsQueue; - uint32_t computeFamilyQueueIndex; - VkQueue computeQueue; - VkCommandPool graphicsCommandPool; - VkCommandPool computeCommandPool; - std::vector graphicsCommandBuffers; - std::vector computeCommandBuffers; - } DeviceInfo; - - typedef struct { - KrDeviceHandle deviceHandle; - VkSurfaceKHR surface; - VkSwapchainKHR swapChain; - std::vector swapChainImages; - VkFormat swapChainImageFormat; - VkExtent2D swapChainExtent; - std::vector swapChainImageViews; - std::vector swapChainFramebuffers; - VkSemaphore imageAvailableSemaphore; - VkSemaphore renderFinishedSemaphore; -#ifdef WIN32 - HWND hWnd; -#endif - } SurfaceInfo; static std::mutex g_SurfaceInfoMutex; static std::mutex g_DeviceInfoMutex; - DeviceInfo& GetDeviceInfo(KrDeviceHandle handle); - SurfaceInfo& GetSurfaceInfo(KrSurfaceHandle handle); + KRDevice& GetDeviceInfo(KrDeviceHandle handle); + KRSurface& GetSurfaceInfo(KrSurfaceHandle handle); #if TARGET_OS_MAC static void attachToView(void *view); @@ -221,10 +192,10 @@ private: std::atomic m_stop; void renderFrame(); - unordered_map m_devices; + unordered_map m_devices; KrDeviceHandle m_topDeviceHandle; - unordered_map m_surfaces; + unordered_map m_surfaces; KrDeviceHandle m_topSurfaceHandle; unordered_map m_surfaceHandleMap; diff --git a/kraken/KRDevice.cpp b/kraken/KRDevice.cpp new file mode 100644 index 0000000..320b293 --- /dev/null +++ b/kraken/KRDevice.cpp @@ -0,0 +1,33 @@ +// +// KRDevice.cpp +// KREngine +// +// Copyright 2021 Kearwood Gilbert. All rights reserved. +// +// Redistribution and use in source and binary forms, with or without modification, are +// permitted provided that the following conditions are met: +// +// 1. Redistributions of source code must retain the above copyright notice, this list of +// conditions and the following disclaimer. +// +// 2. Redistributions in binary form must reproduce the above copyright notice, this list +// of conditions and the following disclaimer in the documentation and/or other materials +// provided with the distribution. +// +// THIS SOFTWARE IS PROVIDED BY KEARWOOD GILBERT ''AS IS'' AND ANY EXPRESS OR IMPLIED +// WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND +// FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL KEARWOOD GILBERT OR +// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR +// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON +// ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING +// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF +// ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +// +// The views and conclusions contained in the software and documentation are those of the +// authors and should not be interpreted as representing official policies, either expressed +// or implied, of Kearwood Gilbert. +// + +#include "KRDevice.h" + diff --git a/kraken/KRDevice.h b/kraken/KRDevice.h new file mode 100644 index 0000000..60c757a --- /dev/null +++ b/kraken/KRDevice.h @@ -0,0 +1,55 @@ +// +// KRDevice.h +// KREngine +// +// Copyright 2021 Kearwood Gilbert. All rights reserved. +// +// Redistribution and use in source and binary forms, with or without modification, are +// permitted provided that the following conditions are met: +// +// 1. Redistributions of source code must retain the above copyright notice, this list of +// conditions and the following disclaimer. +// +// 2. Redistributions in binary form must reproduce the above copyright notice, this list +// of conditions and the following disclaimer in the documentation and/or other materials +// provided with the distribution. +// +// THIS SOFTWARE IS PROVIDED BY KEARWOOD GILBERT ''AS IS'' AND ANY EXPRESS OR IMPLIED +// WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND +// FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL KEARWOOD GILBERT OR +// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR +// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON +// ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING +// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF +// ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +// +// The views and conclusions contained in the software and documentation are those of the +// authors and should not be interpreted as representing official policies, either expressed +// or implied, of Kearwood Gilbert. +// + +#include "KREngine-common.h" + +#ifndef KRDEVICE_H +#define KRDEVICE_H + +class KRDevice +{ +public: + VkPhysicalDevice device; + VkDevice logicalDevice; + VkPhysicalDeviceProperties deviceProperties; + VkPhysicalDeviceFeatures deviceFeatures; + uint32_t graphicsFamilyQueueIndex; + VkQueue graphicsQueue; + uint32_t computeFamilyQueueIndex; + VkQueue computeQueue; + VkCommandPool graphicsCommandPool; + VkCommandPool computeCommandPool; + std::vector graphicsCommandBuffers; + std::vector computeCommandBuffers; +private: +}; + +#endif // KRDEVICE_H diff --git a/kraken/KRPipeline.cpp b/kraken/KRPipeline.cpp index 037fce1..65064cb 100644 --- a/kraken/KRPipeline.cpp +++ b/kraken/KRPipeline.cpp @@ -116,8 +116,8 @@ KRPipeline::KRPipeline(KRContext& context, KrSurfaceHandle surfaceHandle, const m_pipelineLayout = nullptr; m_graphicsPipeline = nullptr; m_renderPass = nullptr; - KRContext::SurfaceInfo& surface = m_pContext->GetSurfaceInfo(surfaceHandle); - KRContext::DeviceInfo& device = m_pContext->GetDeviceInfo(surface.deviceHandle); + KRSurface& surface = m_pContext->GetSurfaceInfo(surfaceHandle); + KRDevice& device = m_pContext->GetDeviceInfo(surface.deviceHandle); strcpy(m_szKey, szKey);