2
0

Renderer.cpp 28 KB

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