Skip to content
Permalink
Browse files

D3D11: Use ComPtr smart pointer where possible

  • Loading branch information...
stenzek committed Mar 9, 2019
1 parent 3d8014b commit 1151a1238f805d5d0d6b149d12344beb661ba853
@@ -11,13 +11,13 @@

namespace DX11
{
static ID3D11Buffer* s_bbox_buffer;
static ID3D11Buffer* s_bbox_staging_buffer;
static ID3D11UnorderedAccessView* s_bbox_uav;
static ComPtr<ID3D11Buffer> s_bbox_buffer;
static ComPtr<ID3D11Buffer> s_bbox_staging_buffer;
static ComPtr<ID3D11UnorderedAccessView> s_bbox_uav;

ID3D11UnorderedAccessView*& BBox::GetUAV()
ID3D11UnorderedAccessView* BBox::GetUAV()
{
return s_bbox_uav;
return s_bbox_uav.Get();
}

void BBox::Init()
@@ -36,15 +36,15 @@ void BBox::Init()
HRESULT hr;
hr = D3D::device->CreateBuffer(&desc, &data, &s_bbox_buffer);
CHECK(SUCCEEDED(hr), "Create BoundingBox Buffer.");
D3DCommon::SetDebugObjectName(s_bbox_buffer, "BoundingBox Buffer");
D3DCommon::SetDebugObjectName(s_bbox_buffer.Get(), "BoundingBox Buffer");

// Second to use as a staging buffer.
desc.Usage = D3D11_USAGE_STAGING;
desc.CPUAccessFlags = D3D11_CPU_ACCESS_READ;
desc.BindFlags = 0;
hr = D3D::device->CreateBuffer(&desc, nullptr, &s_bbox_staging_buffer);
CHECK(SUCCEEDED(hr), "Create BoundingBox Staging Buffer.");
D3DCommon::SetDebugObjectName(s_bbox_staging_buffer, "BoundingBox Staging Buffer");
D3DCommon::SetDebugObjectName(s_bbox_staging_buffer.Get(), "BoundingBox Staging Buffer");

// UAV is required to allow concurrent access.
D3D11_UNORDERED_ACCESS_VIEW_DESC UAVdesc = {};
@@ -53,37 +53,37 @@ void BBox::Init()
UAVdesc.Buffer.FirstElement = 0;
UAVdesc.Buffer.Flags = 0;
UAVdesc.Buffer.NumElements = 4;
hr = D3D::device->CreateUnorderedAccessView(s_bbox_buffer, &UAVdesc, &s_bbox_uav);
hr = D3D::device->CreateUnorderedAccessView(s_bbox_buffer.Get(), &UAVdesc, &s_bbox_uav);
CHECK(SUCCEEDED(hr), "Create BoundingBox UAV.");
D3DCommon::SetDebugObjectName(s_bbox_uav, "BoundingBox UAV");
D3D::stateman->SetOMUAV(s_bbox_uav);
D3DCommon::SetDebugObjectName(s_bbox_uav.Get(), "BoundingBox UAV");
D3D::stateman->SetOMUAV(s_bbox_uav.Get());
}
}

void BBox::Shutdown()
{
SAFE_RELEASE(s_bbox_buffer);
SAFE_RELEASE(s_bbox_staging_buffer);
SAFE_RELEASE(s_bbox_uav);
s_bbox_uav.Reset();
s_bbox_staging_buffer.Reset();
s_bbox_buffer.Reset();
}

void BBox::Set(int index, int value)
{
D3D11_BOX box{index * sizeof(s32), 0, 0, (index + 1) * sizeof(s32), 1, 1};
D3D::context->UpdateSubresource(s_bbox_buffer, 0, &box, &value, 0, 0);
D3D::context->UpdateSubresource(s_bbox_buffer.Get(), 0, &box, &value, 0, 0);
}

int BBox::Get(int index)
{
int data = 0;
D3D::context->CopyResource(s_bbox_staging_buffer, s_bbox_buffer);
D3D::context->CopyResource(s_bbox_staging_buffer.Get(), s_bbox_buffer.Get());
D3D11_MAPPED_SUBRESOURCE map;
HRESULT hr = D3D::context->Map(s_bbox_staging_buffer, 0, D3D11_MAP_READ, 0, &map);
HRESULT hr = D3D::context->Map(s_bbox_staging_buffer.Get(), 0, D3D11_MAP_READ, 0, &map);
if (SUCCEEDED(hr))
{
data = ((s32*)map.pData)[index];
}
D3D::context->Unmap(s_bbox_staging_buffer, 0);
D3D::context->Unmap(s_bbox_staging_buffer.Get(), 0);
return data;
}
}; // namespace DX11
@@ -10,7 +10,7 @@ namespace DX11
class BBox
{
public:
static ID3D11UnorderedAccessView*& GetUAV();
static ID3D11UnorderedAccessView* GetUAV();
static void Init();
static void Shutdown();

@@ -15,22 +15,6 @@
#include "Common/CommonTypes.h"
#include "Common/MsgHandler.h"

#define SAFE_RELEASE(x) \
{ \
if (x) \
(x)->Release(); \
(x) = nullptr; \
}
#define SAFE_DELETE(x) \
{ \
delete (x); \
(x) = nullptr; \
}
#define SAFE_DELETE_ARRAY(x) \
{ \
delete[](x); \
(x) = nullptr; \
}
#define CHECK(cond, Message, ...) \
if (!(cond)) \
{ \
@@ -299,27 +299,14 @@ void StateManager::SyncComputeBindings()
}
} // namespace D3D

StateCache::~StateCache()
{
for (auto& it : m_depth)
SAFE_RELEASE(it.second);

for (auto& it : m_raster)
SAFE_RELEASE(it.second);

for (auto& it : m_blend)
SAFE_RELEASE(it.second);

for (auto& it : m_sampler)
SAFE_RELEASE(it.second);
}
StateCache::~StateCache() = default;

ID3D11SamplerState* StateCache::Get(SamplerState state)
{
std::lock_guard<std::mutex> guard(m_lock);
auto it = m_sampler.find(state.hex);
if (it != m_sampler.end())
return it->second;
return it->second.Get();

D3D11_SAMPLER_DESC sampdc = CD3D11_SAMPLER_DESC(CD3D11_DEFAULT());
if (state.mipmap_filter == SamplerState::Filter::Linear)
@@ -359,19 +346,19 @@ ID3D11SamplerState* StateCache::Get(SamplerState state)
sampdc.MaxAnisotropy = 1u << g_ActiveConfig.iMaxAnisotropy;
}

ID3D11SamplerState* res = nullptr;
ComPtr<ID3D11SamplerState> res;
HRESULT hr = D3D::device->CreateSamplerState(&sampdc, &res);
CHECK(SUCCEEDED(hr), "Creating D3D sampler state failed");
m_sampler.emplace(state.hex, res);
return res;
return res.Get();
}

ID3D11BlendState* StateCache::Get(BlendingState state)
{
std::lock_guard<std::mutex> guard(m_lock);
auto it = m_blend.find(state.hex);
if (it != m_blend.end())
return it->second;
return it->second.Get();

if (state.logicopenable && D3D::device1)
{
@@ -437,20 +424,19 @@ ID3D11BlendState* StateCache::Get(BlendingState state)
tdesc.BlendOp = state.subtract ? D3D11_BLEND_OP_REV_SUBTRACT : D3D11_BLEND_OP_ADD;
tdesc.BlendOpAlpha = state.subtractAlpha ? D3D11_BLEND_OP_REV_SUBTRACT : D3D11_BLEND_OP_ADD;

ID3D11BlendState* res = nullptr;

ComPtr<ID3D11BlendState> res;
HRESULT hr = D3D::device->CreateBlendState(&desc, &res);
CHECK(SUCCEEDED(hr), "Creating D3D blend state failed");
m_blend.emplace(state.hex, res);
return res;
return res.Get();
}

ID3D11RasterizerState* StateCache::Get(RasterizationState state)
{
std::lock_guard<std::mutex> guard(m_lock);
auto it = m_raster.find(state.hex);
if (it != m_raster.end())
return it->second;
return it->second.Get();

static constexpr std::array<D3D11_CULL_MODE, 4> cull_modes = {
{D3D11_CULL_NONE, D3D11_CULL_BACK, D3D11_CULL_FRONT, D3D11_CULL_BACK}};
@@ -460,19 +446,19 @@ ID3D11RasterizerState* StateCache::Get(RasterizationState state)
desc.CullMode = cull_modes[state.cullmode];
desc.ScissorEnable = TRUE;

ID3D11RasterizerState* res = nullptr;
ComPtr<ID3D11RasterizerState> res;
HRESULT hr = D3D::device->CreateRasterizerState(&desc, &res);
CHECK(SUCCEEDED(hr), "Creating D3D rasterizer state failed");
m_raster.emplace(state.hex, res);
return res;
return res.Get();
}

ID3D11DepthStencilState* StateCache::Get(DepthState state)
{
std::lock_guard<std::mutex> guard(m_lock);
auto it = m_depth.find(state.hex);
if (it != m_depth.end())
return it->second;
return it->second.Get();

D3D11_DEPTH_STENCIL_DESC depthdc = CD3D11_DEPTH_STENCIL_DESC(CD3D11_DEFAULT());

@@ -503,12 +489,11 @@ ID3D11DepthStencilState* StateCache::Get(DepthState state)
depthdc.DepthWriteMask = D3D11_DEPTH_WRITE_MASK_ZERO;
}

ID3D11DepthStencilState* res = nullptr;

ComPtr<ID3D11DepthStencilState> res;
HRESULT hr = D3D::device->CreateDepthStencilState(&depthdc, &res);
CHECK(SUCCEEDED(hr), "Creating D3D depth stencil state failed");
m_depth.emplace(state.hex, res);
return res;
return res.Get();
}

D3D11_PRIMITIVE_TOPOLOGY StateCache::GetPrimitiveTopology(PrimitiveType primitive)
@@ -34,10 +34,10 @@ class StateCache
static D3D11_PRIMITIVE_TOPOLOGY GetPrimitiveTopology(PrimitiveType primitive);

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<SamplerState::StorageType, ID3D11SamplerState*> m_sampler;
std::unordered_map<u32, ComPtr<ID3D11DepthStencilState>> m_depth;
std::unordered_map<u32, ComPtr<ID3D11RasterizerState>> m_raster;
std::unordered_map<u32, ComPtr<ID3D11BlendState>> m_blend;
std::unordered_map<SamplerState::StorageType, ComPtr<ID3D11SamplerState>> m_sampler;
std::mutex m_lock;
};

@@ -28,39 +28,9 @@ DXPipeline::DXPipeline(ID3D11InputLayout* input_layout, ID3D11VertexShader* vert
m_rasterizer_state(rasterizer_state), m_depth_state(depth_state), m_blend_state(blend_state),
m_primitive_topology(primitive_topology), m_use_logic_op(use_logic_op)
{
if (m_input_layout)
m_input_layout->AddRef();
if (m_vertex_shader)
m_vertex_shader->AddRef();
if (m_geometry_shader)
m_geometry_shader->AddRef();
if (m_pixel_shader)
m_pixel_shader->AddRef();
if (m_rasterizer_state)
m_rasterizer_state->AddRef();
if (m_depth_state)
m_depth_state->AddRef();
if (m_blend_state)
m_blend_state->AddRef();
}

DXPipeline::~DXPipeline()
{
if (m_input_layout)
m_input_layout->Release();
if (m_vertex_shader)
m_vertex_shader->Release();
if (m_geometry_shader)
m_geometry_shader->Release();
if (m_pixel_shader)
m_pixel_shader->Release();
if (m_rasterizer_state)
m_rasterizer_state->Release();
if (m_depth_state)
m_depth_state->Release();
if (m_blend_state)
m_blend_state->Release();
}
DXPipeline::~DXPipeline() = default;

std::unique_ptr<DXPipeline> DXPipeline::Create(const AbstractPipelineConfig& config)
{
@@ -71,12 +41,7 @@ std::unique_ptr<DXPipeline> DXPipeline::Create(const AbstractPipelineConfig& con
D3D11_PRIMITIVE_TOPOLOGY primitive_topology =
StateCache::GetPrimitiveTopology(config.rasterization_state.primitive);
if (!rasterizer_state || !depth_state || !blend_state)
{
SAFE_RELEASE(rasterizer_state);
SAFE_RELEASE(depth_state);
SAFE_RELEASE(blend_state);
return nullptr;
}

const DXShader* vertex_shader = static_cast<const DXShader*>(config.vertex_shader);
const DXShader* geometry_shader = static_cast<const DXShader*>(config.geometry_shader);
@@ -20,27 +20,27 @@ class DXPipeline final : public AbstractPipeline
bool use_logic_op);
~DXPipeline() override;

ID3D11InputLayout* GetInputLayout() const { return m_input_layout; }
ID3D11VertexShader* GetVertexShader() const { return m_vertex_shader; }
ID3D11GeometryShader* GetGeometryShader() const { return m_geometry_shader; }
ID3D11PixelShader* GetPixelShader() const { return m_pixel_shader; }
ID3D11RasterizerState* GetRasterizerState() const { return m_rasterizer_state; }
ID3D11DepthStencilState* GetDepthState() const { return m_depth_state; }
ID3D11BlendState* GetBlendState() const { return m_blend_state; }
ID3D11InputLayout* GetInputLayout() const { return m_input_layout.Get(); }
ID3D11VertexShader* GetVertexShader() const { return m_vertex_shader.Get(); }
ID3D11GeometryShader* GetGeometryShader() const { return m_geometry_shader.Get(); }
ID3D11PixelShader* GetPixelShader() const { return m_pixel_shader.Get(); }
ID3D11RasterizerState* GetRasterizerState() const { return m_rasterizer_state.Get(); }
ID3D11DepthStencilState* GetDepthState() const { return m_depth_state.Get(); }
ID3D11BlendState* GetBlendState() const { return m_blend_state.Get(); }
D3D11_PRIMITIVE_TOPOLOGY GetPrimitiveTopology() const { return m_primitive_topology; }
bool HasGeometryShader() const { return m_geometry_shader != nullptr; }
bool UseLogicOp() const { return m_use_logic_op; }

static std::unique_ptr<DXPipeline> Create(const AbstractPipelineConfig& config);

private:
ID3D11InputLayout* m_input_layout;
ID3D11VertexShader* m_vertex_shader;
ID3D11GeometryShader* m_geometry_shader;
ID3D11PixelShader* m_pixel_shader;
ID3D11RasterizerState* m_rasterizer_state;
ID3D11DepthStencilState* m_depth_state;
ID3D11BlendState* m_blend_state;
ComPtr<ID3D11InputLayout> m_input_layout;
ComPtr<ID3D11VertexShader> m_vertex_shader;
ComPtr<ID3D11GeometryShader> m_geometry_shader;
ComPtr<ID3D11PixelShader> m_pixel_shader;
ComPtr<ID3D11RasterizerState> m_rasterizer_state;
ComPtr<ID3D11DepthStencilState> m_depth_state;
ComPtr<ID3D11BlendState> m_blend_state;
D3D11_PRIMITIVE_TOPOLOGY m_primitive_topology;
bool m_use_logic_op;
};

0 comments on commit 1151a12

Please sign in to comment.
You can’t perform that action at this time.