Add D3D12 keyed shared mutexes to Dawn

Dawn creates a keyed shared mutex for all wrapped resources and acquires
the mutex before the texture is used in an ExecuteCommandList call.

To coordinate with external clients, backend API has been extended
to allow clients to get and set the acquire key.

Pending and queue command lists have now been merged into one.

A future change will adjust GetPendingCommandContext to return a raw
command context without the need for error handling.

Bug:dawn:217
Change-Id: Ia96c449c305586407153f05ce75a40794b96027e
Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/12220
Reviewed-by: Austin Eng <enga@chromium.org>
Reviewed-by: Corentin Wallez <cwallez@chromium.org>
Commit-Queue: Rafael Cintron <rafael.cintron@microsoft.com>
This commit is contained in:
Rafael Cintron 2019-10-18 00:37:42 +00:00 committed by Commit Bot service account
parent 63f98f6e19
commit b2b2ef57a5
19 changed files with 531 additions and 141 deletions

View File

@ -481,9 +481,7 @@ namespace dawn_native {
} }
void TextureBase::DestroyInternal() { void TextureBase::DestroyInternal() {
if (mState == TextureState::OwnedInternal) {
DestroyImpl(); DestroyImpl();
}
mState = TextureState::Destroyed; mState = TextureState::Destroyed;
} }

View File

@ -17,6 +17,11 @@
namespace dawn_native { namespace d3d12 { namespace dawn_native { namespace d3d12 {
void CommandRecordingContext::AddToSharedTextureList(Texture* texture) {
ASSERT(IsOpen());
mSharedTextures.insert(texture);
}
MaybeError CommandRecordingContext::Open(ID3D12Device* d3d12Device, MaybeError CommandRecordingContext::Open(ID3D12Device* d3d12Device,
CommandAllocatorManager* commandAllocationManager) { CommandAllocatorManager* commandAllocationManager) {
ASSERT(!IsOpen()); ASSERT(!IsOpen());
@ -43,16 +48,30 @@ namespace dawn_native { namespace d3d12 {
return {}; return {};
} }
ResultOrError<ID3D12GraphicsCommandList*> CommandRecordingContext::Close() { MaybeError CommandRecordingContext::ExecuteCommandList(ID3D12CommandQueue* d3d12CommandQueue) {
ASSERT(IsOpen()); if (IsOpen()) {
mIsOpen = false; // Shared textures must be transitioned to common state after the last usage in order
// for them to be used by other APIs like D3D11. We ensure this by transitioning to the
// common state right before command list submission. TransitionUsageNow itself ensures
// no unnecessary transitions happen if the resources is already in the common state.
for (Texture* texture : mSharedTextures) {
texture->TransitionUsageNow(this, D3D12_RESOURCE_STATE_COMMON);
}
MaybeError error = MaybeError error =
CheckHRESULT(mD3d12CommandList->Close(), "D3D12 closing pending command list"); CheckHRESULT(mD3d12CommandList->Close(), "D3D12 closing pending command list");
if (error.IsError()) { if (error.IsError()) {
mD3d12CommandList.Reset(); Release();
DAWN_TRY(std::move(error)); DAWN_TRY(std::move(error));
} }
return mD3d12CommandList.Get();
ID3D12CommandList* d3d12CommandList = GetCommandList();
d3d12CommandQueue->ExecuteCommandLists(1, &d3d12CommandList);
mIsOpen = false;
mSharedTextures.clear();
}
return {};
} }
ID3D12GraphicsCommandList* CommandRecordingContext::GetCommandList() const { ID3D12GraphicsCommandList* CommandRecordingContext::GetCommandList() const {
@ -64,6 +83,7 @@ namespace dawn_native { namespace d3d12 {
void CommandRecordingContext::Release() { void CommandRecordingContext::Release() {
mD3d12CommandList.Reset(); mD3d12CommandList.Reset();
mIsOpen = false; mIsOpen = false;
mSharedTextures.clear();
} }
bool CommandRecordingContext::IsOpen() const { bool CommandRecordingContext::IsOpen() const {

View File

@ -15,23 +15,31 @@
#define DAWNNATIVE_D3D12_COMMANDRECORDINGCONTEXT_H_ #define DAWNNATIVE_D3D12_COMMANDRECORDINGCONTEXT_H_
#include "dawn_native/Error.h" #include "dawn_native/Error.h"
#include "dawn_native/d3d12/TextureD3D12.h"
#include "dawn_native/d3d12/d3d12_platform.h" #include "dawn_native/d3d12/d3d12_platform.h"
#include <set>
namespace dawn_native { namespace d3d12 { namespace dawn_native { namespace d3d12 {
class CommandAllocatorManager; class CommandAllocatorManager;
class Texture;
class CommandRecordingContext { class CommandRecordingContext {
public: public:
void AddToSharedTextureList(Texture* texture);
MaybeError Open(ID3D12Device* d3d12Device, MaybeError Open(ID3D12Device* d3d12Device,
CommandAllocatorManager* commandAllocationManager); CommandAllocatorManager* commandAllocationManager);
ResultOrError<ID3D12GraphicsCommandList*> Close();
ID3D12GraphicsCommandList* GetCommandList() const; ID3D12GraphicsCommandList* GetCommandList() const;
void Release(); void Release();
bool IsOpen() const; bool IsOpen() const;
MaybeError ExecuteCommandList(ID3D12CommandQueue* d3d12CommandQueue);
private: private:
ComPtr<ID3D12GraphicsCommandList> mD3d12CommandList; ComPtr<ID3D12GraphicsCommandList> mD3d12CommandList;
bool mIsOpen = false; bool mIsOpen = false;
std::set<Texture*> mSharedTextures;
}; };
}} // namespace dawn_native::d3d12 }} // namespace dawn_native::d3d12

View File

@ -20,6 +20,7 @@
#include "common/SwapChainUtils.h" #include "common/SwapChainUtils.h"
#include "dawn_native/d3d12/DeviceD3D12.h" #include "dawn_native/d3d12/DeviceD3D12.h"
#include "dawn_native/d3d12/NativeSwapChainImplD3D12.h" #include "dawn_native/d3d12/NativeSwapChainImplD3D12.h"
#include "dawn_native/d3d12/TextureD3D12.h"
namespace dawn_native { namespace d3d12 { namespace dawn_native { namespace d3d12 {
@ -47,11 +48,13 @@ namespace dawn_native { namespace d3d12 {
DawnTexture WrapSharedHandle(DawnDevice device, DawnTexture WrapSharedHandle(DawnDevice device,
const DawnTextureDescriptor* descriptor, const DawnTextureDescriptor* descriptor,
HANDLE sharedHandle) { HANDLE sharedHandle,
uint64_t acquireMutexKey) {
Device* backendDevice = reinterpret_cast<Device*>(device); Device* backendDevice = reinterpret_cast<Device*>(device);
const TextureDescriptor* backendDescriptor = const TextureDescriptor* backendDescriptor =
reinterpret_cast<const TextureDescriptor*>(descriptor); reinterpret_cast<const TextureDescriptor*>(descriptor);
TextureBase* texture = backendDevice->WrapSharedHandle(backendDescriptor, sharedHandle); TextureBase* texture =
backendDevice->WrapSharedHandle(backendDescriptor, sharedHandle, acquireMutexKey);
return reinterpret_cast<DawnTexture>(texture); return reinterpret_cast<DawnTexture>(texture);
} }
}} // namespace dawn_native::d3d12 }} // namespace dawn_native::d3d12

View File

@ -218,7 +218,7 @@ namespace dawn_native { namespace d3d12 {
mDescriptorHeapAllocator->Deallocate(mCompletedSerial); mDescriptorHeapAllocator->Deallocate(mCompletedSerial);
mMapRequestTracker->Tick(mCompletedSerial); mMapRequestTracker->Tick(mCompletedSerial);
mUsedComObjectRefs.ClearUpTo(mCompletedSerial); mUsedComObjectRefs.ClearUpTo(mCompletedSerial);
DAWN_TRY(ExecuteCommandContext(nullptr)); DAWN_TRY(ExecutePendingCommandContext());
DAWN_TRY(NextSerial()); DAWN_TRY(NextSerial());
return {}; return {};
} }
@ -243,26 +243,8 @@ namespace dawn_native { namespace d3d12 {
mUsedComObjectRefs.Enqueue(object, GetPendingCommandSerial()); mUsedComObjectRefs.Enqueue(object, GetPendingCommandSerial());
} }
MaybeError Device::ExecuteCommandContext(CommandRecordingContext* commandContext) { MaybeError Device::ExecutePendingCommandContext() {
UINT numLists = 0; return mPendingCommands.ExecuteCommandList(mCommandQueue.Get());
std::array<ID3D12CommandList*, 2> d3d12CommandLists;
// If there are pending commands, prepend them to ExecuteCommandLists
if (mPendingCommands.IsOpen()) {
ID3D12GraphicsCommandList* d3d12CommandList;
DAWN_TRY_ASSIGN(d3d12CommandList, mPendingCommands.Close());
d3d12CommandLists[numLists++] = d3d12CommandList;
}
if (commandContext != nullptr) {
ID3D12GraphicsCommandList* d3d12CommandList;
DAWN_TRY_ASSIGN(d3d12CommandList, commandContext->Close());
d3d12CommandLists[numLists++] = d3d12CommandList;
}
if (numLists > 0) {
mCommandQueue->ExecuteCommandLists(numLists, d3d12CommandLists.data());
}
return {};
} }
ResultOrError<BindGroupBase*> Device::CreateBindGroupImpl( ResultOrError<BindGroupBase*> Device::CreateBindGroupImpl(
@ -355,26 +337,62 @@ namespace dawn_native { namespace d3d12 {
} }
TextureBase* Device::WrapSharedHandle(const TextureDescriptor* descriptor, TextureBase* Device::WrapSharedHandle(const TextureDescriptor* descriptor,
HANDLE sharedHandle) { HANDLE sharedHandle,
if (ConsumedError(ValidateTextureDescriptor(this, descriptor))) { uint64_t acquireMutexKey) {
TextureBase* dawnTexture;
if (ConsumedError(Texture::Create(this, descriptor, sharedHandle, acquireMutexKey),
&dawnTexture))
return nullptr; return nullptr;
return dawnTexture;
} }
if (ConsumedError(ValidateTextureDescriptorCanBeWrapped(descriptor))) { // We use IDXGIKeyedMutexes to synchronize access between D3D11 and D3D12. D3D11/12 fences
return nullptr; // are a viable alternative but are, unfortunately, not available on all versions of Windows
// 10. Since D3D12 does not directly support keyed mutexes, we need to wrap the D3D12
// resource using 11on12 and QueryInterface the D3D11 representation for the keyed mutex.
ResultOrError<ComPtr<IDXGIKeyedMutex>> Device::CreateKeyedMutexForTexture(
ID3D12Resource* d3d12Resource) {
if (mD3d11On12Device == nullptr) {
ComPtr<ID3D11Device> d3d11Device;
ComPtr<ID3D11DeviceContext> d3d11DeviceContext;
D3D_FEATURE_LEVEL d3dFeatureLevel;
IUnknown* const iUnknownQueue = mCommandQueue.Get();
DAWN_TRY(CheckHRESULT(GetFunctions()->d3d11on12CreateDevice(
mD3d12Device.Get(), 0, nullptr, 0, &iUnknownQueue, 1, 1,
&d3d11Device, &d3d11DeviceContext, &d3dFeatureLevel),
"D3D12 11on12 device create"));
DAWN_TRY(CheckHRESULT(d3d11Device.As(&mD3d11On12Device),
"D3D12 QueryInterface ID3D11Device to ID3D11On12Device"));
} }
ComPtr<ID3D12Resource> d3d12Resource; ComPtr<ID3D11Texture2D> d3d11Texture;
const HRESULT hr = D3D11_RESOURCE_FLAGS resourceFlags;
mD3d12Device->OpenSharedHandle(sharedHandle, IID_PPV_ARGS(&d3d12Resource)); resourceFlags.BindFlags = 0;
resourceFlags.MiscFlags = D3D11_RESOURCE_MISC_SHARED_KEYEDMUTEX;
resourceFlags.CPUAccessFlags = 0;
resourceFlags.StructureByteStride = 0;
DAWN_TRY(CheckHRESULT(mD3d11On12Device->CreateWrappedResource(
d3d12Resource, &resourceFlags, D3D12_RESOURCE_STATE_COMMON,
D3D12_RESOURCE_STATE_COMMON, IID_PPV_ARGS(&d3d11Texture)),
"D3D12 creating a wrapped resource"));
ComPtr<IDXGIKeyedMutex> dxgiKeyedMutex;
DAWN_TRY(CheckHRESULT(d3d11Texture.As(&dxgiKeyedMutex),
"D3D12 QueryInterface ID3D11Texture2D to IDXGIKeyedMutex"));
return dxgiKeyedMutex;
}
void Device::ReleaseKeyedMutexForTexture(ComPtr<IDXGIKeyedMutex> dxgiKeyedMutex) {
ComPtr<ID3D11Resource> d3d11Resource;
HRESULT hr = dxgiKeyedMutex.As(&d3d11Resource);
if (FAILED(hr)) { if (FAILED(hr)) {
return nullptr; return;
} }
if (ConsumedError(ValidateD3D12TextureCanBeWrapped(d3d12Resource.Get(), descriptor))) { ID3D11Resource* d3d11ResourceRaw = d3d11Resource.Get();
return nullptr; mD3d11On12Device->ReleaseWrappedResources(&d3d11ResourceRaw, 1);
} }
return new Texture(this, descriptor, std::move(d3d12Resource));
}
}} // namespace dawn_native::d3d12 }} // namespace dawn_native::d3d12

View File

@ -78,7 +78,7 @@ namespace dawn_native { namespace d3d12 {
void ReferenceUntilUnused(ComPtr<IUnknown> object); void ReferenceUntilUnused(ComPtr<IUnknown> object);
MaybeError ExecuteCommandContext(CommandRecordingContext* commandContext); MaybeError ExecutePendingCommandContext();
ResultOrError<std::unique_ptr<StagingBufferBase>> CreateStagingBuffer(size_t size) override; ResultOrError<std::unique_ptr<StagingBufferBase>> CreateStagingBuffer(size_t size) override;
MaybeError CopyFromStagingToBuffer(StagingBufferBase* source, MaybeError CopyFromStagingToBuffer(StagingBufferBase* source,
@ -94,7 +94,12 @@ namespace dawn_native { namespace d3d12 {
void DeallocateMemory(ResourceHeapAllocation& allocation); void DeallocateMemory(ResourceHeapAllocation& allocation);
TextureBase* WrapSharedHandle(const TextureDescriptor* descriptor, HANDLE sharedHandle); TextureBase* WrapSharedHandle(const TextureDescriptor* descriptor,
HANDLE sharedHandle,
uint64_t acquireMutexKey);
ResultOrError<ComPtr<IDXGIKeyedMutex>> CreateKeyedMutexForTexture(
ID3D12Resource* d3d12Resource);
void ReleaseKeyedMutexForTexture(ComPtr<IDXGIKeyedMutex> dxgiKeyedMutex);
private: private:
ResultOrError<BindGroupBase*> CreateBindGroupImpl( ResultOrError<BindGroupBase*> CreateBindGroupImpl(
@ -126,6 +131,7 @@ namespace dawn_native { namespace d3d12 {
ComPtr<ID3D12Device> mD3d12Device; // Device is owned by adapter and will not be outlived. ComPtr<ID3D12Device> mD3d12Device; // Device is owned by adapter and will not be outlived.
ComPtr<ID3D12CommandQueue> mCommandQueue; ComPtr<ID3D12CommandQueue> mCommandQueue;
ComPtr<ID3D11On12Device> mD3d11On12Device; // 11on12 device corresponding to mCommandQueue
ComPtr<ID3D12CommandSignature> mDispatchIndirectSignature; ComPtr<ID3D12CommandSignature> mDispatchIndirectSignature;
ComPtr<ID3D12CommandSignature> mDrawIndirectSignature; ComPtr<ID3D12CommandSignature> mDrawIndirectSignature;

View File

@ -27,6 +27,7 @@ namespace dawn_native { namespace d3d12 {
DAWN_TRY(LoadD3D12()); DAWN_TRY(LoadD3D12());
DAWN_TRY(LoadDXGI()); DAWN_TRY(LoadDXGI());
DAWN_TRY(LoadD3DCompiler()); DAWN_TRY(LoadD3DCompiler());
DAWN_TRY(LoadD3D11());
LoadPIXRuntime(); LoadPIXRuntime();
return {}; return {};
} }
@ -50,6 +51,16 @@ namespace dawn_native { namespace d3d12 {
return {}; return {};
} }
MaybeError PlatformFunctions::LoadD3D11() {
std::string error;
if (!mD3D11Lib.Open("d3d11.dll", &error) ||
!mD3D11Lib.GetProc(&d3d11on12CreateDevice, "D3D11On12CreateDevice", &error)) {
return DAWN_DEVICE_LOST_ERROR(error.c_str());
}
return {};
}
MaybeError PlatformFunctions::LoadDXGI() { MaybeError PlatformFunctions::LoadDXGI() {
std::string error; std::string error;
if (!mDXGILib.Open("dxgi.dll", &error) || if (!mDXGILib.Open("dxgi.dll", &error) ||

View File

@ -77,13 +77,18 @@ namespace dawn_native { namespace d3d12 {
PFN_SET_MARKER_ON_COMMAND_LIST pixSetMarkerOnCommandList = nullptr; PFN_SET_MARKER_ON_COMMAND_LIST pixSetMarkerOnCommandList = nullptr;
// Functions from D3D11.dll
PFN_D3D11ON12_CREATE_DEVICE d3d11on12CreateDevice = nullptr;
private: private:
MaybeError LoadD3D12(); MaybeError LoadD3D12();
MaybeError LoadD3D11();
MaybeError LoadDXGI(); MaybeError LoadDXGI();
MaybeError LoadD3DCompiler(); MaybeError LoadD3DCompiler();
void LoadPIXRuntime(); void LoadPIXRuntime();
DynamicLib mD3D12Lib; DynamicLib mD3D12Lib;
DynamicLib mD3D11Lib;
DynamicLib mDXGILib; DynamicLib mDXGILib;
DynamicLib mD3DCompilerLib; DynamicLib mD3DCompilerLib;
DynamicLib mPIXEventRuntimeLib; DynamicLib mPIXEventRuntimeLib;

View File

@ -28,13 +28,14 @@ namespace dawn_native { namespace d3d12 {
device->Tick(); device->Tick();
DAWN_TRY(mCommandContext.Open(device->GetD3D12Device().Get(), CommandRecordingContext* commandContext;
device->GetCommandAllocatorManager())); DAWN_TRY_ASSIGN(commandContext, device->GetPendingCommandContext());
for (uint32_t i = 0; i < commandCount; ++i) { for (uint32_t i = 0; i < commandCount; ++i) {
DAWN_TRY(ToBackend(commands[i])->RecordCommands(&mCommandContext, i)); DAWN_TRY(ToBackend(commands[i])->RecordCommands(commandContext, i));
} }
DAWN_TRY(device->ExecuteCommandContext(&mCommandContext)); DAWN_TRY(device->ExecutePendingCommandContext());
DAWN_TRY(device->NextSerial()); DAWN_TRY(device->NextSerial());
return {}; return {};

View File

@ -31,8 +31,6 @@ namespace dawn_native { namespace d3d12 {
private: private:
MaybeError SubmitImpl(uint32_t commandCount, CommandBufferBase* const* commands) override; MaybeError SubmitImpl(uint32_t commandCount, CommandBufferBase* const* commands) override;
CommandRecordingContext mCommandContext;
}; };
}} // namespace dawn_native::d3d12 }} // namespace dawn_native::d3d12

View File

@ -44,8 +44,8 @@ namespace dawn_native { namespace d3d12 {
return nullptr; return nullptr;
} }
ID3D12Resource* nativeTexture = static_cast<ID3D12Resource*>(next.texture.ptr); ComPtr<ID3D12Resource> d3d12Texture = static_cast<ID3D12Resource*>(next.texture.ptr);
return new Texture(ToBackend(GetDevice()), descriptor, nativeTexture); return new Texture(ToBackend(GetDevice()), descriptor, std::move(d3d12Texture));
} }
MaybeError SwapChain::OnBeforePresent(TextureBase* texture) { MaybeError SwapChain::OnBeforePresent(TextureBase* texture) {
@ -57,7 +57,7 @@ namespace dawn_native { namespace d3d12 {
// Perform the necessary transition for the texture to be presented. // Perform the necessary transition for the texture to be presented.
ToBackend(texture)->TransitionUsageNow(commandContext, mTextureUsage); ToBackend(texture)->TransitionUsageNow(commandContext, mTextureUsage);
DAWN_TRY(device->ExecuteCommandContext(nullptr)); DAWN_TRY(device->ExecutePendingCommandContext());
return {}; return {};
} }

View File

@ -19,6 +19,7 @@
#include "dawn_native/DynamicUploader.h" #include "dawn_native/DynamicUploader.h"
#include "dawn_native/Error.h" #include "dawn_native/Error.h"
#include "dawn_native/d3d12/BufferD3D12.h" #include "dawn_native/d3d12/BufferD3D12.h"
#include "dawn_native/d3d12/D3D12Error.h"
#include "dawn_native/d3d12/DescriptorHeapAllocator.h" #include "dawn_native/d3d12/DescriptorHeapAllocator.h"
#include "dawn_native/d3d12/DeviceD3D12.h" #include "dawn_native/d3d12/DeviceD3D12.h"
#include "dawn_native/d3d12/ResourceAllocator.h" #include "dawn_native/d3d12/ResourceAllocator.h"
@ -272,13 +273,52 @@ namespace dawn_native { namespace d3d12 {
ResultOrError<TextureBase*> Texture::Create(Device* device, ResultOrError<TextureBase*> Texture::Create(Device* device,
const TextureDescriptor* descriptor) { const TextureDescriptor* descriptor) {
Ref<Texture> dawnTexture = AcquireRef(new Texture(device, descriptor)); Ref<Texture> dawnTexture =
AcquireRef(new Texture(device, descriptor, TextureState::OwnedInternal));
DAWN_TRY(dawnTexture->InitializeAsInternalTexture()); DAWN_TRY(dawnTexture->InitializeAsInternalTexture());
return dawnTexture.Detach(); return dawnTexture.Detach();
} }
Texture::Texture(Device* device, const TextureDescriptor* descriptor) ResultOrError<TextureBase*> Texture::Create(Device* device,
: TextureBase(device, descriptor, TextureState::OwnedInternal) { const TextureDescriptor* descriptor,
HANDLE sharedHandle,
uint64_t acquireMutexKey) {
Ref<Texture> dawnTexture =
AcquireRef(new Texture(device, descriptor, TextureState::OwnedExternal));
DAWN_TRY(
dawnTexture->InitializeAsExternalTexture(descriptor, sharedHandle, acquireMutexKey));
return dawnTexture.Detach();
}
MaybeError Texture::InitializeAsExternalTexture(const TextureDescriptor* descriptor,
HANDLE sharedHandle,
uint64_t acquireMutexKey) {
Device* dawnDevice = ToBackend(GetDevice());
DAWN_TRY(ValidateTextureDescriptor(dawnDevice, descriptor));
DAWN_TRY(ValidateTextureDescriptorCanBeWrapped(descriptor));
ComPtr<ID3D12Resource> d3d12Resource;
DAWN_TRY(CheckHRESULT(dawnDevice->GetD3D12Device()->OpenSharedHandle(
sharedHandle, IID_PPV_ARGS(&d3d12Resource)),
"D3D12 opening shared handle"));
DAWN_TRY(ValidateD3D12TextureCanBeWrapped(d3d12Resource.Get(), descriptor));
ComPtr<IDXGIKeyedMutex> dxgiKeyedMutex;
DAWN_TRY_ASSIGN(dxgiKeyedMutex,
dawnDevice->CreateKeyedMutexForTexture(d3d12Resource.Get()));
DAWN_TRY(CheckHRESULT(dxgiKeyedMutex->AcquireSync(acquireMutexKey, INFINITE),
"D3D12 acquiring shared mutex"));
mAcquireMutexKey = acquireMutexKey;
mDxgiKeyedMutex = std::move(dxgiKeyedMutex);
mResource = std::move(d3d12Resource);
SetIsSubresourceContentInitialized(true, 0, descriptor->mipLevelCount, 0,
descriptor->arrayLayerCount);
return {};
} }
MaybeError Texture::InitializeAsInternalTexture() { MaybeError Texture::InitializeAsInternalTexture() {
@ -318,7 +358,6 @@ namespace dawn_native { namespace d3d12 {
return {}; return {};
} }
// With this constructor, the lifetime of the ID3D12Resource is externally managed.
Texture::Texture(Device* device, Texture::Texture(Device* device,
const TextureDescriptor* descriptor, const TextureDescriptor* descriptor,
ComPtr<ID3D12Resource> nativeTexture) ComPtr<ID3D12Resource> nativeTexture)
@ -333,9 +372,13 @@ namespace dawn_native { namespace d3d12 {
} }
void Texture::DestroyImpl() { void Texture::DestroyImpl() {
// If we own the resource, release it. Device* device = ToBackend(GetDevice());
ToBackend(GetDevice())->GetResourceAllocator()->Release(mResource); device->GetResourceAllocator()->Release(std::move(mResource));
mResource = nullptr;
if (mDxgiKeyedMutex != nullptr) {
mDxgiKeyedMutex->ReleaseSync(mAcquireMutexKey + 1);
device->ReleaseKeyedMutexForTexture(std::move(mDxgiKeyedMutex));
}
} }
DXGI_FORMAT Texture::GetD3D12Format() const { DXGI_FORMAT Texture::GetD3D12Format() const {
@ -371,6 +414,13 @@ namespace dawn_native { namespace d3d12 {
bool Texture::TransitionUsageAndGetResourceBarrier(CommandRecordingContext* commandContext, bool Texture::TransitionUsageAndGetResourceBarrier(CommandRecordingContext* commandContext,
D3D12_RESOURCE_BARRIER* barrier, D3D12_RESOURCE_BARRIER* barrier,
D3D12_RESOURCE_STATES newState) { D3D12_RESOURCE_STATES newState) {
// Textures with keyed mutexes can be written from other graphics queues. Hence, they
// must be acquired before command list submission to ensure work from the other queues
// has finished. See Device::ExecuteCommandContext.
if (mDxgiKeyedMutex != nullptr) {
commandContext->AddToSharedTextureList(this);
}
// Avoid transitioning the texture when it isn't needed. // Avoid transitioning the texture when it isn't needed.
// TODO(cwallez@chromium.org): Need some form of UAV barriers at some point. // TODO(cwallez@chromium.org): Need some form of UAV barriers at some point.
if (mLastState == newState) { if (mLastState == newState) {

View File

@ -34,9 +34,14 @@ namespace dawn_native { namespace d3d12 {
public: public:
static ResultOrError<TextureBase*> Create(Device* device, static ResultOrError<TextureBase*> Create(Device* device,
const TextureDescriptor* descriptor); const TextureDescriptor* descriptor);
static ResultOrError<TextureBase*> Create(Device* device,
const TextureDescriptor* descriptor,
HANDLE sharedHandle,
uint64_t acquireMutexKey);
Texture(Device* device, Texture(Device* device,
const TextureDescriptor* descriptor, const TextureDescriptor* descriptor,
ComPtr<ID3D12Resource> nativeTexture); ComPtr<ID3D12Resource> d3d12Texture);
~Texture(); ~Texture();
DXGI_FORMAT GetD3D12Format() const; DXGI_FORMAT GetD3D12Format() const;
@ -59,8 +64,12 @@ namespace dawn_native { namespace d3d12 {
uint32_t layerCount); uint32_t layerCount);
private: private:
Texture(Device* device, const TextureDescriptor* descriptor); using TextureBase::TextureBase;
MaybeError InitializeAsInternalTexture(); MaybeError InitializeAsInternalTexture();
MaybeError InitializeAsExternalTexture(const TextureDescriptor* descriptor,
HANDLE sharedHandle,
uint64_t acquireMutexKey);
// Dawn API // Dawn API
void DestroyImpl() override; void DestroyImpl() override;
@ -82,6 +91,9 @@ namespace dawn_native { namespace d3d12 {
Serial mLastUsedSerial = UINT64_MAX; Serial mLastUsedSerial = UINT64_MAX;
bool mValidToDecay = false; bool mValidToDecay = false;
Serial mAcquireMutexKey = 0;
ComPtr<IDXGIKeyedMutex> mDxgiKeyedMutex;
}; };
class TextureView : public TextureViewBase { class TextureView : public TextureViewBase {

View File

@ -15,6 +15,7 @@
#ifndef DAWNNATIVE_D3D12_D3D12PLATFORM_H_ #ifndef DAWNNATIVE_D3D12_D3D12PLATFORM_H_
#define DAWNNATIVE_D3D12_D3D12PLATFORM_H_ #define DAWNNATIVE_D3D12_D3D12PLATFORM_H_
#include <d3d11on12.h>
#include <d3d12.h> #include <d3d12.h>
#include <dxgi1_4.h> #include <dxgi1_4.h>
#include <wrl.h> #include <wrl.h>

View File

@ -343,9 +343,11 @@ namespace dawn_native { namespace metal {
} }
void Texture::DestroyImpl() { void Texture::DestroyImpl() {
if (GetTextureState() == TextureState::OwnedInternal) {
[mMtlTexture release]; [mMtlTexture release];
mMtlTexture = nil; mMtlTexture = nil;
} }
}
id<MTLTexture> Texture::GetMTLTexture() { id<MTLTexture> Texture::GetMTLTexture() {
return mMtlTexture; return mMtlTexture;

View File

@ -163,9 +163,11 @@ namespace dawn_native { namespace opengl {
} }
void Texture::DestroyImpl() { void Texture::DestroyImpl() {
if (GetTextureState() == TextureState::OwnedInternal) {
ToBackend(GetDevice())->gl.DeleteTextures(1, &mHandle); ToBackend(GetDevice())->gl.DeleteTextures(1, &mHandle);
mHandle = 0; mHandle = 0;
} }
}
GLuint Texture::GetHandle() const { GLuint Texture::GetHandle() const {
return mHandle; return mHandle;

View File

@ -578,6 +578,7 @@ namespace dawn_native { namespace vulkan {
} }
void Texture::DestroyImpl() { void Texture::DestroyImpl() {
if (GetTextureState() == TextureState::OwnedInternal) {
Device* device = ToBackend(GetDevice()); Device* device = ToBackend(GetDevice());
// If we own the resource, release it. // If we own the resource, release it.
@ -601,6 +602,7 @@ namespace dawn_native { namespace vulkan {
// If a signal semaphore exists it should be requested before we delete the texture // If a signal semaphore exists it should be requested before we delete the texture
ASSERT(mSignalSemaphore == VK_NULL_HANDLE); ASSERT(mSignalSemaphore == VK_NULL_HANDLE);
} }
}
VkImage Texture::GetHandle() const { VkImage Texture::GetHandle() const {
return mHandle; return mHandle;

View File

@ -30,9 +30,11 @@ namespace dawn_native { namespace d3d12 {
DAWN_NATIVE_EXPORT DawnTextureFormat DAWN_NATIVE_EXPORT DawnTextureFormat
GetNativeSwapChainPreferredFormat(const DawnSwapChainImplementation* swapChain); GetNativeSwapChainPreferredFormat(const DawnSwapChainImplementation* swapChain);
// Note: SharedHandle must be a handle to a texture object.
DAWN_NATIVE_EXPORT DawnTexture WrapSharedHandle(DawnDevice device, DAWN_NATIVE_EXPORT DawnTexture WrapSharedHandle(DawnDevice device,
const DawnTextureDescriptor* descriptor, const DawnTextureDescriptor* descriptor,
HANDLE sharedHandle); HANDLE sharedHandle,
uint64_t acquireMutexKey);
}} // namespace dawn_native::d3d12 }} // namespace dawn_native::d3d12
#endif // DAWNNATIVE_D3D12BACKEND_H_ #endif // DAWNNATIVE_D3D12BACKEND_H_

View File

@ -58,12 +58,36 @@ namespace {
mD3d11Device = std::move(d3d11Device); mD3d11Device = std::move(d3d11Device);
mD3d11DeviceContext = std::move(d3d11DeviceContext); mD3d11DeviceContext = std::move(d3d11DeviceContext);
dawnDescriptor.dimension = dawn::TextureDimension::e2D;
dawnDescriptor.format = dawn::TextureFormat::RGBA8Unorm;
dawnDescriptor.size = {kTestWidth, kTestHeight, 1};
dawnDescriptor.sampleCount = 1;
dawnDescriptor.arrayLayerCount = 1;
dawnDescriptor.mipLevelCount = 1;
dawnDescriptor.usage = dawn::TextureUsage::Sampled | dawn::TextureUsage::CopySrc |
dawn::TextureUsage::OutputAttachment |
dawn::TextureUsage::CopyDst;
d3dDescriptor.Width = kTestWidth;
d3dDescriptor.Height = kTestHeight;
d3dDescriptor.MipLevels = 1;
d3dDescriptor.ArraySize = 1;
d3dDescriptor.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
d3dDescriptor.SampleDesc.Count = 1;
d3dDescriptor.SampleDesc.Quality = 0;
d3dDescriptor.Usage = D3D11_USAGE_DEFAULT;
d3dDescriptor.BindFlags = D3D11_BIND_SHADER_RESOURCE | D3D11_BIND_RENDER_TARGET;
d3dDescriptor.CPUAccessFlags = 0;
d3dDescriptor.MiscFlags =
D3D11_RESOURCE_MISC_SHARED_NTHANDLE | D3D11_RESOURCE_MISC_SHARED_KEYEDMUTEX;
} }
protected: protected:
void WrapSharedHandle(const dawn::TextureDescriptor* dawnDescriptor, void WrapSharedHandle(const dawn::TextureDescriptor* dawnDescriptor,
const D3D11_TEXTURE2D_DESC* d3dDescriptor, const D3D11_TEXTURE2D_DESC* d3dDescriptor,
dawn::Texture* dawnTexture) const { dawn::Texture* dawnTexture,
ID3D11Texture2D** d3d11TextureOut) const {
ComPtr<ID3D11Texture2D> d3d11Texture; ComPtr<ID3D11Texture2D> d3d11Texture;
HRESULT hr = mD3d11Device->CreateTexture2D(d3dDescriptor, nullptr, &d3d11Texture); HRESULT hr = mD3d11Device->CreateTexture2D(d3dDescriptor, nullptr, &d3d11Texture);
ASSERT_EQ(hr, S_OK); ASSERT_EQ(hr, S_OK);
@ -80,12 +104,13 @@ namespace {
DawnTexture texture = dawn_native::d3d12::WrapSharedHandle( DawnTexture texture = dawn_native::d3d12::WrapSharedHandle(
device.Get(), reinterpret_cast<const DawnTextureDescriptor*>(dawnDescriptor), device.Get(), reinterpret_cast<const DawnTextureDescriptor*>(dawnDescriptor),
sharedHandle); sharedHandle, 0);
// Now that we've created all of our resources, we can close the handle // Now that we've created all of our resources, we can close the handle
// since we no longer need it. // since we no longer need it.
::CloseHandle(sharedHandle); ::CloseHandle(sharedHandle);
*dawnTexture = dawn::Texture::Acquire(texture); *dawnTexture = dawn::Texture::Acquire(texture);
*d3d11TextureOut = d3d11Texture.Detach();
} }
static constexpr size_t kTestWidth = 10; static constexpr size_t kTestWidth = 10;
@ -93,6 +118,9 @@ namespace {
ComPtr<ID3D11Device> mD3d11Device; ComPtr<ID3D11Device> mD3d11Device;
ComPtr<ID3D11DeviceContext> mD3d11DeviceContext; ComPtr<ID3D11DeviceContext> mD3d11DeviceContext;
D3D11_TEXTURE2D_DESC d3dDescriptor;
dawn::TextureDescriptor dawnDescriptor;
}; };
} // anonymous namespace } // anonymous namespace
@ -100,35 +128,6 @@ namespace {
// A small fixture used to initialize default data for the D3D12Resource validation tests. // A small fixture used to initialize default data for the D3D12Resource validation tests.
// These tests are skipped if the harness is using the wire. // These tests are skipped if the harness is using the wire.
class D3D12SharedHandleValidation : public D3D12ResourceTestBase { class D3D12SharedHandleValidation : public D3D12ResourceTestBase {
public:
void TestSetUp() override {
D3D12ResourceTestBase::TestSetUp();
dawnDescriptor.dimension = dawn::TextureDimension::e2D;
dawnDescriptor.format = dawn::TextureFormat::BGRA8Unorm;
dawnDescriptor.size = {kTestWidth, kTestHeight, 1};
dawnDescriptor.sampleCount = 1;
dawnDescriptor.arrayLayerCount = 1;
dawnDescriptor.mipLevelCount = 1;
dawnDescriptor.usage = dawn::TextureUsage::OutputAttachment;
d3dDescriptor.Width = kTestWidth;
d3dDescriptor.Height = kTestHeight;
d3dDescriptor.MipLevels = 1;
d3dDescriptor.ArraySize = 1;
d3dDescriptor.Format = DXGI_FORMAT_B8G8R8A8_UNORM;
d3dDescriptor.SampleDesc.Count = 1;
d3dDescriptor.SampleDesc.Quality = 0;
d3dDescriptor.Usage = D3D11_USAGE_DEFAULT;
d3dDescriptor.BindFlags = D3D11_BIND_SHADER_RESOURCE | D3D11_BIND_RENDER_TARGET;
d3dDescriptor.CPUAccessFlags = 0;
d3dDescriptor.MiscFlags =
D3D11_RESOURCE_MISC_SHARED_NTHANDLE | D3D11_RESOURCE_MISC_SHARED_KEYEDMUTEX;
}
protected:
D3D11_TEXTURE2D_DESC d3dDescriptor;
dawn::TextureDescriptor dawnDescriptor;
}; };
// Test a successful wrapping of an D3D12Resource in a texture // Test a successful wrapping of an D3D12Resource in a texture
@ -136,7 +135,8 @@ TEST_P(D3D12SharedHandleValidation, Success) {
DAWN_SKIP_TEST_IF(UsesWire()); DAWN_SKIP_TEST_IF(UsesWire());
dawn::Texture texture; dawn::Texture texture;
WrapSharedHandle(&dawnDescriptor, &d3dDescriptor, &texture); ComPtr<ID3D11Texture2D> d3d11Texture;
WrapSharedHandle(&dawnDescriptor, &d3dDescriptor, &texture, &d3d11Texture);
ASSERT_NE(texture.Get(), nullptr); ASSERT_NE(texture.Get(), nullptr);
} }
@ -147,7 +147,8 @@ TEST_P(D3D12SharedHandleValidation, InvalidTextureDescriptor) {
dawnDescriptor.nextInChain = this; dawnDescriptor.nextInChain = this;
dawn::Texture texture; dawn::Texture texture;
ASSERT_DEVICE_ERROR(WrapSharedHandle(&dawnDescriptor, &d3dDescriptor, &texture)); ComPtr<ID3D11Texture2D> d3d11Texture;
ASSERT_DEVICE_ERROR(WrapSharedHandle(&dawnDescriptor, &d3dDescriptor, &texture, &d3d11Texture));
ASSERT_EQ(texture.Get(), nullptr); ASSERT_EQ(texture.Get(), nullptr);
} }
@ -158,7 +159,8 @@ TEST_P(D3D12SharedHandleValidation, InvalidMipLevelCount) {
dawnDescriptor.mipLevelCount = 2; dawnDescriptor.mipLevelCount = 2;
dawn::Texture texture; dawn::Texture texture;
ASSERT_DEVICE_ERROR(WrapSharedHandle(&dawnDescriptor, &d3dDescriptor, &texture)); ComPtr<ID3D11Texture2D> d3d11Texture;
ASSERT_DEVICE_ERROR(WrapSharedHandle(&dawnDescriptor, &d3dDescriptor, &texture, &d3d11Texture));
ASSERT_EQ(texture.Get(), nullptr); ASSERT_EQ(texture.Get(), nullptr);
} }
@ -169,7 +171,8 @@ TEST_P(D3D12SharedHandleValidation, InvalidArrayLayerCount) {
dawnDescriptor.arrayLayerCount = 2; dawnDescriptor.arrayLayerCount = 2;
dawn::Texture texture; dawn::Texture texture;
ASSERT_DEVICE_ERROR(WrapSharedHandle(&dawnDescriptor, &d3dDescriptor, &texture)); ComPtr<ID3D11Texture2D> d3d11Texture;
ASSERT_DEVICE_ERROR(WrapSharedHandle(&dawnDescriptor, &d3dDescriptor, &texture, &d3d11Texture));
ASSERT_EQ(texture.Get(), nullptr); ASSERT_EQ(texture.Get(), nullptr);
} }
@ -180,7 +183,8 @@ TEST_P(D3D12SharedHandleValidation, InvalidSampleCount) {
dawnDescriptor.sampleCount = 4; dawnDescriptor.sampleCount = 4;
dawn::Texture texture; dawn::Texture texture;
ASSERT_DEVICE_ERROR(WrapSharedHandle(&dawnDescriptor, &d3dDescriptor, &texture)); ComPtr<ID3D11Texture2D> d3d11Texture;
ASSERT_DEVICE_ERROR(WrapSharedHandle(&dawnDescriptor, &d3dDescriptor, &texture, &d3d11Texture));
ASSERT_EQ(texture.Get(), nullptr); ASSERT_EQ(texture.Get(), nullptr);
} }
@ -191,7 +195,8 @@ TEST_P(D3D12SharedHandleValidation, InvalidWidth) {
dawnDescriptor.size.width = kTestWidth + 1; dawnDescriptor.size.width = kTestWidth + 1;
dawn::Texture texture; dawn::Texture texture;
ASSERT_DEVICE_ERROR(WrapSharedHandle(&dawnDescriptor, &d3dDescriptor, &texture)); ComPtr<ID3D11Texture2D> d3d11Texture;
ASSERT_DEVICE_ERROR(WrapSharedHandle(&dawnDescriptor, &d3dDescriptor, &texture, &d3d11Texture));
ASSERT_EQ(texture.Get(), nullptr); ASSERT_EQ(texture.Get(), nullptr);
} }
@ -202,7 +207,8 @@ TEST_P(D3D12SharedHandleValidation, InvalidHeight) {
dawnDescriptor.size.height = kTestHeight + 1; dawnDescriptor.size.height = kTestHeight + 1;
dawn::Texture texture; dawn::Texture texture;
ASSERT_DEVICE_ERROR(WrapSharedHandle(&dawnDescriptor, &d3dDescriptor, &texture)); ComPtr<ID3D11Texture2D> d3d11Texture;
ASSERT_DEVICE_ERROR(WrapSharedHandle(&dawnDescriptor, &d3dDescriptor, &texture, &d3d11Texture));
ASSERT_EQ(texture.Get(), nullptr); ASSERT_EQ(texture.Get(), nullptr);
} }
@ -213,7 +219,8 @@ TEST_P(D3D12SharedHandleValidation, InvalidFormat) {
dawnDescriptor.format = dawn::TextureFormat::R8Unorm; dawnDescriptor.format = dawn::TextureFormat::R8Unorm;
dawn::Texture texture; dawn::Texture texture;
ASSERT_DEVICE_ERROR(WrapSharedHandle(&dawnDescriptor, &d3dDescriptor, &texture)); ComPtr<ID3D11Texture2D> d3d11Texture;
ASSERT_DEVICE_ERROR(WrapSharedHandle(&dawnDescriptor, &d3dDescriptor, &texture, &d3d11Texture));
ASSERT_EQ(texture.Get(), nullptr); ASSERT_EQ(texture.Get(), nullptr);
} }
@ -224,7 +231,8 @@ TEST_P(D3D12SharedHandleValidation, InvalidNumD3DMipLevels) {
d3dDescriptor.MipLevels = 2; d3dDescriptor.MipLevels = 2;
dawn::Texture texture; dawn::Texture texture;
ASSERT_DEVICE_ERROR(WrapSharedHandle(&dawnDescriptor, &d3dDescriptor, &texture)); ComPtr<ID3D11Texture2D> d3d11Texture;
ASSERT_DEVICE_ERROR(WrapSharedHandle(&dawnDescriptor, &d3dDescriptor, &texture, &d3d11Texture));
ASSERT_EQ(texture.Get(), nullptr); ASSERT_EQ(texture.Get(), nullptr);
} }
@ -235,9 +243,252 @@ TEST_P(D3D12SharedHandleValidation, InvalidD3DArraySize) {
d3dDescriptor.ArraySize = 2; d3dDescriptor.ArraySize = 2;
dawn::Texture texture; dawn::Texture texture;
ASSERT_DEVICE_ERROR(WrapSharedHandle(&dawnDescriptor, &d3dDescriptor, &texture)); ComPtr<ID3D11Texture2D> d3d11Texture;
ASSERT_DEVICE_ERROR(WrapSharedHandle(&dawnDescriptor, &d3dDescriptor, &texture, &d3d11Texture));
ASSERT_EQ(texture.Get(), nullptr); ASSERT_EQ(texture.Get(), nullptr);
} }
class D3D12SharedHandleUsageTests : public D3D12ResourceTestBase {
protected:
// Submits a 1x1x1 copy from source to destination
void SimpleCopyTextureToTexture(dawn::Texture source, dawn::Texture destination) {
dawn::TextureCopyView copySrc;
copySrc.texture = source;
copySrc.mipLevel = 0;
copySrc.arrayLayer = 0;
copySrc.origin = {0, 0, 0};
dawn::TextureCopyView copyDst;
copyDst.texture = destination;
copyDst.mipLevel = 0;
copyDst.arrayLayer = 0;
copyDst.origin = {0, 0, 0};
dawn::Extent3D copySize = {1, 1, 1};
dawn::CommandEncoder encoder = device.CreateCommandEncoder();
encoder.CopyTextureToTexture(&copySrc, &copyDst, &copySize);
dawn::CommandBuffer commands = encoder.Finish();
queue.Submit(1, &commands);
}
// Clear a texture on a given device
void ClearImage(dawn::Texture wrappedTexture, const dawn::Color& clearColor) {
dawn::TextureView wrappedView = wrappedTexture.CreateView();
// Submit a clear operation
utils::ComboRenderPassDescriptor renderPassDescriptor({wrappedView}, {});
renderPassDescriptor.cColorAttachments[0].clearColor = clearColor;
dawn::CommandEncoder encoder = device.CreateCommandEncoder();
dawn::RenderPassEncoder pass = encoder.BeginRenderPass(&renderPassDescriptor);
pass.EndPass();
dawn::CommandBuffer commands = encoder.Finish();
queue.Submit(1, &commands);
}
void WrapAndClearD3D11Texture(const dawn::TextureDescriptor* dawnDescriptor,
const D3D11_TEXTURE2D_DESC* d3dDescriptor,
dawn::Texture* dawnTextureOut,
const dawn::Color& clearColor,
ID3D11Texture2D** d3d11TextureOut,
IDXGIKeyedMutex** dxgiKeyedMutexOut) const {
ComPtr<ID3D11Texture2D> d3d11Texture;
HRESULT hr = mD3d11Device->CreateTexture2D(d3dDescriptor, nullptr, &d3d11Texture);
ASSERT_EQ(hr, S_OK);
ComPtr<IDXGIResource1> dxgiResource;
hr = d3d11Texture.As(&dxgiResource);
ASSERT_EQ(hr, S_OK);
HANDLE sharedHandle;
hr = dxgiResource->CreateSharedHandle(
nullptr, DXGI_SHARED_RESOURCE_READ | DXGI_SHARED_RESOURCE_WRITE, nullptr,
&sharedHandle);
ASSERT_EQ(hr, S_OK);
ComPtr<IDXGIKeyedMutex> dxgiKeyedMutex;
hr = d3d11Texture.As(&dxgiKeyedMutex);
ASSERT_EQ(hr, S_OK);
ComPtr<ID3D11RenderTargetView> d3d11RTV;
hr = mD3d11Device->CreateRenderTargetView(d3d11Texture.Get(), nullptr, &d3d11RTV);
ASSERT_EQ(hr, S_OK);
hr = dxgiKeyedMutex->AcquireSync(0, INFINITE);
ASSERT_EQ(hr, S_OK);
const float colorRGBA[] = {clearColor.r, clearColor.g, clearColor.b, clearColor.a};
mD3d11DeviceContext->ClearRenderTargetView(d3d11RTV.Get(), colorRGBA);
hr = dxgiKeyedMutex->ReleaseSync(1);
ASSERT_EQ(hr, S_OK);
DawnTexture dawnTexture = dawn_native::d3d12::WrapSharedHandle(
device.Get(), reinterpret_cast<const DawnTextureDescriptor*>(dawnDescriptor),
sharedHandle, 1);
*dawnTextureOut = dawn::Texture::Acquire(dawnTexture);
*d3d11TextureOut = d3d11Texture.Detach();
*dxgiKeyedMutexOut = dxgiKeyedMutex.Detach();
}
void ExpectPixelRGBA8EQ(UINT64 acquireKey,
ID3D11Texture2D* d3d11Texture,
IDXGIKeyedMutex* dxgiKeyedMutex,
const dawn::Color& color) {
HRESULT hr = dxgiKeyedMutex->AcquireSync(acquireKey, INFINITE);
ASSERT_EQ(hr, S_OK);
D3D11_TEXTURE2D_DESC texture2DDesc;
d3d11Texture->GetDesc(&texture2DDesc);
const CD3D11_TEXTURE2D_DESC texture2DStagingDesc(
texture2DDesc.Format, // Format
texture2DDesc.Width, // Width
texture2DDesc.Height, // Height
1, // ArraySize
1, // MipLevels
0, // BindFlags
D3D11_USAGE_STAGING, // Usage
D3D11_CPU_ACCESS_READ | D3D11_CPU_ACCESS_WRITE); // CPUAccessFlags
ComPtr<ID3D11Texture2D> spD3DTextureStaging;
hr = mD3d11Device->CreateTexture2D(&texture2DStagingDesc, nullptr, &spD3DTextureStaging);
ASSERT_EQ(hr, S_OK);
D3D11_BOX d3dRc;
d3dRc.back = 1;
d3dRc.front = 0;
d3dRc.top = 0;
d3dRc.left = 0;
d3dRc.bottom = texture2DDesc.Height;
d3dRc.right = texture2DDesc.Width;
mD3d11DeviceContext->CopySubresourceRegion(spD3DTextureStaging.Get(), // pDstResource
0, // DstSubresource
0, // DstX
0, // DstY
0, // DstZ
d3d11Texture, // pSrcResource
0, // SrcSubresource
&d3dRc); // pSrcBox
D3D11_MAPPED_SUBRESOURCE mappedResource;
hr = mD3d11DeviceContext->Map(spD3DTextureStaging.Get(), 0, D3D11_MAP_READ_WRITE, 0,
&mappedResource);
ASSERT_EQ(hr, S_OK);
const uint8_t* colorData = static_cast<uint8_t*>(mappedResource.pData);
EXPECT_EQ(colorData[0], color.r * 255u);
EXPECT_EQ(colorData[1], color.g * 255u);
EXPECT_EQ(colorData[2], color.b * 255u);
EXPECT_EQ(colorData[3], color.a * 255u);
mD3d11DeviceContext->Unmap(spD3DTextureStaging.Get(), 0);
hr = dxgiKeyedMutex->ReleaseSync(acquireKey + 1);
ASSERT_EQ(hr, S_OK);
}
};
// 1. Create and clear a D3D11 texture
// 2. Copy the wrapped texture to another dawn texture
// 3. Readback the copied texture and ensure the color matches the original clear color.
TEST_P(D3D12SharedHandleUsageTests, ClearInD3D11CopyAndReadbackInD3D12) {
DAWN_SKIP_TEST_IF(UsesWire());
const dawn::Color clearColor{1.0f, 1.0f, 0.0f, 1.0f};
dawn::Texture dawnSrcTexture;
ComPtr<ID3D11Texture2D> d3d11Texture;
ComPtr<IDXGIKeyedMutex> dxgiKeyedMutex;
WrapAndClearD3D11Texture(&dawnDescriptor, &d3dDescriptor, &dawnSrcTexture, clearColor,
&d3d11Texture, &dxgiKeyedMutex);
// Create a texture on the device and copy the source texture to it.
dawn::Texture dawnCopyDestTexture = device.CreateTexture(&dawnDescriptor);
SimpleCopyTextureToTexture(dawnSrcTexture, dawnCopyDestTexture);
// Readback the destination texture and ensure it contains the colors we used
// to clear the source texture on the D3D device.
EXPECT_PIXEL_RGBA8_EQ(
RGBA8(clearColor.r * 255u, clearColor.g * 255u, clearColor.b * 255u, clearColor.a * 255u),
dawnCopyDestTexture, 0, 0);
}
// 1. Create and clear a D3D11 texture
// 2. Readback the wrapped texture and ensure the color matches the original clear color.
TEST_P(D3D12SharedHandleUsageTests, ClearInD3D11ReadbackInD3D12) {
DAWN_SKIP_TEST_IF(UsesWire());
const dawn::Color clearColor{1.0f, 1.0f, 0.0f, 1.0f};
dawn::Texture dawnTexture;
ComPtr<ID3D11Texture2D> d3d11Texture;
ComPtr<IDXGIKeyedMutex> dxgiKeyedMutex;
WrapAndClearD3D11Texture(&dawnDescriptor, &d3dDescriptor, &dawnTexture, clearColor,
&d3d11Texture, &dxgiKeyedMutex);
// Readback the destination texture and ensure it contains the colors we used
// to clear the source texture on the D3D device.
EXPECT_PIXEL_RGBA8_EQ(
RGBA8(clearColor.r * 255, clearColor.g * 255, clearColor.b * 255, clearColor.a * 255),
dawnTexture, 0, 0);
}
// 1. Create and clear a D3D11 texture
// 2. Wrap it in a Dawn texture and clear it to a different color
// 3. Readback the texture with D3D11 and ensure we receive the color we cleared with Dawn.
TEST_P(D3D12SharedHandleUsageTests, ClearInD3D12ReadbackInD3D11) {
DAWN_SKIP_TEST_IF(UsesWire());
const dawn::Color d3d11ClearColor{1.0f, 1.0f, 0.0f, 1.0f};
dawn::Texture dawnTexture;
ComPtr<ID3D11Texture2D> d3d11Texture;
ComPtr<IDXGIKeyedMutex> dxgiKeyedMutex;
WrapAndClearD3D11Texture(&dawnDescriptor, &d3dDescriptor, &dawnTexture, d3d11ClearColor,
&d3d11Texture, &dxgiKeyedMutex);
const dawn::Color d3d12ClearColor{0.0f, 0.0f, 1.0f, 1.0f};
ClearImage(dawnTexture, d3d12ClearColor);
dawnTexture.Destroy();
// Now that Dawn (via D3D12) has finished writing to the texture, we should be
// able to read it back by copying it to a staging texture and verifying the
// color matches the D3D12 clear color.
ExpectPixelRGBA8EQ(2, d3d11Texture.Get(), dxgiKeyedMutex.Get(), d3d12ClearColor);
}
// 1. Create and clear a D3D11 texture
// 2. Wrap it in a Dawn texture and clear the texture to two different colors.
// 3. Readback the texture with D3D11.
// 4. Verify the readback color was the final color cleared.
TEST_P(D3D12SharedHandleUsageTests, ClearTwiceInD3D12ReadbackInD3D11) {
DAWN_SKIP_TEST_IF(UsesWire());
const dawn::Color d3d11ClearColor{1.0f, 1.0f, 0.0f, 1.0f};
dawn::Texture dawnTexture;
ComPtr<ID3D11Texture2D> d3d11Texture;
ComPtr<IDXGIKeyedMutex> dxgiKeyedMutex;
WrapAndClearD3D11Texture(&dawnDescriptor, &d3dDescriptor, &dawnTexture, d3d11ClearColor,
&d3d11Texture, &dxgiKeyedMutex);
const dawn::Color d3d12ClearColor1{0.0f, 0.0f, 1.0f, 1.0f};
ClearImage(dawnTexture, d3d12ClearColor1);
const dawn::Color d3d12ClearColor2{0.0f, 1.0f, 1.0f, 1.0f};
ClearImage(dawnTexture, d3d12ClearColor2);
dawnTexture.Destroy();
// Now that Dawn (via D3D12) has finished writing to the texture, we should be
// able to read it back by copying it to a staging texture and verifying the
// color matches the last D3D12 clear color.
ExpectPixelRGBA8EQ(2, d3d11Texture.Get(), dxgiKeyedMutex.Get(), d3d12ClearColor2);
}
DAWN_INSTANTIATE_TEST(D3D12SharedHandleValidation, D3D12Backend); DAWN_INSTANTIATE_TEST(D3D12SharedHandleValidation, D3D12Backend);
DAWN_INSTANTIATE_TEST(D3D12SharedHandleUsageTests, D3D12Backend);