-
Notifications
You must be signed in to change notification settings - Fork 303
/
main.cpp
483 lines (391 loc) · 20.2 KB
/
main.cpp
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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
#include "pch.h"
#pragma warning(disable : 4238) // References to temporary classes are okay because they are only used as function parameters.
// Enable this to show element-wise identity multiplied by a scalar placed in a constant node.
// This only applies to the DirectMLX API that builds a graph.
#define MULTIPLY_WITH_SCALAR_CONSTANT 0
using Microsoft::WRL::ComPtr;
void InitializeDirect3D12(
ComPtr<ID3D12Device> & d3D12Device,
ComPtr<ID3D12CommandQueue> & commandQueue,
ComPtr<ID3D12CommandAllocator> & commandAllocator,
ComPtr<ID3D12GraphicsCommandList> & commandList)
{
#if defined(_DEBUG) && !defined(_GAMING_XBOX)
ComPtr<ID3D12Debug> d3D12Debug;
// Throws if the D3D12 debug layer is missing - you must install the Graphics Tools optional feature
THROW_IF_FAILED(D3D12GetDebugInterface(IID_PPV_ARGS(d3D12Debug.GetAddressOf())));
d3D12Debug->EnableDebugLayer();
#endif
#if !defined(_GAMING_XBOX)
ComPtr<IDXGIFactory4> dxgiFactory;
THROW_IF_FAILED(CreateDXGIFactory1(IID_PPV_ARGS(dxgiFactory.GetAddressOf())));
ComPtr<IDXGIAdapter> dxgiAdapter;
UINT adapterIndex{};
HRESULT hr{};
do
{
dxgiAdapter = nullptr;
THROW_IF_FAILED(dxgiFactory->EnumAdapters(adapterIndex, dxgiAdapter.ReleaseAndGetAddressOf()));
++adapterIndex;
hr = ::D3D12CreateDevice(
dxgiAdapter.Get(),
D3D_FEATURE_LEVEL_11_0,
IID_PPV_ARGS(d3D12Device.ReleaseAndGetAddressOf()));
if (hr == DXGI_ERROR_UNSUPPORTED) continue;
THROW_IF_FAILED(hr);
} while (hr != S_OK);
#else
D3D12XBOX_CREATE_DEVICE_PARAMETERS params = {};
params.Version = D3D12_SDK_VERSION;
params.GraphicsCommandQueueRingSizeBytes = D3D12XBOX_DEFAULT_SIZE_BYTES;
params.GraphicsScratchMemorySizeBytes = D3D12XBOX_DEFAULT_SIZE_BYTES;
params.ComputeScratchMemorySizeBytes = D3D12XBOX_DEFAULT_SIZE_BYTES;
#ifdef _DEBUG
params.ProcessDebugFlags = D3D12XBOX_PROCESS_DEBUG_FLAG_VALIDATED;
#endif
THROW_IF_FAILED(D3D12XboxCreateDevice(
nullptr,
¶ms,
IID_GRAPHICS_PPV_ARGS(d3D12Device.ReleaseAndGetAddressOf())
));
#endif
D3D12_COMMAND_QUEUE_DESC commandQueueDesc{};
commandQueueDesc.Type = D3D12_COMMAND_LIST_TYPE_DIRECT;
commandQueueDesc.Flags = D3D12_COMMAND_QUEUE_FLAG_NONE;
THROW_IF_FAILED(d3D12Device->CreateCommandQueue(
&commandQueueDesc,
IID_GRAPHICS_PPV_ARGS(commandQueue.ReleaseAndGetAddressOf())));
THROW_IF_FAILED(d3D12Device->CreateCommandAllocator(
D3D12_COMMAND_LIST_TYPE_DIRECT,
IID_GRAPHICS_PPV_ARGS(commandAllocator.ReleaseAndGetAddressOf())));
THROW_IF_FAILED(d3D12Device->CreateCommandList(
0,
D3D12_COMMAND_LIST_TYPE_DIRECT,
commandAllocator.Get(),
nullptr,
IID_GRAPHICS_PPV_ARGS(commandList.ReleaseAndGetAddressOf())));
}
void CloseExecuteResetWait(
ComPtr<ID3D12Device> d3D12Device,
ComPtr<ID3D12CommandQueue> commandQueue,
ComPtr<ID3D12CommandAllocator> commandAllocator,
ComPtr<ID3D12GraphicsCommandList> commandList)
{
THROW_IF_FAILED(commandList->Close());
ID3D12CommandList* commandLists[] = { commandList.Get() };
commandQueue->ExecuteCommandLists(ARRAYSIZE(commandLists), commandLists);
ComPtr<ID3D12Fence> d3D12Fence;
THROW_IF_FAILED(d3D12Device->CreateFence(
0,
D3D12_FENCE_FLAG_NONE,
IID_GRAPHICS_PPV_ARGS(d3D12Fence.GetAddressOf())));
wil::unique_handle fenceEventHandle(::CreateEvent(nullptr, true, false, nullptr));
THROW_LAST_ERROR_IF_NULL(fenceEventHandle);
THROW_IF_FAILED(commandQueue->Signal(d3D12Fence.Get(), 1));
THROW_IF_FAILED(d3D12Fence->SetEventOnCompletion(1, fenceEventHandle.get()));
::WaitForSingleObjectEx(fenceEventHandle.get(), INFINITE, FALSE);
THROW_IF_FAILED(commandAllocator->Reset());
THROW_IF_FAILED(commandList->Reset(commandAllocator.Get(), nullptr));
}
int main()
{
ComPtr<ID3D12Device> d3D12Device;
ComPtr<ID3D12CommandQueue> commandQueue;
ComPtr<ID3D12CommandAllocator> commandAllocator;
ComPtr<ID3D12GraphicsCommandList> commandList;
// Set up Direct3D 12.
InitializeDirect3D12(d3D12Device, commandQueue, commandAllocator, commandList);
// Create the DirectML device.
DML_CREATE_DEVICE_FLAGS dmlCreateDeviceFlags = DML_CREATE_DEVICE_FLAG_NONE;
#if defined (_DEBUG)
// If the project is in a debug build, then enable debugging via DirectML debug layers with this flag.
dmlCreateDeviceFlags |= DML_CREATE_DEVICE_FLAG_DEBUG;
#endif
ComPtr<IDMLDevice> dmlDevice;
THROW_IF_FAILED(DMLCreateDevice(
d3D12Device.Get(),
dmlCreateDeviceFlags,
IID_PPV_ARGS(dmlDevice.GetAddressOf())));
constexpr UINT tensorSizes[4] = { 1, 2, 3, 4 };
constexpr UINT tensorElementCount = tensorSizes[0] * tensorSizes[1] * tensorSizes[2] * tensorSizes[3];
#if !USE_DMLX
DML_BUFFER_TENSOR_DESC dmlBufferTensorDesc = {};
dmlBufferTensorDesc.DataType = DML_TENSOR_DATA_TYPE_FLOAT32;
dmlBufferTensorDesc.Flags = DML_TENSOR_FLAG_NONE;
dmlBufferTensorDesc.DimensionCount = ARRAYSIZE(tensorSizes);
dmlBufferTensorDesc.Sizes = tensorSizes;
dmlBufferTensorDesc.Strides = nullptr;
dmlBufferTensorDesc.TotalTensorSizeInBytes = DMLCalcBufferTensorSize(
dmlBufferTensorDesc.DataType,
dmlBufferTensorDesc.DimensionCount,
dmlBufferTensorDesc.Sizes,
dmlBufferTensorDesc.Strides);
ComPtr<IDMLOperator> dmlOperator;
{
// Create DirectML operator(s). Operators represent abstract functions such as "multiply", "reduce", "convolution", or even
// compound operations such as recurrent neural nets. This example creates an instance of the Identity operator,
// which applies the function f(x) = x for all elements in a tensor.
DML_TENSOR_DESC dmlTensorDesc{};
dmlTensorDesc.Type = DML_TENSOR_TYPE_BUFFER;
dmlTensorDesc.Desc = &dmlBufferTensorDesc;
DML_ELEMENT_WISE_IDENTITY_OPERATOR_DESC dmlIdentityOperatorDesc{};
dmlIdentityOperatorDesc.InputTensor = &dmlTensorDesc;
dmlIdentityOperatorDesc.OutputTensor = &dmlTensorDesc; // Input and output tensors have same size/type.
// Like Direct3D 12, these DESC structs don't need to be long-lived. This means, for example, that it's safe to place
// the DML_OPERATOR_DESC (and all the subobjects it points to) on the stack, since they're no longer needed after
// CreateOperator returns.
DML_OPERATOR_DESC dmlOperatorDesc{};
dmlOperatorDesc.Type = DML_OPERATOR_ELEMENT_WISE_IDENTITY;
dmlOperatorDesc.Desc = &dmlIdentityOperatorDesc;
THROW_IF_FAILED(dmlDevice->CreateOperator(
&dmlOperatorDesc,
IID_PPV_ARGS(dmlOperator.GetAddressOf())));
}
// Compile the operator into an object that can be dispatched to the GPU. In this step, DirectML performs operator
// fusion and just-in-time (JIT) compilation of shader bytecode, then compiles it into a Direct3D 12 pipeline state object (PSO).
// The resulting compiled operator is a baked, optimized form of an operator suitable for execution on the GPU.
ComPtr<IDMLCompiledOperator> dmlCompiledOperator;
THROW_IF_FAILED(dmlDevice->CompileOperator(
dmlOperator.Get(),
DML_EXECUTION_FLAG_NONE,
IID_PPV_ARGS(dmlCompiledOperator.GetAddressOf())));
// 24 elements * 4 == 96 bytes.
UINT64 tensorBufferSize{ dmlBufferTensorDesc.TotalTensorSizeInBytes };
#else
// Create DirectML operator(s). Operators represent abstract functions such as "multiply", "reduce", "convolution", or even
// compound operations such as recurrent neural nets. This example creates an instance of the Identity operator,
// which applies the function f(x) = x for all elements in a tensor.
ComPtr<IDMLCompiledOperator> dmlCompiledOperator;
dml::Graph graph(dmlDevice.Get());
dml::TensorDesc::Dimensions dimensions(std::begin(tensorSizes), std::end(tensorSizes));
dml::TensorDesc desc = { DML_TENSOR_DATA_TYPE_FLOAT32, dimensions};
dml::Expression input = dml::InputTensor(graph, 0, desc);
#if MULTIPLY_WITH_SCALAR_CONSTANT
// The memory referenced by any constant nodes (e.g, "scalar" below) needs to be kept alive until the graph is compiled.
float scalar = 3.4f;
auto constValue = dml::ConstantData(
graph,
dml::Span<const dml::Byte>(reinterpret_cast<const dml::Byte*>(&scalar), sizeof(scalar)),
dml::TensorDesc{ DML_TENSOR_DATA_TYPE_FLOAT32, {1} });
// Creates the DirectMLX Graph then takes the compiled operator(s) and attaches it to the relative COM Interface.
dml::Expression output = dml::Identity(input) * dml::Reinterpret(constValue, dimensions, dml::TensorStrides{ 0,0,0,0 });
#else
// Creates the DirectMLX Graph then takes the compiled operator(s) and attaches it to the relative COM Interface.
dml::Expression output = dml::Identity(input);
#endif
DML_EXECUTION_FLAGS executionFlags = DML_EXECUTION_FLAG_ALLOW_HALF_PRECISION_COMPUTATION;
dmlCompiledOperator.Attach(graph.Compile(executionFlags, { output }).Detach());
// 24 elements * 4 == 96 bytes.
UINT64 tensorBufferSize{ desc.totalTensorSizeInBytes };
#endif
ComPtr<IDMLOperatorInitializer> dmlOperatorInitializer;
IDMLCompiledOperator* dmlCompiledOperators[] = { dmlCompiledOperator.Get() };
THROW_IF_FAILED(dmlDevice->CreateOperatorInitializer(
ARRAYSIZE(dmlCompiledOperators),
dmlCompiledOperators,
IID_PPV_ARGS(dmlOperatorInitializer.GetAddressOf())));
// Query the operator for the required size (in descriptors) of its binding table.
// You need to initialize an operator exactly once before it can be executed, and
// the two stages require different numbers of descriptors for binding. For simplicity,
// we create a single descriptor heap that's large enough to satisfy them both.
DML_BINDING_PROPERTIES initializeBindingProperties = dmlOperatorInitializer->GetBindingProperties();
DML_BINDING_PROPERTIES executeBindingProperties = dmlCompiledOperator->GetBindingProperties();
UINT descriptorCount = std::max(
initializeBindingProperties.RequiredDescriptorCount,
executeBindingProperties.RequiredDescriptorCount);
// Create descriptor heaps.
ComPtr<ID3D12DescriptorHeap> descriptorHeap;
D3D12_DESCRIPTOR_HEAP_DESC descriptorHeapDesc{};
descriptorHeapDesc.Type = D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV;
descriptorHeapDesc.NumDescriptors = descriptorCount;
descriptorHeapDesc.Flags = D3D12_DESCRIPTOR_HEAP_FLAG_SHADER_VISIBLE;
THROW_IF_FAILED(d3D12Device->CreateDescriptorHeap(
&descriptorHeapDesc,
IID_GRAPHICS_PPV_ARGS(descriptorHeap.GetAddressOf())));
// Set the descriptor heap(s).
ID3D12DescriptorHeap* d3D12DescriptorHeaps[] = { descriptorHeap.Get() };
commandList->SetDescriptorHeaps(ARRAYSIZE(d3D12DescriptorHeaps), d3D12DescriptorHeaps);
// Create a binding table over the descriptor heap we just created.
DML_BINDING_TABLE_DESC dmlBindingTableDesc{};
dmlBindingTableDesc.Dispatchable = dmlOperatorInitializer.Get();
dmlBindingTableDesc.CPUDescriptorHandle = descriptorHeap->GetCPUDescriptorHandleForHeapStart();
dmlBindingTableDesc.GPUDescriptorHandle = descriptorHeap->GetGPUDescriptorHandleForHeapStart();
dmlBindingTableDesc.SizeInDescriptors = descriptorCount;
ComPtr<IDMLBindingTable> dmlBindingTable;
THROW_IF_FAILED(dmlDevice->CreateBindingTable(
&dmlBindingTableDesc,
IID_PPV_ARGS(dmlBindingTable.GetAddressOf())));
// Create the temporary and persistent resources that are necessary for executing an operator.
// The temporary resource is scratch memory (used internally by DirectML), whose contents you don't need to define.
// The persistent resource is long-lived, and you need to initialize it using the IDMLOperatorInitializer.
UINT64 temporaryResourceSize = std::max(
initializeBindingProperties.TemporaryResourceSize,
executeBindingProperties.TemporaryResourceSize);
UINT64 persistentResourceSize = executeBindingProperties.PersistentResourceSize;
// Bind and initialize the operator on the GPU.
ComPtr<ID3D12Resource> temporaryBuffer;
if (temporaryResourceSize != 0)
{
THROW_IF_FAILED(d3D12Device->CreateCommittedResource(
&CD3DX12_HEAP_PROPERTIES(D3D12_HEAP_TYPE_DEFAULT),
D3D12_HEAP_FLAG_NONE,
&CD3DX12_RESOURCE_DESC::Buffer(temporaryResourceSize, D3D12_RESOURCE_FLAG_ALLOW_UNORDERED_ACCESS),
D3D12_RESOURCE_STATE_COMMON,
nullptr,
IID_GRAPHICS_PPV_ARGS(temporaryBuffer.GetAddressOf())));
if (initializeBindingProperties.TemporaryResourceSize != 0)
{
DML_BUFFER_BINDING bufferBinding{ temporaryBuffer.Get(), 0, temporaryResourceSize };
DML_BINDING_DESC bindingDesc{ DML_BINDING_TYPE_BUFFER, &bufferBinding };
dmlBindingTable->BindTemporaryResource(&bindingDesc);
}
}
ComPtr<ID3D12Resource> persistentBuffer;
if (persistentResourceSize != 0)
{
THROW_IF_FAILED(d3D12Device->CreateCommittedResource(
&CD3DX12_HEAP_PROPERTIES(D3D12_HEAP_TYPE_DEFAULT),
D3D12_HEAP_FLAG_NONE,
&CD3DX12_RESOURCE_DESC::Buffer(persistentResourceSize, D3D12_RESOURCE_FLAG_ALLOW_UNORDERED_ACCESS),
D3D12_RESOURCE_STATE_COMMON,
nullptr,
IID_GRAPHICS_PPV_ARGS(persistentBuffer.GetAddressOf())));
// The persistent resource should be bound as the output to the IDMLOperatorInitializer.
DML_BUFFER_BINDING bufferBinding{ persistentBuffer.Get(), 0, persistentResourceSize };
DML_BINDING_DESC bindingDesc{ DML_BINDING_TYPE_BUFFER, &bufferBinding };
dmlBindingTable->BindOutputs(1, &bindingDesc);
}
// The command recorder is a stateless object that records Dispatches into an existing Direct3D 12 command list.
ComPtr<IDMLCommandRecorder> dmlCommandRecorder;
THROW_IF_FAILED(dmlDevice->CreateCommandRecorder(
IID_PPV_ARGS(dmlCommandRecorder.GetAddressOf())));
// Record execution of the operator initializer.
dmlCommandRecorder->RecordDispatch(
commandList.Get(),
dmlOperatorInitializer.Get(),
dmlBindingTable.Get());
// Close the Direct3D 12 command list, and submit it for execution as you would any other command list. You could
// in principle record the execution into the same command list as the initialization, but you need only to Initialize
// once, and typically you want to Execute an operator more frequently than that.
CloseExecuteResetWait(d3D12Device, commandQueue, commandAllocator, commandList);
//
// Bind and execute the operator on the GPU.
//
commandList->SetDescriptorHeaps(ARRAYSIZE(d3D12DescriptorHeaps), d3D12DescriptorHeaps);
// Reset the binding table to bind for the operator we want to execute (it was previously used to bind for the
// initializer).
dmlBindingTableDesc.Dispatchable = dmlCompiledOperator.Get();
THROW_IF_FAILED(dmlBindingTable->Reset(&dmlBindingTableDesc));
if (temporaryResourceSize != 0)
{
DML_BUFFER_BINDING bufferBinding{ temporaryBuffer.Get(), 0, temporaryResourceSize };
DML_BINDING_DESC bindingDesc{ DML_BINDING_TYPE_BUFFER, &bufferBinding };
dmlBindingTable->BindTemporaryResource(&bindingDesc);
}
if (persistentResourceSize != 0)
{
DML_BUFFER_BINDING bufferBinding{ persistentBuffer.Get(), 0, persistentResourceSize };
DML_BINDING_DESC bindingDesc{ DML_BINDING_TYPE_BUFFER, &bufferBinding };
dmlBindingTable->BindPersistentResource(&bindingDesc);
}
// Create tensor buffers for upload/input/output/readback of the tensor elements.
ComPtr<ID3D12Resource> uploadBuffer;
THROW_IF_FAILED(d3D12Device->CreateCommittedResource(
&CD3DX12_HEAP_PROPERTIES(D3D12_HEAP_TYPE_UPLOAD),
D3D12_HEAP_FLAG_NONE,
&CD3DX12_RESOURCE_DESC::Buffer(tensorBufferSize),
D3D12_RESOURCE_STATE_GENERIC_READ,
nullptr,
IID_GRAPHICS_PPV_ARGS(uploadBuffer.GetAddressOf())));
ComPtr<ID3D12Resource> inputBuffer;
THROW_IF_FAILED(d3D12Device->CreateCommittedResource(
&CD3DX12_HEAP_PROPERTIES(D3D12_HEAP_TYPE_DEFAULT),
D3D12_HEAP_FLAG_NONE,
&CD3DX12_RESOURCE_DESC::Buffer(tensorBufferSize, D3D12_RESOURCE_FLAG_ALLOW_UNORDERED_ACCESS),
D3D12_RESOURCE_STATE_COPY_DEST,
nullptr,
IID_GRAPHICS_PPV_ARGS(inputBuffer.GetAddressOf())));
std::wcout << std::fixed; std::wcout.precision(4);
std::array<FLOAT, tensorElementCount> inputTensorElementArray;
{
std::wcout << L"input tensor: ";
for (auto & element : inputTensorElementArray)
{
element = 1.618f;
std::wcout << element << L' ';
};
std::wcout << std::endl;
D3D12_SUBRESOURCE_DATA tensorSubresourceData{};
tensorSubresourceData.pData = inputTensorElementArray.data();
tensorSubresourceData.RowPitch = static_cast<LONG_PTR>(tensorBufferSize);
tensorSubresourceData.SlicePitch = tensorSubresourceData.RowPitch;
// Upload the input tensor to the GPU.
::UpdateSubresources(
commandList.Get(),
inputBuffer.Get(),
uploadBuffer.Get(),
0,
0,
1,
&tensorSubresourceData);
commandList->ResourceBarrier(
1,
&CD3DX12_RESOURCE_BARRIER::Transition(
inputBuffer.Get(),
D3D12_RESOURCE_STATE_COPY_DEST,
D3D12_RESOURCE_STATE_UNORDERED_ACCESS
)
);
}
DML_BUFFER_BINDING inputBufferBinding{ inputBuffer.Get(), 0, tensorBufferSize };
DML_BINDING_DESC inputBindingDesc{ DML_BINDING_TYPE_BUFFER, &inputBufferBinding };
dmlBindingTable->BindInputs(1, &inputBindingDesc);
ComPtr<ID3D12Resource> outputBuffer;
THROW_IF_FAILED(d3D12Device->CreateCommittedResource(
&CD3DX12_HEAP_PROPERTIES(D3D12_HEAP_TYPE_DEFAULT),
D3D12_HEAP_FLAG_NONE,
&CD3DX12_RESOURCE_DESC::Buffer(tensorBufferSize, D3D12_RESOURCE_FLAG_ALLOW_UNORDERED_ACCESS),
D3D12_RESOURCE_STATE_UNORDERED_ACCESS,
nullptr,
IID_GRAPHICS_PPV_ARGS(outputBuffer.GetAddressOf())));
DML_BUFFER_BINDING outputBufferBinding{ outputBuffer.Get(), 0, tensorBufferSize };
DML_BINDING_DESC outputBindingDesc{ DML_BINDING_TYPE_BUFFER, &outputBufferBinding };
dmlBindingTable->BindOutputs(1, &outputBindingDesc);
// Record execution of the compiled operator.
dmlCommandRecorder->RecordDispatch(commandList.Get(), dmlCompiledOperator.Get(), dmlBindingTable.Get());
CloseExecuteResetWait(d3D12Device, commandQueue, commandAllocator, commandList);
// The output buffer now contains the result of the identity operator,
// so read it back if you want the CPU to access it.
ComPtr<ID3D12Resource> readbackBuffer;
THROW_IF_FAILED(d3D12Device->CreateCommittedResource(
&CD3DX12_HEAP_PROPERTIES(D3D12_HEAP_TYPE_READBACK),
D3D12_HEAP_FLAG_NONE,
&CD3DX12_RESOURCE_DESC::Buffer(tensorBufferSize),
D3D12_RESOURCE_STATE_COPY_DEST,
nullptr,
IID_GRAPHICS_PPV_ARGS(readbackBuffer.GetAddressOf())));
commandList->ResourceBarrier(
1,
&CD3DX12_RESOURCE_BARRIER::Transition(
outputBuffer.Get(),
D3D12_RESOURCE_STATE_UNORDERED_ACCESS,
D3D12_RESOURCE_STATE_COPY_SOURCE
)
);
commandList->CopyResource(readbackBuffer.Get(), outputBuffer.Get());
CloseExecuteResetWait(d3D12Device, commandQueue, commandAllocator, commandList);
D3D12_RANGE tensorBufferRange{ 0, static_cast<SIZE_T>(tensorBufferSize) };
FLOAT* outputBufferData{};
THROW_IF_FAILED(readbackBuffer->Map(0, &tensorBufferRange, reinterpret_cast<void**>(&outputBufferData)));
std::wstring outputString = L"output tensor: ";
for (size_t tensorElementIndex{ 0 }; tensorElementIndex < tensorElementCount; ++tensorElementIndex, ++outputBufferData)
{
outputString += std::to_wstring(*outputBufferData) + L' ';
}
std::wcout << outputString << std::endl;
OutputDebugStringW(outputString.c_str());
D3D12_RANGE emptyRange{ 0, 0 };
readbackBuffer->Unmap(0, &emptyRange);
}