From 1b2530749ea3dc4937cdd7aa37e5a144e6d10183 Mon Sep 17 00:00:00 2001 From: Egor Yusov Date: Sun, 12 Nov 2017 20:14:15 -0800 Subject: Updated to version 2.1 --- .../PluginSource/build/UWP/EngineRoot.props | 29 +++ .../PluginSource/build/UWP/GhostCubePlugin.vcxproj | 282 +++++++++++++++++++++ .../build/UWP/GhostCubePlugin.vcxproj.filters | 6 + .../PluginSource/build/Win32/EngineRoot.props | 29 +++ .../build/Win32/GhostCubePlugin-Debug.vgdbsettings | 76 ++++++ .../Win32/GhostCubePlugin-Release.vgdbsettings | 76 ++++++ .../build/Win32/GhostCubePlugin.vcxproj | 255 +++++++++++++++++++ .../build/Win32/GhostCubePlugin.vcxproj.filters | 37 +++ .../PluginSource/build/Win32/jni/Android.mk | 80 ++++++ .../PluginSource/build/Win32/jni/Application.mk | 7 + .../Windows.Shared/GhostCubePlugin.Shared.vcxitems | 38 +++ .../GhostCubePlugin.Shared.vcxitems.filters | 39 +++ .../build/Windows.Shared/GhostCubePlugin.props | 16 ++ .../PluginSource/src/PlatformBase.h | 58 +++++ .../GhostCubePlugin/PluginSource/src/RenderAPI.cpp | 29 +++ .../GhostCubePlugin/PluginSource/src/RenderAPI.h | 51 ++++ .../PluginSource/src/RenderAPI_D3D11.cpp | 93 +++++++ .../PluginSource/src/RenderAPI_D3D12.cpp | 195 ++++++++++++++ .../PluginSource/src/RenderAPI_OpenGLCoreES.cpp | 118 +++++++++ .../PluginSource/src/RenderingPlugin.cpp | 210 +++++++++++++++ .../PluginSource/src/RenderingPlugin.def | 11 + .../PluginSource/src/SamplePlugin.cpp | 174 +++++++++++++ .../PluginSource/src/SamplePlugin.h | 20 ++ .../PluginSource/src/Unity/IUnityGraphics.h | 49 ++++ .../PluginSource/src/Unity/IUnityGraphicsD3D11.h | 9 + .../PluginSource/src/Unity/IUnityGraphicsD3D12.h | 48 ++++ .../PluginSource/src/Unity/IUnityGraphicsD3D9.h | 10 + .../PluginSource/src/Unity/IUnityGraphicsMetal.h | 47 ++++ .../PluginSource/src/Unity/IUnityInterface.h | 149 +++++++++++ 29 files changed, 2241 insertions(+) create mode 100644 unityplugin/GhostCubePlugin/PluginSource/build/UWP/EngineRoot.props create mode 100644 unityplugin/GhostCubePlugin/PluginSource/build/UWP/GhostCubePlugin.vcxproj create mode 100644 unityplugin/GhostCubePlugin/PluginSource/build/UWP/GhostCubePlugin.vcxproj.filters create mode 100644 unityplugin/GhostCubePlugin/PluginSource/build/Win32/EngineRoot.props create mode 100644 unityplugin/GhostCubePlugin/PluginSource/build/Win32/GhostCubePlugin-Debug.vgdbsettings create mode 100644 unityplugin/GhostCubePlugin/PluginSource/build/Win32/GhostCubePlugin-Release.vgdbsettings create mode 100644 unityplugin/GhostCubePlugin/PluginSource/build/Win32/GhostCubePlugin.vcxproj create mode 100644 unityplugin/GhostCubePlugin/PluginSource/build/Win32/GhostCubePlugin.vcxproj.filters create mode 100644 unityplugin/GhostCubePlugin/PluginSource/build/Win32/jni/Android.mk create mode 100644 unityplugin/GhostCubePlugin/PluginSource/build/Win32/jni/Application.mk create mode 100644 unityplugin/GhostCubePlugin/PluginSource/build/Windows.Shared/GhostCubePlugin.Shared.vcxitems create mode 100644 unityplugin/GhostCubePlugin/PluginSource/build/Windows.Shared/GhostCubePlugin.Shared.vcxitems.filters create mode 100644 unityplugin/GhostCubePlugin/PluginSource/build/Windows.Shared/GhostCubePlugin.props create mode 100644 unityplugin/GhostCubePlugin/PluginSource/src/PlatformBase.h create mode 100644 unityplugin/GhostCubePlugin/PluginSource/src/RenderAPI.cpp create mode 100644 unityplugin/GhostCubePlugin/PluginSource/src/RenderAPI.h create mode 100644 unityplugin/GhostCubePlugin/PluginSource/src/RenderAPI_D3D11.cpp create mode 100644 unityplugin/GhostCubePlugin/PluginSource/src/RenderAPI_D3D12.cpp create mode 100644 unityplugin/GhostCubePlugin/PluginSource/src/RenderAPI_OpenGLCoreES.cpp create mode 100644 unityplugin/GhostCubePlugin/PluginSource/src/RenderingPlugin.cpp create mode 100644 unityplugin/GhostCubePlugin/PluginSource/src/RenderingPlugin.def create mode 100644 unityplugin/GhostCubePlugin/PluginSource/src/SamplePlugin.cpp create mode 100644 unityplugin/GhostCubePlugin/PluginSource/src/SamplePlugin.h create mode 100644 unityplugin/GhostCubePlugin/PluginSource/src/Unity/IUnityGraphics.h create mode 100644 unityplugin/GhostCubePlugin/PluginSource/src/Unity/IUnityGraphicsD3D11.h create mode 100644 unityplugin/GhostCubePlugin/PluginSource/src/Unity/IUnityGraphicsD3D12.h create mode 100644 unityplugin/GhostCubePlugin/PluginSource/src/Unity/IUnityGraphicsD3D9.h create mode 100644 unityplugin/GhostCubePlugin/PluginSource/src/Unity/IUnityGraphicsMetal.h create mode 100644 unityplugin/GhostCubePlugin/PluginSource/src/Unity/IUnityInterface.h (limited to 'unityplugin/GhostCubePlugin/PluginSource') diff --git a/unityplugin/GhostCubePlugin/PluginSource/build/UWP/EngineRoot.props b/unityplugin/GhostCubePlugin/PluginSource/build/UWP/EngineRoot.props new file mode 100644 index 0000000..95f31c5 --- /dev/null +++ b/unityplugin/GhostCubePlugin/PluginSource/build/UWP/EngineRoot.props @@ -0,0 +1,29 @@ + + + + + ..\.. + $(ProjectRoot)\..\..\.. + $(EngineRoot)\diligentcore + $(EngineRoot)\diligenttools + + + + + + $(ProjectRoot) + + + $(TerrainRoot) + + + $(EngineRoot) + + + $(CoreRoot) + + + $(ToolsRoot) + + + \ No newline at end of file diff --git a/unityplugin/GhostCubePlugin/PluginSource/build/UWP/GhostCubePlugin.vcxproj b/unityplugin/GhostCubePlugin/PluginSource/build/UWP/GhostCubePlugin.vcxproj new file mode 100644 index 0000000..c374b65 --- /dev/null +++ b/unityplugin/GhostCubePlugin/PluginSource/build/UWP/GhostCubePlugin.vcxproj @@ -0,0 +1,282 @@ + + + + + Debug + Win32 + + + Release + Win32 + + + Debug + x64 + + + Release + x64 + + + Debug + ARM + + + Release + ARM + + + + {0ba2f2ac-6c53-4ce5-aa37-9c6ded067096} + DirectXApp + GhostCubePlugin + en-US + 14.0 + true + Windows Store + 10.0.10586.0 + 10.0.10240.0 + 10.0 + + + + DynamicLibrary + true + v140 + + + DynamicLibrary + true + v140 + + + DynamicLibrary + true + v140 + + + DynamicLibrary + false + true + v140 + true + + + DynamicLibrary + false + true + v140 + true + + + DynamicLibrary + false + true + v140 + true + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + GhostCubePlugin_TemporaryKey.pfx + + + + + + + + + + d3d12.lib;%(AdditionalDependencies) + %(AdditionalLibraryDirectories); $(VCInstallDir)\lib\store\arm; $(VCInstallDir)\lib\arm + ..\..\src\RenderingPlugin.def + + + pch.h + $(IntDir)pch.pch + $(ProjectDir);$(IntermediateOutputPath);%(AdditionalIncludeDirectories) + /bigobj %(AdditionalOptions) + WINDOWS_UWP;UNITY_METRO;_WINDLL;_DEBUG;%(PreprocessorDefinitions) + + + copy $(OutDir)$(TargetName).dll $(ProjectRoot)\..\UnityProject\Assets\Plugins\Metro\UWP\ARM\$(ProjectName).dll +copy $(OutDir)$(TargetName).pdb $(ProjectRoot)\..\UnityProject\Assets\Plugins\Metro\UWP\ARM\$(ProjectName).pdb + copying $(TargetName).dll and $(TargetName).pdb to $(ProjectRoot)\..\UnityProject\Assets\Plugins\Metro\UWP\ARM + + + + + d3d12.lib;%(AdditionalDependencies) + %(AdditionalLibraryDirectories); $(VCInstallDir)\lib\store\arm; $(VCInstallDir)\lib\arm + ..\..\src\RenderingPlugin.def + + + pch.h + $(IntDir)pch.pch + $(ProjectDir);$(IntermediateOutputPath);%(AdditionalIncludeDirectories) + /bigobj %(AdditionalOptions) + WINDOWS_UWP;UNITY_METRO;_WINDLL;NDEBUG;%(PreprocessorDefinitions) + + + copy $(OutDir)$(TargetName).dll $(ProjectRoot)\..\UnityProject\Assets\Plugins\Metro\UWP\ARM\$(ProjectName).dll +copy $(OutDir)$(TargetName).pdb $(ProjectRoot)\..\UnityProject\Assets\Plugins\Metro\UWP\ARM\$(ProjectName).pdb + copying $(TargetName).dll and $(TargetName).pdb to $(ProjectRoot)\..\UnityProject\Assets\Plugins\Metro\UWP\ARM + + + + + d3d12.lib;%(AdditionalDependencies) + %(AdditionalLibraryDirectories); $(VCInstallDir)\lib\store; $(VCInstallDir)\lib + ..\..\src\RenderingPlugin.def + + + pch.h + $(IntDir)pch.pch + $(ProjectDir);$(IntermediateOutputPath);%(AdditionalIncludeDirectories) + /bigobj %(AdditionalOptions) + WINDOWS_UWP;UNITY_METRO;_WINDLL;_DEBUG;%(PreprocessorDefinitions) + + + copy $(OutDir)$(TargetName).dll $(ProjectRoot)\..\UnityProject\Assets\Plugins\Metro\UWP\x86\$(ProjectName).dll +copy $(OutDir)$(TargetName).pdb $(ProjectRoot)\..\UnityProject\Assets\Plugins\Metro\UWP\x86\$(ProjectName).pdb + copying $(TargetName).dll and $(TargetName).pdb to $(ProjectRoot)\..\UnityProject\Assets\Plugins\Metro\UWP\x86 + + + + + d3d12.lib;%(AdditionalDependencies) + %(AdditionalLibraryDirectories); $(VCInstallDir)\lib\store; $(VCInstallDir)\lib + ..\..\src\RenderingPlugin.def + + + pch.h + $(IntDir)pch.pch + $(ProjectDir);$(IntermediateOutputPath);%(AdditionalIncludeDirectories) + /bigobj %(AdditionalOptions) + WINDOWS_UWP;UNITY_METRO;_WINDLL;NDEBUG;%(PreprocessorDefinitions) + + + copy $(OutDir)$(TargetName).dll $(ProjectRoot)\..\UnityProject\Assets\Plugins\Metro\UWP\x86\$(ProjectName).dll +copy $(OutDir)$(TargetName).pdb $(ProjectRoot)\..\UnityProject\Assets\Plugins\Metro\UWP\x86\$(ProjectName).pdb + copying $(TargetName).dll and $(TargetName).pdb to $(ProjectRoot)\..\UnityProject\Assets\Plugins\Metro\UWP\x86 + + + + + d3d12.lib;%(AdditionalDependencies) + %(AdditionalLibraryDirectories); $(VCInstallDir)\lib\store\amd64; $(VCInstallDir)\lib\amd64 + ..\..\src\RenderingPlugin.def + + + pch.h + $(IntDir)pch.pch + $(ProjectDir);$(IntermediateOutputPath);%(AdditionalIncludeDirectories) + /bigobj %(AdditionalOptions) + WINDOWS_UWP;UNITY_METRO;_WINDLL;_DEBUG;%(PreprocessorDefinitions) + + + copy $(OutDir)$(TargetName).dll $(ProjectRoot)\..\UnityProject\Assets\Plugins\Metro\UWP\x64\$(ProjectName).dll +copy $(OutDir)$(TargetName).pdb $(ProjectRoot)\..\UnityProject\Assets\Plugins\Metro\UWP\x64\$(ProjectName).pdb + copying $(TargetName).dll and $(TargetName).pdb to $(ProjectRoot)\..\UnityProject\Assets\Plugins\Metro\UWP\x64 + + + + + d3d12.lib;%(AdditionalDependencies) + %(AdditionalLibraryDirectories); $(VCInstallDir)\lib\store\amd64; $(VCInstallDir)\lib\amd64 + ..\..\src\RenderingPlugin.def + + + pch.h + $(IntDir)pch.pch + $(ProjectDir);$(IntermediateOutputPath);%(AdditionalIncludeDirectories) + /bigobj %(AdditionalOptions) + WINDOWS_UWP;UNITY_METRO;_WINDLL;NDEBUG;%(PreprocessorDefinitions) + + + copy $(OutDir)$(TargetName).dll $(ProjectRoot)\..\UnityProject\Assets\Plugins\Metro\UWP\x64\$(ProjectName).dll +copy $(OutDir)$(TargetName).pdb $(ProjectRoot)\..\UnityProject\Assets\Plugins\Metro\UWP\x64\$(ProjectName).pdb + copying $(TargetName).dll and $(TargetName).pdb to $(ProjectRoot)\..\UnityProject\Assets\Plugins\Metro\UWP\x64 + + + + + {6de3eb2b-8a56-4d27-8a39-587f7a70584c} + + + {29acd2a7-4c49-4973-9673-8f57ef37070f} + + + {1a5a1c1d-c717-4680-a929-40e6324fe048} + + + {acd6f956-ff3b-4fe9-8be3-c0eecfdbbf93} + + + {dfe6435d-cc9d-4ca1-bf4e-5640c522d815} + + + {7a1a6e2c-3952-4a57-ad30-abd92aa39c69} + + + {ff7d4949-dc3e-488b-8f78-1d32f1ca3b4c} + + + {91adc1dd-aa8e-492a-ac19-3e0dab9ad665} + + + + + + + + + + + + \ No newline at end of file diff --git a/unityplugin/GhostCubePlugin/PluginSource/build/UWP/GhostCubePlugin.vcxproj.filters b/unityplugin/GhostCubePlugin/PluginSource/build/UWP/GhostCubePlugin.vcxproj.filters new file mode 100644 index 0000000..26ca0eb --- /dev/null +++ b/unityplugin/GhostCubePlugin/PluginSource/build/UWP/GhostCubePlugin.vcxproj.filters @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/unityplugin/GhostCubePlugin/PluginSource/build/Win32/EngineRoot.props b/unityplugin/GhostCubePlugin/PluginSource/build/Win32/EngineRoot.props new file mode 100644 index 0000000..95f31c5 --- /dev/null +++ b/unityplugin/GhostCubePlugin/PluginSource/build/Win32/EngineRoot.props @@ -0,0 +1,29 @@ + + + + + ..\.. + $(ProjectRoot)\..\..\.. + $(EngineRoot)\diligentcore + $(EngineRoot)\diligenttools + + + + + + $(ProjectRoot) + + + $(TerrainRoot) + + + $(EngineRoot) + + + $(CoreRoot) + + + $(ToolsRoot) + + + \ No newline at end of file diff --git a/unityplugin/GhostCubePlugin/PluginSource/build/Win32/GhostCubePlugin-Debug.vgdbsettings b/unityplugin/GhostCubePlugin/PluginSource/build/Win32/GhostCubePlugin-Debug.vgdbsettings new file mode 100644 index 0000000..3f5b24b --- /dev/null +++ b/unityplugin/GhostCubePlugin/PluginSource/build/Win32/GhostCubePlugin-Debug.vgdbsettings @@ -0,0 +1,76 @@ + + + Debug + + + + MinGWWindowsSlash + + $(ProjectDir) + + + android-19 + true + true + bin;obj + false + false + + + + + + false + false + true + false + false + false + false + true + false + KillApp + + false + false + false + false + false + false + false + false + + false + false + main + true + + + false + false + 5039 + 5039 + 10000 + 0 + false + Ask + true + true + + + + + + + + + + + Default + + + + true + + + \ No newline at end of file diff --git a/unityplugin/GhostCubePlugin/PluginSource/build/Win32/GhostCubePlugin-Release.vgdbsettings b/unityplugin/GhostCubePlugin/PluginSource/build/Win32/GhostCubePlugin-Release.vgdbsettings new file mode 100644 index 0000000..669691f --- /dev/null +++ b/unityplugin/GhostCubePlugin/PluginSource/build/Win32/GhostCubePlugin-Release.vgdbsettings @@ -0,0 +1,76 @@ + + + Release + + + + MinGWWindowsSlash + + $(ProjectDir) + + + android-19 + false + true + bin;obj + false + false + + + + + + false + false + true + false + false + false + false + true + false + KillApp + + false + false + false + false + false + false + false + false + + false + false + main + true + + + false + false + 5039 + 5039 + 10000 + 0 + false + Ask + true + true + + + + + + + + + + + Default + + + + true + + + \ No newline at end of file diff --git a/unityplugin/GhostCubePlugin/PluginSource/build/Win32/GhostCubePlugin.vcxproj b/unityplugin/GhostCubePlugin/PluginSource/build/Win32/GhostCubePlugin.vcxproj new file mode 100644 index 0000000..4166510 --- /dev/null +++ b/unityplugin/GhostCubePlugin/PluginSource/build/Win32/GhostCubePlugin.vcxproj @@ -0,0 +1,255 @@ + + + + + Debug + ARM + + + Debug + Win32 + + + Debug + x64 + + + Release + ARM + + + Release + Win32 + + + Release + x64 + + + + + {7380f7e6-315f-4b4e-92eb-e6aeee865298} + + + {fe289cc7-15d6-4a76-b9cb-f61cab3192c9} + + + {fff8071a-db82-485b-adc3-f0b675296869} + + + {40fd58ed-aabc-4cbd-bee4-33116995a626} + + + {15e346d3-fde6-4b29-88b7-fca14dada501} + + + {052dd700-477c-4512-a7f4-b05ebef5c80e} + + + {c6014499-0cf2-43ec-a773-a4e354fb2d74} + + + {1a782154-a5bd-4a05-b837-efc77f126c84} + + + {e333476e-15e3-4d43-a4cf-8748058b3be9} + + + {8ada5f93-7a38-4ad8-b8f5-1ffd4d4f630c} + + + {58f32677-436b-412a-bbf8-2b1310d82cd8} + + + + + + + + + + true + true + true + true + + + true + true + true + true + + + true + true + true + true + + + true + true + true + true + + + + {EDA960FC-AA6C-44B3-8ABB-90765DA4D516} + Win32Proj + GhostCubePlugin + 10.0.10586.0 + + + + DynamicLibrary + true + v140 + Unicode + + + DynamicLibrary + true + v140 + Unicode + + + DynamicLibrary + false + v140 + true + Unicode + + + DynamicLibrary + false + v140 + true + Unicode + + + v120 + Makefile + + + v120 + Makefile + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Level3 + Disabled + WIN32;_DEBUG;_LIB;%(PreprocessorDefinitions) + + + Windows + true + ..\..\src\RenderingPlugin.def + dxgi.lib;d3d12.lib;d3d11.lib;d3dcompiler.lib;opengl32.lib;%(AdditionalDependencies) + + + copy $(OutDir)$(TargetName).dll $(ProjectRoot)\..\UnityProject\Assets\Plugins\x86\$(ProjectName).dll +copy $(OutDir)$(TargetName).pdb $(ProjectRoot)\..\UnityProject\Assets\Plugins\x86\$(ProjectName).pdb + copying $(TargetName).dll and $(TargetName).pdb to $(ProjectRoot)\..\UnityProject\Assets\Plugins\x86 + + + + + Level3 + Disabled + WIN32;_DEBUG;_LIB;%(PreprocessorDefinitions) + + + Windows + true + ..\..\src\RenderingPlugin.def + dxgi.lib;d3d12.lib;d3d11.lib;d3dcompiler.lib;opengl32.lib;%(AdditionalDependencies) + + + copy $(OutDir)$(TargetName).dll $(ProjectRoot)\..\UnityProject\Assets\Plugins\x86_64\$(ProjectName).dll +copy $(OutDir)$(TargetName).pdb $(ProjectRoot)\..\UnityProject\Assets\Plugins\x86_64\$(ProjectName).pdb + copying $(TargetName).dll and $(TargetName).pdb to $(ProjectRoot)\..\UnityProject\Assets\Plugins\x86_64 + + + + + Level3 + MaxSpeed + true + true + WIN32;NDEBUG;_LIB;%(PreprocessorDefinitions) + + + Windows + true + true + true + ..\..\src\RenderingPlugin.def + dxgi.lib;d3d12.lib;d3d11.lib;d3dcompiler.lib;opengl32.lib;%(AdditionalDependencies) + + + copy $(OutDir)$(TargetName).dll $(ProjectRoot)\..\UnityProject\Assets\Plugins\x86\$(ProjectName).dll +copy $(OutDir)$(TargetName).pdb $(ProjectRoot)\..\UnityProject\Assets\Plugins\x86\$(ProjectName).pdb + copying $(TargetName).dll and $(TargetName).pdb to $(ProjectRoot)\..\UnityProject\Assets\Plugins\x86 + + + + + Level3 + MaxSpeed + true + true + WIN32;NDEBUG;_LIB;%(PreprocessorDefinitions) + + + Windows + true + true + true + ..\..\src\RenderingPlugin.def + dxgi.lib;d3d12.lib;d3d11.lib;d3dcompiler.lib;opengl32.lib;%(AdditionalDependencies) + + + copy $(OutDir)$(TargetName).dll $(ProjectRoot)\..\UnityProject\Assets\Plugins\x86_64\$(ProjectName).dll +copy $(OutDir)$(TargetName).pdb $(ProjectRoot)\..\UnityProject\Assets\Plugins\x86_64\$(ProjectName).pdb + copying $(TargetName).dll and $(TargetName).pdb to $(ProjectRoot)\..\UnityProject\Assets\Plugins\x86_64 + + + + + + \ No newline at end of file diff --git a/unityplugin/GhostCubePlugin/PluginSource/build/Win32/GhostCubePlugin.vcxproj.filters b/unityplugin/GhostCubePlugin/PluginSource/build/Win32/GhostCubePlugin.vcxproj.filters new file mode 100644 index 0000000..d6b075f --- /dev/null +++ b/unityplugin/GhostCubePlugin/PluginSource/build/Win32/GhostCubePlugin.vcxproj.filters @@ -0,0 +1,37 @@ + + + + + + Android\jni + + + Android\jni + + + + + {11b4220e-29bf-4572-bee2-1fd8d75f0c48} + + + {f09afa4f-3ec1-407b-acef-eeded2cb895d} + + + {c3a3aa9e-0f29-4329-92ef-929a34b932fb} + + + + + Android\src + + + Android\src + + + Android\src + + + Android\src + + + \ No newline at end of file diff --git a/unityplugin/GhostCubePlugin/PluginSource/build/Win32/jni/Android.mk b/unityplugin/GhostCubePlugin/PluginSource/build/Win32/jni/Android.mk new file mode 100644 index 0000000..37ea10d --- /dev/null +++ b/unityplugin/GhostCubePlugin/PluginSource/build/Win32/jni/Android.mk @@ -0,0 +1,80 @@ +# Android NDK project makefile autogenerated by Premake + +# Preamble +DEPENDENCY_PATH := $(call my-dir) +LOCAL_PATH := $(abspath $(DEPENDENCY_PATH)) +include $(CLEAR_VARS) + + +# Project configuration +LOCAL_MODULE := GhostCubePlugin +LOCAL_CFLAGS := -std=c++11 -DUSE_GL3_STUB=0 -DUNITY_ANDROID -fvisibility=hidden +LOCAL_CPP_FEATURES := exceptions rtti +LOCAL_STATIC_LIBRARIES += GraphicsEngineOpenGL_static-prebuilt GraphicsEngine-prebuilt HLSL2GLSLConverterLib-prebuilt AndroidPlatform-prebuilt BasicPlatform-prebuilt GraphicsTools-prebuilt Common-prebuilt +LOCAL_LDLIBS := -lGLESv3 -lEGL -llog -landroid + +# Include paths +PROJECT_ROOT := $(LOCAL_PATH)/../../.. +ENGINE_ROOT := $(PROJECT_ROOT)/../../.. +CORE_ROOT := $(ENGINE_ROOT)/diligentcore +LOCAL_C_INCLUDES := $(PROJECT_ROOT)/include $(PROJECT_ROOT)/interface + +# Source files +#VisualGDBAndroid: AutoUpdateSourcesInNextLine +LOCAL_SRC_FILES := ../../../src/RenderAPI.cpp ../../../src/RenderAPI_OpenGLCoreES.cpp ../../../src/RenderingPlugin.cpp ../../../src/SamplePlugin.cpp + +#VisualGDBAndroid: VSExcludeListLocation +VISUALGDB_VS_EXCLUDED_FILES_Release := ../../../src/DLLMain.cpp ../../../src/GLContextWindows.cpp +VISUALGDB_VS_EXCLUDED_FILES_Debug := +LOCAL_SRC_FILES := $(filter-out $(VISUALGDB_VS_EXCLUDED_FILES_$(VGDB_VSCONFIG)),$(LOCAL_SRC_FILES)) + +include $(BUILD_SHARED_LIBRARY) + +include $(CLEAR_VARS) +# Declare pre-built Common library +LOCAL_MODULE := Common-prebuilt +LOCAL_EXPORT_C_INCLUDES := $(CORE_ROOT)/Common/include $(CORE_ROOT)/Common/interface +LOCAL_SRC_FILES := $(CORE_ROOT)/Common/build/Win32/obj/local/$(TARGET_ARCH_ABI)/libCommon.a +include $(PREBUILT_STATIC_LIBRARY) + + +include $(CLEAR_VARS) +# Declare pre-built AndroidPlatform library +LOCAL_MODULE := AndroidPlatform-prebuilt +LOCAL_SRC_FILES := $(CORE_ROOT)/Platforms/Android/build/Windows/obj/local/$(TARGET_ARCH_ABI)/libAndroidPlatform.a +LOCAL_EXPORT_C_INCLUDES := $(CORE_ROOT)/Platforms/interface +include $(PREBUILT_STATIC_LIBRARY) + +include $(CLEAR_VARS) +# Declare pre-built BasicPlatform library +LOCAL_MODULE := BasicPlatform-prebuilt +LOCAL_SRC_FILES := $(CORE_ROOT)/Platforms/Basic/build/Win32/obj/local/$(TARGET_ARCH_ABI)/libBasicPlatform.a +include $(PREBUILT_STATIC_LIBRARY) + +include $(CLEAR_VARS) +# Declare pre-built GraphicsEngine library +LOCAL_MODULE := GraphicsEngine-prebuilt +LOCAL_SRC_FILES := $(CORE_ROOT)/Graphics/GraphicsEngine/build/Win32/obj/local/$(TARGET_ARCH_ABI)/libGraphicsEngine.a +LOCAL_EXPORT_C_INCLUDES := $(CORE_ROOT)/Graphics/GraphicsEngine/include $(CORE_ROOT)/Graphics/GraphicsEngine/interface +include $(PREBUILT_STATIC_LIBRARY) + +include $(CLEAR_VARS) +# Declare pre-built Graphics Tools library +LOCAL_MODULE := GraphicsTools-prebuilt +LOCAL_SRC_FILES := $(CORE_ROOT)/Graphics/GraphicsTools/build/Win32/obj/local/$(TARGET_ARCH_ABI)/libGraphicsTools.a +LOCAL_EXPORT_C_INCLUDES := $(CORE_ROOT)/Graphics/GraphicsTools/include +include $(PREBUILT_STATIC_LIBRARY) + +include $(CLEAR_VARS) +# Declare pre-built HLSL2GLSLConverterLib library +LOCAL_MODULE := HLSL2GLSLConverterLib-prebuilt +LOCAL_SRC_FILES := $(CORE_ROOT)/Graphics/HLSL2GLSLConverterLib/build/Win32/obj/local/$(TARGET_ARCH_ABI)/libHLSL2GLSLConverterLib.a +LOCAL_EXPORT_C_INCLUDES := $(CORE_ROOT)/Graphics/HLSL2GLSLConverterLib/include $(CORE_ROOT)/Graphics/HLSL2GLSLConverterLib/interface +include $(PREBUILT_STATIC_LIBRARY) + +include $(CLEAR_VARS) +# Declare pre-built GraphicsEngineOpenGL library +LOCAL_MODULE := GraphicsEngineOpenGL_static-prebuilt +LOCAL_SRC_FILES := $(CORE_ROOT)/Graphics/GraphicsEngineOpenGL/build/Win32/obj/local/$(TARGET_ARCH_ABI)/libGraphicsEngineOpenGL.a +LOCAL_EXPORT_C_INCLUDES := $(CORE_ROOT)/Graphics/GraphicsEngineOpenGL/interface +include $(PREBUILT_STATIC_LIBRARY) diff --git a/unityplugin/GhostCubePlugin/PluginSource/build/Win32/jni/Application.mk b/unityplugin/GhostCubePlugin/PluginSource/build/Win32/jni/Application.mk new file mode 100644 index 0000000..2a28528 --- /dev/null +++ b/unityplugin/GhostCubePlugin/PluginSource/build/Win32/jni/Application.mk @@ -0,0 +1,7 @@ +# Generated by VisualGDB + +DEPENDENCY_PATH := $(call my-dir) +LOCAL_PATH := $(abspath $(DEPENDENCY_PATH)) +include $(LOCAL_PATH)/../../../../../../diligentcore/Common/make/AppCommon.mk + +APP_MODULES := GhostCubePlugin \ No newline at end of file diff --git a/unityplugin/GhostCubePlugin/PluginSource/build/Windows.Shared/GhostCubePlugin.Shared.vcxitems b/unityplugin/GhostCubePlugin/PluginSource/build/Windows.Shared/GhostCubePlugin.Shared.vcxitems new file mode 100644 index 0000000..6cda2ea --- /dev/null +++ b/unityplugin/GhostCubePlugin/PluginSource/build/Windows.Shared/GhostCubePlugin.Shared.vcxitems @@ -0,0 +1,38 @@ + + + + $(MSBuildAllProjects);$(MSBuildThisFileFullPath) + true + {27b0c157-0a7a-46b0-aed3-919e7af5ef4b} + TerrainCore + GhostCubePlugin.Shared + 248F659F-DAC5-46E8-AC09-60EC9FC95053 + + + + %(AdditionalIncludeDirectories);$(MSBuildThisFileDirectory) + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/unityplugin/GhostCubePlugin/PluginSource/build/Windows.Shared/GhostCubePlugin.Shared.vcxitems.filters b/unityplugin/GhostCubePlugin/PluginSource/build/Windows.Shared/GhostCubePlugin.Shared.vcxitems.filters new file mode 100644 index 0000000..faac556 --- /dev/null +++ b/unityplugin/GhostCubePlugin/PluginSource/build/Windows.Shared/GhostCubePlugin.Shared.vcxitems.filters @@ -0,0 +1,39 @@ + + + + + {1cabdf31-0e7d-4669-8f9a-fe963ebfd442} + + + + + Unity + + + Unity + + + Unity + + + Unity + + + Unity + + + Unity + + + + + + + + + + + + + + \ No newline at end of file diff --git a/unityplugin/GhostCubePlugin/PluginSource/build/Windows.Shared/GhostCubePlugin.props b/unityplugin/GhostCubePlugin/PluginSource/build/Windows.Shared/GhostCubePlugin.props new file mode 100644 index 0000000..343679b --- /dev/null +++ b/unityplugin/GhostCubePlugin/PluginSource/build/Windows.Shared/GhostCubePlugin.props @@ -0,0 +1,16 @@ + + + + + + $(CoreRoot)\Graphics\GraphicsEngine\interface;$(CoreRoot)\Graphics\GraphicsEngineD3D11\interface;$(CoreRoot)\Graphics\GraphicsEngineD3D12\interface;$(CoreRoot)\Graphics\GraphicsEngineOpenGL\interface;$(CoreRoot)\Graphics\HLSL2GLSLConverterLib\interface;$(CoreRoot)\Common\include;$(CoreRoot)\Common\interface;$(CoreRoot)\Platforms\interface;$(CoreRoot)\Graphics\GraphicsTools\include;$(IncludePath) + $(SolutionDir)\bin\DLLs\$(PlatformShortName)\ + $(ProjectName)_$(ConfigSuffix) + + + + NotUsing + + + + \ No newline at end of file diff --git a/unityplugin/GhostCubePlugin/PluginSource/src/PlatformBase.h b/unityplugin/GhostCubePlugin/PluginSource/src/PlatformBase.h new file mode 100644 index 0000000..b691136 --- /dev/null +++ b/unityplugin/GhostCubePlugin/PluginSource/src/PlatformBase.h @@ -0,0 +1,58 @@ +#pragma once + +// Standard base includes, defines that indicate our current platform, etc. + +#include + + +// Which platform we are on? +// UNITY_WIN - Windows (regular win32) +// UNITY_OSX - Mac OS X +// UNITY_LINUX - Linux +// UNITY_IPHONE - iOS +// UNITY_ANDROID - Android +// UNITY_METRO - WSA or UWP +// UNITY_WEBGL - WebGL +#if _MSC_VER + #define UNITY_WIN 1 +#elif defined(__APPLE__) + #if defined(__arm__) || defined(__arm64__) + #define UNITY_IPHONE 1 + #else + #define UNITY_OSX 1 + #endif +#elif defined(UNITY_METRO) || defined(UNITY_ANDROID) || defined(UNITY_LINUX) || defined(UNITY_WEBGL) + // these are defined externally +#elif defined(__EMSCRIPTEN__) + // this is already defined in Unity 5.6 + #define UNITY_WEBGL 1 +#else + #error "Unknown platform!" +#endif + + + +// Which graphics device APIs we possibly support? +#if UNITY_METRO + #define SUPPORT_D3D11 1 + #if WINDOWS_UWP + #define SUPPORT_D3D12 1 + #endif +#elif UNITY_WIN + #define SUPPORT_D3D11 1 + #define SUPPORT_D3D12 1 + #define SUPPORT_OPENGL_UNIFIED 1 + #define SUPPORT_OPENGL_CORE 1 +#elif UNITY_IPHONE || UNITY_ANDROID || UNITY_WEBGL + #define SUPPORT_OPENGL_UNIFIED 1 + #define SUPPORT_OPENGL_ES 1 +#elif UNITY_OSX || UNITY_LINUX + #define SUPPORT_OPENGL_LEGACY 1 + #define SUPPORT_OPENGL_UNIFIED 1 + #define SUPPORT_OPENGL_CORE 1 +#endif + +#if UNITY_IPHONE || UNITY_OSX + #define SUPPORT_METAL 1 +#endif + diff --git a/unityplugin/GhostCubePlugin/PluginSource/src/RenderAPI.cpp b/unityplugin/GhostCubePlugin/PluginSource/src/RenderAPI.cpp new file mode 100644 index 0000000..18278e7 --- /dev/null +++ b/unityplugin/GhostCubePlugin/PluginSource/src/RenderAPI.cpp @@ -0,0 +1,29 @@ +#include "RenderAPI.h" +#include "PlatformBase.h" +#include "Unity/IUnityGraphics.h" +#include "GraphicsUtilities.h" + +using namespace Diligent; + +void RenderAPI::CreateTextureViews(Diligent::ITexture *pRenderTarget, Diligent::ITexture *pDepthBuffer) +{ + m_RTV.Release(); + m_DSV.Release(); + { + const auto &RTDesc = pRenderTarget->GetDesc(); + TextureViewDesc RTVDesc; + RTVDesc.ViewType = TEXTURE_VIEW_RENDER_TARGET; + m_RenderTargetFormat = GetDefaultTextureViewFormat(RTDesc, TEXTURE_VIEW_RENDER_TARGET); + RTVDesc.Format = m_RenderTargetFormat; + pRenderTarget->CreateView(RTVDesc, &m_RTV); + } + + { + const auto &TexDesc = pDepthBuffer->GetDesc(); + TextureViewDesc DSVDesc; + DSVDesc.ViewType = TEXTURE_VIEW_DEPTH_STENCIL; + m_DepthBufferFormat = GetDefaultTextureViewFormat(TexDesc, TEXTURE_VIEW_DEPTH_STENCIL); + DSVDesc.Format = m_DepthBufferFormat; + pDepthBuffer->CreateView(DSVDesc, &m_DSV); + } +} diff --git a/unityplugin/GhostCubePlugin/PluginSource/src/RenderAPI.h b/unityplugin/GhostCubePlugin/PluginSource/src/RenderAPI.h new file mode 100644 index 0000000..8d80f6e --- /dev/null +++ b/unityplugin/GhostCubePlugin/PluginSource/src/RenderAPI.h @@ -0,0 +1,51 @@ +#pragma once + +#include "Unity/IUnityGraphics.h" +#include "RenderDevice.h" +#include "DeviceContext.h" +#include "RefCntAutoPtr.h" + +struct IUnityInterfaces; + +class RenderAPI +{ +public: + virtual ~RenderAPI() { } + + // Process general event like initialization, shutdown, device loss/reset etc. + virtual void ProcessDeviceEvent(UnityGfxDeviceEventType type, IUnityInterfaces* interfaces) = 0; + + virtual void AttachToNativeRenderTexture(void *nativeRenderTargetHandle, void *nativeDepthTextureHandle) = 0; + + // Is the API using "reversed" (1.0 at near plane, 0.0 at far plane) depth buffer? + // Reversed Z is used on modern platforms, and improves depth buffer precision. + virtual bool GetUsesReverseZ() = 0; + + virtual void BeginRendering() {} + + virtual void EndRendering() + { + if (m_Context) + m_Context->InvalidateState(); + } + + virtual bool IsDX() = 0; + + Diligent::IRenderDevice *GetDevice() { return m_Device; } + Diligent::IDeviceContext *GetDeviceContext() { return m_Context; } + + Diligent::TEXTURE_FORMAT GetRenderTargetFormat()const { return m_RenderTargetFormat; }; + Diligent::TEXTURE_FORMAT GetDepthBufferFormat()const { return m_DepthBufferFormat; }; + + void CreateTextureViews(Diligent::ITexture *pRenderTarget, Diligent::ITexture *pDepthBuffer); + +protected: + Diligent::RefCntAutoPtr m_Device; + Diligent::RefCntAutoPtr m_Context; + + Diligent::RefCntAutoPtr m_RTV; + Diligent::RefCntAutoPtr m_DSV; + + Diligent::TEXTURE_FORMAT m_RenderTargetFormat = Diligent::TEX_FORMAT_UNKNOWN; + Diligent::TEXTURE_FORMAT m_DepthBufferFormat = Diligent::TEX_FORMAT_UNKNOWN; +}; diff --git a/unityplugin/GhostCubePlugin/PluginSource/src/RenderAPI_D3D11.cpp b/unityplugin/GhostCubePlugin/PluginSource/src/RenderAPI_D3D11.cpp new file mode 100644 index 0000000..85c81ba --- /dev/null +++ b/unityplugin/GhostCubePlugin/PluginSource/src/RenderAPI_D3D11.cpp @@ -0,0 +1,93 @@ +#include "PlatformBase.h" + +// Direct3D 11 implementation of RenderAPI. + +#if SUPPORT_D3D11 + +#include +#include + +#include "RenderAPI.h" +#include "Unity/IUnityGraphicsD3D11.h" +#include "RenderDeviceFactoryD3D11.h" +#include "RenderDeviceD3D11.h" + +using namespace Diligent; + + +class RenderAPI_D3D11 : public RenderAPI +{ +public: + virtual void ProcessDeviceEvent(UnityGfxDeviceEventType type, IUnityInterfaces* interfaces)override final; + + virtual bool GetUsesReverseZ()override final { return (int)m_d3d11Device->GetFeatureLevel() >= (int)D3D_FEATURE_LEVEL_10_0; } + + virtual bool IsDX()override final { return true; } + + virtual void BeginRendering()override final; + + virtual void AttachToNativeRenderTexture(void *nativeRenderTargetHandle, void *nativeDepthTextureHandle)override final; + + +private: + ID3D11Device* m_d3d11Device = nullptr; +}; + + +RenderAPI* CreateRenderAPI_D3D11() +{ + return new RenderAPI_D3D11(); +} + +void RenderAPI_D3D11::ProcessDeviceEvent(UnityGfxDeviceEventType type, IUnityInterfaces* interfaces) +{ + switch (type) + { + case kUnityGfxDeviceEventInitialize: + { + IUnityGraphicsD3D11* d3d = interfaces->Get(); + m_d3d11Device = d3d->GetDevice(); + CComPtr d3d11ImmediateContext; + m_d3d11Device->GetImmediateContext(&d3d11ImmediateContext); + auto *pFactoryD3d11 = GetEngineFactoryD3D11(); + EngineD3D11Attribs Attribs; + pFactoryD3d11->AttachToD3D11Device(m_d3d11Device, d3d11ImmediateContext, Attribs, &m_Device, &m_Context, 0); + break; + } + + case kUnityGfxDeviceEventShutdown: + m_Context.Release(); + m_Device.Release(); + break; + } +} + + +void RenderAPI_D3D11::BeginRendering() +{ + ITextureView *RTVs[] = { m_RTV }; + m_Context->SetRenderTargets(1, RTVs, m_DSV); +} + +void RenderAPI_D3D11::AttachToNativeRenderTexture(void *nativeRenderTargetHandle, void *nativeDepthTextureHandle) +{ + m_RTV.Release(); + m_DSV.Release(); + + if (nativeRenderTargetHandle != nullptr && nativeDepthTextureHandle != nullptr) + { + RefCntAutoPtr pDeviceD3D11(m_Device, IID_RenderDeviceD3D11); + + auto *pd3d11RenderTarget = reinterpret_cast(nativeRenderTargetHandle); + RefCntAutoPtr pRenderTarget; + pDeviceD3D11->CreateTextureFromD3DResource(pd3d11RenderTarget, &pRenderTarget); + + auto *pd3d11DepthBuffer = reinterpret_cast(nativeDepthTextureHandle); + RefCntAutoPtr pDepthBuffer; + pDeviceD3D11->CreateTextureFromD3DResource(pd3d11DepthBuffer, &pDepthBuffer); + + CreateTextureViews(pRenderTarget, pDepthBuffer); + } +} + +#endif // #if SUPPORT_D3D11 diff --git a/unityplugin/GhostCubePlugin/PluginSource/src/RenderAPI_D3D12.cpp b/unityplugin/GhostCubePlugin/PluginSource/src/RenderAPI_D3D12.cpp new file mode 100644 index 0000000..a29c492 --- /dev/null +++ b/unityplugin/GhostCubePlugin/PluginSource/src/RenderAPI_D3D12.cpp @@ -0,0 +1,195 @@ +#include "PlatformBase.h" + +// Direct3D 12 implementation of RenderAPI. + +#if SUPPORT_D3D12 + +#include + +#define NOMINMAX +#include + +#include "RenderAPI.h" +#include "Unity/IUnityGraphicsD3D12.h" +#include "RenderDeviceFactoryD3D12.h" +#include "RenderDeviceD3D12.h" +#include "CommandQueueD3D12.h" +#include "TextureD3D12.h" +#include "ObjectBase.h" +#include "DefaultRawMemoryAllocator.h" + +using namespace Diligent; + +class UnityCommandQueueImpl : public ObjectBase +{ +public: + using TBase = ObjectBase; + UnityCommandQueueImpl(IReferenceCounters *pRefCounters, IUnityGraphicsD3D12v2* pUnityGraphicsD3D12) : + TBase(pRefCounters), + m_pUnityGraphicsD3D12(pUnityGraphicsD3D12), + m_WaitForGPUEventHandle( CreateEvent(nullptr, false, false, nullptr) ) + { + VERIFY_EXPR(m_WaitForGPUEventHandle != INVALID_HANDLE_VALUE); + } + + ~UnityCommandQueueImpl() + { + CloseHandle(m_WaitForGPUEventHandle); + } + + IMPLEMENT_QUERY_INTERFACE_IN_PLACE( IID_CommandQueueD3D12, TBase ) + + // Returns the fence value that will be signaled next time + virtual UINT64 GetNextFenceValue()override final + { + return m_pUnityGraphicsD3D12->GetNextFrameFenceValue(); + } + + // Executes a given command list + virtual UINT64 ExecuteCommandList(ID3D12GraphicsCommandList* commandList)override final + { + auto NextFenceValue = m_pUnityGraphicsD3D12->GetNextFrameFenceValue(); + m_CurrentFenceValue = m_pUnityGraphicsD3D12->ExecuteCommandList(commandList, static_cast(m_ResourcesToTransition.size()), m_ResourcesToTransition.empty() ? nullptr : m_ResourcesToTransition.data()); + VERIFY(m_CurrentFenceValue >= NextFenceValue, "Current fence value returned by ExecuteCommandList() is less than the next fence value previously queried through GetNextFrameFenceValue()") + m_ResourcesToTransition.clear(); + return std::max(m_CurrentFenceValue, NextFenceValue); + } + + // Returns D3D12 command queue. May return null if queue is anavailable + virtual ID3D12CommandQueue* GetD3D12CommandQueue() + { + return nullptr; + } + + // Returns value of the last completed fence + virtual Uint64 GetCompletedFenceValue() + { + return m_pUnityGraphicsD3D12->GetFrameFence()->GetCompletedValue(); + } + + // Blocks execution until all pending GPU commands are complete + virtual void IdleGPU() + { + if (m_CurrentFenceValue < GetCompletedFenceValue()) + { + auto d3d12Fence = m_pUnityGraphicsD3D12->GetFrameFence(); + d3d12Fence->SetEventOnCompletion(m_CurrentFenceValue, m_WaitForGPUEventHandle); + WaitForSingleObject(m_WaitForGPUEventHandle, INFINITE); + VERIFY(GetCompletedFenceValue() >= m_CurrentFenceValue, "Unexpected signaled fence value"); + } + } + + void TransitionResource(const UnityGraphicsD3D12ResourceState &ResourceState) + { + m_ResourcesToTransition.push_back(ResourceState); + } + +private: + IUnityGraphicsD3D12v2* const m_pUnityGraphicsD3D12; + HANDLE m_WaitForGPUEventHandle = {}; + UINT64 m_CurrentFenceValue = 0; + std::vector m_ResourcesToTransition; +}; + + +class RenderAPI_D3D12 : public RenderAPI +{ +public: + virtual void ProcessDeviceEvent(UnityGfxDeviceEventType type, IUnityInterfaces* interfaces)override final; + + virtual bool GetUsesReverseZ()override final { return true; } + + virtual void BeginRendering()override final; + + virtual void EndRendering()override final; + + virtual bool IsDX()override final { return true; } + + virtual void AttachToNativeRenderTexture(void *nativeRenderTargetHandle, void *nativeDepthTextureHandle)override final; + +private: + IUnityGraphicsD3D12v2* m_UnityGraphicsD3D12 = nullptr; + RefCntAutoPtr m_RenderDeviceD3D12; + RefCntAutoPtr m_CmdQueue; + + RefCntAutoPtr m_RenderTarget; + RefCntAutoPtr m_DepthBuffer; +}; + + +RenderAPI* CreateRenderAPI_D3D12() +{ + return new RenderAPI_D3D12(); +} + +const UINT kNodeMask = 0; + +void RenderAPI_D3D12::ProcessDeviceEvent(UnityGfxDeviceEventType type, IUnityInterfaces* interfaces) +{ + switch (type) + { + case kUnityGfxDeviceEventInitialize: + { + m_UnityGraphicsD3D12 = interfaces->Get(); + auto d3d12Device = m_UnityGraphicsD3D12->GetDevice(); + auto &DefaultAllocator = DefaultRawMemoryAllocator::GetAllocator(); + m_CmdQueue = NEW_RC_OBJ(DefaultAllocator, "UnityCommandQueueImpl instance", UnityCommandQueueImpl)(m_UnityGraphicsD3D12); + auto *pFactoryD3D12 = GetEngineFactoryD3D12(); + EngineD3D12Attribs Attribs; + pFactoryD3D12->AttachToD3D12Device(d3d12Device, m_CmdQueue, Attribs, &m_Device, &m_Context, 0); + m_Device->QueryInterface(IID_RenderDeviceD3D12, reinterpret_cast(static_cast(&m_RenderDeviceD3D12))); + } + break; + + case kUnityGfxDeviceEventShutdown: + m_Context.Release(); + m_Device.Release(); + m_RenderDeviceD3D12.Release(); + break; + } +} + +void RenderAPI_D3D12::BeginRendering() +{ + m_CmdQueue->TransitionResource({ m_RenderTarget->GetD3D12Texture(), D3D12_RESOURCE_STATE_RENDER_TARGET, D3D12_RESOURCE_STATE_RENDER_TARGET }); + m_CmdQueue->TransitionResource({ m_DepthBuffer->GetD3D12Texture(), D3D12_RESOURCE_STATE_DEPTH_WRITE, D3D12_RESOURCE_STATE_DEPTH_WRITE }); + m_RenderTarget->SetD3D12ResourceState(D3D12_RESOURCE_STATE_RENDER_TARGET); + m_DepthBuffer->SetD3D12ResourceState(D3D12_RESOURCE_STATE_DEPTH_WRITE); + ITextureView *RTVs[] = { m_RTV }; + m_Context->SetRenderTargets(1, RTVs, m_DSV); +} + +void RenderAPI_D3D12::EndRendering() +{ + m_Context->Flush(); + m_Context->InvalidateState(); + m_RenderDeviceD3D12->FinishFrame(); +} + +void RenderAPI_D3D12::AttachToNativeRenderTexture(void *nativeRenderTargetHandle, void *nativeDepthTextureHandle) +{ + if (nativeRenderTargetHandle != nullptr && nativeDepthTextureHandle != nullptr) + { + m_RenderTarget.Release(); + m_DepthBuffer.Release(); + + RefCntAutoPtr pDeviceD3D12(m_Device, IID_RenderDeviceD3D12); + + { + auto *pd3d12RenderTarget = reinterpret_cast(nativeRenderTargetHandle); + RefCntAutoPtr pRenderTarget; + pDeviceD3D12->CreateTextureFromD3DResource(pd3d12RenderTarget, &pRenderTarget); + pRenderTarget->QueryInterface(IID_TextureD3D12, reinterpret_cast(static_cast(&m_RenderTarget))); + } + + { + auto *pd3d12DepthBuffer = reinterpret_cast(nativeDepthTextureHandle); + RefCntAutoPtr pDepthBuffer; + pDeviceD3D12->CreateTextureFromD3DResource(pd3d12DepthBuffer, &pDepthBuffer); + pDepthBuffer->QueryInterface(IID_TextureD3D12, reinterpret_cast(static_cast(&m_DepthBuffer))); + } + CreateTextureViews(m_RenderTarget, m_DepthBuffer); + } +} + +#endif // #if SUPPORT_D3D12 diff --git a/unityplugin/GhostCubePlugin/PluginSource/src/RenderAPI_OpenGLCoreES.cpp b/unityplugin/GhostCubePlugin/PluginSource/src/RenderAPI_OpenGLCoreES.cpp new file mode 100644 index 0000000..0b100d1 --- /dev/null +++ b/unityplugin/GhostCubePlugin/PluginSource/src/RenderAPI_OpenGLCoreES.cpp @@ -0,0 +1,118 @@ +#include "PlatformBase.h" + +// OpenGL Core profile (desktop) or OpenGL ES (mobile) implementation of RenderAPI. +// Supports several flavors: Core, ES3 + +#if SUPPORT_OPENGL_UNIFIED + +#include "RenderAPI.h" +#include "RenderDeviceFactoryOpenGL.h" +#include "DeviceContextGL.h" +#include "RenderDeviceGL.h" + +using namespace Diligent; + + +class RenderAPI_OpenGLCoreES : public RenderAPI +{ +public: + RenderAPI_OpenGLCoreES(UnityGfxRenderer apiType) : m_APIType(apiType) {} + + virtual void ProcessDeviceEvent(UnityGfxDeviceEventType type, IUnityInterfaces* interfaces)override final; + + virtual bool GetUsesReverseZ()override final { return false; } + + virtual void BeginRendering()override final; + + virtual bool IsDX()override final { return false; } + + virtual void AttachToNativeRenderTexture(void *nativeRenderTargetHandle, void *nativeDepthTextureHandle)override final; + + void CreateRenderTargetAndDepthBuffer(); + +private: + UnityGfxRenderer m_APIType; + RefCntAutoPtr m_DeviceCtxGL; + Uint32 m_GLRenderTargetHandle = 0; + Uint32 m_GLDepthTextureHandle = 0; + bool m_bGLTexturesUpToDate = false; +}; + + +RenderAPI* CreateRenderAPI_OpenGLCoreES(UnityGfxRenderer apiType) +{ + return new RenderAPI_OpenGLCoreES(apiType); +} + +void RenderAPI_OpenGLCoreES::ProcessDeviceEvent(UnityGfxDeviceEventType type, IUnityInterfaces* interfaces) +{ + if (type == kUnityGfxDeviceEventInitialize) + { + auto *pFactoryGL = GetEngineFactoryOpenGL(); + EngineCreationAttribs Attribs; + pFactoryGL->AttachToActiveGLContext(Attribs, &m_Device, &m_Context); + if (m_Context) + { + m_Context->QueryInterface(IID_DeviceContextGL, reinterpret_cast(static_cast(&m_DeviceCtxGL))); + } + } + else if (type == kUnityGfxDeviceEventShutdown) + { + m_Context.Release(); + m_Device.Release(); + } +} + +void RenderAPI_OpenGLCoreES::CreateRenderTargetAndDepthBuffer() +{ + RefCntAutoPtr pDeviceGL(m_Device, IID_RenderDeviceGL); + + m_RTV.Release(); + m_DSV.Release(); + + RefCntAutoPtr pRenderTarget; + TextureDesc RenderTargetDesc; + RenderTargetDesc.Type = RESOURCE_DIM_TEX_2D; + RenderTargetDesc.BindFlags = BIND_RENDER_TARGET; + RenderTargetDesc.Usage = USAGE_DEFAULT; + RenderTargetDesc.Name = "Unity render target"; + pDeviceGL->CreateTextureFromGLHandle(m_GLRenderTargetHandle, RenderTargetDesc, &pRenderTarget); + + RefCntAutoPtr pDepthBuffer; + TextureDesc DepthBufferDesc; + DepthBufferDesc.Type = RESOURCE_DIM_TEX_2D; + DepthBufferDesc.BindFlags = BIND_DEPTH_STENCIL; + DepthBufferDesc.Usage = USAGE_DEFAULT; + DepthBufferDesc.Name = "Unity depth buffer"; + pDeviceGL->CreateTextureFromGLHandle(m_GLDepthTextureHandle, DepthBufferDesc, &pDepthBuffer); + + CreateTextureViews(pRenderTarget, pDepthBuffer); +} + +void RenderAPI_OpenGLCoreES::BeginRendering() +{ + if(!m_DeviceCtxGL->UpdateCurrentGLContext()) + return; + + RenderAPI::BeginRendering(); + + if (!m_bGLTexturesUpToDate) + { + CreateRenderTargetAndDepthBuffer(); + m_bGLTexturesUpToDate = true; + } + + ITextureView *RTVs[] = { m_RTV }; + m_Context->SetRenderTargets(1, RTVs, m_DSV); +} + +void RenderAPI_OpenGLCoreES::AttachToNativeRenderTexture(void *nativeRenderTargetHandle, void *nativeDepthTextureHandle) +{ + m_GLRenderTargetHandle = static_cast(reinterpret_cast(nativeRenderTargetHandle)); + m_GLDepthTextureHandle = static_cast(reinterpret_cast(nativeDepthTextureHandle)); + m_bGLTexturesUpToDate = false; + // There is no active OpenGL context when this function is called for the first time, + // so we cannot create RTV and DSV here +} + +#endif // #if SUPPORT_OPENGL_UNIFIED diff --git a/unityplugin/GhostCubePlugin/PluginSource/src/RenderingPlugin.cpp b/unityplugin/GhostCubePlugin/PluginSource/src/RenderingPlugin.cpp new file mode 100644 index 0000000..169404f --- /dev/null +++ b/unityplugin/GhostCubePlugin/PluginSource/src/RenderingPlugin.cpp @@ -0,0 +1,210 @@ +// Example low level rendering Unity plugin + +#include "PlatformBase.h" +#include "Unity/IUnityGraphics.h" +#include "RenderAPI.h" +#include "SamplePlugin.h" + +#include + +using namespace Diligent; + +static IUnityInterfaces* s_UnityInterfaces = nullptr; +static IUnityGraphics* s_Graphics = nullptr; + +static std::unique_ptr s_CurrentAPI; +static UnityGfxRenderer s_DeviceType = kUnityGfxRendererNull; +static std::unique_ptr g_SamplePlugin; + + +static float4x4 g_Matrix; +extern "C" void UNITY_INTERFACE_EXPORT UNITY_INTERFACE_API SetMatrixFromUnity + (float m00, float m01, float m02, float m03, + float m10, float m11, float m12, float m13, + float m20, float m21, float m22, float m23, + float m30, float m31, float m32, float m33) +{ + g_Matrix = float4x4( + m00, m01, m02, m03, + m10, m11, m12, m13, + m20, m21, m22, m23, + m30, m31, m32, m33); +} + +static void* g_RenderTargetHandle = nullptr; +static void* g_DepthBufferHandle = nullptr; +extern "C" void UNITY_INTERFACE_EXPORT UNITY_INTERFACE_API SetTexturesFromUnity(void* renderTargetHandle, void *depthBufferHandle) +{ + if (s_CurrentAPI) + { + if (g_RenderTargetHandle != renderTargetHandle || + g_DepthBufferHandle != depthBufferHandle) + { + g_RenderTargetHandle = renderTargetHandle; + g_DepthBufferHandle = depthBufferHandle; + g_SamplePlugin.reset(); + s_CurrentAPI->AttachToNativeRenderTexture(g_RenderTargetHandle, g_DepthBufferHandle); + } + } +} + + +// -------------------------------------------------------------------------- +// UnitySetInterfaces + +static void UNITY_INTERFACE_API OnGraphicsDeviceEvent(UnityGfxDeviceEventType eventType); + + +extern "C" void UNITY_INTERFACE_EXPORT UNITY_INTERFACE_API UnityPluginLoad(IUnityInterfaces* unityInterfaces) +{ +#if (defined(PLATFORM_WIN32) || defined(PLATFORM_UNIVERSAL_WINDOWS)) && (defined(_DEBUG) || defined(DEBUG)) + _CrtSetDbgFlag( _CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF ); +#endif + + s_UnityInterfaces = unityInterfaces; + s_Graphics = s_UnityInterfaces->Get(); + s_Graphics->RegisterDeviceEventCallback(OnGraphicsDeviceEvent); + + // Run OnGraphicsDeviceEvent(initialize) manually on plugin load + OnGraphicsDeviceEvent(kUnityGfxDeviceEventInitialize); +} + +extern "C" void UNITY_INTERFACE_EXPORT UNITY_INTERFACE_API UnityPluginUnload() +{ + s_Graphics->UnregisterDeviceEventCallback(OnGraphicsDeviceEvent); +} + +#if UNITY_WEBGL +typedef void (UNITY_INTERFACE_API * PluginLoadFunc)(IUnityInterfaces* unityInterfaces); +typedef void (UNITY_INTERFACE_API * PluginUnloadFunc)(); + +extern "C" void UnityRegisterRenderingPlugin(PluginLoadFunc loadPlugin, PluginUnloadFunc unloadPlugin); + +extern "C" void UNITY_INTERFACE_EXPORT UNITY_INTERFACE_API RegisterPlugin() +{ + UnityRegisterRenderingPlugin(UnityPluginLoad, UnityPluginUnload); +} +#endif + +// -------------------------------------------------------------------------- +// GraphicsDeviceEvent + + +void CreateRenderAPI(UnityGfxRenderer apiType) +{ +# if SUPPORT_D3D11 + if (apiType == kUnityGfxRendererD3D11) + { + extern RenderAPI* CreateRenderAPI_D3D11(); + s_CurrentAPI.reset( CreateRenderAPI_D3D11() ); + } + # endif // if SUPPORT_D3D11 + +# if SUPPORT_D3D9 + if (apiType == kUnityGfxRendererD3D9) + { + UNSUPPORTED("D3D9 not supported") + } +# endif // if SUPPORT_D3D9 + +# if SUPPORT_D3D12 + if (apiType == kUnityGfxRendererD3D12) + { + extern RenderAPI* CreateRenderAPI_D3D12(); + s_CurrentAPI.reset( CreateRenderAPI_D3D12() ); + } +# endif // if SUPPORT_D3D9 + + +# if SUPPORT_OPENGL_UNIFIED + if (apiType == kUnityGfxRendererOpenGLCore || apiType == kUnityGfxRendererOpenGLES30) + { + extern RenderAPI* CreateRenderAPI_OpenGLCoreES(UnityGfxRenderer apiType); + s_CurrentAPI.reset( CreateRenderAPI_OpenGLCoreES(apiType) ); + } +# endif // if SUPPORT_OPENGL_UNIFIED + +# if SUPPORT_OPENGL_LEGACY + if (apiType == kUnityGfxRendererOpenGL) + { + UNSUPPORTED("Legacy Opengl not supported") + } +# endif // if SUPPORT_OPENGL_LEGACY + +# if SUPPORT_METAL + if (apiType == kUnityGfxRendererMetal) + { + UNSUPPORTED("Metal not supported") + } +# endif // if SUPPORT_METAL +} + + +static void UNITY_INTERFACE_API OnGraphicsDeviceEvent(UnityGfxDeviceEventType eventType) +{ + // Create graphics API implementation upon initialization + if (eventType == kUnityGfxDeviceEventInitialize) + { + VERIFY_EXPR(!s_CurrentAPI); + s_DeviceType = s_Graphics->GetRenderer(); + CreateRenderAPI(s_DeviceType); + if (s_CurrentAPI) + { + s_CurrentAPI->ProcessDeviceEvent(eventType, s_UnityInterfaces); + } + } + else if (eventType == kUnityGfxDeviceEventShutdown) + { + // Cleanup graphics API implementation upon shutdown + // We must destroy all resources before releasing the API + g_SamplePlugin.reset(); + if (s_CurrentAPI) + { + s_CurrentAPI->ProcessDeviceEvent(eventType, s_UnityInterfaces); + s_CurrentAPI.reset(); + } + s_DeviceType = kUnityGfxRendererNull; + } + else if (s_CurrentAPI) + { + // Let the implementation process the device related events + s_CurrentAPI->ProcessDeviceEvent(eventType, s_UnityInterfaces); + } +} + + + +// -------------------------------------------------------------------------- +// OnRenderEvent +// This will be called for GL.IssuePluginEvent script calls; eventID will +// be the integer passed to IssuePluginEvent. In this example, we just ignore +// that value. + +static void UNITY_INTERFACE_API OnRenderEvent(int eventID) +{ + // Unknown / unsupported graphics device type? Do nothing + if (s_CurrentAPI == nullptr || g_RenderTargetHandle == nullptr || g_DepthBufferHandle == nullptr) + return; + + s_CurrentAPI->BeginRendering(); + + if (!g_SamplePlugin) + { + auto RTFormat = s_CurrentAPI->GetRenderTargetFormat(); + auto DepthFormat = s_CurrentAPI->GetDepthBufferFormat(); + g_SamplePlugin.reset(new SamplePlugin(s_CurrentAPI->GetDevice(), s_CurrentAPI->GetUsesReverseZ(), RTFormat, DepthFormat)); + } + g_SamplePlugin->Render(s_CurrentAPI->GetDeviceContext(), g_Matrix ); + + s_CurrentAPI->EndRendering(); +} + + +// -------------------------------------------------------------------------- +// GetRenderEventFunc, an example function we export which is used to get a rendering event callback function. + +extern "C" UnityRenderingEvent UNITY_INTERFACE_EXPORT UNITY_INTERFACE_API GetRenderEventFunc() +{ + return OnRenderEvent; +} + diff --git a/unityplugin/GhostCubePlugin/PluginSource/src/RenderingPlugin.def b/unityplugin/GhostCubePlugin/PluginSource/src/RenderingPlugin.def new file mode 100644 index 0000000..99b4ea5 --- /dev/null +++ b/unityplugin/GhostCubePlugin/PluginSource/src/RenderingPlugin.def @@ -0,0 +1,11 @@ +; file used by Visual Studio plugin builds, mostly for 32-bit +; to stop mangling our exported function names + +LIBRARY + +EXPORTS + UnityPluginLoad + UnityPluginUnload + GetRenderEventFunc + SetMatrixFromUnity + SetTexturesFromUnity \ No newline at end of file diff --git a/unityplugin/GhostCubePlugin/PluginSource/src/SamplePlugin.cpp b/unityplugin/GhostCubePlugin/PluginSource/src/SamplePlugin.cpp new file mode 100644 index 0000000..39a771b --- /dev/null +++ b/unityplugin/GhostCubePlugin/PluginSource/src/SamplePlugin.cpp @@ -0,0 +1,174 @@ + +#include "SamplePlugin.h" +#include "GraphicsUtilities.h" +#include "MapHelper.h" + +using namespace Diligent; + +static const Char* VSSource = R"( +cbuffer Constants +{ + float4x4 g_WorldViewProj; +}; + +struct PSInput +{ + float4 Pos : SV_POSITION; + float4 Color : COLOR0; +}; + +PSInput main(float3 pos : ATTRIB0, float4 color : ATTRIB1) +{ + PSInput ps; + ps.Pos = mul( float4(pos,1.0), g_WorldViewProj); + ps.Color = color; + return ps; +} +)"; + +static const Char* PSSource = R"( +struct PSInput +{ + float4 Pos : SV_POSITION; + float4 Color : COLOR0; +}; + +float4 main(PSInput ps_in) : SV_TARGET +{ + return ps_in.Color; +} +)"; + +SamplePlugin::SamplePlugin(Diligent::IRenderDevice *pDevice, bool UseReverseZ, TEXTURE_FORMAT RTVFormat, TEXTURE_FORMAT DSVFormat) +{ + auto deviceType = pDevice->GetDeviceCaps().DevType; + { + PipelineStateDesc PSODesc; + PSODesc.IsComputePipeline = false; + PSODesc.Name = "Render sample cube PSO"; + PSODesc.GraphicsPipeline.NumRenderTargets = 1; + PSODesc.GraphicsPipeline.RTVFormats[0] = RTVFormat; + PSODesc.GraphicsPipeline.DSVFormat = DSVFormat; + PSODesc.GraphicsPipeline.PrimitiveTopologyType = PRIMITIVE_TOPOLOGY_TYPE_TRIANGLE; + PSODesc.GraphicsPipeline.RasterizerDesc.CullMode = CULL_MODE_BACK; + PSODesc.GraphicsPipeline.RasterizerDesc.FrontCounterClockwise = deviceType == DeviceType::D3D11 || deviceType == DeviceType::D3D12 ? true : false; + PSODesc.GraphicsPipeline.DepthStencilDesc.DepthFunc = UseReverseZ ? COMPARISON_FUNC_GREATER_EQUAL : COMPARISON_FUNC_LESS_EQUAL; + + PSODesc.GraphicsPipeline.BlendDesc.RenderTargets[0].BlendEnable = True; + PSODesc.GraphicsPipeline.BlendDesc.RenderTargets[0].SrcBlend = BLEND_FACTOR_SRC_ALPHA; + PSODesc.GraphicsPipeline.BlendDesc.RenderTargets[0].DestBlend = BLEND_FACTOR_INV_SRC_ALPHA; + PSODesc.GraphicsPipeline.BlendDesc.RenderTargets[0].SrcBlendAlpha = BLEND_FACTOR_ZERO; + PSODesc.GraphicsPipeline.BlendDesc.RenderTargets[0].DestBlendAlpha = BLEND_FACTOR_ONE; + + ShaderCreationAttribs CreationAttribs; + CreationAttribs.SourceLanguage = SHADER_SOURCE_LANGUAGE_HLSL; + CreationAttribs.Desc.DefaultVariableType = SHADER_VARIABLE_TYPE_STATIC; + + CreateUniformBuffer(pDevice, sizeof(float4x4), "SamplePlugin: VS constants CB", &m_VSConstants); + + RefCntAutoPtr pVS; + { + CreationAttribs.Desc.ShaderType = SHADER_TYPE_VERTEX; + CreationAttribs.EntryPoint = "main"; + CreationAttribs.Desc.Name = "Sample cube VS"; + CreationAttribs.Source = VSSource; + pDevice->CreateShader(CreationAttribs, &pVS); + pVS->GetShaderVariable("Constants")->Set(m_VSConstants); + } + + RefCntAutoPtr pPS; + { + CreationAttribs.Desc.ShaderType = SHADER_TYPE_PIXEL; + CreationAttribs.EntryPoint = "main"; + CreationAttribs.Desc.Name = "Sample cube PS"; + CreationAttribs.Source = PSSource; + pDevice->CreateShader(CreationAttribs, &pPS); + } + + LayoutElement LayoutElems[] = + { + LayoutElement(0, 0, 3, VT_FLOAT32, False), + LayoutElement(1, 0, 4, VT_FLOAT32, False) + }; + + PSODesc.GraphicsPipeline.pVS = pVS; + PSODesc.GraphicsPipeline.pPS = pPS; + PSODesc.GraphicsPipeline.InputLayout.LayoutElements = LayoutElems; + PSODesc.GraphicsPipeline.InputLayout.NumElements = _countof(LayoutElems); + pDevice->CreatePipelineState(PSODesc, &m_PSO); + } + + { + struct Vertex + { + float3 pos; + float4 color; + }; + Vertex CubeVerts[8] = + { + {float3(-1,-1,-1), float4(1,0,0,0.5)}, + {float3(-1,+1,-1), float4(0,1,0,0.5)}, + {float3(+1,+1,-1), float4(0,0,1,0.5)}, + {float3(+1,-1,-1), float4(1,1,1,0.5)}, + + {float3(-1,-1,+1), float4(1,1,0,0.5)}, + {float3(-1,+1,+1), float4(0,1,1,0.5)}, + {float3(+1,+1,+1), float4(1,0,1,0.5)}, + {float3(+1,-1,+1), float4(0.2f,0.2f,0.2f,0.5)}, + }; + BufferDesc VertBuffDesc; + VertBuffDesc.Name = "SamplePlugin: cube vertex buffer"; + VertBuffDesc.Usage = USAGE_DEFAULT; + VertBuffDesc.BindFlags = BIND_VERTEX_BUFFER; + VertBuffDesc.uiSizeInBytes = sizeof(CubeVerts); + BufferData VBData; + VBData.pData = CubeVerts; + VBData.DataSize = sizeof(CubeVerts); + pDevice->CreateBuffer(VertBuffDesc, VBData, &m_CubeVertexBuffer); + } + + { + Uint32 Indices[] = + { + 2,0,1, 2,3,0, + 4,6,5, 4,7,6, + 0,7,4, 0,3,7, + 1,0,4, 1,4,5, + 1,5,2, 5,6,2, + 3,6,7, 3,2,6 + }; + BufferDesc IndBuffDesc; + IndBuffDesc.Name = "SamplePlugin: cube index buffer"; + IndBuffDesc.Usage = USAGE_DEFAULT; + IndBuffDesc.BindFlags = BIND_INDEX_BUFFER; + IndBuffDesc.uiSizeInBytes = sizeof(Indices); + BufferData IBData; + IBData.pData = Indices; + IBData.DataSize = sizeof(Indices); + pDevice->CreateBuffer(IndBuffDesc, IBData, &m_CubeIndexBuffer); + } +} + +void SamplePlugin::Render(Diligent::IDeviceContext *pContext, const float4x4 &ViewProjMatrix) +{ + { + MapHelper CBConstants(pContext, m_VSConstants, MAP_WRITE, MAP_FLAG_DISCARD); + *CBConstants = transposeMatrix(ViewProjMatrix); + } + + Uint32 stride = sizeof(float) * 7; + Uint32 offset = 0; + IBuffer *pBuffs[] = {m_CubeVertexBuffer}; + pContext->SetVertexBuffers(0, 1, pBuffs, &stride, &offset, SET_VERTEX_BUFFERS_FLAG_RESET); + pContext->SetIndexBuffer(m_CubeIndexBuffer, 0); + + pContext->SetPipelineState(m_PSO); + pContext->CommitShaderResources(nullptr, COMMIT_SHADER_RESOURCES_FLAG_TRANSITION_RESOURCES); + + DrawAttribs DrawAttrs; + DrawAttrs.IsIndexed = true; + DrawAttrs.IndexType = VT_UINT32; + DrawAttrs.NumIndices = 36; + DrawAttrs.Topology = PRIMITIVE_TOPOLOGY_TRIANGLE_LIST; + pContext->Draw(DrawAttrs); +} diff --git a/unityplugin/GhostCubePlugin/PluginSource/src/SamplePlugin.h b/unityplugin/GhostCubePlugin/PluginSource/src/SamplePlugin.h new file mode 100644 index 0000000..56b4a33 --- /dev/null +++ b/unityplugin/GhostCubePlugin/PluginSource/src/SamplePlugin.h @@ -0,0 +1,20 @@ +#pragma once + +#include "RenderDevice.h" +#include "DeviceContext.h" +#include "RefCntAutoPtr.h" +#include "RenderAPI.h" +#include "BasicMath.h" + +class SamplePlugin +{ +public: + SamplePlugin(Diligent::IRenderDevice *pDevice, bool UseReverseZ, Diligent::TEXTURE_FORMAT RTVFormat, Diligent::TEXTURE_FORMAT DSVFormat); + void Render(Diligent::IDeviceContext *pContext, const float4x4 &ViewProjMatrix); + +private: + Diligent::RefCntAutoPtr m_CubeVertexBuffer; + Diligent::RefCntAutoPtr m_CubeIndexBuffer; + Diligent::RefCntAutoPtr m_VSConstants; + Diligent::RefCntAutoPtr m_PSO; +}; diff --git a/unityplugin/GhostCubePlugin/PluginSource/src/Unity/IUnityGraphics.h b/unityplugin/GhostCubePlugin/PluginSource/src/Unity/IUnityGraphics.h new file mode 100644 index 0000000..f8c8e07 --- /dev/null +++ b/unityplugin/GhostCubePlugin/PluginSource/src/Unity/IUnityGraphics.h @@ -0,0 +1,49 @@ +#pragma once +#include "IUnityInterface.h" + +typedef enum UnityGfxRenderer +{ + kUnityGfxRendererOpenGL = 0, // Legacy OpenGL + kUnityGfxRendererD3D9 = 1, // Direct3D 9 + kUnityGfxRendererD3D11 = 2, // Direct3D 11 + kUnityGfxRendererGCM = 3, // PlayStation 3 + kUnityGfxRendererNull = 4, // "null" device (used in batch mode) + kUnityGfxRendererXenon = 6, // Xbox 360 + kUnityGfxRendererOpenGLES20 = 8, // OpenGL ES 2.0 + kUnityGfxRendererOpenGLES30 = 11, // OpenGL ES 3.x + kUnityGfxRendererGXM = 12, // PlayStation Vita + kUnityGfxRendererPS4 = 13, // PlayStation 4 + kUnityGfxRendererXboxOne = 14, // Xbox One + kUnityGfxRendererMetal = 16, // iOS Metal + kUnityGfxRendererOpenGLCore = 17, // OpenGL core + kUnityGfxRendererD3D12 = 18, // Direct3D 12 +} UnityGfxRenderer; + +typedef enum UnityGfxDeviceEventType +{ + kUnityGfxDeviceEventInitialize = 0, + kUnityGfxDeviceEventShutdown = 1, + kUnityGfxDeviceEventBeforeReset = 2, + kUnityGfxDeviceEventAfterReset = 3, +} UnityGfxDeviceEventType; + +typedef void (UNITY_INTERFACE_API * IUnityGraphicsDeviceEventCallback)(UnityGfxDeviceEventType eventType); + +// Should only be used on the rendering thread unless noted otherwise. +UNITY_DECLARE_INTERFACE(IUnityGraphics) +{ + UnityGfxRenderer (UNITY_INTERFACE_API * GetRenderer)(); // Thread safe + + // This callback will be called when graphics device is created, destroyed, reset, etc. + // It is possible to miss the kUnityGfxDeviceEventInitialize event in case plugin is loaded at a later time, + // when the graphics device is already created. + void (UNITY_INTERFACE_API * RegisterDeviceEventCallback)(IUnityGraphicsDeviceEventCallback callback); + void (UNITY_INTERFACE_API * UnregisterDeviceEventCallback)(IUnityGraphicsDeviceEventCallback callback); +}; +UNITY_REGISTER_INTERFACE_GUID(0x7CBA0A9CA4DDB544ULL,0x8C5AD4926EB17B11ULL,IUnityGraphics) + + + +// Certain Unity APIs (GL.IssuePluginEvent, CommandBuffer.IssuePluginEvent) can callback into native plugins. +// Provide them with an address to a function of this signature. +typedef void (UNITY_INTERFACE_API * UnityRenderingEvent)(int eventId); diff --git a/unityplugin/GhostCubePlugin/PluginSource/src/Unity/IUnityGraphicsD3D11.h b/unityplugin/GhostCubePlugin/PluginSource/src/Unity/IUnityGraphicsD3D11.h new file mode 100644 index 0000000..a3f4c8b --- /dev/null +++ b/unityplugin/GhostCubePlugin/PluginSource/src/Unity/IUnityGraphicsD3D11.h @@ -0,0 +1,9 @@ +#pragma once +#include "IUnityInterface.h" + +// Should only be used on the rendering thread unless noted otherwise. +UNITY_DECLARE_INTERFACE(IUnityGraphicsD3D11) +{ + ID3D11Device* (UNITY_INTERFACE_API * GetDevice)(); +}; +UNITY_REGISTER_INTERFACE_GUID(0xAAB37EF87A87D748ULL,0xBF76967F07EFB177ULL,IUnityGraphicsD3D11) diff --git a/unityplugin/GhostCubePlugin/PluginSource/src/Unity/IUnityGraphicsD3D12.h b/unityplugin/GhostCubePlugin/PluginSource/src/Unity/IUnityGraphicsD3D12.h new file mode 100644 index 0000000..11893f7 --- /dev/null +++ b/unityplugin/GhostCubePlugin/PluginSource/src/Unity/IUnityGraphicsD3D12.h @@ -0,0 +1,48 @@ +#pragma once +#include "IUnityInterface.h" +#ifndef __cplusplus + #include +#endif + +typedef struct UnityGraphicsD3D12ResourceState UnityGraphicsD3D12ResourceState; +struct UnityGraphicsD3D12ResourceState +{ + ID3D12Resource* resource; // Resource to barrier. + D3D12_RESOURCE_STATES expected; // Expected resource state before this command list is executed. + D3D12_RESOURCE_STATES current; // State this resource will be in after this command list is executed. +}; + +// Should only be used on the main thread. +UNITY_DECLARE_INTERFACE(IUnityGraphicsD3D12v2) +{ + ID3D12Device* (UNITY_INTERFACE_API * GetDevice)(); + + ID3D12Fence* (UNITY_INTERFACE_API * GetFrameFence)(); + // Returns the value set on the frame fence once the current frame completes or the GPU is flushed + UINT64 (UNITY_INTERFACE_API * GetNextFrameFenceValue)(); + + // Executes a given command list on a worker thread. + // [Optional] Declares expected and post-execution resource states. + // Returns the fence value. + UINT64 (UNITY_INTERFACE_API * ExecuteCommandList)(ID3D12GraphicsCommandList* commandList, int stateCount, UnityGraphicsD3D12ResourceState* states); +}; +UNITY_REGISTER_INTERFACE_GUID(0xEC39D2F18446C745ULL,0xB1A2626641D6B11FULL,IUnityGraphicsD3D12v2) + + + +// Obsolete +UNITY_DECLARE_INTERFACE(IUnityGraphicsD3D12) +{ + ID3D12Device* (UNITY_INTERFACE_API * GetDevice)(); + ID3D12CommandQueue* (UNITY_INTERFACE_API * GetCommandQueue)(); + + ID3D12Fence* (UNITY_INTERFACE_API * GetFrameFence)(); + // Returns the value set on the frame fence once the current frame completes or the GPU is flushed + UINT64 (UNITY_INTERFACE_API * GetNextFrameFenceValue)(); + + // Returns the state a resource will be in after the last command list is executed + bool (UNITY_INTERFACE_API * GetResourceState)(ID3D12Resource* resource, D3D12_RESOURCE_STATES* outState); + // Specifies the state a resource will be in after a plugin command list with resource barriers is executed + void (UNITY_INTERFACE_API * SetResourceState)(ID3D12Resource* resource, D3D12_RESOURCE_STATES state); +}; +UNITY_REGISTER_INTERFACE_GUID(0xEF4CEC88A45F4C4CULL,0xBD295B6F2A38D9DEULL,IUnityGraphicsD3D12) diff --git a/unityplugin/GhostCubePlugin/PluginSource/src/Unity/IUnityGraphicsD3D9.h b/unityplugin/GhostCubePlugin/PluginSource/src/Unity/IUnityGraphicsD3D9.h new file mode 100644 index 0000000..202b8b2 --- /dev/null +++ b/unityplugin/GhostCubePlugin/PluginSource/src/Unity/IUnityGraphicsD3D9.h @@ -0,0 +1,10 @@ +#pragma once +#include "IUnityInterface.h" + +// Should only be used on the rendering thread unless noted otherwise. +UNITY_DECLARE_INTERFACE(IUnityGraphicsD3D9) +{ + IDirect3D9* (UNITY_INTERFACE_API * GetD3D)(); + IDirect3DDevice9* (UNITY_INTERFACE_API * GetDevice)(); +}; +UNITY_REGISTER_INTERFACE_GUID(0xE90746A523D53C4CULL,0xAC825B19B6F82AC3ULL,IUnityGraphicsD3D9) diff --git a/unityplugin/GhostCubePlugin/PluginSource/src/Unity/IUnityGraphicsMetal.h b/unityplugin/GhostCubePlugin/PluginSource/src/Unity/IUnityGraphicsMetal.h new file mode 100644 index 0000000..c31518f --- /dev/null +++ b/unityplugin/GhostCubePlugin/PluginSource/src/Unity/IUnityGraphicsMetal.h @@ -0,0 +1,47 @@ +#pragma once +#include "IUnityInterface.h" + +#ifndef __OBJC__ + #error metal plugin is objc code. +#endif +#ifndef __clang__ + #error only clang compiler is supported. +#endif +#if !__has_feature(objc_arc) + #error metal demands ARC enabled. +#endif + +@class NSBundle; +@protocol MTLDevice; +@protocol MTLCommandBuffer; +@protocol MTLCommandEncoder; +@protocol MTLTexture; + +struct RenderSurfaceBase; +typedef struct RenderSurfaceBase* UnityRenderBuffer; + +// Should only be used on the rendering thread unless noted otherwise. +UNITY_DECLARE_INTERFACE(IUnityGraphicsMetal) +{ + NSBundle* (UNITY_INTERFACE_API * MetalBundle)(); + id (UNITY_INTERFACE_API * MetalDevice)(); + + id (UNITY_INTERFACE_API * CurrentCommandBuffer)(); + + // for custom rendering support there are two scenarios: + // you want to use current in-flight MTLCommandEncoder (NB: it might be nil) + id (UNITY_INTERFACE_API * CurrentCommandEncoder)(); + // or you might want to create your own encoder. + // In that case you should end unity's encoder before creating your own and end yours before returning control to unity + void (UNITY_INTERFACE_API * EndCurrentCommandEncoder)(); + + // access to RenderBuffer's texure + // NB: you pass here *native* RenderBuffer, acquired by calling (C#) RenderBuffer.GetNativeRenderBufferPtr + // AAResolvedTextureFromRenderBuffer will return nil in case of non-AA RenderBuffer or if called for depth RenderBuffer + // StencilTextureFromRenderBuffer will return nil in case of no-stencil RenderBuffer or if called for color RenderBuffer + id (UNITY_INTERFACE_API * TextureFromRenderBuffer)(UnityRenderBuffer buffer); + id (UNITY_INTERFACE_API * AAResolvedTextureFromRenderBuffer)(UnityRenderBuffer buffer); + id (UNITY_INTERFACE_API * StencilTextureFromRenderBuffer)(UnityRenderBuffer buffer); + +}; +UNITY_REGISTER_INTERFACE_GUID(0x992C8EAEA95811E5ULL,0x9A62C4B5B9876117ULL,IUnityGraphicsMetal) diff --git a/unityplugin/GhostCubePlugin/PluginSource/src/Unity/IUnityInterface.h b/unityplugin/GhostCubePlugin/PluginSource/src/Unity/IUnityInterface.h new file mode 100644 index 0000000..b080f1a --- /dev/null +++ b/unityplugin/GhostCubePlugin/PluginSource/src/Unity/IUnityInterface.h @@ -0,0 +1,149 @@ +#pragma once + +// Unity native plugin API +// Compatible with C99 + +#if defined(__CYGWIN32__) + #define UNITY_INTERFACE_API __stdcall + #define UNITY_INTERFACE_EXPORT __declspec(dllexport) +#elif defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(_WIN64) || defined(WINAPI_FAMILY) + #define UNITY_INTERFACE_API __stdcall + #define UNITY_INTERFACE_EXPORT// __declspec(dllexport) - this is not needed since all exported functions are listed in .def file, + // while this header can also be used by +#elif defined(__MACH__) || defined(__ANDROID__) || defined(__linux__) || defined(__QNX__) + #define UNITY_INTERFACE_API + #define UNITY_INTERFACE_EXPORT __attribute__((visibility("default"))) +#else + #define UNITY_INTERFACE_API + #define UNITY_INTERFACE_EXPORT +#endif + + + +// Unity Interface GUID +// Ensures cross plugin uniqueness. +// +// Template specialization is used to produce a means of looking up a GUID from it's payload type at compile time. +// The net result should compile down to passing around the GUID. +// +// UNITY_REGISTER_INTERFACE_GUID should be placed in the header file of any payload definition outside of all namespaces. +// The payload structure and the registration GUID are all that is required to expose the interface to other systems. +struct UnityInterfaceGUID +{ +#ifdef __cplusplus + UnityInterfaceGUID(unsigned long long high, unsigned long long low) + : m_GUIDHigh(high) + , m_GUIDLow(low) + { + } + + UnityInterfaceGUID(const UnityInterfaceGUID& other) + { + m_GUIDHigh = other.m_GUIDHigh; + m_GUIDLow = other.m_GUIDLow; + } + + UnityInterfaceGUID& operator=(const UnityInterfaceGUID& other) + { + m_GUIDHigh = other.m_GUIDHigh; + m_GUIDLow = other.m_GUIDLow; + return *this; + } + + bool Equals(const UnityInterfaceGUID& other) const { return m_GUIDHigh == other.m_GUIDHigh && m_GUIDLow == other.m_GUIDLow; } + bool LessThan(const UnityInterfaceGUID& other) const { return m_GUIDHigh < other.m_GUIDHigh || (m_GUIDHigh == other.m_GUIDHigh && m_GUIDLow < other.m_GUIDLow); } +#endif + unsigned long long m_GUIDHigh; + unsigned long long m_GUIDLow; +}; +#ifdef __cplusplus +inline bool operator==(const UnityInterfaceGUID& left, const UnityInterfaceGUID& right) { return left.Equals(right); } +inline bool operator!=(const UnityInterfaceGUID& left, const UnityInterfaceGUID& right) { return !left.Equals(right); } +inline bool operator< (const UnityInterfaceGUID& left, const UnityInterfaceGUID& right) { return left.LessThan(right); } +inline bool operator> (const UnityInterfaceGUID& left, const UnityInterfaceGUID& right) { return right.LessThan(left); } +inline bool operator>=(const UnityInterfaceGUID& left, const UnityInterfaceGUID& right) { return !operator< (left,right); } +inline bool operator<=(const UnityInterfaceGUID& left, const UnityInterfaceGUID& right) { return !operator> (left,right); } +#else +typedef struct UnityInterfaceGUID UnityInterfaceGUID; +#endif + + + +#define UNITY_GET_INTERFACE_GUID(TYPE) TYPE##_GUID +#define UNITY_GET_INTERFACE(INTERFACES, TYPE) (TYPE*)INTERFACES->GetInterface(UNITY_GET_INTERFACE_GUID(TYPE)); + +#ifdef __cplusplus + #define UNITY_DECLARE_INTERFACE(NAME) \ + struct NAME : IUnityInterface + + template \ + inline const UnityInterfaceGUID GetUnityInterfaceGUID(); \ + + #define UNITY_REGISTER_INTERFACE_GUID(HASHH, HASHL, TYPE) \ + const UnityInterfaceGUID TYPE##_GUID(HASHH, HASHL); \ + template<> \ + inline const UnityInterfaceGUID GetUnityInterfaceGUID() \ + { \ + return UNITY_GET_INTERFACE_GUID(TYPE); \ + } +#else + #define UNITY_DECLARE_INTERFACE(NAME) \ + typedef struct NAME NAME; \ + struct NAME + + #define UNITY_REGISTER_INTERFACE_GUID(HASHH, HASHL, TYPE) \ + const UnityInterfaceGUID TYPE##_GUID = {HASHH, HASHL}; +#endif + + + +#ifdef __cplusplus +struct IUnityInterface +{ +}; +#else +typedef void IUnityInterface; +#endif + + + +typedef struct IUnityInterfaces +{ + // Returns an interface matching the guid. + // Returns nullptr if the given interface is unavailable in the active Unity runtime. + IUnityInterface* (UNITY_INTERFACE_API * GetInterface)(UnityInterfaceGUID guid); + + // Registers a new interface. + void (UNITY_INTERFACE_API * RegisterInterface)(UnityInterfaceGUID guid, IUnityInterface* ptr); + +#ifdef __cplusplus + // Helper for GetInterface. + template + INTERFACE* Get() + { + return static_cast(GetInterface(GetUnityInterfaceGUID())); + } + + // Helper for RegisterInterface. + template + void Register(IUnityInterface* ptr) + { + RegisterInterface(GetUnityInterfaceGUID(), ptr); + } +#endif +} IUnityInterfaces; + + + +#ifdef __cplusplus +extern "C" { +#endif + +// If exported by a plugin, this function will be called when the plugin is loaded. +//void UNITY_INTERFACE_EXPORT UNITY_INTERFACE_API UnityPluginLoad(IUnityInterfaces* unityInterfaces); +// If exported by a plugin, this function will be called when the plugin is about to be unloaded. +//void UNITY_INTERFACE_EXPORT UNITY_INTERFACE_API UnityPluginUnload(); + +#ifdef __cplusplus +} +#endif -- cgit v1.2.3