2
0

Renderer.cpp 28 KB

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