* claim: #5722 * fix(gpu): unwind preparation error scopes on cancellation (#5722) Move fallible CPU HDR packing before GPU scopes and drain the remaining scope stack in reverse order before propagating denoiser/resource allocation failures. Native regressions cover cancellation and allocation-error cleanup plus subsequent scope reuse. Exact candidate Vulkan-required GPU suite: 101 passed, 0 failed, 3 existing ignored; strict GPU all-target Clippy and repository assets/layers/lists/container checks pass. No driver exception, shader transport or test limit is suppressed.
This commit is contained in:
1 parent
8cef71d2d2
commit
04acdc251f
2 files changed
+134
-72
No files matched your search
+89
-72
@@ -53,6 +53,15 @@ fn checked(scopes: [wgpu::ErrorScopeGuard; 3]) -> Result<()> {
|
||||
error.map_or(Ok(()), |e| Err(gpu(e)))
|
||||
}
|
||||
|
||||
/// Drain every scope, in reverse order, even when resource creation returns an error.
|
||||
/// The ordinary cancellation/error path must not drop an array of guards forwards.
|
||||
fn checked_creation<T>(device: &wgpu::Device, create: impl FnOnce() -> Result<T>) -> Result<T> {
|
||||
let error_scopes = scopes(device);
|
||||
let result = create();
|
||||
checked(error_scopes)?;
|
||||
result
|
||||
}
|
||||
|
||||
/// Open and validate a native device before advertising the executor. Failure leaves the
|
||||
/// caller's CPU host usable. Respects SOLIDCRAFT_GPU, including the software adapter policy.
|
||||
pub fn host() -> Result<ImageHost> {
|
||||
@@ -313,6 +322,33 @@ impl ImageExecutor for Executor {
|
||||
}
|
||||
}
|
||||
|
||||
/// Build all fallible CPU environment data before opening GPU error scopes.
|
||||
fn prepare_environment(request: &ImageRequest, cancel: &CancelToken) -> Result<Vec<u8>> {
|
||||
cancel.check()?;
|
||||
let mut environment_bytes = Vec::new();
|
||||
if let Some(source) = &request.environment {
|
||||
let prepared = source.prepare(cancel)?;
|
||||
environment_bytes = scene::reserved(16 + prepared.texels().len() * 32)?;
|
||||
let [width, height] = prepared.dimensions();
|
||||
for x in [width, height, width * height, 0] {
|
||||
environment_bytes.extend_from_slice(&x.to_le_bytes());
|
||||
}
|
||||
for (index, texel) in prepared.texels().iter().enumerate() {
|
||||
if index % 1024 == 0 {
|
||||
cancel.check()?;
|
||||
}
|
||||
for x in texel.rgb.into_iter().chain([texel.pdf, texel.threshold]) {
|
||||
environment_bytes.extend_from_slice(&x.to_le_bytes());
|
||||
}
|
||||
environment_bytes.extend_from_slice(&texel.alias.to_le_bytes());
|
||||
environment_bytes.extend_from_slice(&[0; 8]);
|
||||
}
|
||||
} else {
|
||||
environment_bytes.resize(48, 0);
|
||||
}
|
||||
Ok(environment_bytes)
|
||||
}
|
||||
|
||||
struct Task {
|
||||
context: Arc<Context>,
|
||||
request: ImageRequest,
|
||||
@@ -348,81 +384,62 @@ impl Task {
|
||||
let upload = scene::flatten(&scene, request.camera.target, &cancel)?;
|
||||
let bvh_ms = start.elapsed().as_secs_f64() * 1000.0;
|
||||
let mut upload_bytes = (upload.geometry.len() + upload.nodes.len() + texture_upload.len()) as u64;
|
||||
let environment_bytes = prepare_environment(&request, &cancel)?;
|
||||
cancel.check()?;
|
||||
let uploading = Instant::now();
|
||||
drop(scene);
|
||||
let device = &context.device;
|
||||
let scopes = [wgpu::ErrorFilter::Validation, wgpu::ErrorFilter::OutOfMemory, wgpu::ErrorFilter::Internal].map(|f| device.push_error_scope(f));
|
||||
let make =
|
||||
|label, bytes: &[u8], usage| device.create_buffer_init(&wgpu::util::BufferInitDescriptor { label: Some(label), contents: bytes, usage });
|
||||
let mut environment_bytes = Vec::new();
|
||||
if let Some(source) = &request.environment {
|
||||
let prepared = source.prepare(&cancel)?;
|
||||
environment_bytes = scene::reserved(16 + prepared.texels().len() * 32)?;
|
||||
let [width, height] = prepared.dimensions();
|
||||
for x in [width, height, width * height, 0] {
|
||||
environment_bytes.extend_from_slice(&x.to_le_bytes());
|
||||
}
|
||||
for (index, texel) in prepared.texels().iter().enumerate() {
|
||||
if index % 1024 == 0 {
|
||||
cancel.check()?;
|
||||
}
|
||||
for x in texel.rgb.into_iter().chain([texel.pdf, texel.threshold]) {
|
||||
environment_bytes.extend_from_slice(&x.to_le_bytes());
|
||||
}
|
||||
environment_bytes.extend_from_slice(&texel.alias.to_le_bytes());
|
||||
environment_bytes.extend_from_slice(&[0; 8]);
|
||||
}
|
||||
} else {
|
||||
environment_bytes.resize(48, 0);
|
||||
}
|
||||
upload_bytes += environment_bytes.len() as u64;
|
||||
let environment = make("path environment", &environment_bytes, wgpu::BufferUsages::STORAGE);
|
||||
drop(environment_bytes);
|
||||
let triangles = make("path triangles", &upload.geometry, wgpu::BufferUsages::STORAGE);
|
||||
let nodes = make("path BVH", &upload.nodes, wgpu::BufferUsages::STORAGE);
|
||||
let texture_buffer = make("path opaque textures", &texture_upload, wgpu::BufferUsages::STORAGE);
|
||||
let uniform = make("path parameters", &[0; PARAMETER_BYTES], wgpu::BufferUsages::UNIFORM | wgpu::BufferUsages::COPY_DST);
|
||||
let size = u64::from(request.width) * u64::from(request.height) * 16;
|
||||
let accumulation = device.create_buffer(&wgpu::BufferDescriptor {
|
||||
label: Some("path accumulation"),
|
||||
size,
|
||||
usage: wgpu::BufferUsages::STORAGE | wgpu::BufferUsages::COPY_SRC,
|
||||
mapped_at_creation: false,
|
||||
});
|
||||
// Inconsistent-medium path count and the first such path's location (see read_linear).
|
||||
let failures = make(
|
||||
"path medium failures",
|
||||
&[0; FAILURE_BYTES as usize],
|
||||
wgpu::BufferUsages::STORAGE | wgpu::BufferUsages::COPY_SRC | wgpu::BufferUsages::COPY_DST,
|
||||
);
|
||||
let readback = device.create_buffer(&wgpu::BufferDescriptor {
|
||||
label: Some("path readback"),
|
||||
size: size + FAILURE_BYTES,
|
||||
usage: wgpu::BufferUsages::MAP_READ | wgpu::BufferUsages::COPY_DST,
|
||||
mapped_at_creation: false,
|
||||
});
|
||||
let mut entries: Vec<_> = [&uniform, &triangles, &nodes, &accumulation, &environment]
|
||||
.iter()
|
||||
.enumerate()
|
||||
.map(|(binding, buffer)| wgpu::BindGroupEntry { binding: binding as u32, resource: buffer.as_entire_binding() })
|
||||
.collect();
|
||||
entries.push(wgpu::BindGroupEntry { binding: 5, resource: texture_buffer.as_entire_binding() });
|
||||
entries.push(wgpu::BindGroupEntry { binding: 6, resource: failures.as_entire_binding() });
|
||||
let bind = device.create_bind_group(&wgpu::BindGroupDescriptor { label: Some("gpu-path"), layout: &context.layout, entries: &entries });
|
||||
let denoiser = if options.denoise {
|
||||
Some(denoise::Resources::new(&context, &uniform, [&triangles, &nodes, &environment, &texture_buffer, &failures], &accumulation, size)?)
|
||||
} else {
|
||||
None
|
||||
};
|
||||
let mut error = None;
|
||||
for scope in scopes.into_iter().rev() {
|
||||
if let Some(e) = pollster::block_on(scope.pop()) {
|
||||
error = Some(e);
|
||||
}
|
||||
}
|
||||
if let Some(e) = error {
|
||||
return Err(gpu(e));
|
||||
}
|
||||
let (uniform, accumulation, failures, readback, bind, denoiser) = checked_creation(device, || {
|
||||
let make = |label, bytes: &[u8], usage| {
|
||||
device.create_buffer_init(&wgpu::util::BufferInitDescriptor { label: Some(label), contents: bytes, usage })
|
||||
};
|
||||
upload_bytes += environment_bytes.len() as u64;
|
||||
let environment = make("path environment", &environment_bytes, wgpu::BufferUsages::STORAGE);
|
||||
drop(environment_bytes);
|
||||
let triangles = make("path triangles", &upload.geometry, wgpu::BufferUsages::STORAGE);
|
||||
let nodes = make("path BVH", &upload.nodes, wgpu::BufferUsages::STORAGE);
|
||||
let texture_buffer = make("path opaque textures", &texture_upload, wgpu::BufferUsages::STORAGE);
|
||||
let uniform = make("path parameters", &[0; PARAMETER_BYTES], wgpu::BufferUsages::UNIFORM | wgpu::BufferUsages::COPY_DST);
|
||||
let size = u64::from(request.width) * u64::from(request.height) * 16;
|
||||
let accumulation = device.create_buffer(&wgpu::BufferDescriptor {
|
||||
label: Some("path accumulation"),
|
||||
size,
|
||||
usage: wgpu::BufferUsages::STORAGE | wgpu::BufferUsages::COPY_SRC,
|
||||
mapped_at_creation: false,
|
||||
});
|
||||
// Inconsistent-medium path count and the first such path's location (see read_linear).
|
||||
let failures = make(
|
||||
"path medium failures",
|
||||
&[0; FAILURE_BYTES as usize],
|
||||
wgpu::BufferUsages::STORAGE | wgpu::BufferUsages::COPY_SRC | wgpu::BufferUsages::COPY_DST,
|
||||
);
|
||||
let readback = device.create_buffer(&wgpu::BufferDescriptor {
|
||||
label: Some("path readback"),
|
||||
size: size + FAILURE_BYTES,
|
||||
usage: wgpu::BufferUsages::MAP_READ | wgpu::BufferUsages::COPY_DST,
|
||||
mapped_at_creation: false,
|
||||
});
|
||||
let mut entries: Vec<_> = [&uniform, &triangles, &nodes, &accumulation, &environment]
|
||||
.iter()
|
||||
.enumerate()
|
||||
.map(|(binding, buffer)| wgpu::BindGroupEntry { binding: binding as u32, resource: buffer.as_entire_binding() })
|
||||
.collect();
|
||||
entries.push(wgpu::BindGroupEntry { binding: 5, resource: texture_buffer.as_entire_binding() });
|
||||
entries.push(wgpu::BindGroupEntry { binding: 6, resource: failures.as_entire_binding() });
|
||||
let bind = device.create_bind_group(&wgpu::BindGroupDescriptor { label: Some("gpu-path"), layout: &context.layout, entries: &entries });
|
||||
let denoiser = if options.denoise {
|
||||
Some(denoise::Resources::new(
|
||||
&context,
|
||||
&uniform,
|
||||
[&triangles, &nodes, &environment, &texture_buffer, &failures],
|
||||
&accumulation,
|
||||
size,
|
||||
)?)
|
||||
} else {
|
||||
None
|
||||
};
|
||||
Ok((uniform, accumulation, failures, readback, bind, denoiser))
|
||||
})?;
|
||||
let submission = context.queue.submit([]);
|
||||
device.poll(wgpu::PollType::Wait { submission_index: Some(submission), timeout: Some(TIMEOUT) }).map_err(gpu)?;
|
||||
let upload_ms = uploading.elapsed().as_secs_f64() * 1000.0;
|
||||
|
||||
@@ -2594,3 +2594,48 @@ fn medium_failures_are_tolerated_only_when_rare() {
|
||||
assert!(!medium_failures_tolerated(531, 1920 * 1080 * 256));
|
||||
assert!(!medium_failures_tolerated(1_u64 << 32, 1_u64 << 32));
|
||||
}
|
||||
|
||||
/// Cancellation in fallible HDR preparation must not drop three wgpu scopes in
|
||||
/// forward order. Exercise the real preparation routine inside the guarded path,
|
||||
/// then prove that the same device still accepts a fresh nested scope stack.
|
||||
#[test]
|
||||
fn cancelled_environment_preparation_unwinds_gpu_error_scopes() {
|
||||
use solidcraft_render::lighting::environment::EnvironmentSource;
|
||||
let Ok((_, device, _queue)) = open_headless() else {
|
||||
let vulkan = crate::select::native_backends().contains(wgpu::Backends::VULKAN);
|
||||
assert!(!vulkan || std::env::var_os("SOLIDCRAFT_REQUIRE_GPU").is_none(), "required Vulkan device unavailable");
|
||||
return;
|
||||
};
|
||||
let mut image = request(1);
|
||||
image.environment = Some(Arc::new(EnvironmentSource::hdr(original_hdr(), 0.0, 1.0, false).unwrap()));
|
||||
let token = CancelToken::default();
|
||||
let outcome = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
|
||||
checked_creation(&device, || {
|
||||
token.cancel();
|
||||
prepare_environment(&image, &token)
|
||||
})
|
||||
}));
|
||||
let error = outcome.expect("ordinary cancellation must not panic").unwrap_err();
|
||||
assert_eq!(error.code, ErrorCode::Cancelled);
|
||||
checked(scopes(&device)).unwrap();
|
||||
let bytes = prepare_environment(&image, &CancelToken::default()).unwrap();
|
||||
let info = image.environment.as_ref().unwrap().info();
|
||||
assert_eq!(bytes.len(), 16 + info.width as usize * info.height as usize * 32);
|
||||
}
|
||||
|
||||
/// A denoiser allocation can fail after the outer resource scopes were opened.
|
||||
/// Preserve its Result while draining every scope, not only the last one.
|
||||
#[test]
|
||||
fn failed_resource_creation_unwinds_gpu_error_scopes() {
|
||||
let Ok((_, device, _queue)) = open_headless() else {
|
||||
let vulkan = crate::select::native_backends().contains(wgpu::Backends::VULKAN);
|
||||
assert!(!vulkan || std::env::var_os("SOLIDCRAFT_REQUIRE_GPU").is_none(), "required Vulkan device unavailable");
|
||||
return;
|
||||
};
|
||||
let outcome = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
|
||||
checked_creation(&device, || -> Result<()> { Err(JobError::new(ErrorCode::LimitExceeded, "synthetic bounded allocation refusal")) })
|
||||
}));
|
||||
let error = outcome.expect("ordinary allocation refusal must not panic").unwrap_err();
|
||||
assert_eq!(error.code, ErrorCode::LimitExceeded);
|
||||
checked(scopes(&device)).unwrap();
|
||||
}
|
||||
Reference in new issue
Block a user