From aef480bcfe9ba15b2bba167e4ce077957c8ffe37 Mon Sep 17 00:00:00 2001 From: Jiawei Shao Date: Thu, 18 Oct 2018 06:00:09 +0000 Subject: [PATCH] Implement CreateDefaultTextureView by CreateTextureView This patch removes CreateDefaultTextureView in all the back-ends of class Device and implements this function by CreateTextureView using a TextureViewDescriptor created from the original texture. Note that this patch only refactors the original implementation of creating default texture. The support of creating texture views from a texture view descriptor will be added in the next several patches. BUG=dawn:16 Change-Id: Iadfc1e17e1cf23a4c1fa8ff44b1fb1a765d21e3f Reviewed-on: https://dawn-review.googlesource.com/c/1840 Reviewed-by: Corentin Wallez Reviewed-by: Kai Ninomiya Commit-Queue: Jiawei Shao --- src/dawn_native/Device.h | 1 - src/dawn_native/Texture.cpp | 32 ++++++++++++++++++++++---- src/dawn_native/Texture.h | 2 +- src/dawn_native/d3d12/DeviceD3D12.cpp | 6 +---- src/dawn_native/d3d12/DeviceD3D12.h | 1 - src/dawn_native/d3d12/TextureD3D12.cpp | 4 +++- src/dawn_native/d3d12/TextureD3D12.h | 2 +- src/dawn_native/metal/DeviceMTL.h | 1 - src/dawn_native/metal/DeviceMTL.mm | 6 +---- src/dawn_native/metal/TextureMTL.h | 2 +- src/dawn_native/metal/TextureMTL.mm | 4 +++- src/dawn_native/null/NullBackend.cpp | 6 +---- src/dawn_native/null/NullBackend.h | 1 - src/dawn_native/opengl/DeviceGL.cpp | 6 +---- src/dawn_native/opengl/DeviceGL.h | 1 - src/dawn_native/opengl/TextureGL.cpp | 4 +++- src/dawn_native/opengl/TextureGL.h | 2 +- src/dawn_native/vulkan/DeviceVk.cpp | 6 +---- src/dawn_native/vulkan/DeviceVk.h | 1 - src/dawn_native/vulkan/TextureVk.cpp | 4 +++- src/dawn_native/vulkan/TextureVk.h | 2 +- 21 files changed, 49 insertions(+), 45 deletions(-) diff --git a/src/dawn_native/Device.h b/src/dawn_native/Device.h index dfa947a21c..59c027212a 100644 --- a/src/dawn_native/Device.h +++ b/src/dawn_native/Device.h @@ -57,7 +57,6 @@ namespace dawn_native { RenderPassDescriptorBuilder* builder) = 0; virtual RenderPipelineBase* CreateRenderPipeline(RenderPipelineBuilder* builder) = 0; virtual SwapChainBase* CreateSwapChain(SwapChainBuilder* builder) = 0; - virtual TextureViewBase* CreateDefaultTextureView(TextureBase* texture) = 0; virtual void TickImpl() = 0; diff --git a/src/dawn_native/Texture.cpp b/src/dawn_native/Texture.cpp index eeb4aa2f68..23ceb75ec3 100644 --- a/src/dawn_native/Texture.cpp +++ b/src/dawn_native/Texture.cpp @@ -79,6 +79,30 @@ namespace dawn_native { return {}; } + TextureViewDescriptor MakeDefaultTextureViewDescriptor(const TextureBase* texture) { + TextureViewDescriptor descriptor; + descriptor.format = texture->GetFormat(); + descriptor.baseArrayLayer = 0; + descriptor.layerCount = texture->GetArrayLayers(); + descriptor.baseMipLevel = 0; + descriptor.levelCount = texture->GetNumMipLevels(); + + // TODO(jiawei.shao@intel.com): support all texture dimensions. + switch (texture->GetDimension()) { + case dawn::TextureDimension::e2D: + if (texture->GetArrayLayers() == 1u) { + descriptor.dimension = dawn::TextureViewDimension::e2D; + } else { + descriptor.dimension = dawn::TextureViewDimension::e2DArray; + } + break; + default: + UNREACHABLE(); + } + + return descriptor; + } + } // anonymous namespace MaybeError ValidateTextureDescriptor(DeviceBase*, const TextureDescriptor* descriptor) { @@ -209,10 +233,8 @@ namespace dawn_native { } TextureViewBase* TextureBase::CreateDefaultTextureView() { - // TODO(jiawei.shao@intel.com): remove Device->CreateDefaultTextureView in all back-ends - // and implement this function by creating a TextureViewDescriptor based on the texture - // and calling CreateTextureView(&descriptor) instead. - return GetDevice()->CreateDefaultTextureView(this); + TextureViewDescriptor descriptor = MakeDefaultTextureViewDescriptor(this); + return GetDevice()->CreateTextureView(this, &descriptor); } TextureViewBase* TextureBase::CreateTextureView(const TextureViewDescriptor* descriptor) { @@ -221,7 +243,7 @@ namespace dawn_native { // TextureViewBase - TextureViewBase::TextureViewBase(TextureBase* texture) + TextureViewBase::TextureViewBase(TextureBase* texture, const TextureViewDescriptor* descriptor) : ObjectBase(texture->GetDevice()), mTexture(texture) { } diff --git a/src/dawn_native/Texture.h b/src/dawn_native/Texture.h index 337f1ba473..1817bc1198 100644 --- a/src/dawn_native/Texture.h +++ b/src/dawn_native/Texture.h @@ -67,7 +67,7 @@ namespace dawn_native { class TextureViewBase : public ObjectBase { public: - TextureViewBase(TextureBase* texture); + TextureViewBase(TextureBase* texture, const TextureViewDescriptor* descriptor); const TextureBase* GetTexture() const; TextureBase* GetTexture(); diff --git a/src/dawn_native/d3d12/DeviceD3D12.cpp b/src/dawn_native/d3d12/DeviceD3D12.cpp index dad2bb7850..231a4eee59 100644 --- a/src/dawn_native/d3d12/DeviceD3D12.cpp +++ b/src/dawn_native/d3d12/DeviceD3D12.cpp @@ -334,14 +334,10 @@ namespace dawn_native { namespace d3d12 { ResultOrError Device::CreateTextureImpl(const TextureDescriptor* descriptor) { return new Texture(this, descriptor); } - TextureViewBase* Device::CreateDefaultTextureView(TextureBase* texture) { - return new TextureView(texture); - } - // TODO(jiawei.shao@intel.com): implement creating texture view with TextureViewDescriptor ResultOrError Device::CreateTextureViewImpl( TextureBase* texture, const TextureViewDescriptor* descriptor) { - return DAWN_UNIMPLEMENTED_ERROR("Creating texture view with descriptor is unimplemented."); + return new TextureView(texture, descriptor); } void Device::CollectPCIInfo() { diff --git a/src/dawn_native/d3d12/DeviceD3D12.h b/src/dawn_native/d3d12/DeviceD3D12.h index 9ac0e1b2d2..3717e2453e 100644 --- a/src/dawn_native/d3d12/DeviceD3D12.h +++ b/src/dawn_native/d3d12/DeviceD3D12.h @@ -51,7 +51,6 @@ namespace dawn_native { namespace d3d12 { RenderPassDescriptorBuilder* builder) override; RenderPipelineBase* CreateRenderPipeline(RenderPipelineBuilder* builder) override; SwapChainBase* CreateSwapChain(SwapChainBuilder* builder) override; - TextureViewBase* CreateDefaultTextureView(TextureBase* texture) override; void TickImpl() override; diff --git a/src/dawn_native/d3d12/TextureD3D12.cpp b/src/dawn_native/d3d12/TextureD3D12.cpp index a7d0a78d8c..0a52364e8e 100644 --- a/src/dawn_native/d3d12/TextureD3D12.cpp +++ b/src/dawn_native/d3d12/TextureD3D12.cpp @@ -187,7 +187,9 @@ namespace dawn_native { namespace d3d12 { mLastUsage = usage; } - TextureView::TextureView(TextureBase* texture) : TextureViewBase(texture) { + // TODO(jiawei.shao@intel.com): create texture view by TextureViewDescriptor + TextureView::TextureView(TextureBase* texture, const TextureViewDescriptor* descriptor) + : TextureViewBase(texture, descriptor) { mSrvDesc.Format = D3D12TextureFormat(GetTexture()->GetFormat()); mSrvDesc.Shader4ComponentMapping = D3D12_DEFAULT_SHADER_4_COMPONENT_MAPPING; switch (GetTexture()->GetDimension()) { diff --git a/src/dawn_native/d3d12/TextureD3D12.h b/src/dawn_native/d3d12/TextureD3D12.h index 58a2488c2f..23a0d52de1 100644 --- a/src/dawn_native/d3d12/TextureD3D12.h +++ b/src/dawn_native/d3d12/TextureD3D12.h @@ -47,7 +47,7 @@ namespace dawn_native { namespace d3d12 { class TextureView : public TextureViewBase { public: - TextureView(TextureBase* builder); + TextureView(TextureBase* texture, const TextureViewDescriptor* descriptor); const D3D12_SHADER_RESOURCE_VIEW_DESC& GetSRVDescriptor() const; D3D12_RENDER_TARGET_VIEW_DESC GetRTVDescriptor(); diff --git a/src/dawn_native/metal/DeviceMTL.h b/src/dawn_native/metal/DeviceMTL.h index 9d0998df8e..da586ee459 100644 --- a/src/dawn_native/metal/DeviceMTL.h +++ b/src/dawn_native/metal/DeviceMTL.h @@ -47,7 +47,6 @@ namespace dawn_native { namespace metal { RenderPassDescriptorBuilder* builder) override; RenderPipelineBase* CreateRenderPipeline(RenderPipelineBuilder* builder) override; SwapChainBase* CreateSwapChain(SwapChainBuilder* builder) override; - TextureViewBase* CreateDefaultTextureView(TextureBase* texture) override; void TickImpl() override; diff --git a/src/dawn_native/metal/DeviceMTL.mm b/src/dawn_native/metal/DeviceMTL.mm index 7036830711..4d62e5d124 100644 --- a/src/dawn_native/metal/DeviceMTL.mm +++ b/src/dawn_native/metal/DeviceMTL.mm @@ -132,14 +132,10 @@ namespace dawn_native { namespace metal { ResultOrError Device::CreateTextureImpl(const TextureDescriptor* descriptor) { return new Texture(this, descriptor); } - TextureViewBase* Device::CreateDefaultTextureView(TextureBase* texture) { - return new TextureView(texture); - } - // TODO(jiawei.shao@intel.com): implement creating texture view with TextureViewDescriptor ResultOrError Device::CreateTextureViewImpl( TextureBase* texture, const TextureViewDescriptor* descriptor) { - return DAWN_UNIMPLEMENTED_ERROR("Creating texture view with descriptor is unimplemented."); + return new TextureView(texture, descriptor); } void Device::TickImpl() { diff --git a/src/dawn_native/metal/TextureMTL.h b/src/dawn_native/metal/TextureMTL.h index 6aca712c3d..01efb210ed 100644 --- a/src/dawn_native/metal/TextureMTL.h +++ b/src/dawn_native/metal/TextureMTL.h @@ -39,7 +39,7 @@ namespace dawn_native { namespace metal { class TextureView : public TextureViewBase { public: - TextureView(TextureBase* texture); + TextureView(TextureBase* texture, const TextureViewDescriptor* descriptor); }; }} // namespace dawn_native::metal diff --git a/src/dawn_native/metal/TextureMTL.mm b/src/dawn_native/metal/TextureMTL.mm index 213df8f11a..7d6d0243b8 100644 --- a/src/dawn_native/metal/TextureMTL.mm +++ b/src/dawn_native/metal/TextureMTL.mm @@ -101,7 +101,9 @@ namespace dawn_native { namespace metal { return mMtlTexture; } - TextureView::TextureView(TextureBase* texture) : TextureViewBase(texture) { + // TODO(jiawei.shao@intel.com): create texture view by texture view descriptor + TextureView::TextureView(TextureBase* texture, const TextureViewDescriptor* descriptor) + : TextureViewBase(texture, descriptor) { } }} // namespace dawn_native::metal diff --git a/src/dawn_native/null/NullBackend.cpp b/src/dawn_native/null/NullBackend.cpp index 10fda39299..86fbca71e9 100644 --- a/src/dawn_native/null/NullBackend.cpp +++ b/src/dawn_native/null/NullBackend.cpp @@ -95,14 +95,10 @@ namespace dawn_native { namespace null { ResultOrError Device::CreateTextureImpl(const TextureDescriptor* descriptor) { return new Texture(this, descriptor); } - TextureViewBase* Device::CreateDefaultTextureView(TextureBase* texture) { - return new TextureView(texture); - } - // TODO(jiawei.shao@intel.com): implement creating texture view by TextureViewDescriptor ResultOrError Device::CreateTextureViewImpl( TextureBase* texture, const TextureViewDescriptor* descriptor) { - return new TextureView(texture); + return new TextureView(texture, descriptor); } void Device::InitFakePCIInfo() { diff --git a/src/dawn_native/null/NullBackend.h b/src/dawn_native/null/NullBackend.h index 1fee9119c8..0c50ae9493 100644 --- a/src/dawn_native/null/NullBackend.h +++ b/src/dawn_native/null/NullBackend.h @@ -105,7 +105,6 @@ namespace dawn_native { namespace null { RenderPassDescriptorBuilder* builder) override; RenderPipelineBase* CreateRenderPipeline(RenderPipelineBuilder* builder) override; SwapChainBase* CreateSwapChain(SwapChainBuilder* builder) override; - TextureViewBase* CreateDefaultTextureView(TextureBase* texture) override; void TickImpl() override; diff --git a/src/dawn_native/opengl/DeviceGL.cpp b/src/dawn_native/opengl/DeviceGL.cpp index 046a66e4cf..f2dc8bcdae 100644 --- a/src/dawn_native/opengl/DeviceGL.cpp +++ b/src/dawn_native/opengl/DeviceGL.cpp @@ -106,14 +106,10 @@ namespace dawn_native { namespace opengl { ResultOrError Device::CreateTextureImpl(const TextureDescriptor* descriptor) { return new Texture(this, descriptor); } - TextureViewBase* Device::CreateDefaultTextureView(TextureBase* texture) { - return new TextureView(texture); - } - // TODO(jiawei.shao@intel.com): implement creating texture view with TextureViewDescriptor ResultOrError Device::CreateTextureViewImpl( TextureBase* texture, const TextureViewDescriptor* descriptor) { - return DAWN_UNIMPLEMENTED_ERROR("Creating texture view with descriptor is unimplemented."); + return new TextureView(texture, descriptor); } void Device::TickImpl() { diff --git a/src/dawn_native/opengl/DeviceGL.h b/src/dawn_native/opengl/DeviceGL.h index b07398ac02..2b39db54e4 100644 --- a/src/dawn_native/opengl/DeviceGL.h +++ b/src/dawn_native/opengl/DeviceGL.h @@ -43,7 +43,6 @@ namespace dawn_native { namespace opengl { RenderPassDescriptorBuilder* builder) override; RenderPipelineBase* CreateRenderPipeline(RenderPipelineBuilder* builder) override; SwapChainBase* CreateSwapChain(SwapChainBuilder* builder) override; - TextureViewBase* CreateDefaultTextureView(TextureBase* texture) override; void TickImpl() override; diff --git a/src/dawn_native/opengl/TextureGL.cpp b/src/dawn_native/opengl/TextureGL.cpp index b0bc81caa1..6b9d90c3f7 100644 --- a/src/dawn_native/opengl/TextureGL.cpp +++ b/src/dawn_native/opengl/TextureGL.cpp @@ -129,7 +129,9 @@ namespace dawn_native { namespace opengl { // TextureView - TextureView::TextureView(TextureBase* texture) : TextureViewBase(texture) { + // TODO(jiawei.shao@intel.com): create texture view by TextureViewDescriptor + TextureView::TextureView(TextureBase* texture, const TextureViewDescriptor* descriptor) + : TextureViewBase(texture, descriptor) { } }} // namespace dawn_native::opengl diff --git a/src/dawn_native/opengl/TextureGL.h b/src/dawn_native/opengl/TextureGL.h index 2fd7ffe7e3..be7d1591c3 100644 --- a/src/dawn_native/opengl/TextureGL.h +++ b/src/dawn_native/opengl/TextureGL.h @@ -46,7 +46,7 @@ namespace dawn_native { namespace opengl { class TextureView : public TextureViewBase { public: - TextureView(TextureBase* texture); + TextureView(TextureBase* texture, const TextureViewDescriptor* descriptor); }; }} // namespace dawn_native::opengl diff --git a/src/dawn_native/vulkan/DeviceVk.cpp b/src/dawn_native/vulkan/DeviceVk.cpp index f898e6158c..5998674225 100644 --- a/src/dawn_native/vulkan/DeviceVk.cpp +++ b/src/dawn_native/vulkan/DeviceVk.cpp @@ -270,15 +270,11 @@ namespace dawn_native { namespace vulkan { ResultOrError Device::CreateTextureImpl(const TextureDescriptor* descriptor) { return new Texture(this, descriptor); } - TextureViewBase* Device::CreateDefaultTextureView(TextureBase* texture) { - return new TextureView(texture); - } - // TODO(jiawei.shao@intel.com): implement creating texture view with TextureViewDescriptor ResultOrError Device::CreateTextureViewImpl( TextureBase* texture, const TextureViewDescriptor* descriptor) { - return DAWN_UNIMPLEMENTED_ERROR("Creating texture view with descriptor is unimplemented."); + return new TextureView(texture, descriptor); } void Device::TickImpl() { diff --git a/src/dawn_native/vulkan/DeviceVk.h b/src/dawn_native/vulkan/DeviceVk.h index 90cfcc609e..c11427c2df 100644 --- a/src/dawn_native/vulkan/DeviceVk.h +++ b/src/dawn_native/vulkan/DeviceVk.h @@ -74,7 +74,6 @@ namespace dawn_native { namespace vulkan { RenderPassDescriptorBuilder* builder) override; RenderPipelineBase* CreateRenderPipeline(RenderPipelineBuilder* builder) override; SwapChainBase* CreateSwapChain(SwapChainBuilder* builder) override; - TextureViewBase* CreateDefaultTextureView(TextureBase* texture) override; void TickImpl() override; diff --git a/src/dawn_native/vulkan/TextureVk.cpp b/src/dawn_native/vulkan/TextureVk.cpp index 6826e19431..0f523adc26 100644 --- a/src/dawn_native/vulkan/TextureVk.cpp +++ b/src/dawn_native/vulkan/TextureVk.cpp @@ -346,7 +346,9 @@ namespace dawn_native { namespace vulkan { mLastUsage = usage; } - TextureView::TextureView(TextureBase* texture) : TextureViewBase(texture) { + // TODO(jiawei.shao@intel.com): create texture view by TextureViewDescriptor + TextureView::TextureView(TextureBase* texture, const TextureViewDescriptor* descriptor) + : TextureViewBase(texture, descriptor) { Device* device = ToBackend(texture->GetDevice()); VkImageViewCreateInfo createInfo; diff --git a/src/dawn_native/vulkan/TextureVk.h b/src/dawn_native/vulkan/TextureVk.h index e1c4831d05..e17ebb2eee 100644 --- a/src/dawn_native/vulkan/TextureVk.h +++ b/src/dawn_native/vulkan/TextureVk.h @@ -50,7 +50,7 @@ namespace dawn_native { namespace vulkan { class TextureView : public TextureViewBase { public: - TextureView(TextureBase* texture); + TextureView(TextureBase* texture, const TextureViewDescriptor* descriptor); ~TextureView(); VkImageView GetHandle() const;