WIP Vulkan Pipeline refactoring

This commit is contained in:
2021-08-16 22:04:02 -07:00
parent 0e2bd45b7f
commit 817cd32044
7 changed files with 37 additions and 39 deletions

View File

@@ -932,7 +932,7 @@ KrResult KRContext::createWindowSurface(const KrCreateWindowSurfaceInfo* createW
if (m_vulkanInstance == VK_NULL_HANDLE) { if (m_vulkanInstance == VK_NULL_HANDLE) {
return KR_ERROR_VULKAN_REQUIRED; return KR_ERROR_VULKAN_REQUIRED;
} }
if (m_surfaces.count(createWindowSurfaceInfo->surfaceHandle)) { if (m_surfaceHandleMap.count(createWindowSurfaceInfo->surfaceHandle)) {
return KR_ERROR_DUPLICATE_HANDLE; return KR_ERROR_DUPLICATE_HANDLE;
} }
@@ -959,33 +959,6 @@ KrResult KRContext::createWindowSurface(const KrCreateWindowSurfaceInfo* createW
m_surfaceHandleMap.insert(std::pair<KrSurfaceMapIndex, KrSurfaceHandle>(createWindowSurfaceInfo->surfaceHandle, surfaceHandle)); m_surfaceHandleMap.insert(std::pair<KrSurfaceMapIndex, KrSurfaceHandle>(createWindowSurfaceInfo->surfaceHandle, surfaceHandle));
m_pPipelineManager->createPipelines(surfaceHandle);
{
KRPipeline* testPipeline = m_pPipelineManager->get("vulkan_test");
KRSurface& surface = *m_surfaces[surfaceHandle];
surface.m_swapChainFramebuffers.resize(surface.m_swapChainImageViews.size());
for (size_t i = 0; i < surface.m_swapChainImageViews.size(); i++) {
VkImageView attachments[] = {
surface.m_swapChainImageViews[i]
};
VkFramebufferCreateInfo framebufferInfo{};
framebufferInfo.sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO;
framebufferInfo.renderPass = testPipeline->getRenderPass();
framebufferInfo.attachmentCount = 1;
framebufferInfo.pAttachments = attachments;
framebufferInfo.width = surface.m_swapChainExtent.width;
framebufferInfo.height = surface.m_swapChainExtent.height;
framebufferInfo.layers = 1;
if (vkCreateFramebuffer(deviceInfo->m_logicalDevice, &framebufferInfo, nullptr, &surface.m_swapChainFramebuffers[i]) != VK_SUCCESS) {
// TODO - Error Handling
}
}
}
return KR_SUCCESS; return KR_SUCCESS;
#else #else
// Not implemented for this platform // Not implemented for this platform

View File

@@ -109,15 +109,14 @@ const char *KRPipeline::KRENGINE_UNIFORM_NAMES[] = {
"fade_color", // KRENGINE_UNIFORM_FADE_COLOR "fade_color", // KRENGINE_UNIFORM_FADE_COLOR
}; };
KRPipeline::KRPipeline(KRContext& context, KrSurfaceHandle surfaceHandle, const char* szKey, const std::vector<KRShader*>& shaders) KRPipeline::KRPipeline(KRContext& context, KrDeviceHandle deviceHandle, VkFormat swapChainImageFormat, uint32_t swapChainWidth, uint32_t swapChainHeight, const char* szKey, const std::vector<KRShader*>& shaders)
: KRContextObject(context) : KRContextObject(context)
, m_iProgram(0) // not used for Vulkan , m_iProgram(0) // not used for Vulkan
{ {
m_pipelineLayout = nullptr; m_pipelineLayout = nullptr;
m_graphicsPipeline = nullptr; m_graphicsPipeline = nullptr;
m_renderPass = nullptr; m_renderPass = nullptr;
KRSurface& surface = m_pContext->GetSurfaceInfo(surfaceHandle); KRDevice& device = m_pContext->GetDeviceInfo(deviceHandle);
KRDevice& device = m_pContext->GetDeviceInfo(surface.m_deviceHandle);
strcpy(m_szKey, szKey); strcpy(m_szKey, szKey);
@@ -145,7 +144,7 @@ KRPipeline::KRPipeline(KRContext& context, KrSurfaceHandle surfaceHandle, const
} }
VkAttachmentDescription colorAttachment{}; VkAttachmentDescription colorAttachment{};
colorAttachment.format = surface.m_swapChainImageFormat; colorAttachment.format = swapChainImageFormat;
colorAttachment.samples = VK_SAMPLE_COUNT_1_BIT; colorAttachment.samples = VK_SAMPLE_COUNT_1_BIT;
colorAttachment.loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR; colorAttachment.loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR;
colorAttachment.storeOp = VK_ATTACHMENT_STORE_OP_STORE; colorAttachment.storeOp = VK_ATTACHMENT_STORE_OP_STORE;
@@ -199,14 +198,15 @@ KRPipeline::KRPipeline(KRContext& context, KrSurfaceHandle surfaceHandle, const
VkViewport viewport{}; VkViewport viewport{};
viewport.x = 0.0f; viewport.x = 0.0f;
viewport.y = 0.0f; viewport.y = 0.0f;
viewport.width = (float)surface.m_swapChainExtent.width; viewport.width = static_cast<float>(swapChainWidth);
viewport.height = (float)surface.m_swapChainExtent.height; viewport.height = static_cast<float>(swapChainHeight);
viewport.minDepth = 0.0f; viewport.minDepth = 0.0f;
viewport.maxDepth = 1.0f; viewport.maxDepth = 1.0f;
VkRect2D scissor{}; VkRect2D scissor{};
scissor.offset = { 0, 0 }; scissor.offset = { 0, 0 };
scissor.extent = surface.m_swapChainExtent; scissor.extent.width = swapChainWidth;
scissor.extent.height = swapChainHeight;
VkPipelineViewportStateCreateInfo viewportState{}; VkPipelineViewportStateCreateInfo viewportState{};
viewportState.sType = VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO; viewportState.sType = VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO;

View File

@@ -44,7 +44,7 @@ class KRShader;
class KRPipeline : public KRContextObject { class KRPipeline : public KRContextObject {
public: public:
KRPipeline(KRContext& context, KrSurfaceHandle surfaceHandle, const char* szKey, const std::vector<KRShader*>& shaders); KRPipeline(KRContext& context, KrDeviceHandle deviceHandle, VkFormat swapChainImageFormat, uint32_t swapChainWidth, uint32_t swapChainHeight, const char* szKey, const std::vector<KRShader*>& shaders);
KRPipeline(KRContext &context, char *szKey, std::string options, std::string vertShaderSource, const std::string fragShaderSource); KRPipeline(KRContext &context, char *szKey, std::string options, std::string vertShaderSource, const std::string fragShaderSource);
virtual ~KRPipeline(); virtual ~KRPipeline();
const char *getKey() const; const char *getKey() const;

View File

@@ -62,7 +62,7 @@ KRPipelineManager::~KRPipelineManager() {
#endif // ANDROID #endif // ANDROID
} }
void KRPipelineManager::createPipelines(KrSurfaceHandle surface) void KRPipelineManager::createPipelines(KRSurface& surface)
{ {
{ {
// vulkan_test // vulkan_test
@@ -70,7 +70,7 @@ void KRPipelineManager::createPipelines(KrSurfaceHandle surface)
std::vector<KRShader*> shaders; std::vector<KRShader*> shaders;
shaders.push_back(m_pContext->getShaderManager()->get(pipeline_name + ".vert", "spv")); shaders.push_back(m_pContext->getShaderManager()->get(pipeline_name + ".vert", "spv"));
shaders.push_back(m_pContext->getShaderManager()->get(pipeline_name + ".frag", "spv")); shaders.push_back(m_pContext->getShaderManager()->get(pipeline_name + ".frag", "spv"));
KRPipeline* pipeline = new KRPipeline(*m_pContext, surface, pipeline_name.c_str(), shaders); KRPipeline* pipeline = new KRPipeline(*m_pContext, surface.m_deviceHandle, surface.m_swapChainImageFormat, surface.m_swapChainExtent.width, surface.m_swapChainExtent.height , pipeline_name.c_str(), shaders);
std::pair<std::string, std::vector<int> > key; std::pair<std::string, std::vector<int> > key;
key.first = pipeline_name; key.first = pipeline_name;
m_pipelines[key] = pipeline; m_pipelines[key] = pipeline;

View File

@@ -35,6 +35,7 @@
#include "KRCamera.h" #include "KRCamera.h"
#include "KRDataBlock.h" #include "KRDataBlock.h"
#include "KRNode.h" #include "KRNode.h"
#include "KRSurface.h"
using std::map; using std::map;
using std::vector; using std::vector;
@@ -51,7 +52,7 @@ class KRPipelineManager : public KRContextObject {
public: public:
KRPipelineManager(KRContext &context); KRPipelineManager(KRContext &context);
virtual ~KRPipelineManager(); virtual ~KRPipelineManager();
void createPipelines(KrSurfaceHandle surface); void createPipelines(KRSurface& surface);
KRPipeline* get(const char* szKey); KRPipeline* get(const char* szKey);
KRPipeline *getPipeline(const std::string &pipeline_name, KRCamera *pCamera, const std::vector<KRPointLight *> &point_lights, const std::vector<KRDirectionalLight *> &directional_lights, const std::vector<KRSpotLight *>&spot_lights, int bone_count, bool bDiffuseMap, bool bNormalMap, bool bSpecMap, bool bReflectionMap, bool bReflectionCubeMap, bool bLightMap, bool bDiffuseMapScale,bool bSpecMapScale, bool bNormalMapScale, bool bReflectionMapScale, bool bDiffuseMapOffset, bool bSpecMapOffset, bool bNormalMapOffset, bool bReflectionMapOffset, bool bAlphaTest, bool bAlphaBlend, KRNode::RenderPass renderPass, bool bRimColor = false); KRPipeline *getPipeline(const std::string &pipeline_name, KRCamera *pCamera, const std::vector<KRPointLight *> &point_lights, const std::vector<KRDirectionalLight *> &directional_lights, const std::vector<KRSpotLight *>&spot_lights, int bone_count, bool bDiffuseMap, bool bNormalMap, bool bSpecMap, bool bReflectionMap, bool bReflectionCubeMap, bool bLightMap, bool bDiffuseMapScale,bool bSpecMapScale, bool bNormalMapScale, bool bReflectionMapScale, bool bDiffuseMapOffset, bool bSpecMapOffset, bool bNormalMapOffset, bool bReflectionMapOffset, bool bAlphaTest, bool bAlphaBlend, KRNode::RenderPass renderPass, bool bRimColor = false);

View File

@@ -202,6 +202,29 @@ KrResult KRSurface::initialize()
} }
} }
KRPipelineManager* pipelineManager = m_pContext->getPipelineManager();
pipelineManager->createPipelines(*this);
KRPipeline* testPipeline = pipelineManager->get("vulkan_test");
m_swapChainFramebuffers.resize(m_swapChainImageViews.size());
for (size_t i = 0; i < m_swapChainImageViews.size(); i++) {
VkImageView attachments[] = { m_swapChainImageViews[i] };
VkFramebufferCreateInfo framebufferInfo{};
framebufferInfo.sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO;
framebufferInfo.renderPass = testPipeline->getRenderPass();
framebufferInfo.attachmentCount = 1;
framebufferInfo.pAttachments = attachments;
framebufferInfo.width = m_swapChainExtent.width;
framebufferInfo.height = m_swapChainExtent.height;
framebufferInfo.layers = 1;
if (vkCreateFramebuffer(deviceInfo->m_logicalDevice, &framebufferInfo, nullptr, &m_swapChainFramebuffers[i]) != VK_SUCCESS) {
return KR_ERROR_VULKAN_FRAMEBUFFER;
}
}
return KR_SUCCESS; return KR_SUCCESS;
} }

View File

@@ -49,6 +49,7 @@ typedef enum {
KR_ERROR_VULKAN, KR_ERROR_VULKAN,
KR_ERROR_VULKAN_REQUIRED, KR_ERROR_VULKAN_REQUIRED,
KR_ERROR_VULKAN_SWAP_CHAIN, KR_ERROR_VULKAN_SWAP_CHAIN,
KR_ERROR_VULKAN_FRAMEBUFFER,
KR_ERROR_NO_DEVICE, KR_ERROR_NO_DEVICE,
KR_ERROR_SHADER_COMPILE_FAILED, KR_ERROR_SHADER_COMPILE_FAILED,
KR_ERROR_UNEXPECTED = 0x10000000, KR_ERROR_UNEXPECTED = 0x10000000,