A variety of features may be used to import externally allocated shared texture memory.
wgpu::FeatureName::SharedTextureMemoryAHardwareBufferwgpu::FeatureName::SharedTextureMemoryDmaBufwgpu::FeatureName::SharedTextureMemoryOpaqueFDwgpu::FeatureName::SharedTextureMemoryZirconHandlewgpu::FeatureName::SharedTextureMemoryDXGISharedHandlewgpu::FeatureName::SharedTextureMemoryD3D11Texture2Dwgpu::FeatureName::SharedTextureMemoryIOSurfaceEach memory type uses an extension struct chained on wgpu::SharedTextureMemoryDescriptor to create the memory. For example:
wgpu::SharedTextureMemoryFooBarDescriptor fooBarDesc = { .fooBar = ..., }; wgpu::SharedTextureMemoryDescriptor desc = {}; desc.nextInChain = &fooBarDesc; wgpu::SharedTextureMemory memory = device.CreateSharedTextureMemory(&desc);
After creating the memory, the properties such as the supported wgpu::TextureUsage can be queried.
wgpu::SharedTextureMemoryProperties properties; memory.GetProperties(&properties); switch (properties.usage) { // ... handle supported usage }
Then, a texture can be created from it. For example:
wgpu::TextureDescriptor textureDesc = { // usage must be a subset of the texture memory's usage .usage = properties.usage }; wgpu::Texture texture = memory.CreateTexture(&textureDesc);
Note: There are restrictions on the configuration that can be requested for textures created from SharedTextureMemory objects:
Textures created from shared texture memory are not valid to use inside a queue operation until access to the memory is explicitly started using BeginAccess. Access is ended using EndAccess. For example:
wgpu::TextureDescriptor textureDesc = { ... }; wgpu::Texture texture = memory.CreateTexture(&textureDesc); // It is valid to create a bind group and encode commands // referencing the texture. wgpu::BindGroup bg = device.CreateBindGroup({..., texture, ... }); wgpu::CommandEncoder encoder = device.CreateCommandEncoder(); wgpu::ComputePass pass = encoder.BeginComputePass(); // ... pass.SetBindGroup(0, bg); // ... pass.End(); wgpu::CommandBuffer commandBuffer = encoder.Finish(); // Begin/EndAccess must wrap usage of the texture on the queue. wgpu::SharedTextureMemoryBeginAccessDescriptor beginAccessDesc = { ... }; memory.BeginAccess(texture, &beginAccessDesc); queue.writeTexture(texture, ...); queue.submit(1, &commandBuffer); wgpu::SharedTextureMemoryEndAccessState endAccessDesc = { ... }; memory.EndAccess(texture, &endAccessDesc);
Multiple textures may be created from the same SharedTextureMemory, but only one texture may have access to the memory at a time.
The Begin/End access descriptors are used to pass data necessary to update and synchronize texture state since the texture may have been externally used. Chained structs that are supported or required vary depending on the memory type. These access descriptors communicate the texture‘s initialized state. They are also used to pass Shared Fences to synchronize access to the texture. When passed in BeginAccess, the texture will wait for the shared fences to pass before use on the GPU, and on EndAccess, the shared fences for the texture’s last usage will be exported. Passing shared fences to BeginAccess allows Dawn to wait on externally-enqueued GPU work, and exporting shared fences from EndAccess then allows other external code to wait for Dawn's GPU work to complete.
wgpu::FeatureName::SharedTextureMemoryDXGIY410 allows importing DXGI_FORMAT_Y410 textures through wgpu::SharedTextureMemoryD3D11Texture2DDescriptor or wgpu::SharedTextureMemoryDXGISharedHandleDescriptor on the D3D11 backend.
Y410 can only be accessed through its DXGI_FORMAT_R10G10B10A2_UNORM view alias, so the memory‘s format is wgpu::TextureFormat::RGB10A2Unorm, and the U, Y, V and A components are read and written as R, G, B and A. D3D11 can’t copy or resolve between Y410 and DXGI_FORMAT_R10G10B10A2_UNORM resources, so the memory doesn‘t support the CopySrc and CopyDst usages, and textures created from it can’t be used as resolve targets.
wgpu::FeatureName::SharedTextureMemoryDXGIY210 allows importing DXGI_FORMAT_Y210 textures through wgpu::SharedTextureMemoryD3D11Texture2DDescriptor or wgpu::SharedTextureMemoryDXGISharedHandleDescriptor on the D3D11 backend. It also requires wgpu::TextureFormat::RGBA16Unorm to be supported, which wgpu::FeatureName::Unorm16TextureFormats or wgpu::FeatureName::TextureFormatsTier1 enables.
Y210 stores two horizontally adjacent pixels in each texel, as Y0, U, Y1 and V in 16 bits each, and can only be accessed through its DXGI_FORMAT_R16G16B16A16_UNORM view alias. The memory‘s format is therefore wgpu::TextureFormat::RGBA16Unorm, its width is half the width of the DXGI_FORMAT_Y210 texture (which must be even), and Y0, U, Y1 and V are read and written as R, G, B and A. D3D11 can’t copy or resolve between Y210 and DXGI_FORMAT_R16G16B16A16_UNORM resources, so the memory doesn‘t support the CopySrc and CopyDst usages, and textures created from it can’t be used as resolve targets.
TODO(crbug.com/dawn/1745): additional documentation
Work-in-progress: https://docs.google.com/document/d/1uRGL6vE1mSbpWd2v_KU5--RT5EjTXtruwiC7Ri3ZKz4/edit