Skip to main content

thindx/headers/d3d11shader.h/interfaces/
module.rs

1use crate::*;
2use crate::ctypes::*;
3use crate::d3d11::*;
4
5use std::ptr::*;
6
7
8/// \[[docs.microsoft.com](https://docs.microsoft.com/en-us/windows/win32/api/d3d11shader/nn-d3d11shader-id3d11module)\]
9/// ID3D11Module
10///
11/// A module interface creates an instance of a module that is used for resource rebinding.
12///
13/// ### Example
14/// ```rust
15/// # use thindx::*;
16/// let d3dc = d3d::Compiler::load_system(47).unwrap();
17/// let lib = d3dc.compile_from_file(
18///     "test/data/library.hlsl", None, None, (), "lib_5_0",
19///     d3d::Compile::OptimizationLevel3, d3d::CompileEffect::None
20/// ).unwrap();
21/// let module : d3d11::Module = d3dc.load_module(&lib).unwrap();
22/// ```
23///
24/// ### See Also
25/// *   [d3d::Compiler::load_module] to create [Module]s
26/// *   [d3d11::FunctionLinkingGraph] for examples
27/// *   [examples::d3dcompiler_03_link]
28#[derive(Clone)] #[repr(transparent)]
29pub struct Module(pub(crate) mcom::Rc<winapi::um::d3d11shader::ID3D11Module>);
30
31convert!(unsafe Module => Unknown, winapi::um::d3d11shader::ID3D11Module);
32
33impl Module {
34    /// \[[docs.microsoft.com](https://docs.microsoft.com/en-us/windows/win32/api/d3d11shader/nf-d3d11shader-id3d11module-createinstance)\]
35    /// ID3D11Module::CreateInstance
36    ///
37    /// Initializes an instance of a shader module that is used for resource rebinding.
38    ///
39    /// ### Arguments
40    /// *   `namespace` - The name of a shader module to initialize.  This can be [None] if you don't want to specify a name for the module.
41    ///
42    /// ### Example
43    /// ```rust
44    /// # use thindx::*;
45    /// # let d3dc = d3d::Compiler::load_system(47).unwrap();
46    /// # let lib = d3dc.compile_from_file("test/data/library.hlsl", None, None, (), "lib_5_0", d3d::Compile::OptimizationLevel3, d3d::CompileEffect::None).unwrap();
47    /// # let module : d3d11::Module = d3dc.load_module(&lib).unwrap();
48    /// #
49    /// // no namespace
50    /// let ns : Option<&str> = None;
51    /// let _ : d3d11::ModuleInstance = module.create_instance(()).unwrap();
52    /// let _ : d3d11::ModuleInstance = module.create_instance(ns).unwrap();
53    ///
54    /// // global namespace
55    /// let _ : d3d11::ModuleInstance = module.create_instance("").unwrap();
56    /// let _ : d3d11::ModuleInstance = module.create_instance(cstr!("")).unwrap();
57    ///
58    /// // specific namespace
59    /// let _ : d3d11::ModuleInstance = module.create_instance("namespace").unwrap();
60    /// let _ : d3d11::ModuleInstance = module.create_instance(cstr!("namespace")).unwrap();
61    /// ```
62    pub fn create_instance(&self, namespace: impl TryIntoAsOptCStr) -> Result<ModuleInstance, MethodError> {
63        let namespace = namespace.try_into().map_err(|e| MethodError::new("ID3D11Module::CreateInstance", e))?;
64        let namespace = namespace.as_opt_cstr();
65
66        let mut instance = null_mut();
67        let hr = unsafe { self.0.CreateInstance(namespace, &mut instance) };
68        MethodError::check("ID3D11Module::CreateInstance", hr)?;
69        Ok(unsafe { ModuleInstance::from_raw(instance) })
70    }
71}