Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Is it possible to convert textures & buffers to/from backend handle types? #1460

Closed
Gonkalbell opened this issue Aug 27, 2020 · 6 comments
Closed
Labels
area: api Issues related to API surface help required We need community help to make this happen. type: enhancement New feature or request

Comments

@Gonkalbell
Copy link

If I'm only concerned about a single backend, and I am using and API that directly returns/uses raw handles to OpenGL/Vulkan objects, is there a way to convert those handles to/from their corresponding wgpu object? I don't seem to see any way to currently do this in wgpu-rs, wgpu-native, or gfx-hal, but I could have missed something. If it's not a feature yet, is there any plan to add it?

@kvark
Copy link
Member

kvark commented Aug 27, 2020

An ability to do this would first need to be added to gfx-rs backends, individually. So it's going to be backend-specific public APIs. Then wgpu-core would need to have some way of creating resources from external handles. There are concerns to think about:

  1. who owns the resource? if the ownership transfer goes to wgpu, then we are good
  2. how should resource be destroyed, given that wgpu doesn't know how it was created?
  3. what can this resource do? often, external resources are much more limited in what they can do

@schulzch
Copy link

I guess accessing texture handles is currently impossible?

Use case: Most VR/AR/MR devices require access to a couple of "framebuffer-ish" texture handles to then do the compositing, e.g.:

/**
 * Converts a Direct3D 12 texture to #varjo_Texture.
 */
VARJO_API struct varjo_Texture varjo_FromD3D12Texture(struct ID3D12Resource* texture);

(see also OpenXR, e.g., here for a more complete example on swap chain construction)

I spend a few minutes digging into gfx-hal/dx12/resource.rs. Am I right that the backend has resources publicly accessible, but wgpu::Backend is a dead end regarding access to the backend?

@kvark
Copy link
Member

kvark commented Aug 31, 2020

Conceptually, wgpu abstracts away the native API. That's the only way we can make it safe and sound.
Clearly, the VR/XR requires some punching through this abstraction, and it needs to touch gfx, wgpu, and wgpu-rs - all of them.

@schulzch
Copy link

schulzch commented Sep 8, 2020

Just to mention another use case: We use Spout to share OpenGL textures (from our particle visualization framework MegaMol) with DirectX (to Unity). This way our framework runs out of process (e.g., on a GPU cluster, not crashing Unity, ...), while simplifying XR prototyping.

@schulzch
Copy link

We sticked with gfx-pre-ll for now and did OpenGL texture ID conversion like this:

pub fn texture_from_id_and_size<T>(
    texture_id: gfx_gl::types::GLuint,
    width: u32,
    height: u32,
) -> gfx_core::handle::Texture<gfx_device_gl::Resources, <T as gfx::format::Formatted>::Surface>
where
    T: gfx::format::TextureFormat + gfx::format::RenderFormat,
{
    use gfx_core::handle::Producer;
    let mut temp: gfx_core::handle::Manager<gfx_device_gl::Resources> =
        gfx_core::handle::Manager::new();
    let raw_texture = temp.make_texture(
        gfx_device_gl::NewTexture::Texture(texture_id),
        gfx_core::texture::Info {
            levels: 1,
            kind: gfx_core::texture::Kind::D2(
                width as u16,
                height as u16,
                gfx_core::texture::AaMode::Single,
            ),
            format: gfx_core::format::SurfaceType::R8_G8_B8_A8,
            bind: gfx_core::memory::Bind::RENDER_TARGET | gfx_core::memory::Bind::TRANSFER_SRC,
            usage: gfx_core::memory::Usage::Data,
        },
    );
    use crate::gfx::memory::Typed;
    gfx::handle::Texture::new(raw_texture)
}

The function above is used here: visual-system-simulator/.../varjo.rs. Maybe it helps :)

kejor referenced this issue in kejor/wgpu-rs Nov 28, 2020
538: Unbox unnecessarily boxed function r=kvark a=lachlansneff

Internally, the async buffer mapping callbacks were unnecessarily boxed. This was pretty easy to fix.

Co-authored-by: Lachlan Sneff <lachlan.sneff@gmail.com>
@kvark kvark transferred this issue from gfx-rs/wgpu-rs Jun 3, 2021
@kvark kvark added area: api Issues related to API surface help required We need community help to make this happen. type: enhancement New feature or request labels Jun 3, 2021
@cwfitzgerald
Copy link
Member

Duplicate of #965.

@cwfitzgerald cwfitzgerald closed this as not planned Won't fix, can't repro, duplicate, stale Jun 5, 2022
Patryk27 pushed a commit to Patryk27/wgpu that referenced this issue Nov 23, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area: api Issues related to API surface help required We need community help to make this happen. type: enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

4 participants