mirror of
https://github.com/storytold/wgpu.git
synced 2026-11-19 14:01:08 +01:00
Add in-memory tracing and inline data in traces
This commit is contained in:
1 parent
e412592b23
commit
985da29bcf
18 files changed
+461
-150
No files matched your search
@@ -127,7 +127,7 @@ fn main() {
|
||||
unsafe { device.start_graphics_debugger_capture() };
|
||||
|
||||
while let Some(action) = actions.pop() {
|
||||
player.process(&device, &queue, action, &dir);
|
||||
player.process(&device, &queue, action, trace::DiskTraceLoader::new(&dir));
|
||||
}
|
||||
|
||||
unsafe { device.stop_graphics_debugger_capture() };
|
||||
@@ -183,7 +183,12 @@ fn main() {
|
||||
target.exit();
|
||||
}
|
||||
Some(action) => {
|
||||
player.process(&device, &queue, action, &dir);
|
||||
player.process(
|
||||
&device,
|
||||
&queue,
|
||||
action,
|
||||
trace::DiskTraceLoader::new(&dir),
|
||||
);
|
||||
}
|
||||
None => {
|
||||
if !done {
|
||||
|
||||
+30
-53
@@ -6,14 +6,14 @@
|
||||
extern crate wgpu_core as wgc;
|
||||
extern crate wgpu_types as wgt;
|
||||
|
||||
use std::{borrow::Cow, convert::Infallible, fs, path::Path, sync::Arc};
|
||||
use std::{borrow::Cow, convert::Infallible, sync::Arc};
|
||||
|
||||
use hashbrown::HashMap;
|
||||
|
||||
use wgc::{
|
||||
binding_model::BindingResource,
|
||||
command::{ArcCommand, ArcReferences, BasePass, Command, PointerReferences},
|
||||
device::trace,
|
||||
device::trace::{self, DataKind, DataLoader},
|
||||
id::PointerId,
|
||||
};
|
||||
|
||||
@@ -94,7 +94,7 @@ impl Player {
|
||||
device: &Arc<wgc::device::Device>,
|
||||
queue: &Arc<wgc::device::queue::Queue>,
|
||||
action: trace::Action<PointerReferences>,
|
||||
dir: &Path,
|
||||
loader: impl DataLoader,
|
||||
) {
|
||||
use wgc::device::trace::Action;
|
||||
log::debug!("action {action:?}");
|
||||
@@ -226,15 +226,17 @@ impl Player {
|
||||
let _bind_group = self.bind_groups.remove(&id).expect("invalid bind group");
|
||||
}
|
||||
Action::CreateShaderModule { id, desc, data } => {
|
||||
log::debug!("Creating shader from {data}");
|
||||
let code = fs::read_to_string(dir.join(&data)).unwrap();
|
||||
let source = if data.ends_with(".wgsl") {
|
||||
wgc::pipeline::ShaderModuleSource::Wgsl(Cow::Owned(code.clone()))
|
||||
} else if data.ends_with(".ron") {
|
||||
let code = loader.load_utf8(&data);
|
||||
let source = if data.kind() == DataKind::Wgsl {
|
||||
wgc::pipeline::ShaderModuleSource::Wgsl(code.clone())
|
||||
} else if data.kind() == DataKind::Ron {
|
||||
let module = ron::de::from_str(&code).unwrap();
|
||||
wgc::pipeline::ShaderModuleSource::Naga(module)
|
||||
} else {
|
||||
panic!("Unknown shader {data}");
|
||||
panic!(
|
||||
"Unknown data kind for CreateShaderModule: {:?}",
|
||||
data.kind()
|
||||
);
|
||||
};
|
||||
match device.create_shader_module(&desc, source) {
|
||||
Ok(module) => self.shader_modules.insert(id, module),
|
||||
@@ -250,8 +252,8 @@ impl Player {
|
||||
runtime_checks,
|
||||
} => {
|
||||
let spirv = data.iter().find_map(|a| {
|
||||
if a.ends_with(".spv") {
|
||||
let data = fs::read(dir.join(a)).unwrap();
|
||||
if a.kind() == DataKind::Spv {
|
||||
let data = loader.load(a);
|
||||
assert!(data.len().is_multiple_of(4));
|
||||
|
||||
Some(Cow::Owned(bytemuck::pod_collect_to_vec(&data)))
|
||||
@@ -259,46 +261,21 @@ impl Player {
|
||||
None
|
||||
}
|
||||
});
|
||||
let dxil = data.iter().find_map(|a| {
|
||||
if a.ends_with(".dxil") {
|
||||
let vec = std::fs::read(dir.join(a)).unwrap();
|
||||
Some(Cow::Owned(vec))
|
||||
} else {
|
||||
None
|
||||
}
|
||||
});
|
||||
let hlsl = data.iter().find_map(|a| {
|
||||
if a.ends_with(".hlsl") {
|
||||
let code = fs::read_to_string(dir.join(a)).unwrap();
|
||||
Some(Cow::Owned(code))
|
||||
} else {
|
||||
None
|
||||
}
|
||||
});
|
||||
let msl = data.iter().find_map(|a| {
|
||||
if a.ends_with(".msl") {
|
||||
let code = fs::read_to_string(dir.join(a)).unwrap();
|
||||
Some(Cow::Owned(code))
|
||||
} else {
|
||||
None
|
||||
}
|
||||
});
|
||||
let glsl = data.iter().find_map(|a| {
|
||||
if a.ends_with(".glsl") {
|
||||
let code = fs::read_to_string(dir.join(a)).unwrap();
|
||||
Some(Cow::Owned(code))
|
||||
} else {
|
||||
None
|
||||
}
|
||||
});
|
||||
let wgsl = data.iter().find_map(|a| {
|
||||
if a.ends_with(".wgsl") {
|
||||
let code = fs::read_to_string(dir.join(a)).unwrap();
|
||||
Some(Cow::Owned(code))
|
||||
} else {
|
||||
None
|
||||
}
|
||||
});
|
||||
let dxil = data
|
||||
.iter()
|
||||
.find_map(|a| (a.kind() == DataKind::Dxil).then(|| loader.load(a)));
|
||||
let hlsl = data
|
||||
.iter()
|
||||
.find_map(|a| (a.kind() == DataKind::Hlsl).then(|| loader.load_utf8(a)));
|
||||
let msl = data
|
||||
.iter()
|
||||
.find_map(|a| (a.kind() == DataKind::Msl).then(|| loader.load_utf8(a)));
|
||||
let glsl = data
|
||||
.iter()
|
||||
.find_map(|a| (a.kind() == DataKind::Glsl).then(|| loader.load_utf8(a)));
|
||||
let wgsl = data
|
||||
.iter()
|
||||
.find_map(|a| (a.kind() == DataKind::Wgsl).then(|| loader.load_utf8(a)));
|
||||
let desc = wgt::CreateShaderModuleDescriptorPassthrough {
|
||||
entry_point,
|
||||
label,
|
||||
@@ -382,7 +359,7 @@ impl Player {
|
||||
queued,
|
||||
} => {
|
||||
let buffer = self.resolve_buffer_id(id);
|
||||
let bin = std::fs::read(dir.join(data)).unwrap();
|
||||
let bin = loader.load(&data);
|
||||
let size = (range.end - range.start) as usize;
|
||||
if queued {
|
||||
queue
|
||||
@@ -401,7 +378,7 @@ impl Player {
|
||||
size,
|
||||
} => {
|
||||
let to = self.resolve_texel_copy_texture_info(to);
|
||||
let bin = std::fs::read(dir.join(data)).unwrap();
|
||||
let bin = loader.load(&data);
|
||||
queue
|
||||
.write_texture(to, &bin, &layout, &size)
|
||||
.expect("Queue::write_texture error");
|
||||
|
||||
@@ -47,7 +47,7 @@
|
||||
label: None,
|
||||
flags: (bits: 3),
|
||||
),
|
||||
data: "empty.wgsl",
|
||||
data: File("empty.wgsl"),
|
||||
),
|
||||
CreateComputePipeline(
|
||||
id: PointerId(0x10),
|
||||
|
||||
@@ -20,7 +20,7 @@
|
||||
),
|
||||
WriteBuffer(
|
||||
id: PointerId(0x10),
|
||||
data: "data1.bin",
|
||||
data: File("data1.bin"),
|
||||
range: (
|
||||
start: 0,
|
||||
end: 16,
|
||||
|
||||
@@ -40,7 +40,7 @@
|
||||
mip_level: 0,
|
||||
array_layer: 0,
|
||||
),
|
||||
data: "quad.bin",
|
||||
data: File("quad.bin"),
|
||||
layout: (
|
||||
offset: 0,
|
||||
bytes_per_row: Some(256),
|
||||
@@ -73,7 +73,7 @@
|
||||
// Make sure there is something in the buffer, otherwise it might be just zero init!
|
||||
WriteBuffer(
|
||||
id: PointerId(0x110),
|
||||
data: "data1.bin",
|
||||
data: File("data1.bin"),
|
||||
range: (
|
||||
start: 0,
|
||||
end: 16,
|
||||
|
||||
@@ -20,7 +20,7 @@
|
||||
label: None,
|
||||
flags: (bits: 3),
|
||||
),
|
||||
data: "empty.wgsl",
|
||||
data: File("empty.wgsl"),
|
||||
),
|
||||
CreateComputePipeline(
|
||||
id: PointerId(0x10),
|
||||
|
||||
@@ -15,7 +15,7 @@
|
||||
label: None,
|
||||
flags: (bits: 3),
|
||||
),
|
||||
data: "quad.wgsl",
|
||||
data: File("quad.wgsl"),
|
||||
),
|
||||
CreateTexture(PointerId(0x10), (
|
||||
label: Some("Output Texture"),
|
||||
|
||||
@@ -67,7 +67,7 @@
|
||||
),
|
||||
WriteBuffer(
|
||||
id: PointerId(0x120),
|
||||
data: "data1.bin",
|
||||
data: File("data1.bin"),
|
||||
range: (
|
||||
start: 4,
|
||||
end: 20,
|
||||
@@ -80,7 +80,7 @@
|
||||
label: None,
|
||||
flags: (bits: 3),
|
||||
),
|
||||
data: "zero-init-buffer-for-binding.wgsl",
|
||||
data: File("zero-init-buffer-for-binding.wgsl"),
|
||||
),
|
||||
CreateBuffer(PointerId(0x130), (
|
||||
label: Some("used in binding"),
|
||||
|
||||
@@ -125,7 +125,7 @@
|
||||
label: None,
|
||||
flags: (bits: 3),
|
||||
),
|
||||
data: "zero-init-texture-binding.wgsl",
|
||||
data: File("zero-init-texture-binding.wgsl"),
|
||||
),
|
||||
CreateComputePipeline(
|
||||
id: PointerId(0x10),
|
||||
|
||||
@@ -21,7 +21,7 @@ use std::{
|
||||
slice,
|
||||
sync::Arc,
|
||||
};
|
||||
use wgc::command::PointerReferences;
|
||||
use wgc::{command::PointerReferences, device::trace::DiskTraceLoader};
|
||||
|
||||
#[derive(serde::Deserialize)]
|
||||
enum ExpectedData {
|
||||
@@ -98,7 +98,7 @@ impl Test<'_> {
|
||||
|
||||
println!("\t\t\tRunning...");
|
||||
for action in self.actions {
|
||||
player.process(&device, &queue, action, dir);
|
||||
player.process(&device, &queue, action, DiskTraceLoader::new(dir));
|
||||
}
|
||||
println!("\t\t\tMapping...");
|
||||
for expect in &self.expectations {
|
||||
|
||||
@@ -997,27 +997,31 @@ impl Global {
|
||||
let device = self.hub.devices.get(device_id);
|
||||
|
||||
#[cfg(feature = "trace")]
|
||||
let data = device.trace.lock().as_mut().map(|trace| match source {
|
||||
#[cfg(feature = "wgsl")]
|
||||
pipeline::ShaderModuleSource::Wgsl(ref code) => {
|
||||
trace.make_binary("wgsl", code.as_bytes())
|
||||
}
|
||||
#[cfg(feature = "glsl")]
|
||||
pipeline::ShaderModuleSource::Glsl(ref code, _) => {
|
||||
trace.make_binary("glsl", code.as_bytes())
|
||||
}
|
||||
#[cfg(feature = "spirv")]
|
||||
pipeline::ShaderModuleSource::SpirV(ref code, _) => {
|
||||
trace.make_binary("spirv", bytemuck::cast_slice::<u32, u8>(code))
|
||||
}
|
||||
pipeline::ShaderModuleSource::Naga(ref module) => {
|
||||
let string =
|
||||
ron::ser::to_string_pretty(module, ron::ser::PrettyConfig::default())
|
||||
.unwrap();
|
||||
trace.make_binary("ron", string.as_bytes())
|
||||
}
|
||||
pipeline::ShaderModuleSource::Dummy(_) => {
|
||||
panic!("found `ShaderModuleSource::Dummy`")
|
||||
let data = device.trace.lock().as_mut().map(|trace| {
|
||||
use crate::device::trace::DataKind;
|
||||
|
||||
match source {
|
||||
#[cfg(feature = "wgsl")]
|
||||
pipeline::ShaderModuleSource::Wgsl(ref code) => {
|
||||
trace.make_binary(DataKind::Wgsl, code.as_bytes())
|
||||
}
|
||||
#[cfg(feature = "glsl")]
|
||||
pipeline::ShaderModuleSource::Glsl(ref code, _) => {
|
||||
trace.make_binary(DataKind::Glsl, code.as_bytes())
|
||||
}
|
||||
#[cfg(feature = "spirv")]
|
||||
pipeline::ShaderModuleSource::SpirV(ref code, _) => {
|
||||
trace.make_binary(DataKind::Spv, bytemuck::cast_slice::<u32, u8>(code))
|
||||
}
|
||||
pipeline::ShaderModuleSource::Naga(ref module) => {
|
||||
let string =
|
||||
ron::ser::to_string_pretty(module, ron::ser::PrettyConfig::default())
|
||||
.unwrap();
|
||||
trace.make_binary(DataKind::Ron, string.as_bytes())
|
||||
}
|
||||
pipeline::ShaderModuleSource::Dummy(_) => {
|
||||
panic!("found `ShaderModuleSource::Dummy`")
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
@@ -1081,17 +1085,22 @@ impl Global {
|
||||
|
||||
#[cfg(feature = "trace")]
|
||||
if let Some(ref mut trace) = *device.trace.lock() {
|
||||
use crate::device::trace::DataKind;
|
||||
|
||||
let mut file_names = Vec::new();
|
||||
for (data, ext) in [
|
||||
(desc.spirv.as_ref().map(|a| bytemuck::cast_slice(a)), "spv"),
|
||||
(desc.dxil.as_deref(), "dxil"),
|
||||
(desc.hlsl.as_ref().map(|a| a.as_bytes()), "hlsl"),
|
||||
(desc.msl.as_ref().map(|a| a.as_bytes()), "msl"),
|
||||
(desc.glsl.as_ref().map(|a| a.as_bytes()), "glsl"),
|
||||
(desc.wgsl.as_ref().map(|a| a.as_bytes()), "wgsl"),
|
||||
for (data, kind) in [
|
||||
(
|
||||
desc.spirv.as_ref().map(|a| bytemuck::cast_slice(a)),
|
||||
DataKind::Spv,
|
||||
),
|
||||
(desc.dxil.as_deref(), DataKind::Dxil),
|
||||
(desc.hlsl.as_ref().map(|a| a.as_bytes()), DataKind::Hlsl),
|
||||
(desc.msl.as_ref().map(|a| a.as_bytes()), DataKind::Msl),
|
||||
(desc.glsl.as_ref().map(|a| a.as_bytes()), DataKind::Glsl),
|
||||
(desc.wgsl.as_ref().map(|a| a.as_bytes()), DataKind::Wgsl),
|
||||
] {
|
||||
if let Some(data) = data {
|
||||
file_names.push(trace.make_binary(ext, data));
|
||||
file_names.push(trace.make_binary(kind, data));
|
||||
}
|
||||
}
|
||||
trace.add(trace::Action::CreateShaderModulePassthrough {
|
||||
|
||||
@@ -1611,11 +1611,13 @@ impl Global {
|
||||
|
||||
#[cfg(feature = "trace")]
|
||||
if let Some(ref mut trace) = *queue.device.trace.lock() {
|
||||
let data_path = trace.make_binary("bin", data);
|
||||
use crate::device::trace::DataKind;
|
||||
let range = buffer_offset..buffer_offset + data.len() as u64;
|
||||
let data = trace.make_binary(DataKind::Bin, data);
|
||||
trace.add(Action::WriteBuffer {
|
||||
id: buffer.to_trace(),
|
||||
data: data_path,
|
||||
range: buffer_offset..buffer_offset + data.len() as u64,
|
||||
data,
|
||||
range,
|
||||
queued: true,
|
||||
});
|
||||
}
|
||||
@@ -1682,10 +1684,11 @@ impl Global {
|
||||
|
||||
#[cfg(feature = "trace")]
|
||||
if let Some(ref mut trace) = *queue.device.trace.lock() {
|
||||
let data_path = trace.make_binary("bin", data);
|
||||
use crate::device::trace::DataKind;
|
||||
let data = trace.make_binary(DataKind::Bin, data);
|
||||
trace.add(Action::WriteTexture {
|
||||
to: destination.to_trace(),
|
||||
data: data_path,
|
||||
data,
|
||||
layout: *data_layout,
|
||||
size: *size,
|
||||
});
|
||||
|
||||
@@ -272,7 +272,7 @@ pub struct Device {
|
||||
pub(crate) default_external_texture_params_buffer: ManuallyDrop<Box<dyn hal::DynBuffer>>,
|
||||
// needs to be dropped last
|
||||
#[cfg(feature = "trace")]
|
||||
pub(crate) trace: Mutex<Option<trace::Trace>>,
|
||||
pub(crate) trace: Mutex<Option<Box<dyn trace::Trace + Send + Sync + 'static>>>,
|
||||
}
|
||||
|
||||
pub(crate) enum DeferredDestroy {
|
||||
@@ -385,9 +385,41 @@ impl Device {
|
||||
}
|
||||
};
|
||||
#[cfg(feature = "trace")]
|
||||
let trace_dir_name: Option<&std::path::PathBuf> = match &desc.trace {
|
||||
let trace: Option<Box<dyn trace::Trace + Send + Sync + 'static>> = match &desc.trace {
|
||||
wgt::Trace::Off => None,
|
||||
wgt::Trace::Directory(d) => Some(d),
|
||||
wgt::Trace::Directory(dir) => match trace::DiskTrace::new(dir.clone()) {
|
||||
Ok(mut trace) => {
|
||||
trace::Trace::add(
|
||||
&mut trace,
|
||||
trace::Action::Init {
|
||||
desc: wgt::DeviceDescriptor {
|
||||
trace: wgt::Trace::Off,
|
||||
..desc.clone()
|
||||
},
|
||||
backend: adapter.backend(),
|
||||
},
|
||||
);
|
||||
Some(Box::new(trace))
|
||||
}
|
||||
Err(e) => {
|
||||
log::error!("Unable to start a trace in '{dir:?}': {e}");
|
||||
None
|
||||
}
|
||||
},
|
||||
wgt::Trace::Memory => {
|
||||
let mut trace = trace::MemoryTrace::new();
|
||||
trace::Trace::add(
|
||||
&mut trace,
|
||||
trace::Action::Init {
|
||||
desc: wgt::DeviceDescriptor {
|
||||
trace: wgt::Trace::Off,
|
||||
..desc.clone()
|
||||
},
|
||||
backend: adapter.backend(),
|
||||
},
|
||||
);
|
||||
Some(Box::new(trace))
|
||||
}
|
||||
// The enum is non_exhaustive, so we must have a fallback arm (that should be
|
||||
// unreachable in practice).
|
||||
t => {
|
||||
@@ -483,25 +515,7 @@ impl Device {
|
||||
tracker_indices: TrackerIndexAllocators::new(),
|
||||
bgl_pool: ResourcePool::new(),
|
||||
#[cfg(feature = "trace")]
|
||||
trace: Mutex::new(
|
||||
rank::DEVICE_TRACE,
|
||||
trace_dir_name.and_then(|path| match trace::Trace::new(path.clone()) {
|
||||
Ok(mut trace) => {
|
||||
trace.add(trace::Action::Init {
|
||||
desc: wgt::DeviceDescriptor {
|
||||
trace: wgt::Trace::Off,
|
||||
..desc.clone()
|
||||
},
|
||||
backend: adapter.backend(),
|
||||
});
|
||||
Some(trace)
|
||||
}
|
||||
Err(e) => {
|
||||
log::error!("Unable to start a trace in '{path:?}': {e}");
|
||||
None
|
||||
}
|
||||
}),
|
||||
),
|
||||
trace: Mutex::new(rank::DEVICE_TRACE, trace),
|
||||
alignments,
|
||||
limits: desc.required_limits.clone(),
|
||||
features: desc.required_features,
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
#[cfg(feature = "trace")]
|
||||
mod record;
|
||||
#[cfg(feature = "replay")]
|
||||
mod replay;
|
||||
|
||||
use core::{convert::Infallible, ops::Range};
|
||||
|
||||
@@ -14,11 +16,98 @@ use crate::{
|
||||
|
||||
#[cfg(feature = "trace")]
|
||||
pub use record::*;
|
||||
#[cfg(feature = "replay")]
|
||||
pub use replay::*;
|
||||
|
||||
type FileName = String;
|
||||
|
||||
pub const FILE_NAME: &str = "trace.ron";
|
||||
|
||||
#[derive(Debug)]
|
||||
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
|
||||
pub enum Data {
|
||||
File(FileName),
|
||||
String(DataKind, String),
|
||||
Binary(DataKind, Vec<u8>),
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
|
||||
#[cfg_attr(
|
||||
feature = "serde",
|
||||
derive(serde::Serialize, serde::Deserialize),
|
||||
serde(rename_all = "lowercase")
|
||||
)]
|
||||
pub enum DataKind {
|
||||
Bin,
|
||||
Wgsl,
|
||||
|
||||
/// IR of Naga module, serialized in RON format
|
||||
Ron,
|
||||
Spv,
|
||||
Dxil,
|
||||
Hlsl,
|
||||
Msl,
|
||||
Glsl,
|
||||
}
|
||||
|
||||
impl core::fmt::Display for DataKind {
|
||||
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
|
||||
let s = match self {
|
||||
DataKind::Bin => "bin",
|
||||
DataKind::Wgsl => "wgsl",
|
||||
DataKind::Ron => "ron",
|
||||
DataKind::Spv => "spv",
|
||||
DataKind::Dxil => "dxil",
|
||||
DataKind::Hlsl => "hlsl",
|
||||
DataKind::Msl => "msl",
|
||||
DataKind::Glsl => "glsl",
|
||||
};
|
||||
write!(f, "{s}")
|
||||
}
|
||||
}
|
||||
|
||||
impl DataKind {
|
||||
#[cfg(feature = "replay")]
|
||||
fn is_string(&self) -> bool {
|
||||
match *self {
|
||||
DataKind::Wgsl | DataKind::Ron | DataKind::Hlsl | DataKind::Msl | DataKind::Glsl => {
|
||||
true
|
||||
}
|
||||
DataKind::Bin | DataKind::Spv | DataKind::Dxil => false,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Data {
|
||||
pub fn kind(&self) -> DataKind {
|
||||
match self {
|
||||
Data::File(file) => {
|
||||
if file.ends_with(".bin") {
|
||||
DataKind::Bin
|
||||
} else if file.ends_with(".wgsl") {
|
||||
DataKind::Wgsl
|
||||
} else if file.ends_with(".ron") {
|
||||
DataKind::Ron
|
||||
} else if file.ends_with(".spv") {
|
||||
DataKind::Spv
|
||||
} else if file.ends_with(".dxil") {
|
||||
DataKind::Dxil
|
||||
} else if file.ends_with(".hlsl") {
|
||||
DataKind::Hlsl
|
||||
} else if file.ends_with(".msl") {
|
||||
DataKind::Msl
|
||||
} else if file.ends_with(".glsl") {
|
||||
DataKind::Glsl
|
||||
} else {
|
||||
panic!("unknown data file extension: {file}");
|
||||
}
|
||||
}
|
||||
Data::String(kind, _) => *kind,
|
||||
Data::Binary(kind, _) => *kind,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[allow(clippy::large_enum_variant)]
|
||||
#[derive(Debug)]
|
||||
#[apply(serde_object_reference_struct)]
|
||||
@@ -79,11 +168,11 @@ pub enum Action<'a, R: ReferenceType> {
|
||||
CreateShaderModule {
|
||||
id: PointerId<markers::ShaderModule>,
|
||||
desc: crate::pipeline::ShaderModuleDescriptor<'a>,
|
||||
data: FileName,
|
||||
data: Data,
|
||||
},
|
||||
CreateShaderModulePassthrough {
|
||||
id: PointerId<markers::ShaderModule>,
|
||||
data: Vec<FileName>,
|
||||
data: Vec<Data>,
|
||||
|
||||
entry_point: String,
|
||||
label: crate::Label<'a>,
|
||||
@@ -119,13 +208,13 @@ pub enum Action<'a, R: ReferenceType> {
|
||||
DestroyQuerySet(PointerId<markers::QuerySet>),
|
||||
WriteBuffer {
|
||||
id: R::Buffer,
|
||||
data: FileName,
|
||||
data: Data,
|
||||
range: Range<wgt::BufferAddress>,
|
||||
queued: bool,
|
||||
},
|
||||
WriteTexture {
|
||||
to: wgt::TexelCopyTextureInfo<R::Texture>,
|
||||
data: FileName,
|
||||
data: Data,
|
||||
layout: wgt::TexelCopyBufferLayout,
|
||||
size: wgt::Extent3d,
|
||||
},
|
||||
|
||||
@@ -1,9 +1,4 @@
|
||||
use alloc::{
|
||||
borrow::Cow,
|
||||
string::{String, ToString},
|
||||
sync::Arc,
|
||||
vec::Vec,
|
||||
};
|
||||
use alloc::{borrow::Cow, string::ToString, sync::Arc, vec::Vec};
|
||||
use core::convert::Infallible;
|
||||
use std::io::Write as _;
|
||||
|
||||
@@ -13,6 +8,7 @@ use crate::{
|
||||
BasePass, ColorAttachments, Command, ComputeCommand, PointerReferences, RenderCommand,
|
||||
RenderPassColorAttachment, ResolvedRenderPassDepthStencilAttachment,
|
||||
},
|
||||
device::trace::{Data, DataKind},
|
||||
id::{markers, PointerId},
|
||||
storage::StorageItem,
|
||||
};
|
||||
@@ -43,15 +39,25 @@ pub(crate) fn new_render_bundle_encoder_descriptor(
|
||||
}
|
||||
}
|
||||
|
||||
pub trait Trace: Send + Sync {
|
||||
fn make_binary(&mut self, kind: DataKind, data: &[u8]) -> Data;
|
||||
|
||||
fn make_string(&mut self, kind: DataKind, data: &str) -> Data;
|
||||
|
||||
fn add(&mut self, action: Action<'_, PointerReferences>)
|
||||
where
|
||||
for<'a> Action<'a, PointerReferences>: serde::Serialize;
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct Trace {
|
||||
pub struct DiskTrace {
|
||||
path: std::path::PathBuf,
|
||||
file: std::fs::File,
|
||||
config: ron::ser::PrettyConfig,
|
||||
binary_id: usize,
|
||||
data_id: usize,
|
||||
}
|
||||
|
||||
impl Trace {
|
||||
impl DiskTrace {
|
||||
pub fn new(path: std::path::PathBuf) -> Result<Self, std::io::Error> {
|
||||
log::debug!("Tracing into '{path:?}'");
|
||||
let mut file = std::fs::File::create(path.join(FILE_NAME))?;
|
||||
@@ -60,18 +66,32 @@ impl Trace {
|
||||
path,
|
||||
file,
|
||||
config: ron::ser::PrettyConfig::default(),
|
||||
binary_id: 0,
|
||||
data_id: 0,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
pub fn make_binary(&mut self, kind: &str, data: &[u8]) -> String {
|
||||
self.binary_id += 1;
|
||||
let name = std::format!("data{}.{}", self.binary_id, kind);
|
||||
impl Trace for DiskTrace {
|
||||
/// Store `[u8]` data in the trace.
|
||||
///
|
||||
/// Using a string `kind` is probably a bug, but should work as long as the
|
||||
/// data is UTF-8.
|
||||
fn make_binary(&mut self, kind: DataKind, data: &[u8]) -> Data {
|
||||
self.data_id += 1;
|
||||
let name = std::format!("data{}.{}", self.data_id, kind);
|
||||
let _ = std::fs::write(self.path.join(&name), data);
|
||||
name
|
||||
Data::File(name)
|
||||
}
|
||||
|
||||
pub(crate) fn add(&mut self, action: Action<'_, PointerReferences>)
|
||||
/// Store `str` data in the trace.
|
||||
///
|
||||
/// Using a binary `kind` is fine, but it's not clear why not use
|
||||
/// `make_binary` instead.
|
||||
fn make_string(&mut self, kind: DataKind, data: &str) -> Data {
|
||||
self.make_binary(kind, data.as_bytes())
|
||||
}
|
||||
|
||||
fn add(&mut self, action: Action<'_, PointerReferences>)
|
||||
where
|
||||
for<'a> Action<'a, PointerReferences>: serde::Serialize,
|
||||
{
|
||||
@@ -86,12 +106,52 @@ impl Trace {
|
||||
}
|
||||
}
|
||||
|
||||
impl Drop for Trace {
|
||||
impl Drop for DiskTrace {
|
||||
fn drop(&mut self) {
|
||||
let _ = self.file.write_all(b"]");
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Default)]
|
||||
pub struct MemoryTrace {
|
||||
actions: Vec<Action<'static, PointerReferences>>,
|
||||
}
|
||||
|
||||
impl MemoryTrace {
|
||||
pub fn new() -> Self {
|
||||
Self::default()
|
||||
}
|
||||
|
||||
pub fn actions(&self) -> &[Action<'static, PointerReferences>] {
|
||||
&self.actions
|
||||
}
|
||||
}
|
||||
|
||||
impl Trace for MemoryTrace {
|
||||
/// Store `[u8]` data in the trace.
|
||||
///
|
||||
/// Using a string `kind` is probably a bug, but should work as long as the
|
||||
/// data is UTF-8.
|
||||
fn make_binary(&mut self, kind: DataKind, data: &[u8]) -> Data {
|
||||
Data::Binary(kind, data.to_vec())
|
||||
}
|
||||
|
||||
/// Store `str` data in the trace.
|
||||
///
|
||||
/// Using a binary `kind` is fine, but it's not clear why not use
|
||||
/// `make_binary` instead.
|
||||
fn make_string(&mut self, kind: DataKind, data: &str) -> Data {
|
||||
Data::String(kind, data.to_string())
|
||||
}
|
||||
|
||||
fn add(&mut self, action: Action<'_, PointerReferences>)
|
||||
where
|
||||
for<'a> Action<'a, PointerReferences>: serde::Serialize,
|
||||
{
|
||||
self.actions.push(action_to_owned(action))
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) trait IntoTrace {
|
||||
type Output;
|
||||
fn into_trace(self) -> Self::Output;
|
||||
@@ -565,9 +625,11 @@ impl IntoTrace for ArcRenderCommand {
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> IntoTrace for crate::binding_model::ResolvedPipelineLayoutDescriptor<'a> {
|
||||
type Output =
|
||||
crate::binding_model::PipelineLayoutDescriptor<'a, PointerId<markers::BindGroupLayout>>;
|
||||
impl IntoTrace for crate::binding_model::ResolvedPipelineLayoutDescriptor<'_> {
|
||||
type Output = crate::binding_model::PipelineLayoutDescriptor<
|
||||
'static,
|
||||
PointerId<markers::BindGroupLayout>,
|
||||
>;
|
||||
fn into_trace(self) -> Self::Output {
|
||||
crate::binding_model::PipelineLayoutDescriptor {
|
||||
label: self.label.map(|l| Cow::Owned(l.into_owned())),
|
||||
@@ -756,3 +818,100 @@ impl<T: IntoTrace> IntoTrace for Option<T> {
|
||||
self.map(|v| v.into_trace())
|
||||
}
|
||||
}
|
||||
|
||||
/// For selected `Action`s (mostly actions containing only owned data), return a
|
||||
/// copy with `'static` lifetime.
|
||||
///
|
||||
/// This is used for in-memory tracing.
|
||||
///
|
||||
/// # Panics
|
||||
///
|
||||
/// If `action` is not supported for in-memory tracing (likely because it
|
||||
/// contains borrowed data).
|
||||
fn action_to_owned(action: Action<'_, PointerReferences>) -> Action<'static, PointerReferences> {
|
||||
use Action as A;
|
||||
match action {
|
||||
A::ConfigureSurface(surface, config) => A::ConfigureSurface(surface, config),
|
||||
A::CreateBuffer(buffer, desc) => A::CreateBuffer(
|
||||
buffer,
|
||||
wgt::BufferDescriptor {
|
||||
label: desc.label.map(|l| Cow::Owned(l.into_owned())),
|
||||
size: desc.size,
|
||||
usage: desc.usage,
|
||||
mapped_at_creation: desc.mapped_at_creation,
|
||||
},
|
||||
),
|
||||
A::FreeBuffer(buffer) => A::FreeBuffer(buffer),
|
||||
A::DestroyBuffer(buffer) => A::DestroyBuffer(buffer),
|
||||
A::FreeTexture(texture) => A::FreeTexture(texture),
|
||||
A::DestroyTexture(texture) => A::DestroyTexture(texture),
|
||||
A::DestroyTextureView(texture_view) => A::DestroyTextureView(texture_view),
|
||||
A::FreeExternalTexture(external_texture) => A::FreeExternalTexture(external_texture),
|
||||
A::DestroyExternalTexture(external_texture) => A::DestroyExternalTexture(external_texture),
|
||||
A::DestroySampler(sampler) => A::DestroySampler(sampler),
|
||||
A::GetSurfaceTexture { id, parent } => A::GetSurfaceTexture { id, parent },
|
||||
A::Present(surface) => A::Present(surface),
|
||||
A::DiscardSurfaceTexture(surface) => A::DiscardSurfaceTexture(surface),
|
||||
A::DestroyBindGroupLayout(layout) => A::DestroyBindGroupLayout(layout),
|
||||
A::DestroyPipelineLayout(layout) => A::DestroyPipelineLayout(layout),
|
||||
A::DestroyBindGroup(bind_group) => A::DestroyBindGroup(bind_group),
|
||||
A::DestroyShaderModule(shader_module) => A::DestroyShaderModule(shader_module),
|
||||
A::DestroyComputePipeline(pipeline) => A::DestroyComputePipeline(pipeline),
|
||||
A::DestroyRenderPipeline(pipeline) => A::DestroyRenderPipeline(pipeline),
|
||||
A::DestroyPipelineCache(cache) => A::DestroyPipelineCache(cache),
|
||||
A::DestroyRenderBundle(render_bundle) => A::DestroyRenderBundle(render_bundle),
|
||||
A::DestroyQuerySet(query_set) => A::DestroyQuerySet(query_set),
|
||||
A::WriteBuffer {
|
||||
id,
|
||||
data,
|
||||
range,
|
||||
queued,
|
||||
} => A::WriteBuffer {
|
||||
id,
|
||||
data,
|
||||
range,
|
||||
queued,
|
||||
},
|
||||
A::WriteTexture {
|
||||
to,
|
||||
data,
|
||||
layout,
|
||||
size,
|
||||
} => A::WriteTexture {
|
||||
to,
|
||||
data,
|
||||
layout,
|
||||
size,
|
||||
},
|
||||
A::Submit(index, commands) => A::Submit(index, commands),
|
||||
A::FailedCommands {
|
||||
commands,
|
||||
failed_at_submit,
|
||||
error,
|
||||
} => A::FailedCommands {
|
||||
commands,
|
||||
failed_at_submit,
|
||||
error,
|
||||
},
|
||||
A::DestroyBlas(blas) => A::DestroyBlas(blas),
|
||||
A::DestroyTlas(tlas) => A::DestroyTlas(tlas),
|
||||
|
||||
A::Init { .. }
|
||||
| A::CreateTexture(..)
|
||||
| A::CreateTextureView { .. }
|
||||
| A::CreateExternalTexture { .. }
|
||||
| A::CreateSampler(..)
|
||||
| A::CreateBindGroupLayout(..)
|
||||
| A::CreatePipelineLayout(..)
|
||||
| A::CreateBindGroup(..)
|
||||
| A::CreateShaderModule { .. }
|
||||
| A::CreateShaderModulePassthrough { .. }
|
||||
| A::CreateComputePipeline { .. }
|
||||
| A::CreateGeneralRenderPipeline { .. }
|
||||
| A::CreatePipelineCache { .. }
|
||||
| A::CreateRenderBundle { .. }
|
||||
| A::CreateQuerySet { .. }
|
||||
| A::CreateBlas { .. }
|
||||
| A::CreateTlas { .. } => panic!("Unsupported action for tracing: {action:?}"),
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
use alloc::borrow::Cow;
|
||||
|
||||
use super::Data;
|
||||
|
||||
pub trait DataLoader {
|
||||
fn load<'loader, 'data>(&'loader self, data: &'data Data) -> Cow<'data, [u8]>;
|
||||
fn load_utf8<'loader, 'data>(&'loader self, data: &'data Data) -> Cow<'data, str>;
|
||||
}
|
||||
|
||||
#[cfg(feature = "std")]
|
||||
pub struct DiskTraceLoader<'a>(&'a std::path::Path);
|
||||
|
||||
#[cfg(feature = "std")]
|
||||
impl<'a> DiskTraceLoader<'a> {
|
||||
pub fn new(path: &'a std::path::Path) -> DiskTraceLoader<'a> {
|
||||
DiskTraceLoader(path)
|
||||
}
|
||||
}
|
||||
|
||||
impl DataLoader for DiskTraceLoader<'_> {
|
||||
fn load<'loader, 'data>(&'loader self, data: &'data Data) -> Cow<'data, [u8]> {
|
||||
match data {
|
||||
Data::File(file) => {
|
||||
Cow::from(std::fs::read(self.0.join(file)).expect("Failed to read data file"))
|
||||
}
|
||||
Data::String(_, s) => Cow::from(s.as_bytes()),
|
||||
Data::Binary(_, b) => Cow::from(b),
|
||||
}
|
||||
}
|
||||
|
||||
/// Load UTF-8 string data
|
||||
///
|
||||
/// # Panics
|
||||
///
|
||||
/// If the data kind is not a string format or the data is not valid UTF-8
|
||||
fn load_utf8<'loader, 'data>(&'loader self, data: &'data Data) -> Cow<'data, str> {
|
||||
match data {
|
||||
Data::File(file) => Cow::from(
|
||||
std::fs::read_to_string(self.0.join(file)).expect("Failed to read data file"),
|
||||
),
|
||||
Data::String(kind, s) => {
|
||||
assert!(kind.is_string(), "{kind:?} cannot be loaded as a string");
|
||||
Cow::from(s)
|
||||
}
|
||||
Data::Binary(kind, b) => {
|
||||
assert!(kind.is_string(), "{kind:?} cannot be loaded as a string");
|
||||
Cow::from(core::str::from_utf8(b).unwrap())
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -815,9 +815,9 @@ impl Buffer {
|
||||
BufferMapState::Init { staging_buffer } => {
|
||||
#[cfg(feature = "trace")]
|
||||
if let Some(ref mut trace) = *device.trace.lock() {
|
||||
use crate::device::trace::IntoTrace;
|
||||
use crate::device::trace::{DataKind, IntoTrace};
|
||||
|
||||
let data = trace.make_binary("bin", staging_buffer.get_data());
|
||||
let data = trace.make_binary(DataKind::Bin, staging_buffer.get_data());
|
||||
trace.add(trace::Action::WriteBuffer {
|
||||
id: self.to_trace(),
|
||||
data,
|
||||
@@ -878,10 +878,10 @@ impl Buffer {
|
||||
if host == HostMap::Write {
|
||||
#[cfg(feature = "trace")]
|
||||
if let Some(ref mut trace) = *device.trace.lock() {
|
||||
use crate::device::trace::IntoTrace;
|
||||
use crate::device::trace::{DataKind, IntoTrace};
|
||||
|
||||
let size = range.end - range.start;
|
||||
let data = trace.make_binary("bin", unsafe {
|
||||
let data = trace.make_binary(DataKind::Bin, unsafe {
|
||||
core::slice::from_raw_parts(mapping.ptr.as_ptr(), size as usize)
|
||||
});
|
||||
trace.add(trace::Action::WriteBuffer {
|
||||
|
||||
@@ -95,9 +95,13 @@ pub enum Trace {
|
||||
#[default]
|
||||
Off,
|
||||
|
||||
/// Tracing enabled.
|
||||
/// Write trace to disk.
|
||||
#[cfg(feature = "trace")]
|
||||
// This must be owned rather than `&'a Path`, because if it were that, then the lifetime
|
||||
// parameter would be unused when the "trace" feature is disabled, which is prohibited.
|
||||
Directory(std::path::PathBuf),
|
||||
|
||||
/// Store trace in memory.
|
||||
#[cfg(feature = "trace")]
|
||||
Memory,
|
||||
}
|
||||
Reference in new issue
Block a user