2
0

Renderer.cpp 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826
  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. // First check if we have the Windows 1803 IDXGIFactory6 interface
  168. ComPtr<IDXGIFactory6> factory6;
  169. if (SUCCEEDED(mDXGIFactory->QueryInterface(IID_PPV_ARGS(&factory6))))
  170. {
  171. for (UINT index = 0; DXGI_ERROR_NOT_FOUND != factory6->EnumAdapterByGpuPreference(index, DXGI_GPU_PREFERENCE_HIGH_PERFORMANCE, IID_PPV_ARGS(&adapter)); ++index)
  172. {
  173. DXGI_ADAPTER_DESC1 desc;
  174. adapter->GetDesc1(&desc);
  175. // We don't want software renderers
  176. if (desc.Flags & DXGI_ADAPTER_FLAG_SOFTWARE)
  177. continue;
  178. // Check to see whether the adapter supports Direct3D 12
  179. if (SUCCEEDED(D3D12CreateDevice(adapter.Get(), D3D_FEATURE_LEVEL_11_0, IID_PPV_ARGS(&mDevice))))
  180. break;
  181. }
  182. }
  183. else
  184. {
  185. // Fall back to the older method that may not get the fastest GPU
  186. for (UINT index = 0; DXGI_ERROR_NOT_FOUND != mDXGIFactory->EnumAdapters1(index, &adapter); ++index)
  187. {
  188. DXGI_ADAPTER_DESC1 desc;
  189. adapter->GetDesc1(&desc);
  190. // We don't want software renderers
  191. if (desc.Flags & DXGI_ADAPTER_FLAG_SOFTWARE)
  192. continue;
  193. // Check to see whether the adapter supports Direct3D 12
  194. if (SUCCEEDED(D3D12CreateDevice(adapter.Get(), D3D_FEATURE_LEVEL_11_0, IID_PPV_ARGS(&mDevice))))
  195. break;
  196. }
  197. }
  198. #ifdef _DEBUG
  199. // Enable breaking on errors
  200. ComPtr<ID3D12InfoQueue> info_queue;
  201. if (SUCCEEDED(mDevice.As(&info_queue)))
  202. {
  203. info_queue->SetBreakOnSeverity(D3D12_MESSAGE_SEVERITY_CORRUPTION, TRUE);
  204. info_queue->SetBreakOnSeverity(D3D12_MESSAGE_SEVERITY_ERROR, TRUE);
  205. info_queue->SetBreakOnSeverity(D3D12_MESSAGE_SEVERITY_WARNING, TRUE);
  206. // Disable an error that triggers on Windows 11 with a hybrid graphic system
  207. // See: https://stackoverflow.com/questions/69805245/directx-12-application-is-crashing-in-windows-11
  208. D3D12_MESSAGE_ID hide[] =
  209. {
  210. D3D12_MESSAGE_ID_RESOURCE_BARRIER_MISMATCHING_COMMAND_LIST_TYPE,
  211. };
  212. D3D12_INFO_QUEUE_FILTER filter = { };
  213. filter.DenyList.NumIDs = static_cast<UINT>( std::size( hide ) );
  214. filter.DenyList.pIDList = hide;
  215. info_queue->AddStorageFilterEntries( &filter );
  216. }
  217. #endif // _DEBUG
  218. // Disable full screen transitions
  219. FatalErrorIfFailed(mDXGIFactory->MakeWindowAssociation(mhWnd, DXGI_MWA_NO_ALT_ENTER));
  220. // Create heaps
  221. mRTVHeap.Init(mDevice.Get(), D3D12_DESCRIPTOR_HEAP_TYPE_RTV, D3D12_DESCRIPTOR_HEAP_FLAG_NONE, 2);
  222. mDSVHeap.Init(mDevice.Get(), D3D12_DESCRIPTOR_HEAP_TYPE_DSV, D3D12_DESCRIPTOR_HEAP_FLAG_NONE, 4);
  223. mSRVHeap.Init(mDevice.Get(), D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV, D3D12_DESCRIPTOR_HEAP_FLAG_SHADER_VISIBLE, 128);
  224. // Create a command queue
  225. D3D12_COMMAND_QUEUE_DESC queue_desc = {};
  226. queue_desc.Flags = D3D12_COMMAND_QUEUE_FLAG_NONE;
  227. queue_desc.Type = D3D12_COMMAND_LIST_TYPE_DIRECT;
  228. FatalErrorIfFailed(mDevice->CreateCommandQueue(&queue_desc, IID_PPV_ARGS(&mCommandQueue)));
  229. // Create a command allocator for each frame
  230. for (uint n = 0; n < cFrameCount; n++)
  231. FatalErrorIfFailed(mDevice->CreateCommandAllocator(D3D12_COMMAND_LIST_TYPE_DIRECT, IID_PPV_ARGS(&mCommandAllocators[n])));
  232. // Describe and create the swap chain
  233. DXGI_SWAP_CHAIN_DESC swap_chain_desc = {};
  234. swap_chain_desc.BufferCount = cFrameCount;
  235. swap_chain_desc.BufferDesc.Width = mWindowWidth;
  236. swap_chain_desc.BufferDesc.Height = mWindowHeight;
  237. swap_chain_desc.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
  238. swap_chain_desc.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
  239. swap_chain_desc.SwapEffect = DXGI_SWAP_EFFECT_FLIP_DISCARD;
  240. swap_chain_desc.OutputWindow = mhWnd;
  241. swap_chain_desc.SampleDesc.Count = 1;
  242. swap_chain_desc.Windowed = TRUE;
  243. ComPtr<IDXGISwapChain> swap_chain;
  244. FatalErrorIfFailed(mDXGIFactory->CreateSwapChain(mCommandQueue.Get(), &swap_chain_desc, &swap_chain));
  245. FatalErrorIfFailed(swap_chain.As(&mSwapChain));
  246. mFrameIndex = mSwapChain->GetCurrentBackBufferIndex();
  247. CreateRenterTargets();
  248. CreateDepthBuffer();
  249. // Create a root signature suitable for all our shaders
  250. D3D12_ROOT_PARAMETER params[3] = {};
  251. // Mapping a constant buffer to slot 0 for the vertex shader
  252. params[0].ParameterType = D3D12_ROOT_PARAMETER_TYPE_CBV;
  253. params[0].Descriptor.ShaderRegister = 0;
  254. params[0].ShaderVisibility = D3D12_SHADER_VISIBILITY_VERTEX;
  255. // Mapping a constant buffer to slot 1 in the pixel shader
  256. params[1].ParameterType = D3D12_ROOT_PARAMETER_TYPE_CBV;
  257. params[1].Descriptor.ShaderRegister = 1;
  258. params[1].ShaderVisibility = D3D12_SHADER_VISIBILITY_PIXEL;
  259. // Mapping a texture to slot 2 in the pixel shader
  260. D3D12_DESCRIPTOR_RANGE range = {};
  261. range.RangeType = D3D12_DESCRIPTOR_RANGE_TYPE_SRV;
  262. range.BaseShaderRegister = 2;
  263. range.NumDescriptors = 1;
  264. params[2].ParameterType = D3D12_ROOT_PARAMETER_TYPE_DESCRIPTOR_TABLE;
  265. params[2].DescriptorTable.NumDescriptorRanges = 1;
  266. params[2].DescriptorTable.pDescriptorRanges = &range;
  267. params[2].ShaderVisibility = D3D12_SHADER_VISIBILITY_PIXEL;
  268. D3D12_STATIC_SAMPLER_DESC samplers[3] = {};
  269. // Sampler 0: Non-wrapping linear filtering
  270. samplers[0].Filter = D3D12_FILTER_MIN_MAG_MIP_LINEAR;
  271. samplers[0].AddressU = D3D12_TEXTURE_ADDRESS_MODE_CLAMP;
  272. samplers[0].AddressV = D3D12_TEXTURE_ADDRESS_MODE_CLAMP;
  273. samplers[0].AddressW = D3D12_TEXTURE_ADDRESS_MODE_CLAMP;
  274. samplers[0].MipLODBias = 0.0f;
  275. samplers[0].MaxAnisotropy = 1;
  276. samplers[0].ComparisonFunc = D3D12_COMPARISON_FUNC_ALWAYS;
  277. samplers[0].BorderColor = D3D12_STATIC_BORDER_COLOR_TRANSPARENT_BLACK;
  278. samplers[0].MinLOD = 0.0f;
  279. samplers[0].MaxLOD = D3D12_FLOAT32_MAX;
  280. samplers[0].ShaderRegister = 0;
  281. samplers[0].ShaderVisibility = D3D12_SHADER_VISIBILITY_PIXEL;
  282. // Sampler 1: Wrapping and linear filtering
  283. samplers[1] = samplers[0];
  284. samplers[1].AddressU = D3D12_TEXTURE_ADDRESS_MODE_WRAP;
  285. samplers[1].AddressV = D3D12_TEXTURE_ADDRESS_MODE_WRAP;
  286. samplers[1].AddressW = D3D12_TEXTURE_ADDRESS_MODE_WRAP;
  287. samplers[1].ShaderRegister = 1;
  288. // Sampler 2: Point filtering, using SampleCmp mode to compare if sampled value <= reference value (for shadows)
  289. samplers[2] = samplers[0];
  290. samplers[2].Filter = D3D12_FILTER_COMPARISON_MIN_MAG_LINEAR_MIP_POINT;
  291. samplers[2].ComparisonFunc = D3D12_COMPARISON_FUNC_LESS_EQUAL;
  292. samplers[2].ShaderRegister = 2;
  293. D3D12_ROOT_SIGNATURE_DESC root_signature_desc = {};
  294. root_signature_desc.Flags = D3D12_ROOT_SIGNATURE_FLAG_ALLOW_INPUT_ASSEMBLER_INPUT_LAYOUT;
  295. root_signature_desc.NumParameters = ARRAYSIZE(params);
  296. root_signature_desc.pParameters = params;
  297. root_signature_desc.NumStaticSamplers = ARRAYSIZE(samplers);
  298. root_signature_desc.pStaticSamplers = samplers;
  299. ComPtr<ID3DBlob> signature;
  300. ComPtr<ID3DBlob> error;
  301. FatalErrorIfFailed(D3D12SerializeRootSignature(&root_signature_desc, D3D_ROOT_SIGNATURE_VERSION_1, &signature, &error));
  302. FatalErrorIfFailed(mDevice->CreateRootSignature(0, signature->GetBufferPointer(), signature->GetBufferSize(), IID_PPV_ARGS(&mRootSignature)));
  303. // Create the command list
  304. FatalErrorIfFailed(mDevice->CreateCommandList(0, D3D12_COMMAND_LIST_TYPE_DIRECT, mCommandAllocators[mFrameIndex].Get(), nullptr, IID_PPV_ARGS(&mCommandList)));
  305. // 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
  306. FatalErrorIfFailed(mCommandList->Close());
  307. // Create synchronization object
  308. FatalErrorIfFailed(mDevice->CreateFence(mFenceValues[mFrameIndex], D3D12_FENCE_FLAG_NONE, IID_PPV_ARGS(&mFence)));
  309. // Increment fence value so we don't skip waiting the first time a command list is executed
  310. mFenceValues[mFrameIndex]++;
  311. // Create an event handle to use for frame synchronization
  312. mFenceEvent = CreateEvent(nullptr, FALSE, FALSE, nullptr);
  313. if (mFenceEvent == nullptr)
  314. FatalErrorIfFailed(HRESULT_FROM_WIN32(GetLastError()));
  315. // Initialize the queue used to upload resources to the GPU
  316. mUploadQueue.Initialize(mDevice.Get());
  317. // Create constant buffer. One per frame to avoid overwriting the constant buffer while the GPU is still using it.
  318. for (uint n = 0; n < cFrameCount; ++n)
  319. {
  320. mVertexShaderConstantBufferProjection[n] = CreateConstantBuffer(sizeof(VertexShaderConstantBuffer));
  321. mVertexShaderConstantBufferOrtho[n] = CreateConstantBuffer(sizeof(VertexShaderConstantBuffer));
  322. mPixelShaderConstantBuffer[n] = CreateConstantBuffer(sizeof(PixelShaderConstantBuffer));
  323. }
  324. // Store global renderer now that we're done initializing
  325. sRenderer = this;
  326. }
  327. void Renderer::OnWindowResize()
  328. {
  329. // Wait for the previous frame to be rendered
  330. WaitForGpu();
  331. // Get new window size
  332. RECT rc;
  333. GetClientRect(mhWnd, &rc);
  334. mWindowWidth = max<LONG>(rc.right - rc.left, 8);
  335. mWindowHeight = max<LONG>(rc.bottom - rc.top, 8);
  336. // Free the render targets and views to allow resizing the swap chain
  337. for (uint n = 0; n < cFrameCount; ++n)
  338. {
  339. mRTVHeap.Free(mRenderTargetViews[n]);
  340. mRenderTargets[n].Reset();
  341. }
  342. // Resize the swap chain buffers
  343. FatalErrorIfFailed(mSwapChain->ResizeBuffers(cFrameCount, mWindowWidth, mWindowHeight, DXGI_FORMAT_R8G8B8A8_UNORM, 0));
  344. // Back buffer index may have changed after the resize (it always seems to go to 0 again)
  345. mFrameIndex = mSwapChain->GetCurrentBackBufferIndex();
  346. // 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
  347. for (uint n = 0; n < cFrameCount; ++n)
  348. if (mFrameIndex != n)
  349. mFenceValues[n] = mFence->GetCompletedValue();
  350. // Recreate render targets
  351. CreateRenterTargets();
  352. // Recreate depth buffer
  353. CreateDepthBuffer();
  354. }
  355. /// Construct a perspective matrix
  356. static inline Mat44 sPerspective(float inFovY, float inAspect, float inNear, float inFar)
  357. {
  358. float height = 1.0f / Tan(0.5f * inFovY);
  359. float width = height / inAspect;
  360. float range = inFar / (inNear - inFar);
  361. 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));
  362. }
  363. void Renderer::BeginFrame(const CameraState &inCamera, float inWorldScale)
  364. {
  365. JPH_PROFILE_FUNCTION();
  366. // Store state
  367. mCameraState = inCamera;
  368. // Reset command allocator
  369. FatalErrorIfFailed(mCommandAllocators[mFrameIndex]->Reset());
  370. // Reset command list
  371. FatalErrorIfFailed(mCommandList->Reset(mCommandAllocators[mFrameIndex].Get(), nullptr));
  372. // Set root signature
  373. mCommandList->SetGraphicsRootSignature(mRootSignature.Get());
  374. // Set SRV heap
  375. ID3D12DescriptorHeap *heaps[] = { mSRVHeap.Get() };
  376. mCommandList->SetDescriptorHeaps(_countof(heaps), heaps);
  377. // Indicate that the back buffer will be used as a render target.
  378. D3D12_RESOURCE_BARRIER barrier;
  379. barrier.Type = D3D12_RESOURCE_BARRIER_TYPE_TRANSITION;
  380. barrier.Flags = D3D12_RESOURCE_BARRIER_FLAG_NONE;
  381. barrier.Transition.pResource = mRenderTargets[mFrameIndex].Get();
  382. barrier.Transition.StateBefore = D3D12_RESOURCE_STATE_PRESENT;
  383. barrier.Transition.StateAfter = D3D12_RESOURCE_STATE_RENDER_TARGET;
  384. barrier.Transition.Subresource = D3D12_RESOURCE_BARRIER_ALL_SUBRESOURCES;
  385. mCommandList->ResourceBarrier(1, &barrier);
  386. // Set the main back buffer as render target
  387. SetRenderTarget(nullptr);
  388. // Clear the back buffer.
  389. const float blue[] = { 0.098f, 0.098f, 0.439f, 1.000f };
  390. mCommandList->ClearRenderTargetView(mRenderTargetViews[mFrameIndex], blue, 0, nullptr);
  391. mCommandList->ClearDepthStencilView(mDepthStencilView, D3D12_CLEAR_FLAG_DEPTH, 1.0f, 0, 0, nullptr);
  392. // Light properties
  393. Vec3 light_pos = inWorldScale * Vec3(250, 250, 250);
  394. Vec3 light_tgt = Vec3::sZero();
  395. Vec3 light_up = Vec3(0, 1, 0);
  396. Vec3 light_fwd = (light_tgt - light_pos).Normalized();
  397. float light_fov = DegreesToRadians(20.0f);
  398. float light_near = 1.0f;
  399. float light_far = 1000.0f;
  400. // Camera properties
  401. float camera_fovy = inCamera.mFOVY;
  402. float camera_aspect = static_cast<float>(GetWindowWidth()) / GetWindowHeight();
  403. float camera_fovx = 2.0f * ATan(camera_aspect * Tan(0.5f * camera_fovy));
  404. float camera_near = 0.01f * inWorldScale;
  405. float camera_far = inCamera.mFarPlane * inWorldScale;
  406. // Set constants for vertex shader in projection mode
  407. VertexShaderConstantBuffer *vs = mVertexShaderConstantBufferProjection[mFrameIndex]->Map<VertexShaderConstantBuffer>();
  408. // Camera projection and view
  409. vs->mProjection = sPerspective(camera_fovy, camera_aspect, camera_near, camera_far);
  410. Vec3 cam_pos = Vec3(inCamera.mPos - mBaseOffset);
  411. Vec3 tgt = cam_pos + inCamera.mForward;
  412. vs->mView = Mat44::sLookAt(cam_pos, tgt, inCamera.mUp);
  413. // Light projection and view
  414. vs->mLightProjection = sPerspective(light_fov, 1.0f, light_near, light_far);
  415. vs->mLightView = Mat44::sLookAt(light_pos, light_tgt, light_up);
  416. mVertexShaderConstantBufferProjection[mFrameIndex]->Unmap();
  417. // Set constants for vertex shader in ortho mode
  418. vs = mVertexShaderConstantBufferOrtho[mFrameIndex]->Map<VertexShaderConstantBuffer>();
  419. // Camera ortho projection and view
  420. 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));
  421. vs->mView = Mat44::sIdentity();
  422. // Light projection and view are unused in ortho mode
  423. vs->mLightView = Mat44::sIdentity();
  424. vs->mLightProjection = Mat44::sIdentity();
  425. mVertexShaderConstantBufferOrtho[mFrameIndex]->Unmap();
  426. // Switch to 3d projection mode
  427. SetProjectionMode();
  428. // Set constants for pixel shader
  429. PixelShaderConstantBuffer *ps = mPixelShaderConstantBuffer[mFrameIndex]->Map<PixelShaderConstantBuffer>();
  430. ps->mCameraPos = Vec4(cam_pos, 0);
  431. ps->mLightPos = Vec4(light_pos, 0);
  432. mPixelShaderConstantBuffer[mFrameIndex]->Unmap();
  433. // Set the pixel shader constant buffer data.
  434. mPixelShaderConstantBuffer[mFrameIndex]->Bind(1);
  435. // Calculate camera frustum
  436. mCameraFrustum = Frustum(cam_pos, inCamera.mForward, inCamera.mUp, camera_fovx, camera_fovy, camera_near, camera_far);
  437. // Calculate light frustum
  438. mLightFrustum = Frustum(light_pos, light_fwd, light_up, light_fov, light_fov, light_near, light_far);
  439. }
  440. void Renderer::EndFrame()
  441. {
  442. JPH_PROFILE_FUNCTION();
  443. // Indicate that the back buffer will now be used to present.
  444. D3D12_RESOURCE_BARRIER barrier;
  445. barrier.Type = D3D12_RESOURCE_BARRIER_TYPE_TRANSITION;
  446. barrier.Flags = D3D12_RESOURCE_BARRIER_FLAG_NONE;
  447. barrier.Transition.pResource = mRenderTargets[mFrameIndex].Get();
  448. barrier.Transition.StateBefore = D3D12_RESOURCE_STATE_RENDER_TARGET;
  449. barrier.Transition.StateAfter = D3D12_RESOURCE_STATE_PRESENT;
  450. barrier.Transition.Subresource = D3D12_RESOURCE_BARRIER_ALL_SUBRESOURCES;
  451. mCommandList->ResourceBarrier(1, &barrier);
  452. // Close the command list
  453. FatalErrorIfFailed(mCommandList->Close());
  454. // Execute the command list
  455. ID3D12CommandList* command_lists[] = { mCommandList.Get() };
  456. mCommandQueue->ExecuteCommandLists(_countof(command_lists), command_lists);
  457. // Present the frame
  458. FatalErrorIfFailed(mSwapChain->Present(1, 0));
  459. // Schedule a Signal command in the queue
  460. UINT64 current_fence_value = mFenceValues[mFrameIndex];
  461. FatalErrorIfFailed(mCommandQueue->Signal(mFence.Get(), current_fence_value));
  462. // Update the frame index
  463. mFrameIndex = mSwapChain->GetCurrentBackBufferIndex();
  464. // If the next frame is not ready to be rendered yet, wait until it is ready
  465. UINT64 completed_value = mFence->GetCompletedValue();
  466. if (completed_value < mFenceValues[mFrameIndex])
  467. {
  468. FatalErrorIfFailed(mFence->SetEventOnCompletion(mFenceValues[mFrameIndex], mFenceEvent));
  469. WaitForSingleObjectEx(mFenceEvent, INFINITE, FALSE);
  470. }
  471. // Release all used resources
  472. mDelayReleased[mFrameIndex].clear();
  473. // Anything that's not used yet can be removed, delayed objects are now available
  474. mResourceCache.clear();
  475. mDelayCached[mFrameIndex].swap(mResourceCache);
  476. // Set the fence value for the next frame.
  477. mFenceValues[mFrameIndex] = current_fence_value + 1;
  478. }
  479. void Renderer::SetProjectionMode()
  480. {
  481. mVertexShaderConstantBufferProjection[mFrameIndex]->Bind(0);
  482. }
  483. void Renderer::SetOrthoMode()
  484. {
  485. mVertexShaderConstantBufferOrtho[mFrameIndex]->Bind(0);
  486. }
  487. Ref<Texture> Renderer::CreateTexture(const Surface *inSurface)
  488. {
  489. return new Texture(this, inSurface);
  490. }
  491. Ref<Texture> Renderer::CreateRenderTarget(int inWidth, int inHeight)
  492. {
  493. return new Texture(this, inWidth, inHeight);
  494. }
  495. void Renderer::SetRenderTarget(Texture *inRenderTarget)
  496. {
  497. // Unset the previous render target
  498. if (mRenderTargetTexture != nullptr)
  499. mRenderTargetTexture->SetAsRenderTarget(false);
  500. mRenderTargetTexture = nullptr;
  501. if (inRenderTarget == nullptr)
  502. {
  503. // Set the main back buffer as render target
  504. mCommandList->OMSetRenderTargets(1, &mRenderTargetViews[mFrameIndex], FALSE, &mDepthStencilView);
  505. // Set viewport
  506. D3D12_VIEWPORT viewport = { 0.0f, 0.0f, static_cast<float>(mWindowWidth), static_cast<float>(mWindowHeight), 0.0f, 1.0f };
  507. mCommandList->RSSetViewports(1, &viewport);
  508. // Set scissor rect
  509. D3D12_RECT scissor_rect = { 0, 0, static_cast<LONG>(mWindowWidth), static_cast<LONG>(mWindowHeight) };
  510. mCommandList->RSSetScissorRects(1, &scissor_rect);
  511. }
  512. else
  513. {
  514. // Use the texture as render target
  515. inRenderTarget->SetAsRenderTarget(true);
  516. mRenderTargetTexture = inRenderTarget;
  517. }
  518. }
  519. ComPtr<ID3DBlob> Renderer::CreateVertexShader(const char *inFileName)
  520. {
  521. UINT flags = D3DCOMPILE_ENABLE_STRICTNESS;
  522. #ifdef _DEBUG
  523. flags |= D3DCOMPILE_DEBUG | D3DCOMPILE_SKIP_OPTIMIZATION;
  524. #endif
  525. const D3D_SHADER_MACRO defines[] =
  526. {
  527. { nullptr, nullptr }
  528. };
  529. // Read shader source file
  530. Array<uint8> data = ReadData(inFileName);
  531. // Compile source
  532. ComPtr<ID3DBlob> shader_blob, error_blob;
  533. HRESULT hr = D3DCompile(&data[0],
  534. (uint)data.size(),
  535. inFileName,
  536. defines,
  537. D3D_COMPILE_STANDARD_FILE_INCLUDE,
  538. "main",
  539. "vs_5_0",
  540. flags,
  541. 0,
  542. shader_blob.GetAddressOf(),
  543. error_blob.GetAddressOf());
  544. if (FAILED(hr))
  545. {
  546. // Throw error if compilation failed
  547. if (error_blob)
  548. OutputDebugStringA((const char *)error_blob->GetBufferPointer());
  549. FatalError("Failed to compile vertex shader");
  550. }
  551. return shader_blob;
  552. }
  553. ComPtr<ID3DBlob> Renderer::CreatePixelShader(const char *inFileName)
  554. {
  555. UINT flags = D3DCOMPILE_ENABLE_STRICTNESS;
  556. #ifdef _DEBUG
  557. flags |= D3DCOMPILE_DEBUG | D3DCOMPILE_SKIP_OPTIMIZATION;
  558. #endif
  559. const D3D_SHADER_MACRO defines[] =
  560. {
  561. { nullptr, nullptr }
  562. };
  563. // Read shader source file
  564. Array<uint8> data = ReadData(inFileName);
  565. // Compile source
  566. ComPtr<ID3DBlob> shader_blob, error_blob;
  567. HRESULT hr = D3DCompile(&data[0],
  568. (uint)data.size(),
  569. inFileName,
  570. defines,
  571. D3D_COMPILE_STANDARD_FILE_INCLUDE,
  572. "main",
  573. "ps_5_0",
  574. flags,
  575. 0,
  576. shader_blob.GetAddressOf(),
  577. error_blob.GetAddressOf());
  578. if (FAILED(hr))
  579. {
  580. // Throw error if compilation failed
  581. if (error_blob)
  582. OutputDebugStringA((const char *)error_blob->GetBufferPointer());
  583. FatalError("Failed to compile pixel shader");
  584. }
  585. return shader_blob;
  586. }
  587. unique_ptr<ConstantBuffer> Renderer::CreateConstantBuffer(uint inBufferSize)
  588. {
  589. return make_unique<ConstantBuffer>(this, inBufferSize);
  590. }
  591. 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)
  592. {
  593. return make_unique<PipelineState>(this, inVertexShader, inInputDescription, inInputDescriptionCount, inPixelShader, inFillMode, inTopology, inDepthTest, inBlendMode, inCullMode);
  594. }
  595. ComPtr<ID3D12Resource> Renderer::CreateD3DResource(D3D12_HEAP_TYPE inHeapType, D3D12_RESOURCE_STATES inResourceState, uint64 inSize)
  596. {
  597. // Create a new resource
  598. D3D12_RESOURCE_DESC desc;
  599. desc.Dimension = D3D12_RESOURCE_DIMENSION_BUFFER;
  600. desc.Alignment = 0;
  601. desc.Width = inSize;
  602. desc.Height = 1;
  603. desc.DepthOrArraySize = 1;
  604. desc.MipLevels = 1;
  605. desc.Format = DXGI_FORMAT_UNKNOWN;
  606. desc.SampleDesc.Count = 1;
  607. desc.SampleDesc.Quality = 0;
  608. desc.Layout = D3D12_TEXTURE_LAYOUT_ROW_MAJOR;
  609. desc.Flags = D3D12_RESOURCE_FLAG_NONE;
  610. D3D12_HEAP_PROPERTIES heap_properties = {};
  611. heap_properties.Type = inHeapType;
  612. heap_properties.CPUPageProperty = D3D12_CPU_PAGE_PROPERTY_UNKNOWN;
  613. heap_properties.MemoryPoolPreference = D3D12_MEMORY_POOL_UNKNOWN;
  614. heap_properties.CreationNodeMask = 1;
  615. heap_properties.VisibleNodeMask = 1;
  616. ComPtr<ID3D12Resource> resource;
  617. FatalErrorIfFailed(mDevice->CreateCommittedResource(&heap_properties, D3D12_HEAP_FLAG_NONE, &desc, inResourceState, nullptr, IID_PPV_ARGS(&resource)));
  618. return resource;
  619. }
  620. void Renderer::CopyD3DResource(ID3D12Resource *inDest, const void *inSrc, uint64 inSize)
  621. {
  622. // Copy data to destination buffer
  623. void *data;
  624. D3D12_RANGE range = { 0, 0 }; // We're not going to read
  625. FatalErrorIfFailed(inDest->Map(0, &range, &data));
  626. memcpy(data, inSrc, size_t(inSize));
  627. inDest->Unmap(0, nullptr);
  628. }
  629. void Renderer::CopyD3DResource(ID3D12Resource *inDest, ID3D12Resource *inSrc, uint64 inSize)
  630. {
  631. // Start a commandlist for the upload
  632. ID3D12GraphicsCommandList *list = mUploadQueue.Start();
  633. // Copy the data to the GPU
  634. list->CopyBufferRegion(inDest, 0, inSrc, 0, inSize);
  635. // Change the state of the resource to generic read
  636. D3D12_RESOURCE_BARRIER barrier;
  637. barrier.Type = D3D12_RESOURCE_BARRIER_TYPE_TRANSITION;
  638. barrier.Flags = D3D12_RESOURCE_BARRIER_FLAG_NONE;
  639. barrier.Transition.pResource = inDest;
  640. barrier.Transition.StateBefore = D3D12_RESOURCE_STATE_COPY_DEST;
  641. barrier.Transition.StateAfter = D3D12_RESOURCE_STATE_GENERIC_READ;
  642. barrier.Transition.Subresource = D3D12_RESOURCE_BARRIER_ALL_SUBRESOURCES;
  643. list->ResourceBarrier(1, &barrier);
  644. // Wait for copying to finish
  645. mUploadQueue.ExecuteAndWait();
  646. }
  647. ComPtr<ID3D12Resource> Renderer::CreateD3DResourceOnDefaultHeap(const void *inData, uint64 inSize)
  648. {
  649. ComPtr<ID3D12Resource> upload = CreateD3DResourceOnUploadHeap(inSize);
  650. ComPtr<ID3D12Resource> resource = CreateD3DResource(D3D12_HEAP_TYPE_DEFAULT, D3D12_RESOURCE_STATE_COMMON, inSize);
  651. CopyD3DResource(upload.Get(), inData, inSize);
  652. CopyD3DResource(resource.Get(), upload.Get(), inSize);
  653. RecycleD3DResourceOnUploadHeap(upload.Get(), inSize);
  654. return resource;
  655. }
  656. ComPtr<ID3D12Resource> Renderer::CreateD3DResourceOnUploadHeap(uint64 inSize)
  657. {
  658. // Try cache first
  659. ResourceCache::iterator i = mResourceCache.find(inSize);
  660. if (i != mResourceCache.end() && !i->second.empty())
  661. {
  662. ComPtr<ID3D12Resource> resource = i->second.back();
  663. i->second.pop_back();
  664. return resource;
  665. }
  666. return CreateD3DResource(D3D12_HEAP_TYPE_UPLOAD, D3D12_RESOURCE_STATE_GENERIC_READ, inSize);
  667. }
  668. void Renderer::RecycleD3DResourceOnUploadHeap(ID3D12Resource *inResource, uint64 inSize)
  669. {
  670. if (!mIsExiting)
  671. mDelayCached[mFrameIndex][inSize].push_back(inResource);
  672. }
  673. void Renderer::RecycleD3DObject(ID3D12Object *inResource)
  674. {
  675. if (!mIsExiting)
  676. mDelayReleased[mFrameIndex].push_back(inResource);
  677. }