add Renderer to code

This commit is contained in:
hoen 2024-04-03 21:28:08 +08:00
parent 976ff14298
commit 1f3b73f5ce
14 changed files with 375 additions and 168 deletions

View File

@ -23,4 +23,4 @@ test: VulkanTest
clean:
rm -f VulkanTest
rm -f *.spv
rm -f *.spv

Binary file not shown.

View File

@ -1,55 +1,88 @@
#include "first_app.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;
};
FirstApp::FirstApp()
{
loadModels();
loadGameObjects();
createPipelineLayout();
recreateSwapChain();
createCommandBuffers();
createPipeline();
}
FirstApp::~FirstApp()
{
vkDestroyPipelineLayout(device.device(), pipelineLayout, nullptr);
vkDestroyPipelineLayout(m_device.device(), m_pipelineLayout, nullptr);
}
void FirstApp::run()
{
while (!window.shouldClose())
while (!m_window.shouldClose())
{
glfwPollEvents();
drawFrame();
if (auto commandBuffer = m_renderer.beginFrame())
{
m_renderer.beginSwapChainRenderPass(commandBuffer);
renderGameObjects(commandBuffer);
m_renderer.endSwapChainRenderPass(commandBuffer);
m_renderer.endFrame();
}
}
vkDeviceWaitIdle(device.device());
vkDeviceWaitIdle(m_device.device());
}
void FirstApp::loadModels()
void FirstApp::loadGameObjects()
{
std::vector<Model::Vertex> vertices{
{{0.0f, -0.5f}, {1.0f, 0.0f, 0.0f}},
{{0.5f, 0.5f}, {0.0f, 1.0f, 0.0f}},
{{-0.5f, 0.5f}, {0.0f, 0.0f, 1.0f}}
};
model = std::make_unique<Model>(device, vertices);
auto model = std::make_shared<Model>(m_device, vertices);
auto triangle = GameObject::createGameObject();
triangle.m_model = model;
triangle.m_color = {.1f, .8f, .1f};
triangle.m_transform2d.translation.x = .2f;
triangle.m_transform2d.scale = {2.0f, .5f};
triangle.m_transform2d.rotation = .25f * glm::two_pi<float>();
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 = 0;
pipelineLayoutInfo.pPushConstantRanges = nullptr;
pipelineLayoutInfo.pushConstantRangeCount = 1;
pipelineLayoutInfo.pPushConstantRanges = &pushConstantRange;
if (vkCreatePipelineLayout(device.device(), &pipelineLayoutInfo, nullptr, &pipelineLayout) !=
if (vkCreatePipelineLayout(m_device.device(), &pipelineLayoutInfo, nullptr, &m_pipelineLayout) !=
VK_SUCCESS)
{
throw std::runtime_error("failed to create pipeline layout!");
@ -58,152 +91,41 @@ namespace hk
void FirstApp::createPipeline()
{
assert(swapChain != nullptr && "Cannot create pipeline before swap chain");
assert(pipelineLayout != nullptr && "Cannot create pipeline before pipeline layout");
assert(m_pipelineLayout != nullptr && "Cannot create pipeline before pipeline layout");
PipelineConfigInfo pipelineConfig{};
Pipeline::defaultPipelineConfigInfo(pipelineConfig);
pipelineConfig.renderPass = swapChain->getRenderPass();
pipelineConfig.pipelineLayout = pipelineLayout;
pipeline = std::make_unique<Pipeline>(
device,
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::recreateSwapChain()
void FirstApp::renderGameObjects(VkCommandBuffer commandBuffer)
{
auto extent = window.getExtend();
while (extent.width == 0 || extent.height == 0)
m_pipeline->bind(commandBuffer);
for (auto &obj : m_gameObjects)
{
extent = window.getExtend();
glfwWaitEvents();
}
obj.m_transform2d.rotation = glm::mod(obj.m_transform2d.rotation + 0.01f, glm::two_pi<float>());
vkDeviceWaitIdle(device.device());
SimplePushConstantData push{};
push.offset = obj.m_transform2d.translation;
push.color = obj.m_color;
push.transform = obj.m_transform2d.mat2();
if (swapChain == nullptr)
{
swapChain = std::make_unique<SwapChain>(device, extent);
}
else
{
swapChain = std::make_unique<SwapChain>(device, extent, std::move(swapChain));
if (swapChain->imageCount() != commandBuffers.size())
{
freeCommandBuffers();
createCommandBuffers();
}
}
// if render pass compatible do nothing else
createPipeline();
}
void FirstApp::createCommandBuffers()
{
commandBuffers.resize(swapChain->imageCount());
VkCommandBufferAllocateInfo allocInfo{};
allocInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO;
allocInfo.level = VK_COMMAND_BUFFER_LEVEL_PRIMARY;
allocInfo.commandPool = device.getCommandPool();
allocInfo.commandBufferCount = static_cast<uint32_t>(commandBuffers.size());
if (vkAllocateCommandBuffers(device.device(), &allocInfo, commandBuffers.data()) !=
VK_SUCCESS)
{
throw std::runtime_error("failed to allocate command buffers!");
}
}
void FirstApp::freeCommandBuffers()
{
vkFreeCommandBuffers(
device.device(),
device.getCommandPool(),
static_cast<float>(commandBuffers.size()),
commandBuffers.data());
commandBuffers.clear();
}
void FirstApp::recordCommandBuffer(int imageIndex)
{
VkCommandBufferBeginInfo beginInfo{};
beginInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO;
if (vkBeginCommandBuffer(commandBuffers[imageIndex], &beginInfo) != VK_SUCCESS)
{
throw std::runtime_error("failed to begin recording command buffer!");
}
VkRenderPassBeginInfo renderPassInfo{};
renderPassInfo.sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO;
renderPassInfo.renderPass = swapChain->getRenderPass();
renderPassInfo.framebuffer = swapChain->getFrameBuffer(imageIndex);
renderPassInfo.renderArea.offset = {0, 0};
renderPassInfo.renderArea.extent = swapChain->getSwapChainExtent();
std::array<VkClearValue, 2> clearValues{};
clearValues[0].color = {0.1f, 0.1f, 0.1f, 1.0f};
clearValues[1].depthStencil = {1.0f, 0};
renderPassInfo.clearValueCount = static_cast<uint32_t>(clearValues.size());
renderPassInfo.pClearValues = clearValues.data();
vkCmdBeginRenderPass(commandBuffers[imageIndex], &renderPassInfo, VK_SUBPASS_CONTENTS_INLINE);
VkViewport viewport{};
viewport.x = 0.0f;
viewport.y = 0.0f;
viewport.width = static_cast<float>(swapChain->getSwapChainExtent().width);
viewport.height = static_cast<float>(swapChain->getSwapChainExtent().height);
viewport.minDepth = 0.0f;
viewport.maxDepth = 1.0f;
VkRect2D scissor{{0, 0}, swapChain->getSwapChainExtent()};
vkCmdSetViewport(commandBuffers[imageIndex], 0, 1, &viewport);
vkCmdSetScissor(commandBuffers[imageIndex], 0, 1, &scissor);
pipeline->bind(commandBuffers[imageIndex]);
model->bind(commandBuffers[imageIndex]);
model->draw(commandBuffers[imageIndex]);
vkCmdEndRenderPass(commandBuffers[imageIndex]);
if (vkEndCommandBuffer(commandBuffers[imageIndex]) != VK_SUCCESS)
{
throw std::runtime_error("failed to record command buffer!");
}
}
void FirstApp::drawFrame()
{
uint32_t imageIndex;
auto result = swapChain->acquireNextImage(&imageIndex);
if (result == VK_ERROR_OUT_OF_DATE_KHR)
{
recreateSwapChain();
return;
}
if (result != VK_SUCCESS && result != VK_SUBOPTIMAL_KHR)
{
throw std::runtime_error("failed to acquire swap chain image!");
}
recordCommandBuffer(imageIndex);
result = swapChain->submitCommandBuffers(&commandBuffers[imageIndex], &imageIndex);
if (result == VK_ERROR_OUT_OF_DATE_KHR || result == VK_SUBOPTIMAL_KHR || window.wasWindowResized())
{
window.resetWindowResizedFlag();
recreateSwapChain();
return;
}
if (result != VK_SUCCESS)
{
throw std::runtime_error("failed to present swap chain image!");
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);
}
}
}

View File

@ -1,10 +1,10 @@
#pragma once
#include "hk_device.hpp"
#include "hk_game_object.hpp"
#include "hk_pipeline.hpp"
#include "hk_swap_chain.hpp"
#include "hk_window.hpp"
#include "hk_model.hpp"
#include "hk_renderer.hpp"
// std
#include <memory>
@ -27,21 +27,16 @@ namespace hk
void run();
private:
void loadModels();
void loadGameObjects();
void createPipelineLayout();
void createPipeline();
void createCommandBuffers();
void freeCommandBuffers();
void drawFrame();
void recreateSwapChain();
void recordCommandBuffer(int imageIndex);
void renderGameObjects(VkCommandBuffer commandBuffer);
Window window{WIDTH, HEIGHT, "Hello Vulkan!"};
Device device{window};
std::unique_ptr<SwapChain> swapChain;
std::unique_ptr<Pipeline> pipeline;
VkPipelineLayout pipelineLayout;
std::vector<VkCommandBuffer> commandBuffers;
std::unique_ptr<Model> model;
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;
};
}

Binary file not shown.

Binary file not shown.

59
hk_game_object.hpp Normal file
View File

@ -0,0 +1,59 @@
#pragma once
#include "hk_model.hpp"
// std
#include <memory>
namespace hk
{
struct Transform2dComponent
{
glm::vec2 translation{};
glm::vec2 scale{1.0f, 1.0f};
float rotation;
glm::mat2 mat2()
{
const float cos = glm::cos(rotation);
const float sin = glm::sin(rotation);
glm::mat2 rotationMat{
{cos, sin},
{-sin, cos}};
glm::mat2 scaleMat{
{scale.x, .0f},
{.0f, scale.y}};
return rotationMat * scaleMat;
}
};
class GameObject
{
public:
using id_t = unsigned int;
static GameObject createGameObject()
{
static id_t currentId = 0;
return GameObject{currentId++};
}
GameObject(const GameObject &) = delete;
GameObject &operator=(const GameObject &) = delete;
GameObject(GameObject &&) = default;
GameObject &operator=(GameObject &&) = default;
id_t getId() const { return m_id; }
std::shared_ptr<Model> m_model{};
glm::vec3 m_color{};
Transform2dComponent m_transform2d{};
private:
GameObject(id_t objId) : m_id(objId) {}
id_t m_id;
};
} // namespace hk

170
hk_renderer.cpp Normal file
View File

@ -0,0 +1,170 @@
#include "hk_renderer.hpp"
// std
#include <stdexcept>
#include <array>
#include <cassert>
namespace hk
{
Renderer::Renderer(Window &window, Device &device) : m_window{window}, m_device{device}
{
recreateSwapChain();
createCommandBuffers();
}
Renderer::~Renderer()
{
freeCommandBuffers();
}
void Renderer::recreateSwapChain()
{
auto extent = m_window.getExtend();
while (extent.width == 0 || extent.height == 0)
{
extent = m_window.getExtend();
glfwWaitEvents();
}
vkDeviceWaitIdle(m_device.device());
if (m_swapChain == nullptr)
{
m_swapChain = std::make_unique<SwapChain>(m_device, extent);
}
else
{
m_swapChain = std::make_unique<SwapChain>(m_device, extent, std::move(m_swapChain));
if (m_swapChain->imageCount() != m_commandBuffers.size())
{
freeCommandBuffers();
createCommandBuffers();
}
}
// TODO: we'll come back to this in just a moment
}
void Renderer::createCommandBuffers()
{
m_commandBuffers.resize(m_swapChain->imageCount());
VkCommandBufferAllocateInfo allocInfo{};
allocInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO;
allocInfo.level = VK_COMMAND_BUFFER_LEVEL_PRIMARY;
allocInfo.commandPool = m_device.getCommandPool();
allocInfo.commandBufferCount = static_cast<uint32_t>(m_commandBuffers.size());
if (vkAllocateCommandBuffers(m_device.device(), &allocInfo, m_commandBuffers.data()) !=
VK_SUCCESS)
{
throw std::runtime_error("failed to allocate command buffers!");
}
}
void Renderer::freeCommandBuffers()
{
vkFreeCommandBuffers(
m_device.device(),
m_device.getCommandPool(),
static_cast<float>(m_commandBuffers.size()),
m_commandBuffers.data());
m_commandBuffers.clear();
}
VkCommandBuffer Renderer::beginFrame()
{
assert(!m_isFrameStarted && "Can't call beginFrame while already in progress");
auto result = m_swapChain->acquireNextImage(&m_currentImageIndex);
if (result == VK_ERROR_OUT_OF_DATE_KHR)
{
recreateSwapChain();
return nullptr;
}
if (result != VK_SUCCESS && result != VK_SUBOPTIMAL_KHR)
{
throw std::runtime_error("failed to acquire swap chain image!");
}
m_isFrameStarted = true;
auto commandBuffer = getCurrentCommandBuffer();
VkCommandBufferBeginInfo beginInfo{};
beginInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO;
if (vkBeginCommandBuffer(commandBuffer, &beginInfo) != VK_SUCCESS)
{
throw std::runtime_error("failed to begin recording command buffer!");
}
return commandBuffer;
}
void Renderer::endFrame()
{
assert(m_isFrameStarted && "Can't call endFrame while frame is not in progress");
auto commandBuffer = getCurrentCommandBuffer();
if (vkEndCommandBuffer(commandBuffer) != VK_SUCCESS)
{
throw std::runtime_error("failed to record command buffer!");
}
auto result = m_swapChain->submitCommandBuffers(&commandBuffer, &m_currentImageIndex);
if (result == VK_ERROR_OUT_OF_DATE_KHR || result == VK_SUBOPTIMAL_KHR || m_window.wasWindowResized())
{
m_window.resetWindowResizedFlag();
recreateSwapChain();
}
if (result != VK_SUCCESS)
{
throw std::runtime_error("failed to present swap chain image!");
}
m_isFrameStarted = false;
}
void Renderer::beginSwapChainRenderPass(VkCommandBuffer commandBuffer)
{
assert(m_isFrameStarted && "Can't call beginSwapChainRenderPass if frame is not in progress");
assert(commandBuffer == getCurrentCommandBuffer() && "Can't beging render pass on command buffer from a differnt frame");
VkRenderPassBeginInfo renderPassInfo{};
renderPassInfo.sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO;
renderPassInfo.renderPass = m_swapChain->getRenderPass();
renderPassInfo.framebuffer = m_swapChain->getFrameBuffer(m_currentImageIndex);
renderPassInfo.renderArea.offset = {0, 0};
renderPassInfo.renderArea.extent = m_swapChain->getSwapChainExtent();
std::array<VkClearValue, 2> clearValues{};
clearValues[0].color = {0.01f, 0.01f, 0.01f, 1.0f};
clearValues[1].depthStencil = {1.0f, 0};
renderPassInfo.clearValueCount = static_cast<uint32_t>(clearValues.size());
renderPassInfo.pClearValues = clearValues.data();
vkCmdBeginRenderPass(commandBuffer, &renderPassInfo, VK_SUBPASS_CONTENTS_INLINE);
VkViewport viewport{};
viewport.x = 0.0f;
viewport.y = 0.0f;
viewport.width = static_cast<float>(m_swapChain->getSwapChainExtent().width);
viewport.height = static_cast<float>(m_swapChain->getSwapChainExtent().height);
viewport.minDepth = 0.0f;
viewport.maxDepth = 1.0f;
VkRect2D scissor{{0, 0}, m_swapChain->getSwapChainExtent()};
vkCmdSetViewport(commandBuffer, 0, 1, &viewport);
vkCmdSetScissor(commandBuffer, 0, 1, &scissor);
}
void Renderer::endSwapChainRenderPass(VkCommandBuffer commandBuffer)
{
assert(m_isFrameStarted && "Can't call endSwapChainRenderPass if frame is not in progress");
assert(commandBuffer == getCurrentCommandBuffer() && "Can't end render pass on command buffer from a differnt frame");
vkCmdEndRenderPass(commandBuffer);
}
}

51
hk_renderer.hpp Normal file
View File

@ -0,0 +1,51 @@
#pragma once
#include "hk_device.hpp"
#include "hk_swap_chain.hpp"
#include "hk_window.hpp"
#include "hk_game_object.hpp"
// std
#include <memory>
#include <vector>
namespace hk
{
class Renderer
{
public:
Renderer(Window &window, Device &device);
~Renderer();
Renderer(const Renderer &) = delete;
Renderer &operator=(const Renderer &) = delete;
VkRenderPass getSwapChainRenderPass() const { return m_swapChain->getRenderPass(); }
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];
}
VkCommandBuffer beginFrame();
void endFrame();
void beginSwapChainRenderPass(VkCommandBuffer commandBuffer);
void endSwapChainRenderPass(VkCommandBuffer commandBuffer);
private:
void createCommandBuffers();
void freeCommandBuffers();
void drawFrame();
void recreateSwapChain();
Window& m_window;
Device& m_device;
std::unique_ptr<SwapChain> m_swapChain;
std::vector<VkCommandBuffer> m_commandBuffers;
uint32_t m_currentImageIndex;
bool m_isFrameStarted;
};
}

View File

@ -431,8 +431,10 @@ namespace hk
}
}
// for (const auto &availablePresentMode : availablePresentModes) {
// if (availablePresentMode == VK_PRESENT_MODE_IMMEDIATE_KHR) {
// for (const auto &availablePresentMode : availablePresentModes)
// {
// if (availablePresentMode == VK_PRESENT_MODE_IMMEDIATE_KHR)
// {
// std::cout << "Present mode: Immediate" << std::endl;
// return availablePresentMode;
// }

View File

@ -1,8 +1,13 @@
#version 450
layout (location = 0) in vec3 fragColor;
layout (location = 0) out vec4 outColor;
layout(push_constant) uniform Push{
mat2 transform;
vec2 offset;
vec3 color;
} push;
void main(){
outColor = vec4(fragColor, 1.0);
outColor = vec4(push.color, 1.0);
}

Binary file not shown.

View File

@ -3,9 +3,12 @@
layout(location = 0) in vec2 position;
layout(location = 1) in vec3 color;
layout(location = 0) out vec3 fragColor;
layout(push_constant) uniform Push{
mat2 transform;
vec2 offset;
vec3 color;
} push;
void main(){
gl_Position = vec4(position, 0.0, 1.0);
fragColor = color;
gl_Position = vec4(push.transform * position + push.offset, 0.0, 1.0);
}

Binary file not shown.