extract the d3d descriptors and cover the sweep and guard paths

This commit is contained in:
2026-09-21 02:10:52 +03:00
parent 930d67fdd6
commit c9ee532bd3
4 changed files with 170 additions and 94 deletions
+32
View File
@@ -360,3 +360,35 @@ fn extract_ffmpeg_from_zip(archive_path: &Path, dest: &Path) -> Result<()> {
} }
bail!("ffmpeg.exe not found inside the zip archive"); bail!("ffmpeg.exe not found inside the zip archive");
} }
#[cfg(test)]
mod tests {
use super::*;
/// Stands in for the body of extract_ffmpeg_from_7z: it builds a scratch
/// directory, arms the guard, then leaves through the `?` the way a failed
/// extraction does.
fn fails_with_guard_alive(scratch: &Path) -> Result<()> {
fs::create_dir_all(scratch).context("create scratch")?;
let _guard = DirGuard(scratch.to_path_buf());
fs::write(scratch.join("ffmpeg.exe"), b"x").context("write payload")?;
bail!("deliberate extraction failure")
}
/// The scratch tree used to be removed only on the success path, so a failed
/// bootstrap left the whole unpacked ffmpeg build sitting in temp/.
#[test]
fn dir_guard_removes_scratch_even_on_early_exit() {
let dir = std::env::temp_dir().join(format!("bytesize-guard-test-{}", std::process::id()));
let scratch = dir.join("temp").join("ffmpeg-extract");
drop(fs::remove_dir_all(&dir));
assert!(fails_with_guard_alive(&scratch).is_err());
assert!(
!scratch.exists(),
"scratch dir should be gone after the error return"
);
drop(fs::remove_dir_all(&dir));
}
}
+122 -93
View File
@@ -45,6 +45,16 @@ const MAX_ENCODE_WIDTH: u32 = 3840;
const AUTO_ENCODE_WIDTH: u32 = 1920; const AUTO_ENCODE_WIDTH: u32 = 1920;
const FALLBACK_REFRESH_HZ: u32 = 60; const FALLBACK_REFRESH_HZ: u32 = 60;
/// Fullscreen-triangle vertices, shared by the vertex-buffer description and the
/// subresource that points at them. Module scope so the returned subresource can
/// reference it without borrowing a stack local.
static VERTS: [f32; 12] = [
// pos.x pos.y u v
-1.0, -1.0, 0.0, 1.0, // bottom-left
3.0, -1.0, 2.0, 1.0, // bottom-right (off-screen, completes the triangle)
-1.0, 3.0, 0.0, -1.0, // top-left (off-screen)
];
/// The cap to apply to the encode width in pixels. /// The cap to apply to the encode width in pixels.
/// Auto keeps the historic 1920 cap so nothing changes for existing users; /// Auto keeps the historic 1920 cap so nothing changes for existing users;
/// Full records at native res up to `MAX_ENCODE_WIDTH` (3840). /// Full records at native res up to `MAX_ENCODE_WIDTH` (3840).
@@ -95,13 +105,112 @@ struct TonemapPipeline {
height: u32, height: u32,
} }
// One D3D11 object per descriptor, in the order the API documents them; the // The D3D11 descriptors are pure data - no device, no unsafe - so they live
// length is the API's, not ours, and splitting it would scatter a single // outside the pipeline constructor. Pulling them out keeps that constructor to
// initialization sequence across helpers that each need half the locals. // the create-and-store sequence and lets each descriptor read on its own.
#[allow( fn new_sampler_desc() -> D3D11_SAMPLER_DESC {
clippy::too_many_lines, // Point-clamp sampler - 1:1 screen-space sampling of the captured frame.
reason = "linear D3D11 resource setup: each step needs the previous object in scope" D3D11_SAMPLER_DESC {
)] Filter: D3D11_FILTER_MIN_MAG_MIP_POINT,
AddressU: D3D11_TEXTURE_ADDRESS_CLAMP,
AddressV: D3D11_TEXTURE_ADDRESS_CLAMP,
AddressW: D3D11_TEXTURE_ADDRESS_CLAMP,
MipLODBias: 0.0,
MaxAnisotropy: 0,
ComparisonFunc: windows::Win32::Graphics::Direct3D11::D3D11_COMPARISON_NEVER,
BorderColor: [0.0; 4],
MinLOD: 0.0,
MaxLOD: 0.0,
}
}
fn new_rasterizer_desc() -> D3D11_RASTERIZER_DESC {
// No culling. The default rasterizer state culls back faces, which silently
// killed the fullscreen triangle (the winding vs FrontCounterClockwise
// interaction culls the whole tri). CULL_NONE removes that variable.
D3D11_RASTERIZER_DESC {
FillMode: D3D11_FILL_SOLID,
CullMode: D3D11_CULL_NONE,
FrontCounterClockwise: windows::core::BOOL::default(),
DepthBias: 0,
DepthBiasClamp: 0.0,
SlopeScaledDepthBias: 0.0,
DepthClipEnable: windows::core::BOOL::default(),
ScissorEnable: windows::core::BOOL(1),
MultisampleEnable: windows::core::BOOL::default(),
AntialiasedLineEnable: windows::core::BOOL::default(),
}
}
/// SDR render target (BGRA8). The shader writes gamma-encoded bt709 here.
fn new_render_target_desc(width: u32, height: u32) -> D3D11_TEXTURE2D_DESC {
D3D11_TEXTURE2D_DESC {
Width: width,
Height: height,
MipLevels: 1,
ArraySize: 1,
Format: DXGI_FORMAT_B8G8R8A8_UNORM,
SampleDesc: DXGI_SAMPLE_DESC { Count: 1, Quality: 0 },
Usage: D3D11_USAGE_DEFAULT,
BindFlags: (D3D11_BIND_RENDER_TARGET.0 | D3D11_BIND_SHADER_RESOURCE.0) as u32,
CPUAccessFlags: 0,
MiscFlags: 0,
}
}
fn new_render_target_view_desc() -> D3D11_RENDER_TARGET_VIEW_DESC {
D3D11_RENDER_TARGET_VIEW_DESC {
Format: DXGI_FORMAT_B8G8R8A8_UNORM,
ViewDimension: D3D11_RTV_DIMENSION_TEXTURE2D,
Anonymous: windows::Win32::Graphics::Direct3D11::D3D11_RENDER_TARGET_VIEW_DESC_0 {
Texture2D: D3D11_TEX2D_RTV { MipSlice: 0 },
},
}
}
/// Staging texture for CPU readback. Same format as the render target.
fn new_staging_desc(width: u32, height: u32) -> D3D11_TEXTURE2D_DESC {
D3D11_TEXTURE2D_DESC {
Width: width,
Height: height,
MipLevels: 1,
ArraySize: 1,
Format: DXGI_FORMAT_B8G8R8A8_UNORM,
SampleDesc: DXGI_SAMPLE_DESC { Count: 1, Quality: 0 },
Usage: D3D11_USAGE_STAGING,
BindFlags: 0,
CPUAccessFlags: D3D11_CPU_ACCESS_READ.0 as u32,
MiscFlags: 0,
}
}
/// 3-vertex buffer for a fullscreen triangle. Each vertex is pos.xy + uv.xy
/// (4 floats = 16 bytes). The triangle covers clip space (-1..1) with UV 0..1.
/// Winding is clockwise for the default D3D11 front-face setting.
///
/// Returns the buffer description paired with the subresource that points at
/// the vertex data; both have to outlive the `CreateBuffer` call that consumes
/// them.
fn new_vertex_buffer() -> (D3D11_BUFFER_DESC, D3D11_SUBRESOURCE_DATA) {
(
D3D11_BUFFER_DESC {
// VERTS is a fixed 12-float array, so its size is a compile-time
// constant well inside u32.
ByteWidth: u32::try_from(size_of_val(&VERTS)).unwrap_or(0),
Usage: D3D11_USAGE_IMMUTABLE,
BindFlags: D3D11_BIND_VERTEX_BUFFER.0 as u32,
CPUAccessFlags: 0,
MiscFlags: 0,
StructureByteStride: 0,
},
D3D11_SUBRESOURCE_DATA {
pSysMem: VERTS.as_ptr().cast::<std::ffi::c_void>(),
SysMemPitch: 0,
SysMemSlicePitch: 0,
},
)
}
impl TonemapPipeline { impl TonemapPipeline {
fn new(device: &ID3D11Device, width: u32, height: u32) -> Result<Self> { fn new(device: &ID3D11Device, width: u32, height: u32) -> Result<Self> {
// Vertex shader: fullscreen-triangle pass-through (no vertex buffer). // Vertex shader: fullscreen-triangle pass-through (no vertex buffer).
@@ -149,30 +258,7 @@ impl TonemapPipeline {
} }
let input_layout = input_layout.ok_or_else(|| anyhow!("CreateInputLayout returned null"))?; let input_layout = input_layout.ok_or_else(|| anyhow!("CreateInputLayout returned null"))?;
// 3-vertex buffer for a fullscreen triangle. Each vertex is pos.xy + uv.xy let (vb_desc, vb_init) = new_vertex_buffer();
// (4 floats = 16 bytes). Triangle covers clip space (-1..1) with UV 0..1.
// Winding is clockwise for default D3D11 front-face (FrontCounterClockwise=false).
let verts: [f32; 12] = [
// pos.x pos.y u v
-1.0, -1.0, 0.0, 1.0, // bottom-left
3.0, -1.0, 2.0, 1.0, // bottom-right (off-screen, completes the triangle)
-1.0, 3.0, 0.0, -1.0, // top-left (off-screen)
];
let vb_desc = D3D11_BUFFER_DESC {
// verts is a 12-float array, so the size is a compile-time constant
// well inside u32.
ByteWidth: u32::try_from(size_of_val(&verts)).unwrap_or(0),
Usage: D3D11_USAGE_IMMUTABLE,
BindFlags: D3D11_BIND_VERTEX_BUFFER.0 as u32,
CPUAccessFlags: 0,
MiscFlags: 0,
StructureByteStride: 0,
};
let vb_init = D3D11_SUBRESOURCE_DATA {
pSysMem: verts.as_ptr().cast::<std::ffi::c_void>(),
SysMemPitch: 0,
SysMemSlicePitch: 0,
};
let mut vertex_buffer = None; let mut vertex_buffer = None;
unsafe { unsafe {
// SAFETY: both descriptions are stack locals that outlive the call, // SAFETY: both descriptions are stack locals that outlive the call,
@@ -189,19 +275,7 @@ impl TonemapPipeline {
} }
let ps = ps.ok_or_else(|| anyhow!("CreatePixelShader returned null"))?; let ps = ps.ok_or_else(|| anyhow!("CreatePixelShader returned null"))?;
// Point-clamp sampler - 1:1 screen-space sampling of the captured frame. let sampler_desc = new_sampler_desc();
let sampler_desc = D3D11_SAMPLER_DESC {
Filter: D3D11_FILTER_MIN_MAG_MIP_POINT,
AddressU: D3D11_TEXTURE_ADDRESS_CLAMP,
AddressV: D3D11_TEXTURE_ADDRESS_CLAMP,
AddressW: D3D11_TEXTURE_ADDRESS_CLAMP,
MipLODBias: 0.0,
MaxAnisotropy: 0,
ComparisonFunc: windows::Win32::Graphics::Direct3D11::D3D11_COMPARISON_NEVER,
BorderColor: [0.0; 4],
MinLOD: 0.0,
MaxLOD: 0.0,
};
let mut sampler = None; let mut sampler = None;
unsafe { unsafe {
// SAFETY: sampler_desc is a fully-initialized stack local and the // SAFETY: sampler_desc is a fully-initialized stack local and the
@@ -210,22 +284,7 @@ impl TonemapPipeline {
} }
let sampler = sampler.ok_or_else(|| anyhow!("CreateSamplerState returned null"))?; let sampler = sampler.ok_or_else(|| anyhow!("CreateSamplerState returned null"))?;
// Rasterizer with no culling. The default rasterizer state culls let rasterizer_desc = new_rasterizer_desc();
// back faces, which silently killed the fullscreen triangle (the
// winding vs FrontCounterClockwise interaction culls the whole tri).
// CULL_NONE removes that variable entirely.
let rasterizer_desc = D3D11_RASTERIZER_DESC {
FillMode: D3D11_FILL_SOLID,
CullMode: D3D11_CULL_NONE,
FrontCounterClockwise: windows::core::BOOL::default(),
DepthBias: 0,
DepthBiasClamp: 0.0,
SlopeScaledDepthBias: 0.0,
DepthClipEnable: windows::core::BOOL::default(),
ScissorEnable: windows::core::BOOL(1),
MultisampleEnable: windows::core::BOOL::default(),
AntialiasedLineEnable: windows::core::BOOL::default(),
};
let mut rasterizer = None; let mut rasterizer = None;
unsafe { unsafe {
// SAFETY: rasterizer_desc is a fully-initialized stack local and the // SAFETY: rasterizer_desc is a fully-initialized stack local and the
@@ -235,19 +294,7 @@ impl TonemapPipeline {
let rasterizer = let rasterizer =
rasterizer.ok_or_else(|| anyhow!("CreateRasterizerState returned null"))?; rasterizer.ok_or_else(|| anyhow!("CreateRasterizerState returned null"))?;
// SDR render target (BGRA8). The shader writes gamma-encoded bt709 here. let rt_desc = new_render_target_desc(width, height);
let rt_desc = D3D11_TEXTURE2D_DESC {
Width: width,
Height: height,
MipLevels: 1,
ArraySize: 1,
Format: DXGI_FORMAT_B8G8R8A8_UNORM,
SampleDesc: DXGI_SAMPLE_DESC { Count: 1, Quality: 0 },
Usage: D3D11_USAGE_DEFAULT,
BindFlags: (D3D11_BIND_RENDER_TARGET.0 | D3D11_BIND_SHADER_RESOURCE.0) as u32,
CPUAccessFlags: 0,
MiscFlags: 0,
};
let mut render_target = None; let mut render_target = None;
unsafe { unsafe {
// SAFETY: rt_desc is a fully-initialized stack local and the // SAFETY: rt_desc is a fully-initialized stack local and the
@@ -256,13 +303,7 @@ impl TonemapPipeline {
} }
let render_target = render_target.ok_or_else(|| anyhow!("CreateTexture2D rt returned null"))?; let render_target = render_target.ok_or_else(|| anyhow!("CreateTexture2D rt returned null"))?;
let view_desc = D3D11_RENDER_TARGET_VIEW_DESC { let view_desc = new_render_target_view_desc();
Format: DXGI_FORMAT_B8G8R8A8_UNORM,
ViewDimension: D3D11_RTV_DIMENSION_TEXTURE2D,
Anonymous: windows::Win32::Graphics::Direct3D11::D3D11_RENDER_TARGET_VIEW_DESC_0 {
Texture2D: D3D11_TEX2D_RTV { MipSlice: 0 },
},
};
let mut rtv = None; let mut rtv = None;
unsafe { unsafe {
// SAFETY: render_target was just created on this device and // SAFETY: render_target was just created on this device and
@@ -271,19 +312,7 @@ impl TonemapPipeline {
} }
let rtv = rtv.ok_or_else(|| anyhow!("CreateRenderTargetView returned null"))?; let rtv = rtv.ok_or_else(|| anyhow!("CreateRenderTargetView returned null"))?;
// Staging texture for CPU readback. Same format as the RT. let staging_desc = new_staging_desc(width, height);
let staging_desc = D3D11_TEXTURE2D_DESC {
Width: width,
Height: height,
MipLevels: 1,
ArraySize: 1,
Format: DXGI_FORMAT_B8G8R8A8_UNORM,
SampleDesc: DXGI_SAMPLE_DESC { Count: 1, Quality: 0 },
Usage: D3D11_USAGE_STAGING,
BindFlags: 0,
CPUAccessFlags: D3D11_CPU_ACCESS_READ.0 as u32,
MiscFlags: 0,
};
let mut staging = None; let mut staging = None;
unsafe { unsafe {
// SAFETY: staging_desc is a fully-initialized stack local and the // SAFETY: staging_desc is a fully-initialized stack local and the
+13
View File
@@ -1089,6 +1089,19 @@ mod tests {
} }
} }
/// Windows keeps whatever case a name was created with, so the temp sweep
/// matches extensions case-insensitively. It used to compare with
/// ends_with(".mp4"), which silently missed an upper-case recording.
#[test]
fn extension_match_ignores_case() {
assert!(has_ext("segment-001.mp4", "mp4"));
assert!(has_ext("SEGMENT-001.MP4", "mp4"));
assert!(has_ext("Segment-001.Mp4", "mp4"));
assert!(!has_ext("segment-001.mkv", "mp4"));
assert!(!has_ext("segment-001", "mp4"));
assert!(!has_ext("notsegment.mp4.bak", "mp4"));
}
/// The pcm head-padding step writes a sibling `*.pcm.padtmp` next to the /// The pcm head-padding step writes a sibling `*.pcm.padtmp` next to the
/// concatenated audio. It used to fall outside every sweep pattern, so a /// concatenated audio. It used to fall outside every sweep pattern, so a
/// crash mid-pad left a file the size of the whole audio track in temp/. /// crash mid-pad left a file the size of the whole audio track in temp/.
+3 -1
View File
@@ -63,7 +63,9 @@ Push-Location $root
try { try {
$null = Invoke-Step 'svelte-check (frontend types)' { npm run check } $null = Invoke-Step 'svelte-check (frontend types)' { npm run check }
if ($failures.Count -eq 0) { $null = Invoke-Step 'cargo check' { cargo check --manifest-path src-tauri/Cargo.toml } } if ($failures.Count -eq 0) { $null = Invoke-Step 'cargo check' { cargo check --manifest-path src-tauri/Cargo.toml } }
if ($failures.Count -eq 0) { $null = Invoke-Step 'cargo clippy' { cargo clippy --manifest-path src-tauri/Cargo.toml } } # -D warnings matters: clippy exits 0 when it only has warnings, so without
# this the gate passes on a warning-laden build.
if ($failures.Count -eq 0) { $null = Invoke-Step 'cargo clippy' { cargo clippy --manifest-path src-tauri/Cargo.toml -- -D warnings } }
if ($failures.Count -eq 0) { $null = Invoke-Step 'cargo test' { cargo test --manifest-path src-tauri/Cargo.toml } } if ($failures.Count -eq 0) { $null = Invoke-Step 'cargo test' { cargo test --manifest-path src-tauri/Cargo.toml } }
} }
finally { finally {