// Jolt Physics Library (https://github.com/jrouwe/JoltPhysics) // SPDX-FileCopyrightText: 2024 Jorrit Rouwe // SPDX-License-Identifier: MIT #pragma once #include #include #include #include #include #include /// DirectX 12 renderer class RendererDX12 : public Renderer { public: /// Destructor virtual ~RendererDX12() override; // See: Renderer virtual void Initialize(ApplicationWindow *inWindow) override; virtual void BeginFrame(const CameraState &inCamera, float inWorldScale) override; virtual void EndShadowPass() override; virtual void EndFrame() override; virtual void SetProjectionMode() override; virtual void SetOrthoMode() override; virtual Ref CreateTexture(const Surface *inSurface) override; virtual Ref CreateVertexShader(const char *inName) override; virtual Ref CreatePixelShader(const char *inName) override; virtual unique_ptr CreatePipelineState(const VertexShader *inVertexShader, const PipelineState::EInputDescription *inInputDescription, uint inInputDescriptionCount, const PixelShader *inPixelShader, PipelineState::EDrawPass inDrawPass, PipelineState::EFillMode inFillMode, PipelineState::ETopology inTopology, PipelineState::EDepthTest inDepthTest, PipelineState::EBlendMode inBlendMode, PipelineState::ECullMode inCullMode) override; virtual RenderPrimitive * CreateRenderPrimitive(PipelineState::ETopology inType) override; virtual RenderInstances * CreateRenderInstances() override; virtual Texture * GetShadowMap() const override { return mShadowMap.GetPtr(); } virtual void OnWindowResize() override; /// Create a constant buffer unique_ptr CreateConstantBuffer(uint inBufferSize); /// Create a buffer on the default heap (usable for permanent buffers) ComPtr CreateD3DResourceOnDefaultHeap(const void *inData, uint64 inSize); /// Create buffer on the upload heap (usable for temporary buffers). ComPtr CreateD3DResourceOnUploadHeap(uint64 inSize); /// Recycle a buffer on the upload heap. This puts it back in a cache and will reuse it when it is certain the GPU is no longer referencing it. void RecycleD3DResourceOnUploadHeap(ID3D12Resource *inResource, uint64 inSize); /// Keeps a reference to the resource until the current frame has finished void RecycleD3DObject(ID3D12Object *inResource); /// Access to the most important DirectX structures ID3D12Device * GetDevice() { return mDevice.Get(); } ID3D12RootSignature * GetRootSignature() { return mRootSignature.Get(); } ID3D12GraphicsCommandList * GetCommandList() { JPH_ASSERT(mInFrame); return mCommandList.Get(); } CommandQueueDX12 & GetUploadQueue() { return mUploadQueue; } DescriptorHeapDX12 & GetDSVHeap() { return mDSVHeap; } DescriptorHeapDX12 & GetSRVHeap() { return mSRVHeap; } private: // Wait for pending GPU work to complete void WaitForGpu(); // Create render targets and their views void CreateRenderTargets(); // Create a depth buffer for the back buffer void CreateDepthBuffer(); // Function to create a ID3D12Resource on specified heap with specified state ComPtr CreateD3DResource(D3D12_HEAP_TYPE inHeapType, D3D12_RESOURCE_STATES inResourceState, uint64 inSize); // Copy CPU memory into a ID3D12Resource void CopyD3DResource(ID3D12Resource *inDest, const void *inSrc, uint64 inSize); // Copy a CPU resource to a GPU resource void CopyD3DResource(ID3D12Resource *inDest, ID3D12Resource *inSrc, uint64 inSize); // DirectX interfaces ComPtr mDXGIFactory; ComPtr mDevice; DescriptorHeapDX12 mRTVHeap; ///< Render target view heap DescriptorHeapDX12 mDSVHeap; ///< Depth stencil view heap DescriptorHeapDX12 mSRVHeap; ///< Shader resource view heap ComPtr mSwapChain; ComPtr mRenderTargets[cFrameCount]; ///< Two render targets (we're double buffering in order for the CPU to continue while the GPU is rendering) D3D12_CPU_DESCRIPTOR_HANDLE mRenderTargetViews[cFrameCount]; ///< The two render views corresponding to the render targets ComPtr mDepthStencilBuffer; ///< The main depth buffer D3D12_CPU_DESCRIPTOR_HANDLE mDepthStencilView { 0 }; ///< A view for binding the depth buffer ComPtr mCommandAllocators[cFrameCount]; ///< Two command allocator lists (one per frame) ComPtr mCommandQueue; ///< The command queue that will execute commands (there's only 1 since we want to finish rendering 1 frame before moving onto the next) ComPtr mCommandList; ///< The command list ComPtr mRootSignature; ///< The root signature, we have a simple application so we only need 1, which is suitable for all our shaders Ref mShadowMap; ///< Used to render shadow maps CommandQueueDX12 mUploadQueue; ///< Queue used to upload resources to GPU memory unique_ptr mVertexShaderConstantBufferProjection[cFrameCount]; unique_ptr mVertexShaderConstantBufferOrtho[cFrameCount]; unique_ptr mPixelShaderConstantBuffer[cFrameCount]; // Synchronization objects used to finish rendering and swapping before reusing a command queue HANDLE mFenceEvent; ///< Fence event to wait for the previous frame rendering to complete (in order to free 1 of the buffers) ComPtr mFence; ///< Fence object, used to signal the end of a frame UINT64 mFenceValues[cFrameCount] = {}; ///< Values that were used to signal completion of one of the two frames using ResourceCache = UnorderedMap>>; ResourceCache mResourceCache; ///< Cache items ready to be reused ResourceCache mDelayCached[cFrameCount]; ///< List of reusable ID3D12Resources that are potentially referenced by the GPU so can be used only when the GPU finishes Array> mDelayReleased[cFrameCount]; ///< Objects that are potentially referenced by the GPU so can only be freed when the GPU finishes bool mIsExiting = false; ///< When exiting we don't want to add references too buffers };