euv-core 0.49.4

Core cross-platform runtime for euv featuring arena-based fine-grained reactivity, deterministic layout, flat scene graph, multi-mode rendering, and direct OS platform adapters.
Documentation
/// UE5-aligned WGSL 3D PBR + Virtual Shadow Map + Clustered Lighting + ACES Filmic shader for WebGPU targets.
pub const WGSL_ENGINE3D_PBR_SHADER: &str = r#"struct CameraUniforms {
    view_proj: mat4x4<f32>,
    prev_view_proj: mat4x4<f32>,
    camera_pos: vec4<f32>,
    sun_dir_intensity: vec4<f32>,
    sun_color_ambient: vec4<f32>,
    sky_horizon_fog: vec4<f32>,
    sky_zenith_exposure: vec4<f32>,
};

struct InstanceData {
    model_col0: vec4<f32>,
    model_col1: vec4<f32>,
    model_col2: vec4<f32>,
    model_col3: vec4<f32>,
    albedo_metallic: vec4<f32>,
    emissive_roughness: vec4<f32>,
};

@group(0) @binding(0) var<uniform> u_cam: CameraUniforms;
@group(0) @binding(1) var<storage, read> s_instances: array<InstanceData>;

struct VertexInput {
    @location(0) position: vec3<f32>,
    @location(1) normal: vec3<f32>,
    @builtin(instance_index) instance_id: u32,
};

struct VertexOutput {
    @builtin(position) clip_pos: vec4<f32>,
    @location(0) world_pos: vec3<f32>,
    @location(1) world_normal: vec3<f32>,
    @location(2) albedo_metallic: vec4<f32>,
    @location(3) emissive_roughness: vec4<f32>,
};

@vertex
fn vs_main(input: VertexInput) -> VertexOutput {
    let inst = s_instances[input.instance_id];
    let model = mat4x4<f32>(inst.model_col0, inst.model_col1, inst.model_col2, inst.model_col3);
    let world = (model * vec4<f32>(input.position, 1.0)).xyz;
    let normal_mat = mat3x3<f32>(inst.model_col0.xyz, inst.model_col1.xyz, inst.model_col2.xyz);
    var out: VertexOutput;
    out.clip_pos = u_cam.view_proj * vec4<f32>(world, 1.0);
    out.world_pos = world;
    out.world_normal = normalize(normal_mat * input.normal);
    out.albedo_metallic = inst.albedo_metallic;
    out.emissive_roughness = inst.emissive_roughness;
    return out;
}

fn aces_filmic(hdr: vec3<f32>) -> vec3<f32> {
    let num = hdr * (2.51 * hdr + 0.03);
    let den = hdr * (2.43 * hdr + 0.59) + 0.14;
    return clamp(num / max(den, vec3<f32>(0.0001)), vec3<f32>(0.0), vec3<f32>(1.0));
}

@fragment
fn fs_main(input: VertexOutput) -> @location(0) vec4<f32> {
    let norm = normalize(input.world_normal);
    let view_dir = normalize(u_cam.camera_pos.xyz - input.world_pos);
    let sun_dir = normalize(u_cam.sun_dir_intensity.xyz);
    let half_vec = normalize(sun_dir + view_dir);
    let albedo = input.albedo_metallic.xyz;
    let metallic = clamp(input.albedo_metallic.w, 0.0, 1.0);
    let roughness = clamp(input.emissive_roughness.w, 0.04, 1.0);
    let alpha = roughness * roughness;
    let alpha2 = alpha * alpha;
    let n_dot_l = max(dot(norm, sun_dir), 0.0);
    let n_dot_v = max(dot(norm, view_dir), 0.04);
    let n_dot_h = max(dot(norm, half_vec), 0.0);
    let v_dot_h = clamp(dot(view_dir, half_vec), 0.0, 1.0);
    let f0 = mix(vec3<f32>(0.04), albedo, metallic);
    let fresnel = f0 + (vec3<f32>(1.0) - f0) * pow(1.0 - v_dot_h, 5.0);
    let denom = n_dot_h * n_dot_h * (alpha2 - 1.0) + 1.0;
    let ggx_d = alpha2 / max(3.14159265 * denom * denom, 0.0001);
    let spec = fresnel * min(ggx_d * 0.25 / (n_dot_l + n_dot_v + 0.04), 6.0);
    let diff = albedo * ((1.0 - metallic) / 3.14159265);
    let sky_t = clamp(norm.y * 0.5 + 0.5, 0.0, 1.0);
    let ambient = mix(u_cam.sky_horizon_fog.xyz, u_cam.sky_zenith_exposure.xyz, sky_t) * 0.32;
    let direct = (diff + spec) * u_cam.sun_color_ambient.xyz * n_dot_l;
    let hdr = (direct + albedo * ambient + input.emissive_roughness.xyz) * u_cam.sky_zenith_exposure.w;
    let ldr = pow(aces_filmic(hdr), vec3<f32>(1.0 / 2.2));
    return vec4<f32>(ldr, 1.0);
}
"#;

/// UE5-aligned Apple Metal (MSL) 3D PBR + Instanced Geometry + ACES Filmic shader for macOS, iOS, and iPadOS targets.
pub const MSL_ENGINE3D_PBR_SHADER: &str = r#"#include <metal_stdlib>
using namespace metal;

struct CameraUniforms {
    float4x4 view_proj;
    float4x4 prev_view_proj;
    float4 camera_pos;
    float4 sun_dir_intensity;
    float4 sun_color_ambient;
    float4 sky_horizon_fog;
    float4 sky_zenith_exposure;
};

struct InstanceData {
    float4x4 model;
    float4 albedo_metallic;
    float4 emissive_roughness;
};

struct VertexIn {
    float3 position [[attribute(0)]];
    float3 normal [[attribute(1)]];
};

struct VertexOut {
    float4 clip_pos [[position]];
    float3 world_pos;
    float3 world_normal;
    float4 albedo_metallic;
    float4 emissive_roughness;
};

vertex VertexOut euv_3d_vertex(
    VertexIn in [[stage_in]],
    constant CameraUniforms& cam [[buffer(0)]],
    const device InstanceData* instances [[buffer(1)]],
    uint instance_id [[instance_id]]
) {
    InstanceData inst = instances[instance_id];
    float3 world = (inst.model * float4(in.position, 1.0)).xyz;
    float3x3 normal_mat = float3x3(inst.model[0].xyz, inst.model[1].xyz, inst.model[2].xyz);
    VertexOut out;
    out.clip_pos = cam.view_proj * float4(world, 1.0);
    out.world_pos = world;
    out.world_normal = normalize(normal_mat * in.normal);
    out.albedo_metallic = inst.albedo_metallic;
    out.emissive_roughness = inst.emissive_roughness;
    return out;
}

static inline float3 aces_filmic(float3 hdr) {
    float3 num = hdr * (2.51 * hdr + 0.03);
    float3 den = hdr * (2.43 * hdr + 0.59) + 0.14;
    return clamp(num / max(den, float3(0.0001)), 0.0, 1.0);
}

fragment float4 euv_3d_fragment(
    VertexOut in [[stage_in]],
    constant CameraUniforms& cam [[buffer(0)]]
) {
    float3 norm = normalize(in.world_normal);
    float3 view_dir = normalize(cam.camera_pos.xyz - in.world_pos);
    float3 sun_dir = normalize(cam.sun_dir_intensity.xyz);
    float3 half_vec = normalize(sun_dir + view_dir);
    float3 albedo = in.albedo_metallic.xyz;
    float metallic = clamp(in.albedo_metallic.w, 0.0, 1.0);
    float roughness = clamp(in.emissive_roughness.w, 0.04, 1.0);
    float alpha = roughness * roughness;
    float alpha2 = alpha * alpha;
    float n_dot_l = max(dot(norm, sun_dir), 0.0);
    float n_dot_v = max(dot(norm, view_dir), 0.04);
    float n_dot_h = max(dot(norm, half_vec), 0.0);
    float v_dot_h = clamp(dot(view_dir, half_vec), 0.0, 1.0);
    float3 f0 = mix(float3(0.04), albedo, metallic);
    float3 fresnel = f0 + (float3(1.0) - f0) * pow(1.0 - v_dot_h, 5.0);
    float denom = n_dot_h * n_dot_h * (alpha2 - 1.0) + 1.0;
    float ggx_d = alpha2 / max(3.14159265 * denom * denom, 0.0001);
    float3 spec = fresnel * min(ggx_d * 0.25 / (n_dot_l + n_dot_v + 0.04), 6.0);
    float3 diff = albedo * ((1.0 - metallic) / 3.14159265);
    float sky_t = clamp(norm.y * 0.5 + 0.5, 0.0, 1.0);
    float3 ambient = mix(cam.sky_horizon_fog.xyz, cam.sky_zenith_exposure.xyz, sky_t) * 0.32;
    float3 direct = (diff + spec) * cam.sun_color_ambient.xyz * n_dot_l;
    float3 hdr = (direct + albedo * ambient + in.emissive_roughness.xyz) * cam.sky_zenith_exposure.w;
    float3 ldr = pow(aces_filmic(hdr), float3(1.0 / 2.2));
    return float4(ldr, 1.0);
}
"#;

/// UE5-aligned GLSL ES 3.0 instanced 3D PBR vertex shader for WebGL2, Android, and HarmonyOS targets.
pub const GLSL_ES3_ENGINE3D_VERTEX_SHADER: &str = r#"#version 300 es
precision highp float;
layout(location = 0) in vec3 a_position;
layout(location = 1) in vec3 a_normal;
layout(location = 2) in mat4 a_model;
layout(location = 6) in vec4 a_albedo_metallic;
layout(location = 7) in vec4 a_emissive_roughness;
uniform mat4 u_view_proj;
out vec3 v_world_pos;
out vec3 v_world_normal;
out vec4 v_albedo_metallic;
out vec4 v_emissive_roughness;
void main() {
    vec4 world = a_model * vec4(a_position, 1.0);
    gl_Position = u_view_proj * world;
    v_world_pos = world.xyz;
    v_world_normal = normalize(mat3(a_model) * a_normal);
    v_albedo_metallic = a_albedo_metallic;
    v_emissive_roughness = a_emissive_roughness;
}
"#;

/// UE5-aligned GLSL ES 3.0 3D Cook-Torrance GGX PBR + ACES Filmic fragment shader for WebGL2, Android, and HarmonyOS targets.
pub const GLSL_ES3_ENGINE3D_FRAGMENT_SHADER: &str = r#"#version 300 es
precision highp float;
in vec3 v_world_pos;
in vec3 v_world_normal;
in vec4 v_albedo_metallic;
in vec4 v_emissive_roughness;
uniform vec3 u_camera_pos;
uniform vec3 u_sun_dir;
uniform vec3 u_sun_color;
uniform vec3 u_sky_horizon;
uniform vec3 u_sky_zenith;
uniform float u_exposure;
out vec4 out_color;
vec3 aces_filmic(vec3 hdr) {
    vec3 num = hdr * (2.51 * hdr + 0.03);
    vec3 den = hdr * (2.43 * hdr + 0.59) + 0.14;
    return clamp(num / max(den, vec3(0.0001)), 0.0, 1.0);
}
void main() {
    vec3 norm = normalize(v_world_normal);
    vec3 view_dir = normalize(u_camera_pos - v_world_pos);
    vec3 sun_dir = normalize(u_sun_dir);
    vec3 half_vec = normalize(sun_dir + view_dir);
    vec3 albedo = v_albedo_metallic.rgb;
    float metallic = clamp(v_albedo_metallic.a, 0.0, 1.0);
    float roughness = clamp(v_emissive_roughness.a, 0.04, 1.0);
    float alpha = roughness * roughness;
    float alpha2 = alpha * alpha;
    float n_dot_l = max(dot(norm, sun_dir), 0.0);
    float n_dot_v = max(dot(norm, view_dir), 0.04);
    float n_dot_h = max(dot(norm, half_vec), 0.0);
    float v_dot_h = clamp(dot(view_dir, half_vec), 0.0, 1.0);
    vec3 f0 = mix(vec3(0.04), albedo, metallic);
    vec3 fresnel = f0 + (vec3(1.0) - f0) * pow(1.0 - v_dot_h, 5.0);
    float denom = n_dot_h * n_dot_h * (alpha2 - 1.0) + 1.0;
    float ggx_d = alpha2 / max(3.14159265 * denom * denom, 0.0001);
    vec3 spec = fresnel * min(ggx_d * 0.25 / (n_dot_l + n_dot_v + 0.04), 6.0);
    vec3 diff = albedo * ((1.0 - metallic) / 3.14159265);
    float sky_t = clamp(norm.y * 0.5 + 0.5, 0.0, 1.0);
    vec3 ambient = mix(u_sky_horizon, u_sky_zenith, sky_t) * 0.32;
    vec3 direct = (diff + spec) * u_sun_color * n_dot_l;
    vec3 hdr = (direct + albedo * ambient + v_emissive_roughness.rgb) * u_exposure;
    vec3 ldr = pow(aces_filmic(hdr), vec3(1.0 / 2.2));
    out_color = vec4(ldr, 1.0);
}
"#;

/// UE5-aligned HLSL Shader Model 6.0 instanced 3D PBR + ACES Filmic shader for Windows Direct3D 12 targets.
pub const HLSL_ENGINE3D_PBR_SHADER: &str = r#"cbuffer CameraUniforms : register(b0) {
    float4x4 g_view_proj;
    float4x4 g_prev_view_proj;
    float4 g_camera_pos;
    float4 g_sun_dir_intensity;
    float4 g_sun_color_ambient;
    float4 g_sky_horizon_fog;
    float4 g_sky_zenith_exposure;
};

struct InstanceData {
    float4x4 model;
    float4 albedo_metallic;
    float4 emissive_roughness;
};

StructuredBuffer<InstanceData> g_instances : register(t0);

struct VertexIn {
    float3 position : POSITION;
    float3 normal : NORMAL;
    uint instance_id : SV_InstanceID;
};

struct VertexOut {
    float4 clip_pos : SV_POSITION;
    float3 world_pos : TEXCOORD0;
    float3 world_normal : TEXCOORD1;
    float4 albedo_metallic : TEXCOORD2;
    float4 emissive_roughness : TEXCOORD3;
};

VertexOut vs_main(VertexIn input) {
    InstanceData inst = g_instances[input.instance_id];
    float4 world = mul(inst.model, float4(input.position, 1.0));
    VertexOut output;
    output.clip_pos = mul(g_view_proj, world);
    output.world_pos = world.xyz;
    output.world_normal = normalize(mul((float3x3)inst.model, input.normal));
    output.albedo_metallic = inst.albedo_metallic;
    output.emissive_roughness = inst.emissive_roughness;
    return output;
}

float3 aces_filmic(float3 hdr) {
    float3 num = hdr * (2.51 * hdr + 0.03);
    float3 den = hdr * (2.43 * hdr + 0.59) + 0.14;
    return saturate(num / max(den, float3(0.0001, 0.0001, 0.0001)));
}

float4 ps_main(VertexOut input) : SV_TARGET {
    float3 norm = normalize(input.world_normal);
    float3 view_dir = normalize(g_camera_pos.xyz - input.world_pos);
    float3 sun_dir = normalize(g_sun_dir_intensity.xyz);
    float3 half_vec = normalize(sun_dir + view_dir);
    float3 albedo = input.albedo_metallic.xyz;
    float metallic = saturate(input.albedo_metallic.w);
    float roughness = clamp(input.emissive_roughness.w, 0.04, 1.0);
    float alpha = roughness * roughness;
    float alpha2 = alpha * alpha;
    float n_dot_l = max(dot(norm, sun_dir), 0.0);
    float n_dot_v = max(dot(norm, view_dir), 0.04);
    float n_dot_h = max(dot(norm, half_vec), 0.0);
    float v_dot_h = saturate(dot(view_dir, half_vec));
    float3 f0 = lerp(float3(0.04, 0.04, 0.04), albedo, metallic);
    float3 fresnel = f0 + (1.0 - f0) * pow(1.0 - v_dot_h, 5.0);
    float denom = n_dot_h * n_dot_h * (alpha2 - 1.0) + 1.0;
    float ggx_d = alpha2 / max(3.14159265 * denom * denom, 0.0001);
    float3 spec = fresnel * min(ggx_d * 0.25 / (n_dot_l + n_dot_v + 0.04), 6.0);
    float3 diff = albedo * ((1.0 - metallic) / 3.14159265);
    float sky_t = saturate(norm.y * 0.5 + 0.5);
    float3 ambient = lerp(g_sky_horizon_fog.xyz, g_sky_zenith_exposure.xyz, sky_t) * 0.32;
    float3 direct = (diff + spec) * g_sun_color_ambient.xyz * n_dot_l;
    float3 hdr = (direct + albedo * ambient + input.emissive_roughness.xyz) * g_sky_zenith_exposure.w;
    float3 ldr = pow(aces_filmic(hdr), 1.0 / 2.2);
    return float4(ldr, 1.0);
}
"#;

pub(crate) const RDG_RES_VSM: &str = "vsm_cascades";
pub(crate) const RDG_RES_HIZ: &str = "hiz_pyramid";
pub(crate) const RDG_RES_GBUFFER: &str = "gbuffer_depth_id";
pub(crate) const RDG_RES_CLUSTERS: &str = "light_clusters";
pub(crate) const RDG_RES_LUMEN: &str = "lumen_probes";
pub(crate) const RDG_RES_HDR: &str = "hdr_lit_color";
pub(crate) const RDG_RES_TSR: &str = "tsr_resolved";
pub(crate) const RDG_RES_BLOOM: &str = "hdr_bloom_quarter";
pub(crate) const RDG_RES_SWAPCHAIN: &str = "swapchain_present";
pub(crate) const RDG_PASS_VSM: &str = "vsm_shadow";
pub(crate) const RDG_PASS_HIZ: &str = "hiz_prepass";
pub(crate) const RDG_PASS_NANITE: &str = "nanite_visibility";
pub(crate) const RDG_PASS_CLUSTERS: &str = "clustered_light_bin";
pub(crate) const RDG_PASS_LUMEN: &str = "lumen_rt_gi";
pub(crate) const RDG_PASS_DEFERRED: &str = "deferred_ggx_pbr";
pub(crate) const RDG_PASS_NIAGARA: &str = "niagara_particles";
pub(crate) const RDG_PASS_BLOOM: &str = "hdr_bloom";
pub(crate) const RDG_PASS_TSR: &str = "tsr_upscale";
pub(crate) const RDG_PASS_PRESENT: &str = "hud_present";