minecraftvulkan/engine/xe_render_system.cpp

154 lines
4.6 KiB
C++
Raw Normal View History

2022-09-19 16:54:23 +00:00
#include "xe_render_system.hpp"
#include <vulkan/vulkan.h>
#include "xe_device.hpp"
#include "xe_pipeline.hpp"
#include "xe_frame_info.hpp"
#include "xe_game_object.hpp"
#include "xe_swap_chain.hpp"
#include "xe_renderer.hpp"
#include "xe_descriptors.hpp"
#include <memory>
#include <stdexcept>
2022-09-19 20:35:45 +00:00
#include <iostream>
2022-09-19 16:54:23 +00:00
namespace xe {
XeRenderSystem::XeRenderSystem(
XeDevice &device,
XeRenderer &renderer,
2022-09-19 20:35:45 +00:00
XeDescriptorPool &xeDescriptorPool,
XeDescriptorSetLayout &xeDescriptorSetLayout,
2022-09-19 16:54:23 +00:00
std::string vert,
std::string frag,
uint32_t pushCunstantDataSize,
uint32_t uniformBufferDataSize)
: xeDevice{device} {
createUniformBuffers(xeDescriptorPool, xeDescriptorSetLayout, uniformBufferDataSize);
createPipelineLayout(xeDescriptorSetLayout, pushCunstantDataSize, uniformBufferDataSize);
createPipeline(renderer.getSwapChainRenderPass(), vert, frag);
}
XeRenderSystem::~XeRenderSystem() {};
2022-09-19 20:35:45 +00:00
void XeRenderSystem::createUniformBuffers(XeDescriptorPool &xeDescriptorPool, XeDescriptorSetLayout &xeDescriptorSetLayout, uint32_t uniformBufferDataSize) {
2022-09-19 16:54:23 +00:00
if(uniformBufferDataSize == 0) return;
uboBuffers = std::vector<std::unique_ptr<XeBuffer>>(XeSwapChain::MAX_FRAMES_IN_FLIGHT);
for (int i = 0; i < uboBuffers.size(); i++) {
uboBuffers[i] = std::make_unique<XeBuffer>(
xeDevice,
uniformBufferDataSize,
XeSwapChain::MAX_FRAMES_IN_FLIGHT,
VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT,
VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT);
uboBuffers[i]->map();
}
descriptorSets = std::vector<VkDescriptorSet>(XeSwapChain::MAX_FRAMES_IN_FLIGHT);
for (int i = 0; i < descriptorSets.size(); i++) {
auto bufferInfo = uboBuffers[i]->descriptorInfo();
2022-09-19 20:35:45 +00:00
XeDescriptorWriter(xeDescriptorSetLayout, xeDescriptorPool)
2022-09-19 16:54:23 +00:00
.writeBuffer(0, &bufferInfo)
.build(descriptorSets[i]);
}
}
2022-09-19 20:35:45 +00:00
void XeRenderSystem::createPipelineLayout(XeDescriptorSetLayout &xeDescriptorSetLayout, uint32_t pushCunstantDataSize, uint32_t uniformBufferDataSize) {
VkPushConstantRange pushConstantRange;
pushConstantRange.stageFlags = VK_SHADER_STAGE_VERTEX_BIT | VK_SHADER_STAGE_FRAGMENT_BIT;
pushConstantRange.offset = 0;
pushConstantRange.size = pushCunstantDataSize;
2022-09-19 16:54:23 +00:00
VkPipelineLayoutCreateInfo pipelineLayoutInfo{};
pipelineLayoutInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO;
if (pushCunstantDataSize > 0) {
pipelineLayoutInfo.pushConstantRangeCount = 1;
pipelineLayoutInfo.pPushConstantRanges = &pushConstantRange;
} else {
pipelineLayoutInfo.pushConstantRangeCount = 0;
pipelineLayoutInfo.pPushConstantRanges = nullptr;
}
if (uniformBufferDataSize > 0) {
2022-09-19 20:35:45 +00:00
std::vector<VkDescriptorSetLayout> descriptorSetLayouts{xeDescriptorSetLayout.getDescriptorSetLayout()};
2022-09-19 16:54:23 +00:00
pipelineLayoutInfo.setLayoutCount = static_cast<uint32_t>(descriptorSetLayouts.size());
pipelineLayoutInfo.pSetLayouts = descriptorSetLayouts.data();
} else {
pipelineLayoutInfo.setLayoutCount = 0;
pipelineLayoutInfo.pSetLayouts = nullptr;
}
if(vkCreatePipelineLayout(xeDevice.device(), &pipelineLayoutInfo, nullptr, &pipelineLayout) != VK_SUCCESS) {
std::runtime_error("failed to create pipeline layout!");
}
2022-09-19 20:35:45 +00:00
2022-09-19 16:54:23 +00:00
}
void XeRenderSystem::createPipeline(VkRenderPass renderPass, std::string vert, std::string frag) {
assert(pipelineLayout != nullptr && "Cannot create pipeline before pipeline layout");
PipelineConfigInfo pipelineConfig{};
XePipeline::defaultPipelineConfigInfo(pipelineConfig);
pipelineConfig.renderPass = renderPass;
pipelineConfig.pipelineLayout = pipelineLayout;
xePipeline = std::make_unique<XePipeline>(
xeDevice,
vert,
frag,
pipelineConfig
);
}
2022-09-19 20:35:45 +00:00
void XeRenderSystem::renderGameObjects(
int frameIndex,
VkCommandBuffer commandBuffer,
std::vector<XeGameObject> &gameObjects,
void *pushConstantData,
uint32_t pushConstantSize,
void* uniformBufferData,
uint32_t uniformBufferSize) {
uboBuffers[frameIndex]->writeToBuffer(uniformBufferData);
uboBuffers[frameIndex]->flush();
xePipeline->bind(commandBuffer);
2022-09-19 16:54:23 +00:00
2022-09-19 20:35:45 +00:00
if(pushConstantData == NULL) {
2022-09-19 16:54:23 +00:00
vkCmdBindDescriptorSets(
2022-09-19 20:35:45 +00:00
commandBuffer,
2022-09-19 16:54:23 +00:00
VK_PIPELINE_BIND_POINT_GRAPHICS,
pipelineLayout,
0,
1,
2022-09-19 20:35:45 +00:00
&descriptorSets[frameIndex],
2022-09-19 16:54:23 +00:00
0,
nullptr);
}
for (auto& obj: gameObjects) {
2022-09-19 20:35:45 +00:00
if(pushConstantData == NULL) {
vkCmdPushConstants(
commandBuffer,
pipelineLayout,
VK_SHADER_STAGE_VERTEX_BIT | VK_SHADER_STAGE_FRAGMENT_BIT,
0,
pushConstantSize,
&pushConstantData);
}
obj.model->bind(commandBuffer);
obj.model->draw(commandBuffer);
2022-09-19 16:54:23 +00:00
}
}
}