add simple render system

This commit is contained in:
hoen 2024-04-03 22:22:29 +08:00
parent 1f3b73f5ce
commit b9c5ec712d
11 changed files with 164 additions and 97 deletions

Binary file not shown.

View File

@ -1,4 +1,5 @@
#include "first_app.hpp"
#include "simple_render_system.hpp"
// libs
#define GLM_FORCE_RADIANS
@ -12,27 +13,18 @@
namespace hk
{
struct SimplePushConstantData
{
glm::mat2 transform{1.0f};
glm::vec2 offset;
alignas(16) glm::vec3 color;
};
FirstApp::FirstApp()
{
loadGameObjects();
createPipelineLayout();
createPipeline();
}
FirstApp::~FirstApp()
{
vkDestroyPipelineLayout(m_device.device(), m_pipelineLayout, nullptr);
}
void FirstApp::run()
{
SimpleRenderSystem simpleRenderSystem{m_device, m_renderer.getSwapChainRenderPass()};
while (!m_window.shouldClose())
{
glfwPollEvents();
@ -40,7 +32,7 @@ namespace hk
if (auto commandBuffer = m_renderer.beginFrame())
{
m_renderer.beginSwapChainRenderPass(commandBuffer);
renderGameObjects(commandBuffer);
simpleRenderSystem.renderGameObjects(commandBuffer, m_gameObjects);
m_renderer.endSwapChainRenderPass(commandBuffer);
m_renderer.endFrame();
}
@ -67,65 +59,4 @@ namespace hk
m_gameObjects.push_back(std::move(triangle));
}
void FirstApp::createPipelineLayout()
{
VkPushConstantRange pushConstantRange{};
pushConstantRange.stageFlags = VK_SHADER_STAGE_VERTEX_BIT | VK_SHADER_STAGE_FRAGMENT_BIT;
pushConstantRange.offset = 0;
pushConstantRange.size = sizeof(SimplePushConstantData);
VkPipelineLayoutCreateInfo pipelineLayoutInfo{};
pipelineLayoutInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO;
pipelineLayoutInfo.setLayoutCount = 0;
pipelineLayoutInfo.pSetLayouts = nullptr;
pipelineLayoutInfo.pushConstantRangeCount = 1;
pipelineLayoutInfo.pPushConstantRanges = &pushConstantRange;
if (vkCreatePipelineLayout(m_device.device(), &pipelineLayoutInfo, nullptr, &m_pipelineLayout) !=
VK_SUCCESS)
{
throw std::runtime_error("failed to create pipeline layout!");
}
}
void FirstApp::createPipeline()
{
assert(m_pipelineLayout != nullptr && "Cannot create pipeline before pipeline layout");
PipelineConfigInfo pipelineConfig{};
Pipeline::defaultPipelineConfigInfo(pipelineConfig);
pipelineConfig.renderPass = m_renderer.getSwapChainRenderPass();
pipelineConfig.pipelineLayout = m_pipelineLayout;
m_pipeline = std::make_unique<Pipeline>(
m_device,
"shaders/simple_shader.vert.spv",
"shaders/simple_shader.frag.spv",
pipelineConfig);
}
void FirstApp::renderGameObjects(VkCommandBuffer commandBuffer)
{
m_pipeline->bind(commandBuffer);
for (auto &obj : m_gameObjects)
{
obj.m_transform2d.rotation = glm::mod(obj.m_transform2d.rotation + 0.01f, glm::two_pi<float>());
SimplePushConstantData push{};
push.offset = obj.m_transform2d.translation;
push.color = obj.m_color;
push.transform = obj.m_transform2d.mat2();
vkCmdPushConstants(
commandBuffer,
m_pipelineLayout,
VK_SHADER_STAGE_VERTEX_BIT | VK_SHADER_STAGE_FRAGMENT_BIT,
0,
sizeof(SimplePushConstantData),
&push);
obj.m_model->bind(commandBuffer);
obj.m_model->draw(commandBuffer);
}
}
}
} // namespace hk

View File

@ -2,7 +2,6 @@
#include "hk_device.hpp"
#include "hk_game_object.hpp"
#include "hk_pipeline.hpp"
#include "hk_window.hpp"
#include "hk_renderer.hpp"
@ -28,15 +27,10 @@ namespace hk
private:
void loadGameObjects();
void createPipelineLayout();
void createPipeline();
void renderGameObjects(VkCommandBuffer commandBuffer);
Window m_window{WIDTH, HEIGHT, "Hello Vulkan!"};
Device m_device{m_window};
Renderer m_renderer{m_window, m_device};
std::unique_ptr<Pipeline> m_pipeline;
VkPipelineLayout m_pipelineLayout;
std::vector<GameObject> m_gameObjects;
};
}

View File

@ -37,11 +37,12 @@ namespace hk
}
else
{
m_swapChain = std::make_unique<SwapChain>(m_device, extent, std::move(m_swapChain));
if (m_swapChain->imageCount() != m_commandBuffers.size())
std::shared_ptr<SwapChain> oldSwapChain = std::move(m_swapChain);
m_swapChain = std::make_unique<SwapChain>(m_device, extent, oldSwapChain);
if (!oldSwapChain->compareSwapFormats(*m_swapChain.get()))
{
freeCommandBuffers();
createCommandBuffers();
throw std::runtime_error("Swap chain image(or depth) format has changed!");
}
}
@ -51,7 +52,7 @@ namespace hk
void Renderer::createCommandBuffers()
{
m_commandBuffers.resize(m_swapChain->imageCount());
m_commandBuffers.resize(SwapChain::MAX_FRAMES_IN_FLIGHT);
VkCommandBufferAllocateInfo allocInfo{};
allocInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO;
@ -126,6 +127,7 @@ namespace hk
}
m_isFrameStarted = false;
m_currentFrameIndex = (m_currentFrameIndex + 1) % SwapChain::MAX_FRAMES_IN_FLIGHT;
}
void Renderer::beginSwapChainRenderPass(VkCommandBuffer commandBuffer)
{

View File

@ -25,7 +25,12 @@ namespace hk
bool isFrameInProgress() const {return m_isFrameStarted;}
VkCommandBuffer getCurrentCommandBuffer() const {
assert(m_isFrameStarted && "Cannot get command buffer when frame not in progress");
return m_commandBuffers[m_currentImageIndex];
return m_commandBuffers[m_currentFrameIndex];
}
int getFrameIndex() const {
assert(m_isFrameStarted && "Cannot get frame index when frame not in progress");
return m_currentFrameIndex;
}
VkCommandBuffer beginFrame();
@ -46,6 +51,7 @@ namespace hk
std::vector<VkCommandBuffer> m_commandBuffers;
uint32_t m_currentImageIndex;
int m_currentFrameIndex;
bool m_isFrameStarted;
};
}

View File

@ -272,15 +272,15 @@ namespace hk
subpass.pDepthStencilAttachment = &depthAttachmentRef;
VkSubpassDependency dependency = {};
dependency.srcSubpass = VK_SUBPASS_EXTERNAL;
dependency.srcAccessMask = 0;
dependency.srcStageMask =
VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT | VK_PIPELINE_STAGE_EARLY_FRAGMENT_TESTS_BIT;
dependency.dstSubpass = 0;
dependency.dstStageMask =
VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT | VK_PIPELINE_STAGE_EARLY_FRAGMENT_TESTS_BIT;
dependency.dstAccessMask =
VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT | VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT;
dependency.srcSubpass = VK_SUBPASS_EXTERNAL;
dependency.srcAccessMask = 0;
dependency.srcStageMask =
VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT | VK_PIPELINE_STAGE_EARLY_FRAGMENT_TESTS_BIT;
std::array<VkAttachmentDescription, 2> attachments = {colorAttachment, depthAttachment};
VkRenderPassCreateInfo renderPassInfo = {};
@ -329,6 +329,7 @@ namespace hk
void SwapChain::createDepthResources()
{
VkFormat depthFormat = findDepthFormat();
swapChainDepthFormat = depthFormat;
VkExtent2D swapChainExtent = getSwapChainExtent();
depthImages.resize(imageCount());
@ -422,14 +423,14 @@ namespace hk
VkPresentModeKHR SwapChain::chooseSwapPresentMode(
const std::vector<VkPresentModeKHR> &availablePresentModes)
{
for (const auto &availablePresentMode : availablePresentModes)
{
if (availablePresentMode == VK_PRESENT_MODE_MAILBOX_KHR)
{
std::cout << "Present mode: Mailbox" << std::endl;
return availablePresentMode;
}
}
// for (const auto &availablePresentMode : availablePresentModes)
// {
// if (availablePresentMode == VK_PRESENT_MODE_MAILBOX_KHR)
// {
// std::cout << "Present mode: Mailbox" << std::endl;
// return availablePresentMode;
// }
// }
// for (const auto &availablePresentMode : availablePresentModes)
// {

View File

@ -43,6 +43,12 @@ namespace hk
VkResult acquireNextImage(uint32_t *imageIndex);
VkResult submitCommandBuffers(const VkCommandBuffer *buffers, uint32_t *imageIndex);
bool compareSwapFormats(const SwapChain &swapCahin) const
{
return swapCahin.swapChainDepthFormat == swapChainDepthFormat &&
swapCahin.swapChainImageFormat == swapChainImageFormat;
}
private:
void init();
void createSwapChain();
@ -60,6 +66,7 @@ namespace hk
VkExtent2D chooseSwapExtent(const VkSurfaceCapabilitiesKHR &capabilities);
VkFormat swapChainImageFormat;
VkFormat swapChainDepthFormat;
VkExtent2D swapChainExtent;
std::vector<VkFramebuffer> swapChainFramebuffers;

93
simple_render_system.cpp Normal file
View File

@ -0,0 +1,93 @@
#include "simple_render_system.hpp"
// libs
#define GLM_FORCE_RADIANS
#define GLM_FORCE_DEPTH_ZERO_TO_ONE
#include <glm/glm.hpp>
#include <glm/gtc/constants.hpp>
// std
#include <stdexcept>
#include <array>
namespace hk
{
struct SimplePushConstantData
{
glm::mat2 transform{1.0f};
glm::vec2 offset;
alignas(16) glm::vec3 color;
};
SimpleRenderSystem::SimpleRenderSystem(Device &device, VkRenderPass renderPass) : m_device{device}
{
createPipelineLayout();
createPipeline(renderPass);
}
SimpleRenderSystem::~SimpleRenderSystem()
{
vkDestroyPipelineLayout(m_device.device(), m_pipelineLayout, nullptr);
}
void SimpleRenderSystem::createPipelineLayout()
{
VkPushConstantRange pushConstantRange{};
pushConstantRange.stageFlags = VK_SHADER_STAGE_VERTEX_BIT | VK_SHADER_STAGE_FRAGMENT_BIT;
pushConstantRange.offset = 0;
pushConstantRange.size = sizeof(SimplePushConstantData);
VkPipelineLayoutCreateInfo pipelineLayoutInfo{};
pipelineLayoutInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO;
pipelineLayoutInfo.setLayoutCount = 0;
pipelineLayoutInfo.pSetLayouts = nullptr;
pipelineLayoutInfo.pushConstantRangeCount = 1;
pipelineLayoutInfo.pPushConstantRanges = &pushConstantRange;
if (vkCreatePipelineLayout(m_device.device(), &pipelineLayoutInfo, nullptr, &m_pipelineLayout) !=
VK_SUCCESS)
{
throw std::runtime_error("failed to create pipeline layout!");
}
}
void SimpleRenderSystem::createPipeline(VkRenderPass renderPass)
{
assert(m_pipelineLayout != nullptr && "Cannot create pipeline before pipeline layout");
PipelineConfigInfo pipelineConfig{};
Pipeline::defaultPipelineConfigInfo(pipelineConfig);
pipelineConfig.renderPass = renderPass;
pipelineConfig.pipelineLayout = m_pipelineLayout;
m_pipeline = std::make_unique<Pipeline>(
m_device,
"shaders/simple_shader.vert.spv",
"shaders/simple_shader.frag.spv",
pipelineConfig);
}
void SimpleRenderSystem::renderGameObjects(VkCommandBuffer commandBuffer, std::vector<GameObject> &gameObjects)
{
m_pipeline->bind(commandBuffer);
for (auto &obj : gameObjects)
{
obj.m_transform2d.rotation = glm::mod(obj.m_transform2d.rotation + 0.01f, glm::two_pi<float>());
SimplePushConstantData push{};
push.offset = obj.m_transform2d.translation;
push.color = obj.m_color;
push.transform = obj.m_transform2d.mat2();
vkCmdPushConstants(
commandBuffer,
m_pipelineLayout,
VK_SHADER_STAGE_VERTEX_BIT | VK_SHADER_STAGE_FRAGMENT_BIT,
0,
sizeof(SimplePushConstantData),
&push);
obj.m_model->bind(commandBuffer);
obj.m_model->draw(commandBuffer);
}
}
}

33
simple_render_system.hpp Normal file
View File

@ -0,0 +1,33 @@
#pragma once
#include "hk_device.hpp"
#include "hk_game_object.hpp"
#include "hk_pipeline.hpp"
// std
#include <memory>
#include <vector>
namespace hk
{
class SimpleRenderSystem
{
public:
SimpleRenderSystem(Device &device, VkRenderPass renderPass);
~SimpleRenderSystem();
SimpleRenderSystem(const SimpleRenderSystem &) = delete;
SimpleRenderSystem &operator=(const SimpleRenderSystem &) = delete;
void renderGameObjects(VkCommandBuffer commandBuffer, std::vector<GameObject> &gameObjects);
private:
void createPipelineLayout();
void createPipeline(VkRenderPass renderPass);
Device &m_device;
std::unique_ptr<Pipeline> m_pipeline;
VkPipelineLayout m_pipelineLayout;
};
}