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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
#![allow(clippy::transmute_ptr_to_ptr)]
#![allow(clippy::too_many_arguments)]

use crate::os::{HRESULT, LPCWSTR, LPWSTR};
use com::{interfaces, interfaces::IUnknown, IID};
use std::ffi::c_void;

pub type DxcCreateInstanceProc<T> =
    extern "system" fn(rclsid: &IID, riid: &IID, ppv: *mut Option<T>) -> HRESULT;

pub type DxcCreateInstanceProc2 = extern "system" fn(
    malloc: /* IMalloc */ *const c_void,
    rclsid: &IID,
    riid: &IID,
    ppv: *mut *mut c_void,
) -> HRESULT;

pub const DFCC_DXIL: u32 = u32::from_le_bytes([b'D', b'X', b'I', b'L']);

interfaces! {
    #[uuid("8ba5fb08-5195-40e2-ac58-0d989c3a0102")]
    pub(crate) unsafe interface IDxcBlob: IUnknown {
        pub(crate) fn get_buffer_pointer(&self) -> *mut c_void;
        pub(crate) fn get_buffer_size(&self) -> usize;
    }

    #[uuid("7241d424-2646-4191-97c0-98e96e42fc68")]
    pub(crate) unsafe interface IDxcBlobEncoding: IDxcBlob {
        pub(crate) fn get_encoding(&self, known: *mut u32, code_page: *mut u32) -> HRESULT;
    }

    #[uuid("e5204dc7-d18c-4c3c-bdfb-851673980fe7")]
    pub(crate) unsafe interface IDxcLibrary: IUnknown {
        pub(crate) fn set_malloc(&self, malloc: *const c_void) -> HRESULT;
        pub(crate) fn create_blob_from_blob(
            &self,
            blob: IDxcBlob,
            offset: u32,
            length: u32,
            result_blob: *mut Option<IDxcBlob>,
        ) -> HRESULT;
        pub(crate) fn create_blob_from_file(
            &self,
            filename: LPCWSTR,
            code_page: *const u32,
            blob_encoding: *mut Option<IDxcBlobEncoding>,
        ) -> HRESULT;
        pub(crate) fn create_blob_with_encoding_from_pinned(
            &self,
            text: *const c_void,
            size: u32,
            code_page: u32,
            blob_encoding: *mut Option<IDxcBlobEncoding>,
        ) -> HRESULT;
        pub(crate) fn create_blob_with_encoding_on_heap_copy(
            &self,
            text: *const c_void,
            size: u32,
            code_page: u32,
            blob_encoding: *mut Option<IDxcBlobEncoding>,
        ) -> HRESULT;
        pub(crate) fn create_blob_with_encoding_on_malloc(
            &self,
            text: *const c_void,
            malloc: *const /* IMalloc */ c_void,
            size: u32,
            code_page: u32,
            blob_encoding: *mut Option<IDxcBlobEncoding>,
        ) -> HRESULT;
        pub(crate) fn create_include_handler(
            &self,
            include_handler: *mut Option<IDxcIncludeHandler>,
        ) -> HRESULT;
        pub(crate) fn create_stream_from_blob_read_only(
            &self,
            blob: IDxcBlob,
            stream: *mut *mut /* IStream */ c_void,
        ) -> HRESULT;
        pub(crate) fn get_blob_as_utf8(
            &self,
            blob: IDxcBlob,
            blob_encoding: *mut Option<IDxcBlobEncoding>,
        ) -> HRESULT;
        pub(crate) fn get_blob_as_utf16(
            &self,
            blob: IDxcBlob,
            blob_encoding: *mut Option<IDxcBlobEncoding>,
        ) -> HRESULT;
    }

    #[uuid("cedb484a-d4e9-445a-b991-ca21ca157dc2")]
    pub(crate) unsafe interface IDxcOperationResult: IUnknown {
        pub(crate) fn get_status(&self, status: *mut u32) -> HRESULT;
        pub(crate) fn get_result(&self, result: *mut Option<IDxcBlob>) -> HRESULT;
        pub(crate) fn get_error_buffer(&self, errors: *mut Option<IDxcBlobEncoding>) -> HRESULT;
    }

    #[uuid("7f61fc7d-950d-467f-b3e3-3c02fb49187c")]
    pub(crate) unsafe interface IDxcIncludeHandler: IUnknown {
        pub(crate) fn load_source(
            &self,
            filename: LPCWSTR,
            include_source: *mut Option<IDxcBlob>,
        ) -> HRESULT;
    }
}

#[repr(C)]
pub struct DxcDefine {
    pub name: LPCWSTR,
    pub value: LPCWSTR,
}

interfaces! {
    #[uuid("8c210bf3-011f-4422-8d70-6f9acb8db617")]
    pub(crate) unsafe interface IDxcCompiler: IUnknown {
        pub(crate) fn compile(
            &self,
            blob: IDxcBlob,
            source_name: LPCWSTR,
            entry_point: LPCWSTR,
            target_profile: LPCWSTR,
            arguments: *const LPCWSTR,
            arg_count: u32,
            defines: *const DxcDefine,
            def_count: u32,
            include_handler: Option<IDxcIncludeHandler>,
            result: *mut Option<IDxcOperationResult>,
        ) -> HRESULT;

        pub(crate) fn preprocess(
            &self,
            blob: IDxcBlob,
            source_name: LPCWSTR,
            arguments: *const LPCWSTR,
            arg_count: u32,
            defines: *const DxcDefine,
            def_count: u32,
            include_handler: Option<IDxcIncludeHandler>,
            result: *mut Option<IDxcOperationResult>,
        ) -> HRESULT;

        pub(crate) fn disassemble(
            &self,
            blob: IDxcBlob,
            disassembly: *mut Option<IDxcBlobEncoding>,
        ) -> HRESULT;
    }

    #[uuid("a005a9d9-b8bb-4594-b5c9-0e633bec4d37")]
    pub(crate) unsafe interface IDxcCompiler2: IDxcCompiler {
        pub(crate) fn compile_with_debug(
            &self,
            blob: IDxcBlob,
            source_name: LPCWSTR,
            entry_point: LPCWSTR,
            target_profile: LPCWSTR,
            arguments: *const LPCWSTR,
            arg_count: u32,
            defines: *const DxcDefine,
            def_count: u32,
            include_handler: Option<IDxcIncludeHandler>,
            result: *mut Option<IDxcOperationResult>,
            debug_blob_name: *mut LPWSTR,
            debug_blob: *mut Option<IDxcBlob>,
        ) -> HRESULT;
    }

    #[uuid("f1b5be2a-62dd-4327-a1c2-42ac1e1e78e6")]
    pub(crate) unsafe interface IDxcLinker: IUnknown {
        pub(crate) fn register_library(&self, lib_name: LPCWSTR, lib: IDxcBlob) -> HRESULT;

        pub(crate) fn link(
            &self,
            entry_name: LPCWSTR,
            target_profile: LPCWSTR,
            lib_names: *const LPCWSTR,
            lib_count: u32,
            arguments: *const LPCWSTR,
            arg_count: u32,
            result: *mut Option<IDxcOperationResult>,
        ) -> HRESULT;
    }
}

pub const DXC_VALIDATOR_FLAGS_DEFAULT: u32 = 0;
pub const DXC_VALIDATOR_FLAGS_IN_PLACE_EDIT: u32 = 1; // Validator is allowed to update shader blob in-place.
pub const DXC_VALIDATOR_FLAGS_ROOT_SIGNATURE_ONLY: u32 = 2;
pub const DXC_VALIDATOR_FLAGS_MODULE_ONLY: u32 = 4;
pub const DXC_VALIDATOR_FLAGS_VALID_MASK: u32 = 0x7;

interfaces! {
    #[uuid("a6e82bd2-1fd7-4826-9811-2857e797f49a")]
    pub(crate) unsafe interface IDxcValidator: IUnknown {
        pub(crate) fn validate(
            &self,
            shader: IDxcBlob,
            flags: u32,
            result: *mut Option<IDxcOperationResult>,
        ) -> HRESULT;
    }

    #[uuid("334b1f50-2292-4b35-99a1-25588d8c17fe")]
    pub(crate) unsafe interface IDxcContainerBuilder: IUnknown {
        pub(crate) fn load(&self, dxil_container_header: IDxcBlob) -> HRESULT;
        pub(crate) fn add_part(&self, four_cc: u32, source: IDxcBlob) -> HRESULT;
        pub(crate) fn remove_part(&self, four_cc: u32) -> HRESULT;
        pub(crate) fn seralize_container(
            &self,
            result: *mut Option<IDxcOperationResult>,
        ) -> HRESULT;
    }

    #[uuid("091f7a26-1c1f-4948-904b-e6e3a8a771d5")]
    pub(crate) unsafe interface IDxcAssembler: IUnknown {
        pub(crate) fn assemble_to_container(
            &self,
            shader: IDxcBlob,
            result: *mut Option<IDxcOperationResult>,
        ) -> HRESULT;
    }

    #[uuid("d2c21b26-8350-4bdc-976a-331ce6f4c54c")]
    pub(crate) unsafe interface IDxcContainerReflection: IUnknown {
        pub(crate) fn load(&self, container: IDxcBlob) -> HRESULT;
        pub(crate) fn get_part_count(&self, result: *mut u32) -> HRESULT;
        pub(crate) fn get_part_kind(&self, idx: u32, result: *mut u32) -> HRESULT;
        pub(crate) fn get_part_content(&self, idx: u32, result: *mut Option<IDxcBlob>) -> HRESULT;
        pub(crate) fn find_first_part_kind(&self, kind: u32, result: *mut u32) -> HRESULT;
        pub(crate) fn get_part_reflection(
            &self,
            idx: u32,
            iid: *const IID,
            object: *mut Option<IUnknown>,
        ) -> HRESULT;
    }

    #[uuid("5a58797d-a72c-478d-8ba2-efc6b0efe88e")]
    pub(crate) unsafe interface ID3D12ShaderReflection: IUnknown {
        pub(crate) fn get_desc(&self, p_desc: *mut c_void) -> HRESULT;
        pub(crate) fn get_constant_buffer_by_index(&self, index: u32) -> *mut c_void;
        pub(crate) fn get_constant_buffer_by_name(&self, name: *const c_void) -> *mut c_void;
        pub(crate) fn get_resource_binding_desc(
            &self,
            resource_index: u32,
            p_desc: *mut c_void,
        ) -> HRESULT;
        pub(crate) fn get_input_parameter_desc(
            &self,
            parameter_index: u32,
            p_desc: *mut c_void,
        ) -> HRESULT;
        pub(crate) fn get_output_parameter_desc(
            &self,
            parameter_index: u32,
            p_desc: *mut c_void,
        ) -> HRESULT;
        pub(crate) fn get_patch_constant_parameter_desc(
            &self,
            parameter_index: u32,
            p_desc: *mut c_void,
        ) -> HRESULT;
        pub(crate) fn get_variable_by_name(&self, name: *const c_void) -> *mut c_void;
        pub(crate) fn get_resource_binding_desc_by_name(
            &self,
            name: *const c_void,
            p_desc: *mut c_void,
        ) -> HRESULT;
        pub(crate) fn get_mov_instruction_count(&self) -> u32;
        pub(crate) fn get_movc_instruction_count(&self) -> u32;
        pub(crate) fn get_conversion_instruction_count(&self) -> u32;
        pub(crate) fn get_bitwise_instruction_count(&self) -> u32;
        pub(crate) fn get_gs_input_primitive(&self) -> u32;
        pub(crate) fn is_sample_frequency_shader(&self) -> bool;
        pub(crate) fn get_num_interface_slots(&self) -> u32;
        pub(crate) fn get_min_feature_level(&self, p_level: *mut c_void) -> HRESULT;
        pub(crate) fn get_thread_group_size(
            &self,
            size_x: *mut u32,
            size_y: *mut u32,
            size_z: *mut u32,
        ) -> u32;
        pub(crate) fn get_requires_flags(&self) -> u64;
    }

    #[uuid("ae2cd79f-cc22-453f-9b6b-b124e7a5204c")]
    pub(crate) unsafe interface IDxcOptimizerPass: IUnknown {
        pub(crate) fn get_option_name(&self, result: *mut LPWSTR) -> HRESULT;
        pub(crate) fn get_description(&self, result: *mut LPWSTR) -> HRESULT;
        pub(crate) fn get_option_arg_count(&self, count: *mut u32) -> HRESULT;
        pub(crate) fn get_option_arg_name(&self, arg_idx: u32, result: *mut LPWSTR) -> HRESULT;
        pub(crate) fn get_option_arg_description(
            &self,
            arg_idx: u32,
            result: *mut LPWSTR,
        ) -> HRESULT;
    }

    #[uuid("25740e2e-9cba-401b-9119-4fb42f39f270")]
    pub(crate) unsafe interface IDxcOptimizer: IUnknown {
        pub(crate) fn get_available_pass_count(&self, count: *mut u32) -> HRESULT;
        pub(crate) fn get_available_pass(
            &self,
            index: u32,
            result: *mut Option<IDxcOptimizerPass>,
        ) -> HRESULT;
        pub(crate) fn run_optimizer(
            &self,
            blob: IDxcBlob,
            options: *const LPCWSTR,
            option_count: u32,
            output_module: *mut Option<IDxcBlob>,
            output_text: *mut Option<IDxcBlobEncoding>,
        ) -> HRESULT;
    }
}

pub const DXC_VERSION_INFO_FLAGS_NONE: u32 = 0;
pub const DXC_VERSION_INFO_FLAGS_DEBUG: u32 = 1; // Matches VS_FF_DEBUG
pub const DXC_VERSION_INFO_FLAGS_INTERNAL: u32 = 2; // Internal Validator (non-signing)

interfaces! {
    #[uuid("b04f5b50-2059-4f12-a8ff-a1e0cde1cc7e")]
    pub(crate) unsafe interface IDxcVersionInfo: IUnknown {
        pub(crate) fn get_version(&self, major: *mut u32, minor: *mut u32) -> HRESULT;
        pub(crate) fn get_flags(&self, flags: *mut u32) -> HRESULT;
    }

    #[uuid("fb6904c4-42f0-4b62-9c46-983af7da7c83")]
    pub(crate) unsafe interface IDxcVersionInfo2: IUnknown {
        pub(crate) fn get_commit_info(
            &self,
            commit_count: *mut u32,
            commit_hash: *mut *mut u8,
        ) -> HRESULT;
    }
}

pub const CLSID_DxcCompiler: IID = IID {
    data1: 0x73e22d93,
    data2: 0xe6ce,
    data3: 0x47f3,
    data4: [0xb5, 0xbf, 0xf0, 0x66, 0x4f, 0x39, 0xc1, 0xb0],
};
pub const CLSID_DxcLinker: IID = IID {
    data1: 0xef6a8087,
    data2: 0xb0ea,
    data3: 0x4d56,
    data4: [0x9e, 0x45, 0xd0, 0x7e, 0x1a, 0x8b, 0x78, 0x6],
};
pub const CLSID_DxcDiaDataSource: IID = IID {
    data1: 0xcd1f6b73,
    data2: 0x2ab0,
    data3: 0x484d,
    data4: [0x8e, 0xdc, 0xeb, 0xe7, 0xa4, 0x3c, 0xa0, 0x9f],
};
pub const CLSID_DxcLibrary: IID = IID {
    data1: 0x6245d6af,
    data2: 0x66e0,
    data3: 0x48fd,
    data4: [0x80, 0xb4, 0x4d, 0x27, 0x17, 0x96, 0x74, 0x8c],
};
pub const CLSID_DxcValidator: IID = IID {
    data1: 0x8ca3e215,
    data2: 0xf728,
    data3: 0x4cf3,
    data4: [0x8c, 0xdd, 0x88, 0xaf, 0x91, 0x75, 0x87, 0xa1],
};
pub const CLSID_DxcAssembler: IID = IID {
    data1: 0xd728db68,
    data2: 0xf903,
    data3: 0x4f80,
    data4: [0x94, 0xcd, 0xdc, 0xcf, 0x76, 0xec, 0x71, 0x51],
};
pub const CLSID_DxcContainerReflection: IID = IID {
    data1: 0xb9f54489,
    data2: 0x55b8,
    data3: 0x400c,
    data4: [0xba, 0x3a, 0x16, 0x75, 0xe4, 0x72, 0x8b, 0x91],
};
pub const CLSID_DxcOptimizer: IID = IID {
    data1: 0xae2cd79f,
    data2: 0xcc22,
    data3: 0x453f,
    data4: [0x9b, 0x6b, 0xb1, 0x24, 0xe7, 0xa5, 0x20, 0x4c],
};
pub const CLSID_DxcContainerBuilder: IID = IID {
    data1: 0x94134294,
    data2: 0x411f,
    data3: 0x4574,
    data4: [0xb4, 0xd0, 0x87, 0x41, 0xe2, 0x52, 0x40, 0xd2],
};