2015-05-24 04:55:12 +00:00
|
|
|
// Copyright 2014 Dolphin Emulator Project
|
2015-05-17 23:08:10 +00:00
|
|
|
// Licensed under GPLv2+
|
2014-06-08 22:36:26 +00:00
|
|
|
// Refer to the license.txt file included.
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2017-07-20 05:25:31 +00:00
|
|
|
#include <array>
|
2017-04-09 20:15:57 +00:00
|
|
|
#include <cstddef>
|
2014-06-08 22:36:26 +00:00
|
|
|
#include <unordered_map>
|
|
|
|
|
2014-06-09 09:55:25 +00:00
|
|
|
#include "Common/BitField.h"
|
2014-06-08 22:36:26 +00:00
|
|
|
#include "Common/CommonTypes.h"
|
|
|
|
#include "VideoBackends/D3D/D3DBase.h"
|
2017-04-29 14:54:22 +00:00
|
|
|
#include "VideoCommon/RenderState.h"
|
2014-06-08 22:36:26 +00:00
|
|
|
|
|
|
|
namespace DX11
|
|
|
|
{
|
|
|
|
class StateCache
|
|
|
|
{
|
|
|
|
public:
|
2017-09-30 06:19:16 +00:00
|
|
|
~StateCache();
|
|
|
|
|
2016-06-24 08:43:46 +00: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 14:54:22 +00:00
|
|
|
ID3D11BlendState* Get(BlendingState state);
|
2017-04-30 08:07:57 +00:00
|
|
|
ID3D11RasterizerState* Get(RasterizationState state);
|
2017-04-30 05:54:45 +00:00
|
|
|
ID3D11DepthStencilState* Get(DepthState state);
|
2014-06-08 22:36:26 +00:00
|
|
|
|
2017-04-30 08:07:57 +00:00
|
|
|
// Convert RasterState primitive type to D3D11 primitive topology.
|
|
|
|
static D3D11_PRIMITIVE_TOPOLOGY GetPrimitiveTopology(PrimitiveType primitive);
|
|
|
|
|
2014-06-08 22:36:26 +00:00
|
|
|
private:
|
2016-06-24 08:43:46 +00:00
|
|
|
std::unordered_map<u32, ID3D11DepthStencilState*> m_depth;
|
|
|
|
std::unordered_map<u32, ID3D11RasterizerState*> m_raster;
|
|
|
|
std::unordered_map<u32, ID3D11BlendState*> m_blend;
|
2017-10-18 09:02:56 +00:00
|
|
|
std::unordered_map<SamplerState::StorageType, ID3D11SamplerState*> m_sampler;
|
2014-06-08 22:36:26 +00:00
|
|
|
};
|
|
|
|
|
2014-06-23 06:11:07 +00:00
|
|
|
namespace D3D
|
|
|
|
{
|
2018-02-22 09:40:09 +00:00
|
|
|
class StateManager
|
2014-06-23 06:11:07 +00:00
|
|
|
{
|
|
|
|
public:
|
2018-02-22 09:40:09 +00:00
|
|
|
StateManager();
|
|
|
|
~StateManager();
|
2014-06-23 06:11:07 +00:00
|
|
|
|
2018-02-22 09:40:09 +00:00
|
|
|
void SetBlendState(ID3D11BlendState* state)
|
|
|
|
{
|
|
|
|
if (m_current.blendState != state)
|
|
|
|
m_dirtyFlags |= DirtyFlag_BlendState;
|
2014-06-23 06:11:07 +00:00
|
|
|
|
2018-02-22 09:40:09 +00:00
|
|
|
m_pending.blendState = state;
|
|
|
|
}
|
2014-06-23 06:11:07 +00:00
|
|
|
|
2018-02-22 09:40:09 +00:00
|
|
|
void SetDepthState(ID3D11DepthStencilState* state)
|
|
|
|
{
|
|
|
|
if (m_current.depthState != state)
|
|
|
|
m_dirtyFlags |= DirtyFlag_DepthState;
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2018-02-22 09:40:09 +00:00
|
|
|
m_pending.depthState = state;
|
|
|
|
}
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2018-02-22 09:40:09 +00:00
|
|
|
void SetRasterizerState(ID3D11RasterizerState* state)
|
|
|
|
{
|
|
|
|
if (m_current.rasterizerState != state)
|
|
|
|
m_dirtyFlags |= DirtyFlag_RasterizerState;
|
|
|
|
|
|
|
|
m_pending.rasterizerState = state;
|
|
|
|
}
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2017-04-09 20:15:57 +00:00
|
|
|
void SetTexture(size_t index, ID3D11ShaderResourceView* texture)
|
2016-06-24 08:43:46 +00:00
|
|
|
{
|
|
|
|
if (m_current.textures[index] != texture)
|
|
|
|
m_dirtyFlags |= DirtyFlag_Texture0 << index;
|
|
|
|
|
|
|
|
m_pending.textures[index] = texture;
|
|
|
|
}
|
|
|
|
|
2017-04-09 20:15:57 +00:00
|
|
|
void SetSampler(size_t index, ID3D11SamplerState* sampler)
|
2016-06-24 08:43:46 +00:00
|
|
|
{
|
|
|
|
if (m_current.samplers[index] != sampler)
|
|
|
|
m_dirtyFlags |= DirtyFlag_Sampler0 << index;
|
|
|
|
|
|
|
|
m_pending.samplers[index] = sampler;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SetPixelConstants(ID3D11Buffer* buffer0, ID3D11Buffer* buffer1 = nullptr)
|
|
|
|
{
|
|
|
|
if (m_current.pixelConstants[0] != buffer0 || m_current.pixelConstants[1] != buffer1)
|
|
|
|
m_dirtyFlags |= DirtyFlag_PixelConstants;
|
|
|
|
|
|
|
|
m_pending.pixelConstants[0] = buffer0;
|
|
|
|
m_pending.pixelConstants[1] = buffer1;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SetVertexConstants(ID3D11Buffer* buffer)
|
|
|
|
{
|
|
|
|
if (m_current.vertexConstants != buffer)
|
|
|
|
m_dirtyFlags |= DirtyFlag_VertexConstants;
|
|
|
|
|
|
|
|
m_pending.vertexConstants = buffer;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SetGeometryConstants(ID3D11Buffer* buffer)
|
|
|
|
{
|
|
|
|
if (m_current.geometryConstants != buffer)
|
|
|
|
m_dirtyFlags |= DirtyFlag_GeometryConstants;
|
|
|
|
|
|
|
|
m_pending.geometryConstants = buffer;
|
|
|
|
}
|
|
|
|
|
2018-02-09 13:33:56 +00:00
|
|
|
void SetComputeConstants(ID3D11Buffer* buffer)
|
|
|
|
{
|
|
|
|
if (m_current.computeConstants != buffer)
|
|
|
|
m_dirtyFlags |= DirtyFlag_ComputeConstants;
|
|
|
|
|
|
|
|
m_pending.computeConstants = buffer;
|
|
|
|
}
|
|
|
|
|
2016-06-24 08:43:46 +00:00
|
|
|
void SetVertexBuffer(ID3D11Buffer* buffer, u32 stride, u32 offset)
|
|
|
|
{
|
|
|
|
if (m_current.vertexBuffer != buffer || m_current.vertexBufferStride != stride ||
|
|
|
|
m_current.vertexBufferOffset != offset)
|
|
|
|
m_dirtyFlags |= DirtyFlag_VertexBuffer;
|
|
|
|
|
|
|
|
m_pending.vertexBuffer = buffer;
|
|
|
|
m_pending.vertexBufferStride = stride;
|
|
|
|
m_pending.vertexBufferOffset = offset;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SetIndexBuffer(ID3D11Buffer* buffer)
|
|
|
|
{
|
|
|
|
if (m_current.indexBuffer != buffer)
|
|
|
|
m_dirtyFlags |= DirtyFlag_IndexBuffer;
|
|
|
|
|
|
|
|
m_pending.indexBuffer = buffer;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY topology)
|
|
|
|
{
|
|
|
|
if (m_current.topology != topology)
|
|
|
|
m_dirtyFlags |= DirtyFlag_InputAssembler;
|
|
|
|
|
|
|
|
m_pending.topology = topology;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SetInputLayout(ID3D11InputLayout* layout)
|
|
|
|
{
|
|
|
|
if (m_current.inputLayout != layout)
|
|
|
|
m_dirtyFlags |= DirtyFlag_InputAssembler;
|
|
|
|
|
|
|
|
m_pending.inputLayout = layout;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SetPixelShader(ID3D11PixelShader* shader)
|
|
|
|
{
|
|
|
|
if (m_current.pixelShader != shader)
|
|
|
|
m_dirtyFlags |= DirtyFlag_PixelShader;
|
|
|
|
|
|
|
|
m_pending.pixelShader = shader;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SetPixelShaderDynamic(ID3D11PixelShader* shader, ID3D11ClassInstance* const* classInstances,
|
|
|
|
u32 classInstancesCount)
|
|
|
|
{
|
|
|
|
D3D::context->PSSetShader(shader, classInstances, classInstancesCount);
|
|
|
|
m_current.pixelShader = shader;
|
|
|
|
m_pending.pixelShader = shader;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SetVertexShader(ID3D11VertexShader* shader)
|
|
|
|
{
|
|
|
|
if (m_current.vertexShader != shader)
|
|
|
|
m_dirtyFlags |= DirtyFlag_VertexShader;
|
|
|
|
|
|
|
|
m_pending.vertexShader = shader;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SetGeometryShader(ID3D11GeometryShader* shader)
|
|
|
|
{
|
|
|
|
if (m_current.geometryShader != shader)
|
|
|
|
m_dirtyFlags |= DirtyFlag_GeometryShader;
|
|
|
|
|
|
|
|
m_pending.geometryShader = shader;
|
|
|
|
}
|
|
|
|
|
2018-02-09 13:33:56 +00:00
|
|
|
void SetComputeShader(ID3D11ComputeShader* shader)
|
|
|
|
{
|
|
|
|
if (m_current.computeShader != shader)
|
|
|
|
m_dirtyFlags |= DirtyFlag_ComputeShader;
|
|
|
|
|
|
|
|
m_pending.computeShader = shader;
|
|
|
|
}
|
|
|
|
|
2016-06-24 08:43:46 +00:00
|
|
|
// removes currently set texture from all slots, returns mask of previously bound slots
|
|
|
|
u32 UnsetTexture(ID3D11ShaderResourceView* srv);
|
|
|
|
void SetTextureByMask(u32 textureSlotMask, ID3D11ShaderResourceView* srv);
|
|
|
|
|
|
|
|
// call this immediately before any drawing operation or to explicitly apply pending resource
|
|
|
|
// state changes
|
|
|
|
void Apply();
|
2014-06-23 06:11:07 +00:00
|
|
|
|
|
|
|
private:
|
2016-06-24 08:43:46 +00:00
|
|
|
enum DirtyFlags
|
|
|
|
{
|
|
|
|
DirtyFlag_Texture0 = 1 << 0,
|
|
|
|
DirtyFlag_Texture1 = 1 << 1,
|
|
|
|
DirtyFlag_Texture2 = 1 << 2,
|
|
|
|
DirtyFlag_Texture3 = 1 << 3,
|
|
|
|
DirtyFlag_Texture4 = 1 << 4,
|
|
|
|
DirtyFlag_Texture5 = 1 << 5,
|
|
|
|
DirtyFlag_Texture6 = 1 << 6,
|
|
|
|
DirtyFlag_Texture7 = 1 << 7,
|
|
|
|
|
|
|
|
DirtyFlag_Sampler0 = 1 << 8,
|
|
|
|
DirtyFlag_Sampler1 = 1 << 9,
|
|
|
|
DirtyFlag_Sampler2 = 1 << 10,
|
|
|
|
DirtyFlag_Sampler3 = 1 << 11,
|
|
|
|
DirtyFlag_Sampler4 = 1 << 12,
|
|
|
|
DirtyFlag_Sampler5 = 1 << 13,
|
|
|
|
DirtyFlag_Sampler6 = 1 << 14,
|
|
|
|
DirtyFlag_Sampler7 = 1 << 15,
|
|
|
|
|
|
|
|
DirtyFlag_PixelConstants = 1 << 16,
|
|
|
|
DirtyFlag_VertexConstants = 1 << 17,
|
|
|
|
DirtyFlag_GeometryConstants = 1 << 18,
|
2018-02-09 13:33:56 +00:00
|
|
|
DirtyFlag_ComputeConstants = 1 << 19,
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2018-02-09 13:33:56 +00:00
|
|
|
DirtyFlag_VertexBuffer = 1 << 20,
|
|
|
|
DirtyFlag_IndexBuffer = 1 << 21,
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2018-02-09 13:33:56 +00:00
|
|
|
DirtyFlag_PixelShader = 1 << 22,
|
|
|
|
DirtyFlag_VertexShader = 1 << 23,
|
|
|
|
DirtyFlag_GeometryShader = 1 << 24,
|
|
|
|
DirtyFlag_ComputeShader = 1 << 25,
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2018-02-09 13:33:56 +00:00
|
|
|
DirtyFlag_InputAssembler = 1 << 26,
|
2018-02-22 09:40:09 +00:00
|
|
|
DirtyFlag_BlendState = 1 << 27,
|
|
|
|
DirtyFlag_DepthState = 1 << 28,
|
|
|
|
DirtyFlag_RasterizerState = 1 << 29,
|
2016-06-24 08:43:46 +00:00
|
|
|
};
|
|
|
|
|
2018-02-22 09:40:09 +00:00
|
|
|
u32 m_dirtyFlags = ~0u;
|
2016-06-24 08:43:46 +00:00
|
|
|
|
|
|
|
struct Resources
|
|
|
|
{
|
2017-07-20 05:25:31 +00:00
|
|
|
std::array<ID3D11ShaderResourceView*, 8> textures;
|
|
|
|
std::array<ID3D11SamplerState*, 8> samplers;
|
|
|
|
std::array<ID3D11Buffer*, 2> pixelConstants;
|
2016-06-24 08:43:46 +00:00
|
|
|
ID3D11Buffer* vertexConstants;
|
|
|
|
ID3D11Buffer* geometryConstants;
|
2018-02-09 13:33:56 +00:00
|
|
|
ID3D11Buffer* computeConstants;
|
2016-06-24 08:43:46 +00:00
|
|
|
ID3D11Buffer* vertexBuffer;
|
|
|
|
ID3D11Buffer* indexBuffer;
|
|
|
|
u32 vertexBufferStride;
|
|
|
|
u32 vertexBufferOffset;
|
|
|
|
D3D11_PRIMITIVE_TOPOLOGY topology;
|
|
|
|
ID3D11InputLayout* inputLayout;
|
|
|
|
ID3D11PixelShader* pixelShader;
|
|
|
|
ID3D11VertexShader* vertexShader;
|
|
|
|
ID3D11GeometryShader* geometryShader;
|
2018-02-09 13:33:56 +00:00
|
|
|
ID3D11ComputeShader* computeShader;
|
2018-02-22 09:40:09 +00:00
|
|
|
ID3D11BlendState* blendState;
|
|
|
|
ID3D11DepthStencilState* depthState;
|
|
|
|
ID3D11RasterizerState* rasterizerState;
|
2016-06-24 08:43:46 +00:00
|
|
|
};
|
|
|
|
|
2018-02-22 09:40:09 +00:00
|
|
|
Resources m_pending = {};
|
|
|
|
Resources m_current = {};
|
2014-06-23 06:11:07 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
extern StateManager* stateman;
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
2015-03-01 16:17:09 +00:00
|
|
|
} // namespace DX11
|