Skip to content

Commit

Permalink
Add support for precision=double
Browse files Browse the repository at this point in the history
add transform2d/3d-itests back
Add --double to check.sh
Make check.sh run in the stable toolchain, since that's what the CI does
  • Loading branch information
lilizoey committed Mar 8, 2023
1 parent 7d42ebb commit 341bc96
Show file tree
Hide file tree
Showing 28 changed files with 740 additions and 386 deletions.
69 changes: 61 additions & 8 deletions .github/workflows/full-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,20 @@ jobs:


clippy:
name: clippy (${{ matrix.name }})
runs-on: ubuntu-20.04
strategy:
fail-fast: false
matrix:
include:
- name: linux
rust-toolchain: stable
godot-binary: godot.linuxbsd.editor.dev.x86_64

- name: linux-double
rust-toolchain: stable
godot-binary: godot.linuxbsd.editor.dev.double.x86_64
rust-extra-args: --features double-precision
steps:
- uses: actions/checkout@v3

Expand All @@ -56,13 +69,14 @@ jobs:
- name: "Install Godot"
uses: ./.github/composite/godot-install
with:
artifact-name: godot-linux
godot-binary: godot.linuxbsd.editor.dev.x86_64
artifact-name: godot-${{ matrix.name }}
godot-binary: ${{ matrix.godot-binary }}

- name: "Check clippy"
run: |
cargo clippy --all-targets $GDEXT_FEATURES -- -D clippy::suspicious -D clippy::style -D clippy::complexity -D clippy::perf \
-D clippy::dbg_macro -D clippy::todo -D clippy::unimplemented -D warnings
cargo clippy --all-targets $GDEXT_FEATURES ${{ matrix.rust-extra-args }} -- \
-D clippy::suspicious -D clippy::style -D clippy::complexity -D clippy::perf \
-D clippy::dbg_macro -D clippy::todo -D clippy::unimplemented -D warnings
unit-test:
Expand All @@ -80,12 +94,24 @@ jobs:
os: macos-11
rust-toolchain: stable
godot-binary: godot.macos.editor.dev.x86_64
with-llvm: true

- name: macos-double
os: macos-11
rust-toolchain: stable
godot-binary: godot.macos.editor.dev.double.x86_64
with-llvm: true

- name: windows
os: windows-latest
rust-toolchain: stable-x86_64-pc-windows-msvc
godot-binary: godot.windows.editor.dev.x86_64.exe

- name: windows-double
os: windows-latest
rust-toolchain: stable-x86_64-pc-windows-msvc
godot-binary: godot.windows.editor.dev.double.x86_64.exe

# Don't use latest Ubuntu (22.04) as it breaks lots of ecosystem compatibility.
# If ever moving to ubuntu-latest, need to manually install libtinfo5 for LLVM.
- name: linux
Expand All @@ -98,6 +124,12 @@ jobs:
rust-toolchain: stable
rust-special: -minimal-deps
godot-binary: godot.linuxbsd.editor.dev.x86_64

- name: linux-double
os: ubuntu-20.04
rust-toolchain: stable
godot-binary: godot.linuxbsd.editor.dev.double.x86_64
rust-extra-args: --features double-precision

steps:
- uses: actions/checkout@v3
Expand All @@ -107,7 +139,7 @@ jobs:
with:
rust: stable
cache-key: ${{ matrix.rust-special }} # 'minimal-deps' or empty/not defined
with-llvm: ${{ matrix.name == 'macos' }}
with-llvm: ${{ matrix.with-llvm }}

- name: "Install Rust nightly (minimal deps)"
uses: ./.github/composite/rust
Expand All @@ -129,10 +161,10 @@ jobs:
godot-binary: ${{ matrix.godot-binary }}

- name: "Compile tests"
run: cargo test $GDEXT_FEATURES --no-run
run: cargo test $GDEXT_FEATURES --no-run ${{ matrix.rust-extra-args }}

- name: "Test"
run: cargo test $GDEXT_FEATURES
run: cargo test $GDEXT_FEATURES ${{ matrix.rust-extra-args }}


godot-itest:
Expand All @@ -151,12 +183,26 @@ jobs:
os: macos-12
rust-toolchain: stable
godot-binary: godot.macos.editor.dev.x86_64
with-llvm: true

- name: macos-double
os: macos-12
rust-toolchain: stable
godot-binary: godot.macos.editor.dev.double.x86_64
rust-extra-args: --features double-precision
with-llvm: true

- name: windows
os: windows-latest
rust-toolchain: stable-x86_64-pc-windows-msvc
godot-binary: godot.windows.editor.dev.x86_64.exe

- name: windows-double
os: windows-latest
rust-toolchain: stable-x86_64-pc-windows-msvc
godot-binary: godot.windows.editor.dev.double.x86_64.exe
rust-extra-args: --features double-precision

# Don't use latest Ubuntu (22.04) as it breaks lots of ecosystem compatibility.
# If ever moving to ubuntu-latest, need to manually install libtinfo5 for LLVM.
- name: linux
Expand All @@ -182,6 +228,12 @@ jobs:
godot-binary: godot.linuxbsd.editor.dev.x86_64.llvm.san
godot-args: -- --disallow-focus

- name: linux-double
os: ubuntu-20.04
rust-toolchain: stable
godot-binary: godot.linuxbsd.editor.dev.double.x86_64
rust-extra-args: --features double-precision

steps:
- uses: actions/checkout@v3

Expand All @@ -191,7 +243,8 @@ jobs:
artifact-name: godot-${{ matrix.name }}
godot-binary: ${{ matrix.godot-binary }}
godot-args: ${{ matrix.godot-args }}
with-llvm: ${{ matrix.name == 'macos' }}
with-llvm: ${{ matrix.with-llvm }}
rust-extra-args: ${{ matrix.rust-extra-args }}


license-guard:
Expand Down
20 changes: 17 additions & 3 deletions .github/workflows/minimal-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,20 @@ jobs:


clippy:
name: clippy (${{ matrix.name }})
runs-on: ubuntu-20.04
strategy:
fail-fast: false
matrix:
include:
- name: linux
rust-toolchain: stable
godot-binary: godot.linuxbsd.editor.dev.x86_64

- name: linux-double
rust-toolchain: stable
godot-binary: godot.linuxbsd.editor.dev.double.x86_64
rust-extra-args: --features double-precision
steps:
- uses: actions/checkout@v3

Expand All @@ -56,12 +69,13 @@ jobs:
- name: "Install Godot"
uses: ./.github/composite/godot-install
with:
artifact-name: godot-linux
godot-binary: godot.linuxbsd.editor.dev.x86_64
artifact-name: godot-${{ matrix.name }}
godot-binary: ${{ matrix.godot-binary }}

- name: "Check clippy"
run: |
cargo clippy --all-targets $GDEXT_FEATURES -- -D clippy::suspicious -D clippy::style -D clippy::complexity -D clippy::perf \
cargo clippy --all-targets $GDEXT_FEATURES ${{ matrix.rust-extra-args }} -- \
-D clippy::suspicious -D clippy::style -D clippy::complexity -D clippy::perf \
-D clippy::dbg_macro -D clippy::todo -D clippy::unimplemented -D warnings
Expand Down
11 changes: 11 additions & 0 deletions Contributing.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,5 +60,16 @@ $ cargo fmt
$ check.sh clippy
```

## Real

Certain types in Godot use either a single or double-precision float internally, such as `Vector2`. When using these types we
use the `real` type instead of choosing either `f32` or `f64`. Thus our code is portable between Godot binaries compiled with
`precision=single` or `precision=double`.

To run the testing suite with `double-precision` enabled you may add `--double` to a `check.sh` invocation:
```
$ check.sh --double
```

[GitHub issue]: https://github.com/godot-rust/gdextension/issues
[Discord]: https://discord.gg/aKUCJ8rJsc
50 changes: 33 additions & 17 deletions check.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,10 @@
# Note: at the moment, there is a lot of useless recompilation.
# This should be better once unit tests and #[cfg] are sorted out.

# No args specified: do everything
if [ "$#" -eq 0 ]; then
args=("fmt" "clippy" "test" "itest")
else
args=("$@")
fi

# --help menu
for arg in "${args[@]}"; do
for arg in $@; do
if [ "$arg" == "--help" ]; then
echo "Usage: check.sh [<commands>]"
echo "Usage: check.sh [--double] [<commands>]"
echo ""
echo "Each specified command will be run (until one fails)."
echo "If no commands are specified, all checks are run (no doc; may take several minutes)."
Expand All @@ -32,13 +25,37 @@ for arg in "${args[@]}"; do
echo " doc generate docs for 'godot' crate"
echo " dok generate docs and open in browser"
echo ""
echo "Options:"
echo " --double run check with double-precision"
echo ""
echo "Examples:"
echo " check.sh fmt clippy"
echo " check.sh"
echo " check.sh --double clippy"
exit 0
fi
done

firstArg=1
toolchain=""
extraArgs=()

if [[ "$1" == "--double" ]]; then
firstArg=2
extraArgs+=("--features double-precision")
fi

args=()

for arg in "${@:$firstArg}"; do
args+=("$arg")
done

# No args specified: do everything
if [ ${#args[@]} -eq 0 ]; then
args=("fmt" "clippy" "test" "itest")
fi

# For integration tests
function findGodot() {
# User-defined GODOT4_BIN
Expand Down Expand Up @@ -67,32 +84,31 @@ function findGodot() {
fi
}

#features="--features crate/feature"
features=""
cmds=()
extraArgs="${extraArgs[@]}"

for arg in "${args[@]}"; do
case "$arg" in
fmt)
cmds+=("cargo fmt --all -- --check")
cmds+=("cargo $toolchain fmt --all -- --check")
;;
clippy)
cmds+=("cargo clippy $features -- -D clippy::suspicious -D clippy::style -D clippy::complexity -D clippy::perf -D clippy::dbg_macro -D clippy::todo -D clippy::unimplemented -D warnings")
cmds+=("cargo $toolchain clippy $extraArgs -- -D clippy::suspicious -D clippy::style -D clippy::complexity -D clippy::perf -D clippy::dbg_macro -D clippy::todo -D clippy::unimplemented -D warnings")
;;
test)
cmds+=("cargo test $features")
cmds+=("cargo $toolchain test $extraArgs")
;;
itest)
findGodot

cmds+=("cargo build -p itest")
cmds+=("cargo $toolchain build -p itest $extraArgs")
cmds+=("$godotBin --path itest/godot --headless")
;;
doc)
cmds+=("cargo doc --lib -p godot --no-deps $features")
cmds+=("cargo $toolchain doc --lib -p godot --no-deps $extraArgs")
;;
dok)
cmds+=("cargo doc --lib -p godot --no-deps $features --open")
cmds+=("cargo $toolchain doc --lib -p godot --no-deps $extraArgs --open")
;;
*)
echo "Unrecognized command '$arg'"
Expand Down
2 changes: 1 addition & 1 deletion examples/dodge-the-creeps/rust/src/main_scene.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ impl Main {
let range = rng.gen_range(mob.min_speed..mob.max_speed);

mob.set_linear_velocity(Vector2::new(range, 0.0));
let lin_vel = mob.get_linear_velocity().rotated(direction as f32);
let lin_vel = mob.get_linear_velocity().rotated(real::from_f64(direction));
mob.set_linear_velocity(lin_vel);
}

Expand Down
4 changes: 2 additions & 2 deletions examples/dodge-the-creeps/rust/src/mob.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ const MOB_TYPES: [MobType; 3] = [MobType::Walk, MobType::Swim, MobType::Fly];
#[derive(GodotClass)]
#[class(base=RigidBody2D)]
pub struct Mob {
pub min_speed: f32,
pub max_speed: f32,
pub min_speed: real,
pub max_speed: real,

#[base]
base: Base<RigidBody2D>,
Expand Down
4 changes: 2 additions & 2 deletions examples/dodge-the-creeps/rust/src/player.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use godot::prelude::*;
#[derive(GodotClass)]
#[class(base=Area2D)]
pub struct Player {
speed: f32,
speed: real,
screen_size: Vector2,

#[base]
Expand Down Expand Up @@ -100,7 +100,7 @@ impl GodotExt for Player {
animated_sprite.stop();
}

let change = velocity * delta as f32;
let change = velocity * real::from_f64(delta);
let position = self.base.get_global_position() + change;
let position = Vector2::new(
position.x.clamp(0.0, self.screen_size.x),
Expand Down
1 change: 1 addition & 0 deletions godot-codegen/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ categories = ["game-engines", "graphics"]
[features]
codegen-fmt = []
codegen-full = []
double-precision = []

[dependencies]
quote = "1"
Expand Down
3 changes: 3 additions & 0 deletions godot-codegen/src/api_parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,9 @@ pub fn load_extension_api(watch: &mut StopWatch) -> (ExtensionApi, &'static str)
// For float/double inference, see:
// * https://github.com/godotengine/godot-proposals/issues/892
// * https://github.com/godotengine/godot-cpp/pull/728
#[cfg(feature = "double-precision")]
let build_config = "double_64"; // TODO infer this
#[cfg(not(feature = "double-precision"))]
let build_config = "float_64"; // TODO infer this

let json: String = godot_exe::load_extension_api_json(watch);
Expand Down
1 change: 1 addition & 0 deletions godot-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ default = []
trace = []
codegen-fmt = ["godot-ffi/codegen-fmt"]
codegen-full = ["godot-codegen/codegen-full"]
double-precision = ["godot-codegen/double-precision"]

[dependencies]
godot-ffi = { path = "../godot-ffi" }
Expand Down
Loading

0 comments on commit 341bc96

Please sign in to comment.