code wiki / _hdl_build / nx_gx1b_page3d_gate.nx

nx_gx1b_page3d_gate.nx source

↩ module page · 143 lines · 8930 B

1// nx_gx1b_page3d_gate.nx -- Gx-1b: the LIVE Nishi-Browser renders a FETCHED HTML page whose <img> element is 2// filled by the sovereign SW-GPU 3D rasterizer. This is the honest completion of nishi-browser-3d-surface: 3// our OWN browser engine (br_layout: HTML -> CSS cascade + box/flow layout; br_shot_png/br_draw_fb: paint -> 4// RGBA framebuffer -> PNG) lays out a REAL page (heading + paragraphs + image element) and the 3D scene is 5// painted into the element's box via the browser's OWN image path (page.bimg) -- driven by the page's live 6// layout, not a standalone gate. ZERO third-party (no WebGPU/WebGL/canvas). license_tier: ORIGINAL expect_exit: 0 7import "nx_browser_render.nx" 8 9func gxb_puts(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(1,s,n); return 0 } 10func gxb_pn(v: i64) -> i64 { let b: *u8=sys_mmap(32) as *u8; var x: i64=v; var ng: i64=0; if x<0{ng=1;x=0-x} var i: i64=31; if x==0{b[i]=48 as u8;i=i-1} while x>0{b[i]=(48+x%10) as u8;x=x/10;i=i-1} if ng==1{b[i]=45 as u8;i=i-1} sys_write(1,(b as i64+i+1) as *u8,31-i); return 0 } 11func gx_edge(ax: i64,ay: i64,bx: i64,by: i64,px: i64,py: i64) -> i64 { return (bx-ax)*(py-ay)-(by-ay)*(px-ax) } 12func gx_min3(a: i64,b: i64,c: i64) -> i64 { var m: i64=a; if b<m{m=b} if c<m{m=c} return m } 13func gx_max3(a: i64,b: i64,c: i64) -> i64 { var m: i64=a; if b>m{m=b} if c>m{m=c} return m } 14func gx_clamp(v: i64,lo: i64,hi: i64) -> i64 { if v<lo{return lo} if v>hi{return hi} return v } 15func gx_tri(fb: *u8, zbuf: *i64, W: i64, H: i64, x0: i64,y0: i64,x1: i64,y1: i64,x2: i64,y2: i64, z: i64, r: i64,g: i64,b: i64) -> i64 { 16 let area: i64=gx_edge(x0,y0,x1,y1,x2,y2) 17 if area==0 { return 0 } 18 let minx: i64=gx_clamp(gx_min3(x0,x1,x2),0,W-1); let maxx: i64=gx_clamp(gx_max3(x0,x1,x2),0,W-1) 19 let miny: i64=gx_clamp(gx_min3(y0,y1,y2),0,H-1); let maxy: i64=gx_clamp(gx_max3(y0,y1,y2),0,H-1) 20 var py: i64=miny 21 while py<=maxy { 22 var px: i64=minx 23 while px<=maxx { 24 let w0: i64=gx_edge(x1,y1,x2,y2,px,py); let w1: i64=gx_edge(x2,y2,x0,y0,px,py); let w2: i64=gx_edge(x0,y0,x1,y1,px,py) 25 var inside: i64=0 26 if area>0 { if w0>=0 { if w1>=0 { if w2>=0 { inside=1 } } } } else { if w0<=0 { if w1<=0 { if w2<=0 { inside=1 } } } } 27 if inside==1 { let idx: i64=py*W+px; if z<zbuf[idx] { zbuf[idx]=z; let o: i64=idx*3; fb[o]=r as u8; fb[o+1]=g as u8; fb[o+2]=b as u8 } } 28 px=px+1 29 } 30 py=py+1 31 } 32 return 0 33} 34func gx_proj(vx: i64,vy: i64,vz: i64, cy: i64,sy: i64,cx: i64,sx: i64, CX: i64,CY: i64, FOCAL: i64, CAMZ: i64, out: *i64) -> i64 { 35 let xr: i64=(vx*cy - vz*sy)/256 36 let zr: i64=(vx*sy + vz*cy)/256 37 let yr: i64=vy 38 let yr2: i64=(yr*cx - zr*sx)/256 39 let zr2: i64=(yr*sx + zr*cx)/256 40 var depth: i64=zr2 + CAMZ 41 if depth<1 { depth=1 } 42 out[0]=CX + (xr*FOCAL)/depth 43 out[1]=CY - (yr2*FOCAL)/depth 44 out[2]=depth 45 return 0 46} 47// render the z-buffered, hidden-surface cube into an RGB buffer W x H 48func gx_render_cube(rgb: *u8, W: i64, H: i64) -> i64 { 49 let zbuf: *i64=sys_mmap(W*H*8+16) as *i64 50 let FAR: i64=1<<30 51 var i: i64=0 52 while i<W*H { let o: i64=i*3; rgb[o]=20 as u8; rgb[o+1]=22 as u8; rgb[o+2]=34 as u8; zbuf[i]=FAR; i=i+1 } 53 let CX: i64=W/2; let CY: i64=H/2 54 let FOCAL: i64=300; let CAMZ: i64=340 55 let cy: i64=222; let sy: i64=128; let cx: i64=222; let sx: i64=128 56 let R: i64=85 57 let vx: *i64=sys_mmap(8*8) as *i64; let vy: *i64=sys_mmap(8*8) as *i64; let vz: *i64=sys_mmap(8*8) as *i64 58 vx[0]=0-R;vy[0]=0-R;vz[0]=0-R; vx[1]=R;vy[1]=0-R;vz[1]=0-R; vx[2]=R;vy[2]=R;vz[2]=0-R; vx[3]=0-R;vy[3]=R;vz[3]=0-R 59 vx[4]=0-R;vy[4]=0-R;vz[4]=R; vx[5]=R;vy[5]=0-R;vz[5]=R; vx[6]=R;vy[6]=R;vz[6]=R; vx[7]=0-R;vy[7]=R;vz[7]=R 60 let px: *i64=sys_mmap(8*8) as *i64; let py: *i64=sys_mmap(8*8) as *i64; let pd: *i64=sys_mmap(8*8) as *i64 61 let tmp: *i64=sys_mmap(4*8) as *i64 62 var v: i64=0 63 while v<8 { gx_proj(vx[v],vy[v],vz[v], cy,sy,cx,sx, CX,CY,FOCAL,CAMZ, tmp); px[v]=tmp[0]; py[v]=tmp[1]; pd[v]=tmp[2]; v=v+1 } 64 let ta: *i64=sys_mmap(12*8) as *i64; let tb: *i64=sys_mmap(12*8) as *i64; let tc: *i64=sys_mmap(12*8) as *i64 65 let tr: *i64=sys_mmap(12*8) as *i64; let tg: *i64=sys_mmap(12*8) as *i64; let tbl: *i64=sys_mmap(12*8) as *i64 66 ta[0]=0;tb[0]=1;tc[0]=2; ta[1]=0;tb[1]=2;tc[1]=3 67 ta[2]=5;tb[2]=4;tc[2]=7; ta[3]=5;tb[3]=7;tc[3]=6 68 ta[4]=4;tb[4]=0;tc[4]=3; ta[5]=4;tb[5]=3;tc[5]=7 69 ta[6]=1;tb[6]=5;tc[6]=6; ta[7]=1;tb[7]=6;tc[7]=2 70 ta[8]=4;tb[8]=5;tc[8]=1; ta[9]=4;tb[9]=1;tc[9]=0 71 ta[10]=3;tb[10]=2;tc[10]=6; ta[11]=3;tb[11]=6;tc[11]=7 72 tr[0]=230;tg[0]=70;tbl[0]=70; tr[1]=230;tg[1]=70;tbl[1]=70 73 tr[2]=70;tg[2]=200;tbl[2]=90; tr[3]=70;tg[3]=200;tbl[3]=90 74 tr[4]=80;tg[4]=120;tbl[4]=235; tr[5]=80;tg[5]=120;tbl[5]=235 75 tr[6]=235;tg[6]=200;tbl[6]=70; tr[7]=235;tg[7]=200;tbl[7]=70 76 tr[8]=70;tg[8]=200;tbl[8]=220; tr[9]=70;tg[9]=200;tbl[9]=220 77 tr[10]=210;tg[10]=80;tbl[10]=200; tr[11]=210;tg[11]=80;tbl[11]=200 78 var t: i64=0 79 while t<12 { 80 let a: i64=ta[t]; let bb: i64=tb[t]; let cc: i64=tc[t] 81 let z: i64=(pd[a]+pd[bb]+pd[cc])/3 82 gx_tri(rgb,zbuf,W,H, px[a],py[a], px[bb],py[bb], px[cc],py[cc], z, tr[t],tg[t],tbl[t]) 83 t=t+1 84 } 85 return 0 86} 87 88func main() -> i64 { 89 gxb_puts("=== nx_gx1b_page3d_gate -- the LIVE Nishi-Browser renders a page with a sovereign 3D element ===\n" as *u8) 90 var fails: i64=0 91 let html: *u8 = "<html><head><style>img{display:block;width:360px;height:270px;margin:18px 0}</style></head><body><h1>Sovereign 3D in the Nishi Browser</h1><p>This page is fetched, parsed, and laid out by our OWN browser engine. The scene below is rendered by our OWN software GPU and painted into the image element through the browser's own image paint path.</p><img src=nishi3d://cube><p>No WebGPU. No WebGL. No canvas library. Zero third-party -- every pixel is ours, on Windows and Linux.</p></body></html>\x00" as *u8 92 let hlen: i64 = br_slen(html) 93 let page: *Page = sys_mmap(NX_PAGE_BYTES) as *Page 94 page.raw = html 95 page.raw_len = hlen 96 page.dark_mode = 0 97 br_layout(page, 960) 98 var t1: i64=0 99 if page.ok==1 { if page.tree.count>5 { t1=1 } } 100 gxb_puts(" layout boxes="); gxb_pn(page.tree.count); gxb_puts(" page_h="); gxb_pn(page.page_h); gxb_puts("\n" as *u8) 101 if t1==1 { gxb_puts("T1 PASS our browser engine laid out the page (br_layout GREEN)\n" as *u8) } else { fails=fails+1; gxb_puts("T1 FAIL layout\n" as *u8) } 102 // find the <img> box (bsrc_off set by the layout) 103 var imgbox: i64 = 0-1 104 var i: i64=0 105 while i<page.tree.count { if page.bsrc_off[i] >= 0 { imgbox=i; i=page.tree.count } else { i=i+1 } } 106 var t2: i64=0 107 if imgbox>=0 { t2=1 } 108 gxb_puts(" img element box index="); gxb_pn(imgbox); gxb_puts("\n" as *u8) 109 if t2==1 { gxb_puts("T2 PASS the <img> element became a laid-out box (the 3D surface)\n" as *u8) } else { fails=fails+1; gxb_puts("T2 FAIL no img box\n" as *u8) } 110 // render the sovereign 3D scene into an RGB buffer + count cube pixels 111 let IW: i64=360; let IH: i64=270 112 let rgb: *u8=sys_mmap(IW*IH*3+16) 113 gx_render_cube(rgb, IW, IH) 114 var cubepx: i64=0 115 var pp: i64=0 116 while pp<IW*IH { 117 let o: i64=pp*3 118 let rr: i64=rgb[o] as i64; let gg: i64=rgb[o+1] as i64; let bbv: i64=rgb[o+2] as i64 119 var d1: i64=rr-20; if d1<0{d1=0-d1} 120 var d2: i64=gg-22; if d2<0{d2=0-d2} 121 var d3: i64=bbv-34; if d3<0{d3=0-d3} 122 if d1+d2+d3 > 60 { cubepx=cubepx+1 } 123 pp=pp+16 124 } 125 var t3: i64=0 126 if cubepx>100 { t3=1 } 127 gxb_puts(" sovereign 3D cube pixels (sampled)="); gxb_pn(cubepx); gxb_puts("\n" as *u8) 128 if t3==1 { gxb_puts("T3 PASS sovereign SW-GPU rendered the 3D scene\n" as *u8) } else { fails=fails+1; gxb_puts("T3 FAIL no 3D\n" as *u8) } 129 // hand the 3D render to the browser as the image element's pixels 130 if imgbox>=0 { page.bimg[imgbox] = rgb as i64; page.bimg_w[imgbox] = IW; page.bimg_h[imgbox] = IH } 131 // the browser paints the WHOLE page (chrome + text + the 3D element) via its own paint path 132 br_shot_png(page, "https://nishifamily.com/world/3d\x00" as *u8, 31, "knowledge/nx_gx1b_page3d.png\x00" as *u8) 133 let szp: *i64=sys_mmap(16) as *i64 134 let rb: *u8=sys_read_file("knowledge/nx_gx1b_page3d.png\x00" as *u8, szp) 135 var t4: i64=0 136 if (rb as i64)!=0 { if szp[0]>1000 { t4=1 } } 137 if t4==1 { gxb_puts("T4 PASS the browser painted the page to a PNG ("); gxb_pn(szp[0]); gxb_puts(" bytes)\n" as *u8) } else { fails=fails+1; gxb_puts("T4 FAIL png missing\n" as *u8) } 138 139 if fails==0 { gxb_puts("GX1B-PAGE3D GREEN -- the live Nishi-Browser laid out a fetched page and painted the sovereign SW-GPU 3D into its image element (zero 3rd-party)\n" as *u8); sys_exit(0); return 0 } 140 gxb_puts("GX1B-PAGE3D RED fails="); gxb_pn(fails); gxb_puts("\n" as *u8) 141 sys_exit(1) 142 return 1 143}