-
-
Notifications
You must be signed in to change notification settings - Fork 23
vtfpp: byte order portability #79
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
Open
partyvan
wants to merge
34
commits into
craftablescience:main
Choose a base branch
from
partyvan:vtfpp-endian
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
34 commits
Select commit
Hold shift + click to select a range
68905dc
vtfpp: maintain byte order of underlying buffers
partyvan 07aaa98
vtfpp: ImageConversion: disambiguate LERep conversions
partyvan 91bcd67
sourcepp: adopt LERep from vtfpp
partyvan 986a5aa
sourcepp: Bits: ergonomics+docs
partyvan ae0e619
docs: let doxygen know about some alias-declaring macros
partyvan 798731e
feat(ext): allow exotic architectures to provide their own compresson…
partyvan 7ab8776
sourcepp: more LERep constructors + convenient read
partyvan 6206f0d
vtfpp: start using LERep where applicable
partyvan bb5d864
Merge branch 'main' into vtfpp-endian
partyvan db8764c
vtfpp: ImagePixelV2
partyvan 1f4cf9b
vtfpp: low-hanging fruit for ImagePixelV2
partyvan 25e5282
sourcepp: add SOURCEPP_REVERSE to macros and clean up helper names
partyvan 2495698
vtfpp: ImagePixelV2: reverse offsets in BITS declarations (oopsie)
partyvan 9694ad5
vtfpp: ImagePixelV2: make BITS pixel formats use LERep!
partyvan a82ddfd
fix(cmake): enable compliant preprocessor on msvc
partyvan ad37b2d
vtfpp: ImagePixelV2: constexpr correctness
partyvan a761f0c
sourcepp: reinterpret_le -> deref_as_le
partyvan d83a760
vtfpp: account for byte order sensitivity in compressonator
partyvan e816e6b
vtfpp: use f32le when cubemapping
partyvan e0e722f
vtfpp: ImagePixelV2: more conster more gooder
partyvan f5bde62
vtfpp: more ImagePixelV2
partyvan 4688d22
vtfpp: all usage of ImagePixel replaced by ImagePixelV2
partyvan a530687
vtfpp: ImagePixelV2 simple declarations: store LERep; callable as bas…
partyvan 8cbcea6
vtfpp: add image format tests
partyvan 7ae949e
sourcepp: why was this public in the first place
partyvan 1d9a33e
vtfpp: fix qoi parse on BE
partyvan 7ba4aea
vtfpp: lift execution conditionals to a dedicated macro; deduplicate …
partyvan 2e9f78b
vtfpp: make 16 bit stb reads portable
partyvan 45fa7a1
vtfpp: add vtf read order checks to tests as well
partyvan 2d300ff
vtfpp: give extfmt_img tests corresponding roundtrip attempts
partyvan 5d03779
vtfpp: back to native during resize
partyvan 72083c9
vtfpp: compressed write tests
partyvan c58f776
Merge remote-tracking branch 'upstream/main' into vtfpp-endian
partyvan fcdd59c
fix(ext): cmake indentation
partyvan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,102 @@ | ||
| #pragma once | ||
|
|
||
| #include <type_traits> | ||
|
|
||
| #include <sourcepp/Math.h> | ||
|
|
||
| namespace sourcepp::bits { | ||
|
|
||
| /// A view of a given numeric type `A` that is always little-endian in memory. | ||
| /// Typically becomes no-op at `-O2` on LE targets. | ||
| /// | ||
| /// Any instance of `*reinterpret_cast<NUMERIC_TYPE *>` etc., or use of numeric types | ||
| /// in structures destined for I/O buffers is suspect, and, in either case, should likely | ||
| /// be replaced by some invocation of this class. This can be syntactically cumbersome, | ||
| /// so convenient aliases are provided with f32le, etc. | ||
| /// | ||
| /// Special provisions for arithmetic outside possible implicit conversions are intentionally omitted. | ||
| template<sourcepp::math::Arithmetic A> | ||
| class LERep : private std::array<std::byte, sizeof(A)> { | ||
| using uint_according = typename std::conditional<sizeof(A) == 8, | ||
| uint64_t, | ||
| typename std::conditional<sizeof(A) == 4, | ||
| uint32_t, | ||
| typename std::conditional<sizeof(A) == 2, | ||
| uint16_t, | ||
| uint8_t>::type>::type>::type; | ||
| public: | ||
|
|
||
| /// Conversion to native byte order. | ||
| /// `LERep<A>` ⇒ `A`. | ||
| constexpr operator A() const { | ||
| uint_according ret = 0; | ||
| for (size_t offs = 0; auto &b : *this) { | ||
| ret |= (static_cast<uint_according>(b) << offs) & (uint_according(0xFFu) << offs); | ||
| offs += 8; | ||
| } | ||
| return *reinterpret_cast<A *>(&ret); | ||
| } | ||
|
|
||
| /// Conversion from native byte order. | ||
| /// `A` ⇒ `LERep<A>`. | ||
| constexpr LERep &operator=(const A &v) { | ||
| auto in = *reinterpret_cast<const uint_according *>(&v); | ||
| for (size_t offs = 0; auto &b : *this) { | ||
| b = static_cast<std::byte>((in >> offs) & uint_according(0xFFu)); | ||
| offs += 8; | ||
| } | ||
| return *this; | ||
| } | ||
|
|
||
| /// Convenience for arithmetic conversion from some B. | ||
| /// (`B` ⇒ `A`) ⇒ (`B` ⇒ `LERep<A>`). | ||
| template<sourcepp::math::Arithmetic B> requires (std::convertible_to<B, A> || std::is_same_v<A, half>) | ||
| constexpr LERep(const B &u) { this->operator=(static_cast<A>(u)); } | ||
|
|
||
| /// Convenience for arithmetic conversion between LERep of distinct but arithmetically convertible types. | ||
| /// (`B` ⇒ `A`) ⇒ (`LERep<B>` ⇒ `LERep<A>`). | ||
| template<sourcepp::math::Arithmetic B> requires std::convertible_to<B, A> | ||
| constexpr LERep(const LERep<B> &u) { this->operator=(u.operator A()); } | ||
|
|
||
| /// Convenience for arithmetic conversion to some B. | ||
| /// (`LERep<A>` ⇒ `A`) ∧ (`A` ⇒ `B`) ⇒ (`LERep<A>` ⇒ `B`). | ||
| template<sourcepp::math::Arithmetic B> requires std::convertible_to<A, B> | ||
| constexpr operator B() const { return static_cast<B>(this->operator A()); } | ||
|
|
||
| /// Permits uninitialized LERep, for parity with the associated A. | ||
| constexpr LERep() {} | ||
|
|
||
| /// Construct a value from a byte buffer. | ||
| constexpr explicit LERep(std::span<const std::byte, sizeof(A)> arr) { | ||
| std::memcpy(this, arr.data(), sizeof(A)); | ||
| } | ||
| }; | ||
|
|
||
| /// Read a given value of type A from a pointer into arbitrary data presumed to hold a little-endian representation of A. | ||
| /// Subject to the usual litany of cautions about | ||
| /// | ||
| /// *reinterpret_cast<T *>(p) | ||
| /// | ||
| /// , such invocations, where spuriously dependent upon host byte order, may be replaced with: | ||
| /// | ||
| /// deref_as_le<T>(p) | ||
| template<sourcepp::math::Arithmetic A> | ||
| [[nodiscard]] constexpr A deref_as_le(const void *p) { | ||
| return LERep<A>(std::span<const std::byte, sizeof(A)>(static_cast<const std::byte *>(p), sizeof(A))).operator A(); | ||
| } | ||
|
|
||
| #define SOURCEPP_LEREP_DEFINE(N, V)\ | ||
| using N##le = LERep<V> | ||
| #define SOURCEPP_LEREP_DEFINE_P(PFX, N16, N32, N64) \ | ||
| SOURCEPP_LEREP_DEFINE(PFX##16, N16); \ | ||
| SOURCEPP_LEREP_DEFINE(PFX##32, N32); \ | ||
| SOURCEPP_LEREP_DEFINE(PFX##64, N64) | ||
|
|
||
| SOURCEPP_LEREP_DEFINE_P(i, int16_t, int32_t, int64_t); | ||
| SOURCEPP_LEREP_DEFINE_P(ui, uint16_t, uint32_t, uint64_t); | ||
| SOURCEPP_LEREP_DEFINE_P(f, half, float, double); | ||
|
|
||
| #undef SOURCEPP_LEREP_DEFINE_P | ||
| #undef SOURCEPP_LEREP_DEFINE | ||
|
|
||
| } // namespace sourcepp::bits |
This file contains hidden or 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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
cmake/AddPrettyParser.cmakedoes this too, is this necessary to pull to a larger scope?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i don't think it was applying in all the places
ImageConversion.hwas getting included, but i can double check