Skip to content

Commit 7fa4299

Browse files
refactor(core): extract Global::validate_pass_timestamp_writes
1 parent 71c392a commit 7fa4299

File tree

3 files changed

+59
-105
lines changed

3 files changed

+59
-105
lines changed

wgpu-core/src/command/compute.rs

Lines changed: 9 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,7 @@ use crate::{
2828
use thiserror::Error;
2929
use wgt::{BufferAddress, DynamicOffset};
3030

31-
use super::{
32-
bind::BinderError, memory_init::CommandBufferTextureMemoryActions, SimplifiedQueryType,
33-
};
31+
use super::{bind::BinderError, memory_init::CommandBufferTextureMemoryActions};
3432
use crate::ray_tracing::TlasAction;
3533
use std::sync::Arc;
3634
use std::{fmt, mem::size_of, str};
@@ -310,64 +308,15 @@ impl Global {
310308
Err(e) => return make_err(e, arc_desc),
311309
};
312310

313-
arc_desc.timestamp_writes = if let Some(tw) = desc.timestamp_writes {
314-
let &PassTimestampWrites {
315-
query_set,
316-
beginning_of_pass_write_index,
317-
end_of_pass_write_index,
318-
} = tw;
319-
320-
match cmd_buf
321-
.device
322-
.require_features(wgt::Features::TIMESTAMP_QUERY)
323-
{
324-
Ok(()) => (),
325-
Err(e) => return make_err(e.into(), arc_desc),
326-
}
327-
328-
let query_set = match hub.query_sets.get(query_set).get() {
329-
Ok(query_set) => query_set,
330-
Err(e) => return make_err(e.into(), arc_desc),
331-
};
332-
333-
match query_set.same_device(&cmd_buf.device) {
334-
Ok(()) => (),
335-
Err(e) => return make_err(e.into(), arc_desc),
336-
}
337-
338-
for idx in [beginning_of_pass_write_index, end_of_pass_write_index]
339-
.into_iter()
340-
.flatten()
341-
{
342-
match query_set.validate_query(SimplifiedQueryType::Timestamp, idx, None) {
343-
Ok(()) => (),
344-
Err(e) => return make_err(e.into(), arc_desc),
345-
}
346-
}
347-
348-
if let Some((begin, end)) = beginning_of_pass_write_index.zip(end_of_pass_write_index) {
349-
if begin == end {
350-
return make_err(
351-
CommandEncoderError::TimestampWriteIndicesEqual { idx: begin },
352-
arc_desc,
353-
);
354-
}
355-
}
356-
357-
if beginning_of_pass_write_index
358-
.or(end_of_pass_write_index)
359-
.is_none()
360-
{
361-
return make_err(CommandEncoderError::TimestampWriteIndicesMissing, arc_desc);
362-
}
363-
364-
Some(ArcPassTimestampWrites {
365-
query_set,
366-
beginning_of_pass_write_index,
367-
end_of_pass_write_index,
311+
arc_desc.timestamp_writes = match desc
312+
.timestamp_writes
313+
.map(|tw| {
314+
Self::validate_pass_timestamp_writes(&cmd_buf.device, &*hub.query_sets.read(), tw)
368315
})
369-
} else {
370-
None
316+
.transpose()
317+
{
318+
Ok(ok) => ok,
319+
Err(e) => return make_err(e.into(), arc_desc),
371320
};
372321

373322
(ComputePass::new(Some(cmd_buf), arc_desc), None)

wgpu-core/src/command/mod.rs

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@ use crate::snatch::SnatchGuard;
3333

3434
use crate::init_tracker::BufferInitTrackerAction;
3535
use crate::ray_tracing::{BlasAction, TlasAction};
36-
use crate::resource::{InvalidResourceError, Labeled};
36+
use crate::resource::{Fallible, InvalidResourceError, Labeled, ParentDevice as _, QuerySet};
37+
use crate::storage::Storage;
3738
use crate::track::{DeviceTracker, Tracker, UsageScope};
3839
use crate::LabelHelpers;
3940
use crate::{api_log, global::Global, id, resource_log, Label};
@@ -782,6 +783,50 @@ impl Global {
782783
}
783784
Ok(())
784785
}
786+
787+
fn validate_pass_timestamp_writes(
788+
device: &Device,
789+
query_sets: &Storage<Fallible<QuerySet>>,
790+
timestamp_writes: &PassTimestampWrites,
791+
) -> Result<ArcPassTimestampWrites, CommandEncoderError> {
792+
let &PassTimestampWrites {
793+
query_set,
794+
beginning_of_pass_write_index,
795+
end_of_pass_write_index,
796+
} = timestamp_writes;
797+
798+
device.require_features(wgt::Features::TIMESTAMP_QUERY)?;
799+
800+
let query_set = query_sets.get(query_set).get()?;
801+
802+
query_set.same_device(device)?;
803+
804+
for idx in [beginning_of_pass_write_index, end_of_pass_write_index]
805+
.into_iter()
806+
.flatten()
807+
{
808+
query_set.validate_query(SimplifiedQueryType::Timestamp, idx, None)?;
809+
}
810+
811+
if let Some((begin, end)) = beginning_of_pass_write_index.zip(end_of_pass_write_index) {
812+
if begin == end {
813+
return Err(CommandEncoderError::TimestampWriteIndicesEqual { idx: begin });
814+
}
815+
}
816+
817+
if beginning_of_pass_write_index
818+
.or(end_of_pass_write_index)
819+
.is_none()
820+
{
821+
return Err(CommandEncoderError::TimestampWriteIndicesMissing);
822+
}
823+
824+
Ok(ArcPassTimestampWrites {
825+
query_set,
826+
beginning_of_pass_write_index,
827+
end_of_pass_write_index,
828+
})
829+
}
785830
}
786831

787832
fn push_constant_clear<PushFn>(offset: u32, size_bytes: u32, mut push_fn: PushFn)

wgpu-core/src/command/render.rs

Lines changed: 4 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
use crate::binding_model::BindGroup;
22
use crate::command::{
33
validate_and_begin_occlusion_query, validate_and_begin_pipeline_statistics_query,
4-
SimplifiedQueryType,
54
};
65
use crate::init_tracker::BufferInitTrackerAction;
76
use crate::pipeline::RenderPipeline;
@@ -1392,49 +1391,10 @@ impl Global {
13921391
None
13931392
};
13941393

1395-
arc_desc.timestamp_writes = if let Some(tw) = desc.timestamp_writes {
1396-
let &PassTimestampWrites {
1397-
query_set,
1398-
beginning_of_pass_write_index,
1399-
end_of_pass_write_index,
1400-
} = tw;
1401-
1402-
let query_set = query_sets.get(query_set).get()?;
1403-
1404-
device.require_features(wgt::Features::TIMESTAMP_QUERY)?;
1405-
1406-
query_set.same_device(device)?;
1407-
1408-
for idx in [beginning_of_pass_write_index, end_of_pass_write_index]
1409-
.into_iter()
1410-
.flatten()
1411-
{
1412-
query_set.validate_query(SimplifiedQueryType::Timestamp, idx, None)?;
1413-
}
1414-
1415-
if let Some((begin, end)) =
1416-
beginning_of_pass_write_index.zip(end_of_pass_write_index)
1417-
{
1418-
if begin == end {
1419-
return Err(CommandEncoderError::TimestampWriteIndicesEqual { idx: begin });
1420-
}
1421-
}
1422-
1423-
if beginning_of_pass_write_index
1424-
.or(end_of_pass_write_index)
1425-
.is_none()
1426-
{
1427-
return Err(CommandEncoderError::TimestampWriteIndicesMissing);
1428-
}
1429-
1430-
Some(ArcPassTimestampWrites {
1431-
query_set,
1432-
beginning_of_pass_write_index,
1433-
end_of_pass_write_index,
1434-
})
1435-
} else {
1436-
None
1437-
};
1394+
arc_desc.timestamp_writes = desc
1395+
.timestamp_writes
1396+
.map(|tw| Global::validate_pass_timestamp_writes(device, &*query_sets, tw))
1397+
.transpose()?;
14381398

14391399
arc_desc.occlusion_query_set =
14401400
if let Some(occlusion_query_set) = desc.occlusion_query_set {

0 commit comments

Comments
 (0)