forked from EmbarkStudios/rust-gpu
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy patharch.rs
More file actions
175 lines (157 loc) · 5.2 KB
/
arch.rs
File metadata and controls
175 lines (157 loc) · 5.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
//! SPIR-V Instrinics
//!
//! This module is intended as a low level abstraction over SPIR-V instructions.
//! These functions will typically map to a single instruction, and will perform
//! no additional safety checks beyond type-checking.
use crate::{scalar::Scalar, vector::Vector};
mod barrier;
mod demote_to_helper_invocation_ext;
mod derivative;
mod primitive;
mod ray_tracing;
pub use barrier::*;
pub use demote_to_helper_invocation_ext::*;
pub use derivative::*;
pub use primitive::*;
pub use ray_tracing::*;
/// Result is true if any component of `vector` is true, otherwise result is
/// false.
#[spirv_std_macros::gpu_only]
#[doc(alias = "OpAny")]
#[inline]
pub fn any<V: Vector<bool, N>, const N: usize>(vector: V) -> bool {
let mut result = false;
unsafe {
asm! {
// Types & Constants
"%bool = OpTypeBool",
"%u8 = OpTypeInt 8 0",
"%u8_0 = OpConstant %u8 0",
"%u8_1 = OpConstant %u8 1",
"%glam_vec_type = OpTypeVector %u8 {len}",
"%bool_vec_type = OpTypeVector %bool {len}",
"%false_vec = OpConstantNull %glam_vec_type",
// Code
"%vector = OpLoad %glam_vec_type {vector}",
"%bool_vec = OpINotEqual %bool_vec_type %vector %false_vec",
"%result = OpAny %bool %bool_vec",
"%boolean = OpSelect %u8 %result %u8_1 %u8_0",
"OpStore {result} %boolean",
vector = in(reg) &vector,
len = const N,
result = in(reg) &mut result
}
}
result
}
/// Result is true if all components of `vector` is true, otherwise result is
/// false.
#[spirv_std_macros::gpu_only]
#[doc(alias = "OpAll")]
#[inline]
pub fn all<V: Vector<bool, N>, const N: usize>(vector: V) -> bool {
let mut result = false;
unsafe {
asm! {
// Types & Constants
"%bool = OpTypeBool",
"%u8 = OpTypeInt 8 0",
"%u8_0 = OpConstant %u8 0",
"%u8_1 = OpConstant %u8 1",
"%glam_vec_type = OpTypeVector %u8 {len}",
"%bool_vec_type = OpTypeVector %bool {len}",
"%false_vec = OpConstantNull %glam_vec_type",
// Code
"%vector = OpLoad %glam_vec_type {vector}",
"%bool_vec = OpINotEqual %bool_vec_type %vector %false_vec",
"%result = OpAll %bool %bool_vec",
"%boolean = OpSelect %u8 %result %u8_1 %u8_0",
"OpStore {element} %boolean",
vector = in(reg) &vector,
len = const N,
element = in(reg) &mut result
}
}
result
}
/// Extract a single, dynamically selected, component of a vector.
///
/// # Safety
/// Behavior is undefined if `index`’s value is greater than or equal to the
/// number of components in `vector`.
#[spirv_std_macros::gpu_only]
#[doc(alias = "OpVectorExtractDynamic")]
#[inline]
pub unsafe fn vector_extract_dynamic<T: Scalar, const N: usize>(
vector: impl Vector<T, N>,
index: usize,
) -> T {
let mut result = T::default();
asm! {
"%vector = OpLoad _ {vector}",
"%element = OpVectorExtractDynamic _ %vector {index}",
"OpStore {element} %element",
vector = in(reg) &vector,
index = in(reg) index,
element = in(reg) &mut result
}
result
}
/// Make a copy of a vector, with a single, variably selected,
/// component modified.
///
/// # Safety
/// Behavior is undefined if `index`’s value is greater than or equal to the
/// number of components in `vector`.
#[spirv_std_macros::gpu_only]
#[doc(alias = "OpVectorInsertDynamic")]
#[inline]
pub unsafe fn vector_insert_dynamic<T: Scalar, V: Vector<T, N>, const N: usize>(
vector: V,
index: usize,
element: T,
) -> V {
let mut result = V::default();
asm! {
"%vector = OpLoad _ {vector}",
"%element = OpLoad _ {element}",
"%new_vector = OpVectorInsertDynamic _ %vector %element {index}",
"OpStore {result} %new_vector",
vector = in(reg) &vector,
index = in(reg) index,
element = in(reg) &element,
result = in(reg) &mut result,
}
result
}
/// Fragment-shader discard. Equivalvent to `discard()` from GLSL
///
/// Ceases all further processing in any invocation that executes it: Only
/// instructions these invocations executed before [kill] have observable side
/// effects.
#[spirv_std_macros::gpu_only]
#[doc(alias = "OpKill", alias = "discard")]
#[allow(clippy::empty_loop)]
pub fn kill() -> ! {
unsafe { asm!("OpKill", options(noreturn)) }
}
/// Read from the shader clock.
///
/// Requires the `SPV_KHR_shader_clock` extension and the `ShaderClockKHR` capability.
///
/// See:
/// <https://htmlpreview.github.io/?https://github.com/KhronosGroup/SPIRV-Registry/blob/master/extensions/KHR/SPV_KHR_shader_clock.html>
#[spirv_std_macros::gpu_only]
#[doc(alias = "OpReadClockKHR")]
pub unsafe fn read_clock_khr() -> u64 {
let mut result: u64 = 0;
asm! {
"%uint = OpTypeInt 32 0",
"%scope = OpConstant %uint {scope}",
"%result = OpReadClockKHR typeof*{result} %scope",
"OpStore {result} %result",
result = in(reg) &mut result,
scope = const crate::memory::Scope::Subgroup as _,
};
result
}