diff options
| author | Egor Yusov <egor.yusov@gmail.com> | 2018-02-07 01:03:16 +0000 |
|---|---|---|
| committer | Egor Yusov <egor.yusov@gmail.com> | 2018-02-07 01:03:16 +0000 |
| commit | 6e7bac7b34a86f4e8e1a663631d5f260c064ac0a (patch) | |
| tree | 36ad11810cdb9b722411ec9365cc3f8babbac323 /unityplugin/UnityEmulator | |
| parent | Update README.md (diff) | |
| download | DiligentEngine-6e7bac7b34a86f4e8e1a663631d5f260c064ac0a.tar.gz DiligentEngine-6e7bac7b34a86f4e8e1a663631d5f260c064ac0a.zip | |
Reworked Win32 native app implementation
Diffstat (limited to 'unityplugin/UnityEmulator')
| -rw-r--r-- | unityplugin/UnityEmulator/CMakeLists.txt | 376 | ||||
| -rw-r--r-- | unityplugin/UnityEmulator/include/UnityApp.h | 74 | ||||
| -rw-r--r-- | unityplugin/UnityEmulator/include/UnitySceneBase.h | 7 | ||||
| -rw-r--r-- | unityplugin/UnityEmulator/src/UnityApp.cpp | 202 | ||||
| -rw-r--r-- | unityplugin/UnityEmulator/src/Windows/UnityAppWin32.cpp | 82 | ||||
| -rw-r--r-- | unityplugin/UnityEmulator/src/Windows/WinMain.cpp | 343 |
6 files changed, 552 insertions, 532 deletions
diff --git a/unityplugin/UnityEmulator/CMakeLists.txt b/unityplugin/UnityEmulator/CMakeLists.txt index a3ac326..d40b110 100644 --- a/unityplugin/UnityEmulator/CMakeLists.txt +++ b/unityplugin/UnityEmulator/CMakeLists.txt @@ -1,205 +1,209 @@ -cmake_minimum_required (VERSION 3.6) - -project(UnityEmulator CXX) - -set(SOURCE - src/UnityGraphicsEmulator.cpp -) - -set(INCLUDE - include/DiligentGraphicsAdapter.h - include/ResourceStateTransitionHandler.h - include/UnityGraphicsEmulator.h - include/UnitySceneBase.h -) - -if(D3D11_SUPPORTED) - list(APPEND SOURCE src/DiligentGraphicsAdapterD3D11.cpp) - list(APPEND SOURCE src/UnityGraphicsD3D11Emulator.cpp) - list(APPEND SOURCE src/UnityGraphicsD3D11Impl.h) - list(APPEND INCLUDE include/DiligentGraphicsAdapterD3D11.h) - list(APPEND INCLUDE include/UnityGraphicsD3D11Emulator.h) -endif() - -if(D3D12_SUPPORTED) - list(APPEND SOURCE src/DiligentGraphicsAdapterD3D12.cpp) - list(APPEND SOURCE src/UnityGraphicsD3D12Emulator.cpp) - list(APPEND SOURCE src/UnityGraphicsD3D12Impl.h) - - list(APPEND INCLUDE include/DiligentGraphicsAdapterD3D12.h) - list(APPEND INCLUDE include/UnityGraphicsD3D12Emulator.h) -endif() - -if(GL_SUPPORTED OR GLES_SUPPORTED) - list(APPEND SOURCE src/UnityGraphicsGLCoreES_Emulator.cpp) - list(APPEND SOURCE src/DiligentGraphicsAdapterGL.cpp) - - list(APPEND INCLUDE include/UnityGraphicsGLCoreES_Emulator.h) - list(APPEND INCLUDE include/DiligentGraphicsAdapterGL.h) -endif() - -if(GL_SUPPORTED) - list(APPEND SOURCE src/UnityGraphicsGLCore_Impl.cpp) - list(APPEND INCLUDE src/UnityGraphicsGLCore_Impl.h) -endif() - -if(GLES_SUPPORTED) - list(APPEND SOURCE src/Android/UnityGraphicsGLESAndroid_Impl.cpp) - list(APPEND INCLUDE src/Android/UnityGraphicsGLESAndroid_Impl.h) -endif() - -if(PLATFORM_WIN32) - list(APPEND SOURCE src/Windows/WinMain.cpp) - -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 UWP_INCLUDE UWP_INCLUDE_DIR) - get_target_property(EMULATOR_SOURCE_DIR UnityEmulator SOURCE_DIR) - - set(${UWP_SOURCE} - ${EMULATOR_SOURCE_DIR}/src/UWP/App.cpp - ${EMULATOR_SOURCE_DIR}/src/UWP/DeviceResources.cpp - ${EMULATOR_SOURCE_DIR}/src/UWP/UnityEmulatorAppMain.cpp - PARENT_SCOPE - ) - - set(${UWP_INCLUDE} - ${EMULATOR_SOURCE_DIR}/src/UWP/App.h - ${EMULATOR_SOURCE_DIR}/src/UWP/DeviceResources.h - ${EMULATOR_SOURCE_DIR}/src/UWP/DirectXHelper.h - ${EMULATOR_SOURCE_DIR}/src/UWP/pch2.h - ${EMULATOR_SOURCE_DIR}/src/UWP/StepTimer.h - ${EMULATOR_SOURCE_DIR}/src/UWP/UnityEmulatorAppMain.h - PARENT_SCOPE - ) - - set(${UWP_INCLUDE_DIR} - ${EMULATOR_SOURCE_DIR}/Src/UWP - PARENT_SCOPE - ) - endfunction(get_emulator_uwp_source) - -elseif(PLATFORM_ANDROID) - list(APPEND SOURCE src/Android/AndroidMainImpl.cpp) -elseif(PLATFORM_LINUX) - list(APPEND SOURCE src/Linux/LinuxMain.cpp) -elseif(PLATFORM_MACOS) - list(APPEND SOURCE - src/MacOS/Renderer.cpp - ) - list(APPEND INCLUDE - src/MacOS/Renderer.h - ) - set_source_files_properties(src/UnityGraphicsGLCore_Impl.cpp - PROPERTIES COMPILE_FLAGS "-x objective-c++" - ) - +cmake_minimum_required (VERSION 3.6)
+
+project(UnityEmulator CXX)
+
+set(SOURCE
+ src/UnityGraphicsEmulator.cpp
+)
+
+set(INCLUDE
+ include/DiligentGraphicsAdapter.h
+ include/ResourceStateTransitionHandler.h
+ include/UnityGraphicsEmulator.h
+ include/UnityApp.h
+ include/UnitySceneBase.h
+)
+
+if(D3D11_SUPPORTED)
+ list(APPEND SOURCE src/DiligentGraphicsAdapterD3D11.cpp)
+ list(APPEND SOURCE src/UnityGraphicsD3D11Emulator.cpp)
+ list(APPEND SOURCE src/UnityGraphicsD3D11Impl.h)
+ list(APPEND INCLUDE include/DiligentGraphicsAdapterD3D11.h)
+ list(APPEND INCLUDE include/UnityGraphicsD3D11Emulator.h)
+endif()
+
+if(D3D12_SUPPORTED)
+ list(APPEND SOURCE src/DiligentGraphicsAdapterD3D12.cpp)
+ list(APPEND SOURCE src/UnityGraphicsD3D12Emulator.cpp)
+ list(APPEND SOURCE src/UnityGraphicsD3D12Impl.h)
+
+ list(APPEND INCLUDE include/DiligentGraphicsAdapterD3D12.h)
+ list(APPEND INCLUDE include/UnityGraphicsD3D12Emulator.h)
+endif()
+
+if(GL_SUPPORTED OR GLES_SUPPORTED)
+ list(APPEND SOURCE src/UnityGraphicsGLCoreES_Emulator.cpp)
+ list(APPEND SOURCE src/DiligentGraphicsAdapterGL.cpp)
+
+ list(APPEND INCLUDE include/UnityGraphicsGLCoreES_Emulator.h)
+ list(APPEND INCLUDE include/DiligentGraphicsAdapterGL.h)
+endif()
+
+if(GL_SUPPORTED)
+ list(APPEND SOURCE src/UnityGraphicsGLCore_Impl.cpp)
+ list(APPEND INCLUDE src/UnityGraphicsGLCore_Impl.h)
+endif()
+
+if(GLES_SUPPORTED)
+ list(APPEND SOURCE src/Android/UnityGraphicsGLESAndroid_Impl.cpp)
+ list(APPEND INCLUDE src/Android/UnityGraphicsGLESAndroid_Impl.h)
+endif()
+
+list(APPEND SOURCE src/UnityApp.cpp)
+
+if(PLATFORM_WIN32)
+ list(APPEND SOURCE src/Windows/UnityAppWin32.cpp)
+
+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 UWP_INCLUDE UWP_INCLUDE_DIR)
+ get_target_property(EMULATOR_SOURCE_DIR UnityEmulator SOURCE_DIR)
+
+ set(${UWP_SOURCE}
+ ${EMULATOR_SOURCE_DIR}/src/UWP/App.cpp
+ ${EMULATOR_SOURCE_DIR}/src/UWP/DeviceResources.cpp
+ ${EMULATOR_SOURCE_DIR}/src/UWP/UnityEmulatorAppMain.cpp
+ PARENT_SCOPE
+ )
+
+ set(${UWP_INCLUDE}
+ ${EMULATOR_SOURCE_DIR}/src/UWP/App.h
+ ${EMULATOR_SOURCE_DIR}/src/UWP/DeviceResources.h
+ ${EMULATOR_SOURCE_DIR}/src/UWP/DirectXHelper.h
+ ${EMULATOR_SOURCE_DIR}/src/UWP/pch2.h
+ ${EMULATOR_SOURCE_DIR}/src/UWP/StepTimer.h
+ ${EMULATOR_SOURCE_DIR}/src/UWP/UnityEmulatorAppMain.h
+ PARENT_SCOPE
+ )
+
+ set(${UWP_INCLUDE_DIR}
+ ${EMULATOR_SOURCE_DIR}/Src/UWP
+ PARENT_SCOPE
+ )
+ endfunction(get_emulator_uwp_source)
+
+elseif(PLATFORM_ANDROID)
+ list(APPEND SOURCE src/Android/AndroidMainImpl.cpp)
+elseif(PLATFORM_LINUX)
+ list(APPEND SOURCE src/Linux/LinuxMain.cpp)
+elseif(PLATFORM_MACOS)
+ list(APPEND SOURCE
+ src/MacOS/Renderer.cpp
+ )
+ list(APPEND INCLUDE
+ src/MacOS/Renderer.h
+ )
+ set_source_files_properties(src/UnityGraphicsGLCore_Impl.cpp
+ PROPERTIES COMPILE_FLAGS "-x objective-c++"
+ )
+
function(get_emulator_macos_source MAC_SOURCE MAC_INCLUDE MAC_RESOURCES MAC_INFO_PLIST MAC_INCLUDE_DIRS)
get_target_property(EMULATOR_SOURCE_DIR UnityEmulator SOURCE_DIR)
- set(${MAC_SOURCE} + set(${MAC_SOURCE}
${EMULATOR_SOURCE_DIR}/Apple/Source/main.m
- ${EMULATOR_SOURCE_DIR}/Apple/Source/Classes/OSX/WindowController.m - ${EMULATOR_SOURCE_DIR}/Apple/Source/Classes/OSX/AppDelegate.m - ${EMULATOR_SOURCE_DIR}/Apple/Source/Classes/OSX/FullscreenWindow.m + ${EMULATOR_SOURCE_DIR}/Apple/Source/Classes/OSX/WindowController.m
+ ${EMULATOR_SOURCE_DIR}/Apple/Source/Classes/OSX/AppDelegate.m
+ ${EMULATOR_SOURCE_DIR}/Apple/Source/Classes/OSX/FullscreenWindow.m
${EMULATOR_SOURCE_DIR}/Apple/Source/Classes/OSX/GLView.m
- ) - set_source_files_properties(${MAC_SOURCE} PROPERTIES - COMPILE_FLAGS "-x objective-c++" - ) - set( MAC_SOURCE ${MAC_SOURCE} PARENT_SCOPE ) - + )
+ set_source_files_properties(${MAC_SOURCE} PROPERTIES
+ COMPILE_FLAGS "-x objective-c++"
+ )
+ set( MAC_SOURCE ${MAC_SOURCE} PARENT_SCOPE )
+
set(${MAC_INCLUDE}
- ${EMULATOR_SOURCE_DIR}/Apple/Source/Classes/OSX/WindowController.h - ${EMULATOR_SOURCE_DIR}/Apple/Source/Classes/OSX/AppDelegate.h - ${EMULATOR_SOURCE_DIR}/Apple/Source/Classes/OSX/FullscreenWindow.h + ${EMULATOR_SOURCE_DIR}/Apple/Source/Classes/OSX/WindowController.h
+ ${EMULATOR_SOURCE_DIR}/Apple/Source/Classes/OSX/AppDelegate.h
+ ${EMULATOR_SOURCE_DIR}/Apple/Source/Classes/OSX/FullscreenWindow.h
${EMULATOR_SOURCE_DIR}/Apple/Source/Classes/OSX/GLView.h
PARENT_SCOPE
)
- +
set(${MAC_RESOURCES}
- ${EMULATOR_SOURCE_DIR}/Apple/Data/OSX/Base.lproj/Main.storyboard + ${EMULATOR_SOURCE_DIR}/Apple/Data/OSX/Base.lproj/Main.storyboard
${EMULATOR_SOURCE_DIR}/Apple/Data/OSX/Images.xcassets/AppIcon.appiconset/dg.icns
PARENT_SCOPE
- ) - + )
+
set(${MAC_INFO_PLIST}
${EMULATOR_SOURCE_DIR}/Apple/Data/OSX/Info.plist
PARENT_SCOPE
- ) - + )
+
set(${MAC_INCLUDE_DIRS}
${EMULATOR_SOURCE_DIR}/Apple/Source/Classes/OSX
PARENT_SCOPE
)
- endfunction() - -elseif(PLATFORM_IOS) - -else() - message(FATAL_ERROR "Unknown platform") -endif() - - -add_library(UnityEmulator STATIC ${SOURCE} ${INCLUDE}) -set_common_target_properties(UnityEmulator) - -target_include_directories(UnityEmulator -PRIVATE - ../GhostCubePlugin/PluginSource/src/Unity -PUBLIC - include -) - -if(MSVC) - target_compile_options(UnityEmulator PRIVATE -DUNICODE) -endif() - -target_link_libraries(UnityEmulator -PRIVATE - BuildSettings -PUBLIC - Common - GraphicsEngine - GraphicsTools - TargetPlatform -) - -if(GL_SUPPORTED) - target_link_libraries(UnityEmulator PRIVATE glew-static) -endif() - -if(GL_SUPPORTED OR GLES_SUPPORTED) - target_link_libraries(UnityEmulator PUBLIC GraphicsEngineOpenGL-static) -endif() - -if(D3D11_SUPPORTED) - target_link_libraries(UnityEmulator PUBLIC GraphicsEngineD3DBase GraphicsEngineD3D11-static) -endif() - -if(D3D12_SUPPORTED) - target_link_libraries(UnityEmulator PUBLIC GraphicsEngineD3DBase GraphicsEngineD3D12-static) -endif() - -if(PLATFORM_UNIVERSAL_WINDOWS) - target_link_libraries(UnityEmulator PRIVATE dxguid.lib) -elseif(PLATFORM_ANDROID) - target_link_libraries(UnityEmulator PRIVATE NDKHelper GLESv3 android PUBLIC NativeAppGlue) - target_include_directories(UnityEmulator PRIVATE - ${ANDROID_NDK}/sources/android/cpufeatures - ) -elseif(PLATFORM_MACOS) - target_include_directories(UnityEmulator PUBLIC - src/MacOS - ) -endif() - -source_group("src" FILES ${SOURCE}) -source_group("include" FILES ${INCLUDE}) - -set_target_properties(UnityEmulator PROPERTIES - FOLDER Unity -) + endfunction()
+
+elseif(PLATFORM_IOS)
+
+else()
+ message(FATAL_ERROR "Unknown platform")
+endif()
+
+
+add_library(UnityEmulator STATIC ${SOURCE} ${INCLUDE})
+set_common_target_properties(UnityEmulator)
+
+target_include_directories(UnityEmulator
+PRIVATE
+ ../GhostCubePlugin/PluginSource/src/Unity
+PUBLIC
+ include
+)
+
+if(MSVC)
+ target_compile_options(UnityEmulator PRIVATE -DUNICODE)
+endif()
+
+target_link_libraries(UnityEmulator
+PRIVATE
+ BuildSettings
+PUBLIC
+ NativeAppBase
+ Common
+ GraphicsEngine
+ GraphicsTools
+ TargetPlatform
+)
+
+if(GL_SUPPORTED)
+ target_link_libraries(UnityEmulator PRIVATE glew-static)
+endif()
+
+if(GL_SUPPORTED OR GLES_SUPPORTED)
+ target_link_libraries(UnityEmulator PUBLIC GraphicsEngineOpenGL-static)
+endif()
+
+if(D3D11_SUPPORTED)
+ target_link_libraries(UnityEmulator PUBLIC GraphicsEngineD3DBase GraphicsEngineD3D11-static)
+endif()
+
+if(D3D12_SUPPORTED)
+ target_link_libraries(UnityEmulator PUBLIC GraphicsEngineD3DBase GraphicsEngineD3D12-static)
+endif()
+
+if(PLATFORM_UNIVERSAL_WINDOWS)
+ target_link_libraries(UnityEmulator PRIVATE dxguid.lib)
+elseif(PLATFORM_ANDROID)
+ target_link_libraries(UnityEmulator PRIVATE NDKHelper GLESv3 android PUBLIC NativeAppGlue)
+ target_include_directories(UnityEmulator PRIVATE
+ ${ANDROID_NDK}/sources/android/cpufeatures
+ )
+elseif(PLATFORM_MACOS)
+ target_include_directories(UnityEmulator PUBLIC
+ src/MacOS
+ )
+endif()
+
+source_group("src" FILES ${SOURCE})
+source_group("include" FILES ${INCLUDE})
+
+set_target_properties(UnityEmulator PROPERTIES
+ FOLDER Unity
+)
diff --git a/unityplugin/UnityEmulator/include/UnityApp.h b/unityplugin/UnityEmulator/include/UnityApp.h new file mode 100644 index 0000000..c5f12e8 --- /dev/null +++ b/unityplugin/UnityEmulator/include/UnityApp.h @@ -0,0 +1,74 @@ +/* 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 Initialize(const struct NativeAppAttributes &NativeAppAttribs)override; + virtual void PlatformRender()override; + // Return true if the message has been handled and no further processing is required + virtual bool HandleNativeMessage(struct NativeMessage &msg)override { return false; } + virtual void Resize(int width, int height)override; + virtual void Update(double CurrTime, double ElapsedTime)override; + + bool LoadPlugin(); + +protected: + std::unique_ptr<UnitySceneBase> m_Scene; + Diligent::DeviceType m_DeviceType = Diligent::DeviceType::Undefined; + std::string m_AppTitle; + + class UnityGraphicsEmulator *m_GraphicsEmulator = nullptr; + std::unique_ptr<class DiligentGraphicsAdapter> m_DiligentGraphics; + std::unique_ptr<IResourceStateTransitionHandler> 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/UnitySceneBase.h b/unityplugin/UnityEmulator/include/UnitySceneBase.h index f70b502..a65de9e 100644 --- a/unityplugin/UnityEmulator/include/UnitySceneBase.h +++ b/unityplugin/UnityEmulator/include/UnitySceneBase.h @@ -48,11 +48,12 @@ public: m_WindowHeight = NewHeight; } - virtual const char* GetSceneName() = 0; + virtual const char* GetSceneName()const = 0; - virtual const char* GetPluginName() = 0; + virtual const char* GetPluginName()const = 0; - virtual void Render(UnityRenderingEvent RenderEventFunc, double CurrTime, double ElapsedTime) = 0; + virtual void Update(double CurrTime, double ElapsedTime) = 0; + virtual void Render(UnityRenderingEvent RenderEventFunc) = 0; void SetDiligentGraphicsAdapter(DiligentGraphicsAdapter *DiligentGraphics) { diff --git a/unityplugin/UnityEmulator/src/UnityApp.cpp b/unityplugin/UnityEmulator/src/UnityApp.cpp new file mode 100644 index 0000000..b95dad0 --- /dev/null +++ b/unityplugin/UnityEmulator/src/UnityApp.cpp @@ -0,0 +1,202 @@ +/* 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; + +NativeAppBase* CreateApplication() +{ + return new UnityApp(); +} + +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::Initialize(const struct NativeAppAttributes &NativeAppAttribs) +{ + switch (m_DeviceType) + { +#if D3D11_SUPPORTED + case DeviceType::D3D11: + { + auto &GraphicsD3D11Emulator = UnityGraphicsD3D11Emulator::GetInstance(); + GraphicsD3D11Emulator.CreateD3D11DeviceAndContext(); + GraphicsD3D11Emulator.CreateSwapChain(NativeAppAttribs.NativeWindowHandle, NativeAppAttribs.WindowWidth, NativeAppAttribs.WindowHeight); + m_GraphicsEmulator = &GraphicsD3D11Emulator; + auto *pDiligentAdapterD3D11 = new DiligentGraphicsAdapterD3D11(GraphicsD3D11Emulator); + m_DiligentGraphics.reset(pDiligentAdapterD3D11); + pDiligentAdapterD3D11->InitProxySwapChain(); + } + break; +#endif + +#if D3D12_SUPPORTED + case DeviceType::D3D12: + { + auto &GraphicsD3D12Emulator = UnityGraphicsD3D12Emulator::GetInstance(); + GraphicsD3D12Emulator.CreateD3D12DeviceAndCommandQueue(); + GraphicsD3D12Emulator.CreateSwapChain(NativeAppAttribs.NativeWindowHandle, NativeAppAttribs.WindowWidth, NativeAppAttribs.WindowHeight); + m_GraphicsEmulator = &GraphicsD3D12Emulator; + auto *pDiligentAdapterD3D12 = new DiligentGraphicsAdapterD3D12(GraphicsD3D12Emulator); + m_DiligentGraphics.reset(pDiligentAdapterD3D12); + pDiligentAdapterD3D12->InitProxySwapChain(); + } + break; +#endif + +#if OPENGL_SUPPORTED + case DeviceType::OpenGL: + { + auto &GraphicsGLCoreES_Emulator = UnityGraphicsGLCoreES_Emulator::GetInstance(); + GraphicsGLCoreES_Emulator.InitGLContext(NativeAppAttribs.NativeWindowHandle, 4, 4); + m_GraphicsEmulator = &GraphicsGLCoreES_Emulator; + m_DiligentGraphics.reset(new DiligentGraphicsAdapterGL(GraphicsGLCoreES_Emulator)); + } + break; +#endif + + default: + LOG_ERROR_AND_THROW("Unsupported device type"); + } + + 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(); +} + +void UnityApp::Update(double CurrTime, double ElapsedTime) +{ + m_Scene->Update(CurrTime, ElapsedTime); +} + +void UnityApp::PlatformRender() +{ + m_GraphicsEmulator->BeginFrame(); + m_DiligentGraphics->BeginFrame(); + + m_Scene->Render(RenderEventFunc); + + m_DiligentGraphics->EndFrame(); + m_GraphicsEmulator->EndFrame(); + + 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/Windows/UnityAppWin32.cpp b/unityplugin/UnityEmulator/src/Windows/UnityAppWin32.cpp new file mode 100644 index 0000000..2fe174c --- /dev/null +++ b/unityplugin/UnityEmulator/src/Windows/UnityAppWin32.cpp @@ -0,0 +1,82 @@ +/* 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. + */ + +#define NOMINMAX +#include <Windows.h> + +#include "UnityGraphicsEmulator.h" +#include "UnityApp.h" +#include "IUnityInterface.h" +#include "Errors.h" + +HMODULE g_DLLHandle; + +void* UnityApp::LoadPluginFunction(const char* FunctionName) +{ + auto Func = GetProcAddress(g_DLLHandle, FunctionName); + VERIFY( Func != nullptr, "Failed to import plugin function \"", FunctionName, "\"." ); + return Func; +} + +bool UnityApp::LoadPlugin() +{ + std::string LibName = m_Scene->GetPluginName(); +#if _WIN64 + LibName += "_64"; +#else + LibName += "_32"; +#endif + +#ifdef _DEBUG + LibName += "d"; +#else + LibName += "r"; +#endif + + LibName += ".dll"; + g_DLLHandle = LoadLibraryA( LibName.c_str() ); + if( g_DLLHandle == NULL ) + { + LOG_ERROR_MESSAGE( "Failed to load ", LibName, " library." ); + return false; + } + + UnityPluginLoad = reinterpret_cast<TUnityPluginLoad>( GetProcAddress(g_DLLHandle, "UnityPluginLoad") ); + UnityPluginUnload = reinterpret_cast<TUnityPluginUnload>( GetProcAddress(g_DLLHandle, "UnityPluginUnload") ); + GetRenderEventFunc = reinterpret_cast<TGetRenderEventFunc>( GetProcAddress(g_DLLHandle, "GetRenderEventFunc") ); + if( UnityPluginLoad == nullptr || UnityPluginUnload == nullptr || GetRenderEventFunc == nullptr ) + { + LOG_ERROR_MESSAGE( "Failed to import plugin functions from ", LibName, " library." ); + FreeLibrary( g_DLLHandle ); + return false; + } + + return true; +} + +void UnityApp::UnloadPlugin() +{ + m_GraphicsEmulator->InvokeDeviceEventCallback(kUnityGfxDeviceEventShutdown); + UnityPluginUnload(); + FreeLibrary(g_DLLHandle); +} diff --git a/unityplugin/UnityEmulator/src/Windows/WinMain.cpp b/unityplugin/UnityEmulator/src/Windows/WinMain.cpp deleted file mode 100644 index 2a202e0..0000000 --- a/unityplugin/UnityEmulator/src/Windows/WinMain.cpp +++ /dev/null @@ -1,343 +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 <memory> -#include <iomanip> - -#define NOMINMAX -#include <Windows.h> - -#include "DeviceCaps.h" -#include "Errors.h" -#include "Timer.h" - -#include "IUnityInterface.h" -#include "UnityGraphicsD3D11Emulator.h" -#include "UnityGraphicsD3D12Emulator.h" -#include "UnityGraphicsGLCoreES_Emulator.h" -#include "DiligentGraphicsAdapterD3D11.h" -#include "DiligentGraphicsAdapterD3D12.h" -#include "DiligentGraphicsAdapterGL.h" -#include "UnitySceneBase.h" -#include "StringTools.h" - -using namespace Diligent; - -LRESULT CALLBACK MessageProc(HWND, UINT, WPARAM, LPARAM); - -UnityGraphicsEmulator *g_GraphicsEmulator = nullptr; - -typedef void (UNITY_INTERFACE_API *TUnityPluginLoad)(IUnityInterfaces* unityInterfaces); -typedef void (UNITY_INTERFACE_API *TUnityPluginUnload)(); -typedef UnityRenderingEvent(UNITY_INTERFACE_API *TGetRenderEventFunc)(); - -TUnityPluginLoad UnityPluginLoad; -TUnityPluginUnload UnityPluginUnload; -TGetRenderEventFunc GetRenderEventFunc; - -std::unique_ptr<DiligentGraphicsAdapter> g_pDiligentGraphics; -std::unique_ptr<UnitySceneBase> g_pScene; - -HMODULE g_DLLHandle; - -static UINT g_WindowWidth = 1024; -static UINT g_WindowHeight = 768; - -static void* LoadPluginFunction(const char* FunctionName) -{ - auto Func = GetProcAddress(g_DLLHandle, FunctionName); - VERIFY( Func != nullptr, "Failed to import plugin function \"", FunctionName, "\"." ); - return Func; -} - -bool LoadPlugin() -{ - std::string LibName = g_pScene->GetPluginName(); -#if _WIN64 - LibName += "_64"; -#else - LibName += "_32"; -#endif - -#ifdef _DEBUG - LibName += "d"; -#else - LibName += "r"; -#endif - - LibName += ".dll"; - g_DLLHandle = LoadLibraryA( LibName.c_str() ); - if( g_DLLHandle == NULL ) - { - LOG_ERROR_MESSAGE( "Failed to load ", LibName, " library." ); - return false; - } - - UnityPluginLoad = reinterpret_cast<TUnityPluginLoad>( GetProcAddress(g_DLLHandle, "UnityPluginLoad") ); - UnityPluginUnload = reinterpret_cast<TUnityPluginUnload>( GetProcAddress(g_DLLHandle, "UnityPluginUnload") ); - GetRenderEventFunc = reinterpret_cast<TGetRenderEventFunc>( GetProcAddress(g_DLLHandle, "GetRenderEventFunc") ); - if( UnityPluginLoad == nullptr || UnityPluginUnload == nullptr || GetRenderEventFunc == nullptr ) - { - LOG_ERROR_MESSAGE( "Failed to import plugin functions from ", LibName, " library." ); - FreeLibrary( g_DLLHandle ); - return false; - } - - return true; -} - - -void UnloadPlugin() -{ - g_GraphicsEmulator->InvokeDeviceEventCallback(kUnityGfxDeviceEventShutdown); - UnityPluginUnload(); - FreeLibrary(g_DLLHandle); -} - -// Main -int WINAPI WinMain(HINSTANCE instance, HINSTANCE, LPSTR, int cmdShow) -{ -#if defined(_DEBUG) || defined(DEBUG) - _CrtSetDbgFlag( _CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF ); -#endif - - g_pScene.reset(CreateScene()); - - std::wstring Title = WidenString(g_pScene->GetSceneName()); - - DeviceType DevType = DeviceType::Undefined; - std::wstring CmdLine = GetCommandLine(); - std::wstring Key = L"mode="; - auto pos = CmdLine.find( Key ); - if( pos != std::string::npos ) - { - pos += Key.length(); - auto Val = CmdLine.substr( pos ); - if(Val == L"D3D11") - { - DevType = DeviceType::D3D11; - Title.append( L" (D3D11)" ); - } - else if(Val == L"D3D12") - { - DevType = DeviceType::D3D12; - Title.append( L" (D3D12)" ); - } - else if(Val == L"GL") - { - DevType = DeviceType::OpenGL; - Title.append( L" (OpenGL)" ); - } - else - { - LOG_ERROR("Unknown device type. Only the following types are supported: D3D11, D3D12, GL"); - return -1; - } - } - else - { - LOG_INFO_MESSAGE("Device type is not specified. Using D3D11 device"); - DevType = DeviceType::D3D11; - Title.append( L" (D3D11)" ); - } - // Register our window class - WNDCLASSEX wcex = { sizeof(WNDCLASSEX), CS_HREDRAW|CS_VREDRAW, MessageProc, - 0L, 0L, instance, NULL, NULL, NULL, NULL, L"SampleApp", NULL }; - RegisterClassEx(&wcex); - - // Create a window - UINT ClientAreaWidth = 1280; - UINT ClientAreaHeight = 1024; - RECT rc = { 0, 0, static_cast<LONG>(ClientAreaWidth), static_cast<LONG>(ClientAreaHeight) }; - AdjustWindowRect(&rc, WS_OVERLAPPEDWINDOW, FALSE); - HWND wnd = CreateWindow(L"SampleApp", Title.c_str(), - WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, - rc.right-rc.left, rc.bottom-rc.top, NULL, NULL, instance, NULL); - if (!wnd) - { - MessageBox(NULL, L"Cannot create window", L"Error", MB_OK|MB_ICONERROR); - return -1; - } - ShowWindow(wnd, cmdShow); - UpdateWindow(wnd); - - bool IsDX = false; - try - { - switch (DevType) - { - case DeviceType::D3D11: - { - auto &GraphicsD3D11Emulator = UnityGraphicsD3D11Emulator::GetInstance(); - GraphicsD3D11Emulator.CreateD3D11DeviceAndContext(); - GraphicsD3D11Emulator.CreateSwapChain(wnd, ClientAreaWidth, ClientAreaHeight); - g_GraphicsEmulator = &GraphicsD3D11Emulator; - IsDX = true; - auto *pDiligentAdapterD3D11 = new DiligentGraphicsAdapterD3D11(GraphicsD3D11Emulator); - g_pDiligentGraphics.reset(pDiligentAdapterD3D11); - pDiligentAdapterD3D11->InitProxySwapChain(); - } - break; - - case DeviceType::D3D12: - { - auto &GraphicsD3D12Emulator = UnityGraphicsD3D12Emulator::GetInstance(); - GraphicsD3D12Emulator.CreateD3D12DeviceAndCommandQueue(); - GraphicsD3D12Emulator.CreateSwapChain(wnd, ClientAreaWidth, ClientAreaHeight); - g_GraphicsEmulator = &GraphicsD3D12Emulator; - IsDX = true; - auto *pDiligentAdapterD3D12 = new DiligentGraphicsAdapterD3D12(GraphicsD3D12Emulator); - g_pDiligentGraphics.reset(pDiligentAdapterD3D12); - pDiligentAdapterD3D12->InitProxySwapChain(); - } - break; - - case DeviceType::OpenGL: - { - auto &GraphicsGLCoreES_Emulator = UnityGraphicsGLCoreES_Emulator::GetInstance(); - GraphicsGLCoreES_Emulator.InitGLContext(wnd, 4, 4); - g_GraphicsEmulator = &GraphicsGLCoreES_Emulator; - g_pDiligentGraphics.reset(new DiligentGraphicsAdapterGL(GraphicsGLCoreES_Emulator)); - } - break; - - default: - LOG_ERROR("Unsupported device type"); - return -1; - } - } - catch (std::runtime_error &err) - { - LOG_ERROR("Failed to initialize unity graphics emulator: ", err.what()); - return -1; - } - - g_pScene->SetDiligentGraphicsAdapter(g_pDiligentGraphics.get()); - g_pScene->OnGraphicsInitialized(); - if (DevType == DeviceType::D3D12) - { - UnityGraphicsD3D12Emulator::GetInstance().SetTransitionHandler(g_pScene->GetStateTransitionHandler()); - } - - if (!LoadPlugin()) - { - return -1; - } - - g_pScene->OnPluginLoad(LoadPluginFunction); - UnityPluginLoad(&g_GraphicsEmulator->GeUnityInterfaces()); - - auto RenderEventFunc = GetRenderEventFunc(); - - Timer timer; - auto PrevTime = timer.GetElapsedTime(); - double filteredFrameTime = 0.0; - - // Main message loop - MSG msg = {0}; - while (WM_QUIT != msg.message) - { - if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) - { - TranslateMessage(&msg); - DispatchMessage(&msg); - } - else - { - g_GraphicsEmulator->BeginFrame(); - g_pDiligentGraphics->BeginFrame(); - - auto CurrTime = timer.GetElapsedTime(); - auto ElapsedTime = CurrTime - PrevTime; - PrevTime = CurrTime; - - g_pScene->Render(RenderEventFunc, CurrTime, ElapsedTime); - - g_pDiligentGraphics->EndFrame(); - g_GraphicsEmulator->EndFrame(); - - g_GraphicsEmulator->Present(); - - double filterScale = 0.2; - filteredFrameTime = filteredFrameTime * (1.0 - filterScale) + filterScale * ElapsedTime; - std::wstringstream fpsCounterSS; - fpsCounterSS << " - " << std::fixed << std::setprecision(1) << filteredFrameTime * 1000; - fpsCounterSS << " ms (" << 1.0 / filteredFrameTime << " fps)"; - SetWindowText(wnd, (Title + fpsCounterSS.str()).c_str()); - } - } - - g_pScene->OnPluginUnload(); - g_pScene.reset(); - UnloadPlugin(); - - g_pDiligentGraphics.reset(); - g_GraphicsEmulator->Release(); - - return (int)msg.wParam; -} - -// Called every time the application receives a message -LRESULT CALLBACK MessageProc(HWND wnd, UINT message, WPARAM wParam, LPARAM lParam) -{ - // Send event message to AntTweakBar - switch (message) - { - case WM_PAINT: - { - PAINTSTRUCT ps; - BeginPaint(wnd, &ps); - EndPaint(wnd, &ps); - return 0; - } - case WM_SIZE: // Window size has been changed - if(g_GraphicsEmulator) - { - g_WindowWidth = LOWORD(lParam); - g_WindowHeight = HIWORD(lParam); - g_pDiligentGraphics->PreSwapChainResize(); - g_GraphicsEmulator->ResizeSwapChain(g_WindowWidth, g_WindowHeight); - g_pDiligentGraphics->PostSwapChainResize(); - g_pScene->OnWindowResize(g_WindowWidth, g_WindowHeight); - } - return 0; - case WM_CHAR: - if (wParam == VK_ESCAPE) - PostQuitMessage(0); - return 0; - case WM_DESTROY: - PostQuitMessage(0); - return 0; - default: - { - struct WindowMessageData - { - HWND hWnd; - UINT message; - WPARAM wParam; - LPARAM lParam; - }msg{wnd, message, wParam, lParam}; - return DefWindowProc(wnd, message, wParam, lParam); - } - } -} |
