RendererDX12.cpp 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708
  1. // Jolt Physics Library (https://github.com/jrouwe/JoltPhysics)
  2. // SPDX-FileCopyrightText: 2024 Jorrit Rouwe
  3. // SPDX-License-Identifier: MIT
  4. #include <TestFramework.h>
  5. #include <Renderer/DX12/RendererDX12.h>
  6. #include <Renderer/DX12/RenderPrimitiveDX12.h>
  7. #include <Renderer/DX12/PipelineStateDX12.h>
  8. #include <Renderer/DX12/VertexShaderDX12.h>
  9. #include <Renderer/DX12/PixelShaderDX12.h>
  10. #include <Renderer/DX12/TextureDX12.h>
  11. #include <Renderer/DX12/RenderInstancesDX12.h>
  12. #include <Renderer/DX12/FatalErrorIfFailedDX12.h>
  13. #include <Jolt/Core/Profiler.h>
  14. #include <Utils/ReadData.h>
  15. #include <Utils/Log.h>
  16. #include <d3dcompiler.h>
  17. #ifdef JPH_DEBUG
  18. #include <d3d12sdklayers.h>
  19. #endif
  20. RendererDX12::~RendererDX12()
  21. {
  22. // Ensure that the GPU is no longer referencing resources that are about to be cleaned up by the destructor.
  23. WaitForGpu();
  24. // Don't add more stuff to the delay reference list
  25. mIsExiting = true;
  26. CloseHandle(mFenceEvent);
  27. }
  28. void RendererDX12::WaitForGpu()
  29. {
  30. // Schedule a Signal command in the queue
  31. UINT64 current_fence_value = mFenceValues[mFrameIndex];
  32. FatalErrorIfFailed(mCommandQueue->Signal(mFence.Get(), current_fence_value));
  33. // Wait until the fence has been processed
  34. FatalErrorIfFailed(mFence->SetEventOnCompletion(current_fence_value, mFenceEvent));
  35. WaitForSingleObjectEx(mFenceEvent, INFINITE, FALSE);
  36. // Increment the fence value for all frames
  37. for (uint n = 0; n < cFrameCount; ++n)
  38. mFenceValues[n] = current_fence_value + 1;
  39. // Release all used resources
  40. for (Array<ComPtr<ID3D12Object>> &list : mDelayReleased)
  41. list.clear();
  42. // Anything that's not used yet can be removed, delayed objects are now available
  43. mResourceCache.clear();
  44. mDelayCached[mFrameIndex].swap(mResourceCache);
  45. }
  46. void RendererDX12::CreateRenderTargets()
  47. {
  48. // Create render targets and views
  49. for (uint n = 0; n < cFrameCount; ++n)
  50. {
  51. mRenderTargetViews[n] = mRTVHeap.Allocate();
  52. FatalErrorIfFailed(mSwapChain->GetBuffer(n, IID_PPV_ARGS(&mRenderTargets[n])));
  53. mDevice->CreateRenderTargetView(mRenderTargets[n].Get(), nullptr, mRenderTargetViews[n]);
  54. }
  55. }
  56. void RendererDX12::CreateDepthBuffer()
  57. {
  58. // Free any previous depth stencil view
  59. if (mDepthStencilView.ptr != 0)
  60. mDSVHeap.Free(mDepthStencilView);
  61. // Free any previous depth stencil buffer
  62. mDepthStencilBuffer.Reset();
  63. // Allocate depth stencil buffer
  64. D3D12_CLEAR_VALUE clear_value = {};
  65. clear_value.Format = DXGI_FORMAT_D32_FLOAT;
  66. clear_value.DepthStencil.Depth = 0.0f;
  67. clear_value.DepthStencil.Stencil = 0;
  68. D3D12_HEAP_PROPERTIES heap_properties = {};
  69. heap_properties.Type = D3D12_HEAP_TYPE_DEFAULT;
  70. heap_properties.CPUPageProperty = D3D12_CPU_PAGE_PROPERTY_UNKNOWN;
  71. heap_properties.MemoryPoolPreference = D3D12_MEMORY_POOL_UNKNOWN;
  72. heap_properties.CreationNodeMask = 1;
  73. heap_properties.VisibleNodeMask = 1;
  74. D3D12_RESOURCE_DESC depth_stencil_desc = {};
  75. depth_stencil_desc.Dimension = D3D12_RESOURCE_DIMENSION_TEXTURE2D;
  76. depth_stencil_desc.Alignment = 0;
  77. depth_stencil_desc.Width = mWindowWidth;
  78. depth_stencil_desc.Height = mWindowHeight;
  79. depth_stencil_desc.DepthOrArraySize = 1;
  80. depth_stencil_desc.MipLevels = 1;
  81. depth_stencil_desc.Format = DXGI_FORMAT_D32_FLOAT;
  82. depth_stencil_desc.SampleDesc.Count = 1;
  83. depth_stencil_desc.SampleDesc.Quality = 0;
  84. depth_stencil_desc.Layout = D3D12_TEXTURE_LAYOUT_UNKNOWN;
  85. depth_stencil_desc.Flags = D3D12_RESOURCE_FLAG_ALLOW_DEPTH_STENCIL;
  86. FatalErrorIfFailed(mDevice->CreateCommittedResource(&heap_properties, D3D12_HEAP_FLAG_NONE, &depth_stencil_desc, D3D12_RESOURCE_STATE_DEPTH_WRITE, &clear_value, IID_PPV_ARGS(&mDepthStencilBuffer)));
  87. // Allocate depth stencil view
  88. D3D12_DEPTH_STENCIL_VIEW_DESC depth_stencil_view_desc = {};
  89. depth_stencil_view_desc.Format = DXGI_FORMAT_D32_FLOAT;
  90. depth_stencil_view_desc.ViewDimension = D3D12_DSV_DIMENSION_TEXTURE2D;
  91. depth_stencil_view_desc.Flags = D3D12_DSV_FLAG_NONE;
  92. mDepthStencilView = mDSVHeap.Allocate();
  93. mDevice->CreateDepthStencilView(mDepthStencilBuffer.Get(), &depth_stencil_view_desc, mDepthStencilView);
  94. }
  95. void RendererDX12::Initialize()
  96. {
  97. Renderer::Initialize();
  98. #if defined(JPH_DEBUG)
  99. // Enable the D3D12 debug layer
  100. ComPtr<ID3D12Debug> debug_controller;
  101. if (SUCCEEDED(D3D12GetDebugInterface(IID_PPV_ARGS(&debug_controller))))
  102. debug_controller->EnableDebugLayer();
  103. #endif
  104. // Create DXGI factory
  105. FatalErrorIfFailed(CreateDXGIFactory1(IID_PPV_ARGS(&mDXGIFactory)));
  106. // Find adapter
  107. ComPtr<IDXGIAdapter1> adapter;
  108. HRESULT result = E_FAIL;
  109. // First check if we have the Windows 1803 IDXGIFactory6 interface
  110. ComPtr<IDXGIFactory6> factory6;
  111. if (SUCCEEDED(mDXGIFactory->QueryInterface(IID_PPV_ARGS(&factory6))))
  112. {
  113. for (UINT index = 0; DXGI_ERROR_NOT_FOUND != factory6->EnumAdapterByGpuPreference(index, DXGI_GPU_PREFERENCE_HIGH_PERFORMANCE, IID_PPV_ARGS(&adapter)); ++index)
  114. {
  115. DXGI_ADAPTER_DESC1 desc;
  116. adapter->GetDesc1(&desc);
  117. // We don't want software renderers
  118. if (desc.Flags & DXGI_ADAPTER_FLAG_SOFTWARE)
  119. continue;
  120. // Check to see whether the adapter supports Direct3D 12
  121. result = D3D12CreateDevice(adapter.Get(), D3D_FEATURE_LEVEL_11_0, IID_PPV_ARGS(&mDevice));
  122. if (SUCCEEDED(result))
  123. break;
  124. }
  125. }
  126. else
  127. {
  128. // Fall back to the older method that may not get the fastest GPU
  129. for (UINT index = 0; DXGI_ERROR_NOT_FOUND != mDXGIFactory->EnumAdapters1(index, &adapter); ++index)
  130. {
  131. DXGI_ADAPTER_DESC1 desc;
  132. adapter->GetDesc1(&desc);
  133. // We don't want software renderers
  134. if (desc.Flags & DXGI_ADAPTER_FLAG_SOFTWARE)
  135. continue;
  136. // Check to see whether the adapter supports Direct3D 12
  137. result = D3D12CreateDevice(adapter.Get(), D3D_FEATURE_LEVEL_11_0, IID_PPV_ARGS(&mDevice));
  138. if (SUCCEEDED(result))
  139. break;
  140. }
  141. }
  142. // Check if we managed to obtain a device
  143. FatalErrorIfFailed(result);
  144. #ifdef JPH_DEBUG
  145. // Enable breaking on errors
  146. ComPtr<ID3D12InfoQueue> info_queue;
  147. if (SUCCEEDED(mDevice.As(&info_queue)))
  148. {
  149. info_queue->SetBreakOnSeverity(D3D12_MESSAGE_SEVERITY_CORRUPTION, TRUE);
  150. info_queue->SetBreakOnSeverity(D3D12_MESSAGE_SEVERITY_ERROR, TRUE);
  151. info_queue->SetBreakOnSeverity(D3D12_MESSAGE_SEVERITY_WARNING, TRUE);
  152. // Disable an error that triggers on Windows 11 with a hybrid graphic system
  153. // See: https://stackoverflow.com/questions/69805245/directx-12-application-is-crashing-in-windows-11
  154. D3D12_MESSAGE_ID hide[] =
  155. {
  156. D3D12_MESSAGE_ID_RESOURCE_BARRIER_MISMATCHING_COMMAND_LIST_TYPE,
  157. };
  158. D3D12_INFO_QUEUE_FILTER filter = { };
  159. filter.DenyList.NumIDs = static_cast<UINT>( std::size( hide ) );
  160. filter.DenyList.pIDList = hide;
  161. info_queue->AddStorageFilterEntries( &filter );
  162. }
  163. #endif // JPH_DEBUG
  164. // Disable full screen transitions
  165. FatalErrorIfFailed(mDXGIFactory->MakeWindowAssociation(mhWnd, DXGI_MWA_NO_ALT_ENTER));
  166. // Create heaps
  167. mRTVHeap.Init(mDevice.Get(), D3D12_DESCRIPTOR_HEAP_TYPE_RTV, D3D12_DESCRIPTOR_HEAP_FLAG_NONE, 2);
  168. mDSVHeap.Init(mDevice.Get(), D3D12_DESCRIPTOR_HEAP_TYPE_DSV, D3D12_DESCRIPTOR_HEAP_FLAG_NONE, 4);
  169. mSRVHeap.Init(mDevice.Get(), D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV, D3D12_DESCRIPTOR_HEAP_FLAG_SHADER_VISIBLE, 128);
  170. // Create a command queue
  171. D3D12_COMMAND_QUEUE_DESC queue_desc = {};
  172. queue_desc.Flags = D3D12_COMMAND_QUEUE_FLAG_NONE;
  173. queue_desc.Type = D3D12_COMMAND_LIST_TYPE_DIRECT;
  174. FatalErrorIfFailed(mDevice->CreateCommandQueue(&queue_desc, IID_PPV_ARGS(&mCommandQueue)));
  175. // Create a command allocator for each frame
  176. for (uint n = 0; n < cFrameCount; n++)
  177. FatalErrorIfFailed(mDevice->CreateCommandAllocator(D3D12_COMMAND_LIST_TYPE_DIRECT, IID_PPV_ARGS(&mCommandAllocators[n])));
  178. // Describe and create the swap chain
  179. DXGI_SWAP_CHAIN_DESC swap_chain_desc = {};
  180. swap_chain_desc.BufferCount = cFrameCount;
  181. swap_chain_desc.BufferDesc.Width = mWindowWidth;
  182. swap_chain_desc.BufferDesc.Height = mWindowHeight;
  183. swap_chain_desc.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
  184. swap_chain_desc.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
  185. swap_chain_desc.SwapEffect = DXGI_SWAP_EFFECT_FLIP_DISCARD;
  186. swap_chain_desc.OutputWindow = mhWnd;
  187. swap_chain_desc.SampleDesc.Count = 1;
  188. swap_chain_desc.Windowed = TRUE;
  189. ComPtr<IDXGISwapChain> swap_chain;
  190. FatalErrorIfFailed(mDXGIFactory->CreateSwapChain(mCommandQueue.Get(), &swap_chain_desc, &swap_chain));
  191. FatalErrorIfFailed(swap_chain.As(&mSwapChain));
  192. mFrameIndex = mSwapChain->GetCurrentBackBufferIndex();
  193. CreateRenderTargets();
  194. CreateDepthBuffer();
  195. // Create a root signature suitable for all our shaders
  196. D3D12_ROOT_PARAMETER params[3] = {};
  197. // Mapping a constant buffer to slot 0 for the vertex shader
  198. params[0].ParameterType = D3D12_ROOT_PARAMETER_TYPE_CBV;
  199. params[0].Descriptor.ShaderRegister = 0;
  200. params[0].ShaderVisibility = D3D12_SHADER_VISIBILITY_VERTEX;
  201. // Mapping a constant buffer to slot 1 in the pixel shader
  202. params[1].ParameterType = D3D12_ROOT_PARAMETER_TYPE_CBV;
  203. params[1].Descriptor.ShaderRegister = 1;
  204. params[1].ShaderVisibility = D3D12_SHADER_VISIBILITY_PIXEL;
  205. // Mapping a texture to slot 2 in the pixel shader
  206. D3D12_DESCRIPTOR_RANGE range = {};
  207. range.RangeType = D3D12_DESCRIPTOR_RANGE_TYPE_SRV;
  208. range.BaseShaderRegister = 2;
  209. range.NumDescriptors = 1;
  210. params[2].ParameterType = D3D12_ROOT_PARAMETER_TYPE_DESCRIPTOR_TABLE;
  211. params[2].DescriptorTable.NumDescriptorRanges = 1;
  212. params[2].DescriptorTable.pDescriptorRanges = &range;
  213. params[2].ShaderVisibility = D3D12_SHADER_VISIBILITY_PIXEL;
  214. D3D12_STATIC_SAMPLER_DESC samplers[3] = {};
  215. // Sampler 0: Non-wrapping linear filtering
  216. samplers[0].Filter = D3D12_FILTER_MIN_MAG_MIP_LINEAR;
  217. samplers[0].AddressU = D3D12_TEXTURE_ADDRESS_MODE_CLAMP;
  218. samplers[0].AddressV = D3D12_TEXTURE_ADDRESS_MODE_CLAMP;
  219. samplers[0].AddressW = D3D12_TEXTURE_ADDRESS_MODE_CLAMP;
  220. samplers[0].MipLODBias = 0.0f;
  221. samplers[0].MaxAnisotropy = 1;
  222. samplers[0].ComparisonFunc = D3D12_COMPARISON_FUNC_ALWAYS;
  223. samplers[0].BorderColor = D3D12_STATIC_BORDER_COLOR_TRANSPARENT_BLACK;
  224. samplers[0].MinLOD = 0.0f;
  225. samplers[0].MaxLOD = D3D12_FLOAT32_MAX;
  226. samplers[0].ShaderRegister = 0;
  227. samplers[0].ShaderVisibility = D3D12_SHADER_VISIBILITY_PIXEL;
  228. // Sampler 1: Wrapping and linear filtering
  229. samplers[1] = samplers[0];
  230. samplers[1].AddressU = D3D12_TEXTURE_ADDRESS_MODE_WRAP;
  231. samplers[1].AddressV = D3D12_TEXTURE_ADDRESS_MODE_WRAP;
  232. samplers[1].AddressW = D3D12_TEXTURE_ADDRESS_MODE_WRAP;
  233. samplers[1].ShaderRegister = 1;
  234. // Sampler 2: Point filtering, using SampleCmp mode to compare if sampled value >= reference value (for shadows)
  235. samplers[2] = samplers[0];
  236. samplers[2].Filter = D3D12_FILTER_COMPARISON_MIN_MAG_LINEAR_MIP_POINT;
  237. samplers[2].ComparisonFunc = D3D12_COMPARISON_FUNC_GREATER_EQUAL;
  238. samplers[2].ShaderRegister = 2;
  239. D3D12_ROOT_SIGNATURE_DESC root_signature_desc = {};
  240. root_signature_desc.Flags = D3D12_ROOT_SIGNATURE_FLAG_ALLOW_INPUT_ASSEMBLER_INPUT_LAYOUT;
  241. root_signature_desc.NumParameters = ARRAYSIZE(params);
  242. root_signature_desc.pParameters = params;
  243. root_signature_desc.NumStaticSamplers = ARRAYSIZE(samplers);
  244. root_signature_desc.pStaticSamplers = samplers;
  245. ComPtr<ID3DBlob> signature;
  246. ComPtr<ID3DBlob> error;
  247. FatalErrorIfFailed(D3D12SerializeRootSignature(&root_signature_desc, D3D_ROOT_SIGNATURE_VERSION_1, &signature, &error));
  248. FatalErrorIfFailed(mDevice->CreateRootSignature(0, signature->GetBufferPointer(), signature->GetBufferSize(), IID_PPV_ARGS(&mRootSignature)));
  249. // Create the command list
  250. FatalErrorIfFailed(mDevice->CreateCommandList(0, D3D12_COMMAND_LIST_TYPE_DIRECT, mCommandAllocators[mFrameIndex].Get(), nullptr, IID_PPV_ARGS(&mCommandList)));
  251. // Command lists are created in the recording state, but there is nothing to record yet. The main loop expects it to be closed, so close it now
  252. FatalErrorIfFailed(mCommandList->Close());
  253. // Create synchronization object
  254. FatalErrorIfFailed(mDevice->CreateFence(mFenceValues[mFrameIndex], D3D12_FENCE_FLAG_NONE, IID_PPV_ARGS(&mFence)));
  255. // Increment fence value so we don't skip waiting the first time a command list is executed
  256. mFenceValues[mFrameIndex]++;
  257. // Create an event handle to use for frame synchronization
  258. mFenceEvent = CreateEvent(nullptr, FALSE, FALSE, nullptr);
  259. if (mFenceEvent == nullptr)
  260. FatalErrorIfFailed(HRESULT_FROM_WIN32(GetLastError()));
  261. // Initialize the queue used to upload resources to the GPU
  262. mUploadQueue.Initialize(mDevice.Get());
  263. // Create constant buffer. One per frame to avoid overwriting the constant buffer while the GPU is still using it.
  264. for (uint n = 0; n < cFrameCount; ++n)
  265. {
  266. mVertexShaderConstantBufferProjection[n] = CreateConstantBuffer(sizeof(VertexShaderConstantBuffer));
  267. mVertexShaderConstantBufferOrtho[n] = CreateConstantBuffer(sizeof(VertexShaderConstantBuffer));
  268. mPixelShaderConstantBuffer[n] = CreateConstantBuffer(sizeof(PixelShaderConstantBuffer));
  269. }
  270. // Create depth only texture (no color buffer, as seen from light)
  271. mShadowMap = new TextureDX12(this, cShadowMapSize, cShadowMapSize);
  272. }
  273. void RendererDX12::OnWindowResize()
  274. {
  275. Renderer::OnWindowResize();
  276. // Wait for the previous frame to be rendered
  277. WaitForGpu();
  278. // Free the render targets and views to allow resizing the swap chain
  279. for (uint n = 0; n < cFrameCount; ++n)
  280. {
  281. mRTVHeap.Free(mRenderTargetViews[n]);
  282. mRenderTargets[n].Reset();
  283. }
  284. // Resize the swap chain buffers
  285. FatalErrorIfFailed(mSwapChain->ResizeBuffers(cFrameCount, mWindowWidth, mWindowHeight, DXGI_FORMAT_R8G8B8A8_UNORM, 0));
  286. // Back buffer index may have changed after the resize (it always seems to go to 0 again)
  287. mFrameIndex = mSwapChain->GetCurrentBackBufferIndex();
  288. // Since we may have switched frame index and we know everything is done, we need to update the fence value for our other frame as completed
  289. for (uint n = 0; n < cFrameCount; ++n)
  290. if (mFrameIndex != n)
  291. mFenceValues[n] = mFence->GetCompletedValue();
  292. // Recreate render targets
  293. CreateRenderTargets();
  294. // Recreate depth buffer
  295. CreateDepthBuffer();
  296. }
  297. void RendererDX12::BeginFrame(const CameraState &inCamera, float inWorldScale)
  298. {
  299. JPH_PROFILE_FUNCTION();
  300. Renderer::BeginFrame(inCamera, inWorldScale);
  301. // Reset command allocator
  302. FatalErrorIfFailed(mCommandAllocators[mFrameIndex]->Reset());
  303. // Reset command list
  304. FatalErrorIfFailed(mCommandList->Reset(mCommandAllocators[mFrameIndex].Get(), nullptr));
  305. // Set root signature
  306. mCommandList->SetGraphicsRootSignature(mRootSignature.Get());
  307. // Set SRV heap
  308. ID3D12DescriptorHeap *heaps[] = { mSRVHeap.Get() };
  309. mCommandList->SetDescriptorHeaps(_countof(heaps), heaps);
  310. // Indicate that the back buffer will be used as a render target.
  311. D3D12_RESOURCE_BARRIER barrier;
  312. barrier.Type = D3D12_RESOURCE_BARRIER_TYPE_TRANSITION;
  313. barrier.Flags = D3D12_RESOURCE_BARRIER_FLAG_NONE;
  314. barrier.Transition.pResource = mRenderTargets[mFrameIndex].Get();
  315. barrier.Transition.StateBefore = D3D12_RESOURCE_STATE_PRESENT;
  316. barrier.Transition.StateAfter = D3D12_RESOURCE_STATE_RENDER_TARGET;
  317. barrier.Transition.Subresource = D3D12_RESOURCE_BARRIER_ALL_SUBRESOURCES;
  318. mCommandList->ResourceBarrier(1, &barrier);
  319. // Clear the back buffer.
  320. const float blue[] = { 0.098f, 0.098f, 0.439f, 1.000f };
  321. mCommandList->ClearRenderTargetView(mRenderTargetViews[mFrameIndex], blue, 0, nullptr);
  322. mCommandList->ClearDepthStencilView(mDepthStencilView, D3D12_CLEAR_FLAG_DEPTH, 0.0f, 0, 0, nullptr);
  323. // Set constants for vertex shader in projection mode
  324. VertexShaderConstantBuffer *vs = mVertexShaderConstantBufferProjection[mFrameIndex]->Map<VertexShaderConstantBuffer>();
  325. *vs = mVSBuffer;
  326. mVertexShaderConstantBufferProjection[mFrameIndex]->Unmap();
  327. // Set constants for vertex shader in ortho mode
  328. vs = mVertexShaderConstantBufferOrtho[mFrameIndex]->Map<VertexShaderConstantBuffer>();
  329. *vs = mVSBufferOrtho;
  330. mVertexShaderConstantBufferOrtho[mFrameIndex]->Unmap();
  331. // Switch to 3d projection mode
  332. SetProjectionMode();
  333. // Set constants for pixel shader
  334. PixelShaderConstantBuffer *ps = mPixelShaderConstantBuffer[mFrameIndex]->Map<PixelShaderConstantBuffer>();
  335. *ps = mPSBuffer;
  336. mPixelShaderConstantBuffer[mFrameIndex]->Unmap();
  337. // Set the pixel shader constant buffer data.
  338. mPixelShaderConstantBuffer[mFrameIndex]->Bind(1);
  339. // Start drawing the shadow pass
  340. mShadowMap->SetAsRenderTarget(true);
  341. }
  342. void RendererDX12::EndShadowPass()
  343. {
  344. JPH_PROFILE_FUNCTION();
  345. // Finish drawing the shadow pass
  346. mShadowMap->SetAsRenderTarget(false);
  347. // Set the main back buffer as render target
  348. mCommandList->OMSetRenderTargets(1, &mRenderTargetViews[mFrameIndex], FALSE, &mDepthStencilView);
  349. // Set viewport
  350. D3D12_VIEWPORT viewport = { 0.0f, 0.0f, static_cast<float>(mWindowWidth), static_cast<float>(mWindowHeight), 0.0f, 1.0f };
  351. mCommandList->RSSetViewports(1, &viewport);
  352. // Set scissor rect
  353. D3D12_RECT scissor_rect = { 0, 0, static_cast<LONG>(mWindowWidth), static_cast<LONG>(mWindowHeight) };
  354. mCommandList->RSSetScissorRects(1, &scissor_rect);
  355. }
  356. void RendererDX12::EndFrame()
  357. {
  358. JPH_PROFILE_FUNCTION();
  359. Renderer::EndFrame();
  360. // Indicate that the back buffer will now be used to present.
  361. D3D12_RESOURCE_BARRIER barrier;
  362. barrier.Type = D3D12_RESOURCE_BARRIER_TYPE_TRANSITION;
  363. barrier.Flags = D3D12_RESOURCE_BARRIER_FLAG_NONE;
  364. barrier.Transition.pResource = mRenderTargets[mFrameIndex].Get();
  365. barrier.Transition.StateBefore = D3D12_RESOURCE_STATE_RENDER_TARGET;
  366. barrier.Transition.StateAfter = D3D12_RESOURCE_STATE_PRESENT;
  367. barrier.Transition.Subresource = D3D12_RESOURCE_BARRIER_ALL_SUBRESOURCES;
  368. mCommandList->ResourceBarrier(1, &barrier);
  369. // Close the command list
  370. FatalErrorIfFailed(mCommandList->Close());
  371. // Execute the command list
  372. ID3D12CommandList* command_lists[] = { mCommandList.Get() };
  373. mCommandQueue->ExecuteCommandLists(_countof(command_lists), command_lists);
  374. // Present the frame
  375. FatalErrorIfFailed(mSwapChain->Present(1, 0));
  376. // Schedule a Signal command in the queue
  377. UINT64 current_fence_value = mFenceValues[mFrameIndex];
  378. FatalErrorIfFailed(mCommandQueue->Signal(mFence.Get(), current_fence_value));
  379. // Update the frame index
  380. mFrameIndex = mSwapChain->GetCurrentBackBufferIndex();
  381. // If the next frame is not ready to be rendered yet, wait until it is ready
  382. UINT64 completed_value = mFence->GetCompletedValue();
  383. if (completed_value < mFenceValues[mFrameIndex])
  384. {
  385. FatalErrorIfFailed(mFence->SetEventOnCompletion(mFenceValues[mFrameIndex], mFenceEvent));
  386. WaitForSingleObjectEx(mFenceEvent, INFINITE, FALSE);
  387. }
  388. // Release all used resources
  389. mDelayReleased[mFrameIndex].clear();
  390. // Anything that's not used yet can be removed, delayed objects are now available
  391. mResourceCache.clear();
  392. mDelayCached[mFrameIndex].swap(mResourceCache);
  393. // Set the fence value for the next frame.
  394. mFenceValues[mFrameIndex] = current_fence_value + 1;
  395. }
  396. void RendererDX12::SetProjectionMode()
  397. {
  398. JPH_ASSERT(mInFrame);
  399. mVertexShaderConstantBufferProjection[mFrameIndex]->Bind(0);
  400. }
  401. void RendererDX12::SetOrthoMode()
  402. {
  403. JPH_ASSERT(mInFrame);
  404. mVertexShaderConstantBufferOrtho[mFrameIndex]->Bind(0);
  405. }
  406. Ref<Texture> RendererDX12::CreateTexture(const Surface *inSurface)
  407. {
  408. return new TextureDX12(this, inSurface);
  409. }
  410. Ref<VertexShader> RendererDX12::CreateVertexShader(const char *inFileName)
  411. {
  412. UINT flags = D3DCOMPILE_ENABLE_STRICTNESS;
  413. #ifdef JPH_DEBUG
  414. flags |= D3DCOMPILE_DEBUG | D3DCOMPILE_SKIP_OPTIMIZATION;
  415. #endif
  416. const D3D_SHADER_MACRO defines[] =
  417. {
  418. { nullptr, nullptr }
  419. };
  420. // Read shader source file
  421. Array<uint8> data = ReadData((String(inFileName) + ".hlsl").c_str());
  422. // Compile source
  423. ComPtr<ID3DBlob> shader_blob, error_blob;
  424. HRESULT hr = D3DCompile(&data[0],
  425. (uint)data.size(),
  426. inFileName,
  427. defines,
  428. D3D_COMPILE_STANDARD_FILE_INCLUDE,
  429. "main",
  430. "vs_5_0",
  431. flags,
  432. 0,
  433. shader_blob.GetAddressOf(),
  434. error_blob.GetAddressOf());
  435. if (FAILED(hr))
  436. {
  437. // Throw error if compilation failed
  438. if (error_blob)
  439. OutputDebugStringA((const char *)error_blob->GetBufferPointer());
  440. FatalError("Failed to compile vertex shader");
  441. }
  442. return new VertexShaderDX12(shader_blob);
  443. }
  444. Ref<PixelShader> RendererDX12::CreatePixelShader(const char *inFileName)
  445. {
  446. UINT flags = D3DCOMPILE_ENABLE_STRICTNESS;
  447. #ifdef JPH_DEBUG
  448. flags |= D3DCOMPILE_DEBUG | D3DCOMPILE_SKIP_OPTIMIZATION;
  449. #endif
  450. const D3D_SHADER_MACRO defines[] =
  451. {
  452. { nullptr, nullptr }
  453. };
  454. // Read shader source file
  455. Array<uint8> data = ReadData((String(inFileName) + ".hlsl").c_str());
  456. // Compile source
  457. ComPtr<ID3DBlob> shader_blob, error_blob;
  458. HRESULT hr = D3DCompile(&data[0],
  459. (uint)data.size(),
  460. inFileName,
  461. defines,
  462. D3D_COMPILE_STANDARD_FILE_INCLUDE,
  463. "main",
  464. "ps_5_0",
  465. flags,
  466. 0,
  467. shader_blob.GetAddressOf(),
  468. error_blob.GetAddressOf());
  469. if (FAILED(hr))
  470. {
  471. // Throw error if compilation failed
  472. if (error_blob)
  473. OutputDebugStringA((const char *)error_blob->GetBufferPointer());
  474. FatalError("Failed to compile pixel shader");
  475. }
  476. return new PixelShaderDX12(shader_blob);
  477. }
  478. unique_ptr<ConstantBufferDX12> RendererDX12::CreateConstantBuffer(uint inBufferSize)
  479. {
  480. return make_unique<ConstantBufferDX12>(this, inBufferSize);
  481. }
  482. unique_ptr<PipelineState> RendererDX12::CreatePipelineState(const VertexShader *inVertexShader, const PipelineState::EInputDescription *inInputDescription, uint inInputDescriptionCount, const PixelShader *inPixelShader, PipelineState::EDrawPass inDrawPass, PipelineState::EFillMode inFillMode, PipelineState::ETopology inTopology, PipelineState::EDepthTest inDepthTest, PipelineState::EBlendMode inBlendMode, PipelineState::ECullMode inCullMode)
  483. {
  484. return make_unique<PipelineStateDX12>(this, static_cast<const VertexShaderDX12 *>(inVertexShader), inInputDescription, inInputDescriptionCount, static_cast<const PixelShaderDX12 *>(inPixelShader), inDrawPass, inFillMode, inTopology, inDepthTest, inBlendMode, inCullMode);
  485. }
  486. RenderPrimitive *RendererDX12::CreateRenderPrimitive(PipelineState::ETopology inType)
  487. {
  488. return new RenderPrimitiveDX12(this, inType);
  489. }
  490. RenderInstances *RendererDX12::CreateRenderInstances()
  491. {
  492. return new RenderInstancesDX12(this);
  493. }
  494. ComPtr<ID3D12Resource> RendererDX12::CreateD3DResource(D3D12_HEAP_TYPE inHeapType, D3D12_RESOURCE_STATES inResourceState, uint64 inSize)
  495. {
  496. // Create a new resource
  497. D3D12_RESOURCE_DESC desc;
  498. desc.Dimension = D3D12_RESOURCE_DIMENSION_BUFFER;
  499. desc.Alignment = 0;
  500. desc.Width = inSize;
  501. desc.Height = 1;
  502. desc.DepthOrArraySize = 1;
  503. desc.MipLevels = 1;
  504. desc.Format = DXGI_FORMAT_UNKNOWN;
  505. desc.SampleDesc.Count = 1;
  506. desc.SampleDesc.Quality = 0;
  507. desc.Layout = D3D12_TEXTURE_LAYOUT_ROW_MAJOR;
  508. desc.Flags = D3D12_RESOURCE_FLAG_NONE;
  509. D3D12_HEAP_PROPERTIES heap_properties = {};
  510. heap_properties.Type = inHeapType;
  511. heap_properties.CPUPageProperty = D3D12_CPU_PAGE_PROPERTY_UNKNOWN;
  512. heap_properties.MemoryPoolPreference = D3D12_MEMORY_POOL_UNKNOWN;
  513. heap_properties.CreationNodeMask = 1;
  514. heap_properties.VisibleNodeMask = 1;
  515. ComPtr<ID3D12Resource> resource;
  516. FatalErrorIfFailed(mDevice->CreateCommittedResource(&heap_properties, D3D12_HEAP_FLAG_NONE, &desc, inResourceState, nullptr, IID_PPV_ARGS(&resource)));
  517. return resource;
  518. }
  519. void RendererDX12::CopyD3DResource(ID3D12Resource *inDest, const void *inSrc, uint64 inSize)
  520. {
  521. // Copy data to destination buffer
  522. void *data;
  523. D3D12_RANGE range = { 0, 0 }; // We're not going to read
  524. FatalErrorIfFailed(inDest->Map(0, &range, &data));
  525. memcpy(data, inSrc, size_t(inSize));
  526. inDest->Unmap(0, nullptr);
  527. }
  528. void RendererDX12::CopyD3DResource(ID3D12Resource *inDest, ID3D12Resource *inSrc, uint64 inSize)
  529. {
  530. // Start a commandlist for the upload
  531. ID3D12GraphicsCommandList *list = mUploadQueue.Start();
  532. // Copy the data to the GPU
  533. list->CopyBufferRegion(inDest, 0, inSrc, 0, inSize);
  534. // Change the state of the resource to generic read
  535. D3D12_RESOURCE_BARRIER barrier;
  536. barrier.Type = D3D12_RESOURCE_BARRIER_TYPE_TRANSITION;
  537. barrier.Flags = D3D12_RESOURCE_BARRIER_FLAG_NONE;
  538. barrier.Transition.pResource = inDest;
  539. barrier.Transition.StateBefore = D3D12_RESOURCE_STATE_COPY_DEST;
  540. barrier.Transition.StateAfter = D3D12_RESOURCE_STATE_GENERIC_READ;
  541. barrier.Transition.Subresource = D3D12_RESOURCE_BARRIER_ALL_SUBRESOURCES;
  542. list->ResourceBarrier(1, &barrier);
  543. // Wait for copying to finish
  544. mUploadQueue.ExecuteAndWait();
  545. }
  546. ComPtr<ID3D12Resource> RendererDX12::CreateD3DResourceOnDefaultHeap(const void *inData, uint64 inSize)
  547. {
  548. ComPtr<ID3D12Resource> upload = CreateD3DResourceOnUploadHeap(inSize);
  549. ComPtr<ID3D12Resource> resource = CreateD3DResource(D3D12_HEAP_TYPE_DEFAULT, D3D12_RESOURCE_STATE_COMMON, inSize);
  550. CopyD3DResource(upload.Get(), inData, inSize);
  551. CopyD3DResource(resource.Get(), upload.Get(), inSize);
  552. RecycleD3DResourceOnUploadHeap(upload.Get(), inSize);
  553. return resource;
  554. }
  555. ComPtr<ID3D12Resource> RendererDX12::CreateD3DResourceOnUploadHeap(uint64 inSize)
  556. {
  557. // Try cache first
  558. ResourceCache::iterator i = mResourceCache.find(inSize);
  559. if (i != mResourceCache.end() && !i->second.empty())
  560. {
  561. ComPtr<ID3D12Resource> resource = i->second.back();
  562. i->second.pop_back();
  563. return resource;
  564. }
  565. return CreateD3DResource(D3D12_HEAP_TYPE_UPLOAD, D3D12_RESOURCE_STATE_GENERIC_READ, inSize);
  566. }
  567. void RendererDX12::RecycleD3DResourceOnUploadHeap(ID3D12Resource *inResource, uint64 inSize)
  568. {
  569. if (!mIsExiting)
  570. mDelayCached[mFrameIndex][inSize].push_back(inResource);
  571. }
  572. void RendererDX12::RecycleD3DObject(ID3D12Object *inResource)
  573. {
  574. if (!mIsExiting)
  575. mDelayReleased[mFrameIndex].push_back(inResource);
  576. }