upleb.uk

Public git repos — served from a NIP-34 GRASP relay at git.upleb.uk

summaryrefslogtreecommitdiff
path: root/tools/tex_headers.py
diff options
context:
space:
mode:
authorVonsek <45567075+Vonsek@users.noreply.github.com>2026-11-22 18:22:49 +0100
committerVonsek <45567075+Vonsek@users.noreply.github.com>2026-11-22 18:22:49 +0100
commitd041ba4664a3ef4c8cbaa1428dcee9db3085e173 (patch)
tree1c957fc461feff0a71334ec700613f8431363d13 /tools/tex_headers.py
parent49475796c226c8511b9470bf97a4daf57b8af323 (diff)
fix(tools): encode DDS pitch and top mip size correctly
The DDS header describes a single uncompressed row or the top compressed level. Recording the complete payload misstates those layouts when lower mips or multiple slices follow.
Diffstat (limited to 'tools/tex_headers.py')
-rw-r--r--tools/tex_headers.py23
1 files changed, 17 insertions, 6 deletions
diff --git a/tools/tex_headers.py b/tools/tex_headers.py
index 389b83cf2..994220f4f 100644
--- a/tools/tex_headers.py
+++ b/tools/tex_headers.py
@@ -554,7 +554,11 @@ D3D10_RESOURCE_MISC_TEXTURECUBE = 0x4
554 554
555 555
556def dds_header(w, h, depth=1, mips=1, fmt=28, is_cube=False, linear_size=0, array_size=1): 556def dds_header(w, h, depth=1, mips=1, fmt=28, is_cube=False, linear_size=0, array_size=1):
557 """Build a DX10 DDS header; array_size counts cubes, not faces, for cubemaps.""" 557 """Build a DX10 DDS header; array_size counts cubes for cubemaps.
558
559 For known uncompressed formats, derive the row pitch. A nonzero linear_size
560 overrides the derived top mip size for compressed formats.
561 """
558 hdr = bytearray(128) 562 hdr = bytearray(128)
559 hdr[0:4] = b"DDS " 563 hdr[0:4] = b"DDS "
560 flags = DDSD_CAPS | DDSD_HEIGHT | DDSD_WIDTH | DDSD_PIXELFORMAT 564 flags = DDSD_CAPS | DDSD_HEIGHT | DDSD_WIDTH | DDSD_PIXELFORMAT
@@ -562,10 +566,15 @@ def dds_header(w, h, depth=1, mips=1, fmt=28, is_cube=False, linear_size=0, arra
562 flags |= DDSD_MIPMAPCOUNT 566 flags |= DDSD_MIPMAPCOUNT
563 if depth > 1: 567 if depth > 1:
564 flags |= DDSD_DEPTH 568 flags |= DDSD_DEPTH
565 if linear_size > 0: 569 layout = FMT_INFO.get(fmt)
566 flags |= DDSD_LINEARSIZE 570 if layout is not None and layout[1] == 1:
571 flags |= DDSD_PITCH
572 pitch_or_size = calc_mip_level_bytes(w, 1, fmt=fmt)
567 else: 573 else:
568 flags |= 0x80000 574 flags |= DDSD_LINEARSIZE
575 pitch_or_size = linear_size
576 if layout is not None and linear_size == 0:
577 pitch_or_size = calc_mip_level_bytes(w, h, depth=depth, fmt=fmt)
569 578
570 caps = DDSCAPS_TEXTURE 579 caps = DDSCAPS_TEXTURE
571 if mips > 1: 580 if mips > 1:
@@ -579,7 +588,8 @@ def dds_header(w, h, depth=1, mips=1, fmt=28, is_cube=False, linear_size=0, arra
579 if is_cube: 588 if is_cube:
580 caps2 |= DDSCAPS2_CUBEMAP_ALLFACES 589 caps2 |= DDSCAPS2_CUBEMAP_ALLFACES
581 590
582 struct.pack_into("<IIIIIII", hdr, 4, 124, flags, h, w, linear_size, depth if depth > 1 else 0, mips if mips > 1 else 1) 591 struct.pack_into("<IIIIIII", hdr, 4, 124, flags, h, w, pitch_or_size,
592 depth if depth > 1 else 0, mips if mips > 1 else 1)
583 struct.pack_into("<II4s", hdr, 76, 32, 4, b"DX10") 593 struct.pack_into("<II4s", hdr, 76, 32, 4, b"DX10")
584 struct.pack_into("<II", hdr, 108, caps, caps2) 594 struct.pack_into("<II", hdr, 108, caps, caps2)
585 595
@@ -590,8 +600,9 @@ def dds_header(w, h, depth=1, mips=1, fmt=28, is_cube=False, linear_size=0, arra
590 600
591 601
592def dds_bytes(w, h, fmt, payload=b"", mips=1, depth=1, is_cube=False, array_size=1): 602def dds_bytes(w, h, fmt, payload=b"", mips=1, depth=1, is_cube=False, array_size=1):
603 linear_size = 0 if fmt in FMT_INFO else len(payload)
593 return dds_header(w, h, depth=depth, mips=mips, fmt=fmt, is_cube=is_cube, 604 return dds_header(w, h, depth=depth, mips=mips, fmt=fmt, is_cube=is_cube,
594 linear_size=len(payload), array_size=array_size) + payload 605 linear_size=linear_size, array_size=array_size) + payload
595 606
596 607
597def parse_dds_header(data): 608def parse_dds_header(data):