D3D12: Use Texture::Create pattern for swapchain textures

Bug: dawn:269
Change-Id: Ia4b48126c153ddff2feefc1bf04dcc9adc783bd1
Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/37421
Auto-Submit: Corentin Wallez <cwallez@chromium.org>
Reviewed-by: Austin Eng <enga@chromium.org>
Reviewed-by: Rafael Cintron <rafael.cintron@microsoft.com>
Reviewed-by: Corentin Wallez <cwallez@chromium.org>
Commit-Queue: Corentin Wallez <cwallez@chromium.org>
This commit is contained in:
Corentin Wallez 2021-01-13 08:53:34 +00:00 committed by Commit Bot service account
parent 7e4aa3d4e6
commit 111ba65a5e
4 changed files with 55 additions and 35 deletions

View File

@ -446,13 +446,13 @@ namespace dawn_native { namespace d3d12 {
HANDLE sharedHandle, HANDLE sharedHandle,
ExternalMutexSerial acquireMutexKey, ExternalMutexSerial acquireMutexKey,
bool isSwapChainTexture) { bool isSwapChainTexture) {
Ref<TextureBase> dawnTexture; Ref<Texture> dawnTexture;
if (ConsumedError(Texture::Create(this, descriptor, sharedHandle, acquireMutexKey, if (ConsumedError(Texture::Create(this, descriptor, sharedHandle, acquireMutexKey,
isSwapChainTexture), isSwapChainTexture),
&dawnTexture)) &dawnTexture)) {
return nullptr; return nullptr;
}
return dawnTexture; return {dawnTexture};
} }
// We use IDXGIKeyedMutexes to synchronize access between D3D11 and D3D12. D3D11/12 fences // We use IDXGIKeyedMutexes to synchronize access between D3D11 and D3D12. D3D11/12 fences

View File

@ -36,16 +36,24 @@ namespace dawn_native { namespace d3d12 {
} }
TextureBase* SwapChain::GetNextTextureImpl(const TextureDescriptor* descriptor) { TextureBase* SwapChain::GetNextTextureImpl(const TextureDescriptor* descriptor) {
DeviceBase* device = GetDevice();
const auto& im = GetImplementation(); const auto& im = GetImplementation();
DawnSwapChainNextTexture next = {}; DawnSwapChainNextTexture next = {};
DawnSwapChainError error = im.GetNextTexture(im.userData, &next); DawnSwapChainError error = im.GetNextTexture(im.userData, &next);
if (error) { if (error) {
GetDevice()->HandleError(InternalErrorType::Internal, error); device->HandleError(InternalErrorType::Internal, error);
return nullptr; return nullptr;
} }
ComPtr<ID3D12Resource> d3d12Texture = static_cast<ID3D12Resource*>(next.texture.ptr); ComPtr<ID3D12Resource> d3d12Texture = static_cast<ID3D12Resource*>(next.texture.ptr);
return new Texture(ToBackend(GetDevice()), descriptor, std::move(d3d12Texture)); Ref<Texture> dawnTexture;
if (device->ConsumedError(
Texture::Create(ToBackend(GetDevice()), descriptor, std::move(d3d12Texture)),
&dawnTexture)) {
return nullptr;
}
return dawnTexture.Detach();
} }
MaybeError SwapChain::OnBeforePresent(TextureViewBase* view) { MaybeError SwapChain::OnBeforePresent(TextureViewBase* view) {

View File

@ -378,19 +378,21 @@ namespace dawn_native { namespace d3d12 {
return {}; return {};
} }
ResultOrError<Ref<TextureBase>> Texture::Create(Device* device, // static
const TextureDescriptor* descriptor) { ResultOrError<Ref<Texture>> Texture::Create(Device* device,
const TextureDescriptor* descriptor) {
Ref<Texture> dawnTexture = Ref<Texture> dawnTexture =
AcquireRef(new Texture(device, descriptor, TextureState::OwnedInternal)); AcquireRef(new Texture(device, descriptor, TextureState::OwnedInternal));
DAWN_TRY(dawnTexture->InitializeAsInternalTexture()); DAWN_TRY(dawnTexture->InitializeAsInternalTexture());
return std::move(dawnTexture); return std::move(dawnTexture);
} }
ResultOrError<Ref<TextureBase>> Texture::Create(Device* device, // static
const ExternalImageDescriptor* descriptor, ResultOrError<Ref<Texture>> Texture::Create(Device* device,
HANDLE sharedHandle, const ExternalImageDescriptor* descriptor,
ExternalMutexSerial acquireMutexKey, HANDLE sharedHandle,
bool isSwapChainTexture) { ExternalMutexSerial acquireMutexKey,
bool isSwapChainTexture) {
const TextureDescriptor* textureDescriptor = const TextureDescriptor* textureDescriptor =
reinterpret_cast<const TextureDescriptor*>(descriptor->cTextureDescriptor); reinterpret_cast<const TextureDescriptor*>(descriptor->cTextureDescriptor);
@ -403,6 +405,16 @@ namespace dawn_native { namespace d3d12 {
return std::move(dawnTexture); return std::move(dawnTexture);
} }
// static
ResultOrError<Ref<Texture>> Texture::Create(Device* device,
const TextureDescriptor* descriptor,
ComPtr<ID3D12Resource> d3d12Texture) {
Ref<Texture> dawnTexture =
AcquireRef(new Texture(device, descriptor, TextureState::OwnedExternal));
DAWN_TRY(dawnTexture->InitializeAsSwapChainTexture(std::move(d3d12Texture)));
return std::move(dawnTexture);
}
MaybeError Texture::InitializeAsExternalTexture(const TextureDescriptor* descriptor, MaybeError Texture::InitializeAsExternalTexture(const TextureDescriptor* descriptor,
HANDLE sharedHandle, HANDLE sharedHandle,
ExternalMutexSerial acquireMutexKey, ExternalMutexSerial acquireMutexKey,
@ -485,25 +497,24 @@ namespace dawn_native { namespace d3d12 {
return {}; return {};
} }
Texture::Texture(Device* device, const TextureDescriptor* descriptor, TextureState state) MaybeError Texture::InitializeAsSwapChainTexture(ComPtr<ID3D12Resource> d3d12Texture) {
: TextureBase(device, descriptor, state),
mSubresourceStateAndDecay(
GetSubresourceCount(),
{D3D12_RESOURCE_STATES::D3D12_RESOURCE_STATE_COMMON, kMaxExecutionSerial, false}) {
}
Texture::Texture(Device* device,
const TextureDescriptor* descriptor,
ComPtr<ID3D12Resource> nativeTexture)
: Texture(device, descriptor, TextureState::OwnedExternal) {
AllocationInfo info; AllocationInfo info;
info.mMethod = AllocationMethod::kExternal; info.mMethod = AllocationMethod::kExternal;
// When creating the ResourceHeapAllocation, the resource heap is set to nullptr because the // When creating the ResourceHeapAllocation, the resource heap is set to nullptr because the
// texture is owned externally. The texture's owning entity must remain responsible for // texture is owned externally. The texture's owning entity must remain responsible for
// memory management. // memory management.
mResourceAllocation = {info, 0, std::move(nativeTexture), nullptr}; mResourceAllocation = { info, 0, std::move(d3d12Texture), nullptr };
SetIsSubresourceContentInitialized(true, GetAllSubresources()); SetIsSubresourceContentInitialized(true, GetAllSubresources());
return {};
}
Texture::Texture(Device* device, const TextureDescriptor* descriptor, TextureState state)
: TextureBase(device, descriptor, state),
mSubresourceStateAndDecay(
GetSubresourceCount(),
{D3D12_RESOURCE_STATES::D3D12_RESOURCE_STATE_COMMON, kMaxExecutionSerial, false}) {
} }
Texture::~Texture() { Texture::~Texture() {

View File

@ -36,16 +36,16 @@ namespace dawn_native { namespace d3d12 {
class Texture final : public TextureBase { class Texture final : public TextureBase {
public: public:
static ResultOrError<Ref<TextureBase>> Create(Device* device, static ResultOrError<Ref<Texture>> Create(Device* device,
const TextureDescriptor* descriptor); const TextureDescriptor* descriptor);
static ResultOrError<Ref<TextureBase>> Create(Device* device, static ResultOrError<Ref<Texture>> Create(Device* device,
const ExternalImageDescriptor* descriptor, const ExternalImageDescriptor* descriptor,
HANDLE sharedHandle, HANDLE sharedHandle,
ExternalMutexSerial acquireMutexKey, ExternalMutexSerial acquireMutexKey,
bool isSwapChainTexture); bool isSwapChainTexture);
Texture(Device* device, static ResultOrError<Ref<Texture>> Create(Device* device,
const TextureDescriptor* descriptor, const TextureDescriptor* descriptor,
ComPtr<ID3D12Resource> d3d12Texture); ComPtr<ID3D12Resource> d3d12Texture);
DXGI_FORMAT GetD3D12Format() const; DXGI_FORMAT GetD3D12Format() const;
ID3D12Resource* GetD3D12Resource() const; ID3D12Resource* GetD3D12Resource() const;
@ -88,6 +88,7 @@ namespace dawn_native { namespace d3d12 {
HANDLE sharedHandle, HANDLE sharedHandle,
ExternalMutexSerial acquireMutexKey, ExternalMutexSerial acquireMutexKey,
bool isSwapChainTexture); bool isSwapChainTexture);
MaybeError InitializeAsSwapChainTexture(ComPtr<ID3D12Resource> d3d12Texture);
// Dawn API // Dawn API
void DestroyImpl() override; void DestroyImpl() override;