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

Guidance for dealing with callbacks #3341

Closed
sagacity opened this issue Nov 13, 2024 · 5 comments
Closed

Guidance for dealing with callbacks #3341

sagacity opened this issue Nov 13, 2024 · 5 comments
Labels
question Further information is requested

Comments

@sagacity
Copy link

sagacity commented Nov 13, 2024

Suggestion

The windows::Windows::Win32::Graphics::Direct3D::Fxc::D3DCompile API call supports providing an implementation of the ID3DInclude interface, so that application developers can provide custom callbacks to handle #include file handling.

In C++ this works by creating a class and deriving it from ID3DInclude, like so:

class MyCustomInclude : public ID3DInclude
{
    HRESULT Open(D3D_INCLUDE_TYPE IncludeType, LPCSTR pFileName, LPCVOID pParentData, LPCVOID* ppData, UINT* pBytes) override
    {
        // ...get some bytes based on input parameters...
    }

    HRESULT Close(LPCVOID pData) override
    {
        // ...free buffers, etc...
    }
};

It's unclear to me how this needs to be handled in windows-rs, though. ID3DInclude is not modelled as a trait I can implement, so my next thought was to manually fill the ID3DInclude_Vtbl with Open and Close function pointers. However, it's unclear to me how to take that vtable, construct a new ID3DInclude from that and provide it to D3DCompile.

It would be nice if there would be a snippet of documentation or a sample showing how to deal with these interface implementations, but I'm not sure how often that comes up in other areas of the Windows APIs.

@sagacity sagacity added the enhancement New feature or request label Nov 13, 2024
@kennykerr kennykerr added question Further information is requested and removed enhancement New feature or request labels Nov 13, 2024
@kennykerr
Copy link
Collaborator

kennykerr commented Nov 13, 2024

You may be missing the "implement" feature in the latest release. This requirement has since been removed (#3333) which should make this easier to discover in future.

[dependencies.windows]
version = "0.58"
features = [
    "implement",
    "Win32_Graphics_Direct3D",
]
use std::ffi::c_void;
use windows::{core::*, Win32::Graphics::Direct3D::*};

struct Include;

impl ID3DInclude_Impl for Include {
    fn Open(
        &self,
        _: D3D_INCLUDE_TYPE,
        _: &PCSTR,
        _: *const c_void,
        _: *mut *mut c_void,
        _: *mut u32,
    ) -> Result<()> {
        todo!()
    }
    fn Close(&self, _: *const c_void) -> Result<()> {
        todo!()
    }
}

fn main() {
    let _include = ID3DInclude::new(&Include);
}

@sagacity
Copy link
Author

I did indeed miss that, thanks!

@sagacity
Copy link
Author

sagacity commented Nov 13, 2024

Sorry to reopen this, but there is currently a caveat: the new method of ID3DInclude is behind a std feature gate, which doesn't seem needed afaict. It does create a Box so it does need extern crate alloc, as well. Can this feature gate be removed? It does seem to run fine without this gate.

That also explains why I missed it, since I'm building with no-std.

@sagacity sagacity reopened this Nov 13, 2024
@kennykerr
Copy link
Collaborator

Yes, I'm planning to fix that: #3335

@sagacity
Copy link
Author

Wow, thanks for all the hard work, it's very much appreciated.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants