1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
use crate::*;
use crate::ctypes::*;
use crate::d3d11::*;
use winapi::um::d3d11shader::*;
use std::marker::PhantomData;
use std::ptr::NonNull;
/// \[[docs.microsoft.com](https://docs.microsoft.com/en-us/windows/win32/api/d3d11shader/nn-d3d11shader-id3d11shaderreflectionconstantbuffer)\]
/// ID3D11ShaderReflectionConstantBuffer
///
/// This shader-reflection interface provides access to a constant buffer.
///
/// ### See Also
/// * [d3d11::ShaderReflection::get_constant_buffer_by_index]
/// * [d3d11::ShaderReflection::get_constant_buffer_by_name]
#[derive(Clone)] #[repr(transparent)]
pub struct ShaderReflectionConstantBuffer<'r> {
ptr: NonNull<ID3D11ShaderReflectionConstantBuffer>,
phantom: PhantomData<&'r LibraryReflection>,
}
impl<'r> ShaderReflectionConstantBuffer<'r> {
pub(crate) unsafe fn from_raw(_: impl ParentOrPhantom<'r>, ptr: *mut ID3D11ShaderReflectionConstantBuffer) -> Self {
Self {
ptr: NonNull::new(ptr).expect("ShaderReflectionConstantBuffer should never be null"),
phantom: PhantomData,
}
}
}
impl<'r> ShaderReflectionConstantBuffer<'r> {
/// \[[docs.microsoft.com](https://docs.microsoft.com/en-us/windows/win32/api/d3d11shader/nf-d3d11shader-id3d11shaderreflectionconstantbuffer-getdesc)\]
/// ID3D11ShaderReflectionConstantBuffer::GetDesc
///
/// Get a constant-buffer description.
///
/// ### Example
/// ```rust
/// # use thindx::{*, d3d::*}; let d3dc = Compiler::load_system(47).unwrap();
/// let shader = d3dc.compile_from_file(
/// r"test\data\library.hlsl", None, None, (), "lib_5_0",
/// Compile::Debug, CompileEffect::None
/// ).unwrap();
///
/// let r : d3d11::LibraryReflection = d3dc.reflect_library(&shader).unwrap();
///
/// let scale4 = r.functions().unwrap().find(|f|
/// f.get_desc().unwrap().name.to_bytes() == b"scale4"
/// ).unwrap();
///
/// let valid = scale4.get_constant_buffer_by_name("ExampleCBuffer");
/// let desc = valid.get_desc().unwrap();
/// println!("{:#?}", desc);
///
/// let invalid = scale4.get_constant_buffer_by_name("Nonexistant");
/// assert_eq!(Some(E::FAIL), invalid.get_desc().err().map(|e| e.kind()));
/// ```
///
/// ### Output
/// ```text
/// ShaderBufferDesc {
/// name: Some(
/// "ExampleCBuffer",
/// ),
/// type: CT::CBuffer,
/// variables: 1,
/// size: 16,
/// flags: CBF::None,
/// }
/// ```
pub fn get_desc(&self) -> Result<ShaderBufferDesc<'r>, MethodError> {
let mut desc = ShaderBufferDesc::default();
let hr = unsafe { self.ptr.as_ref().GetDesc(desc.as_mut_ptr()) };
MethodError::check("ID3D11ShaderReflectionConstantBuffer::GetDesc", hr)?;
Ok(desc)
}
/// \[[docs.microsoft.com](https://docs.microsoft.com/en-us/windows/win32/api/d3d11shader/nf-d3d11shader-id3d11shaderreflectionconstantbuffer-getvariablebyindex)\]
/// ID3D11ShaderReflectionConstantBuffer::GetVariableByIndex
///
/// Get a shader-reflection variable by index.
///
/// ### Example
/// ```rust
/// # use thindx::{*, d3d::*}; let d3dc = Compiler::load_system(47).unwrap();
/// let shader = d3dc.compile_from_file(
/// r"test\data\library.hlsl", None, None, (), "lib_5_0",
/// Compile::Debug, CompileEffect::None
/// ).unwrap();
///
/// let r : d3d11::LibraryReflection = d3dc.reflect_library(&shader).unwrap();
///
/// let scale4 = r.functions().unwrap().find(|f|
/// f.get_desc().unwrap().name.to_bytes() == b"scale4"
/// ).unwrap();
///
/// let valid = scale4.get_constant_buffer_by_name("ExampleCBuffer");
/// println!("{:#?}", valid.get_variable_by_index(0).get_desc().unwrap());
/// assert_eq!(Some(E::FAIL), valid.get_variable_by_index(1).get_desc().err().map(|e| e.kind()));
///
/// let invalid = scale4.get_constant_buffer_by_name("Nonexistant");
/// assert_eq!(Some(E::FAIL), invalid.get_variable_by_index(0).get_desc().err().map(|e| e.kind()));
/// assert_eq!(Some(E::FAIL), invalid.get_variable_by_index(1).get_desc().err().map(|e| e.kind()));
/// ```
///
/// ### Output
/// ```text
/// ShaderVariableDesc {
/// name: Some(
/// "scale",
/// ),
/// start_offset: 0,
/// size: 4,
/// flags: SVF::Used,
/// default_value: 0x0000000000000000,
/// start_texture: 4294967295,
/// texture_size: 0,
/// start_sampler: 4294967295,
/// sampler_size: 0,
/// }
/// ```
//#allow_missing_argument_docs
pub fn get_variable_by_index(&self, index: u32) -> ShaderReflectionVariable<'r> {
let ptr = unsafe { self.ptr.as_ref().GetVariableByIndex(index) };
unsafe { ShaderReflectionVariable::from_raw(self.phantom, ptr) }
}
/// \[[docs.microsoft.com](https://docs.microsoft.com/en-us/windows/win32/api/d3d11shader/nf-d3d11shader-id3d11shaderreflectionconstantbuffer-getvariablebyname)\]
/// ID3D11ShaderReflectionConstantBuffer::GetVariableByName
///
/// Get a shader-reflection variable by name.
///
/// ### Example
/// ```rust
/// # use thindx::{*, d3d::*}; let d3dc = Compiler::load_system(47).unwrap();
/// let shader = d3dc.compile_from_file(
/// r"test\data\library.hlsl", None, None, (), "lib_5_0",
/// Compile::Debug, CompileEffect::None
/// ).unwrap();
///
/// let r : d3d11::LibraryReflection = d3dc.reflect_library(&shader).unwrap();
///
/// let scale4 = r.functions().unwrap().find(|f|
/// f.get_desc().unwrap().name.to_bytes() == b"scale4"
/// ).unwrap();
///
/// let valid = scale4.get_constant_buffer_by_name("ExampleCBuffer");
/// println!("{:#?}", valid.get_variable_by_name("scale").get_desc().unwrap());
/// assert_eq!(Some(E::FAIL), valid.get_variable_by_name("nope" ).get_desc().err().map(|e| e.kind()));
///
/// let invalid = scale4.get_constant_buffer_by_name("Nonexistant");
/// assert_eq!(Some(E::FAIL), invalid.get_variable_by_name("scale").get_desc().err().map(|e| e.kind()));
/// assert_eq!(Some(E::FAIL), invalid.get_variable_by_name("nope" ).get_desc().err().map(|e| e.kind()));
/// ```
///
/// ### Output
/// ```text
/// ShaderVariableDesc {
/// name: Some(
/// "scale",
/// ),
/// start_offset: 0,
/// size: 4,
/// flags: SVF::Used,
/// default_value: 0x0000000000000000,
/// start_texture: 4294967295,
/// texture_size: 0,
/// start_sampler: 4294967295,
/// sampler_size: 0,
/// }
/// ```
//#allow_missing_argument_docs
pub fn get_variable_by_name(&self, name: impl TryIntoAsCStr) -> ShaderReflectionVariable<'r> {
let name = name.try_into().ok();
let name = name.as_ref().map_or(cstr!("").as_cstr(), |n| n.as_cstr());
let ptr = unsafe { self.ptr.as_ref().GetVariableByName(name) };
unsafe { ShaderReflectionVariable::from_raw(self.phantom, ptr) }
}
}