-
Notifications
You must be signed in to change notification settings - Fork 1
/
test.cpp
253 lines (225 loc) · 14.8 KB
/
test.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
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
#ifndef _WIN32
#include <wsl/winadapter.h>
#endif
#include <directx/d3d12.h>
#include <directx/dxcore.h>
#include <directx/d3dx12.h>
#include <dxguids/dxguids.h>
#include <wrl/client.h>
#include <iostream>
#include <iomanip>
#ifndef _WIN32
#include <unistd.h>
#include <dlfcn.h>
#endif
const unsigned char g_cs[] = {
0x44, 0x58, 0x42, 0x43, 0x7c, 0x0d, 0x5d, 0x0d, 0xc9, 0x70, 0x0b, 0x01,
0x4d, 0x1e, 0x88, 0x20, 0xbd, 0xab, 0xc4, 0xa2, 0x01, 0x00, 0x00, 0x00,
0x2c, 0x06, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00, 0x38, 0x00, 0x00, 0x00,
0x48, 0x00, 0x00, 0x00, 0x58, 0x00, 0x00, 0x00, 0x68, 0x00, 0x00, 0x00,
0xbc, 0x00, 0x00, 0x00, 0xf4, 0x00, 0x00, 0x00, 0x53, 0x46, 0x49, 0x30,
0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x49, 0x53, 0x47, 0x31, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x08, 0x00, 0x00, 0x00, 0x4f, 0x53, 0x47, 0x31, 0x08, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, 0x50, 0x53, 0x56, 0x30,
0x4c, 0x00, 0x00, 0x00, 0x24, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0x05, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
0x10, 0x00, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x52, 0x54, 0x53, 0x30,
0x30, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
0x18, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x30, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x24, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x44, 0x58, 0x49, 0x4c, 0x30, 0x05, 0x00, 0x00,
0x60, 0x00, 0x05, 0x00, 0x4c, 0x01, 0x00, 0x00, 0x44, 0x58, 0x49, 0x4c,
0x00, 0x01, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00, 0x18, 0x05, 0x00, 0x00,
0x42, 0x43, 0xc0, 0xde, 0x21, 0x0c, 0x00, 0x00, 0x43, 0x01, 0x00, 0x00,
0x0b, 0x82, 0x20, 0x00, 0x02, 0x00, 0x00, 0x00, 0x13, 0x00, 0x00, 0x00,
0x07, 0x81, 0x23, 0x91, 0x41, 0xc8, 0x04, 0x49, 0x06, 0x10, 0x32, 0x39,
0x92, 0x01, 0x84, 0x0c, 0x25, 0x05, 0x08, 0x19, 0x1e, 0x04, 0x8b, 0x62,
0x80, 0x14, 0x45, 0x02, 0x42, 0x92, 0x0b, 0x42, 0xa4, 0x10, 0x32, 0x14,
0x38, 0x08, 0x18, 0x4b, 0x0a, 0x32, 0x52, 0x88, 0x48, 0x90, 0x14, 0x20,
0x43, 0x46, 0x88, 0xa5, 0x00, 0x19, 0x32, 0x42, 0xe4, 0x48, 0x0e, 0x90,
0x91, 0x22, 0xc4, 0x50, 0x41, 0x51, 0x81, 0x8c, 0xe1, 0x83, 0xe5, 0x8a,
0x04, 0x29, 0x46, 0x06, 0x51, 0x18, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00,
0x1b, 0x8c, 0xe0, 0xff, 0xff, 0xff, 0xff, 0x07, 0x40, 0x02, 0xaa, 0x0d,
0x84, 0xf0, 0xff, 0xff, 0xff, 0xff, 0x03, 0x20, 0x01, 0x00, 0x00, 0x00,
0x49, 0x18, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x13, 0x82, 0x60, 0x42,
0x20, 0x00, 0x00, 0x00, 0x89, 0x20, 0x00, 0x00, 0x24, 0x00, 0x00, 0x00,
0x32, 0x22, 0x48, 0x09, 0x20, 0x64, 0x85, 0x04, 0x93, 0x22, 0xa4, 0x84,
0x04, 0x93, 0x22, 0xe3, 0x84, 0xa1, 0x90, 0x14, 0x12, 0x4c, 0x8a, 0x8c,
0x0b, 0x84, 0xa4, 0x4c, 0x10, 0x40, 0x73, 0x04, 0x60, 0x30, 0x93, 0x19,
0x8c, 0x03, 0x3b, 0x84, 0xc3, 0x3c, 0xcc, 0x83, 0x1b, 0xc8, 0xc2, 0x2d,
0xcc, 0x02, 0x3d, 0xc8, 0x43, 0x3d, 0x8c, 0x03, 0x3d, 0xd4, 0x83, 0x3c,
0x94, 0x03, 0x39, 0x88, 0x42, 0x3d, 0x98, 0x83, 0x39, 0x94, 0x83, 0x3c,
0xf0, 0x41, 0x3d, 0xb8, 0xc3, 0x3c, 0xa4, 0xc3, 0x39, 0xb8, 0x43, 0x39,
0x90, 0x03, 0x18, 0xa4, 0x83, 0x3b, 0xd0, 0x83, 0x1f, 0xa0, 0x00, 0x50,
0x18, 0x01, 0x28, 0xc1, 0x20, 0x32, 0x47, 0x80, 0x90, 0xb9, 0x67, 0xb8,
0xfc, 0x09, 0x7b, 0x08, 0xc9, 0x0f, 0x81, 0x66, 0x58, 0x08, 0x14, 0x9c,
0x39, 0x82, 0xa0, 0x18, 0x08, 0x18, 0x00, 0x12, 0xa9, 0xa2, 0x0c, 0x80,
0x00, 0x00, 0x00, 0x00, 0x43, 0x6c, 0x20, 0x60, 0x8e, 0x00, 0x14, 0x00,
0x13, 0x14, 0x72, 0xc0, 0x87, 0x74, 0x60, 0x87, 0x36, 0x68, 0x87, 0x79,
0x68, 0x03, 0x72, 0xc0, 0x87, 0x0d, 0xaf, 0x50, 0x0e, 0x6d, 0xd0, 0x0e,
0x7a, 0x50, 0x0e, 0x6d, 0x00, 0x0f, 0x7a, 0x30, 0x07, 0x72, 0xa0, 0x07,
0x73, 0x20, 0x07, 0x6d, 0x90, 0x0e, 0x71, 0xa0, 0x07, 0x73, 0x20, 0x07,
0x6d, 0x90, 0x0e, 0x78, 0xa0, 0x07, 0x73, 0x20, 0x07, 0x6d, 0x90, 0x0e,
0x71, 0x60, 0x07, 0x7a, 0x30, 0x07, 0x72, 0xd0, 0x06, 0xe9, 0x30, 0x07,
0x72, 0xa0, 0x07, 0x73, 0x20, 0x07, 0x6d, 0x90, 0x0e, 0x76, 0x40, 0x07,
0x7a, 0x60, 0x07, 0x74, 0xd0, 0x06, 0xe6, 0x10, 0x07, 0x76, 0xa0, 0x07,
0x73, 0x20, 0x07, 0x6d, 0x60, 0x0e, 0x73, 0x20, 0x07, 0x7a, 0x30, 0x07,
0x72, 0xd0, 0x06, 0xe6, 0x60, 0x07, 0x74, 0xa0, 0x07, 0x76, 0x40, 0x07,
0x6d, 0xe0, 0x0e, 0x78, 0xa0, 0x07, 0x71, 0x60, 0x07, 0x7a, 0x30, 0x07,
0x72, 0xa0, 0x07, 0x76, 0x40, 0x07, 0x3a, 0x0f, 0x24, 0x90, 0x21, 0x23,
0x45, 0x44, 0x00, 0x1e, 0x00, 0xf0, 0x90, 0x87, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0x21, 0x8f, 0x02, 0x04, 0x40,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x43, 0x1e, 0x06, 0x08,
0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xb2, 0x40, 0x00,
0x0b, 0x00, 0x00, 0x00, 0x32, 0x1e, 0x98, 0x14, 0x19, 0x11, 0x4c, 0x90,
0x8c, 0x09, 0x26, 0x47, 0xc6, 0x04, 0x43, 0x02, 0x25, 0x30, 0x02, 0x50,
0x08, 0xc5, 0x40, 0x62, 0x06, 0x80, 0x40, 0x61, 0x50, 0x1a, 0x01, 0xa0,
0x30, 0x03, 0x40, 0xa0, 0x0c, 0xca, 0xa1, 0x14, 0xe8, 0x15, 0x08, 0x00,
0x79, 0x18, 0x00, 0x00, 0x41, 0x00, 0x00, 0x00, 0x1a, 0x03, 0x4c, 0x90,
0x46, 0x02, 0x13, 0xc4, 0x83, 0x0c, 0x6f, 0x0c, 0x24, 0xc6, 0x45, 0x66,
0x43, 0x10, 0x4c, 0x10, 0x00, 0x62, 0x82, 0x00, 0x14, 0x1b, 0x84, 0x81,
0x98, 0x20, 0x00, 0xc6, 0x06, 0x61, 0x30, 0x28, 0x8c, 0xcd, 0x4d, 0x10,
0x80, 0x63, 0xc3, 0x80, 0x24, 0xc4, 0x04, 0x41, 0x40, 0x58, 0x9c, 0x7d,
0x55, 0x05, 0x59, 0x4d, 0x10, 0x80, 0x64, 0x82, 0x90, 0x28, 0x1b, 0x16,
0x62, 0x61, 0x08, 0x62, 0x68, 0x1c, 0xc7, 0x39, 0x36, 0x04, 0xcf, 0x06,
0x02, 0x80, 0x00, 0x60, 0x82, 0x10, 0x2c, 0x0c, 0xd0, 0x26, 0x08, 0x00,
0x33, 0x41, 0x00, 0x9a, 0x09, 0x02, 0xe0, 0x6c, 0x30, 0x92, 0x89, 0x22,
0x2a, 0x6b, 0x83, 0x60, 0x5c, 0x1b, 0x06, 0x42, 0xc2, 0x26, 0x08, 0x45,
0xb0, 0x01, 0xd8, 0x30, 0x0c, 0xdb, 0xb6, 0x21, 0xe0, 0x36, 0x0c, 0x83,
0xd6, 0x4d, 0x10, 0x9e, 0x67, 0xc3, 0x30, 0x0c, 0xc3, 0x06, 0x82, 0xf8,
0x0c, 0x30, 0xd8, 0x50, 0x68, 0x08, 0x10, 0x85, 0x41, 0x15, 0x36, 0x36,
0xbb, 0x36, 0x97, 0x34, 0xb2, 0x32, 0x37, 0xba, 0x29, 0x41, 0x50, 0x85,
0x0c, 0xcf, 0xc5, 0xae, 0x4c, 0x6e, 0x2e, 0xed, 0xcd, 0x6d, 0x4a, 0x40,
0x34, 0x21, 0xc3, 0x73, 0xb1, 0x0b, 0x63, 0xb3, 0x2b, 0x93, 0x9b, 0x12,
0x18, 0x75, 0xc8, 0xf0, 0x5c, 0xe6, 0xd0, 0xc2, 0xc8, 0xca, 0xe4, 0x9a,
0xde, 0xc8, 0xca, 0xd8, 0xa6, 0x04, 0x49, 0x19, 0x32, 0x3c, 0x17, 0xb9,
0xb2, 0xb9, 0xb7, 0x3a, 0xb9, 0xb1, 0xb2, 0xb9, 0x29, 0x01, 0x54, 0x89,
0x0c, 0xcf, 0x85, 0x2e, 0x0f, 0xae, 0x2c, 0xc8, 0xcd, 0xed, 0x8d, 0x2e,
0x8c, 0x2e, 0xed, 0xcd, 0x6d, 0x6e, 0x8a, 0x80, 0x75, 0x75, 0xc8, 0xf0,
0x5c, 0xca, 0xdc, 0xe8, 0xe4, 0xf2, 0xa0, 0xde, 0xd2, 0xdc, 0xe8, 0xe6,
0xa6, 0x04, 0x61, 0x00, 0x79, 0x18, 0x00, 0x00, 0x4c, 0x00, 0x00, 0x00,
0x33, 0x08, 0x80, 0x1c, 0xc4, 0xe1, 0x1c, 0x66, 0x14, 0x01, 0x3d, 0x88,
0x43, 0x38, 0x84, 0xc3, 0x8c, 0x42, 0x80, 0x07, 0x79, 0x78, 0x07, 0x73,
0x98, 0x71, 0x0c, 0xe6, 0x00, 0x0f, 0xed, 0x10, 0x0e, 0xf4, 0x80, 0x0e,
0x33, 0x0c, 0x42, 0x1e, 0xc2, 0xc1, 0x1d, 0xce, 0xa1, 0x1c, 0x66, 0x30,
0x05, 0x3d, 0x88, 0x43, 0x38, 0x84, 0x83, 0x1b, 0xcc, 0x03, 0x3d, 0xc8,
0x43, 0x3d, 0x8c, 0x03, 0x3d, 0xcc, 0x78, 0x8c, 0x74, 0x70, 0x07, 0x7b,
0x08, 0x07, 0x79, 0x48, 0x87, 0x70, 0x70, 0x07, 0x7a, 0x70, 0x03, 0x76,
0x78, 0x87, 0x70, 0x20, 0x87, 0x19, 0xcc, 0x11, 0x0e, 0xec, 0x90, 0x0e,
0xe1, 0x30, 0x0f, 0x6e, 0x30, 0x0f, 0xe3, 0xf0, 0x0e, 0xf0, 0x50, 0x0e,
0x33, 0x10, 0xc4, 0x1d, 0xde, 0x21, 0x1c, 0xd8, 0x21, 0x1d, 0xc2, 0x61,
0x1e, 0x66, 0x30, 0x89, 0x3b, 0xbc, 0x83, 0x3b, 0xd0, 0x43, 0x39, 0xb4,
0x03, 0x3c, 0xbc, 0x83, 0x3c, 0x84, 0x03, 0x3b, 0xcc, 0xf0, 0x14, 0x76,
0x60, 0x07, 0x7b, 0x68, 0x07, 0x37, 0x68, 0x87, 0x72, 0x68, 0x07, 0x37,
0x80, 0x87, 0x70, 0x90, 0x87, 0x70, 0x60, 0x07, 0x76, 0x28, 0x07, 0x76,
0xf8, 0x05, 0x76, 0x78, 0x87, 0x77, 0x80, 0x87, 0x5f, 0x08, 0x87, 0x71,
0x18, 0x87, 0x72, 0x98, 0x87, 0x79, 0x98, 0x81, 0x2c, 0xee, 0xf0, 0x0e,
0xee, 0xe0, 0x0e, 0xf5, 0xc0, 0x0e, 0xec, 0x30, 0x03, 0x62, 0xc8, 0xa1,
0x1c, 0xe4, 0xa1, 0x1c, 0xcc, 0xa1, 0x1c, 0xe4, 0xa1, 0x1c, 0xdc, 0x61,
0x1c, 0xca, 0x21, 0x1c, 0xc4, 0x81, 0x1d, 0xca, 0x61, 0x06, 0xd6, 0x90,
0x43, 0x39, 0xc8, 0x43, 0x39, 0x98, 0x43, 0x39, 0xc8, 0x43, 0x39, 0xb8,
0xc3, 0x38, 0x94, 0x43, 0x38, 0x88, 0x03, 0x3b, 0x94, 0xc3, 0x2f, 0xbc,
0x83, 0x3c, 0xfc, 0x82, 0x3b, 0xd4, 0x03, 0x3b, 0xb0, 0xc3, 0x8c, 0xcc,
0x21, 0x07, 0x7c, 0x70, 0x03, 0x74, 0x60, 0x07, 0x37, 0x90, 0x87, 0x72,
0x98, 0x87, 0x77, 0xa8, 0x07, 0x79, 0x18, 0x87, 0x72, 0x70, 0x83, 0x70,
0xa0, 0x07, 0x7a, 0x90, 0x87, 0x74, 0x10, 0x87, 0x7a, 0xa0, 0x87, 0x72,
0x00, 0x00, 0x00, 0x00, 0x71, 0x20, 0x00, 0x00, 0x13, 0x00, 0x00, 0x00,
0x05, 0x40, 0x06, 0x81, 0xdf, 0xf9, 0x5b, 0x0d, 0x5a, 0x81, 0xc0, 0x99,
0xf5, 0x47, 0x92, 0x5e, 0xa7, 0x74, 0x79, 0x7d, 0x4c, 0xaf, 0xcb, 0xcb,
0x64, 0x61, 0xdd, 0x6c, 0x2e, 0xcb, 0x81, 0x49, 0x20, 0x30, 0x68, 0x01,
0x82, 0x20, 0x99, 0x80, 0x34, 0x5c, 0xbe, 0xf3, 0xf8, 0x42, 0x44, 0x00,
0x13, 0x11, 0x02, 0xcd, 0xb0, 0x10, 0x36, 0x50, 0x0d, 0x97, 0xef, 0x3c,
0x7e, 0x40, 0x15, 0x05, 0x11, 0xb1, 0x93, 0x13, 0x11, 0x3e, 0x72, 0xdb,
0x00, 0x00, 0x00, 0x00, 0x61, 0x20, 0x00, 0x00, 0x16, 0x00, 0x00, 0x00,
0x13, 0x04, 0x41, 0x2c, 0x10, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00,
0x04, 0x4a, 0x8e, 0x4c, 0x09, 0x10, 0x28, 0xc5, 0x80, 0x22, 0xa5, 0x4f,
0x9b, 0x01, 0x00, 0x00, 0x23, 0x06, 0x09, 0x00, 0x82, 0x60, 0xa0, 0x4c,
0x05, 0x01, 0x41, 0xcb, 0x88, 0x41, 0x03, 0x80, 0x20, 0x18, 0x30, 0x13,
0x11, 0x44, 0xd1, 0x20, 0x08, 0x42, 0x81, 0xe1, 0x40, 0x00, 0x00, 0x00,
0x05, 0x00, 0x00, 0x00, 0x56, 0x31, 0x19, 0xbf, 0xab, 0xbd, 0xbf, 0xab,
0xbd, 0xbf, 0x34, 0x45, 0x94, 0x30, 0x05, 0x54, 0x01, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
};
#define VERIFY_SUCCEEDED(expr) hr = (expr); if (FAILED(hr)) { std::cout << "FAILED: " #expr << ": " << std::hex << hr << std::endl; return hr; } else { std::cout << "Succeeded: " #expr << std::endl; }
#define VERIFY_ARE_EQUAL(a, b) if (a != b) { std::cout << "NOT EQUAL: " << a << ", " << b << std::endl; return 1; } else { std::cout << "Equal: " #a " and " #b << std::endl; }
using Microsoft::WRL::ComPtr;
int main()
{
using FnDXCoreCreateAdapterFactory = HRESULT (*)(REFIID, void**);
#ifdef _WIN32
auto dxcore = LoadLibraryA("dxcore.dll");
auto d3d12 = LoadLibraryA("d3d12.dll");
auto pfnDXCoreCreateAdapterFactory = (FnDXCoreCreateAdapterFactory)GetProcAddress(dxcore, "DXCoreCreateAdapterFactory");
auto pfnD3D12CreateDevice = (decltype(&D3D12CreateDevice))GetProcAddress(d3d12, "D3D12CreateDevice");
#else
auto dxcore = dlopen("libdxcore.so", RTLD_NOW | RTLD_LOCAL);
auto d3d12 = dlopen("libd3d12.so", RTLD_NOW | RTLD_LOCAL);
auto pfnDXCoreCreateAdapterFactory = (FnDXCoreCreateAdapterFactory)dlsym(dxcore, "DXCoreCreateAdapterFactory");
auto pfnD3D12CreateDevice = (decltype(&D3D12CreateDevice))dlsym(d3d12, "D3D12CreateDevice");
#endif
HRESULT hr = S_OK;
ComPtr<IDXCoreAdapterFactory> spFactory;
VERIFY_SUCCEEDED(pfnDXCoreCreateAdapterFactory(IID_PPV_ARGS(&spFactory)));
ComPtr<IDXCoreAdapterList> spList;
VERIFY_SUCCEEDED(spFactory->CreateAdapterList(1, &DXCORE_ADAPTER_ATTRIBUTE_D3D12_CORE_COMPUTE, IID_PPV_ARGS(&spList)));
ComPtr<IDXCoreAdapter> spAdapter;
VERIFY_SUCCEEDED(spList->GetAdapter(0, IID_PPV_ARGS(&spAdapter)));
ComPtr<ID3D12Device> spDevice;
VERIFY_SUCCEEDED(pfnD3D12CreateDevice(spAdapter.Get(), D3D_FEATURE_LEVEL_1_0_CORE, IID_PPV_ARGS(&spDevice)));
ComPtr<ID3D12Resource> spUAV, spStaging;
auto UAVDesc = CD3DX12_RESOURCE_DESC::Buffer(65536, D3D12_RESOURCE_FLAG_ALLOW_UNORDERED_ACCESS);
auto UAVHeapDesc = CD3DX12_HEAP_PROPERTIES(D3D12_HEAP_TYPE_DEFAULT);
auto StagingDesc = CD3DX12_RESOURCE_DESC::Buffer(65536);
auto StagingHeapDesc = CD3DX12_HEAP_PROPERTIES(D3D12_HEAP_TYPE_READBACK);
VERIFY_SUCCEEDED(spDevice->CreateCommittedResource(&UAVHeapDesc, D3D12_HEAP_FLAG_SHARED, &UAVDesc, D3D12_RESOURCE_STATE_COMMON, nullptr, IID_PPV_ARGS(&spUAV)));
VERIFY_SUCCEEDED(spDevice->CreateCommittedResource(&StagingHeapDesc, D3D12_HEAP_FLAG_NONE, &StagingDesc, D3D12_RESOURCE_STATE_COMMON, nullptr, IID_PPV_ARGS(&spStaging)));
HANDLE hRawSharedHandle;
VERIFY_SUCCEEDED(spDevice->CreateSharedHandle(spUAV.Get(), nullptr, 0x10000000L, nullptr, &hRawSharedHandle));
ComPtr<ID3D12Resource> spUAV2, spUAV3;
VERIFY_SUCCEEDED(spDevice->OpenSharedHandle(hRawSharedHandle, IID_PPV_ARGS(&spUAV2)));
VERIFY_SUCCEEDED(spDevice->OpenSharedHandle(hRawSharedHandle, IID_PPV_ARGS(&spUAV3)));
#ifdef _WIN32
CloseHandle(hRawSharedHandle);
#else
close((int)(intptr_t)hRawSharedHandle);
#endif
ComPtr<ID3D12RootSignature> spRootSig;
VERIFY_SUCCEEDED(spDevice->CreateRootSignature(0, g_cs, sizeof(g_cs), IID_PPV_ARGS(&spRootSig)));
ComPtr<ID3D12PipelineState> spPSO;
D3D12_COMPUTE_PIPELINE_STATE_DESC PSODesc = {};
PSODesc.CS.pShaderBytecode = g_cs;
PSODesc.CS.BytecodeLength = sizeof(g_cs);
VERIFY_SUCCEEDED(spDevice->CreateComputePipelineState(&PSODesc, IID_PPV_ARGS(&spPSO)));
ComPtr<ID3D12CommandAllocator> spAllocator;
VERIFY_SUCCEEDED(spDevice->CreateCommandAllocator(D3D12_COMMAND_LIST_TYPE_COMPUTE, IID_PPV_ARGS(&spAllocator)));
ComPtr<ID3D12GraphicsCommandList> spCommandList;
VERIFY_SUCCEEDED(spDevice->CreateCommandList(0, D3D12_COMMAND_LIST_TYPE_COMPUTE, spAllocator.Get(), spPSO.Get(), IID_PPV_ARGS(&spCommandList)));
spCommandList->SetComputeRootSignature(spRootSig.Get());
spCommandList->SetComputeRootUnorderedAccessView(0, spUAV->GetGPUVirtualAddress());
spCommandList->SetPipelineState(spPSO.Get());
spCommandList->Dispatch(1, 1, 1);
auto barrier = CD3DX12_RESOURCE_BARRIER::Transition(spUAV.Get(), D3D12_RESOURCE_STATE_UNORDERED_ACCESS, D3D12_RESOURCE_STATE_COPY_SOURCE);
spCommandList->ResourceBarrier(1, &barrier);
spCommandList->CopyResource(spStaging.Get(), spUAV2.Get());
VERIFY_SUCCEEDED(spCommandList->Close());
ComPtr<ID3D12CommandQueue> spQueue;
D3D12_COMMAND_QUEUE_DESC CQDesc = { D3D12_COMMAND_LIST_TYPE_COMPUTE };
VERIFY_SUCCEEDED(spDevice->CreateCommandQueue(&CQDesc, IID_PPV_ARGS(&spQueue)));
spQueue->ExecuteCommandLists(1, CommandListCast(spCommandList.GetAddressOf()));
ComPtr<ID3D12Fence> spFence;
VERIFY_SUCCEEDED(spDevice->CreateFence(0, D3D12_FENCE_FLAG_NONE, IID_PPV_ARGS(&spFence)));
VERIFY_SUCCEEDED(spQueue->Signal(spFence.Get(), 1));
VERIFY_SUCCEEDED(spFence->SetEventOnCompletion(1, nullptr));
VERIFY_ARE_EQUAL(spFence->GetCompletedValue(), 1ull);
void* pData = nullptr;
VERIFY_SUCCEEDED(spStaging->Map(0, nullptr, &pData));
VERIFY_ARE_EQUAL(*reinterpret_cast<UINT*>(pData), 0xd3d12u);
}