From 2f99eee7c5f0088e3c39c21a5d2efd7335953ae8 Mon Sep 17 00:00:00 2001 From: Tyler Murphy Date: Mon, 19 Sep 2022 16:35:45 -0400 Subject: [PATCH] even more refactoring --- engine/xe_engine.cpp | 42 +++++++++++-- engine/xe_engine.hpp | 22 +++++++ engine/xe_pipeline.cpp | 2 +- engine/xe_render_system.cpp | 73 ++++++++++++---------- engine/xe_render_system.hpp | 27 ++++++--- engine/xe_renderer.cpp | 1 - res/shaders/simple_shader.frag.spv | Bin 0 -> 1112 bytes res/shaders/simple_shader.vert.spv | Bin 0 -> 2520 bytes src/first_app.cpp | 57 ++++++++++------- src/simple_render_system.cpp | 94 ----------------------------- src/simple_render_system.hpp | 37 ------------ 11 files changed, 154 insertions(+), 201 deletions(-) create mode 100644 res/shaders/simple_shader.frag.spv create mode 100644 res/shaders/simple_shader.vert.spv delete mode 100644 src/simple_render_system.cpp delete mode 100644 src/simple_render_system.hpp diff --git a/engine/xe_engine.cpp b/engine/xe_engine.cpp index ff9c95b..6de221a 100644 --- a/engine/xe_engine.cpp +++ b/engine/xe_engine.cpp @@ -1,15 +1,17 @@ #include "xe_engine.hpp" #include "xe_descriptors.hpp" +#include 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}, + xeCamera{} { loadDescriptors(); }; -XeEngine::~XeEngine() {}; - void XeEngine::loadDescriptors() { xeDescriptorPool = XeDescriptorPool::Builder(xeDevice) .setMaxSets(XeSwapChain::MAX_FRAMES_IN_FLIGHT) @@ -22,12 +24,12 @@ void XeEngine::loadDescriptors() { .build(); } -std::unique_ptr XeEngine::createRenderSystem(const std::string &vert, const std::string &frag, typename pushCunstant, typename uniformBuffer) { +std::unique_ptr XeEngine::createRenderSystem(const std::string &vert, const std::string &frag, uint32_t pushCunstantDataSize, uint32_t uniformBufferDataSize) { return std::make_unique( xeDevice, xeRenderer, - xeDescriptorPool, - xeDescriptorSetLayout, + *xeDescriptorPool, + *xeDescriptorSetLayout, vert, frag, pushCunstantDataSize, @@ -35,4 +37,32 @@ std::unique_ptr XeEngine::createRenderSystem(const std::string & ); } +std::shared_ptr XeEngine::createModel(const std::string &filename) { + return XeModel::createModelFromFile(xeDevice, filename); +} + +void XeEngine::render( + XeRenderSystem& xeRenderSystem, + std::vector& gameObjects, + void* pushConstantData, + uint32_t pushConstantSize, + void* uniformBufferData, + uint32_t uniformBufferSize) { + + auto commandBuffer = xeRenderer.getCurrentCommandBuffer(); + xeRenderer.beginSwapChainRenderPass(commandBuffer); + + xeRenderSystem.renderGameObjects( + xeRenderer.getFrameIndex(), + commandBuffer, + gameObjects, + pushConstantData, + pushConstantSize, + uniformBufferData, + uniformBufferSize + ); + + xeRenderer.endSwapChainRenderPass(commandBuffer); +} + } \ No newline at end of file diff --git a/engine/xe_engine.hpp b/engine/xe_engine.hpp index 7beb962..d9f79ca 100644 --- a/engine/xe_engine.hpp +++ b/engine/xe_engine.hpp @@ -6,6 +6,7 @@ #include "xe_render_system.hpp" #include "xe_descriptors.hpp" +#include namespace xe { class XeEngine { @@ -26,8 +27,28 @@ class XeEngine { return xeRenderer; }; + XeCamera& getCamera() { + return xeCamera; + }; + std::unique_ptr createRenderSystem(const std::string &vert, const std::string &frag, uint32_t pushCunstantDataSize, uint32_t uniformBufferDataSize); + std::shared_ptr createModel(const std::string &filename); + + bool beginFrame() { return xeRenderer.beginFrame(); } + + void endFrame() { return xeRenderer.endFrame(); } + + void render( + XeRenderSystem& xeRenderSystem, + std::vector& gameObjects, + void* pushConstantData, + uint32_t pushConstantSize, + void* uniformBufferData, + uint32_t uniformBufferSize); + + void close() { vkDeviceWaitIdle(xeDevice.device()); } + private: void loadDescriptors(); @@ -35,6 +56,7 @@ class XeEngine { XeWindow xeWindow; XeDevice xeDevice; XeRenderer xeRenderer; + XeCamera xeCamera; std::unique_ptr xeDescriptorPool; std::unique_ptr xeDescriptorSetLayout; diff --git a/engine/xe_pipeline.cpp b/engine/xe_pipeline.cpp index d06d09b..c34d2a9 100755 --- a/engine/xe_pipeline.cpp +++ b/engine/xe_pipeline.cpp @@ -145,7 +145,7 @@ namespace xe { configInfo.rasterizationInfo.rasterizerDiscardEnable = VK_FALSE; configInfo.rasterizationInfo.polygonMode = VK_POLYGON_MODE_FILL; configInfo.rasterizationInfo.lineWidth = 1.0f; - configInfo.rasterizationInfo.cullMode = VK_CULL_MODE_NONE; + configInfo.rasterizationInfo.cullMode = VK_CULL_MODE_BACK_BIT; configInfo.rasterizationInfo.frontFace = VK_FRONT_FACE_CLOCKWISE; configInfo.rasterizationInfo.depthBiasEnable = VK_FALSE; configInfo.rasterizationInfo.depthBiasConstantFactor = 0.0f; diff --git a/engine/xe_render_system.cpp b/engine/xe_render_system.cpp index ec56972..17e62dc 100644 --- a/engine/xe_render_system.cpp +++ b/engine/xe_render_system.cpp @@ -12,14 +12,15 @@ #include #include +#include + namespace xe { -template XeRenderSystem::XeRenderSystem( XeDevice &device, XeRenderer &renderer, - std::unique_ptr &xeDescriptorPool, - std::unique_ptr &xeDescriptorSetLayout, + XeDescriptorPool &xeDescriptorPool, + XeDescriptorSetLayout &xeDescriptorSetLayout, std::string vert, std::string frag, uint32_t pushCunstantDataSize, @@ -31,12 +32,9 @@ XeRenderSystem::XeRenderSystem( } -template XeRenderSystem::~XeRenderSystem() {}; - -template -void XeRenderSystem::createUniformBuffers(std::unique_ptr &xeDescriptorPool, std::unique_ptr &xeDescriptorSetLayout, uint32_t uniformBufferDataSize) { +void XeRenderSystem::createUniformBuffers(XeDescriptorPool &xeDescriptorPool, XeDescriptorSetLayout &xeDescriptorSetLayout, uint32_t uniformBufferDataSize) { if(uniformBufferDataSize == 0) return; uboBuffers = std::vector>(XeSwapChain::MAX_FRAMES_IN_FLIGHT); @@ -53,24 +51,24 @@ void XeRenderSystem::createUniformBuffers(std::unique_ptr &xeD descriptorSets = std::vector(XeSwapChain::MAX_FRAMES_IN_FLIGHT); for (int i = 0; i < descriptorSets.size(); i++) { auto bufferInfo = uboBuffers[i]->descriptorInfo(); - XeDescriptorWriter(*xeDescriptorSetLayout, *xeDescriptorPool) + XeDescriptorWriter(xeDescriptorSetLayout, xeDescriptorPool) .writeBuffer(0, &bufferInfo) .build(descriptorSets[i]); } } -template -void XeRenderSystem::createPipelineLayout(std::unique_ptr &xeDescriptorSetLayout, uint32_t pushCunstantDataSize, uint32_t uniformBufferDataSize) { +void XeRenderSystem::createPipelineLayout(XeDescriptorSetLayout &xeDescriptorSetLayout, uint32_t pushCunstantDataSize, uint32_t uniformBufferDataSize) { + + VkPushConstantRange pushConstantRange; + pushConstantRange.stageFlags = VK_SHADER_STAGE_VERTEX_BIT | VK_SHADER_STAGE_FRAGMENT_BIT; + pushConstantRange.offset = 0; + pushConstantRange.size = pushCunstantDataSize; 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 { @@ -79,7 +77,7 @@ void XeRenderSystem::createPipelineLayout(std::unique_ptr } if (uniformBufferDataSize > 0) { - std::vector descriptorSetLayouts{xeDescriptorSetLayout->getDescriptorSetLayout()}; + std::vector descriptorSetLayouts{xeDescriptorSetLayout.getDescriptorSetLayout()}; pipelineLayoutInfo.setLayoutCount = static_cast(descriptorSetLayouts.size()); pipelineLayoutInfo.pSetLayouts = descriptorSetLayouts.data(); } else { @@ -90,10 +88,10 @@ void XeRenderSystem::createPipelineLayout(std::unique_ptr 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"); @@ -109,35 +107,46 @@ void XeRenderSystem::createPipeline(VkRenderPass renderPass, std::string vert, s ); } +void XeRenderSystem::renderGameObjects( + int frameIndex, + VkCommandBuffer commandBuffer, + std::vector &gameObjects, + void *pushConstantData, + uint32_t pushConstantSize, + void* uniformBufferData, + uint32_t uniformBufferSize) { + + uboBuffers[frameIndex]->writeToBuffer(uniformBufferData); + uboBuffers[frameIndex]->flush(); + + xePipeline->bind(commandBuffer); -template -void XeRenderSystem::renderGameObjects(XeFrameInfo &frameInfo, std::vector &gameObjects, XeRenderSystem::XeData pushConstantData) { - xePipeline->bind(frameInfo.commandBuffer); - - if(uboBuffers.size() > 0) { + if(pushConstantData == NULL) { vkCmdBindDescriptorSets( - frameInfo.commandBuffer, + commandBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, pipelineLayout, 0, 1, - &descriptorSets[frameInfo.frameIndex], + &descriptorSets[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); + if(pushConstantData == NULL) { + vkCmdPushConstants( + commandBuffer, + pipelineLayout, + VK_SHADER_STAGE_VERTEX_BIT | VK_SHADER_STAGE_FRAGMENT_BIT, + 0, + pushConstantSize, + &pushConstantData); + } - obj.model->bind(frameInfo.commandBuffer); - obj.model->draw(frameInfo.commandBuffer); + obj.model->bind(commandBuffer); + obj.model->draw(commandBuffer); } } diff --git a/engine/xe_render_system.hpp b/engine/xe_render_system.hpp index 42079df..608e03a 100644 --- a/engine/xe_render_system.hpp +++ b/engine/xe_render_system.hpp @@ -5,12 +5,13 @@ #include "xe_pipeline.hpp" #include "xe_frame_info.hpp" #include "xe_game_object.hpp" +#include "xe_descriptors.hpp" +#include "xe_renderer.hpp" #include namespace xe { -template class XeRenderSystem { public: @@ -19,23 +20,33 @@ class XeRenderSystem { XeRenderSystem( XeDevice &device, XeRenderer &renderer, - std::unique_ptr &xeDescriptorPool, - std::unique_ptr &xeDescriptorSetLayout, + XeDescriptorPool &xeDescriptorPool, + XeDescriptorSetLayout &xeDescriptorSetLayout, std::string vert, std::string frag, - uint32_t pushCunstantDataSize, - uint32_t uniformBufferDataSize); + 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); + void setUnifroms(XeFrameInfo & frameInfo); + + void renderGameObjects( + int frameIndex, + VkCommandBuffer commandBuffer, + std::vector &gameObjects, + void *pushConstantData, + uint32_t pushConstantSize, + void* uniformBufferData, + uint32_t uniformBufferSize); 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 createUniformBuffers(XeDescriptorPool &xeDescriptorPool, XeDescriptorSetLayout &xeDescriptorSetLayout, uint32_t uniformBufferDataSize); + void createPipelineLayout(XeDescriptorSetLayout &xeDescriptorSetLayout, uint32_t pushCunstantDataSize, uint32_t uniformBufferDataSize); void createPipeline(VkRenderPass renderPass, std::string vert, std::string frag); XeDevice& xeDevice; diff --git a/engine/xe_renderer.cpp b/engine/xe_renderer.cpp index 0c14cec..1714e46 100644 --- a/engine/xe_renderer.cpp +++ b/engine/xe_renderer.cpp @@ -17,7 +17,6 @@ namespace xe { XeRenderer::XeRenderer(XeWindow& window, XeDevice& device) : xeWindow{window}, xeDevice{device} { recreateSwapChain(); createCommandBuffers(); - loadDescriptorPool(); } XeRenderer::~XeRenderer() { freeCommandBuffers(); } diff --git a/res/shaders/simple_shader.frag.spv b/res/shaders/simple_shader.frag.spv new file mode 100644 index 0000000000000000000000000000000000000000..ff80cc6a1d171521e711f7b80cfd961f390701ef GIT binary patch literal 1112 zcmYk4%}?7v5XGki8w%yS1=<6GazsKsK!t>$NT@OgS5WoiYMI19=+seGv%h*YscKQ2MCa#G{5k3N`^lhwD6*u;%Pi^T zRo1QZ-x<8=7B4Tm#jw{V1Y)%71{REJyJ3Cm3RkI|kYJ?cs!Fdt2=-ccQQV@a zIn+xqR=;w;(BR6Sd3N_Dt*iVVJd_?d^xepG<#!k7*EcoyfqGn(A1Wi-)^xrY4sN&$ z^&@80Ray?NTy?8+sOf-{vlNsT&-KXhR(QY35h-T28!{F#bN2C>V&6j`{H*B{?lzlN z8{>ajY+-1CcOr|_3%U5us6$`xWh@_WjYH)e_e$vN`d-_0J@#C8{EoWqcWoW$_4D!X z&<2LK747&fVSJUGS9J>IH)U(m#yeuZl}+u_@^HZqo0z$R!Jm2Iw9G~X0`NFdjp+m z$2;hqcoW`AzjUV0FUd=zXXoL(@ALjY=Y)a&nIY%;+=x5wwq3DKyM8gw9dm>E?$>wg zbKR^xH-Gyk9w*#D?ugGxd9jWN`^_*$J}!8|_ku5M2|o%0`t@snufQX!=+_(m_O@T& zXtX+=MmIZ%f<_d^L8Bd}K`RU21o#bC{KB{u_1aa3nmnqtYwDSf_M*m4kUkI6EcivL z*n&sKmUP1`Ok%M^xj(byd>CiD;jgMpOktyE+18@4vmSP{X50!6ZJY$QUKAbLYMPTX zVAb?Hquq~^m(A$e%S7^^JDPLYJ8AMWvnKKLFnIf^W(hj0BE%U&WGfvX|rWvG@<1_7@G6#Ko=yp)K&ybtd zUc|mg(x|=LX}0pSBA)v$Y~&ks%hDqn-iosz)`1@s=fRtJl{WX*k|;?f6U!TE9qI%5 zd+EJ#Zd2Cch4CJXWgK(Bu^Y!-!ErCfA;a-bd|8n3+Z4myuxbMSrqAT;$DBSCcdFt? zUHn!A@)Ap4UwOHkX@O<>RH5c|?S8DT>D9W%{a?4ZXX;kts(mnn-Lb{d8=Up#NuCr~ z^v1oRo1Ocq+PNEa>fq0}jLx~9RTObk($JB|B@Jr#g!A&J4|L9dOn-;9dje~=)Q?Ig z|3&E|+VO{*l*D~m4s5`a->7zKVqYqCzB@SToY3wG?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 literal 0 HcmV?d00001 diff --git a/src/first_app.cpp b/src/first_app.cpp index 1813d75..e460c6b 100755 --- a/src/first_app.cpp +++ b/src/first_app.cpp @@ -3,7 +3,6 @@ #include "xe_camera.hpp" #include "xe_game_object.hpp" #include "xe_model.hpp" -#include "simple_render_system.hpp" #include "keyboard_movement_controller.hpp" @@ -16,12 +15,17 @@ #include #include #include - +#include namespace xe { -struct GlobalUbo { - glm::mat4 projectionView{1.f}; - glm::vec3 lightDirection = glm::normalize(glm::vec3{-1.f, 3.f, 1.f}); +struct UniformBuffer { + alignas(16) glm::mat4 projectionView{1.f}; + alignas(4) glm::vec3 lightDirection = glm::normalize(glm::vec3{-1.f, 3.f, 1.f}); +}; + +struct PushConstant { + alignas(16) glm::mat4 modelMatrix{1.f}; + alignas(16) glm::mat4 normalMatrix{1.f}; }; FirstApp::FirstApp() : xeEngine{WIDTH, HEIGHT, "Hello, Vulkan!"} { @@ -32,7 +36,12 @@ FirstApp::~FirstApp() {} void FirstApp::run() { - std::unique_ptr renderSystem = xeEngine.createRenderSystem("fw","fd",0,0); + std::unique_ptr simpleRenderSystem = xeEngine.createRenderSystem( + "res/shaders/simple_shader.vert.spv", + "res/shaders/simple_shader.frag.spv", + sizeof(PushConstant), + sizeof(UniformBuffer)); + XeCamera camera{}; camera.setViewTarget(glm::vec3(-1.f, -2.f, 20.f), glm::vec3(0.f, 0.f, 2.5f)); @@ -51,34 +60,38 @@ void FirstApp::run() { cameraController.moveInPlaneXZ(xeEngine.getWindow().getGLFWwindow(), frameTime, viewerObject); camera.setViewYXZ(viewerObject.transform.translation, viewerObject.transform.rotation); - float aspect = xeRenderer.getAspectRatio(); + float aspect = xeEngine.getRenderer().getAspectRatio(); camera.setPerspectiveProjection(glm::radians(50.f), aspect, 0.1f, 100.f); - if(auto commandBuffer = xeRenderer.beginFrame()) { + if(xeEngine.beginFrame()) { - int frameIndex = xeRenderer.getFrameIndex(); - - // update - GlobalUbo ubo{}; + PushConstant pc{}; + pc.modelMatrix = gameObjects[0].transform.mat4(); + pc.normalMatrix = gameObjects[0].transform.normalMatrix(); + + UniformBuffer ubo{}; ubo.projectionView = camera.getProjection() * camera.getView(); - // uboBuffers[frameIndex]->writeToBuffer(&ubo); - // uboBuffers[frameIndex]->flush(); + + xeEngine.render( + *simpleRenderSystem, + gameObjects, + &pc, + sizeof(pc), + &ubo, + sizeof(ubo) + ); + + xeEngine.endFrame(); - // // render - // xeRenderer.beginSwapChainRenderPass(commandBuffer); - // simpleRenderSystem.renderGameObjects(frameInfo, gameObjects); - // xeRenderer.endSwapChainRenderPass(commandBuffer); - // xeRenderer.endFrame(); } - } - vkDeviceWaitIdle(xeDevice.device()); + xeEngine.close(); } void FirstApp::loadGameObjects() { - std::shared_ptr xeModel = XeModel::createModelFromFile(xeDevice, "res/models/stanford-dragon.obj"); + std::shared_ptr xeModel = xeEngine.createModel("res/models/stanford-dragon.obj"); auto cube = XeGameObject::createGameObject(); cube.model = xeModel; diff --git a/src/simple_render_system.cpp b/src/simple_render_system.cpp deleted file mode 100644 index 6e4ab30..0000000 --- a/src/simple_render_system.cpp +++ /dev/null @@ -1,94 +0,0 @@ -#include "simple_render_system.hpp" -#include "xe_device.hpp" -#include -#include - -#define GLM_FORCE_RADIANS -#define GLM_FORCE_DEPTH_ZERO_TO_ONE -#include -#include - -#include -#include -#include - -namespace xe { - -struct SimplePushConstantData { - glm::mat4 modelMatrix{1.f}; - glm::mat4 normalMatrix{1.f}; -}; - -SimpleRenderSystem::SimpleRenderSystem(XeDevice& device, VkRenderPass renderPass, VkDescriptorSetLayout globalSetLayout) : xeDevice{device} { - createPipelineLayout(globalSetLayout); - createPipeline(renderPass); -} - -SimpleRenderSystem::~SimpleRenderSystem() { vkDestroyPipelineLayout(xeDevice.device(), pipelineLayout, nullptr); } - -void SimpleRenderSystem::createPipelineLayout(VkDescriptorSetLayout globalSetLayout) { - - VkPushConstantRange pushConstantRange; - pushConstantRange.stageFlags = VK_SHADER_STAGE_VERTEX_BIT | VK_SHADER_STAGE_FRAGMENT_BIT; - pushConstantRange.offset = 0; - pushConstantRange.size = sizeof(SimplePushConstantData); - - std::vector descriptorSetLayouts{globalSetLayout}; - - VkPipelineLayoutCreateInfo pipelineLayoutInfo{}; - pipelineLayoutInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO; - pipelineLayoutInfo.setLayoutCount = static_cast(descriptorSetLayouts.size()); - pipelineLayoutInfo.pSetLayouts = descriptorSetLayouts.data(); - pipelineLayoutInfo.pushConstantRangeCount = 1; - pipelineLayoutInfo.pPushConstantRanges = &pushConstantRange; - if(vkCreatePipelineLayout(xeDevice.device(), &pipelineLayoutInfo, nullptr, &pipelineLayout) != VK_SUCCESS) { - std::runtime_error("failed to create pipeline layout!"); - } -} - -void SimpleRenderSystem::createPipeline(VkRenderPass renderPass) { - assert(pipelineLayout != nullptr && "Canor create pipeline before pipeline layout"); - - PipelineConfigInfo pipelineConfig{}; - XePipeline::defaultPipelineConfigInfo(pipelineConfig); - pipelineConfig.renderPass = renderPass; - pipelineConfig.pipelineLayout = pipelineLayout; - xePipeline = std::make_unique( - xeDevice, - "res/shaders/simple_shader.vert.spv", - "res/shaders/simple_shader.frag.spv", - pipelineConfig - ); -} - -void SimpleRenderSystem::renderGameObjects(XeFrameInfo &frameInfo, std::vector &gameObjects) { - xePipeline->bind(frameInfo.commandBuffer); - - vkCmdBindDescriptorSets( - frameInfo.commandBuffer, - VK_PIPELINE_BIND_POINT_GRAPHICS, - pipelineLayout, - 0, - 1, - &frameInfo.globalDescriptorSet, - 0, - nullptr); - - for (auto& obj: gameObjects) { - SimplePushConstantData push{}; - push.modelMatrix = obj.transform.mat4(); - push.normalMatrix = obj.transform.normalMatrix(); - - vkCmdPushConstants( - frameInfo.commandBuffer, - pipelineLayout, - VK_SHADER_STAGE_VERTEX_BIT | VK_SHADER_STAGE_FRAGMENT_BIT, - 0, - sizeof(SimplePushConstantData), - &push); - obj.model->bind(frameInfo.commandBuffer); - obj.model->draw(frameInfo.commandBuffer); - } -} - -} \ No newline at end of file diff --git a/src/simple_render_system.hpp b/src/simple_render_system.hpp deleted file mode 100644 index b0d4a39..0000000 --- a/src/simple_render_system.hpp +++ /dev/null @@ -1,37 +0,0 @@ -#pragma once - -#include "xe_camera.hpp" -#include "xe_pipeline.hpp" -#include "xe_device.hpp" -#include "xe_game_object.hpp" -#include "xe_frame_info.hpp" - -#include -#include -#include - -namespace xe { -class SimpleRenderSystem { - public: - - SimpleRenderSystem(XeDevice& device, VkRenderPass renderPass, VkDescriptorSetLayout globalSetLayout); - ~SimpleRenderSystem(); - - SimpleRenderSystem(const SimpleRenderSystem &) = delete; - SimpleRenderSystem operator=(const SimpleRenderSystem &) = delete; - - void renderGameObjects( - XeFrameInfo &frameInfo, - std::vector &gameObjects - ); - - private: - void createPipelineLayout(VkDescriptorSetLayout globalSetLayout); - void createPipeline(VkRenderPass renderPass); - - XeDevice& xeDevice; - - std::unique_ptr xePipeline; - VkPipelineLayout pipelineLayout; -}; -} \ No newline at end of file