From e51eeb6902ac83c97a56987f4b999027490bc1ae Mon Sep 17 00:00:00 2001 From: Egor Yusov Date: Fri, 9 Feb 2018 20:21:10 -0800 Subject: Reworked win32 and uwp cmake build rules --- unityplugin/UnityEmulator/CMakeLists.txt | 32 +-- unityplugin/UnityEmulator/include/UnityApp.h | 75 ------- unityplugin/UnityEmulator/include/UnityAppBase.h | 75 +++++++ unityplugin/UnityEmulator/src/UWP/UnityAppUWP.cpp | 12 +- unityplugin/UnityEmulator/src/UnityApp.cpp | 215 --------------------- unityplugin/UnityEmulator/src/UnityAppBase.cpp | 215 +++++++++++++++++++++ .../UnityEmulator/src/Windows/UnityAppWin32.cpp | 10 +- 7 files changed, 321 insertions(+), 313 deletions(-) delete mode 100644 unityplugin/UnityEmulator/include/UnityApp.h create mode 100644 unityplugin/UnityEmulator/include/UnityAppBase.h delete mode 100644 unityplugin/UnityEmulator/src/UnityApp.cpp create mode 100644 unityplugin/UnityEmulator/src/UnityAppBase.cpp (limited to 'unityplugin/UnityEmulator') diff --git a/unityplugin/UnityEmulator/CMakeLists.txt b/unityplugin/UnityEmulator/CMakeLists.txt index 20f2347..5e7cb42 100644 --- a/unityplugin/UnityEmulator/CMakeLists.txt +++ b/unityplugin/UnityEmulator/CMakeLists.txt @@ -10,7 +10,6 @@ set(INCLUDE include/DiligentGraphicsAdapter.h include/ResourceStateTransitionHandler.h include/UnityGraphicsEmulator.h - include/UnityApp.h include/UnitySceneBase.h ) @@ -50,42 +49,51 @@ if(GLES_SUPPORTED) endif() if(PLATFORM_WIN32) - list(APPEND SOURCE src/Windows/UnityAppWin32.cpp) - list(APPEND SOURCE src/UnityApp.cpp) - + list(APPEND SOURCE src/Windows/UnityAppWin32.cpp src/UnityAppBase.cpp) + list(APPEND INCLUDE include/UnityAppBase.h) + elseif(PLATFORM_UNIVERSAL_WINDOWS) # Windows Runtime types cannot be included into static libraries # https://social.msdn.microsoft.com/Forums/en-US/269db513-64ef-4817-a025-43954f614eb3/lnk4264-why-are-static-libraries-not-recommended-when-authoring-windows-runtime-types?forum=winappswithnativecode # So as a workaround, we will include all source files into the target app project - function(get_emulator_uwp_source UWP_SOURCE_IN UWP_INCLUDE_IN UWP_INCLUDE_DIR_IN) + function(append_emulator_uwp_source TARGET_NAME) get_target_property(EMULATOR_SOURCE_DIR UnityEmulator SOURCE_DIR) - set(UWP_SOURCE - ${UWP_SOURCE_IN} + set(EMULATOR_UWP_SOURCE ${EMULATOR_SOURCE_DIR}/src/UWP/UnityAppUWP.cpp - ${EMULATOR_SOURCE_DIR}/src/UnityApp.cpp - PARENT_SCOPE + ${EMULATOR_SOURCE_DIR}/src/UnityAppBase.cpp + ) + set(EMULATOR_UWP_INCLUDE + ${EMULATOR_SOURCE_DIR}/include/UnityAppBase.h ) - endfunction(get_emulator_uwp_source) + + target_sources(${TARGET_NAME} PRIVATE ${EMULATOR_UWP_SOURCE} ${EMULATOR_UWP_INCLUDE}) + source_group("src\\UnityEmulator" FILES ${EMULATOR_UWP_SOURCE}) + source_group("include\\UnityEmulator" FILES ${EMULATOR_UWP_INCLUDE}) + endfunction() elseif(PLATFORM_ANDROID) list(APPEND SOURCE src/Android/AndroidMainImpl.cpp - src/UnityApp.cpp + src/UnityAppBase.cpp ) + list(APPEND INCLUDE include/UnityAppBase.h) elseif(PLATFORM_LINUX) list(APPEND SOURCE src/Linux/LinuxMain.cpp - src/UnityApp.cpp + src/UnityAppBase.cpp ) + list(APPEND INCLUDE include/UnityAppBase.h) elseif(PLATFORM_MACOS) list(APPEND SOURCE src/Linux/LinuxMain.cpp src/MacOS/Renderer.cpp + src/UnityAppBase.cpp ) list(APPEND INCLUDE src/MacOS/Renderer.h + include/UnityAppBase.h ) set_source_files_properties(src/UnityGraphicsGLCore_Impl.cpp PROPERTIES COMPILE_FLAGS "-x objective-c++" diff --git a/unityplugin/UnityEmulator/include/UnityApp.h b/unityplugin/UnityEmulator/include/UnityApp.h deleted file mode 100644 index cfc25e2..0000000 --- a/unityplugin/UnityEmulator/include/UnityApp.h +++ /dev/null @@ -1,75 +0,0 @@ -/* Copyright 2015-2018 Egor Yusov - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF ANY PROPRIETARY RIGHTS. - * - * In no event and under no legal theory, whether in tort (including negligence), - * contract, or otherwise, unless required by applicable law (such as deliberate - * and grossly negligent acts) or agreed to in writing, shall any Contributor be - * liable for any damages, including any direct, indirect, special, incidental, - * or consequential damages of any character arising as a result of this License or - * out of the use or inability to use the software (including but not limited to damages - * for loss of goodwill, work stoppage, computer failure or malfunction, or any and - * all other commercial damages or losses), even if such Contributor has been advised - * of the possibility of such damages. - */ -#pragma once - -#include "NativeAppBase.h" -#include "RenderDevice.h" -#include "DeviceContext.h" -#include "RefCntAutoPtr.h" -#include "UnitySceneBase.h" -#include "IUnityGraphics.h" -#include "DiligentGraphicsAdapter.h" -#include "ResourceStateTransitionHandler.h" - -typedef void* (*TLoadPluginFunction)(const char *FunctionName); - -class UnityApp : public NativeAppBase -{ -public: - UnityApp(); - virtual ~UnityApp()override; - - virtual void ProcessCommandLine(const char *CmdLine)override; - virtual const char* GetAppTitle()const override { return m_AppTitle.c_str(); } - virtual void Render()override; - virtual void Present()override; - virtual void Resize(int width, int height)override; - virtual void Update(double CurrTime, double ElapsedTime)override; - - bool LoadPlugin(); - -protected: - virtual void InitGraphics(void *NativeWindowHandle, int WindowWidth, int WindowHeight); - virtual void InitScene(); - - std::unique_ptr m_Scene; - Diligent::DeviceType m_DeviceType = Diligent::DeviceType::Undefined; - std::string m_AppTitle; - - class UnityGraphicsEmulator *m_GraphicsEmulator = nullptr; - std::unique_ptr m_DiligentGraphics; - std::unique_ptr m_pStateTransitionHandler; - - typedef void (UNITY_INTERFACE_API *TUnityPluginLoad)(IUnityInterfaces* unityInterfaces); - typedef void (UNITY_INTERFACE_API *TUnityPluginUnload)(); - typedef UnityRenderingEvent(UNITY_INTERFACE_API *TGetRenderEventFunc)(); - - TUnityPluginLoad UnityPluginLoad = nullptr; - TUnityPluginUnload UnityPluginUnload = nullptr; - TGetRenderEventFunc GetRenderEventFunc = nullptr; - UnityRenderingEvent RenderEventFunc = nullptr; - - void UnloadPlugin(); - static void* LoadPluginFunction(const char* FunctionName); -}; - diff --git a/unityplugin/UnityEmulator/include/UnityAppBase.h b/unityplugin/UnityEmulator/include/UnityAppBase.h new file mode 100644 index 0000000..10945f1 --- /dev/null +++ b/unityplugin/UnityEmulator/include/UnityAppBase.h @@ -0,0 +1,75 @@ +/* Copyright 2015-2018 Egor Yusov + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF ANY PROPRIETARY RIGHTS. + * + * In no event and under no legal theory, whether in tort (including negligence), + * contract, or otherwise, unless required by applicable law (such as deliberate + * and grossly negligent acts) or agreed to in writing, shall any Contributor be + * liable for any damages, including any direct, indirect, special, incidental, + * or consequential damages of any character arising as a result of this License or + * out of the use or inability to use the software (including but not limited to damages + * for loss of goodwill, work stoppage, computer failure or malfunction, or any and + * all other commercial damages or losses), even if such Contributor has been advised + * of the possibility of such damages. + */ +#pragma once + +#include "NativeAppBase.h" +#include "RenderDevice.h" +#include "DeviceContext.h" +#include "RefCntAutoPtr.h" +#include "UnitySceneBase.h" +#include "IUnityGraphics.h" +#include "DiligentGraphicsAdapter.h" +#include "ResourceStateTransitionHandler.h" + +typedef void* (*TLoadPluginFunction)(const char *FunctionName); + +class UnityAppBase : public NativeAppBase +{ +public: + UnityAppBase(); + virtual ~UnityAppBase()override; + + virtual void ProcessCommandLine(const char *CmdLine)override; + virtual const char* GetAppTitle()const override { return m_AppTitle.c_str(); } + virtual void Render()override; + virtual void Present()override; + virtual void Resize(int width, int height)override; + virtual void Update(double CurrTime, double ElapsedTime)override; + + bool LoadPlugin(); + +protected: + virtual void InitGraphics(void *NativeWindowHandle, int WindowWidth, int WindowHeight); + virtual void InitScene(); + + std::unique_ptr m_Scene; + Diligent::DeviceType m_DeviceType = Diligent::DeviceType::Undefined; + std::string m_AppTitle; + + class UnityGraphicsEmulator *m_GraphicsEmulator = nullptr; + std::unique_ptr m_DiligentGraphics; + std::unique_ptr m_pStateTransitionHandler; + + typedef void (UNITY_INTERFACE_API *TUnityPluginLoad)(IUnityInterfaces* unityInterfaces); + typedef void (UNITY_INTERFACE_API *TUnityPluginUnload)(); + typedef UnityRenderingEvent(UNITY_INTERFACE_API *TGetRenderEventFunc)(); + + TUnityPluginLoad UnityPluginLoad = nullptr; + TUnityPluginUnload UnityPluginUnload = nullptr; + TGetRenderEventFunc GetRenderEventFunc = nullptr; + UnityRenderingEvent RenderEventFunc = nullptr; + + void UnloadPlugin(); + static void* LoadPluginFunction(const char* FunctionName); +}; + diff --git a/unityplugin/UnityEmulator/src/UWP/UnityAppUWP.cpp b/unityplugin/UnityEmulator/src/UWP/UnityAppUWP.cpp index a6d3c3d..8996e51 100644 --- a/unityplugin/UnityEmulator/src/UWP/UnityAppUWP.cpp +++ b/unityplugin/UnityEmulator/src/UWP/UnityAppUWP.cpp @@ -21,7 +21,7 @@ * of the possibility of such damages. */ -#include "UnityApp.h" +#include "UnityAppBase.h" #include "IUnityInterface.h" #include "UnityGraphicsD3D11Emulator.h" #include "UnityGraphicsD3D12Emulator.h" @@ -33,7 +33,7 @@ using namespace Diligent; -class UnityAppUWP final : public UnityApp +class UnityAppUWP final : public UnityAppBase { public: UnityAppUWP() @@ -60,7 +60,7 @@ public: { return; } - UnityApp::Render(); + UnityAppBase::Render(); m_bFrameReady = true; } @@ -224,14 +224,14 @@ NativeAppBase* CreateApplication() HMODULE g_DLLHandle; -void* UnityApp::LoadPluginFunction(const char* FunctionName) +void* UnityAppBase::LoadPluginFunction(const char* FunctionName) { auto Func = GetProcAddress(g_DLLHandle, FunctionName); VERIFY(Func != nullptr, "Failed to import plugin function \"", FunctionName, "\"."); return Func; } -bool UnityApp::LoadPlugin() +bool UnityAppBase::LoadPlugin() { std::string LibName = m_Scene->GetPluginName(); @@ -278,7 +278,7 @@ bool UnityApp::LoadPlugin() return true; } -void UnityApp::UnloadPlugin() +void UnityAppBase::UnloadPlugin() { m_GraphicsEmulator->InvokeDeviceEventCallback(kUnityGfxDeviceEventShutdown); UnityPluginUnload(); diff --git a/unityplugin/UnityEmulator/src/UnityApp.cpp b/unityplugin/UnityEmulator/src/UnityApp.cpp deleted file mode 100644 index c26be33..0000000 --- a/unityplugin/UnityEmulator/src/UnityApp.cpp +++ /dev/null @@ -1,215 +0,0 @@ -/* Copyright 2015-2018 Egor Yusov - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF ANY PROPRIETARY RIGHTS. - * - * In no event and under no legal theory, whether in tort (including negligence), - * contract, or otherwise, unless required by applicable law (such as deliberate - * and grossly negligent acts) or agreed to in writing, shall any Contributor be - * liable for any damages, including any direct, indirect, special, incidental, - * or consequential damages of any character arising as a result of this License or - * out of the use or inability to use the software (including but not limited to damages - * for loss of goodwill, work stoppage, computer failure or malfunction, or any and - * all other commercial damages or losses), even if such Contributor has been advised - * of the possibility of such damages. - */ - -#include "UnityApp.h" -#include "IUnityInterface.h" - -#if D3D11_SUPPORTED -# include "UnityGraphicsD3D11Emulator.h" -# include "DiligentGraphicsAdapterD3D11.h" -#endif - -#if D3D12_SUPPORTED -# include "UnityGraphicsD3D12Emulator.h" -# include "DiligentGraphicsAdapterD3D12.h" -#endif - -#if OPENGL_SUPPORTED -# include "UnityGraphicsGLCoreES_Emulator.h" -# include "DiligentGraphicsAdapterGL.h" -#endif - -#include "UnityApp.h" -#include "StringTools.h" -#include "Errors.h" - -using namespace Diligent; - -UnityApp::UnityApp() : - m_Scene(CreateScene()) -{ - m_AppTitle = m_Scene->GetSceneName(); -} - -UnityApp::~UnityApp() -{ - m_Scene->OnPluginUnload(); - m_Scene.reset(); - UnloadPlugin(); - - m_DiligentGraphics.reset(); - m_GraphicsEmulator->Release(); -} - - - -void UnityApp::ProcessCommandLine(const char *CmdLine) -{ - const auto* Key = "mode="; - const auto *pos = strstr(CmdLine, Key); - if (pos != nullptr) - { - pos += strlen(Key); - if (_stricmp(pos, "D3D11") == 0) - { - m_DeviceType = DeviceType::D3D11; - } - else if (_stricmp(pos, "D3D12") == 0) - { - m_DeviceType = DeviceType::D3D12; - } - else if (_stricmp(pos, "GL") == 0) - { - m_DeviceType = DeviceType::OpenGL; - } - else - { - LOG_ERROR_AND_THROW("Unknown device type. Only the following types are supported: D3D11, D3D12, GL"); - } - } - else - { - LOG_INFO_MESSAGE("Device type is not specified. Using D3D11 device"); - m_DeviceType = DeviceType::D3D11; - } - - switch (m_DeviceType) - { - case DeviceType::D3D11: m_AppTitle.append(" (D3D11)"); break; - case DeviceType::D3D12: m_AppTitle.append(" (D3D12)"); break; - case DeviceType::OpenGL: m_AppTitle.append(" (OpenGL)"); break; - default: UNEXPECTED("Unknown device type"); - } -} - -void UnityApp::InitGraphics(void *NativeWindowHandle, int WindowWidth, int WindowHeight) -{ - switch (m_DeviceType) - { -#if D3D11_SUPPORTED - case DeviceType::D3D11: - { - auto &GraphicsD3D11Emulator = UnityGraphicsD3D11Emulator::GetInstance(); - GraphicsD3D11Emulator.CreateD3D11DeviceAndContext(); - m_GraphicsEmulator = &GraphicsD3D11Emulator; - auto *pDiligentAdapterD3D11 = new DiligentGraphicsAdapterD3D11(GraphicsD3D11Emulator); - m_DiligentGraphics.reset(pDiligentAdapterD3D11); - if (NativeWindowHandle != nullptr) - { - GraphicsD3D11Emulator.CreateSwapChain(NativeWindowHandle, WindowWidth, WindowHeight); - pDiligentAdapterD3D11->InitProxySwapChain(); - } - } - break; -#endif - -#if D3D12_SUPPORTED - case DeviceType::D3D12: - { - auto &GraphicsD3D12Emulator = UnityGraphicsD3D12Emulator::GetInstance(); - GraphicsD3D12Emulator.CreateD3D12DeviceAndCommandQueue(); - m_GraphicsEmulator = &GraphicsD3D12Emulator; - auto *pDiligentAdapterD3D12 = new DiligentGraphicsAdapterD3D12(GraphicsD3D12Emulator); - m_DiligentGraphics.reset(pDiligentAdapterD3D12); - if (NativeWindowHandle != nullptr) - { - GraphicsD3D12Emulator.CreateSwapChain(NativeWindowHandle, WindowWidth, WindowHeight); - pDiligentAdapterD3D12->InitProxySwapChain(); - } - } - break; -#endif - -#if OPENGL_SUPPORTED - case DeviceType::OpenGL: - { - VERIFY_EXPR(NativeWindowHandle != nullptr); - auto &GraphicsGLCoreES_Emulator = UnityGraphicsGLCoreES_Emulator::GetInstance(); - GraphicsGLCoreES_Emulator.InitGLContext(NativeWindowHandle, 4, 4); - m_GraphicsEmulator = &GraphicsGLCoreES_Emulator; - m_DiligentGraphics.reset(new DiligentGraphicsAdapterGL(GraphicsGLCoreES_Emulator)); - } - break; -#endif - - default: - LOG_ERROR_AND_THROW("Unsupported device type"); - } -} - -void UnityApp::InitScene() -{ - m_Scene->SetDiligentGraphicsAdapter(m_DiligentGraphics.get()); - m_Scene->OnGraphicsInitialized(); - if (m_DeviceType == DeviceType::D3D12) - { - UnityGraphicsD3D12Emulator::GetInstance().SetTransitionHandler(m_Scene->GetStateTransitionHandler()); - } - - if (!LoadPlugin()) - { - LOG_ERROR_AND_THROW("Failed to load plugin"); - } - - m_Scene->OnPluginLoad(LoadPluginFunction); - UnityPluginLoad(&m_GraphicsEmulator->GeUnityInterfaces()); - - RenderEventFunc = GetRenderEventFunc(); - - unsigned int SCWidth = 0; - unsigned int SCHeight = 0; - m_GraphicsEmulator->GetBackBufferSize(SCWidth, SCHeight); - m_Scene->OnWindowResize(SCWidth, SCHeight); -} - -void UnityApp::Update(double CurrTime, double ElapsedTime) -{ - m_Scene->Update(CurrTime, ElapsedTime); -} - -void UnityApp::Render() -{ - m_GraphicsEmulator->BeginFrame(); - m_DiligentGraphics->BeginFrame(); - - m_Scene->Render(RenderEventFunc); - - m_DiligentGraphics->EndFrame(); - m_GraphicsEmulator->EndFrame(); -} - -void UnityApp::Present() -{ - m_GraphicsEmulator->Present(); -} - -void UnityApp::Resize(int width, int height) -{ - if (m_GraphicsEmulator) - { - m_DiligentGraphics->PreSwapChainResize(); - m_GraphicsEmulator->ResizeSwapChain(width, height); - m_DiligentGraphics->PostSwapChainResize(); - m_Scene->OnWindowResize(width, height); - } -} diff --git a/unityplugin/UnityEmulator/src/UnityAppBase.cpp b/unityplugin/UnityEmulator/src/UnityAppBase.cpp new file mode 100644 index 0000000..9a2a3df --- /dev/null +++ b/unityplugin/UnityEmulator/src/UnityAppBase.cpp @@ -0,0 +1,215 @@ +/* Copyright 2015-2018 Egor Yusov + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF ANY PROPRIETARY RIGHTS. + * + * In no event and under no legal theory, whether in tort (including negligence), + * contract, or otherwise, unless required by applicable law (such as deliberate + * and grossly negligent acts) or agreed to in writing, shall any Contributor be + * liable for any damages, including any direct, indirect, special, incidental, + * or consequential damages of any character arising as a result of this License or + * out of the use or inability to use the software (including but not limited to damages + * for loss of goodwill, work stoppage, computer failure or malfunction, or any and + * all other commercial damages or losses), even if such Contributor has been advised + * of the possibility of such damages. + */ + +#include "UnityAppBase.h" +#include "IUnityInterface.h" + +#if D3D11_SUPPORTED +# include "UnityGraphicsD3D11Emulator.h" +# include "DiligentGraphicsAdapterD3D11.h" +#endif + +#if D3D12_SUPPORTED +# include "UnityGraphicsD3D12Emulator.h" +# include "DiligentGraphicsAdapterD3D12.h" +#endif + +#if OPENGL_SUPPORTED +# include "UnityGraphicsGLCoreES_Emulator.h" +# include "DiligentGraphicsAdapterGL.h" +#endif + +#include "UnityAppBase.h" +#include "StringTools.h" +#include "Errors.h" + +using namespace Diligent; + +UnityAppBase::UnityAppBase() : + m_Scene(CreateScene()) +{ + m_AppTitle = m_Scene->GetSceneName(); +} + +UnityAppBase::~UnityAppBase() +{ + m_Scene->OnPluginUnload(); + m_Scene.reset(); + UnloadPlugin(); + + m_DiligentGraphics.reset(); + m_GraphicsEmulator->Release(); +} + + + +void UnityAppBase::ProcessCommandLine(const char *CmdLine) +{ + const auto* Key = "mode="; + const auto *pos = strstr(CmdLine, Key); + if (pos != nullptr) + { + pos += strlen(Key); + if (_stricmp(pos, "D3D11") == 0) + { + m_DeviceType = DeviceType::D3D11; + } + else if (_stricmp(pos, "D3D12") == 0) + { + m_DeviceType = DeviceType::D3D12; + } + else if (_stricmp(pos, "GL") == 0) + { + m_DeviceType = DeviceType::OpenGL; + } + else + { + LOG_ERROR_AND_THROW("Unknown device type. Only the following types are supported: D3D11, D3D12, GL"); + } + } + else + { + LOG_INFO_MESSAGE("Device type is not specified. Using D3D11 device"); + m_DeviceType = DeviceType::D3D11; + } + + switch (m_DeviceType) + { + case DeviceType::D3D11: m_AppTitle.append(" (D3D11)"); break; + case DeviceType::D3D12: m_AppTitle.append(" (D3D12)"); break; + case DeviceType::OpenGL: m_AppTitle.append(" (OpenGL)"); break; + default: UNEXPECTED("Unknown device type"); + } +} + +void UnityAppBase::InitGraphics(void *NativeWindowHandle, int WindowWidth, int WindowHeight) +{ + switch (m_DeviceType) + { +#if D3D11_SUPPORTED + case DeviceType::D3D11: + { + auto &GraphicsD3D11Emulator = UnityGraphicsD3D11Emulator::GetInstance(); + GraphicsD3D11Emulator.CreateD3D11DeviceAndContext(); + m_GraphicsEmulator = &GraphicsD3D11Emulator; + auto *pDiligentAdapterD3D11 = new DiligentGraphicsAdapterD3D11(GraphicsD3D11Emulator); + m_DiligentGraphics.reset(pDiligentAdapterD3D11); + if (NativeWindowHandle != nullptr) + { + GraphicsD3D11Emulator.CreateSwapChain(NativeWindowHandle, WindowWidth, WindowHeight); + pDiligentAdapterD3D11->InitProxySwapChain(); + } + } + break; +#endif + +#if D3D12_SUPPORTED + case DeviceType::D3D12: + { + auto &GraphicsD3D12Emulator = UnityGraphicsD3D12Emulator::GetInstance(); + GraphicsD3D12Emulator.CreateD3D12DeviceAndCommandQueue(); + m_GraphicsEmulator = &GraphicsD3D12Emulator; + auto *pDiligentAdapterD3D12 = new DiligentGraphicsAdapterD3D12(GraphicsD3D12Emulator); + m_DiligentGraphics.reset(pDiligentAdapterD3D12); + if (NativeWindowHandle != nullptr) + { + GraphicsD3D12Emulator.CreateSwapChain(NativeWindowHandle, WindowWidth, WindowHeight); + pDiligentAdapterD3D12->InitProxySwapChain(); + } + } + break; +#endif + +#if OPENGL_SUPPORTED + case DeviceType::OpenGL: + { + VERIFY_EXPR(NativeWindowHandle != nullptr); + auto &GraphicsGLCoreES_Emulator = UnityGraphicsGLCoreES_Emulator::GetInstance(); + GraphicsGLCoreES_Emulator.InitGLContext(NativeWindowHandle, 4, 4); + m_GraphicsEmulator = &GraphicsGLCoreES_Emulator; + m_DiligentGraphics.reset(new DiligentGraphicsAdapterGL(GraphicsGLCoreES_Emulator)); + } + break; +#endif + + default: + LOG_ERROR_AND_THROW("Unsupported device type"); + } +} + +void UnityAppBase::InitScene() +{ + m_Scene->SetDiligentGraphicsAdapter(m_DiligentGraphics.get()); + m_Scene->OnGraphicsInitialized(); + if (m_DeviceType == DeviceType::D3D12) + { + UnityGraphicsD3D12Emulator::GetInstance().SetTransitionHandler(m_Scene->GetStateTransitionHandler()); + } + + if (!LoadPlugin()) + { + LOG_ERROR_AND_THROW("Failed to load plugin"); + } + + m_Scene->OnPluginLoad(LoadPluginFunction); + UnityPluginLoad(&m_GraphicsEmulator->GeUnityInterfaces()); + + RenderEventFunc = GetRenderEventFunc(); + + unsigned int SCWidth = 0; + unsigned int SCHeight = 0; + m_GraphicsEmulator->GetBackBufferSize(SCWidth, SCHeight); + m_Scene->OnWindowResize(SCWidth, SCHeight); +} + +void UnityAppBase::Update(double CurrTime, double ElapsedTime) +{ + m_Scene->Update(CurrTime, ElapsedTime); +} + +void UnityAppBase::Render() +{ + m_GraphicsEmulator->BeginFrame(); + m_DiligentGraphics->BeginFrame(); + + m_Scene->Render(RenderEventFunc); + + m_DiligentGraphics->EndFrame(); + m_GraphicsEmulator->EndFrame(); +} + +void UnityAppBase::Present() +{ + m_GraphicsEmulator->Present(); +} + +void UnityAppBase::Resize(int width, int height) +{ + if (m_GraphicsEmulator) + { + m_DiligentGraphics->PreSwapChainResize(); + m_GraphicsEmulator->ResizeSwapChain(width, height); + m_DiligentGraphics->PostSwapChainResize(); + m_Scene->OnWindowResize(width, height); + } +} diff --git a/unityplugin/UnityEmulator/src/Windows/UnityAppWin32.cpp b/unityplugin/UnityEmulator/src/Windows/UnityAppWin32.cpp index 8b56b4e..d128b8c 100644 --- a/unityplugin/UnityEmulator/src/Windows/UnityAppWin32.cpp +++ b/unityplugin/UnityEmulator/src/Windows/UnityAppWin32.cpp @@ -25,13 +25,13 @@ #include #include "UnityGraphicsEmulator.h" -#include "UnityApp.h" +#include "UnityAppBase.h" #include "IUnityInterface.h" #include "Errors.h" HMODULE g_DLLHandle; -class UnityAppWin32 : public UnityApp +class UnityAppWin32 : public UnityAppBase { public: virtual void OnWindowCreated(HWND hWnd, LONG WindowWidth, LONG WindowHeight)override final @@ -46,14 +46,14 @@ NativeAppBase* CreateApplication() return new UnityAppWin32(); } -void* UnityApp::LoadPluginFunction(const char* FunctionName) +void* UnityAppBase::LoadPluginFunction(const char* FunctionName) { auto Func = GetProcAddress(g_DLLHandle, FunctionName); VERIFY( Func != nullptr, "Failed to import plugin function \"", FunctionName, "\"." ); return Func; } -bool UnityApp::LoadPlugin() +bool UnityAppBase::LoadPlugin() { std::string LibName = m_Scene->GetPluginName(); #if _WIN64 @@ -89,7 +89,7 @@ bool UnityApp::LoadPlugin() return true; } -void UnityApp::UnloadPlugin() +void UnityAppBase::UnloadPlugin() { m_GraphicsEmulator->InvokeDeviceEventCallback(kUnityGfxDeviceEventShutdown); UnityPluginUnload(); -- cgit v1.2.3