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

Add half precision floating point support to StreamPeer and FileAccess #97716

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

pafuent
Copy link
Contributor

@pafuent pafuent commented Oct 2, 2024

Closes godotengine/godot-proposals#5983

Adds put/get methods to StreamPeer that handles half precision floating point values.
Adds endode/decode half precision floating point to marshalls.

This PR is rebase into master of #78872. Also I added unit tests to it.

@pafuent pafuent requested review from a team as code owners October 2, 2024 03:44
@fire fire requested a review from a team October 2, 2024 04:55
Copy link
Contributor

@RadiantUwU RadiantUwU left a comment

Choose a reason for hiding this comment

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

Shouldnt we extend it to FileAccess and PackedByteArray now given the chance to do so?

@pafuent
Copy link
Contributor Author

pafuent commented Oct 2, 2024

PackedByteArray already have those:
encode_half binded here and implemented here
decode_half binded here and implemented here

@pafuent pafuent force-pushed the add_half_precision_floating_point_to_stream_peer branch from 1f108d2 to e00cbe8 Compare October 2, 2024 11:07
@pafuent pafuent force-pushed the add_half_precision_floating_point_to_stream_peer branch 2 times, most recently from 1aea23d to 19fd983 Compare October 3, 2024 12:09
@pafuent pafuent requested a review from a team as a code owner October 3, 2024 12:09
@RadiantUwU
Copy link
Contributor

RadiantUwU commented Oct 3, 2024

We should change the title and the topics since now it also modifies FileAccess (core)

@AThousandShips AThousandShips changed the title Add half precision floating point support to StreamPeer Add half precision floating point support to StreamPeer and FileAccess Oct 3, 2024
@pafuent pafuent force-pushed the add_half_precision_floating_point_to_stream_peer branch from 19fd983 to 58fe877 Compare October 17, 2024 13:33
@pafuent
Copy link
Contributor Author

pafuent commented Oct 17, 2024

Friendly remainder

Copy link
Member

@Calinou Calinou left a comment

Choose a reason for hiding this comment

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

Tested locally, it works as expected.

Thanks for adding unit tests 🙂

extends Node2D


func _ready() -> void:
	var fa := FileAccess.open("user://output.bin", FileAccess.WRITE_READ)
	fa.store_half(0.123456)
	fa.store_float(0.123456)
	fa.store_double(0.123456)

	fa.store_double(-12345)
	fa.store_float(-12345)
	fa.store_half(-12345)

	OS.shell_open(ProjectSettings.globalize_path("user://"))
	get_tree().quit()

Using ImHex to view the file written by Godot:

image

image

Note the lack of precision on the second example, as half floats can't represent all integer values outside of [-2048; 2048].

tests/core/io/test_file_access.h Outdated Show resolved Hide resolved
tests/core/io/test_file_access.h Outdated Show resolved Hide resolved
tests/core/io/test_file_access.h Outdated Show resolved Hide resolved
tests/core/io/test_file_access.h Outdated Show resolved Hide resolved
tests/core/io/test_file_access.h Outdated Show resolved Hide resolved
tests/core/io/test_file_access.h Outdated Show resolved Hide resolved
tests/core/io/test_file_access.h Outdated Show resolved Hide resolved
tests/core/io/test_file_access.h Outdated Show resolved Hide resolved
Closes godotengine/godot-proposals#5983

Adds put/get methods to `StreamPeer` that handles half precision
floating point values.
Adds endode/decode half precision floating point to `marshalls`.
Adds `get_half` and `store_half` to `FileAccess`

Co-Authored-By: "Alfonso J. Ramos" <[email protected]>
@pafuent pafuent force-pushed the add_half_precision_floating_point_to_stream_peer branch from 58fe877 to 189ee5f Compare November 1, 2024 19:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants