-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSuzumeRenderSystem.cpp
More file actions
96 lines (82 loc) · 3.17 KB
/
SuzumeRenderSystem.cpp
File metadata and controls
96 lines (82 loc) · 3.17 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
90
91
92
93
94
95
96
#include "SuzumeRenderSystem.hpp"
#include "SuzumeDevice.hpp"
#include "glm/fwd.hpp"
#include "glm/gtc/constants.hpp"
#include <array>
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <stdexcept>
#include <vulkan/vulkan_core.h>
#define GLM_FORCE_RADIANS
#define GLM_FORCE_DEPTH_ZERO_TO_ONE
#include <cassert>
namespace Suzume {
struct SuzumePushConstantData {
glm::mat4 transform{
1.0f}; // default @ identity matrix // 8 bytes at offset 0
alignas(16) glm::vec3
color; // 12 bytes at offset 16 (vec3 requires 16-byte alignment in GLSL)
};
SuzumeRenderSystem::SuzumeRenderSystem(SuzumeDevice &device,
VkRenderPass renderPass)
: suzumeDevice{device} {
createPipelineLayout();
createPipeline(renderPass);
}
SuzumeRenderSystem::~SuzumeRenderSystem() {
vkDestroyPipelineLayout(suzumeDevice.device(), pipelineLayout, nullptr);
}
void SuzumeRenderSystem::createPipelineLayout() {
VkPushConstantRange pushConstantRange{};
pushConstantRange.stageFlags =
VK_SHADER_STAGE_VERTEX_BIT | VK_SHADER_STAGE_FRAGMENT_BIT;
pushConstantRange.offset = 0;
pushConstantRange.size = sizeof(SuzumePushConstantData);
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(suzumeDevice.device(), &pipelineLayoutInfo,
nullptr, &pipelineLayout) != VK_SUCCESS) {
throw std::runtime_error("failed to create pipeline layout!");
}
}
void SuzumeRenderSystem::createPipeline(VkRenderPass renderPass) {
PipelineConfigInfo pipelineConfig{};
SuzumePipeline::defaultPipelineConfigInfo(pipelineConfig);
pipelineConfig.renderPass = renderPass;
pipelineConfig.pipelineLayout = pipelineLayout;
suzumePipeline = std::make_unique<SuzumePipeline>(
suzumeDevice, "shaders/SuzumeShader.vert.spv",
"shaders/SuzumeShader.frag.spv", pipelineConfig);
}
void SuzumeRenderSystem::renderGameObjects(
VkCommandBuffer commandBuffer, std::vector<SuzumeGameObject> &gameObjects) {
suzumePipeline->bind(commandBuffer);
// updater for testing shit
int i = 0;
for (auto &obj : gameObjects) {
i += 1;
obj.transform.rotation.y =
glm::mod<float>(obj.transform.rotation.y + 0.001f * i,
2.f * glm::pi<float>()); // rotate veritcally
obj.transform.rotation.x =
glm::mod<float>(obj.transform.rotation.x + 0.0005f * i,
2.f * glm::pi<float>()); // rotate horizontally
}
// renderer
for (auto &obj : gameObjects) {
SuzumePushConstantData push{};
push.color = obj.color;
push.transform = obj.transform.mat4();
vkCmdPushConstants(commandBuffer, pipelineLayout,
VK_SHADER_STAGE_VERTEX_BIT |
VK_SHADER_STAGE_FRAGMENT_BIT,
0, sizeof(SuzumePushConstantData), &push);
obj.model->bind(commandBuffer);
obj.model->draw(commandBuffer);
}
}
} // namespace Suzume