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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
use crate::*;
use crate::d3d::*;
use std::ptr::*;
/// <h1 id="parts" class="section-header"><a href="#parts">Manipulate Bytecode by BlobPart</a></h1>
impl Compiler {
/// \[[docs.microsoft.com](https://docs.microsoft.com/en-us/windows/win32/api/d3dcompiler/nf-d3dcompiler-d3dgetblobpart)\]
/// D3DGetBlobPart
///
/// Retrieves a specific part from a compilation result.
///
/// ### Arguments
/// * `src_data` - Compiled HLSL code.
/// * `part` - The [BlobPart] to retrieve
/// * `flags` - Reserved (pass [None])
///
/// ### Errors
/// * [THINERR::MISSING_DLL_EXPORT] - on `d3dcompiler_42.dll` and earlier
/// * [D3DERR::INVALIDCALL] - on data that wasn't compiled shader code.
///
/// ### Example
/// ```rust
/// # use thindx::d3d::*; let d3dc = Compiler::load_system(47).unwrap();
/// # let shader = d3dc.compile_from_file(r"test\data\basic.hlsl", None, None, "ps_main", "ps_4_0", Compile::Debug, CompileEffect::None).unwrap();
/// let shader2 = d3dc.set_blob_part(
/// &shader, Blob::PrivateData, (), b"testing 123"
/// ).unwrap();
///
/// assert_eq!(b"testing 123", d3dc.get_blob_part(
/// &shader2, Blob::PrivateData, None
/// ).unwrap().as_bytes());
/// ```
///
/// ### Remarks
/// * This was introduced by d3dcompiler_43.dll, and is unavailable in earlier versions.
pub fn get_blob_part(&self, src_data: &Bytecode, part: impl Into<BlobPart>, flags: Option<std::convert::Infallible>) -> Result<BytesBlob, MethodError> {
let f = self.D3DGetBlobPart.ok_or(MethodError("D3DGetBlobPart", THINERR::MISSING_DLL_EXPORT))?;
let src_data = src_data.as_bytes();
let part = part.into().into();
let _ = flags;
let mut blob = null_mut();
// SAFETY: ❌ needs fuzz testing against ~4GB `data` to attempt to induce alloc overflow bugs
// * `f` ✔️ should be valid/sound like all `self.*`
// * `src_data` ❌ needs fuzz testing against ~4GB data to attempt to induce alloc overflow bugs
// * `src_data` ✔️ should be valid bytecode as implied by [`Bytecode`]
// * `part` ⚠️ could be invalid
// * `flags` ✔️ are reserved/0
// * `blob` ✔️ is a simple out-param
let hr = unsafe { f(src_data.as_ptr().cast(), src_data.len(), part, 0, &mut blob) };
MethodError::check("D3DGetBlobPart", hr)?;
// SAFETY: ✔️ `blob` is null (from_raw panics) or a valid non-dangling pointer (from_raw takes ownership). ReadOnlyBlob imposes no content requirements.
Ok(BytesBlob::new(unsafe { ReadOnlyBlob::from_raw(blob) }))
}
/// \[[docs.microsoft.com](https://docs.microsoft.com/en-us/windows/win32/api/d3dcompiler/nf-d3dcompiler-d3dgetdebuginfo)\]
/// D3DGetDebugInfo
///
/// > **Note:** You can use this API to develop your Windows Store apps, but you can't use it in apps that you submit to the Windows Store.
///
/// Gets shader debug information.
///
/// ### Arguments
/// * `src_data` - Shader bytecode
///
/// ### Errors
/// * [THINERR::MISSING_DLL_EXPORT] - `d3dcompiler_39.dll` and earlier
/// * [E::FAIL] - `src_data` wasn't compiled with [d3d::Compile::Debug]
/// * [E::FAIL] - `src_data`'s debug info is too modern? (cannot load debug info for shaders compiled with `d3dcompiler_47.dll`)
///
/// ### Example
/// ```rust
/// # use thindx::*;
/// # if d3d::Compiler::load_system(43).is_err() { return; }
/// let d3dc = d3d::Compiler::load_system(43).unwrap();
/// # let shader_src = std::fs::read(r"test\data\basic.hlsl").unwrap();
/// # let shader = d3dc.compile(&shader_src, cstr!(r"test\data\basic.hlsl"), None, None, "ps_main", "ps_4_0", d3d::Compile::Debug, d3d::CompileEffect::None).unwrap();
/// let debug_info : d3d::BytesBlob = d3dc.get_debug_info(&shader).unwrap();
/// assert!(debug_info.len() > 0);
/// #
/// # // This is not legal, despite MSDN suggesting that src_data can contain "either uncompiled or compiled HLSL code" (didn't sound right)
/// # assert_eq!(d3dc.get_debug_info(unsafe { d3d::Bytecode::from_unchecked(&shader_src) }).err().map(|err| err.kind()), Some(E::FAIL), "uncompiled shader code isn't actually legal");
/// #
/// # let shader = d3dc.compile(&shader_src, cstr!(r"test\data\basic.hlsl"), None, None, "ps_main", "ps_4_0", d3d::Compile::None, d3d::CompileEffect::None).unwrap();
/// # assert_eq!(d3dc.get_debug_info(&shader).err().map(|err| err.kind()), Some(E::FAIL), "shader was compiled without debug info");
/// #
/// # let d3dc = d3d::Compiler::load_system(47).unwrap();
/// # let shader = d3dc.compile(&shader_src, cstr!(r"test\data\basic.hlsl"), None, None, "ps_main", "ps_4_0", d3d::Compile::Debug, d3d::CompileEffect::None).unwrap();
/// # assert_eq!(d3dc.get_debug_info(&shader).err().map(|err| err.kind()), Some(E::FAIL), "shader was compiled with d3dcompiler_47.dll");
/// ```
///
/// ### Output
/// ```text
/// BytesBlob(2625 bytes)
/// ```
///
/// ### Remarks
/// * This was introduced by d3dcompiler_40.dll, and is unavailable in earlier versions.
pub fn get_debug_info(&self, src_data: &Bytecode) -> Result<BytesBlob, MethodError> {
let f = self.D3DGetDebugInfo.ok_or(MethodError("D3DGetDebugInfo", THINERR::MISSING_DLL_EXPORT))?;
let src_data = src_data.as_bytes();
// SAFETY: ❌ needs fuzz testing against ~4GB `data` to attempt to induce alloc overflow bugs
// * `f` ✔️ should be valid/sound like all `self.*`
// * `src_data` ❌ needs fuzz testing against ~4GB data to attempt to induce alloc overflow bugs
// * `src_data` ✔️ should be valid bytecode as implied by [`Bytecode`]
// * `blob` ✔️ is a simple out-param
let mut blob = null_mut();
let hr = unsafe { f(src_data.as_ptr().cast(), src_data.len(), &mut blob) };
MethodError::check("D3DGetDebugInfo", hr)?;
// SAFETY: ✔️ `blob` is null (from_raw panics) or a valid non-dangling pointer (from_raw takes ownership). ReadOnlyBlob imposes no content requirements.
Ok(BytesBlob::new(unsafe { ReadOnlyBlob::from_raw(blob) }))
}
/// \[[docs.microsoft.com](https://docs.microsoft.com/en-us/windows/win32/api/d3dcompiler/nf-d3dcompiler-d3dgetinputandoutputsignatureblob)\]
/// D3DGetInputAndOutputSignatureBlob
///
/// > **Note:** [get_input_and_output_signature_blob](Self::get_input_and_output_signature_blob) may be altered or
/// > unavailable for releases after Windows 8.1. Instead use [get_glob_part](Self::get_blob_part) with the
/// > [Blob::InputAndOutputSignatureBlob] value.
///
/// Gets the input and output signatures from a compilation result.
///
/// ### Example
/// ```rust
/// # use thindx::d3d::*; let d3dc = Compiler::load_system(47).unwrap();
/// # let shader = d3dc.compile_from_file(r"test\data\basic.hlsl", None, None, "ps_main", "ps_4_0", Compile::Debug, CompileEffect::None).unwrap();
/// let signature = d3dc.get_input_and_output_signature_blob(&shader).unwrap();
/// println!("{:?}", &signature);
/// ```
///
/// ### Output
/// ```text
/// [68, 88, 66, 67, 97, ...
/// ```
// #[requires(d3dcompiler=33)] // or earlier?
//#allow_missing_argument_docs
pub fn get_input_and_output_signature_blob(&self, src_data: &Bytecode) -> Result<BytesBlob, MethodError> {
let f = self.D3DGetInputAndOutputSignatureBlob.ok_or(MethodError("D3DGetInputAndOutputSignatureBlob", THINERR::MISSING_DLL_EXPORT))?;
let src_data = src_data.as_bytes();
// SAFETY: ❌ needs fuzz testing against ~4GB `data` to attempt to induce alloc overflow bugs
// * `f` ✔️ should be valid/sound like all `self.*`
// * `src_data` ❌ needs fuzz testing against ~4GB data to attempt to induce alloc overflow bugs
// * `src_data` ✔️ should be valid bytecode as implied by [`Bytecode`]
// * `blob` ✔️ is a simple out-param
let mut blob = null_mut();
let hr = unsafe { f(src_data.as_ptr().cast(), src_data.len(), &mut blob) };
MethodError::check("D3DGetInputAndOutputSignatureBlob", hr)?;
// SAFETY: ✔️ `blob` is null (from_raw panics) or a valid non-dangling pointer (from_raw takes ownership). ReadOnlyBlob imposes no content requirements.
Ok(BytesBlob::new(unsafe { ReadOnlyBlob::from_raw(blob) }))
}
/// \[[docs.microsoft.com](https://docs.microsoft.com/en-us/windows/win32/api/d3dcompiler/nf-d3dcompiler-d3dgetinputsignatureblob)\]
/// D3DGetInputSignatureBlob
///
/// > **Note:** [get_input_signature_blob](Self::get_input_signature_blob) may be altered or
/// > unavailable for releases after Windows 8.1. Instead use [get_glob_part](Self::get_blob_part) with the
/// > [Blob::InputSignatureBlob] value.
///
/// Gets the input signature from a compilation result.
///
/// ### Example
/// ```rust
/// # use thindx::d3d::*; let d3dc = Compiler::load_system(47).unwrap();
/// # let shader = d3dc.compile_from_file(r"test\data\basic.hlsl", None, None, "ps_main", "ps_4_0", Compile::Debug, CompileEffect::None).unwrap();
/// let signature = d3dc.get_input_signature_blob(&shader).unwrap();
/// println!("{:?}", &signature);
/// ```
///
/// ### Output
/// ```text
/// [68, 88, 66, 67, 53, ...
/// ```
// #[requires(d3dcompiler=33)] // or earlier?
//#allow_missing_argument_docs
pub fn get_input_signature_blob(&self, src_data: &Bytecode) -> Result<BytesBlob, MethodError> {
let f = self.D3DGetInputSignatureBlob.ok_or(MethodError("D3DGetInputSignatureBlob", THINERR::MISSING_DLL_EXPORT))?;
let src_data = src_data.as_bytes();
// SAFETY: ❌ needs fuzz testing against ~4GB `data` to attempt to induce alloc overflow bugs
// * `f` ✔️ should be valid/sound like all `self.*`
// * `src_data` ❌ needs fuzz testing against ~4GB data to attempt to induce alloc overflow bugs
// * `src_data` ✔️ should be valid bytecode as implied by [`Bytecode`]
// * `blob` ✔️ is a simple out-param
let mut blob = null_mut();
let hr = unsafe { f(src_data.as_ptr().cast(), src_data.len(), &mut blob) };
MethodError::check("D3DGetInputSignatureBlob", hr)?;
// SAFETY: ✔️ `blob` is null (from_raw panics) or a valid non-dangling pointer (from_raw takes ownership). ReadOnlyBlob imposes no content requirements.
Ok(BytesBlob::new(unsafe { ReadOnlyBlob::from_raw(blob) }))
}
/// \[[docs.microsoft.com](https://docs.microsoft.com/en-us/windows/win32/api/d3dcompiler/nf-d3dcompiler-d3dgetoutputsignatureblob)\]
/// D3DGetOutputSignatureBlob
///
/// > **Note:** [get_output_signature_blob](Self::get_output_signature_blob) may be altered or
/// > unavailable for releases after Windows 8.1. Instead use [get_glob_part](Self::get_blob_part) with the
/// > [Blob::OutputSignatureBlob] value.
///
/// Gets the output signature from a compilation result.
///
/// ### Example
/// ```rust
/// # use thindx::d3d::*; let d3dc = Compiler::load_system(47).unwrap();
/// # let shader = d3dc.compile_from_file(r"test\data\basic.hlsl", None, None, "ps_main", "ps_4_0", Compile::Debug, CompileEffect::None).unwrap();
/// let signature = d3dc.get_output_signature_blob(&shader).unwrap();
/// println!("{:?}", &signature);
/// ```
///
/// ### Output
/// ```text
/// [68, 88, 66, 67, 210, ...
/// ```
// #[requires(d3dcompiler=33)] // or earlier?
//#allow_missing_argument_docs
pub fn get_output_signature_blob(&self, src_data: &Bytecode) -> Result<BytesBlob, MethodError> {
let f = self.D3DGetOutputSignatureBlob.ok_or(MethodError("D3DGetOutputSignatureBlob", THINERR::MISSING_DLL_EXPORT))?;
let src_data = src_data.as_bytes();
// SAFETY: ❌ needs fuzz testing against ~4GB `data` to attempt to induce alloc overflow bugs
// * `f` ✔️ should be valid/sound like all `self.*`
// * `src_data` ❌ needs fuzz testing against ~4GB data to attempt to induce alloc overflow bugs
// * `src_data` ✔️ should be valid bytecode as implied by [`Bytecode`]
// * `blob` ✔️ is a simple out-param
let mut blob = null_mut();
let hr = unsafe { f(src_data.as_ptr().cast(), src_data.len(), &mut blob) };
MethodError::check("D3DGetOutputSignatureBlob", hr)?;
// SAFETY: ✔️ `blob` is null (from_raw panics) or a valid non-dangling pointer (from_raw takes ownership). ReadOnlyBlob imposes no content requirements.
Ok(BytesBlob::new(unsafe { ReadOnlyBlob::from_raw(blob) }))
}
/// \[[docs.microsoft.com](https://docs.microsoft.com/en-us/windows/win32/api/d3dcompiler/nf-d3dcompiler-d3dsetblobpart)\]
/// D3DSetBlobPart
///
/// Sets information in a compilation result.
///
/// ### Arguments
/// * `src_data` - The original compiled shader data.
/// * `part` - What part to replace (currently only [Blob::PrivateData] is supported.)
/// * `flags` - Resereved. Pass `()`.
/// * `part_data` - The part data to set.
///
/// ### Errors
/// * [THINERR::MISSING_DLL_EXPORT] - `d3dcompiler_43.dll` and earlier
///
/// ### Example
/// ```rust
/// # use thindx::d3d::*; let d3dc = Compiler::load_system(47).unwrap();
/// # let shader = d3dc.compile_from_file(r"test\data\basic.hlsl", None, None, "ps_main", "ps_4_0", Compile::Debug, CompileEffect::None).unwrap();
/// let shader2 = d3dc.set_blob_part(
/// &shader, Blob::PrivateData, (), b"testing 123"
/// ).unwrap();
///
/// assert_eq!(b"testing 123", d3dc.get_blob_part(
/// &shader2, Blob::PrivateData, None
/// ).unwrap().as_bytes());
/// ```
///
/// ### Remarks
/// * This was introduced by d3dcompiler_44.dll, and is unavailable in earlier versions.
pub fn set_blob_part(
&self,
src_data: &Bytecode,
part: impl Into<BlobPart>,
flags: (),
part_data: &[u8],
) -> Result<CodeBlob, MethodError> {
let f = self.D3DSetBlobPart.ok_or(MethodError("D3DSetBlobPart", THINERR::MISSING_DLL_EXPORT))?;
let src_data = src_data.as_bytes();
let _ = flags;
// SAFETY: ❌ needs fuzz testing against ~4GB `data` to attempt to induce alloc overflow bugs
// * `f` ✔️ should be valid/sound like all `self.*`
// * `src_data` ❌ needs fuzz testing against ~4GB data to attempt to induce alloc overflow bugs
// * `src_data` ✔️ should be valid bytecode as implied by [`Bytecode`]
// * `blob` ✔️ is a simple out-param
// * `part` ⚠️ could be invalid
// * `part_data` ❌ could be invalid for `part`
let mut blob = null_mut();
let hr = unsafe { f(src_data.as_ptr().cast(), src_data.len(), part.into().into(), 0, part_data.as_ptr().cast(), part_data.len(), &mut blob) };
MethodError::check("D3DSetBlobPart", hr)?;
// SAFETY: ⚠️
// * `blob` ✔️ should be null (from_raw panics) or a valid non-dangling blob (from_raw takes ownership)
// * `blob` ⚠️ should be a shader blob, but invalid params above could violate CodeBlob's precondition (of the blob being valid DXBC or DXIL bytecode)
Ok(unsafe { CodeBlob::from_unchecked(ReadOnlyBlob::from_raw(blob)) })
}
/// \[[docs.microsoft.com](https://docs.microsoft.com/en-us/windows/win32/api/d3dcompiler/nf-d3dcompiler-d3dstripshader)\]
/// D3DStripShader
///
/// Removes unwanted blobs from a compilation result.
///
/// ### Arguments
/// * `src_data` - The original shader bytecode.
/// * `strip_flags` - The [CompilerStripFlags] to use.
///
/// ### Errors
/// * [THINERR::MISSING_DLL_EXPORT] - `d3dcompiler_39.dll` and earlier
///
/// ### Example
/// ```rust
/// # use thindx::d3d::*; let d3dc = Compiler::load_system(47).unwrap();
/// let shader = d3dc.compile_from_file(
/// r"test\data\basic.hlsl", None, None, "ps_main", "ps_4_0",
/// Compile::Debug, CompileEffect::None
/// ).unwrap();
///
/// let shader = d3dc.strip_shader(
/// &shader, CompilerStripFlags::DebugInfo
/// ).unwrap();
/// ```
///
/// ### Remarks
/// * This was introduced by d3dcompiler_40.dll, and is unavailable in earlier versions.
pub fn strip_shader(
&self,
src_data: &Bytecode,
strip_flags: impl Into<CompilerStripFlags>,
) -> Result<CodeBlob, MethodError> {
let f = self.D3DStripShader.ok_or(MethodError("D3DStripShader", THINERR::MISSING_DLL_EXPORT))?;
let mut blob = null_mut();
// SAFETY: ❌ needs fuzz testing against ~4GB `data` to attempt to induce alloc overflow bugs
// * `f` ✔️ should be valid/sound like all `self.*`
// * `src_data` ❌ needs fuzz testing against ~4GB data to attempt to induce alloc overflow bugs
// * `src_data` ✔️ should be valid bytecode as implied by [`Bytecode`]
// * `strip_flags` ⚠️ could be invalid
// * `blob` ✔️ is a simple out-param
let hr = unsafe { f(src_data.as_ptr().cast(), src_data.len(), strip_flags.into().into(), &mut blob) };
MethodError::check("D3DStripShader", hr)?;
// SAFETY: ⚠️
// * `blob` ✔️ should be null (from_raw panics) or a valid non-dangling blob (from_raw takes ownership)
// * `blob` ⚠️ should be a shader blob, but invalid params above could violate CodeBlob's precondition (of the blob being valid DXBC or DXIL bytecode)
Ok(unsafe { CodeBlob::from_unchecked(ReadOnlyBlob::from_raw(blob)) })
}
}