From 87417f483c12c9d479b9cb50613640e2338a4dcc Mon Sep 17 00:00:00 2001 From: Dynamitos Date: Wed, 24 Jan 2024 23:10:33 +0100 Subject: [PATCH] Runs on MacOS, tho window size is terrible --- src/Editor/main.cpp | 2 +- src/Engine/Actor/CMakeLists.txt | 7 ++- src/Engine/Actor/StaticMeshActor.cpp | 23 +++++++++ src/Engine/Actor/StaticMeshActor.h | 16 ++++++ src/Engine/Component/Mesh.h | 3 -- src/Engine/Graphics/Initializer.h | 2 +- src/Engine/Graphics/RenderPass/BasePass.cpp | 10 +--- src/Engine/Graphics/RenderPass/BasePass.h | 1 - .../Graphics/RenderPass/DepthPrepass.cpp | 7 --- src/Engine/Graphics/RenderPass/DepthPrepass.h | 2 - .../Graphics/RenderPass/LightCullingPass.cpp | 2 +- .../Graphics/RenderPass/SkyboxRenderPass.cpp | 2 - src/Engine/Graphics/RenderTarget.h | 50 ++++++++++++++++--- src/Engine/Graphics/VertexData.cpp | 1 + src/Engine/Graphics/Vulkan/PipelineCache.cpp | 2 +- src/Engine/Graphics/Vulkan/Resources.cpp | 2 +- src/Engine/Graphics/Vulkan/Window.cpp | 20 ++++++-- src/Engine/Graphics/Vulkan/Window.h | 1 + src/Engine/MinimalEngine.h | 1 - src/Engine/Scene/Scene.h | 7 ++- src/Engine/System/MeshUpdater.cpp | 1 + 21 files changed, 119 insertions(+), 43 deletions(-) create mode 100644 src/Engine/Actor/StaticMeshActor.cpp create mode 100644 src/Engine/Actor/StaticMeshActor.h diff --git a/src/Editor/main.cpp b/src/Editor/main.cpp index 0bd7a0f..b1f0ac8 100644 --- a/src/Editor/main.cpp +++ b/src/Editor/main.cpp @@ -233,7 +233,7 @@ int main() sceneViewInfo.dimensions.size.y = 1080; sceneViewInfo.dimensions.offset.x = 0; sceneViewInfo.dimensions.offset.y = 0; - sceneViewInfo.numSamples = Gfx::SE_SAMPLE_COUNT_4_BIT; + sceneViewInfo.numSamples = Gfx::SE_SAMPLE_COUNT_1_BIT; OGameView sceneView = new Editor::PlayView(graphics, window, sceneViewInfo, binaryPath.generic_string()); //ViewportCreateInfo inspectorViewInfo; diff --git a/src/Engine/Actor/CMakeLists.txt b/src/Engine/Actor/CMakeLists.txt index 31f4ac0..f610757 100644 --- a/src/Engine/Actor/CMakeLists.txt +++ b/src/Engine/Actor/CMakeLists.txt @@ -9,7 +9,9 @@ target_sources(Engine Entity.cpp Entity.h PointLightActor.cpp - PointLightActor.h) + PointLightActor.h + StaticMeshActor.h + StaticMeshActor.cpp) target_sources(Engine PUBLIC FILE_SET HEADERS @@ -18,4 +20,5 @@ target_sources(Engine CameraActor.h DirectionalLightActor.h Entity.h - PointLightActor.h) + PointLightActor.h + StaticMeshActor.h) diff --git a/src/Engine/Actor/StaticMeshActor.cpp b/src/Engine/Actor/StaticMeshActor.cpp new file mode 100644 index 0000000..46e0203 --- /dev/null +++ b/src/Engine/Actor/StaticMeshActor.cpp @@ -0,0 +1,23 @@ +#include "StaticMeshActor.h" + +using namespace Seele; + +StaticMeshActor::StaticMeshActor(PScene scene, PMeshAsset mesh) + : Actor(scene) +{ + attachComponent(mesh); +} + +Seele::StaticMeshActor::~StaticMeshActor() +{ +} + +Component::Mesh &Seele::StaticMeshActor::getMesh() +{ + return accessComponent(); +} + +const Component::Mesh &Seele::StaticMeshActor::getMesh() const +{ + return accessComponent(); +} diff --git a/src/Engine/Actor/StaticMeshActor.h b/src/Engine/Actor/StaticMeshActor.h new file mode 100644 index 0000000..66853f6 --- /dev/null +++ b/src/Engine/Actor/StaticMeshActor.h @@ -0,0 +1,16 @@ +#pragma once +#include "Actor.h" +#include "Component/Mesh.h" + +namespace Seele +{ +class StaticMeshActor : public Actor +{ +public: + StaticMeshActor(PScene scene, PMeshAsset mesh); + virtual ~StaticMeshActor(); + Component::Mesh& getMesh(); + const Component::Mesh& getMesh() const; +private: +}; +} // namespace Seele diff --git a/src/Engine/Component/Mesh.h b/src/Engine/Component/Mesh.h index 7ea0f0d..6dd63fa 100644 --- a/src/Engine/Component/Mesh.h +++ b/src/Engine/Component/Mesh.h @@ -1,8 +1,5 @@ #pragma once #include "Asset/MeshAsset.h" -#include "Graphics/VertexData.h" -#include "Material/MaterialInstance.h" -#include "Graphics/Mesh.h" namespace Seele { diff --git a/src/Engine/Graphics/Initializer.h b/src/Engine/Graphics/Initializer.h index 5e6f09e..c6bd454 100644 --- a/src/Engine/Graphics/Initializer.h +++ b/src/Engine/Graphics/Initializer.h @@ -155,7 +155,7 @@ struct RasterizationState float depthBiasConstantFactor = 0; float depthBiasClamp = 0; float depthBiasSlopeFactor = 0; - float lineWidth = 0; + float lineWidth = 1.0; }; struct MultisampleState { diff --git a/src/Engine/Graphics/RenderPass/BasePass.cpp b/src/Engine/Graphics/RenderPass/BasePass.cpp index 9aaaa5e..d633e9d 100644 --- a/src/Engine/Graphics/RenderPass/BasePass.cpp +++ b/src/Engine/Graphics/RenderPass/BasePass.cpp @@ -71,7 +71,6 @@ void BasePass::render() tLightGrid->pipelineBarrier( Gfx::SE_ACCESS_SHADER_WRITE_BIT, Gfx::SE_PIPELINE_STAGE_COMPUTE_SHADER_BIT, Gfx::SE_ACCESS_SHADER_READ_BIT, Gfx::SE_PIPELINE_STAGE_FRAGMENT_SHADER_BIT); - colorBuffer->changeLayout(Gfx::SE_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL); descriptorSets[INDEX_VIEW_PARAMS] = viewParamsSet; descriptorSets[INDEX_LIGHT_ENV] = scene->getLightEnvironment()->getDescriptorSet(); @@ -186,14 +185,7 @@ void BasePass::endFrame() void BasePass::publishOutputs() { - colorBuffer = graphics->createTexture2D(TextureCreateInfo{ - .format = viewport->getOwner()->getBackBuffer()->getFormat(), - .width = viewport->getWidth(), - .height = viewport->getHeight(), - .samples = viewport->getSamples(), - .usage = Gfx::SE_IMAGE_USAGE_COLOR_ATTACHMENT_BIT, - }); - colorAttachment = new Gfx::RenderTargetAttachment(colorBuffer, Gfx::SE_ATTACHMENT_LOAD_OP_CLEAR, Gfx::SE_ATTACHMENT_STORE_OP_STORE); + colorAttachment = new Gfx::SwapchainAttachment(viewport, Gfx::SE_ATTACHMENT_LOAD_OP_CLEAR, Gfx::SE_ATTACHMENT_STORE_OP_STORE); resources->registerRenderPassOutput("BASEPASS_COLOR", colorAttachment); } diff --git a/src/Engine/Graphics/RenderPass/BasePass.h b/src/Engine/Graphics/RenderPass/BasePass.h index 6c3d93c..9d8b589 100644 --- a/src/Engine/Graphics/RenderPass/BasePass.h +++ b/src/Engine/Graphics/RenderPass/BasePass.h @@ -20,7 +20,6 @@ public: static void modifyRenderPassMacros(Map& defines); private: Gfx::ORenderTargetAttachment colorAttachment; - Gfx::OTexture2D colorBuffer; Gfx::PTexture2D depthBuffer; Gfx::PShaderBuffer oLightIndexList; Gfx::PShaderBuffer tLightIndexList; diff --git a/src/Engine/Graphics/RenderPass/DepthPrepass.cpp b/src/Engine/Graphics/RenderPass/DepthPrepass.cpp index fd59f6d..0027771 100644 --- a/src/Engine/Graphics/RenderPass/DepthPrepass.cpp +++ b/src/Engine/Graphics/RenderPass/DepthPrepass.cpp @@ -148,26 +148,19 @@ void DepthPrepass::publishOutputs() .format = Gfx::SE_FORMAT_D32_SFLOAT, .width = viewport->getOwner()->getFramebufferWidth(), .height = viewport->getOwner()->getFramebufferHeight(), - .samples = viewport->getSamples(), .usage = Gfx::SE_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT, }; depthBuffer = graphics->createTexture2D(depthBufferInfo); depthAttachment = new Gfx::RenderTargetAttachment(depthBuffer, Gfx::SE_ATTACHMENT_LOAD_OP_CLEAR, Gfx::SE_ATTACHMENT_STORE_OP_STORE); depthAttachment->clear.depthStencil.depth = 1.0f; - depthBufferInfo.samples = Gfx::SE_SAMPLE_COUNT_1_BIT; - depthResolveBuffer = graphics->createTexture2D(depthBufferInfo); - depthResolveAttachment = - new Gfx::RenderTargetAttachment(depthResolveBuffer, Gfx::SE_ATTACHMENT_LOAD_OP_DONT_CARE, Gfx::SE_ATTACHMENT_STORE_OP_STORE); resources->registerRenderPassOutput("DEPTHPREPASS_DEPTH", depthAttachment); - resources->registerRenderPassOutput("DEPTHPREPASS_RESOLVED_DEPTH", depthResolveAttachment); } void DepthPrepass::createRenderPass() { Gfx::ORenderTargetLayout layout = new Gfx::RenderTargetLayout{ .depthAttachment = depthAttachment, - .depthResolveAttachment = depthResolveAttachment, }; renderPass = graphics->createRenderPass(std::move(layout), viewport); } diff --git a/src/Engine/Graphics/RenderPass/DepthPrepass.h b/src/Engine/Graphics/RenderPass/DepthPrepass.h index 0a7c9ce..4e556cf 100644 --- a/src/Engine/Graphics/RenderPass/DepthPrepass.h +++ b/src/Engine/Graphics/RenderPass/DepthPrepass.h @@ -19,9 +19,7 @@ public: static void modifyRenderPassMacros(Map& defines); private: Gfx::ORenderTargetAttachment depthAttachment; - Gfx::ORenderTargetAttachment depthResolveAttachment; Gfx::OTexture2D depthBuffer; - Gfx::OTexture2D depthResolveBuffer; Array descriptorSets; diff --git a/src/Engine/Graphics/RenderPass/LightCullingPass.cpp b/src/Engine/Graphics/RenderPass/LightCullingPass.cpp index 1175503..88f812d 100644 --- a/src/Engine/Graphics/RenderPass/LightCullingPass.cpp +++ b/src/Engine/Graphics/RenderPass/LightCullingPass.cpp @@ -174,7 +174,7 @@ void LightCullingPass::publishOutputs() void LightCullingPass::createRenderPass() { - depthAttachment = resources->requestRenderTarget("DEPTHPREPASS_RESOLVED_DEPTH")->getTexture(); + depthAttachment = resources->requestRenderTarget("DEPTHPREPASS_DEPTH")->getTexture(); } void LightCullingPass::modifyRenderPassMacros(Map&) diff --git a/src/Engine/Graphics/RenderPass/SkyboxRenderPass.cpp b/src/Engine/Graphics/RenderPass/SkyboxRenderPass.cpp index 2d6c70c..0147c1d 100644 --- a/src/Engine/Graphics/RenderPass/SkyboxRenderPass.cpp +++ b/src/Engine/Graphics/RenderPass/SkyboxRenderPass.cpp @@ -51,8 +51,6 @@ void SkyboxRenderPass::render() renderCommand->draw(36, 1, 0, 0); graphics->executeCommands(Array{ renderCommand }); graphics->endRenderPass(); - baseColorAttachment->getTexture()->changeLayout(Gfx::SE_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL); - graphics->resolveTexture(baseColorAttachment->getTexture(), viewport->getOwner()->getBackBuffer()); } void SkyboxRenderPass::endFrame() diff --git a/src/Engine/Graphics/RenderTarget.h b/src/Engine/Graphics/RenderTarget.h index 28a743a..20707fa 100644 --- a/src/Engine/Graphics/RenderTarget.h +++ b/src/Engine/Graphics/RenderTarget.h @@ -15,24 +15,24 @@ public: SeAttachmentStoreOp storeOp = SE_ATTACHMENT_STORE_OP_STORE, SeAttachmentLoadOp stencilLoadOp = SE_ATTACHMENT_LOAD_OP_DONT_CARE, SeAttachmentStoreOp stencilStoreOp = SE_ATTACHMENT_STORE_OP_DONT_CARE); - ~RenderTargetAttachment(); - PTexture2D getTexture() + virtual ~RenderTargetAttachment(); + virtual PTexture2D getTexture() { return texture; } - SeFormat getFormat() const + virtual SeFormat getFormat() const { return texture->getFormat(); } - SeSampleCountFlags getNumSamples() const + virtual SeSampleCountFlags getNumSamples() const { return texture->getNumSamples(); } - uint32 getWidth() const + virtual uint32 getWidth() const { return texture->getWidth(); } - uint32 getHeight() const + virtual uint32 getHeight() const { return texture->getHeight(); } @@ -51,6 +51,44 @@ protected: }; DEFINE_REF(RenderTargetAttachment) +class SwapchainAttachment : public RenderTargetAttachment +{ +public: + SwapchainAttachment(PViewport viewport, + SeAttachmentLoadOp loadOp = SE_ATTACHMENT_LOAD_OP_LOAD, + SeAttachmentStoreOp storeOp = SE_ATTACHMENT_STORE_OP_STORE, + SeAttachmentLoadOp stencilLoadOp = SE_ATTACHMENT_LOAD_OP_DONT_CARE, + SeAttachmentStoreOp stencilStoreOp = SE_ATTACHMENT_STORE_OP_DONT_CARE) + : RenderTargetAttachment(nullptr, loadOp, storeOp, stencilLoadOp, stencilStoreOp) + , viewport(viewport) + {} + virtual ~SwapchainAttachment() + { + } + virtual PTexture2D getTexture() + { + return viewport->getOwner()->getBackBuffer(); + } + virtual SeFormat getFormat() const + { + return viewport->getOwner()->getSwapchainFormat(); + } + virtual SeSampleCountFlags getNumSamples() const + { + return viewport->getSamples(); + } + virtual uint32 getWidth() const + { + return viewport->getWidth(); + } + virtual uint32 getHeight() const + { + return viewport->getHeight(); + } +private: + PViewport viewport; +}; + class RenderTargetLayout { public: diff --git a/src/Engine/Graphics/VertexData.cpp b/src/Engine/Graphics/VertexData.cpp index 9abcecc..9b17646 100644 --- a/src/Engine/Graphics/VertexData.cpp +++ b/src/Engine/Graphics/VertexData.cpp @@ -6,6 +6,7 @@ #include "Graphics/Descriptor.h" #include "Component/Mesh.h" #include "Graphics/Shader.h" +#include "Graphics/Mesh.h" #include "Containers/Set.h" using namespace Seele; diff --git a/src/Engine/Graphics/Vulkan/PipelineCache.cpp b/src/Engine/Graphics/Vulkan/PipelineCache.cpp index 2c592ce..6e9bf2a 100644 --- a/src/Engine/Graphics/Vulkan/PipelineCache.cpp +++ b/src/Engine/Graphics/Vulkan/PipelineCache.cpp @@ -140,7 +140,7 @@ PGraphicsPipeline PipelineCache::createPipeline(Gfx::LegacyPipelineCreateInfo gf .depthBiasConstantFactor = gfxInfo.rasterizationState.depthBiasConstantFactor, .depthBiasClamp = gfxInfo.rasterizationState.depthBiasClamp, .depthBiasSlopeFactor = gfxInfo.rasterizationState.depthBiasSlopeFactor, - .lineWidth = 0, + .lineWidth = gfxInfo.rasterizationState.lineWidth, }; hash = CRC::Calculate(&rasterizationState, sizeof(rasterizationState), CRC::CRC_32(), hash); diff --git a/src/Engine/Graphics/Vulkan/Resources.cpp b/src/Engine/Graphics/Vulkan/Resources.cpp index 2f65c32..863afde 100644 --- a/src/Engine/Graphics/Vulkan/Resources.cpp +++ b/src/Engine/Graphics/Vulkan/Resources.cpp @@ -20,7 +20,7 @@ Semaphore::Semaphore(PGraphics graphics) Semaphore::~Semaphore() { - vkDestroySemaphore(graphics->getDevice(), handle, nullptr); + graphics->getDestructionManager()->queueSemaphore(graphics->getGraphicsCommands()->getCommands(), handle); } Fence::Fence(PGraphics graphics) diff --git a/src/Engine/Graphics/Vulkan/Window.cpp b/src/Engine/Graphics/Vulkan/Window.cpp index efbb7f5..a987a10 100644 --- a/src/Engine/Graphics/Vulkan/Window.cpp +++ b/src/Engine/Graphics/Vulkan/Window.cpp @@ -53,7 +53,8 @@ void glfwCloseCallback(GLFWwindow* handle) Window* window = (Window*)glfwGetWindowUserPointer(handle); window->close(); } -void glfwResizeCallback(GLFWwindow* handle, int width, int height) + +void glfwFramebufferResizeCallback(GLFWwindow* handle, int width, int height) { Window* window = (Window*)glfwGetWindowUserPointer(handle); window->resize(width, height); @@ -69,6 +70,8 @@ Window::Window(PGraphics graphics, const WindowCreateInfo &createInfo) GLFWwindow *handle = glfwCreateWindow(createInfo.width, createInfo.height, createInfo.title, nullptr, nullptr); windowHandle = handle; glfwSetWindowUserPointer(handle, this); + int w, h; + glfwGetWindowSize(handle, &w, &h); glfwSetKeyCallback(handle, &glfwKeyCallback); glfwSetCursorPosCallback(handle, &glfwMouseMoveCallback); @@ -76,7 +79,8 @@ Window::Window(PGraphics graphics, const WindowCreateInfo &createInfo) glfwSetScrollCallback(handle, &glfwScrollCallback); glfwSetDropCallback(handle, &glfwFileCallback); glfwSetWindowCloseCallback(handle, &glfwCloseCallback); - glfwSetWindowSizeCallback(handle, &glfwResizeCallback); + glfwSetFramebufferSizeCallback(handle, &glfwFramebufferResizeCallback); + //glfwSetWindowSizeCallback(handle, &glfwResizeCallback); glfwCreateWindowSurface(instance, handle, nullptr, &surface); @@ -104,8 +108,10 @@ void Window::pollInput() void Window::beginFrame() { - vkAcquireNextImageKHR(graphics->getDevice(), swapchain, std::numeric_limits::max(), imageAvailableSemaphores[currentSemaphoreIndex]->getHandle(), VK_NULL_HANDLE, ¤tImageIndex); - swapChainTextures[currentImageIndex]->changeLayout(Gfx::SE_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL); + imageAvailableFences[currentSemaphoreIndex]->wait(100000); + imageAvailableFences[currentSemaphoreIndex]->reset(); + vkAcquireNextImageKHR(graphics->getDevice(), swapchain, std::numeric_limits::max(), imageAvailableSemaphores[currentSemaphoreIndex]->getHandle(), imageAvailableFences[currentSemaphoreIndex]->getHandle(), ¤tImageIndex); + swapChainTextures[currentImageIndex]->changeLayout(Gfx::SE_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL); graphics->getGraphicsCommands()->getCommands()->waitForSemaphore(VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT, imageAvailableSemaphores[currentSemaphoreIndex]); } @@ -334,6 +340,12 @@ void Window::createSwapChain() .samples = 1, .usage = Gfx::SE_IMAGE_USAGE_COLOR_ATTACHMENT_BIT | Gfx::SE_IMAGE_USAGE_TRANSFER_DST_BIT, }, swapChainImages[i]); + if(imageAvailableFences[i] != nullptr) + { + imageAvailableFences[i]->wait(100000); + imageAvailableFences[i]->reset(); + } + imageAvailableFences[i] = new Fence(graphics); imageAvailableSemaphores[i] = new Semaphore(graphics); renderingDoneSemaphores[i] = new Semaphore(graphics); } diff --git a/src/Engine/Graphics/Vulkan/Window.h b/src/Engine/Graphics/Vulkan/Window.h index cb45811..61e040e 100644 --- a/src/Engine/Graphics/Vulkan/Window.h +++ b/src/Engine/Graphics/Vulkan/Window.h @@ -55,6 +55,7 @@ protected: void *windowHandle; StaticArray swapChainImages; StaticArray swapChainTextures; + StaticArray imageAvailableFences; StaticArray imageAvailableSemaphores; StaticArray renderingDoneSemaphores; uint32 currentImageIndex = 0; diff --git a/src/Engine/MinimalEngine.h b/src/Engine/MinimalEngine.h index a90fee7..2aff069 100644 --- a/src/Engine/MinimalEngine.h +++ b/src/Engine/MinimalEngine.h @@ -26,7 +26,6 @@ typedef OwningPtr O##x; \ } - namespace Seele { template diff --git a/src/Engine/Scene/Scene.h b/src/Engine/Scene/Scene.h index 1b4bb67..1e4ba7e 100644 --- a/src/Engine/Scene/Scene.h +++ b/src/Engine/Scene/Scene.h @@ -5,6 +5,7 @@ #include "Physics/PhysicsSystem.h" #include "Component/Skybox.h" #include "LightEnvironment.h" +#include namespace Seele { @@ -18,7 +19,9 @@ public: void update(float deltaTime); entt::entity createEntity() { - return registry.create(); + auto result = registry.create(); + std::cout << "Created " << (uint64_t)result << " at " << this << std::endl; + return result; } void destroyEntity(entt::entity identifier) { @@ -27,6 +30,8 @@ public: template Component& attachComponent(entt::entity entity, Args&&... args) { + std::cout << "Emplace " << (uint64_t)entity << std::endl; + std::cout << "Exists " << registry.valid(entity) << std::endl; return registry.emplace(entity, std::forward(args)...); } template diff --git a/src/Engine/System/MeshUpdater.cpp b/src/Engine/System/MeshUpdater.cpp index 12a27b7..ace1b8e 100644 --- a/src/Engine/System/MeshUpdater.cpp +++ b/src/Engine/System/MeshUpdater.cpp @@ -1,5 +1,6 @@ #include "MeshUpdater.h" #include "Component/Mesh.h" +#include "Graphics/Mesh.h" using namespace Seele; using namespace Seele::System;