taut 0.2.2

A Rust GUI library for Windows, Linux and macOS that aims to be small, fast and accessible, with zero dependencies and nothing compiled in that your program doesn't use.
Documentation
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
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
//! Direct3D 12 drawing (the feature `gpu-draw`): a recorded paint (`gpu_draw::DrawList`) drawn
//! by Direct3D 12 into a window's texture, as Direct3D 11 draws it (`gpu_draw_windows`).
//!
//! - The shaders are Direct3D 11's HLSL, compiled when the device is made by
//!   `d3dcompiler_47.dll` for shader model 5.1 (`vs_5_1`, `ps_5_1`), with a root signature of
//!   one table: the quads, the cache and the patches (`rootsig_1_0`).
//! - The quads go up into a buffer in the upload heap, read where they are; the texels through
//!   an upload buffer, copied into the cache and patch textures.
//! - Everything is recorded into the device's command list, after the window's waiting copies:
//!   the caller runs it and waits.
use crate::gpu_draw::{DrawList, Quad, SIDE};
use crate::gpu_draw_windows::{MAIN, PRELUDE, compiled};
use crate::layer_d3d11::{B8G8R8A8_UNORM, Box3, Com, function, release};
use crate::windows::named;
use std::ffi::c_void;
use std::ptr::{null, null_mut};

/// The interfaces made.
const ROOT: [u32; 4] = named(0xC54A_6B66, 0x72DF, 0x4EE8, [0x8B, 0xE5, 0xA9, 0x46, 0xA1, 0x42, 0x92, 0x14]);
const PIPELINE: [u32; 4] = named(0x765A_30F3, 0xF624, 0x4C6F, [0xA8, 0x28, 0xAC, 0xE9, 0x48, 0x62, 0x24, 0x45]);
const HEAP: [u32; 4] = named(0x8EFB_471D, 0x616C, 0x4F49, [0x90, 0xF7, 0x12, 0x7B, 0xB7, 0x63, 0xFA, 0x51]);
const RESOURCE: [u32; 4] = named(0x6964_42BE, 0xA72E, 0x4059, [0xBC, 0x79, 0x5B, 0x5C, 0x98, 0x04, 0x0F, 0xAD]);

// Resource states, as d3d12.h has them.
pub(crate) const RENDER_TARGET: u32 = 0x4;
const PIXEL_SHADER_RESOURCE: u32 = 0x80;
const COPY_DESTINATION: u32 = 0x400;
const GENERIC_READ: u32 = 0xAC3;
/// A resource that may be drawn into (`D3D12_RESOURCE_FLAG_ALLOW_RENDER_TARGET`).
pub(crate) const DRAWN_INTO: u32 = 0x1;
/// Rows copied from a buffer start a multiple of this apart, and each copy at twice it.
const PITCH: usize = 256;
/// The root signature: one table of three views, the quads (t0), the cache (t1) and the
/// patches (t2), seen by every stage.
const SIGNATURE: &str = "#define ROOT \"DescriptorTable(SRV(t0, numDescriptors = 3))\"\n";

/// `D3D12_HEAP_PROPERTIES`.
#[repr(C)]
struct HeapProperties {
    kind: u32,
    page: u32,
    pool: u32,
    creation_mask: u32,
    visible_mask: u32,
}

/// `D3D12_RESOURCE_DESC`.
#[repr(C)]
struct ResourceDescription {
    dimension: u32,
    alignment: u64,
    width: u64,
    height: u32,
    depth: u16,
    levels: u16,
    format: u32,
    samples: [u32; 2],
    layout: u32,
    flags: u32,
}

/// `D3D12_TEXTURE_COPY_LOCATION`: a texture's first subresource (`kind` 0), or a placed
/// footprint in a buffer (1): its offset, format, width, height, depth and row pitch.
#[repr(C)]
struct Place {
    resource: Com,
    kind: u32,
    offset: u64,
    footprint: [u32; 5],
}

/// `D3D12_RESOURCE_BARRIER`, a transition of a whole resource.
#[repr(C)]
struct Barrier {
    kind: u32,
    flags: u32,
    resource: Com,
    subresource: u32,
    before: u32,
    after: u32,
}

/// A transition of all of `resource` from `before` to `after`.
#[inline]
fn barrier(resource: Com, before: u32, after: u32) -> Barrier {
    Barrier { kind: 0, flags: 0, resource, subresource: 0xFFFF_FFFF, before, after }
}

/// `D3D12_SHADER_BYTECODE`.
#[repr(C)]
#[derive(Clone, Copy)]
struct Bytecode {
    code: *const u32,
    size: usize,
}

/// `D3D12_STREAM_OUTPUT_DESC`.
#[repr(C)]
struct StreamOutput {
    declarations: *const c_void,
    count: u32,
    strides: *const u32,
    stride_count: u32,
    rasterized: u32,
}

/// `D3D12_RENDER_TARGET_BLEND_DESC`.
#[repr(C)]
#[derive(Clone, Copy)]
struct TargetBlend {
    enable: i32,
    logic_enable: i32,
    source: u32,
    destination: u32,
    operation: u32,
    source_alpha: u32,
    destination_alpha: u32,
    operation_alpha: u32,
    logic: u32,
    write_mask: u8,
}

/// `D3D12_GRAPHICS_PIPELINE_STATE_DESC`, as d3d12.h lays it out.
#[repr(C)]
struct PipelineDescription {
    root: Com,
    /// Vertex, pixel, domain, hull and geometry shaders.
    shaders: [Bytecode; 5],
    stream_output: StreamOutput,
    alpha_to_coverage: i32,
    independent_blend: i32,
    targets: [TargetBlend; 8],
    sample_mask: u32,
    /// Fill, cull, front counter-clockwise, depth bias, its clamp and slope (as bits), depth
    /// clip, multisample, antialiased lines, forced sample count, conservative rasterization.
    rasterizer: [u32; 11],
    /// Depth and stencil, all off.
    depth: [u32; 13],
    input_layout: [usize; 2],
    strip_cut: u32,
    topology: u32,
    target_count: u32,
    formats: [u32; 8],
    depth_format: u32,
    samples: [u32; 2],
    node_mask: u32,
    cached: [usize; 2],
    flags: u32,
}

/// `D3D12_SHADER_RESOURCE_VIEW_DESC` of a buffer: format, dimension (1), component mapping,
/// then the first element, the count, the stride and flags.
#[repr(C)]
struct BufferView {
    format: u32,
    dimension: u32,
    mapping: u32,
    first: u64,
    count: u32,
    stride: u32,
    flags: u32,
}

/// A buffer in the upload heap: it, where the CPU writes it, and its size in bytes.
#[derive(Clone, Copy, Debug)]
struct Written {
    buffer: Com,
    at: *mut u8,
    room: usize,
}

/// A committed resource of `described` in a heap of `kind`, starting in `state`.
fn committed(device: Com, kind: u32, described: &ResourceDescription, state: u32) -> Option<Com> {
    let (heap, mut made) = (HeapProperties { kind, page: 0, pool: 0, creation_mask: 0, visible_mask: 0 }, null_mut());
    type Make = unsafe extern "system" fn(Com, *const HeapProperties, u32, *const ResourceDescription, u32, *const c_void, *const [u32; 4], *mut Com) -> i32;
    // SAFETY: the device's CreateCommittedResource (27), with whole descriptions.
    let result = unsafe { function::<Make>(device, 27)(device, &heap, 0, described, state, null(), &RESOURCE, &mut made) };
    (result >= 0).then_some(made)
}

/// A buffer of `bytes` in the upload heap, mapped; nothing where none is made.
fn upload_buffer(device: Com, bytes: usize) -> Option<Written> {
    let described = ResourceDescription { dimension: 1, alignment: 0, width: bytes as u64, height: 1, depth: 1, levels: 1, format: 0, samples: [1, 0], layout: 1, flags: 0 };
    let (buffer, mut at) = (committed(device, 2, &described, GENERIC_READ)?, null_mut());
    // SAFETY: the buffer's Map (8), with nothing read; released where it fails.
    unsafe {
        if function::<unsafe extern "system" fn(Com, u32, *const [usize; 2], *mut *mut u8) -> i32>(buffer, 8)(buffer, 0, &[0, 0], &mut at) < 0 {
            release(buffer);
            return None;
        }
    }
    Some(Written { buffer, at, room: bytes })
}

/// What a Direct3D 12 device draws with: its root signature and pipeline state, a shader-seen
/// heap of the three views and a heap of the target's, the cache and patch textures, and the
/// buffers the texels and quads go up through.
pub(crate) struct Pipeline {
    root: Com,
    pipeline: Com,
    views: Com,
    targets: Com,
    /// The views' step in their heap.
    step: usize,
    textures: [Com; 2],
    /// The textures are ready for shaders (else, never copied into yet).
    readied: bool,
    staging: Option<Written>,
    quads: Option<Written>,
}

// SAFETY: as its device: made on the device's thread, then used by the windows' alone.
unsafe impl Send for Pipeline {}

impl std::fmt::Debug for Pipeline {
    #[inline]
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("Pipeline").field("readied", &self.readied).finish_non_exhaustive()
    }
}

/// A texture `SIDE` square that shaders read, in its first state (copied into).
fn texture(device: Com) -> Option<Com> {
    let side = SIDE as u32;
    let described = ResourceDescription { dimension: 3, alignment: 0, width: side as u64, height: side, depth: 1, levels: 1, format: B8G8R8A8_UNORM, samples: [1, 0], layout: 0, flags: 0 };
    committed(device, 1, &described, COPY_DESTINATION)
}

/// A descriptor heap of `kind` (0 views, 2 render targets) holding `count`, seen by shaders if
/// `seen`.
fn heap(device: Com, kind: u32, count: u32, seen: bool) -> Option<Com> {
    let (described, mut made) = ([kind, count, seen as u32, 0], null_mut());
    // SAFETY: the device's CreateDescriptorHeap (14) from a whole description.
    let result = unsafe { function::<unsafe extern "system" fn(Com, *const [u32; 4], *const [u32; 4], *mut Com) -> i32>(device, 14)(device, &described, &HEAP, &mut made) };
    (result >= 0).then_some(made)
}

/// Where `heap`'s descriptors start, for the CPU.
fn start_of(heap: Com) -> usize {
    let mut start = 0usize;
    // SAFETY: the heap's GetCPUDescriptorHandleForHeapStart (9), its result through a pointer.
    unsafe { function::<unsafe extern "system" fn(Com, *mut usize) -> *mut usize>(heap, 9)(heap, &mut start) };
    start
}

impl Pipeline {
    /// What `device` (an `ID3D12Device`) draws with; nothing where any part can't be made.
    pub(crate) fn new(device: Com) -> Option<Pipeline> {
        let source = format!("{PRELUDE}{}{MAIN}", crate::gpu_draw::SHADING);
        let (corner, pixel) = (compiled(&source, c"corner", c"vs_5_1")?, compiled(&source, c"pixel", c"ps_5_1")?);
        let signature = compiled(SIGNATURE, c"ROOT", c"rootsig_1_0")?;
        let mut made = Pipeline { root: null_mut(), pipeline: null_mut(), views: null_mut(), targets: null_mut(), step: 0, textures: [null_mut(); 2], readied: false, staging: None, quads: None };
        type MakeRoot = unsafe extern "system" fn(Com, u32, *const u32, usize, *const [u32; 4], *mut Com) -> i32;
        // SAFETY: the device's CreateRootSignature (16) from compiled words; its
        // GetDescriptorHandleIncrementSize (15) of views (0).
        unsafe {
            if function::<MakeRoot>(device, 16)(device, 0, signature.as_ptr(), signature.len() * 4, &ROOT, &mut made.root) < 0 {
                return None;
            }
            made.step = function::<unsafe extern "system" fn(Com, u32) -> u32>(device, 15)(device, 0) as usize;
        }
        made.pipeline = pipeline(device, made.root, &corner, &pixel)?;
        (made.views, made.targets) = (heap(device, 0, 3, true)?, heap(device, 2, 1, false)?);
        made.textures = [texture(device)?, texture(device)?];
        let start = start_of(made.views);
        for (at, texture) in made.textures.iter().enumerate() {
            // SAFETY: the device's CreateShaderResourceView (18), the whole texture, into the
            // heap's second and third places.
            unsafe { function::<unsafe extern "system" fn(Com, Com, *const c_void, usize)>(device, 18)(device, *texture, null(), start + (at + 1) * made.step) };
        }
        Some(made)
    }

    /// Readies `list` to be recorded: its texels and quads written to the upload buffers, made
    /// larger where they must be. (The device is done with them: each frame is waited for.)
    /// Says whether they hold it.
    pub(crate) fn prepare(&mut self, device: Com, list: &DrawList) -> bool {
        let staged = list.uploads.iter().map(|upload| (upload.at[2] as usize * 4).next_multiple_of(PITCH) * upload.at[3] as usize + 2 * PITCH).sum::<usize>();
        let quads = list.quads.len().max(1) * size_of::<Quad>();
        let grown = self.quads.is_none_or(|written| written.room < quads);
        let (Some(staging), Some(quads)) = (Pipeline::hold(&mut self.staging, device, staged), Pipeline::hold(&mut self.quads, device, quads)) else { return false };
        if grown {
            self.view_quads(device, quads);
        }
        // SAFETY: the buffer holds the quads (checked above), mapped, read by nothing now.
        unsafe { std::ptr::copy_nonoverlapping(list.quads.as_ptr(), quads.at.cast::<Quad>(), list.quads.len()) };
        let mut offset = 0;
        for upload in &list.uploads {
            let ([width, height], pitch) = ([upload.at[2] as usize, upload.at[3] as usize], (upload.at[2] as usize * 4).next_multiple_of(PITCH));
            for row in 0..height {
                let from = &list.staging[upload.from + row * width..upload.from + (row + 1) * width];
                // SAFETY: the buffer holds every row at its pitch (summed above).
                unsafe { std::ptr::copy_nonoverlapping(from.as_ptr().cast::<u8>(), staging.at.add(offset + row * pitch), width * 4) };
            }
            offset = (offset + pitch * height).next_multiple_of(2 * PITCH);
        }
        true
    }

    /// The upload buffer `written`, made at least `bytes` big where it is not.
    fn hold(written: &mut Option<Written>, device: Com, bytes: usize) -> Option<Written> {
        if let Some(held) = written.filter(|held| held.room >= bytes) {
            return Some(held);
        }
        if let Some(old) = written.take() {
            // SAFETY: held here alone; the device is done with it.
            unsafe { release(old.buffer) };
        }
        *written = upload_buffer(device, bytes.next_power_of_two().max(65_536));
        *written
    }

    /// The quads' view in the heap's first place: `quads` as rows of four floats.
    fn view_quads(&self, device: Com, quads: Written) {
        // Four floats an element (`DXGI_FORMAT_R32G32B32A32_FLOAT`, 2), a buffer (1), every
        // component as it is (0x1688).
        let view = BufferView { format: 2, dimension: 1, mapping: 0x1688, first: 0, count: (quads.room / 16) as u32, stride: 0, flags: 0 };
        // SAFETY: the device's CreateShaderResourceView (18) from a whole description.
        unsafe { function::<unsafe extern "system" fn(Com, Com, *const BufferView, usize)>(device, 18)(device, quads.buffer, &view, start_of(self.views)) };
    }

    /// Records into `commands` (`ID3D12GraphicsCommandList`, open) the prepared list drawn into
    /// `picture` (a window's texture, `size` pixels, made to be drawn into, copied into before
    /// and after) inside `area` (left, top, right, bottom): its texels copied up first.
    pub(crate) fn record(&self, device: Com, commands: Com, picture: Com, size: [i32; 2], list: &DrawList, area: [i32; 4]) {
        self.put_up(commands, list);
        let target = start_of(self.targets);
        let mut views = 0u64;
        let (viewport, before, after) = ([0.0, 0.0, size[0] as f32, size[1] as f32, 0.0, 1.0], barrier(picture, COPY_DESTINATION, RENDER_TARGET), barrier(picture, RENDER_TARGET, COPY_DESTINATION));
        type Barriers = unsafe extern "system" fn(Com, u32, *const Barrier);
        // SAFETY: the device's CreateRenderTargetView (20), the whole texture; the heap's
        // GetGPUDescriptorHandleForHeapStart (10). The list's methods by place in its vtable,
        // each with what it takes: 26 ResourceBarrier, 30 SetGraphicsRootSignature, 25
        // SetPipelineState, 28 SetDescriptorHeaps, 32 SetGraphicsRootDescriptorTable, 20
        // IASetPrimitiveTopology (a strip, 5), 21 RSSetViewports, 22 RSSetScissorRects, 46
        // OMSetRenderTargets, 12 DrawInstanced.
        unsafe {
            function::<unsafe extern "system" fn(Com, Com, *const c_void, usize)>(device, 20)(device, picture, null(), target);
            function::<unsafe extern "system" fn(Com, *mut u64) -> *mut u64>(self.views, 10)(self.views, &mut views);
            function::<Barriers>(commands, 26)(commands, 1, &before);
            function::<unsafe extern "system" fn(Com, Com)>(commands, 30)(commands, self.root);
            function::<unsafe extern "system" fn(Com, Com)>(commands, 25)(commands, self.pipeline);
            function::<unsafe extern "system" fn(Com, u32, *const Com)>(commands, 28)(commands, 1, &self.views);
            function::<unsafe extern "system" fn(Com, u32, u64)>(commands, 32)(commands, 0, views);
            function::<unsafe extern "system" fn(Com, u32)>(commands, 20)(commands, 5);
            function::<unsafe extern "system" fn(Com, u32, *const [f32; 6])>(commands, 21)(commands, 1, &viewport);
            function::<unsafe extern "system" fn(Com, u32, *const [i32; 4])>(commands, 22)(commands, 1, &area);
            function::<unsafe extern "system" fn(Com, u32, *const usize, i32, *const usize)>(commands, 46)(commands, 1, &target, 0, null());
            if !list.quads.is_empty() {
                function::<unsafe extern "system" fn(Com, u32, u32, u32, u32)>(commands, 12)(commands, 4, list.quads.len() as u32, 0, 0);
            }
            function::<Barriers>(commands, 26)(commands, 1, &after);
        }
    }

    /// Records the list's texels copied from the upload buffer into the textures, which are
    /// then ready for shaders.
    fn put_up(&self, commands: Com, list: &DrawList) {
        let Some(staging) = self.staging else { return };
        type Barriers = unsafe extern "system" fn(Com, u32, *const Barrier);
        type CopyRegion = unsafe extern "system" fn(Com, *const Place, u32, u32, u32, *const Place, *const Box3);
        let (copied, mut offset) = ([0, 1].map(|texture| list.uploads.iter().any(|upload| upload.texture == texture)), 0);
        // Ready textures go back to being copied into, where they are; new ones are so.
        let to_copy: Vec<Barrier> = (0..2).filter(|at| copied[*at] && self.readied).map(|at| barrier(self.textures[at], PIXEL_SHADER_RESOURCE, COPY_DESTINATION)).collect();
        let to_read: Vec<Barrier> = (0..2).filter(|at| copied[*at] || !self.readied).map(|at| barrier(self.textures[at], COPY_DESTINATION, PIXEL_SHADER_RESOURCE)).collect();
        // SAFETY: the list's ResourceBarrier (26) and CopyTextureRegion (16): each footprint
        // lies in the buffer as `prepare` wrote it, each place in its texture.
        unsafe {
            if !to_copy.is_empty() {
                function::<Barriers>(commands, 26)(commands, to_copy.len() as u32, to_copy.as_ptr());
            }
            for upload in &list.uploads {
                let ([x, y, width, height], pitch) = (upload.at.map(|side| side as u32), (upload.at[2] as usize * 4).next_multiple_of(PITCH));
                let to = Place { resource: self.textures[upload.texture as usize], kind: 0, offset: 0, footprint: [0; 5] };
                let from = Place { resource: staging.buffer, kind: 1, offset: offset as u64, footprint: [B8G8R8A8_UNORM, width, height, 1, pitch as u32] };
                function::<CopyRegion>(commands, 16)(commands, &to, x, y, 0, &from, null());
                offset = (offset + pitch * height as usize).next_multiple_of(2 * PITCH);
            }
            if !to_read.is_empty() {
                function::<Barriers>(commands, 26)(commands, to_read.len() as u32, to_read.as_ptr());
            }
        }
    }

    /// Notes that what `record` recorded was run: the textures are ready for shaders.
    pub(crate) fn ran(&mut self) {
        self.readied = true;
    }
}

impl Drop for Pipeline {
    fn drop(&mut self) {
        let written = [self.staging, self.quads].map(|written| written.map_or(null_mut(), |written| written.buffer));
        let all = [self.pipeline, self.root, self.views, self.targets, self.textures[0], self.textures[1], written[0], written[1]];
        // SAFETY: each is held once, by this pipeline; the device is done with them.
        unsafe { all.into_iter().for_each(|object| release(object)) };
    }
}

/// The pipeline state: the quads as triangle strips, nothing culled, no depth, blended over
/// what is there as Direct3D 11's: one, one minus the second colour.
fn pipeline(device: Com, root: Com, corner: &[u32], pixel: &[u32]) -> Option<Com> {
    let none = Bytecode { code: null(), size: 0 };
    let shaders = [Bytecode { code: corner.as_ptr(), size: corner.len() * 4 }, Bytecode { code: pixel.as_ptr(), size: pixel.len() * 4 }, none, none, none];
    // One (2), one minus the second colour (17), added (1); alpha likewise (2, 19, 1); logic
    // "no op" (4) off; all four written.
    let target = TargetBlend { enable: 1, logic_enable: 0, source: 2, destination: 17, operation: 1, source_alpha: 2, destination_alpha: 19, operation_alpha: 1, logic: 4, write_mask: 0xf };
    let described = PipelineDescription {
        root,
        shaders,
        stream_output: StreamOutput { declarations: null(), count: 0, strides: null(), stride_count: 0, rasterized: 0 },
        alpha_to_coverage: 0,
        independent_blend: 0,
        targets: [target; 8],
        sample_mask: u32::MAX,
        rasterizer: [3, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0],
        depth: [0, 0, 8, 0, 0xffff, 1, 1, 1, 8, 1, 1, 1, 8],
        input_layout: [0; 2],
        strip_cut: 0,
        topology: 3,
        target_count: 1,
        formats: [B8G8R8A8_UNORM, 0, 0, 0, 0, 0, 0, 0],
        depth_format: 0,
        samples: [1, 0],
        node_mask: 0,
        cached: [0; 2],
        flags: 0,
    };
    let mut made = null_mut();
    type Make = unsafe extern "system" fn(Com, *const PipelineDescription, *const [u32; 4], *mut Com) -> i32;
    // SAFETY: the device's CreateGraphicsPipelineState (10); the description and the code it
    // points to are whole.
    (unsafe { function::<Make>(device, 10)(device, &described, &PIPELINE, &mut made) } >= 0).then_some(made)
}

#[cfg(test)]
pub(crate) mod tests {
    use super::*;
    use crate::gpu_draw::tests::{Scene, compare};

    #[test]
    fn the_structures_are_laid_out_as_direct3d_lays_them_out() {
        assert_eq!(size_of::<PipelineDescription>(), 656);
        assert_eq!(std::mem::offset_of!(PipelineDescription, formats), 580);
        assert_eq!(size_of::<BufferView>(), 40);
        assert_eq!(std::mem::offset_of!(BufferView, first), 16);
        assert_eq!(size_of::<Place>(), 48);
        assert_eq!(size_of::<Barrier>(), 32);
    }

    /// Draws the scene on WARP's Direct3D 12 into a texture first filled with `under`, and
    /// reads it back.
    fn drawn(device: &crate::gpu_d3d12::Device, pipeline: &mut Pipeline, scene: &Scene, list: &DrawList) -> Vec<u32> {
        crate::gpu_d3d12::tests::drawn_on_warp(device, scene.size, &scene.under, |device, commands, picture| {
            assert!(pipeline.prepare(device, list));
            pipeline.record(device, commands, picture, scene.size, list, [0, 0, scene.size[0], scene.size[1]]);
        })
    }

    #[test]
    fn direct3d_12_draws_every_kind_of_thing_as_the_cpu_does() {
        let device = crate::gpu_d3d12::tests::warp();
        let mut pipeline = Pipeline::new(device.com()).expect("the shaders compile and the states are made");
        for scale in [1.0, 1.5, 2.0] {
            let (mut scene, mut list) = (Scene::new(scale), DrawList::default());
            scene.record(&mut list);
            assert!(!list.failed, "all of it on the GPU");
            let gpu = drawn(&device, &mut pipeline, &scene, &list);
            pipeline.ran();
            compare(&scene, &gpu, &format!("Direct3D 12 (WARP) at {scale}"));
            scene.record(&mut list);
            let again = drawn(&device, &mut pipeline, &scene, &list);
            compare(&scene, &again, &format!("Direct3D 12 again at {scale}"));
        }
        drop(pipeline);
    }
}