diff --git a/external/zlib b/external/zlib index 04f42ce..09155ea 160000 --- a/external/zlib +++ b/external/zlib @@ -1 +1 @@ -Subproject commit 04f42ceca40f73e2978b50e93806c2a18c1281fc +Subproject commit 09155eaa2f9270dc4ed1fa13e2b4b2613e6e4851 diff --git a/res/shaders/lib/MaterialParameter.slang b/res/shaders/lib/MaterialParameter.slang index c77ac8e..6284b55 100644 --- a/res/shaders/lib/MaterialParameter.slang +++ b/res/shaders/lib/MaterialParameter.slang @@ -20,14 +20,13 @@ struct LightingParameter // data passed to fragment shader struct FragmentParameter { - // clip position has to be the first attribute for some reason???? - float4 position_CS : SV_Position; float3 position_TS; float3 viewDir_TS; float3 normal_WS; float3 tangent_WS; float3 biTangent_WS; float3 position_WS; + float4 position_CS; float2 texCoords; float3 vertexColor; MaterialParameter getMaterialParameter() diff --git a/src/Engine/Component/KeyboardInput.h b/src/Engine/Component/KeyboardInput.h index 33d3b61..48adb0e 100644 --- a/src/Engine/Component/KeyboardInput.h +++ b/src/Engine/Component/KeyboardInput.h @@ -12,6 +12,8 @@ struct KeyboardInput bool mouse2; float mouseX; float mouseY; + float deltaX; + float deltaY; }; } // namespace Component } // namespace Seele \ No newline at end of file diff --git a/src/Engine/Graphics/Vulkan/Command.cpp b/src/Engine/Graphics/Vulkan/Command.cpp index ebacfe8..9710e37 100644 --- a/src/Engine/Graphics/Vulkan/Command.cpp +++ b/src/Engine/Graphics/Vulkan/Command.cpp @@ -468,6 +468,13 @@ CommandPool::CommandPool(PGraphics graphics, PQueue queue) CommandPool::~CommandPool() { + for (auto& command : allocatedBuffers) + { + command->waitForCommand(); + } + allocatedRenderCommands.clear(); + allocatedComputeCommands.clear(); + allocatedBuffers.clear(); vkDestroyCommandPool(graphics->getDevice(), commandPool, nullptr); graphics = nullptr; queue = nullptr; diff --git a/src/Engine/Graphics/Vulkan/Graphics.cpp b/src/Engine/Graphics/Vulkan/Graphics.cpp index a161134..7d8721f 100644 --- a/src/Engine/Graphics/Vulkan/Graphics.cpp +++ b/src/Engine/Graphics/Vulkan/Graphics.cpp @@ -522,8 +522,6 @@ void Graphics::createDevice(GraphicsInitializer initializer) .pNext = &descriptorIndexing, .queueCreateInfoCount = (uint32)queueInfos.size(), .pQueueCreateInfos = queueInfos.data(), - .enabledLayerCount = (uint32_t)initializer.layers.size(), - .ppEnabledLayerNames = initializer.layers.data(), .enabledExtensionCount = (uint32)initializer.deviceExtensions.size(), .ppEnabledExtensionNames = initializer.deviceExtensions.data(), .pEnabledFeatures = &features, diff --git a/src/Engine/Graphics/Vulkan/Resources.cpp b/src/Engine/Graphics/Vulkan/Resources.cpp index 54167b2..637c9e4 100644 --- a/src/Engine/Graphics/Vulkan/Resources.cpp +++ b/src/Engine/Graphics/Vulkan/Resources.cpp @@ -1,6 +1,8 @@ #include "Resources.h" #include "Enums.h" #include "Graphics.h" +#include "Command.h" +#include "Window.h" using namespace Seele; using namespace Seele::Vulkan; @@ -18,17 +20,7 @@ Semaphore::Semaphore(PGraphics graphics) Semaphore::~Semaphore() { - uint64 value = 0; - VkSemaphoreWaitInfo waitInfo = { - .sType = VK_STRUCTURE_TYPE_SEMAPHORE_WAIT_INFO, - .pNext = nullptr, - .flags = 0, - .semaphoreCount = 1, - .pSemaphores = &handle, - .pValues = &value, - }; - VK_CHECK(vkWaitSemaphores(graphics->getDevice(), &waitInfo, 10000)); - vkDestroySemaphore(graphics->getDevice(), handle, nullptr); + graphics->getDestructionManager()->queueSemaphore(graphics->getGraphicsCommands()->getCommands(), handle); } Fence::Fence(PGraphics graphics) diff --git a/src/Engine/Graphics/Vulkan/Texture.cpp b/src/Engine/Graphics/Vulkan/Texture.cpp index 8e1a892..4e07859 100644 --- a/src/Engine/Graphics/Vulkan/Texture.cpp +++ b/src/Engine/Graphics/Vulkan/Texture.cpp @@ -41,9 +41,11 @@ TextureBase::TextureBase(PGraphics graphics, VkImageViewType viewType, , image(existingImage) , aspect(getAspectFromFormat(createInfo.format)) , layout(Gfx::SE_IMAGE_LAYOUT_UNDEFINED) + , ownsImage(false) { if (existingImage == VK_NULL_HANDLE) { + ownsImage = true; PAllocator pool = graphics->getAllocator(); VkImageType type = VK_IMAGE_TYPE_MAX_ENUM; VkImageCreateFlags flags = 0; @@ -175,13 +177,16 @@ TextureBase::TextureBase(PGraphics graphics, VkImageViewType viewType, }, }; - VK_CHECK(vkCreateImageView(graphics->getDevice(), &viewInfo, nullptr, &defaultView)); + VK_CHECK(vkCreateImageView(graphics->getDevice(), &viewInfo, nullptr, &imageView)); } TextureBase::~TextureBase() { - graphics->getDestructionManager()->queueImage(graphics->getQueueCommands(currentOwner)->getCommands(), image); - graphics->getDestructionManager()->queueImageView(graphics->getQueueCommands(currentOwner)->getCommands(), defaultView); + if (ownsImage) + { + graphics->getDestructionManager()->queueImage(graphics->getQueueCommands(currentOwner)->getCommands(), image); + } + graphics->getDestructionManager()->queueImageView(graphics->getQueueCommands(currentOwner)->getCommands(), imageView); } diff --git a/src/Engine/Graphics/Vulkan/Texture.h b/src/Engine/Graphics/Vulkan/Texture.h index 25371e9..4c1a115 100644 --- a/src/Engine/Graphics/Vulkan/Texture.h +++ b/src/Engine/Graphics/Vulkan/Texture.h @@ -21,7 +21,7 @@ public: } constexpr VkImageView getView() const { - return defaultView; + return imageView; } constexpr Gfx::SeImageLayout getLayout() const { @@ -72,9 +72,10 @@ protected: Gfx::SeFormat format; Gfx::SeImageUsageFlags usage; VkImage image; - VkImageView defaultView; + VkImageView imageView; VkImageAspectFlags aspect; Gfx::SeImageLayout layout; + uint8 ownsImage; friend class Graphics; }; diff --git a/src/Engine/Graphics/Vulkan/Window.cpp b/src/Engine/Graphics/Vulkan/Window.cpp index ae505e7..dbd85cb 100644 --- a/src/Engine/Graphics/Vulkan/Window.cpp +++ b/src/Engine/Graphics/Vulkan/Window.cpp @@ -49,6 +49,11 @@ void glfwCloseCallback(GLFWwindow* handle) Window* window = (Window*)glfwGetWindowUserPointer(handle); window->closeCallback(); } +void glfwResizeCallback(GLFWwindow* handle, int width, int height) +{ + Window* window = (Window*)glfwGetWindowUserPointer(handle); + window->resize(width, height); +} Window::Window(PGraphics graphics, const WindowCreateInfo &createInfo) : graphics(graphics) @@ -68,20 +73,11 @@ Window::Window(PGraphics graphics, const WindowCreateInfo &createInfo) glfwSetScrollCallback(handle, &glfwScrollCallback); glfwSetDropCallback(handle, &glfwFileCallback); glfwSetWindowCloseCallback(handle, &glfwCloseCallback); + glfwSetWindowSizeCallback(handle, &glfwResizeCallback); glfwCreateWindowSurface(instance, handle, nullptr, &surface); - vkGetPhysicalDeviceSurfaceCapabilitiesKHR(graphics->getPhysicalDevice(), surface, &capabilities); - - uint32 numFormats; - vkGetPhysicalDeviceSurfaceFormatsKHR(graphics->getPhysicalDevice(), surface, &numFormats, nullptr); - supportedFormats.resize(numFormats); - vkGetPhysicalDeviceSurfaceFormatsKHR(graphics->getPhysicalDevice(), surface, &numFormats, supportedFormats.data()); - - uint32 numPresentModes; - vkGetPhysicalDeviceSurfacePresentModesKHR(graphics->getPhysicalDevice(), surface, &numPresentModes, nullptr); - supportedPresentModes.resize(numFormats); - vkGetPhysicalDeviceSurfacePresentModesKHR(graphics->getPhysicalDevice(), surface, &numPresentModes, supportedPresentModes.data()); + querySurface(); chooseSwapSurfaceFormat(); framebufferFormat = cast(format.format); chooseSwapPresentMode(); @@ -175,6 +171,33 @@ void Window::setCloseCallback(std::function callback) closeCallback = callback; } +void Window::resize(int width, int height) +{ + querySurface(); + chooseSwapSurfaceFormat(); + framebufferFormat = cast(format.format); + chooseSwapPresentMode(); + chooseSwapExtent(); + framebufferWidth = extent.width; + framebufferHeight = extent.height; + createSwapChain(); +} + +void Window::querySurface() +{ + vkGetPhysicalDeviceSurfaceCapabilitiesKHR(graphics->getPhysicalDevice(), surface, &capabilities); + + uint32 numFormats; + vkGetPhysicalDeviceSurfaceFormatsKHR(graphics->getPhysicalDevice(), surface, &numFormats, nullptr); + supportedFormats.resize(numFormats); + vkGetPhysicalDeviceSurfaceFormatsKHR(graphics->getPhysicalDevice(), surface, &numFormats, supportedFormats.data()); + + uint32 numPresentModes; + vkGetPhysicalDeviceSurfacePresentModesKHR(graphics->getPhysicalDevice(), surface, &numPresentModes, nullptr); + supportedPresentModes.resize(numFormats); + vkGetPhysicalDeviceSurfacePresentModesKHR(graphics->getPhysicalDevice(), surface, &numPresentModes, supportedPresentModes.data()); +} + void Window::chooseSwapSurfaceFormat() { for (const auto& supportedFormat : supportedFormats) diff --git a/src/Engine/Graphics/Vulkan/Window.h b/src/Engine/Graphics/Vulkan/Window.h index 088b7be..198abfb 100644 --- a/src/Engine/Graphics/Vulkan/Window.h +++ b/src/Engine/Graphics/Vulkan/Window.h @@ -23,6 +23,7 @@ public: virtual void setScrollCallback(std::function callback) override; virtual void setFileCallback(std::function callback) override; virtual void setCloseCallback(std::function callback); + void resize(int width, int height); std::function keyCallback; std::function mouseMoveCallback; @@ -31,6 +32,7 @@ public: std::function fileCallback; std::function closeCallback; protected: + void querySurface(); void chooseSwapSurfaceFormat(); void chooseSwapPresentMode(); void chooseSwapExtent(); diff --git a/src/Engine/Material/Material.cpp b/src/Engine/Material/Material.cpp index c84d443..481e578 100644 --- a/src/Engine/Material/Material.cpp +++ b/src/Engine/Material/Material.cpp @@ -123,11 +123,11 @@ void Material::compile() // initialize variable state for(const auto& expr :codeExpressions) { - codeStream << expr->evaluate(varState); + codeStream << "\t\t" << expr->evaluate(varState); } for(const auto& [name, exp] : brdf.variables) { - codeStream << "\t\tresult." << name << " = " << varState[exp] << ";"; + codeStream << "\t\tresult." << name << " = " << varState[exp] << ";" << std::endl; } codeStream << "\t\treturn result;\n"; codeStream << "\t}\n"; diff --git a/src/Engine/System/CMakeLists.txt b/src/Engine/System/CMakeLists.txt index 56fbb1b..fe6432a 100644 --- a/src/Engine/System/CMakeLists.txt +++ b/src/Engine/System/CMakeLists.txt @@ -5,6 +5,8 @@ target_sources(Engine ComponentSystem.h Executor.h Executor.cpp + KeyboardInput.h + KeyboardInput.cpp LightGather.h LightGather.cpp MeshUpdater.h @@ -19,6 +21,7 @@ target_sources(Engine CameraUpdater.h ComponentSystem.h Executor.h + KeyboardInput.h LightGather.h MeshUpdater.h SystemBase.h diff --git a/src/Engine/System/KeyboardInput.cpp b/src/Engine/System/KeyboardInput.cpp new file mode 100644 index 0000000..802f782 --- /dev/null +++ b/src/Engine/System/KeyboardInput.cpp @@ -0,0 +1,51 @@ +#include "KeyboardInput.h" + +using namespace Seele; +using namespace Seele::System; + +KeyboardInput::KeyboardInput(PScene scene) + : ComponentSystem(scene) +{ +} + +KeyboardInput::~KeyboardInput() +{ +} + +void KeyboardInput::update(Component::KeyboardInput& input) +{ + std::memcpy(input.keys.data(), keys.data(), sizeof(keys)); + input.deltaX = deltaX; + input.deltaY = deltaY; + input.mouseX = mouseX; + input.mouseY = mouseY; + input.mouse1 = mouse1; + input.mouse2 = mouse2; +} + +void KeyboardInput::keyCallback(KeyCode code, InputAction action, KeyModifier modifier) +{ + keys[code] = action != InputAction::RELEASE; +} + +void KeyboardInput::mouseCallback(double x, double y) +{ + mouseX = x; + mouseY = y; + deltaX = x - lastMouseX; + deltaY = y - lastMouseY; + lastMouseX = x; + lastMouseY = y; +} + +void KeyboardInput::mouseButtonCallback(MouseButton button, InputAction action, KeyModifier modifier) +{ + if (button == MouseButton::MOUSE_BUTTON_1) + { + mouse1 = action != InputAction::RELEASE; + } + if (button == MouseButton::MOUSE_BUTTON_2) + { + mouse2 = action != InputAction::RELEASE; + } +} diff --git a/src/Engine/System/KeyboardInput.h b/src/Engine/System/KeyboardInput.h new file mode 100644 index 0000000..f0fb3b9 --- /dev/null +++ b/src/Engine/System/KeyboardInput.h @@ -0,0 +1,31 @@ +#pragma once +#include "ComponentSystem.h" +#include "Component/KeyboardInput.h" + +namespace Seele +{ +namespace System +{ +class KeyboardInput : public ComponentSystem +{ +public: + KeyboardInput(PScene scene); + virtual ~KeyboardInput(); + virtual void update(Component::KeyboardInput& input) override; + void keyCallback(KeyCode code, InputAction action, KeyModifier modifier); + void mouseCallback(double xPos, double yPos); + void mouseButtonCallback(MouseButton button, InputAction action, KeyModifier modifier); +private: + Seele::StaticArray keys; + bool mouse1 = false; + bool mouse2 = false; + float lastMouseX = 0; + float lastMouseY = 0; + float mouseX = 0; + float mouseY = 0; + float deltaX = 0; + float deltaY = 0; +}; +DEFINE_REF(KeyboardInput) +} +} \ No newline at end of file diff --git a/src/Engine/System/SystemGraph.cpp b/src/Engine/System/SystemGraph.cpp index 9666eef..51f53d5 100644 --- a/src/Engine/System/SystemGraph.cpp +++ b/src/Engine/System/SystemGraph.cpp @@ -2,7 +2,7 @@ using namespace Seele; -void SystemGraph::addSystem(System::UPSystemBase system) +void SystemGraph::addSystem(System::OSystemBase system) { systems.add(std::move(system)); } diff --git a/src/Engine/System/SystemGraph.h b/src/Engine/System/SystemGraph.h index 8b09ed5..d38e817 100644 --- a/src/Engine/System/SystemGraph.h +++ b/src/Engine/System/SystemGraph.h @@ -7,10 +7,10 @@ namespace Seele class SystemGraph { public: - void addSystem(System::UPSystemBase system); + void addSystem(System::OSystemBase system); void run(dp::thread_pool<>& threadPool, float deltaTime); private: - Array systems; + Array systems; }; DEFINE_REF(SystemGraph) } // namespace Seele; \ No newline at end of file diff --git a/src/Engine/Window/GameView.cpp b/src/Engine/Window/GameView.cpp index a68b136..3f4b930 100644 --- a/src/Engine/Window/GameView.cpp +++ b/src/Engine/Window/GameView.cpp @@ -21,7 +21,6 @@ GameView::GameView(Gfx::PGraphics graphics, PWindow window, const ViewportCreate SkyboxRenderPass(graphics, scene) )) { - camera = new CameraActor(scene); reloadGame(); renderGraph.updateViewport(viewport); } @@ -65,7 +64,12 @@ void GameView::prepareRender() void GameView::render() { - renderGraph.render(camera->accessComponent()); + Component::Camera cam; + scene->view([&cam](Component::Camera& c) { + if (c.mainCamera) + cam = c; + }); + renderGraph.render(cam); } void GameView::reloadGame() @@ -74,75 +78,33 @@ void GameView::reloadGame() systemGraph = new SystemGraph(); gameInterface.getGame()->setupScene(scene, systemGraph); + System::OKeyboardInput keyInput = new System::KeyboardInput(scene); + keyboardSystem = keyInput; + systemGraph->addSystem(std::move(keyInput)); systemGraph->addSystem(new System::LightGather(scene)); systemGraph->addSystem(new System::MeshUpdater(scene)); systemGraph->addSystem(new System::CameraUpdater(scene)); } -void GameView::keyCallback(KeyCode code, InputAction action, KeyModifier) +void GameView::keyCallback(KeyCode code, InputAction action, KeyModifier modifier) { - scene->view([=, this](Component::KeyboardInput& input) - { - //if(code == KeyCode::KEY_R && action == InputAction::PRESS) - //{ - // auto cubeMesh = AssetRegistry::findMesh("cube"); - // PEntity cube2 = new Entity(scene); - // Component::Transform& cube2Transform = cube2->attachComponent(); - // cube2Transform.setPosition(Vector(0, 20, 0)); - // cube2Transform.setRotation(Quaternion(Vector(0.f, 90.f, 90.f))); - // cube2Transform.setScale(Vector(2.f, 2.f, 2.f)); - // cubeMesh->physicsMesh.type = Component::ColliderType::DYNAMIC; - // cube2->attachComponent(cubeMesh->physicsMesh); - // cube2->attachComponent(cubeMesh); - // Component::RigidBody& physics2 = cube2->attachComponent(); - // physics2.linearMomentum = Vector(0, -10, 0); - // physics2.angularMomentum = Vector(0, 0, 0); - //} - // - //if(code == KeyCode::KEY_B && action == InputAction::PRESS) - //{ - // showDebug = !showDebug; - // debugPassData.vertices.clear(); - //} - if(code == KeyCode::KEY_R && action == InputAction::PRESS) - { - reloadGame(); - } - - input.keys[code] = action != InputAction::RELEASE; - }); + keyboardSystem->keyCallback(code, action, modifier); } void GameView::mouseMoveCallback(double xPos, double yPos) { - scene->view([=](Component::KeyboardInput& input) - { - input.mouseX = static_cast(xPos); - input.mouseY = static_cast(yPos); - }); + keyboardSystem->mouseCallback(xPos, yPos); } -void GameView::mouseButtonCallback(MouseButton button, InputAction action, KeyModifier) +void GameView::mouseButtonCallback(MouseButton button, InputAction action, KeyModifier modifier) { - scene->view([=](Component::KeyboardInput& input) - { - if (button == MouseButton::MOUSE_BUTTON_1) - { - input.mouse1 = action != InputAction::RELEASE; - } - if (button == MouseButton::MOUSE_BUTTON_2) - { - input.mouse2 = action != InputAction::RELEASE; - } - }); + keyboardSystem->mouseButtonCallback(button, action, modifier); } void GameView::scrollCallback(double, double) { - } void GameView::fileCallback(int, const char**) { - } diff --git a/src/Engine/Window/GameView.h b/src/Engine/Window/GameView.h index 798d40c..51a79f0 100644 --- a/src/Engine/Window/GameView.h +++ b/src/Engine/Window/GameView.h @@ -5,6 +5,7 @@ #include "Graphics/RenderPass/LightCullingPass.h" #include "Graphics/RenderPass/BasePass.h" #include "Graphics/RenderPass/SkyboxRenderPass.h" +#include "System/KeyboardInput.h" #ifdef WIN32 #include "Platform/Windows/GameInterface.h" // TODO #else @@ -28,7 +29,6 @@ public: void reloadGame(); private: OScene scene; - OEntity camera; GameInterface gameInterface; RenderGraph< DepthPrepass, @@ -38,6 +38,7 @@ private: > renderGraph; PSystemGraph systemGraph; + System::PKeyboardInput keyboardSystem; dp::thread_pool<> threadPool; float updateTime = 0;