mirror of
https://github.com/ProjectLeonida/mandy.git
synced 2026-12-06 08:58:36 +00:00
Merge pull request #1754 from kirigayakazima/codex/glb-skinning-padded-buffer-copies
glb_skinning: avoid copying padded BIN data for every accessor
This commit is contained in:
@ -648,6 +648,35 @@ class GlbSkinningTests(unittest.TestCase):
|
||||
self.assertIn("must not overwrite the input", proc.stderr)
|
||||
self.assertEqual(before, path.read_bytes())
|
||||
|
||||
def test_analysis_does_not_copy_large_unused_payloads(self):
|
||||
doc, binary, _ = glb_skinning.parse_glb(make_glb())
|
||||
size = 4 * 1024 * 1024
|
||||
doc["buffers"][0]["byteLength"] = size - 1
|
||||
raw = build_glb(doc, binary.ljust(size - 1, b"\0"))
|
||||
extension = struct.pack("<II", size, 0x12345678) + bytes(size)
|
||||
raw = raw[:8] + struct.pack("<I", len(raw) + len(extension)) + raw[12:] + extension
|
||||
path = self.write("padded.glb", raw)
|
||||
measure = """
|
||||
import sys, tracemalloc
|
||||
from pathlib import Path
|
||||
sys.path.insert(0, sys.argv[1])
|
||||
import glb_skinning
|
||||
data = Path(sys.argv[2]).read_bytes()
|
||||
tracemalloc.start()
|
||||
report = glb_skinning.analyze_glb(data)
|
||||
assert report["ok"], report["errors"]
|
||||
print(tracemalloc.get_traced_memory()[1])
|
||||
"""
|
||||
peak = subprocess.check_output([sys.executable, "-I", "-c", measure, str(TOOLS), str(path)], timeout=60)
|
||||
self.assertLess(int(peak), size, "inspection allocated a full unused payload")
|
||||
|
||||
def test_accessor_cannot_read_the_bin_padding(self):
|
||||
doc = {"buffers": [{"byteLength": 3}],
|
||||
"bufferViews": [{"buffer": 0, "byteLength": 4}],
|
||||
"accessors": [{"bufferView": 0, "componentType": 5121, "count": 1, "type": "VEC4"}]}
|
||||
with self.assertRaisesRegex(glb_skinning.GlbSkinError, "exceeds buffer 0"):
|
||||
glb_skinning._Reader(doc, bytes(4)).read(0, "JOINTS_0")
|
||||
|
||||
def test_accessor_byte_stride_and_offset(self):
|
||||
joints = ((0, 0, 0, 0), (0, 1, 0, 0), (1, 1, 0, 0))
|
||||
weights = ((1.0, 0.0, 0.0, 0.0),
|
||||
|
||||
@ -144,7 +144,12 @@ def _reject_json_constant(token):
|
||||
|
||||
|
||||
def parse_glb(data):
|
||||
"""Return ``(gltf, bin_chunk, container)`` for a binary glTF 2.0 container."""
|
||||
"""Return the JSON, embedded BIN bytes (if present) and container metadata."""
|
||||
gltf, bin_view, container = _parse_chunks(data)
|
||||
return gltf, bytes(bin_view) if bin_view is not None else None, container
|
||||
|
||||
|
||||
def _parse_chunks(data):
|
||||
if not isinstance(data, (bytes, bytearray, memoryview)):
|
||||
raise GlbSkinError("GLB data must be bytes-like")
|
||||
raw = bytes(data)
|
||||
@ -178,16 +183,15 @@ def parse_glb(data):
|
||||
raise GlbSkinError(
|
||||
f"GLB chunk at {offset} declares {chunk_length} bytes, past the end of the file"
|
||||
)
|
||||
payload = raw[start:end]
|
||||
if chunk_index == 0:
|
||||
if chunk_type != CHUNK_TYPE_JSON:
|
||||
raise GlbSkinError(
|
||||
f"first GLB chunk must be JSON (0x{CHUNK_TYPE_JSON:08X}), "
|
||||
f"got 0x{chunk_type:08X}"
|
||||
)
|
||||
json_chunk = payload
|
||||
json_chunk = raw[start:end]
|
||||
elif chunk_index == 1 and chunk_type == CHUNK_TYPE_BIN:
|
||||
bin_chunk = payload
|
||||
bin_chunk = memoryview(raw)[start:end]
|
||||
else:
|
||||
if chunk_type == CHUNK_TYPE_JSON:
|
||||
raise GlbSkinError("repeated JSON chunk in GLB")
|
||||
@ -371,12 +375,12 @@ class _Reader:
|
||||
|
||||
if self.buffer_limit == 0:
|
||||
raise GlbSkinError(f"accessors[{index}] needs buffer 0, but the GLB has no embedded buffer")
|
||||
data = self.bin_chunk[: self.buffer_limit]
|
||||
# The view and accessor bounds above already exclude any BIN alignment padding.
|
||||
unpacker = struct.Struct("<" + fmt_char * components)
|
||||
base = view_offset + accessor_offset
|
||||
values = []
|
||||
for i in range(count):
|
||||
values.append(unpacker.unpack_from(data, base + i * stride))
|
||||
values.append(unpacker.unpack_from(self.bin_chunk, base + i * stride))
|
||||
meta["buffer_view"] = view_index
|
||||
return values, meta
|
||||
|
||||
@ -936,7 +940,7 @@ def _validate_primitive(reader, primitive, mesh_index, mesh_name, primitive_inde
|
||||
|
||||
def analyze_glb(data):
|
||||
"""Validate a GLB and return a JSON-serializable report."""
|
||||
gltf, bin_chunk, container = parse_glb(data)
|
||||
gltf, bin_chunk, container = _parse_chunks(data)
|
||||
reader = _Reader(gltf, bin_chunk)
|
||||
warnings = []
|
||||
errors = []
|
||||
|
||||
Reference in New Issue
Block a user