forked from microsoft/DirectML
-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.h
232 lines (196 loc) · 6.34 KB
/
util.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
//-----------------------------------------------------------------------------
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
//-----------------------------------------------------------------------------
#pragma once
#include <gpgmm_d3d12.h>
inline void ThrowIfFailed(HRESULT hr)
{
if (FAILED(hr))
throw std::exception();
}
inline void ThrowIfNull(void* p)
{
if (!p)
throw std::exception();
}
// DML_BUFFER_TENSOR_DESC (DML_TENSOR_TYPE_BUFFER)
struct DmlBufferTensorDesc
{
DML_TENSOR_DATA_TYPE dataType = DML_TENSOR_DATA_TYPE_UNKNOWN;
DML_TENSOR_FLAGS flags = DML_TENSOR_FLAG_NONE;
std::vector<uint32_t> sizes;
std::optional<std::vector<uint32_t>> strides;
uint64_t totalTensorSizeInBytes = 0;
uint32_t guaranteedBaseOffsetAlignment = 0;
DmlBufferTensorDesc() = default;
/*implicit*/ DmlBufferTensorDesc(const DML_BUFFER_TENSOR_DESC& desc)
: dataType(desc.DataType),
flags(desc.Flags),
sizes(desc.Sizes, desc.Sizes + desc.DimensionCount),
totalTensorSizeInBytes(desc.TotalTensorSizeInBytes),
guaranteedBaseOffsetAlignment(desc.GuaranteedBaseOffsetAlignment)
{
if (desc.Strides)
{
strides.emplace(desc.Strides, desc.Strides + desc.DimensionCount);
}
}
// Constructs a DmlBufferTensorDesc from a generic DML_TENSOR_DESC. The type must be DML_TENSOR_TYPE_BUFFER.
/*implicit*/ DmlBufferTensorDesc(const DML_TENSOR_DESC& desc)
: DmlBufferTensorDesc(*static_cast<const DML_BUFFER_TENSOR_DESC*>(desc.Desc))
{
assert(desc.Type == DML_TENSOR_TYPE_BUFFER);
}
uint32_t GetDimensionCount() const
{
assert(!strides || strides->size() == sizes.size());
return static_cast<uint32_t>(sizes.size());
}
operator DML_BUFFER_TENSOR_DESC() const
{
DML_BUFFER_TENSOR_DESC bufferTensorDesc;
bufferTensorDesc.DataType = dataType;
bufferTensorDesc.DimensionCount = GetDimensionCount();
bufferTensorDesc.Flags = flags;
bufferTensorDesc.GuaranteedBaseOffsetAlignment = guaranteedBaseOffsetAlignment;
bufferTensorDesc.Sizes = sizes.data();
bufferTensorDesc.Strides = strides ? strides->data() : nullptr;
bufferTensorDesc.TotalTensorSizeInBytes = totalTensorSizeInBytes;
return bufferTensorDesc;
}
};
// (DML_BINDING_TYPE_NONE)
struct DmlNoneBinding
{
};
// DML_BUFFER_BINDING (DML_BINDING_TYPE_BUFFER)
struct DmlBufferBinding
{
ID3D12Resource* buffer;
uint64_t offset;
uint64_t sizeInBytes;
DmlBufferBinding() = default;
/*implicit*/ DmlBufferBinding(const DML_BUFFER_BINDING& desc)
: buffer(desc.Buffer),
offset(desc.Offset),
sizeInBytes(desc.SizeInBytes)
{
}
};
// DML_BUFFER_ARRAY_BINDING (DML_BINDING_TYPE_BUFFER_ARRAY)
struct DmlBufferArrayBinding
{
std::vector<DmlBufferBinding> bindings;
DmlBufferArrayBinding() = default;
/*implicit*/ DmlBufferArrayBinding(const DML_BUFFER_ARRAY_BINDING& desc)
: bindings(desc.Bindings, desc.Bindings + desc.BindingCount)
{
}
};
inline Microsoft::WRL::ComPtr<gpgmm::d3d12::ResourceAllocation> CreateResource(
gpgmm::d3d12::ResourceAllocator* resourceAllocator,
const D3D12_RESOURCE_DESC& resourceDesc,
const D3D12_HEAP_PROPERTIES& heapProperties,
D3D12_RESOURCE_STATES initialState
)
{
gpgmm::d3d12::ALLOCATION_DESC allocationDesc = {};
allocationDesc.HeapType = heapProperties.Type;
Microsoft::WRL::ComPtr<gpgmm::d3d12::ResourceAllocation> resource;
ThrowIfFailed(resourceAllocator->CreateResource(
allocationDesc,
resourceDesc,
initialState,
nullptr,
resource.GetAddressOf()));
return resource;
}
inline Microsoft::WRL::ComPtr<gpgmm::d3d12::ResourceAllocation> CreateCpuCustomBuffer(
gpgmm::d3d12::ResourceAllocator* resourceAllocator,
UINT64 sizeInBytes,
D3D12_RESOURCE_FLAGS flags = D3D12_RESOURCE_FLAG_ALLOW_UNORDERED_ACCESS
)
{
D3D12_HEAP_PROPERTIES heapProperties = {
D3D12_HEAP_TYPE_CUSTOM,
D3D12_CPU_PAGE_PROPERTY_WRITE_COMBINE,
D3D12_MEMORY_POOL_L0,
0,
0
};
return CreateResource(
resourceAllocator,
CD3DX12_RESOURCE_DESC::Buffer(sizeInBytes, flags),
heapProperties,
D3D12_RESOURCE_STATE_UNORDERED_ACCESS
);
}
inline Microsoft::WRL::ComPtr<gpgmm::d3d12::ResourceAllocation> CreateDefaultBuffer(
gpgmm::d3d12::ResourceAllocator* resourceAllocator,
UINT64 sizeInBytes,
D3D12_RESOURCE_FLAGS flags = D3D12_RESOURCE_FLAG_ALLOW_UNORDERED_ACCESS
)
{
return CreateResource(
resourceAllocator,
CD3DX12_RESOURCE_DESC::Buffer(sizeInBytes, flags),
CD3DX12_HEAP_PROPERTIES(D3D12_HEAP_TYPE_DEFAULT),
D3D12_RESOURCE_STATE_UNORDERED_ACCESS
);
}
inline Microsoft::WRL::ComPtr<gpgmm::d3d12::ResourceAllocation> CreateReadBackBuffer(gpgmm::d3d12::ResourceAllocator* resourceAllocator, UINT64 sizeInBytes)
{
return CreateResource(
resourceAllocator,
CD3DX12_RESOURCE_DESC::Buffer(sizeInBytes),
CD3DX12_HEAP_PROPERTIES(D3D12_HEAP_TYPE_READBACK),
D3D12_RESOURCE_STATE_COPY_DEST
);
}
void FillGpuBuffer(
ID3D12GraphicsCommandList* commandList,
ID3D12DescriptorHeap* descriptorHeapCpuVisible,
ID3D12DescriptorHeap* descriptorHeapGpuVisible,
uint32_t descriptorOffset,
ID3D12Resource* buffer,
uint32_t value
);
void WaitForQueueToComplete(ID3D12CommandQueue* queue);
inline std::string UintVectorToString(std::vector<uint32_t> const& v)
{
if (v.empty())
return std::string();
return std::accumulate(v.begin() + 1, v.end(), std::to_string(v[0]),
[](std::string const& a, int b) {
return a + ',' + std::to_string(b);
});
}
template <typename T>
T RoundUpToMultiple(T value, T multiple)
{
static_assert(std::is_integral_v<T>);
T remainder = value % multiple;
if (remainder != 0)
{
value += multiple - remainder;
}
return value;
}
// Rounds up a value to the nearest power of two
template <typename T>
T RoundUpToPow2(T value)
{
static_assert(std::is_integral_v<T>);
if (value >= std::numeric_limits<T>::max() / 2)
{
ThrowIfFailed(E_INVALIDARG); // overflow
}
T pow2 = 1;
while (pow2 < value)
{
pow2 *= 2;
}
return pow2;
}