Made simple render system
This commit is contained in:
BIN
VulkanTest
BIN
VulkanTest
Binary file not shown.
6
build/cve_renderer.d
Normal file
6
build/cve_renderer.d
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
build/cve_renderer.o: cve_renderer.cpp cve_renderer.hpp cve_window.hpp \
|
||||||
|
cve_device.hpp cve_swap_chain.hpp
|
||||||
|
cve_renderer.hpp:
|
||||||
|
cve_window.hpp:
|
||||||
|
cve_device.hpp:
|
||||||
|
cve_swap_chain.hpp:
|
||||||
BIN
build/cve_renderer.o
Normal file
BIN
build/cve_renderer.o
Normal file
Binary file not shown.
@@ -1,10 +1,12 @@
|
|||||||
build/first_app.o: first_app.cpp first_app.hpp cve_window.hpp \
|
build/first_app.o: first_app.cpp first_app.hpp cve_window.hpp \
|
||||||
cve_pipeline.hpp cve_device.hpp cve_swap_chain.hpp cve_game_object.hpp \
|
cve_device.hpp cve_game_object.hpp cve_model.hpp cve_renderer.hpp \
|
||||||
cve_model.hpp
|
cve_swap_chain.hpp simple_render_system.hpp cve_pipeline.hpp
|
||||||
first_app.hpp:
|
first_app.hpp:
|
||||||
cve_window.hpp:
|
cve_window.hpp:
|
||||||
cve_pipeline.hpp:
|
|
||||||
cve_device.hpp:
|
cve_device.hpp:
|
||||||
cve_swap_chain.hpp:
|
|
||||||
cve_game_object.hpp:
|
cve_game_object.hpp:
|
||||||
cve_model.hpp:
|
cve_model.hpp:
|
||||||
|
cve_renderer.hpp:
|
||||||
|
cve_swap_chain.hpp:
|
||||||
|
simple_render_system.hpp:
|
||||||
|
cve_pipeline.hpp:
|
||||||
|
|||||||
Binary file not shown.
@@ -1,9 +1,9 @@
|
|||||||
build/main.o: main.cpp first_app.hpp cve_window.hpp cve_pipeline.hpp \
|
build/main.o: main.cpp first_app.hpp cve_window.hpp cve_device.hpp \
|
||||||
cve_device.hpp cve_swap_chain.hpp cve_game_object.hpp cve_model.hpp
|
cve_game_object.hpp cve_model.hpp cve_renderer.hpp cve_swap_chain.hpp
|
||||||
first_app.hpp:
|
first_app.hpp:
|
||||||
cve_window.hpp:
|
cve_window.hpp:
|
||||||
cve_pipeline.hpp:
|
|
||||||
cve_device.hpp:
|
cve_device.hpp:
|
||||||
cve_swap_chain.hpp:
|
|
||||||
cve_game_object.hpp:
|
cve_game_object.hpp:
|
||||||
cve_model.hpp:
|
cve_model.hpp:
|
||||||
|
cve_renderer.hpp:
|
||||||
|
cve_swap_chain.hpp:
|
||||||
|
|||||||
BIN
build/main.o
BIN
build/main.o
Binary file not shown.
9
build/simple_render_system.d
Normal file
9
build/simple_render_system.d
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
build/simple_render_system.o: simple_render_system.cpp \
|
||||||
|
simple_render_system.hpp cve_pipeline.hpp cve_device.hpp cve_window.hpp \
|
||||||
|
cve_game_object.hpp cve_model.hpp
|
||||||
|
simple_render_system.hpp:
|
||||||
|
cve_pipeline.hpp:
|
||||||
|
cve_device.hpp:
|
||||||
|
cve_window.hpp:
|
||||||
|
cve_game_object.hpp:
|
||||||
|
cve_model.hpp:
|
||||||
BIN
build/simple_render_system.o
Normal file
BIN
build/simple_render_system.o
Normal file
Binary file not shown.
143
cve_renderer.cpp
Normal file
143
cve_renderer.cpp
Normal file
@@ -0,0 +1,143 @@
|
|||||||
|
#include "cve_renderer.hpp"
|
||||||
|
|
||||||
|
#include <stdexcept>
|
||||||
|
#include <array>
|
||||||
|
|
||||||
|
namespace cve {
|
||||||
|
CveRenderer::CveRenderer(CveWindow& window, CveDevice& device) : cveWindow{window}, cveDevice{device}{
|
||||||
|
recreateSwapChain();
|
||||||
|
createCommandBuffers();
|
||||||
|
}
|
||||||
|
|
||||||
|
CveRenderer::~CveRenderer() {
|
||||||
|
freeCommandBuffers();
|
||||||
|
}
|
||||||
|
|
||||||
|
void CveRenderer::recreateSwapChain() {
|
||||||
|
auto extent = cveWindow.getExtent();
|
||||||
|
while (extent.width == 0 || extent.height == 0) {
|
||||||
|
extent = cveWindow.getExtent();
|
||||||
|
glfwWaitEvents();
|
||||||
|
}
|
||||||
|
|
||||||
|
vkDeviceWaitIdle(cveDevice.device());
|
||||||
|
|
||||||
|
if (cveSwapChain == nullptr) {
|
||||||
|
cveSwapChain = std::make_unique<CveSwapChain>(cveDevice, extent);
|
||||||
|
} else {
|
||||||
|
cveSwapChain = std::make_unique<CveSwapChain>(cveDevice, extent, std::move(cveSwapChain));
|
||||||
|
if (cveSwapChain->imageCount() != commandBuffers.size()) {
|
||||||
|
freeCommandBuffers();
|
||||||
|
createCommandBuffers();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Come back
|
||||||
|
}
|
||||||
|
|
||||||
|
void CveRenderer::createCommandBuffers() {
|
||||||
|
commandBuffers.resize(cveSwapChain->imageCount());
|
||||||
|
|
||||||
|
VkCommandBufferAllocateInfo allocInfo{};
|
||||||
|
allocInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO;
|
||||||
|
allocInfo.level = VK_COMMAND_BUFFER_LEVEL_PRIMARY;
|
||||||
|
allocInfo.commandPool = cveDevice.getCommandPool();
|
||||||
|
allocInfo.commandBufferCount = static_cast<uint32_t>(commandBuffers.size());
|
||||||
|
|
||||||
|
if (vkAllocateCommandBuffers(cveDevice.device(), &allocInfo, commandBuffers.data()) !=
|
||||||
|
VK_SUCCESS) {
|
||||||
|
throw std::runtime_error("Failed to allocate command buffers");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void CveRenderer::freeCommandBuffers() {
|
||||||
|
vkFreeCommandBuffers(cveDevice.device(), cveDevice.getCommandPool(),
|
||||||
|
static_cast<uint32_t>(commandBuffers.size()), commandBuffers.data());
|
||||||
|
commandBuffers.clear();
|
||||||
|
}
|
||||||
|
|
||||||
|
VkCommandBuffer CveRenderer::beginFrame() {
|
||||||
|
assert(!isFrameStarted && "Can't call begin frame if frame is already in progress");
|
||||||
|
|
||||||
|
auto result = cveSwapChain->acquireNextImage(¤tImageIndex);
|
||||||
|
|
||||||
|
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!");
|
||||||
|
}
|
||||||
|
|
||||||
|
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 command buffer recording");
|
||||||
|
}
|
||||||
|
|
||||||
|
return commandBuffer;
|
||||||
|
}
|
||||||
|
void CveRenderer::endFrame() {
|
||||||
|
assert(isFrameStarted && "Can't end a frame that hasn't started");
|
||||||
|
auto commandBuffer = getCurrentCommandBuffer();
|
||||||
|
|
||||||
|
if (vkEndCommandBuffer(commandBuffer) != VK_SUCCESS) {
|
||||||
|
throw std::runtime_error("Failed to record command buffer!");
|
||||||
|
}
|
||||||
|
|
||||||
|
auto result = cveSwapChain->submitCommandBuffers(&commandBuffer, ¤tImageIndex);
|
||||||
|
if (result == VK_ERROR_OUT_OF_DATE_KHR || result == VK_SUBOPTIMAL_KHR || cveWindow.wasWindowResized()) {
|
||||||
|
cveWindow.resetWindowResizedFlag();
|
||||||
|
recreateSwapChain();
|
||||||
|
} else if (result != VK_SUCCESS) {
|
||||||
|
throw std::runtime_error("Failed to present swap chain image!");
|
||||||
|
}
|
||||||
|
|
||||||
|
isFrameStarted = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
void CveRenderer::beginSwapChainRenderPass(VkCommandBuffer commandBuffer) {
|
||||||
|
assert(isFrameStarted && "Can't begin swap chain render pass if frame isn't started");
|
||||||
|
assert(commandBuffer == getCurrentCommandBuffer() &&
|
||||||
|
"Can't begin render pass on buffer from another frame.");
|
||||||
|
|
||||||
|
VkRenderPassBeginInfo renderPassInfo{};
|
||||||
|
renderPassInfo.sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO;
|
||||||
|
renderPassInfo.renderPass = cveSwapChain->getRenderPass();
|
||||||
|
renderPassInfo.framebuffer = cveSwapChain->getFrameBuffer(currentImageIndex);
|
||||||
|
|
||||||
|
renderPassInfo.renderArea.offset = {0, 0};
|
||||||
|
renderPassInfo.renderArea.extent = cveSwapChain->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>(cveSwapChain->getSwapChainExtent().width);
|
||||||
|
viewport.height = static_cast<float>(cveSwapChain->getSwapChainExtent().height);
|
||||||
|
viewport.minDepth = 0.0f;
|
||||||
|
viewport.maxDepth = 1.0f;
|
||||||
|
VkRect2D scissor{{0, 0}, cveSwapChain->getSwapChainExtent()};
|
||||||
|
vkCmdSetViewport(commandBuffer, 0, 1, &viewport);
|
||||||
|
vkCmdSetScissor(commandBuffer, 0, 1, &scissor);
|
||||||
|
}
|
||||||
|
void CveRenderer::endSwapChainRenderPass(VkCommandBuffer commandBuffer) {
|
||||||
|
assert(isFrameStarted && "Can't end swap chain render pass if frame isn't started");
|
||||||
|
assert(commandBuffer == getCurrentCommandBuffer() &&
|
||||||
|
"Can't end render pass on buffer from another frame.");
|
||||||
|
|
||||||
|
vkCmdEndRenderPass(commandBuffer);
|
||||||
|
}
|
||||||
|
}
|
||||||
47
cve_renderer.hpp
Normal file
47
cve_renderer.hpp
Normal file
@@ -0,0 +1,47 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include "cve_window.hpp"
|
||||||
|
#include "cve_device.hpp"
|
||||||
|
#include "cve_swap_chain.hpp"
|
||||||
|
|
||||||
|
#include <cassert>
|
||||||
|
#include <memory>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
namespace cve {
|
||||||
|
class CveRenderer {
|
||||||
|
public:
|
||||||
|
|
||||||
|
CveRenderer(CveWindow &window, CveDevice &device);
|
||||||
|
~CveRenderer();
|
||||||
|
|
||||||
|
CveRenderer(const CveRenderer &) = delete;
|
||||||
|
CveRenderer &operator=(const CveRenderer &) = delete;
|
||||||
|
|
||||||
|
VkRenderPass getSwapChainRenderPass() const { return cveSwapChain->getRenderPass(); }
|
||||||
|
bool isFrameInProgress() const { return isFrameStarted; }
|
||||||
|
|
||||||
|
VkCommandBuffer getCurrentCommandBuffer() const {
|
||||||
|
assert(isFrameStarted && "Cannot get command buffer when frame is not in progress");
|
||||||
|
return commandBuffers[currentImageIndex];
|
||||||
|
}
|
||||||
|
|
||||||
|
VkCommandBuffer beginFrame();
|
||||||
|
void endFrame();
|
||||||
|
void beginSwapChainRenderPass(VkCommandBuffer commandBuffer);
|
||||||
|
void endSwapChainRenderPass(VkCommandBuffer commandBuffer);
|
||||||
|
|
||||||
|
private:
|
||||||
|
void createCommandBuffers();
|
||||||
|
void freeCommandBuffers();
|
||||||
|
void recreateSwapChain();
|
||||||
|
|
||||||
|
CveWindow& cveWindow;
|
||||||
|
CveDevice& cveDevice;
|
||||||
|
std::unique_ptr<CveSwapChain> cveSwapChain;
|
||||||
|
std::vector<VkCommandBuffer> commandBuffers;
|
||||||
|
|
||||||
|
uint32_t currentImageIndex;
|
||||||
|
bool isFrameStarted = false;
|
||||||
|
};
|
||||||
|
}
|
||||||
196
first_app.cpp
196
first_app.cpp
@@ -1,4 +1,5 @@
|
|||||||
#include "first_app.hpp"
|
#include "first_app.hpp"
|
||||||
|
#include "simple_render_system.hpp"
|
||||||
|
|
||||||
#include <stdexcept>
|
#include <stdexcept>
|
||||||
#include <array>
|
#include <array>
|
||||||
@@ -9,27 +10,23 @@
|
|||||||
#include <glm/gtc/constants.hpp>
|
#include <glm/gtc/constants.hpp>
|
||||||
|
|
||||||
namespace cve {
|
namespace cve {
|
||||||
struct SimplePushConstantData{
|
|
||||||
glm::mat2 transform{1.f}; // Identity matrix
|
|
||||||
glm::vec2 offset;
|
|
||||||
alignas(16) glm::vec3 color;
|
|
||||||
};
|
|
||||||
|
|
||||||
FirstApp::FirstApp() {
|
FirstApp::FirstApp() {
|
||||||
loadGameObjects();
|
loadGameObjects();
|
||||||
createPipelineLayout();
|
|
||||||
recreateSwapChain();
|
|
||||||
createCommandBuffers();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
FirstApp::~FirstApp() {
|
FirstApp::~FirstApp() {}
|
||||||
vkDestroyPipelineLayout(cveDevice.device(), pipelineLayout, nullptr);
|
|
||||||
}
|
|
||||||
|
|
||||||
void FirstApp::run() {
|
void FirstApp::run() {
|
||||||
|
SimpleRenderSystem simpleRenderSystem{cveDevice, cveRenderer.getSwapChainRenderPass()};
|
||||||
|
|
||||||
while (!cveWindow.shouldClose()) {
|
while (!cveWindow.shouldClose()) {
|
||||||
glfwPollEvents();
|
glfwPollEvents();
|
||||||
drawFrame();
|
if (auto commandBuffer = cveRenderer.beginFrame()) {
|
||||||
|
cveRenderer.beginSwapChainRenderPass(commandBuffer);
|
||||||
|
simpleRenderSystem.renderGameObjects(commandBuffer, gameObjects);
|
||||||
|
cveRenderer.endSwapChainRenderPass(commandBuffer);
|
||||||
|
cveRenderer.endFrame();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
vkDeviceWaitIdle(cveDevice.device());
|
vkDeviceWaitIdle(cveDevice.device());
|
||||||
@@ -53,177 +50,4 @@ namespace cve {
|
|||||||
|
|
||||||
gameObjects.push_back(std::move(triangle));
|
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(cveDevice.device(), &pipelineLayoutInfo, nullptr, &pipelineLayout) !=
|
|
||||||
VK_SUCCESS) {
|
|
||||||
throw std::runtime_error("Failed to create pipeline layout");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void FirstApp::createPipeline() {
|
|
||||||
assert(cveSwapChain != nullptr && "Cannot create pipeline before swap chain");
|
|
||||||
assert(pipelineLayout != nullptr && "Cannot create pipeline before pipeline layout");
|
|
||||||
|
|
||||||
PipelineConfigInfo pipelineConfig{};
|
|
||||||
CvePipeline::defaultPipelineConfigInfo(pipelineConfig);
|
|
||||||
pipelineConfig.renderPass = cveSwapChain->getRenderPass();
|
|
||||||
pipelineConfig.pipelineLayout = pipelineLayout;
|
|
||||||
cvePipeline = std::make_unique<CvePipeline>(
|
|
||||||
cveDevice,
|
|
||||||
"shaders/simple_shader.vert.spv",
|
|
||||||
"shaders/simple_shader.frag.spv",
|
|
||||||
pipelineConfig
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
void FirstApp::recreateSwapChain() {
|
|
||||||
auto extent = cveWindow.getExtent();
|
|
||||||
while (extent.width == 0 || extent.height == 0) {
|
|
||||||
extent = cveWindow.getExtent();
|
|
||||||
glfwWaitEvents();
|
|
||||||
}
|
|
||||||
|
|
||||||
vkDeviceWaitIdle(cveDevice.device());
|
|
||||||
|
|
||||||
if (cveSwapChain == nullptr) {
|
|
||||||
cveSwapChain = std::make_unique<CveSwapChain>(cveDevice, extent);
|
|
||||||
} else {
|
|
||||||
cveSwapChain = std::make_unique<CveSwapChain>(cveDevice, extent, std::move(cveSwapChain));
|
|
||||||
if (cveSwapChain->imageCount() != commandBuffers.size()) {
|
|
||||||
freeCommandBuffers();
|
|
||||||
createCommandBuffers();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Check if render pass is compatible
|
|
||||||
createPipeline();
|
|
||||||
}
|
|
||||||
|
|
||||||
void FirstApp::createCommandBuffers() {
|
|
||||||
commandBuffers.resize(cveSwapChain->imageCount());
|
|
||||||
|
|
||||||
VkCommandBufferAllocateInfo allocInfo{};
|
|
||||||
allocInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO;
|
|
||||||
allocInfo.level = VK_COMMAND_BUFFER_LEVEL_PRIMARY;
|
|
||||||
allocInfo.commandPool = cveDevice.getCommandPool();
|
|
||||||
allocInfo.commandBufferCount = static_cast<uint32_t>(commandBuffers.size());
|
|
||||||
|
|
||||||
if (vkAllocateCommandBuffers(cveDevice.device(), &allocInfo, commandBuffers.data()) !=
|
|
||||||
VK_SUCCESS) {
|
|
||||||
throw std::runtime_error("Failed to allocate command buffers");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void FirstApp::freeCommandBuffers() {
|
|
||||||
vkFreeCommandBuffers(cveDevice.device(), cveDevice.getCommandPool(),
|
|
||||||
static_cast<uint32_t>(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 command buffer recording");
|
|
||||||
}
|
|
||||||
|
|
||||||
VkRenderPassBeginInfo renderPassInfo{};
|
|
||||||
renderPassInfo.sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO;
|
|
||||||
renderPassInfo.renderPass = cveSwapChain->getRenderPass();
|
|
||||||
renderPassInfo.framebuffer = cveSwapChain->getFrameBuffer(imageIndex);
|
|
||||||
|
|
||||||
renderPassInfo.renderArea.offset = {0, 0};
|
|
||||||
renderPassInfo.renderArea.extent = cveSwapChain->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(commandBuffers[imageIndex], &renderPassInfo, VK_SUBPASS_CONTENTS_INLINE);
|
|
||||||
|
|
||||||
VkViewport viewport{};
|
|
||||||
viewport.x = 0.0f;
|
|
||||||
viewport.y = 0.0f;
|
|
||||||
viewport.width = static_cast<float>(cveSwapChain->getSwapChainExtent().width);
|
|
||||||
viewport.height = static_cast<float>(cveSwapChain->getSwapChainExtent().height);
|
|
||||||
viewport.minDepth = 0.0f;
|
|
||||||
viewport.maxDepth = 1.0f;
|
|
||||||
VkRect2D scissor{{0, 0}, cveSwapChain->getSwapChainExtent()};
|
|
||||||
vkCmdSetViewport(commandBuffers[imageIndex], 0, 1, &viewport);
|
|
||||||
vkCmdSetScissor(commandBuffers[imageIndex], 0, 1, &scissor);
|
|
||||||
|
|
||||||
renderGameObjects(commandBuffers[imageIndex]);
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
vkCmdEndRenderPass(commandBuffers[imageIndex]);
|
|
||||||
if (vkEndCommandBuffer(commandBuffers[imageIndex]) != VK_SUCCESS) {
|
|
||||||
throw std::runtime_error("Failed to record command buffer!");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void FirstApp::renderGameObjects(VkCommandBuffer commandBuffer) {
|
|
||||||
cvePipeline->bind(commandBuffer);
|
|
||||||
|
|
||||||
for (auto& obj: gameObjects) {
|
|
||||||
obj.transform2d.rotation = glm::mod(obj.transform2d.rotation + 0.001f, glm::two_pi<float>());
|
|
||||||
|
|
||||||
SimplePushConstantData push{};
|
|
||||||
push.offset = obj.transform2d.translation;
|
|
||||||
push.color = obj.color;
|
|
||||||
push.transform = obj.transform2d.mat2();
|
|
||||||
|
|
||||||
vkCmdPushConstants(
|
|
||||||
commandBuffer,
|
|
||||||
pipelineLayout,
|
|
||||||
VK_SHADER_STAGE_VERTEX_BIT | VK_SHADER_STAGE_FRAGMENT_BIT,
|
|
||||||
0,
|
|
||||||
sizeof(SimplePushConstantData),
|
|
||||||
&push
|
|
||||||
);
|
|
||||||
|
|
||||||
obj.model->bind(commandBuffer);
|
|
||||||
obj.model->draw(commandBuffer);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void FirstApp::drawFrame() {
|
|
||||||
uint32_t imageIndex;
|
|
||||||
auto result = cveSwapChain->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 = cveSwapChain->submitCommandBuffers(&commandBuffers[imageIndex], &imageIndex);
|
|
||||||
if (result == VK_ERROR_OUT_OF_DATE_KHR || result == VK_SUBOPTIMAL_KHR || cveWindow.wasWindowResized()) {
|
|
||||||
cveWindow.resetWindowResizedFlag();
|
|
||||||
recreateSwapChain();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
if (result != VK_SUCCESS) {
|
|
||||||
throw std::runtime_error("Failed to present swap chain image!");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,10 +1,9 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include "cve_window.hpp"
|
#include "cve_window.hpp"
|
||||||
#include "cve_pipeline.hpp"
|
|
||||||
#include "cve_device.hpp"
|
#include "cve_device.hpp"
|
||||||
#include "cve_swap_chain.hpp"
|
|
||||||
#include "cve_game_object.hpp"
|
#include "cve_game_object.hpp"
|
||||||
|
#include "cve_renderer.hpp"
|
||||||
|
|
||||||
#include <memory>
|
#include <memory>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
@@ -24,21 +23,10 @@ namespace cve {
|
|||||||
void run();
|
void run();
|
||||||
private:
|
private:
|
||||||
void loadGameObjects();
|
void loadGameObjects();
|
||||||
void createPipelineLayout();
|
|
||||||
void createPipeline();
|
|
||||||
void createCommandBuffers();
|
|
||||||
void freeCommandBuffers();
|
|
||||||
void drawFrame();
|
|
||||||
void recreateSwapChain();
|
|
||||||
void recordCommandBuffer(int imageIndex);
|
|
||||||
void renderGameObjects(VkCommandBuffer commandBuffer);
|
|
||||||
|
|
||||||
CveWindow cveWindow{WIDTH, HEIGHT, "Hello Vulkan!"};
|
CveWindow cveWindow{WIDTH, HEIGHT, "Hello Vulkan!"};
|
||||||
CveDevice cveDevice{cveWindow};
|
CveDevice cveDevice{cveWindow};
|
||||||
std::unique_ptr<CveSwapChain> cveSwapChain;
|
CveRenderer cveRenderer{cveWindow, cveDevice};
|
||||||
std::unique_ptr<CvePipeline> cvePipeline;
|
|
||||||
VkPipelineLayout pipelineLayout;
|
|
||||||
std::vector<VkCommandBuffer> commandBuffers;
|
|
||||||
std::vector<CveGameObject> gameObjects;
|
std::vector<CveGameObject> gameObjects;
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|||||||
85
simple_render_system.cpp
Normal file
85
simple_render_system.cpp
Normal file
@@ -0,0 +1,85 @@
|
|||||||
|
#include "simple_render_system.hpp"
|
||||||
|
|
||||||
|
#include <stdexcept>
|
||||||
|
#include <array>
|
||||||
|
|
||||||
|
#define GLM_FORCE_RADIANS
|
||||||
|
#define GLM_FORCE_DEPTH_ZERO_TO_ONE
|
||||||
|
#include <glm/glm.hpp>
|
||||||
|
#include <glm/gtc/constants.hpp>
|
||||||
|
|
||||||
|
namespace cve {
|
||||||
|
struct SimplePushConstantData{
|
||||||
|
glm::mat2 transform{1.f}; // Identity matrix
|
||||||
|
glm::vec2 offset;
|
||||||
|
alignas(16) glm::vec3 color;
|
||||||
|
};
|
||||||
|
|
||||||
|
SimpleRenderSystem::SimpleRenderSystem(CveDevice& device, VkRenderPass renderPass):
|
||||||
|
cveDevice{device} {
|
||||||
|
createPipelineLayout();
|
||||||
|
createPipeline(renderPass);
|
||||||
|
}
|
||||||
|
|
||||||
|
SimpleRenderSystem::~SimpleRenderSystem() {
|
||||||
|
vkDestroyPipelineLayout(cveDevice.device(), 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(cveDevice.device(), &pipelineLayoutInfo, nullptr, &pipelineLayout) !=
|
||||||
|
VK_SUCCESS) {
|
||||||
|
throw std::runtime_error("Failed to create pipeline layout");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void SimpleRenderSystem::createPipeline(VkRenderPass renderPass) {
|
||||||
|
assert(pipelineLayout != nullptr && "Cannot create pipeline before pipeline layout");
|
||||||
|
|
||||||
|
PipelineConfigInfo pipelineConfig{};
|
||||||
|
CvePipeline::defaultPipelineConfigInfo(pipelineConfig);
|
||||||
|
pipelineConfig.renderPass = renderPass;
|
||||||
|
pipelineConfig.pipelineLayout = pipelineLayout;
|
||||||
|
cvePipeline = std::make_unique<CvePipeline>(
|
||||||
|
cveDevice,
|
||||||
|
"shaders/simple_shader.vert.spv",
|
||||||
|
"shaders/simple_shader.frag.spv",
|
||||||
|
pipelineConfig
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
void SimpleRenderSystem::renderGameObjects(VkCommandBuffer commandBuffer, std::vector<CveGameObject> &gameObjects) {
|
||||||
|
cvePipeline->bind(commandBuffer);
|
||||||
|
|
||||||
|
for (auto& obj: gameObjects) {
|
||||||
|
obj.transform2d.rotation = glm::mod(obj.transform2d.rotation + 0.001f, glm::two_pi<float>());
|
||||||
|
|
||||||
|
SimplePushConstantData push{};
|
||||||
|
push.offset = obj.transform2d.translation;
|
||||||
|
push.color = obj.color;
|
||||||
|
push.transform = obj.transform2d.mat2();
|
||||||
|
|
||||||
|
vkCmdPushConstants(
|
||||||
|
commandBuffer,
|
||||||
|
pipelineLayout,
|
||||||
|
VK_SHADER_STAGE_VERTEX_BIT | VK_SHADER_STAGE_FRAGMENT_BIT,
|
||||||
|
0,
|
||||||
|
sizeof(SimplePushConstantData),
|
||||||
|
&push
|
||||||
|
);
|
||||||
|
|
||||||
|
obj.model->bind(commandBuffer);
|
||||||
|
obj.model->draw(commandBuffer);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
29
simple_render_system.hpp
Normal file
29
simple_render_system.hpp
Normal file
@@ -0,0 +1,29 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include "cve_pipeline.hpp"
|
||||||
|
#include "cve_device.hpp"
|
||||||
|
#include "cve_game_object.hpp"
|
||||||
|
|
||||||
|
#include <memory>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
namespace cve {
|
||||||
|
class SimpleRenderSystem {
|
||||||
|
public:
|
||||||
|
|
||||||
|
SimpleRenderSystem(CveDevice &device, VkRenderPass renderPass);
|
||||||
|
~SimpleRenderSystem();
|
||||||
|
|
||||||
|
SimpleRenderSystem(const SimpleRenderSystem &) = delete;
|
||||||
|
SimpleRenderSystem &operator=(const SimpleRenderSystem &) = delete;
|
||||||
|
void renderGameObjects(VkCommandBuffer commandBuffer, std::vector<CveGameObject>& gameObjects);
|
||||||
|
|
||||||
|
private:
|
||||||
|
void createPipelineLayout();
|
||||||
|
void createPipeline(VkRenderPass renderPass);
|
||||||
|
|
||||||
|
CveDevice& cveDevice;
|
||||||
|
std::unique_ptr<CvePipeline> cvePipeline;
|
||||||
|
VkPipelineLayout pipelineLayout;
|
||||||
|
};
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user