nx_texr_descriptor_t279.nx source
↩ module page · 134 lines · 6853 B
1// nx_nxa_texbake_region.nx -- CLI for the PER-REGION skin atlas + periodic micro-relief tile (TEXR):
2// nx_nxa_texbake_region measure <in.nxa> [res] the honest per-tile ruler, bakes nothing
3// nx_nxa_texbake_region bake <in.nxa> <out.nxa> [res] [seed] [hue] TEXR written additively (TEXM untouched)
4// res 0 or absent = the resolution the asset's own TEXM was baked at. Core in nx_nxa_texbake_region_lib.nx
5// (the gate imports the same implementation). Fail-closed, named reasons. EXITS: 0 OK | 2 usage |
6// 3 bad container | 4 missing section | 5 mismatch | 6 ledger missing | 1 I/O
7// license_tier: ORIGINAL No hw writes (Rule 26).
8import "nx_syscalls.nx"
9import "nx_nxa_texbake_region_lib.nx"
10import "nx_sha256.nx"
11const NR_E_USAGE: i64 = 2
12const NR_DEFAULT_SEED: i64 = 1
13
14func nr_atoi(s: *u8) -> i64 {
15 var v: i64 = 0
16 var i: i64 = 0
17 var neg: i64 = 0
18 if s[0] == (45 as u8) { neg = 1; i = 1 }
19 while s[i] >= (48 as u8) {
20 if s[i] > (57 as u8) { return v }
21 v = v*10 + ((s[i] as i64) - 48)
22 i = i + 1
23 }
24 if neg == 1 { return 0 - v }
25 return v
26}
27func nr_is(s: *u8, t: *u8) -> i64 {
28 var i: i64 = 0
29 while t[i] != (0 as u8) { if s[i] != t[i] { return 0 } i = i + 1 }
30 if s[i] != (0 as u8) { return 0 }
31 return 1
32}
33func nr_inspect(inp:*u8)->i64{
34 let size:*i64=sys_mmap_try(__size_of(i64)) as *i64
35 if (size as i64)<=0{nt_outs("{\"schema\":\"nishi.texr.inspect.v1\",\"state\":\"unavailable\",\"reason\":\"allocation\"}\n");return NTR_E_IO}
36 let b:*u8=sys_map_file(inp,size)
37 if (b as i64)<=0{sys_munmap_direct(size as *u8,__size_of(i64));nt_outs("{\"schema\":\"nishi.texr.inspect.v1\",\"state\":\"unavailable\",\"reason\":\"input-map\"}\n");return NTR_E_IO}
38 let n:i64=size[0]
39 var o:i64=0-3
40 if n>=32{let header:*i64=b as *i64;if header[0]==nxa_magic(){o=ntr_texr_find(b,n)}}
41 var rc:i64=NTR_OK
42 if o<0{
43 if o==(0-1){nt_outs("{\"schema\":\"nishi.texr.inspect.v1\",\"state\":\"absent\",\"section\":\"TEXR\"}\n");rc=NTR_E_NOSEC}
44 else{nt_outs("{\"schema\":\"nishi.texr.inspect.v1\",\"state\":\"refused\",\"validationCode\":");nt_outn(o);nt_outs("}\n");rc=NTR_E_BAD}
45 }else{
46 let digestBytes:i64=SHA256_DIGEST_BYTES
47 let identityBytes:i64=digestBytes*3+1
48 let identity:*u8=sys_mmap_try(identityBytes)
49 if (identity as i64)<=0{sys_munmap_direct(b,n);sys_munmap_direct(size as *u8,__size_of(i64));return NTR_E_IO}
50 let digest:*u8=((identity as i64)+digestBytes*2+1) as *u8
51 if sha256_digest_checked_native(b,n,digest)!=0{sys_munmap_direct(identity,identityBytes);sys_munmap_direct(b,n);sys_munmap_direct(size as *u8,__size_of(i64));return NTR_E_IO}
52 let alphabet:*u8="0123456789abcdef"
53 var hi:i64=0
54 while hi<digestBytes{identity[hi*2]=alphabet[(digest[hi] as i64)/16];identity[hi*2+1]=alphabet[(digest[hi] as i64)%16];hi=hi+1}
55 identity[digestBytes*2]=0 as u8
56 let w:*i64=b as *i64
57 let ns:i64=w[o+NTR_W_NSETS];let nj:i64=w[o+NTR_W_NJ];let stride:i64=w[o+NTR_W_SETW]
58 let blob:i64=o+NTR_HDR_WORDS+nj*NTR_JOINT_WORDS+ns*stride
59 nt_outs("{\"schema\":\"nishi.texr.inspect.v1\",\"state\":\"structure-validated\",\"encodedImagesValidated\":false,\"physicalAccuracyValidated\":false,\"resolution\":")
60 nt_outn(w[o+NTR_W_RES]);nt_outs(",\"artifactSha256\":\"");nt_outs(identity);nt_outs("\",\"artifactBytes\":");nt_outn(n)
61 nt_outs(",\"encodingEvidence\":{\"basis\":\"validated-TEXR-version-contract\",\"version\":");nt_outn(w[o+NTR_W_VER]);nt_outs(",\"producerVerified\":false,\"qualificationJoin\":\"artifactSha256\"}")
62 nt_outs(",\"microResolution\":");nt_outn(w[o+NTR_W_MPX])
63 nt_outs(",\"declaredMicroRepeatUm\":");nt_outn(w[o+NTR_W_MREP])
64 nt_outs(",\"joints\":");nt_outn(nj);nt_outs(",\"sets\":[")
65 var s:i64=0
66 while s<ns{
67 if s>0{nt_outs(",")}
68 let at:i64=o+NTR_HDR_WORDS+nj*NTR_JOINT_WORDS+s*stride
69 nt_outs("{\"region\":");nt_outn(w[at+NTR_S_REGION]);nt_outs(",\"maps\":[")
70 var m:i64=0
71 while m<NTR_NMAPS{
72 if m>0{nt_outs(",")}
73 let rec:i64=at+NTR_SET_META+m*NTR_MAPREC
74 nt_outs("{\"kind\":");nt_outn(w[rec]);nt_outs(",\"semantic\":\"")
75 if w[rec]==NTR_MAP_ALBEDO{nt_outs("albedo")}
76 if w[rec]==NTR_MAP_DISP{nt_outs("signed-height")}
77 if w[rec]==NTR_MAP_NORMAL{nt_outs("normal")}
78 if w[rec]==NTR_MAP_TDISP{nt_outs("periodic-signed-height")}
79 if w[rec]==NTR_MAP_TNORMAL{nt_outs("periodic-normal")}
80 nt_outs("\",\"fileByteOffset\":");nt_outn(blob*NTR_WORD+w[rec+1])
81 nt_outs(",\"bytes\":");nt_outn(w[rec+2])
82 if w[rec]==NTR_MAP_NORMAL||w[rec]==NTR_MAP_TNORMAL{
83 nt_outs(",\"normalEncoding\":{\"id\":");nt_outn(NTB_NORMAL_ENCODING_NATIVE_MID_SCALE)
84 nt_outs(",\"name\":\"native-rgb8-mid128-scale127-v1\",\"linearTransfer\":true,\"normalizationRequired\":true,\"decoderOwner\":\"runtime/nx_nxa_texbake_lib.nx:ntb_normal_decode_rgb8\",\"byteMidpoint\":");nt_outn(NTB_N_MID);nt_outs(",\"componentScale\":");nt_outn(NTB_N_SCALE);nt_outs("}")
85 }
86 nt_outs("}");m=m+1
87 }
88 nt_outs("]}");s=s+1
89 }
90 nt_outs("]}\n")
91 if sys_munmap_direct(identity,identityBytes)!=0{rc=NTR_E_IO}
92 }
93 let unmapped:i64=sys_munmap_direct(b,n)
94 let released:i64=sys_munmap_direct(size as *u8,__size_of(i64))
95 if unmapped!=0||released!=0{return NTR_E_IO}
96 return rc
97}
98
99func main(argc: i64, argv: *i64) -> i64 {
100 if argc < 3 {
101 nt_err("usage: nx_nxa_texbake_region inspect <in.nxa> | measure <in.nxa> [res] | bake <in.nxa> <out.nxa> [res] [seed] [hue]\n" as *u8)
102 nt_err(" measure: per-region chart footprints (UV->3D Jacobian) and band adequacy, unique and micro; writes nothing\n" as *u8)
103 nt_err(" bake: per-region albedo + 16-bit height + derived normal + periodic micro tile, written as TEXR\n" as *u8)
104 sys_exit(NR_E_USAGE)
105 return NR_E_USAGE
106 }
107 let verb: *u8 = argv[1] as *u8
108 if nr_is(verb, "inspect" as *u8) == 1 {
109 if argc != 3 { return NR_E_USAGE }
110 return nr_inspect(argv[2] as *u8)
111 }
112 if nr_is(verb, "measure" as *u8) == 1 {
113 var res: i64 = 0
114 if argc >= 4 { res = nr_atoi(argv[3] as *u8) }
115 let rc: i64 = ntr_measure(argv[2] as *u8, res)
116 sys_exit(rc)
117 return rc
118 }
119 if nr_is(verb, "bake" as *u8) == 1 {
120 if argc < 4 { nt_err("usage: nx_nxa_texbake_region bake <in.nxa> <out.nxa> [res] [seed] [hue]\n" as *u8); sys_exit(NR_E_USAGE); return NR_E_USAGE }
121 var res: i64 = 0
122 if argc >= 5 { res = nr_atoi(argv[4] as *u8) }
123 var seed: i64 = NR_DEFAULT_SEED
124 if argc >= 6 { seed = nr_atoi(argv[5] as *u8) }
125 var hue: i64 = NTB_DEFAULT_HUE
126 if argc >= 7 { hue = nr_atoi(argv[6] as *u8) }
127 let rc: i64 = ntr_apply(argv[2] as *u8, argv[3] as *u8, res, seed, hue)
128 sys_exit(rc)
129 return rc
130 }
131 nt_err("nx_nxa_texbake_region: unknown verb (inspect | measure | bake)\n" as *u8)
132 sys_exit(NR_E_USAGE)
133 return NR_E_USAGE
134}