2014-12-05 02:01:20 +00:00
|
|
|
// Copyright 2014 Dolphin Emulator Project
|
2021-07-05 01:22:19 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
2014-12-05 02:01:20 +00:00
|
|
|
|
2021-12-10 02:22:16 +00:00
|
|
|
#include "VideoBackends/D3D/D3DBoundingBox.h"
|
|
|
|
|
2021-04-17 17:14:43 +00:00
|
|
|
#include <algorithm>
|
|
|
|
#include <array>
|
|
|
|
|
2021-11-10 23:55:33 +00:00
|
|
|
#include "Common/Assert.h"
|
2016-01-17 21:54:31 +00:00
|
|
|
#include "Common/CommonTypes.h"
|
|
|
|
#include "Common/MsgHandler.h"
|
2021-11-10 23:55:33 +00:00
|
|
|
|
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
|
|
|
|
|
|
|
namespace DX11
|
|
|
|
{
|
2021-06-09 11:42:21 +00:00
|
|
|
D3DBoundingBox::~D3DBoundingBox()
|
2014-12-05 02:01:20 +00:00
|
|
|
{
|
2021-06-09 11:42:21 +00:00
|
|
|
m_uav.Reset();
|
|
|
|
m_staging_buffer.Reset();
|
|
|
|
m_buffer.Reset();
|
2014-12-05 02:01:20 +00:00
|
|
|
}
|
|
|
|
|
2021-06-09 11:42:21 +00:00
|
|
|
bool D3DBoundingBox::Initialize()
|
2014-12-05 02:01:20 +00:00
|
|
|
{
|
2021-04-17 17:14:43 +00:00
|
|
|
// Create 2 buffers here.
|
|
|
|
// First for unordered access on default pool.
|
2021-06-09 11:42:21 +00:00
|
|
|
auto desc = CD3D11_BUFFER_DESC(NUM_BBOX_VALUES * sizeof(BBoxType), D3D11_BIND_UNORDERED_ACCESS,
|
2022-05-22 19:28:12 +00:00
|
|
|
D3D11_USAGE_DEFAULT, 0, D3D11_RESOURCE_MISC_BUFFER_ALLOW_RAW_VIEWS,
|
|
|
|
sizeof(BBoxType));
|
2021-06-09 11:42:21 +00:00
|
|
|
const BBoxType initial_values[NUM_BBOX_VALUES] = {0, 0, 0, 0};
|
2021-04-17 17:14:43 +00:00
|
|
|
D3D11_SUBRESOURCE_DATA data;
|
|
|
|
data.pSysMem = initial_values;
|
2021-06-09 11:42:21 +00:00
|
|
|
data.SysMemPitch = NUM_BBOX_VALUES * sizeof(BBoxType);
|
2021-04-17 17:14:43 +00:00
|
|
|
data.SysMemSlicePitch = 0;
|
|
|
|
HRESULT hr;
|
2021-06-09 11:42:21 +00:00
|
|
|
hr = D3D::device->CreateBuffer(&desc, &data, &m_buffer);
|
2021-12-12 20:50:13 +00:00
|
|
|
ASSERT_MSG(VIDEO, SUCCEEDED(hr), "Failed to create BoundingBox Buffer: {}", DX11HRWrap(hr));
|
2021-06-09 11:42:21 +00:00
|
|
|
if (FAILED(hr))
|
|
|
|
return false;
|
|
|
|
D3DCommon::SetDebugObjectName(m_buffer.Get(), "BoundingBox Buffer");
|
2021-04-17 17:14:43 +00:00
|
|
|
|
|
|
|
// Second to use as a staging buffer.
|
|
|
|
desc.Usage = D3D11_USAGE_STAGING;
|
|
|
|
desc.CPUAccessFlags = D3D11_CPU_ACCESS_READ;
|
|
|
|
desc.BindFlags = 0;
|
2022-05-22 19:28:12 +00:00
|
|
|
desc.MiscFlags = 0;
|
2021-06-09 11:42:21 +00:00
|
|
|
hr = D3D::device->CreateBuffer(&desc, nullptr, &m_staging_buffer);
|
2021-12-12 20:50:13 +00:00
|
|
|
ASSERT_MSG(VIDEO, SUCCEEDED(hr), "Failed to create BoundingBox Staging Buffer: {}",
|
|
|
|
DX11HRWrap(hr));
|
2021-06-09 11:42:21 +00:00
|
|
|
if (FAILED(hr))
|
|
|
|
return false;
|
|
|
|
D3DCommon::SetDebugObjectName(m_staging_buffer.Get(), "BoundingBox Staging Buffer");
|
2021-04-17 17:14:43 +00:00
|
|
|
|
|
|
|
// UAV is required to allow concurrent access.
|
|
|
|
D3D11_UNORDERED_ACCESS_VIEW_DESC UAVdesc = {};
|
2022-05-22 19:28:12 +00:00
|
|
|
UAVdesc.Format = DXGI_FORMAT_R32_TYPELESS;
|
2021-04-17 17:14:43 +00:00
|
|
|
UAVdesc.ViewDimension = D3D11_UAV_DIMENSION_BUFFER;
|
|
|
|
UAVdesc.Buffer.FirstElement = 0;
|
2022-05-22 19:28:12 +00:00
|
|
|
UAVdesc.Buffer.Flags = D3D11_BUFFER_UAV_FLAG_RAW;
|
2021-04-17 17:14:43 +00:00
|
|
|
UAVdesc.Buffer.NumElements = NUM_BBOX_VALUES;
|
2021-06-09 11:42:21 +00:00
|
|
|
hr = D3D::device->CreateUnorderedAccessView(m_buffer.Get(), &UAVdesc, &m_uav);
|
2021-12-12 20:50:13 +00:00
|
|
|
ASSERT_MSG(VIDEO, SUCCEEDED(hr), "Failed to create BoundingBox UAV: {}", DX11HRWrap(hr));
|
2021-06-09 11:42:21 +00:00
|
|
|
if (FAILED(hr))
|
|
|
|
return false;
|
|
|
|
D3DCommon::SetDebugObjectName(m_uav.Get(), "BoundingBox UAV");
|
|
|
|
D3D::stateman->SetOMUAV(m_uav.Get());
|
2021-04-17 17:14:43 +00:00
|
|
|
|
2021-06-09 11:42:21 +00:00
|
|
|
return true;
|
2014-12-05 02:01:20 +00:00
|
|
|
}
|
|
|
|
|
2021-06-09 11:42:21 +00:00
|
|
|
std::vector<BBoxType> D3DBoundingBox::Read(u32 index, u32 length)
|
2014-12-05 02:01:20 +00:00
|
|
|
{
|
2021-06-09 11:42:21 +00:00
|
|
|
std::vector<BBoxType> values(length);
|
|
|
|
D3D::context->CopyResource(m_staging_buffer.Get(), m_buffer.Get());
|
2021-04-17 17:14:43 +00:00
|
|
|
|
2014-12-05 02:01:20 +00:00
|
|
|
D3D11_MAPPED_SUBRESOURCE map;
|
2021-06-09 11:42:21 +00:00
|
|
|
HRESULT hr = D3D::context->Map(m_staging_buffer.Get(), 0, D3D11_MAP_READ, 0, &map);
|
2014-12-05 02:01:20 +00:00
|
|
|
if (SUCCEEDED(hr))
|
|
|
|
{
|
2021-06-09 11:42:21 +00:00
|
|
|
std::memcpy(values.data(), reinterpret_cast<const u8*>(map.pData) + sizeof(BBoxType) * index,
|
|
|
|
sizeof(BBoxType) * length);
|
2021-04-17 17:14:43 +00:00
|
|
|
|
2021-06-09 11:42:21 +00:00
|
|
|
D3D::context->Unmap(m_staging_buffer.Get(), 0);
|
2014-12-05 02:01:20 +00:00
|
|
|
}
|
2021-04-17 17:14:43 +00:00
|
|
|
|
2021-06-09 11:42:21 +00:00
|
|
|
return values;
|
2021-04-17 17:14:43 +00:00
|
|
|
}
|
|
|
|
|
2023-12-09 19:54:17 +00:00
|
|
|
void D3DBoundingBox::Write(u32 index, std::span<const BBoxType> values)
|
2021-04-17 17:14:43 +00:00
|
|
|
{
|
2021-06-09 11:42:21 +00:00
|
|
|
D3D11_BOX box{index * sizeof(BBoxType),
|
|
|
|
0,
|
|
|
|
0,
|
2023-12-09 19:54:17 +00:00
|
|
|
static_cast<u32>((index + values.size()) * sizeof(BBoxType)),
|
2021-06-09 11:42:21 +00:00
|
|
|
1,
|
|
|
|
1};
|
|
|
|
D3D::context->UpdateSubresource(m_buffer.Get(), 0, &box, values.data(), 0, 0);
|
2021-04-17 17:14:43 +00:00
|
|
|
}
|
|
|
|
|
2024-08-18 13:08:44 +00:00
|
|
|
} // namespace DX11
|