//! Host pipelines for title pipelines (decision 0007, step 3): the //! title's graphics state with the translated shaders, under a root //! signature of our own that the translated shaders' bindings define //! (`victory_shader::emit`): per stage one block of root constants for the //! user data, root views for the title's root views, static samplers for //! the immediate samplers, and directly indexed heaps. use std::ffi::{CStr, CString}; use std::sync::Arc; use victory_shader::emit::{MEMORY_SPACE, ROOT_CBV_SPACE, SAMPLER_SPACE, USER_DATA_SPACE}; use windows::Win32::Graphics::Direct3D::ID3DBlob; use windows::Win32::Graphics::Direct3D12::*; use windows::Win32::Graphics::Dxgi::Common::*; use windows::core::PCSTR; use super::{Shader, Translated}; use crate::host::host; /// A stream's graphics state, owned (the title's stream lives only for the /// creation call). Subobjects the stream leaves out keep D3D12's defaults. pub(crate) struct GraphicsState { blend: D3D12_BLEND_DESC, sample_mask: u32, rasterizer: D3D12_RASTERIZER_DESC, depth: D3D12_DEPTH_STENCIL_DESC, /// Input elements, their names pointing into `names`. inputs: Vec, names: Vec, strip_cut: D3D12_INDEX_BUFFER_STRIP_CUT_VALUE, pub(crate) topology: D3D12_PRIMITIVE_TOPOLOGY_TYPE, pub(crate) targets: D3D12_RT_FORMAT_ARRAY, depth_format: DXGI_FORMAT, samples: DXGI_SAMPLE_DESC, } impl Default for GraphicsState { fn default() -> Self { let target = D3D12_RENDER_TARGET_BLEND_DESC { SrcBlend: D3D12_BLEND_ONE, DestBlend: D3D12_BLEND_ZERO, BlendOp: D3D12_BLEND_OP_ADD, SrcBlendAlpha: D3D12_BLEND_ONE, DestBlendAlpha: D3D12_BLEND_ZERO, BlendOpAlpha: D3D12_BLEND_OP_ADD, LogicOp: D3D12_LOGIC_OP_NOOP, RenderTargetWriteMask: D3D12_COLOR_WRITE_ENABLE_ALL.0 as u8, ..Default::default() }; let keep = D3D12_DEPTH_STENCILOP_DESC { StencilFailOp: D3D12_STENCIL_OP_KEEP, StencilDepthFailOp: D3D12_STENCIL_OP_KEEP, StencilPassOp: D3D12_STENCIL_OP_KEEP, StencilFunc: D3D12_COMPARISON_FUNC_ALWAYS, }; GraphicsState { blend: D3D12_BLEND_DESC { RenderTarget: [target; 8], ..Default::default() }, sample_mask: u32::MAX, rasterizer: D3D12_RASTERIZER_DESC { FillMode: D3D12_FILL_MODE_SOLID, CullMode: D3D12_CULL_MODE_BACK, DepthClipEnable: true.into(), ..Default::default() }, depth: D3D12_DEPTH_STENCIL_DESC { DepthEnable: true.into(), DepthWriteMask: D3D12_DEPTH_WRITE_MASK_ALL, DepthFunc: D3D12_COMPARISON_FUNC_LESS, StencilReadMask: 0xff, StencilWriteMask: 0xff, FrontFace: keep, BackFace: keep, ..Default::default() }, inputs: Vec::new(), names: Vec::new(), strip_cut: D3D12_INDEX_BUFFER_STRIP_CUT_VALUE_DISABLED, topology: D3D12_PRIMITIVE_TOPOLOGY_TYPE_UNDEFINED, targets: D3D12_RT_FORMAT_ARRAY::default(), depth_format: DXGI_FORMAT_UNKNOWN, samples: DXGI_SAMPLE_DESC { Count: 1, Quality: 0, }, } } } impl GraphicsState { /// Takes in one subobject of a stream: its type, then its data at +4 /// (+8 for those holding pointers). /// /// # Safety /// `at` points at a whole subobject of type `kind`. pub(crate) unsafe fn read(&mut self, kind: u32, at: *const u8) { // SAFETY: the caller passes a whole subobject. unsafe { let data = at.add(4); match kind { 8 => self.blend = data.cast::().read_unaligned(), 9 => self.sample_mask = data.cast::().read_unaligned(), 10 => self.rasterizer = data.cast::().read_unaligned(), 11 => self.depth = data.cast::().read_unaligned(), 21 => { // shortcut: the depth bounds test is dropped (the host // pipeline uses the plain description); bring it back // with a stream if a title enables it. let d = data.cast::().read_unaligned(); self.depth = D3D12_DEPTH_STENCIL_DESC { DepthEnable: d.DepthEnable, DepthWriteMask: d.DepthWriteMask, DepthFunc: d.DepthFunc, StencilEnable: d.StencilEnable, StencilReadMask: d.StencilReadMask, StencilWriteMask: d.StencilWriteMask, FrontFace: d.FrontFace, BackFace: d.BackFace, }; } 12 => { let layout = at.add(8).cast::().read_unaligned(); if layout.pInputElementDescs.is_null() { return; } let elements = std::slice::from_raw_parts( layout.pInputElementDescs, layout.NumElements as usize, ); self.names = elements .iter() .map(|e| CStr::from_ptr(e.SemanticName.0.cast()).to_owned()) .collect(); self.inputs = elements .iter() .zip(&self.names) .map(|(e, name)| D3D12_INPUT_ELEMENT_DESC { SemanticName: PCSTR(name.as_ptr().cast()), Format: DXGI_FORMAT( crate::resource::pc_format(e.Format.0 as u32) as i32 ), ..*e }) .collect(); } 13 => { self.strip_cut = D3D12_INDEX_BUFFER_STRIP_CUT_VALUE(data.cast::().read_unaligned()) } 14 => { self.topology = D3D12_PRIMITIVE_TOPOLOGY_TYPE(data.cast::().read_unaligned()) } 15 => { self.targets = data.cast::().read_unaligned(); for f in &mut self.targets.RTFormats { f.0 = crate::resource::pc_format(f.0 as u32) as i32; } } 16 => { let f = data.cast::().read_unaligned(); self.depth_format = DXGI_FORMAT(crate::resource::pc_format(f) as i32); } 17 => self.samples = data.cast::().read_unaligned(), _ => {} } } } /// Everything the state holds, as text: part of a pipeline's name in /// the library and of the key a title pipeline is started early by. pub(crate) fn describe(&self) -> String { let inputs: Vec = self .inputs .iter() .zip(&self.names) .map(|(e, name)| { format!( "{name:?} {} {} {} {} {} {}", e.SemanticIndex, e.Format.0, e.InputSlot, e.AlignedByteOffset, e.InputSlotClass.0, e.InstanceDataStepRate ) }) .collect(); format!( "{:?} {} {:?} {:?} {inputs:?} {} {} {:?} {} {:?}", self.blend, self.sample_mask, self.rasterizer, self.depth, self.strip_cut.0, self.topology.0, self.targets, self.depth_format.0, self.samples ) } /// The vertex buffer slots the input layout reads. fn slots(&self) -> u32 { self.inputs .iter() .fold(0, |m, e| m | 1 << e.InputSlot.min(31)) } /// The input elements with their byte offsets worked out. fn elements(&self) -> Vec { let mut next = [0u32; 32]; self.inputs .iter() .map(|e| { let slot = e.InputSlot.min(31) as usize; let format = e.Format.0 as u32; let bytes = crate::resource::element(format).map_or(0, |(b, _)| b as u32); // An appended element starts at the next multiple of // min(4, its size) after the previous one, where D3D12 (and // vkd3d) place it; the host pipeline gets the title's // descriptions, so the offsets read here must match. let offset = if e.AlignedByteOffset == D3D12_APPEND_ALIGNED_ELEMENT { next[slot] .checked_next_multiple_of(bytes.clamp(1, 4)) .unwrap_or(u32::MAX) } else { e.AlignedByteOffset }; next[slot] = offset.saturating_add(bytes); Input { slot: slot as u32, offset, format, per_instance: e.InputSlotClass == D3D12_INPUT_CLASSIFICATION_PER_INSTANCE_DATA, } }) .collect() } } /// One input element: vertex buffer slot, byte offset, format. #[derive(Debug, Clone, Copy)] pub(crate) struct Input { pub(crate) slot: u32, pub(crate) offset: u32, pub(crate) format: u32, pub(crate) per_instance: bool, } /// The console's own primitive topology types. const RECTANGLES: i32 = 19; const QUADS: i32 = 32; /// One stage of a host pipeline and where its bindings go. pub(crate) struct Stage { pub(crate) shader: Arc, /// The root parameter of its user data, and its size in dwords. pub(crate) user_data: Option<(u32, u32)>, /// Root parameters of its root views: (title root parameter, ours). pub(crate) root_cbvs: Vec<(u8, u32)>, pub(crate) root_srvs: Vec<(u8, u32)>, pub(crate) root_uavs: Vec<(u8, u32)>, /// Our root parameters of the title-memory page table and of the /// written-pages bitmap (`crate::memory`), if it reads or writes there. pub(crate) memory: Option, pub(crate) memory_dirty: Option, /// Our root parameter of the heap's view addresses (`crate::memory`). pub(crate) view_addresses: Option, } impl Stage { pub(crate) fn translated(&self) -> &Translated { self.shader .translated() .expect("host pipelines are made from translated shaders") } } pub(crate) struct HostPipeline { /// A compute pipeline (else graphics). pub(crate) compute: bool, /// A mesh pipeline (graphics without vertex input). pub(crate) mesh: bool, pub(crate) pso: ID3D12PipelineState, pub(crate) root: ID3D12RootSignature, /// Vertex, then pixel stage, or the compute stage. pub(crate) stages: Vec, /// A graphics pipeline of the console's rectangle lists whose geometry /// shader completes each rectangle (`victory_shader::emit::rectangle_gs`): /// it draws them as triangles, one per rectangle. pub(crate) rectangles: bool, /// The vertex buffer slots it reads (bit per slot). pub(crate) slots: u32, pub(crate) inputs: Vec, /// Its shaders, for reports of slow submissions. pub(crate) name: String, /// Our root parameters of the view-mark table and the buffer views' /// written-pages bitmap, if a stage marks its writes through views /// (`victory_shader::emit`, "Write marks"). pub(crate) marks: Option<(u32, u32)>, } /// Logs a pipeline that gets no host pipeline, once per reason. fn refuse(reason: &'static str, shaders: &[Arc]) -> Option { static SEEN: std::sync::Mutex> = std::sync::Mutex::new(Vec::new()); let mut seen = SEEN.lock().unwrap_or_else(|e| e.into_inner()); if !seen.contains(&reason) { seen.push(reason); tracing::warn!(?shaders, "no host pipeline: {reason} (logged once)"); } None } impl HostPipeline { /// A host pipeline for vertex and pixel shaders with the graphics /// state `g`, or for one compute shader. pub(crate) fn new(shaders: &[Arc], g: Option<&GraphicsState>) -> Option { let find = |stage| shaders.iter().find(|s| s.stage == stage); let compute = match shaders { [cs] if cs.stage == "compute" => Some(cs), _ => None, }; if compute.is_none() && shaders .iter() .any(|s| !matches!(s.stage, "vertex" | "pixel" | "mesh")) { return refuse( "only vertex, mesh, pixel and compute stages are translated", shaders, ); } let mesh = find("mesh"); let stages = if let Some(cs) = compute { vec![(cs, D3D12_SHADER_VISIBILITY_ALL)] } else { let first = match (mesh, find("vertex")) { (Some(ms), _) => (ms, D3D12_SHADER_VISIBILITY_MESH), (None, Some(vs)) => (vs, D3D12_SHADER_VISIBILITY_VERTEX), (None, None) => return refuse("no vertex or mesh shader", shaders), }; let mut stages = vec![first]; stages.extend(find("pixel").map(|ps| (ps, D3D12_SHADER_VISIBILITY_PIXEL))); stages }; if mesh.is_some() && !host()?.mesh_shaders() { return refuse("the host GPU has no mesh shaders (or no shader model 6.6)", shaders); } for (s, _) in &stages { s.translated()?; } let host = host()?; // `SFI0`: 64-bit atomics on typed resources and on heap resources. let atomics64 = stages.iter().any(|(s, _)| { let Some(t) = s.translated() else { return false; }; victory_shader::Container::parse(&t.host.dxil) .ok() .and_then(|c| { c.part(victory_shader::FourCC::SFI0)? .data .get(..4)? .try_into() .ok() }) .is_some_and(|b: [u8; 4]| u32::from_le_bytes(b) & 0x1040_0000 != 0) }); if atomics64 && !host.heap_atomics64() { return refuse( "the host GPU has no 64-bit atomics on typed heap resources", shaders, ); } let mut options = D3D12_FEATURE_DATA_D3D12_OPTIONS19::default(); // SAFETY: the feature query writes the structure of the given size. // Older runtimes may not recognize OPTIONS19; that means no support. let aniso_point_mip_supported = unsafe { host.device.CheckFeatureSupport( D3D12_FEATURE_D3D12_OPTIONS19, (&raw mut options).cast(), size_of_val(&options) as u32, ) } .is_ok() && options.AnisoFilterWithPointMipSupported.as_bool(); // The root signature the translated shaders' bindings define. let mut params: Vec = Vec::new(); let mut samplers = Vec::new(); let mut bound = Vec::new(); for &(shader, visibility) in &stages { let t = &shader.translated()?.host; let mut stage = Stage { shader: shader.clone(), user_data: None, root_cbvs: Vec::new(), root_srvs: Vec::new(), root_uavs: Vec::new(), memory: None, memory_dirty: None, view_addresses: None, }; let dwords = t.user_data_dwords(); if dwords > 0 { stage.user_data = Some((params.len() as u32, dwords)); params.push(D3D12_ROOT_PARAMETER1 { ParameterType: D3D12_ROOT_PARAMETER_TYPE_32BIT_CONSTANTS, Anonymous: D3D12_ROOT_PARAMETER1_0 { Constants: D3D12_ROOT_CONSTANTS { ShaderRegister: 0, RegisterSpace: USER_DATA_SPACE, Num32BitValues: dwords, }, }, ShaderVisibility: visibility, }); } for (slots, kind, out) in [ ( &t.root_cbvs, D3D12_ROOT_PARAMETER_TYPE_CBV, &mut stage.root_cbvs, ), ( &t.root_srvs, D3D12_ROOT_PARAMETER_TYPE_SRV, &mut stage.root_srvs, ), ( &t.root_uavs, D3D12_ROOT_PARAMETER_TYPE_UAV, &mut stage.root_uavs, ), ] { for &slot in slots { out.push((slot, params.len() as u32)); params.push(D3D12_ROOT_PARAMETER1 { ParameterType: kind, Anonymous: D3D12_ROOT_PARAMETER1_0 { Descriptor: D3D12_ROOT_DESCRIPTOR1 { ShaderRegister: u32::from(slot), RegisterSpace: ROOT_CBV_SPACE, Flags: D3D12_ROOT_DESCRIPTOR_FLAG_DATA_VOLATILE, }, }, ShaderVisibility: visibility, }); } } for (used, kind, register, out) in [ (t.memory, D3D12_ROOT_PARAMETER_TYPE_SRV, 0, &mut stage.memory), (t.memory_writes, D3D12_ROOT_PARAMETER_TYPE_UAV, 0, &mut stage.memory_dirty), (t.view_addresses, D3D12_ROOT_PARAMETER_TYPE_SRV, 1, &mut stage.view_addresses), ] { if used { *out = Some(params.len() as u32); params.push(D3D12_ROOT_PARAMETER1 { ParameterType: kind, Anonymous: D3D12_ROOT_PARAMETER1_0 { Descriptor: D3D12_ROOT_DESCRIPTOR1 { ShaderRegister: register, RegisterSpace: MEMORY_SPACE, Flags: D3D12_ROOT_DESCRIPTOR_FLAG_DATA_VOLATILE, }, }, ShaderVisibility: visibility, }); } } for (n, s) in t.static_samplers.iter().enumerate() { samplers.push(D3D12_STATIC_SAMPLER_DESC { Filter: D3D12_FILTER(s.filter_for_host(aniso_point_mip_supported) as i32), AddressU: D3D12_TEXTURE_ADDRESS_MODE(s.address[0] as i32), AddressV: D3D12_TEXTURE_ADDRESS_MODE(s.address[1] as i32), AddressW: D3D12_TEXTURE_ADDRESS_MODE(s.address[2] as i32), MipLODBias: s.mip_lod_bias, MaxAnisotropy: s.max_anisotropy, ComparisonFunc: D3D12_COMPARISON_FUNC(s.comparison as i32), BorderColor: D3D12_STATIC_BORDER_COLOR(s.border as i32), MinLOD: s.min_lod, MaxLOD: s.max_lod, ShaderRegister: n as u32, RegisterSpace: SAMPLER_SPACE, ShaderVisibility: visibility, }); } bound.push(stage); } // One pair for all stages: they share the heap and the bitmap. let marks = stages .iter() .any(|(s, _)| s.translated().is_some_and(|t| t.host.view_marks)) .then(|| { let first = params.len() as u32; for (kind, register) in [ (D3D12_ROOT_PARAMETER_TYPE_SRV, victory_shader::emit::VIEW_MARKS), (D3D12_ROOT_PARAMETER_TYPE_UAV, victory_shader::emit::VIEW_WRITES), ] { params.push(D3D12_ROOT_PARAMETER1 { ParameterType: kind, Anonymous: D3D12_ROOT_PARAMETER1_0 { Descriptor: D3D12_ROOT_DESCRIPTOR1 { ShaderRegister: register, RegisterSpace: MEMORY_SPACE, Flags: D3D12_ROOT_DESCRIPTOR_FLAG_DATA_VOLATILE, }, }, ShaderVisibility: D3D12_SHADER_VISIBILITY_ALL, }); } (first, first + 1) }); let (root, root_blob) = match root_signature(&host.device, ¶ms, &samplers, compute.is_some()) { Ok(root) => root, Err(e) => { tracing::warn!(error = %e, ?shaders, "host root signature refused"); return None; } }; let code = |i: usize| { stages.get(i).and_then(|(s, _)| s.translated()).map_or( D3D12_SHADER_BYTECODE::default(), |t| D3D12_SHADER_BYTECODE { pShaderBytecode: t.host.dxil.as_ptr().cast(), BytecodeLength: t.host.dxil.len(), }, ) }; if compute.is_some() { let desc = D3D12_COMPUTE_PIPELINE_STATE_DESC { // SAFETY: a borrowed pointer the description never releases. pRootSignature: unsafe { std::mem::transmute_copy(&root) }, CS: code(0), ..Default::default() }; let name = super::library::name("compute", &root_blob, &[&code_bytes(&code(0))], ""); let made = super::library::pipeline( &name, // SAFETY: the description points at live data. |library, name| unsafe { library.LoadComputePipeline(name, &desc) }, // SAFETY: as above. || unsafe { host.device.CreateComputePipelineState(&desc) }, ); let pso = match made { Ok(pso) => pso, Err(e) => { tracing::warn!(error = %e, ?shaders, "host pipeline refused"); host.log_messages(); return None; } }; tracing::info!(?shaders, "host pipeline"); crate::host::name(&pso, || format!("{shaders:?}")); return Some(HostPipeline { compute: true, mesh: false, pso, root, stages: bound, name: format!("{shaders:?}"), rectangles: false, slots: 0, inputs: Vec::new(), marks, }); } let Some(g) = g else { return refuse("graphics state not kept (plain description)", shaders); }; let state = g.describe(); if mesh.is_some() { let name = super::library::name( "mesh", &root_blob, &[&code_bytes(&code(0)), &code_bytes(&code(1))], &state, ); let pso = match mesh_pipeline(&host.device, &root, code(0), code(1), g, &name) { Ok(pso) => pso, Err(e) => { tracing::warn!(error = %e, ?shaders, "host mesh pipeline refused"); host.log_messages(); return None; } }; tracing::info!(?shaders, "host mesh pipeline"); crate::host::name(&pso, || format!("{shaders:?}")); return Some(HostPipeline { compute: false, mesh: true, pso, root, stages: bound, name: format!("{shaders:?}"), rectangles: false, slots: 0, inputs: Vec::new(), marks, }); } // Rectangle lists: a geometry shader made for the vertex shader adds // each rectangle's fourth corner (else rectangles are expanded on // the CPU from vertex inputs, see `draw`). let gs = match (g.topology.0, stages.first().and_then(|(s, _)| s.translated())) { (RECTANGLES, Some(vs)) => match victory_shader::emit::rectangle_gs(&vs.host.dxil) { Ok(gs) => gs, Err(e) => { tracing::warn!(error = %e, ?shaders, "no rectangle geometry shader"); Vec::new() } }, _ => Vec::new(), }; let desc = D3D12_GRAPHICS_PIPELINE_STATE_DESC { // SAFETY: a borrowed pointer the description never releases. pRootSignature: unsafe { std::mem::transmute_copy(&root) }, VS: code(0), PS: code(1), GS: D3D12_SHADER_BYTECODE { pShaderBytecode: if gs.is_empty() { std::ptr::null() } else { gs.as_ptr().cast() }, BytecodeLength: gs.len(), }, BlendState: g.blend, SampleMask: g.sample_mask, RasterizerState: g.rasterizer, DepthStencilState: g.depth, InputLayout: D3D12_INPUT_LAYOUT_DESC { pInputElementDescs: g.inputs.as_ptr(), NumElements: g.inputs.len() as u32, }, IBStripCutValue: g.strip_cut, PrimitiveTopologyType: host_topology(g), NumRenderTargets: g.targets.NumRenderTargets, RTVFormats: g.targets.RTFormats, DSVFormat: g.depth_format, SampleDesc: g.samples, ..Default::default() }; let name = super::library::name( "graphics", &root_blob, &[&code_bytes(&code(0)), &code_bytes(&code(1)), &gs], &state, ); let started = std::time::Instant::now(); let made = super::library::pipeline( &name, // SAFETY: the description points at live data. |library, name| unsafe { library.LoadGraphicsPipeline(name, &desc) }, // SAFETY: as above. || unsafe { host.device.CreateGraphicsPipelineState(&desc) }, ); let pso = match made { Ok(pso) => pso, Err(e) => { tracing::warn!(error = %e, ?shaders, "host pipeline refused"); host.log_messages(); return None; } }; tracing::info!(?shaders, ms = started.elapsed().as_millis(), rectangles = !gs.is_empty(), "host pipeline"); crate::host::name(&pso, || format!("{shaders:?}")); tracing::debug!( ?shaders, depth = ?g.depth, dsv = g.depth_format.0, targets = ?&g.targets.RTFormats[..g.targets.NumRenderTargets.min(8) as usize], blend = ?g.blend.RenderTarget[0], cull = g.rasterizer.CullMode.0, "host graphics state" ); Some(HostPipeline { compute: false, mesh: false, pso, root, stages: bound, name: format!("{shaders:?}"), rectangles: !gs.is_empty(), slots: g.slots(), inputs: g.elements(), marks, }) } } /// The host's topology type: the console's rectangle and quad types draw /// as triangles on the host (see `draw`). fn host_topology(g: &GraphicsState) -> D3D12_PRIMITIVE_TOPOLOGY_TYPE { match g.topology.0 { RECTANGLES | QUADS => D3D12_PRIMITIVE_TOPOLOGY_TYPE_TRIANGLE, _ => g.topology, } } /// A mesh pipeline from the title's graphics state: the pipeline-state /// stream form (no input layout). fn mesh_pipeline( device: &ID3D12Device, root: &ID3D12RootSignature, ms: D3D12_SHADER_BYTECODE, ps: D3D12_SHADER_BYTECODE, g: &GraphicsState, name: &str, ) -> windows::core::Result { use windows::core::Interface; /// A stream subobject: its type, then its description, pointer-aligned. #[repr(C, align(8))] struct Sub(D3D12_PIPELINE_STATE_SUBOBJECT_TYPE, T); #[repr(C)] struct Stream { root: Sub<*mut std::ffi::c_void>, ms: Sub, ps: Sub, blend: Sub, mask: Sub, raster: Sub, depth: Sub, targets: Sub, dsv: Sub, samples: Sub, topology: Sub, } let mut stream = Stream { root: Sub(D3D12_PIPELINE_STATE_SUBOBJECT_TYPE_ROOT_SIGNATURE, root.as_raw()), ms: Sub(D3D12_PIPELINE_STATE_SUBOBJECT_TYPE_MS, ms), ps: Sub(D3D12_PIPELINE_STATE_SUBOBJECT_TYPE_PS, ps), blend: Sub(D3D12_PIPELINE_STATE_SUBOBJECT_TYPE_BLEND, g.blend), mask: Sub(D3D12_PIPELINE_STATE_SUBOBJECT_TYPE_SAMPLE_MASK, g.sample_mask), raster: Sub(D3D12_PIPELINE_STATE_SUBOBJECT_TYPE_RASTERIZER, g.rasterizer), depth: Sub(D3D12_PIPELINE_STATE_SUBOBJECT_TYPE_DEPTH_STENCIL, g.depth), targets: Sub(D3D12_PIPELINE_STATE_SUBOBJECT_TYPE_RENDER_TARGET_FORMATS, g.targets), dsv: Sub(D3D12_PIPELINE_STATE_SUBOBJECT_TYPE_DEPTH_STENCIL_FORMAT, g.depth_format), samples: Sub(D3D12_PIPELINE_STATE_SUBOBJECT_TYPE_SAMPLE_DESC, g.samples), // The type the host derives from the mesh shader anyway, given: a // pipeline library compares descriptions with it, and loads none // made from a stream without it. topology: Sub(D3D12_PIPELINE_STATE_SUBOBJECT_TYPE_PRIMITIVE_TOPOLOGY, host_topology(g)), }; let desc = D3D12_PIPELINE_STATE_STREAM_DESC { SizeInBytes: size_of::(), pPipelineStateSubobjectStream: (&raw mut stream).cast(), }; let device = device.cast::()?; super::library::pipeline( name, // SAFETY: the stream holds whole subobjects pointing at live data. |library, name| unsafe { library.LoadPipeline(name, &desc) }, // SAFETY: as above. || unsafe { device.CreatePipelineState(&desc) }, ) } /// The bytes of shader code (none for an absent stage). fn code_bytes(code: &D3D12_SHADER_BYTECODE) -> Vec { if code.pShaderBytecode.is_null() { return Vec::new(); } // SAFETY: a translation's own bytes, alive while its shader is. unsafe { std::slice::from_raw_parts(code.pShaderBytecode.cast::(), code.BytecodeLength) }.to_vec() } fn root_signature( device: &ID3D12Device, params: &[D3D12_ROOT_PARAMETER1], samplers: &[D3D12_STATIC_SAMPLER_DESC], compute: bool, ) -> windows::core::Result<(ID3D12RootSignature, Vec)> { let input = if compute { D3D12_ROOT_SIGNATURE_FLAG_NONE } else { D3D12_ROOT_SIGNATURE_FLAG_ALLOW_INPUT_ASSEMBLER_INPUT_LAYOUT }; let desc = D3D12_VERSIONED_ROOT_SIGNATURE_DESC { Version: windows::Win32::Graphics::Direct3D12::D3D_ROOT_SIGNATURE_VERSION_1_1, Anonymous: D3D12_VERSIONED_ROOT_SIGNATURE_DESC_0 { Desc_1_1: D3D12_ROOT_SIGNATURE_DESC1 { NumParameters: params.len() as u32, pParameters: params.as_ptr(), NumStaticSamplers: samplers.len() as u32, pStaticSamplers: samplers.as_ptr(), Flags: input | D3D12_ROOT_SIGNATURE_FLAG_CBV_SRV_UAV_HEAP_DIRECTLY_INDEXED | D3D12_ROOT_SIGNATURE_FLAG_SAMPLER_HEAP_DIRECTLY_INDEXED, }, }, }; // SAFETY: the description points at live arrays; the blob is read in // full before it is released. unsafe { let mut blob: Option = None; let mut error: Option = None; if let Err(e) = D3D12SerializeVersionedRootSignature(&desc, &mut blob, Some(&mut error)) { if let Some(error) = error { let text = std::slice::from_raw_parts( error.GetBufferPointer().cast::(), error.GetBufferSize(), ); tracing::warn!("root signature: {}", String::from_utf8_lossy(text)); } return Err(e); } let blob = blob.ok_or_else(windows::core::Error::empty)?; let bytes = std::slice::from_raw_parts(blob.GetBufferPointer().cast::(), blob.GetBufferSize()); Ok((device.CreateRootSignature(0, bytes)?, bytes.to_vec())) } } #[cfg(test)] mod tests { use super::*; fn element(format: DXGI_FORMAT, offset: u32) -> D3D12_INPUT_ELEMENT_DESC { D3D12_INPUT_ELEMENT_DESC { SemanticName: PCSTR(c"TEXCOORD".as_ptr().cast()), SemanticIndex: 0, Format: format, InputSlot: 0, AlignedByteOffset: offset, InputSlotClass: D3D12_INPUT_CLASSIFICATION_PER_VERTEX_DATA, InstanceDataStepRate: 0, } } fn offsets(inputs: &[D3D12_INPUT_ELEMENT_DESC]) -> Vec { let state = GraphicsState { inputs: inputs.to_vec(), ..GraphicsState::default() }; state.elements().iter().map(|e| e.offset).collect() } /// An appended element after one of 1 or 2 bytes started right after /// it; D3D12 aligns it to min(4, its size), so the rectangle expansion /// read its vertices from the wrong bytes. #[test] fn appended_elements_are_aligned() { const APPEND: u32 = D3D12_APPEND_ALIGNED_ELEMENT; // RG8 (2 bytes) then RG32 float: 4, not 2. let a = [ element(DXGI_FORMAT_R8G8_UNORM, 0), element(DXGI_FORMAT_R32G32_FLOAT, APPEND), ]; assert_eq!(offsets(&a), [0, 4]); // R8 then R16 float: 2, not 1; then R8 packs right after it. let b = [ element(DXGI_FORMAT_R8_UNORM, 0), element(DXGI_FORMAT_R16_FLOAT, APPEND), element(DXGI_FORMAT_R8_UNORM, APPEND), ]; assert_eq!(offsets(&b), [0, 2, 4]); // Already aligned layouts are unchanged. let c = [ element(DXGI_FORMAT_R8G8B8A8_UNORM, 0), element(DXGI_FORMAT_R32G32_FLOAT, APPEND), element(DXGI_FORMAT_R32G32B32_FLOAT, APPEND), ]; assert_eq!(offsets(&c), [0, 4, 12]); } /// An explicit offset near 4 GiB overflowed the next appended offset /// (a panic in debug builds). #[test] fn a_huge_explicit_offset_does_not_overflow() { let inputs = [ element(DXGI_FORMAT_R32G32B32A32_FLOAT, u32::MAX - 8), element(DXGI_FORMAT_R32_FLOAT, D3D12_APPEND_ALIGNED_ELEMENT), ]; let _ = offsets(&inputs); } }