Implemented KRRenderPass::begin and KRRenderPass::end

This commit is contained in:
2022-03-27 21:56:45 -07:00
parent 2818deb0e3
commit 36f804e176
9 changed files with 52 additions and 30 deletions

View File

@@ -36,6 +36,7 @@
#include "KRSpotLight.h"
#include "KRPointLight.h"
#include "KRContext.h"
#include "KRRenderPass.h"
const char *KRPipeline::KRENGINE_UNIFORM_NAMES[] = {
@@ -109,7 +110,7 @@ const char *KRPipeline::KRENGINE_UNIFORM_NAMES[] = {
"fade_color", // KRENGINE_UNIFORM_FADE_COLOR
};
KRPipeline::KRPipeline(KRContext& context, KRSurface& surface, const char* szKey, const std::vector<KRShader*>& shaders, uint32_t vertexAttributes, KRMesh::model_format_t modelFormat)
KRPipeline::KRPipeline(KRContext& context, KRSurface& surface, KRRenderPass& renderPass, const char* szKey, const std::vector<KRShader*>& shaders, uint32_t vertexAttributes, KRMesh::model_format_t modelFormat)
: KRContextObject(context)
, m_iProgram(0) // not used for Vulkan
{
@@ -333,7 +334,7 @@ KRPipeline::KRPipeline(KRContext& context, KRSurface& surface, const char* szKey
pipelineInfo.pColorBlendState = &colorBlending;
pipelineInfo.pDynamicState = nullptr;
pipelineInfo.layout = m_pipelineLayout;
pipelineInfo.renderPass = surface.getRenderPass();
pipelineInfo.renderPass = renderPass.m_renderPass;
pipelineInfo.subpass = 0;
pipelineInfo.basePipelineHandle = VK_NULL_HANDLE;
pipelineInfo.basePipelineIndex = -1;

View File

@@ -43,11 +43,12 @@
class KRShader;
class KRSurface;
class KRRenderPass;
class KRPipeline : public KRContextObject {
public:
KRPipeline(KRContext& context, KRSurface& surface, const char* szKey, const std::vector<KRShader*>& shaders, uint32_t vertexAttributes, KRMesh::model_format_t modelFormat);
KRPipeline(KRContext& context, KRSurface& surface, KRRenderPass& renderPass, const char* szKey, const std::vector<KRShader*>& shaders, uint32_t vertexAttributes, KRMesh::model_format_t modelFormat);
KRPipeline(KRContext &context, char *szKey, std::string options, std::string vertShaderSource, const std::string fragShaderSource);
virtual ~KRPipeline();
const char *getKey() const;

View File

@@ -62,7 +62,7 @@ KRPipelineManager::~KRPipelineManager() {
#endif // ANDROID
}
KRPipeline* KRPipelineManager::getPipeline(KRSurface& surface, const std::string& shader_name, uint32_t vertexAttributes, KRMesh::model_format_t modelFormat)
KRPipeline* KRPipelineManager::getPipeline(KRSurface& surface, KRRenderPass& renderPass, const std::string& shader_name, uint32_t vertexAttributes, KRMesh::model_format_t modelFormat)
{
std::pair<std::string, std::vector<int> > key;
key.first = shader_name;
@@ -80,7 +80,7 @@ KRPipeline* KRPipelineManager::getPipeline(KRSurface& surface, const std::string
std::vector<KRShader*> shaders;
shaders.push_back(m_pContext->getShaderManager()->get(shader_name + ".vert", "spv"));
shaders.push_back(m_pContext->getShaderManager()->get(shader_name + ".frag", "spv"));
KRPipeline* pipeline = new KRPipeline(*m_pContext, surface, shader_name.c_str(), shaders, vertexAttributes, modelFormat);
KRPipeline* pipeline = new KRPipeline(*m_pContext, surface, renderPass, shader_name.c_str(), shaders, vertexAttributes, modelFormat);
m_pipelines[key] = pipeline;

View File

@@ -55,7 +55,7 @@ public:
virtual ~KRPipelineManager();
KRPipeline* get(const char* szKey);
KRPipeline *getPipeline(KRSurface& surface, const std::string& shader_name, uint32_t vertexAttributes, KRMesh::model_format_t modelFormat);
KRPipeline *getPipeline(KRSurface& surface, KRRenderPass& renderPass, const std::string& shader_name, uint32_t vertexAttributes, KRMesh::model_format_t modelFormat);
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);
bool selectPipeline(KRCamera &camera, KRPipeline *pPipeline, const KRViewport &viewport, const Matrix4 &matModel, const std::vector<KRPointLight *> &point_lights, const std::vector<KRDirectionalLight *> &directional_lights, const std::vector<KRSpotLight *>&spot_lights, int bone_count, const KRNode::RenderPass &renderPass, const Vector3 &rim_color, float rim_power, const Vector4 &fade_color);

View File

@@ -30,6 +30,7 @@
//
#include "KRPresentationThread.h"
#include "KRRenderPass.h"
KRPresentationThread::KRPresentationThread(KRContext& context)
: KRContextObject(context)
@@ -153,32 +154,21 @@ void KRPresentationThread::renderFrame()
// TODO - Add error handling...
}
std::array<VkClearValue, 2> clearValues{};
clearValues[0].color = { {0.0f, 0.0f, 0.0f, 1.0f} };
clearValues[1].depthStencil = { 1.0f, 0 };
VkRenderPassBeginInfo renderPassInfo{};
renderPassInfo.sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO;
renderPassInfo.renderPass = surface.getRenderPass();
renderPassInfo.framebuffer = surface.m_swapChainFramebuffers[frameIndex % surface.m_swapChainFramebuffers.size()];
renderPassInfo.renderArea.offset = { 0, 0 };
renderPassInfo.renderArea.extent = surface.m_swapChainExtent;
renderPassInfo.clearValueCount = static_cast<uint32_t>(clearValues.size());
renderPassInfo.pClearValues = clearValues.data();
vkCmdBeginRenderPass(commandBuffer, &renderPassInfo, VK_SUBPASS_CONTENTS_INLINE);
KRRenderPass& forwardOpaquePass = surface.getForwardOpaquePass();
forwardOpaquePass.begin(commandBuffer, surface, frameIndex);
KRMeshManager::KRVBOData& testVertices = getContext().getMeshManager()->KRENGINE_VBO_DATA_2D_SQUARE_VERTICES;
bool haveMesh = testVertices.isVBOReady();
if (haveMesh) {
KRPipeline* testPipeline = m_pContext->getPipelineManager()->getPipeline(surface, "vulkan_test", testVertices.getVertexAttributes(), KRMesh::model_format_t::KRENGINE_MODEL_FORMAT_STRIP);
KRPipeline* testPipeline = m_pContext->getPipelineManager()->getPipeline(surface, forwardOpaquePass, "vulkan_test", testVertices.getVertexAttributes(), KRMesh::model_format_t::KRENGINE_MODEL_FORMAT_STRIP);
testPipeline->bind(commandBuffer);
testVertices.bind(commandBuffer);
vkCmdDraw(commandBuffer, 4, 1, 0, 0);
}
vkCmdEndRenderPass(commandBuffer);
forwardOpaquePass.end(commandBuffer);
if (vkEndCommandBuffer(commandBuffer) != VK_SUCCESS) {
m_activeState = PresentThreadState::error;
// TODO - Add error handling...

View File

@@ -30,6 +30,7 @@
//
#include "KRRenderPass.h"
#include "KRSurface.h""
KRRenderPass::KRRenderPass(KRContext& context)
: KRContextObject(context)
@@ -115,3 +116,26 @@ void KRRenderPass::destroy(KRDevice &device)
m_renderPass = VK_NULL_HANDLE;
}
}
void KRRenderPass::begin(VkCommandBuffer& commandBuffer, KRSurface& surface, uint64_t frameIndex)
{
std::array<VkClearValue, 2> clearValues{};
clearValues[0].color = { {0.0f, 0.0f, 0.0f, 1.0f} };
clearValues[1].depthStencil = { 1.0f, 0 };
VkRenderPassBeginInfo renderPassInfo{};
renderPassInfo.sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO;
renderPassInfo.renderPass = m_renderPass;
renderPassInfo.framebuffer = surface.m_swapChainFramebuffers[frameIndex % surface.m_swapChainFramebuffers.size()];
renderPassInfo.renderArea.offset = { 0, 0 };
renderPassInfo.renderArea.extent = surface.m_swapChainExtent;
renderPassInfo.clearValueCount = static_cast<uint32_t>(clearValues.size());
renderPassInfo.pClearValues = clearValues.data();
vkCmdBeginRenderPass(commandBuffer, &renderPassInfo, VK_SUBPASS_CONTENTS_INLINE);
}
void KRRenderPass::end(VkCommandBuffer& commandBuffer)
{
vkCmdEndRenderPass(commandBuffer);
}

View File

@@ -35,6 +35,8 @@
#include "KREngine-common.h"
#include "KRContext.h"
class KRSurface;
class KRRenderPass : public KRContextObject
{
public:
@@ -43,6 +45,9 @@ public:
void create(KRDevice& device, VkFormat swapChainImageFormat, VkFormat depthImageFormat);
void destroy(KRDevice& device);
void begin(VkCommandBuffer &commandBuffer, KRSurface& surface, uint64_t frameIndex);
void end(VkCommandBuffer& commandBuffer);
// private:
VkRenderPass m_renderPass;
};

View File

@@ -323,7 +323,7 @@ KrResult KRSurface::createSwapChain()
VkFramebufferCreateInfo framebufferInfo{};
framebufferInfo.sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO;
framebufferInfo.renderPass = getRenderPass();
framebufferInfo.renderPass = m_forwardOpaquePass->m_renderPass;
framebufferInfo.attachmentCount = static_cast<uint32_t>(attachments.size());
framebufferInfo.pAttachments = attachments.data();
framebufferInfo.width = m_swapChainExtent.width;
@@ -391,11 +391,6 @@ void KRSurface::createRenderPasses()
m_forwardOpaquePass->create(*device, m_swapChainImageFormat, m_depthImageFormat);
}
VkRenderPass& KRSurface::getRenderPass()
{
return m_forwardOpaquePass->m_renderPass;
}
std::unique_ptr<KRDevice>& KRSurface::getDevice()
{
return m_pContext->getDeviceManager()->getDevice(m_deviceHandle);
@@ -415,3 +410,9 @@ VkFormat KRSurface::getDepthFormat() const
{
return m_depthImageFormat;
}
KRRenderPass& KRSurface::getForwardOpaquePass()
{
return *m_forwardOpaquePass;
}

View File

@@ -59,7 +59,7 @@ public:
KrResult initialize();
KrResult recreateSwapChain();
void createRenderPasses();
VkRenderPass& getRenderPass();
KRRenderPass& getForwardOpaquePass();
#ifdef WIN32
HWND m_hWnd;