Refactored SurfaceInfo to KRSurface and DeviceInfo to KRDevice. Created new files for KRSurface and KRDevice.

This commit is contained in:
2021-08-16 15:49:17 -07:00
parent 09b9841c03
commit 92e7dec2fa
6 changed files with 115 additions and 54 deletions

View File

@@ -22,6 +22,8 @@ add_sources(KRBundleManager.cpp)
add_sources(KRCamera.cpp) add_sources(KRCamera.cpp)
add_sources(KRCollider.cpp) add_sources(KRCollider.cpp)
add_sources(KRContext.cpp) add_sources(KRContext.cpp)
add_sources(KRDevice.cpp)
add_sources(KRSurface.cpp)
add_sources(KRStreamer.cpp) add_sources(KRStreamer.cpp)
IF(APPLE) IF(APPLE)
add_sources(KREngine.mm) add_sources(KREngine.mm)

View File

@@ -787,7 +787,7 @@ KRContext::destroyDeviceContexts()
{ {
const std::lock_guard<std::mutex> lock(KRContext::g_DeviceInfoMutex); const std::lock_guard<std::mutex> lock(KRContext::g_DeviceInfoMutex);
for (auto itr = m_devices.begin(); itr != m_devices.end(); itr++) { 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->graphicsCommandPool, nullptr);
vkDestroyCommandPool(deviceInfo->logicalDevice, deviceInfo->computeCommandPool, nullptr); vkDestroyCommandPool(deviceInfo->logicalDevice, deviceInfo->computeCommandPool, nullptr);
vkDestroyDevice(deviceInfo->logicalDevice, nullptr); vkDestroyDevice(deviceInfo->logicalDevice, nullptr);
@@ -808,8 +808,8 @@ KRContext::destroySurfaces()
} }
const std::lock_guard<std::mutex> surfaceLock(KRContext::g_SurfaceInfoMutex); const std::lock_guard<std::mutex> surfaceLock(KRContext::g_SurfaceInfoMutex);
for (auto itr = m_surfaces.begin(); itr != m_surfaces.end(); itr++) { for (auto itr = m_surfaces.begin(); itr != m_surfaces.end(); itr++) {
SurfaceInfo& surfaceInfo = (*itr).second; KRSurface& surfaceInfo = (*itr).second;
DeviceInfo& deviceInfo = GetDeviceInfo(surfaceInfo.deviceHandle); KRDevice& deviceInfo = GetDeviceInfo(surfaceInfo.deviceHandle);
for (auto framebuffer : surfaceInfo.swapChainFramebuffers) { for (auto framebuffer : surfaceInfo.swapChainFramebuffers) {
vkDestroyFramebuffer(deviceInfo.logicalDevice, framebuffer, nullptr); vkDestroyFramebuffer(deviceInfo.logicalDevice, framebuffer, nullptr);
} }
@@ -926,12 +926,12 @@ KrResult KRContext::createWindowSurface(const KrCreateWindowSurfaceInfo* createW
const std::lock_guard<std::mutex> surfaceLock(KRContext::g_SurfaceInfoMutex); const std::lock_guard<std::mutex> surfaceLock(KRContext::g_SurfaceInfoMutex);
const std::lock_guard<std::mutex> deviceLock(KRContext::g_DeviceInfoMutex); const std::lock_guard<std::mutex> deviceLock(KRContext::g_DeviceInfoMutex);
DeviceInfo* deviceInfo = nullptr; KRDevice* deviceInfo = nullptr;
#ifdef WIN32 #ifdef WIN32
HWND hWnd = static_cast<HWND>(createWindowSurfaceInfo->hWnd); HWND hWnd = static_cast<HWND>(createWindowSurfaceInfo->hWnd);
SurfaceInfo info{}; KRSurface info{};
info.hWnd = hWnd; info.hWnd = hWnd;
VkWin32SurfaceCreateInfoKHR createInfo{}; VkWin32SurfaceCreateInfoKHR createInfo{};
@@ -943,7 +943,7 @@ KrResult KRContext::createWindowSurface(const KrCreateWindowSurfaceInfo* createW
} }
for (auto itr = m_devices.begin(); itr != m_devices.end(); itr++) { for (auto itr = m_devices.begin(); itr != m_devices.end(); itr++) {
DeviceInfo* device = &(*itr).second; KRDevice* device = &(*itr).second;
VkBool32 canPresent = false; VkBool32 canPresent = false;
vkGetPhysicalDeviceSurfaceSupportKHR(device->device, device->graphicsFamilyQueueIndex, info.surface, &canPresent); vkGetPhysicalDeviceSurfaceSupportKHR(device->device, device->graphicsFamilyQueueIndex, info.surface, &canPresent);
if (canPresent) { if (canPresent) {
@@ -1098,7 +1098,7 @@ KrResult KRContext::createWindowSurface(const KrCreateWindowSurfaceInfo* createW
} }
KrSurfaceHandle surfaceHandle = ++m_topSurfaceHandle; KrSurfaceHandle surfaceHandle = ++m_topSurfaceHandle;
m_surfaces.insert(std::pair<KrSurfaceHandle, SurfaceInfo>(surfaceHandle, info)); m_surfaces.insert(std::pair<KrSurfaceHandle, KRSurface>(surfaceHandle, info));
m_surfaceHandleMap.insert(std::pair<KrSurfaceMapIndex, KrSurfaceHandle>(createWindowSurfaceInfo->surfaceHandle, surfaceHandle)); m_surfaceHandleMap.insert(std::pair<KrSurfaceMapIndex, KrSurfaceHandle>(createWindowSurfaceInfo->surfaceHandle, surfaceHandle));
@@ -1107,7 +1107,7 @@ KrResult KRContext::createWindowSurface(const KrCreateWindowSurfaceInfo* createW
{ {
KRPipeline* testPipeline = m_pPipelineManager->get("vulkan_test"); KRPipeline* testPipeline = m_pPipelineManager->get("vulkan_test");
SurfaceInfo& surface = m_surfaces[surfaceHandle]; KRSurface& surface = m_surfaces[surfaceHandle];
surface.swapChainFramebuffers.resize(surface.swapChainImageViews.size()); surface.swapChainFramebuffers.resize(surface.swapChainImageViews.size());
for (size_t i = 0; i < surface.swapChainImageViews.size(); i++) { 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()) { if (itr == m_surfaces.end()) {
return KR_ERROR_NOT_FOUND; return KR_ERROR_NOT_FOUND;
} }
SurfaceInfo* surfaceInfo = &(*itr).second; KRSurface* surfaceInfo = &(*itr).second;
DeviceInfo& deviceInfo = GetDeviceInfo(surfaceInfo->deviceHandle); KRDevice& deviceInfo = GetDeviceInfo(surfaceInfo->deviceHandle);
for (auto imageView : surfaceInfo->swapChainImageViews) { for (auto imageView : surfaceInfo->swapChainImageViews) {
vkDestroyImageView(deviceInfo.logicalDevice, imageView, nullptr); vkDestroyImageView(deviceInfo.logicalDevice, imageView, nullptr);
} }
@@ -1198,8 +1198,8 @@ void KRContext::renderFrame()
const std::lock_guard<std::mutex> surfaceLock(KRContext::g_SurfaceInfoMutex); const std::lock_guard<std::mutex> surfaceLock(KRContext::g_SurfaceInfoMutex);
for (auto surfaceItr = m_surfaces.begin(); surfaceItr != m_surfaces.end(); surfaceItr++) { for (auto surfaceItr = m_surfaces.begin(); surfaceItr != m_surfaces.end(); surfaceItr++) {
SurfaceInfo& surface = (*surfaceItr).second; KRSurface& surface = (*surfaceItr).second;
DeviceInfo& device = GetDeviceInfo(surface.deviceHandle); KRDevice& device = GetDeviceInfo(surface.deviceHandle);
uint32_t imageIndex = 0; uint32_t imageIndex = 0;
vkAcquireNextImageKHR(device.logicalDevice, surface.swapChain, UINT64_MAX, surface.imageAvailableSemaphore, VK_NULL_HANDLE, &imageIndex); vkAcquireNextImageKHR(device.logicalDevice, surface.swapChain, UINT64_MAX, surface.imageAvailableSemaphore, VK_NULL_HANDLE, &imageIndex);
@@ -1269,7 +1269,7 @@ void KRContext::renderFrame()
frameIndex++; frameIndex++;
} }
KRContext::SurfaceInfo& KRContext::GetSurfaceInfo(KrSurfaceHandle handle) KRSurface& KRContext::GetSurfaceInfo(KrSurfaceHandle handle)
{ {
auto itr = m_surfaces.find(handle); auto itr = m_surfaces.find(handle);
if (itr == m_surfaces.end()) { if (itr == m_surfaces.end()) {
@@ -1278,7 +1278,7 @@ KRContext::SurfaceInfo& KRContext::GetSurfaceInfo(KrSurfaceHandle handle)
return m_surfaces[handle]; return m_surfaces[handle];
} }
KRContext::DeviceInfo& KRContext::GetDeviceInfo(KrDeviceHandle handle) KRDevice& KRContext::GetDeviceInfo(KrDeviceHandle handle)
{ {
return m_devices[handle]; return m_devices[handle];
} }
@@ -1302,7 +1302,7 @@ void KRContext::createDevices()
VK_KHR_SWAPCHAIN_EXTENSION_NAME VK_KHR_SWAPCHAIN_EXTENSION_NAME
}; };
std::vector<DeviceInfo> deviceInfos; std::vector<KRDevice> deviceInfos;
for (const VkPhysicalDevice& device : devices) { for (const VkPhysicalDevice& device : devices) {
VkPhysicalDeviceProperties deviceProperties; VkPhysicalDeviceProperties deviceProperties;
@@ -1377,7 +1377,7 @@ void KRContext::createDevices()
} }
} }
if (addDevice) { if (addDevice) {
DeviceInfo& info = deviceInfos.emplace_back(DeviceInfo{}); KRDevice& info = deviceInfos.emplace_back(KRDevice{});
info.device = device; info.device = device;
info.deviceProperties = deviceProperties; info.deviceProperties = deviceProperties;
info.deviceFeatures = deviceFeatures; info.deviceFeatures = deviceFeatures;
@@ -1386,7 +1386,7 @@ void KRContext::createDevices()
} }
} }
for (DeviceInfo& info: deviceInfos) { for (KRDevice& info: deviceInfos) {
VkDeviceQueueCreateInfo queueCreateInfo[2]{}; VkDeviceQueueCreateInfo queueCreateInfo[2]{};
float queuePriority = 1.0f; float queuePriority = 1.0f;
queueCreateInfo[0].sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO; queueCreateInfo[0].sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO;

View File

@@ -22,6 +22,8 @@
#include "KRShaderManager.h" #include "KRShaderManager.h"
#include "KRSourceManager.h" #include "KRSourceManager.h"
#include "KRStreamer.h" #include "KRStreamer.h"
#include "KRDevice.h"
#include "KRSurface.h"
class KRAudioManager; class KRAudioManager;
@@ -128,43 +130,12 @@ public:
static void activateStreamerContext(); static void activateStreamerContext();
static void activateRenderContext(); 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<VkCommandBuffer> graphicsCommandBuffers;
std::vector<VkCommandBuffer> computeCommandBuffers;
} DeviceInfo;
typedef struct {
KrDeviceHandle deviceHandle;
VkSurfaceKHR surface;
VkSwapchainKHR swapChain;
std::vector<VkImage> swapChainImages;
VkFormat swapChainImageFormat;
VkExtent2D swapChainExtent;
std::vector<VkImageView> swapChainImageViews;
std::vector<VkFramebuffer> swapChainFramebuffers;
VkSemaphore imageAvailableSemaphore;
VkSemaphore renderFinishedSemaphore;
#ifdef WIN32
HWND hWnd;
#endif
} SurfaceInfo;
static std::mutex g_SurfaceInfoMutex; static std::mutex g_SurfaceInfoMutex;
static std::mutex g_DeviceInfoMutex; static std::mutex g_DeviceInfoMutex;
DeviceInfo& GetDeviceInfo(KrDeviceHandle handle); KRDevice& GetDeviceInfo(KrDeviceHandle handle);
SurfaceInfo& GetSurfaceInfo(KrSurfaceHandle handle); KRSurface& GetSurfaceInfo(KrSurfaceHandle handle);
#if TARGET_OS_MAC #if TARGET_OS_MAC
static void attachToView(void *view); static void attachToView(void *view);
@@ -221,10 +192,10 @@ private:
std::atomic<bool> m_stop; std::atomic<bool> m_stop;
void renderFrame(); void renderFrame();
unordered_map<KrDeviceHandle, DeviceInfo> m_devices; unordered_map<KrDeviceHandle, KRDevice> m_devices;
KrDeviceHandle m_topDeviceHandle; KrDeviceHandle m_topDeviceHandle;
unordered_map<KrSurfaceHandle, SurfaceInfo> m_surfaces; unordered_map<KrSurfaceHandle, KRSurface> m_surfaces;
KrDeviceHandle m_topSurfaceHandle; KrDeviceHandle m_topSurfaceHandle;
unordered_map<KrSurfaceMapIndex, KrSurfaceHandle> m_surfaceHandleMap; unordered_map<KrSurfaceMapIndex, KrSurfaceHandle> m_surfaceHandleMap;

33
kraken/KRDevice.cpp Normal file
View File

@@ -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"

55
kraken/KRDevice.h Normal file
View File

@@ -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<VkCommandBuffer> graphicsCommandBuffers;
std::vector<VkCommandBuffer> computeCommandBuffers;
private:
};
#endif // KRDEVICE_H

View File

@@ -116,8 +116,8 @@ KRPipeline::KRPipeline(KRContext& context, KrSurfaceHandle surfaceHandle, const
m_pipelineLayout = nullptr; m_pipelineLayout = nullptr;
m_graphicsPipeline = nullptr; m_graphicsPipeline = nullptr;
m_renderPass = nullptr; m_renderPass = nullptr;
KRContext::SurfaceInfo& surface = m_pContext->GetSurfaceInfo(surfaceHandle); KRSurface& surface = m_pContext->GetSurfaceInfo(surfaceHandle);
KRContext::DeviceInfo& device = m_pContext->GetDeviceInfo(surface.deviceHandle); KRDevice& device = m_pContext->GetDeviceInfo(surface.deviceHandle);
strcpy(m_szKey, szKey); strcpy(m_szKey, szKey);