Skip to content

Commit

Permalink
Merge pull request #6004 from lioncash/d3d
Browse files Browse the repository at this point in the history
D3D: Eliminate redundant ID3D11DeviceChild* casts
  • Loading branch information
stenzek committed Sep 3, 2017
2 parents 005d0b3 + 64de8a9 commit ce59121
Show file tree
Hide file tree
Showing 12 changed files with 85 additions and 104 deletions.
33 changes: 28 additions & 5 deletions Source/Core/VideoBackends/D3D/D3DBase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -386,7 +386,7 @@ HRESULT Create(HWND wnd)
MessageBox(wnd, _T("Failed to associate the window"), _T("Dolphin Direct3D 11 backend"),
MB_OK | MB_ICONERROR);

SetDebugObjectName((ID3D11DeviceChild*)context, "device context");
SetDebugObjectName(context, "device context");
SAFE_RELEASE(factory);
SAFE_RELEASE(adapter);

Expand All @@ -410,8 +410,8 @@ HRESULT Create(HWND wnd)
backbuf = new D3DTexture2D(buf, D3D11_BIND_RENDER_TARGET);
SAFE_RELEASE(buf);
CHECK(backbuf != nullptr, "Create back buffer texture");
SetDebugObjectName((ID3D11DeviceChild*)backbuf->GetTex(), "backbuffer texture");
SetDebugObjectName((ID3D11DeviceChild*)backbuf->GetRTV(), "backbuffer render target view");
SetDebugObjectName(backbuf->GetTex(), "backbuffer texture");
SetDebugObjectName(backbuf->GetRTV(), "backbuffer render target view");

context->OMSetRenderTargets(1, &backbuf->GetRTV(), nullptr);

Expand Down Expand Up @@ -571,8 +571,8 @@ void Reset()
backbuf = new D3DTexture2D(buf, D3D11_BIND_RENDER_TARGET);
SAFE_RELEASE(buf);
CHECK(backbuf != nullptr, "Create back buffer texture");
SetDebugObjectName((ID3D11DeviceChild*)backbuf->GetTex(), "backbuffer texture");
SetDebugObjectName((ID3D11DeviceChild*)backbuf->GetRTV(), "backbuffer render target view");
SetDebugObjectName(backbuf->GetTex(), "backbuffer texture");
SetDebugObjectName(backbuf->GetRTV(), "backbuffer render target view");
}

bool BeginFrame()
Expand Down Expand Up @@ -618,6 +618,29 @@ bool GetFullscreenState()
return !!state;
}

void SetDebugObjectName(ID3D11DeviceChild* resource, const char* name)
{
#if defined(_DEBUG) || defined(DEBUGFAST)
if (resource)
resource->SetPrivateData(WKPDID_D3DDebugObjectName, (UINT)(name ? strlen(name) : 0), name);
#endif
}

std::string GetDebugObjectName(ID3D11DeviceChild* resource)
{
std::string name;
#if defined(_DEBUG) || defined(DEBUGFAST)
if (resource)
{
UINT size = 0;
resource->GetPrivateData(WKPDID_D3DDebugObjectName, &size, nullptr); // get required size
name.resize(size);
resource->GetPrivateData(WKPDID_D3DDebugObjectName, &size, const_cast<char*>(name.data()));
}
#endif
return name;
}

} // namespace D3D

} // namespace DX11
30 changes: 2 additions & 28 deletions Source/Core/VideoBackends/D3D/D3DBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -81,34 +81,8 @@ bool GetFullscreenState();
// This function will assign a name to the given resource.
// The DirectX debug layer will make it easier to identify resources that way,
// e.g. when listing up all resources who have unreleased references.
template <typename T>
void SetDebugObjectName(T resource, const char* name)
{
static_assert(std::is_convertible<T, ID3D11DeviceChild*>::value,
"resource must be convertible to ID3D11DeviceChild*");
#if defined(_DEBUG) || defined(DEBUGFAST)
if (resource)
resource->SetPrivateData(WKPDID_D3DDebugObjectName, (UINT)(name ? strlen(name) : 0), name);
#endif
}

template <typename T>
std::string GetDebugObjectName(T resource)
{
static_assert(std::is_convertible<T, ID3D11DeviceChild*>::value,
"resource must be convertible to ID3D11DeviceChild*");
std::string name;
#if defined(_DEBUG) || defined(DEBUGFAST)
if (resource)
{
UINT size = 0;
resource->GetPrivateData(WKPDID_D3DDebugObjectName, &size, nullptr); // get required size
name.resize(size);
resource->GetPrivateData(WKPDID_D3DDebugObjectName, &size, const_cast<char*>(name.data()));
}
#endif
return name;
}
void SetDebugObjectName(ID3D11DeviceChild* resource, const char* name);
std::string GetDebugObjectName(ID3D11DeviceChild* resource);

} // namespace D3D

Expand Down
10 changes: 4 additions & 6 deletions Source/Core/VideoBackends/D3D/D3DState.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,7 @@ ID3D11SamplerState* StateCache::Get(SamplerState state)
if (FAILED(hr))
PanicAlert("Fail %s %d\n", __FILE__, __LINE__);

D3D::SetDebugObjectName((ID3D11DeviceChild*)res, "sampler state used to emulate the GX pipeline");
D3D::SetDebugObjectName(res, "sampler state used to emulate the GX pipeline");
m_sampler.emplace(state.packed, res);

return res;
Expand Down Expand Up @@ -414,7 +414,7 @@ ID3D11BlendState* StateCache::Get(BlendState state)
if (FAILED(hr))
PanicAlert("Failed to create blend state at %s %d\n", __FILE__, __LINE__);

D3D::SetDebugObjectName((ID3D11DeviceChild*)res, "blend state used to emulate the GX pipeline");
D3D::SetDebugObjectName(res, "blend state used to emulate the GX pipeline");
m_blend.emplace(state.packed, res);

return res;
Expand All @@ -436,8 +436,7 @@ ID3D11RasterizerState* StateCache::Get(RasterizerState state)
if (FAILED(hr))
PanicAlert("Failed to create rasterizer state at %s %d\n", __FILE__, __LINE__);

D3D::SetDebugObjectName((ID3D11DeviceChild*)res,
"rasterizer state used to emulate the GX pipeline");
D3D::SetDebugObjectName(res, "rasterizer state used to emulate the GX pipeline");
m_raster.emplace(state.packed, res);

return res;
Expand Down Expand Up @@ -482,8 +481,7 @@ ID3D11DepthStencilState* StateCache::Get(ZMode state)

HRESULT hr = D3D::device->CreateDepthStencilState(&depthdc, &res);
if (SUCCEEDED(hr))
D3D::SetDebugObjectName((ID3D11DeviceChild*)res,
"depth-stencil state used to emulate the GX pipeline");
D3D::SetDebugObjectName(res, "depth-stencil state used to emulate the GX pipeline");
else
PanicAlert("Failed to create depth state at %s %d\n", __FILE__, __LINE__);

Expand Down
16 changes: 8 additions & 8 deletions Source/Core/VideoBackends/D3D/D3DUtil.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ int CD3DFont::Init()
PanicAlert("Failed to create font texture");
return hr;
}
D3D::SetDebugObjectName((ID3D11DeviceChild*)buftex, "texture of a CD3DFont object");
D3D::SetDebugObjectName(buftex, "texture of a CD3DFont object");

// Lock the surface and write the alpha values for the set pixels
D3D11_MAPPED_SUBRESOURCE texmap;
Expand Down Expand Up @@ -284,7 +284,7 @@ int CD3DFont::Init()
m_pshader = D3D::CompileAndCreatePixelShader(fontpixshader);
if (m_pshader == nullptr)
PanicAlert("Failed to create pixel shader, %s %d\n", __FILE__, __LINE__);
D3D::SetDebugObjectName((ID3D11DeviceChild*)m_pshader, "pixel shader of a CD3DFont object");
D3D::SetDebugObjectName(m_pshader, "pixel shader of a CD3DFont object");

D3DBlob* vsbytecode;
D3D::CompileVertexShader(fontvertshader, &vsbytecode);
Expand All @@ -293,7 +293,7 @@ int CD3DFont::Init()
m_vshader = D3D::CreateVertexShaderFromByteCode(vsbytecode);
if (m_vshader == nullptr)
PanicAlert("Failed to create vertex shader, %s %d\n", __FILE__, __LINE__);
D3D::SetDebugObjectName((ID3D11DeviceChild*)m_vshader, "vertex shader of a CD3DFont object");
D3D::SetDebugObjectName(m_vshader, "vertex shader of a CD3DFont object");

const D3D11_INPUT_ELEMENT_DESC desc[] = {
{"POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0},
Expand All @@ -319,13 +319,13 @@ int CD3DFont::Init()
blenddesc.RenderTarget[0].BlendOpAlpha = D3D11_BLEND_OP_ADD;
hr = D3D::device->CreateBlendState(&blenddesc, &m_blendstate);
CHECK(hr == S_OK, "Create font blend state");
D3D::SetDebugObjectName((ID3D11DeviceChild*)m_blendstate, "blend state of a CD3DFont object");
D3D::SetDebugObjectName(m_blendstate, "blend state of a CD3DFont object");

D3D11_RASTERIZER_DESC rastdesc = CD3D11_RASTERIZER_DESC(D3D11_FILL_SOLID, D3D11_CULL_NONE, false,
0, 0.f, 0.f, false, false, false, false);
hr = D3D::device->CreateRasterizerState(&rastdesc, &m_raststate);
CHECK(hr == S_OK, "Create font rasterizer state");
D3D::SetDebugObjectName((ID3D11DeviceChild*)m_raststate, "rasterizer state of a CD3DFont object");
D3D::SetDebugObjectName(m_raststate, "rasterizer state of a CD3DFont object");

D3D11_BUFFER_DESC vbdesc =
CD3D11_BUFFER_DESC(MAX_NUM_VERTICES * sizeof(FONT2DVERTEX), D3D11_BIND_VERTEX_BUFFER,
Expand All @@ -335,7 +335,7 @@ int CD3DFont::Init()
PanicAlert("Failed to create font vertex buffer at %s, line %d\n", __FILE__, __LINE__);
return hr;
}
D3D::SetDebugObjectName((ID3D11DeviceChild*)m_pVB, "vertex buffer of a CD3DFont object");
D3D::SetDebugObjectName(m_pVB, "vertex buffer of a CD3DFont object");
return S_OK;
}

Expand Down Expand Up @@ -521,7 +521,7 @@ void InitUtils()
if (FAILED(hr))
PanicAlert("Failed to create sampler state at %s %d\n", __FILE__, __LINE__);
else
SetDebugObjectName((ID3D11DeviceChild*)point_copy_sampler, "point copy sampler state");
SetDebugObjectName(point_copy_sampler, "point copy sampler state");

samDesc = CD3D11_SAMPLER_DESC(D3D11_FILTER_MIN_MAG_MIP_LINEAR, D3D11_TEXTURE_ADDRESS_BORDER,
D3D11_TEXTURE_ADDRESS_BORDER, D3D11_TEXTURE_ADDRESS_BORDER, 0.f, 1,
Expand All @@ -530,7 +530,7 @@ void InitUtils()
if (FAILED(hr))
PanicAlert("Failed to create sampler state at %s %d\n", __FILE__, __LINE__);
else
SetDebugObjectName((ID3D11DeviceChild*)linear_copy_sampler, "linear copy sampler state");
SetDebugObjectName(linear_copy_sampler, "linear copy sampler state");

// cached data used to avoid unnecessarily reloading the vertex buffers
memset(&tex_quad_data, 0, sizeof(tex_quad_data));
Expand Down
5 changes: 2 additions & 3 deletions Source/Core/VideoBackends/D3D/DXTexture.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,8 @@ DXTexture::DXTexture(const TextureConfig& tex_config) : AbstractTexture(tex_conf
m_texture = new D3DTexture2D(pTexture, D3D11_BIND_SHADER_RESOURCE);

// TODO: better debug names
D3D::SetDebugObjectName((ID3D11DeviceChild*)m_texture->GetTex(),
"a texture of the TextureCache");
D3D::SetDebugObjectName((ID3D11DeviceChild*)m_texture->GetSRV(),
D3D::SetDebugObjectName(m_texture->GetTex(), "a texture of the TextureCache");
D3D::SetDebugObjectName(m_texture->GetSRV(),
"shader resource view of a texture of the TextureCache");

SAFE_RELEASE(pTexture);
Expand Down
45 changes: 19 additions & 26 deletions Source/Core/VideoBackends/D3D/FramebufferManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -125,11 +125,9 @@ FramebufferManager::FramebufferManager(int target_width, int target_height)
DXGI_FORMAT_R8G8B8A8_UNORM, DXGI_FORMAT_UNKNOWN, DXGI_FORMAT_R8G8B8A8_UNORM,
(sample_desc.Count > 1));
SAFE_RELEASE(buf);
D3D::SetDebugObjectName((ID3D11DeviceChild*)m_efb.color_tex->GetTex(), "EFB color texture");
D3D::SetDebugObjectName((ID3D11DeviceChild*)m_efb.color_tex->GetSRV(),
"EFB color texture shader resource view");
D3D::SetDebugObjectName((ID3D11DeviceChild*)m_efb.color_tex->GetRTV(),
"EFB color texture render target view");
D3D::SetDebugObjectName(m_efb.color_tex->GetTex(), "EFB color texture");
D3D::SetDebugObjectName(m_efb.color_tex->GetSRV(), "EFB color texture shader resource view");
D3D::SetDebugObjectName(m_efb.color_tex->GetRTV(), "EFB color texture render target view");

// Temporary EFB color texture - used in ReinterpretPixelData
texdesc =
Expand All @@ -144,11 +142,10 @@ FramebufferManager::FramebufferManager(int target_width, int target_height)
DXGI_FORMAT_R8G8B8A8_UNORM, DXGI_FORMAT_UNKNOWN, DXGI_FORMAT_R8G8B8A8_UNORM,
(sample_desc.Count > 1));
SAFE_RELEASE(buf);
D3D::SetDebugObjectName((ID3D11DeviceChild*)m_efb.color_temp_tex->GetTex(),
"EFB color temp texture");
D3D::SetDebugObjectName((ID3D11DeviceChild*)m_efb.color_temp_tex->GetSRV(),
D3D::SetDebugObjectName(m_efb.color_temp_tex->GetTex(), "EFB color temp texture");
D3D::SetDebugObjectName(m_efb.color_temp_tex->GetSRV(),
"EFB color temp texture shader resource view");
D3D::SetDebugObjectName((ID3D11DeviceChild*)m_efb.color_temp_tex->GetRTV(),
D3D::SetDebugObjectName(m_efb.color_temp_tex->GetRTV(),
"EFB color temp texture render target view");

// Render buffer for AccessEFB (color data)
Expand All @@ -157,18 +154,18 @@ FramebufferManager::FramebufferManager(int target_width, int target_height)
CHECK(hr == S_OK, "create EFB color read texture (hr=%#x)", hr);
m_efb.color_read_texture = new D3DTexture2D(buf, D3D11_BIND_RENDER_TARGET);
SAFE_RELEASE(buf);
D3D::SetDebugObjectName((ID3D11DeviceChild*)m_efb.color_read_texture->GetTex(),
D3D::SetDebugObjectName(m_efb.color_read_texture->GetTex(),
"EFB color read texture (used in Renderer::AccessEFB)");
D3D::SetDebugObjectName(
(ID3D11DeviceChild*)m_efb.color_read_texture->GetRTV(),
m_efb.color_read_texture->GetRTV(),
"EFB color read texture render target view (used in Renderer::AccessEFB)");

// AccessEFB - Sysmem buffer used to retrieve the pixel data from depth_read_texture
texdesc = CD3D11_TEXTURE2D_DESC(DXGI_FORMAT_R8G8B8A8_UNORM, 1, 1, 1, 1, 0, D3D11_USAGE_STAGING,
D3D11_CPU_ACCESS_READ);
hr = D3D::device->CreateTexture2D(&texdesc, nullptr, &m_efb.color_staging_buf);
CHECK(hr == S_OK, "create EFB color staging buffer (hr=%#x)", hr);
D3D::SetDebugObjectName((ID3D11DeviceChild*)m_efb.color_staging_buf,
D3D::SetDebugObjectName(m_efb.color_staging_buf,
"EFB color staging texture (used for Renderer::AccessEFB)");

// EFB depth buffer - primary depth buffer
Expand All @@ -183,30 +180,28 @@ FramebufferManager::FramebufferManager(int target_width, int target_height)
buf, (D3D11_BIND_FLAG)(D3D11_BIND_DEPTH_STENCIL | D3D11_BIND_SHADER_RESOURCE),
DXGI_FORMAT_R32_FLOAT, DXGI_FORMAT_D32_FLOAT, DXGI_FORMAT_UNKNOWN, (sample_desc.Count > 1));
SAFE_RELEASE(buf);
D3D::SetDebugObjectName((ID3D11DeviceChild*)m_efb.depth_tex->GetTex(), "EFB depth texture");
D3D::SetDebugObjectName((ID3D11DeviceChild*)m_efb.depth_tex->GetDSV(),
"EFB depth texture depth stencil view");
D3D::SetDebugObjectName((ID3D11DeviceChild*)m_efb.depth_tex->GetSRV(),
"EFB depth texture shader resource view");
D3D::SetDebugObjectName(m_efb.depth_tex->GetTex(), "EFB depth texture");
D3D::SetDebugObjectName(m_efb.depth_tex->GetDSV(), "EFB depth texture depth stencil view");
D3D::SetDebugObjectName(m_efb.depth_tex->GetSRV(), "EFB depth texture shader resource view");

// Render buffer for AccessEFB (depth data)
texdesc = CD3D11_TEXTURE2D_DESC(DXGI_FORMAT_R32_FLOAT, 1, 1, 1, 1, D3D11_BIND_RENDER_TARGET);
hr = D3D::device->CreateTexture2D(&texdesc, nullptr, &buf);
CHECK(hr == S_OK, "create EFB depth read texture (hr=%#x)", hr);
m_efb.depth_read_texture = new D3DTexture2D(buf, D3D11_BIND_RENDER_TARGET);
SAFE_RELEASE(buf);
D3D::SetDebugObjectName((ID3D11DeviceChild*)m_efb.depth_read_texture->GetTex(),
D3D::SetDebugObjectName(m_efb.depth_read_texture->GetTex(),
"EFB depth read texture (used in Renderer::AccessEFB)");
D3D::SetDebugObjectName(
(ID3D11DeviceChild*)m_efb.depth_read_texture->GetRTV(),
m_efb.depth_read_texture->GetRTV(),
"EFB depth read texture render target view (used in Renderer::AccessEFB)");

// AccessEFB - Sysmem buffer used to retrieve the pixel data from depth_read_texture
texdesc = CD3D11_TEXTURE2D_DESC(DXGI_FORMAT_R32_FLOAT, 1, 1, 1, 1, 0, D3D11_USAGE_STAGING,
D3D11_CPU_ACCESS_READ);
hr = D3D::device->CreateTexture2D(&texdesc, nullptr, &m_efb.depth_staging_buf);
CHECK(hr == S_OK, "create EFB depth staging buffer (hr=%#x)", hr);
D3D::SetDebugObjectName((ID3D11DeviceChild*)m_efb.depth_staging_buf,
D3D::SetDebugObjectName(m_efb.depth_staging_buf,
"EFB depth staging texture (used for Renderer::AccessEFB)");

if (g_ActiveConfig.iMultisamples > 1)
Expand All @@ -221,9 +216,8 @@ FramebufferManager::FramebufferManager(int target_width, int target_height)
m_efb.resolved_color_tex =
new D3DTexture2D(buf, D3D11_BIND_SHADER_RESOURCE, DXGI_FORMAT_R8G8B8A8_UNORM);
SAFE_RELEASE(buf);
D3D::SetDebugObjectName((ID3D11DeviceChild*)m_efb.resolved_color_tex->GetTex(),
"EFB color resolve texture");
D3D::SetDebugObjectName((ID3D11DeviceChild*)m_efb.resolved_color_tex->GetSRV(),
D3D::SetDebugObjectName(m_efb.resolved_color_tex->GetTex(), "EFB color resolve texture");
D3D::SetDebugObjectName(m_efb.resolved_color_tex->GetSRV(),
"EFB color resolve texture shader resource view");

texdesc =
Expand All @@ -236,9 +230,8 @@ FramebufferManager::FramebufferManager(int target_width, int target_height)
buf, (D3D11_BIND_FLAG)(D3D11_BIND_SHADER_RESOURCE | D3D11_BIND_RENDER_TARGET),
DXGI_FORMAT_R32_FLOAT, DXGI_FORMAT_UNKNOWN, DXGI_FORMAT_R32_FLOAT);
SAFE_RELEASE(buf);
D3D::SetDebugObjectName((ID3D11DeviceChild*)m_efb.resolved_depth_tex->GetTex(),
"EFB depth resolve texture");
D3D::SetDebugObjectName((ID3D11DeviceChild*)m_efb.resolved_depth_tex->GetSRV(),
D3D::SetDebugObjectName(m_efb.resolved_depth_tex->GetTex(), "EFB depth resolve texture");
D3D::SetDebugObjectName(m_efb.resolved_depth_tex->GetSRV(),
"EFB depth resolve texture shader resource view");
}
else
Expand Down
6 changes: 3 additions & 3 deletions Source/Core/VideoBackends/D3D/GeometryShaderCache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -143,18 +143,18 @@ void GeometryShaderCache::Init()
D3D11_USAGE_DYNAMIC, D3D11_CPU_ACCESS_WRITE);
HRESULT hr = D3D::device->CreateBuffer(&gbdesc, nullptr, &gscbuf);
CHECK(hr == S_OK, "Create geometry shader constant buffer (size=%u)", gbsize);
D3D::SetDebugObjectName((ID3D11DeviceChild*)gscbuf,
D3D::SetDebugObjectName(gscbuf,
"geometry shader constant buffer used to emulate the GX pipeline");

// used when drawing clear quads
ClearGeometryShader = D3D::CompileAndCreateGeometryShader(clear_shader_code);
CHECK(ClearGeometryShader != nullptr, "Create clear geometry shader");
D3D::SetDebugObjectName((ID3D11DeviceChild*)ClearGeometryShader, "clear geometry shader");
D3D::SetDebugObjectName(ClearGeometryShader, "clear geometry shader");

// used for buffer copy
CopyGeometryShader = D3D::CompileAndCreateGeometryShader(copy_shader_code);
CHECK(CopyGeometryShader != nullptr, "Create copy geometry shader");
D3D::SetDebugObjectName((ID3D11DeviceChild*)CopyGeometryShader, "copy geometry shader");
D3D::SetDebugObjectName(CopyGeometryShader, "copy geometry shader");

Clear();

Expand Down
3 changes: 1 addition & 2 deletions Source/Core/VideoBackends/D3D/NativeVertexFormat.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -130,8 +130,7 @@ void D3DVertexFormat::SetInputLayout(D3DBlob* vs_bytecode)
m_elems.data(), m_num_elems, vs_bytecode->Data(), vs_bytecode->Size(), &m_layout);
if (FAILED(hr))
PanicAlert("Failed to create input layout, %s %d\n", __FILE__, __LINE__);
DX11::D3D::SetDebugObjectName((ID3D11DeviceChild*)m_layout,
"input layout used to emulate the GX pipeline");
DX11::D3D::SetDebugObjectName(m_layout, "input layout used to emulate the GX pipeline");
}
DX11::D3D::stateman->SetInputLayout(m_layout);
}
Expand Down

0 comments on commit ce59121

Please sign in to comment.