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

fix TypedArray set #17817

Merged
merged 2 commits into from
Nov 11, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions native/cocos/core/TypedArray.h
Original file line number Diff line number Diff line change
Expand Up @@ -192,9 +192,9 @@ class TypedArrayTemp {
}

void set(ArrayBuffer *buffer, uint32_t offset) {
CC_ASSERT(buffer->byteLength() + offset <= _byteEndPos);
CC_ASSERT(buffer->byteLength() + _byteOffset + offset * BYTES_PER_ELEMENT <= _byteEndPos);
CC_ASSERT(_buffer);
memcpy(_buffer->_data + offset, buffer->_data, buffer->byteLength());
memcpy(_buffer->_data + _byteOffset + offset * BYTES_PER_ELEMENT, buffer->_data, buffer->byteLength());
Copy link
Contributor

Choose a reason for hiding this comment

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

Good catch.
In this case, I think another two set methods should also consider the current _byteOffset while copying data.

template <typename T>
template <typename SrcType>
typename std::enable_if_t<std::is_same<T, SrcType>::value, void> TypedArrayTemp<T>::set(const TypedArrayTemp<SrcType> &array, uint32_t offset) {
    CC_ASSERT(_buffer);
    uint32_t dstByteOffset = offset * BYTES_PER_ELEMENT;
    uint32_t srcByteOffset = array.byteOffset();
    uint32_t srcCount = array.length();
    CC_ASSERT(dstByteOffset + srcCount * TypedArrayTemp<SrcType>::BYTES_PER_ELEMENT <= _byteEndPos);
    memcpy(_buffer->_data + dstByteOffset, array._buffer->_data + srcByteOffset, array.byteLength());
}
template <typename T>
template <typename SrcType>
typename std::enable_if_t<!std::is_same<T, SrcType>::value, void> TypedArrayTemp<T>::set(const TypedArrayTemp<SrcType> &array, uint32_t offset) {
    CC_ASSERT(_buffer);
    uint32_t dstByteOffset = offset * BYTES_PER_ELEMENT;
    uint32_t srcByteOffset = array.byteOffset();
    uint32_t srcCount = array.length();
    uint32_t remainCount = (_byteEndPos - dstByteOffset) / BYTES_PER_ELEMENT;
    CC_ASSERT_LE(srcCount, remainCount);
    for (uint32_t i = 0; i < srcCount; ++i) {
        (*this)[offset + i] = reinterpret_cast<T>(array[i]);
    }
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Add a commit to fix these function templates.

}

template <typename SrcType>
Expand Down Expand Up @@ -291,7 +291,7 @@ template <typename T>
template <typename SrcType>
typename std::enable_if_t<std::is_same<T, SrcType>::value, void> TypedArrayTemp<T>::set(const TypedArrayTemp<SrcType> &array, uint32_t offset) {
CC_ASSERT(_buffer);
uint32_t dstByteOffset = offset * BYTES_PER_ELEMENT;
uint32_t dstByteOffset = _byteOffset + offset * BYTES_PER_ELEMENT;
uint32_t srcByteOffset = array.byteOffset();
uint32_t srcCount = array.length();
CC_ASSERT(dstByteOffset + srcCount * TypedArrayTemp<SrcType>::BYTES_PER_ELEMENT <= _byteEndPos);
Expand All @@ -302,7 +302,7 @@ template <typename T>
template <typename SrcType>
typename std::enable_if_t<!std::is_same<T, SrcType>::value, void> TypedArrayTemp<T>::set(const TypedArrayTemp<SrcType> &array, uint32_t offset) {
CC_ASSERT(_buffer);
uint32_t dstByteOffset = offset * BYTES_PER_ELEMENT;
uint32_t dstByteOffset = _byteOffset + offset * BYTES_PER_ELEMENT;
uint32_t srcByteOffset = array.byteOffset();
uint32_t srcCount = array.length();
uint32_t remainCount = (_byteEndPos - dstByteOffset) / BYTES_PER_ELEMENT;
Expand Down
Loading