#pragma once #include #include "xe_device.hpp" #include "xe_pipeline.hpp" #include "xe_game_object.hpp" #include "xe_descriptors.hpp" #include "xe_renderer.hpp" #include "xe_engine.hpp" #include "xe_image.hpp" #include #include #include namespace xe { class RenderSystem { public: class Builder { public: Builder(Engine &xeEngine, std::string vert, std::string frag) : xeEngine{xeEngine}, vert{vert}, frag{frag} {} 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; } Builder& addPushConstant(uint32_t size) { pushCunstantDataSize = size; return *this; } Builder& addUniformBinding(uint32_t binding, uint32_t size) { uniformBindings[binding] = size; return *this; } Builder& addTextureBinding(uint32_t binding, Image* image) { imageBindings[binding] = image; return *this; } Builder& setCulling(bool enabled) { cullingEnabled = enabled; return *this; } std::unique_ptr build() { return std::make_unique(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)); } private: std::map uniformBindings{}; std::map imageBindings{}; uint32_t pushCunstantDataSize{0}; std::vector attributeDescptions{}; uint32_t vertexSize; std::string vert; std::string frag; bool cullingEnabled{false}; Engine &xeEngine; }; RenderSystem( Engine &xeEngine, std::string vert, std::string frag, std::map uniformBindings, std::map imageBindings, uint32_t pushCunstantDataSize, bool cullingEnabled, std::vector attributeDescptions, uint32_t vertexSize ); ~RenderSystem(); RenderSystem(const RenderSystem &) = delete; RenderSystem operator=(const RenderSystem &) = delete; void start(); void loadPushConstant(void *pushConstantData); void loadUniformObject(uint32_t binding, void *uniformBufferData); void loadTexture(uint32_t binding, Image *image); void render(GameObject &gameObject); void stop(); private: void createTextureSampler(); void createDescriptorSetLayout(); void createUniformBuffers(); void createDescriptorSets(); void updateDescriptorSet(int frameIndex, bool allocate); void createPipelineLayout(); void createPipeline(VkRenderPass renderPass, std::string vert, std::string frag, bool cullingEnabled, std::vector attributeDescptions, uint32_t vertexSize); bool boundPipeline{false}; bool boundDescriptor{false}; Device& xeDevice; Renderer& xeRenderer; std::map>> uboBuffers{}; std::map uniformBindings; std::map imageBindings; std::vector descriptorSets; uint32_t pushCunstantDataSize; VkSampler textureSampler; VkPipelineLayout pipelineLayout; std::unique_ptr xePipeline; std::unique_ptr &xeDescriptorPool; std::unique_ptr xeDescriptorSetLayout; }; }