-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSimpleRenderSystem.cpp
More file actions
89 lines (76 loc) · 3.14 KB
/
SimpleRenderSystem.cpp
File metadata and controls
89 lines (76 loc) · 3.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#include "SimpleRenderSystem.hpp"
#define GLM_FORCE_RADIANS
#define GLM_FORCE_DEPTH_ZERO_TO_ONE
#include <glm/glm.hpp>
#include <glm/gtc/constants.hpp>
#include <stdexcept>
#include <array>
#include <cassert>
namespace engine {
struct SimplePushConstantData
{
glm::mat2 transform{ 1.f };
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("error while creating pipelineLayout");
}
}
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,
"E:/Code/VulkanSDL/shaders/simple_shader.vert.spv",
"E:/Code/VulkanSDL/shaders/simple_shader.frag.spv",
pipelineConfig);
}
void SimpleRenderSystem::renderGameObjects(VkCommandBuffer commandBuffer, std::vector<GameObject>& gameObjects) {
int i = 0;
for (auto& obj : gameObjects) {
i += 1;
obj.transform2d.rotation =
glm::mod<float>(obj.transform2d.rotation + 0.000025f * i, glm::two_pi<float>());
}
m_pipeline->bind(commandBuffer);
for (auto& obj : gameObjects) {
SimplePushConstantData push{};
push.offset = obj.transform2d.translation;
push.color = obj.color;
push.transform = obj.transform2d.mat2();
vkCmdPushConstants(
commandBuffer,
m_pipelineLayout,
VK_SHADER_STAGE_VERTEX_BIT | VK_SHADER_STAGE_FRAGMENT_BIT,
0,
sizeof(SimplePushConstantData),
&push
);
obj.model->bind(commandBuffer);
obj.model->draw(commandBuffer);
}
}
} // namespace engine