@@ -87,18 +87,20 @@ pub unsafe fn vector_extract_dynamic<T: Scalar, const N: usize>(
87
87
vector : impl Vector < T , N > ,
88
88
index : usize ,
89
89
) -> T {
90
- let mut result = T :: default ( ) ;
91
-
92
- asm ! {
93
- "%vector = OpLoad _ {vector}" ,
94
- "%element = OpVectorExtractDynamic _ %vector {index}" ,
95
- "OpStore {element} %element" ,
96
- vector = in( reg) & vector,
97
- index = in( reg) index,
98
- element = in( reg) & mut result
99
- }
90
+ unsafe {
91
+ let mut result = T :: default ( ) ;
100
92
101
- result
93
+ asm ! {
94
+ "%vector = OpLoad _ {vector}" ,
95
+ "%element = OpVectorExtractDynamic _ %vector {index}" ,
96
+ "OpStore {element} %element" ,
97
+ vector = in( reg) & vector,
98
+ index = in( reg) index,
99
+ element = in( reg) & mut result
100
+ }
101
+
102
+ result
103
+ }
102
104
}
103
105
104
106
/// Make a copy of a vector, with a single, variably selected,
@@ -115,20 +117,22 @@ pub unsafe fn vector_insert_dynamic<T: Scalar, V: Vector<T, N>, const N: usize>(
115
117
index : usize ,
116
118
element : T ,
117
119
) -> V {
118
- let mut result = V :: default ( ) ;
119
-
120
- asm ! {
121
- "%vector = OpLoad _ {vector}" ,
122
- "%element = OpLoad _ {element}" ,
123
- "%new_vector = OpVectorInsertDynamic _ %vector %element {index}" ,
124
- "OpStore {result} %new_vector" ,
125
- vector = in( reg) & vector,
126
- index = in( reg) index,
127
- element = in( reg) & element,
128
- result = in( reg) & mut result,
129
- }
120
+ unsafe {
121
+ let mut result = V :: default ( ) ;
130
122
131
- result
123
+ asm ! {
124
+ "%vector = OpLoad _ {vector}" ,
125
+ "%element = OpLoad _ {element}" ,
126
+ "%new_vector = OpVectorInsertDynamic _ %vector %element {index}" ,
127
+ "OpStore {result} %new_vector" ,
128
+ vector = in( reg) & vector,
129
+ index = in( reg) index,
130
+ element = in( reg) & element,
131
+ result = in( reg) & mut result,
132
+ }
133
+
134
+ result
135
+ }
132
136
}
133
137
134
138
/// Fragment-shader discard. Equivalvent to `discard()` from GLSL
@@ -155,17 +159,19 @@ pub fn kill() -> ! {
155
159
#[ spirv_std_macros:: gpu_only]
156
160
#[ doc( alias = "OpReadClockKHR" ) ]
157
161
pub unsafe fn read_clock_khr < const SCOPE : u32 > ( ) -> u64 {
158
- let mut result: u64 ;
159
-
160
- asm ! {
161
- "%uint = OpTypeInt 32 0" ,
162
- "%scope = OpConstant %uint {scope}" ,
163
- "{result} = OpReadClockKHR typeof*{result} %scope" ,
164
- result = out( reg) result,
165
- scope = const SCOPE ,
166
- } ;
162
+ unsafe {
163
+ let mut result: u64 ;
167
164
168
- result
165
+ asm ! {
166
+ "%uint = OpTypeInt 32 0" ,
167
+ "%scope = OpConstant %uint {scope}" ,
168
+ "{result} = OpReadClockKHR typeof*{result} %scope" ,
169
+ result = out( reg) result,
170
+ scope = const SCOPE ,
171
+ } ;
172
+
173
+ result
174
+ }
169
175
}
170
176
171
177
/// Like `read_clock_khr` but returns a vector to avoid requiring the `Int64`
@@ -179,35 +185,39 @@ pub unsafe fn read_clock_khr<const SCOPE: u32>() -> u64 {
179
185
#[ spirv_std_macros:: gpu_only]
180
186
#[ doc( alias = "OpReadClockKHR" ) ]
181
187
pub unsafe fn read_clock_uvec2_khr < V : Vector < u32 , 2 > , const SCOPE : u32 > ( ) -> V {
182
- let mut result = V :: default ( ) ;
188
+ unsafe {
189
+ let mut result = V :: default ( ) ;
183
190
184
- asm ! {
185
- "%uint = OpTypeInt 32 0" ,
186
- "%scope = OpConstant %uint {scope}" ,
187
- "%result = OpReadClockKHR typeof*{result} %scope" ,
188
- "OpStore {result} %result" ,
189
- result = in( reg) & mut result,
190
- scope = const SCOPE ,
191
- } ;
191
+ asm ! {
192
+ "%uint = OpTypeInt 32 0" ,
193
+ "%scope = OpConstant %uint {scope}" ,
194
+ "%result = OpReadClockKHR typeof*{result} %scope" ,
195
+ "OpStore {result} %result" ,
196
+ result = in( reg) & mut result,
197
+ scope = const SCOPE ,
198
+ } ;
192
199
193
- result
200
+ result
201
+ }
194
202
}
195
203
196
204
#[ cfg( target_arch = "spirv" ) ]
197
205
unsafe fn call_glsl_op_with_ints < T : Integer , const OP : u32 > ( a : T , b : T ) -> T {
198
- let mut result = T :: default ( ) ;
199
- asm ! (
200
- "%glsl = OpExtInstImport \" GLSL.std.450\" " ,
201
- "%a = OpLoad _ {a}" ,
202
- "%b = OpLoad _ {b}" ,
203
- "%result = OpExtInst typeof*{result} %glsl {op} %a %b" ,
204
- "OpStore {result} %result" ,
205
- a = in( reg) & a,
206
- b = in( reg) & b,
207
- result = in( reg) & mut result,
208
- op = const OP
209
- ) ;
210
- result
206
+ unsafe {
207
+ let mut result = T :: default ( ) ;
208
+ asm ! (
209
+ "%glsl = OpExtInstImport \" GLSL.std.450\" " ,
210
+ "%a = OpLoad _ {a}" ,
211
+ "%b = OpLoad _ {b}" ,
212
+ "%result = OpExtInst typeof*{result} %glsl {op} %a %b" ,
213
+ "OpStore {result} %result" ,
214
+ a = in( reg) & a,
215
+ b = in( reg) & b,
216
+ result = in( reg) & mut result,
217
+ op = const OP
218
+ ) ;
219
+ result
220
+ }
211
221
}
212
222
213
223
/// Compute the minimum of two unsigned integers via a GLSL extended instruction.
@@ -254,83 +264,91 @@ pub trait IndexUnchecked<T> {
254
264
impl < T > IndexUnchecked < T > for [ T ] {
255
265
#[ cfg( target_arch = "spirv" ) ]
256
266
unsafe fn index_unchecked ( & self , index : usize ) -> & T {
257
- // FIXME(eddyb) `let mut result = T::default()` uses (for `asm!`), with this.
258
- let mut result_slot = core:: mem:: MaybeUninit :: uninit ( ) ;
259
- asm ! {
260
- "%slice_ptr = OpLoad _ {slice_ptr_ptr}" ,
261
- "%data_ptr = OpCompositeExtract _ %slice_ptr 0" ,
262
- "%result = OpAccessChain _ %data_ptr {index}" ,
263
- "OpStore {result_slot} %result" ,
264
- slice_ptr_ptr = in( reg) & self ,
265
- index = in( reg) index,
266
- result_slot = in( reg) result_slot. as_mut_ptr( ) ,
267
+ unsafe {
268
+ // FIXME(eddyb) `let mut result = T::default()` uses (for `asm!`), with this.
269
+ let mut result_slot = core:: mem:: MaybeUninit :: uninit ( ) ;
270
+ asm ! {
271
+ "%slice_ptr = OpLoad _ {slice_ptr_ptr}" ,
272
+ "%data_ptr = OpCompositeExtract _ %slice_ptr 0" ,
273
+ "%result = OpAccessChain _ %data_ptr {index}" ,
274
+ "OpStore {result_slot} %result" ,
275
+ slice_ptr_ptr = in( reg) & self ,
276
+ index = in( reg) index,
277
+ result_slot = in( reg) result_slot. as_mut_ptr( ) ,
278
+ }
279
+ result_slot. assume_init ( )
267
280
}
268
- result_slot. assume_init ( )
269
281
}
270
282
271
283
#[ cfg( not( target_arch = "spirv" ) ) ]
272
284
unsafe fn index_unchecked ( & self , index : usize ) -> & T {
273
- self . get_unchecked ( index)
285
+ unsafe { self . get_unchecked ( index) }
274
286
}
275
287
276
288
#[ cfg( target_arch = "spirv" ) ]
277
289
unsafe fn index_unchecked_mut ( & mut self , index : usize ) -> & mut T {
278
- // FIXME(eddyb) `let mut result = T::default()` uses (for `asm!`), with this.
279
- let mut result_slot = core:: mem:: MaybeUninit :: uninit ( ) ;
280
- asm ! {
281
- "%slice_ptr = OpLoad _ {slice_ptr_ptr}" ,
282
- "%data_ptr = OpCompositeExtract _ %slice_ptr 0" ,
283
- "%result = OpAccessChain _ %data_ptr {index}" ,
284
- "OpStore {result_slot} %result" ,
285
- slice_ptr_ptr = in( reg) & self ,
286
- index = in( reg) index,
287
- result_slot = in( reg) result_slot. as_mut_ptr( ) ,
290
+ unsafe {
291
+ // FIXME(eddyb) `let mut result = T::default()` uses (for `asm!`), with this.
292
+ let mut result_slot = core:: mem:: MaybeUninit :: uninit ( ) ;
293
+ asm ! {
294
+ "%slice_ptr = OpLoad _ {slice_ptr_ptr}" ,
295
+ "%data_ptr = OpCompositeExtract _ %slice_ptr 0" ,
296
+ "%result = OpAccessChain _ %data_ptr {index}" ,
297
+ "OpStore {result_slot} %result" ,
298
+ slice_ptr_ptr = in( reg) & self ,
299
+ index = in( reg) index,
300
+ result_slot = in( reg) result_slot. as_mut_ptr( ) ,
301
+ }
302
+ result_slot. assume_init ( )
288
303
}
289
- result_slot. assume_init ( )
290
304
}
291
305
292
306
#[ cfg( not( target_arch = "spirv" ) ) ]
293
307
unsafe fn index_unchecked_mut ( & mut self , index : usize ) -> & mut T {
294
- self . get_unchecked_mut ( index)
308
+ unsafe { self . get_unchecked_mut ( index) }
295
309
}
296
310
}
297
311
298
312
impl < T , const N : usize > IndexUnchecked < T > for [ T ; N ] {
299
313
#[ cfg( target_arch = "spirv" ) ]
300
314
unsafe fn index_unchecked ( & self , index : usize ) -> & T {
301
- // FIXME(eddyb) `let mut result = T::default()` uses (for `asm!`), with this.
302
- let mut result_slot = core:: mem:: MaybeUninit :: uninit ( ) ;
303
- asm ! {
304
- "%result = OpAccessChain _ {array_ptr} {index}" ,
305
- "OpStore {result_slot} %result" ,
306
- array_ptr = in( reg) self ,
307
- index = in( reg) index,
308
- result_slot = in( reg) result_slot. as_mut_ptr( ) ,
315
+ unsafe {
316
+ // FIXME(eddyb) `let mut result = T::default()` uses (for `asm!`), with this.
317
+ let mut result_slot = core:: mem:: MaybeUninit :: uninit ( ) ;
318
+ asm ! {
319
+ "%result = OpAccessChain _ {array_ptr} {index}" ,
320
+ "OpStore {result_slot} %result" ,
321
+ array_ptr = in( reg) self ,
322
+ index = in( reg) index,
323
+ result_slot = in( reg) result_slot. as_mut_ptr( ) ,
324
+ }
325
+ result_slot. assume_init ( )
309
326
}
310
- result_slot. assume_init ( )
311
327
}
312
328
313
329
#[ cfg( not( target_arch = "spirv" ) ) ]
314
330
unsafe fn index_unchecked ( & self , index : usize ) -> & T {
315
- self . get_unchecked ( index)
331
+ unsafe { self . get_unchecked ( index) }
316
332
}
317
333
318
334
#[ cfg( target_arch = "spirv" ) ]
319
335
unsafe fn index_unchecked_mut ( & mut self , index : usize ) -> & mut T {
320
- // FIXME(eddyb) `let mut result = T::default()` uses (for `asm!`), with this.
321
- let mut result_slot = core:: mem:: MaybeUninit :: uninit ( ) ;
322
- asm ! {
323
- "%result = OpAccessChain _ {array_ptr} {index}" ,
324
- "OpStore {result_slot} %result" ,
325
- array_ptr = in( reg) self ,
326
- index = in( reg) index,
327
- result_slot = in( reg) result_slot. as_mut_ptr( ) ,
336
+ unsafe {
337
+ // FIXME(eddyb) `let mut result = T::default()` uses (for `asm!`), with this.
338
+ let mut result_slot = core:: mem:: MaybeUninit :: uninit ( ) ;
339
+ asm ! {
340
+ "%result = OpAccessChain _ {array_ptr} {index}" ,
341
+ "OpStore {result_slot} %result" ,
342
+ array_ptr = in( reg) self ,
343
+ index = in( reg) index,
344
+ result_slot = in( reg) result_slot. as_mut_ptr( ) ,
345
+ }
346
+ result_slot. assume_init ( )
328
347
}
329
- result_slot. assume_init ( )
330
348
}
331
349
332
350
#[ cfg( not( target_arch = "spirv" ) ) ]
333
351
unsafe fn index_unchecked_mut ( & mut self , index : usize ) -> & mut T {
334
- self . get_unchecked_mut ( index)
352
+ unsafe { self . get_unchecked_mut ( index) }
335
353
}
336
354
}
0 commit comments