2024-02-21 13:28:50 +08:00
|
|
|
#include "first_app.hpp"
|
|
|
|
|
2024-04-03 21:28:08 +08:00
|
|
|
// libs
|
|
|
|
#define GLM_FORCE_RADIANS
|
|
|
|
#define GLM_FORCE_DEPTH_ZERO_TO_ONE
|
|
|
|
#include <glm/glm.hpp>
|
|
|
|
#include <glm/gtc/constants.hpp>
|
|
|
|
|
2024-02-21 13:28:50 +08:00
|
|
|
// std
|
|
|
|
#include <stdexcept>
|
|
|
|
#include <array>
|
|
|
|
|
|
|
|
namespace hk
|
|
|
|
{
|
2024-04-03 21:28:08 +08:00
|
|
|
struct SimplePushConstantData
|
|
|
|
{
|
|
|
|
glm::mat2 transform{1.0f};
|
|
|
|
glm::vec2 offset;
|
|
|
|
alignas(16) glm::vec3 color;
|
|
|
|
};
|
|
|
|
|
2024-02-21 13:28:50 +08:00
|
|
|
FirstApp::FirstApp()
|
|
|
|
{
|
2024-04-03 21:28:08 +08:00
|
|
|
loadGameObjects();
|
2024-02-21 13:28:50 +08:00
|
|
|
createPipelineLayout();
|
2024-04-03 21:28:08 +08:00
|
|
|
createPipeline();
|
2024-02-21 13:28:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
FirstApp::~FirstApp()
|
|
|
|
{
|
2024-04-03 21:28:08 +08:00
|
|
|
vkDestroyPipelineLayout(m_device.device(), m_pipelineLayout, nullptr);
|
2024-02-21 13:28:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void FirstApp::run()
|
|
|
|
{
|
2024-04-03 21:28:08 +08:00
|
|
|
while (!m_window.shouldClose())
|
2024-02-21 13:28:50 +08:00
|
|
|
{
|
|
|
|
glfwPollEvents();
|
2024-04-03 21:28:08 +08:00
|
|
|
|
|
|
|
if (auto commandBuffer = m_renderer.beginFrame())
|
|
|
|
{
|
|
|
|
m_renderer.beginSwapChainRenderPass(commandBuffer);
|
|
|
|
renderGameObjects(commandBuffer);
|
|
|
|
m_renderer.endSwapChainRenderPass(commandBuffer);
|
|
|
|
m_renderer.endFrame();
|
|
|
|
}
|
2024-02-21 13:28:50 +08:00
|
|
|
}
|
|
|
|
|
2024-04-03 21:28:08 +08:00
|
|
|
vkDeviceWaitIdle(m_device.device());
|
2024-02-21 13:28:50 +08:00
|
|
|
}
|
|
|
|
|
2024-04-03 21:28:08 +08:00
|
|
|
void FirstApp::loadGameObjects()
|
2024-02-21 13:28:50 +08:00
|
|
|
{
|
|
|
|
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}}
|
|
|
|
};
|
2024-04-03 21:28:08 +08:00
|
|
|
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));
|
2024-02-21 13:28:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void FirstApp::createPipelineLayout()
|
|
|
|
{
|
2024-04-03 21:28:08 +08:00
|
|
|
VkPushConstantRange pushConstantRange{};
|
|
|
|
pushConstantRange.stageFlags = VK_SHADER_STAGE_VERTEX_BIT | VK_SHADER_STAGE_FRAGMENT_BIT;
|
|
|
|
pushConstantRange.offset = 0;
|
|
|
|
pushConstantRange.size = sizeof(SimplePushConstantData);
|
|
|
|
|
2024-02-21 13:28:50 +08:00
|
|
|
VkPipelineLayoutCreateInfo pipelineLayoutInfo{};
|
|
|
|
pipelineLayoutInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO;
|
|
|
|
pipelineLayoutInfo.setLayoutCount = 0;
|
|
|
|
pipelineLayoutInfo.pSetLayouts = nullptr;
|
2024-04-03 21:28:08 +08:00
|
|
|
pipelineLayoutInfo.pushConstantRangeCount = 1;
|
|
|
|
pipelineLayoutInfo.pPushConstantRanges = &pushConstantRange;
|
2024-02-21 13:28:50 +08:00
|
|
|
|
2024-04-03 21:28:08 +08:00
|
|
|
if (vkCreatePipelineLayout(m_device.device(), &pipelineLayoutInfo, nullptr, &m_pipelineLayout) !=
|
2024-02-21 13:28:50 +08:00
|
|
|
VK_SUCCESS)
|
|
|
|
{
|
|
|
|
throw std::runtime_error("failed to create pipeline layout!");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void FirstApp::createPipeline()
|
|
|
|
{
|
2024-04-03 21:28:08 +08:00
|
|
|
assert(m_pipelineLayout != nullptr && "Cannot create pipeline before pipeline layout");
|
2024-02-21 21:01:49 +08:00
|
|
|
|
|
|
|
PipelineConfigInfo pipelineConfig{};
|
|
|
|
Pipeline::defaultPipelineConfigInfo(pipelineConfig);
|
2024-02-21 13:28:50 +08:00
|
|
|
|
2024-04-03 21:28:08 +08:00
|
|
|
pipelineConfig.renderPass = m_renderer.getSwapChainRenderPass();
|
|
|
|
pipelineConfig.pipelineLayout = m_pipelineLayout;
|
|
|
|
m_pipeline = std::make_unique<Pipeline>(
|
|
|
|
m_device,
|
2024-02-21 13:28:50 +08:00
|
|
|
"shaders/simple_shader.vert.spv",
|
|
|
|
"shaders/simple_shader.frag.spv",
|
|
|
|
pipelineConfig);
|
|
|
|
}
|
|
|
|
|
2024-04-03 21:28:08 +08:00
|
|
|
void FirstApp::renderGameObjects(VkCommandBuffer commandBuffer)
|
2024-02-21 13:28:50 +08:00
|
|
|
{
|
2024-04-03 21:28:08 +08:00
|
|
|
m_pipeline->bind(commandBuffer);
|
|
|
|
for (auto &obj : m_gameObjects)
|
2024-02-21 13:28:50 +08:00
|
|
|
{
|
2024-04-03 21:28:08 +08:00
|
|
|
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);
|
2024-02-21 13:28:50 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|