| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
#pragma once |
| 5 |
|
| 6 |
#include <d3d12.h> |
| 7 |
|
| 8 |
#include "VideoBackends/D3D12/DescriptorAllocator.h" |
| 9 |
#include "VideoBackends/D3D12/DescriptorHeapManager.h" |
| 10 |
#include "VideoCommon/AbstractGfx.h" |
| 11 |
#include "VideoCommon/Constants.h" |
| 12 |
|
| 13 |
namespace DX12 |
| 14 |
{ |
| 15 |
class DXFramebuffer; |
| 16 |
class DXTexture; |
| 17 |
class DXShader; |
| 18 |
class DXPipeline; |
| 19 |
class SwapChain; |
| 20 |
|
| 21 |
class Gfx final : public ::AbstractGfx |
| 22 |
{ |
| 23 |
public: |
| 24 |
Gfx(std::unique_ptr<SwapChain> swap_chain, float backbuffer_scale); |
| 25 |
~Gfx() override; |
| 26 |
|
| 27 |
static Gfx* GetInstance() { return static_cast<Gfx*>(g_gfx.get()); } |
| 28 |
|
| 29 |
bool IsHeadless() const override; |
| 30 |
|
| 31 |
std::unique_ptr<AbstractTexture> CreateTexture(const TextureConfig& config, |
| 32 |
std::string_view name) override; |
| 33 |
std::unique_ptr<AbstractStagingTexture> |
| 34 |
CreateStagingTexture(StagingTextureType type, const TextureConfig& config) override; |
| 35 |
std::unique_ptr<AbstractFramebuffer> |
| 36 |
CreateFramebuffer(AbstractTexture* color_attachment, AbstractTexture* depth_attachment, |
| 37 |
std::vector<AbstractTexture*> additional_color_attachments) override; |
| 38 |
|
| 39 |
std::unique_ptr<AbstractShader> |
| 40 |
CreateShaderFromSource(ShaderStage stage, std::string_view source, |
| 41 |
VideoCommon::ShaderIncluder* shader_includer, |
| 42 |
std::string_view name) override; |
| 43 |
std::unique_ptr<AbstractShader> CreateShaderFromBinary(ShaderStage stage, const void* data, |
| 44 |
size_t length, |
| 45 |
std::string_view name) override; |
| 46 |
std::unique_ptr<NativeVertexFormat> |
| 47 |
CreateNativeVertexFormat(const PortableVertexDeclaration& vtx_decl) override; |
| 48 |
std::unique_ptr<AbstractPipeline> CreatePipeline(const AbstractPipelineConfig& config, |
| 49 |
const void* cache_data = nullptr, |
| 50 |
size_t cache_data_length = 0) override; |
| 51 |
|
| 52 |
void Flush() override; |
| 53 |
void WaitForGPUIdle() override; |
| 54 |
|
| 55 |
void ClearRegion(const MathUtil::Rectangle<int>& target_rc, bool color_enable, bool alpha_enable, |
| 56 |
bool z_enable, u32 color, u32 z) override; |
| 57 |
|
| 58 |
void SetPipeline(const AbstractPipeline* pipeline) override; |
| 59 |
void SetFramebuffer(AbstractFramebuffer* framebuffer) override; |
| 60 |
void SetAndDiscardFramebuffer(AbstractFramebuffer* framebuffer) override; |
| 61 |
void SetAndClearFramebuffer(AbstractFramebuffer* framebuffer, const ClearColor& color_value = {}, |
| 62 |
float depth_value = 0.0f) override; |
| 63 |
void SetScissorRect(const MathUtil::Rectangle<int>& rc) override; |
| 64 |
void SetTexture(u32 index, const AbstractTexture* texture) override; |
| 65 |
void SetSamplerState(u32 index, const SamplerState& state) override; |
| 66 |
void SetComputeImageTexture(u32 index, AbstractTexture* texture, bool read, bool write) override; |
| 67 |
void UnbindTexture(const AbstractTexture* texture) override; |
| 68 |
void SetViewport(float x, float y, float width, float height, float near_depth, |
| 69 |
float far_depth) override; |
| 70 |
void Draw(u32 base_vertex, u32 num_vertices) override; |
| 71 |
void DrawIndexed(u32 base_index, u32 num_indices, u32 base_vertex) override; |
| 72 |
void DispatchComputeShader(const AbstractShader* shader, u32 groupsize_x, u32 groupsize_y, |
| 73 |
u32 groupsize_z, u32 groups_x, u32 groups_y, u32 groups_z) override; |
| 74 |
bool BindBackbuffer(const ClearColor& clear_color = {}) override; |
| 75 |
void PresentBackbuffer() override; |
| 76 |
|
| 77 |
SurfaceInfo GetSurfaceInfo() const override; |
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
void ExecuteCommandList(bool wait_for_completion); |
| 82 |
|
| 83 |
|
| 84 |
void SetConstantBuffer(u32 index, D3D12_GPU_VIRTUAL_ADDRESS address); |
| 85 |
|
| 86 |
|
| 87 |
void SetTextureDescriptor(u32 index, D3D12_CPU_DESCRIPTOR_HANDLE handle); |
| 88 |
|
| 89 |
|
| 90 |
void SetPixelShaderUAV(D3D12_CPU_DESCRIPTOR_HANDLE handle); |
| 91 |
|
| 92 |
|
| 93 |
void SetVertexBuffer(D3D12_GPU_VIRTUAL_ADDRESS address, D3D12_CPU_DESCRIPTOR_HANDLE srv, |
| 94 |
u32 stride, u32 size); |
| 95 |
void SetIndexBuffer(D3D12_GPU_VIRTUAL_ADDRESS address, u32 size, DXGI_FORMAT format); |
| 96 |
|
| 97 |
|
| 98 |
bool ApplyState(); |
| 99 |
|
| 100 |
protected: |
| 101 |
void OnConfigChanged(u32 bits) override; |
| 102 |
|
| 103 |
private: |
| 104 |
|
| 105 |
enum DirtyStates |
| 106 |
{ |
| 107 |
DirtyState_Framebuffer = (1 << 0), |
| 108 |
DirtyState_Pipeline = (1 << 1), |
| 109 |
DirtyState_Textures = (1 << 2), |
| 110 |
DirtyState_Samplers = (1 << 3), |
| 111 |
DirtyState_Viewport = (1 << 4), |
| 112 |
DirtyState_ScissorRect = (1 << 5), |
| 113 |
DirtyState_ComputeImageTexture = (1 << 6), |
| 114 |
DirtyState_PS_UAV = (1 << 7), |
| 115 |
DirtyState_PS_CBV = (1 << 8), |
| 116 |
DirtyState_VS_CBV = (1 << 9), |
| 117 |
DirtyState_CUS_CBV = (1 << 10), |
| 118 |
DirtyState_GS_CBV = (1 << 11), |
| 119 |
DirtyState_SRV_Descriptor = (1 << 12), |
| 120 |
DirtyState_Sampler_Descriptor = (1 << 13), |
| 121 |
DirtyState_UAV_Descriptor = (1 << 14), |
| 122 |
DirtyState_VertexBuffer = (1 << 15), |
| 123 |
DirtyState_IndexBuffer = (1 << 16), |
| 124 |
DirtyState_PrimitiveTopology = (1 << 17), |
| 125 |
DirtyState_RootSignature = (1 << 18), |
| 126 |
DirtyState_ComputeRootSignature = (1 << 19), |
| 127 |
DirtyState_DescriptorHeaps = (1 << 20), |
| 128 |
DirtyState_VS_SRV = (1 << 21), |
| 129 |
DirtyState_VS_SRV_Descriptor = (1 << 22), |
| 130 |
|
| 131 |
DirtyState_All = |
| 132 |
DirtyState_Framebuffer | DirtyState_Pipeline | DirtyState_Textures | DirtyState_Samplers | |
| 133 |
DirtyState_Viewport | DirtyState_ScissorRect | DirtyState_ComputeImageTexture | |
| 134 |
DirtyState_PS_UAV | DirtyState_PS_CBV | DirtyState_VS_CBV | DirtyState_CUS_CBV | |
| 135 |
DirtyState_GS_CBV | DirtyState_SRV_Descriptor | DirtyState_Sampler_Descriptor | |
| 136 |
DirtyState_UAV_Descriptor | DirtyState_VertexBuffer | DirtyState_IndexBuffer | |
| 137 |
DirtyState_PrimitiveTopology | DirtyState_RootSignature | DirtyState_ComputeRootSignature | |
| 138 |
DirtyState_DescriptorHeaps | DirtyState_VS_SRV | DirtyState_VS_SRV_Descriptor |
| 139 |
}; |
| 140 |
|
| 141 |
void CheckForSwapChainChanges(); |
| 142 |
|
| 143 |
void BindFramebuffer(DXFramebuffer* fb); |
| 144 |
void SetRootSignatures(); |
| 145 |
void SetDescriptorHeaps(); |
| 146 |
void UpdateDescriptorTables(); |
| 147 |
bool UpdateSRVDescriptorTable(); |
| 148 |
bool UpdateUAVDescriptorTable(); |
| 149 |
bool UpdateVSSRVDescriptorTable(); |
| 150 |
bool UpdateComputeUAVDescriptorTable(); |
| 151 |
bool UpdateSamplerDescriptorTable(); |
| 152 |
|
| 153 |
float m_backbuffer_scale; |
| 154 |
|
| 155 |
|
| 156 |
std::unique_ptr<SwapChain> m_swap_chain; |
| 157 |
|
| 158 |
|
| 159 |
struct |
| 160 |
{ |
| 161 |
ID3D12RootSignature* root_signature = nullptr; |
| 162 |
DXShader* compute_shader = nullptr; |
| 163 |
std::array<D3D12_GPU_VIRTUAL_ADDRESS, 4> constant_buffers = {}; |
| 164 |
std::array<D3D12_CPU_DESCRIPTOR_HANDLE, VideoCommon::MAX_PIXEL_SHADER_SAMPLERS> textures = {}; |
| 165 |
D3D12_CPU_DESCRIPTOR_HANDLE vs_srv = {}; |
| 166 |
D3D12_CPU_DESCRIPTOR_HANDLE ps_uav = {}; |
| 167 |
SamplerStateSet samplers = {}; |
| 168 |
const DXTexture* compute_image_texture = nullptr; |
| 169 |
D3D12_VIEWPORT viewport = {}; |
| 170 |
D3D12_RECT scissor = {}; |
| 171 |
D3D12_GPU_DESCRIPTOR_HANDLE vertex_srv_descriptor_base = {}; |
| 172 |
D3D12_GPU_DESCRIPTOR_HANDLE srv_descriptor_base = {}; |
| 173 |
D3D12_GPU_DESCRIPTOR_HANDLE sampler_descriptor_base = {}; |
| 174 |
D3D12_GPU_DESCRIPTOR_HANDLE uav_descriptor_base = {}; |
| 175 |
D3D12_GPU_DESCRIPTOR_HANDLE compute_uav_descriptor_base = {}; |
| 176 |
D3D12_VERTEX_BUFFER_VIEW vertex_buffer = {}; |
| 177 |
D3D12_INDEX_BUFFER_VIEW index_buffer = {}; |
| 178 |
D3D12_PRIMITIVE_TOPOLOGY primitive_topology = D3D_PRIMITIVE_TOPOLOGY_TRIANGLELIST; |
| 179 |
bool using_integer_rtv = false; |
| 180 |
} m_state; |
| 181 |
u32 m_dirty_bits = DirtyState_All; |
| 182 |
}; |
| 183 |
} |