RendererDX12.cpp 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707
  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 <Window/ApplicationWindowWin.h>
  14. #include <Jolt/Core/Profiler.h>
  15. #include <Utils/ReadData.h>
  16. #include <Utils/Log.h>
  17. #include <d3dcompiler.h>
  18. #ifdef JPH_DEBUG
  19. #include <d3d12sdklayers.h>
  20. #endif
  21. RendererDX12::~RendererDX12()
  22. {
  23. // Ensure that the GPU is no longer referencing resources that are about to be cleaned up by the destructor.
  24. WaitForGpu();
  25. // Don't add more stuff to the delay reference list
  26. mIsExiting = true;
  27. CloseHandle(mFenceEvent);
  28. }
  29. void RendererDX12::WaitForGpu()
  30. {
  31. // Schedule a Signal command in the queue
  32. UINT64 current_fence_value = mFenceValues[mFrameIndex];
  33. FatalErrorIfFailed(mCommandQueue->Signal(mFence.Get(), current_fence_value));
  34. // Wait until the fence has been processed
  35. FatalErrorIfFailed(mFence->SetEventOnCompletion(current_fence_value, mFenceEvent));
  36. WaitForSingleObjectEx(mFenceEvent, INFINITE, FALSE);
  37. // Increment the fence value for all frames
  38. for (uint n = 0; n < cFrameCount; ++n)
  39. mFenceValues[n] = current_fence_value + 1;
  40. // Release all used resources
  41. for (Array<ComPtr<ID3D12Object>> &list : mDelayReleased)
  42. list.clear();
  43. // Anything that's not used yet can be removed, delayed objects are now available
  44. mResourceCache.clear();
  45. mDelayCached[mFrameIndex].swap(mResourceCache);
  46. }
  47. void RendererDX12::CreateRenderTargets()
  48. {
  49. // Create render targets and views
  50. for (uint n = 0; n < cFrameCount; ++n)
  51. {
  52. mRenderTargetViews[n] = mRTVHeap.Allocate();
  53. FatalErrorIfFailed(mSwapChain->GetBuffer(n, IID_PPV_ARGS(&mRenderTargets[n])));
  54. mDevice->CreateRenderTargetView(mRenderTargets[n].Get(), nullptr, mRenderTargetViews[n]);
  55. }
  56. }
  57. void RendererDX12::CreateDepthBuffer()
  58. {
  59. // Free any previous depth stencil view
  60. if (mDepthStencilView.ptr != 0)
  61. mDSVHeap.Free(mDepthStencilView);
  62. // Free any previous depth stencil buffer
  63. mDepthStencilBuffer.Reset();
  64. // Allocate depth stencil buffer
  65. D3D12_CLEAR_VALUE clear_value = {};
  66. clear_value.Format = DXGI_FORMAT_D32_FLOAT;
  67. clear_value.DepthStencil.Depth = 0.0f;
  68. clear_value.DepthStencil.Stencil = 0;
  69. D3D12_HEAP_PROPERTIES heap_properties = {};
  70. heap_properties.Type = D3D12_HEAP_TYPE_DEFAULT;
  71. heap_properties.CPUPageProperty = D3D12_CPU_PAGE_PROPERTY_UNKNOWN;
  72. heap_properties.MemoryPoolPreference = D3D12_MEMORY_POOL_UNKNOWN;
  73. heap_properties.CreationNodeMask = 1;
  74. heap_properties.VisibleNodeMask = 1;
  75. D3D12_RESOURCE_DESC depth_stencil_desc = {};
  76. depth_stencil_desc.Dimension = D3D12_RESOURCE_DIMENSION_TEXTURE2D;
  77. depth_stencil_desc.Alignment = 0;
  78. depth_stencil_desc.Width = mWindow->GetWindowWidth();
  79. depth_stencil_desc.Height = mWindow->GetWindowHeight();
  80. depth_stencil_desc.DepthOrArraySize = 1;
  81. depth_stencil_desc.MipLevels = 1;
  82. depth_stencil_desc.Format = DXGI_FORMAT_D32_FLOAT;
  83. depth_stencil_desc.SampleDesc.Count = 1;
  84. depth_stencil_desc.SampleDesc.Quality = 0;
  85. depth_stencil_desc.Layout = D3D12_TEXTURE_LAYOUT_UNKNOWN;
  86. depth_stencil_desc.Flags = D3D12_RESOURCE_FLAG_ALLOW_DEPTH_STENCIL;
  87. FatalErrorIfFailed(mDevice->CreateCommittedResource(&heap_properties, D3D12_HEAP_FLAG_NONE, &depth_stencil_desc, D3D12_RESOURCE_STATE_DEPTH_WRITE, &clear_value, IID_PPV_ARGS(&mDepthStencilBuffer)));
  88. // Allocate depth stencil view
  89. D3D12_DEPTH_STENCIL_VIEW_DESC depth_stencil_view_desc = {};
  90. depth_stencil_view_desc.Format = DXGI_FORMAT_D32_FLOAT;
  91. depth_stencil_view_desc.ViewDimension = D3D12_DSV_DIMENSION_TEXTURE2D;
  92. depth_stencil_view_desc.Flags = D3D12_DSV_FLAG_NONE;
  93. mDepthStencilView = mDSVHeap.Allocate();
  94. mDevice->CreateDepthStencilView(mDepthStencilBuffer.Get(), &depth_stencil_view_desc, mDepthStencilView);
  95. }
  96. void RendererDX12::Initialize(ApplicationWindow *inWindow)
  97. {
  98. Renderer::Initialize(inWindow);
  99. #if defined(JPH_DEBUG)
  100. // Enable the D3D12 debug layer
  101. ComPtr<ID3D12Debug> debug_controller;
  102. if (SUCCEEDED(D3D12GetDebugInterface(IID_PPV_ARGS(&debug_controller))))
  103. debug_controller->EnableDebugLayer();
  104. #endif
  105. // Create DXGI factory
  106. FatalErrorIfFailed(CreateDXGIFactory1(IID_PPV_ARGS(&mDXGIFactory)));
  107. // Find adapter
  108. ComPtr<IDXGIAdapter1> adapter;
  109. HRESULT result = E_FAIL;
  110. // First check if we have the Windows 1803 IDXGIFactory6 interface
  111. ComPtr<IDXGIFactory6> factory6;
  112. if (SUCCEEDED(mDXGIFactory->QueryInterface(IID_PPV_ARGS(&factory6))))
  113. {
  114. for (UINT index = 0; DXGI_ERROR_NOT_FOUND != factory6->EnumAdapterByGpuPreference(index, DXGI_GPU_PREFERENCE_HIGH_PERFORMANCE, IID_PPV_ARGS(&adapter)); ++index)
  115. {
  116. DXGI_ADAPTER_DESC1 desc;
  117. adapter->GetDesc1(&desc);
  118. // We don't want software renderers
  119. if (desc.Flags & DXGI_ADAPTER_FLAG_SOFTWARE)
  120. continue;
  121. // Check to see whether the adapter supports Direct3D 12
  122. result = D3D12CreateDevice(adapter.Get(), D3D_FEATURE_LEVEL_11_0, IID_PPV_ARGS(&mDevice));
  123. if (SUCCEEDED(result))
  124. break;
  125. }
  126. }
  127. else
  128. {
  129. // Fall back to the older method that may not get the fastest GPU
  130. for (UINT index = 0; DXGI_ERROR_NOT_FOUND != mDXGIFactory->EnumAdapters1(index, &adapter); ++index)
  131. {
  132. DXGI_ADAPTER_DESC1 desc;
  133. adapter->GetDesc1(&desc);
  134. // We don't want software renderers
  135. if (desc.Flags & DXGI_ADAPTER_FLAG_SOFTWARE)
  136. continue;
  137. // Check to see whether the adapter supports Direct3D 12
  138. result = D3D12CreateDevice(adapter.Get(), D3D_FEATURE_LEVEL_11_0, IID_PPV_ARGS(&mDevice));
  139. if (SUCCEEDED(result))
  140. break;
  141. }
  142. }
  143. // Check if we managed to obtain a device
  144. FatalErrorIfFailed(result);
  145. #ifdef JPH_DEBUG
  146. // Enable breaking on errors
  147. ComPtr<ID3D12InfoQueue> info_queue;
  148. if (SUCCEEDED(mDevice.As(&info_queue)))
  149. {
  150. info_queue->SetBreakOnSeverity(D3D12_MESSAGE_SEVERITY_CORRUPTION, TRUE);
  151. info_queue->SetBreakOnSeverity(D3D12_MESSAGE_SEVERITY_ERROR, TRUE);
  152. info_queue->SetBreakOnSeverity(D3D12_MESSAGE_SEVERITY_WARNING, TRUE);
  153. // Disable an error that triggers on Windows 11 with a hybrid graphic system
  154. // See: https://stackoverflow.com/questions/69805245/directx-12-application-is-crashing-in-windows-11
  155. D3D12_MESSAGE_ID hide[] =
  156. {
  157. D3D12_MESSAGE_ID_RESOURCE_BARRIER_MISMATCHING_COMMAND_LIST_TYPE,
  158. };
  159. D3D12_INFO_QUEUE_FILTER filter = { };
  160. filter.DenyList.NumIDs = static_cast<UINT>( std::size( hide ) );
  161. filter.DenyList.pIDList = hide;
  162. info_queue->AddStorageFilterEntries( &filter );
  163. }
  164. #endif // JPH_DEBUG
  165. // Disable full screen transitions
  166. FatalErrorIfFailed(mDXGIFactory->MakeWindowAssociation(static_cast<ApplicationWindowWin *>(mWindow)->GetWindowHandle(), DXGI_MWA_NO_ALT_ENTER));
  167. // Create heaps
  168. mRTVHeap.Init(mDevice.Get(), D3D12_DESCRIPTOR_HEAP_TYPE_RTV, D3D12_DESCRIPTOR_HEAP_FLAG_NONE, 2);
  169. mDSVHeap.Init(mDevice.Get(), D3D12_DESCRIPTOR_HEAP_TYPE_DSV, D3D12_DESCRIPTOR_HEAP_FLAG_NONE, 4);
  170. mSRVHeap.Init(mDevice.Get(), D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV, D3D12_DESCRIPTOR_HEAP_FLAG_SHADER_VISIBLE, 128);
  171. // Create a command queue
  172. D3D12_COMMAND_QUEUE_DESC queue_desc = {};
  173. queue_desc.Flags = D3D12_COMMAND_QUEUE_FLAG_NONE;
  174. queue_desc.Type = D3D12_COMMAND_LIST_TYPE_DIRECT;
  175. FatalErrorIfFailed(mDevice->CreateCommandQueue(&queue_desc, IID_PPV_ARGS(&mCommandQueue)));
  176. // Create a command allocator for each frame
  177. for (uint n = 0; n < cFrameCount; n++)
  178. FatalErrorIfFailed(mDevice->CreateCommandAllocator(D3D12_COMMAND_LIST_TYPE_DIRECT, IID_PPV_ARGS(&mCommandAllocators[n])));
  179. // Describe and create the swap chain
  180. DXGI_SWAP_CHAIN_DESC swap_chain_desc = {};
  181. swap_chain_desc.BufferCount = cFrameCount;
  182. swap_chain_desc.BufferDesc.Width = mWindow->GetWindowWidth();
  183. swap_chain_desc.BufferDesc.Height = mWindow->GetWindowHeight();
  184. swap_chain_desc.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
  185. swap_chain_desc.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
  186. swap_chain_desc.SwapEffect = DXGI_SWAP_EFFECT_FLIP_DISCARD;
  187. swap_chain_desc.OutputWindow = static_cast<ApplicationWindowWin *>(mWindow)->GetWindowHandle();
  188. swap_chain_desc.SampleDesc.Count = 1;
  189. swap_chain_desc.Windowed = TRUE;
  190. ComPtr<IDXGISwapChain> swap_chain;
  191. FatalErrorIfFailed(mDXGIFactory->CreateSwapChain(mCommandQueue.Get(), &swap_chain_desc, &swap_chain));
  192. FatalErrorIfFailed(swap_chain.As(&mSwapChain));
  193. mFrameIndex = mSwapChain->GetCurrentBackBufferIndex();
  194. CreateRenderTargets();
  195. CreateDepthBuffer();
  196. // Create a root signature suitable for all our shaders
  197. D3D12_ROOT_PARAMETER params[3] = {};
  198. // Mapping a constant buffer to slot 0 for the vertex shader
  199. params[0].ParameterType = D3D12_ROOT_PARAMETER_TYPE_CBV;
  200. params[0].Descriptor.ShaderRegister = 0;
  201. params[0].ShaderVisibility = D3D12_SHADER_VISIBILITY_VERTEX;
  202. // Mapping a constant buffer to slot 1 in the pixel shader
  203. params[1].ParameterType = D3D12_ROOT_PARAMETER_TYPE_CBV;
  204. params[1].Descriptor.ShaderRegister = 1;
  205. params[1].ShaderVisibility = D3D12_SHADER_VISIBILITY_PIXEL;
  206. // Mapping a texture to slot 2 in the pixel shader
  207. D3D12_DESCRIPTOR_RANGE range = {};
  208. range.RangeType = D3D12_DESCRIPTOR_RANGE_TYPE_SRV;
  209. range.BaseShaderRegister = 2;
  210. range.NumDescriptors = 1;
  211. params[2].ParameterType = D3D12_ROOT_PARAMETER_TYPE_DESCRIPTOR_TABLE;
  212. params[2].DescriptorTable.NumDescriptorRanges = 1;
  213. params[2].DescriptorTable.pDescriptorRanges = &range;
  214. params[2].ShaderVisibility = D3D12_SHADER_VISIBILITY_PIXEL;
  215. D3D12_STATIC_SAMPLER_DESC samplers[3] = {};
  216. // Sampler 0: Non-wrapping linear filtering
  217. samplers[0].Filter = D3D12_FILTER_MIN_MAG_MIP_LINEAR;
  218. samplers[0].AddressU = D3D12_TEXTURE_ADDRESS_MODE_CLAMP;
  219. samplers[0].AddressV = D3D12_TEXTURE_ADDRESS_MODE_CLAMP;
  220. samplers[0].AddressW = D3D12_TEXTURE_ADDRESS_MODE_CLAMP;
  221. samplers[0].MipLODBias = 0.0f;
  222. samplers[0].MaxAnisotropy = 1;
  223. samplers[0].ComparisonFunc = D3D12_COMPARISON_FUNC_ALWAYS;
  224. samplers[0].BorderColor = D3D12_STATIC_BORDER_COLOR_TRANSPARENT_BLACK;
  225. samplers[0].MinLOD = 0.0f;
  226. samplers[0].MaxLOD = D3D12_FLOAT32_MAX;
  227. samplers[0].ShaderRegister = 0;
  228. samplers[0].ShaderVisibility = D3D12_SHADER_VISIBILITY_PIXEL;
  229. // Sampler 1: Wrapping and linear filtering
  230. samplers[1] = samplers[0];
  231. samplers[1].AddressU = D3D12_TEXTURE_ADDRESS_MODE_WRAP;
  232. samplers[1].AddressV = D3D12_TEXTURE_ADDRESS_MODE_WRAP;
  233. samplers[1].AddressW = D3D12_TEXTURE_ADDRESS_MODE_WRAP;
  234. samplers[1].ShaderRegister = 1;
  235. // Sampler 2: Point filtering, using SampleCmp mode to compare if sampled value >= reference value (for shadows)
  236. samplers[2] = samplers[0];
  237. samplers[2].Filter = D3D12_FILTER_COMPARISON_MIN_MAG_LINEAR_MIP_POINT;
  238. samplers[2].ComparisonFunc = D3D12_COMPARISON_FUNC_GREATER_EQUAL;
  239. samplers[2].ShaderRegister = 2;
  240. D3D12_ROOT_SIGNATURE_DESC root_signature_desc = {};
  241. root_signature_desc.Flags = D3D12_ROOT_SIGNATURE_FLAG_ALLOW_INPUT_ASSEMBLER_INPUT_LAYOUT;
  242. root_signature_desc.NumParameters = ARRAYSIZE(params);
  243. root_signature_desc.pParameters = params;
  244. root_signature_desc.NumStaticSamplers = ARRAYSIZE(samplers);
  245. root_signature_desc.pStaticSamplers = samplers;
  246. ComPtr<ID3DBlob> signature;
  247. ComPtr<ID3DBlob> error;
  248. FatalErrorIfFailed(D3D12SerializeRootSignature(&root_signature_desc, D3D_ROOT_SIGNATURE_VERSION_1, &signature, &error));
  249. FatalErrorIfFailed(mDevice->CreateRootSignature(0, signature->GetBufferPointer(), signature->GetBufferSize(), IID_PPV_ARGS(&mRootSignature)));
  250. // Create the command list
  251. FatalErrorIfFailed(mDevice->CreateCommandList(0, D3D12_COMMAND_LIST_TYPE_DIRECT, mCommandAllocators[mFrameIndex].Get(), nullptr, IID_PPV_ARGS(&mCommandList)));
  252. // 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
  253. FatalErrorIfFailed(mCommandList->Close());
  254. // Create synchronization object
  255. FatalErrorIfFailed(mDevice->CreateFence(mFenceValues[mFrameIndex], D3D12_FENCE_FLAG_NONE, IID_PPV_ARGS(&mFence)));
  256. // Increment fence value so we don't skip waiting the first time a command list is executed
  257. mFenceValues[mFrameIndex]++;
  258. // Create an event handle to use for frame synchronization
  259. mFenceEvent = CreateEvent(nullptr, FALSE, FALSE, nullptr);
  260. if (mFenceEvent == nullptr)
  261. FatalErrorIfFailed(HRESULT_FROM_WIN32(GetLastError()));
  262. // Initialize the queue used to upload resources to the GPU
  263. mUploadQueue.Initialize(mDevice.Get());
  264. // Create constant buffer. One per frame to avoid overwriting the constant buffer while the GPU is still using it.
  265. for (uint n = 0; n < cFrameCount; ++n)
  266. {
  267. mVertexShaderConstantBufferProjection[n] = CreateConstantBuffer(sizeof(VertexShaderConstantBuffer));
  268. mVertexShaderConstantBufferOrtho[n] = CreateConstantBuffer(sizeof(VertexShaderConstantBuffer));
  269. mPixelShaderConstantBuffer[n] = CreateConstantBuffer(sizeof(PixelShaderConstantBuffer));
  270. }
  271. // Create depth only texture (no color buffer, as seen from light)
  272. mShadowMap = new TextureDX12(this, cShadowMapSize, cShadowMapSize);
  273. }
  274. void RendererDX12::OnWindowResize()
  275. {
  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, mWindow->GetWindowWidth(), mWindow->GetWindowHeight(), 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>(mWindow->GetWindowWidth()), static_cast<float>(mWindow->GetWindowHeight()), 0.0f, 1.0f };
  351. mCommandList->RSSetViewports(1, &viewport);
  352. // Set scissor rect
  353. D3D12_RECT scissor_rect = { 0, 0, static_cast<LONG>(mWindow->GetWindowWidth()), static_cast<LONG>(mWindow->GetWindowHeight()) };
  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. }