Skip to content

Commit 4a1f6c9

Browse files
committed
move mod: make read_clock() better match glsl intrinsic
1 parent 6f7ceac commit 4a1f6c9

File tree

2 files changed

+27
-25
lines changed

2 files changed

+27
-25
lines changed

crates/spirv-std/src/shader_clock.rs

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,53 +3,53 @@
33
//! GLSL: [`GL_EXT_shader_realtime_clock`](https://github.com/KhronosGroup/GLSL/blob/main/extensions/ext/GL_EXT_shader_realtime_clock.txt)
44
//! SPIRV: [`SPV_KHR_shader_clock`](https://github.khronos.org/SPIRV-Registry/extensions/KHR/SPV_KHR_shader_clock.html)
55
6+
#[cfg(target_arch = "spirv")]
7+
use crate::memory::Scope;
68
#[cfg(target_arch = "spirv")]
79
use core::arch::asm;
810
use glam::UVec2;
911

10-
/// Read from the shader clock with either the `Subgroup` or `Device` scope.
12+
/// The [`read_clock`] function returns a 64-bit value representing a real-time clock that is globally coherent by
13+
/// all invocations on the GPU. See [`read_clock_uvec2`] for a variant that doesn't require 64-bit support.
1114
///
12-
/// See: <https://github.khronos.org/SPIRV-Registry/extensions/KHR/SPV_KHR_shader_clock.html>
15+
/// The units of time are not defined for either of these operations and will wrap after exceeding the maximum value
16+
/// representable in 64 bits. These functions serve as code motion barriers.
1317
#[spirv_std_macros::gpu_only]
1418
#[doc(alias = "OpReadClockKHR")]
15-
pub fn read_clock_khr<const SCOPE: u32>() -> u64 {
19+
pub fn read_clock() -> u64 {
1620
unsafe {
17-
let mut result: u64;
18-
21+
let mut result = Default::default();
1922
asm! {
2023
"%uint = OpTypeInt 32 0",
2124
"%scope = OpConstant %uint {scope}",
22-
"{result} = OpReadClockKHR typeof*{result} %scope",
23-
result = out(reg) result,
24-
scope = const SCOPE,
25+
"%result = OpReadClockKHR typeof*{result} %scope",
26+
"OpStore {result} %result",
27+
result = in(reg) &mut result,
28+
scope = const (Scope::Device as u32),
2529
};
26-
2730
result
2831
}
2932
}
3033

31-
/// Like `read_clock_khr` but returns a vector to avoid requiring the `Int64`
32-
/// capability. It returns a 'vector of two-components of 32-bit unsigned
33-
/// integer type with the first component containing the 32 least significant
34-
/// bits and the second component containing the 32 most significant bits.'
34+
/// [`read_clock_uvec2`] returns the same value encoded as a two-component vector of 32-bit unsigned integers with the
35+
/// first component containing the 32 least significant bits and the second component containing the 32 most significant
36+
/// bits. See [`read_clock`] for a variant that returns a single `u64`.
3537
///
36-
/// See:
37-
/// <https://htmlpreview.github.io/?https://github.com/KhronosGroup/SPIRV-Registry/blob/master/extensions/KHR/SPV_KHR_shader_clock.html>
38+
/// The units of time are not defined for either of these operations and will wrap after exceeding the maximum value
39+
/// representable in 64 bits. These functions serve as code motion barriers.
3840
#[spirv_std_macros::gpu_only]
3941
#[doc(alias = "OpReadClockKHR")]
40-
pub fn read_clock_uvec2_khr<const SCOPE: u32>() -> UVec2 {
42+
pub fn read_clock_uvec2() -> UVec2 {
4143
unsafe {
42-
let mut result = UVec2::default();
43-
44+
let mut result = Default::default();
4445
asm! {
4546
"%uint = OpTypeInt 32 0",
4647
"%scope = OpConstant %uint {scope}",
4748
"%result = OpReadClockKHR typeof*{result} %scope",
4849
"OpStore {result} %result",
4950
result = in(reg) &mut result,
50-
scope = const SCOPE,
51+
scope = const (Scope::Device as u32),
5152
};
52-
5353
result
5454
}
5555
}

tests/compiletests/ui/arch/read_clock_khr.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,14 @@ use glam::UVec2;
55
use spirv_std::spirv;
66
use spirv_std::{
77
memory::Scope,
8-
shader_clock::{read_clock_khr, read_clock_uvec2_khr},
8+
shader_clock::{read_clock, read_clock_uvec2},
99
};
1010

1111
#[spirv(fragment)]
12-
pub fn main() {
13-
let clock_time = unsafe { read_clock_khr::<{ Scope::Subgroup as u32 }>() };
14-
15-
let clock_time_uvec2: UVec2 = unsafe { read_clock_uvec2_khr::<{ Scope::Subgroup as u32 }>() };
12+
pub fn main(out: &mut u32) {
13+
unsafe {
14+
let clock_time: u64 = read_clock();
15+
let clock_time_uvec2: UVec2 = read_clock_uvec2();
16+
*out = clock_time as u32 + clock_time_uvec2.x;
17+
}
1618
}

0 commit comments

Comments
 (0)