pub use crate::layer_d3d11::Error;
use crate::layer_d3d11::{B8G8R8A8_UNORM, Com, check, function, release};
use crate::surface::Surface;
use crate::ui::Ui;
use crate::windows::named;
use std::ffi::c_void;
use std::ptr::{null, null_mut};
const ALLOCATOR: [u32; 4] = named(0x6102_DEE4, 0xAF59, 0x4B09, [0xB9, 0x99, 0xB4, 0x4D, 0x73, 0xF0, 0x9B, 0x24]);
const LIST: [u32; 4] = named(0x5B16_0D0F, 0xAC1B, 0x4185, [0x8B, 0xA8, 0xB3, 0xAE, 0x42, 0xA5, 0xA4, 0x55]);
const FENCE: [u32; 4] = named(0x0A75_3DCF, 0xC4D8, 0x4B91, [0xAD, 0xF6, 0xBE, 0x5A, 0x60, 0xD9, 0x5A, 0x76]);
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]);
const COPY_DEST: u32 = 0x400;
const PIXEL_SHADER_RESOURCE: u32 = 0x80;
const GENERIC_READ: u32 = 0xAC3;
const PITCH: usize = 256;
#[repr(C)]
struct HeapProperties {
kind: u32,
page: u32,
pool: u32,
creation_mask: u32,
visible_mask: u32,
}
#[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,
}
#[repr(C)]
struct Barrier {
kind: u32,
flags: u32,
resource: Com,
subresource: u32,
before: u32,
after: u32,
}
#[repr(C)]
struct CopyPlace {
resource: Com,
kind: u32,
offset: u64,
footprint: [u32; 5],
}
#[repr(C)]
#[derive(Clone, Copy)]
struct Bytecode {
code: *const u32,
size: usize,
}
#[repr(C)]
struct StreamOutput {
declarations: *const c_void,
count: u32,
strides: *const u32,
stride_count: u32,
rasterized: u32,
}
#[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,
}
#[repr(C)]
struct PipelineDescription {
root: Com,
shaders: [Bytecode; 5],
stream_output: StreamOutput,
alpha_to_coverage: i32,
independent_blend: i32,
targets: [TargetBlend; 8],
sample_mask: u32,
rasterizer: [u32; 11],
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,
}
const CORNER: [u32; 188] = [
0x43425844, 0x724a9f6a, 0x9a27a619, 0xb8c19895, 0x9d266890, 0x00000001, 0x000002f0, 0x00000005, 0x00000034, 0x000000a0, 0x000000d4, 0x00000128, 0x00000254, 0x46454452, 0x00000064, 0x00000000,
0x00000000, 0x00000000, 0x0000003c, 0xfffe0500, 0x00000100, 0x0000003c, 0x31314452, 0x0000003c, 0x00000018, 0x00000020, 0x00000028, 0x00000024, 0x0000000c, 0x00000000, 0x7263694d, 0x666f736f,
0x52282074, 0x4c482029, 0x53204c53, 0x65646168, 0x6f432072, 0x6c69706d, 0x31207265, 0x00312e30, 0x4e475349, 0x0000002c, 0x00000001, 0x00000008, 0x00000020, 0x00000000, 0x00000006, 0x00000001,
0x00000000, 0x00000101, 0x565f5653, 0x65747265, 0x00444978, 0x4e47534f, 0x0000004c, 0x00000002, 0x00000008, 0x00000038, 0x00000000, 0x00000001, 0x00000003, 0x00000000, 0x0000000f, 0x00000044,
0x00000000, 0x00000000, 0x00000003, 0x00000001, 0x00000c03, 0x505f5653, 0x7469736f, 0x006e6f69, 0x43414c50, 0xabab0045, 0x58454853, 0x00000124, 0x00010050, 0x00000049, 0x0100086a, 0x04000060,
0x00101012, 0x00000000, 0x00000006, 0x04000067, 0x001020f2, 0x00000000, 0x00000001, 0x03000065, 0x00102032, 0x00000001, 0x02000068, 0x00000001, 0x07000001, 0x00100012, 0x00000000, 0x0010100a,
0x00000000, 0x00004001, 0x00000001, 0x05000056, 0x00100012, 0x00000000, 0x0010000a, 0x00000000, 0x09000032, 0x00102012, 0x00000000, 0x0010000a, 0x00000000, 0x00004001, 0x40000000, 0x00004001,
0xbf800000, 0x07000055, 0x00100042, 0x00000000, 0x0010100a, 0x00000000, 0x00004001, 0x00000001, 0x05000056, 0x00100022, 0x00000000, 0x0010002a, 0x00000000, 0x0a000032, 0x00102022, 0x00000000,
0x8010001a, 0x00000041, 0x00000000, 0x00004001, 0x40000000, 0x00004001, 0x3f800000, 0x05000036, 0x00102032, 0x00000001, 0x00100046, 0x00000000, 0x08000036, 0x001020c2, 0x00000000, 0x00004002,
0x00000000, 0x00000000, 0x00000000, 0x3f800000, 0x0100003e, 0x54415453, 0x00000094, 0x00000009, 0x00000001, 0x00000000, 0x00000003, 0x00000002, 0x00000000, 0x00000002, 0x00000001, 0x00000000,
0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000002, 0x00000000, 0x00000002, 0x00000000, 0x00000000, 0x00000000,
0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
];
const PIXEL: [u32; 184] = [
0x43425844, 0xac159bdd, 0x01dcc9ed, 0xd3a0945a, 0x85a3a132, 0x00000001, 0x000002e0, 0x00000005, 0x00000034, 0x000000c8, 0x0000011c, 0x00000150, 0x00000244, 0x46454452, 0x0000008c, 0x00000000,
0x00000000, 0x00000001, 0x0000003c, 0xffff0500, 0x00000100, 0x00000064, 0x31314452, 0x0000003c, 0x00000018, 0x00000020, 0x00000028, 0x00000024, 0x0000000c, 0x00000000, 0x0000005c, 0x00000002,
0x00000005, 0x00000004, 0xffffffff, 0x00000000, 0x00000001, 0x0000000d, 0x74636970, 0x00657275, 0x7263694d, 0x666f736f, 0x52282074, 0x4c482029, 0x53204c53, 0x65646168, 0x6f432072, 0x6c69706d,
0x31207265, 0x00312e30, 0x4e475349, 0x0000004c, 0x00000002, 0x00000008, 0x00000038, 0x00000000, 0x00000001, 0x00000003, 0x00000000, 0x0000000f, 0x00000044, 0x00000000, 0x00000000, 0x00000003,
0x00000001, 0x00000303, 0x505f5653, 0x7469736f, 0x006e6f69, 0x43414c50, 0xabab0045, 0x4e47534f, 0x0000002c, 0x00000001, 0x00000008, 0x00000020, 0x00000000, 0x00000000, 0x00000003, 0x00000000,
0x0000000f, 0x545f5653, 0x65677261, 0xabab0074, 0x58454853, 0x000000ec, 0x00000050, 0x0000003b, 0x0100086a, 0x04001858, 0x00107000, 0x00000000, 0x00005555, 0x03001062, 0x00101032, 0x00000001,
0x03000065, 0x001020f2, 0x00000000, 0x02000068, 0x00000001, 0x8900103d, 0x800000c2, 0x00155543, 0x00100032, 0x00000000, 0x00004001, 0x00000000, 0x00107e46, 0x00000000, 0x05000056, 0x00100032,
0x00000000, 0x00100046, 0x00000000, 0x07000038, 0x00100032, 0x00000000, 0x00100046, 0x00000000, 0x00101046, 0x00000001, 0x0500001b, 0x00100032, 0x00000000, 0x00100046, 0x00000000, 0x08000036,
0x001000c2, 0x00000000, 0x00004002, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x8900002d, 0x800000c2, 0x00155543, 0x001020f2, 0x00000000, 0x00100e46, 0x00000000, 0x00107e46, 0x00000000,
0x0100003e, 0x54415453, 0x00000094, 0x00000007, 0x00000001, 0x00000000, 0x00000002, 0x00000001, 0x00000000, 0x00000000, 0x00000001, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
0x00000000, 0x00000000, 0x00000001, 0x00000000, 0x00000000, 0x00000000, 0x00000001, 0x00000000, 0x00000002, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
];
const SIGNATURE: [u32; 27] = [
0x43425844, 0x0eef3b37, 0x67bb9168, 0x73dbf8e0, 0xc9aa0db9, 0x00000001, 0x0000006c, 0x00000001, 0x00000024, 0x30535452, 0x00000040, 0x00000001, 0x00000001, 0x00000018, 0x00000000, 0x00000040,
0x00000000, 0x00000000, 0x00000005, 0x00000024, 0x00000001, 0x0000002c, 0x00000000, 0x00000001, 0x00000000, 0x00000000, 0xffffffff,
];
pub struct Layer {
device: Com,
queue: Com,
allocator: Com,
list: Com,
fence: Com,
fenced: u64,
root: Com,
pipeline: Com,
heap: Com,
texture: Com,
upload: Com,
written: *mut u8,
pitch: usize,
filled: bool,
surface: Surface,
}
impl std::fmt::Debug for Layer {
#[inline]
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("Layer").field("width", &self.surface.width).field("height", &self.surface.height).finish_non_exhaustive()
}
}
impl Layer {
#[inline]
pub unsafe fn new(device: *mut c_void, queue: *mut c_void, format: u32) -> Result<Layer, Error> {
unsafe { [device, queue].into_iter().for_each(|object| _ = function::<unsafe extern "system" fn(Com) -> u32>(object, 1)(object)) };
let mut layer = Layer {
device,
queue,
allocator: null_mut(),
list: null_mut(),
fence: null_mut(),
fenced: 0,
root: null_mut(),
pipeline: null_mut(),
heap: null_mut(),
texture: null_mut(),
upload: null_mut(),
written: null_mut(),
pitch: 0,
filled: false,
surface: Surface::default(),
};
type Make = unsafe extern "system" fn(Com, u32, *const [u32; 4], *mut Com) -> i32;
type MakeList = unsafe extern "system" fn(Com, u32, u32, Com, Com, *const [u32; 4], *mut Com) -> i32;
type MakeFence = unsafe extern "system" fn(Com, u64, u32, *const [u32; 4], *mut Com) -> i32;
type MakeRoot = unsafe extern "system" fn(Com, u32, *const u32, usize, *const [u32; 4], *mut Com) -> i32;
let heap = [0u32, 1, 1, 0];
unsafe {
check(function::<Make>(device, 9)(device, 0, &ALLOCATOR, &mut layer.allocator), "making the command allocator")?;
check(function::<MakeList>(device, 12)(device, 0, 0, layer.allocator, null_mut(), &LIST, &mut layer.list), "making the command list")?;
check(function::<unsafe extern "system" fn(Com) -> i32>(layer.list, 9)(layer.list), "making the command list")?;
check(function::<MakeFence>(device, 36)(device, 0, 0, &FENCE, &mut layer.fence), "making the fence")?;
check(function::<MakeRoot>(device, 16)(device, 0, SIGNATURE.as_ptr(), SIGNATURE.len() * 4, &ROOT, &mut layer.root), "making the root signature")?;
check(function::<unsafe extern "system" fn(Com, *const [u32; 4], *const [u32; 4], *mut Com) -> i32>(device, 14)(device, &heap, &HEAP, &mut layer.heap), "making the descriptor heap")?;
}
layer.pipeline = pipeline(device, layer.root, format)?;
Ok(layer)
}
#[inline]
pub fn update(&mut self, ui: &mut Ui) -> Result<(), Error> {
check(unsafe { function::<unsafe extern "system" fn(Com) -> i32>(self.device, 37)(self.device) }, "drawing")?;
let size = (self.surface.width, self.surface.height);
let mut changed = self.surface.update(ui);
if (self.surface.width, self.surface.height) != size || self.texture.is_null() {
self.make_texture().inspect_err(|_| self.surface = Surface::default())?;
changed = [[0, 0, self.surface.width, self.surface.height]].into_iter().filter(|_| !self.texture.is_null()).collect();
}
if changed.is_empty() {
return Ok(());
}
self.settle();
let (width, pitch) = (self.surface.width, self.pitch);
for &[left, top, right, bottom] in &changed {
for row in top..bottom {
let from = &self.surface.pixels[row * width + left..row * width + right];
unsafe { std::ptr::copy_nonoverlapping(from.as_ptr().cast::<u8>(), self.written.add(row * pitch + left * 4), from.len() * 4) };
}
}
self.copy(&changed)
}
#[inline]
fn copy(&mut self, places: &[[usize; 4]]) -> Result<(), Error> {
let (list, footprint) = (self.list, [B8G8R8A8_UNORM, self.surface.width as u32, self.surface.height as u32, 1, self.pitch as u32]);
let to = CopyPlace { resource: self.texture, kind: 0, offset: 0, footprint: [0; 5] };
let from = CopyPlace { resource: self.upload, kind: 1, offset: 0, footprint };
let barrier = |before, after| Barrier { kind: 0, flags: 0, resource: self.texture, subresource: u32::MAX, before, after };
type CopyRegion = unsafe extern "system" fn(Com, *const CopyPlace, u32, u32, u32, *const CopyPlace, *const [u32; 6]);
type Barriers = unsafe extern "system" fn(Com, u32, *const Barrier);
unsafe {
check(function::<unsafe extern "system" fn(Com) -> i32>(self.allocator, 8)(self.allocator), "recording the copy")?;
check(function::<unsafe extern "system" fn(Com, Com, Com) -> i32>(list, 10)(list, self.allocator, null_mut()), "recording the copy")?;
if self.filled {
function::<Barriers>(list, 26)(list, 1, &barrier(PIXEL_SHADER_RESOURCE, COPY_DEST));
}
for &[left, top, right, bottom] in places {
let area = [left as u32, top as u32, 0, right as u32, bottom as u32, 1];
function::<CopyRegion>(list, 16)(list, &to, left as u32, top as u32, 0, &from, &area);
}
function::<Barriers>(list, 26)(list, 1, &barrier(COPY_DEST, PIXEL_SHADER_RESOURCE));
check(function::<unsafe extern "system" fn(Com) -> i32>(list, 9)(list), "recording the copy")?;
function::<unsafe extern "system" fn(Com, u32, *const Com)>(self.queue, 10)(self.queue, 1, &list);
}
self.filled = true;
self.signal("running the copy")
}
#[inline]
fn signal(&mut self, doing: &'static str) -> Result<(), Error> {
self.fenced += 1;
check(unsafe { function::<unsafe extern "system" fn(Com, Com, u64) -> i32>(self.queue, 14)(self.queue, self.fence, self.fenced) }, doing)
}
#[inline]
fn settle(&self) {
unsafe {
if function::<unsafe extern "system" fn(Com) -> u64>(self.fence, 8)(self.fence) < self.fenced {
function::<unsafe extern "system" fn(Com, u64, isize) -> i32>(self.fence, 9)(self.fence, self.fenced, 0);
}
}
}
#[inline]
fn make_texture(&mut self) -> Result<(), Error> {
if !self.texture.is_null() {
self.signal("waiting for the frames before")?;
}
self.settle();
unsafe { [std::mem::replace(&mut self.texture, null_mut()), std::mem::replace(&mut self.upload, null_mut())].into_iter().for_each(|object| release(object)) };
(self.written, self.filled) = (null_mut(), false);
if self.surface.pixels.is_empty() {
return Ok(());
}
let (width, height) = (self.surface.width, self.surface.height);
self.pitch = (width * 4).div_ceil(PITCH) * PITCH;
let texture = ResourceDescription { dimension: 3, alignment: 0, width: width as u64, height: height as u32, depth: 1, levels: 1, format: B8G8R8A8_UNORM, samples: [1, 0], layout: 0, flags: 0 };
let buffer = ResourceDescription { dimension: 1, alignment: 0, width: (self.pitch * height) as u64, height: 1, depth: 1, levels: 1, format: 0, samples: [1, 0], layout: 1, flags: 0 };
let heap = |kind| HeapProperties { kind, page: 0, pool: 0, creation_mask: 0, visible_mask: 0 };
type Make = unsafe extern "system" fn(Com, *const HeapProperties, u32, *const ResourceDescription, u32, *const c_void, *const [u32; 4], *mut Com) -> i32;
let (device, mut there, mut start) = (self.device, null_mut(), 0usize);
unsafe {
check(function::<Make>(device, 27)(device, &heap(1), 0, &texture, COPY_DEST, null(), &RESOURCE, &mut self.texture), "making the texture")?;
check(function::<Make>(device, 27)(device, &heap(2), 0, &buffer, GENERIC_READ, null(), &RESOURCE, &mut self.upload), "making the upload buffer")?;
check(function::<unsafe extern "system" fn(Com, u32, *const [usize; 2], *mut *mut c_void) -> i32>(self.upload, 8)(self.upload, 0, &[0, 0], &mut there), "mapping the upload buffer")?;
function::<unsafe extern "system" fn(Com, *mut usize) -> *mut usize>(self.heap, 9)(self.heap, &mut start);
function::<unsafe extern "system" fn(Com, Com, *const c_void, usize)>(device, 18)(device, self.texture, null(), start);
}
self.written = there.cast();
Ok(())
}
#[inline]
pub unsafe fn draw(&mut self, ui: &mut Ui, list: *mut c_void, area: [i32; 4]) -> Result<(), Error> {
self.update(ui)?;
if !self.filled {
return Ok(());
}
let [x, y, width, height] = area;
let viewport = [x as f32, y as f32, self.surface.width as f32, self.surface.height as f32, 0.0, 1.0];
let scissor = [x.max(0), y.max(0), x + width, y + height];
let mut start = 0u64;
unsafe {
function::<unsafe extern "system" fn(Com, *mut u64) -> *mut u64>(self.heap, 10)(self.heap, &mut start);
function::<unsafe extern "system" fn(Com, Com)>(list, 30)(list, self.root);
function::<unsafe extern "system" fn(Com, Com)>(list, 25)(list, self.pipeline);
function::<unsafe extern "system" fn(Com, u32, *const Com)>(list, 28)(list, 1, &self.heap);
function::<unsafe extern "system" fn(Com, u32, u64)>(list, 32)(list, 0, start);
function::<unsafe extern "system" fn(Com, u32)>(list, 20)(list, 5);
function::<unsafe extern "system" fn(Com, u32, *const [f32; 6])>(list, 21)(list, 1, &viewport);
function::<unsafe extern "system" fn(Com, u32, *const [i32; 4])>(list, 22)(list, 1, &scissor);
function::<unsafe extern "system" fn(Com, u32, u32, u32, u32)>(list, 12)(list, 4, 1, 0, 0);
}
Ok(())
}
#[inline]
pub fn texture(&self) -> *mut c_void {
self.texture
}
#[inline]
pub fn surface(&self) -> &Surface {
&self.surface
}
}
impl Drop for Layer {
#[inline]
fn drop(&mut self) {
if !self.fence.is_null() && self.signal("letting go").is_ok() {
self.settle();
}
let held = [self.upload, self.texture, self.heap, self.pipeline, self.root, self.fence, self.list, self.allocator, self.queue, self.device];
unsafe { held.into_iter().for_each(|object| release(object)) };
}
}
#[inline]
fn pipeline(device: Com, root: Com, format: u32) -> Result<Com, Error> {
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];
let target = TargetBlend { enable: 1, logic_enable: 0, source: 2, destination: 6, operation: 1, source_alpha: 2, destination_alpha: 6, 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: [format, 0, 0, 0, 0, 0, 0, 0],
depth_format: 0,
samples: [1, 0],
node_mask: 0,
cached: [0; 2],
flags: 0,
};
let mut pipeline = null_mut();
type Make = unsafe extern "system" fn(Com, *const PipelineDescription, *const [u32; 4], *mut Com) -> i32;
check(unsafe { function::<Make>(device, 10)(device, &described, &PIPELINE, &mut pipeline) }, "making the pipeline state")?;
Ok(pipeline)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn the_structures_are_laid_out_as_direct3d_lays_them_out() {
assert_eq!(size_of::<HeapProperties>(), 20);
assert_eq!(size_of::<ResourceDescription>(), 56);
assert_eq!(size_of::<Barrier>(), 32);
assert_eq!(size_of::<CopyPlace>(), 48);
assert_eq!(size_of::<TargetBlend>(), 40);
assert_eq!(size_of::<PipelineDescription>(), 656);
assert_eq!(std::mem::offset_of!(PipelineDescription, sample_mask), 448);
assert_eq!(std::mem::offset_of!(PipelineDescription, input_layout), 552);
assert_eq!(std::mem::offset_of!(PipelineDescription, formats), 580);
assert_eq!(std::mem::offset_of!(PipelineDescription, cached), 632);
}
#[test]
fn the_shaders_are_their_source_compiled() {
type Compile = unsafe extern "system" fn(*const u8, usize, *const u8, *const c_void, *const c_void, *const u8, *const u8, u32, u32, *mut Com, *mut Com) -> i32;
#[link(name = "kernel32")]
unsafe extern "system" {
fn LoadLibraryW(name: *const u16) -> isize;
fn GetProcAddress(library: isize, name: *const u8) -> usize;
}
let file = include_str!("layer_d3d12.rs").replace("\r\n", "\n");
let start = file.find("/// ```hlsl\n").unwrap() + "/// ```hlsl\n".len();
let end = start + file[start..].find("/// ```\n").unwrap();
let source: String = file[start..end].lines().map(|line| format!("{}\n", line.strip_prefix("/// ").unwrap_or(""))).collect();
let library: Vec<u16> = "d3dcompiler_47.dll\0".encode_utf16().collect();
let compiled = |entry: &str, target: &str| unsafe {
let compile: Compile = std::mem::transmute(GetProcAddress(LoadLibraryW(library.as_ptr()), c"D3DCompile".as_ptr().cast()));
let (entry, target, mut code, mut errors) = (format!("{entry}\0"), format!("{target}\0"), null_mut(), null_mut());
assert!(compile(source.as_ptr(), source.len(), null(), null(), null(), entry.as_ptr(), target.as_ptr(), 0, 0, &mut code, &mut errors) >= 0);
let start = function::<unsafe extern "system" fn(Com) -> *const u8>(code, 3)(code);
let bytes = std::slice::from_raw_parts(start, function::<unsafe extern "system" fn(Com) -> usize>(code, 4)(code));
let words: Vec<u32> = bytes.chunks(4).map(|word| u32::from_le_bytes(word.try_into().unwrap())).collect();
[code, errors].into_iter().for_each(|blob| release(blob));
words
};
assert_eq!(compiled("corner", "vs_5_0"), CORNER, "the vertex shader");
assert_eq!(compiled("pixel", "ps_5_0"), PIXEL, "the pixel shader");
assert_eq!(compiled("TAUT", "rootsig_1_0"), SIGNATURE, "the root signature");
}
}