From 328edf51962c3bad8307cf3201b33428b6b72f0e Mon Sep 17 00:00:00 2001 From: HOEGLER Stefan Date: Thu, 5 Mar 2020 11:43:38 +0100 Subject: [PATCH] Added scene renderer --- src/Engine/Containers/Array.h | 2 + src/Engine/Graphics/CMakeLists.txt | 6 ++ src/Engine/Graphics/Graphics.h | 8 +- src/Engine/Graphics/GraphicsResources.h | 2 +- src/Engine/Graphics/RenderCore.cpp | 33 ++++++++ src/Engine/Graphics/RenderCore.h | 16 ++++ src/Engine/Graphics/RenderPath.cpp | 4 + src/Engine/Graphics/RenderPath.h | 2 + src/Engine/Graphics/SceneRenderPath.cpp | 0 src/Engine/Graphics/SceneRenderPath.h | 17 ++++ src/Engine/Graphics/SceneView.cpp | 15 ++++ src/Engine/Graphics/SceneView.h | 12 +++ src/Engine/Graphics/View.cpp | 18 ++++- src/Engine/Graphics/View.h | 12 +-- src/Engine/Graphics/Vulkan/VulkanGraphics.cpp | 23 +++++- src/Engine/Graphics/Vulkan/VulkanGraphics.h | 8 +- src/Engine/Graphics/Window.cpp | 17 +++- src/Engine/Graphics/Window.h | 46 +++++++++-- src/Engine/Graphics/WindowManager.cpp | 31 +++++-- src/Engine/Graphics/WindowManager.h | 6 +- src/Engine/Math/Math.h | 32 ++++++++ src/Engine/MinimalEngine.h | 57 +++++++++++-- src/Engine/main.cpp | 80 ++----------------- 23 files changed, 334 insertions(+), 113 deletions(-) create mode 100644 src/Engine/Graphics/RenderCore.cpp create mode 100644 src/Engine/Graphics/RenderCore.h create mode 100644 src/Engine/Graphics/SceneRenderPath.cpp create mode 100644 src/Engine/Graphics/SceneRenderPath.h create mode 100644 src/Engine/Graphics/SceneView.cpp create mode 100644 src/Engine/Graphics/SceneView.h diff --git a/src/Engine/Containers/Array.h b/src/Engine/Containers/Array.h index 678d59e..83b7b53 100644 --- a/src/Engine/Containers/Array.h +++ b/src/Engine/Containers/Array.h @@ -17,6 +17,7 @@ namespace Seele , arraySize(0) { _data = (T*)malloc(DEFAULT_ALLOC_SIZE * sizeof(T)); + memset(_data, 0, sizeof(T) * DEFAULT_ALLOC_SIZE); refreshIterators(); } Array(size_t size, T value = T()) @@ -112,6 +113,7 @@ namespace Seele allocated += DEFAULT_ALLOC_SIZE; void* tempArray = malloc(sizeof(T) * allocated); std::memcpy(tempArray, _data, arraySize * sizeof(T)); + memset(tempArray, 0, sizeof(T) * allocated); delete _data; _data = (T*)tempArray; } diff --git a/src/Engine/Graphics/CMakeLists.txt b/src/Engine/Graphics/CMakeLists.txt index 3962a2b..d8dd645 100644 --- a/src/Engine/Graphics/CMakeLists.txt +++ b/src/Engine/Graphics/CMakeLists.txt @@ -2,10 +2,16 @@ target_sources(SeeleEngine PRIVATE Graphics.h Graphics.cpp + RenderCore.h + RenderCore.cpp RenderPath.h RenderPath.cpp + SceneRenderPath.h + SceneRenderPath.cpp View.h View.cpp + SceneView.h + SceneView.cpp WindowManager.h WindowManager.cpp Window.h diff --git a/src/Engine/Graphics/Graphics.h b/src/Engine/Graphics/Graphics.h index f758442..94a471c 100644 --- a/src/Engine/Graphics/Graphics.h +++ b/src/Engine/Graphics/Graphics.h @@ -9,12 +9,10 @@ namespace Seele { { public: virtual void init(GraphicsInitializer initializer) = 0; - virtual void beginFrame() = 0; - virtual void endFrame() = 0; + virtual void beginFrame(void* windowHandle) = 0; + virtual void endFrame(void* windowHandle) = 0; virtual void* createWindow(const WindowCreateInfo& createInfo) = 0; - - //Singleton - private: + protected: Graphics(); ~Graphics(); friend class Window; diff --git a/src/Engine/Graphics/GraphicsResources.h b/src/Engine/Graphics/GraphicsResources.h index 5d018f5..5ca4334 100644 --- a/src/Engine/Graphics/GraphicsResources.h +++ b/src/Engine/Graphics/GraphicsResources.h @@ -1,5 +1,5 @@ #pragma once - +#include "MinimalEngine.h" namespace Seele { struct GraphicsInitializer diff --git a/src/Engine/Graphics/RenderCore.cpp b/src/Engine/Graphics/RenderCore.cpp new file mode 100644 index 0000000..304d7f9 --- /dev/null +++ b/src/Engine/Graphics/RenderCore.cpp @@ -0,0 +1,33 @@ +#include "RenderCore.h" + +Seele::RenderCore::RenderCore() +{ + windowManager = new WindowManager(); +} + +Seele::RenderCore::~RenderCore() +{ +} + +void Seele::RenderCore::init() +{ + WindowCreateInfo mainWindowInfo; + mainWindowInfo.title = "SeeleEngine"; + mainWindowInfo.width = 1280; + mainWindowInfo.height = 720; + mainWindowInfo.bFullscreen = false; + windowManager->addWindow(mainWindowInfo); +} + +void Seele::RenderCore::renderLoop() +{ + while (true) + { + windowManager->beginFrame(); + windowManager->endFrame(); + } +} + +void Seele::RenderCore::shutdown() +{ +} diff --git a/src/Engine/Graphics/RenderCore.h b/src/Engine/Graphics/RenderCore.h new file mode 100644 index 0000000..12c5d0c --- /dev/null +++ b/src/Engine/Graphics/RenderCore.h @@ -0,0 +1,16 @@ +#pragma once +#include "WindowManager.h" +namespace Seele +{ + class RenderCore + { + public: + RenderCore(); + ~RenderCore(); + void init(); + void renderLoop(); + void shutdown(); + private: + PWindowManager windowManager; + }; +} \ No newline at end of file diff --git a/src/Engine/Graphics/RenderPath.cpp b/src/Engine/Graphics/RenderPath.cpp index 979078d..eaeea80 100644 --- a/src/Engine/Graphics/RenderPath.cpp +++ b/src/Engine/Graphics/RenderPath.cpp @@ -4,3 +4,7 @@ Seele::RenderPath::RenderPath(Graphics* graphics) : graphics(graphics) { } + +Seele::RenderPath::~RenderPath() +{ +} diff --git a/src/Engine/Graphics/RenderPath.h b/src/Engine/Graphics/RenderPath.h index a11e706..b4f58e1 100644 --- a/src/Engine/Graphics/RenderPath.h +++ b/src/Engine/Graphics/RenderPath.h @@ -8,12 +8,14 @@ namespace Seele public: RenderPath(Graphics* graphics); virtual ~RenderPath(); + virtual void applyArea(Rect area) = 0; virtual void init() = 0; virtual void beginFrame() = 0; virtual void render() = 0; virtual void endFrame() = 0; protected: Graphics* graphics; + Rect area; }; DECLARE_REF(RenderPath); } \ No newline at end of file diff --git a/src/Engine/Graphics/SceneRenderPath.cpp b/src/Engine/Graphics/SceneRenderPath.cpp new file mode 100644 index 0000000..e69de29 diff --git a/src/Engine/Graphics/SceneRenderPath.h b/src/Engine/Graphics/SceneRenderPath.h new file mode 100644 index 0000000..2bbc9d3 --- /dev/null +++ b/src/Engine/Graphics/SceneRenderPath.h @@ -0,0 +1,17 @@ +#pragma once +#include "RenderPath.h" + +namespace Seele +{ + class SceneRenderPath : public RenderPath + { + public: + SceneRenderPath(); + virtual ~SceneRenderPath(); + virtual void applyArea(Rect area) override; + virtual void init() override; + virtual void beginFrame() override; + virtual void render() override; + virtual void endFrame() override; + }; +} \ No newline at end of file diff --git a/src/Engine/Graphics/SceneView.cpp b/src/Engine/Graphics/SceneView.cpp new file mode 100644 index 0000000..20fd190 --- /dev/null +++ b/src/Engine/Graphics/SceneView.cpp @@ -0,0 +1,15 @@ +#include "SceneView.h" +#include "SceneRenderPath.h" + +Seele::SceneView::SceneView() +{ +} + +Seele::SceneView::~SceneView() +{ +} + +void Seele::SceneView::initRenderer() +{ + renderer = new SceneRenderPath(); +} diff --git a/src/Engine/Graphics/SceneView.h b/src/Engine/Graphics/SceneView.h new file mode 100644 index 0000000..e1e9a33 --- /dev/null +++ b/src/Engine/Graphics/SceneView.h @@ -0,0 +1,12 @@ +#pragma once +#include "View.h" +namespace Seele +{ + class SceneView : public View + { + public: + SceneView(); + ~SceneView(); + virtual void initRenderer() override; + }; +} \ No newline at end of file diff --git a/src/Engine/Graphics/View.cpp b/src/Engine/Graphics/View.cpp index 80d79a5..9dfcdee 100644 --- a/src/Engine/Graphics/View.cpp +++ b/src/Engine/Graphics/View.cpp @@ -1,10 +1,24 @@ #include "View.h" -Seele::View::View(PSection owner) - : owner(owner) +Seele::View::View() { } Seele::View::~View() { } + +void Seele::View::beginFrame() +{ + renderer->beginFrame(); +} + +void Seele::View::endFrame() +{ + renderer->endFrame(); +} + +void Seele::View::applyArea(Rect area) +{ + renderer->applyArea(area); +} diff --git a/src/Engine/Graphics/View.h b/src/Engine/Graphics/View.h index 7ca9a4c..5eaacf7 100644 --- a/src/Engine/Graphics/View.h +++ b/src/Engine/Graphics/View.h @@ -1,5 +1,4 @@ #pragma once -#include "Window.h" #include "RenderPath.h" namespace Seele { @@ -7,11 +6,14 @@ namespace Seele class View { public: - View(PSection owner); - ~View(); - private: + View(); + virtual ~View(); + virtual void initRenderer() = 0; + void beginFrame(); + void endFrame(); + void applyArea(Rect area); + protected: PRenderPath renderer; - PSection owner; }; DECLARE_REF(View) diff --git a/src/Engine/Graphics/Vulkan/VulkanGraphics.cpp b/src/Engine/Graphics/Vulkan/VulkanGraphics.cpp index cc15545..9e5eb03 100644 --- a/src/Engine/Graphics/Vulkan/VulkanGraphics.cpp +++ b/src/Engine/Graphics/Vulkan/VulkanGraphics.cpp @@ -1,18 +1,35 @@ #include "VulkanGraphics.h" +#include void Seele::VulkanGraphics::init(GraphicsInitializer initializer) { } -void Seele::VulkanGraphics::beginFrame() +void Seele::VulkanGraphics::beginFrame(void* windowHandle) { + GLFWwindow* window = static_cast(windowHandle); + glfwPollEvents(); } -void Seele::VulkanGraphics::endFrame() +void Seele::VulkanGraphics::endFrame(void* windowHandle) { + GLFWwindow* window = static_cast(windowHandle); + } void* Seele::VulkanGraphics::createWindow(const WindowCreateInfo& createInfo) { - return nullptr; + GLFWwindow* window = glfwCreateWindow(createInfo.width, createInfo.height, createInfo.title, createInfo.bFullscreen ? glfwGetPrimaryMonitor() : nullptr, nullptr); + return window; +} + +Seele::VulkanGraphics::VulkanGraphics() +{ + glfwInit(); + glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API); +} + +Seele::VulkanGraphics::~VulkanGraphics() +{ + glfwTerminate(); } diff --git a/src/Engine/Graphics/Vulkan/VulkanGraphics.h b/src/Engine/Graphics/Vulkan/VulkanGraphics.h index d1c955c..46b2a9b 100644 --- a/src/Engine/Graphics/Vulkan/VulkanGraphics.h +++ b/src/Engine/Graphics/Vulkan/VulkanGraphics.h @@ -7,8 +7,12 @@ namespace Seele public: // Inherited via Graphics virtual void init(GraphicsInitializer initializer) override; - virtual void beginFrame() override; - virtual void endFrame() override; + virtual void beginFrame(void* windowHandle) override; + virtual void endFrame(void* windowHandle) override; virtual void* createWindow(const WindowCreateInfo& createInfo) override; + protected: + VulkanGraphics(); + virtual ~VulkanGraphics(); + friend class Window; }; } \ No newline at end of file diff --git a/src/Engine/Graphics/Window.cpp b/src/Engine/Graphics/Window.cpp index 34dbad4..d8184d0 100644 --- a/src/Engine/Graphics/Window.cpp +++ b/src/Engine/Graphics/Window.cpp @@ -1,14 +1,29 @@ #include "Window.h" #include "Vulkan/VulkanGraphics.h" +#include "SceneView.h" Seele::Window::Window(const WindowCreateInfo& createInfo) : width(createInfo.width) , height(createInfo.height) - , windowHandle(nullptr) { + center = new Section(); + center->resizeArea(Rect(1, 1, 0, 0)); + center->addView(new SceneView()); graphics = new VulkanGraphics(); + windowHandle = graphics->createWindow(createInfo); } Seele::Window::~Window() { } + +void Seele::Window::beginFrame() +{ + graphics->beginFrame(windowHandle); + center->beginFrame(); +} + +void Seele::Window::endFrame() +{ + graphics->endFrame(windowHandle); +} diff --git a/src/Engine/Graphics/Window.h b/src/Engine/Graphics/Window.h index bab48c9..c19c0cc 100644 --- a/src/Engine/Graphics/Window.h +++ b/src/Engine/Graphics/Window.h @@ -1,5 +1,6 @@ #pragma once -#include "MinimalEngine.h" +#include "GraphicsResources.h" +#include "View.h" namespace Seele { // A window is divided into 5 sections, using a border layout @@ -13,23 +14,52 @@ namespace Seele { // +-------------BOTTOM----------------+ // In every section, there can be any number // of views, when there are multiple, they stack to tabs - struct Section + class Section { + public: + Section() + {} + void resizeArea(Rect area) + { + this->area = area; + } + void beginFrame() + { + views[0]->beginFrame(); + } + void endFrame() + { + views[0]->endFrame(); + } + void addView(PView view) + { + view->applyArea(area); + views.add(view); + } + void clearViews(PView view) + { + views.clear(); + } + void removeView(PView view) + { + views.remove(views.find(view)); + } + private: + Rect area; + Array views; }; DECLARE_REF(Section) - + class Graphics; class Window { public: Window(const WindowCreateInfo& createInfo); ~Window(); + void beginFrame(); + void endFrame(); private: void* windowHandle; - Section top; - Section bot; - Section left; - Section right; - Section center; + PSection center; uint32 width; uint32 height; Graphics* graphics; diff --git a/src/Engine/Graphics/WindowManager.cpp b/src/Engine/Graphics/WindowManager.cpp index c07ea56..3d1806c 100644 --- a/src/Engine/Graphics/WindowManager.cpp +++ b/src/Engine/Graphics/WindowManager.cpp @@ -1,16 +1,31 @@ #include "WindowManager.h" -Seele::WindowManager::WindowManager(GraphicsInitializer initializer) +Seele::WindowManager::WindowManager() { - //TODO Parse layout file - WindowCreateInfo mainWindowInfo; - mainWindowInfo.title = "SeeleEngine"; - mainWindowInfo.width = 1280; - mainWindowInfo.height = 720; - Window* mainWindow = new Window(mainWindowInfo); - windows.add(mainWindow); } Seele::WindowManager::~WindowManager() { } + +void Seele::WindowManager::addWindow(const WindowCreateInfo& createInfo) +{ + PWindow window = new Window(createInfo); + windows.add(window); +} + +void Seele::WindowManager::beginFrame() +{ + for (auto window : windows) + { + window->beginFrame(); + } +} + +void Seele::WindowManager::endFrame() +{ + for (auto window : windows) + { + window->endFrame(); + } +} diff --git a/src/Engine/Graphics/WindowManager.h b/src/Engine/Graphics/WindowManager.h index 2c88026..bf369d9 100644 --- a/src/Engine/Graphics/WindowManager.h +++ b/src/Engine/Graphics/WindowManager.h @@ -8,9 +8,13 @@ namespace Seele class WindowManager { public: - WindowManager(GraphicsInitializer initializer); + WindowManager(); ~WindowManager(); + void addWindow(const WindowCreateInfo& createInfo); + void beginFrame(); + void endFrame(); private: Array windows; }; + DECLARE_REF(WindowManager); } \ No newline at end of file diff --git a/src/Engine/Math/Math.h b/src/Engine/Math/Math.h index e69de29..9bce35d 100644 --- a/src/Engine/Math/Math.h +++ b/src/Engine/Math/Math.h @@ -0,0 +1,32 @@ +#include "Vector.h" +#include "Matrix.h" + +namespace Seele +{ + struct Rect + { + Rect() + : size(0, 0) + , offset(0, 0) + {} + Rect(float sizeX, float sizeY, float offsetX, float offsetY) + : size(sizeX, sizeY) + , offset(offsetX, offsetY) + {} + Rect(Vector2 size, Vector2 offset) + : size(size) + , offset(offset) + {} + bool isEmpty() const + { + return size.x == 0 || size.y == 0; + } + Vector2 size; + Vector2 offset; + }; + struct Rect3D + { + Vector3 size; + Vector3 offset; + }; +} \ No newline at end of file diff --git a/src/Engine/MinimalEngine.h b/src/Engine/MinimalEngine.h index 40d2983..858edb0 100644 --- a/src/Engine/MinimalEngine.h +++ b/src/Engine/MinimalEngine.h @@ -1,12 +1,14 @@ #pragma once #include -#include +#include #include #include +#include "Math/Math.h" #define DECLARE_REF(x) class x; \ typedef RefPtr P##x; \ - typedef UniquePtr UP##x; + typedef UniquePtr UP##x; \ + typedef WeakPtr W##x; namespace Seele { @@ -24,6 +26,10 @@ namespace Seele class RefPtr { public: + RefPtr() + { + object = nullptr; + } RefPtr(T* ptr) { object = new RefObject(ptr); @@ -37,7 +43,10 @@ namespace Seele { if (this != &other) { - object->removeRef(); + if (object != nullptr) + { + object->removeRef(); + } object = other.object; object->addRef(); } @@ -47,11 +56,17 @@ namespace Seele { object->removeRef(); } + bool operator==(const RefPtr& other) const + { + return object == other.object; + } T* operator->() { + assert(object != nullptr); return object->getHandle(); } private: + class RefObject { public: @@ -68,7 +83,10 @@ namespace Seele } ~RefObject() {} - + bool operator==(const RefObject& other) const + { + return handle == other.handle && refCount == other.refCount; + } void addRef() { refCount++; @@ -92,8 +110,37 @@ namespace Seele std::atomic_uint64_t refCount; }; RefObject* object; + template + friend class WeakPtr; + }; + template + class WeakPtr + { + public: + WeakPtr() + {} + WeakPtr(RefPtr& sharedPtr) + : pointer(sharedPtr.object) + {} + WeakPtr& operator=(WeakPtr& weakPtr) + { + pointer = weakPtr.pointer; + return *this; + } + WeakPtr& operator=(RefPtr& sharedPtr) + { + pointer = sharedPtr; + return *this; + } + RefPtr& lock() + { + RefPtr temp; + temp.object = pointer; + return temp; + } + private: + RefPtr::RefObject* pointer; }; - template class UniquePtr { diff --git a/src/Engine/main.cpp b/src/Engine/main.cpp index 66bd235..52b29c9 100644 --- a/src/Engine/main.cpp +++ b/src/Engine/main.cpp @@ -1,74 +1,10 @@ -#include - -// include headers that implement a archive in simple text format -#include -#include -#include -#include -#include -#include -#include "Graphics/Graphics.h" - -///////////////////////////////////////////////////////////// -// gps coordinate -// -// illustrates serialization for a simple type -// -class gps_position +#include "Graphics/RenderCore.h" +using namespace Seele; +int main() { -private: - friend class boost::serialization::access; - // When the class Archive corresponds to an output archive, the - // & operator is defined similar to <<. Likewise, when the class Archive - // is a type of input archive the & operator is defined similar to >>. - template - void serialize(Archive& ar, const unsigned int version) - { - ar& degrees; - ar& minutes; - ar& seconds; - } - int degrees; - int minutes; - float seconds; -public: - gps_position() {}; - gps_position(int d, int m, float s) : - degrees(d), minutes(m), seconds(s) - {} -}; - -int main() { - // create and open a character archive for output - std::ofstream ofs("filename"); - - // create class instance - const gps_position g(35, 59, 24.567f); - - // save _data to archive - { - boost::archive::text_oarchive oa(ofs); - // write class instance to archive - oa << g; - // archive and stream closed when destructors are called - } - - // ... some time later restore the class instance to its orginal state - gps_position newg; - { - // create and open an archive for input - std::ifstream ifs("filename"); - boost::archive::text_iarchive ia(ifs); - // read class state from archive - ia >> newg; - // archive and stream closed when destructors are called - } - std::cout << "Hello World! " << std::endl; - VkInstance instance; - glm::vec4 vector(0, 1, 0, 1); - Seele::Graphics& graphics = Seele::Graphics::getInstance(); - std::cout << vector.y << std::endl; - std::cout << glfwInit() << std::endl; - - return 0; + RenderCore core; + core.init(); + core.renderLoop(); + core.shutdown(); + return 0; } \ No newline at end of file