소스 검색

Made it optional to compile the CPU compute fallback system (#1874)

Also fixed building on Windows without DX12 enabled
Jorrit Rouwe 2 주 전
부모
커밋
64dc58af3b

+ 3 - 0
Build/CMakeLists.txt

@@ -130,6 +130,9 @@ option(JPH_USE_VK "Use Vulkan" ON)
 # Option to compile with Metal compute
 option(JPH_USE_MTL "Use Metal" ON)
 
+# Option to compile with CPU fallback compute
+option(JPH_USE_CPU_COMPUTE "Use CPU Compute" ON)
+
 # Determine which configurations exist
 if (CMAKE_CURRENT_SOURCE_DIR STREQUAL CMAKE_SOURCE_DIR) # Only do this when we're at the top level, see: https://gitlab.kitware.com/cmake/cmake/-/issues/24181
 	if ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "MSVC" OR "${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU")

+ 5 - 0
Jolt/Compute/CPU/ComputeBufferCPU.cpp

@@ -3,6 +3,9 @@
 // SPDX-License-Identifier: MIT
 
 #include <Jolt/Jolt.h>
+
+#ifdef JPH_USE_CPU_COMPUTE
+
 #include <Jolt/Compute/CPU/ComputeBufferCPU.h>
 
 JPH_NAMESPACE_BEGIN
@@ -29,3 +32,5 @@ ComputeBufferResult ComputeBufferCPU::CreateReadBackBuffer() const
 }
 
 JPH_NAMESPACE_END
+
+#endif // JPH_USE_CPU_COMPUTE

+ 4 - 0
Jolt/Compute/CPU/ComputeBufferCPU.h

@@ -6,6 +6,8 @@
 
 #include <Jolt/Compute/ComputeBuffer.h>
 
+#ifdef JPH_USE_CPU_COMPUTE
+
 JPH_NAMESPACE_BEGIN
 
 /// Buffer that can be used with the CPU compute system
@@ -30,3 +32,5 @@ private:
 };
 
 JPH_NAMESPACE_END
+
+#endif // JPH_USE_CPU_COMPUTE

+ 5 - 0
Jolt/Compute/CPU/ComputeQueueCPU.cpp

@@ -3,6 +3,9 @@
 // SPDX-License-Identifier: MIT
 
 #include <Jolt/Jolt.h>
+
+#ifdef JPH_USE_CPU_COMPUTE
+
 #include <Jolt/Compute/CPU/ComputeQueueCPU.h>
 #include <Jolt/Compute/CPU/ComputeShaderCPU.h>
 #include <Jolt/Compute/CPU/ComputeBufferCPU.h>
@@ -94,3 +97,5 @@ void ComputeQueueCPU::Wait()
 }
 
 JPH_NAMESPACE_END
+
+#endif // JPH_USE_CPU_COMPUTE

+ 5 - 0
Jolt/Compute/CPU/ComputeQueueCPU.h

@@ -5,6 +5,9 @@
 #pragma once
 
 #include <Jolt/Compute/ComputeQueue.h>
+
+#ifdef JPH_USE_CPU_COMPUTE
+
 #include <Jolt/Compute/CPU/ComputeShaderCPU.h>
 #include <Jolt/Core/UnorderedSet.h>
 
@@ -36,3 +39,5 @@ private:
 };
 
 JPH_NAMESPACE_END
+
+#endif // JPH_USE_CPU_COMPUTE

+ 4 - 0
Jolt/Compute/CPU/ComputeShaderCPU.h

@@ -6,6 +6,8 @@
 
 #include <Jolt/Compute/ComputeShader.h>
 
+#ifdef JPH_USE_CPU_COMPUTE
+
 JPH_NAMESPACE_BEGIN
 
 class ShaderWrapper;
@@ -36,3 +38,5 @@ private:
 };
 
 JPH_NAMESPACE_END
+
+#endif // JPH_USE_CPU_COMPUTE

+ 5 - 0
Jolt/Compute/CPU/ComputeSystemCPU.cpp

@@ -3,6 +3,9 @@
 // SPDX-License-Identifier: MIT
 
 #include <Jolt/Jolt.h>
+
+#ifdef JPH_USE_CPU_COMPUTE
+
 #include <Jolt/Compute/CPU/ComputeSystemCPU.h>
 #include <Jolt/Compute/CPU/ComputeQueueCPU.h>
 #include <Jolt/Compute/CPU/ComputeBufferCPU.h>
@@ -44,3 +47,5 @@ ComputeSystemResult CreateComputeSystemCPU()
 }
 
 JPH_NAMESPACE_END
+
+#endif // JPH_USE_CPU_COMPUTE

+ 5 - 0
Jolt/Compute/CPU/ComputeSystemCPU.h

@@ -5,6 +5,9 @@
 #pragma once
 
 #include <Jolt/Compute/ComputeSystem.h>
+
+#ifdef JPH_USE_CPU_COMPUTE
+
 #include <Jolt/Core/UnorderedMap.h>
 #include <Jolt/Compute/CPU/ComputeShaderCPU.h>
 
@@ -45,3 +48,5 @@ private:
 #define JPH_REGISTER_SHADER(sys, name)				JPH::JPH_SHADER_WRAPPER_FUNCTION_NAME(name)(sys)
 
 JPH_NAMESPACE_END
+
+#endif // JPH_USE_CPU_COMPUTE

+ 4 - 0
Jolt/Compute/CPU/ShaderWrapper.h

@@ -4,6 +4,8 @@
 
 #pragma once
 
+#ifdef JPH_USE_CPU_COMPUTE
+
 JPH_NAMESPACE_BEGIN
 
 namespace HLSLToCPP { struct uint3; }
@@ -23,3 +25,5 @@ public:
 };
 
 JPH_NAMESPACE_END
+
+#endif // JPH_USE_CPU_COMPUTE

+ 2 - 0
Jolt/Compute/ComputeSystem.h

@@ -40,9 +40,11 @@ using ComputeSystemResult = Result<Ref<ComputeSystem>>;
 extern JPH_EXPORT ComputeSystemResult	CreateComputeSystemVK();
 #endif
 
+#ifdef JPH_USE_CPU_COMPUTE
 /// Factory function to create a compute system that falls back to CPU.
 /// This is intended mainly for debugging purposes and is not optimized for performance
 extern JPH_EXPORT ComputeSystemResult	CreateComputeSystemCPU();
+#endif
 
 #ifdef JPH_USE_DX12
 

+ 0 - 4
Jolt/Compute/DX12/IncludeDX12.h

@@ -4,8 +4,6 @@
 
 #pragma once
 
-#ifdef JPH_USE_DX12
-
 #include <Jolt/Core/IncludeWindows.h>
 #include <Jolt/Core/StringTools.h>
 
@@ -49,5 +47,3 @@ inline bool HRFailed(HRESULT inHR)
 }
 
 JPH_NAMESPACE_END
-
-#endif // JPH_USE_DX12

+ 28 - 16
Jolt/Jolt.cmake

@@ -18,18 +18,6 @@ set(JOLT_PHYSICS_SRC_FILES
 	${JOLT_PHYSICS_ROOT}/Compute/ComputeQueue.h
 	${JOLT_PHYSICS_ROOT}/Compute/ComputeSystem.h
 	${JOLT_PHYSICS_ROOT}/Compute/ComputeShader.h
-	${JOLT_PHYSICS_ROOT}/Compute/CPU/ComputeQueueCPU.cpp
-	${JOLT_PHYSICS_ROOT}/Compute/CPU/ComputeQueueCPU.h
-	${JOLT_PHYSICS_ROOT}/Compute/CPU/ComputeBufferCPU.cpp
-	${JOLT_PHYSICS_ROOT}/Compute/CPU/ComputeBufferCPU.h
-	${JOLT_PHYSICS_ROOT}/Compute/CPU/ComputeSystemCPU.cpp
-	${JOLT_PHYSICS_ROOT}/Compute/CPU/ComputeSystemCPU.h
-	${JOLT_PHYSICS_ROOT}/Compute/CPU/ComputeShaderCPU.h
-	${JOLT_PHYSICS_ROOT}/Compute/CPU/HLSLToCPP.h
-	${JOLT_PHYSICS_ROOT}/Compute/CPU/ShaderWrapper.h
-	${JOLT_PHYSICS_ROOT}/Compute/CPU/WrapShaderBegin.h
-	${JOLT_PHYSICS_ROOT}/Compute/CPU/WrapShaderBindings.h
-	${JOLT_PHYSICS_ROOT}/Compute/CPU/WrapShaderEnd.h
 	${JOLT_PHYSICS_ROOT}/Core/ARMNeon.h
 	${JOLT_PHYSICS_ROOT}/Core/Array.h
 	${JOLT_PHYSICS_ROOT}/Core/Atomics.h
@@ -445,9 +433,6 @@ set(JOLT_PHYSICS_SRC_FILES
 	${JOLT_PHYSICS_ROOT}/Renderer/DebugRendererRecorder.h
 	${JOLT_PHYSICS_ROOT}/Renderer/DebugRendererSimple.cpp
 	${JOLT_PHYSICS_ROOT}/Renderer/DebugRendererSimple.h
-	${JOLT_PHYSICS_ROOT}/Shaders/HairWrapper.cpp
-	${JOLT_PHYSICS_ROOT}/Shaders/HairWrapper.h
-	${JOLT_PHYSICS_ROOT}/Shaders/TestComputeWrapper.cpp
 	${JOLT_PHYSICS_ROOT}/Skeleton/SkeletalAnimation.cpp
 	${JOLT_PHYSICS_ROOT}/Skeleton/SkeletalAnimation.h
 	${JOLT_PHYSICS_ROOT}/Skeleton/Skeleton.cpp
@@ -486,7 +471,7 @@ if (ENABLE_OBJECT_STREAM)
 	)
 endif()
 
-if (JPH_USE_DX12 OR JPH_USE_VK OR JPH_USE_MTL)
+if (JPH_USE_DX12 OR JPH_USE_VK OR JPH_USE_MTL OR JPH_USE_CPU_COMPUTE)
 	# Compute shaders
 	set(JOLT_PHYSICS_SHADERS
 		${JOLT_PHYSICS_ROOT}/Shaders/HairApplyDeltaTransform.hlsl
@@ -541,6 +526,28 @@ if (JPH_USE_DX12 OR JPH_USE_VK OR JPH_USE_MTL)
 	)
 endif()
 
+# CPU compute support
+if (JPH_USE_CPU_COMPUTE)
+	set(JOLT_PHYSICS_SRC_FILES
+		${JOLT_PHYSICS_SRC_FILES}
+		${JOLT_PHYSICS_ROOT}/Compute/CPU/ComputeQueueCPU.cpp
+		${JOLT_PHYSICS_ROOT}/Compute/CPU/ComputeQueueCPU.h
+		${JOLT_PHYSICS_ROOT}/Compute/CPU/ComputeBufferCPU.cpp
+		${JOLT_PHYSICS_ROOT}/Compute/CPU/ComputeBufferCPU.h
+		${JOLT_PHYSICS_ROOT}/Compute/CPU/ComputeSystemCPU.cpp
+		${JOLT_PHYSICS_ROOT}/Compute/CPU/ComputeSystemCPU.h
+		${JOLT_PHYSICS_ROOT}/Compute/CPU/ComputeShaderCPU.h
+		${JOLT_PHYSICS_ROOT}/Compute/CPU/HLSLToCPP.h
+		${JOLT_PHYSICS_ROOT}/Compute/CPU/ShaderWrapper.h
+		${JOLT_PHYSICS_ROOT}/Compute/CPU/WrapShaderBegin.h
+		${JOLT_PHYSICS_ROOT}/Compute/CPU/WrapShaderBindings.h
+		${JOLT_PHYSICS_ROOT}/Compute/CPU/WrapShaderEnd.h
+		${JOLT_PHYSICS_ROOT}/Shaders/HairWrapper.cpp
+		${JOLT_PHYSICS_ROOT}/Shaders/HairWrapper.h
+		${JOLT_PHYSICS_ROOT}/Shaders/TestComputeWrapper.cpp
+	)
+endif()
+
 if (WIN32)
 	# Add natvis file
 	set(JOLT_PHYSICS_SRC_FILES ${JOLT_PHYSICS_SRC_FILES} ${JOLT_PHYSICS_ROOT}/Jolt.natvis)
@@ -817,6 +824,11 @@ if (JPH_USE_MTL)
 	target_link_libraries(Jolt LINK_PUBLIC "-framework Foundation -framework Metal -framework MetalKit")
 endif()
 
+# Compile CPU compute support
+if (JPH_USE_CPU_COMPUTE)
+	target_compile_definitions(Jolt PUBLIC JPH_USE_CPU_COMPUTE)
+endif()
+
 # Enable the debug renderer
 if (DEBUG_RENDERER_IN_DISTRIBUTION)
 	target_compile_definitions(Jolt PUBLIC "JPH_DEBUG_RENDERER")

+ 4 - 0
Jolt/Shaders/HairWrapper.cpp

@@ -4,6 +4,8 @@
 
 #include <Jolt/Jolt.h>
 
+#ifdef JPH_USE_CPU_COMPUTE
+
 #include <Jolt/Shaders/HairWrapper.h>
 
 #define JPH_SHADER_NAME HairTeleport
@@ -133,3 +135,5 @@ void JPH_EXPORT HairRegisterShaders(ComputeSystemCPU *inComputeSystem)
 }
 
 JPH_NAMESPACE_END
+
+#endif // JPH_USE_CPU_COMPUTE

+ 4 - 0
Jolt/Shaders/HairWrapper.h

@@ -4,6 +4,8 @@
 
 #pragma once
 
+#ifdef JPH_USE_CPU_COMPUTE
+
 JPH_NAMESPACE_BEGIN
 
 class ComputeSystemCPU;
@@ -11,3 +13,5 @@ class ComputeSystemCPU;
 void JPH_EXPORT HairRegisterShaders(ComputeSystemCPU *inComputeSystem);
 
 JPH_NAMESPACE_END
+
+#endif // JPH_USE_CPU_COMPUTE

+ 4 - 0
Jolt/Shaders/TestComputeWrapper.cpp

@@ -4,6 +4,8 @@
 
 #include <Jolt/Jolt.h>
 
+#ifdef JPH_USE_CPU_COMPUTE
+
 #define JPH_SHADER_NAME TestCompute
 #include <Jolt/Compute/CPU/WrapShaderBegin.h>
 #include "TestCompute.hlsl"
@@ -17,3 +19,5 @@
 #include <Jolt/Compute/CPU/WrapShaderBindings.h>
 #include "TestCompute2Bindings.h"
 #include <Jolt/Compute/CPU/WrapShaderEnd.h>
+
+#endif // JPH_USE_CPU_COMPUTE

+ 8 - 0
Samples/SamplesApp.cpp

@@ -505,10 +505,12 @@ SamplesApp::SamplesApp(const String &inCommandLine) :
 		FatalError(queue_result.GetError().c_str());
 	mComputeQueue = queue_result.Get();
 
+#ifdef JPH_USE_CPU_COMPUTE
 	// Create compute system CPU
 	mComputeSystemCPU = StaticCast<ComputeSystemCPU>(CreateComputeSystemCPU().Get());
 	HairRegisterShaders(mComputeSystemCPU);
 	mComputeQueueCPU = mComputeSystemCPU->CreateComputeQueue().Get();
+#endif // JPH_USE_CPU_COMPUTE
 
 	{
 		// Disable allocation checking
@@ -567,7 +569,9 @@ SamplesApp::SamplesApp(const String &inCommandLine) :
 			mDebugUI->CreateCheckBox(phys_settings, "Record State For Playback", mRecordState, [this](UICheckBox::EState inState) { mRecordState = inState == UICheckBox::STATE_CHECKED; });
 			mDebugUI->CreateCheckBox(phys_settings, "Check Determinism", mCheckDeterminism, [this](UICheckBox::EState inState) { mCheckDeterminism = inState == UICheckBox::STATE_CHECKED; });
 			mDebugUI->CreateCheckBox(phys_settings, "Install Contact Listener", mInstallContactListener, [this](UICheckBox::EState inState) { mInstallContactListener = inState == UICheckBox::STATE_CHECKED; StartTest(mTestClass); });
+#ifdef JPH_USE_CPU_COMPUTE
 			mDebugUI->CreateCheckBox(phys_settings, "Use GPU Compute System", mUseGPUCompute, [this](UICheckBox::EState inState) { mUseGPUCompute = inState == UICheckBox::STATE_CHECKED; StartTest(mTestClass); });
+#endif // JPH_USE_CPU_COMPUTE
 			mDebugUI->ShowMenu(phys_settings);
 		});
 	#ifdef JPH_DEBUG_RENDERER
@@ -785,10 +789,14 @@ void SamplesApp::StartTest(const RTTI *inRTTI)
 	mTest = static_cast<Test *>(inRTTI->CreateObject());
 	mTest->SetPhysicsSystem(mPhysicsSystem);
 	mTest->SetJobSystem(mJobSystem);
+#ifdef JPH_USE_CPU_COMPUTE
 	if (mUseGPUCompute)
+#endif // JPH_USE_CPU_COMPUTE
 		mTest->SetComputeSystem(mComputeSystem, mComputeQueue);
+#ifdef JPH_USE_CPU_COMPUTE
 	else
 		mTest->SetComputeSystem(mComputeSystemCPU, mComputeQueueCPU);
+#endif // JPH_USE_CPU_COMPUTE
 	mTest->SetDebugRenderer(mDebugRenderer);
 	mTest->SetTempAllocator(mTempAllocator);
 	if (mInstallContactListener)

+ 4 - 0
Samples/SamplesApp.h

@@ -95,8 +95,10 @@ private:
 	JobSystem *				mJobSystemValidating = nullptr;								// The job system to use when validating determinism
 	Ref<ComputeSystem>		mComputeSystem = nullptr;									// The compute system to use for compute jobs
 	Ref<ComputeQueue>		mComputeQueue = nullptr;									// The compute queue to use for compute jobs
+#ifdef JPH_USE_CPU_COMPUTE
 	Ref<ComputeSystemCPU>	mComputeSystemCPU = nullptr;								// The compute system to use for CPU compute jobs
 	Ref<ComputeQueue>		mComputeQueueCPU = nullptr;									// The compute queue to use for CPU compute jobs
+#endif // JPH_USE_CPU_COMPUTE
 	BPLayerInterfaceImpl	mBroadPhaseLayerInterface;									// The broadphase layer interface that maps object layers to broadphase layers
 	ObjectVsBroadPhaseLayerFilterImpl mObjectVsBroadPhaseLayerFilter;					// Class that filters object vs broadphase layers
 	ObjectLayerPairFilterImpl mObjectVsObjectLayerFilter;								// Class that filters object vs object layers
@@ -132,7 +134,9 @@ private:
 
 	// Test settings
 	bool					mInstallContactListener = false;							// When true, the contact listener is installed the next time the test is reset
+#ifdef JPH_USE_CPU_COMPUTE
 	bool					mUseGPUCompute = true;										// When true, uses the GPU compute system for compute jobs
+#endif // JPH_USE_CPU_COMPUTE
 
 	// State recording and determinism checks
 	bool					mRecordState = false;										// When true, the state of the physics system is recorded in mPlaybackFrames every physics update

+ 1 - 1
TestFramework/TestFramework.cmake

@@ -307,7 +307,7 @@ if (NOT CROSS_COMPILE_ARM AND (JPH_USE_VK OR JPH_USE_DX12 OR JPH_USE_MTL))
 
 	if (WIN32)
 		# Windows configuration
-		target_link_libraries(TestFramework LINK_PUBLIC Jolt dinput8.lib shcore.lib)
+		target_link_libraries(TestFramework LINK_PUBLIC Jolt dinput8.lib shcore.lib dxguid.lib)
 	endif()
 	if (LINUX)
 		# Linux configuration

+ 8 - 0
UnitTests/Compute/ComputeTests.cpp

@@ -4,6 +4,8 @@
 
 #include "UnitTestFramework.h"
 
+#if defined(JPH_USE_DX12) || defined(JPH_USE_VK) || defined(JPH_USE_MTL) || defined(JPH_USE_CPU_COMPUTE)
+
 #include <Jolt/Compute/ComputeSystem.h>
 #include <Jolt/Compute/CPU/ComputeSystemCPU.h>
 #include <Jolt/Shaders/TestComputeBindings.h>
@@ -23,8 +25,10 @@ JPH_SUPPRESS_WARNINGS_STD_END
 #include <CoreFoundation/CoreFoundation.h>
 #endif
 
+#ifdef JPH_USE_CPU_COMPUTE
 JPH_DECLARE_REGISTER_SHADER(TestCompute)
 JPH_DECLARE_REGISTER_SHADER(TestCompute2)
+#endif // JPH_USE_CPU_COMPUTE
 
 TEST_SUITE("ComputeTests")
 {
@@ -348,6 +352,7 @@ TEST_SUITE("ComputeTests")
 	}
 #endif // JPH_USE_VK
 
+#ifdef JPH_USE_CPU_COMPUTE
 	TEST_CASE("TestComputeCPU")
 	{
 		ComputeSystemResult compute_system = CreateComputeSystemCPU();
@@ -360,4 +365,7 @@ TEST_SUITE("ComputeTests")
 			RunTests(compute_system.Get());
 		}
 	}
+#endif // JPH_USE_CPU_COMPUTE
 }
+
+#endif // defined(JPH_USE_DX12) || defined(JPH_USE_VK) || defined(JPH_USE_MTL) || defined(JPH_USE_CPU_COMPUTE)