Make SwapChain use a CreateView() instead of APICreateView()

This simplifies reference management and make the swapchain's reentrant
code avoid APIEntryPoints.

Bug: dawn:723
Change-Id: I6c456b9ec349c73d783dbb12a284f322d0be4e1a
Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/84763
Reviewed-by: Loko Kung <lokokung@google.com>
Commit-Queue: Corentin Wallez <cwallez@chromium.org>
This commit is contained in:
Corentin Wallez 2022-03-29 09:35:53 +00:00 committed by Dawn LUCI CQ
parent 3d45533446
commit e690a6654a
12 changed files with 50 additions and 43 deletions

View File

@ -316,34 +316,38 @@ namespace dawn::native {
}
TextureViewBase* NewSwapChainBase::APIGetCurrentTextureView() {
if (GetDevice()->ConsumedError(ValidateGetCurrentTextureView())) {
Ref<TextureViewBase> result;
if (GetDevice()->ConsumedError(GetCurrentTextureView(), &result,
"calling %s.GetCurrentTextureView()", this)) {
return TextureViewBase::MakeError(GetDevice());
}
return result.Detach();
}
ResultOrError<Ref<TextureViewBase>> NewSwapChainBase::GetCurrentTextureView() {
DAWN_TRY(ValidateGetCurrentTextureView());
if (mCurrentTextureView != nullptr) {
// Calling GetCurrentTextureView always returns a new reference so add it even when
// reusing the existing texture view.
mCurrentTextureView->Reference();
return mCurrentTextureView.Get();
// Calling GetCurrentTextureView always returns a new reference.
return mCurrentTextureView;
}
TextureViewBase* view = nullptr;
if (GetDevice()->ConsumedError(GetCurrentTextureViewImpl(), &view)) {
return TextureViewBase::MakeError(GetDevice());
}
DAWN_TRY_ASSIGN(mCurrentTextureView, GetCurrentTextureViewImpl());
// Check that the return texture view matches exactly what was given for this descriptor.
ASSERT(view->GetTexture()->GetFormat().format == mFormat);
ASSERT(IsSubset(mUsage, view->GetTexture()->GetUsage()));
ASSERT(view->GetLevelCount() == 1);
ASSERT(view->GetLayerCount() == 1);
ASSERT(view->GetDimension() == wgpu::TextureViewDimension::e2D);
ASSERT(view->GetTexture()->GetMipLevelVirtualSize(view->GetBaseMipLevel()).width == mWidth);
ASSERT(view->GetTexture()->GetMipLevelVirtualSize(view->GetBaseMipLevel()).height ==
mHeight);
ASSERT(mCurrentTextureView->GetTexture()->GetFormat().format == mFormat);
ASSERT(IsSubset(mUsage, mCurrentTextureView->GetTexture()->GetUsage()));
ASSERT(mCurrentTextureView->GetLevelCount() == 1);
ASSERT(mCurrentTextureView->GetLayerCount() == 1);
ASSERT(mCurrentTextureView->GetDimension() == wgpu::TextureViewDimension::e2D);
ASSERT(mCurrentTextureView->GetTexture()
->GetMipLevelVirtualSize(mCurrentTextureView->GetBaseMipLevel())
.width == mWidth);
ASSERT(mCurrentTextureView->GetTexture()
->GetMipLevelVirtualSize(mCurrentTextureView->GetBaseMipLevel())
.height == mHeight);
mCurrentTextureView = view;
return view;
return mCurrentTextureView;
}
void NewSwapChainBase::APIPresent() {

View File

@ -154,7 +154,8 @@ namespace dawn::native {
// manner, starting with GetCurrentTextureViewImpl.
// The returned texture view must match the swapchain descriptor exactly.
virtual ResultOrError<TextureViewBase*> GetCurrentTextureViewImpl() = 0;
ResultOrError<Ref<TextureViewBase>> GetCurrentTextureView();
virtual ResultOrError<Ref<TextureViewBase>> GetCurrentTextureViewImpl() = 0;
// The call to present must destroy the current view's texture so further access to it are
// invalid.
virtual MaybeError PresentImpl() = 0;

View File

@ -668,12 +668,17 @@ namespace dawn::native {
return {clampedCopyExtentWidth, clampedCopyExtentHeight, extent.depthOrArrayLayers};
}
ResultOrError<Ref<TextureViewBase>> TextureBase::CreateView(
const TextureViewDescriptor* descriptor) {
return GetDevice()->CreateTextureView(this, descriptor);
}
TextureViewBase* TextureBase::APICreateView(const TextureViewDescriptor* descriptor) {
DeviceBase* device = GetDevice();
Ref<TextureViewBase> result;
if (device->ConsumedError(device->CreateTextureView(this, descriptor), &result,
"calling %s.CreateView(%s).", this, descriptor)) {
if (device->ConsumedError(CreateView(descriptor), &result, "calling %s.CreateView(%s).",
this, descriptor)) {
return TextureViewBase::MakeError(device);
}
return result.Detach();

View File

@ -91,6 +91,9 @@ namespace dawn::native {
const Origin3D& origin,
const Extent3D& extent) const;
ResultOrError<Ref<TextureViewBase>> CreateView(
const TextureViewDescriptor* descriptor = nullptr);
// Dawn API
TextureViewBase* APICreateView(const TextureViewDescriptor* descriptor = nullptr);
void APIDestroy();

View File

@ -332,7 +332,7 @@ namespace dawn::native::d3d12 {
return {};
}
ResultOrError<TextureViewBase*> SwapChain::GetCurrentTextureViewImpl() {
ResultOrError<Ref<TextureViewBase>> SwapChain::GetCurrentTextureViewImpl() {
Device* device = ToBackend(GetDevice());
// Synchronously wait until previous operations on the next swapchain buffer are finished.
@ -346,9 +346,7 @@ namespace dawn::native::d3d12 {
TextureDescriptor descriptor = GetSwapChainBaseTextureDescriptor(this);
DAWN_TRY_ASSIGN(mApiTexture, Texture::Create(ToBackend(GetDevice()), &descriptor,
mBuffers[mCurrentBuffer]));
// TODO(dawn:723): change to not use AcquireRef for reentrant object creation.
return mApiTexture->APICreateView();
return mApiTexture->CreateView();
}
MaybeError SwapChain::DetachAndWaitForDeallocation() {

View File

@ -63,7 +63,7 @@ namespace dawn::native::d3d12 {
// NewSwapChainBase implementation
MaybeError PresentImpl() override;
ResultOrError<TextureViewBase*> GetCurrentTextureViewImpl() override;
ResultOrError<Ref<TextureViewBase>> GetCurrentTextureViewImpl() override;
void DetachFromSurfaceImpl() override;
// Does the swapchain initialization steps assuming there is nothing we can reuse.

View File

@ -58,7 +58,7 @@ namespace dawn::native::metal {
Ref<Texture> mTexture;
MaybeError PresentImpl() override;
ResultOrError<TextureViewBase*> GetCurrentTextureViewImpl() override;
ResultOrError<Ref<TextureViewBase>> GetCurrentTextureViewImpl() override;
void DetachFromSurfaceImpl() override;
};

View File

@ -129,7 +129,7 @@ namespace dawn::native::metal {
return {};
}
ResultOrError<TextureViewBase*> SwapChain::GetCurrentTextureViewImpl() {
ResultOrError<Ref<TextureViewBase>> SwapChain::GetCurrentTextureViewImpl() {
ASSERT(mCurrentDrawable == nullptr);
mCurrentDrawable = [*mLayer nextDrawable];
@ -137,8 +137,7 @@ namespace dawn::native::metal {
mTexture = Texture::CreateWrapping(ToBackend(GetDevice()), &textureDesc,
[*mCurrentDrawable texture]);
// TODO(dawn:723): change to not use AcquireRef for reentrant object creation.
return mTexture->APICreateView();
return mTexture->CreateView();
}
void SwapChain::DetachFromSurfaceImpl() {

View File

@ -421,13 +421,12 @@ namespace dawn::native::null {
return {};
}
ResultOrError<TextureViewBase*> SwapChain::GetCurrentTextureViewImpl() {
ResultOrError<Ref<TextureViewBase>> SwapChain::GetCurrentTextureViewImpl() {
TextureDescriptor textureDesc = GetSwapChainBaseTextureDescriptor(this);
// TODO(dawn:723): change to not use AcquireRef for reentrant object creation.
mTexture = AcquireRef(
new Texture(GetDevice(), &textureDesc, TextureBase::TextureState::OwnedInternal));
// TODO(dawn:723): change to not use AcquireRef for reentrant object creation.
return mTexture->APICreateView();
return mTexture->CreateView();
}
void SwapChain::DetachFromSurfaceImpl() {

View File

@ -297,7 +297,7 @@ namespace dawn::native::null {
Ref<Texture> mTexture;
MaybeError PresentImpl() override;
ResultOrError<TextureViewBase*> GetCurrentTextureViewImpl() override;
ResultOrError<Ref<TextureViewBase>> GetCurrentTextureViewImpl() override;
void DetachFromSurfaceImpl() override;
};

View File

@ -593,11 +593,11 @@ namespace dawn::native::vulkan {
}
}
ResultOrError<TextureViewBase*> SwapChain::GetCurrentTextureViewImpl() {
ResultOrError<Ref<TextureViewBase>> SwapChain::GetCurrentTextureViewImpl() {
return GetCurrentTextureViewInternal();
}
ResultOrError<TextureViewBase*> SwapChain::GetCurrentTextureViewInternal(bool isReentrant) {
ResultOrError<Ref<TextureViewBase>> SwapChain::GetCurrentTextureViewInternal(bool isReentrant) {
Device* device = ToBackend(GetDevice());
// Transiently create a semaphore that will be signaled when the presentation engine is done
@ -664,8 +664,7 @@ namespace dawn::native::vulkan {
// In the happy path we can use the swapchain image directly.
if (!mConfig.needsBlit) {
// TODO(dawn:723): change to not use AcquireRef for reentrant object creation.
return mTexture->APICreateView();
return mTexture->CreateView();
}
// The blit texture always perfectly matches what the user requested for the swapchain.
@ -673,8 +672,7 @@ namespace dawn::native::vulkan {
TextureDescriptor desc = GetSwapChainBaseTextureDescriptor(this);
DAWN_TRY_ASSIGN(mBlitTexture,
Texture::Create(device, &desc, VK_IMAGE_USAGE_TRANSFER_SRC_BIT));
// TODO(dawn:723): change to not use AcquireRef for reentrant object creation.
return mBlitTexture->APICreateView();
return mBlitTexture->CreateView();
}
void SwapChain::DetachFromSurfaceImpl() {

View File

@ -75,11 +75,11 @@ namespace dawn::native::vulkan {
bool needsBlit = false;
};
ResultOrError<Config> ChooseConfig(const VulkanSurfaceInfo& surfaceInfo) const;
ResultOrError<TextureViewBase*> GetCurrentTextureViewInternal(bool isReentrant = false);
ResultOrError<Ref<TextureViewBase>> GetCurrentTextureViewInternal(bool isReentrant = false);
// NewSwapChainBase implementation
MaybeError PresentImpl() override;
ResultOrError<TextureViewBase*> GetCurrentTextureViewImpl() override;
ResultOrError<Ref<TextureViewBase>> GetCurrentTextureViewImpl() override;
void DetachFromSurfaceImpl() override;
Config mConfig;