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

buffer: copy unaligned data in GetFloatData #15247

Merged
merged 2 commits into from
Jul 9, 2024
Merged
Show file tree
Hide file tree
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
6 changes: 4 additions & 2 deletions packages/dev/core/src/Buffers/buffer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1025,7 +1025,7 @@ export class VertexBuffer {
} else if (data instanceof ArrayBuffer) {
return new Float32Array(data, byteOffset, count);
} else {
let offset = data.byteOffset + byteOffset;
const offset = data.byteOffset + byteOffset;
if (forceCopy) {
const result = new Float32Array(count);
const source = new Float32Array(data.buffer, offset, count);
Expand All @@ -1039,7 +1039,9 @@ export class VertexBuffer {
const remainder = offset % 4;

if (remainder) {
offset = Math.max(0, offset - remainder);
Logger.Warn("GetFloatData: copied misaligned data.");
// If not aligned, copy the data to aligned buffer
bghgary marked this conversation as resolved.
Show resolved Hide resolved
return new Float32Array(data.buffer.slice(offset, offset + count * 4));
}

return new Float32Array(data.buffer, offset, count);
Expand Down
8 changes: 6 additions & 2 deletions packages/dev/core/src/Buffers/bufferUtils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { DataArray } from "../types";
import { VertexBuffer } from "./buffer";
import { Logger } from "../Misc/logger";

/**
* Copies the given data array to the given float array.
Expand Down Expand Up @@ -41,12 +42,15 @@ export function CopyFloatData(
const floatData = new Float32Array(input, byteOffset, count);
output.set(floatData);
} else {
let offset = input.byteOffset + byteOffset;
const offset = input.byteOffset + byteOffset;

// Protect against bad data
const remainder = offset % 4;
if (remainder) {
offset = Math.max(0, offset - remainder);
Logger.Warn("CopyFloatData: copied misaligned data.");
// If not aligned, copy the data to aligned buffer
output.set(new Float32Array(input.buffer.slice(offset, offset + count * 4)));
return;
}

const floatData = new Float32Array(input.buffer, offset, count);
Expand Down