minecraftvulkan/engine/xe_render_system.hpp

138 lines
4 KiB
C++
Raw Normal View History

2022-09-19 16:54:23 +00:00
#pragma once
#include <vulkan/vulkan.h>
#include "xe_device.hpp"
#include "xe_pipeline.hpp"
#include "xe_game_object.hpp"
2022-09-19 20:35:45 +00:00
#include "xe_descriptors.hpp"
#include "xe_renderer.hpp"
2022-09-20 01:28:41 +00:00
#include "xe_engine.hpp"
2022-09-21 02:02:58 +00:00
#include "xe_image.hpp"
2022-09-19 16:54:23 +00:00
#include <memory>
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
2022-09-25 16:07:49 +00:00
Builder& addVertexBinding(uint32_t binding, uint32_t dimension, uint32_t offset){
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& 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-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-25 16:07:49 +00:00
return std::make_unique<RenderSystem>(xeEngine, std::move(vert), std::move(frag), std::move(uniformBindings), std::move(imageBindings), 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-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-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);
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-21 16:36:12 +00:00
void createTextureSampler();
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-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
VkSampler textureSampler;
2022-09-19 16:54:23 +00:00
VkPipelineLayout pipelineLayout;
2022-09-25 01:16:13 +00:00
std::unique_ptr<Pipeline> xePipeline;
std::unique_ptr<DescriptorPool> &xeDescriptorPool;
std::unique_ptr<DescriptorSetLayout> xeDescriptorSetLayout;
2022-09-19 16:54:23 +00:00
};
}