Files
DroidDeck/tools/vkbridge/shaders/gen_bc_tables.py
T
The412Banner ee0fb08268 Mali bridge (#517, by ASEbrahim) on the non-Adreno test branch
Squash of Droid-Deck/DroidDeck#517 (ASEbrahim/DroidDeck mali-bridge @ 6b823341): the Vulkan
bridge (tools/vkbridge) that runs the guest's Vulkan on the phone's own driver, with BC1-BC7,
clip/cull distance, vertex-divisor and depth-clip emulation and dma-heap memory sharing for Mali.
All of the bridge is ASEbrahim's work; squashed only to carry it on this test branch.

Combined with #570 on this branch, a device draws through exactly one path:
- Adreno: Turnip, unchanged (checked first; the bridge setting is ignored there).
- PowerVR / Xclipse with the system HAL and a render node: the system-Vulkan path (#570).
- Anything else with the Setup toggle on (Mali): the bridge.
2026-10-12 18:34:49 -04:00

92 lines
4.1 KiB
Python

#!/usr/bin/env python3
# SPDX-License-Identifier: GPL-3.0-or-later
"""
Generates bc_tables.glsl from bcdec.h (third_party/bcdec, MIT/Unlicense): the BC6H per-mode bit
layouts (as a table instead of 14 hand-written cases) and the BC6H/BC7 partition and anchor tables.
BC6H layout entry (uint): target (r0..r3 = 0..3, g0..g3 = 4..7, b0..b3 = 8..11, partition = 12)
| nbits << 4 | shift << 9 | reversed << 14.
"""
import os
import re
here = os.path.dirname(os.path.abspath(__file__))
src = open(os.path.join(here, '..', 'third_party', 'bcdec', 'bcdec.h')).read()
# ---- BC6H modes
bc6 = src[src.rindex('BCDECDEF void bcdec_bc6h_half('):src.rindex('BCDECDEF void bcdec_bc6h_float(')]
cases = re.findall(r'case (0b[01]+): \{(.*?)\} break;', bc6, re.S)
comp = {'r': 0, 'g': 4, 'b': 8}
modes = {}
for code, body in cases:
entries = []
mode_idx = None
body = re.sub(r'/\*.*?\*/', '', body, flags=re.S)
for line in body.split('\n'):
line = line.split('/*')[0].strip()
if not line:
continue
m = re.match(r'mode = (\d+);', line)
if m:
mode_idx = int(m.group(1))
continue
m = re.match(r'partition = bcdec__bitstream_read_bits\(&bstream, (\d+)\);', line)
if m:
entries.append((12, int(m.group(1)), 0, 0))
continue
m = re.match(r'([rgb])\[(\d)\] \|= bcdec__bitstream_read_bit(s_r|s)?\(&bstream(?:, (\d+))?\)(?: << (\d+))?;', line)
if not m:
raise SystemExit(f'unparsed BC6H line: {line}')
c, i, kind, n, sh = m.groups()
nbits = int(n) if n else 1
entries.append((comp[c] + int(i), nbits, int(sh) if sh else 0, 1 if kind == 's_r' else 0))
modes[int(code, 2)] = (mode_idx, entries)
out = []
out.append('/* Generated by gen_bc_tables.py from bcdec.h (MIT/Unlicense, Sergii Kudlai) - do not edit. */')
code_to_mode = [-1] * 32
offsets, flat = [0] * 14, []
for code in sorted(modes, key=lambda c: modes[c][0]):
idx, entries = modes[code]
code_to_mode[code] = idx
offsets[idx] = len(flat)
for t, n, sh, rv in entries:
flat.append(t | (n << 4) | (sh << 9) | (rv << 14))
offsets.append(len(flat))
out.append(f'const int BC6_MODE_OF_CODE[32] = int[32]({", ".join(map(str, code_to_mode))});')
out.append(f'const uint BC6_LAYOUT_START[15] = uint[15]({", ".join(str(o) + "u" for o in offsets)});')
out.append(f'const uint BC6_LAYOUT[{len(flat)}] = uint[{len(flat)}]({", ".join(str(v) + "u" for v in flat)});')
def actual_bits(fn_src, rows):
m = re.search(r'static char actual_bits_count\[\d\]\[\d+\] = \{(.*?)\};', fn_src, re.S)
return [list(map(int, re.findall(r'-?\d+', r.split('/*')[0]))) for r in re.findall(r'\{([^{}]*)\}', m.group(1))][:rows]
ab6 = actual_bits(bc6, 4)
for k, name in enumerate(['W', 'DR', 'DG', 'DB']):
out.append(f'const int BC6_BITS_{name}[14] = int[14]({", ".join(map(str, ab6[k]))});')
# ---- partition tables (BC7's 2- and 3-subset; BC6H uses the first 32 of the 2-subset table)
bc7 = src[src.rindex('BCDECDEF void bcdec_bc7('):]
table = bc7[bc7.index('partition_sets[2][64][4][4] = {'):bc7.index('static int aWeight2')]
rows = re.findall(r'\{ \{([^}]*)\}, \{([^}]*)\}, \{([^}]*)\}, \{([^}]*)\} \}', table)
assert len(rows) == 128, len(rows)
for name, part in (('BC7_PART2', rows[:64]), ('BC7_PART3', rows[64:])):
packed, anchors = [], []
for r in part:
vals = [int(v) for row in r for v in row.split(',')]
word, anc = 0, [0, 0, 0]
for t, v in enumerate(vals):
word |= (v & 3) << (2 * t)
if v & 0x80:
anc[v & 3] = t
packed.append(word)
anchors.append(anc[1] | (anc[2] << 4))
out.append(f'const uint {name}[64] = uint[64]({", ".join(str(p) + "u" for p in packed)});')
out.append(f'const uint {name}_ANCHOR[64] = uint[64]({", ".join(str(a) + "u" for a in anchors)});')
ab7 = actual_bits(bc7, 2)
out.append(f'const int BC7_BITS_RGB[8] = int[8]({", ".join(map(str, ab7[0]))});')
out.append(f'const int BC7_BITS_A[8] = int[8]({", ".join(map(str, ab7[1]))});')
open(os.path.join(here, 'bc_tables.glsl'), 'w').write('\n'.join(out) + '\n')
print(f'bc_tables.glsl: {len(flat)} BC6H layout entries, {len(modes)} modes')