| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166 |
- //! Draws on the host GPU (decision 0007, step 3). A list keeps the state
- //! the title sets for its draws (pipeline, descriptor heaps, root
- //! arguments, vertex and index buffers, targets); each draw then brings the
- //! host list up to date: the host pipeline and our root signature, the
- //! user data the translated shaders read, the host copies of the buffers
- //! and descriptors they read, and the states of the textures involved.
- //! State the host keeps across draws (viewports, scissors, blend factor,
- //! stencil reference, topology, targets) is set on the host list as the
- //! title sets it.
- use std::sync::atomic::{AtomicU64, Ordering};
- use victory_shader::{ResourceType, RootType, UsedResource};
- use crate::descriptor::View;
- use windows::Win32::Foundation::RECT;
- use windows::Win32::Graphics::Direct3D::D3D_PRIMITIVE_TOPOLOGY;
- use windows::Win32::Graphics::Direct3D12::*;
- use windows::Win32::Graphics::Dxgi::Common::DXGI_FORMAT;
- use windows::core::Interface;
- use crate::host::HostList;
- use crate::object::This;
- use crate::pipeline::host::HostPipeline;
- /// A root argument as the title set it.
- #[derive(Debug, Clone, Default)]
- enum Root {
- #[default]
- None,
- /// A descriptor table's first descriptor (an address in a heap).
- Table(u64),
- Constants(Vec<u32>),
- /// A root view's GPU address.
- View(u64),
- }
- #[repr(C)]
- #[derive(Debug, Clone, Copy, Default)]
- pub(crate) struct VertexView {
- address: u64,
- size: u32,
- stride: u32,
- }
- #[repr(C)]
- #[derive(Debug, Clone, Copy, Default)]
- pub(crate) struct IndexView {
- address: u64,
- size: u32,
- format: u32,
- }
- /// What one title list has set, and what its host list was last given.
- #[derive(Default)]
- pub(crate) struct State {
- pipeline: usize,
- /// View heap, sampler heap.
- heaps: [usize; 2],
- graphics: Vec<Root>,
- compute: Vec<Root>,
- vertex: [VertexView; 32],
- index: IndexView,
- /// Bound render target and depth resources, for their host states.
- targets: Vec<usize>,
- depth: usize,
- /// The title's primitive topology (the host's may differ).
- topology: u32,
- /// What the host list has: pipeline, root signature, heaps.
- host_pipeline: usize,
- /// Graphics, compute.
- host_roots: [usize; 2],
- host_heaps: [usize; 2],
- /// Tables made afresh in this list: (table, what it holds) → first
- /// dynamic slot.
- fresh_tables: std::collections::HashMap<(u64, u64), u32>,
- /// The dynamic slots the list holds: title heap, first, count; given
- /// back when the list is reset or released.
- dynamic: Vec<(usize, u32, u32)>,
- }
- impl Drop for State {
- fn drop(&mut self) {
- for &(heap, first, n) in &self.dynamic {
- crate::descriptor::release_dynamic(heap, first, n);
- }
- }
- }
- /// Draws recorded on the host GPU since the last frame line.
- static DRAWN: AtomicU64 = AtomicU64::new(0);
- /// Draws run on the host so far.
- pub(crate) fn drawn() -> u64 {
- DRAWN.load(Ordering::Relaxed)
- }
- /// The draws traced since the last present (diagnostics builds, with
- /// debug logging for this module): logged when the frame comes out black.
- #[cfg(feature = "diagnostics")]
- static FRAME: std::sync::Mutex<Vec<String>> = std::sync::Mutex::new(Vec::new());
- /// The draws traced since the last call.
- #[cfg(feature = "diagnostics")]
- pub(crate) fn take_frame() -> Vec<String> {
- std::mem::take(&mut *FRAME.lock().unwrap_or_else(|e| e.into_inner()))
- }
- /// Draws, mesh dispatches and dispatches recorded but not run on the host
- /// (no host pipeline) since the last call.
- pub(crate) fn take_dropped() -> u64 {
- DROPPED.swap(0, Ordering::Relaxed)
- }
- static DROPPED: AtomicU64 = AtomicU64::new(0);
- /// Marks the last traced draw as not run on the host.
- #[cfg(feature = "diagnostics")]
- fn dropped(pipeline: usize) {
- crate::pipeline::note_drop(pipeline as This);
- if let Some(last) = FRAME.lock().unwrap_or_else(|e| e.into_inner()).last_mut() {
- last.push_str(" DROPPED");
- }
- }
- /// Dispatches run on the host so far.
- static DISPATCHED: AtomicU64 = AtomicU64::new(0);
- pub(crate) fn dispatched() -> u64 {
- DISPATCHED.load(Ordering::Relaxed)
- }
- /// The host address of a root view: its buffer's host copy, or for a
- /// constant buffer not 256-byte aligned there (the console needs 16) a
- /// copy in upload memory.
- /// shortcut: a root view has no size; constant buffers are taken as 64 KiB,
- /// other views as 1 MiB (both cut at their buffer's end).
- fn root_view(list: &HostList, address: u64, srv: bool) -> Option<u64> {
- let shadow = crate::shadow::find(address, 1)?;
- let len = shadow
- .left(address)
- .min(if srv { 1 << 20 } else { 1 << 16 });
- let gpu = shadow.gpu(address);
- if srv || gpu % 256 == 0 {
- shadow.refresh(list, address, len);
- return Some(gpu);
- }
- let (gpu, to) = list.scratch(len)?;
- list.defer_fill(to, address as *const u8, len as usize);
- Some(gpu)
- }
- macro_rules! once {
- ($($arg:tt)*) => {{
- static ONCE: std::sync::Once = std::sync::Once::new();
- ONCE.call_once(|| tracing::warn!($($arg)*));
- }};
- }
- fn set(roots: &mut Vec<Root>, slot: u32, root: Root) {
- let slot = slot as usize;
- if roots.len() <= slot {
- roots.resize(slot + 1, Root::None);
- }
- roots[slot] = root;
- }
- impl State {
- pub(crate) fn reset(&mut self, pipeline: This) {
- *self = State::default();
- self.pipeline = pipeline as usize;
- }
- /// The title's topology; console-only ones become host lists (see
- /// [`crate::primitive`]).
- pub(crate) fn set_topology(&mut self, list: &HostList, topology: u32) {
- self.topology = topology;
- let host = match topology {
- RECTANGLES => 4,
- TRIANGLE_FAN | LINE_LOOP | QUADS | QUAD_STRIP => {
- once!(topology, "console topology drawn as a triangle list");
- 4
- }
- t => t,
- };
- // SAFETY: an open list.
- unsafe {
- list.list
- .IASetPrimitiveTopology(D3D_PRIMITIVE_TOPOLOGY(host as i32))
- };
- }
- pub(crate) fn set_pipeline(&mut self, pipeline: This) {
- self.pipeline = pipeline as usize;
- }
- pub(crate) fn set_heaps(&mut self, heaps: &[This]) {
- self.heaps = [0; 2];
- for &h in heaps {
- if !h.is_null() {
- let samplers = crate::descriptor::heap(h).samplers();
- self.heaps[usize::from(samplers)] = h as usize;
- }
- }
- }
- fn roots(&mut self, compute: bool) -> &mut Vec<Root> {
- if compute {
- &mut self.compute
- } else {
- &mut self.graphics
- }
- }
- pub(crate) fn set_table(&mut self, compute: bool, slot: u32, handle: u64) {
- set(self.roots(compute), slot, Root::Table(handle));
- }
- pub(crate) fn set_view(&mut self, compute: bool, slot: u32, address: u64) {
- set(self.roots(compute), slot, Root::View(address));
- }
- pub(crate) fn set_constants(&mut self, compute: bool, slot: u32, values: &[u32], offset: u32) {
- let roots = self.roots(compute);
- let s = slot as usize;
- if !matches!(roots.get(s), Some(Root::Constants(_))) {
- set(roots, slot, Root::Constants(Vec::new()));
- }
- if let Root::Constants(c) = &mut roots[s] {
- let end = offset as usize + values.len();
- if c.len() < end {
- c.resize(end, 0);
- }
- c[offset as usize..end].copy_from_slice(values);
- }
- }
- pub(crate) fn set_vertex_buffers(&mut self, start: u32, views: &[VertexView]) {
- for (i, v) in views.iter().enumerate() {
- if let Some(slot) = self.vertex.get_mut(start as usize + i) {
- *slot = *v;
- }
- }
- }
- pub(crate) fn set_index_buffer(&mut self, view: Option<&IndexView>) {
- self.index = view.copied().unwrap_or_default();
- }
- /// Render targets and depth, set on the host list at once.
- pub(crate) fn set_targets(&mut self, list: &HostList, handles: &[u64], depth: u64) {
- let Some(host) = crate::host::host() else {
- return;
- };
- self.targets.clear();
- let mut rtvs = Vec::new();
- for &h in handles {
- let Some(record) = crate::descriptor::rtv_record(h) else {
- once!(
- handle = format_args!("{h:#x}"),
- "render target without a recorded view"
- );
- continue;
- };
- let Some(texture) = crate::resource::host_resource(record.resource) else {
- continue;
- };
- self.targets.push(record.resource as usize);
- #[cfg(feature = "diagnostics")]
- crate::capture::note_target(record.resource);
- rtvs.push(host.rtv(texture, record.desc().as_ref()));
- }
- self.depth = 0;
- let dsv = crate::descriptor::dsv_record(depth).and_then(|record| {
- let texture = crate::resource::host_resource(record.resource)?;
- self.depth = record.resource as usize;
- #[cfg(feature = "diagnostics")]
- crate::capture::note_target(record.resource);
- Some(host.dsv(texture, record.depth_desc().as_ref()))
- });
- // SAFETY: an open list; the views were just made.
- unsafe {
- list.list.OMSetRenderTargets(
- rtvs.len() as u32,
- (!rtvs.is_empty()).then_some(rtvs.as_ptr()),
- false,
- dsv.as_ref().map(std::ptr::from_ref),
- )
- };
- }
- /// The user-data value of a root argument, as translated shaders read
- /// it: a table's byte offset in its heap, a constant's value.
- fn user_value(&self, compute: bool, slot: u8, root: RootType, dword: u32) -> u32 {
- match (self.roots_ref(compute).get(usize::from(slot)), root) {
- (Some(Root::Table(handle)), RootType::Table) => match self.heap_of(*handle) {
- Some(heap) => (handle - heap.start) as u32,
- None => {
- once!(
- handle = format_args!("{handle:#x}"),
- "descriptor table outside the bound heaps"
- );
- 0
- }
- },
- (Some(Root::Constants(c)), RootType::Constants) => {
- c.get(dword as usize).copied().unwrap_or(0)
- }
- _ => {
- once!(
- slot,
- ?root,
- "a shader reads a root argument the title did not set"
- );
- 0
- }
- }
- }
- /// The bound heap holding `handle`.
- fn heap_of(&self, handle: u64) -> Option<&'static crate::descriptor::Heap> {
- self.heaps
- .iter()
- .filter(|&&h| h != 0)
- .map(|&h| crate::descriptor::heap(h as This))
- .find(|h| h.index(handle).is_some())
- }
- /// Brings the host list up to date for a draw; false if the draw cannot
- /// run on the host.
- fn prepare(&mut self, list: &HostList, indexed: bool) -> bool {
- self.prepare_for(list, indexed, false)
- }
- /// As [`State::prepare`], for draws (`mesh` false) or mesh dispatches.
- fn prepare_for(&mut self, list: &HostList, indexed: bool, mesh: bool) -> bool {
- let Some(p) = crate::pipeline::host_pipeline(self.pipeline as This) else {
- return false;
- };
- if p.compute || p.mesh != mesh {
- once!(mesh, "a draw or mesh dispatch with another kind of pipeline");
- return false;
- }
- self.bind(list, p, false);
- let l = &list.list;
- // Vertex buffers the input layout reads.
- let mut views = [D3D12_VERTEX_BUFFER_VIEW::default(); 32];
- let mut count = 0;
- for (slot, (view, &v)) in views.iter_mut().zip(&self.vertex).enumerate() {
- if p.slots & 1 << slot == 0 {
- continue;
- }
- count = slot + 1;
- if v.address == 0 || v.size == 0 {
- continue;
- }
- let Some(shadow) = crate::shadow::find(v.address, u64::from(v.size)) else {
- continue;
- };
- shadow.refresh(list, v.address, u64::from(v.size));
- *view = D3D12_VERTEX_BUFFER_VIEW {
- BufferLocation: shadow.gpu(v.address),
- SizeInBytes: v.size,
- StrideInBytes: v.stride,
- };
- }
- // SAFETY: an open list; the views name live host buffers.
- unsafe {
- if count > 0 {
- l.IASetVertexBuffers(0, Some(&views[..count]));
- }
- }
- if indexed {
- let v = self.index;
- let Some(shadow) = crate::shadow::find(v.address, u64::from(v.size)) else {
- return false;
- };
- shadow.refresh(list, v.address, u64::from(v.size));
- let view = D3D12_INDEX_BUFFER_VIEW {
- BufferLocation: shadow.gpu(v.address),
- SizeInBytes: v.size,
- Format: DXGI_FORMAT(v.format as i32),
- };
- // SAFETY: an open list; the view names a live host buffer.
- unsafe { l.IASetIndexBuffer(Some(&view)) };
- }
- const RENDER_TARGET: u32 = 0x4;
- const DEPTH_WRITE: u32 = 0x10;
- for &t in &self.targets {
- crate::list::ensure_state(list, t as This, RENDER_TARGET);
- }
- if self.depth != 0 {
- crate::list::ensure_state(list, self.depth as This, DEPTH_WRITE);
- }
- true
- }
- /// A descriptor a shader reads through a table: its host slot made if
- /// it changed, constant data copied, textures made readable.
- /// What draws and dispatches share: heaps, our root signature, the
- /// pipeline, and per stage the user data, root views and the
- /// descriptors its tables hold.
- fn bind(&mut self, list: &HostList, p: &HostPipeline, compute: bool) {
- let l = &list.list;
- // Heaps first: a root signature with directly indexed heaps must be
- // set after them.
- if self.host_heaps != self.heaps {
- let heaps: Vec<Option<ID3D12DescriptorHeap>> = self
- .heaps
- .iter()
- .filter(|&&h| h != 0)
- .filter_map(|&h| crate::descriptor::heap(h as This).host())
- .map(|h| Some(h.heap.clone()))
- .collect();
- heaps.iter().flatten().for_each(|h| list.keep(h));
- // SAFETY: an open list and shader-visible heaps.
- unsafe { l.SetDescriptorHeaps(&heaps) };
- self.host_heaps = self.heaps;
- self.host_roots = [0; 2];
- }
- let root = p.root.as_raw() as usize;
- if self.host_roots[usize::from(compute)] != root {
- // SAFETY: an open list and a live root signature.
- unsafe {
- if compute {
- l.SetComputeRootSignature(&p.root);
- } else {
- l.SetGraphicsRootSignature(&p.root);
- }
- }
- self.host_roots[usize::from(compute)] = root;
- }
- // By host object: a title pipeline released and another made at its
- // address is another host pipeline (the list keeps the old one).
- if self.host_pipeline != p.pso.as_raw() as usize {
- // SAFETY: an open list and a live pipeline.
- unsafe { l.SetPipelineState(&p.pso) };
- self.host_pipeline = p.pso.as_raw() as usize;
- list.used_pipeline(&p.name);
- // The title may release the pipeline before it submits the list.
- list.keep(&p.pso);
- list.keep(&p.root);
- }
- for stage in &p.stages {
- let t = stage.translated();
- let made = self.tables(list, &t.used, compute);
- if let Some((param, dwords)) = stage.user_data {
- let mut values = vec![0u32; dwords as usize];
- for u in &t.host.user_data {
- let fresh = made
- .iter()
- .find(|m| m.0 == u.slot && u.root == RootType::Table);
- values[u.index as usize] = match fresh {
- Some(&(_, offset)) => offset,
- None => self.user_value(compute, u.slot, u.root, u.dword),
- };
- }
- // SAFETY: an open list; `values` covers the parameter.
- unsafe {
- if compute {
- l.SetComputeRoot32BitConstants(param, dwords, values.as_ptr().cast(), 0);
- } else {
- l.SetGraphicsRoot32BitConstants(param, dwords, values.as_ptr().cast(), 0);
- }
- }
- }
- for (views, srv) in [(&stage.root_cbvs, false), (&stage.root_srvs, true)] {
- for &(slot, param) in views {
- let Some(Root::View(address)) = self.roots_ref(compute).get(usize::from(slot))
- else {
- once!(slot, "a shader reads a root view the title did not set");
- continue;
- };
- let Some(gpu) = root_view(list, *address, srv) else {
- continue;
- };
- // SAFETY: an open list; the address is in live memory.
- unsafe {
- match (compute, srv) {
- (false, false) => l.SetGraphicsRootConstantBufferView(param, gpu),
- (false, true) => l.SetGraphicsRootShaderResourceView(param, gpu),
- (true, false) => l.SetComputeRootConstantBufferView(param, gpu),
- (true, true) => l.SetComputeRootShaderResourceView(param, gpu),
- }
- }
- }
- }
- }
- }
- /// Runs a dispatch on the host.
- pub(crate) fn dispatch(&mut self, list: &HostList, x: u32, y: u32, z: u32) {
- let Some(p) = crate::pipeline::host_pipeline(self.pipeline as This) else {
- once!(
- pipeline = format_args!("{:#x}", self.pipeline),
- "a dispatch without a host pipeline dropped"
- );
- DROPPED.fetch_add(1, Ordering::Relaxed);
- #[cfg(feature = "diagnostics")]
- crate::pipeline::note_drop(self.pipeline as This);
- return;
- };
- if !p.compute {
- once!("a dispatch with a graphics pipeline");
- return;
- }
- self.bind(list, p, true);
- #[cfg(feature = "diagnostics")]
- list.note_op(|| self.op_text(format!("dispatch {x}x{y}x{z}"), true));
- // SAFETY: an open list with everything set.
- unsafe { list.list.Dispatch(x, y, z) };
- DISPATCHED.fetch_add(1, Ordering::Relaxed);
- }
- /// What a draw or dispatch runs, for the device-removed report
- /// (diagnostics): the pipeline, then per stage the user data it gets
- /// (with the view at that heap index, if one is), the host addresses
- /// of its root views and the views its tables name.
- #[cfg(feature = "diagnostics")]
- fn op_text(&self, kind: String, compute: bool) -> String {
- use std::fmt::Write;
- let Some(p) = crate::pipeline::host_pipeline(self.pipeline as This) else {
- return kind;
- };
- let mut text = format!("{kind} {}", p.name);
- let views = (self.heaps[0] != 0)
- .then(|| crate::descriptor::heap(self.heaps[0] as This).host())
- .flatten();
- for stage in &p.stages {
- let t = stage.translated();
- let _ = write!(text, " | {}:", stage.shader.hash);
- for u in &t.host.user_data {
- let v = self.user_value(compute, u.slot, u.root, u.dword);
- let _ = write!(text, " u{}={v:#x}", u.index);
- let index = if u.root == RootType::Table { v / 32 } else { v };
- if let Some(slot) = views.and_then(|h| h.describe_slot(index)) {
- let _ = write!(text, " [heap {index}: {slot}]");
- }
- }
- for &(slot, _) in stage.root_cbvs.iter().chain(&stage.root_srvs) {
- if let Some(Root::View(address)) = self.roots_ref(compute).get(usize::from(slot)) {
- let host = crate::shadow::find(*address, 1)
- .map(|s| (s.gpu(*address), s.left(*address)));
- let _ = write!(text, " root{slot}={address:#x} host {host:#x?}");
- }
- }
- }
- let _ = write!(text, " tables {:?}", self.reads(compute));
- text
- }
- /// Runs a mesh dispatch of `x` groups on the host (the title's mesh
- /// programs are one-dimensional).
- pub(crate) fn dispatch_mesh(&mut self, list: &HostList, x: u32) {
- #[cfg(feature = "diagnostics")]
- self.trace("mesh", x, 0);
- let ran = self.prepare_for(list, false, true)
- && match list.list.cast::<ID3D12GraphicsCommandList6>() {
- Ok(l6) => {
- #[cfg(feature = "diagnostics")]
- list.note_op(|| self.op_text(format!("mesh {x}"), false));
- // SAFETY: an open list with everything set.
- unsafe { l6.DispatchMesh(x, 1, 1) };
- DRAWN.fetch_add(1, Ordering::Relaxed);
- true
- }
- Err(_) => false,
- };
- if !ran {
- DROPPED.fetch_add(1, Ordering::Relaxed);
- #[cfg(feature = "diagnostics")]
- dropped(self.pipeline);
- }
- }
- fn roots_ref(&self, compute: bool) -> &Vec<Root> {
- if compute {
- &self.compute
- } else {
- &self.graphics
- }
- }
- fn table_entry(&self, list: &HostList, u: &UsedResource, compute: bool) {
- let Some(Root::Table(table)) = self.roots_ref(compute).get(usize::from(u.slot)) else {
- return;
- };
- let handle = table + u64::from(u.table_offset_dwords) * 4;
- let Some(heap) = self.heap_of(handle) else {
- return;
- };
- let (Some(host), Some(index)) = (heap.host(), heap.index(handle)) else {
- return;
- };
- if let Some(view) = host.mirror(index, handle, true) {
- readable(list, u, &view);
- }
- }
- /// The descriptors a shader reads through tables. Tables holding a
- /// constant buffer are made afresh for the draw in dynamic slots, with
- /// the constant data copied to upload memory when the list is
- /// submitted: the host wants constant buffers 256-byte aligned, the
- /// console only 16. Returns those tables' new byte offsets by root
- /// parameter; the other tables' slots are made from the title's.
- ///
- /// Within a list, the same table holding the same descriptors is made
- /// once: its constant data is copied at submission either way, and the
- /// big tables the world binds would otherwise take hundreds of slots per
- /// draw, wrapping the dynamic slots onto lists not yet submitted.
- fn tables(&mut self, list: &HostList, used: &[UsedResource], compute: bool) -> Vec<(u8, u32)> {
- let tables = |u: &&UsedResource| u.root == RootType::Table;
- let mut fresh: Vec<u8> = used
- .iter()
- .filter(tables)
- .filter(|u| u.kind == ResourceType::Cbv)
- .map(|u| u.slot)
- .collect();
- // Tables reaching past a host heap smaller than the title's (the
- // host's limit, 1,000,000 views on NVIDIA) are made afresh too: the
- // title's slots there have no host slot of their own.
- for u in used.iter().filter(tables) {
- if fresh.contains(&u.slot) {
- continue;
- }
- let Some(&Root::Table(table)) = self.roots_ref(compute).get(usize::from(u.slot)) else {
- continue;
- };
- let end = table + u64::from(u.table_offset_dwords) * 4;
- let Some(heap) = self.heap_of(table) else {
- continue;
- };
- let (Some(host), Some(index)) = (heap.host(), heap.index(end)) else {
- continue;
- };
- if index >= host.count() {
- fresh.push(u.slot);
- }
- }
- fresh.sort_unstable();
- fresh.dedup();
- for u in used.iter().filter(tables) {
- if !fresh.contains(&u.slot) {
- self.table_entry(list, u, compute);
- }
- }
- let mut made = Vec::new();
- for slot in fresh {
- let Some(&Root::Table(table)) = self.roots_ref(compute).get(usize::from(slot)) else {
- continue;
- };
- let entries: Vec<&UsedResource> = used
- .iter()
- .filter(tables)
- .filter(|u| u.slot == slot)
- .collect();
- let Some(heap) = self.heap_of(table) else {
- continue;
- };
- let Some(host) = heap.host() else {
- continue;
- };
- let n = entries
- .iter()
- .map(|u| u.table_offset_dwords / 8 + 1)
- .max()
- .unwrap_or(1);
- // The table and what its entries hold (record words).
- let key = {
- use std::hash::{Hash, Hasher};
- let mut h = std::collections::hash_map::DefaultHasher::new();
- for k in 0..n {
- let at = table + u64::from(k) * 32;
- // SAFETY: a descriptor slot in title memory.
- unsafe { (at as *const [u32; 3]).read_unaligned() }.hash(&mut h);
- }
- (table, h.finish())
- };
- if let Some(&first) = self.fresh_tables.get(&key) {
- for u in entries.iter().filter(|u| u.kind != ResourceType::Cbv) {
- let handle = table + u64::from(u.table_offset_dwords) * 4;
- if let Some(view) = crate::descriptor::view(handle) {
- readable(list, u, &view);
- }
- }
- made.push((slot, first * 32));
- continue;
- }
- let Some(first) = host.dynamic(n) else {
- continue;
- };
- self.fresh_tables.insert(key, first);
- self.dynamic
- .push((std::ptr::from_ref(heap) as usize, first, n));
- // The whole range: a shader may index past the entries its
- // used-resource list names (descriptor arrays).
- for k in 0..n {
- let index = first + k;
- let used = entries.iter().find(|u| u.table_offset_dwords / 8 == k);
- match crate::descriptor::view(table + u64::from(k) * 32) {
- Some(View::Constant(address, size)) => {
- let Some((gpu, to)) = list.scratch(u64::from(size)) else {
- host.make_null(index);
- continue;
- };
- list.defer_fill(to, address as *const u8, size as usize);
- host.constant_at(index, gpu, size);
- }
- Some(view) => {
- host.make_at(index, &view);
- if let Some(u) = used {
- readable(list, u, &view);
- }
- }
- None => {
- host.make_null(index);
- if used.is_some() {
- once!(slot, "a table entry without a view");
- }
- }
- }
- }
- made.push((slot, first * 32));
- }
- made
- }
- /// The first draws, at debug level: what they bind and read
- /// (diagnostics builds).
- #[cfg(feature = "diagnostics")]
- fn trace(&self, kind: &str, count: u32, first: u32) {
- static TRACED: AtomicU64 = AtomicU64::new(0);
- if !tracing::enabled!(tracing::Level::DEBUG) {
- return;
- }
- let listed = crate::capture::listed(crate::queue::presented() + 1);
- let logged = listed || TRACED.fetch_add(1, Ordering::Relaxed) < 64;
- let targets: Vec<String> = self
- .targets
- .iter()
- .filter_map(|&t| crate::resource::host_resource(t as This))
- .map(|t| {
- // SAFETY: a live resource.
- let d = unsafe { t.GetDesc() };
- format!("{}x{} f{}", d.Width, d.Height, d.Format.0)
- })
- .collect();
- let v = self.vertex[0];
- let line = format!(
- "{kind} {count} from {first} topology {} pipeline {:#x} targets {targets:?} vb {:#x}+{} stride {} reads {:?}",
- self.topology,
- self.pipeline,
- v.address,
- v.size,
- v.stride,
- self.reads(false)
- );
- if logged {
- tracing::debug!("draw {line}");
- }
- let mut frame = FRAME.lock().unwrap_or_else(|e| e.into_inner());
- if frame.len() < 256 {
- frame.push(line);
- }
- }
- /// What a draw's shaders read, one line per used resource (diagnostics
- /// builds).
- #[cfg(feature = "diagnostics")]
- fn reads(&self, compute: bool) -> Vec<String> {
- let Some(p) = crate::pipeline::host_pipeline(self.pipeline as This) else {
- return vec!["no host pipeline".into()];
- };
- let mut out = Vec::new();
- for stage in &p.stages {
- let t = stage.translated();
- for u in &t.used {
- let what = match (u.root, self.roots_ref(compute).get(usize::from(u.slot))) {
- (RootType::Table, Some(Root::Table(table))) => {
- let handle = table + u64::from(u.table_offset_dwords) * 4;
- describe(crate::descriptor::view(handle).as_ref())
- }
- (root, _) => format!("{root:?}"),
- };
- out.push(format!(
- "{} {:?} s{}+{}: {what}",
- stage.shader.hash, u.kind, u.slot, u.table_offset_dwords
- ));
- }
- }
- out
- }
- pub(crate) fn draw(
- &mut self,
- list: &HostList,
- vertices: u32,
- instances: u32,
- first: u32,
- first_instance: u32,
- ) {
- #[cfg(feature = "diagnostics")]
- self.trace("draw", vertices, first);
- let rects = self.topology == RECTANGLES;
- let ran = self.prepare(list, false) && (!rects || self.rectangles(list, vertices, first));
- if ran {
- // Rectangles were expanded into their own vertex buffers.
- let (count, from) = if rects {
- (vertices / 3 * 6, 0)
- } else {
- (vertices, first)
- };
- #[cfg(feature = "diagnostics")]
- list.note_op(|| self.op_text(format!("draw {count}x{instances}"), false));
- // SAFETY: an open list with everything set.
- unsafe {
- list.list
- .DrawInstanced(count, instances, from, first_instance)
- };
- DRAWN.fetch_add(1, Ordering::Relaxed);
- }
- if !ran {
- DROPPED.fetch_add(1, Ordering::Relaxed);
- #[cfg(feature = "diagnostics")]
- dropped(self.pipeline);
- }
- }
- /// Binds vertex buffers holding the rectangles of vertices `first..first
- /// + count` as triangles, made when the list is submitted.
- fn rectangles(&self, list: &HostList, count: u32, first: u32) -> bool {
- let Some(p) = crate::pipeline::host_pipeline(self.pipeline as This) else {
- return false;
- };
- let rects = u64::from(count / 3);
- for slot in 0..32 {
- let elements: Vec<(u32, u32)> = p
- .inputs
- .iter()
- .filter(|e| e.slot == slot && !e.per_instance)
- .map(|e| (e.offset, e.format))
- .collect();
- if elements.is_empty() {
- continue;
- }
- let v = self.vertex[slot as usize];
- let stride = u64::from(v.stride);
- let from = v.address + u64::from(first) * stride;
- if v.address == 0
- || stride == 0
- || u64::from(first + count) * stride > u64::from(v.size)
- || crate::shadow::find(from, rects * 3 * stride).is_none()
- {
- once!(slot, "rectangle vertices outside their buffer");
- return false;
- }
- let Some((gpu, to)) = list.scratch(rects * 6 * stride) else {
- return false;
- };
- let (from, to, stride) = (from as usize, to as usize, stride as usize);
- let n = rects as usize;
- list.defer_host(move || {
- // SAFETY: the vertices lie in a title buffer (checked when
- // recorded) and the scratch memory is the list's.
- let (src, dst) = unsafe {
- (
- std::slice::from_raw_parts(from as *const u8, n * 3 * stride),
- std::slice::from_raw_parts_mut(to as *mut u8, n * 6 * stride),
- )
- };
- crate::primitive::expand_rects(src, dst, stride, &elements);
- });
- let view = D3D12_VERTEX_BUFFER_VIEW {
- BufferLocation: gpu,
- SizeInBytes: (rects * 6 * stride as u64) as u32,
- StrideInBytes: v.stride,
- };
- // SAFETY: an open list; the view names live scratch memory.
- unsafe { list.list.IASetVertexBuffers(slot, Some(&[view])) };
- }
- true
- }
- pub(crate) fn draw_indexed(
- &mut self,
- list: &HostList,
- indices: u32,
- instances: u32,
- first: u32,
- base: i32,
- first_instance: u32,
- ) {
- #[cfg(feature = "diagnostics")]
- self.trace("indexed", indices, first);
- if self.topology == RECTANGLES {
- once!("indexed rectangles not drawn");
- }
- let ran = self.topology != RECTANGLES && self.prepare(list, true);
- if ran {
- #[cfg(feature = "diagnostics")]
- list.note_op(|| self.op_text(format!("indexed {indices}x{instances}"), false));
- // SAFETY: an open list with everything set.
- unsafe {
- list.list
- .DrawIndexedInstanced(indices, instances, first, base, first_instance)
- };
- DRAWN.fetch_add(1, Ordering::Relaxed);
- }
- if !ran {
- DROPPED.fetch_add(1, Ordering::Relaxed);
- #[cfg(feature = "diagnostics")]
- dropped(self.pipeline);
- }
- }
- }
- impl State {
- /// Indirect execution of a one-argument signature of `kind` (PC
- /// numbering: 0 draw, 1 indexed draw, 2 dispatch, 10 mesh dispatch) with
- /// records `stride` bytes apart at title address `arguments`, and the
- /// record count at `count` if the title gives one: the GPU reads them
- /// from the buffer's host copy, where earlier dispatches may have
- /// written them. False if it cannot run on the host.
- pub(crate) fn execute_indirect(
- &mut self,
- list: &HostList,
- (kind, stride, max): (u32, u32, u32),
- arguments: u64,
- count: Option<u64>,
- ) -> bool {
- const DRAW: u32 = 0;
- const INDEXED: u32 = 1;
- const DISPATCH: u32 = 2;
- const MESH: u32 = 10;
- #[cfg(feature = "diagnostics")]
- let why = |what: &str| crate::pipeline::note_why(format!("indirect kind {kind}: {what}"));
- #[cfg(not(feature = "diagnostics"))]
- let why = |_: &str| {};
- let len = u64::from(stride) * u64::from(max.max(1));
- let Some(args) = crate::shadow::find(arguments, len) else {
- why("arguments outside title buffers");
- return false;
- };
- let counter = match count {
- Some(at) => match crate::shadow::find(at, 4) {
- Some(s) => Some((s, at)),
- None => {
- why("count outside title buffers");
- return false;
- }
- },
- None => None,
- };
- let Some(signature) = matches!(kind, DRAW | INDEXED | DISPATCH | MESH)
- .then(|| host_signature(kind, stride))
- .flatten()
- else {
- why("no host signature");
- return false;
- };
- args.refresh(list, arguments, len);
- if let Some((s, at)) = &counter {
- s.refresh(list, *at, 4);
- }
- // Bounded first: the clamp binds a compute pipeline of its own.
- if !crate::indirect::bound(list, &args.buffer, args.gpu(arguments), (kind, stride, max)) {
- why("not bounded (no clamp, or arguments not 4-byte aligned)");
- return false;
- }
- self.host_pipeline = 0;
- self.host_roots[1] = 0;
- let ran = match kind {
- DRAW | INDEXED => self.topology != RECTANGLES && self.prepare(list, kind == INDEXED),
- MESH => self.prepare_for(list, false, true),
- DISPATCH => match crate::pipeline::host_pipeline(self.pipeline as This) {
- Some(p) if p.compute => {
- self.bind(list, p, true);
- true
- }
- _ => false,
- },
- _ => false,
- };
- if !ran {
- crate::indirect::done(list, &args.buffer);
- #[cfg(feature = "diagnostics")]
- match crate::pipeline::host_pipeline(self.pipeline as This) {
- None => crate::pipeline::note_drop(self.pipeline as This),
- Some(p) => why(&format!("prepare failed (compute {}, mesh {})", p.compute, p.mesh)),
- }
- return false;
- }
- #[cfg(feature = "diagnostics")]
- list.note_op(|| {
- self.op_text(format!("indirect kind {kind} max {max} at {arguments:#x}"), kind == DISPATCH)
- });
- // SAFETY: an open list with everything set; the buffers are host
- // copies the list keeps alive (refresh), the signature is cached.
- unsafe {
- list.list.ExecuteIndirect(
- &signature,
- max,
- &args.buffer,
- args.offset(arguments),
- counter.as_ref().map(|(s, _)| &s.buffer),
- counter.as_ref().map_or(0, |(s, at)| s.offset(*at)),
- )
- };
- crate::indirect::done(list, &args.buffer);
- if kind == DISPATCH {
- DISPATCHED.fetch_add(1, Ordering::Relaxed);
- } else {
- DRAWN.fetch_add(1, Ordering::Relaxed);
- }
- true
- }
- }
- /// The host command signature for one argument of `kind` with records
- /// `stride` bytes apart (no root arguments, so no root signature), made
- /// once.
- fn host_signature(kind: u32, stride: u32) -> Option<ID3D12CommandSignature> {
- static MADE: std::sync::Mutex<Vec<((u32, u32), ID3D12CommandSignature)>> =
- std::sync::Mutex::new(Vec::new());
- let mut made = MADE.lock().unwrap_or_else(|e| e.into_inner());
- if let Some((_, s)) = made.iter().find(|(k, _)| *k == (kind, stride)) {
- return Some(s.clone());
- }
- let argument = D3D12_INDIRECT_ARGUMENT_DESC {
- Type: D3D12_INDIRECT_ARGUMENT_TYPE(kind as i32),
- ..Default::default()
- };
- let desc = D3D12_COMMAND_SIGNATURE_DESC {
- ByteStride: stride,
- NumArgumentDescs: 1,
- pArgumentDescs: &argument,
- NodeMask: 0,
- };
- let mut signature: Option<ID3D12CommandSignature> = None;
- // SAFETY: a description of one argument; no root arguments.
- let result = unsafe {
- crate::host::host()?.device.CreateCommandSignature(
- &desc,
- None::<&ID3D12RootSignature>,
- &mut signature,
- )
- };
- if let Err(e) = result {
- once!(error = %e, kind, stride, "host command signature refused");
- return None;
- }
- let signature = signature?;
- made.push(((kind, stride), signature.clone()));
- Some(signature)
- }
- /// Gets what a shader reads or writes through a view ready: buffer data
- /// copied, textures moved to the state they need.
- fn readable(list: &HostList, u: &UsedResource, view: &View) {
- const SHADER_RESOURCE: u32 = 0x40 | 0x80;
- const UNORDERED_ACCESS: u32 = 0x8;
- let placed = crate::descriptor::host::placed_texture(view);
- let view = placed.as_ref().unwrap_or(view);
- // Buffer views read title memory: their range is copied at submission;
- // what shaders write through unordered access goes back to it after.
- if let Some((address, len)) = crate::descriptor::host::buffer_range(view)
- && len > 0
- && let Some(shadow) = crate::shadow::find(address, len)
- {
- shadow.refresh(list, address, len);
- if u.kind == ResourceType::Uav {
- shadow.gpu_writes(list, address, len);
- }
- }
- match (view, u.kind) {
- (View::Shader(resource, _), ResourceType::Srv) => {
- if let Some(t) = crate::resource::host_resource(*resource as This)
- && crate::texture::title_written(t)
- {
- crate::texture::upload(list, *resource as This, t);
- }
- crate::list::ensure_state(list, *resource as This, SHADER_RESOURCE);
- #[cfg(feature = "diagnostics")]
- if let Some(t) = crate::resource::host_resource(*resource as This) {
- // SAFETY: a live resource.
- let d = unsafe { t.GetDesc() };
- if d.Flags.0 & 7 == 0 {
- tracing::debug!(
- resource = format_args!("{resource:#x}"),
- ?d,
- "a shader reads a texture the title fills"
- );
- }
- }
- }
- (View::Unordered(resource, _, _), ResourceType::Uav) => {
- #[cfg(feature = "diagnostics")]
- crate::capture::note_target(*resource as This);
- crate::list::ensure_state(list, *resource as This, UNORDERED_ACCESS);
- }
- _ => {}
- }
- }
- /// A view in a few words (diagnostics builds).
- #[cfg(feature = "diagnostics")]
- fn describe(view: Option<&View>) -> String {
- let texture = |r: This| {
- crate::resource::host_resource(r).map(|t| {
- // SAFETY: a live resource.
- let d = unsafe { t.GetDesc() };
- format!("{}x{} f{} flags {:#x}", d.Width, d.Height, d.Format.0, d.Flags.0)
- })
- };
- match view {
- None => "no view".into(),
- Some(v @ View::Shader(r, _)) => texture(*r as This)
- .unwrap_or_else(|| format!("buffer {:?}", crate::descriptor::host::buffer_range(v))),
- Some(View::Unordered(r, ..)) => format!("uav {:?}", texture(*r as This)),
- Some(View::Placed(u, r, d)) => format!(
- "placed uav={u} dim {} {}x{} f{} layout {:#x} at {:#x}",
- r[0],
- u64::from(r[4]) | u64::from(r[5]) << 32,
- r[6],
- r[8],
- r[11],
- u64::from(d[10]) | u64::from(d[11]) << 32
- ),
- // The first four values, as a colour rectangle's constants are.
- Some(View::Constant(address, size))
- if *size >= 16 && crate::texture::committed(*address as *const u8, 16) =>
- {
- // SAFETY: 16 committed bytes of title memory.
- let v = unsafe { (*address as *const [f32; 4]).read_unaligned() };
- format!("Constant({address:#x}, {size}) {v:?}")
- }
- Some(v) => format!("{v:?}").chars().take(60).collect(),
- }
- }
- /// The console's own primitive topologies.
- const TRIANGLE_FAN: u32 = 6;
- const RECTANGLES: u32 = 17;
- const LINE_LOOP: u32 = 18;
- const QUADS: u32 = 19;
- const QUAD_STRIP: u32 = 20;
- /// Fixed-function state, straight onto the host list.
- pub(crate) fn viewports(list: &HostList, viewports: &[D3D12_VIEWPORT]) {
- // SAFETY: an open list.
- unsafe { list.list.RSSetViewports(viewports) };
- }
- pub(crate) fn scissors(list: &HostList, rects: &[RECT]) {
- // SAFETY: an open list.
- unsafe { list.list.RSSetScissorRects(rects) };
- }
- pub(crate) fn blend_factor(list: &HostList, factor: Option<&[f32; 4]>) {
- // SAFETY: an open list.
- unsafe { list.list.OMSetBlendFactor(factor) };
- }
- pub(crate) fn stencil_ref(list: &HostList, reference: u32) {
- // SAFETY: an open list.
- unsafe { list.list.OMSetStencilRef(reference) };
- }
|