minecraftvulkan/engine/xe_render_system.cpp

236 lines
7.5 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_game_object.hpp"
#include "xe_swap_chain.hpp"
#include "xe_renderer.hpp"
#include "xe_descriptors.hpp"
2022-09-20 01:28:41 +00:00
#include "xe_engine.hpp"
2022-09-19 16:54:23 +00:00
#include <memory>
#include <stdexcept>
2022-09-19 20:35:45 +00:00
#include <iostream>
2022-09-19 16:54:23 +00:00
namespace xe {
2022-09-25 01:16:13 +00:00
RenderSystem::RenderSystem(
Engine &xeEngine,
2022-09-19 16:54:23 +00:00
std::string vert,
2022-09-21 16:36:12 +00:00
std::string frag,
std::map<uint32_t, uint32_t> uniformBindings,
2022-09-25 01:16:13 +00:00
std::map<uint32_t, Image*> imageBindings,
2022-09-21 20:49:43 +00:00
uint32_t pushCunstantDataSize,
2022-09-25 16:07:49 +00:00
bool cullingEnabled,
std::vector<VkVertexInputAttributeDescription> attributeDescptions,
uint32_t vertexSize
2022-09-21 02:02:58 +00:00
) : xeDevice{xeEngine.xeDevice},
xeRenderer{xeEngine.xeRenderer},
2022-09-21 03:04:33 +00:00
xeDescriptorPool{xeEngine.xeDescriptorPool},
2022-09-21 02:02:58 +00:00
pushCunstantDataSize{pushCunstantDataSize},
2022-09-21 16:36:12 +00:00
uniformBindings{uniformBindings},
imageBindings{imageBindings} {
createTextureSampler();
2022-09-21 02:02:58 +00:00
createDescriptorSetLayout();
createUniformBuffers();
2022-09-21 16:36:12 +00:00
createDescriptorSets();
2022-09-21 02:02:58 +00:00
createPipelineLayout();
2022-09-25 16:07:49 +00:00
createPipeline(xeRenderer.getSwapChainRenderPass(), vert, frag, cullingEnabled, attributeDescptions, vertexSize);
2022-09-19 16:54:23 +00:00
}
2022-09-25 01:16:13 +00:00
RenderSystem::~RenderSystem() {
2022-09-20 03:17:15 +00:00
vkDestroyPipelineLayout(xeDevice.device(), pipelineLayout, nullptr);
2022-09-21 16:36:12 +00:00
vkDestroySampler(xeDevice.device(), textureSampler, nullptr);
2022-09-20 03:17:15 +00:00
};
2022-09-19 16:54:23 +00:00
2022-09-25 01:16:13 +00:00
void RenderSystem::createTextureSampler() {
2022-09-21 16:36:12 +00:00
VkSamplerCreateInfo samplerInfo{};
samplerInfo.sType = VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO;
samplerInfo.magFilter = VK_FILTER_LINEAR;
samplerInfo.minFilter = VK_FILTER_LINEAR;
samplerInfo.addressModeU = VK_SAMPLER_ADDRESS_MODE_REPEAT;
samplerInfo.addressModeV = VK_SAMPLER_ADDRESS_MODE_REPEAT;
samplerInfo.addressModeW = VK_SAMPLER_ADDRESS_MODE_REPEAT;
samplerInfo.anisotropyEnable = VK_FALSE;
samplerInfo.borderColor = VK_BORDER_COLOR_INT_OPAQUE_BLACK;
samplerInfo.unnormalizedCoordinates = VK_FALSE;
samplerInfo.compareEnable = VK_FALSE;
samplerInfo.compareOp = VK_COMPARE_OP_ALWAYS;
samplerInfo.mipmapMode = VK_SAMPLER_MIPMAP_MODE_LINEAR;
samplerInfo.mipLodBias = 0.0f;
samplerInfo.minLod = 0.0f;
samplerInfo.maxLod = 0.0f;
if (vkCreateSampler(xeDevice.device(), &samplerInfo, nullptr, &textureSampler) != VK_SUCCESS) {
throw std::runtime_error("failed to create texture sampler!");
}
}
2022-09-25 01:16:13 +00:00
void RenderSystem::createDescriptorSetLayout() {
DescriptorSetLayout::Builder builder{xeDevice};
2022-09-21 16:36:12 +00:00
for ( const auto &[binding, size]: uniformBindings) {
2022-09-21 02:02:58 +00:00
builder.addBinding(binding, VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, VK_SHADER_STAGE_VERTEX_BIT, nullptr);
}
2022-09-21 16:36:12 +00:00
for ( const auto &[binding, image]: imageBindings) {
2022-09-21 02:02:58 +00:00
builder.addBinding(binding, VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, VK_SHADER_STAGE_FRAGMENT_BIT, &textureSampler);
}
xeDescriptorSetLayout = builder.build();
}
2022-09-25 01:16:13 +00:00
void RenderSystem::createUniformBuffers() {
2022-09-21 16:36:12 +00:00
for ( const auto &[binding, bufferSize]: uniformBindings) {
2022-09-25 01:16:13 +00:00
uboBuffers[binding] = std::vector<std::unique_ptr<Buffer>>(SwapChain::MAX_FRAMES_IN_FLIGHT);
2022-09-21 16:36:12 +00:00
for (int i = 0; i < uboBuffers[binding].size(); i++) {
2022-09-25 01:16:13 +00:00
uboBuffers[binding][i] = std::make_unique<Buffer>(
2022-09-21 16:36:12 +00:00
xeDevice,
bufferSize,
2022-09-25 01:16:13 +00:00
SwapChain::MAX_FRAMES_IN_FLIGHT,
2022-09-21 16:36:12 +00:00
VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT,
VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT);
uboBuffers[binding][i]->map();
}
2022-09-19 16:54:23 +00:00
}
2022-09-21 02:02:58 +00:00
}
2022-09-25 01:16:13 +00:00
void RenderSystem::createDescriptorSets() {
2022-09-21 02:02:58 +00:00
2022-09-25 01:16:13 +00:00
descriptorSets = std::vector<VkDescriptorSet>(SwapChain::MAX_FRAMES_IN_FLIGHT);
2022-09-21 03:04:33 +00:00
for (int i = 0; i < descriptorSets.size(); i++) {
2022-09-21 16:36:12 +00:00
updateDescriptorSet(i, true);
2022-09-21 02:02:58 +00:00
}
}
2022-09-25 01:16:13 +00:00
void RenderSystem::updateDescriptorSet(int frameIndex, bool allocate) {
2022-09-21 02:02:58 +00:00
2022-09-25 01:16:13 +00:00
DescriptorWriter writer{*xeDescriptorSetLayout, *xeDescriptorPool};
2022-09-21 16:36:12 +00:00
std::vector<VkDescriptorBufferInfo> bufferInfos{};
int i = 0;
for ( const auto &[binding, size]: uniformBindings) {
bufferInfos.push_back(uboBuffers[binding][frameIndex]->descriptorInfo());
writer.writeBuffer(binding, &bufferInfos[i]);
i++;
}
for ( const auto &[binding, image]: imageBindings) {
VkDescriptorImageInfo imageInfo{};
imageInfo.imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
imageInfo.imageView = image->textureImageView;
imageInfo.sampler = textureSampler;
writer.writeImage(binding, &imageInfo);
}
if (allocate) {
writer.build(descriptorSets[frameIndex]);
} else {
writer.overwrite(descriptorSets[frameIndex]);
}
2022-09-21 03:04:33 +00:00
2022-09-19 16:54:23 +00:00
}
2022-09-25 01:16:13 +00:00
void RenderSystem::createPipelineLayout() {
2022-09-19 20:35:45 +00:00
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
2022-09-21 16:36:12 +00:00
std::vector<VkDescriptorSetLayout> descriptorSetLayouts{xeDescriptorSetLayout->getDescriptorSetLayout()};
2022-09-19 16:54:23 +00:00
VkPipelineLayoutCreateInfo pipelineLayoutInfo{};
pipelineLayoutInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO;
2022-09-21 16:36:12 +00:00
pipelineLayoutInfo.setLayoutCount = static_cast<uint32_t>(descriptorSetLayouts.size());
pipelineLayoutInfo.pSetLayouts = descriptorSetLayouts.data();
2022-09-19 16:54:23 +00:00
if (pushCunstantDataSize > 0) {
pipelineLayoutInfo.pushConstantRangeCount = 1;
pipelineLayoutInfo.pPushConstantRanges = &pushConstantRange;
} else {
pipelineLayoutInfo.pushConstantRangeCount = 0;
pipelineLayoutInfo.pPushConstantRanges = 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
}
2022-09-25 16:07:49 +00:00
void RenderSystem::createPipeline(VkRenderPass renderPass, std::string vert, std::string frag, bool cullingEnabled, std::vector<VkVertexInputAttributeDescription> attributeDescptions, uint32_t vertexSize) {
2022-09-19 16:54:23 +00:00
assert(pipelineLayout != nullptr && "Cannot create pipeline before pipeline layout");
PipelineConfigInfo pipelineConfig{};
2022-09-25 01:16:13 +00:00
Pipeline::defaultPipelineConfigInfo(pipelineConfig);
2022-09-21 20:49:43 +00:00
if (cullingEnabled) {
pipelineConfig.rasterizationInfo.cullMode = VK_CULL_MODE_BACK_BIT;
}
2022-09-19 16:54:23 +00:00
pipelineConfig.renderPass = renderPass;
pipelineConfig.pipelineLayout = pipelineLayout;
2022-09-25 01:16:13 +00:00
xePipeline = std::make_unique<Pipeline>(
2022-09-19 16:54:23 +00:00
xeDevice,
vert,
frag,
2022-09-25 16:07:49 +00:00
pipelineConfig,
attributeDescptions,
vertexSize
2022-09-19 16:54:23 +00:00
);
}
2022-09-25 01:16:13 +00:00
void RenderSystem::start() {
2022-09-21 02:02:58 +00:00
xeRenderer.beginSwapChainRenderPass(xeRenderer.getCurrentCommandBuffer());
xePipeline->bind(xeRenderer.getCurrentCommandBuffer());
if(descriptorSets.size() > 0) {
2022-09-19 16:54:23 +00:00
vkCmdBindDescriptorSets(
2022-09-20 01:28:41 +00:00
xeRenderer.getCurrentCommandBuffer(),
2022-09-19 16:54:23 +00:00
VK_PIPELINE_BIND_POINT_GRAPHICS,
pipelineLayout,
0,
1,
2022-09-20 01:28:41 +00:00
&descriptorSets[xeRenderer.getFrameIndex()],
2022-09-19 16:54:23 +00:00
0,
nullptr);
2022-09-21 02:02:58 +00:00
2022-09-19 16:54:23 +00:00
}
2022-09-21 02:02:58 +00:00
}
2022-09-25 01:16:13 +00:00
void RenderSystem::loadPushConstant(void *pushConstantData) {
2022-09-20 01:28:41 +00:00
vkCmdPushConstants(
xeRenderer.getCurrentCommandBuffer(),
2022-09-19 20:35:45 +00:00
pipelineLayout,
VK_SHADER_STAGE_VERTEX_BIT | VK_SHADER_STAGE_FRAGMENT_BIT,
0,
2022-09-21 02:02:58 +00:00
pushCunstantDataSize,
2022-09-20 01:28:41 +00:00
pushConstantData);
}
2022-09-25 01:16:13 +00:00
void RenderSystem::loadUniformObject(uint32_t binding, void *uniformBufferData) {
2022-09-21 16:36:12 +00:00
uboBuffers[binding][xeRenderer.getFrameIndex()]->writeToBuffer(uniformBufferData);
2022-09-21 02:02:58 +00:00
}
2022-09-25 01:16:13 +00:00
void RenderSystem::loadTexture(uint32_t binding, Image *image) {
2022-09-21 16:36:12 +00:00
imageBindings[binding] = image;
updateDescriptorSet(xeRenderer.getFrameIndex(), false);
2022-09-20 01:28:41 +00:00
}
2022-09-19 20:35:45 +00:00
2022-09-25 01:16:13 +00:00
void RenderSystem::render(GameObject &gameObject) {
2022-09-19 16:54:23 +00:00
2022-09-20 01:28:41 +00:00
gameObject.model->bind(xeRenderer.getCurrentCommandBuffer());
gameObject.model->draw(xeRenderer.getCurrentCommandBuffer());
}
2022-09-25 01:16:13 +00:00
void RenderSystem::stop() {
2022-09-20 01:28:41 +00:00
xeRenderer.endSwapChainRenderPass(xeRenderer.getCurrentCommandBuffer());
2022-09-19 16:54:23 +00:00
}
}