Debian Codegraph @

Source/Core/VideoBackends/D3D/D3DBoundingBox.cpp

raw symbols SCIP
1 // Copyright 2014 Dolphin Emulator Project
2 // SPDX-License-Identifier: GPL-2.0-or-later
3
4 #include "VideoBackends/D3D/D3DBoundingBox.h"
5
6 #include <algorithm>
7 #include <array>
8
9 #include "Common/Assert.h"
10 #include "Common/CommonTypes.h"
11 #include "Common/MsgHandler.h"
12
13 #include "VideoBackends/D3D/D3DState.h"
14 #include "VideoBackends/D3DCommon/D3DCommon.h"
15
16 namespace DX11
17 {
18 D3DBoundingBox::~D3DBoundingBox()
19 {
20 m_uav.Reset();
21 m_staging_buffer.Reset();
22 m_buffer.Reset();
23 }
24
25 bool D3DBoundingBox::Initialize()
26 {
27 // Create 2 buffers here.
28 // First for unordered access on default pool.
29 auto desc = CD3D11_BUFFER_DESC(NUM_BBOX_VALUES * sizeof(BBoxType), D3D11_BIND_UNORDERED_ACCESS,
30 D3D11_USAGE_DEFAULT, 0, D3D11_RESOURCE_MISC_BUFFER_ALLOW_RAW_VIEWS,
31 sizeof(BBoxType));
32 const BBoxType initial_values[NUM_BBOX_VALUES] = {0, 0, 0, 0};
33 D3D11_SUBRESOURCE_DATA data;
34 data.pSysMem = initial_values;
35 data.SysMemPitch = NUM_BBOX_VALUES * sizeof(BBoxType);
36 data.SysMemSlicePitch = 0;
37 HRESULT hr;
38 hr = D3D::device->CreateBuffer(&desc, &data, &m_buffer);
39 ASSERT_MSG(VIDEO, SUCCEEDED(hr), "Failed to create BoundingBox Buffer: {}", DX11HRWrap(hr));
40 if (FAILED(hr))
41 return false;
42 D3DCommon::SetDebugObjectName(m_buffer.Get(), "BoundingBox Buffer");
43
44 // Second to use as a staging buffer.
45 desc.Usage = D3D11_USAGE_STAGING;
46 desc.CPUAccessFlags = D3D11_CPU_ACCESS_READ;
47 desc.BindFlags = 0;
48 desc.MiscFlags = 0;
49 hr = D3D::device->CreateBuffer(&desc, nullptr, &m_staging_buffer);
50 ASSERT_MSG(VIDEO, SUCCEEDED(hr), "Failed to create BoundingBox Staging Buffer: {}",
51 DX11HRWrap(hr));
52 if (FAILED(hr))
53 return false;
54 D3DCommon::SetDebugObjectName(m_staging_buffer.Get(), "BoundingBox Staging Buffer");
55
56 // UAV is required to allow concurrent access.
57 D3D11_UNORDERED_ACCESS_VIEW_DESC UAVdesc = {};
58 UAVdesc.Format = DXGI_FORMAT_R32_TYPELESS;
59 UAVdesc.ViewDimension = D3D11_UAV_DIMENSION_BUFFER;
60 UAVdesc.Buffer.FirstElement = 0;
61 UAVdesc.Buffer.Flags = D3D11_BUFFER_UAV_FLAG_RAW;
62 UAVdesc.Buffer.NumElements = NUM_BBOX_VALUES;
63 hr = D3D::device->CreateUnorderedAccessView(m_buffer.Get(), &UAVdesc, &m_uav);
64 ASSERT_MSG(VIDEO, SUCCEEDED(hr), "Failed to create BoundingBox UAV: {}", DX11HRWrap(hr));
65 if (FAILED(hr))
66 return false;
67 D3DCommon::SetDebugObjectName(m_uav.Get(), "BoundingBox UAV");
68 D3D::stateman->SetOMUAV(m_uav.Get());
69
70 return true;
71 }
72
73 std::vector<BBoxType> D3DBoundingBox::Read(u32 index, u32 length)
74 {
75 std::vector<BBoxType> values(length);
76 D3D::context->CopyResource(m_staging_buffer.Get(), m_buffer.Get());
77
78 D3D11_MAPPED_SUBRESOURCE map;
79 HRESULT hr = D3D::context->Map(m_staging_buffer.Get(), 0, D3D11_MAP_READ, 0, &map);
80 if (SUCCEEDED(hr))
81 {
82 std::memcpy(values.data(), static_cast<const u8*>(map.pData) + sizeof(BBoxType) * index,
83 sizeof(BBoxType) * length);
84
85 D3D::context->Unmap(m_staging_buffer.Get(), 0);
86 }
87
88 return values;
89 }
90
91 void D3DBoundingBox::Write(u32 index, std::span<const BBoxType> values)
92 {
93 D3D11_BOX box{index * sizeof(BBoxType),
94 0,
95 0,
96 static_cast<u32>((index + values.size()) * sizeof(BBoxType)),
97 1,
98 1};
99 D3D::context->UpdateSubresource(m_buffer.Get(), 0, &box, values.data(), 0, 0);
100 }
101
102 } // namespace DX11