nx_game_shader_backend_lib.nx source
↩ module page · 69 lines · 2659 B
1// One shader-capture owner for normal and explicitly selected staged builds.
2// Digest checks bracket each execution; they detect changed files, not hostile swap-and-restore races.
3import "nx_game_page_artifact_lib.nx"
4import "nx_tool_run.nx"
5const GSB_LIVE: *u8 = "./nx_wgsl.elf"
6const GSB_LIVE_ABS: *u8 = "/volume1/homes/elderwesto/nishihost/nx_wgsl.elf"
7const GSB_STAGED: *u8 = "/volume1/homes/elderwesto/nishihost/nx_wgsl.sov.elf.new"
8static gsb_expected: *u8
9static gsb_failed: i64
10static gsb_expected_bytes: i64
11
12func gsb_matches() -> i64 {
13 let fd: i64 = sys_openat_rd(GSB_STAGED)
14 if fd < 0 { return 0 }
15 let extent: i64 = sys_lseek(fd,0,2)
16 sys_close(fd)
17 if extent != gsb_expected_bytes { return 0 }
18 var size: i64 = 0
19 let bytes: *u8 = sys_read_file(GSB_STAGED, &size)
20 if (bytes as i64) == 0 { return 0 }
21 var ok: i64 = 0
22 if size == gsb_expected_bytes {
23 let digest: *u8 = sys_mmap(GPA_SHA_HEX + 1)
24 gpa_sha(bytes,size,digest)
25 ok = fsx_seq(digest,gsb_expected)
26 sys_munmap(digest,GPA_SHA_HEX + 1)
27 }
28 sys_free_file(bytes,size)
29 return ok
30}
31func gsb_select(expected: *u8,expected_bytes: i64) -> i64 {
32 // A rejected explicit selection must never recover through the live fallback.
33 gsb_failed = 1
34 gsb_expected = 0 as *u8
35 if expected_bytes <= 0 { return 0 }
36 gsb_expected_bytes = expected_bytes
37 if vw_slen(expected) != GPA_SHA_HEX { return 0 }
38 var i: i64 = 0
39 while i < GPA_SHA_HEX {
40 if gpa_hex_digit(expected[i] as i64) < 0 { return 0 }
41 i = i + 1
42 }
43 gsb_expected = expected
44 if gsb_matches() != 1 { return 0 }
45 gsb_failed = 0
46 return 1
47}
48func gsb_once(path: *u8,command: *u8,buf: *u8,cap: i64,ol: *i64,timeout_ms: i64) -> i64 {
49 let argv: *i64 = sys_mmap(24) as *i64
50 argv[0]=path as i64;argv[1]=command as i64;argv[2]=0
51 var truncated: i64 = TR_FIT
52 let rc: i64 = tr_run_capture_tr(path,argv,buf,cap,ol,timeout_ms,&truncated)
53 sys_munmap(argv as *u8,24)
54 if rc != 0 || truncated != TR_FIT { ol[0]=0;return -1 }
55 return 0
56}
57func gsb_capture(command: *u8,buf: *u8,cap: i64,ol: *i64,timeout_ms: i64) -> i64 {
58 ol[0]=0
59 if gsb_failed != 0 { return -1 }
60 if (gsb_expected as i64) != 0 {
61 if gsb_failed != 0 || gsb_matches() != 1 { gsb_failed=1;return -1 }
62 let rc: i64=gsb_once(GSB_STAGED,command,buf,cap,ol,timeout_ms)
63 if rc != 0 || gsb_matches() != 1 { ol[0]=0;gsb_failed=1;return -1 }
64 return 0
65 }
66 var rc: i64=gsb_once(GSB_LIVE,command,buf,cap,ol,timeout_ms)
67 if ol[0] <= 0 { rc=gsb_once(GSB_LIVE_ABS,command,buf,cap,ol,timeout_ms) }
68 return rc
69}