-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsimple_render_system.cpp
More file actions
85 lines (69 loc) · 2.84 KB
/
simple_render_system.cpp
File metadata and controls
85 lines (69 loc) · 2.84 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
#include "simple_render_system.hpp"
#include <stdexcept>
#include <array>
#include <cassert>
#define GLM_FORCE_RADIANS
#define GLM_FORCE_DEPTH_ZERO_TO_ONE
#include <glm/glm.hpp>
#include <glm/gtc/constants.hpp>
namespace sve {
struct SimplePushConstantData {
glm::mat4 modelMatrix{1.f};
glm::mat4 normalMatrix{1.f};
};
SimpleRenderSystem::SimpleRenderSystem(SveDevice& device, VkRenderPass renderPass, VkDescriptorSetLayout globalSetLayout) : sveDevice{ device } {
createPipelineLayout(globalSetLayout);
createPipeline(renderPass);
}
SimpleRenderSystem::~SimpleRenderSystem() {
vkDestroyPipelineLayout(sveDevice.device(), pipelineLayout, nullptr);
}
void SimpleRenderSystem::createPipelineLayout(VkDescriptorSetLayout globalSetLayout) {
VkPushConstantRange pushConstantRange{};
pushConstantRange.stageFlags = VK_SHADER_STAGE_VERTEX_BIT | VK_SHADER_STAGE_FRAGMENT_BIT;
pushConstantRange.offset = 0;
pushConstantRange.size = sizeof(SimplePushConstantData);
std::vector<VkDescriptorSetLayout> descriptorSetLayouts{globalSetLayout};
VkPipelineLayoutCreateInfo pipelineLayoutInfo{};
pipelineLayoutInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO;
pipelineLayoutInfo.setLayoutCount = static_cast<uint32_t>(descriptorSetLayouts.size());
pipelineLayoutInfo.pSetLayouts = descriptorSetLayouts.data();
pipelineLayoutInfo.pushConstantRangeCount = 1;
pipelineLayoutInfo.pPushConstantRanges = &pushConstantRange;
if (vkCreatePipelineLayout(sveDevice.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{};
SvePipeline::defaultPipelineConfigInfo(pipelineConfig);
pipelineConfig.renderPass = renderPass;
pipelineConfig.pipelineLayout = pipelineLayout;
svePipeline = std::make_unique<SvePipeline>(
sveDevice,
"shaders/simple_shader.vert.spv",
"shaders/simple_shader.frag.spv",
pipelineConfig
);
}
void SimpleRenderSystem::renderGameObjects(FrameInfo& frameInfo, std::vector<SveGameObject>& gameObjects) {
svePipeline->bind(frameInfo.commandBuffer);
vkCmdBindDescriptorSets(
frameInfo.commandBuffer,
VK_PIPELINE_BIND_POINT_GRAPHICS,
pipelineLayout,
0, 1,
&frameInfo.globalDescriptorSet,
0,
nullptr);
for (auto& obj : gameObjects) {
SimplePushConstantData push{};
push.modelMatrix = obj.transform.mat4();
push.normalMatrix = obj.transform.normalMatrix();
vkCmdPushConstants(frameInfo.commandBuffer, pipelineLayout, VK_SHADER_STAGE_VERTEX_BIT | VK_SHADER_STAGE_FRAGMENT_BIT, 0, sizeof(SimplePushConstantData), &push);
obj.model->bind(frameInfo.commandBuffer);
obj.model->draw(frameInfo.commandBuffer);
}
}
}