D3D12: Pass usage to ProduceTexture

Allow external images to specify all allowed texture usages which will
be checked by ProduceTexture. This avoids needing to assume the usage
between multiple calls to ProduceTexture is always the same.

Bug=dawn:625

Change-Id: I9fdb59af23e6c160e939ab1c51de03542248c0c8
Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/44260
Commit-Queue: Bryan Bernhart <bryan.bernhart@intel.com>
Reviewed-by: Corentin Wallez <cwallez@chromium.org>
This commit is contained in:
Bryan Bernhart 2021-03-12 02:27:56 +00:00 committed by Commit Bot service account
parent 0702b70469
commit 2f72aeda4f
4 changed files with 41 additions and 1 deletions

View File

@ -17,6 +17,8 @@
#include "dawn_native/D3D12Backend.h" #include "dawn_native/D3D12Backend.h"
#include "common/Log.h"
#include "common/Math.h"
#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"
@ -68,8 +70,14 @@ namespace dawn_native { namespace d3d12 {
const ExternalImageAccessDescriptorDXGIKeyedMutex* descriptor) { const ExternalImageAccessDescriptorDXGIKeyedMutex* descriptor) {
Device* backendDevice = reinterpret_cast<Device*>(device); Device* backendDevice = reinterpret_cast<Device*>(device);
// Ensure the texture usage is allowed
if (!IsSubset(descriptor->usage, mUsage)) {
dawn::ErrorLog() << "Texture usage is not valid for external image";
return nullptr;
}
TextureDescriptor textureDescriptor = {}; TextureDescriptor textureDescriptor = {};
textureDescriptor.usage = static_cast<wgpu::TextureUsage>(mUsage); textureDescriptor.usage = static_cast<wgpu::TextureUsage>(descriptor->usage);
textureDescriptor.dimension = static_cast<wgpu::TextureDimension>(mDimension); textureDescriptor.dimension = static_cast<wgpu::TextureDimension>(mDimension);
textureDescriptor.size = {mSize.width, mSize.height, mSize.depth}; textureDescriptor.size = {mSize.width, mSize.height, mSize.depth};
textureDescriptor.format = static_cast<wgpu::TextureFormat>(mFormat); textureDescriptor.format = static_cast<wgpu::TextureFormat>(mFormat);
@ -149,6 +157,7 @@ namespace dawn_native { namespace d3d12 {
externalAccessDesc.isInitialized = descriptor->isInitialized; externalAccessDesc.isInitialized = descriptor->isInitialized;
externalAccessDesc.isSwapChainTexture = descriptor->isSwapChainTexture; externalAccessDesc.isSwapChainTexture = descriptor->isSwapChainTexture;
externalAccessDesc.acquireMutexKey = descriptor->acquireMutexKey; externalAccessDesc.acquireMutexKey = descriptor->acquireMutexKey;
externalAccessDesc.usage = descriptor->cTextureDescriptor->usage;
return externalImage->ProduceTexture(device, &externalAccessDesc); return externalImage->ProduceTexture(device, &externalAccessDesc);
} }

View File

@ -231,6 +231,7 @@ namespace dawn_native {
struct DAWN_NATIVE_EXPORT ExternalImageAccessDescriptor { struct DAWN_NATIVE_EXPORT ExternalImageAccessDescriptor {
public: public:
bool isInitialized; // Whether the texture is initialized on import bool isInitialized; // Whether the texture is initialized on import
WGPUTextureUsageFlags usage;
}; };
struct DAWN_NATIVE_EXPORT ExternalImageExportInfo { struct DAWN_NATIVE_EXPORT ExternalImageExportInfo {

View File

@ -122,6 +122,7 @@ namespace {
dawn_native::d3d12::ExternalImageAccessDescriptorDXGIKeyedMutex externalAccessDesc; dawn_native::d3d12::ExternalImageAccessDescriptorDXGIKeyedMutex externalAccessDesc;
externalAccessDesc.acquireMutexKey = 0; externalAccessDesc.acquireMutexKey = 0;
externalAccessDesc.usage = static_cast<WGPUTextureUsageFlags>(dawnDesc->usage);
*dawnTexture = wgpu::Texture::Acquire( *dawnTexture = wgpu::Texture::Acquire(
externalImage->ProduceTexture(device.Get(), &externalAccessDesc)); externalImage->ProduceTexture(device.Get(), &externalAccessDesc));
@ -361,6 +362,7 @@ class D3D12SharedHandleUsageTests : public D3D12ResourceTestBase {
dawn_native::d3d12::ExternalImageAccessDescriptorDXGIKeyedMutex externalAccessDesc; dawn_native::d3d12::ExternalImageAccessDescriptorDXGIKeyedMutex externalAccessDesc;
externalAccessDesc.acquireMutexKey = 1; externalAccessDesc.acquireMutexKey = 1;
externalAccessDesc.isInitialized = isInitialized; externalAccessDesc.isInitialized = isInitialized;
externalAccessDesc.usage = static_cast<WGPUTextureUsageFlags>(dawnDescriptor->usage);
*dawnTextureOut = wgpu::Texture::Acquire( *dawnTextureOut = wgpu::Texture::Acquire(
externalImage->ProduceTexture(device.Get(), &externalAccessDesc)); externalImage->ProduceTexture(device.Get(), &externalAccessDesc));
@ -573,6 +575,7 @@ TEST_P(D3D12SharedHandleUsageTests, ReuseExternalImage) {
dawn_native::d3d12::ExternalImageAccessDescriptorDXGIKeyedMutex externalAccessDesc; dawn_native::d3d12::ExternalImageAccessDescriptorDXGIKeyedMutex externalAccessDesc;
externalAccessDesc.acquireMutexKey = 1; externalAccessDesc.acquireMutexKey = 1;
externalAccessDesc.isInitialized = true; externalAccessDesc.isInitialized = true;
externalAccessDesc.usage = static_cast<WGPUTextureUsageFlags>(baseDawnDescriptor.usage);
texture = texture =
wgpu::Texture::Acquire(externalImage->ProduceTexture(device.Get(), &externalAccessDesc)); wgpu::Texture::Acquire(externalImage->ProduceTexture(device.Get(), &externalAccessDesc));
@ -590,5 +593,31 @@ TEST_P(D3D12SharedHandleUsageTests, ReuseExternalImage) {
} }
} }
// Produce a new texture with a usage not specified in the external image.
TEST_P(D3D12SharedHandleUsageTests, ExternalImageUsage) {
DAWN_SKIP_TEST_IF(UsesWire());
dawn_native::d3d12::ExternalImageAccessDescriptorDXGIKeyedMutex externalAccessDesc;
externalAccessDesc.acquireMutexKey = 1;
externalAccessDesc.isInitialized = true;
wgpu::Texture texture;
ComPtr<ID3D11Texture2D> d3d11Texture;
std::unique_ptr<dawn_native::d3d12::ExternalImageDXGI> externalImage;
WrapSharedHandle(&baseDawnDescriptor, &baseD3dDescriptor, &texture, &d3d11Texture,
&externalImage);
ASSERT_NE(texture.Get(), nullptr);
externalAccessDesc.usage = WGPUTextureUsage_Storage;
texture =
wgpu::Texture::Acquire(externalImage->ProduceTexture(device.Get(), &externalAccessDesc));
ASSERT_EQ(texture.Get(), nullptr);
externalAccessDesc.usage = WGPUTextureUsage_Sampled;
texture =
wgpu::Texture::Acquire(externalImage->ProduceTexture(device.Get(), &externalAccessDesc));
ASSERT_NE(texture.Get(), nullptr);
}
DAWN_INSTANTIATE_TEST(D3D12SharedHandleValidation, D3D12Backend()); DAWN_INSTANTIATE_TEST(D3D12SharedHandleValidation, D3D12Backend());
DAWN_INSTANTIATE_TEST(D3D12SharedHandleUsageTests, D3D12Backend()); DAWN_INSTANTIATE_TEST(D3D12SharedHandleUsageTests, D3D12Backend());

View File

@ -213,6 +213,7 @@ namespace {
dawn_native::d3d12::ExternalImageAccessDescriptorDXGIKeyedMutex externalAccessDesc; dawn_native::d3d12::ExternalImageAccessDescriptorDXGIKeyedMutex externalAccessDesc;
externalAccessDesc.acquireMutexKey = 1; externalAccessDesc.acquireMutexKey = 1;
externalAccessDesc.isInitialized = true; externalAccessDesc.isInitialized = true;
externalAccessDesc.usage = static_cast<WGPUTextureUsageFlags>(textureDesc.usage);
*dawnTextureOut = wgpu::Texture::Acquire( *dawnTextureOut = wgpu::Texture::Acquire(
externalImage->ProduceTexture(device.Get(), &externalAccessDesc)); externalImage->ProduceTexture(device.Get(), &externalAccessDesc));