Skip to content

Support unowned SDL_GPUDevice handles #177

@pixlark

Description

@pixlark

SDL_GPUDevice isn't always instantiated by rust usercode. In the case where you need to access the GPU Device that's initialized by the 2D renderer, you can get a *mut SDL_GPUDevice handle, but you can't turn that into a Device.
Here is an official SDL example demonstrating how this would be done in C.

The examples goes through a few hoops in order to get the GPUDevice pointer, namely reading it from a pointer property on the SDL_Renderer - so because of that, maybe this wouldn't be considered a primary enough usecase to warrant being supported by the Rust API.

However, in SDL 3.4, there will be a new function SDL_CreateGPURenderer, which removes those hoops and provides you with an SDL_GPUDevice pointer that is owned by the SDL_Renderer. So when 3.4 comes out, this problem will need to be dealt with anyways.

Internally, the SDL_GPUDevice pointer in a Device is held by DeviceContainer, which assumes ownership over the pointer, meaning that when it's dropped, it callsSDL_DestroyGPUDevice.

This means that you can't just add an unsafe fn from_pointer(ptr: *mut SDL_GPUDevice) -> Device without running into double-free issues (in my experience it caused a hard crash when trying to close the program).

So in order to support an unowned Device, the type would need to be refactored somehow, perhaps by creating another DeviceRef<'a> type that borrows from an owned Device, and using that everywhere instead? Similar to String vs. &str?

I'm not really sure, which is why I'm creating this issue instead of just submitting a PR - hopefully someone more familiar with these bindings has some ideas. I am open to doing the legwork though and writing the PR, just not sure how to go about it.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions