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>,
csm_splits: vec4<f32>,
csm_matrices: array<mat4x4<f32>, 4>,
ibl_sh: array<vec4<f32>, 9>,
tonemap_cluster_params: 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>,
material_skin_morph_flags: vec4<u32>,
};
struct GpuMaterialData {
base_color_factor: vec4<f32>,
emissive_alpha_cutoff: vec4<f32>,
pbr_ior_specular: vec4<f32>,
specular_color_clearcoat: vec4<f32>,
clearcoat_normal_occ_trans: vec4<f32>,
volume_attenuation: vec4<f32>,
sheen_color_roughness: vec4<f32>,
tex_indices_a: vec4<u32>,
tex_indices_b: vec4<u32>,
flags_volume: vec4<u32>,
};
struct PackedLocalLight {
pos_inv_r2: vec4<f32>,
radiance_radius: vec4<f32>,
dir_outer_cos: vec4<f32>,
inv_cone_area: vec4<f32>,
};
@group(0) @binding(0) var<uniform> u_cam: CameraUniforms;
@group(0) @binding(1) var<storage, read> s_instances: array<InstanceData>;
@group(0) @binding(2) var<storage, read> s_materials: array<GpuMaterialData>;
@group(0) @binding(3) var<storage, read> s_lights: array<PackedLocalLight>;
@group(0) @binding(4) var t_textures: texture_2d_array<f32>;
@group(0) @binding(5) var t_csm_shadow: texture_depth_2d_array;
@group(0) @binding(6) var t_ibl_oct: texture_2d<f32>;
@group(0) @binding(7) var t_brdf_lut: texture_2d<f32>;
@group(0) @binding(8) var s_linear: sampler;
@group(0) @binding(9) var s_shadow: sampler_comparison;
struct VertexInput {
@location(0) position: vec3<f32>,
@location(1) normal: vec3<f32>,
@location(2) tangent: vec4<f32>,
@location(3) uv0: vec2<f32>,
@location(4) uv1: vec2<f32>,
@location(5) color0: vec4<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) world_tangent: vec4<f32>,
@location(3) uv0: vec2<f32>,
@location(4) color0: vec4<f32>,
@location(5) albedo_metallic: vec4<f32>,
@location(6) emissive_roughness: vec4<f32>,
@location(7) @interpolate(flat) material_flags: vec4<u32>,
};
fn cofactor_normal_matrix(c0: vec3<f32>, c1: vec3<f32>, c2: vec3<f32>) -> mat3x3<f32> {
let n0 = cross(c1, c2);
let n1 = cross(c2, c0);
let n2 = cross(c0, c1);
let det = dot(c0, n0);
let inv_det = select(1.0, 1.0 / det, abs(det) > 1.0e-8);
return mat3x3<f32>(n0 * inv_det, n1 * inv_det, n2 * inv_det);
}
@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 = cofactor_normal_matrix(inst.model_col0.xyz, inst.model_col1.xyz, inst.model_col2.xyz);
let world_n = normalize(normal_mat * input.normal);
let world_t = normalize((model * vec4<f32>(input.tangent.xyz, 0.0)).xyz);
var out: VertexOutput;
out.clip_pos = u_cam.view_proj * vec4<f32>(world, 1.0);
out.world_pos = world;
out.world_normal = world_n;
out.world_tangent = vec4<f32>(world_t, input.tangent.w);
out.uv0 = input.uv0;
out.color0 = input.color0;
out.albedo_metallic = inst.albedo_metallic;
out.emissive_roughness = inst.emissive_roughness;
out.material_flags = inst.material_skin_morph_flags;
return out;
}
fn dielectric_f0_from_ior(ior: f32) -> f32 {
let r = (ior - 1.0) / max(ior + 1.0, 1.0e-4);
return r * r;
}
fn distribution_ggx(n_dot_h: f32, alpha2: f32) -> f32 {
let denom = n_dot_h * n_dot_h * (alpha2 - 1.0) + 1.0;
return alpha2 / max(3.14159265 * denom * denom, 1.0e-5);
}
fn visibility_smith_ggx(n_dot_v: f32, n_dot_l: f32, alpha: f32) -> f32 {
let a2 = alpha * alpha;
let ggx_v = n_dot_l * sqrt(n_dot_v * n_dot_v * (1.0 - a2) + a2);
let ggx_l = n_dot_v * sqrt(n_dot_l * n_dot_l * (1.0 - a2) + a2);
return 0.5 / max(ggx_v + ggx_l, 1.0e-5);
}
fn distribution_charlie(roughness: f32, n_dot_h: f32) -> f32 {
let inv_r = 1.0 / max(roughness, 0.07);
let sin2_h = max(1.0 - n_dot_h * n_dot_h, 0.0078125);
return (2.0 + inv_r) * pow(sin2_h, inv_r * 0.5) / 6.2831853;
}
fn visibility_ashikhmin(n_dot_v: f32, n_dot_l: f32) -> f32 {
return 1.0 / max(4.0 * (n_dot_l + n_dot_v - n_dot_l * n_dot_v), 1.0e-4);
}
fn octahedral_encode_dir(dir: vec3<f32>) -> vec2<f32> {
let d = normalize(dir);
let l1 = max(abs(d.x) + abs(d.y) + abs(d.z), 1.0e-6);
var ox = d.x / l1;
var oz = d.z / l1;
if (d.y < 0.0) {
let px = ox;
let pz = oz;
ox = (1.0 - abs(pz)) * select(-1.0, 1.0, px >= 0.0);
oz = (1.0 - abs(px)) * select(-1.0, 1.0, pz >= 0.0);
}
return vec2<f32>(ox * 0.5 + 0.5, oz * 0.5 + 0.5);
}
fn eval_sh9_diffuse(norm: vec3<f32>) -> vec3<f32> {
let x = norm.x;
let y = norm.y;
let z = norm.z;
var irrad = u_cam.ibl_sh[0].xyz * 0.2820948;
irrad += u_cam.ibl_sh[1].xyz * (0.4886025 * y);
irrad += u_cam.ibl_sh[2].xyz * (0.4886025 * z);
irrad += u_cam.ibl_sh[3].xyz * (0.4886025 * x);
irrad += u_cam.ibl_sh[4].xyz * (1.0925484 * x * y);
irrad += u_cam.ibl_sh[5].xyz * (1.0925484 * y * z);
irrad += u_cam.ibl_sh[6].xyz * (0.3153916 * (3.0 * z * z - 1.0));
irrad += u_cam.ibl_sh[7].xyz * (1.0925484 * x * z);
irrad += u_cam.ibl_sh[8].xyz * (0.5462742 * (x * x - y * y));
return max(irrad, vec3<f32>(0.0));
}
fn sample_csm_vogel_16(world_pos: vec3<f32>, norm: vec3<f32>, sun_dir: vec3<f32>, view_depth: f32) -> f32 {
var cascade: i32 = 3;
if (view_depth < u_cam.csm_splits.x) {
cascade = 0;
} else if (view_depth < u_cam.csm_splits.y) {
cascade = 1;
} else if (view_depth < u_cam.csm_splits.z) {
cascade = 2;
}
let biased_pos = world_pos + norm * (0.04 + 0.02 * f32(cascade));
let light_clip = u_cam.csm_matrices[cascade] * vec4<f32>(biased_pos, 1.0);
let uv = light_clip.xy * 0.5 + vec2<f32>(0.5, 0.5);
let depth = light_clip.z;
if (uv.x < 0.01 || uv.x > 0.99 || uv.y < 0.01 || uv.y > 0.99 || depth <= 0.0 || depth >= 1.0) {
return 1.0;
}
let n_dot_l = clamp(dot(norm, sun_dir), 0.05, 1.0);
let bias = clamp(0.0022 * (1.0 - n_dot_l) + 0.0012, 0.001, 0.008);
let angle = fract(sin(dot(world_pos, vec3<f32>(12.9898, 37.719, 78.233))) * 43758.547) * 6.2831853;
let c_rot = cos(angle);
let s_rot = sin(angle);
let radius = 0.0025;
var vis: f32 = 0.0;
for (var tap: i32 = 0; tap < 16; tap = tap + 1) {
let r_tap = sqrt((f32(tap) + 0.5) / 16.0) * radius;
let theta = f32(tap) * 2.39996323;
let ox = cos(theta) * r_tap;
let oy = sin(theta) * r_tap;
let rot_uv = uv + vec2<f32>(ox * c_rot - oy * s_rot, ox * s_rot + oy * c_rot);
vis += textureSampleCompare(t_csm_shadow, s_shadow, rot_uv, cascade, depth - bias);
}
return 0.22 + 0.78 * (vis / 16.0);
}
fn agx_contrast_poly(x: vec3<f32>) -> vec3<f32> {
let x2 = x * x;
let x4 = x2 * x2;
return clamp(15.5 * x4 * x2 - 40.14 * x4 * x + 31.96 * x4 - 6.868 * x2 * x + 0.4298 * x2 + 0.1191 * x - 0.00232, vec3<f32>(0.0), vec3<f32>(1.0));
}
fn agx_tonemap(hdr: vec3<f32>) -> vec3<f32> {
let inset = mat3x3<f32>(
vec3<f32>(0.84247906, 0.04232824, 0.04237565),
vec3<f32>(0.07843360, 0.87846864, 0.07843360),
vec3<f32>(0.07922375, 0.07916613, 0.87914297),
);
let outset = mat3x3<f32>(
vec3<f32>(1.19687900, -0.05289685, -0.05297163),
vec3<f32>(-0.09802088, 1.15190313, -0.09804345),
vec3<f32>(-0.09902975, -0.09896118, 1.15107367),
);
let val = max(inset * max(hdr, vec3<f32>(1.0e-6)), vec3<f32>(1.0e-6));
let log_val = clamp((log2(val) - vec3<f32>(-12.47393)) / vec3<f32>(16.5), vec3<f32>(0.0), vec3<f32>(1.0));
let curved = agx_contrast_poly(log_val);
return clamp(outset * curved, vec3<f32>(0.0), vec3<f32>(1.0));
}
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, @builtin(front_facing) is_front: bool) -> @location(0) vec4<f32> {
let use_principled = (input.material_flags.w & 2u) != 0u;
var base_rgba = vec4<f32>(input.albedo_metallic.xyz, 1.0) * input.color0;
var metallic = clamp(input.albedo_metallic.w, 0.0, 1.0);
var roughness = clamp(input.emissive_roughness.w, 0.04, 1.0);
var emissive = input.emissive_roughness.xyz;
var ior: f32 = 1.5;
var spec_factor: f32 = 1.0;
var spec_color = vec3<f32>(1.0);
var clearcoat: f32 = 0.0;
var clearcoat_rough: f32 = 0.04;
var sheen_color = vec3<f32>(0.0);
var sheen_rough: f32 = 0.5;
var transmission: f32 = 0.0;
var thickness: f32 = 0.0;
var atten_color = vec3<f32>(1.0);
var atten_dist: f32 = 1.0e6;
var occlusion: f32 = 1.0;
var unlit: bool = false;
var norm = normalize(input.world_normal);
if (!is_front) {
norm = -norm;
}
let t_ortho = normalize(input.world_tangent.xyz - norm * dot(input.world_tangent.xyz, norm));
let bitangent = cross(norm, t_ortho) * input.world_tangent.w;
if (use_principled) {
let mat = s_materials[input.material_flags.x];
base_rgba = mat.base_color_factor * input.color0;
if (mat.tex_indices_a.x != 0xffffffffu) {
base_rgba *= textureSample(t_textures, s_linear, input.uv0, i32(mat.tex_indices_a.x));
}
let flags = mat.flags_volume.x;
if ((flags & 1u) != 0u && base_rgba.a < mat.emissive_alpha_cutoff.w) {
discard;
}
unlit = (flags & 8u) != 0u;
metallic = clamp(mat.pbr_ior_specular.x, 0.0, 1.0);
roughness = clamp(mat.pbr_ior_specular.y, 0.04, 1.0);
if (mat.tex_indices_a.y != 0xffffffffu) {
let mr = textureSample(t_textures, s_linear, input.uv0, i32(mat.tex_indices_a.y));
roughness = clamp(roughness * mr.g, 0.04, 1.0);
metallic = clamp(metallic * mr.b, 0.0, 1.0);
}
if (mat.tex_indices_a.z != 0xffffffffu) {
let map_n = textureSample(t_textures, s_linear, input.uv0, i32(mat.tex_indices_a.z)).xyz * 2.0 - vec3<f32>(1.0);
let scaled_n = normalize(vec3<f32>(map_n.xy * mat.clearcoat_normal_occ_trans.y, map_n.z));
norm = normalize(t_ortho * scaled_n.x + bitangent * scaled_n.y + norm * scaled_n.z);
}
if (mat.tex_indices_a.w != 0xffffffffu) {
let occ_s = textureSample(t_textures, s_linear, input.uv0, i32(mat.tex_indices_a.w)).r;
occlusion = mix(1.0, occ_s, mat.clearcoat_normal_occ_trans.z);
}
emissive = mat.emissive_alpha_cutoff.xyz;
if (mat.tex_indices_b.x != 0xffffffffu) {
emissive *= textureSample(t_textures, s_linear, input.uv0, i32(mat.tex_indices_b.x)).rgb;
}
ior = mat.pbr_ior_specular.z;
spec_factor = mat.pbr_ior_specular.w;
spec_color = mat.specular_color_clearcoat.xyz;
clearcoat = mat.specular_color_clearcoat.w;
clearcoat_rough = clamp(mat.clearcoat_normal_occ_trans.x, 0.04, 1.0);
transmission = mat.clearcoat_normal_occ_trans.w;
atten_color = mat.volume_attenuation.xyz;
thickness = mat.volume_attenuation.w;
atten_dist = max(bitcast<f32>(mat.flags_volume.y), 1.0e-4);
sheen_color = mat.sheen_color_roughness.xyz;
sheen_rough = clamp(mat.sheen_color_roughness.w, 0.07, 1.0);
}
if (unlit) {
let hdr_u = (base_rgba.rgb + emissive) * u_cam.sky_zenith_exposure.w;
let mapped_u = select(aces_filmic(hdr_u), agx_tonemap(hdr_u), u_cam.tonemap_cluster_params.x > 0.5);
return vec4<f32>(pow(mapped_u, vec3<f32>(1.0 / 2.2)), base_rgba.a);
}
let view_vec = u_cam.camera_pos.xyz - input.world_pos;
let view_dist = length(view_vec);
let view_dir = view_vec / max(view_dist, 1.0e-4);
let sun_dir = normalize(u_cam.sun_dir_intensity.xyz);
let n_dot_v = max(dot(norm, view_dir), 0.04);
let n_dot_l = max(dot(norm, sun_dir), 0.0);
let half_vec = normalize(sun_dir + view_dir);
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 dielec_f0 = min(vec3<f32>(dielectric_f0_from_ior(ior)) * spec_color, vec3<f32>(1.0)) * spec_factor;
let f0 = mix(dielec_f0, base_rgba.rgb, metallic);
let alpha = roughness * roughness;
let alpha2 = alpha * alpha;
let fresnel = f0 + (vec3<f32>(1.0) - f0) * pow(1.0 - v_dot_h, 5.0);
let ggx_d = distribution_ggx(n_dot_h, alpha2);
let ggx_v = visibility_smith_ggx(n_dot_v, max(n_dot_l, 0.04), alpha);
let brdf_uv = vec2<f32>(n_dot_v, roughness);
let brdf_lut = textureSample(t_brdf_lut, s_linear, brdf_uv).rg;
let e_ss = clamp(brdf_lut.x + brdf_lut.y, 0.05, 1.0);
let multi_scatter = vec3<f32>(1.0) + f0 * ((1.0 / e_ss) - 1.0);
let spec_direct = fresnel * (ggx_d * ggx_v) * multi_scatter;
let diff_direct = base_rgba.rgb * ((1.0 - metallic) * (1.0 - transmission) / 3.14159265);
let shadow_vis = sample_csm_vogel_16(input.world_pos, norm, sun_dir, view_dist);
var color_hdr = (diff_direct + spec_direct) * u_cam.sun_color_ambient.xyz * (n_dot_l * shadow_vis);
if (clearcoat > 0.001) {
let cc_alpha = clearcoat_rough * clearcoat_rough;
let cc_f = 0.04 + 0.96 * pow(1.0 - v_dot_h, 5.0);
let cc_d = distribution_ggx(n_dot_h, cc_alpha * cc_alpha);
let cc_v = visibility_smith_ggx(n_dot_v, max(n_dot_l, 0.04), cc_alpha);
color_hdr = color_hdr * (1.0 - clearcoat * cc_f) + vec3<f32>(cc_f * cc_d * cc_v * clearcoat) * u_cam.sun_color_ambient.xyz * (n_dot_l * shadow_vis);
}
if (dot(sheen_color, sheen_color) > 1.0e-5) {
let sh_d = distribution_charlie(sheen_rough, n_dot_h);
let sh_v = visibility_ashikhmin(n_dot_v, max(n_dot_l, 0.04));
color_hdr += sheen_color * (sh_d * sh_v) * u_cam.sun_color_ambient.xyz * (n_dot_l * shadow_vis);
}
let light_count = min(u32(u_cam.tonemap_cluster_params.y), 16u);
for (var l_idx: u32 = 0u; l_idx < light_count; l_idx = l_idx + 1u) {
let pl = s_lights[l_idx];
let to_l = pl.pos_inv_r2.xyz - input.world_pos;
let d2 = max(dot(to_l, to_l), 1.0e-4);
let ratio2 = d2 * pl.pos_inv_r2.w;
if (ratio2 < 1.0) {
let l_dir = to_l * inverseSqrt(d2);
let nl = max(dot(norm, l_dir), 0.0);
if (nl > 0.0) {
var spot = 1.0;
if (pl.dir_outer_cos.w > -1.5) {
let c_th = dot(-l_dir, pl.dir_outer_cos.xyz);
let raw_s = clamp((c_th - pl.dir_outer_cos.w) * pl.inv_cone_area.x, 0.0, 1.0);
spot = raw_s * raw_s;
}
let win = clamp(1.0 - ratio2 * ratio2, 0.0, 1.0);
let atten = (win * win * spot) / d2;
let h_l = normalize(l_dir + view_dir);
let nh = max(dot(norm, h_l), 0.0);
let vh = clamp(dot(view_dir, h_l), 0.0, 1.0);
let f_l = f0 + (vec3<f32>(1.0) - f0) * pow(1.0 - vh, 5.0);
let s_l = f_l * distribution_ggx(nh, alpha2) * visibility_smith_ggx(n_dot_v, nl, alpha);
color_hdr += (diff_direct + s_l) * pl.radiance_radius.xyz * (nl * atten);
}
}
}
let sh_irrad = eval_sh9_diffuse(norm);
let refl_dir = reflect(-view_dir, norm);
let ibl_spec = textureSampleLevel(t_ibl_oct, s_linear, octahedral_encode_dir(refl_dir), roughness * 5.0).rgb;
let ibl_diff = base_rgba.rgb * ((1.0 - metallic) * (1.0 - transmission)) * sh_irrad;
let ibl_f = f0 * brdf_lut.x + vec3<f32>(brdf_lut.y);
color_hdr += (ibl_diff + ibl_spec * ibl_f * multi_scatter) * occlusion;
if (transmission > 0.001) {
let refr_dir = refract(-view_dir, norm, 1.0 / max(ior, 1.0));
let refr_env = textureSampleLevel(t_ibl_oct, s_linear, octahedral_encode_dir(refr_dir), roughness * 4.0).rgb;
let sigma = -log(max(atten_color, vec3<f32>(1.0e-4))) / atten_dist;
let beer = exp(-sigma * max(thickness, 0.01));
color_hdr += refr_env * base_rgba.rgb * beer * (transmission * (1.0 - metallic));
}
let hdr_final = (color_hdr + emissive) * u_cam.sky_zenith_exposure.w;
let mapped = select(aces_filmic(hdr_final), agx_tonemap(hdr_final), u_cam.tonemap_cluster_params.x > 0.5);
let ldr = pow(mapped, vec3<f32>(1.0 / 2.2));
return vec4<f32>(ldr, base_rgba.a);
}
"#;
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;
float4 csm_splits;
float4x4 csm_matrices[4];
float4 ibl_sh[9];
float4 tonemap_cluster_params;
};
struct InstanceData {
float4x4 model;
float4 albedo_metallic;
float4 emissive_roughness;
uint4 material_skin_morph_flags;
};
struct VertexExtra {
float4 tangent;
float4 uv0_uv1;
float4 color0;
};
struct GpuMaterialData {
float4 base_color_factor;
float4 emissive_alpha_cutoff;
float4 pbr_ior_specular;
float4 specular_color_clearcoat;
float4 clearcoat_normal_occ_trans;
float4 volume_attenuation;
float4 sheen_color_roughness;
uint4 tex_indices_a;
uint4 tex_indices_b;
uint4 flags_volume;
};
struct PackedLocalLight {
float4 pos_inv_r2;
float4 radiance_radius;
float4 dir_outer_cos;
float4 inv_cone_area;
};
struct VertexIn {
float3 position [[attribute(0)]];
float3 normal [[attribute(1)]];
};
struct VertexOut {
float4 clip_pos [[position]];
float3 world_pos;
float3 world_normal;
float4 world_tangent;
float2 uv0;
float4 color0;
float4 albedo_metallic;
float4 emissive_roughness;
uint4 material_flags [[flat]];
};
static inline float3x3 cofactor_normal_matrix(float4x4 m) {
float3 c0 = m[0].xyz;
float3 c1 = m[1].xyz;
float3 c2 = m[2].xyz;
float3 n0 = cross(c1, c2);
float3 n1 = cross(c2, c0);
float3 n2 = cross(c0, c1);
float det = dot(c0, n0);
float inv_det = (abs(det) > 1.0e-8f) ? (1.0f / det) : 1.0f;
return float3x3(n0 * inv_det, n1 * inv_det, n2 * inv_det);
}
static inline float dielectric_f0_from_ior(float ior) {
float r = (ior - 1.0f) / max(ior + 1.0f, 1.0e-4f);
return r * r;
}
static inline float distribution_ggx(float n_dot_h, float alpha2) {
float denom = n_dot_h * n_dot_h * (alpha2 - 1.0f) + 1.0f;
return alpha2 / max(3.14159265f * denom * denom, 1.0e-5f);
}
static inline float visibility_smith_ggx(float n_dot_v, float n_dot_l, float alpha) {
float a2 = alpha * alpha;
float ggx_v = n_dot_l * sqrt(n_dot_v * n_dot_v * (1.0f - a2) + a2);
float ggx_l = n_dot_v * sqrt(n_dot_l * n_dot_l * (1.0f - a2) + a2);
return 0.5f / max(ggx_v + ggx_l, 1.0e-5f);
}
static inline float distribution_charlie(float roughness, float n_dot_h) {
float inv_r = 1.0f / max(roughness, 0.07f);
float sin2_h = max(1.0f - n_dot_h * n_dot_h, 0.0078125f);
return (2.0f + inv_r) * pow(sin2_h, inv_r * 0.5f) / 6.2831853f;
}
static inline float visibility_ashikhmin(float n_dot_v, float n_dot_l) {
return 1.0f / max(4.0f * (n_dot_l + n_dot_v - n_dot_l * n_dot_v), 1.0e-4f);
}
static inline float2 octahedral_encode_dir(float3 dir) {
float3 d = normalize(dir);
float l1 = max(abs(d.x) + abs(d.y) + abs(d.z), 1.0e-6f);
float ox = d.x / l1;
float oz = d.z / l1;
if (d.y < 0.0f) {
float px = ox;
float pz = oz;
ox = (1.0f - abs(pz)) * (px >= 0.0f ? 1.0f : -1.0f);
oz = (1.0f - abs(px)) * (pz >= 0.0f ? 1.0f : -1.0f);
}
return float2(ox * 0.5f + 0.5f, oz * 0.5f + 0.5f);
}
static inline float3 eval_sh9_diffuse(constant CameraUniforms& cam, float3 norm) {
float x = norm.x;
float y = norm.y;
float z = norm.z;
float3 irrad = cam.ibl_sh[0].xyz * 0.2820948f;
irrad += cam.ibl_sh[1].xyz * (0.4886025f * y);
irrad += cam.ibl_sh[2].xyz * (0.4886025f * z);
irrad += cam.ibl_sh[3].xyz * (0.4886025f * x);
irrad += cam.ibl_sh[4].xyz * (1.0925484f * x * y);
irrad += cam.ibl_sh[5].xyz * (1.0925484f * y * z);
irrad += cam.ibl_sh[6].xyz * (0.3153916f * (3.0f * z * z - 1.0f));
irrad += cam.ibl_sh[7].xyz * (1.0925484f * x * z);
irrad += cam.ibl_sh[8].xyz * (0.5462742f * (x * x - y * y));
return max(irrad, float3(0.0f));
}
static inline float sample_csm_vogel_16(
constant CameraUniforms& cam,
depth2d_array<float> csm_shadow,
sampler lin_sampler,
float3 world_pos,
float3 norm,
float3 sun_dir,
float view_depth
) {
uint cascade = 3u;
if (view_depth < cam.csm_splits.x) {
cascade = 0u;
} else if (view_depth < cam.csm_splits.y) {
cascade = 1u;
} else if (view_depth < cam.csm_splits.z) {
cascade = 2u;
}
float3 biased_pos = world_pos + norm * (0.04f + 0.02f * float(cascade));
float4 light_clip = cam.csm_matrices[cascade] * float4(biased_pos, 1.0f);
float2 uv = light_clip.xy * 0.5f + float2(0.5f, 0.5f);
float depth = light_clip.z;
if (uv.x < 0.01f || uv.x > 0.99f || uv.y < 0.01f || uv.y > 0.99f || depth <= 0.0f || depth >= 1.0f) {
return 1.0f;
}
float n_dot_l = clamp(dot(norm, sun_dir), 0.05f, 1.0f);
float bias = clamp(0.0022f * (1.0f - n_dot_l) + 0.0012f, 0.001f, 0.008f);
float angle = fract(sin(dot(world_pos, float3(12.9898f, 37.719f, 78.233f))) * 43758.547f) * 6.2831853f;
float c_rot = cos(angle);
float s_rot = sin(angle);
float radius = 0.0025f;
float vis = 0.0f;
for (int tap = 0; tap < 16; ++tap) {
float r_tap = sqrt((float(tap) + 0.5f) / 16.0f) * radius;
float theta = float(tap) * 2.39996323f;
float ox = cos(theta) * r_tap;
float oy = sin(theta) * r_tap;
float2 rot_uv = uv + float2(ox * c_rot - oy * s_rot, ox * s_rot + oy * c_rot);
float sampled_d = csm_shadow.sample(lin_sampler, rot_uv, cascade);
vis += (depth - bias <= sampled_d) ? 1.0f : 0.0f;
}
return 0.22f + 0.78f * (vis / 16.0f);
}
static inline float3 agx_contrast_poly(float3 x) {
float3 x2 = x * x;
float3 x4 = x2 * x2;
return clamp(15.5f * x4 * x2 - 40.14f * x4 * x + 31.96f * x4 - 6.868f * x2 * x + 0.4298f * x2 + 0.1191f * x - 0.00232f, 0.0f, 1.0f);
}
static inline float3 agx_tonemap(float3 hdr) {
float3x3 inset = float3x3(
float3(0.84247906f, 0.04232824f, 0.04237565f),
float3(0.07843360f, 0.87846864f, 0.07843360f),
float3(0.07922375f, 0.07916613f, 0.87914297f)
);
float3x3 outset = float3x3(
float3(1.19687900f, -0.05289685f, -0.05297163f),
float3(-0.09802088f, 1.15190313f, -0.09804345f),
float3(-0.09902975f, -0.09896118f, 1.15107367f)
);
float3 val = max(inset * max(hdr, float3(1.0e-6f)), float3(1.0e-6f));
float3 log_val = clamp((log2(val) - float3(-12.47393f)) / float3(16.5f), 0.0f, 1.0f);
return clamp(outset * agx_contrast_poly(log_val), 0.0f, 1.0f);
}
static inline float3 aces_filmic(float3 hdr) {
float3 num = hdr * (2.51f * hdr + 0.03f);
float3 den = hdr * (2.43f * hdr + 0.59f) + 0.14f;
return clamp(num / max(den, float3(0.0001f)), 0.0f, 1.0f);
}
vertex VertexOut euv_3d_vertex(
VertexIn in [[stage_in]],
constant CameraUniforms& cam [[buffer(0)]],
const device InstanceData* instances [[buffer(1)]],
const device VertexExtra* extras [[buffer(3)]],
uint vertex_id [[vertex_id]],
uint instance_id [[instance_id]]
) {
InstanceData inst = instances[instance_id];
float3 world = (inst.model * float4(in.position, 1.0f)).xyz;
float3x3 normal_mat = cofactor_normal_matrix(inst.model);
float3 world_n = normalize(normal_mat * in.normal);
VertexExtra extra = extras[vertex_id];
float3 world_t = normalize((inst.model * float4(extra.tangent.xyz, 0.0f)).xyz);
VertexOut out;
out.clip_pos = cam.view_proj * float4(world, 1.0f);
out.world_pos = world;
out.world_normal = world_n;
out.world_tangent = float4(world_t, extra.tangent.w);
out.uv0 = extra.uv0_uv1.xy;
out.color0 = extra.color0;
out.albedo_metallic = inst.albedo_metallic;
out.emissive_roughness = inst.emissive_roughness;
out.material_flags = inst.material_skin_morph_flags;
return out;
}
fragment float4 euv_3d_fragment(
VertexOut in [[stage_in]],
bool is_front [[front_facing]],
constant CameraUniforms& cam [[buffer(0)]],
const device GpuMaterialData* materials [[buffer(4)]],
const device PackedLocalLight* local_lights [[buffer(5)]],
texture2d_array<float> tex_pool [[texture(0)]],
depth2d_array<float> csm_shadow [[texture(1)]],
texture2d<float> ibl_oct [[texture(2)]],
texture2d<float> brdf_lut_tex [[texture(3)]],
sampler lin_sampler [[sampler(0)]]
) {
bool use_principled = (in.material_flags.w & 2u) != 0u;
float4 base_rgba = float4(in.albedo_metallic.xyz, 1.0f) * in.color0;
float metallic = clamp(in.albedo_metallic.w, 0.0f, 1.0f);
float roughness = clamp(in.emissive_roughness.w, 0.04f, 1.0f);
float3 emissive = in.emissive_roughness.xyz;
float ior = 1.5f;
float spec_factor = 1.0f;
float3 spec_color = float3(1.0f);
float clearcoat = 0.0f;
float clearcoat_rough = 0.04f;
float3 sheen_color = float3(0.0f);
float sheen_rough = 0.5f;
float transmission = 0.0f;
float thickness = 0.0f;
float3 atten_color = float3(1.0f);
float atten_dist = 1.0e6f;
float occlusion = 1.0f;
bool unlit = false;
float3 norm = normalize(in.world_normal);
if (!is_front) {
norm = -norm;
}
float3 t_ortho = normalize(in.world_tangent.xyz - norm * dot(in.world_tangent.xyz, norm));
float3 bitangent = cross(norm, t_ortho) * in.world_tangent.w;
if (use_principled) {
GpuMaterialData mat = materials[in.material_flags.x];
base_rgba = mat.base_color_factor * in.color0;
if (mat.tex_indices_a.x != 0xffffffffu) {
base_rgba *= tex_pool.sample(lin_sampler, in.uv0, mat.tex_indices_a.x);
}
uint flags = mat.flags_volume.x;
if ((flags & 1u) != 0u && base_rgba.a < mat.emissive_alpha_cutoff.w) {
discard_fragment();
}
unlit = (flags & 8u) != 0u;
metallic = clamp(mat.pbr_ior_specular.x, 0.0f, 1.0f);
roughness = clamp(mat.pbr_ior_specular.y, 0.04f, 1.0f);
if (mat.tex_indices_a.y != 0xffffffffu) {
float4 mr = tex_pool.sample(lin_sampler, in.uv0, mat.tex_indices_a.y);
roughness = clamp(roughness * mr.g, 0.04f, 1.0f);
metallic = clamp(metallic * mr.b, 0.0f, 1.0f);
}
if (mat.tex_indices_a.z != 0xffffffffu) {
float3 map_n = tex_pool.sample(lin_sampler, in.uv0, mat.tex_indices_a.z).xyz * 2.0f - float3(1.0f);
float3 scaled_n = normalize(float3(map_n.xy * mat.clearcoat_normal_occ_trans.y, map_n.z));
norm = normalize(t_ortho * scaled_n.x + bitangent * scaled_n.y + norm * scaled_n.z);
}
if (mat.tex_indices_a.w != 0xffffffffu) {
float occ_s = tex_pool.sample(lin_sampler, in.uv0, mat.tex_indices_a.w).r;
occlusion = mix(1.0f, occ_s, mat.clearcoat_normal_occ_trans.z);
}
emissive = mat.emissive_alpha_cutoff.xyz;
if (mat.tex_indices_b.x != 0xffffffffu) {
emissive *= tex_pool.sample(lin_sampler, in.uv0, mat.tex_indices_b.x).rgb;
}
ior = mat.pbr_ior_specular.z;
spec_factor = mat.pbr_ior_specular.w;
spec_color = mat.specular_color_clearcoat.xyz;
clearcoat = mat.specular_color_clearcoat.w;
clearcoat_rough = clamp(mat.clearcoat_normal_occ_trans.x, 0.04f, 1.0f);
transmission = mat.clearcoat_normal_occ_trans.w;
atten_color = mat.volume_attenuation.xyz;
thickness = mat.volume_attenuation.w;
atten_dist = max(as_type<float>(mat.flags_volume.y), 1.0e-4f);
sheen_color = mat.sheen_color_roughness.xyz;
sheen_rough = clamp(mat.sheen_color_roughness.w, 0.07f, 1.0f);
}
if (unlit) {
float3 hdr_u = (base_rgba.rgb + emissive) * cam.sky_zenith_exposure.w;
float3 mapped_u = (cam.tonemap_cluster_params.x > 0.5f) ? agx_tonemap(hdr_u) : aces_filmic(hdr_u);
return float4(pow(mapped_u, float3(1.0f / 2.2f)), base_rgba.a);
}
float3 view_vec = cam.camera_pos.xyz - in.world_pos;
float view_dist = length(view_vec);
float3 view_dir = view_vec / max(view_dist, 1.0e-4f);
float3 sun_dir = normalize(cam.sun_dir_intensity.xyz);
float n_dot_v = max(dot(norm, view_dir), 0.04f);
float n_dot_l = max(dot(norm, sun_dir), 0.0f);
float3 half_vec = normalize(sun_dir + view_dir);
float n_dot_h = max(dot(norm, half_vec), 0.0f);
float v_dot_h = clamp(dot(view_dir, half_vec), 0.0f, 1.0f);
float3 dielec_f0 = min(float3(dielectric_f0_from_ior(ior)) * spec_color, float3(1.0f)) * spec_factor;
float3 f0 = mix(dielec_f0, base_rgba.rgb, metallic);
float alpha = roughness * roughness;
float alpha2 = alpha * alpha;
float3 fresnel = f0 + (float3(1.0f) - f0) * pow(1.0f - v_dot_h, 5.0f);
float ggx_d = distribution_ggx(n_dot_h, alpha2);
float ggx_v = visibility_smith_ggx(n_dot_v, max(n_dot_l, 0.04f), alpha);
float2 brdf_lut = brdf_lut_tex.sample(lin_sampler, float2(n_dot_v, roughness)).rg;
float e_ss = clamp(brdf_lut.x + brdf_lut.y, 0.05f, 1.0f);
float3 multi_scatter = float3(1.0f) + f0 * ((1.0f / e_ss) - 1.0f);
float3 spec_direct = fresnel * (ggx_d * ggx_v) * multi_scatter;
float3 diff_direct = base_rgba.rgb * ((1.0f - metallic) * (1.0f - transmission) / 3.14159265f);
float shadow_vis = sample_csm_vogel_16(cam, csm_shadow, lin_sampler, in.world_pos, norm, sun_dir, view_dist);
float3 color_hdr = (diff_direct + spec_direct) * cam.sun_color_ambient.xyz * (n_dot_l * shadow_vis);
if (clearcoat > 0.001f) {
float cc_alpha = clearcoat_rough * clearcoat_rough;
float cc_f = 0.04f + 0.96f * pow(1.0f - v_dot_h, 5.0f);
float cc_d = distribution_ggx(n_dot_h, cc_alpha * cc_alpha);
float cc_v = visibility_smith_ggx(n_dot_v, max(n_dot_l, 0.04f), cc_alpha);
color_hdr = color_hdr * (1.0f - clearcoat * cc_f) + float3(cc_f * cc_d * cc_v * clearcoat) * cam.sun_color_ambient.xyz * (n_dot_l * shadow_vis);
}
if (dot(sheen_color, sheen_color) > 1.0e-5f) {
float sh_d = distribution_charlie(sheen_rough, n_dot_h);
float sh_v = visibility_ashikhmin(n_dot_v, max(n_dot_l, 0.04f));
color_hdr += sheen_color * (sh_d * sh_v) * cam.sun_color_ambient.xyz * (n_dot_l * shadow_vis);
}
uint light_count = min(uint(cam.tonemap_cluster_params.y), 16u);
for (uint l_idx = 0u; l_idx < light_count; ++l_idx) {
PackedLocalLight pl = local_lights[l_idx];
float3 to_l = pl.pos_inv_r2.xyz - in.world_pos;
float d2 = max(dot(to_l, to_l), 1.0e-4f);
float ratio2 = d2 * pl.pos_inv_r2.w;
if (ratio2 < 1.0f) {
float3 l_dir = to_l * rsqrt(d2);
float nl = max(dot(norm, l_dir), 0.0f);
if (nl > 0.0f) {
float spot = 1.0f;
if (pl.dir_outer_cos.w > -1.5f) {
float c_th = dot(-l_dir, pl.dir_outer_cos.xyz);
float raw_s = clamp((c_th - pl.dir_outer_cos.w) * pl.inv_cone_area.x, 0.0f, 1.0f);
spot = raw_s * raw_s;
}
float win = clamp(1.0f - ratio2 * ratio2, 0.0f, 1.0f);
float atten = (win * win * spot) / d2;
float3 h_l = normalize(l_dir + view_dir);
float nh = max(dot(norm, h_l), 0.0f);
float vh = clamp(dot(view_dir, h_l), 0.0f, 1.0f);
float3 f_l = f0 + (float3(1.0f) - f0) * pow(1.0f - vh, 5.0f);
float3 s_l = f_l * distribution_ggx(nh, alpha2) * visibility_smith_ggx(n_dot_v, nl, alpha);
color_hdr += (diff_direct + s_l) * pl.radiance_radius.xyz * (nl * atten);
}
}
}
float3 sh_irrad = eval_sh9_diffuse(cam, norm);
float3 refl_dir = reflect(-view_dir, norm);
float3 ibl_spec = ibl_oct.sample(lin_sampler, octahedral_encode_dir(refl_dir), level(roughness * 5.0f)).rgb;
float3 ibl_diff = base_rgba.rgb * ((1.0f - metallic) * (1.0f - transmission)) * sh_irrad;
float3 ibl_f = f0 * brdf_lut.x + float3(brdf_lut.y);
color_hdr += (ibl_diff + ibl_spec * ibl_f * multi_scatter) * occlusion;
if (transmission > 0.001f) {
float3 refr_dir = refract(-view_dir, norm, 1.0f / max(ior, 1.0f));
float3 refr_env = ibl_oct.sample(lin_sampler, octahedral_encode_dir(refr_dir), level(roughness * 4.0f)).rgb;
float3 sigma = -log(max(atten_color, float3(1.0e-4f))) / atten_dist;
float3 beer = exp(-sigma * max(thickness, 0.01f));
color_hdr += refr_env * base_rgba.rgb * beer * (transmission * (1.0f - metallic));
}
float3 hdr_final = (color_hdr + emissive) * cam.sky_zenith_exposure.w;
float3 mapped = (cam.tonemap_cluster_params.x > 0.5f) ? agx_tonemap(hdr_final) : aces_filmic(hdr_final);
float3 ldr = pow(mapped, float3(1.0f / 2.2f));
return float4(ldr, base_rgba.a);
}
"#;
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 vec4 a_tangent;
layout(location = 3) in vec2 a_uv0;
layout(location = 4) in vec4 a_color0;
layout(location = 5) in mat4 a_model;
layout(location = 9) in vec4 a_albedo_metallic;
layout(location = 10) in vec4 a_emissive_roughness;
uniform mat4 u_view_proj;
out vec3 v_world_pos;
out vec3 v_world_normal;
out vec4 v_world_tangent;
out vec2 v_uv0;
out vec4 v_color0;
out vec4 v_albedo_metallic;
out vec4 v_emissive_roughness;
mat3 cofactor_normal_matrix(mat4 m) {
vec3 c0 = m[0].xyz;
vec3 c1 = m[1].xyz;
vec3 c2 = m[2].xyz;
vec3 n0 = cross(c1, c2);
vec3 n1 = cross(c2, c0);
vec3 n2 = cross(c0, c1);
float det = dot(c0, n0);
float inv_det = abs(det) > 1.0e-8 ? (1.0 / det) : 1.0;
return mat3(n0 * inv_det, n1 * inv_det, n2 * inv_det);
}
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(cofactor_normal_matrix(a_model) * a_normal);
v_world_tangent = vec4(normalize((a_model * vec4(a_tangent.xyz, 0.0)).xyz), a_tangent.w);
v_uv0 = a_uv0;
v_color0 = a_color0;
v_albedo_metallic = a_albedo_metallic;
v_emissive_roughness = a_emissive_roughness;
}
"#;
pub const GLSL_ES3_ENGINE3D_FRAGMENT_SHADER: &str = r#"#version 300 es
precision highp float;
precision highp sampler2DArray;
in vec3 v_world_pos;
in vec3 v_world_normal;
in vec4 v_world_tangent;
in vec2 v_uv0;
in vec4 v_color0;
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;
uniform vec4 u_pbr_ior_spec_cc;
uniform vec4 u_cc_rough_nscale_occ_trans;
uniform vec4 u_sheen_color_rough;
uniform vec4 u_volume_atten_thick;
uniform vec4 u_alpha_flags;
uniform vec4 u_csm_splits;
uniform mat4 u_csm_matrices[4];
uniform vec3 u_ibl_sh[9];
uniform vec4 u_local_lights_pos[16];
uniform vec4 u_local_lights_rad[16];
uniform vec4 u_local_lights_dir[16];
uniform int u_local_light_count;
uniform int u_use_agx;
uniform sampler2D u_base_color_tex;
uniform sampler2D u_normal_tex;
uniform sampler2D u_mr_tex;
uniform sampler2D u_ibl_oct_tex;
uniform sampler2D u_brdf_lut_tex;
uniform sampler2DArray u_csm_shadow_tex;
out vec4 out_color;
float dielectric_f0_from_ior(float ior) {
float r = (ior - 1.0) / max(ior + 1.0, 1.0e-4);
return r * r;
}
float distribution_ggx(float n_dot_h, float alpha2) {
float denom = n_dot_h * n_dot_h * (alpha2 - 1.0) + 1.0;
return alpha2 / max(3.14159265 * denom * denom, 1.0e-5);
}
float visibility_smith_ggx(float n_dot_v, float n_dot_l, float alpha) {
float a2 = alpha * alpha;
float ggx_v = n_dot_l * sqrt(n_dot_v * n_dot_v * (1.0 - a2) + a2);
float ggx_l = n_dot_v * sqrt(n_dot_l * n_dot_l * (1.0 - a2) + a2);
return 0.5 / max(ggx_v + ggx_l, 1.0e-5);
}
float distribution_charlie(float roughness, float n_dot_h) {
float inv_r = 1.0 / max(roughness, 0.07);
float sin2_h = max(1.0 - n_dot_h * n_dot_h, 0.0078125);
return (2.0 + inv_r) * pow(sin2_h, inv_r * 0.5) / 6.2831853;
}
float visibility_ashikhmin(float n_dot_v, float n_dot_l) {
return 1.0 / max(4.0 * (n_dot_l + n_dot_v - n_dot_l * n_dot_v), 1.0e-4);
}
vec2 octahedral_encode_dir(vec3 dir) {
vec3 d = normalize(dir);
float l1 = max(abs(d.x) + abs(d.y) + abs(d.z), 1.0e-6);
float ox = d.x / l1;
float oz = d.z / l1;
if (d.y < 0.0) {
float px = ox;
float pz = oz;
ox = (1.0 - abs(pz)) * (px >= 0.0 ? 1.0 : -1.0);
oz = (1.0 - abs(px)) * (pz >= 0.0 ? 1.0 : -1.0);
}
return vec2(ox * 0.5 + 0.5, oz * 0.5 + 0.5);
}
vec3 eval_sh9_diffuse(vec3 norm) {
vec3 irrad = u_ibl_sh[0] * 0.2820948;
irrad += u_ibl_sh[1] * (0.4886025 * norm.y);
irrad += u_ibl_sh[2] * (0.4886025 * norm.z);
irrad += u_ibl_sh[3] * (0.4886025 * norm.x);
irrad += u_ibl_sh[4] * (1.0925484 * norm.x * norm.y);
irrad += u_ibl_sh[5] * (1.0925484 * norm.y * norm.z);
irrad += u_ibl_sh[6] * (0.3153916 * (3.0 * norm.z * norm.z - 1.0));
irrad += u_ibl_sh[7] * (1.0925484 * norm.x * norm.z);
irrad += u_ibl_sh[8] * (0.5462742 * (norm.x * norm.x - norm.y * norm.y));
return max(irrad, vec3(0.0));
}
float sample_csm_vogel_16(vec3 world_pos, vec3 norm, vec3 sun_dir, float view_depth) {
int cascade = 3;
if (view_depth < u_csm_splits.x) { cascade = 0; }
else if (view_depth < u_csm_splits.y) { cascade = 1; }
else if (view_depth < u_csm_splits.z) { cascade = 2; }
vec3 biased_pos = world_pos + norm * (0.04 + 0.02 * float(cascade));
vec4 light_clip = u_csm_matrices[cascade] * vec4(biased_pos, 1.0);
vec2 uv = light_clip.xy * 0.5 + vec2(0.5);
float depth = light_clip.z;
if (uv.x < 0.01 || uv.x > 0.99 || uv.y < 0.01 || uv.y > 0.99 || depth <= 0.0 || depth >= 1.0) {
return 1.0;
}
float n_dot_l = clamp(dot(norm, sun_dir), 0.05, 1.0);
float bias = clamp(0.0022 * (1.0 - n_dot_l) + 0.0012, 0.001, 0.008);
float angle = fract(sin(dot(world_pos, vec3(12.9898, 37.719, 78.233))) * 43758.547) * 6.2831853;
float c_rot = cos(angle);
float s_rot = sin(angle);
float vis = 0.0;
for (int tap = 0; tap < 16; ++tap) {
float r_tap = sqrt((float(tap) + 0.5) / 16.0) * 0.0025;
float theta = float(tap) * 2.39996323;
float ox = cos(theta) * r_tap;
float oy = sin(theta) * r_tap;
vec2 rot_uv = uv + vec2(ox * c_rot - oy * s_rot, ox * s_rot + oy * c_rot);
float sampled_d = texture(u_csm_shadow_tex, vec3(rot_uv, float(cascade))).r;
vis += (depth - bias <= sampled_d) ? 1.0 : 0.0;
}
return 0.22 + 0.78 * (vis / 16.0);
}
vec3 agx_contrast_poly(vec3 x) {
vec3 x2 = x * x;
vec3 x4 = x2 * x2;
return clamp(15.5 * x4 * x2 - 40.14 * x4 * x + 31.96 * x4 - 6.868 * x2 * x + 0.4298 * x2 + 0.1191 * x - 0.00232, 0.0, 1.0);
}
vec3 agx_tonemap(vec3 hdr) {
mat3 inset = mat3(
vec3(0.84247906, 0.04232824, 0.04237565),
vec3(0.07843360, 0.87846864, 0.07843360),
vec3(0.07922375, 0.07916613, 0.87914297)
);
mat3 outset = mat3(
vec3(1.19687900, -0.05289685, -0.05297163),
vec3(-0.09802088, 1.15190313, -0.09804345),
vec3(-0.09902975, -0.09896118, 1.15107367)
);
vec3 val = max(inset * max(hdr, vec3(1.0e-6)), vec3(1.0e-6));
vec3 log_val = clamp((log2(val) - vec3(-12.47393)) / vec3(16.5), 0.0, 1.0);
return clamp(outset * agx_contrast_poly(log_val), 0.0, 1.0);
}
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() {
vec4 base_rgba = vec4(v_albedo_metallic.rgb, 1.0) * v_color0;
if (u_alpha_flags.z > 0.5) {
base_rgba *= texture(u_base_color_tex, v_uv0);
}
if (u_alpha_flags.y > 0.5 && base_rgba.a < u_alpha_flags.x) {
discard;
}
if (u_alpha_flags.w > 0.5) {
vec3 hdr_u = (base_rgba.rgb + v_emissive_roughness.rgb) * u_exposure;
vec3 mapped_u = u_use_agx != 0 ? agx_tonemap(hdr_u) : aces_filmic(hdr_u);
out_color = vec4(pow(mapped_u, vec3(1.0 / 2.2)), base_rgba.a);
return;
}
vec3 norm = normalize(v_world_normal);
if (!gl_FrontFacing) {
norm = -norm;
}
vec3 t_ortho = normalize(v_world_tangent.xyz - norm * dot(v_world_tangent.xyz, norm));
vec3 bitangent = cross(norm, t_ortho) * v_world_tangent.w;
if (u_cc_rough_nscale_occ_trans.y > 0.001) {
vec3 map_n = texture(u_normal_tex, v_uv0).xyz * 2.0 - vec3(1.0);
vec3 scaled_n = normalize(vec3(map_n.xy * u_cc_rough_nscale_occ_trans.y, map_n.z));
norm = normalize(t_ortho * scaled_n.x + bitangent * scaled_n.y + norm * scaled_n.z);
}
vec4 mr_tex = texture(u_mr_tex, v_uv0);
float metallic = clamp(v_albedo_metallic.a * mr_tex.b, 0.0, 1.0);
float roughness = clamp(v_emissive_roughness.a * mr_tex.g, 0.04, 1.0);
vec3 view_vec = u_camera_pos - v_world_pos;
float view_dist = length(view_vec);
vec3 view_dir = view_vec / max(view_dist, 1.0e-4);
vec3 sun_dir = normalize(u_sun_dir);
vec3 half_vec = normalize(sun_dir + view_dir);
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);
float ior = max(u_pbr_ior_spec_cc.x, 1.0);
float spec_factor = u_pbr_ior_spec_cc.y;
float clearcoat = u_pbr_ior_spec_cc.z;
float clearcoat_rough = clamp(u_cc_rough_nscale_occ_trans.x, 0.04, 1.0);
float transmission = u_cc_rough_nscale_occ_trans.w;
vec3 f0 = mix(vec3(dielectric_f0_from_ior(ior) * spec_factor), base_rgba.rgb, metallic);
float alpha = roughness * roughness;
float alpha2 = alpha * alpha;
vec3 fresnel = f0 + (vec3(1.0) - f0) * pow(1.0 - v_dot_h, 5.0);
vec2 brdf_lut = texture(u_brdf_lut_tex, vec2(n_dot_v, roughness)).rg;
float e_ss = clamp(brdf_lut.x + brdf_lut.y, 0.05, 1.0);
vec3 multi_scatter = vec3(1.0) + f0 * ((1.0 / e_ss) - 1.0);
vec3 spec = fresnel * distribution_ggx(n_dot_h, alpha2) * visibility_smith_ggx(n_dot_v, max(n_dot_l, 0.04), alpha) * multi_scatter;
vec3 diff = base_rgba.rgb * ((1.0 - metallic) * (1.0 - transmission) / 3.14159265);
float shadow_vis = sample_csm_vogel_16(v_world_pos, norm, sun_dir, view_dist);
vec3 color_hdr = (diff + spec) * u_sun_color * (n_dot_l * shadow_vis);
if (clearcoat > 0.001) {
float cc_alpha = clearcoat_rough * clearcoat_rough;
float cc_f = 0.04 + 0.96 * pow(1.0 - v_dot_h, 5.0);
float cc_spec = cc_f * distribution_ggx(n_dot_h, cc_alpha * cc_alpha) * visibility_smith_ggx(n_dot_v, max(n_dot_l, 0.04), cc_alpha);
color_hdr = color_hdr * (1.0 - clearcoat * cc_f) + vec3(cc_spec * clearcoat) * u_sun_color * (n_dot_l * shadow_vis);
}
if (dot(u_sheen_color_rough.rgb, u_sheen_color_rough.rgb) > 1.0e-5) {
float sh_d = distribution_charlie(u_sheen_color_rough.w, n_dot_h);
float sh_v = visibility_ashikhmin(n_dot_v, max(n_dot_l, 0.04));
color_hdr += u_sheen_color_rough.rgb * (sh_d * sh_v) * u_sun_color * (n_dot_l * shadow_vis);
}
for (int l_idx = 0; l_idx < 16; ++l_idx) {
if (l_idx >= u_local_light_count) { break; }
vec3 to_l = u_local_lights_pos[l_idx].xyz - v_world_pos;
float d2 = max(dot(to_l, to_l), 1.0e-4);
float ratio2 = d2 * u_local_lights_pos[l_idx].w;
if (ratio2 < 1.0) {
vec3 l_dir = to_l * inversesqrt(d2);
float nl = max(dot(norm, l_dir), 0.0);
if (nl > 0.0) {
float win = clamp(1.0 - ratio2 * ratio2, 0.0, 1.0);
float atten = (win * win) / d2;
vec3 h_l = normalize(l_dir + view_dir);
float nh = max(dot(norm, h_l), 0.0);
float vh = clamp(dot(view_dir, h_l), 0.0, 1.0);
vec3 f_l = f0 + (vec3(1.0) - f0) * pow(1.0 - vh, 5.0);
vec3 s_l = f_l * distribution_ggx(nh, alpha2) * visibility_smith_ggx(n_dot_v, nl, alpha);
color_hdr += (diff + s_l) * u_local_lights_rad[l_idx].rgb * (nl * atten);
}
}
}
vec3 sh_irrad = eval_sh9_diffuse(norm);
vec3 refl_dir = reflect(-view_dir, norm);
vec3 ibl_spec = textureLod(u_ibl_oct_tex, octahedral_encode_dir(refl_dir), roughness * 5.0).rgb;
vec3 ibl_f = f0 * brdf_lut.x + vec3(brdf_lut.y);
color_hdr += base_rgba.rgb * ((1.0 - metallic) * (1.0 - transmission)) * sh_irrad + ibl_spec * ibl_f * multi_scatter;
if (transmission > 0.001) {
vec3 refr_dir = refract(-view_dir, norm, 1.0 / ior);
vec3 refr_env = textureLod(u_ibl_oct_tex, octahedral_encode_dir(refr_dir), roughness * 4.0).rgb;
vec3 sigma = -log(max(u_volume_atten_thick.rgb, vec3(1.0e-4))) / max(u_pbr_ior_spec_cc.w, 1.0e-4);
vec3 beer = exp(-sigma * max(u_volume_atten_thick.w, 0.01));
color_hdr += refr_env * base_rgba.rgb * beer * (transmission * (1.0 - metallic));
}
vec3 hdr = (color_hdr + v_emissive_roughness.rgb) * u_exposure;
vec3 mapped = u_use_agx != 0 ? agx_tonemap(hdr) : aces_filmic(hdr);
vec3 ldr = pow(mapped, vec3(1.0 / 2.2));
out_color = vec4(ldr, base_rgba.a);
}
"#;
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;
float4 g_csm_splits;
float4x4 g_csm_matrices[4];
float4 g_ibl_sh[9];
float4 g_tonemap_cluster_params;
};
struct InstanceData {
float4x4 model;
float4 albedo_metallic;
float4 emissive_roughness;
uint4 material_skin_morph_flags;
};
struct GpuMaterialData {
float4 base_color_factor;
float4 emissive_alpha_cutoff;
float4 pbr_ior_specular;
float4 specular_color_clearcoat;
float4 clearcoat_normal_occ_trans;
float4 volume_attenuation;
float4 sheen_color_roughness;
uint4 tex_indices_a;
uint4 tex_indices_b;
uint4 flags_volume;
};
struct PackedLocalLight {
float4 pos_inv_r2;
float4 radiance_radius;
float4 dir_outer_cos;
float4 inv_cone_area;
};
StructuredBuffer<InstanceData> g_instances : register(t0);
StructuredBuffer<GpuMaterialData> g_materials : register(t1);
StructuredBuffer<PackedLocalLight> g_lights : register(t2);
Texture2DArray<float4> g_textures : register(t3);
Texture2DArray<float> g_csm_shadow : register(t4);
Texture2D<float4> g_ibl_oct : register(t5);
Texture2D<float2> g_brdf_lut : register(t6);
SamplerState g_linear_sampler : register(s0);
struct VertexIn {
float3 position : POSITION;
float3 normal : NORMAL;
float4 tangent : TANGENT;
float2 uv0 : TEXCOORD0;
float4 color0 : COLOR0;
uint instance_id : SV_InstanceID;
};
struct VertexOut {
float4 clip_pos : SV_POSITION;
float3 world_pos : TEXCOORD0;
float3 world_normal : TEXCOORD1;
float4 world_tangent : TEXCOORD2;
float2 uv0 : TEXCOORD3;
float4 color0 : TEXCOORD4;
float4 albedo_metallic : TEXCOORD5;
float4 emissive_roughness : TEXCOORD6;
nointerpolation uint4 material_flags : TEXCOORD7;
};
float3x3 cofactor_normal_matrix(float4x4 m) {
float3 c0 = float3(m._11, m._21, m._31);
float3 c1 = float3(m._12, m._22, m._32);
float3 c2 = float3(m._13, m._23, m._33);
float3 n0 = cross(c1, c2);
float3 n1 = cross(c2, c0);
float3 n2 = cross(c0, c1);
float det = dot(c0, n0);
float inv_det = (abs(det) > 1.0e-8) ? (1.0 / det) : 1.0;
return float3x3(n0 * inv_det, n1 * inv_det, n2 * inv_det);
}
VertexOut vs_main(VertexIn input) {
InstanceData inst = g_instances[input.instance_id];
float4 world = mul(inst.model, float4(input.position, 1.0));
float3x3 normal_mat = cofactor_normal_matrix(inst.model);
VertexOut output;
output.clip_pos = mul(g_view_proj, world);
output.world_pos = world.xyz;
output.world_normal = normalize(mul(input.normal, normal_mat));
output.world_tangent = float4(normalize(mul((float3x3)inst.model, input.tangent.xyz)), input.tangent.w);
output.uv0 = input.uv0;
output.color0 = input.color0;
output.albedo_metallic = inst.albedo_metallic;
output.emissive_roughness = inst.emissive_roughness;
output.material_flags = inst.material_skin_morph_flags;
return output;
}
float dielectric_f0_from_ior(float ior) {
float r = (ior - 1.0) / max(ior + 1.0, 1.0e-4);
return r * r;
}
float distribution_ggx(float n_dot_h, float alpha2) {
float denom = n_dot_h * n_dot_h * (alpha2 - 1.0) + 1.0;
return alpha2 / max(3.14159265 * denom * denom, 1.0e-5);
}
float visibility_smith_ggx(float n_dot_v, float n_dot_l, float alpha) {
float a2 = alpha * alpha;
float ggx_v = n_dot_l * sqrt(n_dot_v * n_dot_v * (1.0 - a2) + a2);
float ggx_l = n_dot_v * sqrt(n_dot_l * n_dot_l * (1.0 - a2) + a2);
return 0.5 / max(ggx_v + ggx_l, 1.0e-5);
}
float distribution_charlie(float roughness, float n_dot_h) {
float inv_r = 1.0 / max(roughness, 0.07);
float sin2_h = max(1.0 - n_dot_h * n_dot_h, 0.0078125);
return (2.0 + inv_r) * pow(sin2_h, inv_r * 0.5) / 6.2831853;
}
float visibility_ashikhmin(float n_dot_v, float n_dot_l) {
return 1.0 / max(4.0 * (n_dot_l + n_dot_v - n_dot_l * n_dot_v), 1.0e-4);
}
float2 octahedral_encode_dir(float3 dir) {
float3 d = normalize(dir);
float l1 = max(abs(d.x) + abs(d.y) + abs(d.z), 1.0e-6);
float ox = d.x / l1;
float oz = d.z / l1;
if (d.y < 0.0) {
float px = ox;
float pz = oz;
ox = (1.0 - abs(pz)) * (px >= 0.0 ? 1.0 : -1.0);
oz = (1.0 - abs(px)) * (pz >= 0.0 ? 1.0 : -1.0);
}
return float2(ox * 0.5 + 0.5, oz * 0.5 + 0.5);
}
float3 eval_sh9_diffuse(float3 norm) {
float3 irrad = g_ibl_sh[0].xyz * 0.2820948;
irrad += g_ibl_sh[1].xyz * (0.4886025 * norm.y);
irrad += g_ibl_sh[2].xyz * (0.4886025 * norm.z);
irrad += g_ibl_sh[3].xyz * (0.4886025 * norm.x);
irrad += g_ibl_sh[4].xyz * (1.0925484 * norm.x * norm.y);
irrad += g_ibl_sh[5].xyz * (1.0925484 * norm.y * norm.z);
irrad += g_ibl_sh[6].xyz * (0.3153916 * (3.0 * norm.z * norm.z - 1.0));
irrad += g_ibl_sh[7].xyz * (1.0925484 * norm.x * norm.z);
irrad += g_ibl_sh[8].xyz * (0.5462742 * (norm.x * norm.x - norm.y * norm.y));
return max(irrad, float3(0.0, 0.0, 0.0));
}
float sample_csm_vogel_16(float3 world_pos, float3 norm, float3 sun_dir, float view_depth) {
int cascade = 3;
if (view_depth < g_csm_splits.x) { cascade = 0; }
else if (view_depth < g_csm_splits.y) { cascade = 1; }
else if (view_depth < g_csm_splits.z) { cascade = 2; }
float3 biased_pos = world_pos + norm * (0.04 + 0.02 * float(cascade));
float4 light_clip = mul(g_csm_matrices[cascade], float4(biased_pos, 1.0));
float2 uv = light_clip.xy * 0.5 + float2(0.5, 0.5);
float depth = light_clip.z;
if (uv.x < 0.01 || uv.x > 0.99 || uv.y < 0.01 || uv.y > 0.99 || depth <= 0.0 || depth >= 1.0) {
return 1.0;
}
float n_dot_l = clamp(dot(norm, sun_dir), 0.05, 1.0);
float bias = clamp(0.0022 * (1.0 - n_dot_l) + 0.0012, 0.001, 0.008);
float angle = frac(sin(dot(world_pos, float3(12.9898, 37.719, 78.233))) * 43758.547) * 6.2831853;
float c_rot = cos(angle);
float s_rot = sin(angle);
float vis = 0.0;
[unroll]
for (int tap = 0; tap < 16; ++tap) {
float r_tap = sqrt((float(tap) + 0.5) / 16.0) * 0.0025;
float theta = float(tap) * 2.39996323;
float ox = cos(theta) * r_tap;
float oy = sin(theta) * r_tap;
float2 rot_uv = uv + float2(ox * c_rot - oy * s_rot, ox * s_rot + oy * c_rot);
float sampled_d = g_csm_shadow.SampleLevel(g_linear_sampler, float3(rot_uv, float(cascade)), 0.0);
vis += (depth - bias <= sampled_d) ? 1.0 : 0.0;
}
return 0.22 + 0.78 * (vis / 16.0);
}
float3 agx_contrast_poly(float3 x) {
float3 x2 = x * x;
float3 x4 = x2 * x2;
return saturate(15.5 * x4 * x2 - 40.14 * x4 * x + 31.96 * x4 - 6.868 * x2 * x + 0.4298 * x2 + 0.1191 * x - 0.00232);
}
float3 agx_tonemap(float3 hdr) {
float3x3 inset = float3x3(
0.84247906, 0.07843360, 0.07922375,
0.04232824, 0.87846864, 0.07916613,
0.04237565, 0.07843360, 0.87914297
);
float3x3 outset = float3x3(
1.19687900, -0.09802088, -0.09902975,
-0.05289685, 1.15190313, -0.09896118,
-0.05297163, -0.09804345, 1.15107367
);
float3 val = max(mul(inset, max(hdr, float3(1.0e-6, 1.0e-6, 1.0e-6))), float3(1.0e-6, 1.0e-6, 1.0e-6));
float3 log_val = saturate((log2(val) - float3(-12.47393, -12.47393, -12.47393)) / 16.5);
return saturate(mul(outset, agx_contrast_poly(log_val)));
}
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, bool is_front : SV_IsFrontFace) : SV_TARGET {
bool use_principled = (input.material_flags.w & 2u) != 0u;
float4 base_rgba = float4(input.albedo_metallic.xyz, 1.0) * input.color0;
float metallic = saturate(input.albedo_metallic.w);
float roughness = clamp(input.emissive_roughness.w, 0.04, 1.0);
float3 emissive = input.emissive_roughness.xyz;
float ior = 1.5;
float spec_factor = 1.0;
float3 spec_color = float3(1.0, 1.0, 1.0);
float clearcoat = 0.0;
float clearcoat_rough = 0.04;
float3 sheen_color = float3(0.0, 0.0, 0.0);
float sheen_rough = 0.5;
float transmission = 0.0;
float thickness = 0.0;
float3 atten_color = float3(1.0, 1.0, 1.0);
float atten_dist = 1.0e6;
float occlusion = 1.0;
bool unlit = false;
float3 norm = normalize(input.world_normal);
if (!is_front) { norm = -norm; }
float3 t_ortho = normalize(input.world_tangent.xyz - norm * dot(input.world_tangent.xyz, norm));
float3 bitangent = cross(norm, t_ortho) * input.world_tangent.w;
if (use_principled) {
GpuMaterialData mat = g_materials[input.material_flags.x];
base_rgba = mat.base_color_factor * input.color0;
if (mat.tex_indices_a.x != 0xffffffffu) {
base_rgba *= g_textures.Sample(g_linear_sampler, float3(input.uv0, float(mat.tex_indices_a.x)));
}
uint flags = mat.flags_volume.x;
if ((flags & 1u) != 0u && base_rgba.a < mat.emissive_alpha_cutoff.w) {
discard;
}
unlit = (flags & 8u) != 0u;
metallic = saturate(mat.pbr_ior_specular.x);
roughness = clamp(mat.pbr_ior_specular.y, 0.04, 1.0);
if (mat.tex_indices_a.y != 0xffffffffu) {
float4 mr = g_textures.Sample(g_linear_sampler, float3(input.uv0, float(mat.tex_indices_a.y)));
roughness = clamp(roughness * mr.g, 0.04, 1.0);
metallic = saturate(metallic * mr.b);
}
if (mat.tex_indices_a.z != 0xffffffffu) {
float3 map_n = g_textures.Sample(g_linear_sampler, float3(input.uv0, float(mat.tex_indices_a.z))).xyz * 2.0 - 1.0;
float3 scaled_n = normalize(float3(map_n.xy * mat.clearcoat_normal_occ_trans.y, map_n.z));
norm = normalize(t_ortho * scaled_n.x + bitangent * scaled_n.y + norm * scaled_n.z);
}
if (mat.tex_indices_a.w != 0xffffffffu) {
float occ_s = g_textures.Sample(g_linear_sampler, float3(input.uv0, float(mat.tex_indices_a.w))).r;
occlusion = lerp(1.0, occ_s, mat.clearcoat_normal_occ_trans.z);
}
emissive = mat.emissive_alpha_cutoff.xyz;
ior = mat.pbr_ior_specular.z;
spec_factor = mat.pbr_ior_specular.w;
spec_color = mat.specular_color_clearcoat.xyz;
clearcoat = mat.specular_color_clearcoat.w;
clearcoat_rough = clamp(mat.clearcoat_normal_occ_trans.x, 0.04, 1.0);
transmission = mat.clearcoat_normal_occ_trans.w;
atten_color = mat.volume_attenuation.xyz;
thickness = mat.volume_attenuation.w;
atten_dist = max(asfloat(mat.flags_volume.y), 1.0e-4);
sheen_color = mat.sheen_color_roughness.xyz;
sheen_rough = clamp(mat.sheen_color_roughness.w, 0.07, 1.0);
}
if (unlit) {
float3 hdr_u = (base_rgba.rgb + emissive) * g_sky_zenith_exposure.w;
float3 mapped_u = (g_tonemap_cluster_params.x > 0.5) ? agx_tonemap(hdr_u) : aces_filmic(hdr_u);
return float4(pow(mapped_u, 1.0 / 2.2), base_rgba.a);
}
float3 view_vec = g_camera_pos.xyz - input.world_pos;
float view_dist = length(view_vec);
float3 view_dir = view_vec / max(view_dist, 1.0e-4);
float3 sun_dir = normalize(g_sun_dir_intensity.xyz);
float3 half_vec = normalize(sun_dir + view_dir);
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 dielec_f0 = min(dielectric_f0_from_ior(ior) * spec_color, float3(1.0, 1.0, 1.0)) * spec_factor;
float3 f0 = lerp(dielec_f0, base_rgba.rgb, metallic);
float alpha = roughness * roughness;
float alpha2 = alpha * alpha;
float3 fresnel = f0 + (1.0 - f0) * pow(1.0 - v_dot_h, 5.0);
float2 brdf_lut = g_brdf_lut.Sample(g_linear_sampler, float2(n_dot_v, roughness));
float e_ss = clamp(brdf_lut.x + brdf_lut.y, 0.05, 1.0);
float3 multi_scatter = 1.0 + f0 * ((1.0 / e_ss) - 1.0);
float3 spec = fresnel * distribution_ggx(n_dot_h, alpha2) * visibility_smith_ggx(n_dot_v, max(n_dot_l, 0.04), alpha) * multi_scatter;
float3 diff = base_rgba.rgb * ((1.0 - metallic) * (1.0 - transmission) / 3.14159265);
float shadow_vis = sample_csm_vogel_16(input.world_pos, norm, sun_dir, view_dist);
float3 color_hdr = (diff + spec) * g_sun_color_ambient.xyz * (n_dot_l * shadow_vis);
if (clearcoat > 0.001) {
float cc_alpha = clearcoat_rough * clearcoat_rough;
float cc_f = 0.04 + 0.96 * pow(1.0 - v_dot_h, 5.0);
float cc_spec = cc_f * distribution_ggx(n_dot_h, cc_alpha * cc_alpha) * visibility_smith_ggx(n_dot_v, max(n_dot_l, 0.04), cc_alpha);
color_hdr = color_hdr * (1.0 - clearcoat * cc_f) + cc_spec * clearcoat * g_sun_color_ambient.xyz * (n_dot_l * shadow_vis);
}
if (dot(sheen_color, sheen_color) > 1.0e-5) {
float sh_d = distribution_charlie(sheen_rough, n_dot_h);
float sh_v = visibility_ashikhmin(n_dot_v, max(n_dot_l, 0.04));
color_hdr += sheen_color * (sh_d * sh_v) * g_sun_color_ambient.xyz * (n_dot_l * shadow_vis);
}
uint light_count = min((uint)g_tonemap_cluster_params.y, 16u);
for (uint l_idx = 0u; l_idx < light_count; ++l_idx) {
PackedLocalLight pl = g_lights[l_idx];
float3 to_l = pl.pos_inv_r2.xyz - input.world_pos;
float d2 = max(dot(to_l, to_l), 1.0e-4);
float ratio2 = d2 * pl.pos_inv_r2.w;
if (ratio2 < 1.0) {
float3 l_dir = to_l * rsqrt(d2);
float nl = max(dot(norm, l_dir), 0.0);
if (nl > 0.0) {
float win = saturate(1.0 - ratio2 * ratio2);
float atten = (win * win) / d2;
float3 h_l = normalize(l_dir + view_dir);
float nh = max(dot(norm, h_l), 0.0);
float vh = saturate(dot(view_dir, h_l));
float3 f_l = f0 + (1.0 - f0) * pow(1.0 - vh, 5.0);
float3 s_l = f_l * distribution_ggx(nh, alpha2) * visibility_smith_ggx(n_dot_v, nl, alpha);
color_hdr += (diff + s_l) * pl.radiance_radius.xyz * (nl * atten);
}
}
}
float3 sh_irrad = eval_sh9_diffuse(norm);
float3 refl_dir = reflect(-view_dir, norm);
float3 ibl_spec = g_ibl_oct.SampleLevel(g_linear_sampler, octahedral_encode_dir(refl_dir), roughness * 5.0).rgb;
float3 ibl_f = f0 * brdf_lut.x + brdf_lut.y;
color_hdr += (base_rgba.rgb * ((1.0 - metallic) * (1.0 - transmission)) * sh_irrad + ibl_spec * ibl_f * multi_scatter) * occlusion;
if (transmission > 0.001) {
float3 refr_dir = refract(-view_dir, norm, 1.0 / max(ior, 1.0));
float3 refr_env = g_ibl_oct.SampleLevel(g_linear_sampler, octahedral_encode_dir(refr_dir), roughness * 4.0).rgb;
float3 sigma = -log(max(atten_color, float3(1.0e-4, 1.0e-4, 1.0e-4))) / atten_dist;
float3 beer = exp(-sigma * max(thickness, 0.01));
color_hdr += refr_env * base_rgba.rgb * beer * (transmission * (1.0 - metallic));
}
float3 hdr = (color_hdr + emissive) * g_sky_zenith_exposure.w;
float3 mapped = (g_tonemap_cluster_params.x > 0.5) ? agx_tonemap(hdr) : aces_filmic(hdr);
float3 ldr = pow(mapped, 1.0 / 2.2);
return float4(ldr, base_rgba.a);
}
"#;
pub const WGSL_ENGINE3D_COMPUTE_SHADER: &str = r#"struct Vertex64 {
pos_nx: vec4<f32>,
ny_nz_tx_ty: vec4<f32>,
tz_tw_uv0: vec4<f32>,
uv1_col_flags: vec4<f32>,
};
struct SkinInfluence {
joints_0: vec4<u32>,
weights_0: vec4<f32>,
joints_1: vec4<u32>,
weights_1: vec4<f32>,
};
struct MorphDelta {
dpos_pad: vec4<f32>,
dnorm_pad: vec4<f32>,
};
struct MeshletCullBound {
center_radius: vec4<f32>,
cone_axis_cutoff: vec4<f32>,
draw_range: vec4<u32>,
};
struct DrawIndexedIndirectArgs {
index_count: u32,
instance_count: u32,
first_index: u32,
base_vertex: i32,
first_instance: u32,
};
struct ComputeParams {
vertex_count: u32,
morph_target_count: u32,
skin_mode_dqs: u32,
meshlet_count: u32,
camera_pos: vec4<f32>,
frustum_planes: array<vec4<f32>, 6>,
};
@group(0) @binding(0) var<uniform> u_params: ComputeParams;
@group(0) @binding(1) var<storage, read> s_bind_verts: array<Vertex64>;
@group(0) @binding(2) var<storage, read> s_influences: array<SkinInfluence>;
@group(0) @binding(3) var<storage, read> s_morph_deltas: array<MorphDelta>;
@group(0) @binding(4) var<storage, read> s_morph_weights: array<f32>;
@group(0) @binding(5) var<storage, read> s_bone_mats: array<mat4x4<f32>>;
@group(0) @binding(6) var<storage, read_write> s_out_verts: array<Vertex64>;
@group(0) @binding(7) var<storage, read> s_meshlets: array<MeshletCullBound>;
@group(0) @binding(8) var<storage, read_write> s_indirect: array<DrawIndexedIndirectArgs>;
@group(0) @binding(9) var<storage, read_write> s_draw_counter: atomic<u32>;
@compute @workgroup_size(64)
fn cs_morph_skin(@builtin(global_invocation_id) gid: vec3<u32>) {
let vid = gid.x;
if (vid >= u_params.vertex_count) {
return;
}
var vert = s_bind_verts[vid];
var pos = vert.pos_nx.xyz;
var norm = vec3<f32>(vert.pos_nx.w, vert.ny_nz_tx_ty.x, vert.ny_nz_tx_ty.y);
for (var m_idx: u32 = 0u; m_idx < u_params.morph_target_count; m_idx = m_idx + 1u) {
let w = s_morph_weights[m_idx];
if (abs(w) > 1.0e-5) {
let delta = s_morph_deltas[m_idx * u_params.vertex_count + vid];
pos += delta.dpos_pad.xyz * w;
norm += delta.dnorm_pad.xyz * w;
}
}
norm = normalize(norm);
let inf = s_influences[vid];
var out_pos = vec3<f32>(0.0);
var out_norm = vec3<f32>(0.0);
var w_sum: f32 = 0.0;
let j0 = inf.joints_0;
let w0 = inf.weights_0;
for (var lane: u32 = 0u; lane < 4u; lane = lane + 1u) {
let bw = w0[lane];
if (bw > 1.0e-6) {
let bm = s_bone_mats[j0[lane]];
out_pos += (bm * vec4<f32>(pos, 1.0)).xyz * bw;
out_norm += (bm * vec4<f32>(norm, 0.0)).xyz * bw;
w_sum += bw;
}
}
let j1 = inf.joints_1;
let w1 = inf.weights_1;
for (var lane: u32 = 0u; lane < 4u; lane = lane + 1u) {
let bw = w1[lane];
if (bw > 1.0e-6) {
let bm = s_bone_mats[j1[lane]];
out_pos += (bm * vec4<f32>(pos, 1.0)).xyz * bw;
out_norm += (bm * vec4<f32>(norm, 0.0)).xyz * bw;
w_sum += bw;
}
}
if (w_sum > 1.0e-5) {
pos = out_pos / w_sum;
norm = normalize(out_norm);
}
vert.pos_nx = vec4<f32>(pos, norm.x);
vert.ny_nz_tx_ty.x = norm.y;
vert.ny_nz_tx_ty.y = norm.z;
s_out_verts[vid] = vert;
}
@compute @workgroup_size(64)
fn cs_frustum_meshlet_cull(@builtin(global_invocation_id) gid: vec3<u32>) {
let mid = gid.x;
if (mid >= u_params.meshlet_count) {
return;
}
let bound = s_meshlets[mid];
let center = bound.center_radius.xyz;
let radius = bound.center_radius.w;
for (var p: u32 = 0u; p < 6u; p = p + 1u) {
let plane = u_params.frustum_planes[p];
if (dot(plane.xyz, center) + plane.w < -radius) {
return;
}
}
let cutoff = bound.cone_axis_cutoff.w;
if (cutoff > 0.05) {
let to_center = center - u_params.camera_pos.xyz;
let dist = length(to_center);
let sin_a = sqrt(max(1.0 - cutoff * cutoff, 0.0));
if (dot(to_center, bound.cone_axis_cutoff.xyz) > dist * sin_a + radius) {
return;
}
}
let slot = atomicAdd(&s_draw_counter, 1u);
s_indirect[slot] = DrawIndexedIndirectArgs(
bound.draw_range.y * 3u,
1u,
bound.draw_range.x * 3u,
0,
bound.draw_range.z,
);
}
"#;
pub const MSL_ENGINE3D_COMPUTE_SHADER: &str = r#"#include <metal_stdlib>
using namespace metal;
struct Vertex64 {
float4 pos_nx;
float4 ny_nz_tx_ty;
float4 tz_tw_uv0;
float4 uv1_col_flags;
};
struct SkinInfluence {
uint4 joints_0;
float4 weights_0;
uint4 joints_1;
float4 weights_1;
};
struct MorphDelta {
float4 dpos_pad;
float4 dnorm_pad;
};
struct MeshletCullBound {
float4 center_radius;
float4 cone_axis_cutoff;
uint4 draw_range;
};
struct DrawIndexedIndirectArgs {
uint index_count;
uint instance_count;
uint first_index;
int base_vertex;
uint first_instance;
};
struct ComputeParams {
uint vertex_count;
uint morph_target_count;
uint skin_mode_dqs;
uint meshlet_count;
float4 camera_pos;
float4 frustum_planes[6];
};
kernel void euv_3d_compute_morph_skin(
constant ComputeParams& params [[buffer(0)]],
const device Vertex64* bind_verts [[buffer(1)]],
const device SkinInfluence* influences [[buffer(2)]],
const device MorphDelta* morph_deltas [[buffer(3)]],
const device float* morph_weights [[buffer(4)]],
const device float4x4* bone_mats [[buffer(5)]],
device Vertex64* out_verts [[buffer(6)]],
uint vid [[thread_position_in_grid]]
) {
if (vid >= params.vertex_count) {
return;
}
Vertex64 vert = bind_verts[vid];
float3 pos = vert.pos_nx.xyz;
float3 norm = float3(vert.pos_nx.w, vert.ny_nz_tx_ty.x, vert.ny_nz_tx_ty.y);
for (uint m_idx = 0u; m_idx < params.morph_target_count; ++m_idx) {
float w = morph_weights[m_idx];
if (abs(w) > 1.0e-5f) {
MorphDelta delta = morph_deltas[m_idx * params.vertex_count + vid];
pos += delta.dpos_pad.xyz * w;
norm += delta.dnorm_pad.xyz * w;
}
}
norm = normalize(norm);
SkinInfluence inf = influences[vid];
float3 out_pos = float3(0.0f);
float3 out_norm = float3(0.0f);
float w_sum = 0.0f;
for (uint lane = 0u; lane < 4u; ++lane) {
float bw = inf.weights_0[lane];
if (bw > 1.0e-6f) {
float4x4 bm = bone_mats[inf.joints_0[lane]];
out_pos += (bm * float4(pos, 1.0f)).xyz * bw;
out_norm += (bm * float4(norm, 0.0f)).xyz * bw;
w_sum += bw;
}
}
for (uint lane = 0u; lane < 4u; ++lane) {
float bw = inf.weights_1[lane];
if (bw > 1.0e-6f) {
float4x4 bm = bone_mats[inf.joints_1[lane]];
out_pos += (bm * float4(pos, 1.0f)).xyz * bw;
out_norm += (bm * float4(norm, 0.0f)).xyz * bw;
w_sum += bw;
}
}
if (w_sum > 1.0e-5f) {
pos = out_pos / w_sum;
norm = normalize(out_norm);
}
vert.pos_nx = float4(pos, norm.x);
vert.ny_nz_tx_ty.x = norm.y;
vert.ny_nz_tx_ty.y = norm.z;
out_verts[vid] = vert;
}
kernel void euv_3d_compute_cull(
constant ComputeParams& params [[buffer(0)]],
const device MeshletCullBound* meshlets [[buffer(1)]],
device DrawIndexedIndirectArgs* indirect_args [[buffer(2)]],
device atomic_uint* draw_counter [[buffer(3)]],
uint mid [[thread_position_in_grid]]
) {
if (mid >= params.meshlet_count) {
return;
}
MeshletCullBound bound = meshlets[mid];
float3 center = bound.center_radius.xyz;
float radius = bound.center_radius.w;
for (uint p = 0u; p < 6u; ++p) {
float4 plane = params.frustum_planes[p];
if (dot(plane.xyz, center) + plane.w < -radius) {
return;
}
}
float cutoff = bound.cone_axis_cutoff.w;
if (cutoff > 0.05f) {
float3 to_center = center - params.camera_pos.xyz;
float dist = length(to_center);
float sin_a = sqrt(max(1.0f - cutoff * cutoff, 0.0f));
if (dot(to_center, bound.cone_axis_cutoff.xyz) > dist * sin_a + radius) {
return;
}
}
uint slot = atomic_fetch_add_explicit(draw_counter, 1u, memory_order_relaxed);
indirect_args[slot].index_count = bound.draw_range.y * 3u;
indirect_args[slot].instance_count = 1u;
indirect_args[slot].first_index = bound.draw_range.x * 3u;
indirect_args[slot].base_vertex = 0;
indirect_args[slot].first_instance = bound.draw_range.z;
}
"#;
pub const GLSL_ES3_ENGINE3D_COMPUTE_SHADER: &str = r#"#version 310 es
layout(local_size_x = 64, local_size_y = 1, local_size_z = 1) in;
struct Vertex64 {
vec4 pos_nx;
vec4 ny_nz_tx_ty;
vec4 tz_tw_uv0;
vec4 uv1_col_flags;
};
struct SkinInfluence {
uvec4 joints_0;
vec4 weights_0;
uvec4 joints_1;
vec4 weights_1;
};
struct MorphDelta {
vec4 dpos_pad;
vec4 dnorm_pad;
};
struct MeshletCullBound {
vec4 center_radius;
vec4 cone_axis_cutoff;
uvec4 draw_range;
};
struct DrawIndexedIndirectArgs {
uint index_count;
uint instance_count;
uint first_index;
int base_vertex;
uint first_instance;
};
layout(std430, binding = 0) readonly buffer BindVerts { Vertex64 s_bind_verts[]; };
layout(std430, binding = 1) readonly buffer Influences { SkinInfluence s_influences[]; };
layout(std430, binding = 2) readonly buffer MorphDeltas { MorphDelta s_morph_deltas[]; };
layout(std430, binding = 3) readonly buffer MorphWeights { float s_morph_weights[]; };
layout(std430, binding = 4) readonly buffer BoneMats { mat4 s_bone_mats[]; };
layout(std430, binding = 5) writeonly buffer OutVerts { Vertex64 s_out_verts[]; };
layout(std430, binding = 6) readonly buffer MeshletBounds { MeshletCullBound s_meshlets[]; };
layout(std430, binding = 7) writeonly buffer IndirectArgs { DrawIndexedIndirectArgs s_indirect_args[]; };
uniform uint u_vertex_count;
uniform uint u_morph_target_count;
uniform uint u_meshlet_count;
uniform uint u_pass_cull;
uniform vec4 u_camera_pos;
uniform vec4 u_frustum_planes[6];
void cs_frustum_meshlet_cull(uint mid) {
if (mid >= u_meshlet_count) { return; }
MeshletCullBound bound = s_meshlets[mid];
bool visible = true;
for (int p_idx = 0; p_idx < 6; ++p_idx) {
vec4 pl = u_frustum_planes[p_idx];
if (dot(pl.xyz, bound.center_radius.xyz) + pl.w < -bound.center_radius.w) {
visible = false;
}
}
if (visible && bound.cone_axis_cutoff.w > 0.05) {
vec3 to_cluster = bound.center_radius.xyz - u_camera_pos.xyz;
float dist = length(to_cluster);
float sin_a = sqrt(max(1.0 - bound.cone_axis_cutoff.w * bound.cone_axis_cutoff.w, 0.0));
if (dot(to_cluster, bound.cone_axis_cutoff.xyz) > dist * sin_a + bound.center_radius.w) {
visible = false;
}
}
DrawIndexedIndirectArgs cmd;
cmd.index_count = bound.draw_range.y * 3u;
cmd.instance_count = visible ? 1u : 0u;
cmd.first_index = bound.draw_range.x * 3u;
cmd.base_vertex = 0;
cmd.first_instance = bound.draw_range.z;
s_indirect_args[mid] = cmd;
}
void main() {
uint gid = gl_GlobalInvocationID.x;
if (u_pass_cull != 0u) {
cs_frustum_meshlet_cull(gid);
return;
}
uint vid = gid;
if (vid >= u_vertex_count) { return; }
Vertex64 vert = s_bind_verts[vid];
vec3 pos = vert.pos_nx.xyz;
vec3 norm = vec3(vert.pos_nx.w, vert.ny_nz_tx_ty.x, vert.ny_nz_tx_ty.y);
for (uint m_idx = 0u; m_idx < u_morph_target_count; ++m_idx) {
float w = s_morph_weights[m_idx];
if (abs(w) > 1.0e-5) {
MorphDelta d = s_morph_deltas[m_idx * u_vertex_count + vid];
pos += d.dpos_pad.xyz * w;
norm += d.dnorm_pad.xyz * w;
}
}
norm = normalize(norm);
SkinInfluence inf = s_influences[vid];
vec3 out_pos = vec3(0.0);
vec3 out_norm = vec3(0.0);
float w_sum = 0.0;
for (int lane = 0; lane < 4; ++lane) {
float bw = inf.weights_0[lane];
if (bw > 1.0e-6) {
mat4 bm = s_bone_mats[inf.joints_0[lane]];
out_pos += (bm * vec4(pos, 1.0)).xyz * bw;
out_norm += (bm * vec4(norm, 0.0)).xyz * bw;
w_sum += bw;
}
}
for (int lane = 0; lane < 4; ++lane) {
float bw = inf.weights_1[lane];
if (bw > 1.0e-6) {
mat4 bm = s_bone_mats[inf.joints_1[lane]];
out_pos += (bm * vec4(pos, 1.0)).xyz * bw;
out_norm += (bm * vec4(norm, 0.0)).xyz * bw;
w_sum += bw;
}
}
if (w_sum > 1.0e-5) {
pos = out_pos / w_sum;
norm = normalize(out_norm);
}
vert.pos_nx = vec4(pos, norm.x);
vert.ny_nz_tx_ty.x = norm.y;
vert.ny_nz_tx_ty.y = norm.z;
s_out_verts[vid] = vert;
}
"#;
pub const HLSL_ENGINE3D_COMPUTE_SHADER: &str = r#"struct Vertex64 {
float4 pos_nx;
float4 ny_nz_tx_ty;
float4 tz_tw_uv0;
float4 uv1_col_flags;
};
struct SkinInfluence {
uint4 joints_0;
float4 weights_0;
uint4 joints_1;
float4 weights_1;
};
struct MorphDelta {
float4 dpos_pad;
float4 dnorm_pad;
};
struct MeshletCullBound {
float4 center_radius;
float4 cone_axis_cutoff;
uint4 draw_range;
};
struct DrawIndexedIndirectArgs {
uint index_count;
uint instance_count;
uint first_index;
int base_vertex;
uint first_instance;
};
cbuffer ComputeParams : register(b0) {
uint g_vertex_count;
uint g_morph_target_count;
uint g_skin_mode_dqs;
uint g_meshlet_count;
float4 g_camera_pos;
float4 g_frustum_planes[6];
};
StructuredBuffer<Vertex64> g_bind_verts : register(t0);
StructuredBuffer<SkinInfluence> g_influences : register(t1);
StructuredBuffer<MorphDelta> g_morph_deltas : register(t2);
StructuredBuffer<float> g_morph_weights : register(t3);
StructuredBuffer<float4x4> g_bone_mats : register(t4);
StructuredBuffer<MeshletCullBound> g_meshlets : register(t5);
RWStructuredBuffer<Vertex64> g_out_verts : register(u0);
RWStructuredBuffer<DrawIndexedIndirectArgs> g_indirect : register(u1);
RWByteAddressBuffer g_draw_counter : register(u2);
[numthreads(64, 1, 1)]
void cs_morph_skin(uint3 gid : SV_DispatchThreadID) {
uint vid = gid.x;
if (vid >= g_vertex_count) { return; }
Vertex64 vert = g_bind_verts[vid];
float3 pos = vert.pos_nx.xyz;
float3 norm = float3(vert.pos_nx.w, vert.ny_nz_tx_ty.x, vert.ny_nz_tx_ty.y);
for (uint m_idx = 0u; m_idx < g_morph_target_count; ++m_idx) {
float w = g_morph_weights[m_idx];
if (abs(w) > 1.0e-5) {
MorphDelta d = g_morph_deltas[m_idx * g_vertex_count + vid];
pos += d.dpos_pad.xyz * w;
norm += d.dnorm_pad.xyz * w;
}
}
norm = normalize(norm);
SkinInfluence inf = g_influences[vid];
float3 out_pos = float3(0.0, 0.0, 0.0);
float3 out_norm = float3(0.0, 0.0, 0.0);
float w_sum = 0.0;
[unroll]
for (uint lane = 0u; lane < 4u; ++lane) {
float bw = inf.weights_0[lane];
if (bw > 1.0e-6) {
float4x4 bm = g_bone_mats[inf.joints_0[lane]];
out_pos += mul(bm, float4(pos, 1.0)).xyz * bw;
out_norm += mul((float3x3)bm, norm) * bw;
w_sum += bw;
}
}
[unroll]
for (uint lane = 0u; lane < 4u; ++lane) {
float bw = inf.weights_1[lane];
if (bw > 1.0e-6) {
float4x4 bm = g_bone_mats[inf.joints_1[lane]];
out_pos += mul(bm, float4(pos, 1.0)).xyz * bw;
out_norm += mul((float3x3)bm, norm) * bw;
w_sum += bw;
}
}
if (w_sum > 1.0e-5) {
pos = out_pos / w_sum;
norm = normalize(out_norm);
}
vert.pos_nx = float4(pos, norm.x);
vert.ny_nz_tx_ty.x = norm.y;
vert.ny_nz_tx_ty.y = norm.z;
g_out_verts[vid] = vert;
}
[numthreads(64, 1, 1)]
void cs_frustum_meshlet_cull(uint3 gid : SV_DispatchThreadID) {
uint mid = gid.x;
if (mid >= g_meshlet_count) { return; }
MeshletCullBound bound = g_meshlets[mid];
float3 center = bound.center_radius.xyz;
float radius = bound.center_radius.w;
[unroll]
for (uint p = 0u; p < 6u; ++p) {
float4 plane = g_frustum_planes[p];
if (dot(plane.xyz, center) + plane.w < -radius) {
return;
}
}
float cutoff = bound.cone_axis_cutoff.w;
if (cutoff > 0.05) {
float3 to_center = center - g_camera_pos.xyz;
float dist = length(to_center);
float sin_a = sqrt(max(1.0 - cutoff * cutoff, 0.0));
if (dot(to_center, bound.cone_axis_cutoff.xyz) > dist * sin_a + radius) {
return;
}
}
uint slot = 0u;
g_draw_counter.InterlockedAdd(0u, 1u, slot);
DrawIndexedIndirectArgs args;
args.index_count = bound.draw_range.y * 3u;
args.instance_count = 1u;
args.first_index = bound.draw_range.x * 3u;
args.base_vertex = 0;
args.first_instance = bound.draw_range.z;
g_indirect[slot] = args;
}
"#;
pub const PBR_PSO_VARIANT_OPAQUE: u8 = 0;
pub const PBR_PSO_VARIANT_SKINNED: u8 = 1;
pub const PBR_PSO_VARIANT_ALPHA_BLEND: u8 = 2;
pub const PBR_DEBUG_VIEW_LIT: u8 = 0;
pub const PBR_DEBUG_VIEW_MESHLETS: u8 = 1;
pub const PBR_DEBUG_VIEW_LOD_HEATMAP: u8 = 2;
pub const PBR_DEBUG_VIEW_MIP_RESIDENCY: u8 = 3;
pub const PBR_MAX_LOCAL_LIGHTS: usize = 32;
pub const PBR_INST_FLAG_SKINNED: u32 = 1;
pub const PBR_INST_FLAG_MORPHED: u32 = 2;
pub const PBR_INST_FLAG_DQS: u32 = 4;
pub const PBR_INST_FLAG_ALPHA_BLEND: u32 = 8;
pub const PBR_SCENE_UNIFORM_BYTES: usize = 320;
pub const MSL_PBR_SCENE_SHADER: &str = r#"#include <metal_stdlib>
using namespace metal;
struct PbrSceneUniforms {
float4 vp_c0;
float4 vp_c1;
float4 vp_c2;
float4 vp_c3;
float4 camera_pos_exposure;
float4 sun_dir_fog;
float4 sun_color_ambient;
float4 sky_horizon_tod;
float4 sky_zenith_agx;
float4 ibl_sh[9];
uint4 batch_params;
uint4 draw_offsets;
};
struct PbrQuantizedVert {
ushort pos_x;
ushort pos_y;
ushort pos_z;
ushort pad_flags;
short norm_x;
short norm_y;
short tan_x;
short tan_y;
ushort uv_u;
ushort uv_v;
uchar col_r;
uchar col_g;
uchar col_b;
uchar col_a;
};
struct PbrSkinVert {
ushort j0;
ushort j1;
ushort j2;
ushort j3;
uchar w0;
uchar w1;
uchar w2;
uchar w3;
uint extra_offset;
};
struct PbrInstance {
float4 model_c0;
float4 model_c1;
float4 model_c2;
float4 model_c3;
float4 quant_origin_id;
float4 quant_extent_lod;
uint4 mat_skin_morph_flags;
};
struct PbrMaterial {
float4 base_color_factor;
float4 emissive_alpha_cutoff;
float4 pbr_ior_specular;
float4 specular_color_clearcoat;
float4 clearcoat_normal_occ_trans;
float4 volume_attenuation;
float4 sheen_color_roughness;
uint4 tex_indices_a;
uint4 tex_indices_b;
uint4 flags_volume;
};
struct PbrLocalLight {
float4 position_range;
float4 color_intensity;
float4 direction_kind;
float4 cone_size_params;
};
struct PbrSkyOut {
float4 pos [[position]];
float2 uv;
};
struct PbrSceneVertOut {
float4 clip_pos [[position]];
float3 world_pos;
float3 world_normal;
float4 world_tangent;
float2 uv0;
float4 color0;
uint4 meta [[flat]];
};
static inline float3 pbr_oct_decode(float2 e) {
float3 v = float3(e.x, e.y, 1.0f - abs(e.x) - abs(e.y));
if (v.z < 0.0f) {
float2 s = float2(v.x >= 0.0f ? 1.0f : -1.0f, v.y >= 0.0f ? 1.0f : -1.0f);
v.xy = (1.0f - abs(v.yx)) * s;
}
return normalize(v);
}
static inline float4 pbr_mul_cols(float4 c0, float4 c1, float4 c2, float4 c3, float4 v) {
return c0 * v.x + c1 * v.y + c2 * v.z + c3 * v.w;
}
static inline float3x3 pbr_cofactor_mat(float3 c0, float3 c1, float3 c2) {
float3 n0 = cross(c1, c2);
float3 n1 = cross(c2, c0);
float3 n2 = cross(c0, c1);
float det = dot(c0, n0);
float inv_det = abs(det) > 1.0e-7f ? (1.0f / det) : 1.0f;
return float3x3(n0 * inv_det, n1 * inv_det, n2 * inv_det);
}
static inline float3 pbr_agx_tonemap(float3 x) {
float3 v = max(x, float3(0.0f));
float3 a = v * (v + 0.0245786f) - 0.000090537f;
float3 b = v * (0.983729f * v + 0.4329510f) + 0.238081f;
float3 m = clamp(a / max(b, float3(1.0e-5f)), float3(0.0f), float3(1.0f));
return mix(m, pow(m, float3(0.95f)), 0.18f);
}
static inline float3 pbr_aces_tonemap(float3 x) {
float3 v = max(x, float3(0.0f));
return clamp((v * (2.51f * v + 0.03f)) / (v * (2.43f * v + 0.59f) + 0.14f), float3(0.0f), float3(1.0f));
}
static inline float3 pbr_srgb_encode(float3 c) {
float3 cl = clamp(c, float3(0.0f), float3(1.0f));
float3 lo = cl * 12.92f;
float3 hi = 1.055f * pow(cl, float3(1.0f / 2.4f)) - 0.055f;
return select(hi, lo, cl <= float3(0.0031308f));
}
static inline float3 pbr_srgb_decode(float3 c) {
return pow(clamp(c, float3(0.0f), float3(1.0f)), float3(2.2f));
}
static inline float3 pbr_eval_sh9(float3 n, constant float4* sh) {
float3 res = sh[0].xyz * 0.282095f;
res += sh[1].xyz * (0.488603f * n.y);
res += sh[2].xyz * (0.488603f * n.z);
res += sh[3].xyz * (0.488603f * n.x);
res += sh[4].xyz * (1.092548f * n.x * n.y);
res += sh[5].xyz * (1.092548f * n.y * n.z);
res += sh[6].xyz * (0.315392f * (3.0f * n.z * n.z - 1.0f));
res += sh[7].xyz * (1.092548f * n.x * n.z);
res += sh[8].xyz * (0.546274f * (n.x * n.x - n.y * n.y));
return max(res, float3(0.0f));
}
static inline float pbr_d_ggx(float ndh, float a) {
float a2 = a * a;
float d = ndh * ndh * (a2 - 1.0f) + 1.0f;
return a2 / max(3.14159265f * d * d, 1.0e-5f);
}
static inline float pbr_v_smith_ggx(float ndv, float ndl, float a) {
float a2 = a * a;
float gv = ndl * sqrt(ndv * ndv * (1.0f - a2) + a2);
float gl = ndv * sqrt(ndl * ndl * (1.0f - a2) + a2);
return 0.5f / max(gv + gl, 1.0e-5f);
}
static inline float3 pbr_f_schlick(float vdh, float3 f0) {
float f = pow(clamp(1.0f - vdh, 0.0f, 1.0f), 5.0f);
return f0 + (float3(1.0f) - f0) * f;
}
static inline float3 pbr_eval_direct(
float3 n,
float3 v,
float3 l,
float3 radiance,
float3 albedo,
float metallic,
float roughness,
float3 f0,
float clearcoat,
float cc_rough,
float3 sheen_col,
float sheen_rough
) {
float ndl = max(dot(n, l), 0.0f);
if (ndl <= 0.0f) { return float3(0.0f); }
float ndv = max(dot(n, v), 1.0e-4f);
float3 h = normalize(v + l);
float ndh = max(dot(n, h), 0.0f);
float vdh = max(dot(v, h), 0.0f);
float a = roughness * roughness;
float d = pbr_d_ggx(ndh, a);
float vis = pbr_v_smith_ggx(ndv, ndl, a);
float3 fres = pbr_f_schlick(vdh, f0);
float3 kd = (float3(1.0f) - fres) * (1.0f - metallic);
float3 contrib = (kd * albedo * (1.0f / 3.14159265f) + fres * (d * vis)) * radiance * ndl;
if (clearcoat > 0.01f) {
float ca = max(cc_rough * cc_rough, 0.002f);
float cd = pbr_d_ggx(ndh, ca);
float cv = pbr_v_smith_ggx(ndv, ndl, ca);
float cf = 0.04f + 0.96f * pow(clamp(1.0f - vdh, 0.0f, 1.0f), 5.0f);
contrib = contrib * (1.0f - clearcoat * cf) + float3(clearcoat * cd * cv * cf) * radiance * ndl;
}
float sheen_luma = dot(sheen_col, float3(0.2126f, 0.7152f, 0.0722f));
if (sheen_luma > 0.005f) {
float inv_r = 1.0f / max(sheen_rough, 0.08f);
float sin2 = max(1.0f - ndh * ndh, 0.0f);
float sheen_d = (2.0f + inv_r) * pow(sin2, inv_r * 0.5f) / 6.2831853f;
contrib += sheen_col * (sheen_d * 0.25f) * radiance * ndl;
}
return contrib;
}
static inline float3 pbr_debug_palette(uint tag) {
float h = fract(float(tag) * 0.6180339887f);
float3 k = float3(1.0f, 2.0f / 3.0f, 1.0f / 3.0f);
float3 p = abs(fract(float3(h) + k) * 6.0f - 3.0f);
return mix(float3(0.25f), clamp(p - 1.0f, float3(0.0f), float3(1.0f)), 0.85f);
}
static inline PbrSceneVertOut pbr_scene_vert_impl(
uint vid,
uint iid,
constant PbrSceneUniforms& u,
device const PbrQuantizedVert* verts,
device const PbrInstance* instances,
device const PbrSkinVert* skin_verts,
device const float4* bone_mats,
device const float4* morph_deltas,
device const float* morph_weights,
bool allow_skin
) {
PbrQuantizedVert qv = verts[vid];
PbrInstance inst = instances[iid];
float3 q_norm = float3(float(qv.pos_x), float(qv.pos_y), float(qv.pos_z)) * (1.0f / 65535.0f);
float3 lpos = inst.quant_origin_id.xyz + q_norm * inst.quant_extent_lod.xyz;
float3 lnorm = pbr_oct_decode(float2(float(qv.norm_x), float(qv.norm_y)) * (1.0f / 32767.0f));
float3 ltan = pbr_oct_decode(float2(float(qv.tan_x), float(qv.tan_y)) * (1.0f / 32767.0f));
float tan_sign = ((qv.pad_flags & 16u) != 0u) ? -1.0f : 1.0f;
float2 uv0 = float2(float(as_type<half>(qv.uv_u)), float(as_type<half>(qv.uv_v)));
float4 col0 = float4(float(qv.col_r), float(qv.col_g), float(qv.col_b), float(qv.col_a)) * (1.0f / 255.0f);
uint flags = inst.mat_skin_morph_flags.w;
if ((flags & 2u) != 0u && u.draw_offsets.z > 0u) {
uint morph_off = inst.mat_skin_morph_flags.z;
uint stride = u.draw_offsets.z;
for (uint k = 0u; k < 4u; ++k) {
float w = morph_weights[morph_off + k];
if (abs(w) > 1.0e-5f) {
float4 delta = morph_deltas[k * stride + vid];
lpos += delta.xyz * w;
}
}
}
if (allow_skin && (flags & 1u) != 0u) {
PbrSkinVert sv = skin_verts[vid];
float4 sw = float4(float(sv.w0), float(sv.w1), float(sv.w2), float(sv.w3)) * (1.0f / 255.0f);
float wsum = max(sw.x + sw.y + sw.z + sw.w, 1.0e-5f);
sw *= (1.0f / wsum);
uint bone_off = inst.mat_skin_morph_flags.y;
uint joints[4] = { uint(sv.j0), uint(sv.j1), uint(sv.j2), uint(sv.j3) };
float4 b0 = float4(0.0f);
float4 b1 = float4(0.0f);
float4 b2 = float4(0.0f);
float4 b3 = float4(0.0f);
for (uint k = 0u; k < 4u; ++k) {
float wk = sw[k];
if (wk > 0.0f) {
uint base_m = (bone_off + joints[k]) * 4u;
b0 += bone_mats[base_m + 0u] * wk;
b1 += bone_mats[base_m + 1u] * wk;
b2 += bone_mats[base_m + 2u] * wk;
b3 += bone_mats[base_m + 3u] * wk;
}
}
if ((flags & 4u) != 0u) {
b0.xyz = normalize(b0.xyz + float3(1.0e-6f, 0.0f, 0.0f));
b1.xyz = normalize(b1.xyz - b0.xyz * dot(b0.xyz, b1.xyz) + float3(0.0f, 1.0e-6f, 0.0f));
b2.xyz = normalize(cross(b0.xyz, b1.xyz));
}
lpos = pbr_mul_cols(b0, b1, b2, b3, float4(lpos, 1.0f)).xyz;
lnorm = normalize(pbr_mul_cols(b0, b1, b2, float4(0.0f), float4(lnorm, 0.0f)).xyz);
ltan = normalize(pbr_mul_cols(b0, b1, b2, float4(0.0f), float4(ltan, 0.0f)).xyz);
}
float4 world4 = pbr_mul_cols(inst.model_c0, inst.model_c1, inst.model_c2, inst.model_c3, float4(lpos, 1.0f));
float3x3 nmat = pbr_cofactor_mat(inst.model_c0.xyz, inst.model_c1.xyz, inst.model_c2.xyz);
float3 wn = normalize(nmat * lnorm);
float3 wt = normalize(inst.model_c0.xyz * ltan.x + inst.model_c1.xyz * ltan.y + inst.model_c2.xyz * ltan.z);
PbrSceneVertOut out;
out.clip_pos = pbr_mul_cols(u.vp_c0, u.vp_c1, u.vp_c2, u.vp_c3, float4(world4.xyz, 1.0f));
out.world_pos = world4.xyz;
out.world_normal = wn;
out.world_tangent = float4(wt, tan_sign);
out.uv0 = uv0;
out.color0 = col0;
out.meta = uint4(inst.mat_skin_morph_flags.x, uint((qv.pad_flags >> 8u) & 255u), u.batch_params.y & 255u, flags);
return out;
}
vertex PbrSkyOut euv_pbr_sky_vert(uint vid [[vertex_id]]) {
float2 pos = float2((vid == 1u) ? 3.0f : -1.0f, (vid == 2u) ? 3.0f : -1.0f);
PbrSkyOut out;
out.pos = float4(pos, 0.9999f, 1.0f);
out.uv = pos * 0.5f + 0.5f;
return out;
}
fragment float4 euv_pbr_sky_frag(
PbrSkyOut in [[stage_in]],
constant PbrSceneUniforms& u [[buffer(0)]]
) {
float elev = clamp(in.uv.y, 0.0f, 1.0f);
float3 sky = mix(u.sky_horizon_tod.xyz, u.sky_zenith_agx.xyz, pow(elev, 0.65f));
float3 sun_dir = normalize(u.sun_dir_fog.xyz);
float sun_y = clamp(sun_dir.y * 0.5f + 0.5f, 0.05f, 0.95f);
float2 sun_uv = float2(0.5f + sun_dir.x * 0.28f, sun_y);
float sd = length(in.uv - sun_uv);
float glow = exp(-sd * 8.0f) * 0.45f + smoothstep(0.032f, 0.008f, sd) * 2.2f;
float3 hdr = (sky + u.sun_color_ambient.xyz * glow) * u.camera_pos_exposure.w;
float3 mapped = (u.sky_zenith_agx.w >= 0.5f) ? pbr_agx_tonemap(hdr) : pbr_aces_tonemap(hdr);
return float4(pbr_srgb_encode(mapped), 1.0f);
}
vertex PbrSceneVertOut euv_pbr_scene_vert(
uint vid [[vertex_id]],
uint iid [[instance_id]],
constant PbrSceneUniforms& u [[buffer(0)]],
device const PbrQuantizedVert* verts [[buffer(1)]],
device const PbrInstance* instances [[buffer(2)]],
device const PbrSkinVert* skin_verts [[buffer(3)]],
device const float4* bone_mats [[buffer(4)]],
device const float4* morph_deltas [[buffer(5)]],
device const float* morph_weights [[buffer(6)]]
) {
return pbr_scene_vert_impl(vid, iid, u, verts, instances, skin_verts, bone_mats, morph_deltas, morph_weights, false);
}
vertex PbrSceneVertOut euv_pbr_scene_skinned_vert(
uint vid [[vertex_id]],
uint iid [[instance_id]],
constant PbrSceneUniforms& u [[buffer(0)]],
device const PbrQuantizedVert* verts [[buffer(1)]],
device const PbrInstance* instances [[buffer(2)]],
device const PbrSkinVert* skin_verts [[buffer(3)]],
device const float4* bone_mats [[buffer(4)]],
device const float4* morph_deltas [[buffer(5)]],
device const float* morph_weights [[buffer(6)]]
) {
return pbr_scene_vert_impl(vid, iid, u, verts, instances, skin_verts, bone_mats, morph_deltas, morph_weights, true);
}
fragment float4 euv_pbr_scene_frag(
PbrSceneVertOut in [[stage_in]],
constant PbrSceneUniforms& u [[buffer(0)]],
device const PbrMaterial* materials [[buffer(1)]],
device const PbrLocalLight* lights [[buffer(2)]],
texture2d<float> tex_base [[texture(0)]],
texture2d<float> tex_mr [[texture(1)]],
texture2d<float> tex_normal [[texture(2)]],
texture2d<float> tex_occ [[texture(3)]],
texture2d<float> tex_emissive [[texture(4)]]
) {
constexpr sampler s_pbr(mag_filter::linear, min_filter::linear, mip_filter::linear, address::repeat);
uint mat_idx = u.batch_params.w;
PbrMaterial mat = materials[mat_idx];
uint tex_mask = (u.batch_params.y >> 16u) & 31u;
float3 n_geom = normalize(in.world_normal);
float3 v = normalize(u.camera_pos_exposure.xyz - in.world_pos);
float3 sun_dir = normalize(u.sun_dir_fog.xyz);
uint debug_mode = u.batch_params.x;
if (debug_mode != 0u) {
float ndl_dbg = clamp(dot(n_geom, sun_dir) * 0.5f + 0.5f, 0.22f, 1.0f);
float3 dbg_col = float3(0.8f);
if (debug_mode == 1u) {
dbg_col = pbr_debug_palette(in.meta.y + mat_idx * 17u + 1u);
} else if (debug_mode == 2u) {
uint lod = in.meta.z;
if (lod == 0u) { dbg_col = float3(0.18f, 0.85f, 0.36f); }
else if (lod == 1u) { dbg_col = float3(0.16f, 0.72f, 0.96f); }
else if (lod == 2u) { dbg_col = float3(0.96f, 0.68f, 0.14f); }
else { dbg_col = float3(0.92f, 0.22f, 0.24f); }
} else {
uint mip_tier = (u.batch_params.y >> 8u) & 255u;
if (mip_tier == 0u) { dbg_col = float3(0.16f, 0.88f, 0.40f); }
else if (mip_tier == 1u) { dbg_col = float3(0.98f, 0.74f, 0.16f); }
else if (mip_tier == 2u) { dbg_col = float3(0.94f, 0.24f, 0.56f); }
else { dbg_col = float3(0.32f, 0.46f, 0.68f); }
}
return float4(pbr_srgb_encode(dbg_col * ndl_dbg), 1.0f);
}
float4 base_sample = float4(1.0f);
if ((tex_mask & 1u) != 0u) {
float4 raw_base = tex_base.sample(s_pbr, in.uv0);
base_sample = float4(pbr_srgb_decode(raw_base.rgb), raw_base.a);
}
float4 base_rgba = mat.base_color_factor * base_sample * in.color0;
if ((mat.flags_volume.x & 1u) != 0u && base_rgba.a < mat.emissive_alpha_cutoff.w) {
discard_fragment();
}
float metallic = clamp(mat.pbr_ior_specular.x, 0.0f, 1.0f);
float roughness = clamp(mat.pbr_ior_specular.y, 0.04f, 1.0f);
if ((tex_mask & 2u) != 0u) {
float4 mr = tex_mr.sample(s_pbr, in.uv0);
roughness = clamp(roughness * mr.g, 0.04f, 1.0f);
metallic = clamp(metallic * mr.b, 0.0f, 1.0f);
}
float3 n = n_geom;
if ((tex_mask & 4u) != 0u) {
float3 t = normalize(in.world_tangent.xyz - n_geom * dot(n_geom, in.world_tangent.xyz));
float3 b = cross(n_geom, t) * in.world_tangent.w;
float3 n_ts = tex_normal.sample(s_pbr, in.uv0).xyz * 2.0f - 1.0f;
n_ts.xy *= mat.clearcoat_normal_occ_trans.y;
n = normalize(t * n_ts.x + b * n_ts.y + n_geom * max(n_ts.z, 0.05f));
}
float3 dndx = dfdx(n);
float3 dndy = dfdy(n);
float spec_var = clamp(0.25f * (dot(dndx, dndx) + dot(dndy, dndy)), 0.0f, 0.18f);
roughness = sqrt(clamp(roughness * roughness + spec_var, 0.002f, 1.0f));
float occ = 1.0f;
if ((tex_mask & 8u) != 0u) {
occ = mix(1.0f, tex_occ.sample(s_pbr, in.uv0).r, mat.clearcoat_normal_occ_trans.z);
}
float3 emissive = mat.emissive_alpha_cutoff.xyz;
if ((tex_mask & 16u) != 0u) {
emissive *= pbr_srgb_decode(tex_emissive.sample(s_pbr, in.uv0).rgb);
}
if ((mat.flags_volume.x & 8u) != 0u) {
float3 unlit_hdr = (base_rgba.rgb + emissive) * u.camera_pos_exposure.w;
float3 unlit_map = (u.sky_zenith_agx.w >= 0.5f) ? pbr_agx_tonemap(unlit_hdr) : pbr_aces_tonemap(unlit_hdr);
return float4(pbr_srgb_encode(unlit_map), base_rgba.a);
}
float ior = max(mat.pbr_ior_specular.z, 1.0f);
float f0_dielectric = pow((ior - 1.0f) / (ior + 1.0f), 2.0f) * mat.pbr_ior_specular.w;
float3 f0 = mix(mat.specular_color_clearcoat.xyz * f0_dielectric, base_rgba.rgb, metallic);
float cc_fac = mat.specular_color_clearcoat.w;
float cc_rough = mat.clearcoat_normal_occ_trans.x;
float3 sheen_col = mat.sheen_color_roughness.xyz;
float sheen_rough = mat.sheen_color_roughness.w;
float3 lit = pbr_eval_direct(
n, v, sun_dir, u.sun_color_ambient.xyz, base_rgba.rgb, metallic, roughness, f0, cc_fac, cc_rough, sheen_col, sheen_rough
);
uint light_count = min(u.batch_params.z, 32u);
for (uint li = 0u; li < light_count; ++li) {
PbrLocalLight pl = lights[li];
float3 to_l = pl.position_range.xyz - in.world_pos;
float dist_sq = max(dot(to_l, to_l), 1.0e-4f);
float dist = sqrt(dist_sq);
float range = max(pl.position_range.w, 0.1f);
float ratio = clamp(dist / range, 0.0f, 1.0f);
float win = pow(max(1.0f - ratio * ratio * ratio * ratio, 0.0f), 2.0f);
float atten = (win / (dist_sq + 0.25f)) * pl.color_intensity.w;
float3 ldir = to_l / dist;
if (pl.direction_kind.w > 0.5f && pl.direction_kind.w < 1.5f) {
float cd = dot(-ldir, normalize(pl.direction_kind.xyz));
float spot = clamp((cd - pl.cone_size_params.y) / max(pl.cone_size_params.x - pl.cone_size_params.y, 1.0e-4f), 0.0f, 1.0f);
atten *= spot * spot;
}
if (atten > 1.0e-4f) {
lit += pbr_eval_direct(
n, v, ldir, pl.color_intensity.xyz * atten, base_rgba.rgb, metallic, roughness, f0, cc_fac, cc_rough, sheen_col, sheen_rough
);
}
}
float3 sh_irr = pbr_eval_sh9(n, u.ibl_sh);
float3 sky_amb = mix(u.sky_horizon_tod.xyz, u.sky_zenith_agx.xyz, clamp(n.y * 0.5f + 0.5f, 0.0f, 1.0f));
float3 diff_ibl = max(sh_irr, sky_amb * u.sun_color_ambient.w);
float3 refl = reflect(-v, n);
float3 env_spec = mix(u.sky_horizon_tod.xyz, u.sky_zenith_agx.xyz, clamp(refl.y * 0.5f + 0.5f, 0.0f, 1.0f));
float3 fres_env = f0 + (max(float3(1.0f - roughness), f0) - f0) * pow(clamp(1.0f - max(dot(n, v), 0.0f), 0.0f, 1.0f), 5.0f);
lit += ((float3(1.0f) - fres_env) * (1.0f - metallic) * base_rgba.rgb * diff_ibl + fres_env * env_spec * (0.45f + 0.55f * (1.0f - roughness))) * occ;
lit += emissive;
float cam_dist = length(u.camera_pos_exposure.xyz - in.world_pos);
float fog_t = 1.0f - exp(-cam_dist * u.sun_dir_fog.w);
lit = mix(lit, u.sky_horizon_tod.xyz, clamp(fog_t, 0.0f, 0.85f));
float3 hdr = lit * u.camera_pos_exposure.w;
float3 mapped = (u.sky_zenith_agx.w >= 0.5f) ? pbr_agx_tonemap(hdr) : pbr_aces_tonemap(hdr);
return float4(pbr_srgb_encode(mapped), base_rgba.a);
}
"#;
pub const GLSL_ES3_PBR_SKY_VERT_SHADER: &str = r#"#version 300 es
precision highp float;
out vec2 v_uv;
void main() {
vec2 pos = vec2((gl_VertexID == 1) ? 3.0 : -1.0, (gl_VertexID == 2) ? 3.0 : -1.0);
gl_Position = vec4(pos, 0.9999, 1.0);
v_uv = pos * 0.5 + 0.5;
}
"#;
pub const GLSL_ES3_PBR_SKY_FRAG_SHADER: &str = r#"#version 300 es
precision highp float;
in vec2 v_uv;
out vec4 o_color;
uniform vec4 u_camera_pos_exposure;
uniform vec4 u_sun_dir_fog;
uniform vec4 u_sun_color_ambient;
uniform vec4 u_sky_horizon_tod;
uniform vec4 u_sky_zenith_agx;
vec3 pbr_agx(vec3 x) {
vec3 v = max(x, vec3(0.0));
vec3 a = v * (v + 0.0245786) - 0.000090537;
vec3 b = v * (0.983729 * v + 0.4329510) + 0.238081;
vec3 m = clamp(a / max(b, vec3(1.0e-5)), 0.0, 1.0);
return mix(m, pow(m, vec3(0.95)), 0.18);
}
vec3 pbr_aces(vec3 x) {
vec3 v = max(x, vec3(0.0));
return clamp((v * (2.51 * v + 0.03)) / (v * (2.43 * v + 0.59) + 0.14), 0.0, 1.0);
}
vec3 pbr_srgb(vec3 c) {
vec3 cl = clamp(c, 0.0, 1.0);
return mix(1.055 * pow(cl, vec3(1.0 / 2.4)) - 0.055, cl * 12.92, step(cl, vec3(0.0031308)));
}
void main() {
float elev = clamp(v_uv.y, 0.0, 1.0);
vec3 sky = mix(u_sky_horizon_tod.xyz, u_sky_zenith_agx.xyz, pow(elev, 0.65));
vec3 sun_dir = normalize(u_sun_dir_fog.xyz);
float sun_y = clamp(sun_dir.y * 0.5 + 0.5, 0.05, 0.95);
vec2 sun_uv = vec2(0.5 + sun_dir.x * 0.28, sun_y);
float sd = length(v_uv - sun_uv);
float glow = exp(-sd * 8.0) * 0.45 + smoothstep(0.032, 0.008, sd) * 2.2;
vec3 hdr = (sky + u_sun_color_ambient.xyz * glow) * u_camera_pos_exposure.w;
vec3 mapped = (u_sky_zenith_agx.w >= 0.5) ? pbr_agx(hdr) : pbr_aces(hdr);
o_color = vec4(pbr_srgb(mapped), 1.0);
}
"#;
pub const GLSL_ES3_PBR_SCENE_VERT_SHADER: &str = r#"#version 300 es
precision highp float;
precision highp int;
precision highp sampler2D;
layout(location = 0) in uvec4 a_pos_flags;
layout(location = 1) in ivec4 a_norm_tan;
layout(location = 2) in uvec2 a_uv_col;
layout(location = 3) in uvec4 a_skin_joints;
layout(location = 4) in vec4 a_skin_weights;
layout(location = 5) in vec4 a_model_c0;
layout(location = 6) in vec4 a_model_c1;
layout(location = 7) in vec4 a_model_c2;
layout(location = 8) in vec4 a_model_c3;
layout(location = 9) in vec4 a_quant_origin;
layout(location = 10) in vec4 a_quant_extent;
layout(location = 11) in uvec4 a_inst_meta;
uniform mat4 u_view_proj;
uniform ivec4 u_batch_params;
uniform ivec4 u_draw_offsets;
uniform vec4 u_morph_weights[4];
uniform sampler2D u_bone_tex;
uniform sampler2D u_morph_tex;
out vec3 v_world_pos;
out vec3 v_world_normal;
out vec4 v_world_tangent;
out vec2 v_uv0;
out vec4 v_color0;
flat out uvec4 v_meta;
vec3 oct_decode(vec2 e) {
vec3 v = vec3(e.x, e.y, 1.0 - abs(e.x) - abs(e.y));
if (v.z < 0.0) {
vec2 s = vec2(v.x >= 0.0 ? 1.0 : -1.0, v.y >= 0.0 ? 1.0 : -1.0);
v.xy = (1.0 - abs(v.yx)) * s;
}
return normalize(v);
}
mat3 cofactor_mat(vec3 c0, vec3 c1, vec3 c2) {
vec3 n0 = cross(c1, c2);
vec3 n1 = cross(c2, c0);
vec3 n2 = cross(c0, c1);
float det = dot(c0, n0);
float inv_det = abs(det) > 1.0e-7 ? (1.0 / det) : 1.0;
return mat3(n0 * inv_det, n1 * inv_det, n2 * inv_det);
}
vec4 fetch_bone_col(int bone_idx, int col) {
int bx = (bone_idx & 63) * 4 + col;
int by = bone_idx >> 6;
return texelFetch(u_bone_tex, ivec2(bx, by), 0);
}
vec4 unpack_unorm4x8_es3(uint packed) {
return vec4(
float(packed & 255u),
float((packed >> 8u) & 255u),
float((packed >> 16u) & 255u),
float((packed >> 24u) & 255u)
) * (1.0 / 255.0);
}
void main() {
vec3 q_norm = vec3(a_pos_flags.xyz) * (1.0 / 65535.0);
vec3 lpos = a_quant_origin.xyz + q_norm * a_quant_extent.xyz;
vec3 lnorm = oct_decode(vec2(a_norm_tan.xy) * (1.0 / 32767.0));
vec3 ltan = oct_decode(vec2(a_norm_tan.zw) * (1.0 / 32767.0));
float tan_sign = ((a_pos_flags.w & 16u) != 0u) ? -1.0 : 1.0;
vec2 uv0 = unpackHalf2x16(a_uv_col.x);
vec4 col0 = unpack_unorm4x8_es3(a_uv_col.y);
uint flags = a_inst_meta.w;
if ((flags & 2u) != 0u && u_draw_offsets.z > 0) {
int stride = u_draw_offsets.z;
for (int k = 0; k < 4; ++k) {
float mw = u_morph_weights[0][k];
if (abs(mw) > 1.0e-5) {
int midx = k * stride + gl_VertexID;
vec4 delta = texelFetch(u_morph_tex, ivec2(midx & 255, midx >> 8), 0);
lpos += delta.xyz * mw;
}
}
}
if ((flags & 1u) != 0u) {
vec4 sw = a_skin_weights;
float wsum = max(sw.x + sw.y + sw.z + sw.w, 1.0e-5);
sw *= (1.0 / wsum);
int bone_off = int(a_inst_meta.y);
int joints[4];
joints[0] = int(a_skin_joints.x);
joints[1] = int(a_skin_joints.y);
joints[2] = int(a_skin_joints.z);
joints[3] = int(a_skin_joints.w);
vec4 b0 = vec4(0.0);
vec4 b1 = vec4(0.0);
vec4 b2 = vec4(0.0);
vec4 b3 = vec4(0.0);
for (int k = 0; k < 4; ++k) {
float wk = sw[k];
if (wk > 0.0) {
int bidx = bone_off + joints[k];
b0 += fetch_bone_col(bidx, 0) * wk;
b1 += fetch_bone_col(bidx, 1) * wk;
b2 += fetch_bone_col(bidx, 2) * wk;
b3 += fetch_bone_col(bidx, 3) * wk;
}
}
if ((flags & 4u) != 0u) {
b0.xyz = normalize(b0.xyz + vec3(1.0e-6, 0.0, 0.0));
b1.xyz = normalize(b1.xyz - b0.xyz * dot(b0.xyz, b1.xyz) + vec3(0.0, 1.0e-6, 0.0));
b2.xyz = normalize(cross(b0.xyz, b1.xyz));
}
lpos = (b0 * lpos.x + b1 * lpos.y + b2 * lpos.z + b3).xyz;
lnorm = normalize((b0 * lnorm.x + b1 * lnorm.y + b2 * lnorm.z).xyz);
ltan = normalize((b0 * ltan.x + b1 * ltan.y + b2 * ltan.z).xyz);
}
vec4 world4 = a_model_c0 * lpos.x + a_model_c1 * lpos.y + a_model_c2 * lpos.z + a_model_c3;
mat3 nmat = cofactor_mat(a_model_c0.xyz, a_model_c1.xyz, a_model_c2.xyz);
v_world_pos = world4.xyz;
v_world_normal = normalize(nmat * lnorm);
v_world_tangent = vec4(normalize(a_model_c0.xyz * ltan.x + a_model_c1.xyz * ltan.y + a_model_c2.xyz * ltan.z), tan_sign);
v_uv0 = uv0;
v_color0 = col0;
v_meta = uvec4(a_inst_meta.x, (a_pos_flags.w >> 8u) & 255u, uint(u_batch_params.y & 255), flags);
gl_Position = u_view_proj * vec4(world4.xyz, 1.0);
}
"#;
pub const GLSL_ES3_PBR_SCENE_FRAG_SHADER: &str = r#"#version 300 es
precision highp float;
precision highp int;
precision highp sampler2D;
in vec3 v_world_pos;
in vec3 v_world_normal;
in vec4 v_world_tangent;
in vec2 v_uv0;
in vec4 v_color0;
flat in uvec4 v_meta;
uniform vec4 u_camera_pos_exposure;
uniform vec4 u_sun_dir_fog;
uniform vec4 u_sun_color_ambient;
uniform vec4 u_sky_horizon_tod;
uniform vec4 u_sky_zenith_agx;
uniform vec4 u_ibl_sh[9];
uniform ivec4 u_batch_params;
uniform sampler2D u_tex_base;
uniform sampler2D u_tex_mr;
uniform sampler2D u_tex_normal;
uniform sampler2D u_tex_occ;
uniform sampler2D u_tex_emissive;
uniform sampler2D u_mat_tex;
uniform sampler2D u_light_tex;
out vec4 o_color;
vec3 pbr_agx(vec3 x) {
vec3 v = max(x, vec3(0.0));
vec3 a = v * (v + 0.0245786) - 0.000090537;
vec3 b = v * (0.983729 * v + 0.4329510) + 0.238081;
vec3 m = clamp(a / max(b, vec3(1.0e-5)), 0.0, 1.0);
return mix(m, pow(m, vec3(0.95)), 0.18);
}
vec3 pbr_aces(vec3 x) {
vec3 v = max(x, vec3(0.0));
return clamp((v * (2.51 * v + 0.03)) / (v * (2.43 * v + 0.59) + 0.14), 0.0, 1.0);
}
vec3 pbr_srgb_enc(vec3 c) {
vec3 cl = clamp(c, 0.0, 1.0);
return mix(1.055 * pow(cl, vec3(1.0 / 2.4)) - 0.055, cl * 12.92, step(cl, vec3(0.0031308)));
}
vec3 pbr_srgb_dec(vec3 c) {
return pow(clamp(c, 0.0, 1.0), vec3(2.2));
}
vec3 eval_sh9(vec3 n) {
vec3 res = u_ibl_sh[0].xyz * 0.282095;
res += u_ibl_sh[1].xyz * (0.488603 * n.y);
res += u_ibl_sh[2].xyz * (0.488603 * n.z);
res += u_ibl_sh[3].xyz * (0.488603 * n.x);
res += u_ibl_sh[4].xyz * (1.092548 * n.x * n.y);
res += u_ibl_sh[5].xyz * (1.092548 * n.y * n.z);
res += u_ibl_sh[6].xyz * (0.315392 * (3.0 * n.z * n.z - 1.0));
res += u_ibl_sh[7].xyz * (1.092548 * n.x * n.z);
res += u_ibl_sh[8].xyz * (0.546274 * (n.x * n.x - n.y * n.y));
return max(res, vec3(0.0));
}
float d_ggx(float ndh, float a) {
float a2 = a * a;
float d = ndh * ndh * (a2 - 1.0) + 1.0;
return a2 / max(3.14159265 * d * d, 1.0e-5);
}
float v_smith(float ndv, float ndl, float a) {
float a2 = a * a;
float gv = ndl * sqrt(ndv * ndv * (1.0 - a2) + a2);
float gl = ndv * sqrt(ndl * ndl * (1.0 - a2) + a2);
return 0.5 / max(gv + gl, 1.0e-5);
}
vec3 f_schlick(float vdh, vec3 f0) {
float f = pow(clamp(1.0 - vdh, 0.0, 1.0), 5.0);
return f0 + (vec3(1.0) - f0) * f;
}
vec3 eval_direct(
vec3 n, vec3 v, vec3 l, vec3 radiance, vec3 albedo, float metallic, float roughness,
vec3 f0, float cc_fac, float cc_rough, vec3 sheen_col, float sheen_rough
) {
float ndl = max(dot(n, l), 0.0);
if (ndl <= 0.0) { return vec3(0.0); }
float ndv = max(dot(n, v), 1.0e-4);
vec3 h = normalize(v + l);
float ndh = max(dot(n, h), 0.0);
float vdh = max(dot(v, h), 0.0);
float a = roughness * roughness;
float d = d_ggx(ndh, a);
float vis = v_smith(ndv, ndl, a);
vec3 fres = f_schlick(vdh, f0);
vec3 kd = (vec3(1.0) - fres) * (1.0 - metallic);
vec3 out_c = (kd * albedo * (1.0 / 3.14159265) + fres * (d * vis)) * radiance * ndl;
if (cc_fac > 0.01) {
float ca = max(cc_rough * cc_rough, 0.002);
float cd = d_ggx(ndh, ca);
float cv = v_smith(ndv, ndl, ca);
float cf = 0.04 + 0.96 * pow(clamp(1.0 - vdh, 0.0, 1.0), 5.0);
out_c = out_c * (1.0 - cc_fac * cf) + vec3(cc_fac * cd * cv * cf) * radiance * ndl;
}
float sluma = dot(sheen_col, vec3(0.2126, 0.7152, 0.0722));
if (sluma > 0.005) {
float inv_r = 1.0 / max(sheen_rough, 0.08);
float sin2 = max(1.0 - ndh * ndh, 0.0);
float sd = (2.0 + inv_r) * pow(sin2, inv_r * 0.5) / 6.2831853;
out_c += sheen_col * (sd * 0.25) * radiance * ndl;
}
return out_c;
}
vec3 debug_palette(uint tag) {
float h = fract(float(tag) * 0.6180339887);
vec3 p = abs(fract(vec3(h) + vec3(1.0, 2.0 / 3.0, 1.0 / 3.0)) * 6.0 - 3.0);
return mix(vec3(0.25), clamp(p - 1.0, 0.0, 1.0), 0.85);
}
void main() {
int mat_idx = u_batch_params.w;
int tex_mask = (u_batch_params.y >> 16) & 31;
vec3 n_geom = normalize(v_world_normal);
vec3 v = normalize(u_camera_pos_exposure.xyz - v_world_pos);
vec3 sun_dir = normalize(u_sun_dir_fog.xyz);
int debug_mode = u_batch_params.x;
if (debug_mode != 0) {
float ndl_dbg = clamp(dot(n_geom, sun_dir) * 0.5 + 0.5, 0.22, 1.0);
vec3 dbg_col = vec3(0.8);
if (debug_mode == 1) {
dbg_col = debug_palette(v_meta.y + uint(mat_idx) * 17u + 1u);
} else if (debug_mode == 2) {
uint lod = v_meta.z;
if (lod == 0u) { dbg_col = vec3(0.18, 0.85, 0.36); }
else if (lod == 1u) { dbg_col = vec3(0.16, 0.72, 0.96); }
else if (lod == 2u) { dbg_col = vec3(0.96, 0.68, 0.14); }
else { dbg_col = vec3(0.92, 0.22, 0.24); }
} else {
int mip_tier = (u_batch_params.y >> 8) & 255;
if (mip_tier == 0) { dbg_col = vec3(0.16, 0.88, 0.40); }
else if (mip_tier == 1) { dbg_col = vec3(0.98, 0.74, 0.16); }
else if (mip_tier == 2) { dbg_col = vec3(0.94, 0.24, 0.56); }
else { dbg_col = vec3(0.32, 0.46, 0.68); }
}
o_color = vec4(pbr_srgb_enc(dbg_col * ndl_dbg), 1.0);
return;
}
vec4 m_base = texelFetch(u_mat_tex, ivec2(0, mat_idx), 0);
vec4 m_emis = texelFetch(u_mat_tex, ivec2(1, mat_idx), 0);
vec4 m_pbr = texelFetch(u_mat_tex, ivec2(2, mat_idx), 0);
vec4 m_spec = texelFetch(u_mat_tex, ivec2(3, mat_idx), 0);
vec4 m_cc = texelFetch(u_mat_tex, ivec2(4, mat_idx), 0);
vec4 m_sheen = texelFetch(u_mat_tex, ivec2(6, mat_idx), 0);
vec4 m_flags_f = texelFetch(u_mat_tex, ivec2(9, mat_idx), 0);
uint m_flags = floatBitsToUint(m_flags_f.x);
vec4 base_sample = vec4(1.0);
if ((tex_mask & 1) != 0) {
vec4 raw_b = texture(u_tex_base, v_uv0);
base_sample = vec4(pbr_srgb_dec(raw_b.rgb), raw_b.a);
}
vec4 base_rgba = m_base * base_sample * v_color0;
if ((m_flags & 1u) != 0u && base_rgba.a < m_emis.w) {
discard;
}
float metallic = clamp(m_pbr.x, 0.0, 1.0);
float roughness = clamp(m_pbr.y, 0.04, 1.0);
if ((tex_mask & 2) != 0) {
vec4 mr = texture(u_tex_mr, v_uv0);
roughness = clamp(roughness * mr.g, 0.04, 1.0);
metallic = clamp(metallic * mr.b, 0.0, 1.0);
}
vec3 n = n_geom;
if ((tex_mask & 4) != 0) {
vec3 t = normalize(v_world_tangent.xyz - n_geom * dot(n_geom, v_world_tangent.xyz));
vec3 b = cross(n_geom, t) * v_world_tangent.w;
vec3 n_ts = texture(u_tex_normal, v_uv0).xyz * 2.0 - 1.0;
n_ts.xy *= m_cc.y;
n = normalize(t * n_ts.x + b * n_ts.y + n_geom * max(n_ts.z, 0.05));
}
vec3 dndx = dFdx(n);
vec3 dndy = dFdy(n);
float spec_var = clamp(0.25 * (dot(dndx, dndx) + dot(dndy, dndy)), 0.0, 0.18);
roughness = sqrt(clamp(roughness * roughness + spec_var, 0.002, 1.0));
float occ = 1.0;
if ((tex_mask & 8) != 0) {
occ = mix(1.0, texture(u_tex_occ, v_uv0).r, m_cc.z);
}
vec3 emissive = m_emis.xyz;
if ((tex_mask & 16) != 0) {
emissive *= pbr_srgb_dec(texture(u_tex_emissive, v_uv0).rgb);
}
if ((m_flags & 8u) != 0u) {
vec3 uh = (base_rgba.rgb + emissive) * u_camera_pos_exposure.w;
vec3 um = (u_sky_zenith_agx.w >= 0.5) ? pbr_agx(uh) : pbr_aces(uh);
o_color = vec4(pbr_srgb_enc(um), base_rgba.a);
return;
}
float ior = max(m_pbr.z, 1.0);
float f0_d = pow((ior - 1.0) / (ior + 1.0), 2.0) * m_pbr.w;
vec3 f0 = mix(m_spec.xyz * f0_d, base_rgba.rgb, metallic);
vec3 lit = eval_direct(
n, v, sun_dir, u_sun_color_ambient.xyz, base_rgba.rgb, metallic, roughness, f0,
m_spec.w, m_cc.x, m_sheen.xyz, m_sheen.w
);
int light_count = min(u_batch_params.z, 32);
for (int li = 0; li < 32; ++li) {
if (li >= light_count) { break; }
vec4 lp = texelFetch(u_light_tex, ivec2(0, li), 0);
vec4 lc = texelFetch(u_light_tex, ivec2(1, li), 0);
vec4 ld = texelFetch(u_light_tex, ivec2(2, li), 0);
vec4 ls = texelFetch(u_light_tex, ivec2(3, li), 0);
vec3 to_l = lp.xyz - v_world_pos;
float dist_sq = max(dot(to_l, to_l), 1.0e-4);
float dist = sqrt(dist_sq);
float ratio = clamp(dist / max(lp.w, 0.1), 0.0, 1.0);
float win = pow(max(1.0 - ratio * ratio * ratio * ratio, 0.0), 2.0);
float atten = (win / (dist_sq + 0.25)) * lc.w;
vec3 ldir = to_l / dist;
if (ld.w > 0.5 && ld.w < 1.5) {
float cd = dot(-ldir, normalize(ld.xyz));
float spot = clamp((cd - ls.y) / max(ls.x - ls.y, 1.0e-4), 0.0, 1.0);
atten *= spot * spot;
}
if (atten > 1.0e-4) {
lit += eval_direct(
n, v, ldir, lc.xyz * atten, base_rgba.rgb, metallic, roughness, f0,
m_spec.w, m_cc.x, m_sheen.xyz, m_sheen.w
);
}
}
vec3 sh_irr = eval_sh9(n);
vec3 sky_amb = mix(u_sky_horizon_tod.xyz, u_sky_zenith_agx.xyz, clamp(n.y * 0.5 + 0.5, 0.0, 1.0));
vec3 diff_ibl = max(sh_irr, sky_amb * u_sun_color_ambient.w);
vec3 refl = reflect(-v, n);
vec3 env_spec = mix(u_sky_horizon_tod.xyz, u_sky_zenith_agx.xyz, clamp(refl.y * 0.5 + 0.5, 0.0, 1.0));
vec3 fres_env = f0 + (max(vec3(1.0 - roughness), f0) - f0) * pow(clamp(1.0 - max(dot(n, v), 0.0), 0.0, 1.0), 5.0);
lit += ((vec3(1.0) - fres_env) * (1.0 - metallic) * base_rgba.rgb * diff_ibl + fres_env * env_spec * (0.45 + 0.55 * (1.0 - roughness))) * occ;
lit += emissive;
float cam_dist = length(u_camera_pos_exposure.xyz - v_world_pos);
float fog_t = 1.0 - exp(-cam_dist * u_sun_dir_fog.w);
lit = mix(lit, u_sky_horizon_tod.xyz, clamp(fog_t, 0.0, 0.85));
vec3 hdr = lit * u_camera_pos_exposure.w;
vec3 mapped = (u_sky_zenith_agx.w >= 0.5) ? pbr_agx(hdr) : pbr_aces(hdr);
o_color = vec4(pbr_srgb_enc(mapped), base_rgba.a);
}
"#;
pub const HLSL_PBR_SCENE_SHADER: &str = r#"cbuffer PbrSceneUniforms : register(b0) {
float4 g_vp_c0;
float4 g_vp_c1;
float4 g_vp_c2;
float4 g_vp_c3;
float4 g_camera_pos_exposure;
float4 g_sun_dir_fog;
float4 g_sun_color_ambient;
float4 g_sky_horizon_tod;
float4 g_sky_zenith_agx;
float4 g_ibl_sh[9];
uint4 g_batch_params;
uint4 g_draw_offsets;
};
struct PbrQuantizedVert {
uint pos_xy;
uint pos_z_flags;
uint norm_oct;
uint tan_oct;
uint uv0_f16;
uint color0_rgba8;
};
struct PbrSkinVert {
uint j01;
uint j23;
uint weights_u8;
uint extra_offset;
};
struct PbrInstance {
float4 model_c0;
float4 model_c1;
float4 model_c2;
float4 model_c3;
float4 quant_origin_id;
float4 quant_extent_lod;
uint4 mat_skin_morph_flags;
};
struct PbrMaterial {
float4 base_color_factor;
float4 emissive_alpha_cutoff;
float4 pbr_ior_specular;
float4 specular_color_clearcoat;
float4 clearcoat_normal_occ_trans;
float4 volume_attenuation;
float4 sheen_color_roughness;
uint4 tex_indices_a;
uint4 tex_indices_b;
uint4 flags_volume;
};
struct PbrLocalLight {
float4 position_range;
float4 color_intensity;
float4 direction_kind;
float4 cone_size_params;
};
StructuredBuffer<PbrQuantizedVert> g_verts : register(t0);
StructuredBuffer<uint> g_indices : register(t1);
StructuredBuffer<PbrInstance> g_instances : register(t2);
StructuredBuffer<PbrSkinVert> g_skin_verts : register(t3);
StructuredBuffer<float4> g_bone_mats : register(t4);
StructuredBuffer<float4> g_morph_deltas : register(t5);
StructuredBuffer<float> g_morph_weights : register(t6);
StructuredBuffer<PbrMaterial> g_materials : register(t0);
StructuredBuffer<PbrLocalLight> g_lights : register(t1);
Texture2D<float4> g_tex_base : register(t2);
Texture2D<float4> g_tex_mr : register(t3);
Texture2D<float4> g_tex_normal : register(t4);
Texture2D<float4> g_tex_occ : register(t5);
Texture2D<float4> g_tex_emissive : register(t6);
SamplerState g_pbr_sampler : register(s0);
struct PbrSkyOut {
float4 pos : SV_POSITION;
float2 uv : TEXCOORD0;
};
struct PbrSceneVSOut {
float4 clip_pos : SV_POSITION;
float3 world_pos : TEXCOORD0;
float3 world_normal : TEXCOORD1;
float4 world_tangent : TEXCOORD2;
float2 uv0 : TEXCOORD3;
float4 color0 : TEXCOORD4;
nointerpolation uint4 meta : TEXCOORD5;
};
float2 unpack_snorm16x2(uint packed) {
int lo = (int)(packed << 16u) >> 16;
int hi = (int)packed >> 16;
return clamp(float2((float)lo, (float)hi) * (1.0 / 32767.0), -1.0, 1.0);
}
float3 oct_decode_hlsl(float2 e) {
float3 v = float3(e.x, e.y, 1.0 - abs(e.x) - abs(e.y));
if (v.z < 0.0) {
float2 s = float2(v.x >= 0.0 ? 1.0 : -1.0, v.y >= 0.0 ? 1.0 : -1.0);
v.xy = (1.0 - abs(v.yx)) * s;
}
return normalize(v);
}
float4 mul_cols_hlsl(float4 c0, float4 c1, float4 c2, float4 c3, float4 v) {
return c0 * v.x + c1 * v.y + c2 * v.z + c3 * v.w;
}
float3 pbr_agx_hlsl(float3 x) {
float3 v = max(x, 0.0);
float3 a = v * (v + 0.0245786) - 0.000090537;
float3 b = v * (0.983729 * v + 0.4329510) + 0.238081;
float3 m = saturate(a / max(b, 1.0e-5));
return lerp(m, pow(m, 0.95), 0.18);
}
float3 pbr_aces_hlsl(float3 x) {
float3 v = max(x, 0.0);
return saturate((v * (2.51 * v + 0.03)) / (v * (2.43 * v + 0.59) + 0.14));
}
float3 pbr_srgb_enc_hlsl(float3 c) {
float3 cl = saturate(c);
float3 lo = cl * 12.92;
float3 hi = 1.055 * pow(cl, 1.0 / 2.4) - 0.055;
return (cl <= 0.0031308) ? lo : hi;
}
float3 pbr_srgb_dec_hlsl(float3 c) {
return pow(saturate(c), 2.2);
}
float3 eval_sh9_hlsl(float3 n) {
float3 res = g_ibl_sh[0].xyz * 0.282095;
res += g_ibl_sh[1].xyz * (0.488603 * n.y);
res += g_ibl_sh[2].xyz * (0.488603 * n.z);
res += g_ibl_sh[3].xyz * (0.488603 * n.x);
res += g_ibl_sh[4].xyz * (1.092548 * n.x * n.y);
res += g_ibl_sh[5].xyz * (1.092548 * n.y * n.z);
res += g_ibl_sh[6].xyz * (0.315392 * (3.0 * n.z * n.z - 1.0));
res += g_ibl_sh[7].xyz * (1.092548 * n.x * n.z);
res += g_ibl_sh[8].xyz * (0.546274 * (n.x * n.x - n.y * n.y));
return max(res, 0.0);
}
float d_ggx_hlsl(float ndh, float a) {
float a2 = a * a;
float d = ndh * ndh * (a2 - 1.0) + 1.0;
return a2 / max(3.14159265 * d * d, 1.0e-5);
}
float v_smith_hlsl(float ndv, float ndl, float a) {
float a2 = a * a;
float gv = ndl * sqrt(ndv * ndv * (1.0 - a2) + a2);
float gl = ndv * sqrt(ndl * ndl * (1.0 - a2) + a2);
return 0.5 / max(gv + gl, 1.0e-5);
}
float3 f_schlick_hlsl(float vdh, float3 f0) {
float f = pow(saturate(1.0 - vdh), 5.0);
return f0 + (1.0 - f0) * f;
}
float3 eval_direct_hlsl(
float3 n, float3 v, float3 l, float3 radiance, float3 albedo, float metallic, float roughness,
float3 f0, float cc_fac, float cc_rough, float3 sheen_col, float sheen_rough
) {
float ndl = max(dot(n, l), 0.0);
if (ndl <= 0.0) { return float3(0.0, 0.0, 0.0); }
float ndv = max(dot(n, v), 1.0e-4);
float3 h = normalize(v + l);
float ndh = max(dot(n, h), 0.0);
float vdh = max(dot(v, h), 0.0);
float a = roughness * roughness;
float d = d_ggx_hlsl(ndh, a);
float vis = v_smith_hlsl(ndv, ndl, a);
float3 fres = f_schlick_hlsl(vdh, f0);
float3 kd = (1.0 - fres) * (1.0 - metallic);
float3 out_c = (kd * albedo * (1.0 / 3.14159265) + fres * (d * vis)) * radiance * ndl;
if (cc_fac > 0.01) {
float ca = max(cc_rough * cc_rough, 0.002);
float cd = d_ggx_hlsl(ndh, ca);
float cv = v_smith_hlsl(ndv, ndl, ca);
float cf = 0.04 + 0.96 * pow(saturate(1.0 - vdh), 5.0);
out_c = out_c * (1.0 - cc_fac * cf) + (cc_fac * cd * cv * cf) * radiance * ndl;
}
float sluma = dot(sheen_col, float3(0.2126, 0.7152, 0.0722));
if (sluma > 0.005) {
float inv_r = 1.0 / max(sheen_rough, 0.08);
float sin2 = max(1.0 - ndh * ndh, 0.0);
float sd = (2.0 + inv_r) * pow(sin2, inv_r * 0.5) / 6.2831853;
out_c += sheen_col * (sd * 0.25) * radiance * ndl;
}
return out_c;
}
PbrSkyOut PbrSkyVS(uint vid : SV_VertexID) {
float2 pos = float2((vid == 1u) ? 3.0 : -1.0, (vid == 2u) ? 3.0 : -1.0);
PbrSkyOut o;
o.pos = float4(pos, 0.9999, 1.0);
o.uv = pos * 0.5 + 0.5;
return o;
}
float4 PbrSkyPS(PbrSkyOut inp) : SV_TARGET {
float elev = saturate(inp.uv.y);
float3 sky = lerp(g_sky_horizon_tod.xyz, g_sky_zenith_agx.xyz, pow(elev, 0.65));
float3 sun_dir = normalize(g_sun_dir_fog.xyz);
float sun_y = clamp(sun_dir.y * 0.5 + 0.5, 0.05, 0.95);
float2 sun_uv = float2(0.5 + sun_dir.x * 0.28, sun_y);
float sd = length(inp.uv - sun_uv);
float glow = exp(-sd * 8.0) * 0.45 + smoothstep(0.032, 0.008, sd) * 2.2;
float3 hdr = (sky + g_sun_color_ambient.xyz * glow) * g_camera_pos_exposure.w;
float3 mapped = (g_sky_zenith_agx.w >= 0.5) ? pbr_agx_hlsl(hdr) : pbr_aces_hlsl(hdr);
return float4(pbr_srgb_enc_hlsl(mapped), 1.0);
}
PbrSceneVSOut PbrSceneVS(uint sv_vid : SV_VertexID, uint sv_iid : SV_InstanceID) {
uint idx = g_indices[g_draw_offsets.w + sv_vid] + g_draw_offsets.x;
PbrQuantizedVert qv = g_verts[idx];
PbrInstance inst = g_instances[g_draw_offsets.y + sv_iid];
float3 q_norm = float3(
(float)(qv.pos_xy & 0xFFFFu),
(float)(qv.pos_xy >> 16u),
(float)(qv.pos_z_flags & 0xFFFFu)
) * (1.0 / 65535.0);
uint pad_flags = qv.pos_z_flags >> 16u;
float3 lpos = inst.quant_origin_id.xyz + q_norm * inst.quant_extent_lod.xyz;
float3 lnorm = oct_decode_hlsl(unpack_snorm16x2(qv.norm_oct));
float3 ltan = oct_decode_hlsl(unpack_snorm16x2(qv.tan_oct));
float tan_sign = ((pad_flags & 16u) != 0u) ? -1.0 : 1.0;
float2 uv0 = float2(f16tof32(qv.uv0_f16 & 0xFFFFu), f16tof32(qv.uv0_f16 >> 16u));
float4 col0 = float4(
(float)(qv.color0_rgba8 & 0xFFu),
(float)((qv.color0_rgba8 >> 8u) & 0xFFu),
(float)((qv.color0_rgba8 >> 16u) & 0xFFu),
(float)((qv.color0_rgba8 >> 24u) & 0xFFu)
) * (1.0 / 255.0);
uint flags = inst.mat_skin_morph_flags.w;
if ((flags & 2u) != 0u && g_draw_offsets.z > 0u) {
uint morph_off = inst.mat_skin_morph_flags.z;
uint stride = g_draw_offsets.z;
[unroll]
for (uint k = 0u; k < 4u; ++k) {
float mw = g_morph_weights[morph_off + k];
if (abs(mw) > 1.0e-5) {
lpos += g_morph_deltas[k * stride + idx].xyz * mw;
}
}
}
if ((flags & 1u) != 0u) {
PbrSkinVert sv = g_skin_verts[idx];
uint joints[4] = {
sv.j01 & 0xFFFFu,
sv.j01 >> 16u,
sv.j23 & 0xFFFFu,
sv.j23 >> 16u
};
float4 sw = float4(
(float)(sv.weights_u8 & 0xFFu),
(float)((sv.weights_u8 >> 8u) & 0xFFu),
(float)((sv.weights_u8 >> 16u) & 0xFFu),
(float)((sv.weights_u8 >> 24u) & 0xFFu)
) * (1.0 / 255.0);
sw *= (1.0 / max(sw.x + sw.y + sw.z + sw.w, 1.0e-5));
uint bone_off = inst.mat_skin_morph_flags.y;
float4 b0 = 0.0;
float4 b1 = 0.0;
float4 b2 = 0.0;
float4 b3 = 0.0;
[unroll]
for (uint k = 0u; k < 4u; ++k) {
float wk = sw[k];
if (wk > 0.0) {
uint bm = (bone_off + joints[k]) * 4u;
b0 += g_bone_mats[bm + 0u] * wk;
b1 += g_bone_mats[bm + 1u] * wk;
b2 += g_bone_mats[bm + 2u] * wk;
b3 += g_bone_mats[bm + 3u] * wk;
}
}
if ((flags & 4u) != 0u) {
b0.xyz = normalize(b0.xyz + float3(1.0e-6, 0.0, 0.0));
b1.xyz = normalize(b1.xyz - b0.xyz * dot(b0.xyz, b1.xyz) + float3(0.0, 1.0e-6, 0.0));
b2.xyz = normalize(cross(b0.xyz, b1.xyz));
}
lpos = mul_cols_hlsl(b0, b1, b2, b3, float4(lpos, 1.0)).xyz;
lnorm = normalize(mul_cols_hlsl(b0, b1, b2, float4(0.0, 0.0, 0.0, 0.0), float4(lnorm, 0.0)).xyz);
ltan = normalize(mul_cols_hlsl(b0, b1, b2, float4(0.0, 0.0, 0.0, 0.0), float4(ltan, 0.0)).xyz);
}
float4 world4 = mul_cols_hlsl(inst.model_c0, inst.model_c1, inst.model_c2, inst.model_c3, float4(lpos, 1.0));
float3 n0 = cross(inst.model_c1.xyz, inst.model_c2.xyz);
float3 n1 = cross(inst.model_c2.xyz, inst.model_c0.xyz);
float3 n2 = cross(inst.model_c0.xyz, inst.model_c1.xyz);
float det = dot(inst.model_c0.xyz, n0);
float inv_det = abs(det) > 1.0e-7 ? (1.0 / det) : 1.0;
float3 wn = normalize((n0 * lnorm.x + n1 * lnorm.y + n2 * lnorm.z) * inv_det);
float3 wt = normalize(inst.model_c0.xyz * ltan.x + inst.model_c1.xyz * ltan.y + inst.model_c2.xyz * ltan.z);
PbrSceneVSOut o;
o.clip_pos = mul_cols_hlsl(g_vp_c0, g_vp_c1, g_vp_c2, g_vp_c3, float4(world4.xyz, 1.0));
o.world_pos = world4.xyz;
o.world_normal = wn;
o.world_tangent = float4(wt, tan_sign);
o.uv0 = uv0;
o.color0 = col0;
o.meta = uint4(inst.mat_skin_morph_flags.x, (pad_flags >> 8u) & 255u, g_batch_params.y & 255u, flags);
return o;
}
float4 PbrScenePS(PbrSceneVSOut inp) : SV_TARGET {
uint mat_idx = g_batch_params.w;
PbrMaterial mat = g_materials[mat_idx];
uint tex_mask = (g_batch_params.y >> 16u) & 31u;
float3 n_geom = normalize(inp.world_normal);
float3 v = normalize(g_camera_pos_exposure.xyz - inp.world_pos);
float3 sun_dir = normalize(g_sun_dir_fog.xyz);
uint debug_mode = g_batch_params.x;
if (debug_mode != 0u) {
float ndl_dbg = clamp(dot(n_geom, sun_dir) * 0.5 + 0.5, 0.22, 1.0);
float3 dbg_col = float3(0.8, 0.8, 0.8);
if (debug_mode == 1u) {
float h = frac((float)(inp.meta.y + mat_idx * 17u + 1u) * 0.6180339887);
float3 p = abs(frac(h + float3(1.0, 2.0 / 3.0, 1.0 / 3.0)) * 6.0 - 3.0);
dbg_col = lerp(float3(0.25, 0.25, 0.25), saturate(p - 1.0), 0.85);
} else if (debug_mode == 2u) {
uint lod = inp.meta.z;
if (lod == 0u) { dbg_col = float3(0.18, 0.85, 0.36); }
else if (lod == 1u) { dbg_col = float3(0.16, 0.72, 0.96); }
else if (lod == 2u) { dbg_col = float3(0.96, 0.68, 0.14); }
else { dbg_col = float3(0.92, 0.22, 0.24); }
} else {
uint mip_tier = (g_batch_params.y >> 8u) & 255u;
if (mip_tier == 0u) { dbg_col = float3(0.16, 0.88, 0.40); }
else if (mip_tier == 1u) { dbg_col = float3(0.98, 0.74, 0.16); }
else if (mip_tier == 2u) { dbg_col = float3(0.94, 0.24, 0.56); }
else { dbg_col = float3(0.32, 0.46, 0.68); }
}
return float4(pbr_srgb_enc_hlsl(dbg_col * ndl_dbg), 1.0);
}
float4 base_sample = float4(1.0, 1.0, 1.0, 1.0);
if ((tex_mask & 1u) != 0u) {
float4 rb = g_tex_base.Sample(g_pbr_sampler, inp.uv0);
base_sample = float4(pbr_srgb_dec_hlsl(rb.rgb), rb.a);
}
float4 base_rgba = mat.base_color_factor * base_sample * inp.color0;
if ((mat.flags_volume.x & 1u) != 0u && base_rgba.a < mat.emissive_alpha_cutoff.w) {
discard;
}
float metallic = saturate(mat.pbr_ior_specular.x);
float roughness = clamp(mat.pbr_ior_specular.y, 0.04, 1.0);
if ((tex_mask & 2u) != 0u) {
float4 mr = g_tex_mr.Sample(g_pbr_sampler, inp.uv0);
roughness = clamp(roughness * mr.g, 0.04, 1.0);
metallic = saturate(metallic * mr.b);
}
float3 n = n_geom;
if ((tex_mask & 4u) != 0u) {
float3 t = normalize(inp.world_tangent.xyz - n_geom * dot(n_geom, inp.world_tangent.xyz));
float3 b = cross(n_geom, t) * inp.world_tangent.w;
float3 n_ts = g_tex_normal.Sample(g_pbr_sampler, inp.uv0).xyz * 2.0 - 1.0;
n_ts.xy *= mat.clearcoat_normal_occ_trans.y;
n = normalize(t * n_ts.x + b * n_ts.y + n_geom * max(n_ts.z, 0.05));
}
float3 dndx = ddx(n);
float3 dndy = ddy(n);
float spec_var = clamp(0.25 * (dot(dndx, dndx) + dot(dndy, dndy)), 0.0, 0.18);
roughness = sqrt(clamp(roughness * roughness + spec_var, 0.002, 1.0));
float occ = 1.0;
if ((tex_mask & 8u) != 0u) {
occ = lerp(1.0, g_tex_occ.Sample(g_pbr_sampler, inp.uv0).r, mat.clearcoat_normal_occ_trans.z);
}
float3 emissive = mat.emissive_alpha_cutoff.xyz;
if ((tex_mask & 16u) != 0u) {
emissive *= pbr_srgb_dec_hlsl(g_tex_emissive.Sample(g_pbr_sampler, inp.uv0).rgb);
}
if ((mat.flags_volume.x & 8u) != 0u) {
float3 uh = (base_rgba.rgb + emissive) * g_camera_pos_exposure.w;
float3 um = (g_sky_zenith_agx.w >= 0.5) ? pbr_agx_hlsl(uh) : pbr_aces_hlsl(uh);
return float4(pbr_srgb_enc_hlsl(um), base_rgba.a);
}
float ior = max(mat.pbr_ior_specular.z, 1.0);
float f0_d = pow((ior - 1.0) / (ior + 1.0), 2.0) * mat.pbr_ior_specular.w;
float3 f0 = lerp(mat.specular_color_clearcoat.xyz * f0_d, base_rgba.rgb, metallic);
float3 lit = eval_direct_hlsl(
n, v, sun_dir, g_sun_color_ambient.xyz, base_rgba.rgb, metallic, roughness, f0,
mat.specular_color_clearcoat.w, mat.clearcoat_normal_occ_trans.x,
mat.sheen_color_roughness.xyz, mat.sheen_color_roughness.w
);
uint light_count = min(g_batch_params.z, 32u);
[loop]
for (uint li = 0u; li < light_count; ++li) {
PbrLocalLight pl = g_lights[li];
float3 to_l = pl.position_range.xyz - inp.world_pos;
float dist_sq = max(dot(to_l, to_l), 1.0e-4);
float dist = sqrt(dist_sq);
float ratio = saturate(dist / max(pl.position_range.w, 0.1));
float win = pow(max(1.0 - ratio * ratio * ratio * ratio, 0.0), 2.0);
float atten = (win / (dist_sq + 0.25)) * pl.color_intensity.w;
float3 ldir = to_l / dist;
if (pl.direction_kind.w > 0.5 && pl.direction_kind.w < 1.5) {
float cd = dot(-ldir, normalize(pl.direction_kind.xyz));
float spot = saturate((cd - pl.cone_size_params.y) / max(pl.cone_size_params.x - pl.cone_size_params.y, 1.0e-4));
atten *= spot * spot;
}
if (atten > 1.0e-4) {
lit += eval_direct_hlsl(
n, v, ldir, pl.color_intensity.xyz * atten, base_rgba.rgb, metallic, roughness, f0,
mat.specular_color_clearcoat.w, mat.clearcoat_normal_occ_trans.x,
mat.sheen_color_roughness.xyz, mat.sheen_color_roughness.w
);
}
}
float3 sh_irr = eval_sh9_hlsl(n);
float3 sky_amb = lerp(g_sky_horizon_tod.xyz, g_sky_zenith_agx.xyz, saturate(n.y * 0.5 + 0.5));
float3 diff_ibl = max(sh_irr, sky_amb * g_sun_color_ambient.w);
float3 refl = reflect(-v, n);
float3 env_spec = lerp(g_sky_horizon_tod.xyz, g_sky_zenith_agx.xyz, saturate(refl.y * 0.5 + 0.5));
float3 fres_env = f0 + (max(1.0 - roughness, f0) - f0) * pow(saturate(1.0 - max(dot(n, v), 0.0)), 5.0);
lit += ((1.0 - fres_env) * (1.0 - metallic) * base_rgba.rgb * diff_ibl + fres_env * env_spec * (0.45 + 0.55 * (1.0 - roughness))) * occ;
lit += emissive;
float cam_dist = length(g_camera_pos_exposure.xyz - inp.world_pos);
float fog_t = 1.0 - exp(-cam_dist * g_sun_dir_fog.w);
lit = lerp(lit, g_sky_horizon_tod.xyz, clamp(fog_t, 0.0, 0.85));
float3 hdr = lit * g_camera_pos_exposure.w;
float3 mapped = (g_sky_zenith_agx.w >= 0.5) ? pbr_agx_hlsl(hdr) : pbr_aces_hlsl(hdr);
return float4(pbr_srgb_enc_hlsl(mapped), base_rgba.a);
}
"#;
pub const WGSL_PBR_SCENE_SHADER: &str = r#"struct PbrSceneUniforms {
view_proj: mat4x4<f32>,
camera_pos_exposure: vec4<f32>,
sun_dir_fog: vec4<f32>,
sun_color_ambient: vec4<f32>,
sky_horizon_tod: vec4<f32>,
sky_zenith_agx: vec4<f32>,
ibl_sh: array<vec4<f32>, 9>,
batch_params: vec4<u32>,
draw_offsets: vec4<u32>,
};
struct PbrQuantizedVert {
pos_xy: u32,
pos_z_flags: u32,
norm_oct: u32,
tan_oct: u32,
uv0_f16: u32,
color0_rgba8: u32,
};
struct PbrInstance {
model_c0: vec4<f32>,
model_c1: vec4<f32>,
model_c2: vec4<f32>,
model_c3: vec4<f32>,
quant_origin_id: vec4<f32>,
quant_extent_lod: vec4<f32>,
mat_skin_morph_flags: vec4<u32>,
};
@group(0) @binding(0) var<uniform> u_pbr: PbrSceneUniforms;
@group(0) @binding(1) var<storage, read> s_verts: array<PbrQuantizedVert>;
@group(0) @binding(2) var<storage, read> s_instances: array<PbrInstance>;
struct PbrVsOut {
@builtin(position) clip_pos: vec4<f32>,
@location(0) world_pos: vec3<f32>,
@location(1) world_n: vec3<f32>,
@location(2) @interpolate(flat) meshlet_tag: u32,
};
fn oct_decode_wgsl(e: vec2<f32>) -> vec3<f32> {
var v = vec3<f32>(e.x, e.y, 1.0 - abs(e.x) - abs(e.y));
if (v.z < 0.0) {
let sx = select(-1.0, 1.0, v.x >= 0.0);
let sy = select(-1.0, 1.0, v.y >= 0.0);
v = vec3<f32>((1.0 - abs(v.y)) * sx, (1.0 - abs(v.x)) * sy, v.z);
}
return normalize(v);
}
@vertex
fn vs_pbr_scene(@builtin(vertex_index) vid: u32, @builtin(instance_index) iid: u32) -> PbrVsOut {
let qv = s_verts[vid];
let inst = s_instances[iid];
let q = vec3<f32>(f32(qv.pos_xy & 0xFFFFu), f32(qv.pos_xy >> 16u), f32(qv.pos_z_flags & 0xFFFFu)) / 65535.0;
let lp = inst.quant_origin_id.xyz + q * inst.quant_extent_lod.xyz;
let nx = f32(i32(qv.norm_oct << 16u) >> 16) / 32767.0;
let ny = f32(i32(qv.norm_oct) >> 16) / 32767.0;
let ln = oct_decode_wgsl(vec2<f32>(nx, ny));
let wp = inst.model_c0 * lp.x + inst.model_c1 * lp.y + inst.model_c2 * lp.z + inst.model_c3;
let wn = normalize((inst.model_c0 * ln.x + inst.model_c1 * ln.y + inst.model_c2 * ln.z).xyz);
var out_v: PbrVsOut;
out_v.clip_pos = u_pbr.view_proj * vec4<f32>(wp.xyz, 1.0);
out_v.world_pos = wp.xyz;
out_v.world_n = wn;
out_v.meshlet_tag = (qv.pos_z_flags >> 24u) & 0xFFu;
return out_v;
}
@fragment
fn fs_pbr_scene(in_f: PbrVsOut) -> @location(0) vec4<f32> {
let n = normalize(in_f.world_n);
let dndx = dpdx(n);
let dndy = dpdy(n);
let variance = clamp(dot(dndx, dndx) + dot(dndy, dndy), 0.0, 0.36);
let rough = sqrt(clamp(0.16 + variance, 0.0025, 1.0));
let l = normalize(u_pbr.sun_dir_fog.xyz);
let ndl = max(dot(n, l), 0.0);
let ibl = max(u_pbr.ibl_sh[0].xyz + u_pbr.ibl_sh[1].xyz * n.y, vec3<f32>(0.02));
let hdr = (ibl + u_pbr.sun_color_ambient.xyz * ndl * (1.1 - rough * 0.4)) * u_pbr.camera_pos_exposure.w;
let mapped = clamp((hdr * (2.51 * hdr + 0.03)) / (hdr * (2.43 * hdr + 0.59) + 0.14), vec3<f32>(0.0), vec3<f32>(1.0));
return vec4<f32>(mapped, 1.0);
}
"#;
pub(crate) const RDG_RES_DEFORMED: &str = "deformed_vertices";
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_MORPH_SKIN: &str = "compute_morph_skin";
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_TRANSMISSION: &str = "transmission_alpha";
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";