// Copyright (c) 2012-2023 Wojciech Figat. All rights reserved.
#pragma once
#include "Engine/Graphics/GPUDevice.h"
#include "Engine/Graphics/GPUResource.h"
#include "../GPUDeviceDX.h"
#include "../IncludeDirectXHeaders.h"
#if GRAPHICS_API_DIRECTX11
class Engine;
class GPUContextDX11;
class GPUSwapChainDX11;
///
/// Implementation of Graphics Device for DirectX 11 backend.
///
class GPUDeviceDX11 : public GPUDeviceDX
{
friend GPUContextDX11;
friend GPUSwapChainDX11;
private:
// Private Stuff
ID3D11Device* _device;
ID3D11DeviceContext* _imContext;
IDXGIFactory* _factoryDXGI;
GPUContextDX11* _mainContext;
bool _allowTearing = false;
// Static Samplers
ID3D11SamplerState* _samplerLinearClamp;
ID3D11SamplerState* _samplerPointClamp;
ID3D11SamplerState* _samplerLinearWrap;
ID3D11SamplerState* _samplerPointWrap;
ID3D11SamplerState* _samplerShadow;
ID3D11SamplerState* _samplerShadowPCF;
// Shared data for pipeline states
CriticalSection BlendStatesWriteLocker;
Dictionary BlendStates;
ID3D11RasterizerState* RasterizerStates[3 * 2 * 2]; // Index = CullMode[0-2] + Wireframe[0?3] + DepthClipEnable[0?6]
ID3D11DepthStencilState* DepthStencilStates[9 * 2 * 2]; // Index = ComparisonFunc[0-8] + DepthTestEnable[0?9] + DepthWriteEnable[0?18]
public:
static GPUDevice* Create();
GPUDeviceDX11(IDXGIFactory* dxgiFactory, GPUAdapterDX* adapter);
~GPUDeviceDX11();
public:
// Gets DX11 device
ID3D11Device* GetDevice() const
{
return _device;
}
// Gets DXGI factory
IDXGIFactory* GetDXGIFactory() const
{
return _factoryDXGI;
}
// Gets immediate context
ID3D11DeviceContext* GetIM() const
{
return _imContext;
}
GPUContextDX11* GetMainContextDX11()
{
return _mainContext;
}
ID3D11BlendState* GetBlendState(const BlendingMode& blending);
public:
// [GPUDeviceDX]
GPUContext* GetMainContext() override
{
return reinterpret_cast(_mainContext);
}
void* GetNativePtr() const override
{
return _device;
}
bool Init() override;
void Dispose() override;
void WaitForGPU() override;
void DrawEnd() override;
GPUTexture* CreateTexture(const StringView& name) override;
GPUShader* CreateShader(const StringView& name) override;
GPUPipelineState* CreatePipelineState() override;
GPUTimerQuery* CreateTimerQuery() override;
GPUBuffer* CreateBuffer(const StringView& name) override;
GPUSampler* CreateSampler() override;
GPUSwapChain* CreateSwapChain(Window* window) override;
GPUConstantBuffer* CreateConstantBuffer(uint32 size, const StringView& name) override;
};
///
/// Base interface for GPU resources on DirectX 11
///
class IGPUResourceDX11
{
public:
///
/// Gets DirectX 11 resource object handle.
///
virtual ID3D11Resource* GetResource() = 0;
};
///
/// GPU resource implementation for DirectX 11 backend.
///
template
class GPUResourceDX11 : public GPUResourceBase, public IGPUResourceDX11
{
public:
///
/// Initializes a new instance of the class.
///
/// The graphics device.
/// The resource name.
GPUResourceDX11(GPUDeviceDX11* device, const StringView& name) noexcept
: GPUResourceBase(device, name)
{
}
};
extern GPUDevice* CreateGPUDeviceDX11();
#endif