2014-06-08 22:36:26 +00:00
|
|
|
// Copyright 2013 Dolphin Emulator Project
|
|
|
|
// Licensed under GPLv2
|
|
|
|
// Refer to the license.txt file included.
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2014-06-23 06:11:07 +00:00
|
|
|
#include <stack>
|
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"
|
|
|
|
#include "VideoCommon/BPMemory.h"
|
|
|
|
|
2014-06-23 06:11:07 +00:00
|
|
|
struct ID3D11BlendState;
|
|
|
|
struct ID3D11DepthStencilState;
|
|
|
|
struct ID3D11RasterizerState;
|
|
|
|
|
2014-06-08 22:36:26 +00:00
|
|
|
namespace DX11
|
|
|
|
{
|
|
|
|
|
|
|
|
union RasterizerState
|
|
|
|
{
|
2014-06-09 09:55:25 +00:00
|
|
|
BitField<0, 2, D3D11_CULL_MODE> cull_mode;
|
2014-06-08 22:36:26 +00:00
|
|
|
|
|
|
|
u32 packed;
|
|
|
|
};
|
|
|
|
|
|
|
|
union BlendState
|
|
|
|
{
|
2014-06-09 09:55:25 +00:00
|
|
|
BitField<0, 1, u32> blend_enable;
|
|
|
|
BitField<1, 3, D3D11_BLEND_OP> blend_op;
|
|
|
|
BitField<4, 4, u32> write_mask;
|
|
|
|
BitField<8, 5, D3D11_BLEND> src_blend;
|
|
|
|
BitField<13, 5, D3D11_BLEND> dst_blend;
|
|
|
|
BitField<18, 1, u32> use_dst_alpha;
|
2014-06-08 22:36:26 +00:00
|
|
|
|
|
|
|
u32 packed;
|
|
|
|
};
|
|
|
|
|
|
|
|
union SamplerState
|
|
|
|
{
|
2014-06-09 09:55:25 +00:00
|
|
|
BitField<0, 3, u64> min_filter;
|
|
|
|
BitField<3, 1, u64> mag_filter;
|
|
|
|
BitField<4, 8, u64> min_lod;
|
|
|
|
BitField<12, 8, u64> max_lod;
|
|
|
|
BitField<20, 8, s64> lod_bias;
|
|
|
|
BitField<28, 2, u64> wrap_s;
|
|
|
|
BitField<30, 2, u64> wrap_t;
|
|
|
|
BitField<32, 5, u64> max_anisotropy;
|
2014-06-08 22:36:26 +00:00
|
|
|
|
|
|
|
u64 packed;
|
|
|
|
};
|
|
|
|
|
|
|
|
class StateCache
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
|
|
|
// 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);
|
|
|
|
ID3D11BlendState* Get(BlendState state);
|
|
|
|
ID3D11RasterizerState* Get(RasterizerState state);
|
|
|
|
ID3D11DepthStencilState* Get(ZMode state);
|
|
|
|
|
|
|
|
// Release all cached states and clear hash tables.
|
|
|
|
void Clear();
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
|
|
|
std::unordered_map<u32, ID3D11DepthStencilState*> m_depth;
|
|
|
|
std::unordered_map<u32, ID3D11RasterizerState*> m_raster;
|
|
|
|
std::unordered_map<u32, ID3D11BlendState*> m_blend;
|
|
|
|
std::unordered_map<u64, ID3D11SamplerState*> m_sampler;
|
|
|
|
};
|
|
|
|
|
2014-06-23 06:11:07 +00:00
|
|
|
namespace D3D
|
|
|
|
{
|
|
|
|
|
|
|
|
template<typename T> class AutoState
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
AutoState(const T* object);
|
|
|
|
AutoState(const AutoState<T> &source);
|
|
|
|
~AutoState();
|
|
|
|
|
|
|
|
const inline T* GetPtr() const { return state; }
|
|
|
|
|
|
|
|
private:
|
|
|
|
const T* state;
|
|
|
|
};
|
|
|
|
|
|
|
|
typedef AutoState<ID3D11BlendState> AutoBlendState;
|
|
|
|
typedef AutoState<ID3D11DepthStencilState> AutoDepthStencilState;
|
|
|
|
typedef AutoState<ID3D11RasterizerState> AutoRasterizerState;
|
|
|
|
|
|
|
|
class StateManager
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
StateManager();
|
|
|
|
|
|
|
|
// call any of these to change the affected states
|
|
|
|
void PushBlendState(const ID3D11BlendState* state);
|
|
|
|
void PushDepthState(const ID3D11DepthStencilState* state);
|
|
|
|
void PushRasterizerState(const ID3D11RasterizerState* state);
|
|
|
|
|
|
|
|
// call these after drawing
|
|
|
|
void PopBlendState();
|
|
|
|
void PopDepthState();
|
|
|
|
void PopRasterizerState();
|
|
|
|
|
2014-12-06 13:54:06 +00:00
|
|
|
void SetTexture(u32 index, ID3D11ShaderResourceView* texture)
|
2014-10-29 00:19:09 +00:00
|
|
|
{
|
|
|
|
if (m_current.textures[index] != texture)
|
|
|
|
m_dirtyFlags |= DirtyFlag_Texture0 << index;
|
2014-12-13 20:51:18 +00:00
|
|
|
|
|
|
|
m_pending.textures[index] = texture;
|
2014-10-29 00:19:09 +00:00
|
|
|
}
|
|
|
|
|
2014-12-06 13:54:06 +00:00
|
|
|
void SetSampler(u32 index, ID3D11SamplerState* sampler)
|
2014-10-29 00:19:09 +00:00
|
|
|
{
|
|
|
|
if (m_current.samplers[index] != sampler)
|
|
|
|
m_dirtyFlags |= DirtyFlag_Sampler0 << index;
|
2014-12-13 20:51:18 +00:00
|
|
|
|
|
|
|
m_pending.samplers[index] = sampler;
|
2014-10-29 00:19:09 +00:00
|
|
|
}
|
|
|
|
|
2014-12-06 13:54:06 +00:00
|
|
|
void SetPixelConstants(ID3D11Buffer* buffer0, ID3D11Buffer* buffer1 = nullptr)
|
2014-10-29 00:19:09 +00:00
|
|
|
{
|
2014-12-13 20:51:18 +00:00
|
|
|
if (m_current.pixelConstants[0] != buffer0 || m_current.pixelConstants[1] != buffer1)
|
2014-10-29 00:19:09 +00:00
|
|
|
m_dirtyFlags |= DirtyFlag_PixelConstants;
|
|
|
|
|
2014-12-13 20:51:18 +00:00
|
|
|
m_pending.pixelConstants[0] = buffer0;
|
|
|
|
m_pending.pixelConstants[1] = buffer1;
|
2014-10-29 00:19:09 +00:00
|
|
|
}
|
|
|
|
|
2014-12-06 13:54:06 +00:00
|
|
|
void SetVertexConstants(ID3D11Buffer* buffer)
|
2014-10-29 00:19:09 +00:00
|
|
|
{
|
|
|
|
if (m_current.vertexConstants != buffer)
|
|
|
|
m_dirtyFlags |= DirtyFlag_VertexConstants;
|
2014-12-13 20:51:18 +00:00
|
|
|
|
|
|
|
m_pending.vertexConstants = buffer;
|
2014-10-29 00:19:09 +00:00
|
|
|
}
|
|
|
|
|
2014-12-06 13:54:06 +00:00
|
|
|
void SetGeometryConstants(ID3D11Buffer* buffer)
|
2014-10-29 00:19:09 +00:00
|
|
|
{
|
|
|
|
if (m_current.geometryConstants != buffer)
|
|
|
|
m_dirtyFlags |= DirtyFlag_GeometryConstants;
|
2014-12-13 20:51:18 +00:00
|
|
|
|
|
|
|
m_pending.geometryConstants = buffer;
|
2014-10-29 00:19:09 +00:00
|
|
|
}
|
|
|
|
|
2014-12-06 13:54:06 +00:00
|
|
|
void SetVertexBuffer(ID3D11Buffer* buffer, u32 stride, u32 offset)
|
2014-10-29 00:19:09 +00:00
|
|
|
{
|
|
|
|
if (m_current.vertexBuffer != buffer ||
|
|
|
|
m_current.vertexBufferStride != stride ||
|
|
|
|
m_current.vertexBufferOffset != offset)
|
|
|
|
m_dirtyFlags |= DirtyFlag_VertexBuffer;
|
2014-12-13 20:51:18 +00:00
|
|
|
|
|
|
|
m_pending.vertexBuffer = buffer;
|
|
|
|
m_pending.vertexBufferStride = stride;
|
|
|
|
m_pending.vertexBufferOffset = offset;
|
2014-10-29 00:19:09 +00:00
|
|
|
}
|
|
|
|
|
2014-12-06 13:54:06 +00:00
|
|
|
void SetIndexBuffer(ID3D11Buffer* buffer)
|
2014-10-29 00:19:09 +00:00
|
|
|
{
|
|
|
|
if (m_current.indexBuffer != buffer)
|
|
|
|
m_dirtyFlags |= DirtyFlag_IndexBuffer;
|
2014-12-13 20:51:18 +00:00
|
|
|
|
|
|
|
m_pending.indexBuffer = buffer;
|
2014-10-29 00:19:09 +00:00
|
|
|
}
|
|
|
|
|
2014-12-06 13:54:06 +00:00
|
|
|
void SetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY topology)
|
2014-10-29 00:19:09 +00:00
|
|
|
{
|
|
|
|
if (m_current.topology != topology)
|
|
|
|
m_dirtyFlags |= DirtyFlag_InputAssembler;
|
2014-12-13 20:51:18 +00:00
|
|
|
|
|
|
|
m_pending.topology = topology;
|
2014-10-29 00:19:09 +00:00
|
|
|
}
|
|
|
|
|
2014-12-06 13:54:06 +00:00
|
|
|
void SetInputLayout(ID3D11InputLayout* layout)
|
2014-10-29 00:19:09 +00:00
|
|
|
{
|
|
|
|
if (m_current.inputLayout != layout)
|
|
|
|
m_dirtyFlags |= DirtyFlag_InputAssembler;
|
2014-12-13 20:51:18 +00:00
|
|
|
|
|
|
|
m_pending.inputLayout = layout;
|
2014-10-29 00:19:09 +00:00
|
|
|
}
|
|
|
|
|
2014-12-06 13:54:06 +00:00
|
|
|
void SetPixelShader(ID3D11PixelShader* shader)
|
2014-10-29 00:19:09 +00:00
|
|
|
{
|
|
|
|
if (m_current.pixelShader != shader)
|
|
|
|
m_dirtyFlags |= DirtyFlag_PixelShader;
|
2014-12-13 20:51:18 +00:00
|
|
|
|
|
|
|
m_pending.pixelShader = shader;
|
2014-10-29 00:19:09 +00:00
|
|
|
}
|
|
|
|
|
2014-12-06 13:54:06 +00:00
|
|
|
void SetPixelShaderDynamic(ID3D11PixelShader* shader, ID3D11ClassInstance * const * classInstances, u32 classInstancesCount)
|
2014-10-29 00:19:09 +00:00
|
|
|
{
|
|
|
|
D3D::context->PSSetShader(shader, classInstances, classInstancesCount);
|
2014-12-13 21:09:34 +00:00
|
|
|
m_current.pixelShader = shader;
|
2014-10-29 00:19:09 +00:00
|
|
|
m_pending.pixelShader = shader;
|
|
|
|
}
|
|
|
|
|
2014-12-06 13:54:06 +00:00
|
|
|
void SetVertexShader(ID3D11VertexShader* shader)
|
2014-10-29 00:19:09 +00:00
|
|
|
{
|
|
|
|
if (m_current.vertexShader != shader)
|
|
|
|
m_dirtyFlags |= DirtyFlag_VertexShader;
|
2014-12-13 20:51:18 +00:00
|
|
|
|
|
|
|
m_pending.vertexShader = shader;
|
2014-10-29 00:19:09 +00:00
|
|
|
}
|
|
|
|
|
2014-12-06 13:54:06 +00:00
|
|
|
void SetGeometryShader(ID3D11GeometryShader* shader)
|
2014-10-29 00:19:09 +00:00
|
|
|
{
|
|
|
|
if (m_current.geometryShader != shader)
|
|
|
|
m_dirtyFlags |= DirtyFlag_GeometryShader;
|
2014-12-13 20:51:18 +00:00
|
|
|
|
|
|
|
m_pending.geometryShader = shader;
|
2014-10-29 00:19:09 +00:00
|
|
|
}
|
|
|
|
|
2014-12-12 23:32:08 +00:00
|
|
|
// removes currently set texture from all slots, returns mask of previously bound slots
|
2014-12-17 19:31:08 +00:00
|
|
|
u32 UnsetTexture(ID3D11ShaderResourceView* srv);
|
|
|
|
void SetTextureByMask(u32 textureSlotMask, ID3D11ShaderResourceView* srv);
|
2014-12-12 23:32:08 +00:00
|
|
|
|
2014-10-29 00:19:09 +00:00
|
|
|
// call this immediately before any drawing operation or to explicitly apply pending resource state changes
|
2014-06-23 06:11:07 +00:00
|
|
|
void Apply();
|
|
|
|
|
|
|
|
private:
|
2014-10-29 00:19:09 +00:00
|
|
|
|
|
|
|
std::stack<AutoBlendState> m_blendStates;
|
|
|
|
std::stack<AutoDepthStencilState> m_depthStates;
|
|
|
|
std::stack<AutoRasterizerState> m_rasterizerStates;
|
|
|
|
|
|
|
|
ID3D11BlendState* m_currentBlendState;
|
|
|
|
ID3D11DepthStencilState* m_currentDepthState;
|
|
|
|
ID3D11RasterizerState* m_currentRasterizerState;
|
|
|
|
|
|
|
|
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,
|
|
|
|
|
|
|
|
DirtyFlag_VertexBuffer = 1 << 19,
|
|
|
|
DirtyFlag_IndexBuffer = 1 << 20,
|
|
|
|
|
|
|
|
DirtyFlag_PixelShader = 1 << 21,
|
|
|
|
DirtyFlag_VertexShader = 1 << 22,
|
|
|
|
DirtyFlag_GeometryShader = 1 << 23,
|
|
|
|
|
|
|
|
DirtyFlag_InputAssembler = 1 << 24,
|
|
|
|
};
|
|
|
|
|
|
|
|
u32 m_dirtyFlags;
|
|
|
|
|
|
|
|
struct Resources
|
|
|
|
{
|
|
|
|
ID3D11ShaderResourceView* textures[8];
|
|
|
|
ID3D11SamplerState* samplers[8];
|
|
|
|
ID3D11Buffer* pixelConstants[2];
|
|
|
|
ID3D11Buffer* vertexConstants;
|
|
|
|
ID3D11Buffer* geometryConstants;
|
|
|
|
ID3D11Buffer* vertexBuffer;
|
|
|
|
ID3D11Buffer* indexBuffer;
|
|
|
|
u32 vertexBufferStride;
|
|
|
|
u32 vertexBufferOffset;
|
|
|
|
D3D11_PRIMITIVE_TOPOLOGY topology;
|
|
|
|
ID3D11InputLayout* inputLayout;
|
|
|
|
ID3D11PixelShader* pixelShader;
|
|
|
|
ID3D11VertexShader* vertexShader;
|
|
|
|
ID3D11GeometryShader* geometryShader;
|
|
|
|
};
|
2014-10-29 08:52:08 +00:00
|
|
|
|
2014-10-29 00:19:09 +00:00
|
|
|
Resources m_pending;
|
|
|
|
Resources m_current;
|
2014-06-23 06:11:07 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
extern StateManager* stateman;
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
} // namespace DX11
|