2
0

Renderer.cpp 28 KB

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