Renderer.cpp 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822
  1. // Jolt Physics Library (https://github.com/jrouwe/JoltPhysics)
  2. // SPDX-FileCopyrightText: 2021 Jorrit Rouwe
  3. // SPDX-License-Identifier: MIT
  4. #include <TestFramework.h>
  5. #include <Renderer/Renderer.h>
  6. #include <Renderer/Texture.h>
  7. #include <Renderer/FatalErrorIfFailed.h>
  8. #include <Jolt/Core/Profiler.h>
  9. #include <Utils/ReadData.h>
  10. #include <Utils/Log.h>
  11. #include <d3dcompiler.h>
  12. #include <shellscalingapi.h>
  13. #ifdef _DEBUG
  14. #include <d3d12sdklayers.h>
  15. #endif
  16. static Renderer *sRenderer = nullptr;
  17. struct VertexShaderConstantBuffer
  18. {
  19. Mat44 mView;
  20. Mat44 mProjection;
  21. Mat44 mLightView;
  22. Mat44 mLightProjection;
  23. };
  24. struct PixelShaderConstantBuffer
  25. {
  26. Vec4 mCameraPos;
  27. Vec4 mLightPos;
  28. };
  29. //--------------------------------------------------------------------------------------
  30. // Called every time the application receives a message
  31. //--------------------------------------------------------------------------------------
  32. static LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
  33. {
  34. PAINTSTRUCT ps;
  35. switch (message)
  36. {
  37. case WM_PAINT:
  38. BeginPaint(hWnd, &ps);
  39. EndPaint(hWnd, &ps);
  40. break;
  41. case WM_SIZE:
  42. if (sRenderer != nullptr)
  43. sRenderer->OnWindowResize();
  44. break;
  45. case WM_DESTROY:
  46. PostQuitMessage(0);
  47. break;
  48. default:
  49. return DefWindowProc(hWnd, message, wParam, lParam);
  50. }
  51. return 0;
  52. }
  53. Renderer::~Renderer()
  54. {
  55. // Ensure that the GPU is no longer referencing resources that are about to be cleaned up by the destructor.
  56. WaitForGpu();
  57. // Don't add more stuff to the delay reference list
  58. mIsExiting = true;
  59. CloseHandle(mFenceEvent);
  60. }
  61. void Renderer::WaitForGpu()
  62. {
  63. // Schedule a Signal command in the queue
  64. UINT64 current_fence_value = mFenceValues[mFrameIndex];
  65. FatalErrorIfFailed(mCommandQueue->Signal(mFence.Get(), current_fence_value));
  66. // Wait until the fence has been processed
  67. FatalErrorIfFailed(mFence->SetEventOnCompletion(current_fence_value, mFenceEvent));
  68. WaitForSingleObjectEx(mFenceEvent, INFINITE, FALSE);
  69. // Increment the fence value for all frames
  70. for (uint n = 0; n < cFrameCount; ++n)
  71. mFenceValues[n] = current_fence_value + 1;
  72. // Release all used resources
  73. for (Array<ComPtr<ID3D12Object>> &list : mDelayReleased)
  74. list.clear();
  75. // Anything that's not used yet can be removed, delayed objects are now available
  76. mResourceCache.clear();
  77. mDelayCached[mFrameIndex].swap(mResourceCache);
  78. }
  79. void Renderer::CreateRenterTargets()
  80. {
  81. // Create render targets and views
  82. for (uint n = 0; n < cFrameCount; ++n)
  83. {
  84. mRenderTargetViews[n] = mRTVHeap.Allocate();
  85. FatalErrorIfFailed(mSwapChain->GetBuffer(n, IID_PPV_ARGS(&mRenderTargets[n])));
  86. mDevice->CreateRenderTargetView(mRenderTargets[n].Get(), nullptr, mRenderTargetViews[n]);
  87. }
  88. }
  89. void Renderer::CreateDepthBuffer()
  90. {
  91. // Free any previous depth stencil view
  92. if (mDepthStencilView.ptr != 0)
  93. mDSVHeap.Free(mDepthStencilView);
  94. // Free any previous depth stencil buffer
  95. mDepthStencilBuffer.Reset();
  96. // Allocate depth stencil buffer
  97. D3D12_CLEAR_VALUE clear_value = {};
  98. clear_value.Format = DXGI_FORMAT_D32_FLOAT;
  99. clear_value.DepthStencil.Depth = 1.0f;
  100. clear_value.DepthStencil.Stencil = 0;
  101. D3D12_HEAP_PROPERTIES heap_properties = {};
  102. heap_properties.Type = D3D12_HEAP_TYPE_DEFAULT;
  103. heap_properties.CPUPageProperty = D3D12_CPU_PAGE_PROPERTY_UNKNOWN;
  104. heap_properties.MemoryPoolPreference = D3D12_MEMORY_POOL_UNKNOWN;
  105. heap_properties.CreationNodeMask = 1;
  106. heap_properties.VisibleNodeMask = 1;
  107. D3D12_RESOURCE_DESC depth_stencil_desc = {};
  108. depth_stencil_desc.Dimension = D3D12_RESOURCE_DIMENSION_TEXTURE2D;
  109. depth_stencil_desc.Alignment = 0;
  110. depth_stencil_desc.Width = mWindowWidth;
  111. depth_stencil_desc.Height = mWindowHeight;
  112. depth_stencil_desc.DepthOrArraySize = 1;
  113. depth_stencil_desc.MipLevels = 1;
  114. depth_stencil_desc.Format = DXGI_FORMAT_D32_FLOAT;
  115. depth_stencil_desc.SampleDesc.Count = 1;
  116. depth_stencil_desc.SampleDesc.Quality = 0;
  117. depth_stencil_desc.Layout = D3D12_TEXTURE_LAYOUT_UNKNOWN;
  118. depth_stencil_desc.Flags = D3D12_RESOURCE_FLAG_ALLOW_DEPTH_STENCIL;
  119. FatalErrorIfFailed(mDevice->CreateCommittedResource(&heap_properties, D3D12_HEAP_FLAG_NONE, &depth_stencil_desc, D3D12_RESOURCE_STATE_DEPTH_WRITE, &clear_value, IID_PPV_ARGS(&mDepthStencilBuffer)));
  120. // Allocate depth stencil view
  121. D3D12_DEPTH_STENCIL_VIEW_DESC depth_stencil_view_desc = {};
  122. depth_stencil_view_desc.Format = DXGI_FORMAT_D32_FLOAT;
  123. depth_stencil_view_desc.ViewDimension = D3D12_DSV_DIMENSION_TEXTURE2D;
  124. depth_stencil_view_desc.Flags = D3D12_DSV_FLAG_NONE;
  125. mDepthStencilView = mDSVHeap.Allocate();
  126. mDevice->CreateDepthStencilView(mDepthStencilBuffer.Get(), &depth_stencil_view_desc, mDepthStencilView);
  127. }
  128. void Renderer::Initialize()
  129. {
  130. // Prevent this window from auto scaling
  131. SetProcessDpiAwareness(PROCESS_PER_MONITOR_DPI_AWARE);
  132. // Register class
  133. WNDCLASSEX wcex;
  134. wcex.cbSize = sizeof(WNDCLASSEX);
  135. wcex.style = CS_HREDRAW | CS_VREDRAW;
  136. wcex.lpfnWndProc = WndProc;
  137. wcex.cbClsExtra = 0;
  138. wcex.cbWndExtra = 0;
  139. wcex.hInstance = GetModuleHandle(nullptr);
  140. wcex.hIcon = nullptr;
  141. wcex.hCursor = LoadCursor(nullptr, IDC_ARROW);
  142. wcex.hbrBackground = nullptr;
  143. wcex.lpszMenuName = nullptr;
  144. wcex.lpszClassName = TEXT("TestFrameworkClass");
  145. wcex.hIconSm = nullptr;
  146. if (!RegisterClassEx(&wcex))
  147. FatalError("Failed to register window class");
  148. // Create window
  149. RECT rc = { 0, 0, mWindowWidth, mWindowHeight };
  150. AdjustWindowRect(&rc, WS_OVERLAPPEDWINDOW, FALSE);
  151. mhWnd = CreateWindow(TEXT("TestFrameworkClass"), TEXT("TestFramework"), WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT,
  152. rc.right - rc.left, rc.bottom - rc.top, nullptr, nullptr, wcex.hInstance, nullptr);
  153. if (!mhWnd)
  154. FatalError("Failed to create window");
  155. // Show window
  156. ShowWindow(mhWnd, SW_SHOW);
  157. #if defined(_DEBUG)
  158. // Enable the D3D12 debug layer
  159. ComPtr<ID3D12Debug> debug_controller;
  160. if (SUCCEEDED(D3D12GetDebugInterface(IID_PPV_ARGS(&debug_controller))))
  161. debug_controller->EnableDebugLayer();
  162. #endif
  163. // Create DXGI factory
  164. FatalErrorIfFailed(CreateDXGIFactory1(IID_PPV_ARGS(&mDXGIFactory)));
  165. // Find adapter
  166. ComPtr<IDXGIAdapter1> adapter;
  167. HRESULT result = E_FAIL;
  168. // First check if we have the Windows 1803 IDXGIFactory6 interface
  169. ComPtr<IDXGIFactory6> factory6;
  170. if (SUCCEEDED(mDXGIFactory->QueryInterface(IID_PPV_ARGS(&factory6))))
  171. {
  172. for (UINT index = 0; DXGI_ERROR_NOT_FOUND != factory6->EnumAdapterByGpuPreference(index, DXGI_GPU_PREFERENCE_HIGH_PERFORMANCE, IID_PPV_ARGS(&adapter)); ++index)
  173. {
  174. DXGI_ADAPTER_DESC1 desc;
  175. adapter->GetDesc1(&desc);
  176. // We don't want software renderers
  177. if (desc.Flags & DXGI_ADAPTER_FLAG_SOFTWARE)
  178. continue;
  179. // Check to see whether the adapter supports Direct3D 12
  180. result = D3D12CreateDevice(adapter.Get(), D3D_FEATURE_LEVEL_11_0, IID_PPV_ARGS(&mDevice));
  181. if (SUCCEEDED(result))
  182. break;
  183. }
  184. }
  185. else
  186. {
  187. // Fall back to the older method that may not get the fastest GPU
  188. for (UINT index = 0; DXGI_ERROR_NOT_FOUND != mDXGIFactory->EnumAdapters1(index, &adapter); ++index)
  189. {
  190. DXGI_ADAPTER_DESC1 desc;
  191. adapter->GetDesc1(&desc);
  192. // We don't want software renderers
  193. if (desc.Flags & DXGI_ADAPTER_FLAG_SOFTWARE)
  194. continue;
  195. // Check to see whether the adapter supports Direct3D 12
  196. result = D3D12CreateDevice(adapter.Get(), D3D_FEATURE_LEVEL_11_0, IID_PPV_ARGS(&mDevice));
  197. if (SUCCEEDED(result))
  198. break;
  199. }
  200. }
  201. // Check if we managed to obtain a device
  202. FatalErrorIfFailed(result);
  203. #ifdef _DEBUG
  204. // Enable breaking on errors
  205. ComPtr<ID3D12InfoQueue> info_queue;
  206. if (SUCCEEDED(mDevice.As(&info_queue)))
  207. {
  208. info_queue->SetBreakOnSeverity(D3D12_MESSAGE_SEVERITY_CORRUPTION, TRUE);
  209. info_queue->SetBreakOnSeverity(D3D12_MESSAGE_SEVERITY_ERROR, TRUE);
  210. info_queue->SetBreakOnSeverity(D3D12_MESSAGE_SEVERITY_WARNING, TRUE);
  211. // Disable an error that triggers on Windows 11 with a hybrid graphic system
  212. // See: https://stackoverflow.com/questions/69805245/directx-12-application-is-crashing-in-windows-11
  213. D3D12_MESSAGE_ID hide[] =
  214. {
  215. D3D12_MESSAGE_ID_RESOURCE_BARRIER_MISMATCHING_COMMAND_LIST_TYPE,
  216. };
  217. D3D12_INFO_QUEUE_FILTER filter = { };
  218. filter.DenyList.NumIDs = static_cast<UINT>( std::size( hide ) );
  219. filter.DenyList.pIDList = hide;
  220. info_queue->AddStorageFilterEntries( &filter );
  221. }
  222. #endif // _DEBUG
  223. // Disable full screen transitions
  224. FatalErrorIfFailed(mDXGIFactory->MakeWindowAssociation(mhWnd, DXGI_MWA_NO_ALT_ENTER));
  225. // Create heaps
  226. mRTVHeap.Init(mDevice.Get(), D3D12_DESCRIPTOR_HEAP_TYPE_RTV, D3D12_DESCRIPTOR_HEAP_FLAG_NONE, 2);
  227. mDSVHeap.Init(mDevice.Get(), D3D12_DESCRIPTOR_HEAP_TYPE_DSV, D3D12_DESCRIPTOR_HEAP_FLAG_NONE, 4);
  228. mSRVHeap.Init(mDevice.Get(), D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV, D3D12_DESCRIPTOR_HEAP_FLAG_SHADER_VISIBLE, 128);
  229. // Create a command queue
  230. D3D12_COMMAND_QUEUE_DESC queue_desc = {};
  231. queue_desc.Flags = D3D12_COMMAND_QUEUE_FLAG_NONE;
  232. queue_desc.Type = D3D12_COMMAND_LIST_TYPE_DIRECT;
  233. FatalErrorIfFailed(mDevice->CreateCommandQueue(&queue_desc, IID_PPV_ARGS(&mCommandQueue)));
  234. // Create a command allocator for each frame
  235. for (uint n = 0; n < cFrameCount; n++)
  236. FatalErrorIfFailed(mDevice->CreateCommandAllocator(D3D12_COMMAND_LIST_TYPE_DIRECT, IID_PPV_ARGS(&mCommandAllocators[n])));
  237. // Describe and create the swap chain
  238. DXGI_SWAP_CHAIN_DESC swap_chain_desc = {};
  239. swap_chain_desc.BufferCount = cFrameCount;
  240. swap_chain_desc.BufferDesc.Width = mWindowWidth;
  241. swap_chain_desc.BufferDesc.Height = mWindowHeight;
  242. swap_chain_desc.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
  243. swap_chain_desc.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
  244. swap_chain_desc.SwapEffect = DXGI_SWAP_EFFECT_FLIP_DISCARD;
  245. swap_chain_desc.OutputWindow = mhWnd;
  246. swap_chain_desc.SampleDesc.Count = 1;
  247. swap_chain_desc.Windowed = TRUE;
  248. ComPtr<IDXGISwapChain> swap_chain;
  249. FatalErrorIfFailed(mDXGIFactory->CreateSwapChain(mCommandQueue.Get(), &swap_chain_desc, &swap_chain));
  250. FatalErrorIfFailed(swap_chain.As(&mSwapChain));
  251. mFrameIndex = mSwapChain->GetCurrentBackBufferIndex();
  252. CreateRenterTargets();
  253. CreateDepthBuffer();
  254. // Create a root signature suitable for all our shaders
  255. D3D12_ROOT_PARAMETER params[3] = {};
  256. // Mapping a constant buffer to slot 0 for the vertex shader
  257. params[0].ParameterType = D3D12_ROOT_PARAMETER_TYPE_CBV;
  258. params[0].Descriptor.ShaderRegister = 0;
  259. params[0].ShaderVisibility = D3D12_SHADER_VISIBILITY_VERTEX;
  260. // Mapping a constant buffer to slot 1 in the pixel shader
  261. params[1].ParameterType = D3D12_ROOT_PARAMETER_TYPE_CBV;
  262. params[1].Descriptor.ShaderRegister = 1;
  263. params[1].ShaderVisibility = D3D12_SHADER_VISIBILITY_PIXEL;
  264. // Mapping a texture to slot 2 in the pixel shader
  265. D3D12_DESCRIPTOR_RANGE range = {};
  266. range.RangeType = D3D12_DESCRIPTOR_RANGE_TYPE_SRV;
  267. range.BaseShaderRegister = 2;
  268. range.NumDescriptors = 1;
  269. params[2].ParameterType = D3D12_ROOT_PARAMETER_TYPE_DESCRIPTOR_TABLE;
  270. params[2].DescriptorTable.NumDescriptorRanges = 1;
  271. params[2].DescriptorTable.pDescriptorRanges = &range;
  272. params[2].ShaderVisibility = D3D12_SHADER_VISIBILITY_PIXEL;
  273. D3D12_STATIC_SAMPLER_DESC samplers[3] = {};
  274. // Sampler 0: Non-wrapping linear filtering
  275. samplers[0].Filter = D3D12_FILTER_MIN_MAG_MIP_LINEAR;
  276. samplers[0].AddressU = D3D12_TEXTURE_ADDRESS_MODE_CLAMP;
  277. samplers[0].AddressV = D3D12_TEXTURE_ADDRESS_MODE_CLAMP;
  278. samplers[0].AddressW = D3D12_TEXTURE_ADDRESS_MODE_CLAMP;
  279. samplers[0].MipLODBias = 0.0f;
  280. samplers[0].MaxAnisotropy = 1;
  281. samplers[0].ComparisonFunc = D3D12_COMPARISON_FUNC_ALWAYS;
  282. samplers[0].BorderColor = D3D12_STATIC_BORDER_COLOR_TRANSPARENT_BLACK;
  283. samplers[0].MinLOD = 0.0f;
  284. samplers[0].MaxLOD = D3D12_FLOAT32_MAX;
  285. samplers[0].ShaderRegister = 0;
  286. samplers[0].ShaderVisibility = D3D12_SHADER_VISIBILITY_PIXEL;
  287. // Sampler 1: Wrapping and linear filtering
  288. samplers[1] = samplers[0];
  289. samplers[1].AddressU = D3D12_TEXTURE_ADDRESS_MODE_WRAP;
  290. samplers[1].AddressV = D3D12_TEXTURE_ADDRESS_MODE_WRAP;
  291. samplers[1].AddressW = D3D12_TEXTURE_ADDRESS_MODE_WRAP;
  292. samplers[1].ShaderRegister = 1;
  293. // Sampler 2: Point filtering, using SampleCmp mode to compare if sampled value <= reference value (for shadows)
  294. samplers[2] = samplers[0];
  295. samplers[2].Filter = D3D12_FILTER_COMPARISON_MIN_MAG_LINEAR_MIP_POINT;
  296. samplers[2].ComparisonFunc = D3D12_COMPARISON_FUNC_LESS_EQUAL;
  297. samplers[2].ShaderRegister = 2;
  298. D3D12_ROOT_SIGNATURE_DESC root_signature_desc = {};
  299. root_signature_desc.Flags = D3D12_ROOT_SIGNATURE_FLAG_ALLOW_INPUT_ASSEMBLER_INPUT_LAYOUT;
  300. root_signature_desc.NumParameters = ARRAYSIZE(params);
  301. root_signature_desc.pParameters = params;
  302. root_signature_desc.NumStaticSamplers = ARRAYSIZE(samplers);
  303. root_signature_desc.pStaticSamplers = samplers;
  304. ComPtr<ID3DBlob> signature;
  305. ComPtr<ID3DBlob> error;
  306. FatalErrorIfFailed(D3D12SerializeRootSignature(&root_signature_desc, D3D_ROOT_SIGNATURE_VERSION_1, &signature, &error));
  307. FatalErrorIfFailed(mDevice->CreateRootSignature(0, signature->GetBufferPointer(), signature->GetBufferSize(), IID_PPV_ARGS(&mRootSignature)));
  308. // Create the command list
  309. FatalErrorIfFailed(mDevice->CreateCommandList(0, D3D12_COMMAND_LIST_TYPE_DIRECT, mCommandAllocators[mFrameIndex].Get(), nullptr, IID_PPV_ARGS(&mCommandList)));
  310. // 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
  311. FatalErrorIfFailed(mCommandList->Close());
  312. // Create synchronization object
  313. FatalErrorIfFailed(mDevice->CreateFence(mFenceValues[mFrameIndex], D3D12_FENCE_FLAG_NONE, IID_PPV_ARGS(&mFence)));
  314. // Increment fence value so we don't skip waiting the first time a command list is executed
  315. mFenceValues[mFrameIndex]++;
  316. // Create an event handle to use for frame synchronization
  317. mFenceEvent = CreateEvent(nullptr, FALSE, FALSE, nullptr);
  318. if (mFenceEvent == nullptr)
  319. FatalErrorIfFailed(HRESULT_FROM_WIN32(GetLastError()));
  320. // Initialize the queue used to upload resources to the GPU
  321. mUploadQueue.Initialize(mDevice.Get());
  322. // Create constant buffer. One per frame to avoid overwriting the constant buffer while the GPU is still using it.
  323. for (uint n = 0; n < cFrameCount; ++n)
  324. {
  325. mVertexShaderConstantBufferProjection[n] = CreateConstantBuffer(sizeof(VertexShaderConstantBuffer));
  326. mVertexShaderConstantBufferOrtho[n] = CreateConstantBuffer(sizeof(VertexShaderConstantBuffer));
  327. mPixelShaderConstantBuffer[n] = CreateConstantBuffer(sizeof(PixelShaderConstantBuffer));
  328. }
  329. // Store global renderer now that we're done initializing
  330. sRenderer = this;
  331. }
  332. void Renderer::OnWindowResize()
  333. {
  334. // Wait for the previous frame to be rendered
  335. WaitForGpu();
  336. // Get new window size
  337. RECT rc;
  338. GetClientRect(mhWnd, &rc);
  339. mWindowWidth = max<LONG>(rc.right - rc.left, 8);
  340. mWindowHeight = max<LONG>(rc.bottom - rc.top, 8);
  341. // Free the render targets and views to allow resizing the swap chain
  342. for (uint n = 0; n < cFrameCount; ++n)
  343. {
  344. mRTVHeap.Free(mRenderTargetViews[n]);
  345. mRenderTargets[n].Reset();
  346. }
  347. // Resize the swap chain buffers
  348. FatalErrorIfFailed(mSwapChain->ResizeBuffers(cFrameCount, mWindowWidth, mWindowHeight, DXGI_FORMAT_R8G8B8A8_UNORM, 0));
  349. // Back buffer index may have changed after the resize (it always seems to go to 0 again)
  350. mFrameIndex = mSwapChain->GetCurrentBackBufferIndex();
  351. // 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
  352. for (uint n = 0; n < cFrameCount; ++n)
  353. if (mFrameIndex != n)
  354. mFenceValues[n] = mFence->GetCompletedValue();
  355. // Recreate render targets
  356. CreateRenterTargets();
  357. // Recreate depth buffer
  358. CreateDepthBuffer();
  359. }
  360. void Renderer::BeginFrame(const CameraState &inCamera, float inWorldScale)
  361. {
  362. JPH_PROFILE_FUNCTION();
  363. // Store state
  364. mCameraState = inCamera;
  365. // Reset command allocator
  366. FatalErrorIfFailed(mCommandAllocators[mFrameIndex]->Reset());
  367. // Reset command list
  368. FatalErrorIfFailed(mCommandList->Reset(mCommandAllocators[mFrameIndex].Get(), nullptr));
  369. // Set root signature
  370. mCommandList->SetGraphicsRootSignature(mRootSignature.Get());
  371. // Set SRV heap
  372. ID3D12DescriptorHeap *heaps[] = { mSRVHeap.Get() };
  373. mCommandList->SetDescriptorHeaps(_countof(heaps), heaps);
  374. // Indicate that the back buffer will be used as a render target.
  375. D3D12_RESOURCE_BARRIER barrier;
  376. barrier.Type = D3D12_RESOURCE_BARRIER_TYPE_TRANSITION;
  377. barrier.Flags = D3D12_RESOURCE_BARRIER_FLAG_NONE;
  378. barrier.Transition.pResource = mRenderTargets[mFrameIndex].Get();
  379. barrier.Transition.StateBefore = D3D12_RESOURCE_STATE_PRESENT;
  380. barrier.Transition.StateAfter = D3D12_RESOURCE_STATE_RENDER_TARGET;
  381. barrier.Transition.Subresource = D3D12_RESOURCE_BARRIER_ALL_SUBRESOURCES;
  382. mCommandList->ResourceBarrier(1, &barrier);
  383. // Set the main back buffer as render target
  384. SetRenderTarget(nullptr);
  385. // Clear the back buffer.
  386. const float blue[] = { 0.098f, 0.098f, 0.439f, 1.000f };
  387. mCommandList->ClearRenderTargetView(mRenderTargetViews[mFrameIndex], blue, 0, nullptr);
  388. mCommandList->ClearDepthStencilView(mDepthStencilView, D3D12_CLEAR_FLAG_DEPTH, 1.0f, 0, 0, nullptr);
  389. // Light properties
  390. Vec3 light_pos = inWorldScale * Vec3(250, 250, 250);
  391. Vec3 light_tgt = Vec3::sZero();
  392. Vec3 light_up = Vec3(0, 1, 0);
  393. Vec3 light_fwd = (light_tgt - light_pos).Normalized();
  394. float light_fov = DegreesToRadians(20.0f);
  395. float light_near = 1.0f;
  396. float light_far = 1000.0f;
  397. // Camera properties
  398. float camera_fovy = inCamera.mFOVY;
  399. float camera_aspect = static_cast<float>(GetWindowWidth()) / GetWindowHeight();
  400. float camera_fovx = 2.0f * ATan(camera_aspect * Tan(0.5f * camera_fovy));
  401. float camera_near = 0.01f * inWorldScale;
  402. float camera_far = inCamera.mFarPlane * inWorldScale;
  403. // Set constants for vertex shader in projection mode
  404. VertexShaderConstantBuffer *vs = mVertexShaderConstantBufferProjection[mFrameIndex]->Map<VertexShaderConstantBuffer>();
  405. // Camera projection and view
  406. vs->mProjection = Mat44::sPerspective(camera_fovy, camera_aspect, camera_near, camera_far);
  407. Vec3 cam_pos = Vec3(inCamera.mPos - mBaseOffset);
  408. Vec3 tgt = cam_pos + inCamera.mForward;
  409. vs->mView = Mat44::sLookAt(cam_pos, tgt, inCamera.mUp);
  410. // Light projection and view
  411. vs->mLightProjection = Mat44::sPerspective(light_fov, 1.0f, light_near, light_far);
  412. vs->mLightView = Mat44::sLookAt(light_pos, light_tgt, light_up);
  413. mVertexShaderConstantBufferProjection[mFrameIndex]->Unmap();
  414. // Set constants for vertex shader in ortho mode
  415. vs = mVertexShaderConstantBufferOrtho[mFrameIndex]->Map<VertexShaderConstantBuffer>();
  416. // Camera ortho projection and view
  417. vs->mProjection = Mat44(Vec4(2.0f / mWindowWidth, 0.0f, 0.0f, 0.0f), Vec4(0.0f, -2.0f / mWindowHeight, 0.0f, 0.0f), Vec4(0.0f, 0.0f, -1.0f, 0.0f), Vec4(-1.0f, 1.0f, 0.0f, 1.0f));
  418. vs->mView = Mat44::sIdentity();
  419. // Light projection and view are unused in ortho mode
  420. vs->mLightView = Mat44::sIdentity();
  421. vs->mLightProjection = Mat44::sIdentity();
  422. mVertexShaderConstantBufferOrtho[mFrameIndex]->Unmap();
  423. // Switch to 3d projection mode
  424. SetProjectionMode();
  425. // Set constants for pixel shader
  426. PixelShaderConstantBuffer *ps = mPixelShaderConstantBuffer[mFrameIndex]->Map<PixelShaderConstantBuffer>();
  427. ps->mCameraPos = Vec4(cam_pos, 0);
  428. ps->mLightPos = Vec4(light_pos, 0);
  429. mPixelShaderConstantBuffer[mFrameIndex]->Unmap();
  430. // Set the pixel shader constant buffer data.
  431. mPixelShaderConstantBuffer[mFrameIndex]->Bind(1);
  432. // Calculate camera frustum
  433. mCameraFrustum = Frustum(cam_pos, inCamera.mForward, inCamera.mUp, camera_fovx, camera_fovy, camera_near, camera_far);
  434. // Calculate light frustum
  435. mLightFrustum = Frustum(light_pos, light_fwd, light_up, light_fov, light_fov, light_near, light_far);
  436. }
  437. void Renderer::EndFrame()
  438. {
  439. JPH_PROFILE_FUNCTION();
  440. // Indicate that the back buffer will now be used to present.
  441. D3D12_RESOURCE_BARRIER barrier;
  442. barrier.Type = D3D12_RESOURCE_BARRIER_TYPE_TRANSITION;
  443. barrier.Flags = D3D12_RESOURCE_BARRIER_FLAG_NONE;
  444. barrier.Transition.pResource = mRenderTargets[mFrameIndex].Get();
  445. barrier.Transition.StateBefore = D3D12_RESOURCE_STATE_RENDER_TARGET;
  446. barrier.Transition.StateAfter = D3D12_RESOURCE_STATE_PRESENT;
  447. barrier.Transition.Subresource = D3D12_RESOURCE_BARRIER_ALL_SUBRESOURCES;
  448. mCommandList->ResourceBarrier(1, &barrier);
  449. // Close the command list
  450. FatalErrorIfFailed(mCommandList->Close());
  451. // Execute the command list
  452. ID3D12CommandList* command_lists[] = { mCommandList.Get() };
  453. mCommandQueue->ExecuteCommandLists(_countof(command_lists), command_lists);
  454. // Present the frame
  455. FatalErrorIfFailed(mSwapChain->Present(1, 0));
  456. // Schedule a Signal command in the queue
  457. UINT64 current_fence_value = mFenceValues[mFrameIndex];
  458. FatalErrorIfFailed(mCommandQueue->Signal(mFence.Get(), current_fence_value));
  459. // Update the frame index
  460. mFrameIndex = mSwapChain->GetCurrentBackBufferIndex();
  461. // If the next frame is not ready to be rendered yet, wait until it is ready
  462. UINT64 completed_value = mFence->GetCompletedValue();
  463. if (completed_value < mFenceValues[mFrameIndex])
  464. {
  465. FatalErrorIfFailed(mFence->SetEventOnCompletion(mFenceValues[mFrameIndex], mFenceEvent));
  466. WaitForSingleObjectEx(mFenceEvent, INFINITE, FALSE);
  467. }
  468. // Release all used resources
  469. mDelayReleased[mFrameIndex].clear();
  470. // Anything that's not used yet can be removed, delayed objects are now available
  471. mResourceCache.clear();
  472. mDelayCached[mFrameIndex].swap(mResourceCache);
  473. // Set the fence value for the next frame.
  474. mFenceValues[mFrameIndex] = current_fence_value + 1;
  475. }
  476. void Renderer::SetProjectionMode()
  477. {
  478. mVertexShaderConstantBufferProjection[mFrameIndex]->Bind(0);
  479. }
  480. void Renderer::SetOrthoMode()
  481. {
  482. mVertexShaderConstantBufferOrtho[mFrameIndex]->Bind(0);
  483. }
  484. Ref<Texture> Renderer::CreateTexture(const Surface *inSurface)
  485. {
  486. return new Texture(this, inSurface);
  487. }
  488. Ref<Texture> Renderer::CreateRenderTarget(int inWidth, int inHeight)
  489. {
  490. return new Texture(this, inWidth, inHeight);
  491. }
  492. void Renderer::SetRenderTarget(Texture *inRenderTarget)
  493. {
  494. // Unset the previous render target
  495. if (mRenderTargetTexture != nullptr)
  496. mRenderTargetTexture->SetAsRenderTarget(false);
  497. mRenderTargetTexture = nullptr;
  498. if (inRenderTarget == nullptr)
  499. {
  500. // Set the main back buffer as render target
  501. mCommandList->OMSetRenderTargets(1, &mRenderTargetViews[mFrameIndex], FALSE, &mDepthStencilView);
  502. // Set viewport
  503. D3D12_VIEWPORT viewport = { 0.0f, 0.0f, static_cast<float>(mWindowWidth), static_cast<float>(mWindowHeight), 0.0f, 1.0f };
  504. mCommandList->RSSetViewports(1, &viewport);
  505. // Set scissor rect
  506. D3D12_RECT scissor_rect = { 0, 0, static_cast<LONG>(mWindowWidth), static_cast<LONG>(mWindowHeight) };
  507. mCommandList->RSSetScissorRects(1, &scissor_rect);
  508. }
  509. else
  510. {
  511. // Use the texture as render target
  512. inRenderTarget->SetAsRenderTarget(true);
  513. mRenderTargetTexture = inRenderTarget;
  514. }
  515. }
  516. ComPtr<ID3DBlob> Renderer::CreateVertexShader(const char *inFileName)
  517. {
  518. UINT flags = D3DCOMPILE_ENABLE_STRICTNESS;
  519. #ifdef _DEBUG
  520. flags |= D3DCOMPILE_DEBUG | D3DCOMPILE_SKIP_OPTIMIZATION;
  521. #endif
  522. const D3D_SHADER_MACRO defines[] =
  523. {
  524. { nullptr, nullptr }
  525. };
  526. // Read shader source file
  527. Array<uint8> data = ReadData(inFileName);
  528. // Compile source
  529. ComPtr<ID3DBlob> shader_blob, error_blob;
  530. HRESULT hr = D3DCompile(&data[0],
  531. (uint)data.size(),
  532. inFileName,
  533. defines,
  534. D3D_COMPILE_STANDARD_FILE_INCLUDE,
  535. "main",
  536. "vs_5_0",
  537. flags,
  538. 0,
  539. shader_blob.GetAddressOf(),
  540. error_blob.GetAddressOf());
  541. if (FAILED(hr))
  542. {
  543. // Throw error if compilation failed
  544. if (error_blob)
  545. OutputDebugStringA((const char *)error_blob->GetBufferPointer());
  546. FatalError("Failed to compile vertex shader");
  547. }
  548. return shader_blob;
  549. }
  550. ComPtr<ID3DBlob> Renderer::CreatePixelShader(const char *inFileName)
  551. {
  552. UINT flags = D3DCOMPILE_ENABLE_STRICTNESS;
  553. #ifdef _DEBUG
  554. flags |= D3DCOMPILE_DEBUG | D3DCOMPILE_SKIP_OPTIMIZATION;
  555. #endif
  556. const D3D_SHADER_MACRO defines[] =
  557. {
  558. { nullptr, nullptr }
  559. };
  560. // Read shader source file
  561. Array<uint8> data = ReadData(inFileName);
  562. // Compile source
  563. ComPtr<ID3DBlob> shader_blob, error_blob;
  564. HRESULT hr = D3DCompile(&data[0],
  565. (uint)data.size(),
  566. inFileName,
  567. defines,
  568. D3D_COMPILE_STANDARD_FILE_INCLUDE,
  569. "main",
  570. "ps_5_0",
  571. flags,
  572. 0,
  573. shader_blob.GetAddressOf(),
  574. error_blob.GetAddressOf());
  575. if (FAILED(hr))
  576. {
  577. // Throw error if compilation failed
  578. if (error_blob)
  579. OutputDebugStringA((const char *)error_blob->GetBufferPointer());
  580. FatalError("Failed to compile pixel shader");
  581. }
  582. return shader_blob;
  583. }
  584. unique_ptr<ConstantBuffer> Renderer::CreateConstantBuffer(uint inBufferSize)
  585. {
  586. return make_unique<ConstantBuffer>(this, inBufferSize);
  587. }
  588. unique_ptr<PipelineState> Renderer::CreatePipelineState(ID3DBlob *inVertexShader, const D3D12_INPUT_ELEMENT_DESC *inInputDescription, uint inInputDescriptionCount, ID3DBlob *inPixelShader, D3D12_FILL_MODE inFillMode, D3D12_PRIMITIVE_TOPOLOGY_TYPE inTopology, PipelineState::EDepthTest inDepthTest, PipelineState::EBlendMode inBlendMode, PipelineState::ECullMode inCullMode)
  589. {
  590. return make_unique<PipelineState>(this, inVertexShader, inInputDescription, inInputDescriptionCount, inPixelShader, inFillMode, inTopology, inDepthTest, inBlendMode, inCullMode);
  591. }
  592. ComPtr<ID3D12Resource> Renderer::CreateD3DResource(D3D12_HEAP_TYPE inHeapType, D3D12_RESOURCE_STATES inResourceState, uint64 inSize)
  593. {
  594. // Create a new resource
  595. D3D12_RESOURCE_DESC desc;
  596. desc.Dimension = D3D12_RESOURCE_DIMENSION_BUFFER;
  597. desc.Alignment = 0;
  598. desc.Width = inSize;
  599. desc.Height = 1;
  600. desc.DepthOrArraySize = 1;
  601. desc.MipLevels = 1;
  602. desc.Format = DXGI_FORMAT_UNKNOWN;
  603. desc.SampleDesc.Count = 1;
  604. desc.SampleDesc.Quality = 0;
  605. desc.Layout = D3D12_TEXTURE_LAYOUT_ROW_MAJOR;
  606. desc.Flags = D3D12_RESOURCE_FLAG_NONE;
  607. D3D12_HEAP_PROPERTIES heap_properties = {};
  608. heap_properties.Type = inHeapType;
  609. heap_properties.CPUPageProperty = D3D12_CPU_PAGE_PROPERTY_UNKNOWN;
  610. heap_properties.MemoryPoolPreference = D3D12_MEMORY_POOL_UNKNOWN;
  611. heap_properties.CreationNodeMask = 1;
  612. heap_properties.VisibleNodeMask = 1;
  613. ComPtr<ID3D12Resource> resource;
  614. FatalErrorIfFailed(mDevice->CreateCommittedResource(&heap_properties, D3D12_HEAP_FLAG_NONE, &desc, inResourceState, nullptr, IID_PPV_ARGS(&resource)));
  615. return resource;
  616. }
  617. void Renderer::CopyD3DResource(ID3D12Resource *inDest, const void *inSrc, uint64 inSize)
  618. {
  619. // Copy data to destination buffer
  620. void *data;
  621. D3D12_RANGE range = { 0, 0 }; // We're not going to read
  622. FatalErrorIfFailed(inDest->Map(0, &range, &data));
  623. memcpy(data, inSrc, size_t(inSize));
  624. inDest->Unmap(0, nullptr);
  625. }
  626. void Renderer::CopyD3DResource(ID3D12Resource *inDest, ID3D12Resource *inSrc, uint64 inSize)
  627. {
  628. // Start a commandlist for the upload
  629. ID3D12GraphicsCommandList *list = mUploadQueue.Start();
  630. // Copy the data to the GPU
  631. list->CopyBufferRegion(inDest, 0, inSrc, 0, inSize);
  632. // Change the state of the resource to generic read
  633. D3D12_RESOURCE_BARRIER barrier;
  634. barrier.Type = D3D12_RESOURCE_BARRIER_TYPE_TRANSITION;
  635. barrier.Flags = D3D12_RESOURCE_BARRIER_FLAG_NONE;
  636. barrier.Transition.pResource = inDest;
  637. barrier.Transition.StateBefore = D3D12_RESOURCE_STATE_COPY_DEST;
  638. barrier.Transition.StateAfter = D3D12_RESOURCE_STATE_GENERIC_READ;
  639. barrier.Transition.Subresource = D3D12_RESOURCE_BARRIER_ALL_SUBRESOURCES;
  640. list->ResourceBarrier(1, &barrier);
  641. // Wait for copying to finish
  642. mUploadQueue.ExecuteAndWait();
  643. }
  644. ComPtr<ID3D12Resource> Renderer::CreateD3DResourceOnDefaultHeap(const void *inData, uint64 inSize)
  645. {
  646. ComPtr<ID3D12Resource> upload = CreateD3DResourceOnUploadHeap(inSize);
  647. ComPtr<ID3D12Resource> resource = CreateD3DResource(D3D12_HEAP_TYPE_DEFAULT, D3D12_RESOURCE_STATE_COMMON, inSize);
  648. CopyD3DResource(upload.Get(), inData, inSize);
  649. CopyD3DResource(resource.Get(), upload.Get(), inSize);
  650. RecycleD3DResourceOnUploadHeap(upload.Get(), inSize);
  651. return resource;
  652. }
  653. ComPtr<ID3D12Resource> Renderer::CreateD3DResourceOnUploadHeap(uint64 inSize)
  654. {
  655. // Try cache first
  656. ResourceCache::iterator i = mResourceCache.find(inSize);
  657. if (i != mResourceCache.end() && !i->second.empty())
  658. {
  659. ComPtr<ID3D12Resource> resource = i->second.back();
  660. i->second.pop_back();
  661. return resource;
  662. }
  663. return CreateD3DResource(D3D12_HEAP_TYPE_UPLOAD, D3D12_RESOURCE_STATE_GENERIC_READ, inSize);
  664. }
  665. void Renderer::RecycleD3DResourceOnUploadHeap(ID3D12Resource *inResource, uint64 inSize)
  666. {
  667. if (!mIsExiting)
  668. mDelayCached[mFrameIndex][inSize].push_back(inResource);
  669. }
  670. void Renderer::RecycleD3DObject(ID3D12Object *inResource)
  671. {
  672. if (!mIsExiting)
  673. mDelayReleased[mFrameIndex].push_back(inResource);
  674. }