Renderer.cpp 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832
  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. /// Construct a perspective matrix
  361. static inline Mat44 sPerspective(float inFovY, float inAspect, float inNear, float inFar)
  362. {
  363. float height = 1.0f / Tan(0.5f * inFovY);
  364. float width = height / inAspect;
  365. float range = inFar / (inNear - inFar);
  366. return Mat44(Vec4(width, 0.0f, 0.0f, 0.0f), Vec4(0.0f, height, 0.0f, 0.0f), Vec4(0.0f, 0.0f, range, -1.0f), Vec4(0.0f, 0.0f, range * inNear, 0.0f));
  367. }
  368. void Renderer::BeginFrame(const CameraState &inCamera, float inWorldScale)
  369. {
  370. JPH_PROFILE_FUNCTION();
  371. // Store state
  372. mCameraState = inCamera;
  373. // Reset command allocator
  374. FatalErrorIfFailed(mCommandAllocators[mFrameIndex]->Reset());
  375. // Reset command list
  376. FatalErrorIfFailed(mCommandList->Reset(mCommandAllocators[mFrameIndex].Get(), nullptr));
  377. // Set root signature
  378. mCommandList->SetGraphicsRootSignature(mRootSignature.Get());
  379. // Set SRV heap
  380. ID3D12DescriptorHeap *heaps[] = { mSRVHeap.Get() };
  381. mCommandList->SetDescriptorHeaps(_countof(heaps), heaps);
  382. // Indicate that the back buffer will be used as a render target.
  383. D3D12_RESOURCE_BARRIER barrier;
  384. barrier.Type = D3D12_RESOURCE_BARRIER_TYPE_TRANSITION;
  385. barrier.Flags = D3D12_RESOURCE_BARRIER_FLAG_NONE;
  386. barrier.Transition.pResource = mRenderTargets[mFrameIndex].Get();
  387. barrier.Transition.StateBefore = D3D12_RESOURCE_STATE_PRESENT;
  388. barrier.Transition.StateAfter = D3D12_RESOURCE_STATE_RENDER_TARGET;
  389. barrier.Transition.Subresource = D3D12_RESOURCE_BARRIER_ALL_SUBRESOURCES;
  390. mCommandList->ResourceBarrier(1, &barrier);
  391. // Set the main back buffer as render target
  392. SetRenderTarget(nullptr);
  393. // Clear the back buffer.
  394. const float blue[] = { 0.098f, 0.098f, 0.439f, 1.000f };
  395. mCommandList->ClearRenderTargetView(mRenderTargetViews[mFrameIndex], blue, 0, nullptr);
  396. mCommandList->ClearDepthStencilView(mDepthStencilView, D3D12_CLEAR_FLAG_DEPTH, 1.0f, 0, 0, nullptr);
  397. // Light properties
  398. Vec3 light_pos = inWorldScale * Vec3(250, 250, 250);
  399. Vec3 light_tgt = Vec3::sZero();
  400. Vec3 light_up = Vec3(0, 1, 0);
  401. Vec3 light_fwd = (light_tgt - light_pos).Normalized();
  402. float light_fov = DegreesToRadians(20.0f);
  403. float light_near = 1.0f;
  404. float light_far = 1000.0f;
  405. // Camera properties
  406. float camera_fovy = inCamera.mFOVY;
  407. float camera_aspect = static_cast<float>(GetWindowWidth()) / GetWindowHeight();
  408. float camera_fovx = 2.0f * ATan(camera_aspect * Tan(0.5f * camera_fovy));
  409. float camera_near = 0.01f * inWorldScale;
  410. float camera_far = inCamera.mFarPlane * inWorldScale;
  411. // Set constants for vertex shader in projection mode
  412. VertexShaderConstantBuffer *vs = mVertexShaderConstantBufferProjection[mFrameIndex]->Map<VertexShaderConstantBuffer>();
  413. // Camera projection and view
  414. vs->mProjection = sPerspective(camera_fovy, camera_aspect, camera_near, camera_far);
  415. Vec3 cam_pos = Vec3(inCamera.mPos - mBaseOffset);
  416. Vec3 tgt = cam_pos + inCamera.mForward;
  417. vs->mView = Mat44::sLookAt(cam_pos, tgt, inCamera.mUp);
  418. // Light projection and view
  419. vs->mLightProjection = sPerspective(light_fov, 1.0f, light_near, light_far);
  420. vs->mLightView = Mat44::sLookAt(light_pos, light_tgt, light_up);
  421. mVertexShaderConstantBufferProjection[mFrameIndex]->Unmap();
  422. // Set constants for vertex shader in ortho mode
  423. vs = mVertexShaderConstantBufferOrtho[mFrameIndex]->Map<VertexShaderConstantBuffer>();
  424. // Camera ortho projection and view
  425. 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));
  426. vs->mView = Mat44::sIdentity();
  427. // Light projection and view are unused in ortho mode
  428. vs->mLightView = Mat44::sIdentity();
  429. vs->mLightProjection = Mat44::sIdentity();
  430. mVertexShaderConstantBufferOrtho[mFrameIndex]->Unmap();
  431. // Switch to 3d projection mode
  432. SetProjectionMode();
  433. // Set constants for pixel shader
  434. PixelShaderConstantBuffer *ps = mPixelShaderConstantBuffer[mFrameIndex]->Map<PixelShaderConstantBuffer>();
  435. ps->mCameraPos = Vec4(cam_pos, 0);
  436. ps->mLightPos = Vec4(light_pos, 0);
  437. mPixelShaderConstantBuffer[mFrameIndex]->Unmap();
  438. // Set the pixel shader constant buffer data.
  439. mPixelShaderConstantBuffer[mFrameIndex]->Bind(1);
  440. // Calculate camera frustum
  441. mCameraFrustum = Frustum(cam_pos, inCamera.mForward, inCamera.mUp, camera_fovx, camera_fovy, camera_near, camera_far);
  442. // Calculate light frustum
  443. mLightFrustum = Frustum(light_pos, light_fwd, light_up, light_fov, light_fov, light_near, light_far);
  444. }
  445. void Renderer::EndFrame()
  446. {
  447. JPH_PROFILE_FUNCTION();
  448. // Indicate that the back buffer will now be used to present.
  449. D3D12_RESOURCE_BARRIER barrier;
  450. barrier.Type = D3D12_RESOURCE_BARRIER_TYPE_TRANSITION;
  451. barrier.Flags = D3D12_RESOURCE_BARRIER_FLAG_NONE;
  452. barrier.Transition.pResource = mRenderTargets[mFrameIndex].Get();
  453. barrier.Transition.StateBefore = D3D12_RESOURCE_STATE_RENDER_TARGET;
  454. barrier.Transition.StateAfter = D3D12_RESOURCE_STATE_PRESENT;
  455. barrier.Transition.Subresource = D3D12_RESOURCE_BARRIER_ALL_SUBRESOURCES;
  456. mCommandList->ResourceBarrier(1, &barrier);
  457. // Close the command list
  458. FatalErrorIfFailed(mCommandList->Close());
  459. // Execute the command list
  460. ID3D12CommandList* command_lists[] = { mCommandList.Get() };
  461. mCommandQueue->ExecuteCommandLists(_countof(command_lists), command_lists);
  462. // Present the frame
  463. FatalErrorIfFailed(mSwapChain->Present(1, 0));
  464. // Schedule a Signal command in the queue
  465. UINT64 current_fence_value = mFenceValues[mFrameIndex];
  466. FatalErrorIfFailed(mCommandQueue->Signal(mFence.Get(), current_fence_value));
  467. // Update the frame index
  468. mFrameIndex = mSwapChain->GetCurrentBackBufferIndex();
  469. // If the next frame is not ready to be rendered yet, wait until it is ready
  470. UINT64 completed_value = mFence->GetCompletedValue();
  471. if (completed_value < mFenceValues[mFrameIndex])
  472. {
  473. FatalErrorIfFailed(mFence->SetEventOnCompletion(mFenceValues[mFrameIndex], mFenceEvent));
  474. WaitForSingleObjectEx(mFenceEvent, INFINITE, FALSE);
  475. }
  476. // Release all used resources
  477. mDelayReleased[mFrameIndex].clear();
  478. // Anything that's not used yet can be removed, delayed objects are now available
  479. mResourceCache.clear();
  480. mDelayCached[mFrameIndex].swap(mResourceCache);
  481. // Set the fence value for the next frame.
  482. mFenceValues[mFrameIndex] = current_fence_value + 1;
  483. }
  484. void Renderer::SetProjectionMode()
  485. {
  486. mVertexShaderConstantBufferProjection[mFrameIndex]->Bind(0);
  487. }
  488. void Renderer::SetOrthoMode()
  489. {
  490. mVertexShaderConstantBufferOrtho[mFrameIndex]->Bind(0);
  491. }
  492. Ref<Texture> Renderer::CreateTexture(const Surface *inSurface)
  493. {
  494. return new Texture(this, inSurface);
  495. }
  496. Ref<Texture> Renderer::CreateRenderTarget(int inWidth, int inHeight)
  497. {
  498. return new Texture(this, inWidth, inHeight);
  499. }
  500. void Renderer::SetRenderTarget(Texture *inRenderTarget)
  501. {
  502. // Unset the previous render target
  503. if (mRenderTargetTexture != nullptr)
  504. mRenderTargetTexture->SetAsRenderTarget(false);
  505. mRenderTargetTexture = nullptr;
  506. if (inRenderTarget == nullptr)
  507. {
  508. // Set the main back buffer as render target
  509. mCommandList->OMSetRenderTargets(1, &mRenderTargetViews[mFrameIndex], FALSE, &mDepthStencilView);
  510. // Set viewport
  511. D3D12_VIEWPORT viewport = { 0.0f, 0.0f, static_cast<float>(mWindowWidth), static_cast<float>(mWindowHeight), 0.0f, 1.0f };
  512. mCommandList->RSSetViewports(1, &viewport);
  513. // Set scissor rect
  514. D3D12_RECT scissor_rect = { 0, 0, static_cast<LONG>(mWindowWidth), static_cast<LONG>(mWindowHeight) };
  515. mCommandList->RSSetScissorRects(1, &scissor_rect);
  516. }
  517. else
  518. {
  519. // Use the texture as render target
  520. inRenderTarget->SetAsRenderTarget(true);
  521. mRenderTargetTexture = inRenderTarget;
  522. }
  523. }
  524. ComPtr<ID3DBlob> Renderer::CreateVertexShader(const char *inFileName)
  525. {
  526. UINT flags = D3DCOMPILE_ENABLE_STRICTNESS;
  527. #ifdef _DEBUG
  528. flags |= D3DCOMPILE_DEBUG | D3DCOMPILE_SKIP_OPTIMIZATION;
  529. #endif
  530. const D3D_SHADER_MACRO defines[] =
  531. {
  532. { nullptr, nullptr }
  533. };
  534. // Read shader source file
  535. Array<uint8> data = ReadData(inFileName);
  536. // Compile source
  537. ComPtr<ID3DBlob> shader_blob, error_blob;
  538. HRESULT hr = D3DCompile(&data[0],
  539. (uint)data.size(),
  540. inFileName,
  541. defines,
  542. D3D_COMPILE_STANDARD_FILE_INCLUDE,
  543. "main",
  544. "vs_5_0",
  545. flags,
  546. 0,
  547. shader_blob.GetAddressOf(),
  548. error_blob.GetAddressOf());
  549. if (FAILED(hr))
  550. {
  551. // Throw error if compilation failed
  552. if (error_blob)
  553. OutputDebugStringA((const char *)error_blob->GetBufferPointer());
  554. FatalError("Failed to compile vertex shader");
  555. }
  556. return shader_blob;
  557. }
  558. ComPtr<ID3DBlob> Renderer::CreatePixelShader(const char *inFileName)
  559. {
  560. UINT flags = D3DCOMPILE_ENABLE_STRICTNESS;
  561. #ifdef _DEBUG
  562. flags |= D3DCOMPILE_DEBUG | D3DCOMPILE_SKIP_OPTIMIZATION;
  563. #endif
  564. const D3D_SHADER_MACRO defines[] =
  565. {
  566. { nullptr, nullptr }
  567. };
  568. // Read shader source file
  569. Array<uint8> data = ReadData(inFileName);
  570. // Compile source
  571. ComPtr<ID3DBlob> shader_blob, error_blob;
  572. HRESULT hr = D3DCompile(&data[0],
  573. (uint)data.size(),
  574. inFileName,
  575. defines,
  576. D3D_COMPILE_STANDARD_FILE_INCLUDE,
  577. "main",
  578. "ps_5_0",
  579. flags,
  580. 0,
  581. shader_blob.GetAddressOf(),
  582. error_blob.GetAddressOf());
  583. if (FAILED(hr))
  584. {
  585. // Throw error if compilation failed
  586. if (error_blob)
  587. OutputDebugStringA((const char *)error_blob->GetBufferPointer());
  588. FatalError("Failed to compile pixel shader");
  589. }
  590. return shader_blob;
  591. }
  592. unique_ptr<ConstantBuffer> Renderer::CreateConstantBuffer(uint inBufferSize)
  593. {
  594. return make_unique<ConstantBuffer>(this, inBufferSize);
  595. }
  596. 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)
  597. {
  598. return make_unique<PipelineState>(this, inVertexShader, inInputDescription, inInputDescriptionCount, inPixelShader, inFillMode, inTopology, inDepthTest, inBlendMode, inCullMode);
  599. }
  600. ComPtr<ID3D12Resource> Renderer::CreateD3DResource(D3D12_HEAP_TYPE inHeapType, D3D12_RESOURCE_STATES inResourceState, uint64 inSize)
  601. {
  602. // Create a new resource
  603. D3D12_RESOURCE_DESC desc;
  604. desc.Dimension = D3D12_RESOURCE_DIMENSION_BUFFER;
  605. desc.Alignment = 0;
  606. desc.Width = inSize;
  607. desc.Height = 1;
  608. desc.DepthOrArraySize = 1;
  609. desc.MipLevels = 1;
  610. desc.Format = DXGI_FORMAT_UNKNOWN;
  611. desc.SampleDesc.Count = 1;
  612. desc.SampleDesc.Quality = 0;
  613. desc.Layout = D3D12_TEXTURE_LAYOUT_ROW_MAJOR;
  614. desc.Flags = D3D12_RESOURCE_FLAG_NONE;
  615. D3D12_HEAP_PROPERTIES heap_properties = {};
  616. heap_properties.Type = inHeapType;
  617. heap_properties.CPUPageProperty = D3D12_CPU_PAGE_PROPERTY_UNKNOWN;
  618. heap_properties.MemoryPoolPreference = D3D12_MEMORY_POOL_UNKNOWN;
  619. heap_properties.CreationNodeMask = 1;
  620. heap_properties.VisibleNodeMask = 1;
  621. ComPtr<ID3D12Resource> resource;
  622. FatalErrorIfFailed(mDevice->CreateCommittedResource(&heap_properties, D3D12_HEAP_FLAG_NONE, &desc, inResourceState, nullptr, IID_PPV_ARGS(&resource)));
  623. return resource;
  624. }
  625. void Renderer::CopyD3DResource(ID3D12Resource *inDest, const void *inSrc, uint64 inSize)
  626. {
  627. // Copy data to destination buffer
  628. void *data;
  629. D3D12_RANGE range = { 0, 0 }; // We're not going to read
  630. FatalErrorIfFailed(inDest->Map(0, &range, &data));
  631. memcpy(data, inSrc, size_t(inSize));
  632. inDest->Unmap(0, nullptr);
  633. }
  634. void Renderer::CopyD3DResource(ID3D12Resource *inDest, ID3D12Resource *inSrc, uint64 inSize)
  635. {
  636. // Start a commandlist for the upload
  637. ID3D12GraphicsCommandList *list = mUploadQueue.Start();
  638. // Copy the data to the GPU
  639. list->CopyBufferRegion(inDest, 0, inSrc, 0, inSize);
  640. // Change the state of the resource to generic read
  641. D3D12_RESOURCE_BARRIER barrier;
  642. barrier.Type = D3D12_RESOURCE_BARRIER_TYPE_TRANSITION;
  643. barrier.Flags = D3D12_RESOURCE_BARRIER_FLAG_NONE;
  644. barrier.Transition.pResource = inDest;
  645. barrier.Transition.StateBefore = D3D12_RESOURCE_STATE_COPY_DEST;
  646. barrier.Transition.StateAfter = D3D12_RESOURCE_STATE_GENERIC_READ;
  647. barrier.Transition.Subresource = D3D12_RESOURCE_BARRIER_ALL_SUBRESOURCES;
  648. list->ResourceBarrier(1, &barrier);
  649. // Wait for copying to finish
  650. mUploadQueue.ExecuteAndWait();
  651. }
  652. ComPtr<ID3D12Resource> Renderer::CreateD3DResourceOnDefaultHeap(const void *inData, uint64 inSize)
  653. {
  654. ComPtr<ID3D12Resource> upload = CreateD3DResourceOnUploadHeap(inSize);
  655. ComPtr<ID3D12Resource> resource = CreateD3DResource(D3D12_HEAP_TYPE_DEFAULT, D3D12_RESOURCE_STATE_COMMON, inSize);
  656. CopyD3DResource(upload.Get(), inData, inSize);
  657. CopyD3DResource(resource.Get(), upload.Get(), inSize);
  658. RecycleD3DResourceOnUploadHeap(upload.Get(), inSize);
  659. return resource;
  660. }
  661. ComPtr<ID3D12Resource> Renderer::CreateD3DResourceOnUploadHeap(uint64 inSize)
  662. {
  663. // Try cache first
  664. ResourceCache::iterator i = mResourceCache.find(inSize);
  665. if (i != mResourceCache.end() && !i->second.empty())
  666. {
  667. ComPtr<ID3D12Resource> resource = i->second.back();
  668. i->second.pop_back();
  669. return resource;
  670. }
  671. return CreateD3DResource(D3D12_HEAP_TYPE_UPLOAD, D3D12_RESOURCE_STATE_GENERIC_READ, inSize);
  672. }
  673. void Renderer::RecycleD3DResourceOnUploadHeap(ID3D12Resource *inResource, uint64 inSize)
  674. {
  675. if (!mIsExiting)
  676. mDelayCached[mFrameIndex][inSize].push_back(inResource);
  677. }
  678. void Renderer::RecycleD3DObject(ID3D12Object *inResource)
  679. {
  680. if (!mIsExiting)
  681. mDelayReleased[mFrameIndex].push_back(inResource);
  682. }