68 lines
1.9 KiB
Luau
68 lines
1.9 KiB
Luau
--!strict
|
|
--!native
|
|
-- model/gltf/Pack: the byte loops of glTF accessors (gather strided elements, scatter, sparse substitution); callers
|
|
-- check every range first. glTF pads matrix columns to 4 bytes.
|
|
local Pack = {}
|
|
|
|
local bcopy, readu8, readu16, readu32 = buffer.copy, buffer.readu8, buffer.readu16, buffer.readu32
|
|
|
|
function Pack.gather(dst: buffer, src: buffer, off: number, count: number, elem: number, stride: number, cols: number, colBytes: number, colStride: number)
|
|
if cols == 1 or colBytes == colStride then
|
|
if stride == elem then
|
|
bcopy(dst, 0, src, off, count * elem)
|
|
return
|
|
end
|
|
for i = 0, count - 1 do
|
|
bcopy(dst, i * elem, src, off + i * stride, elem)
|
|
end
|
|
return
|
|
end
|
|
for i = 0, count - 1 do
|
|
local s = off + i * stride
|
|
local d = i * elem
|
|
for c = 0, cols - 1 do
|
|
bcopy(dst, d + c * colBytes, src, s + c * colStride, colBytes)
|
|
end
|
|
end
|
|
end
|
|
|
|
function Pack.scatter(dst: buffer, at: number, src: buffer, count: number, elem: number, stride: number, cols: number, colBytes: number, colStride: number)
|
|
if cols == 1 or colBytes == colStride then
|
|
if stride == elem then
|
|
bcopy(dst, at, src, 0, count * elem)
|
|
return
|
|
end
|
|
for i = 0, count - 1 do
|
|
bcopy(dst, at + i * stride, src, i * elem, elem)
|
|
end
|
|
return
|
|
end
|
|
for i = 0, count - 1 do
|
|
local d = at + i * stride
|
|
local s = i * elem
|
|
for c = 0, cols - 1 do
|
|
bcopy(dst, d + c * colStride, src, s + c * colBytes, colBytes)
|
|
end
|
|
end
|
|
end
|
|
|
|
function Pack.sparse(dst: buffer, count: number, elem: number, idx: buffer, idxOff: number, idxSize: number, values: buffer, valOff: number, n: number): boolean
|
|
for k = 0, n - 1 do
|
|
local i
|
|
if idxSize == 1 then
|
|
i = readu8(idx, idxOff + k)
|
|
elseif idxSize == 2 then
|
|
i = readu16(idx, idxOff + k * 2)
|
|
else
|
|
i = readu32(idx, idxOff + k * 4)
|
|
end
|
|
if i >= count then
|
|
return false
|
|
end
|
|
bcopy(dst, i * elem, values, valOff + k * elem, elem)
|
|
end
|
|
return true
|
|
end
|
|
|
|
return Pack
|