2015-05-23 22:55:12 -06:00
|
|
|
// Copyright 2014 Dolphin Emulator Project
|
2021-07-04 19:22:19 -06:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
2014-06-08 16:36:26 -06:00
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2017-07-19 23:25:31 -06:00
|
|
|
#include <array>
|
2023-02-10 18:10:55 -07:00
|
|
|
#include <bitset>
|
2017-04-09 14:15:57 -06:00
|
|
|
#include <cstddef>
|
2019-03-29 03:55:00 -06:00
|
|
|
#include <memory>
|
2018-02-24 07:21:04 -07:00
|
|
|
#include <mutex>
|
2014-06-08 16:36:26 -06:00
|
|
|
#include <unordered_map>
|
|
|
|
|
2014-06-09 03:55:25 -06:00
|
|
|
#include "Common/BitField.h"
|
2014-06-08 16:36:26 -06:00
|
|
|
#include "Common/CommonTypes.h"
|
|
|
|
#include "VideoBackends/D3D/D3DBase.h"
|
2023-02-09 23:28:06 -07:00
|
|
|
#include "VideoCommon/Constants.h"
|
2017-04-29 08:54:22 -06:00
|
|
|
#include "VideoCommon/RenderState.h"
|
2014-06-08 16:36:26 -06:00
|
|
|
|
|
|
|
namespace DX11
|
|
|
|
{
|
2019-02-14 18:59:50 -07:00
|
|
|
class DXFramebuffer;
|
|
|
|
|
2014-06-08 16:36:26 -06:00
|
|
|
class StateCache
|
|
|
|
{
|
|
|
|
public:
|
2017-09-30 00:19:16 -06:00
|
|
|
~StateCache();
|
|
|
|
|
2014-06-08 16:36:26 -06:00
|
|
|
// Get existing or create new render state.
|
|
|
|
// Returned objects is owned by the cache and does not need to be released.
|
|
|
|
ID3D11SamplerState* Get(SamplerState state);
|
2017-04-29 08:54:22 -06:00
|
|
|
ID3D11BlendState* Get(BlendingState state);
|
2017-04-30 02:07:57 -06:00
|
|
|
ID3D11RasterizerState* Get(RasterizationState state);
|
2017-04-29 23:54:45 -06:00
|
|
|
ID3D11DepthStencilState* Get(DepthState state);
|
2014-06-08 16:36:26 -06:00
|
|
|
|
2017-04-30 02:07:57 -06:00
|
|
|
// Convert RasterState primitive type to D3D11 primitive topology.
|
|
|
|
static D3D11_PRIMITIVE_TOPOLOGY GetPrimitiveTopology(PrimitiveType primitive);
|
|
|
|
|
2014-06-08 16:36:26 -06:00
|
|
|
private:
|
2019-03-09 06:31:37 -07:00
|
|
|
std::unordered_map<u32, ComPtr<ID3D11DepthStencilState>> m_depth;
|
|
|
|
std::unordered_map<u32, ComPtr<ID3D11RasterizerState>> m_raster;
|
|
|
|
std::unordered_map<u32, ComPtr<ID3D11BlendState>> m_blend;
|
2021-08-08 22:11:50 -06:00
|
|
|
std::unordered_map<SamplerState, ComPtr<ID3D11SamplerState>> m_sampler;
|
2018-02-24 07:21:04 -07:00
|
|
|
std::mutex m_lock;
|
2014-06-08 16:36:26 -06:00
|
|
|
};
|
|
|
|
|
2014-06-23 00:11:07 -06:00
|
|
|
namespace D3D
|
|
|
|
{
|
2018-02-22 02:40:09 -07:00
|
|
|
class StateManager
|
2014-06-23 00:11:07 -06:00
|
|
|
{
|
|
|
|
public:
|
2018-02-22 02:40:09 -07:00
|
|
|
StateManager();
|
|
|
|
~StateManager();
|
2014-06-23 00:11:07 -06:00
|
|
|
|
2018-02-22 02:40:09 -07:00
|
|
|
void SetBlendState(ID3D11BlendState* state)
|
|
|
|
{
|
|
|
|
if (m_current.blendState != state)
|
2023-02-10 18:10:55 -07:00
|
|
|
m_dirtyFlags.set(DirtyFlag_BlendState);
|
2014-06-23 00:11:07 -06:00
|
|
|
|
2018-02-22 02:40:09 -07:00
|
|
|
m_pending.blendState = state;
|
|
|
|
}
|
2014-06-23 00:11:07 -06:00
|
|
|
|
2018-02-22 02:40:09 -07:00
|
|
|
void SetDepthState(ID3D11DepthStencilState* state)
|
|
|
|
{
|
|
|
|
if (m_current.depthState != state)
|
2023-02-10 18:10:55 -07:00
|
|
|
m_dirtyFlags.set(DirtyFlag_DepthState);
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2018-02-22 02:40:09 -07:00
|
|
|
m_pending.depthState = state;
|
|
|
|
}
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2018-02-22 02:40:09 -07:00
|
|
|
void SetRasterizerState(ID3D11RasterizerState* state)
|
|
|
|
{
|
|
|
|
if (m_current.rasterizerState != state)
|
2023-02-10 18:10:55 -07:00
|
|
|
m_dirtyFlags.set(DirtyFlag_RasterizerState);
|
2018-02-22 02:40:09 -07:00
|
|
|
|
|
|
|
m_pending.rasterizerState = state;
|
|
|
|
}
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2017-04-09 14:15:57 -06:00
|
|
|
void SetTexture(size_t index, ID3D11ShaderResourceView* texture)
|
2014-10-28 18:19:09 -06:00
|
|
|
{
|
|
|
|
if (m_current.textures[index] != texture)
|
2023-02-10 18:10:55 -07:00
|
|
|
m_dirtyFlags.set(DirtyFlag_Texture0 + index);
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2014-12-13 13:51:18 -07:00
|
|
|
m_pending.textures[index] = texture;
|
2014-10-28 18:19:09 -06:00
|
|
|
}
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2017-04-09 14:15:57 -06:00
|
|
|
void SetSampler(size_t index, ID3D11SamplerState* sampler)
|
2014-10-28 18:19:09 -06:00
|
|
|
{
|
|
|
|
if (m_current.samplers[index] != sampler)
|
2023-02-10 18:10:55 -07:00
|
|
|
m_dirtyFlags.set(DirtyFlag_Sampler0 + index);
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2014-12-13 13:51:18 -07:00
|
|
|
m_pending.samplers[index] = sampler;
|
2014-10-28 18:19:09 -06:00
|
|
|
}
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2023-09-19 18:29:38 -06:00
|
|
|
void SetPixelConstants(ID3D11Buffer* buffer0, ID3D11Buffer* buffer1 = nullptr,
|
|
|
|
ID3D11Buffer* buffer2 = nullptr)
|
2014-10-28 18:19:09 -06:00
|
|
|
{
|
2023-09-19 18:29:38 -06:00
|
|
|
if (m_current.pixelConstants[0] != buffer0 || m_current.pixelConstants[1] != buffer1 ||
|
|
|
|
m_current.pixelConstants[2] != buffer2)
|
2023-02-10 18:10:55 -07:00
|
|
|
m_dirtyFlags.set(DirtyFlag_PixelConstants);
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2014-12-13 13:51:18 -07:00
|
|
|
m_pending.pixelConstants[0] = buffer0;
|
|
|
|
m_pending.pixelConstants[1] = buffer1;
|
2023-09-19 18:29:38 -06:00
|
|
|
m_pending.pixelConstants[2] = buffer2;
|
2014-10-28 18:19:09 -06:00
|
|
|
}
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2014-12-06 06:54:06 -07:00
|
|
|
void SetVertexConstants(ID3D11Buffer* buffer)
|
2014-10-28 18:19:09 -06:00
|
|
|
{
|
|
|
|
if (m_current.vertexConstants != buffer)
|
2023-02-10 18:10:55 -07:00
|
|
|
m_dirtyFlags.set(DirtyFlag_VertexConstants);
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2014-12-13 13:51:18 -07:00
|
|
|
m_pending.vertexConstants = buffer;
|
2014-10-28 18:19:09 -06:00
|
|
|
}
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2014-12-06 06:54:06 -07:00
|
|
|
void SetGeometryConstants(ID3D11Buffer* buffer)
|
2014-10-28 18:19:09 -06:00
|
|
|
{
|
|
|
|
if (m_current.geometryConstants != buffer)
|
2023-02-10 18:10:55 -07:00
|
|
|
m_dirtyFlags.set(DirtyFlag_GeometryConstants);
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2014-12-13 13:51:18 -07:00
|
|
|
m_pending.geometryConstants = buffer;
|
2014-10-28 18:19:09 -06:00
|
|
|
}
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2014-12-06 06:54:06 -07:00
|
|
|
void SetVertexBuffer(ID3D11Buffer* buffer, u32 stride, u32 offset)
|
2014-10-28 18:19:09 -06:00
|
|
|
{
|
|
|
|
if (m_current.vertexBuffer != buffer || m_current.vertexBufferStride != stride ||
|
|
|
|
m_current.vertexBufferOffset != offset)
|
2023-02-10 18:10:55 -07:00
|
|
|
m_dirtyFlags.set(DirtyFlag_VertexBuffer);
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2014-12-13 13:51:18 -07:00
|
|
|
m_pending.vertexBuffer = buffer;
|
|
|
|
m_pending.vertexBufferStride = stride;
|
|
|
|
m_pending.vertexBufferOffset = offset;
|
2014-10-28 18:19:09 -06:00
|
|
|
}
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2014-12-06 06:54:06 -07:00
|
|
|
void SetIndexBuffer(ID3D11Buffer* buffer)
|
2014-10-28 18:19:09 -06:00
|
|
|
{
|
|
|
|
if (m_current.indexBuffer != buffer)
|
2023-02-10 18:10:55 -07:00
|
|
|
m_dirtyFlags.set(DirtyFlag_IndexBuffer);
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2014-12-13 13:51:18 -07:00
|
|
|
m_pending.indexBuffer = buffer;
|
2014-10-28 18:19:09 -06:00
|
|
|
}
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2014-12-06 06:54:06 -07:00
|
|
|
void SetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY topology)
|
2014-10-28 18:19:09 -06:00
|
|
|
{
|
|
|
|
if (m_current.topology != topology)
|
2023-02-10 18:10:55 -07:00
|
|
|
m_dirtyFlags.set(DirtyFlag_InputAssembler);
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2014-12-13 13:51:18 -07:00
|
|
|
m_pending.topology = topology;
|
2014-10-28 18:19:09 -06:00
|
|
|
}
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2014-12-06 06:54:06 -07:00
|
|
|
void SetInputLayout(ID3D11InputLayout* layout)
|
2014-10-28 18:19:09 -06:00
|
|
|
{
|
|
|
|
if (m_current.inputLayout != layout)
|
2023-02-10 18:10:55 -07:00
|
|
|
m_dirtyFlags.set(DirtyFlag_InputAssembler);
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2014-12-13 13:51:18 -07:00
|
|
|
m_pending.inputLayout = layout;
|
2014-10-28 18:19:09 -06:00
|
|
|
}
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2014-12-06 06:54:06 -07:00
|
|
|
void SetPixelShader(ID3D11PixelShader* shader)
|
2014-10-28 18:19:09 -06:00
|
|
|
{
|
|
|
|
if (m_current.pixelShader != shader)
|
2023-02-10 18:10:55 -07:00
|
|
|
m_dirtyFlags.set(DirtyFlag_PixelShader);
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2014-12-13 13:51:18 -07:00
|
|
|
m_pending.pixelShader = shader;
|
2014-10-28 18:19:09 -06:00
|
|
|
}
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2014-12-06 06:54:06 -07:00
|
|
|
void SetPixelShaderDynamic(ID3D11PixelShader* shader, ID3D11ClassInstance* const* classInstances,
|
|
|
|
u32 classInstancesCount)
|
2014-10-28 18:19:09 -06:00
|
|
|
{
|
|
|
|
D3D::context->PSSetShader(shader, classInstances, classInstancesCount);
|
2014-12-13 14:09:34 -07:00
|
|
|
m_current.pixelShader = shader;
|
2014-10-28 18:19:09 -06:00
|
|
|
m_pending.pixelShader = shader;
|
|
|
|
}
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2014-12-06 06:54:06 -07:00
|
|
|
void SetVertexShader(ID3D11VertexShader* shader)
|
2014-10-28 18:19:09 -06:00
|
|
|
{
|
|
|
|
if (m_current.vertexShader != shader)
|
2023-02-10 18:10:55 -07:00
|
|
|
m_dirtyFlags.set(DirtyFlag_VertexShader);
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2014-12-13 13:51:18 -07:00
|
|
|
m_pending.vertexShader = shader;
|
2014-10-28 18:19:09 -06:00
|
|
|
}
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2014-12-06 06:54:06 -07:00
|
|
|
void SetGeometryShader(ID3D11GeometryShader* shader)
|
2014-10-28 18:19:09 -06:00
|
|
|
{
|
|
|
|
if (m_current.geometryShader != shader)
|
2023-02-10 18:10:55 -07:00
|
|
|
m_dirtyFlags.set(DirtyFlag_GeometryShader);
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2014-12-13 13:51:18 -07:00
|
|
|
m_pending.geometryShader = shader;
|
2014-10-28 18:19:09 -06:00
|
|
|
}
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2019-02-14 18:59:50 -07:00
|
|
|
void SetFramebuffer(DXFramebuffer* fb)
|
2018-02-09 06:33:56 -07:00
|
|
|
{
|
2019-02-14 18:59:50 -07:00
|
|
|
if (m_current.framebuffer != fb)
|
2023-02-10 18:10:55 -07:00
|
|
|
m_dirtyFlags.set(DirtyFlag_Framebuffer);
|
2018-02-09 06:33:56 -07:00
|
|
|
|
2019-02-14 18:59:50 -07:00
|
|
|
m_pending.framebuffer = fb;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SetOMUAV(ID3D11UnorderedAccessView* uav)
|
|
|
|
{
|
|
|
|
if (m_current.uav != uav)
|
2023-02-10 18:10:55 -07:00
|
|
|
m_dirtyFlags.set(DirtyFlag_Framebuffer);
|
2019-02-14 18:59:50 -07:00
|
|
|
|
|
|
|
m_pending.uav = uav;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SetIntegerRTV(bool enable)
|
|
|
|
{
|
|
|
|
if (m_current.use_integer_rtv != enable)
|
2023-02-10 18:10:55 -07:00
|
|
|
m_dirtyFlags.set(DirtyFlag_Framebuffer);
|
2019-02-14 18:59:50 -07:00
|
|
|
|
|
|
|
m_pending.use_integer_rtv = enable;
|
2018-02-09 06:33:56 -07:00
|
|
|
}
|
|
|
|
|
2014-12-12 16:32:08 -07:00
|
|
|
// removes currently set texture from all slots, returns mask of previously bound slots
|
2014-12-17 12:31:08 -07:00
|
|
|
u32 UnsetTexture(ID3D11ShaderResourceView* srv);
|
|
|
|
void SetTextureByMask(u32 textureSlotMask, ID3D11ShaderResourceView* srv);
|
2019-02-14 18:59:50 -07:00
|
|
|
void ApplyTextures();
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2014-10-28 18:19:09 -06:00
|
|
|
// call this immediately before any drawing operation or to explicitly apply pending resource
|
|
|
|
// state changes
|
2014-06-23 00:11:07 -06:00
|
|
|
void Apply();
|
|
|
|
|
2019-02-14 18:59:50 -07:00
|
|
|
// Binds constant buffers/textures/samplers to the compute shader stage.
|
|
|
|
// We don't track these explicitly because it's not often-used.
|
2023-06-10 11:35:36 -06:00
|
|
|
void SetComputeUAV(u32 index, ID3D11UnorderedAccessView* uav);
|
2019-02-14 18:59:50 -07:00
|
|
|
void SetComputeShader(ID3D11ComputeShader* shader);
|
|
|
|
void SyncComputeBindings();
|
|
|
|
|
2014-06-23 00:11:07 -06:00
|
|
|
private:
|
2014-10-28 18:19:09 -06:00
|
|
|
enum DirtyFlags
|
|
|
|
{
|
2023-02-10 18:10:55 -07:00
|
|
|
DirtyFlag_Texture0 = 0,
|
|
|
|
DirtyFlag_Sampler0 = DirtyFlag_Texture0 + VideoCommon::MAX_PIXEL_SHADER_SAMPLERS,
|
|
|
|
DirtyFlag_PixelConstants = DirtyFlag_Sampler0 + VideoCommon::MAX_PIXEL_SHADER_SAMPLERS,
|
|
|
|
DirtyFlag_VertexConstants,
|
|
|
|
DirtyFlag_GeometryConstants,
|
|
|
|
|
|
|
|
DirtyFlag_VertexBuffer,
|
|
|
|
DirtyFlag_IndexBuffer,
|
|
|
|
|
|
|
|
DirtyFlag_PixelShader,
|
|
|
|
DirtyFlag_VertexShader,
|
|
|
|
DirtyFlag_GeometryShader,
|
|
|
|
|
|
|
|
DirtyFlag_InputAssembler,
|
|
|
|
DirtyFlag_BlendState,
|
|
|
|
DirtyFlag_DepthState,
|
|
|
|
DirtyFlag_RasterizerState,
|
|
|
|
DirtyFlag_Framebuffer,
|
|
|
|
DirtyFlag_Max
|
2014-10-28 18:19:09 -06:00
|
|
|
};
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2023-02-10 18:10:55 -07:00
|
|
|
std::bitset<DirtyFlags::DirtyFlag_Max> m_dirtyFlags;
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2014-10-28 18:19:09 -06:00
|
|
|
struct Resources
|
|
|
|
{
|
2023-02-09 23:28:06 -07:00
|
|
|
std::array<ID3D11ShaderResourceView*, VideoCommon::MAX_PIXEL_SHADER_SAMPLERS> textures;
|
|
|
|
std::array<ID3D11SamplerState*, VideoCommon::MAX_PIXEL_SHADER_SAMPLERS> samplers;
|
2023-09-19 18:29:38 -06:00
|
|
|
std::array<ID3D11Buffer*, 3> pixelConstants;
|
2014-10-28 18:19:09 -06:00
|
|
|
ID3D11Buffer* vertexConstants;
|
|
|
|
ID3D11Buffer* geometryConstants;
|
|
|
|
ID3D11Buffer* vertexBuffer;
|
|
|
|
ID3D11Buffer* indexBuffer;
|
|
|
|
u32 vertexBufferStride;
|
|
|
|
u32 vertexBufferOffset;
|
|
|
|
D3D11_PRIMITIVE_TOPOLOGY topology;
|
|
|
|
ID3D11InputLayout* inputLayout;
|
|
|
|
ID3D11PixelShader* pixelShader;
|
|
|
|
ID3D11VertexShader* vertexShader;
|
|
|
|
ID3D11GeometryShader* geometryShader;
|
2018-02-22 02:40:09 -07:00
|
|
|
ID3D11BlendState* blendState;
|
|
|
|
ID3D11DepthStencilState* depthState;
|
|
|
|
ID3D11RasterizerState* rasterizerState;
|
2019-02-14 18:59:50 -07:00
|
|
|
DXFramebuffer* framebuffer;
|
|
|
|
ID3D11UnorderedAccessView* uav;
|
|
|
|
bool use_integer_rtv;
|
2014-10-28 18:19:09 -06:00
|
|
|
};
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2018-02-22 02:40:09 -07:00
|
|
|
Resources m_pending = {};
|
|
|
|
Resources m_current = {};
|
2019-02-14 18:59:50 -07:00
|
|
|
|
|
|
|
// Compute resources are synced with the graphics resources when we need them.
|
|
|
|
ID3D11Buffer* m_compute_constants = nullptr;
|
2023-06-10 11:35:36 -06:00
|
|
|
std::array<ID3D11ShaderResourceView*, VideoCommon::MAX_COMPUTE_SHADER_SAMPLERS>
|
|
|
|
m_compute_textures{};
|
|
|
|
std::array<ID3D11SamplerState*, VideoCommon::MAX_COMPUTE_SHADER_SAMPLERS> m_compute_samplers{};
|
|
|
|
std::array<ID3D11UnorderedAccessView*, VideoCommon::MAX_COMPUTE_SHADER_SAMPLERS>
|
|
|
|
m_compute_images{};
|
2019-02-14 18:59:50 -07:00
|
|
|
ID3D11ComputeShader* m_compute_shader = nullptr;
|
2014-06-23 00:11:07 -06:00
|
|
|
};
|
|
|
|
|
2019-03-29 03:55:00 -06:00
|
|
|
extern std::unique_ptr<StateManager> stateman;
|
2014-06-23 00:11:07 -06:00
|
|
|
|
2019-02-14 18:59:50 -07:00
|
|
|
} // namespace D3D
|
2014-06-23 00:11:07 -06:00
|
|
|
|
2015-03-01 09:17:09 -07:00
|
|
|
} // namespace DX11
|