ComputeSystemDX12.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  1. // Jolt Physics Library (https://github.com/jrouwe/JoltPhysics)
  2. // SPDX-FileCopyrightText: 2025 Jorrit Rouwe
  3. // SPDX-License-Identifier: MIT
  4. #include <Jolt/Jolt.h>
  5. #ifdef JPH_USE_DX12
  6. #include <Jolt/Compute/DX12/ComputeSystemDX12.h>
  7. #include <Jolt/Compute/DX12/ComputeQueueDX12.h>
  8. #include <Jolt/Compute/DX12/ComputeShaderDX12.h>
  9. #include <Jolt/Compute/DX12/ComputeBufferDX12.h>
  10. #include <Jolt/Core/StringTools.h>
  11. #include <Jolt/Core/UnorderedMap.h>
  12. JPH_SUPPRESS_WARNINGS_STD_BEGIN
  13. JPH_MSVC_SUPPRESS_WARNING(5204) // 'X': class has virtual functions, but its trivial destructor is not virtual; instances of objects derived from this class may not be destructed correctly
  14. JPH_MSVC2026_PLUS_SUPPRESS_WARNING(4865) // wingdi.h(2806,1): '<unnamed-enum-DISPLAYCONFIG_OUTPUT_TECHNOLOGY_OTHER>': the underlying type will change from 'int' to '__int64' when '/Zc:enumTypes' is specified on the command line
  15. #include <fstream>
  16. #include <d3dcompiler.h>
  17. #include <dxcapi.h>
  18. #ifdef JPH_DEBUG
  19. #include <d3d12sdklayers.h>
  20. #endif
  21. JPH_SUPPRESS_WARNINGS_STD_END
  22. JPH_NAMESPACE_BEGIN
  23. void ComputeSystemDX12::Initialize(ID3D12Device *inDevice, EDebug inDebug)
  24. {
  25. mDevice = inDevice;
  26. mDebug = inDebug;
  27. }
  28. void ComputeSystemDX12::Shutdown()
  29. {
  30. mDevice.Reset();
  31. }
  32. ComPtr<ID3D12Resource> ComputeSystemDX12::CreateD3DResource(D3D12_HEAP_TYPE inHeapType, D3D12_RESOURCE_STATES inResourceState, D3D12_RESOURCE_FLAGS inFlags, uint64 inSize)
  33. {
  34. // Create a new resource
  35. D3D12_RESOURCE_DESC desc;
  36. desc.Dimension = D3D12_RESOURCE_DIMENSION_BUFFER;
  37. desc.Alignment = 0;
  38. desc.Width = inSize;
  39. desc.Height = 1;
  40. desc.DepthOrArraySize = 1;
  41. desc.MipLevels = 1;
  42. desc.Format = DXGI_FORMAT_UNKNOWN;
  43. desc.SampleDesc.Count = 1;
  44. desc.SampleDesc.Quality = 0;
  45. desc.Layout = D3D12_TEXTURE_LAYOUT_ROW_MAJOR;
  46. desc.Flags = inFlags;
  47. D3D12_HEAP_PROPERTIES heap_properties = {};
  48. heap_properties.Type = inHeapType;
  49. heap_properties.CPUPageProperty = D3D12_CPU_PAGE_PROPERTY_UNKNOWN;
  50. heap_properties.MemoryPoolPreference = D3D12_MEMORY_POOL_UNKNOWN;
  51. heap_properties.CreationNodeMask = 1;
  52. heap_properties.VisibleNodeMask = 1;
  53. ComPtr<ID3D12Resource> resource;
  54. if (HRFailed(mDevice->CreateCommittedResource(&heap_properties, D3D12_HEAP_FLAG_NONE, &desc, inResourceState, nullptr, IID_PPV_ARGS(&resource))))
  55. return nullptr;
  56. return resource;
  57. }
  58. ComputeShaderResult ComputeSystemDX12::CreateComputeShader(const char *inName, uint32 inGroupSizeX, uint32 inGroupSizeY, uint32 inGroupSizeZ)
  59. {
  60. ComputeShaderResult result;
  61. // Read shader source file
  62. Array<uint8> data;
  63. String error;
  64. String file_name = String(inName) + ".hlsl";
  65. if (!mShaderLoader(file_name.c_str(), data, error))
  66. {
  67. result.SetError(error);
  68. return result;
  69. }
  70. #ifndef JPH_USE_DXC // Use FXC, the old shader compiler?
  71. UINT flags = D3DCOMPILE_ENABLE_STRICTNESS | D3DCOMPILE_WARNINGS_ARE_ERRORS | D3DCOMPILE_ALL_RESOURCES_BOUND;
  72. #ifdef JPH_DEBUG
  73. flags |= D3DCOMPILE_SKIP_OPTIMIZATION;
  74. #else
  75. flags |= D3DCOMPILE_OPTIMIZATION_LEVEL3;
  76. #endif
  77. if (mDebug == EDebug::DebugSymbols)
  78. flags |= D3DCOMPILE_DEBUG;
  79. const D3D_SHADER_MACRO defines[] =
  80. {
  81. { nullptr, nullptr }
  82. };
  83. // Handles loading include files through the shader loader
  84. struct IncludeHandler : public ID3DInclude
  85. {
  86. IncludeHandler(const ShaderLoader &inShaderLoader) : mShaderLoader(inShaderLoader) { }
  87. virtual ~IncludeHandler() = default;
  88. STDMETHOD (Open)(D3D_INCLUDE_TYPE, LPCSTR inFileName, LPCVOID, LPCVOID *outData, UINT *outNumBytes) override
  89. {
  90. // Read the header file
  91. Array<uint8> file_data;
  92. String error;
  93. if (!mShaderLoader(inFileName, file_data, error))
  94. return E_FAIL;
  95. if (file_data.empty())
  96. {
  97. *outData = nullptr;
  98. *outNumBytes = 0;
  99. return S_OK;
  100. }
  101. // Copy to a new memory block
  102. void *mem = CoTaskMemAlloc(file_data.size());
  103. if (mem == nullptr)
  104. return E_OUTOFMEMORY;
  105. memcpy(mem, file_data.data(), file_data.size());
  106. *outData = mem;
  107. *outNumBytes = (UINT)file_data.size();
  108. return S_OK;
  109. }
  110. STDMETHOD (Close)(LPCVOID inData) override
  111. {
  112. if (inData != nullptr)
  113. CoTaskMemFree(const_cast<void *>(inData));
  114. return S_OK;
  115. }
  116. private:
  117. const ShaderLoader & mShaderLoader;
  118. };
  119. IncludeHandler include_handler(mShaderLoader);
  120. // Compile source
  121. ComPtr<ID3DBlob> shader_blob, error_blob;
  122. if (FAILED(D3DCompile(&data[0],
  123. (uint)data.size(),
  124. file_name.c_str(),
  125. defines,
  126. &include_handler,
  127. "main",
  128. "cs_5_0",
  129. flags,
  130. 0,
  131. shader_blob.GetAddressOf(),
  132. error_blob.GetAddressOf())))
  133. {
  134. if (error_blob)
  135. result.SetError((const char *)error_blob->GetBufferPointer());
  136. else
  137. result.SetError("Shader compile error");
  138. return result;
  139. }
  140. // Get shader description
  141. ComPtr<ID3D12ShaderReflection> reflector;
  142. if (FAILED(D3DReflect(shader_blob->GetBufferPointer(), shader_blob->GetBufferSize(), IID_PPV_ARGS(&reflector))))
  143. {
  144. result.SetError("Failed to reflect shader");
  145. return result;
  146. }
  147. #else
  148. ComPtr<IDxcUtils> utils;
  149. DxcCreateInstance(CLSID_DxcUtils, IID_PPV_ARGS(utils.GetAddressOf()));
  150. // Custom include handler that forwards include loads to mShaderLoader
  151. struct DxcIncludeHandler : public IDxcIncludeHandler
  152. {
  153. DxcIncludeHandler(IDxcUtils *inUtils, const ShaderLoader &inLoader) : mUtils(inUtils), mShaderLoader(inLoader) { }
  154. virtual ~DxcIncludeHandler() = default;
  155. STDMETHODIMP QueryInterface(REFIID riid, void **ppvObject) override
  156. {
  157. JPH_ASSERT(false);
  158. return E_NOINTERFACE;
  159. }
  160. STDMETHODIMP_(ULONG) AddRef(void) override
  161. {
  162. // Allocated on the stack, we don't do ref counting
  163. return 1;
  164. }
  165. STDMETHODIMP_(ULONG) Release(void) override
  166. {
  167. // Allocated on the stack, we don't do ref counting
  168. return 1;
  169. }
  170. // IDxcIncludeHandler::LoadSource uses IDxcBlob**
  171. STDMETHODIMP LoadSource(LPCWSTR inFilename, IDxcBlob **outIncludeSource) override
  172. {
  173. *outIncludeSource = nullptr;
  174. // Convert to UTF-8
  175. char file_name[MAX_PATH];
  176. WideCharToMultiByte(CP_UTF8, 0, inFilename, -1, file_name, sizeof(file_name), nullptr, nullptr);
  177. // Load the header
  178. Array<uint8> file_data;
  179. String error;
  180. if (!mShaderLoader(file_name, file_data, error))
  181. return E_FAIL;
  182. // Create a blob from the loaded data
  183. ComPtr<IDxcBlobEncoding> blob_encoder;
  184. HRESULT hr = mUtils->CreateBlob(file_data.empty()? nullptr : file_data.data(), (uint)file_data.size(), CP_UTF8, blob_encoder.GetAddressOf());
  185. if (FAILED(hr))
  186. return hr;
  187. // Return as IDxcBlob
  188. *outIncludeSource = blob_encoder.Detach();
  189. return S_OK;
  190. }
  191. IDxcUtils * mUtils;
  192. const ShaderLoader & mShaderLoader;
  193. };
  194. DxcIncludeHandler include_handler(utils.Get(), mShaderLoader);
  195. ComPtr<IDxcBlobEncoding> source;
  196. if (HRFailed(utils->CreateBlob(data.data(), (uint)data.size(), CP_UTF8, source.GetAddressOf()), result))
  197. return result;
  198. ComPtr<IDxcCompiler3> compiler;
  199. DxcCreateInstance(CLSID_DxcCompiler, IID_PPV_ARGS(compiler.GetAddressOf()));
  200. Array<LPCWSTR> arguments;
  201. arguments.push_back(L"-E");
  202. arguments.push_back(L"main");
  203. arguments.push_back(L"-T");
  204. arguments.push_back(L"cs_6_0");
  205. arguments.push_back(DXC_ARG_WARNINGS_ARE_ERRORS);
  206. arguments.push_back(DXC_ARG_OPTIMIZATION_LEVEL3);
  207. arguments.push_back(DXC_ARG_ALL_RESOURCES_BOUND);
  208. if (mDebug == EDebug::DebugSymbols)
  209. {
  210. arguments.push_back(DXC_ARG_DEBUG);
  211. arguments.push_back(L"-Qembed_debug");
  212. }
  213. // Provide file name so tools know what the original shader was called (the actual source comes from the blob)
  214. wchar_t w_file_name[MAX_PATH];
  215. MultiByteToWideChar(CP_UTF8, 0, file_name.c_str(), -1, w_file_name, MAX_PATH);
  216. arguments.push_back(w_file_name);
  217. // Compile the shader
  218. DxcBuffer source_buffer;
  219. source_buffer.Ptr = source->GetBufferPointer();
  220. source_buffer.Size = source->GetBufferSize();
  221. source_buffer.Encoding = 0;
  222. ComPtr<IDxcResult> compile_result;
  223. if (FAILED(compiler->Compile(&source_buffer, arguments.data(), (uint32)arguments.size(), &include_handler, IID_PPV_ARGS(compile_result.GetAddressOf()))))
  224. {
  225. result.SetError("Failed to compile shader");
  226. return result;
  227. }
  228. // Check for compilation errors
  229. ComPtr<IDxcBlobUtf8> errors;
  230. compile_result->GetOutput(DXC_OUT_ERRORS, IID_PPV_ARGS(errors.GetAddressOf()), nullptr);
  231. if (errors != nullptr && errors->GetStringLength() > 0)
  232. {
  233. result.SetError((const char *)errors->GetBufferPointer());
  234. return result;
  235. }
  236. // Get the compiled shader code
  237. ComPtr<ID3DBlob> shader_blob;
  238. if (HRFailed(compile_result->GetOutput(DXC_OUT_OBJECT, IID_PPV_ARGS(shader_blob.GetAddressOf()), nullptr), result))
  239. return result;
  240. // Get reflection data
  241. ComPtr<IDxcBlob> reflection_data;
  242. if (HRFailed(compile_result->GetOutput(DXC_OUT_REFLECTION, IID_PPV_ARGS(reflection_data.GetAddressOf()), nullptr), result))
  243. return result;
  244. DxcBuffer reflection_buffer;
  245. reflection_buffer.Ptr = reflection_data->GetBufferPointer();
  246. reflection_buffer.Size = reflection_data->GetBufferSize();
  247. reflection_buffer.Encoding = 0;
  248. ComPtr<ID3D12ShaderReflection> reflector;
  249. if (HRFailed(utils->CreateReflection(&reflection_buffer, IID_PPV_ARGS(reflector.GetAddressOf())), result))
  250. return result;
  251. #endif // JPH_USE_DXC
  252. // Get the shader description
  253. D3D12_SHADER_DESC shader_desc;
  254. if (HRFailed(reflector->GetDesc(&shader_desc), result))
  255. return result;
  256. // Verify that the group sizes match the shader's thread group size
  257. UINT thread_group_size_x, thread_group_size_y, thread_group_size_z;
  258. if (HRFailed(reflector->GetThreadGroupSize(&thread_group_size_x, &thread_group_size_y, &thread_group_size_z), result))
  259. return result;
  260. JPH_ASSERT(inGroupSizeX == thread_group_size_x, "Group size X mismatch");
  261. JPH_ASSERT(inGroupSizeY == thread_group_size_y, "Group size Y mismatch");
  262. JPH_ASSERT(inGroupSizeZ == thread_group_size_z, "Group size Z mismatch");
  263. // Convert parameters to root signature description
  264. Array<String> binding_names;
  265. binding_names.reserve(shader_desc.BoundResources);
  266. UnorderedMap<string_view, uint> name_to_index;
  267. Array<D3D12_ROOT_PARAMETER1> root_params;
  268. for (UINT i = 0; i < shader_desc.BoundResources; ++i)
  269. {
  270. D3D12_SHADER_INPUT_BIND_DESC bind_desc;
  271. reflector->GetResourceBindingDesc(i, &bind_desc);
  272. D3D12_ROOT_PARAMETER1 param = {};
  273. param.ShaderVisibility = D3D12_SHADER_VISIBILITY_ALL;
  274. switch (bind_desc.Type)
  275. {
  276. case D3D_SIT_CBUFFER:
  277. param.ParameterType = D3D12_ROOT_PARAMETER_TYPE_CBV;
  278. break;
  279. case D3D_SIT_STRUCTURED:
  280. case D3D_SIT_BYTEADDRESS:
  281. param.ParameterType = D3D12_ROOT_PARAMETER_TYPE_SRV;
  282. break;
  283. case D3D_SIT_UAV_RWTYPED:
  284. case D3D_SIT_UAV_RWSTRUCTURED:
  285. case D3D_SIT_UAV_RWBYTEADDRESS:
  286. case D3D_SIT_UAV_APPEND_STRUCTURED:
  287. case D3D_SIT_UAV_CONSUME_STRUCTURED:
  288. case D3D_SIT_UAV_RWSTRUCTURED_WITH_COUNTER:
  289. param.ParameterType = D3D12_ROOT_PARAMETER_TYPE_UAV;
  290. break;
  291. case D3D_SIT_TBUFFER:
  292. case D3D_SIT_TEXTURE:
  293. case D3D_SIT_SAMPLER:
  294. case D3D_SIT_RTACCELERATIONSTRUCTURE:
  295. case D3D_SIT_UAV_FEEDBACKTEXTURE:
  296. JPH_ASSERT(false, "Unsupported shader input type");
  297. continue;
  298. }
  299. param.Descriptor.RegisterSpace = bind_desc.Space;
  300. param.Descriptor.ShaderRegister = bind_desc.BindPoint;
  301. param.Descriptor.Flags = D3D12_ROOT_DESCRIPTOR_FLAG_DATA_VOLATILE;
  302. binding_names.push_back(bind_desc.Name); // Add all strings to a pool to keep them alive
  303. name_to_index[string_view(binding_names.back())] = (uint)root_params.size();
  304. root_params.push_back(param);
  305. }
  306. // Create the root signature
  307. D3D12_VERSIONED_ROOT_SIGNATURE_DESC root_sig_desc = {};
  308. root_sig_desc.Version = D3D_ROOT_SIGNATURE_VERSION_1_1;
  309. root_sig_desc.Desc_1_1.NumParameters = (UINT)root_params.size();
  310. root_sig_desc.Desc_1_1.pParameters = root_params.data();
  311. root_sig_desc.Desc_1_1.NumStaticSamplers = 0;
  312. root_sig_desc.Desc_1_1.pStaticSamplers = nullptr;
  313. root_sig_desc.Desc_1_1.Flags = D3D12_ROOT_SIGNATURE_FLAG_NONE;
  314. ComPtr<ID3DBlob> serialized_sig;
  315. ComPtr<ID3DBlob> root_sig_error_blob;
  316. if (FAILED(D3D12SerializeVersionedRootSignature(&root_sig_desc, &serialized_sig, &root_sig_error_blob)))
  317. {
  318. if (root_sig_error_blob)
  319. {
  320. error = StringFormat("Failed to create root signature: %s", (const char *)root_sig_error_blob->GetBufferPointer());
  321. result.SetError(error);
  322. }
  323. else
  324. result.SetError("Failed to create root signature");
  325. return result;
  326. }
  327. ComPtr<ID3D12RootSignature> root_sig;
  328. if (FAILED(mDevice->CreateRootSignature(0, serialized_sig->GetBufferPointer(), serialized_sig->GetBufferSize(), IID_PPV_ARGS(&root_sig))))
  329. {
  330. result.SetError("Failed to create root signature");
  331. return result;
  332. }
  333. // Create a pipeline state object from the root signature and the shader
  334. ComPtr<ID3D12PipelineState> pipeline_state;
  335. D3D12_COMPUTE_PIPELINE_STATE_DESC compute_state_desc = {};
  336. compute_state_desc.pRootSignature = root_sig.Get();
  337. compute_state_desc.CS = { shader_blob->GetBufferPointer(), shader_blob->GetBufferSize() };
  338. if (FAILED(mDevice->CreateComputePipelineState(&compute_state_desc, IID_PPV_ARGS(&pipeline_state))))
  339. {
  340. result.SetError("Failed to create compute pipeline state");
  341. return result;
  342. }
  343. // Set name on DX12 objects for easier debugging
  344. wchar_t w_name[1024];
  345. size_t converted_chars = 0;
  346. mbstowcs_s(&converted_chars, w_name, 1024, inName, _TRUNCATE);
  347. pipeline_state->SetName(w_name);
  348. result.Set(new ComputeShaderDX12(shader_blob, root_sig, pipeline_state, std::move(binding_names), std::move(name_to_index), inGroupSizeX, inGroupSizeY, inGroupSizeZ));
  349. return result;
  350. }
  351. ComputeBufferResult ComputeSystemDX12::CreateComputeBuffer(ComputeBuffer::EType inType, uint64 inSize, uint inStride, const void *inData)
  352. {
  353. ComputeBufferResult result;
  354. Ref<ComputeBufferDX12> buffer = new ComputeBufferDX12(this, inType, inSize, inStride);
  355. if (!buffer->Initialize(inData))
  356. {
  357. result.SetError("Failed to create compute buffer");
  358. return result;
  359. }
  360. result.Set(buffer.GetPtr());
  361. return result;
  362. }
  363. ComputeQueueResult ComputeSystemDX12::CreateComputeQueue()
  364. {
  365. ComputeQueueResult result;
  366. Ref<ComputeQueueDX12> queue = new ComputeQueueDX12();
  367. if (!queue->Initialize(mDevice.Get(), D3D12_COMMAND_LIST_TYPE_COMPUTE, result))
  368. return result;
  369. result.Set(queue.GetPtr());
  370. return result;
  371. }
  372. JPH_NAMESPACE_END
  373. #endif // JPH_USE_DX12