|
| 1 | +name: Compact PR shader headers |
| 2 | + |
| 3 | +on: |
| 4 | + push: |
| 5 | + branches: |
| 6 | + - auto-exposure-pr-source |
| 7 | + |
| 8 | +permissions: |
| 9 | + contents: write |
| 10 | + |
| 11 | +jobs: |
| 12 | + compact: |
| 13 | + if: github.actor != 'github-actions[bot]' |
| 14 | + runs-on: ubuntu-latest |
| 15 | + steps: |
| 16 | + - uses: actions/checkout@v6 |
| 17 | + with: |
| 18 | + fetch-depth: 0 |
| 19 | + ref: auto-exposure-pr-source |
| 20 | + |
| 21 | + - name: Compact generated shader headers without changing bytecode |
| 22 | + shell: bash |
| 23 | + run: | |
| 24 | + python3 - <<'PY' |
| 25 | + from pathlib import Path |
| 26 | + import re |
| 27 | +
|
| 28 | + root = Path('OptiScaler/shaders/dlssnr/precompile') |
| 29 | + d3d = root / 'DlssNr_Shader.h' |
| 30 | + cso = root / 'DlssNr_Shader.cso' |
| 31 | + vk = root / 'DlssNr_Shader_Vk.h' |
| 32 | +
|
| 33 | + def parse_array(text: str, symbol: str) -> bytes: |
| 34 | + m = re.search( |
| 35 | + rf'\b{re.escape(symbol)}\s*\[\]\s*=\s*\{{(?P<body>.*?)\}}\s*;', |
| 36 | + text, |
| 37 | + flags=re.S, |
| 38 | + ) |
| 39 | + if not m: |
| 40 | + raise SystemExit(f'Could not find byte array {symbol}') |
| 41 | + body = m.group('body') |
| 42 | + vals = [] |
| 43 | + for token in re.findall(r'0[xX][0-9a-fA-F]+|\b\d+\b', body): |
| 44 | + value = int(token, 0) |
| 45 | + if not 0 <= value <= 255: |
| 46 | + raise SystemExit(f'{symbol}: out-of-range byte {value}') |
| 47 | + vals.append(value) |
| 48 | + if not vals: |
| 49 | + raise SystemExit(f'{symbol}: no bytes found') |
| 50 | + return bytes(vals) |
| 51 | +
|
| 52 | + def emit(symbol: str, data: bytes, per_line: int = 32) -> str: |
| 53 | + lines = ['#pragma once', '', f'inline static const unsigned char {symbol}[] = {{'] |
| 54 | + for i in range(0, len(data), per_line): |
| 55 | + chunk = data[i:i + per_line] |
| 56 | + lines.append(' ' + ', '.join(f'0x{b:02x}' for b in chunk) + ',') |
| 57 | + lines.append('};') |
| 58 | + lines.append('') |
| 59 | + return '\n'.join(lines) |
| 60 | +
|
| 61 | + d3d_before = d3d.read_text(encoding='utf-8', errors='strict') |
| 62 | + d3d_bytes = parse_array(d3d_before, 'DlssNr_cso') |
| 63 | + cso_bytes = cso.read_bytes() |
| 64 | + if d3d_bytes != cso_bytes: |
| 65 | + raise SystemExit( |
| 66 | + f'D3D header bytecode does not match tracked CSO: header={len(d3d_bytes)}, cso={len(cso_bytes)}' |
| 67 | + ) |
| 68 | + d3d.write_text(emit('DlssNr_cso', d3d_bytes), encoding='utf-8', newline='\n') |
| 69 | +
|
| 70 | + vk_before = vk.read_text(encoding='utf-8', errors='strict') |
| 71 | + vk_bytes = parse_array(vk_before, 'dlssnr_spv') |
| 72 | + if vk_bytes[:4] != bytes((0x03, 0x02, 0x23, 0x07)): |
| 73 | + raise SystemExit('Vulkan header does not start with the SPIR-V magic number') |
| 74 | + if len(vk_bytes) % 4: |
| 75 | + raise SystemExit(f'Vulkan SPIR-V size is not word aligned: {len(vk_bytes)} bytes') |
| 76 | + vk.write_text(emit('dlssnr_spv', vk_bytes), encoding='utf-8', newline='\n') |
| 77 | +
|
| 78 | + # Verify the rewrite is byte-for-byte equivalent. |
| 79 | + if parse_array(d3d.read_text(encoding='utf-8'), 'DlssNr_cso') != d3d_bytes: |
| 80 | + raise SystemExit('D3D bytecode changed during compaction') |
| 81 | + if parse_array(vk.read_text(encoding='utf-8'), 'dlssnr_spv') != vk_bytes: |
| 82 | + raise SystemExit('Vulkan bytecode changed during compaction') |
| 83 | +
|
| 84 | + print(f'D3D: {len(d3d_bytes)} bytes, {sum(1 for _ in d3d.open(encoding="utf-8"))} lines') |
| 85 | + print(f'Vulkan: {len(vk_bytes)} bytes, {sum(1 for _ in vk.open(encoding="utf-8"))} lines') |
| 86 | + PY |
| 87 | +
|
| 88 | + if grep -q '^#if 0' OptiScaler/shaders/dlssnr/precompile/DlssNr_Shader.h; then |
| 89 | + echo 'FXC disassembly block is still present' >&2 |
| 90 | + exit 1 |
| 91 | + fi |
| 92 | +
|
| 93 | + git diff --check |
| 94 | +
|
| 95 | + - name: Fold cleanup into the existing PR commit |
| 96 | + shell: bash |
| 97 | + env: |
| 98 | + BASE_SHA: 7b7220bbb4994a9c8ae60cfc75a44cb67995efb8 |
| 99 | + run: | |
| 100 | + git config user.name "github-actions[bot]" |
| 101 | + git config user.email "41898282+github-actions[bot]@users.noreply.github.com" |
| 102 | +
|
| 103 | + test "$(git rev-parse HEAD~2)" = "$BASE_SHA" |
| 104 | +
|
| 105 | + git rm .github/workflows/compact_pr_shader_headers.yml |
| 106 | + git add OptiScaler/shaders/dlssnr/precompile/DlssNr_Shader.h |
| 107 | + git add OptiScaler/shaders/dlssnr/precompile/DlssNr_Shader_Vk.h |
| 108 | +
|
| 109 | + git reset --soft "$BASE_SHA" |
| 110 | + git add -A |
| 111 | + git commit -m "DLSSNR: add automatic exposure and trim anchors" |
| 112 | + git push --force-with-lease origin HEAD:auto-exposure-pr-source |
0 commit comments