minecraftvulkan/engine/xe_render_system.hpp

157 lines
5 KiB
C++
Raw Normal View History

2022-09-19 16:54:23 +00:00
#pragma once
#include "xe_device.hpp"
#include "xe_pipeline.hpp"
#include "xe_game_object.hpp"
#include "xe_swap_chain.hpp"
2022-09-19 20:35:45 +00:00
#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 <vulkan/vulkan.h>
2022-09-19 16:54:23 +00:00
#include <memory>
#include <stdexcept>
#include <iostream>
2022-09-21 16:36:12 +00:00
#include <map>
2022-09-25 16:07:49 +00:00
#include <vector>
2022-09-19 16:54:23 +00:00
namespace xe {
2022-09-25 01:16:13 +00:00
class RenderSystem {
2022-09-19 16:54:23 +00:00
public:
2022-09-20 01:28:41 +00:00
2022-09-21 16:36:12 +00:00
class Builder {
public:
2022-09-25 01:16:13 +00:00
Builder(Engine &xeEngine, std::string vert, std::string frag) : xeEngine{xeEngine}, vert{vert}, frag{frag} {}
2022-09-21 16:36:12 +00:00
Builder& addVertexBindingf(uint32_t binding, uint32_t dimension, uint32_t offset){
2022-09-25 16:07:49 +00:00
if(dimension == 1)
attributeDescptions.push_back({binding, 0, VK_FORMAT_R32_SFLOAT, offset});
if(dimension == 2)
attributeDescptions.push_back({binding, 0, VK_FORMAT_R32G32_SFLOAT, offset});
if(dimension == 3)
attributeDescptions.push_back({binding, 0, VK_FORMAT_R32G32B32_SFLOAT, offset});
return *this;
}
Builder& addVertexBindingi(uint32_t binding, uint32_t dimension, uint32_t offset){
if(dimension == 1)
attributeDescptions.push_back({binding, 0, VK_FORMAT_R32_SINT, offset});
if(dimension == 2)
attributeDescptions.push_back({binding, 0, VK_FORMAT_R32G32_SINT, offset});
if(dimension == 3)
attributeDescptions.push_back({binding, 0, VK_FORMAT_R32G32B32_SINT, offset});
return *this;
}
2022-09-25 16:07:49 +00:00
Builder& setVertexSize(uint32_t size) {
vertexSize = size;
return *this;
}
2022-09-21 16:36:12 +00:00
Builder& addPushConstant(uint32_t size) {
pushCunstantDataSize = size;
return *this;
}
Builder& addUniformBinding(uint32_t binding, uint32_t size) {
uniformBindings[binding] = size;
return *this;
}
2022-09-25 01:16:13 +00:00
Builder& addTextureBinding(uint32_t binding, Image* image) {
2022-09-21 16:36:12 +00:00
imageBindings[binding] = image;
return *this;
}
2022-09-26 22:03:07 +00:00
Builder& addTextureArrayBinding(uint32_t binding, std::vector<Image*>& image) {
imageArrayBindings[binding] = image;
return *this;
}
2022-09-21 20:49:43 +00:00
Builder& setCulling(bool enabled) {
cullingEnabled = enabled;
return *this;
}
2022-09-25 01:16:13 +00:00
std::unique_ptr<RenderSystem> build() {
2022-09-26 22:03:07 +00:00
return std::make_unique<RenderSystem>(xeEngine, std::move(vert), std::move(frag), std::move(uniformBindings), std::move(imageBindings), std::move(imageArrayBindings), std::move(pushCunstantDataSize), std::move(cullingEnabled), std::move(attributeDescptions), std::move(vertexSize));
2022-09-21 16:36:12 +00:00
}
private:
std::map<uint32_t, uint32_t> uniformBindings{};
2022-09-25 01:16:13 +00:00
std::map<uint32_t, Image*> imageBindings{};
2022-09-26 22:03:07 +00:00
std::map<uint32_t, std::vector<Image*>> imageArrayBindings{};
2022-09-21 16:36:12 +00:00
uint32_t pushCunstantDataSize{0};
2022-09-25 16:07:49 +00:00
std::vector<VkVertexInputAttributeDescription> attributeDescptions{};
uint32_t vertexSize;
2022-09-21 16:36:12 +00:00
std::string vert;
std::string frag;
2022-09-21 20:49:43 +00:00
bool cullingEnabled{false};
2022-09-25 01:16:13 +00:00
Engine &xeEngine;
2022-09-21 16:36:12 +00:00
};
2022-09-25 01:16:13 +00:00
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-26 22:03:07 +00:00
std::map<uint32_t, std::vector<Image*>> imageArrayBindings,
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-20 01:28:41 +00:00
);
2022-09-19 16:54:23 +00:00
2022-09-25 01:16:13 +00:00
~RenderSystem();
2022-09-19 16:54:23 +00:00
2022-09-25 01:16:13 +00:00
RenderSystem(const RenderSystem &) = delete;
RenderSystem operator=(const RenderSystem &) = delete;
2022-09-19 16:54:23 +00:00
2022-09-21 02:02:58 +00:00
void start();
void loadPushConstant(void *pushConstantData);
2022-09-21 16:36:12 +00:00
void loadUniformObject(uint32_t binding, void *uniformBufferData);
2022-09-25 01:16:13 +00:00
void loadTexture(uint32_t binding, Image *image);
2022-09-26 22:03:07 +00:00
void loadTextureArray(uint32_t binding, std::vector<Image*> &images);
2022-09-25 01:16:13 +00:00
void render(GameObject &gameObject);
2022-09-20 01:28:41 +00:00
void stop();
2022-09-19 16:54:23 +00:00
private:
2022-09-20 01:28:41 +00:00
2022-09-29 22:11:53 +00:00
void createDescriptorPool();
2022-09-21 02:02:58 +00:00
void createDescriptorSetLayout();
void createUniformBuffers();
2022-09-21 16:36:12 +00:00
void createDescriptorSets();
void updateDescriptorSet(int frameIndex, bool allocate);
2022-09-21 02:02:58 +00:00
void createPipelineLayout();
2022-09-25 16:07:49 +00:00
void 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
2022-09-20 01:28:41 +00:00
bool boundPipeline{false};
bool boundDescriptor{false};
2022-09-21 16:36:12 +00:00
2022-09-25 01:16:13 +00:00
Device& xeDevice;
Renderer& xeRenderer;
2022-09-19 16:54:23 +00:00
2022-09-25 01:16:13 +00:00
std::map<uint32_t, std::vector<std::unique_ptr<Buffer>>> uboBuffers{};
2022-09-21 16:36:12 +00:00
std::map<uint32_t, uint32_t> uniformBindings;
2022-09-25 01:16:13 +00:00
std::map<uint32_t, Image*> imageBindings;
2022-09-26 22:03:07 +00:00
std::map<uint32_t, std::vector<Image*>> imageArrayBindings{};
2022-09-19 16:54:23 +00:00
std::vector<VkDescriptorSet> descriptorSets;
2022-09-21 16:36:12 +00:00
uint32_t pushCunstantDataSize;
2022-09-21 02:02:58 +00:00
2022-09-19 16:54:23 +00:00
VkPipelineLayout pipelineLayout;
2022-09-25 01:16:13 +00:00
std::unique_ptr<Pipeline> xePipeline;
2022-09-29 22:11:53 +00:00
std::unique_ptr<DescriptorPool> xeDescriptorPool;
2022-09-25 01:16:13 +00:00
std::unique_ptr<DescriptorSetLayout> xeDescriptorSetLayout;
2022-09-19 16:54:23 +00:00
};
}