From 430a008ab34a2d8d381108c4b697894dc7d691c0 Mon Sep 17 00:00:00 2001 From: Tyler Murphy Date: Mon, 19 Sep 2022 12:54:23 -0400 Subject: [PATCH] more recatoring, doesnt compile --- engine/xe_engine.cpp | 25 ++++- engine/xe_engine.hpp | 13 ++- engine/xe_frame_info.hpp | 1 - engine/xe_render_system.cpp | 145 +++++++++++++++++++++++++++++ engine/xe_render_system.hpp | 51 ++++++++++ res/shaders/simple_shader.frag.spv | Bin 1112 -> 0 bytes res/shaders/simple_shader.vert.spv | Bin 2520 -> 0 bytes src/first_app.cpp | 50 ++-------- 8 files changed, 237 insertions(+), 48 deletions(-) create mode 100644 engine/xe_render_system.cpp create mode 100644 engine/xe_render_system.hpp delete mode 100644 res/shaders/simple_shader.frag.spv delete mode 100644 res/shaders/simple_shader.vert.spv diff --git a/engine/xe_engine.cpp b/engine/xe_engine.cpp index 8d2655a..ff9c95b 100644 --- a/engine/xe_engine.cpp +++ b/engine/xe_engine.cpp @@ -4,14 +4,35 @@ namespace xe { XeEngine::XeEngine(int width, int height, std::string name) - : xeWindow{width, height, name}, xeDevice{xeWindow}, xeRenderer{xeWindow, xeDevice} {}; + : xeWindow{width, height, name}, xeDevice{xeWindow}, xeRenderer{xeWindow, xeDevice} { + loadDescriptors(); + }; -void XeEngine::loadDescriptorPool() { +XeEngine::~XeEngine() {}; + +void XeEngine::loadDescriptors() { xeDescriptorPool = XeDescriptorPool::Builder(xeDevice) .setMaxSets(XeSwapChain::MAX_FRAMES_IN_FLIGHT) .addPoolSize(VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, XeSwapChain::MAX_FRAMES_IN_FLIGHT) .addPoolSize(VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, XeSwapChain::MAX_FRAMES_IN_FLIGHT) .build(); + + xeDescriptorSetLayout = XeDescriptorSetLayout::Builder(xeDevice) + .addBinding(0, VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, VK_SHADER_STAGE_VERTEX_BIT) + .build(); +} + +std::unique_ptr XeEngine::createRenderSystem(const std::string &vert, const std::string &frag, typename pushCunstant, typename uniformBuffer) { + return std::make_unique( + xeDevice, + xeRenderer, + xeDescriptorPool, + xeDescriptorSetLayout, + vert, + frag, + pushCunstantDataSize, + uniformBufferDataSize + ); } } \ No newline at end of file diff --git a/engine/xe_engine.hpp b/engine/xe_engine.hpp index 93d3f8b..7beb962 100644 --- a/engine/xe_engine.hpp +++ b/engine/xe_engine.hpp @@ -3,6 +3,8 @@ #include "xe_window.hpp" #include "xe_device.hpp" #include "xe_renderer.hpp" +#include "xe_render_system.hpp" +#include "xe_descriptors.hpp" namespace xe { @@ -16,25 +18,26 @@ class XeEngine { XeEngine(const XeEngine&) = delete; XeEngine operator=(const XeEngine&) = delete; - const XeWindow& getWindow() const { + XeWindow& getWindow() { return xeWindow; }; - const XeRenderer& getRenderer() const { + XeRenderer& getRenderer() { return xeRenderer; }; + std::unique_ptr createRenderSystem(const std::string &vert, const std::string &frag, uint32_t pushCunstantDataSize, uint32_t uniformBufferDataSize); private: - void loadDescriptorPool(); + void loadDescriptors(); XeWindow xeWindow; XeDevice xeDevice; XeRenderer xeRenderer; - std::unique_ptr xeDescriptorPool{}; - std::unique_ptr xeDescriptorSetLayout; + std::unique_ptr xeDescriptorPool; + std::unique_ptr xeDescriptorSetLayout; }; } \ No newline at end of file diff --git a/engine/xe_frame_info.hpp b/engine/xe_frame_info.hpp index d6cf7cb..074895d 100644 --- a/engine/xe_frame_info.hpp +++ b/engine/xe_frame_info.hpp @@ -11,7 +11,6 @@ struct XeFrameInfo { float frameTime; VkCommandBuffer commandBuffer; XeCamera &camera; - VkDescriptorSet globalDescriptorSet; }; } \ No newline at end of file diff --git a/engine/xe_render_system.cpp b/engine/xe_render_system.cpp new file mode 100644 index 0000000..ec56972 --- /dev/null +++ b/engine/xe_render_system.cpp @@ -0,0 +1,145 @@ +#include "xe_render_system.hpp" + +#include +#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 +#include + +namespace xe { + +template +XeRenderSystem::XeRenderSystem( + XeDevice &device, + XeRenderer &renderer, + std::unique_ptr &xeDescriptorPool, + std::unique_ptr &xeDescriptorSetLayout, + 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); +} + + +template +XeRenderSystem::~XeRenderSystem() {}; + + +template +void XeRenderSystem::createUniformBuffers(std::unique_ptr &xeDescriptorPool, std::unique_ptr &xeDescriptorSetLayout, uint32_t uniformBufferDataSize) { + if(uniformBufferDataSize == 0) return; + + uboBuffers = std::vector>(XeSwapChain::MAX_FRAMES_IN_FLIGHT); + for (int i = 0; i < uboBuffers.size(); i++) { + uboBuffers[i] = std::make_unique( + 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(XeSwapChain::MAX_FRAMES_IN_FLIGHT); + for (int i = 0; i < descriptorSets.size(); i++) { + auto bufferInfo = uboBuffers[i]->descriptorInfo(); + XeDescriptorWriter(*xeDescriptorSetLayout, *xeDescriptorPool) + .writeBuffer(0, &bufferInfo) + .build(descriptorSets[i]); + } +} + + +template +void XeRenderSystem::createPipelineLayout(std::unique_ptr &xeDescriptorSetLayout, uint32_t pushCunstantDataSize, uint32_t uniformBufferDataSize) { + + VkPipelineLayoutCreateInfo pipelineLayoutInfo{}; + pipelineLayoutInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO; + + if (pushCunstantDataSize > 0) { + VkPushConstantRange pushConstantRange; + pushConstantRange.stageFlags = VK_SHADER_STAGE_VERTEX_BIT | VK_SHADER_STAGE_FRAGMENT_BIT; + pushConstantRange.offset = 0; + pushConstantRange.size = pushCunstantDataSize; + pipelineLayoutInfo.pushConstantRangeCount = 1; + pipelineLayoutInfo.pPushConstantRanges = &pushConstantRange; + } else { + pipelineLayoutInfo.pushConstantRangeCount = 0; + pipelineLayoutInfo.pPushConstantRanges = nullptr; + } + + if (uniformBufferDataSize > 0) { + std::vector descriptorSetLayouts{xeDescriptorSetLayout->getDescriptorSetLayout()}; + pipelineLayoutInfo.setLayoutCount = static_cast(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!"); + } +} + + +template +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( + xeDevice, + vert, + frag, + pipelineConfig + ); +} + + +template +void XeRenderSystem::renderGameObjects(XeFrameInfo &frameInfo, std::vector &gameObjects, XeRenderSystem::XeData pushConstantData) { + xePipeline->bind(frameInfo.commandBuffer); + + if(uboBuffers.size() > 0) { + vkCmdBindDescriptorSets( + frameInfo.commandBuffer, + VK_PIPELINE_BIND_POINT_GRAPHICS, + pipelineLayout, + 0, + 1, + &descriptorSets[frameInfo.frameIndex], + 0, + nullptr); + } + + for (auto& obj: gameObjects) { + + vkCmdPushConstants( + frameInfo.commandBuffer, + pipelineLayout, + VK_SHADER_STAGE_VERTEX_BIT | VK_SHADER_STAGE_FRAGMENT_BIT, + 0, + sizeof(pushConstantData), + &pushConstantData); + + obj.model->bind(frameInfo.commandBuffer); + obj.model->draw(frameInfo.commandBuffer); + } + +} + +} \ No newline at end of file diff --git a/engine/xe_render_system.hpp b/engine/xe_render_system.hpp new file mode 100644 index 0000000..42079df --- /dev/null +++ b/engine/xe_render_system.hpp @@ -0,0 +1,51 @@ +#pragma once + +#include +#include "xe_device.hpp" +#include "xe_pipeline.hpp" +#include "xe_frame_info.hpp" +#include "xe_game_object.hpp" + +#include + +namespace xe { + +template +class XeRenderSystem { + public: + + struct XeData{}; + + XeRenderSystem( + XeDevice &device, + XeRenderer &renderer, + std::unique_ptr &xeDescriptorPool, + std::unique_ptr &xeDescriptorSetLayout, + std::string vert, + std::string frag, + uint32_t pushCunstantDataSize, + uint32_t uniformBufferDataSize); + + ~XeRenderSystem(); + + XeRenderSystem(const XeRenderSystem &) = delete; + XeRenderSystem operator=(const XeRenderSystem &) = delete; + + void renderGameObjects(XeFrameInfo &frameInfo, std::vector &gameObjects, XeRenderSystem::XeData pushConstantData); + + private: + void createUniformBuffers(std::unique_ptr &xeDescriptorPool, std::unique_ptr &xeDescriptorSetLayout, uint32_t uniformBufferDataSize); + void createPipelineLayout(std::unique_ptr &xeDescriptorSetLayout, uint32_t pushCunstantDataSize, uint32_t uniformBufferDataSize); + void createPipeline(VkRenderPass renderPass, std::string vert, std::string frag); + + XeDevice& xeDevice; + + std::unique_ptr xePipeline; + std::vector> uboBuffers; + std::vector descriptorSets; + + VkPipelineLayout pipelineLayout; + +}; + +} \ No newline at end of file diff --git a/res/shaders/simple_shader.frag.spv b/res/shaders/simple_shader.frag.spv deleted file mode 100644 index ff80cc6a1d171521e711f7b80cfd961f390701ef..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 1112 zcmYk4%}?7v5XGki8w%yS1=<6GazsKsK!t>$NT@OgS5WoiYMI19=+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?bIcH zQabmE4?OkGYG*!p>Y;PbaM;I6nHb_Zb7X-1f_7$tCmx+|4i5Y0rELCRO79b>k3YI6 zz%dWKP7BWccAxmKCDA)Gp>0z5w4$3=lVs``9U@_)&P7`_GK;K{?h zQ-UWvl@Bw`2yiUpE=h*#t8ibH9JB4HTl&2$5RX6hY2i=h#dbxYK0fr!8@MX)9nkYV zoeVmDHq#Bs%*0!;x#uLq(;Inj3UI7#eX-2+tpE>L^i7`@^POa3;4S8sWO%^(G-h5L zG32qB+mhh{%VKtPlDydkfnLasubug#WZo}lL%(+feBnnG3-^OSz6}98=fgXJw>*SL$#D1)?`wzqR}~k9{{T9|xp4ph diff --git a/src/first_app.cpp b/src/first_app.cpp index cc8f634..1813d75 100755 --- a/src/first_app.cpp +++ b/src/first_app.cpp @@ -32,30 +32,7 @@ FirstApp::~FirstApp() {} void FirstApp::run() { - std::vector> uboBuffers(XeSwapChain::MAX_FRAMES_IN_FLIGHT); - for (int i = 0; i < uboBuffers.size(); i++) { - uboBuffers[i] = std::make_unique( - xeDevice, - sizeof(GlobalUbo), - XeSwapChain::MAX_FRAMES_IN_FLIGHT, - VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT, - VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT); - uboBuffers[i]->map(); - } - - auto globalSetLayout = XeDescriptorSetLayout::Builder(xeDevice) - .addBinding(0, VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, VK_SHADER_STAGE_VERTEX_BIT) - .build(); - - std::vector globalDescriptorSets(XeSwapChain::MAX_FRAMES_IN_FLIGHT); - for (int i = 0; i < globalDescriptorSets.size(); i++) { - auto bufferInfo = uboBuffers[i]->descriptorInfo(); - XeDescriptorWriter(*globalSetLayout, *globalPool) - .writeBuffer(0, &bufferInfo) - .build(globalDescriptorSets[i]); - } - - SimpleRenderSystem simpleRenderSystem{xeDevice, xeRenderer.getSwapChainRenderPass(), globalSetLayout->getDescriptorSetLayout()}; + std::unique_ptr renderSystem = xeEngine.createRenderSystem("fw","fd",0,0); XeCamera camera{}; camera.setViewTarget(glm::vec3(-1.f, -2.f, 20.f), glm::vec3(0.f, 0.f, 2.5f)); @@ -64,14 +41,14 @@ void FirstApp::run() { auto currentTime = std::chrono::high_resolution_clock::now(); - while (!xeWindow.shouldClose()) { + while (!xeEngine.getWindow().shouldClose()) { glfwPollEvents(); auto newTime = std::chrono::high_resolution_clock::now(); float frameTime = std::chrono::duration(newTime - currentTime).count(); currentTime = newTime; - cameraController.moveInPlaneXZ(xeWindow.getGLFWwindow(), frameTime, viewerObject); + cameraController.moveInPlaneXZ(xeEngine.getWindow().getGLFWwindow(), frameTime, viewerObject); camera.setViewYXZ(viewerObject.transform.translation, viewerObject.transform.rotation); float aspect = xeRenderer.getAspectRatio(); @@ -80,25 +57,18 @@ void FirstApp::run() { if(auto commandBuffer = xeRenderer.beginFrame()) { int frameIndex = xeRenderer.getFrameIndex(); - XeFrameInfo frameInfo{ - frameIndex, - frameTime, - commandBuffer, - camera, - globalDescriptorSets[frameIndex] - }; // update GlobalUbo ubo{}; ubo.projectionView = camera.getProjection() * camera.getView(); - uboBuffers[frameIndex]->writeToBuffer(&ubo); - uboBuffers[frameIndex]->flush(); + // uboBuffers[frameIndex]->writeToBuffer(&ubo); + // uboBuffers[frameIndex]->flush(); - // render - xeRenderer.beginSwapChainRenderPass(commandBuffer); - simpleRenderSystem.renderGameObjects(frameInfo, gameObjects); - xeRenderer.endSwapChainRenderPass(commandBuffer); - xeRenderer.endFrame(); + // // render + // xeRenderer.beginSwapChainRenderPass(commandBuffer); + // simpleRenderSystem.renderGameObjects(frameInfo, gameObjects); + // xeRenderer.endSwapChainRenderPass(commandBuffer); + // xeRenderer.endFrame(); } }