Commit 5cda39bf
5cda39bf7ddcMerged
Yukihiro "Matz" Matsumoto <[email protected]> · 8 months ago · 3 files · +1 added · ~2 modified
Description
Add class/method codegen for bm_ao_render.rb (work in progress)
Major codegen rewrite:
- Class definitions → C structs (value types for small classes like Vec)
- Method definitions → direct C functions (no dynamic dispatch)
- Getters/setters inlined to struct field access
- Module support (Rand module with static state)
- Integer#times with block → C for loop
- printf/puts/print handled as C stdio calls
- Math.sqrt/cos/sin → C math functions
and/or→ C &&/||returnstatement support- String interpolation now uses printf (no mruby dependency)
Mandelbrot now compiles to pure C (no mruby needed at all). AO render generates struct-based C code but has ~68 remaining unresolved call patterns (array indexing, method chains on objects, module variable access in method bodies).
Co-Authored-By: Claude Opus 4.6 (1M context) [email protected]
Details
- Commit
- 5cda39bf7ddc
- Parent
- 4edb291e3c51
bm_ao_render.rbaddedView file
1+
# AO render benchmark2+
# Original program (C) Syoyo Fujita in JavaScript (and other languages)3+
# https://code.google.com/p/aobench/4+
# Ruby(yarv2llvm) version by Hideki Miura5+
# mruby version by Hideki Miura6+
#7+
8+
IMAGE_WIDTH = Integer(ARGV[0] || 64)9+
IMAGE_HEIGHT = IMAGE_WIDTH10+
NSUBSAMPLES = 211+
NAO_SAMPLES = 812+
13+
module Rand14+
# Use xorshift15+
@x = 12345678916+
@y = 36243606917+
@z = 52128862918+
@w = 8867512319+
BNUM = 1 << 2920+
BNUMF = BNUM.to_f21+
def self.rand22+
x = @x23+
t = x ^ ((x & 0xfffff) << 11)24+
w = @w25+
@x, @y, @z = @y, @z, w26+
w = @w = (w ^ (w >> 19) ^ (t ^ (t >> 8)))27+
(w % BNUM) / BNUMF28+
end29+
end30+
31+
class Vec32+
def initialize(x, y, z)33+
@x = x34+
@y = y35+
@z = z36+
end37+
38+
def x=(v); @x = v; end39+
def y=(v); @y = v; end40+
def z=(v); @z = v; end41+
def x; @x; end42+
def y; @y; end43+
def z; @z; end44+
45+
def vadd(b)46+
Vec.new(@x + b.x, @y + b.y, @z + b.z)47+
end48+
49+
def vsub(b)50+
Vec.new(@x - b.x, @y - b.y, @z - b.z)51+
end52+
53+
def vcross(b)54+
Vec.new(@y * b.z - @z * b.y,55+
@z * b.x - @x * b.z,56+
@x * b.y - @y * b.x)57+
end58+
59+
def vdot(b)60+
r = @x * b.x + @y * b.y + @z * b.z61+
r62+
end63+
64+
def vlength65+
Math.sqrt(@x * @x + @y * @y + @z * @z)66+
end67+
68+
def vnormalize69+
len = vlength70+
v = Vec.new(@x, @y, @z)71+
if len > 1.0e-1772+
v.x = v.x / len73+
v.y = v.y / len74+
v.z = v.z / len75+
end76+
v77+
end78+
end79+
80+
81+
class Sphere82+
def initialize(center, radius)83+
@center = center84+
@radius = radius85+
end86+
87+
def center; @center; end88+
def radius; @radius; end89+
90+
def intersect(ray, isect)91+
rs = ray.org.vsub(@center)92+
b = rs.vdot(ray.dir)93+
c = rs.vdot(rs) - (@radius * @radius)94+
d = b * b - c95+
if d > 0.096+
t = - b - Math.sqrt(d)97+
98+
if t > 0.0 and t < isect.t99+
isect.t = t100+
isect.hit = true101+
isect.pl = Vec.new(ray.org.x + ray.dir.x * t,102+
ray.org.y + ray.dir.y * t,103+
ray.org.z + ray.dir.z * t)104+
n = isect.pl.vsub(@center)105+
isect.n = n.vnormalize106+
end107+
end108+
end109+
end110+
111+
class Plane112+
def initialize(p, n)113+
@p = p114+
@n = n115+
end116+
117+
def intersect(ray, isect)118+
d = [email protected](@n)119+
v = ray.dir.vdot(@n)120+
v0 = v121+
if v < 0.0122+
v0 = -v123+
end124+
if v0 < 1.0e-17125+
return126+
end127+
128+
t = -(ray.org.vdot(@n) + d) / v129+
130+
if t > 0.0 and t < isect.t131+
isect.hit = true132+
isect.t = t133+
isect.n = @n134+
isect.pl = Vec.new(ray.org.x + t * ray.dir.x,135+
ray.org.y + t * ray.dir.y,136+
ray.org.z + t * ray.dir.z)137+
end138+
end139+
end140+
141+
class Ray142+
def initialize(org, dir)143+
@org = org144+
@dir = dir145+
end146+
147+
def org; @org; end148+
def org=(v); @org = v; end149+
def dir; @dir; end150+
def dir=(v); @dir = v; end151+
end152+
153+
class Isect154+
def initialize155+
@t = 10000000.0156+
@hit = false157+
@pl = Vec.new(0.0, 0.0, 0.0)158+
@n = Vec.new(0.0, 0.0, 0.0)159+
end160+
161+
def t; @t; end162+
def t=(v); @t = v; end163+
def hit; @hit; end164+
def hit=(v); @hit = v; end165+
def pl; @pl; end166+
def pl=(v); @pl = v; end167+
def n; @n; end168+
def n=(v); @n = v; end169+
end170+
171+
def clamp(f)172+
i = f * 255.5173+
if i > 255.0174+
i = 255.0175+
end176+
if i < 0.0177+
i = 0.0178+
end179+
i.to_i180+
end181+
182+
def orthoBasis(basis, n)183+
basis[2] = Vec.new(n.x, n.y, n.z)184+
basis[1] = Vec.new(0.0, 0.0, 0.0)185+
186+
if n.x < 0.6 and n.x > -0.6187+
basis[1].x = 1.0188+
elsif n.y < 0.6 and n.y > -0.6189+
basis[1].y = 1.0190+
elsif n.z < 0.6 and n.z > -0.6191+
basis[1].z = 1.0192+
else193+
basis[1].x = 1.0194+
end195+
196+
basis[0] = basis[1].vcross(basis[2])197+
basis[0] = basis[0].vnormalize198+
199+
basis[1] = basis[2].vcross(basis[0])200+
basis[1] = basis[1].vnormalize201+
end202+
203+
class Scene204+
def initialize205+
@spheres = Array.new206+
@spheres[0] = Sphere.new(Vec.new(-2.0, 0.0, -3.5), 0.5)207+
@spheres[1] = Sphere.new(Vec.new(-0.5, 0.0, -3.0), 0.5)208+
@spheres[2] = Sphere.new(Vec.new(1.0, 0.0, -2.2), 0.5)209+
@plane = Plane.new(Vec.new(0.0, -0.5, 0.0), Vec.new(0.0, 1.0, 0.0))210+
end211+
212+
def ambient_occlusion(isect)213+
basis = Array.new(3)214+
orthoBasis(basis, isect.n)215+
216+
ntheta = NAO_SAMPLES217+
nphi = NAO_SAMPLES218+
eps = 0.0001219+
occlusion = 0.0220+
221+
p0 = Vec.new(isect.pl.x + eps * isect.n.x,222+
isect.pl.y + eps * isect.n.y,223+
isect.pl.z + eps * isect.n.z)224+
nphi.times do225+
ntheta.times do226+
r = Rand::rand227+
phi = 2.0 * 3.14159265 * Rand::rand228+
x = Math.cos(phi) * Math.sqrt(1.0 - r)229+
y = Math.sin(phi) * Math.sqrt(1.0 - r)230+
z = Math.sqrt(r)231+
232+
rx = x * basis[0].x + y * basis[1].x + z * basis[2].x233+
ry = x * basis[0].y + y * basis[1].y + z * basis[2].y234+
rz = x * basis[0].z + y * basis[1].z + z * basis[2].z235+
236+
raydir = Vec.new(rx, ry, rz)237+
ray = Ray.new(p0, raydir)238+
239+
occisect = Isect.new240+
@spheres[0].intersect(ray, occisect)241+
@spheres[1].intersect(ray, occisect)242+
@spheres[2].intersect(ray, occisect)243+
@plane.intersect(ray, occisect)244+
if occisect.hit245+
occlusion = occlusion + 1.0246+
end247+
end248+
end249+
250+
occlusion = (ntheta.to_f * nphi.to_f - occlusion) / (ntheta.to_f * nphi.to_f)251+
Vec.new(occlusion, occlusion, occlusion)252+
end253+
254+
def render(w, h, nsubsamples)255+
nsf = nsubsamples.to_f256+
nsfs = nsf * nsf257+
h.times do |y|258+
w.times do |x|259+
rad = Vec.new(0.0, 0.0, 0.0)260+
261+
# Subsampling262+
nsubsamples.times do |v|263+
nsubsamples.times do |u|264+
wf = w.to_f265+
hf = h.to_f266+
xf = x.to_f267+
yf = y.to_f268+
uf = u.to_f269+
vf = v.to_f270+
271+
px = (xf + (uf / nsf) - (wf / 2.0)) / (wf / 2.0)272+
py = -(yf + (vf / nsf) - (hf / 2.0)) / (hf / 2.0)273+
274+
eye = Vec.new(px, py, -1.0).vnormalize275+
276+
ray = Ray.new(Vec.new(0.0, 0.0, 0.0), eye)277+
278+
isect = Isect.new279+
@spheres[0].intersect(ray, isect)280+
@spheres[1].intersect(ray, isect)281+
@spheres[2].intersect(ray, isect)282+
@plane.intersect(ray, isect)283+
if isect.hit284+
col = ambient_occlusion(isect)285+
rad.x = rad.x + col.x286+
rad.y = rad.y + col.y287+
rad.z = rad.z + col.z288+
end289+
end290+
end291+
292+
r = rad.x / nsfs293+
g = rad.y / nsfs294+
b = rad.z / nsfs295+
printf("%c", clamp(r))296+
printf("%c", clamp(g))297+
printf("%c", clamp(b))298+
end299+
end300+
end301+
end302+
303+
# File.open("ao.ppm", "w") do |fp|304+
printf("P6\n")305+
printf("%d %d\n", IMAGE_WIDTH, IMAGE_HEIGHT)306+
printf("255\n", IMAGE_WIDTH, IMAGE_HEIGHT)307+
Scene.new.render(IMAGE_WIDTH, IMAGE_HEIGHT, NSUBSAMPLES)308+
# Scene.new.render(256, 256, 2)309+
# endsrc/codegen.cmodifiedView file
Loading diff…
src/codegen.hmodifiedView file
Loading diff…