ComputeTests.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  1. // Jolt Physics Library (https://github.com/jrouwe/JoltPhysics)
  2. // SPDX-FileCopyrightText: 2025 Jorrit Rouwe
  3. // SPDX-License-Identifier: MIT
  4. #include "UnitTestFramework.h"
  5. #if defined(JPH_USE_DX12) || defined(JPH_USE_VK) || defined(JPH_USE_MTL) || defined(JPH_USE_CPU_COMPUTE)
  6. #include <Jolt/Compute/ComputeSystem.h>
  7. #include <Jolt/Compute/CPU/ComputeSystemCPU.h>
  8. #include <Jolt/Shaders/TestComputeBindings.h>
  9. #include <Jolt/Shaders/TestCompute2Bindings.h>
  10. #include <Jolt/Core/IncludeWindows.h>
  11. #include <Jolt/Core/RTTI.h>
  12. JPH_SUPPRESS_WARNINGS_STD_BEGIN
  13. #include <fstream>
  14. #include <filesystem>
  15. #ifdef JPH_PLATFORM_LINUX
  16. #include <unistd.h>
  17. #endif
  18. JPH_SUPPRESS_WARNINGS_STD_END
  19. #if defined(JPH_PLATFORM_MACOS) || defined(JPH_PLATFORM_IOS)
  20. #include <CoreFoundation/CoreFoundation.h>
  21. #endif
  22. #ifdef JPH_USE_CPU_COMPUTE
  23. JPH_DECLARE_REGISTER_SHADER(TestCompute)
  24. JPH_DECLARE_REGISTER_SHADER(TestCompute2)
  25. #endif // JPH_USE_CPU_COMPUTE
  26. JPH_NAMESPACE_BEGIN
  27. #ifdef JPH_USE_DX12
  28. JPH_DECLARE_RTTI_FOR_FACTORY(JPH_EXPORT, ComputeSystem)
  29. JPH_DECLARE_RTTI_FOR_FACTORY(JPH_EXPORT, ComputeSystemDX12)
  30. JPH_DECLARE_RTTI_FOR_FACTORY(JPH_EXPORT, ComputeSystemDX12Impl)
  31. #endif
  32. #ifdef JPH_USE_VK
  33. JPH_DECLARE_RTTI_FOR_FACTORY(JPH_EXPORT, ComputeSystemVK)
  34. JPH_DECLARE_RTTI_FOR_FACTORY(JPH_EXPORT, ComputeSystemVKWithAllocator)
  35. JPH_DECLARE_RTTI_FOR_FACTORY(JPH_EXPORT, ComputeSystemVKImpl)
  36. #endif
  37. #ifdef JPH_USE_MTL
  38. JPH_DECLARE_RTTI_FOR_FACTORY(JPH_EXPORT, ComputeSystemMTL)
  39. JPH_DECLARE_RTTI_FOR_FACTORY(JPH_EXPORT, ComputeSystemMTLImpl)
  40. #endif
  41. #ifdef JPH_USE_CPU_COMPUTE
  42. JPH_DECLARE_RTTI_FOR_FACTORY(JPH_EXPORT, ComputeSystemCPU)
  43. #endif
  44. JPH_DECLARE_RTTI_FOR_FACTORY(JPH_EXPORT, ShapeSettings)
  45. JPH_NAMESPACE_END
  46. TEST_SUITE("ComputeTests")
  47. {
  48. static const char *cInvalidShaderName = "InvalidShader";
  49. static const char *cInvalidShaderCode = "invalid_shader_code";
  50. static void RunTests(ComputeSystem *inComputeSystem)
  51. {
  52. inComputeSystem->mShaderLoader = [](const char *inName, Array<uint8> &outData, String &outError) {
  53. // Special case to test what happens when an invalid file is returned
  54. if (strstr(inName, cInvalidShaderName) != nullptr)
  55. {
  56. outData.assign(cInvalidShaderCode, cInvalidShaderCode + strlen(cInvalidShaderCode));
  57. return true;
  58. }
  59. #if defined(JPH_PLATFORM_MACOS) || defined(JPH_PLATFORM_IOS)
  60. // In macOS the shaders are copied to the bundle
  61. CFBundleRef bundle = CFBundleGetMainBundle();
  62. CFURLRef resources = CFBundleCopyResourcesDirectoryURL(bundle);
  63. CFURLRef absolute = CFURLCopyAbsoluteURL(resources);
  64. CFRelease(resources);
  65. CFStringRef path_string = CFURLCopyFileSystemPath(absolute, kCFURLPOSIXPathStyle);
  66. CFRelease(absolute);
  67. char path[PATH_MAX];
  68. CFStringGetCString(path_string, path, PATH_MAX, kCFStringEncodingUTF8);
  69. CFRelease(path_string);
  70. String base_path = String(path) + "/Jolt/Shaders/";
  71. #else
  72. // On other platforms, start searching up from the application path
  73. #ifdef JPH_PLATFORM_WINDOWS
  74. char application_path[MAX_PATH] = { 0 };
  75. GetModuleFileName(nullptr, application_path, MAX_PATH);
  76. #elif defined(JPH_PLATFORM_LINUX)
  77. char application_path[PATH_MAX] = { 0 };
  78. int count = readlink("/proc/self/exe", application_path, PATH_MAX);
  79. if (count > 0)
  80. application_path[count] = 0;
  81. #else
  82. // Not implemented
  83. const char *application_path = "";
  84. #endif
  85. String base_path;
  86. filesystem::path shader_path(application_path);
  87. while (!shader_path.empty())
  88. {
  89. filesystem::path parent_path = shader_path.parent_path();
  90. if (parent_path == shader_path)
  91. break;
  92. shader_path = parent_path;
  93. filesystem::path full_path = shader_path / "Jolt" / "Shaders" / "";
  94. if (filesystem::exists(full_path))
  95. {
  96. base_path = String(full_path.string());
  97. break;
  98. }
  99. }
  100. #endif
  101. // Open file
  102. std::ifstream input((base_path + inName).c_str(), std::ios::in | std::ios::binary);
  103. if (!input.is_open())
  104. {
  105. outError = String("Could not open shader file: ") + base_path + inName;
  106. #if defined(JPH_PLATFORM_MACOS) || defined(JPH_PLATFORM_IOS)
  107. outError += "\nThis can fail on macOS when dxc or spirv-cross could not be found so the shaders could not be compiled.";
  108. #endif
  109. return false;
  110. }
  111. // Read contents of file
  112. input.seekg(0, ios_base::end);
  113. ifstream::pos_type length = input.tellg();
  114. input.seekg(0, ios_base::beg);
  115. outData.resize(size_t(length));
  116. if (length == 0)
  117. return true;
  118. input.read((char *)&outData[0], length);
  119. return true;
  120. };
  121. // Create a queue
  122. ComputeQueueResult queue_result = inComputeSystem->CreateComputeQueue();
  123. CHECK(!queue_result.HasError());
  124. Ref<ComputeQueue> queue = queue_result.Get();
  125. CHECK(queue != nullptr);
  126. // Test failing shader creation
  127. {
  128. ComputeShaderResult shader_result = inComputeSystem->CreateComputeShader("NonExistingShader", 64);
  129. CHECK(shader_result.HasError());
  130. }
  131. {
  132. constexpr uint32 cNumElements = 1234; // Not a multiple of cTestComputeGroupSize
  133. constexpr uint32 cNumIterations = 10;
  134. constexpr JPH_float3 cFloat3Value = JPH_float3(0, 0, 0);
  135. constexpr JPH_float3 cFloat3Value2 = JPH_float3(0, 13, 0);
  136. constexpr uint32 cUIntValue = 7;
  137. constexpr uint32 cUploadValue = 42;
  138. // Can't change context buffer while commands are queued, so create multiple constant buffers
  139. Ref<ComputeBuffer> context[cNumIterations];
  140. for (uint32 iter = 0; iter < cNumIterations; ++iter)
  141. {
  142. ComputeBufferResult buffer_result = inComputeSystem->CreateComputeBuffer(ComputeBuffer::EType::ConstantBuffer, 1, sizeof(TestComputeContext));
  143. CHECK(!buffer_result.HasError());
  144. context[iter] = buffer_result.Get();
  145. }
  146. CHECK(context != nullptr);
  147. // Create an upload buffer
  148. ComputeBufferResult upload_buffer_result = inComputeSystem->CreateComputeBuffer(ComputeBuffer::EType::UploadBuffer, 1, sizeof(uint32));
  149. CHECK(!upload_buffer_result.HasError());
  150. Ref<ComputeBuffer> upload_buffer = upload_buffer_result.Get();
  151. CHECK(upload_buffer != nullptr);
  152. uint32 *upload_data = upload_buffer->Map<uint32>(ComputeBuffer::EMode::Write);
  153. upload_data[0] = cUploadValue;
  154. upload_buffer->Unmap();
  155. // Create a read buffer
  156. UnitTestRandom rnd;
  157. Array<uint32> optional_data(cNumElements);
  158. for (uint32 &d : optional_data)
  159. d = rnd();
  160. ComputeBufferResult optional_buffer_result = inComputeSystem->CreateComputeBuffer(ComputeBuffer::EType::Buffer, cNumElements, sizeof(uint32), optional_data.data());
  161. CHECK(!optional_buffer_result.HasError());
  162. Ref<ComputeBuffer> optional_buffer = optional_buffer_result.Get();
  163. CHECK(optional_buffer != nullptr);
  164. // Create a read-write buffer
  165. ComputeBufferResult buffer_result = inComputeSystem->CreateComputeBuffer(ComputeBuffer::EType::RWBuffer, cNumElements, sizeof(uint32));
  166. CHECK(!buffer_result.HasError());
  167. Ref<ComputeBuffer> buffer = buffer_result.Get();
  168. CHECK(buffer != nullptr);
  169. // Create a read back buffer
  170. ComputeBufferResult readback_buffer_result = buffer->CreateReadBackBuffer();
  171. CHECK(!readback_buffer_result.HasError());
  172. Ref<ComputeBuffer> readback_buffer = readback_buffer_result.Get();
  173. CHECK(readback_buffer != nullptr);
  174. // Create the shader
  175. ComputeShaderResult shader_result = inComputeSystem->CreateComputeShader("TestCompute", cTestComputeGroupSize);
  176. if (shader_result.HasError())
  177. {
  178. Trace("Shader could not be created: %s", shader_result.GetError().c_str());
  179. return;
  180. }
  181. Ref<ComputeShader> shader = shader_result.Get();
  182. CHECK(shader != nullptr);
  183. // Schedule work
  184. for (uint32 iter = 0; iter < cNumIterations; ++iter)
  185. {
  186. // Fill in the context
  187. TestComputeContext *value = context[iter]->Map<TestComputeContext>(ComputeBuffer::EMode::Write);
  188. value->cFloat3Value = cFloat3Value;
  189. value->cUIntValue = cUIntValue;
  190. value->cFloat3Value2 = cFloat3Value2;
  191. value->cUIntValue2 = iter;
  192. value->cNumElements = cNumElements;
  193. context[iter]->Unmap();
  194. queue->SetShader(shader);
  195. queue->SetConstantBuffer("gContext", context[iter]);
  196. context[iter] = nullptr; // Release the reference to ensure the queue keeps ownership
  197. queue->SetBuffer("gOptionalData", optional_buffer);
  198. optional_buffer = nullptr; // Release the reference so we test that the queue keeps ownership and that in the 2nd iteration we can set a null buffer
  199. queue->SetBuffer("gUploadData", upload_buffer);
  200. queue->SetRWBuffer("gData", buffer);
  201. queue->Dispatch((cNumElements + cTestComputeGroupSize - 1) / cTestComputeGroupSize);
  202. }
  203. // Run all queued commands
  204. queue->ScheduleReadback(readback_buffer, buffer);
  205. queue->ExecuteAndWait();
  206. // Calculate the expected result
  207. Array<uint32> expected_data(cNumElements);
  208. for (uint32 iter = 0; iter < cNumIterations; ++iter)
  209. {
  210. // Copy of the shader logic
  211. uint cUIntValue2 = iter;
  212. if (cUIntValue2 == 0)
  213. {
  214. // First write, uses optional data and tests that the packing of float3/uint3's works
  215. for (uint32 i = 0; i < cNumElements; ++i)
  216. expected_data[i] = optional_data[i] + int(cFloat3Value2.y) + cUploadValue;
  217. }
  218. else
  219. {
  220. // Read-modify-write gData
  221. for (uint32 i = 0; i < cNumElements; ++i)
  222. expected_data[i] = (expected_data[i] + cUIntValue) * cUIntValue2;
  223. }
  224. }
  225. // Compare computed data with expected data
  226. uint32 *data = readback_buffer->Map<uint32>(ComputeBuffer::EMode::Read);
  227. for (uint32 i = 0; i < cNumElements; ++i)
  228. CHECK(data[i] == expected_data[i]);
  229. readback_buffer->Unmap();
  230. }
  231. // Test helper functions
  232. {
  233. // Create the shader
  234. ComputeShaderResult shader_result = inComputeSystem->CreateComputeShader("TestCompute2", cTestCompute2GroupSize);
  235. if (shader_result.HasError())
  236. {
  237. Trace("Shader could not be created: %s", shader_result.GetError().c_str());
  238. return;
  239. }
  240. Ref<ComputeShader> shader = shader_result.Get();
  241. CHECK(shader != nullptr);
  242. const Mat44 cMat44Value(Vec4(2, 3, 5, 0), Vec4(7, 11, 13, 0), Vec4(13, 15, 17, 0), Vec4(17, 19, 23, 0));
  243. const Vec3 cMat44MulValue(29, 31, 37);
  244. const Vec3 cDecompressedVec3(Vec3(-2, 3, -5).Normalized());
  245. const uint32 cCompressedVec3 = cDecompressedVec3.CompressUnitVector();
  246. const Quat cDecompressedQuat(Vec4(2, -3, 5, -7).Normalized());
  247. const uint32 cCompressedQuat = cDecompressedQuat.CompressUnitQuat();
  248. // Generate input data
  249. TestCompute2Input input;
  250. cMat44Value.StoreFloat4x4(input.mMat44Value);
  251. cMat44MulValue.StoreFloat3(&input.mMat44MulValue);
  252. input.mCompressedVec3 = cCompressedVec3;
  253. input.mCompressedQuat = cCompressedQuat;
  254. // Create input buffer
  255. ComputeBufferResult buffer_result = inComputeSystem->CreateComputeBuffer(ComputeBuffer::EType::Buffer, 1, sizeof(TestCompute2Input), &input);
  256. CHECK(!buffer_result.HasError());
  257. Ref<ComputeBuffer> input_buffer = buffer_result.Get();
  258. // Create a read-write buffer for the output
  259. buffer_result = inComputeSystem->CreateComputeBuffer(ComputeBuffer::EType::RWBuffer, 1, sizeof(TestCompute2Output));
  260. CHECK(!buffer_result.HasError());
  261. Ref<ComputeBuffer> output_buffer = buffer_result.Get();
  262. CHECK(output_buffer != nullptr);
  263. // Create a read back buffer
  264. buffer_result = output_buffer->CreateReadBackBuffer();
  265. CHECK(!buffer_result.HasError());
  266. Ref<ComputeBuffer> readback_buffer = buffer_result.Get();
  267. CHECK(readback_buffer != nullptr);
  268. // Execute the shader
  269. queue->SetShader(shader);
  270. queue->SetBuffer("gInput", input_buffer);
  271. queue->SetRWBuffer("gOutput", output_buffer);
  272. queue->Dispatch(1);
  273. queue->ScheduleReadback(readback_buffer, output_buffer);
  274. queue->ExecuteAndWait();
  275. // Verify the output
  276. TestCompute2Output *output = readback_buffer->Map<TestCompute2Output>(ComputeBuffer::EMode::Read);
  277. const Vec3 expected_mul3x4 = cMat44Value * cMat44MulValue;
  278. CHECK(Vec3(output->mMul3x4Output) == expected_mul3x4);
  279. const Vec3 expected_mul3x3 = cMat44Value.Multiply3x3(cMat44MulValue);
  280. CHECK(Vec3(output->mMul3x3Output) == expected_mul3x3);
  281. const Vec3 expected_decompressed_vec3 = Vec3::sDecompressUnitVector(cCompressedVec3);
  282. CHECK(Vec3(output->mDecompressedVec3).IsClose(expected_decompressed_vec3));
  283. const Quat expected_decompressed_quat = Quat::sDecompressUnitQuat(cCompressedQuat);
  284. CHECK(Quat(output->mDecompressedQuat).IsClose(expected_decompressed_quat));
  285. readback_buffer->Unmap();
  286. }
  287. }
  288. #ifdef JPH_USE_DX12
  289. TEST_CASE("TestComputeDX12")
  290. {
  291. ComputeSystemResult compute_system = CreateComputeSystemDX12();
  292. CHECK(!compute_system.HasError());
  293. if (!compute_system.HasError())
  294. {
  295. CHECK(compute_system.Get() != nullptr);
  296. CHECK(IsKindOf(compute_system.Get(), JPH_RTTI(ComputeSystem)));
  297. CHECK(IsKindOf(compute_system.Get(), JPH_RTTI(ComputeSystemDX12)));
  298. CHECK(IsKindOf(compute_system.Get(), JPH_RTTI(ComputeSystemDX12Impl)));
  299. CHECK(!IsKindOf(compute_system.Get(), JPH_RTTI(ShapeSettings)));
  300. RunTests(compute_system.Get());
  301. // Test failing shader compilation
  302. {
  303. ComputeShaderResult shader_result = compute_system.Get()->CreateComputeShader(cInvalidShaderName, 64);
  304. CHECK(shader_result.HasError());
  305. CHECK(strstr(shader_result.GetError().c_str(), cInvalidShaderCode) != nullptr); // Assume that the error message contains the invalid code
  306. }
  307. }
  308. }
  309. #endif // JPH_USE_DX12
  310. #ifdef JPH_USE_MTL
  311. TEST_CASE("TestComputeMTL")
  312. {
  313. ComputeSystemResult compute_system = CreateComputeSystemMTL();
  314. CHECK(!compute_system.HasError());
  315. if (!compute_system.HasError())
  316. {
  317. CHECK(compute_system.Get() != nullptr);
  318. CHECK(IsKindOf(compute_system.Get(), JPH_RTTI(ComputeSystem)));
  319. CHECK(IsKindOf(compute_system.Get(), JPH_RTTI(ComputeSystemMTL)));
  320. CHECK(IsKindOf(compute_system.Get(), JPH_RTTI(ComputeSystemMTLImpl)));
  321. CHECK(!IsKindOf(compute_system.Get(), JPH_RTTI(ShapeSettings)));
  322. RunTests(compute_system.Get());
  323. }
  324. }
  325. #endif // JPH_USE_MTL
  326. #ifdef JPH_USE_VK
  327. TEST_CASE("TestComputeVK")
  328. {
  329. ComputeSystemResult compute_system = CreateComputeSystemVK();
  330. CHECK(!compute_system.HasError());
  331. if (!compute_system.HasError())
  332. {
  333. CHECK(compute_system.Get() != nullptr);
  334. CHECK(IsKindOf(compute_system.Get(), JPH_RTTI(ComputeSystem)));
  335. CHECK(IsKindOf(compute_system.Get(), JPH_RTTI(ComputeSystemVK)));
  336. CHECK(IsKindOf(compute_system.Get(), JPH_RTTI(ComputeSystemVKWithAllocator)));
  337. CHECK(IsKindOf(compute_system.Get(), JPH_RTTI(ComputeSystemVKImpl)));
  338. CHECK(!IsKindOf(compute_system.Get(), JPH_RTTI(ShapeSettings)));
  339. RunTests(compute_system.Get());
  340. }
  341. }
  342. #endif // JPH_USE_VK
  343. #ifdef JPH_USE_CPU_COMPUTE
  344. TEST_CASE("TestComputeCPU")
  345. {
  346. ComputeSystemResult compute_system = CreateComputeSystemCPU();
  347. CHECK(!compute_system.HasError());
  348. if (!compute_system.HasError())
  349. {
  350. CHECK(compute_system.Get() != nullptr);
  351. CHECK(IsKindOf(compute_system.Get(), JPH_RTTI(ComputeSystem)));
  352. CHECK(IsKindOf(compute_system.Get(), JPH_RTTI(ComputeSystemCPU)));
  353. CHECK(!IsKindOf(compute_system.Get(), JPH_RTTI(ShapeSettings)));
  354. JPH_REGISTER_SHADER(StaticCast<ComputeSystemCPU>(compute_system.Get()), TestCompute);
  355. JPH_REGISTER_SHADER(StaticCast<ComputeSystemCPU>(compute_system.Get()), TestCompute2);
  356. RunTests(compute_system.Get());
  357. }
  358. }
  359. #endif // JPH_USE_CPU_COMPUTE
  360. }
  361. #endif // defined(JPH_USE_DX12) || defined(JPH_USE_VK) || defined(JPH_USE_MTL) || defined(JPH_USE_CPU_COMPUTE)