2014-12-05 02:01:20 +00:00
|
|
|
// Copyright 2014 Dolphin Emulator Project
|
|
|
|
// Licensed under GPLv2+
|
|
|
|
// Refer to the license.txt file included.
|
|
|
|
|
2021-04-17 17:14:43 +00:00
|
|
|
#include <algorithm>
|
|
|
|
#include <array>
|
|
|
|
|
2016-01-17 21:54:31 +00:00
|
|
|
#include "Common/CommonTypes.h"
|
|
|
|
#include "Common/MsgHandler.h"
|
2021-04-17 17:14:43 +00:00
|
|
|
#include "VideoBackends/D3D/D3DBoundingBox.h"
|
2019-02-15 01:59:50 +00:00
|
|
|
#include "VideoBackends/D3D/D3DState.h"
|
2020-09-15 13:07:22 +00:00
|
|
|
#include "VideoBackends/D3DCommon/D3DCommon.h"
|
2014-12-05 02:01:20 +00:00
|
|
|
#include "VideoCommon/VideoConfig.h"
|
|
|
|
|
|
|
|
namespace DX11
|
|
|
|
{
|
2021-04-17 17:14:43 +00:00
|
|
|
static constexpr u32 NUM_BBOX_VALUES = 4;
|
2019-03-09 13:31:37 +00:00
|
|
|
static ComPtr<ID3D11Buffer> s_bbox_buffer;
|
|
|
|
static ComPtr<ID3D11Buffer> s_bbox_staging_buffer;
|
|
|
|
static ComPtr<ID3D11UnorderedAccessView> s_bbox_uav;
|
2021-04-17 17:14:43 +00:00
|
|
|
static std::array<s32, NUM_BBOX_VALUES> s_bbox_values;
|
|
|
|
static std::array<bool, NUM_BBOX_VALUES> s_bbox_dirty;
|
|
|
|
static bool s_bbox_valid = false;
|
2014-12-05 02:01:20 +00:00
|
|
|
|
2019-03-09 13:31:37 +00:00
|
|
|
ID3D11UnorderedAccessView* BBox::GetUAV()
|
2014-12-05 02:01:20 +00:00
|
|
|
{
|
2019-03-09 13:31:37 +00:00
|
|
|
return s_bbox_uav.Get();
|
2014-12-05 02:01:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void BBox::Init()
|
|
|
|
{
|
2021-04-17 17:14:43 +00:00
|
|
|
if (!g_ActiveConfig.backend_info.bSupportsBBox)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Create 2 buffers here.
|
|
|
|
// First for unordered access on default pool.
|
|
|
|
auto desc = CD3D11_BUFFER_DESC(NUM_BBOX_VALUES * sizeof(s32), D3D11_BIND_UNORDERED_ACCESS,
|
|
|
|
D3D11_USAGE_DEFAULT, 0, 0, sizeof(s32));
|
|
|
|
const s32 initial_values[NUM_BBOX_VALUES] = {0, 0, 0, 0};
|
|
|
|
D3D11_SUBRESOURCE_DATA data;
|
|
|
|
data.pSysMem = initial_values;
|
|
|
|
data.SysMemPitch = NUM_BBOX_VALUES * sizeof(s32);
|
|
|
|
data.SysMemSlicePitch = 0;
|
|
|
|
HRESULT hr;
|
|
|
|
hr = D3D::device->CreateBuffer(&desc, &data, &s_bbox_buffer);
|
|
|
|
CHECK(SUCCEEDED(hr), "Create 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.Get(), "BoundingBox Staging Buffer");
|
|
|
|
|
|
|
|
// UAV is required to allow concurrent access.
|
|
|
|
D3D11_UNORDERED_ACCESS_VIEW_DESC UAVdesc = {};
|
|
|
|
UAVdesc.Format = DXGI_FORMAT_R32_SINT;
|
|
|
|
UAVdesc.ViewDimension = D3D11_UAV_DIMENSION_BUFFER;
|
|
|
|
UAVdesc.Buffer.FirstElement = 0;
|
|
|
|
UAVdesc.Buffer.Flags = 0;
|
|
|
|
UAVdesc.Buffer.NumElements = NUM_BBOX_VALUES;
|
|
|
|
hr = D3D::device->CreateUnorderedAccessView(s_bbox_buffer.Get(), &UAVdesc, &s_bbox_uav);
|
|
|
|
CHECK(SUCCEEDED(hr), "Create BoundingBox UAV.");
|
|
|
|
D3DCommon::SetDebugObjectName(s_bbox_uav.Get(), "BoundingBox UAV");
|
|
|
|
D3D::stateman->SetOMUAV(s_bbox_uav.Get());
|
|
|
|
|
|
|
|
s_bbox_dirty = {};
|
|
|
|
s_bbox_valid = true;
|
2014-12-05 02:01:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void BBox::Shutdown()
|
|
|
|
{
|
2019-03-09 13:31:37 +00:00
|
|
|
s_bbox_uav.Reset();
|
|
|
|
s_bbox_staging_buffer.Reset();
|
|
|
|
s_bbox_buffer.Reset();
|
2014-12-05 02:01:20 +00:00
|
|
|
}
|
|
|
|
|
2021-04-17 17:14:43 +00:00
|
|
|
void BBox::Flush()
|
2014-12-05 02:01:20 +00:00
|
|
|
{
|
2021-04-17 17:14:43 +00:00
|
|
|
s_bbox_valid = false;
|
|
|
|
|
|
|
|
if (std::none_of(s_bbox_dirty.begin(), s_bbox_dirty.end(), [](bool dirty) { return dirty; }))
|
|
|
|
return;
|
|
|
|
|
|
|
|
for (u32 start = 0; start < NUM_BBOX_VALUES;)
|
|
|
|
{
|
|
|
|
if (!s_bbox_dirty[start])
|
|
|
|
{
|
|
|
|
start++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
u32 end = start + 1;
|
|
|
|
s_bbox_dirty[start] = false;
|
|
|
|
for (; end < NUM_BBOX_VALUES; end++)
|
|
|
|
{
|
|
|
|
if (!s_bbox_dirty[end])
|
|
|
|
break;
|
|
|
|
|
|
|
|
s_bbox_dirty[end] = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
D3D11_BOX box{start * sizeof(s32), 0, 0, end * sizeof(s32), 1, 1};
|
|
|
|
D3D::context->UpdateSubresource(s_bbox_buffer.Get(), 0, &box, &s_bbox_values[start], 0, 0);
|
|
|
|
}
|
2014-12-05 02:01:20 +00:00
|
|
|
}
|
|
|
|
|
2021-04-17 17:14:43 +00:00
|
|
|
void BBox::Readback()
|
2014-12-05 02:01:20 +00:00
|
|
|
{
|
2019-03-09 13:31:37 +00:00
|
|
|
D3D::context->CopyResource(s_bbox_staging_buffer.Get(), s_bbox_buffer.Get());
|
2021-04-17 17:14:43 +00:00
|
|
|
|
2014-12-05 02:01:20 +00:00
|
|
|
D3D11_MAPPED_SUBRESOURCE map;
|
2019-03-09 13:31:37 +00:00
|
|
|
HRESULT hr = D3D::context->Map(s_bbox_staging_buffer.Get(), 0, D3D11_MAP_READ, 0, &map);
|
2014-12-05 02:01:20 +00:00
|
|
|
if (SUCCEEDED(hr))
|
|
|
|
{
|
2021-04-17 17:14:43 +00:00
|
|
|
for (u32 i = 0; i < NUM_BBOX_VALUES; i++)
|
|
|
|
{
|
|
|
|
if (!s_bbox_dirty[i])
|
|
|
|
{
|
|
|
|
std::memcpy(&s_bbox_values[i], reinterpret_cast<const u8*>(map.pData) + sizeof(s32) * i,
|
|
|
|
sizeof(s32));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
D3D::context->Unmap(s_bbox_staging_buffer.Get(), 0);
|
2014-12-05 02:01:20 +00:00
|
|
|
}
|
2021-04-17 17:14:43 +00:00
|
|
|
|
|
|
|
s_bbox_valid = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void BBox::Set(int index, int value)
|
|
|
|
{
|
|
|
|
if (s_bbox_valid && s_bbox_values[index] == value)
|
|
|
|
return;
|
|
|
|
|
|
|
|
s_bbox_values[index] = value;
|
|
|
|
s_bbox_dirty[index] = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
int BBox::Get(int index)
|
|
|
|
{
|
|
|
|
if (!s_bbox_valid)
|
|
|
|
Readback();
|
|
|
|
|
|
|
|
return s_bbox_values[index];
|
2014-12-05 02:01:20 +00:00
|
|
|
}
|
2019-02-15 01:59:50 +00:00
|
|
|
}; // namespace DX11
|