more recatoring, doesnt compile

This commit is contained in:
Tyler Murphy 2022-09-19 12:54:23 -04:00
parent 8754e31367
commit 430a008ab3
8 changed files with 237 additions and 48 deletions

View file

@ -4,14 +4,35 @@
namespace xe { namespace xe {
XeEngine::XeEngine(int width, int height, std::string name) 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) xeDescriptorPool = XeDescriptorPool::Builder(xeDevice)
.setMaxSets(XeSwapChain::MAX_FRAMES_IN_FLIGHT) .setMaxSets(XeSwapChain::MAX_FRAMES_IN_FLIGHT)
.addPoolSize(VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, 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) .addPoolSize(VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, XeSwapChain::MAX_FRAMES_IN_FLIGHT)
.build(); .build();
xeDescriptorSetLayout = XeDescriptorSetLayout::Builder(xeDevice)
.addBinding(0, VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, VK_SHADER_STAGE_VERTEX_BIT)
.build();
}
std::unique_ptr<XeRenderSystem> XeEngine::createRenderSystem(const std::string &vert, const std::string &frag, typename pushCunstant, typename uniformBuffer) {
return std::make_unique<XeRenderSystem>(
xeDevice,
xeRenderer,
xeDescriptorPool,
xeDescriptorSetLayout,
vert,
frag,
pushCunstantDataSize,
uniformBufferDataSize
);
} }
} }

View file

@ -3,6 +3,8 @@
#include "xe_window.hpp" #include "xe_window.hpp"
#include "xe_device.hpp" #include "xe_device.hpp"
#include "xe_renderer.hpp" #include "xe_renderer.hpp"
#include "xe_render_system.hpp"
#include "xe_descriptors.hpp"
namespace xe { namespace xe {
@ -16,25 +18,26 @@ class XeEngine {
XeEngine(const XeEngine&) = delete; XeEngine(const XeEngine&) = delete;
XeEngine operator=(const XeEngine&) = delete; XeEngine operator=(const XeEngine&) = delete;
const XeWindow& getWindow() const { XeWindow& getWindow() {
return xeWindow; return xeWindow;
}; };
const XeRenderer& getRenderer() const { XeRenderer& getRenderer() {
return xeRenderer; return xeRenderer;
}; };
std::unique_ptr<XeRenderSystem> createRenderSystem(const std::string &vert, const std::string &frag, uint32_t pushCunstantDataSize, uint32_t uniformBufferDataSize);
private: private:
void loadDescriptorPool(); void loadDescriptors();
XeWindow xeWindow; XeWindow xeWindow;
XeDevice xeDevice; XeDevice xeDevice;
XeRenderer xeRenderer; XeRenderer xeRenderer;
std::unique_ptr<XeDescriptorPool> xeDescriptorPool{}; std::unique_ptr<XeDescriptorPool> xeDescriptorPool;
std::unique_ptr<xe::XeDescriptorSetLayout> xeDescriptorSetLayout; std::unique_ptr<XeDescriptorSetLayout> xeDescriptorSetLayout;
}; };
} }

View file

@ -11,7 +11,6 @@ struct XeFrameInfo {
float frameTime; float frameTime;
VkCommandBuffer commandBuffer; VkCommandBuffer commandBuffer;
XeCamera &camera; XeCamera &camera;
VkDescriptorSet globalDescriptorSet;
}; };
} }

145
engine/xe_render_system.cpp Normal file
View file

@ -0,0 +1,145 @@
#include "xe_render_system.hpp"
#include <vulkan/vulkan.h>
#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 <memory>
#include <stdexcept>
namespace xe {
template <typename P, typename U>
XeRenderSystem::XeRenderSystem(
XeDevice &device,
XeRenderer &renderer,
std::unique_ptr<XeDescriptorPool> &xeDescriptorPool,
std::unique_ptr<XeDescriptorSetLayout> &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 <typename P, typename U>
XeRenderSystem::~XeRenderSystem() {};
template <typename P, typename U>
void XeRenderSystem::createUniformBuffers(std::unique_ptr<XeDescriptorPool> &xeDescriptorPool, std::unique_ptr<XeDescriptorSetLayout> &xeDescriptorSetLayout, uint32_t uniformBufferDataSize) {
if(uniformBufferDataSize == 0) return;
uboBuffers = std::vector<std::unique_ptr<XeBuffer>>(XeSwapChain::MAX_FRAMES_IN_FLIGHT);
for (int i = 0; i < uboBuffers.size(); i++) {
uboBuffers[i] = std::make_unique<XeBuffer>(
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<VkDescriptorSet>(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 <typename P, typename U>
void XeRenderSystem::createPipelineLayout(std::unique_ptr<XeDescriptorSetLayout> &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<VkDescriptorSetLayout> descriptorSetLayouts{xeDescriptorSetLayout->getDescriptorSetLayout()};
pipelineLayoutInfo.setLayoutCount = static_cast<uint32_t>(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 <typename P, typename U>
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<XePipeline>(
xeDevice,
vert,
frag,
pipelineConfig
);
}
template <typename P, typename U>
void XeRenderSystem::renderGameObjects(XeFrameInfo &frameInfo, std::vector<XeGameObject> &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);
}
}
}

View file

@ -0,0 +1,51 @@
#pragma once
#include <vulkan/vulkan.h>
#include "xe_device.hpp"
#include "xe_pipeline.hpp"
#include "xe_frame_info.hpp"
#include "xe_game_object.hpp"
#include <memory>
namespace xe {
template<typename P, typename U>
class XeRenderSystem {
public:
struct XeData{};
XeRenderSystem(
XeDevice &device,
XeRenderer &renderer,
std::unique_ptr<XeDescriptorPool> &xeDescriptorPool,
std::unique_ptr<XeDescriptorSetLayout> &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<XeGameObject> &gameObjects, XeRenderSystem::XeData pushConstantData);
private:
void createUniformBuffers(std::unique_ptr<XeDescriptorPool> &xeDescriptorPool, std::unique_ptr<XeDescriptorSetLayout> &xeDescriptorSetLayout, uint32_t uniformBufferDataSize);
void createPipelineLayout(std::unique_ptr<XeDescriptorSetLayout> &xeDescriptorSetLayout, uint32_t pushCunstantDataSize, uint32_t uniformBufferDataSize);
void createPipeline(VkRenderPass renderPass, std::string vert, std::string frag);
XeDevice& xeDevice;
std::unique_ptr<XePipeline> xePipeline;
std::vector<std::unique_ptr<XeBuffer>> uboBuffers;
std::vector<VkDescriptorSet> descriptorSets;
VkPipelineLayout pipelineLayout;
};
}

Binary file not shown.

Binary file not shown.

View file

@ -32,30 +32,7 @@ FirstApp::~FirstApp() {}
void FirstApp::run() { void FirstApp::run() {
std::vector<std::unique_ptr<XeBuffer>> uboBuffers(XeSwapChain::MAX_FRAMES_IN_FLIGHT); std::unique_ptr<XeRenderSystem> renderSystem = xeEngine.createRenderSystem("fw","fd",0,0);
for (int i = 0; i < uboBuffers.size(); i++) {
uboBuffers[i] = std::make_unique<XeBuffer>(
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<VkDescriptorSet> 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()};
XeCamera camera{}; XeCamera camera{};
camera.setViewTarget(glm::vec3(-1.f, -2.f, 20.f), glm::vec3(0.f, 0.f, 2.5f)); 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(); auto currentTime = std::chrono::high_resolution_clock::now();
while (!xeWindow.shouldClose()) { while (!xeEngine.getWindow().shouldClose()) {
glfwPollEvents(); glfwPollEvents();
auto newTime = std::chrono::high_resolution_clock::now(); auto newTime = std::chrono::high_resolution_clock::now();
float frameTime = std::chrono::duration<float, std::chrono::seconds::period>(newTime - currentTime).count(); float frameTime = std::chrono::duration<float, std::chrono::seconds::period>(newTime - currentTime).count();
currentTime = newTime; currentTime = newTime;
cameraController.moveInPlaneXZ(xeWindow.getGLFWwindow(), frameTime, viewerObject); cameraController.moveInPlaneXZ(xeEngine.getWindow().getGLFWwindow(), frameTime, viewerObject);
camera.setViewYXZ(viewerObject.transform.translation, viewerObject.transform.rotation); camera.setViewYXZ(viewerObject.transform.translation, viewerObject.transform.rotation);
float aspect = xeRenderer.getAspectRatio(); float aspect = xeRenderer.getAspectRatio();
@ -80,25 +57,18 @@ void FirstApp::run() {
if(auto commandBuffer = xeRenderer.beginFrame()) { if(auto commandBuffer = xeRenderer.beginFrame()) {
int frameIndex = xeRenderer.getFrameIndex(); int frameIndex = xeRenderer.getFrameIndex();
XeFrameInfo frameInfo{
frameIndex,
frameTime,
commandBuffer,
camera,
globalDescriptorSets[frameIndex]
};
// update // update
GlobalUbo ubo{}; GlobalUbo ubo{};
ubo.projectionView = camera.getProjection() * camera.getView(); ubo.projectionView = camera.getProjection() * camera.getView();
uboBuffers[frameIndex]->writeToBuffer(&ubo); // uboBuffers[frameIndex]->writeToBuffer(&ubo);
uboBuffers[frameIndex]->flush(); // uboBuffers[frameIndex]->flush();
// render // // render
xeRenderer.beginSwapChainRenderPass(commandBuffer); // xeRenderer.beginSwapChainRenderPass(commandBuffer);
simpleRenderSystem.renderGameObjects(frameInfo, gameObjects); // simpleRenderSystem.renderGameObjects(frameInfo, gameObjects);
xeRenderer.endSwapChainRenderPass(commandBuffer); // xeRenderer.endSwapChainRenderPass(commandBuffer);
xeRenderer.endFrame(); // xeRenderer.endFrame();
} }
} }