BRE12
RenderManager.h
1 #pragma once
2 
3 #include <d3d12.h>
4 #include <dxgi1_4.h>
5 #include <tbb/task.h>
6 
7 #include <CommandManager\CommandListPerFrame.h>
8 #include <Camera/Camera.h>
9 #include <GeometryPass\GeometryPass.h>
10 #include <LightingPass\LightingPass.h>
11 #include <PostProcesspass\PostProcesspass.h>
12 #include <SettingsManager\SettingsManager.h>
13 #include <SkyBoxPass\SkyBoxPass.h>
14 #include <ShaderUtils\CBuffers.h>
15 #include <ToneMappingPass\ToneMappingPass.h>
16 #include <Timer/Timer.h>
17 
18 namespace BRE {
19 class CommandListExecutor;
20 class Scene;
21 
22 // Initializes passes (geometry, light, skybox, etc) based on a Scene.
23 // Steps:
24 // - Use RenderManager::Create() to create and spawn and instance.
25 // - When you want to terminate this task, you should call RenderManager::Terminate()
26 class RenderManager : public tbb::task {
27 public:
28  // Preconditions:
29  // - Create() must be called once
30  static RenderManager& Create(Scene& scene) noexcept;
31 
32  ~RenderManager() = default;
33  RenderManager(const RenderManager&) = delete;
34  const RenderManager& operator=(const RenderManager&) = delete;
35  RenderManager(RenderManager&&) = delete;
36  RenderManager& operator=(RenderManager&&) = delete;
37 
38  void Terminate() noexcept;
39 
40 private:
41  explicit RenderManager(Scene& scene);
42 
43  // Called when tbb::task is spawned
44  tbb::task* execute() final override;
45 
46  static RenderManager* sRenderManager;
47 
48  void InitPasses(Scene& scene) noexcept;
49 
50  void CreateFrameBuffersAndRenderTargetViews() noexcept;
51 
52  void CreateDepthStencilBufferAndView() noexcept;
53 
54  void CreateIntermediateColorBufferAndRenderTargetView(const D3D12_RESOURCE_STATES initialState,
55  const wchar_t* resourceName,
56  Microsoft::WRL::ComPtr<ID3D12Resource>& buffer,
57  D3D12_CPU_DESCRIPTOR_HANDLE& renderTargetView) noexcept;
58 
59  ID3D12Resource* CurrentFrameBuffer() const noexcept
60  {
61  BRE_ASSERT(mSwapChain != nullptr);
62  return mFrameBuffers[mSwapChain->GetCurrentBackBufferIndex()].Get();
63  }
64 
65  D3D12_CPU_DESCRIPTOR_HANDLE CurrentFrameBufferCpuDesc() const noexcept
66  {
67  return mFrameBufferRenderTargetViews[mSwapChain->GetCurrentBackBufferIndex()];
68  }
69 
70  D3D12_CPU_DESCRIPTOR_HANDLE DepthStencilCpuDesc() const noexcept
71  {
72  return mDepthBufferRenderTargetView;
73  }
74 
75  void ExecuteFinalPass();
76 
77  void FlushCommandQueue() noexcept;
78  void SignalFenceAndPresent() noexcept;
79 
80  Microsoft::WRL::ComPtr<IDXGISwapChain3> mSwapChain{ nullptr };
81 
82  // Fences data for synchronization purposes.
83  ID3D12Fence* mFence{ nullptr };
84  std::uint32_t mCurrentQueuedFrameIndex{ 0U };
85  std::uint64_t mFenceValueByQueuedFrameIndex[SettingsManager::sQueuedFrameCount]{ 0UL };
86  std::uint64_t mCurrentFenceValue{ 0UL };
87 
88  // Passes
89  GeometryPass mGeometryPass;
90  LightingPass mLightingPass;
91  SkyBoxPass mSkyBoxPass;
92  ToneMappingPass mToneMappingPass;
93  PostProcessPass mPostProcessPass;
94 
95  CommandListPerFrame mFinalCommandListPerFrame;
96 
97  Microsoft::WRL::ComPtr<ID3D12Resource> mFrameBuffers[SettingsManager::sSwapChainBufferCount];
98  D3D12_CPU_DESCRIPTOR_HANDLE mFrameBufferRenderTargetViews[SettingsManager::sSwapChainBufferCount]{ 0UL };
99 
100  ID3D12Resource* mDepthBuffer{ nullptr };
101  D3D12_CPU_DESCRIPTOR_HANDLE mDepthBufferRenderTargetView{ 0UL };
102 
103  // Buffers used for intermediate computations.
104  // They are used as render targets (light pass) or pixel shader resources (post processing passes)
105  Microsoft::WRL::ComPtr<ID3D12Resource> mIntermediateColorBuffer1;
106  D3D12_CPU_DESCRIPTOR_HANDLE mIntermediateColorBuffer1RenderTargetView;
107  Microsoft::WRL::ComPtr<ID3D12Resource> mIntermediateColorBuffer2;
108  D3D12_CPU_DESCRIPTOR_HANDLE mIntermediateColorBuffer2RenderTargetView;
109 
110  // We cache it here, as is is used by most passes.
111  FrameCBuffer mFrameCBuffer;
112 
113  Camera mCamera;
114  Timer mTimer;
115 
116  // When it is true, master render thread is destroyed.
117  bool mTerminate{ false };
118 };
119 
120 }
121 
Definition: Camera.cpp:8
Timer class used mainly to get frame time.
Definition: Timer.h:9
Definition: GeometryPass.h:16
Definition: LightingPass.h:18
Definition: SkyBoxPass.h:14
Definition: PostProcessPass.h:13
Definition: ToneMappingPass.h:12
Definition: CBuffers.h:23
Definition: Camera.h:9
Definition: RenderManager.h:26
Definition: CommandListPerFrame.h:16
Definition: Scene.h:9