forked from microsoft/D3D12TranslationLayer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSharedResourceHelpers.cpp
389 lines (344 loc) · 17.2 KB
/
SharedResourceHelpers.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
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
#include "pch.h"
#include "SharedResourceHelpers.hpp"
namespace D3D12TranslationLayer
{
SharedResourceLocalHandle TRANSLATION_API SharedResourceHelpers::CreateKMTHandle(_In_ HANDLE resourceHandle)
{
CComPtr<IUnknown> spResource;
ThrowFailure(m_ImmCtx.m_pDevice12->OpenSharedHandle(resourceHandle, IID_PPV_ARGS(&spResource)));
UINT handle = GetHandleForResource(spResource);
// No more exceptions
spResource.Detach();
return handle;
}
SharedResourceLocalHandle TRANSLATION_API SharedResourceHelpers::CreateKMTHandle(_In_ IUnknown* pResource)
{
return GetHandleForResource(pResource);
}
IUnknown* TRANSLATION_API SharedResourceHelpers::DetachKMTHandle(SharedResourceLocalHandle handle)
{
std::lock_guard lock(m_OpenResourceMapLock);
// Called by C11On12 after a successful resource open
assert(m_OpenResourceMap[handle] != nullptr);
IUnknown* pUnknown = m_OpenResourceMap[handle];
m_OpenResourceMap[handle] = nullptr;
return pUnknown;
}
void TRANSLATION_API SharedResourceHelpers::DestroyKMTHandle(SharedResourceLocalHandle handle)
{
std::lock_guard lock(m_OpenResourceMapLock);
// This method is called by the core layer in cases of failure during OpenSharedResource
// The KM handle for a shared resource is simply an index into this
// vector, and "destroying" the KM handle should release the underlying resource.
if (m_OpenResourceMap[handle])
{
m_OpenResourceMap[handle]->Release();
m_OpenResourceMap[handle] = nullptr;
}
}
SharedResourceLocalHandle SharedResourceHelpers::GetHandleForResource(_In_ IUnknown* pResource)
{
std::lock_guard lock(m_OpenResourceMapLock);
// The KM handle for a shared resource is simply an index into this
// vector, to allow the core layer to clean up an 11on12 resource the same way as an 11 resource
UINT i = 0;
for (; i < m_OpenResourceMap.size(); ++i)
{
if (m_OpenResourceMap[i] == nullptr)
{
m_OpenResourceMap[i] = pResource;
// It's now safe to detach the smart pointer
return i;
}
}
m_OpenResourceMap.push_back(pResource); // throw( bad_alloc )
// It's now safe to detach the smart pointer
return i;
}
SharedResourceHelpers::SharedResourceHelpers(ImmediateContext& ImmCtx, CreationFlags const& Flags) noexcept
: m_ImmCtx(ImmCtx)
, m_CreationFlags(Flags)
{
}
void TRANSLATION_API SharedResourceHelpers::InitializePrivateDriverData(
DeferredDestructionType destructionType, _Out_writes_bytes_(dataSize) void* pResourcePrivateDriverData, _In_ UINT dataSize)
{
if (dataSize != cPrivateResourceDriverDataSize)
{
ThrowFailure(E_INVALIDARG);
}
__analysis_assume(dataSize == cPrivateResourceDriverDataSize);
SOpenResourcePrivateData* pPrivateData = (SOpenResourcePrivateData*)pResourcePrivateDriverData;
*pPrivateData = SOpenResourcePrivateData(destructionType);
}
IUnknown* TRANSLATION_API SharedResourceHelpers::QueryResourceFromKMTHandle(SharedResourceLocalHandle handle)
{
std::lock_guard lock(m_OpenResourceMapLock);
assert(m_OpenResourceMap[handle] != nullptr);
return m_OpenResourceMap[handle];
}
//----------------------------------------------------------------------------------------------------------------------------------
UINT ConvertPossibleBindFlags(D3D12_RESOURCE_DESC& Desc, D3D12_HEAP_FLAGS HeapFlags, const D3D12_FEATURE_DATA_FORMAT_SUPPORT& formatSupport, bool SupportDisplayableTextures)
{
UINT BindFlags = 0;
if (Desc.Dimension == D3D12_RESOURCE_DIMENSION_BUFFER)
{
BindFlags |= D3D11_BIND_CONSTANT_BUFFER |
D3D11_BIND_INDEX_BUFFER |
D3D11_BIND_VERTEX_BUFFER |
D3D11_BIND_STREAM_OUTPUT;
}
else if (Desc.Dimension == D3D12_RESOURCE_DIMENSION_TEXTURE2D
&& SupportDisplayableTextures
&& (HeapFlags & D3D12_HEAP_FLAG_SHARED)
&& (HeapFlags & D3D12_HEAP_FLAG_ALLOW_DISPLAY)
)
{
BindFlags |= D3D11_BIND_DECODER | D3D11_BIND_VIDEO_ENCODER;
}
else if ((HeapFlags & D3D12_HEAP_FLAG_ALLOW_DISPLAY) == 0)
{
BindFlags |= (formatSupport.Support1 & D3D12_FORMAT_SUPPORT1_DECODER_OUTPUT) ? D3D11_BIND_DECODER : 0u;
BindFlags |= (formatSupport.Support1 & D3D12_FORMAT_SUPPORT1_VIDEO_ENCODER) ? D3D11_BIND_VIDEO_ENCODER : 0u;
}
if (Desc.Flags & D3D12_RESOURCE_FLAG_ALLOW_DEPTH_STENCIL)
BindFlags |= D3D11_BIND_DEPTH_STENCIL;
if (Desc.Flags & D3D12_RESOURCE_FLAG_ALLOW_RENDER_TARGET)
BindFlags |= D3D11_BIND_RENDER_TARGET;
if (Desc.Flags & D3D12_RESOURCE_FLAG_ALLOW_UNORDERED_ACCESS)
BindFlags |= D3D11_BIND_UNORDERED_ACCESS;
if ((Desc.Flags & D3D12_RESOURCE_FLAG_DENY_SHADER_RESOURCE) == 0)
BindFlags |= D3D11_BIND_SHADER_RESOURCE;
return BindFlags;
}
//----------------------------------------------------------------------------------------------------------------------------------
UINT ConvertHeapMiscFlags(D3D12_HEAP_FLAGS HeapFlags, bool bNtHandle, bool bKeyedMutex)
{
UINT MiscFlags = 0;
if (HeapFlags & D3D12_HEAP_FLAG_SHARED)
MiscFlags |= (bKeyedMutex ? D3D11_RESOURCE_MISC_SHARED_KEYEDMUTEX : D3D11_RESOURCE_MISC_SHARED) | (bNtHandle ? D3D11_RESOURCE_MISC_SHARED_NTHANDLE : 0);
return MiscFlags;
}
//----------------------------------------------------------------------------------------------------------------------------------
UINT ConvertPossibleCPUAccessFlags(D3D12_HEAP_PROPERTIES HeapProps, ID3D12Device* pDevice)
{
UINT CPUAccessFlags = 0;
if (HeapProps.Type != D3D12_HEAP_TYPE_CUSTOM)
{
HeapProps = pDevice->GetCustomHeapProperties(HeapProps.CreationNodeMask, HeapProps.Type);
}
switch (HeapProps.CPUPageProperty)
{
case D3D12_CPU_PAGE_PROPERTY_WRITE_BACK: CPUAccessFlags = D3D11_CPU_ACCESS_WRITE | D3D11_CPU_ACCESS_READ; break;
case D3D12_CPU_PAGE_PROPERTY_WRITE_COMBINE: CPUAccessFlags = D3D11_CPU_ACCESS_WRITE; break;
}
return CPUAccessFlags;
}
void TRANSLATION_API SharedResourceHelpers::QueryResourceInfoFromKMTHandle(UINT handle, _In_opt_ const D3D11_RESOURCE_FLAGS* pOverrideFlags, _Out_ ResourceInfo* pResourceInfo)
{
if (pResourceInfo == nullptr)
{
ThrowFailure(E_INVALIDARG);
}
ZeroMemory(pResourceInfo, sizeof(*pResourceInfo));
CComPtr<IUnknown> spUnknown = QueryResourceFromKMTHandle(handle);
bool bShared = false;
ID3D12Object* pObject = nullptr;
// First, check for tile pools
CComPtr<ID3D12Heap> spHeap;
if (SUCCEEDED(spUnknown->QueryInterface(&spHeap)))
{
// Shared tile pools not supported on tier 1
if (m_ImmCtx.GetCaps().TiledResourcesTier == D3D12_TILED_RESOURCES_TIER_1)
{
// TODO: Debug spew
ThrowFailure(E_INVALIDARG);
}
pResourceInfo->TiledPool.m_HeapDesc = spHeap->GetDesc();
pResourceInfo->m_Type = TiledPoolType;
bShared = (pResourceInfo->TiledPool.m_HeapDesc.Flags & D3D12_HEAP_FLAG_SHARED) != 0;
pObject = spHeap;
}
// Not a tile pool, must be a resource
CComPtr<ID3D12Resource1> spResource;
if (SUCCEEDED(spUnknown->QueryInterface(&spResource)))
{
D3D12_HEAP_FLAGS MiscFlags;
pResourceInfo->m_Type = ResourceType;
bShared = SUCCEEDED(spResource->GetHeapProperties(nullptr, &MiscFlags)) &&
(MiscFlags & D3D12_HEAP_FLAG_SHARED) != 0;
pObject = spResource;
}
if (!spResource && !spHeap)
{
// TODO: Debug spew
ThrowFailure(E_INVALIDARG);
}
if (!bShared)
{
pResourceInfo->m_bAllocatedBy9on12 = false;
pResourceInfo->m_bNTHandle = false;
pResourceInfo->m_bSynchronized = false;
pResourceInfo->m_GDIHandle = 0;
}
else
{
D3D12_COMPATIBILITY_SHARED_FLAGS CompatFlags;
ThrowFailure(m_ImmCtx.m_pCompatDevice->ReflectSharedProperties(
pObject, D3D12_REFELCT_SHARED_PROPERTY_COMPATIBILITY_SHARED_FLAGS, &CompatFlags, sizeof(CompatFlags)));
pResourceInfo->m_bNTHandle = (CompatFlags & D3D12_COMPATIBILITY_SHARED_FLAG_NON_NT_HANDLE) == 0;
pResourceInfo->m_bSynchronized = (CompatFlags & D3D12_COMPATIBILITY_SHARED_FLAG_KEYED_MUTEX) != 0;
pResourceInfo->m_bAllocatedBy9on12 = (CompatFlags & D3D12_COMPATIBILITY_SHARED_FLAG_9_ON_12) != 0;
if (pResourceInfo->m_bNTHandle)
{
pResourceInfo->m_GDIHandle = 0;
}
else
{
ThrowFailure(m_ImmCtx.m_pCompatDevice->ReflectSharedProperties(
pObject, D3D12_REFLECT_SHARED_PROPERTY_NON_NT_SHARED_HANDLE, &pResourceInfo->m_GDIHandle, sizeof(pResourceInfo->m_GDIHandle)));
}
}
if (spResource)
{
D3D12_RESOURCE_DESC Desc = spResource->GetDesc();
D3D11_RESOURCE_FLAGS Flags = { };
D3D12_HEAP_FLAGS HeapFlags = D3D12_HEAP_FLAG_NONE;
D3D12_HEAP_PROPERTIES HeapProps = { };
if (Desc.Width > UINT_MAX)
{
// TODO: Debug spew
ThrowFailure(E_INVALIDARG);
}
if (m_CreationFlags.SupportDisplayableTextures
&& Desc.Format == DXGI_FORMAT_420_OPAQUE
)
{
// 420_OPAQUE doesn't exist in D3D12.
Desc.Format = DXGI_FORMAT_NV12;
}
D3D12_FEATURE_DATA_FORMAT_SUPPORT formatSupport = { Desc.Format };
(void)m_ImmCtx.m_pDevice12->CheckFeatureSupport(D3D12_FEATURE_FORMAT_SUPPORT, &formatSupport, sizeof(formatSupport));
if (pOverrideFlags || FAILED(m_ImmCtx.m_pCompatDevice->ReflectSharedProperties(
spResource, D3D12_REFLECT_SHARED_PROPERTY_D3D11_RESOURCE_FLAGS, &Flags, sizeof(Flags))))
{
// First, determine the full set of capabilities from the resource
Flags.MiscFlags = 0;
Flags.CPUAccessFlags = 0;
Flags.StructureByteStride = 0;
if (SUCCEEDED(spResource->GetHeapProperties(&HeapProps, &HeapFlags)))
{
// Committed or placed
Flags.MiscFlags |= ConvertHeapMiscFlags(HeapFlags, pResourceInfo->m_bNTHandle, pResourceInfo->m_bSynchronized);
if (m_CreationFlags.SupportDisplayableTextures
&& (HeapFlags & D3D12_HEAP_FLAG_SHARED)
&& Desc.Dimension != D3D12_RESOURCE_DIMENSION_BUFFER
)
{
if (HeapFlags & D3D12_HEAP_FLAG_ALLOW_DISPLAY)
{
Flags.MiscFlags |= 0x100000 /*D3D11_RESOURCE_MISC_SHARED_DISPLAYABLE*/;
}
if (!(Desc.Flags & D3D12_RESOURCE_FLAG_ALLOW_SIMULTANEOUS_ACCESS))
{
Flags.MiscFlags |= 0x200000 /*D3D11_RESOURCE_MISC_SHARED_EXCLUSIVE_WRITER*/;
}
}
Flags.CPUAccessFlags = ConvertPossibleCPUAccessFlags(HeapProps, m_ImmCtx.m_pDevice12.get());
}
else
{
// Reserved (tiled)
Flags.MiscFlags |= D3D11_RESOURCE_MISC_TILED;
}
Flags.BindFlags = ConvertPossibleBindFlags(Desc, HeapFlags, formatSupport, m_CreationFlags.SupportDisplayableTextures);
{
CComPtr<ID3D12ProtectedResourceSession> spProtectedResourceSession;
if (SUCCEEDED(spResource->GetProtectedResourceSession(IID_PPV_ARGS(&spProtectedResourceSession))))
{
Flags.MiscFlags |= D3D11_RESOURCE_MISC_HW_PROTECTED;
}
}
UINT AssumableBindFlags = (D3D11_BIND_DEPTH_STENCIL | D3D11_BIND_SHADER_RESOURCE |
D3D11_BIND_RENDER_TARGET | D3D11_BIND_UNORDERED_ACCESS);
UINT AssumableMiscFlags = (D3D11_RESOURCE_MISC_RESTRICT_SHARED_RESOURCE |
D3D11_RESOURCE_MISC_SHARED | D3D11_RESOURCE_MISC_SHARED_KEYEDMUTEX | D3D11_RESOURCE_MISC_SHARED_NTHANDLE |
D3D11_RESOURCE_MISC_TILED | D3D11_RESOURCE_MISC_TILE_POOL | D3D11_RESOURCE_MISC_HW_PROTECTED);
if (m_CreationFlags.SupportDisplayableTextures)
{
AssumableBindFlags |= D3D11_BIND_VIDEO_ENCODER | D3D11_BIND_DECODER;
// D3D11_RESOURCE_MISC_SHARED_DISPLAYABLE | D3D11_RESOURCE_MISC_SHARED_EXCLUSIVE_WRITER
AssumableMiscFlags |= 0x300000;
}
static const UINT AssumableCPUFlags = 0;
if (pOverrideFlags)
{
// Most misc flags will all be validated by the open shared resource process in the D3D11 core,
// since validity of misc flags depend on the final bind/CPU flags, not the possible ones
// The only ones we validate here are the ones that are based on heap properties (or lack thereof)
if ((pOverrideFlags->MiscFlags & AssumableMiscFlags) & ~Flags.MiscFlags ||
pOverrideFlags->BindFlags & ~Flags.BindFlags ||
pOverrideFlags->CPUAccessFlags & ~Flags.CPUAccessFlags)
{
// TODO: Debug spew
ThrowFailure(E_INVALIDARG);
}
if (pOverrideFlags->StructureByteStride && Desc.Dimension != D3D12_RESOURCE_DIMENSION_BUFFER)
{
// TODO: Debug spew
ThrowFailure(E_INVALIDARG);
}
Flags.BindFlags = pOverrideFlags->BindFlags;
Flags.CPUAccessFlags = pOverrideFlags->CPUAccessFlags;
Flags.StructureByteStride = pOverrideFlags->StructureByteStride;
}
else
{
Flags.BindFlags &= AssumableBindFlags;
Flags.CPUAccessFlags &= AssumableCPUFlags;
assert((Flags.MiscFlags & ~AssumableMiscFlags) == 0);
}
}
#if DBG
else
{
// Ensure the 11 flags are a subset of capabilities implied by the 12 resource desc
assert(SUCCEEDED(spResource->GetHeapProperties(&HeapProps, &HeapFlags)));
assert((Flags.CPUAccessFlags & ~ConvertPossibleCPUAccessFlags(HeapProps, m_ImmCtx.m_pDevice12.get())) == 0);
assert((Flags.BindFlags & ~ConvertPossibleBindFlags(Desc, HeapFlags, formatSupport, m_CreationFlags.SupportDisplayableTextures)) == 0);
assert((~Flags.MiscFlags & ConvertHeapMiscFlags(HeapFlags, pResourceInfo->m_bNTHandle, pResourceInfo->m_bSynchronized)) == 0);
}
#endif
pResourceInfo->Resource.m_ResourceDesc = Desc;
pResourceInfo->Resource.m_HeapFlags = HeapFlags;
pResourceInfo->Resource.m_HeapProps = HeapProps;
pResourceInfo->Resource.m_Flags11 = Flags;
}
}
unique_comptr<Resource> TRANSLATION_API SharedResourceHelpers::OpenResourceFromKmtHandle(
ResourceCreationArgs& createArgs,
_In_ SharedResourceLocalHandle kmtHandle,
_In_reads_bytes_(resourcePrivateDriverDataSize) void* pResourcePrivateDriverData,
_In_ UINT resourcePrivateDriverDataSize,
_In_ D3D12_RESOURCE_STATES currentState)
{
UNREFERENCED_PARAMETER(resourcePrivateDriverDataSize);
IUnknown* pRefToRelease = QueryResourceFromKMTHandle(kmtHandle);
assert(resourcePrivateDriverDataSize == sizeof(SOpenResourcePrivateData));
SOpenResourcePrivateData* pPrivateData = reinterpret_cast<SOpenResourcePrivateData*>(pResourcePrivateDriverData);
auto spResource = Resource::OpenResource(
&m_ImmCtx,
createArgs,
pRefToRelease,
pPrivateData->GetDeferredDestructionType(),
currentState);
if (pRefToRelease)
{
pRefToRelease->Release();
}
DetachKMTHandle(kmtHandle);
return spResource;
}
}