diff --git a/BUILD.gn b/BUILD.gn index 5b07274df5..a5288db5ea 100644 --- a/BUILD.gn +++ b/BUILD.gn @@ -953,6 +953,10 @@ source_set("dawn_white_box_tests_sources") { } } + if (dawn_enable_d3d12) { + sources += [ "src/tests/white_box/D3D12SmallTextureTests.cpp" ] + } + if (dawn_enable_opengl) { deps += [ ":dawn_glfw" ] } diff --git a/src/dawn_native/d3d12/ResourceAllocatorManagerD3D12.cpp b/src/dawn_native/d3d12/ResourceAllocatorManagerD3D12.cpp index d1707c95f8..895afe06fd 100644 --- a/src/dawn_native/d3d12/ResourceAllocatorManagerD3D12.cpp +++ b/src/dawn_native/d3d12/ResourceAllocatorManagerD3D12.cpp @@ -187,17 +187,41 @@ namespace dawn_native { namespace d3d12 { ResultOrError ResourceAllocatorManager::CreatePlacedResource( D3D12_HEAP_TYPE heapType, - const D3D12_RESOURCE_DESC& resourceDescriptor, + const D3D12_RESOURCE_DESC& requestedResourceDescriptor, D3D12_RESOURCE_STATES initialUsage) { - const size_t resourceHeapKindIndex = GetResourceHeapKind( - resourceDescriptor.Dimension, heapType, resourceDescriptor.Flags, mResourceHeapTier); + const size_t resourceHeapKindIndex = + GetResourceHeapKind(requestedResourceDescriptor.Dimension, heapType, + requestedResourceDescriptor.Flags, mResourceHeapTier); + + // Small resources can take advantage of smaller alignments. For example, + // if the most detailed mip can fit under 64KB, 4KB alignments can be used. + // Must be non-depth or without render-target to use small resource alignment. + // + // Note: Only known to be used for small textures; however, MSDN suggests + // it could be extended for more cases. If so, this could default to always attempt small + // resource placement. + // https://docs.microsoft.com/en-us/windows/win32/api/d3d12/ns-d3d12-d3d12_resource_desc + D3D12_RESOURCE_DESC resourceDescriptor = requestedResourceDescriptor; + resourceDescriptor.Alignment = + (resourceHeapKindIndex == Default_OnlyNonRenderableOrDepthTextures) + ? D3D12_SMALL_RESOURCE_PLACEMENT_ALIGNMENT + : requestedResourceDescriptor.Alignment; + + D3D12_RESOURCE_ALLOCATION_INFO resourceInfo = + mDevice->GetD3D12Device()->GetResourceAllocationInfo(0, 1, &resourceDescriptor); + + // If the request for small resource alignment was rejected, let D3D tell us what the + // required alignment is for this resource. + if (resourceHeapKindIndex == Default_OnlyNonRenderableOrDepthTextures && + resourceInfo.Alignment != D3D12_SMALL_RESOURCE_PLACEMENT_ALIGNMENT) { + resourceDescriptor.Alignment = 0; + resourceInfo = + mDevice->GetD3D12Device()->GetResourceAllocationInfo(0, 1, &resourceDescriptor); + } BuddyMemoryAllocator* allocator = mSubAllocatedResourceAllocators[resourceHeapKindIndex].get(); - const D3D12_RESOURCE_ALLOCATION_INFO resourceInfo = - mDevice->GetD3D12Device()->GetResourceAllocationInfo(0, 1, &resourceDescriptor); - ResourceMemoryAllocation allocation; DAWN_TRY_ASSIGN(allocation, allocator->Allocate(resourceInfo.SizeInBytes, resourceInfo.Alignment)); diff --git a/src/dawn_native/d3d12/ResourceAllocatorManagerD3D12.h b/src/dawn_native/d3d12/ResourceAllocatorManagerD3D12.h index d6a3bab860..b60a024b27 100644 --- a/src/dawn_native/d3d12/ResourceAllocatorManagerD3D12.h +++ b/src/dawn_native/d3d12/ResourceAllocatorManagerD3D12.h @@ -72,7 +72,7 @@ namespace dawn_native { namespace d3d12 { ResultOrError CreatePlacedResource( D3D12_HEAP_TYPE heapType, - const D3D12_RESOURCE_DESC& resourceDescriptor, + const D3D12_RESOURCE_DESC& requestedResourceDescriptor, D3D12_RESOURCE_STATES initialUsage); ResultOrError CreateCommittedResource( diff --git a/src/tests/white_box/D3D12SmallTextureTests.cpp b/src/tests/white_box/D3D12SmallTextureTests.cpp new file mode 100644 index 0000000000..83faa6351d --- /dev/null +++ b/src/tests/white_box/D3D12SmallTextureTests.cpp @@ -0,0 +1,75 @@ +// Copyright 2019 The Dawn Authors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include "tests/DawnTest.h" + +#include "dawn_native/d3d12/TextureD3D12.h" + +using namespace dawn_native::d3d12; + +class D3D12SmallTextureTests : public DawnTest { + protected: + std::vector GetRequiredExtensions() override { + mIsBCFormatSupported = SupportsExtensions({"texture_compression_bc"}); + if (!mIsBCFormatSupported) { + return {}; + } + + return {"texture_compression_bc"}; + } + + bool IsBCFormatSupported() const { + return mIsBCFormatSupported; + } + + private: + bool mIsBCFormatSupported = false; +}; + +// Verify that creating a small compressed textures will be 4KB aligned. +TEST_P(D3D12SmallTextureTests, AlignSmallCompressedTexture) { + DAWN_SKIP_TEST_IF(UsesWire()); + DAWN_SKIP_TEST_IF(!IsBCFormatSupported()); + DAWN_SKIP_TEST_IF(IsIntel()); + + wgpu::TextureDescriptor descriptor; + descriptor.dimension = wgpu::TextureDimension::e2D; + descriptor.size.width = 8; + descriptor.size.height = 8; + descriptor.size.depth = 1; + descriptor.arrayLayerCount = 1; + descriptor.sampleCount = 1; + descriptor.format = wgpu::TextureFormat::BC1RGBAUnorm; + descriptor.mipLevelCount = 1; + descriptor.usage = wgpu::TextureUsage::Sampled; + + // Create a smaller one that allows use of the smaller alignment. + wgpu::Texture texture = device.CreateTexture(&descriptor); + Texture* d3dTexture = reinterpret_cast(texture.Get()); + + EXPECT_EQ(d3dTexture->GetD3D12Resource()->GetDesc().Alignment, + static_cast(D3D12_SMALL_RESOURCE_PLACEMENT_ALIGNMENT)); + + // Create a larger one (>64KB) that forbids use the smaller alignment. + descriptor.size.width = 4096; + descriptor.size.height = 4096; + + texture = device.CreateTexture(&descriptor); + d3dTexture = reinterpret_cast(texture.Get()); + + EXPECT_EQ(d3dTexture->GetD3D12Resource()->GetDesc().Alignment, + static_cast(D3D12_DEFAULT_RESOURCE_PLACEMENT_ALIGNMENT)); +} + +DAWN_INSTANTIATE_TEST(D3D12SmallTextureTests, D3D12Backend); \ No newline at end of file