-
-
Notifications
You must be signed in to change notification settings - Fork 210
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add `RenderingServer` to minimal classes, to be able to test the RID impl against an actual server Add `Engine` to minimal classes, to be able to disable error printing in itests
- Loading branch information
Showing
7 changed files
with
238 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
/* | ||
* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at https://mozilla.org/MPL/2.0/. | ||
*/ | ||
|
||
use std::num::NonZeroU64; | ||
|
||
use godot_ffi as sys; | ||
use sys::{ffi_methods, GodotFfi}; | ||
|
||
/// A RID ("resource ID") is an opaque handle that refers to a Godot `Resource`. | ||
/// | ||
/// RIDs do not grant access to the resource itself. Instead, they can be used in lower-level resource APIs | ||
/// such as the [servers]. See also [Godot API docs for `RID`][docs]. | ||
/// | ||
/// RIDs should be largely safe to work with. Certain calls to servers may fail, however doing so will | ||
/// trigger an error from Godot, and will not cause any UB. | ||
/// | ||
/// # Safety Caveat: | ||
/// | ||
/// In Godot 3, RID was not as safe as described here. We believe that this is fixed in Godot 4, but this has | ||
/// not been extensively tested as of yet. Some confirmed UB from Godot 3 does not occur anymore, but if you | ||
/// find anything suspicious or outright UB please open an issue. | ||
/// | ||
/// [servers]: https://docs.godotengine.org/en/stable/tutorials/optimization/using_servers.html | ||
/// [docs]: https://docs.godotengine.org/en/stable/classes/class_rid.html | ||
// Using normal rust repr to take advantage advantage of the nullable pointer optimization. As this enum is | ||
// eligible for it, it is also guaranteed to have it. Meaning the layout of this type is identical to `u64`. | ||
// See: https://doc.rust-lang.org/nomicon/ffi.html#the-nullable-pointer-optimization | ||
// Cannot use `#[repr(C)]` as it does not use the nullable pointer optimization. | ||
#[derive(Copy, Clone, Eq, PartialEq, Hash, Ord, PartialOrd, Debug)] | ||
pub enum Rid { | ||
/// A valid RID may refer to some resource, but is not guaranteed to do so. | ||
Valid(NonZeroU64), | ||
/// An invalid RID will never refer to a resource. Internally it is represented as a 0. | ||
Invalid, | ||
} | ||
|
||
impl Rid { | ||
/// Create a new RID. | ||
#[inline] | ||
pub const fn new(id: u64) -> Self { | ||
match NonZeroU64::new(id) { | ||
Some(id) => Self::Valid(id), | ||
None => Self::Invalid, | ||
} | ||
} | ||
|
||
/// Convert this RID into a [`u64`]. Returns 0 if it is invalid. | ||
/// | ||
/// _Godot equivalent: `Rid.get_id()`_ | ||
#[inline] | ||
pub const fn to_u64(self) -> u64 { | ||
match self { | ||
Rid::Valid(id) => id.get(), | ||
Rid::Invalid => 0, | ||
} | ||
} | ||
|
||
/// Convert this RID into a [`u64`] if it is valid. Otherwise return None. | ||
#[inline] | ||
pub const fn to_valid_u64(self) -> Option<u64> { | ||
match self { | ||
Rid::Valid(id) => Some(id.get()), | ||
Rid::Invalid => None, | ||
} | ||
} | ||
|
||
/// Convert this RID into a [`NonZeroU64`]. | ||
#[inline] | ||
pub const fn to_non_zero_u64(self) -> Option<NonZeroU64> { | ||
match self { | ||
Rid::Valid(id) => Some(id), | ||
Rid::Invalid => None, | ||
} | ||
} | ||
|
||
/// Returns `true` if this is a valid RID. | ||
#[inline] | ||
pub const fn is_valid(&self) -> bool { | ||
matches!(self, Rid::Valid(_)) | ||
} | ||
|
||
/// Returns `true` if this is an invalid RID. | ||
#[inline] | ||
pub const fn is_invalid(&self) -> bool { | ||
matches!(self, Rid::Invalid) | ||
} | ||
} | ||
|
||
impl GodotFfi for Rid { | ||
ffi_methods! { type sys::GDExtensionTypePtr = *mut Self; .. } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
/* | ||
* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at https://mozilla.org/MPL/2.0/. | ||
*/ | ||
|
||
use std::{collections::HashSet, thread}; | ||
|
||
use godot::{ | ||
engine::RenderingServer, | ||
prelude::{inner::InnerRid, Color, Rid, Vector2}, | ||
}; | ||
|
||
use crate::{itest, suppress_godot_print}; | ||
|
||
#[itest] | ||
fn rid_equiv() { | ||
let invalid: Rid = Rid::Invalid; | ||
let valid: Rid = Rid::new((10 << 32) | 20); | ||
assert!(!InnerRid::from_outer(&invalid).is_valid()); | ||
assert!(InnerRid::from_outer(&valid).is_valid()); | ||
|
||
assert_eq!(InnerRid::from_outer(&invalid).get_id(), 0); | ||
assert_eq!(InnerRid::from_outer(&valid).get_id(), (10 << 32) | 20); | ||
} | ||
|
||
#[itest] | ||
fn canvas_set_parent() { | ||
// This originally caused UB, but still testing it here in case it breaks. | ||
let mut server = RenderingServer::singleton(); | ||
let canvas = server.canvas_create(); | ||
let viewport = server.viewport_create(); | ||
|
||
suppress_godot_print(|| server.canvas_item_set_parent(viewport, canvas)); | ||
suppress_godot_print(|| server.canvas_item_set_parent(viewport, viewport)); | ||
|
||
server.free_rid(canvas); | ||
server.free_rid(viewport); | ||
} | ||
|
||
#[itest] | ||
fn multi_thread_test() { | ||
let threads = (0..10) | ||
.map(|_| { | ||
thread::spawn(|| { | ||
let mut server = RenderingServer::singleton(); | ||
(0..1000).map(|_| server.canvas_item_create()).collect() | ||
}) | ||
}) | ||
.collect::<Vec<_>>(); | ||
|
||
let mut rids: Vec<Rid> = vec![]; | ||
|
||
for thread in threads.into_iter() { | ||
rids.append(&mut thread.join().unwrap()); | ||
} | ||
|
||
let set = rids.iter().cloned().collect::<HashSet<_>>(); | ||
assert_eq!(set.len(), rids.len()); | ||
|
||
let mut server = RenderingServer::singleton(); | ||
|
||
for rid in rids.iter() { | ||
server.canvas_item_add_circle(*rid, Vector2::ZERO, 1.0, Color::from_rgb(1.0, 0.0, 0.0)); | ||
} | ||
|
||
for rid in rids.iter() { | ||
server.free_rid(*rid); | ||
} | ||
} | ||
|
||
/// Check that godot does not crash upon receiving various RIDs that may be edge cases. As it could do in Godot 3. | ||
#[itest] | ||
fn strange_rids() { | ||
let mut server = RenderingServer::singleton(); | ||
let mut rids: Vec<u64> = vec![ | ||
// Invalid RID. | ||
0, | ||
// Normal RID, should work without issue. | ||
1, | ||
10, | ||
// Testing the boundaries of various ints. | ||
u8::MAX as u64, | ||
u16::MAX as u64, | ||
u32::MAX as u64, | ||
u64::MAX, | ||
i8::MIN as u64, | ||
i8::MAX as u64, | ||
i16::MIN as u64, | ||
i16::MAX as u64, | ||
i32::MIN as u64, | ||
i32::MAX as u64, | ||
i64::MIN as u64, | ||
i64::MAX as u64, | ||
// Biggest RIDs possible in Godot (ignoring local indices). | ||
0xFFFFFFFF << 32, | ||
0x7FFFFFFF << 32, | ||
// Godot's servers treats RIDs as two u32s, so testing what happens round the region where | ||
// one u32 overflows into the next. | ||
u32::MAX as u64 + 1, | ||
u32::MAX as u64 + 2, | ||
u32::MAX as u64 - 1, | ||
u32::MAX as u64 - 2, | ||
// A couple random RIDs. | ||
1234567891011121314, | ||
14930753991246632225, | ||
8079365198791785081, | ||
10737267678893224303, | ||
12442588258967011829, | ||
4275912429544145425, | ||
]; | ||
// Checking every number with exactly 2 bits = 1. | ||
// An approximation of exhaustively checking every number. | ||
for i in 0..64 { | ||
for j in 0..63 { | ||
if j >= i { | ||
rids.push((1 << i) | (1 << (j + 1))) | ||
} else { | ||
rids.push((1 << i) | (1 << j)) | ||
} | ||
} | ||
} | ||
|
||
for id in rids.iter() { | ||
suppress_godot_print(|| server.canvas_item_clear(Rid::new(*id))) | ||
} | ||
} |