ludic/packages/ludic.render3d/sky.ludic
Orkuncakilkaya 00df8839e6 perf(render3d): the sky light baked once at start, the terrain load in four steps, actor cull bounds by height
- sky_start_yaw: a game that turns the sky at boot sets it before r3d_init and the image-based light
  is baked once at that yaw; sky_set_yaw to a yaw already baked bakes nothing.
- terrain_init_step: the height field and normals, the sun shadow, the materials, the patches and
  programs as separate steps; r3d_load_count is 8, so a loading bar moves through the terrain (half
  the start-up) instead of jumping over it. terrain_init runs the four in a row as before.
- The per-cascade actor cull centres its sphere at half the model's height and sizes it by the
  larger of height and radius: centred at the radius, it dropped a head at a cascade's edge (11 px
  in town, found by the drawstats comparison). Lake was byte-identical before and after.

OpenGL frames at the five viewpoints unchanged; the game's 59 self-tests pass.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-09-15 15:03:06 +03:00

150 lines
6.7 KiB
Text

# ============================================================================
# sky.ludic — the HDRI sky and its image-based lighting: the equirect radiance
# map (RGB16F, mipped), the sun found in it, a diffuse-convolved irradiance map,
# a GGX-prefiltered map per roughness level (a 2D array), and the split-sum
# BRDF lookup. All convolved on the GPU at load.
# ============================================================================
const SKY_PREFILTER_LEVELS: int = 6
var sky_tex: int = 0
var sky_w: int = 0
var sky_h: int = 0
var sky_irradiance: int = 0
var sky_prefilter: int = 0 # GL_TEXTURE_2D_ARRAY
var sky_brdf: int = 0
var sun_dir: words = null # toward the sun (float bits)
var sun_color: words = null # radiance (float bits)
var sky_fullscreen: Mesh = null
var sky_yaw: int = 0 # radians: the HDRI is turned by this about y
var sky_sun_boost: int = 0x40133333 # 2.3: the photograph's thin cloud dims its sun; a crisper day wants more
var sky_rot_s: int = 0
var sky_rot_c: int = 0
var sun_hdri: words = null # the sun direction as found in the file
# The yaw to bake the light at the first time (radians, float bits). A game that turns the sky at
# start sets it before r3d_init, so the image-based light is baked once at that yaw rather than at
# 0 and then again - which is what sky_set_yaw at boot used to cost.
var sky_start_yaw: int = 0
var sky_baked: bool = false # the IBL set exists, baked at sky_baked_yaw
var sky_baked_yaw: int = 0
var sky_p_irr: int = 0
var sky_p_pre: int = 0
var sky_p_brdf: int = 0
# turn the HDRI so its sun sits at world azimuth `yaw` (radians, 0 = toward -z)
function sky_set_yaw(yaw: int) -> void {
sky_set_rot(yaw)
# world sun = rotY(sun_hdri, -yaw): the lookup rotates a world direction by +yaw
let s = sun_hdri
v3_set(sun_dir, f_sub(f_mul(sky_rot_c, s[0]), f_mul(sky_rot_s, s[2])), s[1], f_add(f_mul(sky_rot_s, s[0]), f_mul(sky_rot_c, s[2])))
# the light is already baked at this yaw: turning to it again bakes nothing
if sky_baked and yaw == sky_baked_yaw { return }
sky_precompute()
}
# turn only the visible sky image (cheap, per frame): the light and the convolved
# maps stay where they are — daylight.ludic moves those on its own terms
function sky_set_rot(yaw: int) -> void {
sky_yaw = yaw
sky_rot_s = f_sin(yaw); sky_rot_c = f_cos(yaw)
}
function sky_bind_rot(prog: int) -> void { u_f2(gpu_uniform(prog, "u_sky_rot"), sky_rot_s, sky_rot_c) }
# direction for an equirect uv (matches equirectUV in lighting.glsl)
function sky_dir_from_uv(o: words, u: int, v: int) -> void {
let phi = f_mul(f_sub(u, F_HALF), f_mul(F_TWO, F_PI))
let theta = f_mul(v, F_PI)
let st = f_sin(theta)
v3_set(o, f_mul(st, f_sin(phi)), f_cos(theta), f_neg(f_mul(st, f_cos(phi))))
}
function sky_load(path: string) -> bool {
sky_tex = tex_load_hdr(path)
if sky_tex == 0 { return false }
sky_w = tex_w; sky_h = tex_h
sun_dir = words(3)
sun_hdri = words(3)
sky_dir_from_uv(sun_hdri, fr(hdr_max_x * 2 + 1, sky_w * 2), fr(hdr_max_y * 2 + 1, sky_h * 2))
v3_copy(sun_dir, sun_hdri)
sky_rot_c = F_ONE
# the sun's irradiance is what the IBL clip leaves out of the map; lighting it
# directly with that keeps sun and sky in the photograph's own proportion
sun_color = v3_new(f_mul(hdr_sun_r, sky_sun_boost), f_mul(hdr_sun_g, sky_sun_boost), f_mul(hdr_sun_b, sky_sun_boost))
print(`sun irradiance: {f_fx(hdr_sun_r)} {f_fx(hdr_sun_g)} {f_fx(hdr_sun_b)} (Q16.16), clip {f_fx(hdr_clip)}`)
print(`sky: {sky_w}x{sky_h}, sun at texel {hdr_max_x},{hdr_max_y}`)
sky_fullscreen = mesh_fullscreen()
if sky_start_yaw != 0 { sky_set_yaw(sky_start_yaw) } else { sky_precompute() }
return true
}
function sky_convolve(prog: int, target_tex: int, layer: int, w: int, h: int, rough: int) -> void {
let fbo = gpu_fb_new()
gpu_fb_bind(fbo)
if layer < 0 { gpu_fb_color(0, target_tex) }
else { gpu_fb_color_layer(0, target_tex, layer) }
gpu_viewport(0, 0, w, h)
gpu_use_program(prog)
r3d_bind_2d(prog, "u_sky", 0, sky_tex)
sky_bind_rot(prog)
u_f(gpu_uniform(prog, "u_sun_clip"), hdr_clip)
u_f(gpu_uniform(prog, "u_rough"), rough)
u_f(gpu_uniform(prog, "u_sky_w"), fi(sky_w))
mesh_draw(sky_fullscreen)
gpu_fb_bind(0)
gpu_fb_free(fbo)
}
# the prefiltered specular's width per level (its height is half): 256, 512 or 1024. The image
# the reflections and the rough sheen are lit from; sky_set_quality bakes it again at a new size.
var sky_prefilter_w: int = 512
function sky_set_quality(w: int) -> void {
if w < 64 or w == sky_prefilter_w { return }
sky_prefilter_w = w
if sky_irradiance != 0 { sky_precompute() }
}
function sky_precompute() -> void {
gpu_depth_test(false)
if sky_irradiance != 0 {
gpu_tex_free(sky_irradiance)
gpu_tex_free(sky_prefilter)
gpu_tex_free(sky_brdf)
}
# irradiance: 128 x 64 equirect
if sky_p_irr == 0 { sky_p_irr = r3d_program("fullscreen.vert", "ibl_irradiance.frag", ""); sky_p_pre = r3d_program("fullscreen.vert", "ibl_prefilter.frag", ""); sky_p_brdf = r3d_program("fullscreen.vert", "ibl_brdf.frag", "") }
let p_irr = sky_p_irr
sky_irradiance = tex_target(128, 64, GL_RGB16F, GL_RGB, GL_FLOAT, GL_LINEAR)
gpu_tex_bind(GPU_TEX2D, sky_irradiance)
gpu_tex_param(GPU_TEX2D, GL_TEXTURE_WRAP_S, GL_REPEAT)
sky_convolve(p_irr, sky_irradiance, -1, 128, 64, F_ZERO)
# prefiltered specular: 6 roughness levels, sky_prefilter_w x half each, as a 2D array
let p_pre = sky_p_pre
sky_prefilter = gpu_tex_new()
gpu_tex_bind(GPU_TEX2D_ARRAY, sky_prefilter)
gpu_tex_image3d(GL_RGB16F, sky_prefilter_w, sky_prefilter_w / 2, SKY_PREFILTER_LEVELS, GL_RGB, GL_FLOAT, null)
gpu_tex_param(GPU_TEX2D_ARRAY, GL_TEXTURE_WRAP_S, GL_REPEAT)
gpu_tex_param(GPU_TEX2D_ARRAY, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE)
gpu_tex_param(GPU_TEX2D_ARRAY, GL_TEXTURE_MAG_FILTER, GL_LINEAR)
gpu_tex_param(GPU_TEX2D_ARRAY, GL_TEXTURE_MIN_FILTER, GL_LINEAR)
for l in 0 .. SKY_PREFILTER_LEVELS {
sky_convolve(p_pre, sky_prefilter, l, sky_prefilter_w, sky_prefilter_w / 2, fr(l, SKY_PREFILTER_LEVELS - 1))
}
# BRDF LUT
let p_brdf = sky_p_brdf
sky_brdf = tex_target(256, 256, GL_RG16F, GL_RG, GL_FLOAT, GL_LINEAR)
sky_convolve(p_brdf, sky_brdf, -1, 256, 256, F_ZERO)
sky_baked = true
sky_baked_yaw = sky_yaw
gpu_check("sky precompute")
}
# bind the IBL set + sun for a lit program (units 12..14)
function sky_bind_lighting(prog: int) -> void {
r3d_bind_2d(prog, "u_irradiance", 12, sky_irradiance)
r3d_bind_tex(prog, "u_prefilter", 13, GPU_TEX2D_ARRAY, sky_prefilter)
r3d_bind_2d(prog, "u_brdf", 14, sky_brdf)
u_v3(gpu_uniform(prog, "u_sun_dir"), sun_dir)
u_v3(gpu_uniform(prog, "u_sun_color"), sun_color)
u_v3(gpu_uniform(prog, "u_cam_pos"), cam_pos)
u_f(gpu_uniform(prog, "u_prefilter_levels"), fi(SKY_PREFILTER_LEVELS))
daylight_bind(prog)
}