Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change c++ code to work with doubles #232

Merged
merged 3 commits into from
Oct 22, 2023

Conversation

SlashScreen
Copy link
Contributor

solves half of issue #30 - doesn't touch shader though

@SlashScreen
Copy link
Contributor Author

Don't merge this until I've gone through everything

@TokisanGames TokisanGames marked this pull request as draft October 19, 2023 08:15
@TokisanGames
Copy link
Owner

TokisanGames commented Oct 19, 2023

You know, thinking about it I think its fine that all instances here are switched to real_t. I was thinking using set_cull_margin(real_t) is a waste of space using a double when only a float is needed. But if we're calling it from gdscript what is coming in is a double anyway.

The only concern I have is with saved variables. Say version for instance. If it's saved as a float into Storage. Then later read by a double engine, is it seamlessly converted? If it's saved as a double, can it be opened by a float engine without issue? If version and all of the other data can be read and written by both engines then it's fine to do everything as real_t.

Also include a line somewhere like __ready() that prints LOG(ERROR, "Float: ", sizeof(float), " Real_t: ", sizeof(real_t)); and include the result here so we can make sure they are different.

@SlashScreen
Copy link
Contributor Author

Asked AThousandShips:

Me:
If a double (f64) value is serialized into a text file, and then opened using a single-precision compiled engine, what happens?
is it just truncated as a float, as if you were casting
And what about the reverse?

AThousandShips: 
Again most are stored as double, just some types rely on the precision setting
Like Vector2/3/4, Basis, Transform2D/3D, etc.
Most cases of float in the engine are f64, and serialisation isn't affected unless you're storing a type that depends

Shouldn't be an issue.

@SlashScreen
Copy link
Contributor Author

SlugFiller:
Also, serialization into a text file truncates floating point way below the limit of single. There's an issue, and I believe, also a PR about it

@@ -75,7 +75,7 @@ void Terrain3D::_initialize() {

void Terrain3D::__ready() {
_initialize();
LOG(WARN, "Float: ", sizeof(float), " Real_t: ", sizeof(real_t));
LOG(WARN, "Float: ", Variant(sizeof(float)), " Real_t: ", Variant(sizeof(real_t)));
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't need these in the repo, I just want you to run it once to confirm that building with precision=double does something, and I'd like you to post your results in the comments.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, Ok

@SlashScreen
Copy link
Contributor Author

Got it to build, as I said in discord. Let me know if I need to change anything.

@SlashScreen SlashScreen marked this pull request as ready for review October 21, 2023 07:08
godot-cpp Outdated
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove from PR. I will update godot-cpp later. Current commit tag is godot-4.1.1-stable

@@ -12,7 +12,7 @@ config_version=5

config/name="Terrain3D"
run/main_scene="res://demo/Demo.tscn"
config/features=PackedStringArray("4.1")
config/features=PackedStringArray("4.1", "Double Precision")
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove from PR. Users can enable this

@@ -75,6 +75,7 @@ void Terrain3D::_initialize() {

void Terrain3D::__ready() {
_initialize();
// LOG(WARN, "Float: ", Variant(int(sizeof(float))), " Real_t: ", Variant(int(sizeof(real_t))));
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove

src/util.cpp Outdated
@@ -58,7 +58,7 @@ Vector2 Util::get_min_max(const Ref<Image> p_image) {
}

/**
* Returns a Image of a float heightmap normalized to RGB8 greyscale and scaled
* Returns a Image of a real_t heightmap normalized to RGB8 greyscale and scaled
Copy link
Owner

@TokisanGames TokisanGames Oct 21, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Change to: Returns a scaled, RGB8 greyscale Image of a heightmap normalized to its heights

@SlashScreen
Copy link
Contributor Author

oh dear. Will me removing godot-cpp from this PR cause issues in the main branch? The CI is freaking out right now.

float alpha_clip = (brush_alpha < 0.1f) ? 0.0f : 1.0f;
int index_base = int(src.r * 255.0f);
int index_overlay = int(src.g * 255.0f);
real_t alpha_clip = (brush_alpha < real_t(0.1f)) ? 0.0f : 1.0f;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Was there any sort of warning from the compiler comparing real_t brush_alpha and 0.1f?

If not, let's remove all of the literal conversions. I now think we only need the real_t conversions for ints. For floats to doubles it is free and unnecessary, unless there are warnings. Let's remove them all from here down to the last lerp on line 233.

https://stackoverflow.com/questions/13045485/division-of-double-with-a-float

The only need I see is where it is casting dest_index

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There were warnings when it came to function parameters; math.lerp didn't like an argument list of mixed floats and real_ts. I can remove the ones outside of those parameters, though.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add any casts to remove warnings. Otherwise, we don't need it for float to double.

@TokisanGames
Copy link
Owner

I looked closer at the godot source. Excluding modules & thirdparty code, .h/.cpp lines using float: 10212, real_t: 6091.

There are only 2 usages of float in all of the physics servers, none in navigation.

Rendering is 99% float, needed only for locations or distance. The other uses are unnecessary.

The moviewriter is a new addition and has 3 floats, 0 real_t.

So my conclusions are:

  1. it appears to be used where it's needed, so hopefully doubles actually does work.
  2. Usage is all over the place and not very disciplined. When real_t is used in files, it is often used on inconsequential things and who knows if double support is completed and actually works.
  3. Blanket search and replace will waste space for those using it, but unlikely to cause a problem.
  4. Literals like 5.0 are doubles, and 5.0f are floats. Godot has real_t x = 1.0f; all over, so I don't think we need an explicit conversion in many cases unless the compiler produces a warning.

This link says float to double conversion on x87 fpu is free, and I've seen that other places as well.
https://stackoverflow.com/questions/13045485/division-of-double-with-a-float

So I think for consistency like the physics and navigation servers, we can use real_t everywhere. Let's wrap up this and merge it in. The shader portion can come separately.

Please make the following changes:

  • Address all the individual comments I made
  • Remove the changes to godot-cpp and project.godot. I will update the former soon along with a version upgrade. The godot-cpp tag is godot-4.1.1-stable
  • Squash your changes somewhat or completely
  • Rebase

@SlashScreen
Copy link
Contributor Author

I've addressed the individual comments. I will soon remove explicit conversions, squash (hopefully I don't screw it up this time), and rebase.

@TokisanGames
Copy link
Owner

oh dear. Will me removing godot-cpp from this PR cause issues in the main branch? The CI is freaking out right now.

I don't mean removing godot-cpp from your repository, aka, deleting the folder and pushing that change. I mean reverting your godot-cpp to the previous version (git checkout godot-4.1.1-stable) and pushing that. That will revert the change you made and reset it back to the version in the main branch.

@TokisanGames TokisanGames merged commit 5b69f3a into TokisanGames:main Oct 22, 2023
@TokisanGames
Copy link
Owner

Great job, thank you. In the future don't use git merge. You can do anything else like rebase, but let github do all merging.

@SlashScreen
Copy link
Contributor Author

SlashScreen commented Oct 22, 2023 via email

@TokisanGames TokisanGames mentioned this pull request Oct 22, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants