Documentation
¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
var Env = &gpu.Shader{ Name: "env", WGSL: `// The environment of viewport3d's image-based lighting (SPEC §28.5), made by compute passes: the neutral default sky, the // prefiltered specular levels (GGX, importance sampled; one level for each roughness) and the diffuse irradiance. Each entry // point writes one level of a cube, as a texture array of six layers (a storage binding cannot take a cube), which the renderer // copies into the cube that the material samples. // // A texel of a face is a direction by the table of faces of OpenGL, Vulkan, Direct3D and WebGPU, which the hardware uses to // sample a cube: +x, -x, +y, -y, +z, -z are the layers 0 to 5. struct Params { // size: x the size of the level that is written, y the roughness, z the number of samples, w the size of the source's first level. size: vec4<f32>, // options: x 1 when the source is 8 bit sRGB, y the strength of the sky. options: vec4<f32>, } @group(0) @binding(0) var<uniform> params: Params; @group(0) @binding(1) var src: texture_cube<f32>; @group(0) @binding(2) var samp: sampler; @group(0) @binding(3) var dst: texture_storage_2d_array<rgba16float, write>; const PI: f32 = 3.14159265358979; // face_direction is the direction of the texel (x, y) of a face of a cube of the given size. (The texel comes as i32: Wine's // d3dcompiler_47 turns f32 of a component of the thread's id, a u32, into a wrong value, and f32 of an i32 into the right one.) fn face_direction(face: u32, x: i32, y: i32, size: f32) -> vec3<f32> { let u = 2.0 * (f32(x) + 0.5) / size - 1.0; let v = 2.0 * (f32(y) + 0.5) / size - 1.0; var d = vec3<f32>(0.0); switch face { case 0u: { d = vec3<f32>(1.0, -v, -u); } case 1u: { d = vec3<f32>(-1.0, -v, u); } case 2u: { d = vec3<f32>(u, 1.0, v); } case 3u: { d = vec3<f32>(u, -1.0, -v); } case 4u: { d = vec3<f32>(u, -v, 1.0); } default: { d = vec3<f32>(-u, -v, -1.0); } } return normalize(d); } fn srgb_to_linear(c: vec3<f32>) -> vec3<f32> { let lo = c / 12.92; let hi = pow((c + vec3<f32>(0.055)) / 1.055, vec3<f32>(2.4)); return select(hi, lo, c <= vec3<f32>(0.04045)); } fn source(d: vec3<f32>, lod: f32) -> vec3<f32> { var c = textureSampleLevel(src, samp, d, lod).rgb; if (params.options.x > 0.5) { c = srgb_to_linear(c); } return c; } // radical_inverse is the Van der Corput sequence of i, and hammersley the point i of n of the Hammersley set. fn hammersley(i: u32, n: u32) -> vec2<f32> { var bits = i; bits = (bits << 16u) | (bits >> 16u); bits = ((bits & 0x55555555u) << 1u) | ((bits & 0xAAAAAAAAu) >> 1u); bits = ((bits & 0x33333333u) << 2u) | ((bits & 0xCCCCCCCCu) >> 2u); bits = ((bits & 0x0F0F0F0Fu) << 4u) | ((bits & 0xF0F0F0F0u) >> 4u); bits = ((bits & 0x00FF00FFu) << 8u) | ((bits & 0xFF00FF00u) >> 8u); return vec2<f32>(f32(i) / f32(n), f32(bits) * 2.3283064365386963e-10); } // The sky is a neutral studio: a gradient from a dark floor through a bright horizon to a pale zenith, a large soft box that // is the key light, and a weaker fill from the other side. It is in linear units (the box is far above 1: the specular // highlights of a smooth surface show it). fn sky(d: vec3<f32>) -> vec3<f32> { let up = d.y; let floor_color = vec3<f32>(0.10, 0.10, 0.11); let horizon = vec3<f32>(0.62, 0.64, 0.67); let zenith = vec3<f32>(0.75, 0.80, 0.90); var c = mix(horizon, zenith, smoothstep(0.0, 1.0, up)); // (smoothstep needs its first edge below its second one: the result is not defined otherwise, and Direct3D gives NaNs.) c = mix(c, floor_color, 1.0 - smoothstep(-0.45, 0.0, up)); let key_dir = normalize(vec3<f32>(0.55, 0.75, 0.45)); let key = pow(max(dot(d, key_dir), 0.0), 24.0); let fill_dir = normalize(vec3<f32>(-0.7, 0.35, -0.5)); let fill = pow(max(dot(d, fill_dir), 0.0), 10.0); return (c + vec3<f32>(1.0, 0.97, 0.92) * key * 6.0 + vec3<f32>(0.75, 0.85, 1.0) * fill * 0.8) * params.options.y; } // cs_sky writes the default sky. @compute @workgroup_size(8, 8, 1) fn cs_sky(@builtin(global_invocation_id) id: vec3<u32>) { let size = u32(params.size.x); if (id.x >= size || id.y >= size) { return; } let d = face_direction(id.z, i32(id.x), i32(id.y), params.size.x); textureStore(dst, vec2<i32>(i32(id.x), i32(id.y)), i32(id.z), vec4<f32>(sky(d), 1.0)); } // cs_specular prefilters the source for the roughness of the params: GGX importance sampling with the view and the normal // both along the direction of the texel, and the level of the source taken from the solid angle of a sample (Krivanek and // Colbert, "Real-time Shading with Filtered Importance Sampling"). @compute @workgroup_size(8, 8, 1) fn cs_specular(@builtin(global_invocation_id) id: vec3<u32>) { let size = u32(params.size.x); if (id.x >= size || id.y >= size) { return; } let n = face_direction(id.z, i32(id.x), i32(id.y), params.size.x); let roughness = params.size.y; let count = u32(params.size.z); let a = roughness * roughness; let a2 = a * a; var up = vec3<f32>(0.0, 1.0, 0.0); if (abs(n.y) > 0.999) { up = vec3<f32>(1.0, 0.0, 0.0); } let tx = normalize(cross(up, n)); let ty = cross(n, tx); let texel_solid_angle = 4.0 * PI / (6.0 * params.size.w * params.size.w); if (a2 < 1.0e-6) { // A mirror: the source as it is. textureStore(dst, vec2<i32>(i32(id.x), i32(id.y)), i32(id.z), vec4<f32>(source(n, 0.0), 1.0)); return; } var color = vec3<f32>(0.0); var weight = 0.0; for (var i = 0u; i < count; i = i + 1u) { let xi = hammersley(i, count); let phi = 2.0 * PI * xi.x; let cos_t = sqrt((1.0 - xi.y) / (1.0 + (a2 - 1.0) * xi.y)); let sin_t = sqrt(max(1.0 - cos_t * cos_t, 0.0)); let h = tx * (sin_t * cos(phi)) + ty * (sin_t * sin(phi)) + n * cos_t; let l = 2.0 * dot(n, h) * h - n; let nl = dot(n, l); if (nl > 0.0) { let nh = max(dot(n, h), 0.0); let d = nh * nh * (a2 - 1.0) + 1.0; let pdf = a2 / (PI * d * d) / 4.0; let sample_solid_angle = 1.0 / (f32(count) * pdf + 1.0e-4); let lod = max(0.5 * log2(sample_solid_angle / texel_solid_angle) + 1.0, 0.0); color = color + source(l, lod) * nl; weight = weight + nl; } } textureStore(dst, vec2<i32>(i32(id.x), i32(id.y)), i32(id.z), vec4<f32>(color / max(weight, 1.0e-4), 1.0)); } // cs_irradiance convolves the source with the cosine over the hemisphere of each texel: the irradiance divided by pi. @compute @workgroup_size(8, 8, 1) fn cs_irradiance(@builtin(global_invocation_id) id: vec3<u32>) { let size = u32(params.size.x); if (id.x >= size || id.y >= size) { return; } let n = face_direction(id.z, i32(id.x), i32(id.y), params.size.x); var up = vec3<f32>(0.0, 1.0, 0.0); if (abs(n.y) > 0.999) { up = vec3<f32>(1.0, 0.0, 0.0); } let tx = normalize(cross(up, n)); let ty = cross(n, tx); let steps = u32(params.size.z); let dphi = 2.0 * PI / f32(steps); let dtheta = 0.5 * PI / f32(steps / 2u); var sum = vec3<f32>(0.0); var total = 0.0; for (var i = 0u; i < steps; i = i + 1u) { let phi = (f32(i) + 0.5) * dphi; for (var j = 0u; j < steps / 2u; j = j + 1u) { let theta = (f32(j) + 0.5) * dtheta; let d = tx * (sin(theta) * cos(phi)) + ty * (sin(theta) * sin(phi)) + n * cos(theta); let w = cos(theta) * sin(theta); // The irradiance is smooth: the source is read from a coarse level. sum = sum + source(d, 3.0) * w; total = total + w; } } textureStore(dst, vec2<i32>(i32(id.x), i32(id.y)), i32(id.z), vec4<f32>(sum / total, 1.0)); } `, SPIRV: spirvEnv, HLSL: `struct Params { float4 size; float4 options; }; static const float PI = 3.1415927; cbuffer params : register(b0, space0) { Params params; } TextureCube<float4> src : register(t1, space0); SamplerState samp : register(s2, space0); RWTexture2DArray<float4> dst_ : register(u3, space0); float3 face_direction(uint face, int x, int y, float size) { float3 d = (0.0).xxx; float u = (((2.0 * (float(x) + 0.5)) / size) - 1.0); float v = (((2.0 * (float(y) + 0.5)) / size) - 1.0); switch(face) { case 0u: { d = float3(1.0, -(v), -(u)); break; } case 1u: { d = float3(-1.0, -(v), u); break; } case 2u: { d = float3(u, 1.0, v); break; } case 3u: { d = float3(u, -1.0, -(v)); break; } case 4u: { d = float3(u, -(v), 1.0); break; } default: { d = float3(-(u), -(v), -1.0); break; } } float3 _e42 = d; return normalize(_e42); } float3 srgb_to_linear(float3 c) { float3 lo = (c / (12.92).xxx); float3 hi = pow(((c + (0.055).xxx) / (1.055).xxx), (2.4).xxx); return ((c <= (0.04045).xxx) ? lo : hi); } float3 source(float3 d_1, float lod) { float3 c_1 = (float3)0; float4 _e4 = src.SampleLevel(samp, d_1, lod); c_1 = _e4.xyz; float _e10 = params.options.x; if ((_e10 > 0.5)) { float3 _e13 = c_1; const float3 _e14 = srgb_to_linear(_e13); c_1 = _e14; } float3 _e15 = c_1; return _e15; } float2 hammersley(uint i_2, uint n) { uint bits = (uint)0; bits = i_2; uint _e3 = bits; uint _e6 = bits; bits = ((_e3 << 16u) | (_e6 >> 16u)); uint _e10 = bits; uint _e15 = bits; bits = (((_e10 & 1431655765u) << 1u) | ((_e15 & 2863311530u) >> 1u)); uint _e21 = bits; uint _e26 = bits; bits = (((_e21 & 858993459u) << 2u) | ((_e26 & 3435973836u) >> 2u)); uint _e32 = bits; uint _e37 = bits; bits = (((_e32 & 252645135u) << 4u) | ((_e37 & 4042322160u) >> 4u)); uint _e43 = bits; uint _e48 = bits; bits = (((_e43 & 16711935u) << 8u) | ((_e48 & 4278255360u) >> 8u)); uint _e57 = bits; return float2((float(i_2) / float(n)), (float(_e57) * 2.3283064e-10)); } float3 sky(float3 d_2) { float3 c_2 = (float3)0; float up_2 = d_2.y; float3 floor_color = float3(0.1, 0.1, 0.11); float3 horizon = float3(0.62, 0.64, 0.67); float3 zenith = float3(0.75, 0.8, 0.9); c_2 = lerp(horizon, zenith, smoothstep(0.0, 1.0, up_2)); float3 _e19 = c_2; c_2 = lerp(_e19, floor_color, (1.0 - smoothstep(-0.45, 0.0, up_2))); float3 key_dir = float3(0.5323273, 0.7259008, 0.4355405); float key = pow(max(dot(d_2, key_dir), 0.0), 24.0); float3 fill_dir = float3(-0.7537347, 0.37686735, -0.53838193); float fill = pow(max(dot(d_2, fill_dir), 0.0), 10.0); float3 _e44 = c_2; float _e64 = params.options.y; return (((_e44 + ((float3(1.0, 0.97, 0.92) * key) * 6.0)) + ((float3(0.75, 0.85, 1.0) * fill) * 0.8)) * _e64); } uint naga_f2u32(float value) { return uint(clamp(value, 0.0, 4294967000.0)); } [numthreads(8, 8, 1)] void cs_sky(uint3 id : SV_DispatchThreadID) { bool local = (bool)0; float _e4 = params.size.x; uint size_1 = naga_f2u32(_e4); if (!((id.x >= size_1))) { local = (id.y >= size_1); } else { local = true; } bool _e14 = local; if (_e14) { return; } float _e23 = params.size.x; const float3 _e24 = face_direction(id.z, int(id.x), int(id.y), _e23); const float3 _e33 = sky(_e24); dst_[int3(int2(int(id.x), int(id.y)), int(id.z))] = float4(_e33, 1.0); return; } [numthreads(8, 8, 1)] void cs_specular(uint3 id_1 : SV_DispatchThreadID) { bool local_1 = (bool)0; float3 up = float3(0.0, 1.0, 0.0); float3 color = (0.0).xxx; float weight = 0.0; uint i = 0u; float _e4 = params.size.x; uint size_2 = naga_f2u32(_e4); if (!((id_1.x >= size_2))) { local_1 = (id_1.y >= size_2); } else { local_1 = true; } bool _e14 = local_1; if (_e14) { return; } float _e23 = params.size.x; const float3 _e24 = face_direction(id_1.z, int(id_1.x), int(id_1.y), _e23); float roughness = params.size.y; float _e32 = params.size.z; uint count = naga_f2u32(_e32); float a = (roughness * roughness); float a2_ = (a * a); if ((abs(_e24.y) > 0.999)) { up = float3(1.0, 0.0, 0.0); } float3 _e49 = up; float3 tx = normalize(cross(_e49, _e24)); float3 ty = cross(_e24, tx); float _e57 = params.size.w; float _e63 = params.size.w; float texel_solid_angle = (12.566371 / ((6.0 * _e57) * _e63)); if ((a2_ < 1e-6)) { const float3 _e77 = source(_e24, 0.0); dst_[int3(int2(int(id_1.x), int(id_1.y)), int(id_1.z))] = float4(_e77, 1.0); return; } uint2 loop_bound = uint2(4294967295u, 4294967295u); bool loop_init = true; while(true) { if (all(loop_bound == uint2(0u, 0u))) { break; } loop_bound -= uint2(loop_bound.y == 0u, 1u); if (!loop_init) { uint _e163 = i; i = (_e163 + 1u); } loop_init = false; uint _e87 = i; if ((_e87 < count)) { } else { break; } { uint _e89 = i; const float2 _e90 = hammersley(_e89, count); float phi = (6.2831855 * _e90.x); float cos_t = sqrt(((1.0 - _e90.y) / (1.0 + ((a2_ - 1.0) * _e90.y)))); float sin_t = sqrt(max((1.0 - (cos_t * cos_t)), 0.0)); float3 h = (((tx * (sin_t * cos(phi))) + (ty * (sin_t * sin(phi)))) + (_e24 * cos_t)); float3 l = (((2.0 * dot(_e24, h)) * h) - _e24); float nl = dot(_e24, l); if ((nl > 0.0)) { float nh = max(dot(_e24, h), 0.0); float d_3 = (((nh * nh) * (a2_ - 1.0)) + 1.0); float pdf = ((a2_ / ((PI * d_3) * d_3)) / 4.0); float sample_solid_angle = (1.0 / ((float(count) * pdf) + 0.0001)); float lod_1 = max(((0.5 * log2((sample_solid_angle / texel_solid_angle))) + 1.0), 0.0); float3 _e157 = color; const float3 _e158 = source(l, lod_1); color = (_e157 + (_e158 * nl)); float _e161 = weight; weight = (_e161 + nl); } } } float3 _e174 = color; float _e175 = weight; dst_[int3(int2(int(id_1.x), int(id_1.y)), int(id_1.z))] = float4((_e174 / (max(_e175, 0.0001)).xxx), 1.0); return; } uint naga_div(uint lhs, uint rhs) { return lhs / (rhs == 0u ? 1u : rhs); } [numthreads(8, 8, 1)] void cs_irradiance(uint3 id_2 : SV_DispatchThreadID) { bool local_2 = (bool)0; float3 up_1 = float3(0.0, 1.0, 0.0); float3 sum = (0.0).xxx; float total = 0.0; uint i_1 = 0u; uint j = (uint)0; float _e4 = params.size.x; uint size_3 = naga_f2u32(_e4); if (!((id_2.x >= size_3))) { local_2 = (id_2.y >= size_3); } else { local_2 = true; } bool _e14 = local_2; if (_e14) { return; } float _e23 = params.size.x; const float3 _e24 = face_direction(id_2.z, int(id_2.x), int(id_2.y), _e23); if ((abs(_e24.y) > 0.999)) { up_1 = float3(1.0, 0.0, 0.0); } float3 _e38 = up_1; float3 tx_1 = normalize(cross(_e38, _e24)); float3 ty_1 = cross(_e24, tx_1); float _e45 = params.size.z; uint steps = naga_f2u32(_e45); float dphi = (6.2831855 / float(steps)); float dtheta = (1.5707964 / float(naga_div(steps, 2u))); uint2 loop_bound_1 = uint2(4294967295u, 4294967295u); bool loop_init_1 = true; while(true) { if (all(loop_bound_1 == uint2(0u, 0u))) { break; } loop_bound_1 -= uint2(loop_bound_1.y == 0u, 1u); if (!loop_init_1) { uint _e105 = i_1; i_1 = (_e105 + 1u); } loop_init_1 = false; uint _e62 = i_1; if ((_e62 < steps)) { } else { break; } { uint _e64 = i_1; float phi_1 = ((float(_e64) + 0.5) * dphi); j = 0u; uint2 loop_bound_2 = uint2(4294967295u, 4294967295u); bool loop_init_2 = true; while(true) { if (all(loop_bound_2 == uint2(0u, 0u))) { break; } loop_bound_2 -= uint2(loop_bound_2.y == 0u, 1u); if (!loop_init_2) { uint _e102 = j; j = (_e102 + 1u); } loop_init_2 = false; uint _e71 = j; if ((_e71 < naga_div(steps, 2u))) { } else { break; } { uint _e75 = j; float theta = ((float(_e75) + 0.5) * dtheta); float3 d_4 = (((tx_1 * (sin(theta) * cos(phi_1))) + (ty_1 * (sin(theta) * sin(phi_1)))) + (_e24 * cos(theta))); float w = (cos(theta) * sin(theta)); float3 _e95 = sum; const float3 _e97 = source(d_4, 3.0); sum = (_e95 + (_e97 * w)); float _e100 = total; total = (_e100 + w); } } } } float3 _e116 = sum; float _e117 = total; dst_[int3(int2(int(id_2.x), int(id_2.y)), int(id_2.z))] = float4((_e116 / (_e117).xxx), 1.0); return; } `, MSL: `// language: metal2.4 #include <metal_stdlib> #include <simd/simd.h> using metal::uint; struct Params { metal::float4 size; metal::float4 options; }; constant float PI = 3.1415927; metal::float3 face_direction( uint face, int x, int y, float size ) { metal::float3 d = metal::float3(0.0); float u = ((2.0 * (static_cast<float>(x) + 0.5)) / size) - 1.0; float v = ((2.0 * (static_cast<float>(y) + 0.5)) / size) - 1.0; switch(face) { case 0u: { d = metal::float3(1.0, -(v), -(u)); break; } case 1u: { d = metal::float3(-1.0, -(v), u); break; } case 2u: { d = metal::float3(u, 1.0, v); break; } case 3u: { d = metal::float3(u, -1.0, -(v)); break; } case 4u: { d = metal::float3(u, -(v), 1.0); break; } default: { d = metal::float3(-(u), -(v), -1.0); break; } } metal::float3 _e42 = d; return metal::normalize(_e42); } metal::float3 srgb_to_linear( metal::float3 c ) { metal::float3 lo = c / metal::float3(12.92); metal::float3 hi = metal::pow((c + metal::float3(0.055)) / metal::float3(1.055), metal::float3(2.4)); return metal::select(hi, lo, c <= metal::float3(0.04045)); } metal::float3 source( metal::float3 d_1, float lod, constant Params& params, metal::texturecube<float, metal::access::sample> src, metal::sampler samp ) { metal::float3 c_1 = {}; metal::float4 _e4 = src.sample(samp, d_1, metal::level(lod)); c_1 = _e4.xyz; float _e10 = params.options.x; if (_e10 > 0.5) { metal::float3 _e13 = c_1; metal::float3 _e14 = srgb_to_linear(_e13); c_1 = _e14; } metal::float3 _e15 = c_1; return _e15; } metal::float2 hammersley( uint i_2, uint n ) { uint bits = {}; bits = i_2; uint _e3 = bits; uint _e6 = bits; bits = (_e3 << 16u) | (_e6 >> 16u); uint _e10 = bits; uint _e15 = bits; bits = ((_e10 & 1431655765u) << 1u) | ((_e15 & 2863311530u) >> 1u); uint _e21 = bits; uint _e26 = bits; bits = ((_e21 & 858993459u) << 2u) | ((_e26 & 3435973836u) >> 2u); uint _e32 = bits; uint _e37 = bits; bits = ((_e32 & 252645135u) << 4u) | ((_e37 & 4042322160u) >> 4u); uint _e43 = bits; uint _e48 = bits; bits = ((_e43 & 16711935u) << 8u) | ((_e48 & 4278255360u) >> 8u); uint _e57 = bits; return metal::float2(static_cast<float>(i_2) / static_cast<float>(n), static_cast<float>(_e57) * 0.00000000023283064); } metal::float3 sky( metal::float3 d_2, constant Params& params ) { metal::float3 c_2 = {}; float up_2 = d_2.y; metal::float3 floor_color = metal::float3(0.1, 0.1, 0.11); metal::float3 horizon = metal::float3(0.62, 0.64, 0.67); metal::float3 zenith = metal::float3(0.75, 0.8, 0.9); c_2 = metal::mix(horizon, zenith, metal::smoothstep(0.0, 1.0, up_2)); metal::float3 _e19 = c_2; c_2 = metal::mix(_e19, floor_color, 1.0 - metal::smoothstep(-0.45, 0.0, up_2)); metal::float3 key_dir = metal::float3(0.5323273, 0.7259008, 0.4355405); float key = metal::pow(metal::max(metal::dot(d_2, key_dir), 0.0), 24.0); metal::float3 fill_dir = metal::float3(-0.7537347, 0.37686735, -0.53838193); float fill = metal::pow(metal::max(metal::dot(d_2, fill_dir), 0.0), 10.0); metal::float3 _e44 = c_2; float _e64 = params.options.y; return ((_e44 + ((metal::float3(1.0, 0.97, 0.92) * key) * 6.0)) + ((metal::float3(0.75, 0.85, 1.0) * fill) * 0.8)) * _e64; } uint naga_f2u32(float value) { return static_cast<uint>(metal::clamp(value, 0.0, 4294967000.0)); } struct cs_skyInput { }; [[max_total_threads_per_threadgroup(64)]] kernel void cs_sky( metal::uint3 id [[thread_position_in_grid]] , constant Params& params [[buffer(0)]] , metal::texture2d_array<float, metal::access::write> dst [[texture(1)]] ) { bool local = {}; float _e4 = params.size.x; uint size_1 = naga_f2u32(_e4); if (!((id.x >= size_1))) { local = id.y >= size_1; } else { local = true; } bool _e14 = local; if (_e14) { return; } float _e23 = params.size.x; metal::float3 _e24 = face_direction(id.z, static_cast<int>(id.x), static_cast<int>(id.y), _e23); metal::float3 _e33 = sky(_e24, params); dst.write(metal::float4(_e33, 1.0), metal::uint2(metal::int2(static_cast<int>(id.x), static_cast<int>(id.y))), static_cast<int>(id.z)); return; } struct cs_specularInput { }; [[max_total_threads_per_threadgroup(64)]] kernel void cs_specular( metal::uint3 id_1 [[thread_position_in_grid]] , constant Params& params [[buffer(0)]] , metal::texturecube<float, metal::access::sample> src [[texture(0)]] , metal::sampler samp [[sampler(0)]] , metal::texture2d_array<float, metal::access::write> dst [[texture(1)]] ) { bool local_1 = {}; metal::float3 up = metal::float3(0.0, 1.0, 0.0); metal::float3 color = metal::float3(0.0); float weight = 0.0; uint i = 0u; float _e4 = params.size.x; uint size_2 = naga_f2u32(_e4); if (!((id_1.x >= size_2))) { local_1 = id_1.y >= size_2; } else { local_1 = true; } bool _e14 = local_1; if (_e14) { return; } float _e23 = params.size.x; metal::float3 _e24 = face_direction(id_1.z, static_cast<int>(id_1.x), static_cast<int>(id_1.y), _e23); float roughness = params.size.y; float _e32 = params.size.z; uint count = naga_f2u32(_e32); float a = roughness * roughness; float a2_ = a * a; if (metal::abs(_e24.y) > 0.999) { up = metal::float3(1.0, 0.0, 0.0); } metal::float3 _e49 = up; metal::float3 tx = metal::normalize(metal::cross(_e49, _e24)); metal::float3 ty = metal::cross(_e24, tx); float _e57 = params.size.w; float _e63 = params.size.w; float texel_solid_angle = 12.566371 / ((6.0 * _e57) * _e63); if (a2_ < 0.000001) { metal::float3 _e77 = source(_e24, 0.0, params, src, samp); dst.write(metal::float4(_e77, 1.0), metal::uint2(metal::int2(static_cast<int>(id_1.x), static_cast<int>(id_1.y))), static_cast<int>(id_1.z)); return; } uint2 loop_bound = uint2(4294967295u); bool loop_init = true; while(true) { if (metal::all(loop_bound == uint2(0u))) { break; } loop_bound -= uint2(loop_bound.y == 0u, 1u); if (!loop_init) { uint _e163 = i; i = _e163 + 1u; } loop_init = false; uint _e87 = i; if (_e87 < count) { } else { break; } { uint _e89 = i; metal::float2 _e90 = hammersley(_e89, count); float phi = 6.2831855 * _e90.x; float cos_t = metal::sqrt((1.0 - _e90.y) / (1.0 + ((a2_ - 1.0) * _e90.y))); float sin_t = metal::sqrt(metal::max(1.0 - (cos_t * cos_t), 0.0)); metal::float3 h = ((tx * (sin_t * metal::cos(phi))) + (ty * (sin_t * metal::sin(phi)))) + (_e24 * cos_t); metal::float3 l = ((2.0 * metal::dot(_e24, h)) * h) - _e24; float nl = metal::dot(_e24, l); if (nl > 0.0) { float nh = metal::max(metal::dot(_e24, h), 0.0); float d_3 = ((nh * nh) * (a2_ - 1.0)) + 1.0; float pdf = (a2_ / ((PI * d_3) * d_3)) / 4.0; float sample_solid_angle = 1.0 / ((static_cast<float>(count) * pdf) + 0.0001); float lod_1 = metal::max((0.5 * metal::log2(sample_solid_angle / texel_solid_angle)) + 1.0, 0.0); metal::float3 _e157 = color; metal::float3 _e158 = source(l, lod_1, params, src, samp); color = _e157 + (_e158 * nl); float _e161 = weight; weight = _e161 + nl; } } } metal::float3 _e174 = color; float _e175 = weight; dst.write(metal::float4(_e174 / metal::float3(metal::max(_e175, 0.0001)), 1.0), metal::uint2(metal::int2(static_cast<int>(id_1.x), static_cast<int>(id_1.y))), static_cast<int>(id_1.z)); return; } uint naga_div(uint lhs, uint rhs) { return lhs / metal::select(rhs, 1u, rhs == 0u); } struct cs_irradianceInput { }; [[max_total_threads_per_threadgroup(64)]] kernel void cs_irradiance( metal::uint3 id_2 [[thread_position_in_grid]] , constant Params& params [[buffer(0)]] , metal::texturecube<float, metal::access::sample> src [[texture(0)]] , metal::sampler samp [[sampler(0)]] , metal::texture2d_array<float, metal::access::write> dst [[texture(1)]] ) { bool local_2 = {}; metal::float3 up_1 = metal::float3(0.0, 1.0, 0.0); metal::float3 sum = metal::float3(0.0); float total = 0.0; uint i_1 = 0u; uint j = {}; float _e4 = params.size.x; uint size_3 = naga_f2u32(_e4); if (!((id_2.x >= size_3))) { local_2 = id_2.y >= size_3; } else { local_2 = true; } bool _e14 = local_2; if (_e14) { return; } float _e23 = params.size.x; metal::float3 _e24 = face_direction(id_2.z, static_cast<int>(id_2.x), static_cast<int>(id_2.y), _e23); if (metal::abs(_e24.y) > 0.999) { up_1 = metal::float3(1.0, 0.0, 0.0); } metal::float3 _e38 = up_1; metal::float3 tx_1 = metal::normalize(metal::cross(_e38, _e24)); metal::float3 ty_1 = metal::cross(_e24, tx_1); float _e45 = params.size.z; uint steps = naga_f2u32(_e45); float dphi = 6.2831855 / static_cast<float>(steps); float dtheta = 1.5707964 / static_cast<float>(naga_div(steps, 2u)); uint2 loop_bound_1 = uint2(4294967295u); bool loop_init_1 = true; while(true) { if (metal::all(loop_bound_1 == uint2(0u))) { break; } loop_bound_1 -= uint2(loop_bound_1.y == 0u, 1u); if (!loop_init_1) { uint _e105 = i_1; i_1 = _e105 + 1u; } loop_init_1 = false; uint _e62 = i_1; if (_e62 < steps) { } else { break; } { uint _e64 = i_1; float phi_1 = (static_cast<float>(_e64) + 0.5) * dphi; j = 0u; uint2 loop_bound_2 = uint2(4294967295u); bool loop_init_2 = true; while(true) { if (metal::all(loop_bound_2 == uint2(0u))) { break; } loop_bound_2 -= uint2(loop_bound_2.y == 0u, 1u); if (!loop_init_2) { uint _e102 = j; j = _e102 + 1u; } loop_init_2 = false; uint _e71 = j; if (_e71 < naga_div(steps, 2u)) { } else { break; } { uint _e75 = j; float theta = (static_cast<float>(_e75) + 0.5) * dtheta; metal::float3 d_4 = ((tx_1 * (metal::sin(theta) * metal::cos(phi_1))) + (ty_1 * (metal::sin(theta) * metal::sin(phi_1)))) + (_e24 * metal::cos(theta)); float w = metal::cos(theta) * metal::sin(theta); metal::float3 _e95 = sum; metal::float3 _e97 = source(d_4, 3.0, params, src, samp); sum = _e95 + (_e97 * w); float _e100 = total; total = _e100 + w; } } } } metal::float3 _e116 = sum; float _e117 = total; dst.write(metal::float4(_e116 / metal::float3(_e117), 1.0), metal::uint2(metal::int2(static_cast<int>(id_2.x), static_cast<int>(id_2.y))), static_cast<int>(id_2.z)); return; } `, Entries: []gpu.EntryPoint{ {Name: "cs_sky", Stage: gpu.ShaderStageCompute, WorkgroupSize: [3]int{8, 8, 1}, Bindings: []gpu.ShaderBinding{ {Name: "params", Group: 0, Binding: 0, Kind: gpu.BindingUniformBuffer, Size: 32, Register: gpu.ShaderRegister{Class: "b", Space: 0, Index: 0}, MetalIndex: 0}, {Name: "dst", Group: 0, Binding: 3, Kind: gpu.BindingStorageTexture, Dimension: gpu.TextureDim2DArray, StorageFormat: "rgba16float", Register: gpu.ShaderRegister{Class: "u", Space: 0, Index: 3}, MetalIndex: 1}, }, }, {Name: "cs_specular", Stage: gpu.ShaderStageCompute, WorkgroupSize: [3]int{8, 8, 1}, Bindings: []gpu.ShaderBinding{ {Name: "params", Group: 0, Binding: 0, Kind: gpu.BindingUniformBuffer, Size: 32, Register: gpu.ShaderRegister{Class: "b", Space: 0, Index: 0}, MetalIndex: 0}, {Name: "src", Group: 0, Binding: 1, Kind: gpu.BindingSampledTexture, Dimension: gpu.TextureDimCube, Register: gpu.ShaderRegister{Class: "t", Space: 0, Index: 1}, MetalIndex: 0}, {Name: "samp", Group: 0, Binding: 2, Kind: gpu.BindingSampler, Register: gpu.ShaderRegister{Class: "s", Space: 0, Index: 2}, MetalIndex: 0}, {Name: "dst", Group: 0, Binding: 3, Kind: gpu.BindingStorageTexture, Dimension: gpu.TextureDim2DArray, StorageFormat: "rgba16float", Register: gpu.ShaderRegister{Class: "u", Space: 0, Index: 3}, MetalIndex: 1}, }, }, {Name: "cs_irradiance", Stage: gpu.ShaderStageCompute, WorkgroupSize: [3]int{8, 8, 1}, Bindings: []gpu.ShaderBinding{ {Name: "params", Group: 0, Binding: 0, Kind: gpu.BindingUniformBuffer, Size: 32, Register: gpu.ShaderRegister{Class: "b", Space: 0, Index: 0}, MetalIndex: 0}, {Name: "src", Group: 0, Binding: 1, Kind: gpu.BindingSampledTexture, Dimension: gpu.TextureDimCube, Register: gpu.ShaderRegister{Class: "t", Space: 0, Index: 1}, MetalIndex: 0}, {Name: "samp", Group: 0, Binding: 2, Kind: gpu.BindingSampler, Register: gpu.ShaderRegister{Class: "s", Space: 0, Index: 2}, MetalIndex: 0}, {Name: "dst", Group: 0, Binding: 3, Kind: gpu.BindingStorageTexture, Dimension: gpu.TextureDim2DArray, StorageFormat: "rgba16float", Register: gpu.ShaderRegister{Class: "u", Space: 0, Index: 3}, MetalIndex: 1}, }, }, }, }
Env is env.wgsl.
var Grid = &gpu.Shader{ Name: "grid", WGSL: `// The grid of viewport3d (SPEC §27.8, "Overlays"): an infinite grid on a plane, with thin minor lines and thicker major ones that // fade away with the distance. One triangle covers the view; for each pixel the fragment shader meets the line of sight with the // plane, the way fs_cap of section.wgsl does, works out where that point is in the plane's own coordinates, and covers the pixel // by its distance to the nearest line of each kind, in pixels (the derivatives of the coordinates), so that the lines have the // same width everywhere and fade out where they come closer than a few pixels, instead of turning to noise. // // The plane's coordinates are the point's position in the plane's two axes, in float64 on the CPU for the eye's part: the // shader adds the eye's coordinates (reduced to the period of the major lines) to the position relative to the eye (D30), so a // grid 50 km from the origin has lines as sharp as one at the origin. // // The record is not an Item: it is seven vec4 (the size of an Item). // What the shaders of viewport3d share (SPEC §27.8): the frame's uniform data, the record of an item, and the section planes. struct Light { // dir points to the light, in view space. dir: vec4<f32>, // color is the light's color times its intensity. color: vec4<f32>, } struct Frame { proj: mat4x4<f32>, // lights are the light at the camera, then up to four directional ones; an unused one has a black color. lights: array<Light, 5>, ambient: vec4<f32>, // viewport is the width and the height of the target in pixels, and their inverses. viewport: vec4<f32>, // params are the near plane's distance, the device pixel ratio and two unused numbers. params: vec4<f32>, // planes are the section planes in view space, relative to the eye (D30): a point p with dot(plane.xyz, p) + plane.w > 0 is // cut away. planes: array<vec4<f32>, 6>, // section is the number of planes in x. section: vec4<f32>, } @group(0) @binding(0) var<uniform> frame: Frame; // Item is the record of a line, a point or a picked item: the matrix that takes the mesh's positions to the view, relative to // the eye, a tint (the highlight of a selected item: its color, and the part of it that replaces the material's in a), and // numbers for the shader that reads it. struct Item { mv: mat4x4<f32>, tint: vec4<f32>, params: vec4<f32>, extra: vec4<f32>, } // section_clipped says whether a point of the view space is cut away by a section plane. skip is the plane to leave out (the // plane that a cap lies on), or -1. fn section_clipped(p: vec3<f32>, skip: i32) -> bool { let count = i32(frame.section.x); for (var i = 0; i < 6; i = i + 1) { if (i >= count) { break; } if (i == skip) { continue; } let plane = frame.planes[i]; if (dot(plane.xyz, p) + plane.w > 0.0) { return true; } } return false; } // PickOut is what a pick pass writes for a pixel: the number of the item (from 1, 0 for nothing) and of the primitive in it, and // the depth. struct PickOut { @location(0) ids: vec2<u32>, @location(1) depth: f32, } struct GridItem { // plane is the plane in view space: xyz its unit normal, w its distance (a point p is on it when dot(xyz, p) + w = 0). plane: vec4<f32>, // u and v are the plane's axes in view space; their w is the eye's coordinate along the axis, reduced to the major period. u: vec4<f32>, v: vec4<f32>, // params are the spacing of the minor lines, how many of them make a major line, and the distances where the lines start to // fade and where they are gone. params: vec4<f32>, minor: vec4<f32>, major: vec4<f32>, // extra has the width of the lines in pixels in x. extra: vec4<f32>, } @group(1) @binding(0) var<uniform> item: GridItem; struct Full { @builtin(position) clip: vec4<f32>, @location(0) ndc: vec2<f32>, } @vertex fn vs_full(@builtin(vertex_index) i: u32) -> Full { // One triangle that covers the view: (-1, -1), (3, -1) and (-1, 3). let x = f32((i << 1u) & 2u); let y = f32(i & 2u); var o: Full; o.ndc = vec2<f32>(x * 2.0 - 1.0, y * 2.0 - 1.0); o.clip = vec4<f32>(o.ndc, 0.0, 1.0); return o; } struct Gridded { @location(0) color: vec4<f32>, @builtin(frag_depth) depth: f32, } // cover is how much of the pixel a line covers: the distance in pixels of the coordinate from the nearest whole number, and the // width of the line. The line is gone where the cells are smaller than a few pixels. fn cover(coord: vec2<f32>, width: f32) -> f32 { let fw = max(fwidth(coord), vec2<f32>(1.0e-6)); let dist = abs(fract(coord - 0.5) - 0.5) / fw; let line = clamp(0.5 * width + 0.5 - dist, vec2<f32>(0.0), vec2<f32>(1.0)); // The size of a cell in pixels, along each axis. let cell = 1.0 / fw; let visible = clamp((cell - 3.0) / 5.0, vec2<f32>(0.0), vec2<f32>(1.0)); return max(line.x * visible.x, line.y * visible.y); } @fragment fn fs_grid(in: Full) -> Gridded { var o: Gridded; o.color = vec4<f32>(0.0); o.depth = 0.0; // The line of sight of the pixel: from the eye, or, in an orthographic view, parallel to the axis of the view. var origin = vec3<f32>(0.0); var dir = vec3<f32>(in.ndc.x / frame.proj[0][0], in.ndc.y / frame.proj[1][1], -1.0); let ortho = frame.proj[3][3] > 0.5; if (ortho) { origin = vec3<f32>(dir.xy, 0.0); dir = vec3<f32>(0.0, 0.0, -1.0); } let facing = dot(item.plane.xyz, dir); // Everything below is worked out for every pixel, and the pixels that the plane does not reach are dropped at the end, so // that the derivatives are taken in uniform control flow. let ok = abs(facing) > 1.0e-9; let t = select(0.0, -(item.plane.w + dot(item.plane.xyz, origin)) / facing, ok); let p = origin + dir * t; let hit = ok && t > 0.0; let spacing = item.params.x; let major_every = item.params.y; let u = dot(p, item.u.xyz) + item.u.w; let v = dot(p, item.v.xyz) + item.v.w; let width = item.extra.x; let minor_cover = cover(vec2<f32>(u, v) / spacing, width); let major_cover = cover(vec2<f32>(u, v) / (spacing * major_every), 2.0 * width); // Fade with the distance, and where the plane is seen edge on, where the lines would only be a haze. var fade = 1.0; if (!ortho) { fade = 1.0 - smoothstep(item.params.z, item.params.w, length(p)); } let edge_on = smoothstep(0.0, 0.2, abs(facing) / length(dir)); let a_minor = minor_cover * item.minor.a * fade * edge_on; let a_major = major_cover * item.major.a * fade * edge_on; // The major lines are over the minor ones. let rgb = item.major.rgb * a_major + item.minor.rgb * a_minor * (1.0 - a_major); let a = a_major + a_minor * (1.0 - a_major); if (!hit || a < 0.003 || section_clipped(p, -1)) { discard; } let k = frame.proj * vec4<f32>(p, 1.0); o.depth = k.z / k.w; o.color = vec4<f32>(rgb, a); return o; } `, SPIRV: spirvGrid, HLSL: `struct Light { float4 dir; float4 color; }; struct Frame { row_major float4x4 proj; Light lights[5]; float4 ambient; float4 viewport; float4 params; float4 planes[6]; float4 section; }; struct Item { row_major float4x4 mv; float4 tint; float4 params; float4 extra; }; struct PickOut { uint2 ids : LOC0; float depth : LOC1; }; struct GridItem { float4 plane; float4 u; float4 v; float4 params; float4 minor; float4 major; float4 extra; }; struct Full { float4 clip_ : SV_Position; float2 ndc : LOC0; }; struct Gridded { float4 color : SV_Target0; float depth : SV_Depth; }; cbuffer frame : register(b0, space0) { Frame frame; } cbuffer item : register(b0, space1) { GridItem item; } struct VertexOutput_vs_full { float2 ndc : LOC0; float4 clip_ : SV_Position; }; struct FragmentInput_fs_grid { float2 ndc_1 : LOC0; float4 clip_1 : SV_Position; }; int naga_f2i32(float value) { return int(clamp(value, -2147483600.0, 2147483500.0)); } bool section_clipped(float3 p, int skip) { int i_1 = int(0); float _e5 = frame.section.x; int count = naga_f2i32(_e5); uint2 loop_bound = uint2(4294967295u, 4294967295u); bool loop_init = true; while(true) { if (all(loop_bound == uint2(0u, 0u))) { break; } loop_bound -= uint2(loop_bound.y == 0u, 1u); if (!loop_init) { int _e28 = i_1; i_1 = asint(asuint(_e28) + asuint(int(1))); } loop_init = false; int _e9 = i_1; if ((_e9 < int(6))) { } else { break; } { int _e12 = i_1; if ((_e12 >= count)) { break; } int _e14 = i_1; if ((_e14 == skip)) { continue; } int _e18 = i_1; float4 plane = frame.planes[_e18]; if (((dot(plane.xyz, p) + plane.w) > 0.0)) { return true; } } } return false; } float cover(float2 coord, float width) { float2 _e2 = fwidth(coord); float2 fw = max(_e2, (1e-6).xx); float2 dist = (abs((frac((coord - (0.5).xx)) - (0.5).xx)) / fw); float2 line_ = clamp(((((0.5 * width) + 0.5)).xx - dist), (0.0).xx, (1.0).xx); float2 cell = ((1.0).xx / fw); float2 visible = clamp(((cell - (3.0).xx) / (5.0).xx), (0.0).xx, (1.0).xx); return max((line_.x * visible.x), (line_.y * visible.y)); } VertexOutput_vs_full vs_full(uint i : SV_VertexID) { Full o = (Full)0; float x = float(((i << 1u) & 2u)); float y = float((i & 2u)); o.ndc = float2(((x * 2.0) - 1.0), ((y * 2.0) - 1.0)); float2 _e22 = o.ndc; o.clip_ = float4(_e22, 0.0, 1.0); Full _e26 = o; Full full = _e26; VertexOutput_vs_full full_1 = { full.ndc, full.clip_ }; return full_1; } Gridded fs_grid(FragmentInput_fs_grid fragmentinput_fs_grid) { Full in_ = { fragmentinput_fs_grid.clip_1, fragmentinput_fs_grid.ndc_1 }; Gridded o_1 = (Gridded)0; float3 origin = (0.0).xxx; float3 dir = (float3)0; bool local = (bool)0; float fade = 1.0; bool local_1 = (bool)0; bool local_2 = (bool)0; o_1.color = (0.0).xxxx; o_1.depth = 0.0; float _e16 = frame.proj[0].x; float _e24 = frame.proj[1].y; dir = float3((in_.ndc.x / _e16), (in_.ndc.y / _e24), -1.0); float _e33 = frame.proj[3].w; bool ortho = (_e33 > 0.5); if (ortho) { float3 _e36 = dir; origin = float3(_e36.xy, 0.0); dir = float3(0.0, 0.0, -1.0); } float4 _e46 = item.plane; float3 _e48 = dir; float facing = dot(_e46.xyz, _e48); bool ok = (abs(facing) > 1e-9); float _e56 = item.plane.w; float4 _e59 = item.plane; float3 _e61 = origin; float t = (ok ? (-((_e56 + dot(_e59.xyz, _e61))) / facing) : 0.0); float3 _e68 = origin; float3 _e69 = dir; float3 p_1 = (_e68 + (_e69 * t)); if (ok) { local = (t > 0.0); } else { local = false; } bool hit = local; float spacing = item.params.x; float major_every = item.params.y; float4 _e88 = item.u; float _e94 = item.u.w; float u = (dot(p_1, _e88.xyz) + _e94); float4 _e98 = item.v; float _e104 = item.v.w; float v = (dot(p_1, _e98.xyz) + _e104); float width_1 = item.extra.x; const float _e113 = cover((float2(u, v) / (spacing).xx), width_1); const float _e120 = cover((float2(u, v) / ((spacing * major_every)).xx), (2.0 * width_1)); if (!(ortho)) { float _e127 = item.params.z; float _e131 = item.params.w; fade = (1.0 - smoothstep(_e127, _e131, length(p_1))); } float3 _e137 = dir; float edge_on = smoothstep(0.0, 0.2, (abs(facing) / length(_e137))); float _e146 = item.minor.w; float _e148 = fade; float a_minor = (((_e113 * _e146) * _e148) * edge_on); float _e154 = item.major.w; float _e156 = fade; float a_major = (((_e120 * _e154) * _e156) * edge_on); float4 _e161 = item.major; float4 _e166 = item.minor; float3 rgb = ((_e161.xyz * a_major) + ((_e166.xyz * a_minor) * (1.0 - a_major))); float a = (a_major + (a_minor * (1.0 - a_major))); if (!(!(hit))) { local_1 = (a < 0.003); } else { local_1 = true; } bool _e184 = local_1; if (!(_e184)) { const bool _e189 = section_clipped(p_1, int(-1)); local_2 = _e189; } else { local_2 = true; } bool _e191 = local_2; if (_e191) { discard; } float4x4 _e194 = frame.proj; float4 k = mul(float4(p_1, 1.0), _e194); o_1.depth = (k.z / k.w); o_1.color = float4(rgb, a); Gridded _e204 = o_1; Gridded gridded = _e204; return gridded; } `, MSL: `// language: metal2.4 #include <metal_stdlib> #include <simd/simd.h> using metal::uint; struct Light { metal::float4 dir; metal::float4 color; }; struct type_3 { Light inner[5]; }; struct type_4 { metal::float4 inner[6]; }; struct Frame { metal::float4x4 proj; type_3 lights; metal::float4 ambient; metal::float4 viewport; metal::float4 params; type_4 planes; metal::float4 section; }; struct Item { metal::float4x4 mv; metal::float4 tint; metal::float4 params; metal::float4 extra; }; struct PickOut { metal::uint2 ids; float depth; char _pad2[4]; }; struct GridItem { metal::float4 plane; metal::float4 u; metal::float4 v; metal::float4 params; metal::float4 minor; metal::float4 major; metal::float4 extra; }; struct Full { metal::float4 clip; metal::float2 ndc; char _pad2[8]; }; struct Gridded { metal::float4 color; float depth; char _pad2[12]; }; int naga_f2i32(float value) { return static_cast<int>(metal::clamp(value, -2147483600.0, 2147483500.0)); } bool section_clipped( metal::float3 p, int skip, constant Frame& frame ) { int i_1 = 0; float _e5 = frame.section.x; int count = naga_f2i32(_e5); uint2 loop_bound = uint2(4294967295u); bool loop_init = true; while(true) { if (metal::all(loop_bound == uint2(0u))) { break; } loop_bound -= uint2(loop_bound.y == 0u, 1u); if (!loop_init) { int _e28 = i_1; i_1 = as_type<int>(as_type<uint>(_e28) + as_type<uint>(1)); } loop_init = false; int _e9 = i_1; if (_e9 < 6) { } else { break; } { int _e12 = i_1; if (_e12 >= count) { break; } int _e14 = i_1; if (_e14 == skip) { continue; } int _e18 = i_1; metal::float4 plane = frame.planes.inner[_e18]; if ((metal::dot(plane.xyz, p) + plane.w) > 0.0) { return true; } } } return false; } float cover( metal::float2 coord, float width ) { metal::float2 _e2 = metal::fwidth(coord); metal::float2 fw = metal::max(_e2, metal::float2(0.000001)); metal::float2 dist = metal::abs(metal::fract(coord - metal::float2(0.5)) - metal::float2(0.5)) / fw; metal::float2 line = metal::clamp(metal::float2((0.5 * width) + 0.5) - dist, metal::float2(0.0), metal::float2(1.0)); metal::float2 cell = metal::float2(1.0) / fw; metal::float2 visible = metal::clamp((cell - metal::float2(3.0)) / metal::float2(5.0), metal::float2(0.0), metal::float2(1.0)); return metal::max(line.x * visible.x, line.y * visible.y); } struct vs_fullInput { }; struct vs_fullOutput { metal::float4 clip [[position]]; metal::float2 ndc [[user(loc0), center_perspective]]; }; vertex vs_fullOutput vs_full( uint i [[vertex_id]] ) { Full o = {}; float x = static_cast<float>((i << 1u) & 2u); float y = static_cast<float>(i & 2u); o.ndc = metal::float2((x * 2.0) - 1.0, (y * 2.0) - 1.0); metal::float2 _e22 = o.ndc; o.clip = metal::float4(_e22, 0.0, 1.0); Full _e26 = o; const auto _tmp = _e26; return vs_fullOutput { _tmp.clip, _tmp.ndc }; } struct fs_gridInput { metal::float2 ndc [[user(loc0), center_perspective]]; }; struct fs_gridOutput { metal::float4 color [[color(0)]]; float depth [[depth(any)]]; }; fragment fs_gridOutput fs_grid( fs_gridInput varyings_1 [[stage_in]] , metal::float4 clip [[position]] , constant Frame& frame [[buffer(0)]] , constant GridItem& item [[buffer(1)]] ) { const Full in = { clip, varyings_1.ndc }; Gridded o_1 = {}; metal::float3 origin = metal::float3(0.0); metal::float3 dir = {}; bool local = {}; float fade = 1.0; bool local_1 = {}; bool local_2 = {}; o_1.color = metal::float4(0.0); o_1.depth = 0.0; float _e16 = frame.proj[0].x; float _e24 = frame.proj[1].y; dir = metal::float3(in.ndc.x / _e16, in.ndc.y / _e24, -1.0); float _e33 = frame.proj[3].w; bool ortho = _e33 > 0.5; if (ortho) { metal::float3 _e36 = dir; origin = metal::float3(_e36.xy, 0.0); dir = metal::float3(0.0, 0.0, -1.0); } metal::float4 _e46 = item.plane; metal::float3 _e48 = dir; float facing = metal::dot(_e46.xyz, _e48); bool ok = metal::abs(facing) > 0.000000001; float _e56 = item.plane.w; metal::float4 _e59 = item.plane; metal::float3 _e61 = origin; float t = ok ? (-((_e56 + metal::dot(_e59.xyz, _e61))) / facing) : 0.0; metal::float3 _e68 = origin; metal::float3 _e69 = dir; metal::float3 p_1 = _e68 + (_e69 * t); if (ok) { local = t > 0.0; } else { local = false; } bool hit = local; float spacing = item.params.x; float major_every = item.params.y; metal::float4 _e88 = item.u; float _e94 = item.u.w; float u = metal::dot(p_1, _e88.xyz) + _e94; metal::float4 _e98 = item.v; float _e104 = item.v.w; float v = metal::dot(p_1, _e98.xyz) + _e104; float width_1 = item.extra.x; float _e113 = cover(metal::float2(u, v) / metal::float2(spacing), width_1); float _e120 = cover(metal::float2(u, v) / metal::float2(spacing * major_every), 2.0 * width_1); if (!(ortho)) { float _e127 = item.params.z; float _e131 = item.params.w; fade = 1.0 - metal::smoothstep(_e127, _e131, metal::length(p_1)); } metal::float3 _e137 = dir; float edge_on = metal::smoothstep(0.0, 0.2, metal::abs(facing) / metal::length(_e137)); float _e146 = item.minor.w; float _e148 = fade; float a_minor = ((_e113 * _e146) * _e148) * edge_on; float _e154 = item.major.w; float _e156 = fade; float a_major = ((_e120 * _e154) * _e156) * edge_on; metal::float4 _e161 = item.major; metal::float4 _e166 = item.minor; metal::float3 rgb = (_e161.xyz * a_major) + ((_e166.xyz * a_minor) * (1.0 - a_major)); float a = a_major + (a_minor * (1.0 - a_major)); if (!(!(hit))) { local_1 = a < 0.003; } else { local_1 = true; } bool _e184 = local_1; if (!(_e184)) { bool _e189 = section_clipped(p_1, -1, frame); local_2 = _e189; } else { local_2 = true; } bool _e191 = local_2; if (_e191) { metal::discard_fragment(); } metal::float4x4 _e194 = frame.proj; metal::float4 k = _e194 * metal::float4(p_1, 1.0); o_1.depth = k.z / k.w; o_1.color = metal::float4(rgb, a); Gridded _e204 = o_1; const auto _tmp_1 = _e204; return fs_gridOutput { _tmp_1.color, _tmp_1.depth }; } `, Entries: []gpu.EntryPoint{ {Name: "vs_full", Stage: gpu.ShaderStageVertex, ReadsVertexIndex: true, Bindings: []gpu.ShaderBinding{}, }, {Name: "fs_grid", Stage: gpu.ShaderStageFragment, Bindings: []gpu.ShaderBinding{ {Name: "frame", Group: 0, Binding: 0, Kind: gpu.BindingUniformBuffer, Size: 384, Register: gpu.ShaderRegister{Class: "b", Space: 0, Index: 0}, MetalIndex: 0}, {Name: "item", Group: 1, Binding: 0, Kind: gpu.BindingUniformBuffer, Size: 112, Register: gpu.ShaderRegister{Class: "b", Space: 1, Index: 0}, MetalIndex: 1}, }, }, }, }
Grid is grid.wgsl.
var Line = &gpu.Shader{ Name: "line", WGSL: `// The lines of viewport3d (SPEC §27.8): each segment is a quad that the vertex shader expands in the screen, so that the width // is in logical pixels whatever the distance. The fragment shader anti-aliases the sides and the ends and cuts a dash // pattern. A segment is an instance (its two ends in the mesh's units and the length of the polyline before it); the four // corners of the quad come from a small vertex buffer: no built-in index is read. // What the shaders of viewport3d share (SPEC §27.8): the frame's uniform data, the record of an item, and the section planes. struct Light { // dir points to the light, in view space. dir: vec4<f32>, // color is the light's color times its intensity. color: vec4<f32>, } struct Frame { proj: mat4x4<f32>, // lights are the light at the camera, then up to four directional ones; an unused one has a black color. lights: array<Light, 5>, ambient: vec4<f32>, // viewport is the width and the height of the target in pixels, and their inverses. viewport: vec4<f32>, // params are the near plane's distance, the device pixel ratio and two unused numbers. params: vec4<f32>, // planes are the section planes in view space, relative to the eye (D30): a point p with dot(plane.xyz, p) + plane.w > 0 is // cut away. planes: array<vec4<f32>, 6>, // section is the number of planes in x. section: vec4<f32>, } @group(0) @binding(0) var<uniform> frame: Frame; // Item is the record of a line, a point or a picked item: the matrix that takes the mesh's positions to the view, relative to // the eye, a tint (the highlight of a selected item: its color, and the part of it that replaces the material's in a), and // numbers for the shader that reads it. struct Item { mv: mat4x4<f32>, tint: vec4<f32>, params: vec4<f32>, extra: vec4<f32>, } // section_clipped says whether a point of the view space is cut away by a section plane. skip is the plane to leave out (the // plane that a cap lies on), or -1. fn section_clipped(p: vec3<f32>, skip: i32) -> bool { let count = i32(frame.section.x); for (var i = 0; i < 6; i = i + 1) { if (i >= count) { break; } if (i == skip) { continue; } let plane = frame.planes[i]; if (dot(plane.xyz, p) + plane.w > 0.0) { return true; } } return false; } // PickOut is what a pick pass writes for a pixel: the number of the item (from 1, 0 for nothing) and of the primitive in it, and // the depth. struct PickOut { @location(0) ids: vec2<u32>, @location(1) depth: f32, } // The quad of a line segment (SPEC §27.8): the vertex shaders of the lines, of the picking of lines and of the outline of a // section expand a segment into a quad in the screen with it, so that the width is in logical pixels whatever the distance. // It needs common.wgsl.inc. struct SegmentQuad { clip: vec4<f32>, // view_pos is the point of the segment that the corner is at, in view space. view_pos: vec3<f32>, // across is the distance in pixels from the center of the line, along: the distance from its first end, along it. across: f32, along: f32, length_px: f32, // ta and tb are the part of the segment in front of the near plane, from 0 to 1. ta: f32, tb: f32, visible: bool, } // segment_quad expands the segment from a to b (view space) at a corner of the quad: corner.x is 0 at the first end and 1 at the // second, corner.y is -1 and 1 for the two sides. width is in logical pixels; pull moves the depth towards the camera by a // part of itself (so that an edge is not hidden by the face it lies on). fn segment_quad(a: vec3<f32>, b: vec3<f32>, corner: vec2<f32>, width: f32, pull: f32) -> SegmentQuad { var o: SegmentQuad; // A segment that is wholly behind the near plane is put outside the clip volume. o.clip = vec4<f32>(2.0, 2.0, 2.0, 1.0); o.visible = false; let near = frame.params.x; let za = -a.z; let zb = -b.z; if (za < near && zb < near) { return o; } // The part of the segment in front of the near plane. var ta = 0.0; var tb = 1.0; let cut = (near - za) / (zb - za); if (za < near) { ta = cut; } if (zb < near) { tb = cut; } let ca = mix(a, b, ta); let cb = mix(a, b, tb); let ka = frame.proj * vec4<f32>(ca, 1.0); let kb = frame.proj * vec4<f32>(cb, 1.0); let half_vp = 0.5 * frame.viewport.xy; let sa = ka.xy / ka.w * half_vp; let sb = kb.xy / kb.w * half_vp; var dir = sb - sa; let span = length(dir); if (span > 0.0001) { dir = dir / span; } else { dir = vec2<f32>(1.0, 0.0); } let nrm = vec2<f32>(-dir.y, dir.x); let half_w = 0.5 * width * frame.params.y; let pad = half_w + 1.0; let at_end = corner.x; let sgn = at_end * 2.0 - 1.0; let offset = dir * (sgn * pad) + nrm * (corner.y * pad); let k = mix(ka, kb, at_end); o.clip = vec4<f32>(k.xy + offset / half_vp * k.w, k.z * (1.0 + pull), k.w); o.view_pos = mix(ca, cb, at_end); o.across = corner.y * pad; o.along = at_end * span + sgn * pad; o.length_px = span; o.ta = ta; o.tb = tb; o.visible = true; return o; } // segment_cover is how much of a pixel of a quad of a segment the line covers, from 0 to 1, with anti-aliased sides and ends. fn segment_cover(width: f32, across: f32, along: f32, length_px: f32) -> f32 { let half_w = 0.5 * width * frame.params.y; var cover = clamp(half_w + 0.5 - abs(across), 0.0, 1.0); cover = cover * clamp(along + half_w + 0.5, 0.0, 1.0) * clamp(length_px + half_w + 0.5 - along, 0.0, 1.0); return cover; } struct LineMaterial { color: vec4<f32>, // params are: x the width in logical pixels, y the number of lengths of the dash pattern (0 for a solid line), z the // relative pull of the line towards the camera (so that an edge is not hidden by the face it lies on), w the sum of the // pattern. params: vec4<f32>, // pattern are the lengths in logical pixels: a dash, a gap, a dash, ... pattern: array<vec4<f32>, 2>, } @group(1) @binding(0) var<uniform> line: LineMaterial; @group(2) @binding(0) var<uniform> item: Item; struct VOut { @builtin(position) clip: vec4<f32>, // across is the distance in pixels from the center of the line, along: the distance from its first end, along it. @location(0) across: f32, @location(1) along: f32, @location(2) length_px: f32, // stipple is the position along the dash pattern, in pixels. @location(3) stipple: f32, @location(4) view_pos: vec3<f32>, // seg is the number of the segment in its mesh, which the picking reports. @location(5) @interpolate(flat) seg: f32, } @vertex fn vs_main(@location(0) p0: vec3<f32>, @location(1) p1: vec3<f32>, @location(2) d0: f32, @location(3) corner: vec2<f32>) -> VOut { return vertex_line(p0, p1, d0, corner, 0.0); } // vs_pick is vs_main for the picking: the segment also has its number. @vertex fn vs_pick(@location(0) p0: vec3<f32>, @location(1) p1: vec3<f32>, @location(2) d0: f32, @location(3) corner: vec2<f32>, @location(4) seg: f32) -> VOut { return vertex_line(p0, p1, d0, corner, seg); } fn vertex_line(p0: vec3<f32>, p1: vec3<f32>, d0: f32, corner: vec2<f32>, seg: f32) -> VOut { var o: VOut; o.seg = seg; let a = (item.mv * vec4<f32>(p0, 1.0)).xyz; let b = (item.mv * vec4<f32>(p1, 1.0)).xyz; let q = segment_quad(a, b, corner, line.params.x, line.params.z); o.clip = q.clip; if (!q.visible) { return o; } o.across = q.across; o.along = q.along; o.length_px = q.length_px; o.view_pos = q.view_pos; // The pattern runs in screen pixels: the pixels for a unit of the mesh at this segment, from its projected length. let seg_len = length(p1 - p0); let units = seg_len * (q.tb - q.ta); var per_unit = 0.0; if (units > 0.0) { per_unit = q.length_px / units; } o.stipple = (d0 + q.ta * seg_len) * per_unit + o.along; return o; } @fragment fn fs_main(in: VOut) -> @location(0) vec4<f32> { if (section_clipped(in.view_pos, -1)) { discard; } let dpr = frame.params.y; var cover = segment_cover(line.params.x, in.across, in.along, in.length_px); let count = i32(line.params.y); if (count > 0) { let total = line.params.w * dpr; let m = in.stipple - floor(in.stipple / total) * total; var acc = 0.0; var dash = 1.0; for (var i = 0; i < 8; i = i + 1) { if (i >= count) { break; } let piece = line.pattern[i / 4][i % 4] * dpr; if (m < acc + piece) { let inside = min(m - acc, acc + piece - m); if (i % 2 == 0) { dash = clamp(0.5 + inside, 0.0, 1.0); } else { dash = clamp(0.5 - inside, 0.0, 1.0); } break; } acc = acc + piece; } cover = cover * dash; } let rgb = mix(line.color.rgb, item.tint.rgb, item.tint.a); let a = line.color.a * cover; return vec4<f32>(rgb * a, a); } @fragment fn fs_pick(in: VOut) -> PickOut { if (section_clipped(in.view_pos, -1)) { discard; } if (segment_cover(line.params.x, in.across, in.along, in.length_px) < 0.5) { discard; } var o: PickOut; o.ids = vec2<u32>(u32(item.params.x), u32(in.seg + 0.5)); o.depth = in.clip.z; return o; } `, SPIRV: spirvLine, HLSL: `struct Light { float4 dir; float4 color; }; struct Frame { row_major float4x4 proj; Light lights[5]; float4 ambient; float4 viewport; float4 params; float4 planes[6]; float4 section; }; struct Item { row_major float4x4 mv; float4 tint; float4 params; float4 extra; }; struct PickOut { uint2 ids : SV_Target0; float depth : SV_Target1; }; struct SegmentQuad { float4 clip_; float3 view_pos; float across; float along; float length_px; float ta; float tb; bool visible; int _end_pad_0; int _end_pad_1; int _end_pad_2; }; struct LineMaterial { float4 color; float4 params; float4 pattern[2]; }; struct VOut { float4 clip_ : SV_Position; float across : LOC0; float along : LOC1; float length_px : LOC2; float stipple : LOC3; float3 view_pos : LOC4; nointerpolation float seg : LOC5; }; cbuffer frame : register(b0, space0) { Frame frame; } cbuffer line_ : register(b0, space1) { LineMaterial line_; } cbuffer item : register(b0, space2) { Item item; } struct VertexOutput_vs_main { float across_1 : LOC0; float along_1 : LOC1; float length_px_1 : LOC2; float stipple : LOC3; float3 view_pos : LOC4; nointerpolation float seg_2 : LOC5; float4 clip_ : SV_Position; }; struct VertexOutput_vs_pick { float across_2 : LOC0; float along_2 : LOC1; float length_px_2 : LOC2; float stipple_1 : LOC3; float3 view_pos_1 : LOC4; nointerpolation float seg_3 : LOC5; float4 clip_1 : SV_Position; }; struct FragmentInput_fs_main { float across_3 : LOC0; float along_3 : LOC1; float length_px_3 : LOC2; float stipple_2 : LOC3; float3 view_pos_2 : LOC4; nointerpolation float seg_4 : LOC5; float4 clip_2 : SV_Position; }; struct FragmentInput_fs_pick { float across_4 : LOC0; float along_4 : LOC1; float length_px_4 : LOC2; float stipple_3 : LOC3; float3 view_pos_3 : LOC4; nointerpolation float seg_5 : LOC5; float4 clip_3 : SV_Position; }; int naga_f2i32(float value) { return int(clamp(value, -2147483600.0, 2147483500.0)); } bool section_clipped(float3 p, int skip) { int i_1 = int(0); float _e5 = frame.section.x; int count = naga_f2i32(_e5); uint2 loop_bound = uint2(4294967295u, 4294967295u); bool loop_init = true; while(true) { if (all(loop_bound == uint2(0u, 0u))) { break; } loop_bound -= uint2(loop_bound.y == 0u, 1u); if (!loop_init) { int _e28 = i_1; i_1 = asint(asuint(_e28) + asuint(int(1))); } loop_init = false; int _e9 = i_1; if ((_e9 < int(6))) { } else { break; } { int _e12 = i_1; if ((_e12 >= count)) { break; } int _e14 = i_1; if ((_e14 == skip)) { continue; } int _e18 = i_1; float4 plane = frame.planes[_e18]; if (((dot(plane.xyz, p) + plane.w) > 0.0)) { return true; } } } return false; } SegmentQuad segment_quad(float3 a, float3 b, float2 corner_2, float width, float pull) { SegmentQuad o_1 = (SegmentQuad)0; bool local = (bool)0; float ta = 0.0; float tb = 1.0; float2 dir = (float2)0; o_1.clip_ = float4(2.0, 2.0, 2.0, 1.0); o_1.visible = false; float near = frame.params.x; float za = -(a.z); float zb = -(b.z); if ((za < near)) { local = (zb < near); } else { local = false; } bool _e27 = local; if (_e27) { SegmentQuad _e28 = o_1; SegmentQuad segmentquad = _e28; return segmentquad; } float cut = ((near - za) / (zb - za)); if ((za < near)) { ta = cut; } if ((zb < near)) { tb = cut; } float _e38 = ta; float3 ca = lerp(a, b, _e38); float _e40 = tb; float3 cb = lerp(a, b, _e40); float4x4 _e44 = frame.proj; float4 ka = mul(float4(ca, 1.0), _e44); float4x4 _e50 = frame.proj; float4 kb = mul(float4(cb, 1.0), _e50); float4 _e56 = frame.viewport; float2 half_vp = (0.5 * _e56.xy); float2 sa = ((ka.xy / (ka.w).xx) * half_vp); float2 sb = ((kb.xy / (kb.w).xx) * half_vp); dir = (sb - sa); float2 _e72 = dir; float span = length(_e72); if ((span > 0.0001)) { float2 _e76 = dir; dir = (_e76 / (span).xx); } else { dir = float2(1.0, 0.0); } float _e83 = dir.y; float _e86 = dir.x; float2 nrm = float2(-(_e83), _e86); float _e93 = frame.params.y; float half_w = ((0.5 * width) * _e93); float pad = (half_w + 1.0); float at_end = corner_2.x; float sgn = ((at_end * 2.0) - 1.0); float2 _e102 = dir; float2 offset = ((_e102 * (sgn * pad)) + (nrm * (corner_2.y * pad))); float4 k = lerp(ka, kb, at_end); o_1.clip_ = float4((k.xy + ((offset / half_vp) * k.w)), (k.z * (1.0 + pull)), k.w); o_1.view_pos = lerp(ca, cb, at_end); o_1.across = (corner_2.y * pad); o_1.along = ((at_end * span) + (sgn * pad)); o_1.length_px = span; float _e133 = ta; o_1.ta = _e133; float _e135 = tb; o_1.tb = _e135; o_1.visible = true; SegmentQuad _e138 = o_1; SegmentQuad segmentquad_1 = _e138; return segmentquad_1; } float segment_cover(float width_1, float across, float along, float length_px) { float cover_1 = (float)0; float _e9 = frame.params.y; float half_w_1 = ((0.5 * width_1) * _e9); cover_1 = clamp(((half_w_1 + 0.5) - abs(across)), 0.0, 1.0); float _e19 = cover_1; cover_1 = ((_e19 * clamp(((along + half_w_1) + 0.5), 0.0, 1.0)) * clamp((((length_px + half_w_1) + 0.5) - along), 0.0, 1.0)); float _e35 = cover_1; return _e35; } VOut vertex_line(float3 p0_2, float3 p1_2, float d0_2, float2 corner_3, float seg_1) { VOut o_2 = (VOut)0; float per_unit = 0.0; o_2.seg = seg_1; float4x4 _e9 = item.mv; float3 a_1 = mul(float4(p0_2, 1.0), _e9).xyz; float4x4 _e16 = item.mv; float3 b_1 = mul(float4(p1_2, 1.0), _e16).xyz; float _e24 = line_.params.x; float _e28 = line_.params.z; SegmentQuad _e29 = segment_quad(a_1, b_1, corner_3, _e24, _e28); o_2.clip_ = _e29.clip_; if (!(_e29.visible)) { VOut _e34 = o_2; VOut vout = _e34; return vout; } o_2.across = _e29.across; o_2.along = _e29.along; o_2.length_px = _e29.length_px; o_2.view_pos = _e29.view_pos; float seg_len = length((p1_2 - p0_2)); float units = (seg_len * (_e29.tb - _e29.ta)); if ((units > 0.0)) { per_unit = (_e29.length_px / units); } float _e59 = per_unit; float _e62 = o_2.along; o_2.stipple = (((d0_2 + (_e29.ta * seg_len)) * _e59) + _e62); VOut _e64 = o_2; VOut vout_1 = _e64; return vout_1; } VertexOutput_vs_main vs_main(float3 p0_ : LOC0, float3 p1_ : LOC1, float d0_ : LOC2, float2 corner : LOC3) { VOut _e5 = vertex_line(p0_, p1_, d0_, corner, 0.0); VOut vout_2 = _e5; VertexOutput_vs_main vout_2_ = { vout_2.across, vout_2.along, vout_2.length_px, vout_2.stipple, vout_2.view_pos, vout_2.seg, vout_2.clip_ }; return vout_2_; } VertexOutput_vs_pick vs_pick(float3 p0_1 : LOC0, float3 p1_1 : LOC1, float d0_1 : LOC2, float2 corner_1 : LOC3, float seg : LOC4) { VOut _e5 = vertex_line(p0_1, p1_1, d0_1, corner_1, seg); VOut vout_3 = _e5; VertexOutput_vs_pick vout_3_ = { vout_3.across, vout_3.along, vout_3.length_px, vout_3.stipple, vout_3.view_pos, vout_3.seg, vout_3.clip_ }; return vout_3_; } int naga_div(int lhs, int rhs) { return lhs / (((lhs == int(-2147483647 - 1) & rhs == -1) | (rhs == 0)) ? 1 : rhs); } int naga_mod(int lhs, int rhs) { int divisor = ((lhs == int(-2147483647 - 1) & rhs == -1) | (rhs == 0)) ? 1 : rhs; return lhs - (lhs / divisor) * divisor; } float4 fs_main(FragmentInput_fs_main fragmentinput_fs_main) : SV_Target0 { VOut in_ = { fragmentinput_fs_main.clip_2, fragmentinput_fs_main.across_3, fragmentinput_fs_main.along_3, fragmentinput_fs_main.length_px_3, fragmentinput_fs_main.stipple_2, fragmentinput_fs_main.view_pos_2, fragmentinput_fs_main.seg_4 }; float cover = (float)0; float acc = 0.0; float dash = 1.0; int i = int(0); const bool _e3 = section_clipped(in_.view_pos, int(-1)); if (_e3) { discard; } float dpr = frame.params.y; float _e11 = line_.params.x; const float _e15 = segment_cover(_e11, in_.across, in_.along, in_.length_px); cover = _e15; float _e20 = line_.params.y; int count_1 = naga_f2i32(_e20); if ((count_1 > int(0))) { float _e27 = line_.params.w; float total = (_e27 * dpr); float m = (in_.stipple - (floor((in_.stipple / total)) * total)); uint2 loop_bound_1 = uint2(4294967295u, 4294967295u); bool loop_init_1 = true; while(true) { if (all(loop_bound_1 == uint2(0u, 0u))) { break; } loop_bound_1 -= uint2(loop_bound_1.y == 0u, 1u); if (!loop_init_1) { int _e84 = i; i = asint(asuint(_e84) + asuint(int(1))); } loop_init_1 = false; int _e41 = i; if ((_e41 < int(8))) { } else { break; } { int _e44 = i; if ((_e44 >= count_1)) { break; } int _e48 = i; int _e52 = i; float _e56 = line_.pattern[naga_div(_e48, int(4))][naga_mod(_e52, int(4))]; float piece = (_e56 * dpr); float _e58 = acc; if ((m < (_e58 + piece))) { float _e61 = acc; float _e63 = acc; float inside = min((m - _e61), ((_e63 + piece) - m)); int _e67 = i; if ((naga_mod(_e67, int(2)) == int(0))) { dash = clamp((0.5 + inside), 0.0, 1.0); } else { dash = clamp((0.5 - inside), 0.0, 1.0); } break; } float _e82 = acc; acc = (_e82 + piece); } } float _e87 = cover; float _e88 = dash; cover = (_e87 * _e88); } float4 _e92 = line_.color; float4 _e96 = item.tint; float _e101 = item.tint.w; float3 rgb = lerp(_e92.xyz, _e96.xyz, _e101); float _e106 = line_.color.w; float _e107 = cover; float a_2 = (_e106 * _e107); return float4((rgb * a_2), a_2); } uint naga_f2u32(float value) { return uint(clamp(value, 0.0, 4294967000.0)); } PickOut fs_pick(FragmentInput_fs_pick fragmentinput_fs_pick) { VOut in_1 = { fragmentinput_fs_pick.clip_3, fragmentinput_fs_pick.across_4, fragmentinput_fs_pick.along_4, fragmentinput_fs_pick.length_px_4, fragmentinput_fs_pick.stipple_3, fragmentinput_fs_pick.view_pos_3, fragmentinput_fs_pick.seg_5 }; PickOut o = (PickOut)0; const bool _e3 = section_clipped(in_1.view_pos, int(-1)); if (_e3) { discard; } float _e7 = line_.params.x; const float _e11 = segment_cover(_e7, in_1.across, in_1.along, in_1.length_px); if ((_e11 < 0.5)) { discard; } float _e19 = item.params.x; o.ids = uint2(naga_f2u32(_e19), naga_f2u32((in_1.seg + 0.5))); o.depth = in_1.clip_.z; PickOut _e29 = o; PickOut pickout = _e29; return pickout; } `, MSL: `// language: metal2.4 #include <metal_stdlib> #include <simd/simd.h> using metal::uint; struct Light { metal::float4 dir; metal::float4 color; }; struct type_3 { Light inner[5]; }; struct type_4 { metal::float4 inner[6]; }; struct Frame { metal::float4x4 proj; type_3 lights; metal::float4 ambient; metal::float4 viewport; metal::float4 params; type_4 planes; metal::float4 section; }; struct Item { metal::float4x4 mv; metal::float4 tint; metal::float4 params; metal::float4 extra; }; struct PickOut { metal::uint2 ids; float depth; char _pad2[4]; }; struct SegmentQuad { metal::float4 clip; metal::packed_float3 view_pos; float across; float along; float length_px; float ta; float tb; bool visible; char _pad8[15]; }; struct type_10 { metal::float4 inner[2]; }; struct LineMaterial { metal::float4 color; metal::float4 params; type_10 pattern; }; struct VOut { metal::float4 clip; float across; float along; float length_px; float stipple; metal::packed_float3 view_pos; float seg; }; int naga_f2i32(float value) { return static_cast<int>(metal::clamp(value, -2147483600.0, 2147483500.0)); } bool section_clipped( metal::float3 p, int skip, constant Frame& frame ) { int i_1 = 0; float _e5 = frame.section.x; int count = naga_f2i32(_e5); uint2 loop_bound = uint2(4294967295u); bool loop_init = true; while(true) { if (metal::all(loop_bound == uint2(0u))) { break; } loop_bound -= uint2(loop_bound.y == 0u, 1u); if (!loop_init) { int _e28 = i_1; i_1 = as_type<int>(as_type<uint>(_e28) + as_type<uint>(1)); } loop_init = false; int _e9 = i_1; if (_e9 < 6) { } else { break; } { int _e12 = i_1; if (_e12 >= count) { break; } int _e14 = i_1; if (_e14 == skip) { continue; } int _e18 = i_1; metal::float4 plane = frame.planes.inner[_e18]; if ((metal::dot(plane.xyz, p) + plane.w) > 0.0) { return true; } } } return false; } SegmentQuad segment_quad( metal::float3 a, metal::float3 b, metal::float2 corner_2, float width, float pull, constant Frame& frame ) { SegmentQuad o_1 = {}; bool local = {}; float ta = 0.0; float tb = 1.0; metal::float2 dir = {}; o_1.clip = metal::float4(2.0, 2.0, 2.0, 1.0); o_1.visible = false; float near = frame.params.x; float za = -(a.z); float zb = -(b.z); if (za < near) { local = zb < near; } else { local = false; } bool _e27 = local; if (_e27) { SegmentQuad _e28 = o_1; return _e28; } float cut = (near - za) / (zb - za); if (za < near) { ta = cut; } if (zb < near) { tb = cut; } float _e38 = ta; metal::float3 ca = metal::mix(a, b, _e38); float _e40 = tb; metal::float3 cb = metal::mix(a, b, _e40); metal::float4x4 _e44 = frame.proj; metal::float4 ka = _e44 * metal::float4(ca, 1.0); metal::float4x4 _e50 = frame.proj; metal::float4 kb = _e50 * metal::float4(cb, 1.0); metal::float4 _e56 = frame.viewport; metal::float2 half_vp = 0.5 * _e56.xy; metal::float2 sa = (ka.xy / metal::float2(ka.w)) * half_vp; metal::float2 sb = (kb.xy / metal::float2(kb.w)) * half_vp; dir = sb - sa; metal::float2 _e72 = dir; float span = metal::length(_e72); if (span > 0.0001) { metal::float2 _e76 = dir; dir = _e76 / metal::float2(span); } else { dir = metal::float2(1.0, 0.0); } float _e83 = dir.y; float _e86 = dir.x; metal::float2 nrm = metal::float2(-(_e83), _e86); float _e93 = frame.params.y; float half_w = (0.5 * width) * _e93; float pad = half_w + 1.0; float at_end = corner_2.x; float sgn = (at_end * 2.0) - 1.0; metal::float2 _e102 = dir; metal::float2 offset = (_e102 * (sgn * pad)) + (nrm * (corner_2.y * pad)); metal::float4 k = metal::mix(ka, kb, at_end); o_1.clip = metal::float4(k.xy + ((offset / half_vp) * k.w), k.z * (1.0 + pull), k.w); o_1.view_pos = metal::mix(ca, cb, at_end); o_1.across = corner_2.y * pad; o_1.along = (at_end * span) + (sgn * pad); o_1.length_px = span; float _e133 = ta; o_1.ta = _e133; float _e135 = tb; o_1.tb = _e135; o_1.visible = true; SegmentQuad _e138 = o_1; return _e138; } float segment_cover( float width_1, float across, float along, float length_px, constant Frame& frame ) { float cover_1 = {}; float _e9 = frame.params.y; float half_w_1 = (0.5 * width_1) * _e9; cover_1 = metal::clamp((half_w_1 + 0.5) - metal::abs(across), 0.0, 1.0); float _e19 = cover_1; cover_1 = (_e19 * metal::clamp((along + half_w_1) + 0.5, 0.0, 1.0)) * metal::clamp(((length_px + half_w_1) + 0.5) - along, 0.0, 1.0); float _e35 = cover_1; return _e35; } VOut vertex_line( metal::float3 p0_2, metal::float3 p1_2, float d0_2, metal::float2 corner_3, float seg_1, constant Frame& frame, constant LineMaterial& line, constant Item& item ) { VOut o_2 = {}; float per_unit = 0.0; o_2.seg = seg_1; metal::float4x4 _e9 = item.mv; metal::float3 a_1 = (_e9 * metal::float4(p0_2, 1.0)).xyz; metal::float4x4 _e16 = item.mv; metal::float3 b_1 = (_e16 * metal::float4(p1_2, 1.0)).xyz; float _e24 = line.params.x; float _e28 = line.params.z; SegmentQuad _e29 = segment_quad(a_1, b_1, corner_3, _e24, _e28, frame); o_2.clip = _e29.clip; if (!(_e29.visible)) { VOut _e34 = o_2; return _e34; } o_2.across = _e29.across; o_2.along = _e29.along; o_2.length_px = _e29.length_px; o_2.view_pos = _e29.view_pos; float seg_len = metal::length(p1_2 - p0_2); float units = seg_len * (_e29.tb - _e29.ta); if (units > 0.0) { per_unit = _e29.length_px / units; } float _e59 = per_unit; float _e62 = o_2.along; o_2.stipple = ((d0_2 + (_e29.ta * seg_len)) * _e59) + _e62; VOut _e64 = o_2; return _e64; } struct vs_mainInput { metal::float3 p0_ [[attribute(0)]]; metal::float3 p1_ [[attribute(1)]]; float d0_ [[attribute(2)]]; metal::float2 corner [[attribute(3)]]; }; struct vs_mainOutput { metal::float4 clip [[position]]; float across [[user(loc0), center_perspective]]; float along [[user(loc1), center_perspective]]; float length_px [[user(loc2), center_perspective]]; float stipple [[user(loc3), center_perspective]]; metal::float3 view_pos [[user(loc4), center_perspective]]; float seg [[user(loc5), flat]]; }; vertex vs_mainOutput vs_main( vs_mainInput varyings [[stage_in]] , constant Frame& frame [[buffer(0)]] , constant LineMaterial& line [[buffer(1)]] , constant Item& item [[buffer(2)]] ) { const auto p0_ = varyings.p0_; const auto p1_ = varyings.p1_; const auto d0_ = varyings.d0_; const auto corner = varyings.corner; VOut _e5 = vertex_line(p0_, p1_, d0_, corner, 0.0, frame, line, item); const auto _tmp = _e5; return vs_mainOutput { _tmp.clip, _tmp.across, _tmp.along, _tmp.length_px, _tmp.stipple, _tmp.view_pos, _tmp.seg }; } struct vs_pickInput { metal::float3 p0_1 [[attribute(0)]]; metal::float3 p1_1 [[attribute(1)]]; float d0_1 [[attribute(2)]]; metal::float2 corner_1 [[attribute(3)]]; float seg [[attribute(4)]]; }; struct vs_pickOutput { metal::float4 clip [[position]]; float across [[user(loc0), center_perspective]]; float along [[user(loc1), center_perspective]]; float length_px [[user(loc2), center_perspective]]; float stipple [[user(loc3), center_perspective]]; metal::float3 view_pos [[user(loc4), center_perspective]]; float seg [[user(loc5), flat]]; }; vertex vs_pickOutput vs_pick( vs_pickInput varyings_1 [[stage_in]] , constant Frame& frame [[buffer(0)]] , constant LineMaterial& line [[buffer(1)]] , constant Item& item [[buffer(2)]] ) { const auto p0_1 = varyings_1.p0_1; const auto p1_1 = varyings_1.p1_1; const auto d0_1 = varyings_1.d0_1; const auto corner_1 = varyings_1.corner_1; const auto seg = varyings_1.seg; VOut _e5 = vertex_line(p0_1, p1_1, d0_1, corner_1, seg, frame, line, item); const auto _tmp_1 = _e5; return vs_pickOutput { _tmp_1.clip, _tmp_1.across, _tmp_1.along, _tmp_1.length_px, _tmp_1.stipple, _tmp_1.view_pos, _tmp_1.seg }; } int naga_div(int lhs, int rhs) { return lhs / metal::select(rhs, 1, (lhs == (-2147483647 - 1) & rhs == -1) | (rhs == 0)); } int naga_mod(int lhs, int rhs) { int divisor = metal::select(rhs, 1, (lhs == (-2147483647 - 1) & rhs == -1) | (rhs == 0)); return lhs - (lhs / divisor) * divisor; } struct fs_mainInput { float across_1 [[user(loc0), center_perspective]]; float along_1 [[user(loc1), center_perspective]]; float length_px_1 [[user(loc2), center_perspective]]; float stipple [[user(loc3), center_perspective]]; metal::float3 view_pos [[user(loc4), center_perspective]]; float seg_2 [[user(loc5), flat]]; }; struct fs_mainOutput { metal::float4 member_2 [[color(0)]]; }; fragment fs_mainOutput fs_main( fs_mainInput varyings_2 [[stage_in]] , metal::float4 clip [[position]] , constant Frame& frame [[buffer(0)]] , constant LineMaterial& line [[buffer(1)]] , constant Item& item [[buffer(2)]] ) { const VOut in = { clip, varyings_2.across_1, varyings_2.along_1, varyings_2.length_px_1, varyings_2.stipple, varyings_2.view_pos, varyings_2.seg_2 }; float cover = {}; float acc = 0.0; float dash = 1.0; int i = 0; bool _e3 = section_clipped(in.view_pos, -1, frame); if (_e3) { metal::discard_fragment(); } float dpr = frame.params.y; float _e11 = line.params.x; float _e15 = segment_cover(_e11, in.across, in.along, in.length_px, frame); cover = _e15; float _e20 = line.params.y; int count_1 = naga_f2i32(_e20); if (count_1 > 0) { float _e27 = line.params.w; float total = _e27 * dpr; float m = in.stipple - (metal::floor(in.stipple / total) * total); uint2 loop_bound_1 = uint2(4294967295u); bool loop_init_1 = true; while(true) { if (metal::all(loop_bound_1 == uint2(0u))) { break; } loop_bound_1 -= uint2(loop_bound_1.y == 0u, 1u); if (!loop_init_1) { int _e84 = i; i = as_type<int>(as_type<uint>(_e84) + as_type<uint>(1)); } loop_init_1 = false; int _e41 = i; if (_e41 < 8) { } else { break; } { int _e44 = i; if (_e44 >= count_1) { break; } int _e48 = i; int _e52 = i; float _e56 = line.pattern.inner[naga_div(_e48, 4)][naga_mod(_e52, 4)]; float piece = _e56 * dpr; float _e58 = acc; if (m < (_e58 + piece)) { float _e61 = acc; float _e63 = acc; float inside = metal::min(m - _e61, (_e63 + piece) - m); int _e67 = i; if (naga_mod(_e67, 2) == 0) { dash = metal::clamp(0.5 + inside, 0.0, 1.0); } else { dash = metal::clamp(0.5 - inside, 0.0, 1.0); } break; } float _e82 = acc; acc = _e82 + piece; } } float _e87 = cover; float _e88 = dash; cover = _e87 * _e88; } metal::float4 _e92 = line.color; metal::float4 _e96 = item.tint; float _e101 = item.tint.w; metal::float3 rgb = metal::mix(_e92.xyz, _e96.xyz, _e101); float _e106 = line.color.w; float _e107 = cover; float a_2 = _e106 * _e107; return fs_mainOutput { metal::float4(rgb * a_2, a_2) }; } uint naga_f2u32(float value) { return static_cast<uint>(metal::clamp(value, 0.0, 4294967000.0)); } struct fs_pickInput { float across_1 [[user(loc0), center_perspective]]; float along_1 [[user(loc1), center_perspective]]; float length_px_1 [[user(loc2), center_perspective]]; float stipple [[user(loc3), center_perspective]]; metal::float3 view_pos [[user(loc4), center_perspective]]; float seg_2 [[user(loc5), flat]]; }; struct fs_pickOutput { metal::uint2 ids [[color(0)]]; float depth [[color(1)]]; }; fragment fs_pickOutput fs_pick( fs_pickInput varyings_3 [[stage_in]] , metal::float4 clip_1 [[position]] , constant Frame& frame [[buffer(0)]] , constant LineMaterial& line [[buffer(1)]] , constant Item& item [[buffer(2)]] ) { const VOut in_1 = { clip_1, varyings_3.across_1, varyings_3.along_1, varyings_3.length_px_1, varyings_3.stipple, varyings_3.view_pos, varyings_3.seg_2 }; PickOut o = {}; bool _e3 = section_clipped(in_1.view_pos, -1, frame); if (_e3) { metal::discard_fragment(); } float _e7 = line.params.x; float _e11 = segment_cover(_e7, in_1.across, in_1.along, in_1.length_px, frame); if (_e11 < 0.5) { metal::discard_fragment(); } float _e19 = item.params.x; o.ids = metal::uint2(naga_f2u32(_e19), naga_f2u32(in_1.seg + 0.5)); o.depth = in_1.clip.z; PickOut _e29 = o; const auto _tmp_2 = _e29; return fs_pickOutput { _tmp_2.ids, _tmp_2.depth }; } `, Entries: []gpu.EntryPoint{ {Name: "vs_main", Stage: gpu.ShaderStageVertex, Inputs: []gpu.VertexInput{ {Name: "p0", Location: 0, Type: gpu.ShaderTypeFloat32x3}, {Name: "p1", Location: 1, Type: gpu.ShaderTypeFloat32x3}, {Name: "d0", Location: 2, Type: gpu.ShaderTypeFloat32}, {Name: "corner", Location: 3, Type: gpu.ShaderTypeFloat32x2}, }, Bindings: []gpu.ShaderBinding{ {Name: "frame", Group: 0, Binding: 0, Kind: gpu.BindingUniformBuffer, Size: 384, Register: gpu.ShaderRegister{Class: "b", Space: 0, Index: 0}, MetalIndex: 0}, {Name: "line", Group: 1, Binding: 0, Kind: gpu.BindingUniformBuffer, Size: 64, Register: gpu.ShaderRegister{Class: "b", Space: 1, Index: 0}, MetalIndex: 1}, {Name: "item", Group: 2, Binding: 0, Kind: gpu.BindingUniformBuffer, Size: 112, Register: gpu.ShaderRegister{Class: "b", Space: 2, Index: 0}, MetalIndex: 2}, }, }, {Name: "vs_pick", Stage: gpu.ShaderStageVertex, Inputs: []gpu.VertexInput{ {Name: "p0", Location: 0, Type: gpu.ShaderTypeFloat32x3}, {Name: "p1", Location: 1, Type: gpu.ShaderTypeFloat32x3}, {Name: "d0", Location: 2, Type: gpu.ShaderTypeFloat32}, {Name: "corner", Location: 3, Type: gpu.ShaderTypeFloat32x2}, {Name: "seg", Location: 4, Type: gpu.ShaderTypeFloat32}, }, Bindings: []gpu.ShaderBinding{ {Name: "frame", Group: 0, Binding: 0, Kind: gpu.BindingUniformBuffer, Size: 384, Register: gpu.ShaderRegister{Class: "b", Space: 0, Index: 0}, MetalIndex: 0}, {Name: "line", Group: 1, Binding: 0, Kind: gpu.BindingUniformBuffer, Size: 64, Register: gpu.ShaderRegister{Class: "b", Space: 1, Index: 0}, MetalIndex: 1}, {Name: "item", Group: 2, Binding: 0, Kind: gpu.BindingUniformBuffer, Size: 112, Register: gpu.ShaderRegister{Class: "b", Space: 2, Index: 0}, MetalIndex: 2}, }, }, {Name: "fs_main", Stage: gpu.ShaderStageFragment, Bindings: []gpu.ShaderBinding{ {Name: "frame", Group: 0, Binding: 0, Kind: gpu.BindingUniformBuffer, Size: 384, Register: gpu.ShaderRegister{Class: "b", Space: 0, Index: 0}, MetalIndex: 0}, {Name: "line", Group: 1, Binding: 0, Kind: gpu.BindingUniformBuffer, Size: 64, Register: gpu.ShaderRegister{Class: "b", Space: 1, Index: 0}, MetalIndex: 1}, {Name: "item", Group: 2, Binding: 0, Kind: gpu.BindingUniformBuffer, Size: 112, Register: gpu.ShaderRegister{Class: "b", Space: 2, Index: 0}, MetalIndex: 2}, }, }, {Name: "fs_pick", Stage: gpu.ShaderStageFragment, Bindings: []gpu.ShaderBinding{ {Name: "frame", Group: 0, Binding: 0, Kind: gpu.BindingUniformBuffer, Size: 384, Register: gpu.ShaderRegister{Class: "b", Space: 0, Index: 0}, MetalIndex: 0}, {Name: "line", Group: 1, Binding: 0, Kind: gpu.BindingUniformBuffer, Size: 64, Register: gpu.ShaderRegister{Class: "b", Space: 1, Index: 0}, MetalIndex: 1}, {Name: "item", Group: 2, Binding: 0, Kind: gpu.BindingUniformBuffer, Size: 112, Register: gpu.ShaderRegister{Class: "b", Space: 2, Index: 0}, MetalIndex: 2}, }, }, }, }
Line is line.wgsl.
var Mesh = &gpu.Shader{ Name: "mesh", WGSL: `// The meshes of triangles of viewport3d (SPEC §27.8): unlit and Blinn-Phong shading. The vertex entry points differ in the // attributes they read (p position, n normal, u texture coordinates, c color); the renderer picks the one that a mesh has. // The positions are float32 relative to the mesh's origin and the model-view matrix of each instance takes them to the view, // whose origin is the eye (D30): the numbers are small near the camera. // What the shaders of viewport3d share (SPEC §27.8): the frame's uniform data, the record of an item, and the section planes. struct Light { // dir points to the light, in view space. dir: vec4<f32>, // color is the light's color times its intensity. color: vec4<f32>, } struct Frame { proj: mat4x4<f32>, // lights are the light at the camera, then up to four directional ones; an unused one has a black color. lights: array<Light, 5>, ambient: vec4<f32>, // viewport is the width and the height of the target in pixels, and their inverses. viewport: vec4<f32>, // params are the near plane's distance, the device pixel ratio and two unused numbers. params: vec4<f32>, // planes are the section planes in view space, relative to the eye (D30): a point p with dot(plane.xyz, p) + plane.w > 0 is // cut away. planes: array<vec4<f32>, 6>, // section is the number of planes in x. section: vec4<f32>, } @group(0) @binding(0) var<uniform> frame: Frame; // Item is the record of a line, a point or a picked item: the matrix that takes the mesh's positions to the view, relative to // the eye, a tint (the highlight of a selected item: its color, and the part of it that replaces the material's in a), and // numbers for the shader that reads it. struct Item { mv: mat4x4<f32>, tint: vec4<f32>, params: vec4<f32>, extra: vec4<f32>, } // section_clipped says whether a point of the view space is cut away by a section plane. skip is the plane to leave out (the // plane that a cap lies on), or -1. fn section_clipped(p: vec3<f32>, skip: i32) -> bool { let count = i32(frame.section.x); for (var i = 0; i < 6; i = i + 1) { if (i >= count) { break; } if (i == skip) { continue; } let plane = frame.planes[i]; if (dot(plane.xyz, p) + plane.w > 0.0) { return true; } } return false; } // PickOut is what a pick pass writes for a pixel: the number of the item (from 1, 0 for nothing) and of the primitive in it, and // the depth. struct PickOut { @location(0) ids: vec2<u32>, @location(1) depth: f32, } struct Material { color: vec4<f32>, // specular is the highlight's color, and its exponent in w. specular: vec4<f32>, // flags are: x 1 when the material is lit, y 1 for vertex colors, z 1 for a texture, w 1 when the output is opaque. flags: vec4<f32>, } @group(1) @binding(0) var<uniform> material: Material; @group(1) @binding(1) var tex: texture_2d<f32>; @group(1) @binding(2) var samp: sampler; struct VOut { @builtin(position) clip: vec4<f32>, @location(0) view_pos: vec3<f32>, @location(1) normal: vec3<f32>, @location(2) uv: vec2<f32>, @location(3) color: vec4<f32>, // tint is the color of a selected item, and in a the part of it that replaces the material's. @location(4) tint: vec4<f32>, } fn vertex_common(p: vec3<f32>, n: vec3<f32>, uv: vec2<f32>, c: vec4<f32>, m0: vec4<f32>, m1: vec4<f32>, m2: vec4<f32>, m3: vec4<f32>, tint: vec4<f32>) -> VOut { let mv = mat4x4<f32>(m0, m1, m2, m3); let vp = mv * vec4<f32>(p, 1.0); var o: VOut; o.view_pos = vp.xyz; o.clip = frame.proj * vp; o.normal = (mv * vec4<f32>(n, 0.0)).xyz; o.uv = uv; o.color = c; o.tint = tint; return o; } @vertex fn vs_p(@location(0) p: vec3<f32>, @location(4) m0: vec4<f32>, @location(5) m1: vec4<f32>, @location(6) m2: vec4<f32>, @location(7) m3: vec4<f32>, @location(8) tint: vec4<f32>) -> VOut { return vertex_common(p, vec3<f32>(0.0), vec2<f32>(0.0), vec4<f32>(1.0), m0, m1, m2, m3, tint); } @vertex fn vs_pn(@location(0) p: vec3<f32>, @location(1) n: vec3<f32>, @location(4) m0: vec4<f32>, @location(5) m1: vec4<f32>, @location(6) m2: vec4<f32>, @location(7) m3: vec4<f32>, @location(8) tint: vec4<f32>) -> VOut { return vertex_common(p, n, vec2<f32>(0.0), vec4<f32>(1.0), m0, m1, m2, m3, tint); } @vertex fn vs_pu(@location(0) p: vec3<f32>, @location(2) uv: vec2<f32>, @location(4) m0: vec4<f32>, @location(5) m1: vec4<f32>, @location(6) m2: vec4<f32>, @location(7) m3: vec4<f32>, @location(8) tint: vec4<f32>) -> VOut { return vertex_common(p, vec3<f32>(0.0), uv, vec4<f32>(1.0), m0, m1, m2, m3, tint); } @vertex fn vs_pc(@location(0) p: vec3<f32>, @location(3) c: vec4<f32>, @location(4) m0: vec4<f32>, @location(5) m1: vec4<f32>, @location(6) m2: vec4<f32>, @location(7) m3: vec4<f32>, @location(8) tint: vec4<f32>) -> VOut { return vertex_common(p, vec3<f32>(0.0), vec2<f32>(0.0), c, m0, m1, m2, m3, tint); } @vertex fn vs_pnu(@location(0) p: vec3<f32>, @location(1) n: vec3<f32>, @location(2) uv: vec2<f32>, @location(4) m0: vec4<f32>, @location(5) m1: vec4<f32>, @location(6) m2: vec4<f32>, @location(7) m3: vec4<f32>, @location(8) tint: vec4<f32>) -> VOut { return vertex_common(p, n, uv, vec4<f32>(1.0), m0, m1, m2, m3, tint); } @vertex fn vs_pnc(@location(0) p: vec3<f32>, @location(1) n: vec3<f32>, @location(3) c: vec4<f32>, @location(4) m0: vec4<f32>, @location(5) m1: vec4<f32>, @location(6) m2: vec4<f32>, @location(7) m3: vec4<f32>, @location(8) tint: vec4<f32>) -> VOut { return vertex_common(p, n, vec2<f32>(0.0), c, m0, m1, m2, m3, tint); } @vertex fn vs_puc(@location(0) p: vec3<f32>, @location(2) uv: vec2<f32>, @location(3) c: vec4<f32>, @location(4) m0: vec4<f32>, @location(5) m1: vec4<f32>, @location(6) m2: vec4<f32>, @location(7) m3: vec4<f32>, @location(8) tint: vec4<f32>) -> VOut { return vertex_common(p, vec3<f32>(0.0), uv, c, m0, m1, m2, m3, tint); } @vertex fn vs_pnuc(@location(0) p: vec3<f32>, @location(1) n: vec3<f32>, @location(2) uv: vec2<f32>, @location(3) c: vec4<f32>, @location(4) m0: vec4<f32>, @location(5) m1: vec4<f32>, @location(6) m2: vec4<f32>, @location(7) m3: vec4<f32>, @location(8) tint: vec4<f32>) -> VOut { return vertex_common(p, n, uv, c, m0, m1, m2, m3, tint); } @fragment fn fs_main(in: VOut, @builtin(front_facing) front: bool) -> @location(0) vec4<f32> { // Derivatives and sampling are taken where the control flow is uniform, then selected. let flat_normal = cross(dpdx(in.view_pos), dpdy(in.view_pos)); let texel = textureSample(tex, samp, in.uv); if (section_clipped(in.view_pos, -1)) { discard; } var base = material.color; if (material.flags.y > 0.5) { base = base * in.color; } if (material.flags.z > 0.5) { base = base * texel; } var rgb = mix(base.rgb, in.tint.rgb, in.tint.a); if (material.flags.x > 0.5) { let v = normalize(-in.view_pos); var n = in.normal; if (dot(n, n) < 0.25) { // No normals: the face is flat, and shaded as seen from the camera. n = normalize(flat_normal); if (dot(n, v) < 0.0) { n = -n; } } else { n = normalize(n); if (!front) { n = -n; } } var light = frame.ambient.rgb; var spec = vec3<f32>(0.0); for (var i = 0; i < 5; i = i + 1) { let l = normalize(frame.lights[i].dir.xyz); let nl = max(dot(n, l), 0.0); light = light + frame.lights[i].color.rgb * nl; if (nl > 0.0) { let h = normalize(l + v); spec = spec + frame.lights[i].color.rgb * pow(max(dot(n, h), 0.0), material.specular.w); } } rgb = rgb * light + material.specular.rgb * spec; } var a = base.a; if (material.flags.w > 0.5) { a = 1.0; } // Premultiplied alpha, for the blending of transparent items. return vec4<f32>(min(rgb, vec3<f32>(1.0)) * a, a); } // fs_mask is the fragment function of the mask that the outline of a selection is made from: white where a selected item is. @fragment fn fs_mask(in: VOut) -> @location(0) vec4<f32> { if (section_clipped(in.view_pos, -1)) { discard; } return vec4<f32>(1.0); } `, SPIRV: spirvMesh, HLSL: `struct Light { float4 dir; float4 color; }; struct Frame { row_major float4x4 proj; Light lights[5]; float4 ambient; float4 viewport; float4 params; float4 planes[6]; float4 section; }; struct Item { row_major float4x4 mv; float4 tint; float4 params; float4 extra; }; struct PickOut { uint2 ids : LOC0; float depth : LOC1; }; struct Material { float4 color; float4 specular; float4 flags; }; struct VOut { float4 clip_ : SV_Position; float3 view_pos : LOC0; float3 normal : LOC1; float2 uv : LOC2; float4 color : LOC3; float4 tint : LOC4; }; cbuffer frame : register(b0, space0) { Frame frame; } cbuffer material : register(b0, space1) { Material material; } Texture2D<float4> tex : register(t1, space1); SamplerState samp : register(s2, space1); struct VertexOutput_vs_p { float3 view_pos : LOC0; float3 normal : LOC1; float2 uv_5 : LOC2; float4 color : LOC3; float4 tint_9 : LOC4; float4 clip_ : SV_Position; }; struct VertexOutput_vs_pn { float3 view_pos_1 : LOC0; float3 normal_1 : LOC1; float2 uv_6 : LOC2; float4 color_1 : LOC3; float4 tint_10 : LOC4; float4 clip_1 : SV_Position; }; struct VertexOutput_vs_pu { float3 view_pos_2 : LOC0; float3 normal_2 : LOC1; float2 uv_7 : LOC2; float4 color_2 : LOC3; float4 tint_11 : LOC4; float4 clip_2 : SV_Position; }; struct VertexOutput_vs_pc { float3 view_pos_3 : LOC0; float3 normal_3 : LOC1; float2 uv_8 : LOC2; float4 color_3 : LOC3; float4 tint_12 : LOC4; float4 clip_3 : SV_Position; }; struct VertexOutput_vs_pnu { float3 view_pos_4 : LOC0; float3 normal_4 : LOC1; float2 uv_9 : LOC2; float4 color_4 : LOC3; float4 tint_13 : LOC4; float4 clip_4 : SV_Position; }; struct VertexOutput_vs_pnc { float3 view_pos_5 : LOC0; float3 normal_5 : LOC1; float2 uv_10 : LOC2; float4 color_5 : LOC3; float4 tint_14 : LOC4; float4 clip_5 : SV_Position; }; struct VertexOutput_vs_puc { float3 view_pos_6 : LOC0; float3 normal_6 : LOC1; float2 uv_11 : LOC2; float4 color_6 : LOC3; float4 tint_15 : LOC4; float4 clip_6 : SV_Position; }; struct VertexOutput_vs_pnuc { float3 view_pos_7 : LOC0; float3 normal_7 : LOC1; float2 uv_12 : LOC2; float4 color_7 : LOC3; float4 tint_16 : LOC4; float4 clip_7 : SV_Position; }; struct FragmentInput_fs_main { float3 view_pos_8 : LOC0; float3 normal_8 : LOC1; float2 uv_13 : LOC2; float4 color_8 : LOC3; float4 tint_17 : LOC4; float4 clip_8 : SV_Position; bool front_1 : SV_IsFrontFace; }; struct FragmentInput_fs_mask { float3 view_pos_9 : LOC0; float3 normal_9 : LOC1; float2 uv_14 : LOC2; float4 color_9 : LOC3; float4 tint_18 : LOC4; float4 clip_9 : SV_Position; }; int naga_f2i32(float value) { return int(clamp(value, -2147483600.0, 2147483500.0)); } bool section_clipped(float3 p_8, int skip) { int i_1 = int(0); float _e5 = frame.section.x; int count = naga_f2i32(_e5); uint2 loop_bound = uint2(4294967295u, 4294967295u); bool loop_init = true; while(true) { if (all(loop_bound == uint2(0u, 0u))) { break; } loop_bound -= uint2(loop_bound.y == 0u, 1u); if (!loop_init) { int _e28 = i_1; i_1 = asint(asuint(_e28) + asuint(int(1))); } loop_init = false; int _e9 = i_1; if ((_e9 < int(6))) { } else { break; } { int _e12 = i_1; if ((_e12 >= count)) { break; } int _e14 = i_1; if ((_e14 == skip)) { continue; } int _e18 = i_1; float4 plane = frame.planes[_e18]; if (((dot(plane.xyz, p_8) + plane.w) > 0.0)) { return true; } } } return false; } VOut vertex_common(float3 p_9, float3 n_5, float2 uv_4, float4 c_4, float4 m0_8, float4 m1_8, float4 m2_8, float4 m3_8, float4 tint_8) { VOut o = (VOut)0; float4x4 mv = float4x4(m0_8, m1_8, m2_8, m3_8); float4 vp = mul(float4(p_9, 1.0), mv); o.view_pos = vp.xyz; float4x4 _e19 = frame.proj; o.clip_ = mul(vp, _e19); o.normal = mul(float4(n_5, 0.0), mv).xyz; o.uv = uv_4; o.color = c_4; o.tint = tint_8; VOut _e29 = o; VOut vout = _e29; return vout; } VertexOutput_vs_p vs_p(float3 p : LOC0, float4 m0_ : LOC4, float4 m1_ : LOC5, float4 m2_ : LOC6, float4 m3_ : LOC7, float4 tint : LOC8) { VOut _e12 = vertex_common(p, (0.0).xxx, (0.0).xx, (1.0).xxxx, m0_, m1_, m2_, m3_, tint); VOut vout_1 = _e12; VertexOutput_vs_p vout_1_ = { vout_1.view_pos, vout_1.normal, vout_1.uv, vout_1.color, vout_1.tint, vout_1.clip_ }; return vout_1_; } VertexOutput_vs_pn vs_pn(float3 p_1 : LOC0, float3 n : LOC1, float4 m0_1 : LOC4, float4 m1_1 : LOC5, float4 m2_1 : LOC6, float4 m3_1 : LOC7, float4 tint_1 : LOC8) { VOut _e11 = vertex_common(p_1, n, (0.0).xx, (1.0).xxxx, m0_1, m1_1, m2_1, m3_1, tint_1); VOut vout_2 = _e11; VertexOutput_vs_pn vout_2_ = { vout_2.view_pos, vout_2.normal, vout_2.uv, vout_2.color, vout_2.tint, vout_2.clip_ }; return vout_2_; } VertexOutput_vs_pu vs_pu(float3 p_2 : LOC0, float2 uv : LOC2, float4 m0_2 : LOC4, float4 m1_2 : LOC5, float4 m2_2 : LOC6, float4 m3_2 : LOC7, float4 tint_2 : LOC8) { VOut _e11 = vertex_common(p_2, (0.0).xxx, uv, (1.0).xxxx, m0_2, m1_2, m2_2, m3_2, tint_2); VOut vout_3 = _e11; VertexOutput_vs_pu vout_3_ = { vout_3.view_pos, vout_3.normal, vout_3.uv, vout_3.color, vout_3.tint, vout_3.clip_ }; return vout_3_; } VertexOutput_vs_pc vs_pc(float3 p_3 : LOC0, float4 c : LOC3, float4 m0_3 : LOC4, float4 m1_3 : LOC5, float4 m2_3 : LOC6, float4 m3_3 : LOC7, float4 tint_3 : LOC8) { VOut _e11 = vertex_common(p_3, (0.0).xxx, (0.0).xx, c, m0_3, m1_3, m2_3, m3_3, tint_3); VOut vout_4 = _e11; VertexOutput_vs_pc vout_4_ = { vout_4.view_pos, vout_4.normal, vout_4.uv, vout_4.color, vout_4.tint, vout_4.clip_ }; return vout_4_; } VertexOutput_vs_pnu vs_pnu(float3 p_4 : LOC0, float3 n_1 : LOC1, float2 uv_1 : LOC2, float4 m0_4 : LOC4, float4 m1_4 : LOC5, float4 m2_4 : LOC6, float4 m3_4 : LOC7, float4 tint_4 : LOC8) { VOut _e10 = vertex_common(p_4, n_1, uv_1, (1.0).xxxx, m0_4, m1_4, m2_4, m3_4, tint_4); VOut vout_5 = _e10; VertexOutput_vs_pnu vout_5_ = { vout_5.view_pos, vout_5.normal, vout_5.uv, vout_5.color, vout_5.tint, vout_5.clip_ }; return vout_5_; } VertexOutput_vs_pnc vs_pnc(float3 p_5 : LOC0, float3 n_2 : LOC1, float4 c_1 : LOC3, float4 m0_5 : LOC4, float4 m1_5 : LOC5, float4 m2_5 : LOC6, float4 m3_5 : LOC7, float4 tint_5 : LOC8) { VOut _e10 = vertex_common(p_5, n_2, (0.0).xx, c_1, m0_5, m1_5, m2_5, m3_5, tint_5); VOut vout_6 = _e10; VertexOutput_vs_pnc vout_6_ = { vout_6.view_pos, vout_6.normal, vout_6.uv, vout_6.color, vout_6.tint, vout_6.clip_ }; return vout_6_; } VertexOutput_vs_puc vs_puc(float3 p_6 : LOC0, float2 uv_2 : LOC2, float4 c_2 : LOC3, float4 m0_6 : LOC4, float4 m1_6 : LOC5, float4 m2_6 : LOC6, float4 m3_6 : LOC7, float4 tint_6 : LOC8) { VOut _e10 = vertex_common(p_6, (0.0).xxx, uv_2, c_2, m0_6, m1_6, m2_6, m3_6, tint_6); VOut vout_7 = _e10; VertexOutput_vs_puc vout_7_ = { vout_7.view_pos, vout_7.normal, vout_7.uv, vout_7.color, vout_7.tint, vout_7.clip_ }; return vout_7_; } VertexOutput_vs_pnuc vs_pnuc(float3 p_7 : LOC0, float3 n_3 : LOC1, float2 uv_3 : LOC2, float4 c_3 : LOC3, float4 m0_7 : LOC4, float4 m1_7 : LOC5, float4 m2_7 : LOC6, float4 m3_7 : LOC7, float4 tint_7 : LOC8) { VOut _e9 = vertex_common(p_7, n_3, uv_3, c_3, m0_7, m1_7, m2_7, m3_7, tint_7); VOut vout_8 = _e9; VertexOutput_vs_pnuc vout_8_ = { vout_8.view_pos, vout_8.normal, vout_8.uv, vout_8.color, vout_8.tint, vout_8.clip_ }; return vout_8_; } float4 fs_main(FragmentInput_fs_main fragmentinput_fs_main) : SV_Target0 { VOut in_ = { fragmentinput_fs_main.clip_8, fragmentinput_fs_main.view_pos_8, fragmentinput_fs_main.normal_8, fragmentinput_fs_main.uv_13, fragmentinput_fs_main.color_8, fragmentinput_fs_main.tint_17 }; bool front = fragmentinput_fs_main.front_1; float4 base = (float4)0; float3 rgb = (float3)0; float3 n_4 = (float3)0; float3 light = (float3)0; float3 spec = (0.0).xxx; int i = int(0); float a = (float)0; float3 _e3 = ddx(in_.view_pos); float3 _e5 = ddy(in_.view_pos); float3 flat_normal = cross(_e3, _e5); float4 texel = tex.Sample(samp, in_.uv); const bool _e13 = section_clipped(in_.view_pos, int(-1)); if (_e13) { discard; } float4 _e16 = material.color; base = _e16; float _e21 = material.flags.y; if ((_e21 > 0.5)) { float4 _e24 = base; base = (_e24 * in_.color); } float _e30 = material.flags.z; if ((_e30 > 0.5)) { float4 _e33 = base; base = (_e33 * texel); } float4 _e35 = base; rgb = lerp(_e35.xyz, in_.tint.xyz, in_.tint.w); float _e46 = material.flags.x; if ((_e46 > 0.5)) { float3 v = normalize(-(in_.view_pos)); n_4 = in_.normal; float3 _e54 = n_4; float3 _e55 = n_4; if ((dot(_e54, _e55) < 0.25)) { n_4 = normalize(flat_normal); float3 _e60 = n_4; if ((dot(_e60, v) < 0.0)) { float3 _e64 = n_4; n_4 = -(_e64); } } else { float3 _e66 = n_4; n_4 = normalize(_e66); if (!(front)) { float3 _e69 = n_4; n_4 = -(_e69); } } float4 _e73 = frame.ambient; light = _e73.xyz; uint2 loop_bound_1 = uint2(4294967295u, 4294967295u); bool loop_init_1 = true; while(true) { if (all(loop_bound_1 == uint2(0u, 0u))) { break; } loop_bound_1 -= uint2(loop_bound_1.y == 0u, 1u); if (!loop_init_1) { int _e129 = i; i = asint(asuint(_e129) + asuint(int(1))); } loop_init_1 = false; int _e81 = i; if ((_e81 < int(5))) { } else { break; } { int _e86 = i; float4 _e89 = frame.lights[_e86].dir; float3 l = normalize(_e89.xyz); float3 _e92 = n_4; float nl = max(dot(_e92, l), 0.0); float3 _e96 = light; int _e99 = i; float4 _e102 = frame.lights[_e99].color; light = (_e96 + (_e102.xyz * nl)); if ((nl > 0.0)) { float3 h = normalize((l + v)); float3 _e110 = spec; int _e113 = i; float4 _e116 = frame.lights[_e113].color; float3 _e118 = n_4; float _e125 = material.specular.w; spec = (_e110 + (_e116.xyz * pow(max(dot(_e118, h), 0.0), _e125))); } } } float3 _e132 = rgb; float3 _e133 = light; float4 _e137 = material.specular; float3 _e139 = spec; rgb = ((_e132 * _e133) + (_e137.xyz * _e139)); } float _e143 = base.w; a = _e143; float _e148 = material.flags.w; if ((_e148 > 0.5)) { a = 1.0; } float3 _e152 = rgb; float _e156 = a; float _e158 = a; return float4((min(_e152, (1.0).xxx) * _e156), _e158); } float4 fs_mask(FragmentInput_fs_mask fragmentinput_fs_mask) : SV_Target0 { VOut in_1 = { fragmentinput_fs_mask.clip_9, fragmentinput_fs_mask.view_pos_9, fragmentinput_fs_mask.normal_9, fragmentinput_fs_mask.uv_14, fragmentinput_fs_mask.color_9, fragmentinput_fs_mask.tint_18 }; const bool _e3 = section_clipped(in_1.view_pos, int(-1)); if (_e3) { discard; } return (1.0).xxxx; } `, MSL: `// language: metal2.4 #include <metal_stdlib> #include <simd/simd.h> using metal::uint; struct Light { metal::float4 dir; metal::float4 color; }; struct type_3 { Light inner[5]; }; struct type_4 { metal::float4 inner[6]; }; struct Frame { metal::float4x4 proj; type_3 lights; metal::float4 ambient; metal::float4 viewport; metal::float4 params; type_4 planes; metal::float4 section; }; struct Item { metal::float4x4 mv; metal::float4 tint; metal::float4 params; metal::float4 extra; }; struct PickOut { metal::uint2 ids; float depth; char _pad2[4]; }; struct Material { metal::float4 color; metal::float4 specular; metal::float4 flags; }; struct VOut { metal::float4 clip; metal::float3 view_pos; metal::float3 normal; metal::float2 uv; char _pad4[8]; metal::float4 color; metal::float4 tint; }; int naga_f2i32(float value) { return static_cast<int>(metal::clamp(value, -2147483600.0, 2147483500.0)); } bool section_clipped( metal::float3 p_8, int skip, constant Frame& frame ) { int i_1 = 0; float _e5 = frame.section.x; int count = naga_f2i32(_e5); uint2 loop_bound = uint2(4294967295u); bool loop_init = true; while(true) { if (metal::all(loop_bound == uint2(0u))) { break; } loop_bound -= uint2(loop_bound.y == 0u, 1u); if (!loop_init) { int _e28 = i_1; i_1 = as_type<int>(as_type<uint>(_e28) + as_type<uint>(1)); } loop_init = false; int _e9 = i_1; if (_e9 < 6) { } else { break; } { int _e12 = i_1; if (_e12 >= count) { break; } int _e14 = i_1; if (_e14 == skip) { continue; } int _e18 = i_1; metal::float4 plane = frame.planes.inner[_e18]; if ((metal::dot(plane.xyz, p_8) + plane.w) > 0.0) { return true; } } } return false; } VOut vertex_common( metal::float3 p_9, metal::float3 n_5, metal::float2 uv_4, metal::float4 c_4, metal::float4 m0_8, metal::float4 m1_8, metal::float4 m2_8, metal::float4 m3_8, metal::float4 tint_8, constant Frame& frame ) { VOut o = {}; metal::float4x4 mv = metal::float4x4(m0_8, m1_8, m2_8, m3_8); metal::float4 vp = mv * metal::float4(p_9, 1.0); o.view_pos = vp.xyz; metal::float4x4 _e19 = frame.proj; o.clip = _e19 * vp; o.normal = (mv * metal::float4(n_5, 0.0)).xyz; o.uv = uv_4; o.color = c_4; o.tint = tint_8; VOut _e29 = o; return _e29; } struct vs_pInput { metal::float3 p [[attribute(0)]]; metal::float4 m0_ [[attribute(4)]]; metal::float4 m1_ [[attribute(5)]]; metal::float4 m2_ [[attribute(6)]]; metal::float4 m3_ [[attribute(7)]]; metal::float4 tint [[attribute(8)]]; }; struct vs_pOutput { metal::float4 clip [[position]]; metal::float3 view_pos [[user(loc0), center_perspective]]; metal::float3 normal [[user(loc1), center_perspective]]; metal::float2 uv [[user(loc2), center_perspective]]; metal::float4 color [[user(loc3), center_perspective]]; metal::float4 tint [[user(loc4), center_perspective]]; }; vertex vs_pOutput vs_p( vs_pInput varyings [[stage_in]] , constant Frame& frame [[buffer(0)]] ) { const auto p = varyings.p; const auto m0_ = varyings.m0_; const auto m1_ = varyings.m1_; const auto m2_ = varyings.m2_; const auto m3_ = varyings.m3_; const auto tint = varyings.tint; VOut _e12 = vertex_common(p, metal::float3(0.0), metal::float2(0.0), metal::float4(1.0), m0_, m1_, m2_, m3_, tint, frame); const auto _tmp = _e12; return vs_pOutput { _tmp.clip, _tmp.view_pos, _tmp.normal, _tmp.uv, _tmp.color, _tmp.tint }; } struct vs_pnInput { metal::float3 p_1 [[attribute(0)]]; metal::float3 n [[attribute(1)]]; metal::float4 m0_1 [[attribute(4)]]; metal::float4 m1_1 [[attribute(5)]]; metal::float4 m2_1 [[attribute(6)]]; metal::float4 m3_1 [[attribute(7)]]; metal::float4 tint_1 [[attribute(8)]]; }; struct vs_pnOutput { metal::float4 clip [[position]]; metal::float3 view_pos [[user(loc0), center_perspective]]; metal::float3 normal [[user(loc1), center_perspective]]; metal::float2 uv [[user(loc2), center_perspective]]; metal::float4 color [[user(loc3), center_perspective]]; metal::float4 tint [[user(loc4), center_perspective]]; }; vertex vs_pnOutput vs_pn( vs_pnInput varyings_1 [[stage_in]] , constant Frame& frame [[buffer(0)]] ) { const auto p_1 = varyings_1.p_1; const auto n = varyings_1.n; const auto m0_1 = varyings_1.m0_1; const auto m1_1 = varyings_1.m1_1; const auto m2_1 = varyings_1.m2_1; const auto m3_1 = varyings_1.m3_1; const auto tint_1 = varyings_1.tint_1; VOut _e11 = vertex_common(p_1, n, metal::float2(0.0), metal::float4(1.0), m0_1, m1_1, m2_1, m3_1, tint_1, frame); const auto _tmp_1 = _e11; return vs_pnOutput { _tmp_1.clip, _tmp_1.view_pos, _tmp_1.normal, _tmp_1.uv, _tmp_1.color, _tmp_1.tint }; } struct vs_puInput { metal::float3 p_2 [[attribute(0)]]; metal::float2 uv [[attribute(2)]]; metal::float4 m0_2 [[attribute(4)]]; metal::float4 m1_2 [[attribute(5)]]; metal::float4 m2_2 [[attribute(6)]]; metal::float4 m3_2 [[attribute(7)]]; metal::float4 tint_2 [[attribute(8)]]; }; struct vs_puOutput { metal::float4 clip [[position]]; metal::float3 view_pos [[user(loc0), center_perspective]]; metal::float3 normal [[user(loc1), center_perspective]]; metal::float2 uv [[user(loc2), center_perspective]]; metal::float4 color [[user(loc3), center_perspective]]; metal::float4 tint [[user(loc4), center_perspective]]; }; vertex vs_puOutput vs_pu( vs_puInput varyings_2 [[stage_in]] , constant Frame& frame [[buffer(0)]] ) { const auto p_2 = varyings_2.p_2; const auto uv = varyings_2.uv; const auto m0_2 = varyings_2.m0_2; const auto m1_2 = varyings_2.m1_2; const auto m2_2 = varyings_2.m2_2; const auto m3_2 = varyings_2.m3_2; const auto tint_2 = varyings_2.tint_2; VOut _e11 = vertex_common(p_2, metal::float3(0.0), uv, metal::float4(1.0), m0_2, m1_2, m2_2, m3_2, tint_2, frame); const auto _tmp_2 = _e11; return vs_puOutput { _tmp_2.clip, _tmp_2.view_pos, _tmp_2.normal, _tmp_2.uv, _tmp_2.color, _tmp_2.tint }; } struct vs_pcInput { metal::float3 p_3 [[attribute(0)]]; metal::float4 c [[attribute(3)]]; metal::float4 m0_3 [[attribute(4)]]; metal::float4 m1_3 [[attribute(5)]]; metal::float4 m2_3 [[attribute(6)]]; metal::float4 m3_3 [[attribute(7)]]; metal::float4 tint_3 [[attribute(8)]]; }; struct vs_pcOutput { metal::float4 clip [[position]]; metal::float3 view_pos [[user(loc0), center_perspective]]; metal::float3 normal [[user(loc1), center_perspective]]; metal::float2 uv [[user(loc2), center_perspective]]; metal::float4 color [[user(loc3), center_perspective]]; metal::float4 tint [[user(loc4), center_perspective]]; }; vertex vs_pcOutput vs_pc( vs_pcInput varyings_3 [[stage_in]] , constant Frame& frame [[buffer(0)]] ) { const auto p_3 = varyings_3.p_3; const auto c = varyings_3.c; const auto m0_3 = varyings_3.m0_3; const auto m1_3 = varyings_3.m1_3; const auto m2_3 = varyings_3.m2_3; const auto m3_3 = varyings_3.m3_3; const auto tint_3 = varyings_3.tint_3; VOut _e11 = vertex_common(p_3, metal::float3(0.0), metal::float2(0.0), c, m0_3, m1_3, m2_3, m3_3, tint_3, frame); const auto _tmp_3 = _e11; return vs_pcOutput { _tmp_3.clip, _tmp_3.view_pos, _tmp_3.normal, _tmp_3.uv, _tmp_3.color, _tmp_3.tint }; } struct vs_pnuInput { metal::float3 p_4 [[attribute(0)]]; metal::float3 n_1 [[attribute(1)]]; metal::float2 uv_1 [[attribute(2)]]; metal::float4 m0_4 [[attribute(4)]]; metal::float4 m1_4 [[attribute(5)]]; metal::float4 m2_4 [[attribute(6)]]; metal::float4 m3_4 [[attribute(7)]]; metal::float4 tint_4 [[attribute(8)]]; }; struct vs_pnuOutput { metal::float4 clip [[position]]; metal::float3 view_pos [[user(loc0), center_perspective]]; metal::float3 normal [[user(loc1), center_perspective]]; metal::float2 uv [[user(loc2), center_perspective]]; metal::float4 color [[user(loc3), center_perspective]]; metal::float4 tint [[user(loc4), center_perspective]]; }; vertex vs_pnuOutput vs_pnu( vs_pnuInput varyings_4 [[stage_in]] , constant Frame& frame [[buffer(0)]] ) { const auto p_4 = varyings_4.p_4; const auto n_1 = varyings_4.n_1; const auto uv_1 = varyings_4.uv_1; const auto m0_4 = varyings_4.m0_4; const auto m1_4 = varyings_4.m1_4; const auto m2_4 = varyings_4.m2_4; const auto m3_4 = varyings_4.m3_4; const auto tint_4 = varyings_4.tint_4; VOut _e10 = vertex_common(p_4, n_1, uv_1, metal::float4(1.0), m0_4, m1_4, m2_4, m3_4, tint_4, frame); const auto _tmp_4 = _e10; return vs_pnuOutput { _tmp_4.clip, _tmp_4.view_pos, _tmp_4.normal, _tmp_4.uv, _tmp_4.color, _tmp_4.tint }; } struct vs_pncInput { metal::float3 p_5 [[attribute(0)]]; metal::float3 n_2 [[attribute(1)]]; metal::float4 c_1 [[attribute(3)]]; metal::float4 m0_5 [[attribute(4)]]; metal::float4 m1_5 [[attribute(5)]]; metal::float4 m2_5 [[attribute(6)]]; metal::float4 m3_5 [[attribute(7)]]; metal::float4 tint_5 [[attribute(8)]]; }; struct vs_pncOutput { metal::float4 clip [[position]]; metal::float3 view_pos [[user(loc0), center_perspective]]; metal::float3 normal [[user(loc1), center_perspective]]; metal::float2 uv [[user(loc2), center_perspective]]; metal::float4 color [[user(loc3), center_perspective]]; metal::float4 tint [[user(loc4), center_perspective]]; }; vertex vs_pncOutput vs_pnc( vs_pncInput varyings_5 [[stage_in]] , constant Frame& frame [[buffer(0)]] ) { const auto p_5 = varyings_5.p_5; const auto n_2 = varyings_5.n_2; const auto c_1 = varyings_5.c_1; const auto m0_5 = varyings_5.m0_5; const auto m1_5 = varyings_5.m1_5; const auto m2_5 = varyings_5.m2_5; const auto m3_5 = varyings_5.m3_5; const auto tint_5 = varyings_5.tint_5; VOut _e10 = vertex_common(p_5, n_2, metal::float2(0.0), c_1, m0_5, m1_5, m2_5, m3_5, tint_5, frame); const auto _tmp_5 = _e10; return vs_pncOutput { _tmp_5.clip, _tmp_5.view_pos, _tmp_5.normal, _tmp_5.uv, _tmp_5.color, _tmp_5.tint }; } struct vs_pucInput { metal::float3 p_6 [[attribute(0)]]; metal::float2 uv_2 [[attribute(2)]]; metal::float4 c_2 [[attribute(3)]]; metal::float4 m0_6 [[attribute(4)]]; metal::float4 m1_6 [[attribute(5)]]; metal::float4 m2_6 [[attribute(6)]]; metal::float4 m3_6 [[attribute(7)]]; metal::float4 tint_6 [[attribute(8)]]; }; struct vs_pucOutput { metal::float4 clip [[position]]; metal::float3 view_pos [[user(loc0), center_perspective]]; metal::float3 normal [[user(loc1), center_perspective]]; metal::float2 uv [[user(loc2), center_perspective]]; metal::float4 color [[user(loc3), center_perspective]]; metal::float4 tint [[user(loc4), center_perspective]]; }; vertex vs_pucOutput vs_puc( vs_pucInput varyings_6 [[stage_in]] , constant Frame& frame [[buffer(0)]] ) { const auto p_6 = varyings_6.p_6; const auto uv_2 = varyings_6.uv_2; const auto c_2 = varyings_6.c_2; const auto m0_6 = varyings_6.m0_6; const auto m1_6 = varyings_6.m1_6; const auto m2_6 = varyings_6.m2_6; const auto m3_6 = varyings_6.m3_6; const auto tint_6 = varyings_6.tint_6; VOut _e10 = vertex_common(p_6, metal::float3(0.0), uv_2, c_2, m0_6, m1_6, m2_6, m3_6, tint_6, frame); const auto _tmp_6 = _e10; return vs_pucOutput { _tmp_6.clip, _tmp_6.view_pos, _tmp_6.normal, _tmp_6.uv, _tmp_6.color, _tmp_6.tint }; } struct vs_pnucInput { metal::float3 p_7 [[attribute(0)]]; metal::float3 n_3 [[attribute(1)]]; metal::float2 uv_3 [[attribute(2)]]; metal::float4 c_3 [[attribute(3)]]; metal::float4 m0_7 [[attribute(4)]]; metal::float4 m1_7 [[attribute(5)]]; metal::float4 m2_7 [[attribute(6)]]; metal::float4 m3_7 [[attribute(7)]]; metal::float4 tint_7 [[attribute(8)]]; }; struct vs_pnucOutput { metal::float4 clip [[position]]; metal::float3 view_pos [[user(loc0), center_perspective]]; metal::float3 normal [[user(loc1), center_perspective]]; metal::float2 uv [[user(loc2), center_perspective]]; metal::float4 color [[user(loc3), center_perspective]]; metal::float4 tint [[user(loc4), center_perspective]]; }; vertex vs_pnucOutput vs_pnuc( vs_pnucInput varyings_7 [[stage_in]] , constant Frame& frame [[buffer(0)]] ) { const auto p_7 = varyings_7.p_7; const auto n_3 = varyings_7.n_3; const auto uv_3 = varyings_7.uv_3; const auto c_3 = varyings_7.c_3; const auto m0_7 = varyings_7.m0_7; const auto m1_7 = varyings_7.m1_7; const auto m2_7 = varyings_7.m2_7; const auto m3_7 = varyings_7.m3_7; const auto tint_7 = varyings_7.tint_7; VOut _e9 = vertex_common(p_7, n_3, uv_3, c_3, m0_7, m1_7, m2_7, m3_7, tint_7, frame); const auto _tmp_7 = _e9; return vs_pnucOutput { _tmp_7.clip, _tmp_7.view_pos, _tmp_7.normal, _tmp_7.uv, _tmp_7.color, _tmp_7.tint }; } struct fs_mainInput { metal::float3 view_pos [[user(loc0), center_perspective]]; metal::float3 normal [[user(loc1), center_perspective]]; metal::float2 uv_5 [[user(loc2), center_perspective]]; metal::float4 color [[user(loc3), center_perspective]]; metal::float4 tint_9 [[user(loc4), center_perspective]]; }; struct fs_mainOutput { metal::float4 member_8 [[color(0)]]; }; fragment fs_mainOutput fs_main( fs_mainInput varyings_8 [[stage_in]] , metal::float4 clip [[position]] , bool front [[front_facing]] , constant Frame& frame [[buffer(0)]] , constant Material& material [[buffer(1)]] , metal::texture2d<float, metal::access::sample> tex [[texture(0)]] , metal::sampler samp [[sampler(0)]] ) { const VOut in = { clip, varyings_8.view_pos, varyings_8.normal, varyings_8.uv_5, {}, varyings_8.color, varyings_8.tint_9 }; metal::float4 base = {}; metal::float3 rgb = {}; metal::float3 n_4 = {}; metal::float3 light = {}; metal::float3 spec = metal::float3(0.0); int i = 0; float a = {}; metal::float3 _e3 = metal::dfdx(in.view_pos); metal::float3 _e5 = metal::dfdy(in.view_pos); metal::float3 flat_normal = metal::cross(_e3, _e5); metal::float4 texel = tex.sample(samp, in.uv); bool _e13 = section_clipped(in.view_pos, -1, frame); if (_e13) { metal::discard_fragment(); } metal::float4 _e16 = material.color; base = _e16; float _e21 = material.flags.y; if (_e21 > 0.5) { metal::float4 _e24 = base; base = _e24 * in.color; } float _e30 = material.flags.z; if (_e30 > 0.5) { metal::float4 _e33 = base; base = _e33 * texel; } metal::float4 _e35 = base; rgb = metal::mix(_e35.xyz, in.tint.xyz, in.tint.w); float _e46 = material.flags.x; if (_e46 > 0.5) { metal::float3 v = metal::normalize(-(in.view_pos)); n_4 = in.normal; metal::float3 _e54 = n_4; metal::float3 _e55 = n_4; if (metal::dot(_e54, _e55) < 0.25) { n_4 = metal::normalize(flat_normal); metal::float3 _e60 = n_4; if (metal::dot(_e60, v) < 0.0) { metal::float3 _e64 = n_4; n_4 = -(_e64); } } else { metal::float3 _e66 = n_4; n_4 = metal::normalize(_e66); if (!(front)) { metal::float3 _e69 = n_4; n_4 = -(_e69); } } metal::float4 _e73 = frame.ambient; light = _e73.xyz; uint2 loop_bound_1 = uint2(4294967295u); bool loop_init_1 = true; while(true) { if (metal::all(loop_bound_1 == uint2(0u))) { break; } loop_bound_1 -= uint2(loop_bound_1.y == 0u, 1u); if (!loop_init_1) { int _e129 = i; i = as_type<int>(as_type<uint>(_e129) + as_type<uint>(1)); } loop_init_1 = false; int _e81 = i; if (_e81 < 5) { } else { break; } { int _e86 = i; metal::float4 _e89 = frame.lights.inner[_e86].dir; metal::float3 l = metal::normalize(_e89.xyz); metal::float3 _e92 = n_4; float nl = metal::max(metal::dot(_e92, l), 0.0); metal::float3 _e96 = light; int _e99 = i; metal::float4 _e102 = frame.lights.inner[_e99].color; light = _e96 + (_e102.xyz * nl); if (nl > 0.0) { metal::float3 h = metal::normalize(l + v); metal::float3 _e110 = spec; int _e113 = i; metal::float4 _e116 = frame.lights.inner[_e113].color; metal::float3 _e118 = n_4; float _e125 = material.specular.w; spec = _e110 + (_e116.xyz * metal::pow(metal::max(metal::dot(_e118, h), 0.0), _e125)); } } } metal::float3 _e132 = rgb; metal::float3 _e133 = light; metal::float4 _e137 = material.specular; metal::float3 _e139 = spec; rgb = (_e132 * _e133) + (_e137.xyz * _e139); } float _e143 = base.w; a = _e143; float _e148 = material.flags.w; if (_e148 > 0.5) { a = 1.0; } metal::float3 _e152 = rgb; float _e156 = a; float _e158 = a; return fs_mainOutput { metal::float4(metal::min(_e152, metal::float3(1.0)) * _e156, _e158) }; } struct fs_maskInput { metal::float3 view_pos [[user(loc0), center_perspective]]; metal::float3 normal [[user(loc1), center_perspective]]; metal::float2 uv_5 [[user(loc2), center_perspective]]; metal::float4 color [[user(loc3), center_perspective]]; metal::float4 tint_9 [[user(loc4), center_perspective]]; }; struct fs_maskOutput { metal::float4 member_9 [[color(0)]]; }; fragment fs_maskOutput fs_mask( fs_maskInput varyings_9 [[stage_in]] , metal::float4 clip_1 [[position]] , constant Frame& frame [[buffer(0)]] ) { const VOut in_1 = { clip_1, varyings_9.view_pos, varyings_9.normal, varyings_9.uv_5, {}, varyings_9.color, varyings_9.tint_9 }; bool _e3 = section_clipped(in_1.view_pos, -1, frame); if (_e3) { metal::discard_fragment(); } return fs_maskOutput { metal::float4(1.0) }; } `, Entries: []gpu.EntryPoint{ {Name: "vs_p", Stage: gpu.ShaderStageVertex, Inputs: []gpu.VertexInput{ {Name: "p", Location: 0, Type: gpu.ShaderTypeFloat32x3}, {Name: "m0", Location: 4, Type: gpu.ShaderTypeFloat32x4}, {Name: "m1", Location: 5, Type: gpu.ShaderTypeFloat32x4}, {Name: "m2", Location: 6, Type: gpu.ShaderTypeFloat32x4}, {Name: "m3", Location: 7, Type: gpu.ShaderTypeFloat32x4}, {Name: "tint", Location: 8, Type: gpu.ShaderTypeFloat32x4}, }, Bindings: []gpu.ShaderBinding{ {Name: "frame", Group: 0, Binding: 0, Kind: gpu.BindingUniformBuffer, Size: 384, Register: gpu.ShaderRegister{Class: "b", Space: 0, Index: 0}, MetalIndex: 0}, }, }, {Name: "vs_pn", Stage: gpu.ShaderStageVertex, Inputs: []gpu.VertexInput{ {Name: "p", Location: 0, Type: gpu.ShaderTypeFloat32x3}, {Name: "n", Location: 1, Type: gpu.ShaderTypeFloat32x3}, {Name: "m0", Location: 4, Type: gpu.ShaderTypeFloat32x4}, {Name: "m1", Location: 5, Type: gpu.ShaderTypeFloat32x4}, {Name: "m2", Location: 6, Type: gpu.ShaderTypeFloat32x4}, {Name: "m3", Location: 7, Type: gpu.ShaderTypeFloat32x4}, {Name: "tint", Location: 8, Type: gpu.ShaderTypeFloat32x4}, }, Bindings: []gpu.ShaderBinding{ {Name: "frame", Group: 0, Binding: 0, Kind: gpu.BindingUniformBuffer, Size: 384, Register: gpu.ShaderRegister{Class: "b", Space: 0, Index: 0}, MetalIndex: 0}, }, }, {Name: "vs_pu", Stage: gpu.ShaderStageVertex, Inputs: []gpu.VertexInput{ {Name: "p", Location: 0, Type: gpu.ShaderTypeFloat32x3}, {Name: "uv", Location: 2, Type: gpu.ShaderTypeFloat32x2}, {Name: "m0", Location: 4, Type: gpu.ShaderTypeFloat32x4}, {Name: "m1", Location: 5, Type: gpu.ShaderTypeFloat32x4}, {Name: "m2", Location: 6, Type: gpu.ShaderTypeFloat32x4}, {Name: "m3", Location: 7, Type: gpu.ShaderTypeFloat32x4}, {Name: "tint", Location: 8, Type: gpu.ShaderTypeFloat32x4}, }, Bindings: []gpu.ShaderBinding{ {Name: "frame", Group: 0, Binding: 0, Kind: gpu.BindingUniformBuffer, Size: 384, Register: gpu.ShaderRegister{Class: "b", Space: 0, Index: 0}, MetalIndex: 0}, }, }, {Name: "vs_pc", Stage: gpu.ShaderStageVertex, Inputs: []gpu.VertexInput{ {Name: "p", Location: 0, Type: gpu.ShaderTypeFloat32x3}, {Name: "c", Location: 3, Type: gpu.ShaderTypeFloat32x4}, {Name: "m0", Location: 4, Type: gpu.ShaderTypeFloat32x4}, {Name: "m1", Location: 5, Type: gpu.ShaderTypeFloat32x4}, {Name: "m2", Location: 6, Type: gpu.ShaderTypeFloat32x4}, {Name: "m3", Location: 7, Type: gpu.ShaderTypeFloat32x4}, {Name: "tint", Location: 8, Type: gpu.ShaderTypeFloat32x4}, }, Bindings: []gpu.ShaderBinding{ {Name: "frame", Group: 0, Binding: 0, Kind: gpu.BindingUniformBuffer, Size: 384, Register: gpu.ShaderRegister{Class: "b", Space: 0, Index: 0}, MetalIndex: 0}, }, }, {Name: "vs_pnu", Stage: gpu.ShaderStageVertex, Inputs: []gpu.VertexInput{ {Name: "p", Location: 0, Type: gpu.ShaderTypeFloat32x3}, {Name: "n", Location: 1, Type: gpu.ShaderTypeFloat32x3}, {Name: "uv", Location: 2, Type: gpu.ShaderTypeFloat32x2}, {Name: "m0", Location: 4, Type: gpu.ShaderTypeFloat32x4}, {Name: "m1", Location: 5, Type: gpu.ShaderTypeFloat32x4}, {Name: "m2", Location: 6, Type: gpu.ShaderTypeFloat32x4}, {Name: "m3", Location: 7, Type: gpu.ShaderTypeFloat32x4}, {Name: "tint", Location: 8, Type: gpu.ShaderTypeFloat32x4}, }, Bindings: []gpu.ShaderBinding{ {Name: "frame", Group: 0, Binding: 0, Kind: gpu.BindingUniformBuffer, Size: 384, Register: gpu.ShaderRegister{Class: "b", Space: 0, Index: 0}, MetalIndex: 0}, }, }, {Name: "vs_pnc", Stage: gpu.ShaderStageVertex, Inputs: []gpu.VertexInput{ {Name: "p", Location: 0, Type: gpu.ShaderTypeFloat32x3}, {Name: "n", Location: 1, Type: gpu.ShaderTypeFloat32x3}, {Name: "c", Location: 3, Type: gpu.ShaderTypeFloat32x4}, {Name: "m0", Location: 4, Type: gpu.ShaderTypeFloat32x4}, {Name: "m1", Location: 5, Type: gpu.ShaderTypeFloat32x4}, {Name: "m2", Location: 6, Type: gpu.ShaderTypeFloat32x4}, {Name: "m3", Location: 7, Type: gpu.ShaderTypeFloat32x4}, {Name: "tint", Location: 8, Type: gpu.ShaderTypeFloat32x4}, }, Bindings: []gpu.ShaderBinding{ {Name: "frame", Group: 0, Binding: 0, Kind: gpu.BindingUniformBuffer, Size: 384, Register: gpu.ShaderRegister{Class: "b", Space: 0, Index: 0}, MetalIndex: 0}, }, }, {Name: "vs_puc", Stage: gpu.ShaderStageVertex, Inputs: []gpu.VertexInput{ {Name: "p", Location: 0, Type: gpu.ShaderTypeFloat32x3}, {Name: "uv", Location: 2, Type: gpu.ShaderTypeFloat32x2}, {Name: "c", Location: 3, Type: gpu.ShaderTypeFloat32x4}, {Name: "m0", Location: 4, Type: gpu.ShaderTypeFloat32x4}, {Name: "m1", Location: 5, Type: gpu.ShaderTypeFloat32x4}, {Name: "m2", Location: 6, Type: gpu.ShaderTypeFloat32x4}, {Name: "m3", Location: 7, Type: gpu.ShaderTypeFloat32x4}, {Name: "tint", Location: 8, Type: gpu.ShaderTypeFloat32x4}, }, Bindings: []gpu.ShaderBinding{ {Name: "frame", Group: 0, Binding: 0, Kind: gpu.BindingUniformBuffer, Size: 384, Register: gpu.ShaderRegister{Class: "b", Space: 0, Index: 0}, MetalIndex: 0}, }, }, {Name: "vs_pnuc", Stage: gpu.ShaderStageVertex, Inputs: []gpu.VertexInput{ {Name: "p", Location: 0, Type: gpu.ShaderTypeFloat32x3}, {Name: "n", Location: 1, Type: gpu.ShaderTypeFloat32x3}, {Name: "uv", Location: 2, Type: gpu.ShaderTypeFloat32x2}, {Name: "c", Location: 3, Type: gpu.ShaderTypeFloat32x4}, {Name: "m0", Location: 4, Type: gpu.ShaderTypeFloat32x4}, {Name: "m1", Location: 5, Type: gpu.ShaderTypeFloat32x4}, {Name: "m2", Location: 6, Type: gpu.ShaderTypeFloat32x4}, {Name: "m3", Location: 7, Type: gpu.ShaderTypeFloat32x4}, {Name: "tint", Location: 8, Type: gpu.ShaderTypeFloat32x4}, }, Bindings: []gpu.ShaderBinding{ {Name: "frame", Group: 0, Binding: 0, Kind: gpu.BindingUniformBuffer, Size: 384, Register: gpu.ShaderRegister{Class: "b", Space: 0, Index: 0}, MetalIndex: 0}, }, }, {Name: "fs_main", Stage: gpu.ShaderStageFragment, Bindings: []gpu.ShaderBinding{ {Name: "frame", Group: 0, Binding: 0, Kind: gpu.BindingUniformBuffer, Size: 384, Register: gpu.ShaderRegister{Class: "b", Space: 0, Index: 0}, MetalIndex: 0}, {Name: "material", Group: 1, Binding: 0, Kind: gpu.BindingUniformBuffer, Size: 48, Register: gpu.ShaderRegister{Class: "b", Space: 1, Index: 0}, MetalIndex: 1}, {Name: "tex", Group: 1, Binding: 1, Kind: gpu.BindingSampledTexture, Register: gpu.ShaderRegister{Class: "t", Space: 1, Index: 1}, MetalIndex: 0}, {Name: "samp", Group: 1, Binding: 2, Kind: gpu.BindingSampler, Register: gpu.ShaderRegister{Class: "s", Space: 1, Index: 2}, MetalIndex: 0}, }, }, {Name: "fs_mask", Stage: gpu.ShaderStageFragment, Bindings: []gpu.ShaderBinding{ {Name: "frame", Group: 0, Binding: 0, Kind: gpu.BindingUniformBuffer, Size: 384, Register: gpu.ShaderRegister{Class: "b", Space: 0, Index: 0}, MetalIndex: 0}, }, }, }, }
Mesh is mesh.wgsl.
var Pbr = &gpu.Shader{ Name: "pbr", WGSL: `// The PBR metallic-roughness material of viewport3d (SPEC §28.5): the BRDF of the glTF 2.0 specification (appendix B: GGX // distribution, the height-correlated Smith visibility, Schlick's Fresnel and a Lambert diffuse), the scene's directional // lights, image-based lighting from the prefiltered environment (split sum, with the analytic fit of the BRDF's table by // Karis), one directional shadow map, normal, occlusion and emissive maps, skinning and morph targets in the vertex shader. // // A mesh of this material has one vertex buffer of a fixed layout (position, normal, tangent, two texture coordinates, color, // joints and weights; what the mesh lacks is zero, and the item's flags say what it has), so there is one vertex function. The // shader writes linear radiance, premultiplied by the alpha: the renderer draws the frame into a float target and tonemap.wgsl // makes the colors of the display (SPEC §28.5, "Decided in step 31.9"). // // The positions are float32 relative to the mesh's origin; the instance's matrix takes them to the view space, whose origin is // the eye (D30). Skin matrices are in the mesh's space (the renderer makes them in float64 relative to the item). // What the shaders of viewport3d share (SPEC §27.8): the frame's uniform data, the record of an item, and the section planes. struct Light { // dir points to the light, in view space. dir: vec4<f32>, // color is the light's color times its intensity. color: vec4<f32>, } struct Frame { proj: mat4x4<f32>, // lights are the light at the camera, then up to four directional ones; an unused one has a black color. lights: array<Light, 5>, ambient: vec4<f32>, // viewport is the width and the height of the target in pixels, and their inverses. viewport: vec4<f32>, // params are the near plane's distance, the device pixel ratio and two unused numbers. params: vec4<f32>, // planes are the section planes in view space, relative to the eye (D30): a point p with dot(plane.xyz, p) + plane.w > 0 is // cut away. planes: array<vec4<f32>, 6>, // section is the number of planes in x. section: vec4<f32>, } @group(0) @binding(0) var<uniform> frame: Frame; // Item is the record of a line, a point or a picked item: the matrix that takes the mesh's positions to the view, relative to // the eye, a tint (the highlight of a selected item: its color, and the part of it that replaces the material's in a), and // numbers for the shader that reads it. struct Item { mv: mat4x4<f32>, tint: vec4<f32>, params: vec4<f32>, extra: vec4<f32>, } // section_clipped says whether a point of the view space is cut away by a section plane. skip is the plane to leave out (the // plane that a cap lies on), or -1. fn section_clipped(p: vec3<f32>, skip: i32) -> bool { let count = i32(frame.section.x); for (var i = 0; i < 6; i = i + 1) { if (i >= count) { break; } if (i == skip) { continue; } let plane = frame.planes[i]; if (dot(plane.xyz, p) + plane.w > 0.0) { return true; } } return false; } // PickOut is what a pick pass writes for a pixel: the number of the item (from 1, 0 for nothing) and of the primitive in it, and // the depth. struct PickOut { @location(0) ids: vec2<u32>, @location(1) depth: f32, } struct TexSlot { // a is the first row of the texture coordinate transform (m00, m01, tx) and the set of coordinates (0 or 1) in w; b is the // second row (m10, m11, ty) and in w 1 when the material has the texture. a: vec4<f32>, b: vec4<f32>, } struct Material { // color is the base color factor, linear, with the alpha. color: vec4<f32>, // factors are the metallic factor, the roughness factor, the scale of the normal and the strength of the occlusion. factors: vec4<f32>, // emissive is the emitted radiance, linear. emissive: vec4<f32>, // flags: x the alpha mode (0 opaque, 1 mask, 2 blend), y the cutoff, z the bits 1 unlit and 2 vertex colors. flags: vec4<f32>, // slots are the textures: base color, metallic-roughness, normal, occlusion, emissive. slots: array<TexSlot, 5>, } @group(1) @binding(0) var<uniform> material: Material; @group(1) @binding(1) var tex_base: texture_2d<f32>; @group(1) @binding(2) var tex_mr: texture_2d<f32>; @group(1) @binding(3) var tex_normal: texture_2d<f32>; @group(1) @binding(4) var tex_occlusion: texture_2d<f32>; @group(1) @binding(5) var tex_emissive: texture_2d<f32>; @group(1) @binding(6) var samp_base: sampler; @group(1) @binding(7) var samp_mr: sampler; @group(1) @binding(8) var samp_normal: sampler; @group(1) @binding(9) var samp_occlusion: sampler; @group(1) @binding(10) var samp_emissive: sampler; struct PBRFrame { // shadow_vp takes a position of the view space (the eye is the origin) to the clip space of the shadow map. shadow_vp: mat4x4<f32>, // view_to_world turns a direction of the view space into the world's. view_to_world: mat4x4<f32>, // params: x the strength of the environment's light, y the index in the frame's lights of the light that casts the shadow (-1: // none), z the depth // bias, w the offset along the normal, in the units of the view space. params: vec4<f32>, // shadow: x the strength of the shadow, y the size of the map in texels, z unused, w unused. shadow: vec4<f32>, // env: x the last mip level of the specular cube. env: vec4<f32>, } @group(2) @binding(0) var<uniform> pf: PBRFrame; @group(2) @binding(1) var env_spec: texture_cube<f32>; @group(2) @binding(2) var env_irr: texture_cube<f32>; @group(2) @binding(3) var env_samp: sampler; @group(2) @binding(4) var shadow_map: texture_2d<f32>; @group(2) @binding(5) var<storage, read> joint_matrices: array<mat4x4<f32>>; @group(2) @binding(6) var<storage, read> morph_weights: array<f32>; // morph_deltas are, for each vertex and each target, the change of the position and of the normal (xyz of two vec4). @group(3) @binding(0) var<storage, read> morph_deltas: array<vec4<f32>>; // The bits of what a mesh has, in the instance's last number. const HAS_NORMAL: u32 = 1u; const HAS_TANGENT: u32 = 2u; const HAS_UV0: u32 = 4u; const HAS_UV1: u32 = 8u; const SKINNED: u32 = 32u; const PI: f32 = 3.14159265358979; struct VOut { @builtin(position) clip: vec4<f32>, @location(0) view_pos: vec3<f32>, @location(1) normal: vec3<f32>, @location(2) tangent: vec4<f32>, @location(3) uv0: vec2<f32>, @location(4) uv1: vec2<f32>, @location(5) color: vec4<f32>, // tint is the color of a selected item, and in a the part of it that replaces the material's. @location(6) tint: vec4<f32>, @location(7) @interpolate(flat) flags: f32, } @vertex fn vs_pbr(@builtin(vertex_index) vertex: u32, @location(0) p: vec3<f32>, @location(1) n: vec3<f32>, @location(2) t: vec4<f32>, @location(3) uv0: vec2<f32>, @location(4) uv1: vec2<f32>, @location(5) c: vec4<f32>, @location(6) joints: vec4<u32>, @location(7) weights: vec4<f32>, @location(8) m0: vec4<f32>, @location(9) m1: vec4<f32>, @location(10) m2: vec4<f32>, @location(11) m3: vec4<f32>, @location(12) tint: vec4<f32>, @location(13) extra: vec4<f32>) -> VOut { let flags = u32(extra.w); var pos = p; var nrm = n; var tan = t.xyz; // Morph targets: the weights of the item, and the changes of this vertex. let targets = u32(extra.z); if (targets > 0u) { let wbase = u32(extra.y); for (var k = 0u; k < targets; k = k + 1u) { let w = morph_weights[wbase + k]; if (w != 0.0) { let i = (vertex * targets + k) * 2u; pos = pos + w * morph_deltas[i].xyz; nrm = nrm + w * morph_deltas[i + 1u].xyz; } } } // Skinning: the four joints' matrices, blended by the weights. if ((flags & SKINNED) != 0u) { let jb = u32(extra.x); let skin = joint_matrices[jb + joints.x] * weights.x + joint_matrices[jb + joints.y] * weights.y + joint_matrices[jb + joints.z] * weights.z + joint_matrices[jb + joints.w] * weights.w; pos = (skin * vec4<f32>(pos, 1.0)).xyz; nrm = (skin * vec4<f32>(nrm, 0.0)).xyz; tan = (skin * vec4<f32>(tan, 0.0)).xyz; } let mv = mat4x4<f32>(m0, m1, m2, m3); let vp = mv * vec4<f32>(pos, 1.0); var o: VOut; o.view_pos = vp.xyz; o.clip = frame.proj * vp; o.normal = (mv * vec4<f32>(nrm, 0.0)).xyz; o.tangent = vec4<f32>((mv * vec4<f32>(tan, 0.0)).xyz, t.w); o.uv0 = uv0; o.uv1 = uv1; o.color = c; o.tint = tint; o.flags = extra.w; return o; } // srgb_to_linear decodes the sRGB transfer function. fn srgb_to_linear(c: vec3<f32>) -> vec3<f32> { let lo = c / 12.92; let hi = pow((c + vec3<f32>(0.055)) / 1.055, vec3<f32>(2.4)); return select(hi, lo, c <= vec3<f32>(0.04045)); } // slot_uv is the texture coordinates of a slot of the material: the set it reads, then its transform. fn slot_uv(i: i32, uv0: vec2<f32>, uv1: vec2<f32>) -> vec2<f32> { let s = material.slots[i]; var uv = uv0; if (s.a.w > 0.5) { uv = uv1; } return vec2<f32>(s.a.x * uv.x + s.a.y * uv.y + s.a.z, s.b.x * uv.x + s.b.y * uv.y + s.b.z); } // slot_has is 1 when the material has the texture of the slot. fn slot_has(i: i32) -> bool { return material.slots[i].b.w > 0.5; } fn distribution_ggx(nh: f32, alpha: f32) -> f32 { let a2 = alpha * alpha; let d = nh * nh * (a2 - 1.0) + 1.0; return a2 / (PI * d * d); } fn visibility_smith_ggx(nl: f32, nv: f32, alpha: f32) -> f32 { let a2 = alpha * alpha; let gv = nl * sqrt(nv * nv * (1.0 - a2) + a2); let gl = nv * sqrt(nl * nl * (1.0 - a2) + a2); let s = gv + gl; if (s > 0.0) { return 0.5 / s; } return 0.0; } // shadow_visibility is the part of the light that reaches a position of the view space: 1 where the shadow map has no shadow. fn shadow_visibility(view_pos: vec3<f32>, n: vec3<f32>) -> f32 { let p = view_pos + n * pf.params.w; let c = pf.shadow_vp * vec4<f32>(p, 1.0); let size = pf.shadow.y; let uv = vec2<f32>(c.x * 0.5 + 0.5, 0.5 - c.y * 0.5); let coord = uv * size; let depth = c.z - pf.params.z; if (uv.x < 0.0 || uv.y < 0.0 || uv.x >= 1.0 || uv.y >= 1.0 || c.z > 1.0) { return 1.0; } // A 3 by 3 filter of comparisons, so that the edge of a shadow is not made of texels. let base = vec2<i32>(floor(coord)); let top = i32(size) - 1; var lit = 0.0; for (var dy = -1; dy <= 1; dy = dy + 1) { for (var dx = -1; dx <= 1; dx = dx + 1) { let q = clamp(base + vec2<i32>(dx, dy), vec2<i32>(0), vec2<i32>(top)); let stored = textureLoad(shadow_map, q, 0).x; if (depth <= stored) { lit = lit + 1.0; } } } return mix(1.0, lit / 9.0, pf.shadow.x); } @fragment fn fs_pbr(in: VOut, @builtin(front_facing) front: bool) -> @location(0) vec4<f32> { let flags = u32(in.flags); // Derivatives and sampling are taken where the control flow is uniform, then selected. let dp_dx = dpdx(in.view_pos); let dp_dy = dpdy(in.view_pos); let uv_base = slot_uv(0, in.uv0, in.uv1); let uv_mr = slot_uv(1, in.uv0, in.uv1); let uv_normal = slot_uv(2, in.uv0, in.uv1); let uv_occlusion = slot_uv(3, in.uv0, in.uv1); let uv_emissive = slot_uv(4, in.uv0, in.uv1); let duv_dx = dpdx(uv_normal); let duv_dy = dpdy(uv_normal); let t_base = textureSample(tex_base, samp_base, uv_base); let t_mr = textureSample(tex_mr, samp_mr, uv_mr); let t_normal = textureSample(tex_normal, samp_normal, uv_normal); let t_occlusion = textureSample(tex_occlusion, samp_occlusion, uv_occlusion); let t_emissive = textureSample(tex_emissive, samp_emissive, uv_emissive); if (section_clipped(in.view_pos, -1)) { discard; } var base = material.color; let bits = u32(material.flags.z); if ((bits & 2u) != 0u) { base = base * vec4<f32>(srgb_to_linear(in.color.rgb), in.color.a); } if (slot_has(0)) { base = base * vec4<f32>(srgb_to_linear(t_base.rgb), t_base.a); } let mode = u32(material.flags.x); var alpha = base.a; if (mode == 0u) { alpha = 1.0; } else if (mode == 1u && alpha < material.flags.y) { discard; } let tint = vec4<f32>(srgb_to_linear(in.tint.rgb), in.tint.a); // An unlit material shows its color. if ((bits & 1u) != 0u) { let c = mix(base.rgb, tint.rgb, tint.a); return vec4<f32>(c * alpha, alpha); } var metallic = material.factors.x; var roughness = material.factors.y; if (slot_has(1)) { roughness = roughness * t_mr.g; metallic = metallic * t_mr.b; } metallic = clamp(metallic, 0.0, 1.0); roughness = clamp(roughness, 0.045, 1.0); var occlusion = 1.0; if (slot_has(3)) { occlusion = 1.0 + material.factors.w * (t_occlusion.r - 1.0); } var emissive = material.emissive.rgb; if (slot_has(4)) { emissive = emissive * srgb_to_linear(t_emissive.rgb); } // The shading frame. A mesh with no normals is flat, shaded as seen from the camera; a back face of a double sided material is // lit as seen from behind. let v = normalize(-in.view_pos); var n = in.normal; // sign is -1 for the back face of a double sided material, which is shaded as seen from behind: the frame turns with it. var sign = 1.0; var bitangent_geom = vec3<f32>(0.0); if ((flags & HAS_NORMAL) == 0u) { n = normalize(cross(dp_dx, dp_dy)); if (dot(n, v) < 0.0) { n = -n; } } else { n = normalize(n); bitangent_geom = cross(n, normalize(in.tangent.xyz)) * in.tangent.w; if (!front) { n = -n; sign = -1.0; } } if (slot_has(2)) { var tangent: vec3<f32>; var bitangent: vec3<f32>; if ((flags & HAS_TANGENT) != 0u) { tangent = normalize(in.tangent.xyz) * sign; bitangent = bitangent_geom * sign; } else { // The frame from the derivatives of the position and of the texture coordinates: the tangent points the way that u grows // on the surface (solving dp = T du + B dv for T), made orthogonal to the normal; the bitangent is the cross product of the // normal and the tangent, as the reference viewer of glTF has it (the green channel of a normal map points up in the // image, towards smaller v). let det = duv_dx.x * duv_dy.y - duv_dy.x * duv_dx.y; var t = (dp_dx * duv_dy.y - dp_dy * duv_dx.y) * select(1.0, -1.0, det < 0.0); t = t - n * dot(n, t); tangent = t * inverseSqrt(max(dot(t, t), 1.0e-20)); bitangent = cross(n, tangent); } var tn = t_normal.xyz * 2.0 - vec3<f32>(1.0); tn = vec3<f32>(tn.xy * material.factors.z, tn.z); n = normalize(tangent * tn.x + bitangent * tn.y + n * tn.z); } let nv = clamp(abs(dot(n, v)), 1.0e-4, 1.0); let f0 = mix(vec3<f32>(0.04), base.rgb, metallic); let diffuse_color = base.rgb * (1.0 - metallic); let alpha_r = roughness * roughness; // The lights: the one at the camera, then the scene's directional lights. A light of intensity 1 gives a white surface that // faces it its color, as in the Blinn-Phong material, so the BRDF is scaled by pi. var color = vec3<f32>(0.0); for (var i = 0; i < 5; i = i + 1) { let l = normalize(frame.lights[i].dir.xyz); let nl = dot(n, l); if (nl > 0.0) { let h = normalize(l + v); let nh = clamp(dot(n, h), 0.0, 1.0); let vh = clamp(dot(v, h), 0.0, 1.0); let fresnel = f0 + (vec3<f32>(1.0) - f0) * pow(1.0 - vh, 5.0); let spec = fresnel * distribution_ggx(nh, alpha_r) * visibility_smith_ggx(nl, nv, alpha_r); let diff = (vec3<f32>(1.0) - fresnel) * diffuse_color / PI; var visible = 1.0; if (i == i32(pf.params.y)) { visible = shadow_visibility(in.view_pos, n); } color = color + (diff + spec) * frame.lights[i].color.rgb * nl * PI * visible; } } // Image-based lighting: the irradiance for the diffuse part and the prefiltered environment for the specular part, with the // fit of the split-sum table by Karis (Mobile Graphics, SIGGRAPH 2014). let nw = (pf.view_to_world * vec4<f32>(n, 0.0)).xyz; let rw = (pf.view_to_world * vec4<f32>(reflect(-v, n), 0.0)).xyz; let irradiance = textureSampleLevel(env_irr, env_samp, nw, 0.0).rgb; let prefiltered = textureSampleLevel(env_spec, env_samp, rw, roughness * pf.env.x).rgb; let c0 = vec4<f32>(-1.0, -0.0275, -0.572, 0.022); let c1 = vec4<f32>(1.0, 0.0425, 1.04, -0.04); let r = roughness * c0 + c1; let a004 = min(r.x * r.x, exp2(-9.28 * nv)) * r.x + r.y; let ab = vec2<f32>(-1.04, 1.04) * a004 + r.zw; let ibl = irradiance * diffuse_color + prefiltered * (f0 * ab.x + vec3<f32>(ab.y)); color = color + ibl * occlusion * pf.params.x + emissive; color = mix(color, tint.rgb, tint.a); return vec4<f32>(color * alpha, alpha); } `, SPIRV: spirvPbr, HLSL: `struct Light { float4 dir; float4 color; }; struct Frame { row_major float4x4 proj; Light lights[5]; float4 ambient; float4 viewport; float4 params; float4 planes[6]; float4 section; }; struct Item { row_major float4x4 mv; float4 tint; float4 params; float4 extra; }; struct PickOut { uint2 ids : LOC0; float depth : LOC1; }; struct TexSlot { float4 a; float4 b; }; struct Material { float4 color; float4 factors; float4 emissive; float4 flags; TexSlot slots[5]; }; struct PBRFrame { row_major float4x4 shadow_vp; row_major float4x4 view_to_world; float4 params; float4 shadow; float4 env; }; struct VOut { float4 clip_ : SV_Position; float3 view_pos : LOC0; float3 normal : LOC1; float4 tangent : LOC2; float2 uv0_ : LOC3; float2 uv1_ : LOC4; float4 color : LOC5; float4 tint : LOC6; nointerpolation float flags : LOC7; }; static const uint HAS_NORMAL = 1u; static const uint HAS_TANGENT = 2u; static const uint HAS_UV0_ = 4u; static const uint HAS_UV1_ = 8u; static const uint SKINNED = 32u; static const float PI = 3.1415927; cbuffer frame : register(b0, space0) { Frame frame; } cbuffer material : register(b0, space1) { Material material; } Texture2D<float4> tex_base : register(t1, space1); Texture2D<float4> tex_mr : register(t2, space1); Texture2D<float4> tex_normal : register(t3, space1); Texture2D<float4> tex_occlusion : register(t4, space1); Texture2D<float4> tex_emissive : register(t5, space1); SamplerState samp_base : register(s6, space1); SamplerState samp_mr : register(s7, space1); SamplerState samp_normal : register(s8, space1); SamplerState samp_occlusion : register(s9, space1); SamplerState samp_emissive : register(s10, space1); cbuffer pf : register(b0, space2) { PBRFrame pf; } TextureCube<float4> env_spec : register(t1, space2); TextureCube<float4> env_irr : register(t2, space2); SamplerState env_samp : register(s3, space2); Texture2D<float4> shadow_map : register(t4, space2); ByteAddressBuffer joint_matrices : register(t5, space2); ByteAddressBuffer morph_weights : register(t6, space2); ByteAddressBuffer morph_deltas : register(t0, space3); struct VertexOutput_vs_pbr { float3 view_pos_1 : LOC0; float3 normal : LOC1; float4 tangent_1 : LOC2; float2 uv0_2 : LOC3; float2 uv1_2 : LOC4; float4 color_1 : LOC5; float4 tint_1 : LOC6; nointerpolation float flags : LOC7; float4 clip_ : SV_Position; }; struct FragmentInput_fs_pbr { float3 view_pos_2 : LOC0; float3 normal_1 : LOC1; float4 tangent_2 : LOC2; float2 uv0_3 : LOC3; float2 uv1_3 : LOC4; float4 color_2 : LOC5; float4 tint_2 : LOC6; nointerpolation float flags_1 : LOC7; float4 clip_1 : SV_Position; bool front_1 : SV_IsFrontFace; }; int naga_f2i32(float value) { return int(clamp(value, -2147483600.0, 2147483500.0)); } bool section_clipped(float3 p_1, int skip) { int i_1 = int(0); float _e5 = frame.section.x; int count = naga_f2i32(_e5); uint2 loop_bound = uint2(4294967295u, 4294967295u); bool loop_init = true; while(true) { if (all(loop_bound == uint2(0u, 0u))) { break; } loop_bound -= uint2(loop_bound.y == 0u, 1u); if (!loop_init) { int _e28 = i_1; i_1 = asint(asuint(_e28) + asuint(int(1))); } loop_init = false; int _e9 = i_1; if ((_e9 < int(6))) { } else { break; } { int _e12 = i_1; if ((_e12 >= count)) { break; } int _e14 = i_1; if ((_e14 == skip)) { continue; } int _e18 = i_1; float4 plane = frame.planes[_e18]; if (((dot(plane.xyz, p_1) + plane.w) > 0.0)) { return true; } } } return false; } float3 srgb_to_linear(float3 c_1) { float3 lo = (c_1 / (12.92).xxx); float3 hi = pow(((c_1 + (0.055).xxx) / (1.055).xxx), (2.4).xxx); return ((c_1 <= (0.04045).xxx) ? lo : hi); } float2 slot_uv(int i_2, float2 uv0_1, float2 uv1_1) { float2 uv = (float2)0; TexSlot s = material.slots[i_2]; uv = uv0_1; if ((s.a.w > 0.5)) { uv = uv1_1; } float _e15 = uv.x; float _e20 = uv.y; float _e29 = uv.x; float _e34 = uv.y; return float2((((s.a.x * _e15) + (s.a.y * _e20)) + s.a.z), (((s.b.x * _e29) + (s.b.y * _e34)) + s.b.z)); } bool slot_has(int i_3) { float _e6 = material.slots[i_3].b.w; return (_e6 > 0.5); } float distribution_ggx(float nh, float alpha_1) { float a2_ = (alpha_1 * alpha_1); float d = (((nh * nh) * (a2_ - 1.0)) + 1.0); return (a2_ / ((PI * d) * d)); } float visibility_smith_ggx(float nl, float nv, float alpha_2) { float a2_1 = (alpha_2 * alpha_2); float gv = (nl * sqrt((((nv * nv) * (1.0 - a2_1)) + a2_1))); float gl = (nv * sqrt((((nl * nl) * (1.0 - a2_1)) + a2_1))); float s_1 = (gv + gl); if ((s_1 > 0.0)) { return (0.5 / s_1); } return 0.0; } int2 naga_f2i32(float2 value) { return int2(clamp(value, -2147483600.0, 2147483500.0)); } float shadow_visibility(float3 view_pos, float3 n_2) { bool local_1 = (bool)0; bool local_2 = (bool)0; bool local_3 = (bool)0; bool local_4 = (bool)0; float lit_ = 0.0; int dy = int(-1); int dx = (int)0; float _e5 = pf.params.w; float3 p_2 = (view_pos + (n_2 * _e5)); float4x4 _e10 = pf.shadow_vp; float4 c_2 = mul(float4(p_2, 1.0), _e10); float size = pf.shadow.y; float2 uv_1 = float2(((c_2.x * 0.5) + 0.5), (0.5 - (c_2.y * 0.5))); float2 coord = (uv_1 * size); float _e34 = pf.params.z; float depth = (c_2.z - _e34); if (!((uv_1.x < 0.0))) { local_1 = (uv_1.y < 0.0); } else { local_1 = true; } bool _e46 = local_1; if (!(_e46)) { local_2 = (uv_1.x >= 1.0); } else { local_2 = true; } bool _e54 = local_2; if (!(_e54)) { local_3 = (uv_1.y >= 1.0); } else { local_3 = true; } bool _e62 = local_3; if (!(_e62)) { local_4 = (c_2.z > 1.0); } else { local_4 = true; } bool _e70 = local_4; if (_e70) { return 1.0; } int2 base_1 = naga_f2i32(floor(coord)); int top = asint(asuint(naga_f2i32(size)) - asuint(int(1))); uint2 loop_bound_1 = uint2(4294967295u, 4294967295u); bool loop_init_1 = true; while(true) { if (all(loop_bound_1 == uint2(0u, 0u))) { break; } loop_bound_1 -= uint2(loop_bound_1.y == 0u, 1u); if (!loop_init_1) { int _e108 = dy; dy = asint(asuint(_e108) + asuint(int(1))); } loop_init_1 = false; int _e81 = dy; if ((_e81 <= int(1))) { } else { break; } { dx = int(-1); uint2 loop_bound_2 = uint2(4294967295u, 4294967295u); bool loop_init_2 = true; while(true) { if (all(loop_bound_2 == uint2(0u, 0u))) { break; } loop_bound_2 -= uint2(loop_bound_2.y == 0u, 1u); if (!loop_init_2) { int _e105 = dx; dx = asint(asuint(_e105) + asuint(int(1))); } loop_init_2 = false; int _e86 = dx; if ((_e86 <= int(1))) { } else { break; } { int _e89 = dx; int _e90 = dy; int2 q = clamp(asint(asuint(base_1) + asuint(int2(_e89, _e90))), (int(0)).xx, (top).xx); float4 _e99 = shadow_map.Load(int3(q, int(0))); float stored = _e99.x; if ((depth <= stored)) { float _e102 = lit_; lit_ = (_e102 + 1.0); } } } } } float _e111 = lit_; float _e117 = pf.shadow.x; return lerp(1.0, (_e111 / 9.0), _e117); } uint naga_f2u32(float value) { return uint(clamp(value, 0.0, 4294967000.0)); } VertexOutput_vs_pbr vs_pbr(uint vertex : SV_VertexID, float3 p : LOC0, float3 n : LOC1, float4 t : LOC2, float2 uv0_ : LOC3, float2 uv1_ : LOC4, float4 c : LOC5, uint4 joints : LOC6, float4 weights : LOC7, float4 m0_ : LOC8, float4 m1_ : LOC9, float4 m2_ : LOC10, float4 m3_ : LOC11, float4 tint : LOC12, float4 extra : LOC13) { float3 pos = (float3)0; float3 nrm = (float3)0; float3 tan_ = (float3)0; uint k = 0u; VOut o = (VOut)0; uint flags_2 = naga_f2u32(extra.w); pos = p; nrm = n; tan_ = t.xyz; uint targets = naga_f2u32(extra.z); if ((targets > 0u)) { uint wbase = naga_f2u32(extra.y); uint2 loop_bound_3 = uint2(4294967295u, 4294967295u); bool loop_init_3 = true; while(true) { if (all(loop_bound_3 == uint2(0u, 0u))) { break; } loop_bound_3 -= uint2(loop_bound_3.y == 0u, 1u); if (!loop_init_3) { uint _e59 = k; k = (_e59 + 1u); } loop_init_3 = false; uint _e29 = k; if ((_e29 < targets)) { } else { break; } { uint _e32 = k; float w = asfloat(morph_weights.Load((wbase + _e32)*4)); if ((w != 0.0)) { uint _e39 = k; uint i_4 = (((vertex * targets) + _e39) * 2u); float3 _e43 = pos; float4 _e46 = asfloat(morph_deltas.Load4(i_4*16)); pos = (_e43 + (w * _e46.xyz)); float3 _e50 = nrm; float4 _e55 = asfloat(morph_deltas.Load4((i_4 + 1u)*16)); nrm = (_e50 + (w * _e55.xyz)); } } } } if (((flags_2 & SKINNED) != 0u)) { uint jb = naga_f2u32(extra.x); float4x4 _e72 = float4x4(asfloat(joint_matrices.Load4((jb + joints.x)*64+0)), asfloat(joint_matrices.Load4((jb + joints.x)*64+16)), asfloat(joint_matrices.Load4((jb + joints.x)*64+32)), asfloat(joint_matrices.Load4((jb + joints.x)*64+48))); float4x4 _e79 = float4x4(asfloat(joint_matrices.Load4((jb + joints.y)*64+0)), asfloat(joint_matrices.Load4((jb + joints.y)*64+16)), asfloat(joint_matrices.Load4((jb + joints.y)*64+32)), asfloat(joint_matrices.Load4((jb + joints.y)*64+48))); float4x4 _e87 = float4x4(asfloat(joint_matrices.Load4((jb + joints.z)*64+0)), asfloat(joint_matrices.Load4((jb + joints.z)*64+16)), asfloat(joint_matrices.Load4((jb + joints.z)*64+32)), asfloat(joint_matrices.Load4((jb + joints.z)*64+48))); float4x4 _e95 = float4x4(asfloat(joint_matrices.Load4((jb + joints.w)*64+0)), asfloat(joint_matrices.Load4((jb + joints.w)*64+16)), asfloat(joint_matrices.Load4((jb + joints.w)*64+32)), asfloat(joint_matrices.Load4((jb + joints.w)*64+48))); float4x4 skin = (((mul(weights.x, _e72) + mul(weights.y, _e79)) + mul(weights.z, _e87)) + mul(weights.w, _e95)); float3 _e99 = pos; pos = mul(float4(_e99, 1.0), skin).xyz; float3 _e104 = nrm; nrm = mul(float4(_e104, 0.0), skin).xyz; float3 _e109 = tan_; tan_ = mul(float4(_e109, 0.0), skin).xyz; } float4x4 mv = float4x4(m0_, m1_, m2_, m3_); float3 _e115 = pos; float4 vp = mul(float4(_e115, 1.0), mv); o.view_pos = vp.xyz; float4x4 _e125 = frame.proj; o.clip_ = mul(vp, _e125); float3 _e128 = nrm; o.normal = mul(float4(_e128, 0.0), mv).xyz; float3 _e134 = tan_; o.tangent = float4(mul(float4(_e134, 0.0), mv).xyz, t.w); o.uv0_ = uv0_; o.uv1_ = uv1_; o.color = c; o.tint = tint; o.flags = extra.w; VOut _e147 = o; VOut vout = _e147; VertexOutput_vs_pbr vout_1 = { vout.view_pos, vout.normal, vout.tangent, vout.uv0_, vout.uv1_, vout.color, vout.tint, vout.flags, vout.clip_ }; return vout_1; } float4 fs_pbr(FragmentInput_fs_pbr fragmentinput_fs_pbr) : SV_Target0 { VOut in_ = { fragmentinput_fs_pbr.clip_1, fragmentinput_fs_pbr.view_pos_2, fragmentinput_fs_pbr.normal_1, fragmentinput_fs_pbr.tangent_2, fragmentinput_fs_pbr.uv0_3, fragmentinput_fs_pbr.uv1_3, fragmentinput_fs_pbr.color_2, fragmentinput_fs_pbr.tint_2, fragmentinput_fs_pbr.flags_1 }; bool front = fragmentinput_fs_pbr.front_1; float4 base = (float4)0; float alpha = (float)0; bool local = (bool)0; float metallic = (float)0; float roughness = (float)0; float occlusion = 1.0; float3 emissive = (float3)0; float3 n_1 = (float3)0; float sign_ = 1.0; float3 bitangent_geom = (0.0).xxx; float3 tangent = (float3)0; float3 bitangent = (float3)0; float3 t_1 = (float3)0; float3 tn = (float3)0; float3 color = (0.0).xxx; int i = int(0); float visible = (float)0; uint flags_3 = naga_f2u32(in_.flags); float3 dp_dx = ddx(in_.view_pos); float3 dp_dy = ddy(in_.view_pos); const float2 _e11 = slot_uv(int(0), in_.uv0_, in_.uv1_); const float2 _e15 = slot_uv(int(1), in_.uv0_, in_.uv1_); const float2 _e19 = slot_uv(int(2), in_.uv0_, in_.uv1_); const float2 _e23 = slot_uv(int(3), in_.uv0_, in_.uv1_); const float2 _e27 = slot_uv(int(4), in_.uv0_, in_.uv1_); float2 duv_dx = ddx(_e19); float2 duv_dy = ddy(_e19); float4 t_base = tex_base.Sample(samp_base, _e11); float4 t_mr = tex_mr.Sample(samp_mr, _e15); float4 t_normal = tex_normal.Sample(samp_normal, _e19); float4 t_occlusion = tex_occlusion.Sample(samp_occlusion, _e23); float4 t_emissive = tex_emissive.Sample(samp_emissive, _e27); const bool _e47 = section_clipped(in_.view_pos, int(-1)); if (_e47) { discard; } float4 _e50 = material.color; base = _e50; float _e55 = material.flags.z; uint bits = naga_f2u32(_e55); if (((bits & 2u) != 0u)) { float4 _e61 = base; const float3 _e64 = srgb_to_linear(in_.color.xyz); base = (_e61 * float4(_e64, in_.color.w)); } const bool _e70 = slot_has(int(0)); if (_e70) { float4 _e71 = base; const float3 _e73 = srgb_to_linear(t_base.xyz); base = (_e71 * float4(_e73, t_base.w)); } float _e80 = material.flags.x; uint mode = naga_f2u32(_e80); float _e83 = base.w; alpha = _e83; if ((mode == 0u)) { alpha = 1.0; } else { if ((mode == 1u)) { float _e92 = alpha; float _e96 = material.flags.y; local = (_e92 < _e96); } else { local = false; } bool _e99 = local; if (_e99) { discard; } } const float3 _e102 = srgb_to_linear(in_.tint.xyz); float4 tint_3 = float4(_e102, in_.tint.w); if (((bits & 1u) != 0u)) { float4 _e110 = base; float3 c_3 = lerp(_e110.xyz, tint_3.xyz, tint_3.w); float _e115 = alpha; float _e117 = alpha; return float4((c_3 * _e115), _e117); } float _e122 = material.factors.x; metallic = _e122; float _e127 = material.factors.y; roughness = _e127; const bool _e130 = slot_has(int(1)); if (_e130) { float _e131 = roughness; roughness = (_e131 * t_mr.y); float _e134 = metallic; metallic = (_e134 * t_mr.z); } float _e137 = metallic; metallic = clamp(_e137, 0.0, 1.0); float _e141 = roughness; roughness = clamp(_e141, 0.045, 1.0); const bool _e148 = slot_has(int(3)); if (_e148) { float _e152 = material.factors.w; occlusion = (1.0 + (_e152 * (t_occlusion.x - 1.0))); } float4 _e161 = material.emissive; emissive = _e161.xyz; const bool _e165 = slot_has(int(4)); if (_e165) { float3 _e166 = emissive; const float3 _e168 = srgb_to_linear(t_emissive.xyz); emissive = (_e166 * _e168); } float3 v = normalize(-(in_.view_pos)); n_1 = in_.normal; if (((flags_3 & HAS_NORMAL) == 0u)) { n_1 = normalize(cross(dp_dx, dp_dy)); float3 _e186 = n_1; if ((dot(_e186, v) < 0.0)) { float3 _e190 = n_1; n_1 = -(_e190); } } else { float3 _e192 = n_1; n_1 = normalize(_e192); float3 _e194 = n_1; bitangent_geom = (cross(_e194, normalize(in_.tangent.xyz)) * in_.tangent.w); if (!(front)) { float3 _e203 = n_1; n_1 = -(_e203); sign_ = -1.0; } } const bool _e207 = slot_has(int(2)); if (_e207) { if (((flags_3 & HAS_TANGENT) != 0u)) { float _e217 = sign_; tangent = (normalize(in_.tangent.xyz) * _e217); float3 _e219 = bitangent_geom; float _e220 = sign_; bitangent = (_e219 * _e220); } else { float det = ((duv_dx.x * duv_dy.y) - (duv_dy.x * duv_dx.y)); t_1 = (((dp_dx * duv_dy.y) - (dp_dy * duv_dx.y)) * ((det < 0.0) ? -1.0 : 1.0)); float3 _e241 = t_1; float3 _e242 = n_1; float3 _e243 = n_1; float3 _e244 = t_1; t_1 = (_e241 - (_e242 * dot(_e243, _e244))); float3 _e248 = t_1; float3 _e249 = t_1; float3 _e250 = t_1; tangent = (_e248 * rsqrt(max(dot(_e249, _e250), 1e-20))); float3 _e256 = n_1; float3 _e257 = tangent; bitangent = cross(_e256, _e257); } tn = ((t_normal.xyz * 2.0) - (1.0).xxx); float3 _e266 = tn; float _e271 = material.factors.z; float _e274 = tn.z; tn = float3((_e266.xy * _e271), _e274); float3 _e276 = tangent; float _e278 = tn.x; float3 _e280 = bitangent; float _e282 = tn.y; float3 _e285 = n_1; float _e287 = tn.z; n_1 = normalize((((_e276 * _e278) + (_e280 * _e282)) + (_e285 * _e287))); } float3 _e291 = n_1; float nv_1 = clamp(abs(dot(_e291, v)), 0.0001, 1.0); float4 _e299 = base; float _e301 = metallic; float3 f0_ = lerp((0.04).xxx, _e299.xyz, _e301); float4 _e303 = base; float _e305 = metallic; float3 diffuse_color = (_e303.xyz * (1.0 - _e305)); float _e309 = roughness; float _e310 = roughness; float alpha_r = (_e309 * _e310); uint2 loop_bound_4 = uint2(4294967295u, 4294967295u); bool loop_init_4 = true; while(true) { if (all(loop_bound_4 == uint2(0u, 0u))) { break; } loop_bound_4 -= uint2(loop_bound_4.y == 0u, 1u); if (!loop_init_4) { int _e391 = i; i = asint(asuint(_e391) + asuint(int(1))); } loop_init_4 = false; int _e317 = i; if ((_e317 < int(5))) { } else { break; } { int _e322 = i; float4 _e325 = frame.lights[_e322].dir; float3 l = normalize(_e325.xyz); float3 _e328 = n_1; float nl_1 = dot(_e328, l); if ((nl_1 > 0.0)) { float3 h = normalize((l + v)); float3 _e334 = n_1; float nh_1 = clamp(dot(_e334, h), 0.0, 1.0); float vh = clamp(dot(v, h), 0.0, 1.0); float3 fresnel = (f0_ + (((1.0).xxx - f0_) * pow((1.0 - vh), 5.0))); const float _e352 = distribution_ggx(nh_1, alpha_r); const float _e354 = visibility_smith_ggx(nl_1, nv_1, alpha_r); float3 spec = ((fresnel * _e352) * _e354); float3 diff = ((((1.0).xxx - fresnel) * diffuse_color) / (3.1415927).xxx); visible = 1.0; int _e365 = i; float _e369 = pf.params.y; if ((_e365 == naga_f2i32(_e369))) { float3 _e373 = n_1; const float _e374 = shadow_visibility(in_.view_pos, _e373); visible = _e374; } float3 _e375 = color; int _e379 = i; float4 _e382 = frame.lights[_e379].color; float _e388 = visible; color = (_e375 + (((((diff + spec) * _e382.xyz) * nl_1) * PI) * _e388)); } } } float4x4 _e396 = pf.view_to_world; float3 _e397 = n_1; float3 nw = mul(float4(_e397, 0.0), _e396).xyz; float4x4 _e404 = pf.view_to_world; float3 _e406 = n_1; float3 rw = mul(float4(reflect(-(v), _e406), 0.0), _e404).xyz; float4 _e415 = env_irr.SampleLevel(env_samp, nw, 0.0); float3 irradiance = _e415.xyz; float _e419 = roughness; float _e423 = pf.env.x; float4 _e425 = env_spec.SampleLevel(env_samp, rw, (_e419 * _e423)); float3 prefiltered = _e425.xyz; float4 c0_ = float4(-1.0, -0.0275, -0.572, 0.022); float4 c1_ = float4(1.0, 0.0425, 1.04, -0.04); float _e437 = roughness; float4 r = ((_e437 * c0_) + c1_); float a004_ = ((min((r.x * r.x), exp2((-9.28 * nv_1))) * r.x) + r.y); float2 ab = ((float2(-1.04, 1.04) * a004_) + r.zw); float3 ibl = ((irradiance * diffuse_color) + (prefiltered * ((f0_ * ab.x) + (ab.y).xxx))); float3 _e465 = color; float _e466 = occlusion; float _e471 = pf.params.x; float3 _e474 = emissive; color = ((_e465 + ((ibl * _e466) * _e471)) + _e474); float3 _e476 = color; color = lerp(_e476, tint_3.xyz, tint_3.w); float3 _e480 = color; float _e481 = alpha; float _e483 = alpha; return float4((_e480 * _e481), _e483); } `, MSL: `// language: metal2.4 #include <metal_stdlib> #include <simd/simd.h> using metal::uint; struct Light { metal::float4 dir; metal::float4 color; }; struct type_3 { Light inner[5]; }; struct type_4 { metal::float4 inner[6]; }; struct Frame { metal::float4x4 proj; type_3 lights; metal::float4 ambient; metal::float4 viewport; metal::float4 params; type_4 planes; metal::float4 section; }; struct Item { metal::float4x4 mv; metal::float4 tint; metal::float4 params; metal::float4 extra; }; struct PickOut { metal::uint2 ids; float depth; char _pad2[4]; }; struct TexSlot { metal::float4 a; metal::float4 b; }; struct type_10 { TexSlot inner[5]; }; struct Material { metal::float4 color; metal::float4 factors; metal::float4 emissive; metal::float4 flags; type_10 slots; }; struct PBRFrame { metal::float4x4 shadow_vp; metal::float4x4 view_to_world; metal::float4 params; metal::float4 shadow; metal::float4 env; }; typedef metal::float4x4 type_14[1]; typedef float type_15[1]; typedef metal::float4 type_16[1]; struct VOut { metal::float4 clip; metal::float3 view_pos; metal::float3 normal; metal::float4 tangent; metal::float2 uv0_; metal::float2 uv1_; metal::float4 color; metal::float4 tint; float flags; char _pad9[12]; }; constant uint HAS_NORMAL = 1u; constant uint HAS_TANGENT = 2u; constant uint HAS_UV0_ = 4u; constant uint HAS_UV1_ = 8u; constant uint SKINNED = 32u; constant float PI = 3.1415927; int naga_f2i32(float value) { return static_cast<int>(metal::clamp(value, -2147483600.0, 2147483500.0)); } bool section_clipped( metal::float3 p_1, int skip, constant Frame& frame ) { int i_1 = 0; float _e5 = frame.section.x; int count = naga_f2i32(_e5); uint2 loop_bound = uint2(4294967295u); bool loop_init = true; while(true) { if (metal::all(loop_bound == uint2(0u))) { break; } loop_bound -= uint2(loop_bound.y == 0u, 1u); if (!loop_init) { int _e28 = i_1; i_1 = as_type<int>(as_type<uint>(_e28) + as_type<uint>(1)); } loop_init = false; int _e9 = i_1; if (_e9 < 6) { } else { break; } { int _e12 = i_1; if (_e12 >= count) { break; } int _e14 = i_1; if (_e14 == skip) { continue; } int _e18 = i_1; metal::float4 plane = frame.planes.inner[_e18]; if ((metal::dot(plane.xyz, p_1) + plane.w) > 0.0) { return true; } } } return false; } metal::float3 srgb_to_linear( metal::float3 c_1 ) { metal::float3 lo = c_1 / metal::float3(12.92); metal::float3 hi = metal::pow((c_1 + metal::float3(0.055)) / metal::float3(1.055), metal::float3(2.4)); return metal::select(hi, lo, c_1 <= metal::float3(0.04045)); } metal::float2 slot_uv( int i_2, metal::float2 uv0_1, metal::float2 uv1_1, constant Material& material ) { metal::float2 uv = {}; TexSlot s = material.slots.inner[i_2]; uv = uv0_1; if (s.a.w > 0.5) { uv = uv1_1; } float _e15 = uv.x; float _e20 = uv.y; float _e29 = uv.x; float _e34 = uv.y; return metal::float2(((s.a.x * _e15) + (s.a.y * _e20)) + s.a.z, ((s.b.x * _e29) + (s.b.y * _e34)) + s.b.z); } bool slot_has( int i_3, constant Material& material ) { float _e6 = material.slots.inner[i_3].b.w; return _e6 > 0.5; } float distribution_ggx( float nh, float alpha_1 ) { float a2_ = alpha_1 * alpha_1; float d = ((nh * nh) * (a2_ - 1.0)) + 1.0; return a2_ / ((PI * d) * d); } float visibility_smith_ggx( float nl, float nv, float alpha_2 ) { float a2_1 = alpha_2 * alpha_2; float gv = nl * metal::sqrt(((nv * nv) * (1.0 - a2_1)) + a2_1); float gl = nv * metal::sqrt(((nl * nl) * (1.0 - a2_1)) + a2_1); float s_1 = gv + gl; if (s_1 > 0.0) { return 0.5 / s_1; } return 0.0; } metal::int2 naga_f2i32(metal::float2 value) { return static_cast<metal::int2>(metal::clamp(value, -2147483600.0, 2147483500.0)); } float shadow_visibility( metal::float3 view_pos, metal::float3 n_2, constant PBRFrame& pf, metal::texture2d<float, metal::access::sample> shadow_map ) { bool local_1 = {}; bool local_2 = {}; bool local_3 = {}; bool local_4 = {}; float lit = 0.0; int dy = -1; int dx = {}; float _e5 = pf.params.w; metal::float3 p_2 = view_pos + (n_2 * _e5); metal::float4x4 _e10 = pf.shadow_vp; metal::float4 c_2 = _e10 * metal::float4(p_2, 1.0); float size = pf.shadow.y; metal::float2 uv_1 = metal::float2((c_2.x * 0.5) + 0.5, 0.5 - (c_2.y * 0.5)); metal::float2 coord = uv_1 * size; float _e34 = pf.params.z; float depth = c_2.z - _e34; if (!((uv_1.x < 0.0))) { local_1 = uv_1.y < 0.0; } else { local_1 = true; } bool _e46 = local_1; if (!(_e46)) { local_2 = uv_1.x >= 1.0; } else { local_2 = true; } bool _e54 = local_2; if (!(_e54)) { local_3 = uv_1.y >= 1.0; } else { local_3 = true; } bool _e62 = local_3; if (!(_e62)) { local_4 = c_2.z > 1.0; } else { local_4 = true; } bool _e70 = local_4; if (_e70) { return 1.0; } metal::int2 base_1 = naga_f2i32(metal::floor(coord)); int top = as_type<int>(as_type<uint>(naga_f2i32(size)) - as_type<uint>(1)); uint2 loop_bound_1 = uint2(4294967295u); bool loop_init_1 = true; while(true) { if (metal::all(loop_bound_1 == uint2(0u))) { break; } loop_bound_1 -= uint2(loop_bound_1.y == 0u, 1u); if (!loop_init_1) { int _e108 = dy; dy = as_type<int>(as_type<uint>(_e108) + as_type<uint>(1)); } loop_init_1 = false; int _e81 = dy; if (_e81 <= 1) { } else { break; } { dx = -1; uint2 loop_bound_2 = uint2(4294967295u); bool loop_init_2 = true; while(true) { if (metal::all(loop_bound_2 == uint2(0u))) { break; } loop_bound_2 -= uint2(loop_bound_2.y == 0u, 1u); if (!loop_init_2) { int _e105 = dx; dx = as_type<int>(as_type<uint>(_e105) + as_type<uint>(1)); } loop_init_2 = false; int _e86 = dx; if (_e86 <= 1) { } else { break; } { int _e89 = dx; int _e90 = dy; metal::int2 q = metal::clamp(as_type<metal::int2>(as_type<metal::uint2>(base_1) + as_type<metal::uint2>(metal::int2(_e89, _e90))), metal::int2(0), metal::int2(top)); metal::float4 _e99 = shadow_map.read(metal::uint2(q), 0); float stored = _e99.x; if (depth <= stored) { float _e102 = lit; lit = _e102 + 1.0; } } } } } float _e111 = lit; float _e117 = pf.shadow.x; return metal::mix(1.0, _e111 / 9.0, _e117); } uint naga_f2u32(float value) { return static_cast<uint>(metal::clamp(value, 0.0, 4294967000.0)); } struct vs_pbrInput { metal::float3 p [[attribute(0)]]; metal::float3 n [[attribute(1)]]; metal::float4 t [[attribute(2)]]; metal::float2 uv0_ [[attribute(3)]]; metal::float2 uv1_ [[attribute(4)]]; metal::float4 c [[attribute(5)]]; metal::uint4 joints [[attribute(6)]]; metal::float4 weights [[attribute(7)]]; metal::float4 m0_ [[attribute(8)]]; metal::float4 m1_ [[attribute(9)]]; metal::float4 m2_ [[attribute(10)]]; metal::float4 m3_ [[attribute(11)]]; metal::float4 tint [[attribute(12)]]; metal::float4 extra [[attribute(13)]]; }; struct vs_pbrOutput { metal::float4 clip [[position]]; metal::float3 view_pos [[user(loc0), center_perspective]]; metal::float3 normal [[user(loc1), center_perspective]]; metal::float4 tangent [[user(loc2), center_perspective]]; metal::float2 uv0_ [[user(loc3), center_perspective]]; metal::float2 uv1_ [[user(loc4), center_perspective]]; metal::float4 color [[user(loc5), center_perspective]]; metal::float4 tint [[user(loc6), center_perspective]]; float flags [[user(loc7), flat]]; }; vertex vs_pbrOutput vs_pbr( vs_pbrInput varyings [[stage_in]] , uint vertex_ [[vertex_id]] , constant Frame& frame [[buffer(0)]] , device type_14 const& joint_matrices [[buffer(3)]] , device type_15 const& morph_weights [[buffer(4)]] , device type_16 const& morph_deltas [[buffer(5)]] ) { const auto p = varyings.p; const auto n = varyings.n; const auto t = varyings.t; const auto uv0_ = varyings.uv0_; const auto uv1_ = varyings.uv1_; const auto c = varyings.c; const auto joints = varyings.joints; const auto weights = varyings.weights; const auto m0_ = varyings.m0_; const auto m1_ = varyings.m1_; const auto m2_ = varyings.m2_; const auto m3_ = varyings.m3_; const auto tint = varyings.tint; const auto extra = varyings.extra; metal::float3 pos = {}; metal::float3 nrm = {}; metal::float3 tan = {}; uint k = 0u; VOut o = {}; uint flags = naga_f2u32(extra.w); pos = p; nrm = n; tan = t.xyz; uint targets = naga_f2u32(extra.z); if (targets > 0u) { uint wbase = naga_f2u32(extra.y); uint2 loop_bound_3 = uint2(4294967295u); bool loop_init_3 = true; while(true) { if (metal::all(loop_bound_3 == uint2(0u))) { break; } loop_bound_3 -= uint2(loop_bound_3.y == 0u, 1u); if (!loop_init_3) { uint _e59 = k; k = _e59 + 1u; } loop_init_3 = false; uint _e29 = k; if (_e29 < targets) { } else { break; } { uint _e32 = k; float w = morph_weights[wbase + _e32]; if (w != 0.0) { uint _e39 = k; uint i_4 = ((vertex_ * targets) + _e39) * 2u; metal::float3 _e43 = pos; metal::float4 _e46 = morph_deltas[i_4]; pos = _e43 + (w * _e46.xyz); metal::float3 _e50 = nrm; metal::float4 _e55 = morph_deltas[i_4 + 1u]; nrm = _e50 + (w * _e55.xyz); } } } } if ((flags & SKINNED) != 0u) { uint jb = naga_f2u32(extra.x); metal::float4x4 _e72 = joint_matrices[jb + joints.x]; metal::float4x4 _e79 = joint_matrices[jb + joints.y]; metal::float4x4 _e87 = joint_matrices[jb + joints.z]; metal::float4x4 _e95 = joint_matrices[jb + joints.w]; metal::float4x4 skin = (((_e72 * weights.x) + (_e79 * weights.y)) + (_e87 * weights.z)) + (_e95 * weights.w); metal::float3 _e99 = pos; pos = (skin * metal::float4(_e99, 1.0)).xyz; metal::float3 _e104 = nrm; nrm = (skin * metal::float4(_e104, 0.0)).xyz; metal::float3 _e109 = tan; tan = (skin * metal::float4(_e109, 0.0)).xyz; } metal::float4x4 mv = metal::float4x4(m0_, m1_, m2_, m3_); metal::float3 _e115 = pos; metal::float4 vp = mv * metal::float4(_e115, 1.0); o.view_pos = vp.xyz; metal::float4x4 _e125 = frame.proj; o.clip = _e125 * vp; metal::float3 _e128 = nrm; o.normal = (mv * metal::float4(_e128, 0.0)).xyz; metal::float3 _e134 = tan; o.tangent = metal::float4((mv * metal::float4(_e134, 0.0)).xyz, t.w); o.uv0_ = uv0_; o.uv1_ = uv1_; o.color = c; o.tint = tint; o.flags = extra.w; VOut _e147 = o; const auto _tmp = _e147; return vs_pbrOutput { _tmp.clip, _tmp.view_pos, _tmp.normal, _tmp.tangent, _tmp.uv0_, _tmp.uv1_, _tmp.color, _tmp.tint, _tmp.flags }; } struct fs_pbrInput { metal::float3 view_pos_1 [[user(loc0), center_perspective]]; metal::float3 normal [[user(loc1), center_perspective]]; metal::float4 tangent_1 [[user(loc2), center_perspective]]; metal::float2 uv0_2 [[user(loc3), center_perspective]]; metal::float2 uv1_2 [[user(loc4), center_perspective]]; metal::float4 color_1 [[user(loc5), center_perspective]]; metal::float4 tint_1 [[user(loc6), center_perspective]]; float flags [[user(loc7), flat]]; }; struct fs_pbrOutput { metal::float4 member_1 [[color(0)]]; }; fragment fs_pbrOutput fs_pbr( fs_pbrInput varyings_1 [[stage_in]] , metal::float4 clip [[position]] , bool front [[front_facing]] , constant Frame& frame [[buffer(0)]] , constant Material& material [[buffer(1)]] , metal::texture2d<float, metal::access::sample> tex_base [[texture(0)]] , metal::texture2d<float, metal::access::sample> tex_mr [[texture(1)]] , metal::texture2d<float, metal::access::sample> tex_normal [[texture(2)]] , metal::texture2d<float, metal::access::sample> tex_occlusion [[texture(3)]] , metal::texture2d<float, metal::access::sample> tex_emissive [[texture(4)]] , metal::sampler samp_base [[sampler(0)]] , metal::sampler samp_mr [[sampler(1)]] , metal::sampler samp_normal [[sampler(2)]] , metal::sampler samp_occlusion [[sampler(3)]] , metal::sampler samp_emissive [[sampler(4)]] , constant PBRFrame& pf [[buffer(2)]] , metal::texturecube<float, metal::access::sample> env_spec [[texture(5)]] , metal::texturecube<float, metal::access::sample> env_irr [[texture(6)]] , metal::sampler env_samp [[sampler(5)]] , metal::texture2d<float, metal::access::sample> shadow_map [[texture(7)]] ) { const VOut in = { clip, varyings_1.view_pos_1, varyings_1.normal, varyings_1.tangent_1, varyings_1.uv0_2, varyings_1.uv1_2, varyings_1.color_1, varyings_1.tint_1, varyings_1.flags }; metal::float4 base = {}; float alpha = {}; bool local = {}; float metallic = {}; float roughness = {}; float occlusion = 1.0; metal::float3 emissive = {}; metal::float3 n_1 = {}; float sign = 1.0; metal::float3 bitangent_geom = metal::float3(0.0); metal::float3 tangent = {}; metal::float3 bitangent = {}; metal::float3 t_1 = {}; metal::float3 tn = {}; metal::float3 color = metal::float3(0.0); int i = 0; float visible = {}; uint flags_1 = naga_f2u32(in.flags); metal::float3 dp_dx = metal::dfdx(in.view_pos); metal::float3 dp_dy = metal::dfdy(in.view_pos); metal::float2 _e11 = slot_uv(0, in.uv0_, in.uv1_, material); metal::float2 _e15 = slot_uv(1, in.uv0_, in.uv1_, material); metal::float2 _e19 = slot_uv(2, in.uv0_, in.uv1_, material); metal::float2 _e23 = slot_uv(3, in.uv0_, in.uv1_, material); metal::float2 _e27 = slot_uv(4, in.uv0_, in.uv1_, material); metal::float2 duv_dx = metal::dfdx(_e19); metal::float2 duv_dy = metal::dfdy(_e19); metal::float4 t_base = tex_base.sample(samp_base, _e11); metal::float4 t_mr = tex_mr.sample(samp_mr, _e15); metal::float4 t_normal = tex_normal.sample(samp_normal, _e19); metal::float4 t_occlusion = tex_occlusion.sample(samp_occlusion, _e23); metal::float4 t_emissive = tex_emissive.sample(samp_emissive, _e27); bool _e47 = section_clipped(in.view_pos, -1, frame); if (_e47) { metal::discard_fragment(); } metal::float4 _e50 = material.color; base = _e50; float _e55 = material.flags.z; uint bits = naga_f2u32(_e55); if ((bits & 2u) != 0u) { metal::float4 _e61 = base; metal::float3 _e64 = srgb_to_linear(in.color.xyz); base = _e61 * metal::float4(_e64, in.color.w); } bool _e70 = slot_has(0, material); if (_e70) { metal::float4 _e71 = base; metal::float3 _e73 = srgb_to_linear(t_base.xyz); base = _e71 * metal::float4(_e73, t_base.w); } float _e80 = material.flags.x; uint mode = naga_f2u32(_e80); float _e83 = base.w; alpha = _e83; if (mode == 0u) { alpha = 1.0; } else { if (mode == 1u) { float _e92 = alpha; float _e96 = material.flags.y; local = _e92 < _e96; } else { local = false; } bool _e99 = local; if (_e99) { metal::discard_fragment(); } } metal::float3 _e102 = srgb_to_linear(in.tint.xyz); metal::float4 tint_1 = metal::float4(_e102, in.tint.w); if ((bits & 1u) != 0u) { metal::float4 _e110 = base; metal::float3 c_3 = metal::mix(_e110.xyz, tint_1.xyz, tint_1.w); float _e115 = alpha; float _e117 = alpha; return fs_pbrOutput { metal::float4(c_3 * _e115, _e117) }; } float _e122 = material.factors.x; metallic = _e122; float _e127 = material.factors.y; roughness = _e127; bool _e130 = slot_has(1, material); if (_e130) { float _e131 = roughness; roughness = _e131 * t_mr.y; float _e134 = metallic; metallic = _e134 * t_mr.z; } float _e137 = metallic; metallic = metal::clamp(_e137, 0.0, 1.0); float _e141 = roughness; roughness = metal::clamp(_e141, 0.045, 1.0); bool _e148 = slot_has(3, material); if (_e148) { float _e152 = material.factors.w; occlusion = 1.0 + (_e152 * (t_occlusion.x - 1.0)); } metal::float4 _e161 = material.emissive; emissive = _e161.xyz; bool _e165 = slot_has(4, material); if (_e165) { metal::float3 _e166 = emissive; metal::float3 _e168 = srgb_to_linear(t_emissive.xyz); emissive = _e166 * _e168; } metal::float3 v = metal::normalize(-(in.view_pos)); n_1 = in.normal; if ((flags_1 & HAS_NORMAL) == 0u) { n_1 = metal::normalize(metal::cross(dp_dx, dp_dy)); metal::float3 _e186 = n_1; if (metal::dot(_e186, v) < 0.0) { metal::float3 _e190 = n_1; n_1 = -(_e190); } } else { metal::float3 _e192 = n_1; n_1 = metal::normalize(_e192); metal::float3 _e194 = n_1; bitangent_geom = metal::cross(_e194, metal::normalize(in.tangent.xyz)) * in.tangent.w; if (!(front)) { metal::float3 _e203 = n_1; n_1 = -(_e203); sign = -1.0; } } bool _e207 = slot_has(2, material); if (_e207) { if ((flags_1 & HAS_TANGENT) != 0u) { float _e217 = sign; tangent = metal::normalize(in.tangent.xyz) * _e217; metal::float3 _e219 = bitangent_geom; float _e220 = sign; bitangent = _e219 * _e220; } else { float det = (duv_dx.x * duv_dy.y) - (duv_dy.x * duv_dx.y); t_1 = ((dp_dx * duv_dy.y) - (dp_dy * duv_dx.y)) * ((det < 0.0) ? -1.0 : 1.0); metal::float3 _e241 = t_1; metal::float3 _e242 = n_1; metal::float3 _e243 = n_1; metal::float3 _e244 = t_1; t_1 = _e241 - (_e242 * metal::dot(_e243, _e244)); metal::float3 _e248 = t_1; metal::float3 _e249 = t_1; metal::float3 _e250 = t_1; tangent = _e248 * metal::rsqrt(metal::max(metal::dot(_e249, _e250), 0.00000000000000000001)); metal::float3 _e256 = n_1; metal::float3 _e257 = tangent; bitangent = metal::cross(_e256, _e257); } tn = (t_normal.xyz * 2.0) - metal::float3(1.0); metal::float3 _e266 = tn; float _e271 = material.factors.z; float _e274 = tn.z; tn = metal::float3(_e266.xy * _e271, _e274); metal::float3 _e276 = tangent; float _e278 = tn.x; metal::float3 _e280 = bitangent; float _e282 = tn.y; metal::float3 _e285 = n_1; float _e287 = tn.z; n_1 = metal::normalize(((_e276 * _e278) + (_e280 * _e282)) + (_e285 * _e287)); } metal::float3 _e291 = n_1; float nv_1 = metal::clamp(metal::abs(metal::dot(_e291, v)), 0.0001, 1.0); metal::float4 _e299 = base; float _e301 = metallic; metal::float3 f0_ = metal::mix(metal::float3(0.04), _e299.xyz, _e301); metal::float4 _e303 = base; float _e305 = metallic; metal::float3 diffuse_color = _e303.xyz * (1.0 - _e305); float _e309 = roughness; float _e310 = roughness; float alpha_r = _e309 * _e310; uint2 loop_bound_4 = uint2(4294967295u); bool loop_init_4 = true; while(true) { if (metal::all(loop_bound_4 == uint2(0u))) { break; } loop_bound_4 -= uint2(loop_bound_4.y == 0u, 1u); if (!loop_init_4) { int _e391 = i; i = as_type<int>(as_type<uint>(_e391) + as_type<uint>(1)); } loop_init_4 = false; int _e317 = i; if (_e317 < 5) { } else { break; } { int _e322 = i; metal::float4 _e325 = frame.lights.inner[_e322].dir; metal::float3 l = metal::normalize(_e325.xyz); metal::float3 _e328 = n_1; float nl_1 = metal::dot(_e328, l); if (nl_1 > 0.0) { metal::float3 h = metal::normalize(l + v); metal::float3 _e334 = n_1; float nh_1 = metal::clamp(metal::dot(_e334, h), 0.0, 1.0); float vh = metal::clamp(metal::dot(v, h), 0.0, 1.0); metal::float3 fresnel = f0_ + ((metal::float3(1.0) - f0_) * metal::pow(1.0 - vh, 5.0)); float _e352 = distribution_ggx(nh_1, alpha_r); float _e354 = visibility_smith_ggx(nl_1, nv_1, alpha_r); metal::float3 spec = (fresnel * _e352) * _e354; metal::float3 diff = ((metal::float3(1.0) - fresnel) * diffuse_color) / metal::float3(3.1415927); visible = 1.0; int _e365 = i; float _e369 = pf.params.y; if (_e365 == naga_f2i32(_e369)) { metal::float3 _e373 = n_1; float _e374 = shadow_visibility(in.view_pos, _e373, pf, shadow_map); visible = _e374; } metal::float3 _e375 = color; int _e379 = i; metal::float4 _e382 = frame.lights.inner[_e379].color; float _e388 = visible; color = _e375 + (((((diff + spec) * _e382.xyz) * nl_1) * PI) * _e388); } } } metal::float4x4 _e396 = pf.view_to_world; metal::float3 _e397 = n_1; metal::float3 nw = (_e396 * metal::float4(_e397, 0.0)).xyz; metal::float4x4 _e404 = pf.view_to_world; metal::float3 _e406 = n_1; metal::float3 rw = (_e404 * metal::float4(metal::reflect(-(v), _e406), 0.0)).xyz; metal::float4 _e415 = env_irr.sample(env_samp, nw, metal::level(0.0)); metal::float3 irradiance = _e415.xyz; float _e419 = roughness; float _e423 = pf.env.x; metal::float4 _e425 = env_spec.sample(env_samp, rw, metal::level(_e419 * _e423)); metal::float3 prefiltered = _e425.xyz; metal::float4 c0_ = metal::float4(-1.0, -0.0275, -0.572, 0.022); metal::float4 c1_ = metal::float4(1.0, 0.0425, 1.04, -0.04); float _e437 = roughness; metal::float4 r = (_e437 * c0_) + c1_; float a004_ = (metal::min(r.x * r.x, metal::exp2(-9.28 * nv_1)) * r.x) + r.y; metal::float2 ab = (metal::float2(-1.04, 1.04) * a004_) + r.zw; metal::float3 ibl = (irradiance * diffuse_color) + (prefiltered * ((f0_ * ab.x) + metal::float3(ab.y))); metal::float3 _e465 = color; float _e466 = occlusion; float _e471 = pf.params.x; metal::float3 _e474 = emissive; color = (_e465 + ((ibl * _e466) * _e471)) + _e474; metal::float3 _e476 = color; color = metal::mix(_e476, tint_1.xyz, tint_1.w); metal::float3 _e480 = color; float _e481 = alpha; float _e483 = alpha; return fs_pbrOutput { metal::float4(_e480 * _e481, _e483) }; } `, Entries: []gpu.EntryPoint{ {Name: "vs_pbr", Stage: gpu.ShaderStageVertex, ReadsVertexIndex: true, Inputs: []gpu.VertexInput{ {Name: "p", Location: 0, Type: gpu.ShaderTypeFloat32x3}, {Name: "n", Location: 1, Type: gpu.ShaderTypeFloat32x3}, {Name: "t", Location: 2, Type: gpu.ShaderTypeFloat32x4}, {Name: "uv0", Location: 3, Type: gpu.ShaderTypeFloat32x2}, {Name: "uv1", Location: 4, Type: gpu.ShaderTypeFloat32x2}, {Name: "c", Location: 5, Type: gpu.ShaderTypeFloat32x4}, {Name: "joints", Location: 6, Type: gpu.ShaderTypeUint32x4}, {Name: "weights", Location: 7, Type: gpu.ShaderTypeFloat32x4}, {Name: "m0", Location: 8, Type: gpu.ShaderTypeFloat32x4}, {Name: "m1", Location: 9, Type: gpu.ShaderTypeFloat32x4}, {Name: "m2", Location: 10, Type: gpu.ShaderTypeFloat32x4}, {Name: "m3", Location: 11, Type: gpu.ShaderTypeFloat32x4}, {Name: "tint", Location: 12, Type: gpu.ShaderTypeFloat32x4}, {Name: "extra", Location: 13, Type: gpu.ShaderTypeFloat32x4}, }, Bindings: []gpu.ShaderBinding{ {Name: "frame", Group: 0, Binding: 0, Kind: gpu.BindingUniformBuffer, Size: 384, Register: gpu.ShaderRegister{Class: "b", Space: 0, Index: 0}, MetalIndex: 0}, {Name: "joint_matrices", Group: 2, Binding: 5, Kind: gpu.BindingStorageBufferReadOnly, Size: 64, Register: gpu.ShaderRegister{Class: "t", Space: 2, Index: 5}, MetalIndex: 3}, {Name: "morph_weights", Group: 2, Binding: 6, Kind: gpu.BindingStorageBufferReadOnly, Size: 4, Register: gpu.ShaderRegister{Class: "t", Space: 2, Index: 6}, MetalIndex: 4}, {Name: "morph_deltas", Group: 3, Binding: 0, Kind: gpu.BindingStorageBufferReadOnly, Size: 16, Register: gpu.ShaderRegister{Class: "t", Space: 3, Index: 0}, MetalIndex: 5}, }, }, {Name: "fs_pbr", Stage: gpu.ShaderStageFragment, Bindings: []gpu.ShaderBinding{ {Name: "frame", Group: 0, Binding: 0, Kind: gpu.BindingUniformBuffer, Size: 384, Register: gpu.ShaderRegister{Class: "b", Space: 0, Index: 0}, MetalIndex: 0}, {Name: "material", Group: 1, Binding: 0, Kind: gpu.BindingUniformBuffer, Size: 224, Register: gpu.ShaderRegister{Class: "b", Space: 1, Index: 0}, MetalIndex: 1}, {Name: "tex_base", Group: 1, Binding: 1, Kind: gpu.BindingSampledTexture, Register: gpu.ShaderRegister{Class: "t", Space: 1, Index: 1}, MetalIndex: 0}, {Name: "tex_mr", Group: 1, Binding: 2, Kind: gpu.BindingSampledTexture, Register: gpu.ShaderRegister{Class: "t", Space: 1, Index: 2}, MetalIndex: 1}, {Name: "tex_normal", Group: 1, Binding: 3, Kind: gpu.BindingSampledTexture, Register: gpu.ShaderRegister{Class: "t", Space: 1, Index: 3}, MetalIndex: 2}, {Name: "tex_occlusion", Group: 1, Binding: 4, Kind: gpu.BindingSampledTexture, Register: gpu.ShaderRegister{Class: "t", Space: 1, Index: 4}, MetalIndex: 3}, {Name: "tex_emissive", Group: 1, Binding: 5, Kind: gpu.BindingSampledTexture, Register: gpu.ShaderRegister{Class: "t", Space: 1, Index: 5}, MetalIndex: 4}, {Name: "samp_base", Group: 1, Binding: 6, Kind: gpu.BindingSampler, Register: gpu.ShaderRegister{Class: "s", Space: 1, Index: 6}, MetalIndex: 0}, {Name: "samp_mr", Group: 1, Binding: 7, Kind: gpu.BindingSampler, Register: gpu.ShaderRegister{Class: "s", Space: 1, Index: 7}, MetalIndex: 1}, {Name: "samp_normal", Group: 1, Binding: 8, Kind: gpu.BindingSampler, Register: gpu.ShaderRegister{Class: "s", Space: 1, Index: 8}, MetalIndex: 2}, {Name: "samp_occlusion", Group: 1, Binding: 9, Kind: gpu.BindingSampler, Register: gpu.ShaderRegister{Class: "s", Space: 1, Index: 9}, MetalIndex: 3}, {Name: "samp_emissive", Group: 1, Binding: 10, Kind: gpu.BindingSampler, Register: gpu.ShaderRegister{Class: "s", Space: 1, Index: 10}, MetalIndex: 4}, {Name: "pf", Group: 2, Binding: 0, Kind: gpu.BindingUniformBuffer, Size: 176, Register: gpu.ShaderRegister{Class: "b", Space: 2, Index: 0}, MetalIndex: 2}, {Name: "env_spec", Group: 2, Binding: 1, Kind: gpu.BindingSampledTexture, Dimension: gpu.TextureDimCube, Register: gpu.ShaderRegister{Class: "t", Space: 2, Index: 1}, MetalIndex: 5}, {Name: "env_irr", Group: 2, Binding: 2, Kind: gpu.BindingSampledTexture, Dimension: gpu.TextureDimCube, Register: gpu.ShaderRegister{Class: "t", Space: 2, Index: 2}, MetalIndex: 6}, {Name: "env_samp", Group: 2, Binding: 3, Kind: gpu.BindingSampler, Register: gpu.ShaderRegister{Class: "s", Space: 2, Index: 3}, MetalIndex: 5}, {Name: "shadow_map", Group: 2, Binding: 4, Kind: gpu.BindingSampledTexture, Sample: gpu.SampleTypeUnfilterableFloat, Register: gpu.ShaderRegister{Class: "t", Space: 2, Index: 4}, MetalIndex: 7}, }, }, }, }
Pbr is pbr.wgsl.
var Pick = &gpu.Shader{ Name: "pick", WGSL: `// The picking of triangles in viewport3d (SPEC §27.8): a pick pass draws the items into a small target that has, for each pixel, // the number of the item and of the triangle, and the depth. The triangles are drawn from a vertex buffer of their corners that // no index reorders, so that vertex_index / 3 is the number of the triangle. The lines and the points have vs_pick and fs_pick // in their own shaders. // What the shaders of viewport3d share (SPEC §27.8): the frame's uniform data, the record of an item, and the section planes. struct Light { // dir points to the light, in view space. dir: vec4<f32>, // color is the light's color times its intensity. color: vec4<f32>, } struct Frame { proj: mat4x4<f32>, // lights are the light at the camera, then up to four directional ones; an unused one has a black color. lights: array<Light, 5>, ambient: vec4<f32>, // viewport is the width and the height of the target in pixels, and their inverses. viewport: vec4<f32>, // params are the near plane's distance, the device pixel ratio and two unused numbers. params: vec4<f32>, // planes are the section planes in view space, relative to the eye (D30): a point p with dot(plane.xyz, p) + plane.w > 0 is // cut away. planes: array<vec4<f32>, 6>, // section is the number of planes in x. section: vec4<f32>, } @group(0) @binding(0) var<uniform> frame: Frame; // Item is the record of a line, a point or a picked item: the matrix that takes the mesh's positions to the view, relative to // the eye, a tint (the highlight of a selected item: its color, and the part of it that replaces the material's in a), and // numbers for the shader that reads it. struct Item { mv: mat4x4<f32>, tint: vec4<f32>, params: vec4<f32>, extra: vec4<f32>, } // section_clipped says whether a point of the view space is cut away by a section plane. skip is the plane to leave out (the // plane that a cap lies on), or -1. fn section_clipped(p: vec3<f32>, skip: i32) -> bool { let count = i32(frame.section.x); for (var i = 0; i < 6; i = i + 1) { if (i >= count) { break; } if (i == skip) { continue; } let plane = frame.planes[i]; if (dot(plane.xyz, p) + plane.w > 0.0) { return true; } } return false; } // PickOut is what a pick pass writes for a pixel: the number of the item (from 1, 0 for nothing) and of the primitive in it, and // the depth. struct PickOut { @location(0) ids: vec2<u32>, @location(1) depth: f32, } @group(1) @binding(0) var<uniform> item: Item; struct Picked { @builtin(position) clip: vec4<f32>, @location(0) view_pos: vec3<f32>, @location(1) @interpolate(flat) prim: u32, } @vertex fn vs_tri(@location(0) p: vec3<f32>, @builtin(vertex_index) i: u32) -> Picked { var o: Picked; let v = item.mv * vec4<f32>(p, 1.0); o.view_pos = v.xyz; o.clip = frame.proj * v; o.prim = i / 3u; return o; } @fragment fn fs_tri(in: Picked) -> PickOut { if (section_clipped(in.view_pos, -1)) { discard; } var o: PickOut; o.ids = vec2<u32>(u32(item.params.x), in.prim); o.depth = in.clip.z; return o; } `, SPIRV: spirvPick, HLSL: `struct Light { float4 dir; float4 color; }; struct Frame { row_major float4x4 proj; Light lights[5]; float4 ambient; float4 viewport; float4 params; float4 planes[6]; float4 section; }; struct Item { row_major float4x4 mv; float4 tint; float4 params; float4 extra; }; struct PickOut { uint2 ids : SV_Target0; float depth : SV_Target1; }; struct Picked { float4 clip_ : SV_Position; float3 view_pos : LOC0; nointerpolation uint prim : LOC1; }; cbuffer frame : register(b0, space0) { Frame frame; } cbuffer item : register(b0, space1) { Item item; } struct VertexOutput_vs_tri { float3 view_pos : LOC0; nointerpolation uint prim : LOC1; float4 clip_ : SV_Position; }; struct FragmentInput_fs_tri { float3 view_pos_1 : LOC0; nointerpolation uint prim_1 : LOC1; float4 clip_1 : SV_Position; }; int naga_f2i32(float value) { return int(clamp(value, -2147483600.0, 2147483500.0)); } bool section_clipped(float3 p_1, int skip) { int i_1 = int(0); float _e5 = frame.section.x; int count = naga_f2i32(_e5); uint2 loop_bound = uint2(4294967295u, 4294967295u); bool loop_init = true; while(true) { if (all(loop_bound == uint2(0u, 0u))) { break; } loop_bound -= uint2(loop_bound.y == 0u, 1u); if (!loop_init) { int _e28 = i_1; i_1 = asint(asuint(_e28) + asuint(int(1))); } loop_init = false; int _e9 = i_1; if ((_e9 < int(6))) { } else { break; } { int _e12 = i_1; if ((_e12 >= count)) { break; } int _e14 = i_1; if ((_e14 == skip)) { continue; } int _e18 = i_1; float4 plane = frame.planes[_e18]; if (((dot(plane.xyz, p_1) + plane.w) > 0.0)) { return true; } } } return false; } uint naga_div(uint lhs, uint rhs) { return lhs / (rhs == 0u ? 1u : rhs); } VertexOutput_vs_tri vs_tri(float3 p : LOC0, uint i : SV_VertexID) { Picked o = (Picked)0; float4x4 _e5 = item.mv; float4 v = mul(float4(p, 1.0), _e5); o.view_pos = v.xyz; float4x4 _e14 = frame.proj; o.clip_ = mul(v, _e14); o.prim = naga_div(i, 3u); Picked _e19 = o; Picked picked = _e19; VertexOutput_vs_tri picked_1 = { picked.view_pos, picked.prim, picked.clip_ }; return picked_1; } uint naga_f2u32(float value) { return uint(clamp(value, 0.0, 4294967000.0)); } PickOut fs_tri(FragmentInput_fs_tri fragmentinput_fs_tri) { Picked in_ = { fragmentinput_fs_tri.clip_1, fragmentinput_fs_tri.view_pos_1, fragmentinput_fs_tri.prim_1 }; PickOut o_1 = (PickOut)0; const bool _e3 = section_clipped(in_.view_pos, int(-1)); if (_e3) { discard; } float _e9 = item.params.x; o_1.ids = uint2(naga_f2u32(_e9), in_.prim); o_1.depth = in_.clip_.z; PickOut _e16 = o_1; PickOut pickout = _e16; return pickout; } `, MSL: `// language: metal2.4 #include <metal_stdlib> #include <simd/simd.h> using metal::uint; struct Light { metal::float4 dir; metal::float4 color; }; struct type_3 { Light inner[5]; }; struct type_4 { metal::float4 inner[6]; }; struct Frame { metal::float4x4 proj; type_3 lights; metal::float4 ambient; metal::float4 viewport; metal::float4 params; type_4 planes; metal::float4 section; }; struct Item { metal::float4x4 mv; metal::float4 tint; metal::float4 params; metal::float4 extra; }; struct PickOut { metal::uint2 ids; float depth; char _pad2[4]; }; struct Picked { metal::float4 clip; metal::packed_float3 view_pos; uint prim; }; int naga_f2i32(float value) { return static_cast<int>(metal::clamp(value, -2147483600.0, 2147483500.0)); } bool section_clipped( metal::float3 p_1, int skip, constant Frame& frame ) { int i_1 = 0; float _e5 = frame.section.x; int count = naga_f2i32(_e5); uint2 loop_bound = uint2(4294967295u); bool loop_init = true; while(true) { if (metal::all(loop_bound == uint2(0u))) { break; } loop_bound -= uint2(loop_bound.y == 0u, 1u); if (!loop_init) { int _e28 = i_1; i_1 = as_type<int>(as_type<uint>(_e28) + as_type<uint>(1)); } loop_init = false; int _e9 = i_1; if (_e9 < 6) { } else { break; } { int _e12 = i_1; if (_e12 >= count) { break; } int _e14 = i_1; if (_e14 == skip) { continue; } int _e18 = i_1; metal::float4 plane = frame.planes.inner[_e18]; if ((metal::dot(plane.xyz, p_1) + plane.w) > 0.0) { return true; } } } return false; } uint naga_div(uint lhs, uint rhs) { return lhs / metal::select(rhs, 1u, rhs == 0u); } struct vs_triInput { metal::float3 p [[attribute(0)]]; }; struct vs_triOutput { metal::float4 clip [[position]]; metal::float3 view_pos [[user(loc0), center_perspective]]; uint prim [[user(loc1), flat]]; }; vertex vs_triOutput vs_tri( vs_triInput varyings [[stage_in]] , uint i [[vertex_id]] , constant Frame& frame [[buffer(0)]] , constant Item& item [[buffer(1)]] ) { const auto p = varyings.p; Picked o = {}; metal::float4x4 _e5 = item.mv; metal::float4 v = _e5 * metal::float4(p, 1.0); o.view_pos = v.xyz; metal::float4x4 _e14 = frame.proj; o.clip = _e14 * v; o.prim = naga_div(i, 3u); Picked _e19 = o; const auto _tmp = _e19; return vs_triOutput { _tmp.clip, _tmp.view_pos, _tmp.prim }; } uint naga_f2u32(float value) { return static_cast<uint>(metal::clamp(value, 0.0, 4294967000.0)); } struct fs_triInput { metal::float3 view_pos [[user(loc0), center_perspective]]; uint prim [[user(loc1), flat]]; }; struct fs_triOutput { metal::uint2 ids [[color(0)]]; float depth [[color(1)]]; }; fragment fs_triOutput fs_tri( fs_triInput varyings_1 [[stage_in]] , metal::float4 clip [[position]] , constant Frame& frame [[buffer(0)]] , constant Item& item [[buffer(1)]] ) { const Picked in = { clip, varyings_1.view_pos, varyings_1.prim }; PickOut o_1 = {}; bool _e3 = section_clipped(in.view_pos, -1, frame); if (_e3) { metal::discard_fragment(); } float _e9 = item.params.x; o_1.ids = metal::uint2(naga_f2u32(_e9), in.prim); o_1.depth = in.clip.z; PickOut _e16 = o_1; const auto _tmp_1 = _e16; return fs_triOutput { _tmp_1.ids, _tmp_1.depth }; } `, Entries: []gpu.EntryPoint{ {Name: "vs_tri", Stage: gpu.ShaderStageVertex, ReadsVertexIndex: true, Inputs: []gpu.VertexInput{ {Name: "p", Location: 0, Type: gpu.ShaderTypeFloat32x3}, }, Bindings: []gpu.ShaderBinding{ {Name: "frame", Group: 0, Binding: 0, Kind: gpu.BindingUniformBuffer, Size: 384, Register: gpu.ShaderRegister{Class: "b", Space: 0, Index: 0}, MetalIndex: 0}, {Name: "item", Group: 1, Binding: 0, Kind: gpu.BindingUniformBuffer, Size: 112, Register: gpu.ShaderRegister{Class: "b", Space: 1, Index: 0}, MetalIndex: 1}, }, }, {Name: "fs_tri", Stage: gpu.ShaderStageFragment, Bindings: []gpu.ShaderBinding{ {Name: "frame", Group: 0, Binding: 0, Kind: gpu.BindingUniformBuffer, Size: 384, Register: gpu.ShaderRegister{Class: "b", Space: 0, Index: 0}, MetalIndex: 0}, {Name: "item", Group: 1, Binding: 0, Kind: gpu.BindingUniformBuffer, Size: 112, Register: gpu.ShaderRegister{Class: "b", Space: 1, Index: 0}, MetalIndex: 1}, }, }, }, }
Pick is pick.wgsl.
var Point = &gpu.Shader{ Name: "point", WGSL: `// The points of viewport3d (SPEC §27.8): each point is a quad of a size in logical pixels that the vertex shader makes in the // screen, and the fragment shader cuts it to a disc or a square with an anti-aliased edge. A point is an instance (its position // in the mesh's units); the corners of the quad come from a small vertex buffer. // What the shaders of viewport3d share (SPEC §27.8): the frame's uniform data, the record of an item, and the section planes. struct Light { // dir points to the light, in view space. dir: vec4<f32>, // color is the light's color times its intensity. color: vec4<f32>, } struct Frame { proj: mat4x4<f32>, // lights are the light at the camera, then up to four directional ones; an unused one has a black color. lights: array<Light, 5>, ambient: vec4<f32>, // viewport is the width and the height of the target in pixels, and their inverses. viewport: vec4<f32>, // params are the near plane's distance, the device pixel ratio and two unused numbers. params: vec4<f32>, // planes are the section planes in view space, relative to the eye (D30): a point p with dot(plane.xyz, p) + plane.w > 0 is // cut away. planes: array<vec4<f32>, 6>, // section is the number of planes in x. section: vec4<f32>, } @group(0) @binding(0) var<uniform> frame: Frame; // Item is the record of a line, a point or a picked item: the matrix that takes the mesh's positions to the view, relative to // the eye, a tint (the highlight of a selected item: its color, and the part of it that replaces the material's in a), and // numbers for the shader that reads it. struct Item { mv: mat4x4<f32>, tint: vec4<f32>, params: vec4<f32>, extra: vec4<f32>, } // section_clipped says whether a point of the view space is cut away by a section plane. skip is the plane to leave out (the // plane that a cap lies on), or -1. fn section_clipped(p: vec3<f32>, skip: i32) -> bool { let count = i32(frame.section.x); for (var i = 0; i < 6; i = i + 1) { if (i >= count) { break; } if (i == skip) { continue; } let plane = frame.planes[i]; if (dot(plane.xyz, p) + plane.w > 0.0) { return true; } } return false; } // PickOut is what a pick pass writes for a pixel: the number of the item (from 1, 0 for nothing) and of the primitive in it, and // the depth. struct PickOut { @location(0) ids: vec2<u32>, @location(1) depth: f32, } struct PointMaterial { color: vec4<f32>, // params are: x the size in logical pixels, y 0 for round points and 1 for square ones. params: vec4<f32>, } @group(1) @binding(0) var<uniform> point: PointMaterial; @group(2) @binding(0) var<uniform> item: Item; struct VOut { @builtin(position) clip: vec4<f32>, // local is the position in pixels from the center of the point. @location(0) local: vec2<f32>, @location(1) view_pos: vec3<f32>, // index is the number of the point in its mesh, which the picking reports. @location(2) @interpolate(flat) index: f32, // tint is the color of the point in the mesh (white when the mesh has none). @location(3) @interpolate(flat) tint: vec4<f32>, } @vertex fn vs_main(@location(0) p: vec3<f32>, @location(1) corner: vec2<f32>) -> VOut { return vertex_point(p, corner, 0.0, vec4<f32>(1.0)); } // vs_color is vs_main for a mesh whose points have colors (the material's VertexColors). @vertex fn vs_color(@location(0) p: vec3<f32>, @location(1) corner: vec2<f32>, @location(3) color: vec4<f32>) -> VOut { return vertex_point(p, corner, 0.0, color); } // vs_pick is vs_main for the picking: the point also has its number. @vertex fn vs_pick(@location(0) p: vec3<f32>, @location(1) corner: vec2<f32>, @location(2) index: f32) -> VOut { return vertex_point(p, corner, index, vec4<f32>(1.0)); } fn vertex_point(p: vec3<f32>, corner: vec2<f32>, index: f32, tint: vec4<f32>) -> VOut { var o: VOut; o.index = index; o.tint = tint; o.clip = vec4<f32>(2.0, 2.0, 2.0, 1.0); let v = item.mv * vec4<f32>(p, 1.0); if (-v.z < frame.params.x) { return o; } let k = frame.proj * v; let half_size = 0.5 * point.params.x * frame.params.y + 1.0; let offset = corner * half_size / (0.5 * frame.viewport.xy); o.clip = vec4<f32>(k.xy + offset * k.w, k.z, k.w); o.local = corner * half_size; o.view_pos = v.xyz; return o; } @fragment fn fs_main(in: VOut) -> @location(0) vec4<f32> { if (section_clipped(in.view_pos, -1)) { discard; } let half_size = 0.5 * point.params.x * frame.params.y; var cover = 0.0; if (point.params.y > 0.5) { cover = clamp(half_size + 0.5 - abs(in.local.x), 0.0, 1.0) * clamp(half_size + 0.5 - abs(in.local.y), 0.0, 1.0); } else { cover = clamp(half_size + 0.5 - length(in.local), 0.0, 1.0); } let rgb = mix(point.color.rgb * in.tint.rgb, item.tint.rgb, item.tint.a); let a = point.color.a * in.tint.a * cover; return vec4<f32>(rgb * a, a); } @fragment fn fs_pick(in: VOut) -> PickOut { if (section_clipped(in.view_pos, -1)) { discard; } let half_size = 0.5 * point.params.x * frame.params.y; var cover = 0.0; if (point.params.y > 0.5) { cover = clamp(half_size + 0.5 - abs(in.local.x), 0.0, 1.0) * clamp(half_size + 0.5 - abs(in.local.y), 0.0, 1.0); } else { cover = clamp(half_size + 0.5 - length(in.local), 0.0, 1.0); } if (cover < 0.5) { discard; } var o: PickOut; o.ids = vec2<u32>(u32(item.params.x), u32(in.index + 0.5)); o.depth = in.clip.z; return o; } `, SPIRV: spirvPoint, HLSL: `struct Light { float4 dir; float4 color; }; struct Frame { row_major float4x4 proj; Light lights[5]; float4 ambient; float4 viewport; float4 params; float4 planes[6]; float4 section; }; struct Item { row_major float4x4 mv; float4 tint; float4 params; float4 extra; }; struct PickOut { uint2 ids : SV_Target0; float depth : SV_Target1; }; struct PointMaterial { float4 color; float4 params; }; struct VOut { float4 clip_ : SV_Position; float2 local : LOC0; float3 view_pos : LOC1; nointerpolation float index : LOC2; nointerpolation float4 tint : LOC3; }; cbuffer frame : register(b0, space0) { Frame frame; } cbuffer point_ : register(b0, space1) { PointMaterial point_; } cbuffer item : register(b0, space2) { Item item; } struct VertexOutput_vs_main { float2 local : LOC0; float3 view_pos : LOC1; nointerpolation float index_2 : LOC2; nointerpolation float4 tint_1 : LOC3; float4 clip_ : SV_Position; }; struct VertexOutput_vs_color { float2 local_1 : LOC0; float3 view_pos_1 : LOC1; nointerpolation float index_3 : LOC2; nointerpolation float4 tint_2 : LOC3; float4 clip_1 : SV_Position; }; struct VertexOutput_vs_pick { float2 local_2 : LOC0; float3 view_pos_2 : LOC1; nointerpolation float index_4 : LOC2; nointerpolation float4 tint_3 : LOC3; float4 clip_2 : SV_Position; }; struct FragmentInput_fs_main { float2 local_3 : LOC0; float3 view_pos_3 : LOC1; nointerpolation float index_5 : LOC2; nointerpolation float4 tint_4 : LOC3; float4 clip_3 : SV_Position; }; struct FragmentInput_fs_pick { float2 local_4 : LOC0; float3 view_pos_4 : LOC1; nointerpolation float index_6 : LOC2; nointerpolation float4 tint_5 : LOC3; float4 clip_4 : SV_Position; }; int naga_f2i32(float value) { return int(clamp(value, -2147483600.0, 2147483500.0)); } bool section_clipped(float3 p_3, int skip) { int i = int(0); float _e5 = frame.section.x; int count = naga_f2i32(_e5); uint2 loop_bound = uint2(4294967295u, 4294967295u); bool loop_init = true; while(true) { if (all(loop_bound == uint2(0u, 0u))) { break; } loop_bound -= uint2(loop_bound.y == 0u, 1u); if (!loop_init) { int _e28 = i; i = asint(asuint(_e28) + asuint(int(1))); } loop_init = false; int _e9 = i; if ((_e9 < int(6))) { } else { break; } { int _e12 = i; if ((_e12 >= count)) { break; } int _e14 = i; if ((_e14 == skip)) { continue; } int _e18 = i; float4 plane = frame.planes[_e18]; if (((dot(plane.xyz, p_3) + plane.w) > 0.0)) { return true; } } } return false; } VOut vertex_point(float3 p_4, float2 corner_3, float index_1, float4 tint) { VOut o_1 = (VOut)0; o_1.index = index_1; o_1.tint = tint; o_1.clip_ = float4(2.0, 2.0, 2.0, 1.0); float4x4 _e15 = item.mv; float4 v = mul(float4(p_4, 1.0), _e15); float _e24 = frame.params.x; if ((-(v.z) < _e24)) { VOut _e26 = o_1; VOut vout = _e26; return vout; } float4x4 _e29 = frame.proj; float4 k = mul(v, _e29); float _e34 = point_.params.x; float _e40 = frame.params.y; float half_size = (((0.5 * _e34) * _e40) + 1.0); float4 _e47 = frame.viewport; float2 offset = ((corner_3 * half_size) / (0.5 * _e47.xy)); o_1.clip_ = float4((k.xy + (offset * k.w)), k.z, k.w); o_1.local = (corner_3 * half_size); o_1.view_pos = v.xyz; VOut _e64 = o_1; VOut vout_1 = _e64; return vout_1; } VertexOutput_vs_main vs_main(float3 p : LOC0, float2 corner : LOC1) { VOut _e5 = vertex_point(p, corner, 0.0, (1.0).xxxx); VOut vout_2 = _e5; VertexOutput_vs_main vout_2_ = { vout_2.local, vout_2.view_pos, vout_2.index, vout_2.tint, vout_2.clip_ }; return vout_2_; } VertexOutput_vs_color vs_color(float3 p_1 : LOC0, float2 corner_1 : LOC1, float4 color : LOC3) { VOut _e4 = vertex_point(p_1, corner_1, 0.0, color); VOut vout_3 = _e4; VertexOutput_vs_color vout_3_ = { vout_3.local, vout_3.view_pos, vout_3.index, vout_3.tint, vout_3.clip_ }; return vout_3_; } VertexOutput_vs_pick vs_pick(float3 p_2 : LOC0, float2 corner_2 : LOC1, float index : LOC2) { VOut _e5 = vertex_point(p_2, corner_2, index, (1.0).xxxx); VOut vout_4 = _e5; VertexOutput_vs_pick vout_4_ = { vout_4.local, vout_4.view_pos, vout_4.index, vout_4.tint, vout_4.clip_ }; return vout_4_; } float4 fs_main(FragmentInput_fs_main fragmentinput_fs_main) : SV_Target0 { VOut in_ = { fragmentinput_fs_main.clip_3, fragmentinput_fs_main.local_3, fragmentinput_fs_main.view_pos_3, fragmentinput_fs_main.index_5, fragmentinput_fs_main.tint_4 }; float cover = 0.0; const bool _e3 = section_clipped(in_.view_pos, int(-1)); if (_e3) { discard; } float _e7 = point_.params.x; float _e13 = frame.params.y; float half_size_1 = ((0.5 * _e7) * _e13); float _e20 = point_.params.y; if ((_e20 > 0.5)) { cover = (clamp(((half_size_1 + 0.5) - abs(in_.local.x)), 0.0, 1.0) * clamp(((half_size_1 + 0.5) - abs(in_.local.y)), 0.0, 1.0)); } else { cover = clamp(((half_size_1 + 0.5) - length(in_.local)), 0.0, 1.0); } float4 _e52 = point_.color; float4 _e59 = item.tint; float _e64 = item.tint.w; float3 rgb = lerp((_e52.xyz * in_.tint.xyz), _e59.xyz, _e64); float _e69 = point_.color.w; float _e73 = cover; float a = ((_e69 * in_.tint.w) * _e73); return float4((rgb * a), a); } uint naga_f2u32(float value) { return uint(clamp(value, 0.0, 4294967000.0)); } PickOut fs_pick(FragmentInput_fs_pick fragmentinput_fs_pick) { VOut in_1 = { fragmentinput_fs_pick.clip_4, fragmentinput_fs_pick.local_4, fragmentinput_fs_pick.view_pos_4, fragmentinput_fs_pick.index_6, fragmentinput_fs_pick.tint_5 }; float cover_1 = 0.0; PickOut o = (PickOut)0; const bool _e3 = section_clipped(in_1.view_pos, int(-1)); if (_e3) { discard; } float _e7 = point_.params.x; float _e13 = frame.params.y; float half_size_2 = ((0.5 * _e7) * _e13); float _e20 = point_.params.y; if ((_e20 > 0.5)) { cover_1 = (clamp(((half_size_2 + 0.5) - abs(in_1.local.x)), 0.0, 1.0) * clamp(((half_size_2 + 0.5) - abs(in_1.local.y)), 0.0, 1.0)); } else { cover_1 = clamp(((half_size_2 + 0.5) - length(in_1.local)), 0.0, 1.0); } float _e50 = cover_1; if ((_e50 < 0.5)) { discard; } float _e58 = item.params.x; o.ids = uint2(naga_f2u32(_e58), naga_f2u32((in_1.index + 0.5))); o.depth = in_1.clip_.z; PickOut _e68 = o; PickOut pickout = _e68; return pickout; } `, MSL: `// language: metal2.4 #include <metal_stdlib> #include <simd/simd.h> using metal::uint; struct Light { metal::float4 dir; metal::float4 color; }; struct type_3 { Light inner[5]; }; struct type_4 { metal::float4 inner[6]; }; struct Frame { metal::float4x4 proj; type_3 lights; metal::float4 ambient; metal::float4 viewport; metal::float4 params; type_4 planes; metal::float4 section; }; struct Item { metal::float4x4 mv; metal::float4 tint; metal::float4 params; metal::float4 extra; }; struct PickOut { metal::uint2 ids; float depth; char _pad2[4]; }; struct PointMaterial { metal::float4 color; metal::float4 params; }; struct VOut { metal::float4 clip; metal::float2 local; char _pad2[8]; metal::packed_float3 view_pos; float index; metal::float4 tint; }; int naga_f2i32(float value) { return static_cast<int>(metal::clamp(value, -2147483600.0, 2147483500.0)); } bool section_clipped( metal::float3 p_3, int skip, constant Frame& frame ) { int i = 0; float _e5 = frame.section.x; int count = naga_f2i32(_e5); uint2 loop_bound = uint2(4294967295u); bool loop_init = true; while(true) { if (metal::all(loop_bound == uint2(0u))) { break; } loop_bound -= uint2(loop_bound.y == 0u, 1u); if (!loop_init) { int _e28 = i; i = as_type<int>(as_type<uint>(_e28) + as_type<uint>(1)); } loop_init = false; int _e9 = i; if (_e9 < 6) { } else { break; } { int _e12 = i; if (_e12 >= count) { break; } int _e14 = i; if (_e14 == skip) { continue; } int _e18 = i; metal::float4 plane = frame.planes.inner[_e18]; if ((metal::dot(plane.xyz, p_3) + plane.w) > 0.0) { return true; } } } return false; } VOut vertex_point( metal::float3 p_4, metal::float2 corner_3, float index_1, metal::float4 tint, constant Frame& frame, constant PointMaterial& point, constant Item& item ) { VOut o_1 = {}; o_1.index = index_1; o_1.tint = tint; o_1.clip = metal::float4(2.0, 2.0, 2.0, 1.0); metal::float4x4 _e15 = item.mv; metal::float4 v = _e15 * metal::float4(p_4, 1.0); float _e24 = frame.params.x; if (-(v.z) < _e24) { VOut _e26 = o_1; return _e26; } metal::float4x4 _e29 = frame.proj; metal::float4 k = _e29 * v; float _e34 = point.params.x; float _e40 = frame.params.y; float half_size = ((0.5 * _e34) * _e40) + 1.0; metal::float4 _e47 = frame.viewport; metal::float2 offset = (corner_3 * half_size) / (0.5 * _e47.xy); o_1.clip = metal::float4(k.xy + (offset * k.w), k.z, k.w); o_1.local = corner_3 * half_size; o_1.view_pos = v.xyz; VOut _e64 = o_1; return _e64; } struct vs_mainInput { metal::float3 p [[attribute(0)]]; metal::float2 corner [[attribute(1)]]; }; struct vs_mainOutput { metal::float4 clip [[position]]; metal::float2 local [[user(loc0), center_perspective]]; metal::float3 view_pos [[user(loc1), center_perspective]]; float index [[user(loc2), flat]]; metal::float4 tint [[user(loc3), flat]]; }; vertex vs_mainOutput vs_main( vs_mainInput varyings [[stage_in]] , constant Frame& frame [[buffer(0)]] , constant PointMaterial& point [[buffer(1)]] , constant Item& item [[buffer(2)]] ) { const auto p = varyings.p; const auto corner = varyings.corner; VOut _e5 = vertex_point(p, corner, 0.0, metal::float4(1.0), frame, point, item); const auto _tmp = _e5; return vs_mainOutput { _tmp.clip, _tmp.local, _tmp.view_pos, _tmp.index, _tmp.tint }; } struct vs_colorInput { metal::float3 p_1 [[attribute(0)]]; metal::float2 corner_1 [[attribute(1)]]; metal::float4 color [[attribute(3)]]; }; struct vs_colorOutput { metal::float4 clip [[position]]; metal::float2 local [[user(loc0), center_perspective]]; metal::float3 view_pos [[user(loc1), center_perspective]]; float index [[user(loc2), flat]]; metal::float4 tint [[user(loc3), flat]]; }; vertex vs_colorOutput vs_color( vs_colorInput varyings_1 [[stage_in]] , constant Frame& frame [[buffer(0)]] , constant PointMaterial& point [[buffer(1)]] , constant Item& item [[buffer(2)]] ) { const auto p_1 = varyings_1.p_1; const auto corner_1 = varyings_1.corner_1; const auto color = varyings_1.color; VOut _e4 = vertex_point(p_1, corner_1, 0.0, color, frame, point, item); const auto _tmp_1 = _e4; return vs_colorOutput { _tmp_1.clip, _tmp_1.local, _tmp_1.view_pos, _tmp_1.index, _tmp_1.tint }; } struct vs_pickInput { metal::float3 p_2 [[attribute(0)]]; metal::float2 corner_2 [[attribute(1)]]; float index [[attribute(2)]]; }; struct vs_pickOutput { metal::float4 clip [[position]]; metal::float2 local [[user(loc0), center_perspective]]; metal::float3 view_pos [[user(loc1), center_perspective]]; float index [[user(loc2), flat]]; metal::float4 tint [[user(loc3), flat]]; }; vertex vs_pickOutput vs_pick( vs_pickInput varyings_2 [[stage_in]] , constant Frame& frame [[buffer(0)]] , constant PointMaterial& point [[buffer(1)]] , constant Item& item [[buffer(2)]] ) { const auto p_2 = varyings_2.p_2; const auto corner_2 = varyings_2.corner_2; const auto index = varyings_2.index; VOut _e5 = vertex_point(p_2, corner_2, index, metal::float4(1.0), frame, point, item); const auto _tmp_2 = _e5; return vs_pickOutput { _tmp_2.clip, _tmp_2.local, _tmp_2.view_pos, _tmp_2.index, _tmp_2.tint }; } struct fs_mainInput { metal::float2 local [[user(loc0), center_perspective]]; metal::float3 view_pos [[user(loc1), center_perspective]]; float index_2 [[user(loc2), flat]]; metal::float4 tint_1 [[user(loc3), flat]]; }; struct fs_mainOutput { metal::float4 member_3 [[color(0)]]; }; fragment fs_mainOutput fs_main( fs_mainInput varyings_3 [[stage_in]] , metal::float4 clip [[position]] , constant Frame& frame [[buffer(0)]] , constant PointMaterial& point [[buffer(1)]] , constant Item& item [[buffer(2)]] ) { const VOut in = { clip, varyings_3.local, {}, varyings_3.view_pos, varyings_3.index_2, varyings_3.tint_1 }; float cover = 0.0; bool _e3 = section_clipped(in.view_pos, -1, frame); if (_e3) { metal::discard_fragment(); } float _e7 = point.params.x; float _e13 = frame.params.y; float half_size_1 = (0.5 * _e7) * _e13; float _e20 = point.params.y; if (_e20 > 0.5) { cover = metal::clamp((half_size_1 + 0.5) - metal::abs(in.local.x), 0.0, 1.0) * metal::clamp((half_size_1 + 0.5) - metal::abs(in.local.y), 0.0, 1.0); } else { cover = metal::clamp((half_size_1 + 0.5) - metal::length(in.local), 0.0, 1.0); } metal::float4 _e52 = point.color; metal::float4 _e59 = item.tint; float _e64 = item.tint.w; metal::float3 rgb = metal::mix(_e52.xyz * in.tint.xyz, _e59.xyz, _e64); float _e69 = point.color.w; float _e73 = cover; float a = (_e69 * in.tint.w) * _e73; return fs_mainOutput { metal::float4(rgb * a, a) }; } uint naga_f2u32(float value) { return static_cast<uint>(metal::clamp(value, 0.0, 4294967000.0)); } struct fs_pickInput { metal::float2 local [[user(loc0), center_perspective]]; metal::float3 view_pos [[user(loc1), center_perspective]]; float index_2 [[user(loc2), flat]]; metal::float4 tint_1 [[user(loc3), flat]]; }; struct fs_pickOutput { metal::uint2 ids [[color(0)]]; float depth [[color(1)]]; }; fragment fs_pickOutput fs_pick( fs_pickInput varyings_4 [[stage_in]] , metal::float4 clip_1 [[position]] , constant Frame& frame [[buffer(0)]] , constant PointMaterial& point [[buffer(1)]] , constant Item& item [[buffer(2)]] ) { const VOut in_1 = { clip_1, varyings_4.local, {}, varyings_4.view_pos, varyings_4.index_2, varyings_4.tint_1 }; float cover_1 = 0.0; PickOut o = {}; bool _e3 = section_clipped(in_1.view_pos, -1, frame); if (_e3) { metal::discard_fragment(); } float _e7 = point.params.x; float _e13 = frame.params.y; float half_size_2 = (0.5 * _e7) * _e13; float _e20 = point.params.y; if (_e20 > 0.5) { cover_1 = metal::clamp((half_size_2 + 0.5) - metal::abs(in_1.local.x), 0.0, 1.0) * metal::clamp((half_size_2 + 0.5) - metal::abs(in_1.local.y), 0.0, 1.0); } else { cover_1 = metal::clamp((half_size_2 + 0.5) - metal::length(in_1.local), 0.0, 1.0); } float _e50 = cover_1; if (_e50 < 0.5) { metal::discard_fragment(); } float _e58 = item.params.x; o.ids = metal::uint2(naga_f2u32(_e58), naga_f2u32(in_1.index + 0.5)); o.depth = in_1.clip.z; PickOut _e68 = o; const auto _tmp_3 = _e68; return fs_pickOutput { _tmp_3.ids, _tmp_3.depth }; } `, Entries: []gpu.EntryPoint{ {Name: "vs_main", Stage: gpu.ShaderStageVertex, Inputs: []gpu.VertexInput{ {Name: "p", Location: 0, Type: gpu.ShaderTypeFloat32x3}, {Name: "corner", Location: 1, Type: gpu.ShaderTypeFloat32x2}, }, Bindings: []gpu.ShaderBinding{ {Name: "frame", Group: 0, Binding: 0, Kind: gpu.BindingUniformBuffer, Size: 384, Register: gpu.ShaderRegister{Class: "b", Space: 0, Index: 0}, MetalIndex: 0}, {Name: "point", Group: 1, Binding: 0, Kind: gpu.BindingUniformBuffer, Size: 32, Register: gpu.ShaderRegister{Class: "b", Space: 1, Index: 0}, MetalIndex: 1}, {Name: "item", Group: 2, Binding: 0, Kind: gpu.BindingUniformBuffer, Size: 112, Register: gpu.ShaderRegister{Class: "b", Space: 2, Index: 0}, MetalIndex: 2}, }, }, {Name: "vs_color", Stage: gpu.ShaderStageVertex, Inputs: []gpu.VertexInput{ {Name: "p", Location: 0, Type: gpu.ShaderTypeFloat32x3}, {Name: "corner", Location: 1, Type: gpu.ShaderTypeFloat32x2}, {Name: "color", Location: 3, Type: gpu.ShaderTypeFloat32x4}, }, Bindings: []gpu.ShaderBinding{ {Name: "frame", Group: 0, Binding: 0, Kind: gpu.BindingUniformBuffer, Size: 384, Register: gpu.ShaderRegister{Class: "b", Space: 0, Index: 0}, MetalIndex: 0}, {Name: "point", Group: 1, Binding: 0, Kind: gpu.BindingUniformBuffer, Size: 32, Register: gpu.ShaderRegister{Class: "b", Space: 1, Index: 0}, MetalIndex: 1}, {Name: "item", Group: 2, Binding: 0, Kind: gpu.BindingUniformBuffer, Size: 112, Register: gpu.ShaderRegister{Class: "b", Space: 2, Index: 0}, MetalIndex: 2}, }, }, {Name: "vs_pick", Stage: gpu.ShaderStageVertex, Inputs: []gpu.VertexInput{ {Name: "p", Location: 0, Type: gpu.ShaderTypeFloat32x3}, {Name: "corner", Location: 1, Type: gpu.ShaderTypeFloat32x2}, {Name: "index", Location: 2, Type: gpu.ShaderTypeFloat32}, }, Bindings: []gpu.ShaderBinding{ {Name: "frame", Group: 0, Binding: 0, Kind: gpu.BindingUniformBuffer, Size: 384, Register: gpu.ShaderRegister{Class: "b", Space: 0, Index: 0}, MetalIndex: 0}, {Name: "point", Group: 1, Binding: 0, Kind: gpu.BindingUniformBuffer, Size: 32, Register: gpu.ShaderRegister{Class: "b", Space: 1, Index: 0}, MetalIndex: 1}, {Name: "item", Group: 2, Binding: 0, Kind: gpu.BindingUniformBuffer, Size: 112, Register: gpu.ShaderRegister{Class: "b", Space: 2, Index: 0}, MetalIndex: 2}, }, }, {Name: "fs_main", Stage: gpu.ShaderStageFragment, Bindings: []gpu.ShaderBinding{ {Name: "frame", Group: 0, Binding: 0, Kind: gpu.BindingUniformBuffer, Size: 384, Register: gpu.ShaderRegister{Class: "b", Space: 0, Index: 0}, MetalIndex: 0}, {Name: "point", Group: 1, Binding: 0, Kind: gpu.BindingUniformBuffer, Size: 32, Register: gpu.ShaderRegister{Class: "b", Space: 1, Index: 0}, MetalIndex: 1}, {Name: "item", Group: 2, Binding: 0, Kind: gpu.BindingUniformBuffer, Size: 112, Register: gpu.ShaderRegister{Class: "b", Space: 2, Index: 0}, MetalIndex: 2}, }, }, {Name: "fs_pick", Stage: gpu.ShaderStageFragment, Bindings: []gpu.ShaderBinding{ {Name: "frame", Group: 0, Binding: 0, Kind: gpu.BindingUniformBuffer, Size: 384, Register: gpu.ShaderRegister{Class: "b", Space: 0, Index: 0}, MetalIndex: 0}, {Name: "point", Group: 1, Binding: 0, Kind: gpu.BindingUniformBuffer, Size: 32, Register: gpu.ShaderRegister{Class: "b", Space: 1, Index: 0}, MetalIndex: 1}, {Name: "item", Group: 2, Binding: 0, Kind: gpu.BindingUniformBuffer, Size: 112, Register: gpu.ShaderRegister{Class: "b", Space: 2, Index: 0}, MetalIndex: 2}, }, }, }, }
Point is point.wgsl.
var Section = &gpu.Shader{ Name: "section", WGSL: `// The sections of viewport3d (SPEC §27.8): the cap that closes a solid mesh where a section plane cuts it, and the outline of the // cut. A cap is drawn in three steps with the stencil, for each plane and each closed item: vs_count and fs_count draw the part // of the item that the plane leaves, back faces adding one to the stencil and front faces taking one away, so that a pixel // whose line of sight ends inside the solid, beyond the plane, is not zero; vs_full and fs_cap then draw the plane itself // over the whole view, where the stencil is not zero, with the depth that the plane has at each pixel; and vs_full with // fs_clear sets the stencil to zero again. The outline is a quad for each triangle that the plane crosses, made by // vs_outline from the triangle's three corners. // // The record of the item has: tint the color of the cap, extra the color of the outline, and params the index of the plane in // x, the width of the outline in logical pixels in y and the pull of the outline towards the camera in z. // What the shaders of viewport3d share (SPEC §27.8): the frame's uniform data, the record of an item, and the section planes. struct Light { // dir points to the light, in view space. dir: vec4<f32>, // color is the light's color times its intensity. color: vec4<f32>, } struct Frame { proj: mat4x4<f32>, // lights are the light at the camera, then up to four directional ones; an unused one has a black color. lights: array<Light, 5>, ambient: vec4<f32>, // viewport is the width and the height of the target in pixels, and their inverses. viewport: vec4<f32>, // params are the near plane's distance, the device pixel ratio and two unused numbers. params: vec4<f32>, // planes are the section planes in view space, relative to the eye (D30): a point p with dot(plane.xyz, p) + plane.w > 0 is // cut away. planes: array<vec4<f32>, 6>, // section is the number of planes in x. section: vec4<f32>, } @group(0) @binding(0) var<uniform> frame: Frame; // Item is the record of a line, a point or a picked item: the matrix that takes the mesh's positions to the view, relative to // the eye, a tint (the highlight of a selected item: its color, and the part of it that replaces the material's in a), and // numbers for the shader that reads it. struct Item { mv: mat4x4<f32>, tint: vec4<f32>, params: vec4<f32>, extra: vec4<f32>, } // section_clipped says whether a point of the view space is cut away by a section plane. skip is the plane to leave out (the // plane that a cap lies on), or -1. fn section_clipped(p: vec3<f32>, skip: i32) -> bool { let count = i32(frame.section.x); for (var i = 0; i < 6; i = i + 1) { if (i >= count) { break; } if (i == skip) { continue; } let plane = frame.planes[i]; if (dot(plane.xyz, p) + plane.w > 0.0) { return true; } } return false; } // PickOut is what a pick pass writes for a pixel: the number of the item (from 1, 0 for nothing) and of the primitive in it, and // the depth. struct PickOut { @location(0) ids: vec2<u32>, @location(1) depth: f32, } // The quad of a line segment (SPEC §27.8): the vertex shaders of the lines, of the picking of lines and of the outline of a // section expand a segment into a quad in the screen with it, so that the width is in logical pixels whatever the distance. // It needs common.wgsl.inc. struct SegmentQuad { clip: vec4<f32>, // view_pos is the point of the segment that the corner is at, in view space. view_pos: vec3<f32>, // across is the distance in pixels from the center of the line, along: the distance from its first end, along it. across: f32, along: f32, length_px: f32, // ta and tb are the part of the segment in front of the near plane, from 0 to 1. ta: f32, tb: f32, visible: bool, } // segment_quad expands the segment from a to b (view space) at a corner of the quad: corner.x is 0 at the first end and 1 at the // second, corner.y is -1 and 1 for the two sides. width is in logical pixels; pull moves the depth towards the camera by a // part of itself (so that an edge is not hidden by the face it lies on). fn segment_quad(a: vec3<f32>, b: vec3<f32>, corner: vec2<f32>, width: f32, pull: f32) -> SegmentQuad { var o: SegmentQuad; // A segment that is wholly behind the near plane is put outside the clip volume. o.clip = vec4<f32>(2.0, 2.0, 2.0, 1.0); o.visible = false; let near = frame.params.x; let za = -a.z; let zb = -b.z; if (za < near && zb < near) { return o; } // The part of the segment in front of the near plane. var ta = 0.0; var tb = 1.0; let cut = (near - za) / (zb - za); if (za < near) { ta = cut; } if (zb < near) { tb = cut; } let ca = mix(a, b, ta); let cb = mix(a, b, tb); let ka = frame.proj * vec4<f32>(ca, 1.0); let kb = frame.proj * vec4<f32>(cb, 1.0); let half_vp = 0.5 * frame.viewport.xy; let sa = ka.xy / ka.w * half_vp; let sb = kb.xy / kb.w * half_vp; var dir = sb - sa; let span = length(dir); if (span > 0.0001) { dir = dir / span; } else { dir = vec2<f32>(1.0, 0.0); } let nrm = vec2<f32>(-dir.y, dir.x); let half_w = 0.5 * width * frame.params.y; let pad = half_w + 1.0; let at_end = corner.x; let sgn = at_end * 2.0 - 1.0; let offset = dir * (sgn * pad) + nrm * (corner.y * pad); let k = mix(ka, kb, at_end); o.clip = vec4<f32>(k.xy + offset / half_vp * k.w, k.z * (1.0 + pull), k.w); o.view_pos = mix(ca, cb, at_end); o.across = corner.y * pad; o.along = at_end * span + sgn * pad; o.length_px = span; o.ta = ta; o.tb = tb; o.visible = true; return o; } // segment_cover is how much of a pixel of a quad of a segment the line covers, from 0 to 1, with anti-aliased sides and ends. fn segment_cover(width: f32, across: f32, along: f32, length_px: f32) -> f32 { let half_w = 0.5 * width * frame.params.y; var cover = clamp(half_w + 0.5 - abs(across), 0.0, 1.0); cover = cover * clamp(along + half_w + 0.5, 0.0, 1.0) * clamp(length_px + half_w + 0.5 - along, 0.0, 1.0); return cover; } @group(1) @binding(0) var<uniform> item: Item; struct Counted { @builtin(position) clip: vec4<f32>, @location(0) view_pos: vec3<f32>, } @vertex fn vs_count(@location(0) p: vec3<f32>) -> Counted { var o: Counted; let v = item.mv * vec4<f32>(p, 1.0); o.view_pos = v.xyz; o.clip = frame.proj * v; return o; } @fragment fn fs_count(in: Counted) -> @location(0) vec4<f32> { // Only what the plane leaves counts. let plane = frame.planes[i32(item.params.x)]; if (dot(plane.xyz, in.view_pos) + plane.w > 0.0) { discard; } return vec4<f32>(0.0); } struct Full { @builtin(position) clip: vec4<f32>, @location(0) ndc: vec2<f32>, } @vertex fn vs_full(@builtin(vertex_index) i: u32) -> Full { // One triangle that covers the view: (-1, -1), (3, -1) and (-1, 3). let x = f32((i << 1u) & 2u); let y = f32(i & 2u); var o: Full; o.ndc = vec2<f32>(x * 2.0 - 1.0, y * 2.0 - 1.0); o.clip = vec4<f32>(o.ndc, 0.0, 1.0); return o; } struct Capped { @location(0) color: vec4<f32>, @builtin(frag_depth) depth: f32, } @fragment fn fs_cap(in: Full) -> Capped { var o: Capped; o.color = vec4<f32>(0.0); o.depth = 0.0; let plane = frame.planes[i32(item.params.x)]; // The line of sight of the pixel: from the eye, or, in an orthographic view, parallel to the axis of the view. var origin = vec3<f32>(0.0); var dir = vec3<f32>(in.ndc.x / frame.proj[0][0], in.ndc.y / frame.proj[1][1], -1.0); if (frame.proj[3][3] > 0.5) { origin = vec3<f32>(dir.xy, 0.0); dir = vec3<f32>(0.0, 0.0, -1.0); } let facing = dot(plane.xyz, dir); if (abs(facing) < 1.0e-9) { discard; } let t = -(plane.w + dot(plane.xyz, origin)) / facing; let p = origin + dir * t; if (t <= 0.0 || section_clipped(p, i32(item.params.x))) { discard; } let k = frame.proj * vec4<f32>(p, 1.0); o.depth = k.z / k.w; o.color = vec4<f32>(item.tint.rgb, 1.0); return o; } @fragment fn fs_clear(in: Full) -> @location(0) vec4<f32> { return vec4<f32>(0.0); } struct Outlined { @builtin(position) clip: vec4<f32>, @location(0) across: f32, @location(1) along: f32, @location(2) length_px: f32, @location(3) view_pos: vec3<f32>, } @vertex fn vs_outline(@location(0) a: vec3<f32>, @location(1) b: vec3<f32>, @location(2) c: vec3<f32>, @location(3) corner: vec2<f32>) -> Outlined { var o: Outlined; o.clip = vec4<f32>(2.0, 2.0, 2.0, 1.0); let plane = frame.planes[i32(item.params.x)]; let va = (item.mv * vec4<f32>(a, 1.0)).xyz; let vb = (item.mv * vec4<f32>(b, 1.0)).xyz; let vc = (item.mv * vec4<f32>(c, 1.0)).xyz; let da = dot(plane.xyz, va) + plane.w; let db = dot(plane.xyz, vb) + plane.w; let dc = dot(plane.xyz, vc) + plane.w; let sa = da > 0.0; let sb = db > 0.0; let sc = dc > 0.0; // The points where the edges that cross the plane cross it. var p0 = vec3<f32>(0.0); var p1 = vec3<f32>(0.0); var n = 0; if (sa != sb) { p0 = mix(va, vb, da / (da - db)); n = 1; } if (sb != sc) { let p = mix(vb, vc, db / (db - dc)); if (n == 0) { p0 = p; } else { p1 = p; } n = n + 1; } if (sc != sa) { let p = mix(vc, va, dc / (dc - da)); if (n == 0) { p0 = p; } else { p1 = p; } n = n + 1; } if (n != 2) { return o; } let q = segment_quad(p0, p1, corner, item.params.y, item.params.z); o.clip = q.clip; o.across = q.across; o.along = q.along; o.length_px = q.length_px; o.view_pos = q.view_pos; return o; } @fragment fn fs_outline(in: Outlined) -> @location(0) vec4<f32> { if (section_clipped(in.view_pos, i32(item.params.x))) { discard; } let cover = segment_cover(item.params.y, in.across, in.along, in.length_px); let a = item.extra.a * cover; return vec4<f32>(item.extra.rgb * a, a); } `, SPIRV: spirvSection, HLSL: `struct Light { float4 dir; float4 color; }; struct Frame { row_major float4x4 proj; Light lights[5]; float4 ambient; float4 viewport; float4 params; float4 planes[6]; float4 section; }; struct Item { row_major float4x4 mv; float4 tint; float4 params; float4 extra; }; struct PickOut { uint2 ids : LOC0; float depth : LOC1; }; struct SegmentQuad { float4 clip_; float3 view_pos; float across; float along; float length_px; float ta; float tb; bool visible; int _end_pad_0; int _end_pad_1; int _end_pad_2; }; struct Counted { float4 clip_ : SV_Position; float3 view_pos : LOC0; }; struct Full { float4 clip_ : SV_Position; float2 ndc : LOC0; }; struct Capped { float4 color : SV_Target0; float depth : SV_Depth; }; struct Outlined { float4 clip_ : SV_Position; float across : LOC0; float along : LOC1; float length_px : LOC2; float3 view_pos : LOC3; }; cbuffer frame : register(b0, space0) { Frame frame; } cbuffer item : register(b0, space1) { Item item; } struct VertexOutput_vs_count { float3 view_pos : LOC0; float4 clip_ : SV_Position; }; struct FragmentInput_fs_count { float3 view_pos_1 : LOC0; float4 clip_1 : SV_Position; }; struct VertexOutput_vs_full { float2 ndc : LOC0; float4 clip_2 : SV_Position; }; struct FragmentInput_fs_cap { float2 ndc_1 : LOC0; float4 clip_3 : SV_Position; }; struct FragmentInput_fs_clear { float2 ndc_2 : LOC0; float4 clip_4 : SV_Position; }; struct VertexOutput_vs_outline { float across_1 : LOC0; float along_1 : LOC1; float length_px_1 : LOC2; float3 view_pos_2 : LOC3; float4 clip_5 : SV_Position; }; struct FragmentInput_fs_outline { float across_2 : LOC0; float along_2 : LOC1; float length_px_2 : LOC2; float3 view_pos_3 : LOC3; float4 clip_6 : SV_Position; }; int naga_f2i32(float value) { return int(clamp(value, -2147483600.0, 2147483500.0)); } bool section_clipped(float3 p_1, int skip) { int i_1 = int(0); float _e5 = frame.section.x; int count = naga_f2i32(_e5); uint2 loop_bound = uint2(4294967295u, 4294967295u); bool loop_init = true; while(true) { if (all(loop_bound == uint2(0u, 0u))) { break; } loop_bound -= uint2(loop_bound.y == 0u, 1u); if (!loop_init) { int _e28 = i_1; i_1 = asint(asuint(_e28) + asuint(int(1))); } loop_init = false; int _e9 = i_1; if ((_e9 < int(6))) { } else { break; } { int _e12 = i_1; if ((_e12 >= count)) { break; } int _e14 = i_1; if ((_e14 == skip)) { continue; } int _e18 = i_1; float4 plane = frame.planes[_e18]; if (((dot(plane.xyz, p_1) + plane.w) > 0.0)) { return true; } } } return false; } SegmentQuad segment_quad(float3 a_1, float3 b_1, float2 corner_1, float width, float pull) { SegmentQuad o_4 = (SegmentQuad)0; bool local_1 = (bool)0; float ta = 0.0; float tb = 1.0; float2 dir_1 = (float2)0; o_4.clip_ = float4(2.0, 2.0, 2.0, 1.0); o_4.visible = false; float near = frame.params.x; float za = -(a_1.z); float zb = -(b_1.z); if ((za < near)) { local_1 = (zb < near); } else { local_1 = false; } bool _e27 = local_1; if (_e27) { SegmentQuad _e28 = o_4; SegmentQuad segmentquad = _e28; return segmentquad; } float cut = ((near - za) / (zb - za)); if ((za < near)) { ta = cut; } if ((zb < near)) { tb = cut; } float _e38 = ta; float3 ca = lerp(a_1, b_1, _e38); float _e40 = tb; float3 cb = lerp(a_1, b_1, _e40); float4x4 _e44 = frame.proj; float4 ka = mul(float4(ca, 1.0), _e44); float4x4 _e50 = frame.proj; float4 kb = mul(float4(cb, 1.0), _e50); float4 _e56 = frame.viewport; float2 half_vp = (0.5 * _e56.xy); float2 sa = ((ka.xy / (ka.w).xx) * half_vp); float2 sb = ((kb.xy / (kb.w).xx) * half_vp); dir_1 = (sb - sa); float2 _e72 = dir_1; float span = length(_e72); if ((span > 0.0001)) { float2 _e76 = dir_1; dir_1 = (_e76 / (span).xx); } else { dir_1 = float2(1.0, 0.0); } float _e83 = dir_1.y; float _e86 = dir_1.x; float2 nrm = float2(-(_e83), _e86); float _e93 = frame.params.y; float half_w = ((0.5 * width) * _e93); float pad = (half_w + 1.0); float at_end = corner_1.x; float sgn = ((at_end * 2.0) - 1.0); float2 _e102 = dir_1; float2 offset = ((_e102 * (sgn * pad)) + (nrm * (corner_1.y * pad))); float4 k = lerp(ka, kb, at_end); o_4.clip_ = float4((k.xy + ((offset / half_vp) * k.w)), (k.z * (1.0 + pull)), k.w); o_4.view_pos = lerp(ca, cb, at_end); o_4.across = (corner_1.y * pad); o_4.along = ((at_end * span) + (sgn * pad)); o_4.length_px = span; float _e133 = ta; o_4.ta = _e133; float _e135 = tb; o_4.tb = _e135; o_4.visible = true; SegmentQuad _e138 = o_4; SegmentQuad segmentquad_1 = _e138; return segmentquad_1; } float segment_cover(float width_1, float across, float along, float length_px) { float cover = (float)0; float _e9 = frame.params.y; float half_w_1 = ((0.5 * width_1) * _e9); cover = clamp(((half_w_1 + 0.5) - abs(across)), 0.0, 1.0); float _e19 = cover; cover = ((_e19 * clamp(((along + half_w_1) + 0.5), 0.0, 1.0)) * clamp((((length_px + half_w_1) + 0.5) - along), 0.0, 1.0)); float _e35 = cover; return _e35; } VertexOutput_vs_count vs_count(float3 p : LOC0) { Counted o = (Counted)0; float4x4 _e4 = item.mv; float4 v = mul(float4(p, 1.0), _e4); o.view_pos = v.xyz; float4x4 _e13 = frame.proj; o.clip_ = mul(v, _e13); Counted _e15 = o; Counted counted = _e15; VertexOutput_vs_count counted_1 = { counted.view_pos, counted.clip_ }; return counted_1; } float4 fs_count(FragmentInput_fs_count fragmentinput_fs_count) : SV_Target0 { Counted in_ = { fragmentinput_fs_count.clip_1, fragmentinput_fs_count.view_pos_1 }; float _e6 = item.params.x; float4 plane_1 = frame.planes[naga_f2i32(_e6)]; if (((dot(plane_1.xyz, in_.view_pos) + plane_1.w) > 0.0)) { discard; } return (0.0).xxxx; } VertexOutput_vs_full vs_full(uint i : SV_VertexID) { Full o_1 = (Full)0; float x = float(((i << 1u) & 2u)); float y = float((i & 2u)); o_1.ndc = float2(((x * 2.0) - 1.0), ((y * 2.0) - 1.0)); float2 _e22 = o_1.ndc; o_1.clip_ = float4(_e22, 0.0, 1.0); Full _e26 = o_1; Full full = _e26; VertexOutput_vs_full full_1 = { full.ndc, full.clip_ }; return full_1; } Capped fs_cap(FragmentInput_fs_cap fragmentinput_fs_cap) { Full in_1 = { fragmentinput_fs_cap.clip_3, fragmentinput_fs_cap.ndc_1 }; Capped o_2 = (Capped)0; float3 origin = (0.0).xxx; float3 dir = (float3)0; bool local = (bool)0; o_2.color = (0.0).xxxx; o_2.depth = 0.0; float _e12 = item.params.x; float4 plane_2 = frame.planes[naga_f2i32(_e12)]; float _e25 = frame.proj[0].x; float _e33 = frame.proj[1].y; dir = float3((in_1.ndc.x / _e25), (in_1.ndc.y / _e33), -1.0); float _e42 = frame.proj[3].w; if ((_e42 > 0.5)) { float3 _e45 = dir; origin = float3(_e45.xy, 0.0); dir = float3(0.0, 0.0, -1.0); } float3 _e54 = dir; float facing = dot(plane_2.xyz, _e54); if ((abs(facing) < 1e-9)) { discard; } float3 _e61 = origin; float t = (-((plane_2.w + dot(plane_2.xyz, _e61))) / facing); float3 _e66 = origin; float3 _e67 = dir; float3 p_2 = (_e66 + (_e67 * t)); if (!((t <= 0.0))) { float _e78 = item.params.x; const bool _e80 = section_clipped(p_2, naga_f2i32(_e78)); local = _e80; } else { local = true; } bool _e82 = local; if (_e82) { discard; } float4x4 _e85 = frame.proj; float4 k_1 = mul(float4(p_2, 1.0), _e85); o_2.depth = (k_1.z / k_1.w); float4 _e96 = item.tint; o_2.color = float4(_e96.xyz, 1.0); Capped _e100 = o_2; Capped capped = _e100; return capped; } float4 fs_clear(FragmentInput_fs_clear fragmentinput_fs_clear) : SV_Target0 { Full in_2 = { fragmentinput_fs_clear.clip_4, fragmentinput_fs_clear.ndc_2 }; return (0.0).xxxx; } VertexOutput_vs_outline vs_outline(float3 a : LOC0, float3 b : LOC1, float3 c : LOC2, float2 corner : LOC3) { Outlined o_3 = (Outlined)0; float3 p0_ = (0.0).xxx; float3 p1_ = (0.0).xxx; int n = int(0); o_3.clip_ = float4(2.0, 2.0, 2.0, 1.0); float _e16 = item.params.x; float4 plane_3 = frame.planes[naga_f2i32(_e16)]; float4x4 _e22 = item.mv; float3 va = mul(float4(a, 1.0), _e22).xyz; float4x4 _e29 = item.mv; float3 vb = mul(float4(b, 1.0), _e29).xyz; float4x4 _e36 = item.mv; float3 vc = mul(float4(c, 1.0), _e36).xyz; float da = (dot(plane_3.xyz, va) + plane_3.w); float db = (dot(plane_3.xyz, vb) + plane_3.w); float dc = (dot(plane_3.xyz, vc) + plane_3.w); bool sa_1 = (da > 0.0); bool sb_1 = (db > 0.0); bool sc = (dc > 0.0); if ((sa_1 != sb_1)) { p0_ = lerp(va, vb, (da / (da - db))); n = int(1); } if ((sb_1 != sc)) { float3 p_3 = lerp(vb, vc, (db / (db - dc))); int _e76 = n; if ((_e76 == int(0))) { p0_ = p_3; } else { p1_ = p_3; } int _e79 = n; n = asint(asuint(_e79) + asuint(int(1))); } if ((sc != sa_1)) { float3 p_4 = lerp(vc, va, (dc / (dc - da))); int _e86 = n; if ((_e86 == int(0))) { p0_ = p_4; } else { p1_ = p_4; } int _e89 = n; n = asint(asuint(_e89) + asuint(int(1))); } int _e92 = n; if ((_e92 != int(2))) { Outlined _e95 = o_3; Outlined outlined = _e95; VertexOutput_vs_outline outlined_1 = { outlined.across, outlined.along, outlined.length_px, outlined.view_pos, outlined.clip_ }; return outlined_1; } float3 _e96 = p0_; float3 _e97 = p1_; float _e101 = item.params.y; float _e105 = item.params.z; SegmentQuad _e106 = segment_quad(_e96, _e97, corner, _e101, _e105); o_3.clip_ = _e106.clip_; o_3.across = _e106.across; o_3.along = _e106.along; o_3.length_px = _e106.length_px; o_3.view_pos = _e106.view_pos; Outlined _e117 = o_3; Outlined outlined_2 = _e117; VertexOutput_vs_outline outlined_2_ = { outlined_2.across, outlined_2.along, outlined_2.length_px, outlined_2.view_pos, outlined_2.clip_ }; return outlined_2_; } float4 fs_outline(FragmentInput_fs_outline fragmentinput_fs_outline) : SV_Target0 { Outlined in_3 = { fragmentinput_fs_outline.clip_6, fragmentinput_fs_outline.across_2, fragmentinput_fs_outline.along_2, fragmentinput_fs_outline.length_px_2, fragmentinput_fs_outline.view_pos_3 }; float _e5 = item.params.x; const bool _e7 = section_clipped(in_3.view_pos, naga_f2i32(_e5)); if (_e7) { discard; } float _e11 = item.params.y; const float _e15 = segment_cover(_e11, in_3.across, in_3.along, in_3.length_px); float _e19 = item.extra.w; float a_2 = (_e19 * _e15); float4 _e23 = item.extra; return float4((_e23.xyz * a_2), a_2); } `, MSL: `// language: metal2.4 #include <metal_stdlib> #include <simd/simd.h> using metal::uint; struct Light { metal::float4 dir; metal::float4 color; }; struct type_3 { Light inner[5]; }; struct type_4 { metal::float4 inner[6]; }; struct Frame { metal::float4x4 proj; type_3 lights; metal::float4 ambient; metal::float4 viewport; metal::float4 params; type_4 planes; metal::float4 section; }; struct Item { metal::float4x4 mv; metal::float4 tint; metal::float4 params; metal::float4 extra; }; struct PickOut { metal::uint2 ids; float depth; char _pad2[4]; }; struct SegmentQuad { metal::float4 clip; metal::packed_float3 view_pos; float across; float along; float length_px; float ta; float tb; bool visible; char _pad8[15]; }; struct Counted { metal::float4 clip; metal::float3 view_pos; }; struct Full { metal::float4 clip; metal::float2 ndc; char _pad2[8]; }; struct Capped { metal::float4 color; float depth; char _pad2[12]; }; struct Outlined { metal::float4 clip; float across; float along; float length_px; char _pad4[4]; metal::float3 view_pos; }; int naga_f2i32(float value) { return static_cast<int>(metal::clamp(value, -2147483600.0, 2147483500.0)); } bool section_clipped( metal::float3 p_1, int skip, constant Frame& frame ) { int i_1 = 0; float _e5 = frame.section.x; int count = naga_f2i32(_e5); uint2 loop_bound = uint2(4294967295u); bool loop_init = true; while(true) { if (metal::all(loop_bound == uint2(0u))) { break; } loop_bound -= uint2(loop_bound.y == 0u, 1u); if (!loop_init) { int _e28 = i_1; i_1 = as_type<int>(as_type<uint>(_e28) + as_type<uint>(1)); } loop_init = false; int _e9 = i_1; if (_e9 < 6) { } else { break; } { int _e12 = i_1; if (_e12 >= count) { break; } int _e14 = i_1; if (_e14 == skip) { continue; } int _e18 = i_1; metal::float4 plane = frame.planes.inner[_e18]; if ((metal::dot(plane.xyz, p_1) + plane.w) > 0.0) { return true; } } } return false; } SegmentQuad segment_quad( metal::float3 a_1, metal::float3 b_1, metal::float2 corner_1, float width, float pull, constant Frame& frame ) { SegmentQuad o_4 = {}; bool local_1 = {}; float ta = 0.0; float tb = 1.0; metal::float2 dir_1 = {}; o_4.clip = metal::float4(2.0, 2.0, 2.0, 1.0); o_4.visible = false; float near = frame.params.x; float za = -(a_1.z); float zb = -(b_1.z); if (za < near) { local_1 = zb < near; } else { local_1 = false; } bool _e27 = local_1; if (_e27) { SegmentQuad _e28 = o_4; return _e28; } float cut = (near - za) / (zb - za); if (za < near) { ta = cut; } if (zb < near) { tb = cut; } float _e38 = ta; metal::float3 ca = metal::mix(a_1, b_1, _e38); float _e40 = tb; metal::float3 cb = metal::mix(a_1, b_1, _e40); metal::float4x4 _e44 = frame.proj; metal::float4 ka = _e44 * metal::float4(ca, 1.0); metal::float4x4 _e50 = frame.proj; metal::float4 kb = _e50 * metal::float4(cb, 1.0); metal::float4 _e56 = frame.viewport; metal::float2 half_vp = 0.5 * _e56.xy; metal::float2 sa = (ka.xy / metal::float2(ka.w)) * half_vp; metal::float2 sb = (kb.xy / metal::float2(kb.w)) * half_vp; dir_1 = sb - sa; metal::float2 _e72 = dir_1; float span = metal::length(_e72); if (span > 0.0001) { metal::float2 _e76 = dir_1; dir_1 = _e76 / metal::float2(span); } else { dir_1 = metal::float2(1.0, 0.0); } float _e83 = dir_1.y; float _e86 = dir_1.x; metal::float2 nrm = metal::float2(-(_e83), _e86); float _e93 = frame.params.y; float half_w = (0.5 * width) * _e93; float pad = half_w + 1.0; float at_end = corner_1.x; float sgn = (at_end * 2.0) - 1.0; metal::float2 _e102 = dir_1; metal::float2 offset = (_e102 * (sgn * pad)) + (nrm * (corner_1.y * pad)); metal::float4 k = metal::mix(ka, kb, at_end); o_4.clip = metal::float4(k.xy + ((offset / half_vp) * k.w), k.z * (1.0 + pull), k.w); o_4.view_pos = metal::mix(ca, cb, at_end); o_4.across = corner_1.y * pad; o_4.along = (at_end * span) + (sgn * pad); o_4.length_px = span; float _e133 = ta; o_4.ta = _e133; float _e135 = tb; o_4.tb = _e135; o_4.visible = true; SegmentQuad _e138 = o_4; return _e138; } float segment_cover( float width_1, float across, float along, float length_px, constant Frame& frame ) { float cover = {}; float _e9 = frame.params.y; float half_w_1 = (0.5 * width_1) * _e9; cover = metal::clamp((half_w_1 + 0.5) - metal::abs(across), 0.0, 1.0); float _e19 = cover; cover = (_e19 * metal::clamp((along + half_w_1) + 0.5, 0.0, 1.0)) * metal::clamp(((length_px + half_w_1) + 0.5) - along, 0.0, 1.0); float _e35 = cover; return _e35; } struct vs_countInput { metal::float3 p [[attribute(0)]]; }; struct vs_countOutput { metal::float4 clip [[position]]; metal::float3 view_pos [[user(loc0), center_perspective]]; }; vertex vs_countOutput vs_count( vs_countInput varyings [[stage_in]] , constant Frame& frame [[buffer(0)]] , constant Item& item [[buffer(1)]] ) { const auto p = varyings.p; Counted o = {}; metal::float4x4 _e4 = item.mv; metal::float4 v = _e4 * metal::float4(p, 1.0); o.view_pos = v.xyz; metal::float4x4 _e13 = frame.proj; o.clip = _e13 * v; Counted _e15 = o; const auto _tmp = _e15; return vs_countOutput { _tmp.clip, _tmp.view_pos }; } struct fs_countInput { metal::float3 view_pos [[user(loc0), center_perspective]]; }; struct fs_countOutput { metal::float4 member_1 [[color(0)]]; }; fragment fs_countOutput fs_count( fs_countInput varyings_1 [[stage_in]] , metal::float4 clip [[position]] , constant Frame& frame [[buffer(0)]] , constant Item& item [[buffer(1)]] ) { const Counted in = { clip, varyings_1.view_pos }; float _e6 = item.params.x; metal::float4 plane_1 = frame.planes.inner[naga_f2i32(_e6)]; if ((metal::dot(plane_1.xyz, in.view_pos) + plane_1.w) > 0.0) { metal::discard_fragment(); } return fs_countOutput { metal::float4(0.0) }; } struct vs_fullInput { }; struct vs_fullOutput { metal::float4 clip [[position]]; metal::float2 ndc [[user(loc0), center_perspective]]; }; vertex vs_fullOutput vs_full( uint i [[vertex_id]] ) { Full o_1 = {}; float x = static_cast<float>((i << 1u) & 2u); float y = static_cast<float>(i & 2u); o_1.ndc = metal::float2((x * 2.0) - 1.0, (y * 2.0) - 1.0); metal::float2 _e22 = o_1.ndc; o_1.clip = metal::float4(_e22, 0.0, 1.0); Full _e26 = o_1; const auto _tmp_1 = _e26; return vs_fullOutput { _tmp_1.clip, _tmp_1.ndc }; } struct fs_capInput { metal::float2 ndc [[user(loc0), center_perspective]]; }; struct fs_capOutput { metal::float4 color [[color(0)]]; float depth [[depth(any)]]; }; fragment fs_capOutput fs_cap( fs_capInput varyings_3 [[stage_in]] , metal::float4 clip_1 [[position]] , constant Frame& frame [[buffer(0)]] , constant Item& item [[buffer(1)]] ) { const Full in_1 = { clip_1, varyings_3.ndc }; Capped o_2 = {}; metal::float3 origin = metal::float3(0.0); metal::float3 dir = {}; bool local = {}; o_2.color = metal::float4(0.0); o_2.depth = 0.0; float _e12 = item.params.x; metal::float4 plane_2 = frame.planes.inner[naga_f2i32(_e12)]; float _e25 = frame.proj[0].x; float _e33 = frame.proj[1].y; dir = metal::float3(in_1.ndc.x / _e25, in_1.ndc.y / _e33, -1.0); float _e42 = frame.proj[3].w; if (_e42 > 0.5) { metal::float3 _e45 = dir; origin = metal::float3(_e45.xy, 0.0); dir = metal::float3(0.0, 0.0, -1.0); } metal::float3 _e54 = dir; float facing = metal::dot(plane_2.xyz, _e54); if (metal::abs(facing) < 0.000000001) { metal::discard_fragment(); } metal::float3 _e61 = origin; float t = -((plane_2.w + metal::dot(plane_2.xyz, _e61))) / facing; metal::float3 _e66 = origin; metal::float3 _e67 = dir; metal::float3 p_2 = _e66 + (_e67 * t); if (!((t <= 0.0))) { float _e78 = item.params.x; bool _e80 = section_clipped(p_2, naga_f2i32(_e78), frame); local = _e80; } else { local = true; } bool _e82 = local; if (_e82) { metal::discard_fragment(); } metal::float4x4 _e85 = frame.proj; metal::float4 k_1 = _e85 * metal::float4(p_2, 1.0); o_2.depth = k_1.z / k_1.w; metal::float4 _e96 = item.tint; o_2.color = metal::float4(_e96.xyz, 1.0); Capped _e100 = o_2; const auto _tmp_2 = _e100; return fs_capOutput { _tmp_2.color, _tmp_2.depth }; } struct fs_clearInput { metal::float2 ndc [[user(loc0), center_perspective]]; }; struct fs_clearOutput { metal::float4 member_4 [[color(0)]]; }; fragment fs_clearOutput fs_clear( fs_clearInput varyings_4 [[stage_in]] , metal::float4 clip_2 [[position]] ) { const Full in_2 = { clip_2, varyings_4.ndc }; return fs_clearOutput { metal::float4(0.0) }; } struct vs_outlineInput { metal::float3 a [[attribute(0)]]; metal::float3 b [[attribute(1)]]; metal::float3 c [[attribute(2)]]; metal::float2 corner [[attribute(3)]]; }; struct vs_outlineOutput { metal::float4 clip [[position]]; float across [[user(loc0), center_perspective]]; float along [[user(loc1), center_perspective]]; float length_px [[user(loc2), center_perspective]]; metal::float3 view_pos [[user(loc3), center_perspective]]; }; vertex vs_outlineOutput vs_outline( vs_outlineInput varyings_5 [[stage_in]] , constant Frame& frame [[buffer(0)]] , constant Item& item [[buffer(1)]] ) { const auto a = varyings_5.a; const auto b = varyings_5.b; const auto c = varyings_5.c; const auto corner = varyings_5.corner; Outlined o_3 = {}; metal::float3 p0_ = metal::float3(0.0); metal::float3 p1_ = metal::float3(0.0); int n = 0; o_3.clip = metal::float4(2.0, 2.0, 2.0, 1.0); float _e16 = item.params.x; metal::float4 plane_3 = frame.planes.inner[naga_f2i32(_e16)]; metal::float4x4 _e22 = item.mv; metal::float3 va = (_e22 * metal::float4(a, 1.0)).xyz; metal::float4x4 _e29 = item.mv; metal::float3 vb = (_e29 * metal::float4(b, 1.0)).xyz; metal::float4x4 _e36 = item.mv; metal::float3 vc = (_e36 * metal::float4(c, 1.0)).xyz; float da = metal::dot(plane_3.xyz, va) + plane_3.w; float db = metal::dot(plane_3.xyz, vb) + plane_3.w; float dc = metal::dot(plane_3.xyz, vc) + plane_3.w; bool sa_1 = da > 0.0; bool sb_1 = db > 0.0; bool sc = dc > 0.0; if (sa_1 != sb_1) { p0_ = metal::mix(va, vb, da / (da - db)); n = 1; } if (sb_1 != sc) { metal::float3 p_3 = metal::mix(vb, vc, db / (db - dc)); int _e76 = n; if (_e76 == 0) { p0_ = p_3; } else { p1_ = p_3; } int _e79 = n; n = as_type<int>(as_type<uint>(_e79) + as_type<uint>(1)); } if (sc != sa_1) { metal::float3 p_4 = metal::mix(vc, va, dc / (dc - da)); int _e86 = n; if (_e86 == 0) { p0_ = p_4; } else { p1_ = p_4; } int _e89 = n; n = as_type<int>(as_type<uint>(_e89) + as_type<uint>(1)); } int _e92 = n; if (_e92 != 2) { Outlined _e95 = o_3; const auto _tmp_3 = _e95; return vs_outlineOutput { _tmp_3.clip, _tmp_3.across, _tmp_3.along, _tmp_3.length_px, _tmp_3.view_pos }; } metal::float3 _e96 = p0_; metal::float3 _e97 = p1_; float _e101 = item.params.y; float _e105 = item.params.z; SegmentQuad _e106 = segment_quad(_e96, _e97, corner, _e101, _e105, frame); o_3.clip = _e106.clip; o_3.across = _e106.across; o_3.along = _e106.along; o_3.length_px = _e106.length_px; o_3.view_pos = _e106.view_pos; Outlined _e117 = o_3; const auto _tmp_4 = _e117; return vs_outlineOutput { _tmp_4.clip, _tmp_4.across, _tmp_4.along, _tmp_4.length_px, _tmp_4.view_pos }; } struct fs_outlineInput { float across_1 [[user(loc0), center_perspective]]; float along_1 [[user(loc1), center_perspective]]; float length_px_1 [[user(loc2), center_perspective]]; metal::float3 view_pos [[user(loc3), center_perspective]]; }; struct fs_outlineOutput { metal::float4 member_6 [[color(0)]]; }; fragment fs_outlineOutput fs_outline( fs_outlineInput varyings_6 [[stage_in]] , metal::float4 clip_3 [[position]] , constant Frame& frame [[buffer(0)]] , constant Item& item [[buffer(1)]] ) { const Outlined in_3 = { clip_3, varyings_6.across_1, varyings_6.along_1, varyings_6.length_px_1, {}, varyings_6.view_pos }; float _e5 = item.params.x; bool _e7 = section_clipped(in_3.view_pos, naga_f2i32(_e5), frame); if (_e7) { metal::discard_fragment(); } float _e11 = item.params.y; float _e15 = segment_cover(_e11, in_3.across, in_3.along, in_3.length_px, frame); float _e19 = item.extra.w; float a_2 = _e19 * _e15; metal::float4 _e23 = item.extra; return fs_outlineOutput { metal::float4(_e23.xyz * a_2, a_2) }; } `, Entries: []gpu.EntryPoint{ {Name: "vs_count", Stage: gpu.ShaderStageVertex, Inputs: []gpu.VertexInput{ {Name: "p", Location: 0, Type: gpu.ShaderTypeFloat32x3}, }, Bindings: []gpu.ShaderBinding{ {Name: "frame", Group: 0, Binding: 0, Kind: gpu.BindingUniformBuffer, Size: 384, Register: gpu.ShaderRegister{Class: "b", Space: 0, Index: 0}, MetalIndex: 0}, {Name: "item", Group: 1, Binding: 0, Kind: gpu.BindingUniformBuffer, Size: 112, Register: gpu.ShaderRegister{Class: "b", Space: 1, Index: 0}, MetalIndex: 1}, }, }, {Name: "fs_count", Stage: gpu.ShaderStageFragment, Bindings: []gpu.ShaderBinding{ {Name: "frame", Group: 0, Binding: 0, Kind: gpu.BindingUniformBuffer, Size: 384, Register: gpu.ShaderRegister{Class: "b", Space: 0, Index: 0}, MetalIndex: 0}, {Name: "item", Group: 1, Binding: 0, Kind: gpu.BindingUniformBuffer, Size: 112, Register: gpu.ShaderRegister{Class: "b", Space: 1, Index: 0}, MetalIndex: 1}, }, }, {Name: "vs_full", Stage: gpu.ShaderStageVertex, ReadsVertexIndex: true, Bindings: []gpu.ShaderBinding{}, }, {Name: "fs_cap", Stage: gpu.ShaderStageFragment, Bindings: []gpu.ShaderBinding{ {Name: "frame", Group: 0, Binding: 0, Kind: gpu.BindingUniformBuffer, Size: 384, Register: gpu.ShaderRegister{Class: "b", Space: 0, Index: 0}, MetalIndex: 0}, {Name: "item", Group: 1, Binding: 0, Kind: gpu.BindingUniformBuffer, Size: 112, Register: gpu.ShaderRegister{Class: "b", Space: 1, Index: 0}, MetalIndex: 1}, }, }, {Name: "fs_clear", Stage: gpu.ShaderStageFragment, Bindings: []gpu.ShaderBinding{}, }, {Name: "vs_outline", Stage: gpu.ShaderStageVertex, Inputs: []gpu.VertexInput{ {Name: "a", Location: 0, Type: gpu.ShaderTypeFloat32x3}, {Name: "b", Location: 1, Type: gpu.ShaderTypeFloat32x3}, {Name: "c", Location: 2, Type: gpu.ShaderTypeFloat32x3}, {Name: "corner", Location: 3, Type: gpu.ShaderTypeFloat32x2}, }, Bindings: []gpu.ShaderBinding{ {Name: "frame", Group: 0, Binding: 0, Kind: gpu.BindingUniformBuffer, Size: 384, Register: gpu.ShaderRegister{Class: "b", Space: 0, Index: 0}, MetalIndex: 0}, {Name: "item", Group: 1, Binding: 0, Kind: gpu.BindingUniformBuffer, Size: 112, Register: gpu.ShaderRegister{Class: "b", Space: 1, Index: 0}, MetalIndex: 1}, }, }, {Name: "fs_outline", Stage: gpu.ShaderStageFragment, Bindings: []gpu.ShaderBinding{ {Name: "frame", Group: 0, Binding: 0, Kind: gpu.BindingUniformBuffer, Size: 384, Register: gpu.ShaderRegister{Class: "b", Space: 0, Index: 0}, MetalIndex: 0}, {Name: "item", Group: 1, Binding: 0, Kind: gpu.BindingUniformBuffer, Size: 112, Register: gpu.ShaderRegister{Class: "b", Space: 1, Index: 0}, MetalIndex: 1}, }, }, }, }
Section is section.wgsl.
var Select = &gpu.Shader{ Name: "select", WGSL: `// The outline of the selection in viewport3d (SPEC §27.8): the selected items that have faces are drawn white into a mask the size // of the view, and fs_outline, over a rectangle around them, colors each pixel that is not in the mask and has a pixel of it within // the radius, with an anti-aliased edge: a line of the radius's width around the silhouette. // // The record has the color of the outline in tint and the radius in pixels in params.x. // What the shaders of viewport3d share (SPEC §27.8): the frame's uniform data, the record of an item, and the section planes. struct Light { // dir points to the light, in view space. dir: vec4<f32>, // color is the light's color times its intensity. color: vec4<f32>, } struct Frame { proj: mat4x4<f32>, // lights are the light at the camera, then up to four directional ones; an unused one has a black color. lights: array<Light, 5>, ambient: vec4<f32>, // viewport is the width and the height of the target in pixels, and their inverses. viewport: vec4<f32>, // params are the near plane's distance, the device pixel ratio and two unused numbers. params: vec4<f32>, // planes are the section planes in view space, relative to the eye (D30): a point p with dot(plane.xyz, p) + plane.w > 0 is // cut away. planes: array<vec4<f32>, 6>, // section is the number of planes in x. section: vec4<f32>, } @group(0) @binding(0) var<uniform> frame: Frame; // Item is the record of a line, a point or a picked item: the matrix that takes the mesh's positions to the view, relative to // the eye, a tint (the highlight of a selected item: its color, and the part of it that replaces the material's in a), and // numbers for the shader that reads it. struct Item { mv: mat4x4<f32>, tint: vec4<f32>, params: vec4<f32>, extra: vec4<f32>, } // section_clipped says whether a point of the view space is cut away by a section plane. skip is the plane to leave out (the // plane that a cap lies on), or -1. fn section_clipped(p: vec3<f32>, skip: i32) -> bool { let count = i32(frame.section.x); for (var i = 0; i < 6; i = i + 1) { if (i >= count) { break; } if (i == skip) { continue; } let plane = frame.planes[i]; if (dot(plane.xyz, p) + plane.w > 0.0) { return true; } } return false; } // PickOut is what a pick pass writes for a pixel: the number of the item (from 1, 0 for nothing) and of the primitive in it, and // the depth. struct PickOut { @location(0) ids: vec2<u32>, @location(1) depth: f32, } @group(1) @binding(0) var<uniform> item: Item; @group(1) @binding(1) var mask: texture_2d<f32>; struct Full { @builtin(position) clip: vec4<f32>, } @vertex fn vs_full(@builtin(vertex_index) i: u32) -> Full { // One triangle that covers the view: (-1, -1), (3, -1) and (-1, 3). let x = f32((i << 1u) & 2u); let y = f32(i & 2u); var o: Full; o.clip = vec4<f32>(x * 2.0 - 1.0, y * 2.0 - 1.0, 0.0, 1.0); return o; } @fragment fn fs_outline(@builtin(position) pos: vec4<f32>) -> @location(0) vec4<f32> { let size = vec2<i32>(frame.viewport.xy); let here = vec2<i32>(pos.xy); if (textureLoad(mask, here, 0).r > 0.5) { discard; } let radius = i32(item.params.x); var nearest = 1.0e9; for (var dy = -radius; dy <= radius; dy = dy + 1) { for (var dx = -radius; dx <= radius; dx = dx + 1) { let q = here + vec2<i32>(dx, dy); if (q.x < 0 || q.y < 0 || q.x >= size.x || q.y >= size.y) { continue; } if (textureLoad(mask, q, 0).r > 0.5) { nearest = min(nearest, f32(dx * dx + dy * dy)); } } } if (nearest > 1.0e8) { discard; } let cover = clamp(f32(radius) + 0.5 - sqrt(nearest), 0.0, 1.0); let a = item.tint.a * cover; return vec4<f32>(item.tint.rgb * a, a); } `, SPIRV: spirvSelect, HLSL: `struct Light { float4 dir; float4 color; }; struct Frame { row_major float4x4 proj; Light lights[5]; float4 ambient; float4 viewport; float4 params; float4 planes[6]; float4 section; }; struct Item { row_major float4x4 mv; float4 tint; float4 params; float4 extra; }; struct PickOut { uint2 ids : LOC0; float depth : LOC1; }; struct Full { float4 clip_ : SV_Position; }; cbuffer frame : register(b0, space0) { Frame frame; } cbuffer item : register(b0, space1) { Item item; } Texture2D<float4> mask : register(t1, space1); struct VertexOutput_vs_full { float4 clip_ : SV_Position; }; struct FragmentInput_fs_outline { float4 pos_1 : SV_Position; }; int naga_f2i32(float value) { return int(clamp(value, -2147483600.0, 2147483500.0)); } bool section_clipped(float3 p, int skip) { int i_1 = int(0); float _e5 = frame.section.x; int count = naga_f2i32(_e5); uint2 loop_bound = uint2(4294967295u, 4294967295u); bool loop_init = true; while(true) { if (all(loop_bound == uint2(0u, 0u))) { break; } loop_bound -= uint2(loop_bound.y == 0u, 1u); if (!loop_init) { int _e28 = i_1; i_1 = asint(asuint(_e28) + asuint(int(1))); } loop_init = false; int _e9 = i_1; if ((_e9 < int(6))) { } else { break; } { int _e12 = i_1; if ((_e12 >= count)) { break; } int _e14 = i_1; if ((_e14 == skip)) { continue; } int _e18 = i_1; float4 plane = frame.planes[_e18]; if (((dot(plane.xyz, p) + plane.w) > 0.0)) { return true; } } } return false; } VertexOutput_vs_full vs_full(uint i : SV_VertexID) { Full o = (Full)0; float x = float(((i << 1u) & 2u)); float y = float((i & 2u)); o.clip_ = float4(((x * 2.0) - 1.0), ((y * 2.0) - 1.0), 0.0, 1.0); Full _e22 = o; Full full = _e22; VertexOutput_vs_full full_1 = { full.clip_ }; return full_1; } int naga_neg(int val) { return asint(-asuint(val)); } int2 naga_f2i32(float2 value) { return int2(clamp(value, -2147483600.0, 2147483500.0)); } float4 fs_outline(FragmentInput_fs_outline fragmentinput_fs_outline) : SV_Target0 { float4 pos = fragmentinput_fs_outline.pos_1; float nearest = 1000000000.0; int dy = (int)0; int dx = (int)0; bool local = (bool)0; bool local_1 = (bool)0; bool local_2 = (bool)0; float4 _e3 = frame.viewport; int2 size = naga_f2i32(_e3.xy); int2 here = naga_f2i32(pos.xy); float4 _e10 = mask.Load(int3(here, int(0))); if ((_e10.x > 0.5)) { discard; } float _e17 = item.params.x; int radius = naga_f2i32(_e17); dy = naga_neg(radius); uint2 loop_bound_1 = uint2(4294967295u, 4294967295u); bool loop_init_1 = true; while(true) { if (all(loop_bound_1 == uint2(0u, 0u))) { break; } loop_bound_1 -= uint2(loop_bound_1.y == 0u, 1u); if (!loop_init_1) { int _e79 = dy; dy = asint(asuint(_e79) + asuint(int(1))); } loop_init_1 = false; int _e23 = dy; if ((_e23 <= radius)) { } else { break; } { dx = naga_neg(radius); uint2 loop_bound_2 = uint2(4294967295u, 4294967295u); bool loop_init_2 = true; while(true) { if (all(loop_bound_2 == uint2(0u, 0u))) { break; } loop_bound_2 -= uint2(loop_bound_2.y == 0u, 1u); if (!loop_init_2) { int _e76 = dx; dx = asint(asuint(_e76) + asuint(int(1))); } loop_init_2 = false; int _e27 = dx; if ((_e27 <= radius)) { } else { break; } { int _e29 = dx; int _e30 = dy; int2 q = asint(asuint(here) + asuint(int2(_e29, _e30))); if (!((q.x < int(0)))) { local = (q.y < int(0)); } else { local = true; } bool _e43 = local; if (!(_e43)) { local_1 = (q.x >= size.x); } else { local_1 = true; } bool _e51 = local_1; if (!(_e51)) { local_2 = (q.y >= size.y); } else { local_2 = true; } bool _e59 = local_2; if (_e59) { continue; } float4 _e62 = mask.Load(int3(q, int(0))); if ((_e62.x > 0.5)) { float _e66 = nearest; int _e67 = dx; int _e68 = dx; int _e70 = dy; int _e71 = dy; nearest = min(_e66, float(asint(asuint(asint(asuint(_e67) * asuint(_e68))) + asuint(asint(asuint(_e70) * asuint(_e71)))))); } } } } } float _e82 = nearest; if ((_e82 > 100000000.0)) { discard; } float _e88 = nearest; float cover = clamp(((float(radius) + 0.5) - sqrt(_e88)), 0.0, 1.0); float _e97 = item.tint.w; float a = (_e97 * cover); float4 _e101 = item.tint; return float4((_e101.xyz * a), a); } `, MSL: `// language: metal2.4 #include <metal_stdlib> #include <simd/simd.h> using metal::uint; struct Light { metal::float4 dir; metal::float4 color; }; struct type_3 { Light inner[5]; }; struct type_4 { metal::float4 inner[6]; }; struct Frame { metal::float4x4 proj; type_3 lights; metal::float4 ambient; metal::float4 viewport; metal::float4 params; type_4 planes; metal::float4 section; }; struct Item { metal::float4x4 mv; metal::float4 tint; metal::float4 params; metal::float4 extra; }; struct PickOut { metal::uint2 ids; float depth; char _pad2[4]; }; struct Full { metal::float4 clip; }; int naga_f2i32(float value) { return static_cast<int>(metal::clamp(value, -2147483600.0, 2147483500.0)); } bool section_clipped( metal::float3 p, int skip, constant Frame& frame ) { int i_1 = 0; float _e5 = frame.section.x; int count = naga_f2i32(_e5); uint2 loop_bound = uint2(4294967295u); bool loop_init = true; while(true) { if (metal::all(loop_bound == uint2(0u))) { break; } loop_bound -= uint2(loop_bound.y == 0u, 1u); if (!loop_init) { int _e28 = i_1; i_1 = as_type<int>(as_type<uint>(_e28) + as_type<uint>(1)); } loop_init = false; int _e9 = i_1; if (_e9 < 6) { } else { break; } { int _e12 = i_1; if (_e12 >= count) { break; } int _e14 = i_1; if (_e14 == skip) { continue; } int _e18 = i_1; metal::float4 plane = frame.planes.inner[_e18]; if ((metal::dot(plane.xyz, p) + plane.w) > 0.0) { return true; } } } return false; } struct vs_fullInput { }; struct vs_fullOutput { metal::float4 clip [[position]]; }; vertex vs_fullOutput vs_full( uint i [[vertex_id]] ) { Full o = {}; float x = static_cast<float>((i << 1u) & 2u); float y = static_cast<float>(i & 2u); o.clip = metal::float4((x * 2.0) - 1.0, (y * 2.0) - 1.0, 0.0, 1.0); Full _e22 = o; const auto _tmp = _e22; return vs_fullOutput { _tmp.clip }; } metal::int2 naga_f2i32(metal::float2 value) { return static_cast<metal::int2>(metal::clamp(value, -2147483600.0, 2147483500.0)); } int naga_neg(int val) { return as_type<int>(-as_type<uint>(val)); } struct fs_outlineInput { }; struct fs_outlineOutput { metal::float4 member_1 [[color(0)]]; }; fragment fs_outlineOutput fs_outline( metal::float4 pos [[position]] , constant Frame& frame [[buffer(0)]] , constant Item& item [[buffer(1)]] , metal::texture2d<float, metal::access::sample> mask [[texture(0)]] ) { float nearest = 1000000000.0; int dy = {}; int dx = {}; bool local = {}; bool local_1 = {}; bool local_2 = {}; metal::float4 _e3 = frame.viewport; metal::int2 size = naga_f2i32(_e3.xy); metal::int2 here = naga_f2i32(pos.xy); metal::float4 _e10 = mask.read(metal::uint2(here), 0); if (_e10.x > 0.5) { metal::discard_fragment(); } float _e17 = item.params.x; int radius = naga_f2i32(_e17); dy = naga_neg(radius); uint2 loop_bound_1 = uint2(4294967295u); bool loop_init_1 = true; while(true) { if (metal::all(loop_bound_1 == uint2(0u))) { break; } loop_bound_1 -= uint2(loop_bound_1.y == 0u, 1u); if (!loop_init_1) { int _e79 = dy; dy = as_type<int>(as_type<uint>(_e79) + as_type<uint>(1)); } loop_init_1 = false; int _e23 = dy; if (_e23 <= radius) { } else { break; } { dx = naga_neg(radius); uint2 loop_bound_2 = uint2(4294967295u); bool loop_init_2 = true; while(true) { if (metal::all(loop_bound_2 == uint2(0u))) { break; } loop_bound_2 -= uint2(loop_bound_2.y == 0u, 1u); if (!loop_init_2) { int _e76 = dx; dx = as_type<int>(as_type<uint>(_e76) + as_type<uint>(1)); } loop_init_2 = false; int _e27 = dx; if (_e27 <= radius) { } else { break; } { int _e29 = dx; int _e30 = dy; metal::int2 q = as_type<metal::int2>(as_type<metal::uint2>(here) + as_type<metal::uint2>(metal::int2(_e29, _e30))); if (!((q.x < 0))) { local = q.y < 0; } else { local = true; } bool _e43 = local; if (!(_e43)) { local_1 = q.x >= size.x; } else { local_1 = true; } bool _e51 = local_1; if (!(_e51)) { local_2 = q.y >= size.y; } else { local_2 = true; } bool _e59 = local_2; if (_e59) { continue; } metal::float4 _e62 = mask.read(metal::uint2(q), 0); if (_e62.x > 0.5) { float _e66 = nearest; int _e67 = dx; int _e68 = dx; int _e70 = dy; int _e71 = dy; nearest = metal::min(_e66, static_cast<float>(as_type<int>(as_type<uint>(as_type<int>(as_type<uint>(_e67) * as_type<uint>(_e68))) + as_type<uint>(as_type<int>(as_type<uint>(_e70) * as_type<uint>(_e71)))))); } } } } } float _e82 = nearest; if (_e82 > 100000000.0) { metal::discard_fragment(); } float _e88 = nearest; float cover = metal::clamp((static_cast<float>(radius) + 0.5) - metal::sqrt(_e88), 0.0, 1.0); float _e97 = item.tint.w; float a = _e97 * cover; metal::float4 _e101 = item.tint; return fs_outlineOutput { metal::float4(_e101.xyz * a, a) }; } `, Entries: []gpu.EntryPoint{ {Name: "vs_full", Stage: gpu.ShaderStageVertex, ReadsVertexIndex: true, Bindings: []gpu.ShaderBinding{}, }, {Name: "fs_outline", Stage: gpu.ShaderStageFragment, Bindings: []gpu.ShaderBinding{ {Name: "frame", Group: 0, Binding: 0, Kind: gpu.BindingUniformBuffer, Size: 384, Register: gpu.ShaderRegister{Class: "b", Space: 0, Index: 0}, MetalIndex: 0}, {Name: "item", Group: 1, Binding: 0, Kind: gpu.BindingUniformBuffer, Size: 112, Register: gpu.ShaderRegister{Class: "b", Space: 1, Index: 0}, MetalIndex: 1}, {Name: "mask", Group: 1, Binding: 1, Kind: gpu.BindingSampledTexture, Sample: gpu.SampleTypeUnfilterableFloat, Register: gpu.ShaderRegister{Class: "t", Space: 1, Index: 1}, MetalIndex: 0}, }, }, }, }
Select is select.wgsl.
var Shadow = &gpu.Shader{ Name: "shadow", WGSL: `// The shadow map of viewport3d (SPEC §28.5): the items seen from the light, as depth. The matrix of an instance takes the item's // positions straight to the clip space of the light (an orthographic projection fitted to the scene), so the shader needs no // frame data. The depth is written as a color of a float target (the depth formats cannot be sampled, SPEC §27.2) and read with // textureLoad by pbr.wgsl. The skin and the morph targets of an item are applied as in the PBR vertex shader. @group(0) @binding(0) var<storage, read> joint_matrices: array<mat4x4<f32>>; @group(0) @binding(1) var<storage, read> morph_weights: array<f32>; @group(1) @binding(0) var<storage, read> morph_deltas: array<vec4<f32>>; struct VOut { @builtin(position) clip: vec4<f32>, @location(0) depth: f32, } const SKINNED: u32 = 32u; // vs_shadow_simple is for the meshes of the other materials: positions only. @vertex fn vs_shadow_simple(@location(0) p: vec3<f32>, @location(8) m0: vec4<f32>, @location(9) m1: vec4<f32>, @location(10) m2: vec4<f32>, @location(11) m3: vec4<f32>) -> VOut { var o: VOut; o.clip = mat4x4<f32>(m0, m1, m2, m3) * vec4<f32>(p, 1.0); o.depth = o.clip.z; return o; } // vs_shadow_full is for the meshes of the PBR material, with the vertex layout of pbr.wgsl. @vertex fn vs_shadow_full(@builtin(vertex_index) vertex: u32, @location(0) p: vec3<f32>, @location(6) joints: vec4<u32>, @location(7) weights: vec4<f32>, @location(8) m0: vec4<f32>, @location(9) m1: vec4<f32>, @location(10) m2: vec4<f32>, @location(11) m3: vec4<f32>, @location(13) extra: vec4<f32>) -> VOut { var pos = p; let targets = u32(extra.z); if (targets > 0u) { let wbase = u32(extra.y); for (var k = 0u; k < targets; k = k + 1u) { let w = morph_weights[wbase + k]; if (w != 0.0) { pos = pos + w * morph_deltas[(vertex * targets + k) * 2u].xyz; } } } if ((u32(extra.w) & SKINNED) != 0u) { let jb = u32(extra.x); let skin = joint_matrices[jb + joints.x] * weights.x + joint_matrices[jb + joints.y] * weights.y + joint_matrices[jb + joints.z] * weights.z + joint_matrices[jb + joints.w] * weights.w; pos = (skin * vec4<f32>(pos, 1.0)).xyz; } var o: VOut; o.clip = mat4x4<f32>(m0, m1, m2, m3) * vec4<f32>(pos, 1.0); o.depth = o.clip.z; return o; } @fragment fn fs_shadow(in: VOut) -> @location(0) f32 { return in.depth; } `, SPIRV: spirvShadow, HLSL: `struct VOut { float4 clip_ : SV_Position; float depth : LOC0; }; static const uint SKINNED = 32u; ByteAddressBuffer joint_matrices : register(t0, space0); ByteAddressBuffer morph_weights : register(t1, space0); ByteAddressBuffer morph_deltas : register(t0, space1); struct VertexOutput_vs_shadow_simple { float depth : LOC0; float4 clip_ : SV_Position; }; struct VertexOutput_vs_shadow_full { float depth_1 : LOC0; float4 clip_1 : SV_Position; }; struct FragmentInput_fs_shadow { float depth_2 : LOC0; float4 clip_2 : SV_Position; }; VertexOutput_vs_shadow_simple vs_shadow_simple(float3 p : LOC0, float4 m0_ : LOC8, float4 m1_ : LOC9, float4 m2_ : LOC10, float4 m3_ : LOC11) { VOut o = (VOut)0; o.clip_ = mul(float4(p, 1.0), float4x4(m0_, m1_, m2_, m3_)); float _e14 = o.clip_.z; o.depth = _e14; VOut _e15 = o; VOut vout = _e15; VertexOutput_vs_shadow_simple vout_1 = { vout.depth, vout.clip_ }; return vout_1; } uint naga_f2u32(float value) { return uint(clamp(value, 0.0, 4294967000.0)); } VertexOutput_vs_shadow_full vs_shadow_full(uint vertex : SV_VertexID, float3 p_1 : LOC0, uint4 joints : LOC6, float4 weights : LOC7, float4 m0_1 : LOC8, float4 m1_1 : LOC9, float4 m2_1 : LOC10, float4 m3_1 : LOC11, float4 extra : LOC13) { float3 pos = (float3)0; uint k = 0u; VOut o_1 = (VOut)0; pos = p_1; uint targets = naga_f2u32(extra.z); if ((targets > 0u)) { uint wbase = naga_f2u32(extra.y); uint2 loop_bound = uint2(4294967295u, 4294967295u); bool loop_init = true; while(true) { if (all(loop_bound == uint2(0u, 0u))) { break; } loop_bound -= uint2(loop_bound.y == 0u, 1u); if (!loop_init) { uint _e39 = k; k = (_e39 + 1u); } loop_init = false; uint _e18 = k; if ((_e18 < targets)) { } else { break; } { uint _e21 = k; float w = asfloat(morph_weights.Load((wbase + _e21)*4)); if ((w != 0.0)) { float3 _e27 = pos; uint _e30 = k; float4 _e35 = asfloat(morph_deltas.Load4((((vertex * targets) + _e30) * 2u)*16)); pos = (_e27 + (w * _e35.xyz)); } } } } if (((naga_f2u32(extra.w) & SKINNED) != 0u)) { uint jb = naga_f2u32(extra.x); float4x4 _e54 = float4x4(asfloat(joint_matrices.Load4((jb + joints.x)*64+0)), asfloat(joint_matrices.Load4((jb + joints.x)*64+16)), asfloat(joint_matrices.Load4((jb + joints.x)*64+32)), asfloat(joint_matrices.Load4((jb + joints.x)*64+48))); float4x4 _e61 = float4x4(asfloat(joint_matrices.Load4((jb + joints.y)*64+0)), asfloat(joint_matrices.Load4((jb + joints.y)*64+16)), asfloat(joint_matrices.Load4((jb + joints.y)*64+32)), asfloat(joint_matrices.Load4((jb + joints.y)*64+48))); float4x4 _e69 = float4x4(asfloat(joint_matrices.Load4((jb + joints.z)*64+0)), asfloat(joint_matrices.Load4((jb + joints.z)*64+16)), asfloat(joint_matrices.Load4((jb + joints.z)*64+32)), asfloat(joint_matrices.Load4((jb + joints.z)*64+48))); float4x4 _e77 = float4x4(asfloat(joint_matrices.Load4((jb + joints.w)*64+0)), asfloat(joint_matrices.Load4((jb + joints.w)*64+16)), asfloat(joint_matrices.Load4((jb + joints.w)*64+32)), asfloat(joint_matrices.Load4((jb + joints.w)*64+48))); float4x4 skin = (((mul(weights.x, _e54) + mul(weights.y, _e61)) + mul(weights.z, _e69)) + mul(weights.w, _e77)); float3 _e81 = pos; pos = mul(float4(_e81, 1.0), skin).xyz; } float3 _e89 = pos; o_1.clip_ = mul(float4(_e89, 1.0), float4x4(m0_1, m1_1, m2_1, m3_1)); float _e96 = o_1.clip_.z; o_1.depth = _e96; VOut _e97 = o_1; VOut vout_2 = _e97; VertexOutput_vs_shadow_full vout_2_ = { vout_2.depth, vout_2.clip_ }; return vout_2_; } float fs_shadow(FragmentInput_fs_shadow fragmentinput_fs_shadow) : SV_Target0 { VOut in_ = { fragmentinput_fs_shadow.clip_2, fragmentinput_fs_shadow.depth_2 }; return in_.depth; } `, MSL: `// language: metal2.4 #include <metal_stdlib> #include <simd/simd.h> using metal::uint; typedef metal::float4x4 type_2[1]; typedef float type_3[1]; typedef metal::float4 type_5[1]; struct VOut { metal::float4 clip; float depth; char _pad2[12]; }; constant uint SKINNED = 32u; struct vs_shadow_simpleInput { metal::float3 p [[attribute(0)]]; metal::float4 m0_ [[attribute(8)]]; metal::float4 m1_ [[attribute(9)]]; metal::float4 m2_ [[attribute(10)]]; metal::float4 m3_ [[attribute(11)]]; }; struct vs_shadow_simpleOutput { metal::float4 clip [[position]]; float depth [[user(loc0), center_perspective]]; }; vertex vs_shadow_simpleOutput vs_shadow_simple( vs_shadow_simpleInput varyings [[stage_in]] ) { const auto p = varyings.p; const auto m0_ = varyings.m0_; const auto m1_ = varyings.m1_; const auto m2_ = varyings.m2_; const auto m3_ = varyings.m3_; VOut o = {}; o.clip = metal::float4x4(m0_, m1_, m2_, m3_) * metal::float4(p, 1.0); float _e14 = o.clip.z; o.depth = _e14; VOut _e15 = o; const auto _tmp = _e15; return vs_shadow_simpleOutput { _tmp.clip, _tmp.depth }; } uint naga_f2u32(float value) { return static_cast<uint>(metal::clamp(value, 0.0, 4294967000.0)); } struct vs_shadow_fullInput { metal::float3 p_1 [[attribute(0)]]; metal::uint4 joints [[attribute(6)]]; metal::float4 weights [[attribute(7)]]; metal::float4 m0_1 [[attribute(8)]]; metal::float4 m1_1 [[attribute(9)]]; metal::float4 m2_1 [[attribute(10)]]; metal::float4 m3_1 [[attribute(11)]]; metal::float4 extra [[attribute(13)]]; }; struct vs_shadow_fullOutput { metal::float4 clip [[position]]; float depth [[user(loc0), center_perspective]]; }; vertex vs_shadow_fullOutput vs_shadow_full( vs_shadow_fullInput varyings_1 [[stage_in]] , uint vertex_ [[vertex_id]] , device type_2 const& joint_matrices [[buffer(0)]] , device type_3 const& morph_weights [[buffer(1)]] , device type_5 const& morph_deltas [[buffer(2)]] ) { const auto p_1 = varyings_1.p_1; const auto joints = varyings_1.joints; const auto weights = varyings_1.weights; const auto m0_1 = varyings_1.m0_1; const auto m1_1 = varyings_1.m1_1; const auto m2_1 = varyings_1.m2_1; const auto m3_1 = varyings_1.m3_1; const auto extra = varyings_1.extra; metal::float3 pos = {}; uint k = 0u; VOut o_1 = {}; pos = p_1; uint targets = naga_f2u32(extra.z); if (targets > 0u) { uint wbase = naga_f2u32(extra.y); uint2 loop_bound = uint2(4294967295u); bool loop_init = true; while(true) { if (metal::all(loop_bound == uint2(0u))) { break; } loop_bound -= uint2(loop_bound.y == 0u, 1u); if (!loop_init) { uint _e39 = k; k = _e39 + 1u; } loop_init = false; uint _e18 = k; if (_e18 < targets) { } else { break; } { uint _e21 = k; float w = morph_weights[wbase + _e21]; if (w != 0.0) { metal::float3 _e27 = pos; uint _e30 = k; metal::float4 _e35 = morph_deltas[((vertex_ * targets) + _e30) * 2u]; pos = _e27 + (w * _e35.xyz); } } } } if ((naga_f2u32(extra.w) & SKINNED) != 0u) { uint jb = naga_f2u32(extra.x); metal::float4x4 _e54 = joint_matrices[jb + joints.x]; metal::float4x4 _e61 = joint_matrices[jb + joints.y]; metal::float4x4 _e69 = joint_matrices[jb + joints.z]; metal::float4x4 _e77 = joint_matrices[jb + joints.w]; metal::float4x4 skin = (((_e54 * weights.x) + (_e61 * weights.y)) + (_e69 * weights.z)) + (_e77 * weights.w); metal::float3 _e81 = pos; pos = (skin * metal::float4(_e81, 1.0)).xyz; } metal::float3 _e89 = pos; o_1.clip = metal::float4x4(m0_1, m1_1, m2_1, m3_1) * metal::float4(_e89, 1.0); float _e96 = o_1.clip.z; o_1.depth = _e96; VOut _e97 = o_1; const auto _tmp_1 = _e97; return vs_shadow_fullOutput { _tmp_1.clip, _tmp_1.depth }; } struct fs_shadowInput { float depth [[user(loc0), center_perspective]]; }; struct fs_shadowOutput { float member_2 [[color(0)]]; }; fragment fs_shadowOutput fs_shadow( fs_shadowInput varyings_2 [[stage_in]] , metal::float4 clip [[position]] ) { const VOut in = { clip, varyings_2.depth }; return fs_shadowOutput { in.depth }; } `, Entries: []gpu.EntryPoint{ {Name: "vs_shadow_simple", Stage: gpu.ShaderStageVertex, Inputs: []gpu.VertexInput{ {Name: "p", Location: 0, Type: gpu.ShaderTypeFloat32x3}, {Name: "m0", Location: 8, Type: gpu.ShaderTypeFloat32x4}, {Name: "m1", Location: 9, Type: gpu.ShaderTypeFloat32x4}, {Name: "m2", Location: 10, Type: gpu.ShaderTypeFloat32x4}, {Name: "m3", Location: 11, Type: gpu.ShaderTypeFloat32x4}, }, Bindings: []gpu.ShaderBinding{}, }, {Name: "vs_shadow_full", Stage: gpu.ShaderStageVertex, ReadsVertexIndex: true, Inputs: []gpu.VertexInput{ {Name: "p", Location: 0, Type: gpu.ShaderTypeFloat32x3}, {Name: "joints", Location: 6, Type: gpu.ShaderTypeUint32x4}, {Name: "weights", Location: 7, Type: gpu.ShaderTypeFloat32x4}, {Name: "m0", Location: 8, Type: gpu.ShaderTypeFloat32x4}, {Name: "m1", Location: 9, Type: gpu.ShaderTypeFloat32x4}, {Name: "m2", Location: 10, Type: gpu.ShaderTypeFloat32x4}, {Name: "m3", Location: 11, Type: gpu.ShaderTypeFloat32x4}, {Name: "extra", Location: 13, Type: gpu.ShaderTypeFloat32x4}, }, Bindings: []gpu.ShaderBinding{ {Name: "joint_matrices", Group: 0, Binding: 0, Kind: gpu.BindingStorageBufferReadOnly, Size: 64, Register: gpu.ShaderRegister{Class: "t", Space: 0, Index: 0}, MetalIndex: 0}, {Name: "morph_weights", Group: 0, Binding: 1, Kind: gpu.BindingStorageBufferReadOnly, Size: 4, Register: gpu.ShaderRegister{Class: "t", Space: 0, Index: 1}, MetalIndex: 1}, {Name: "morph_deltas", Group: 1, Binding: 0, Kind: gpu.BindingStorageBufferReadOnly, Size: 16, Register: gpu.ShaderRegister{Class: "t", Space: 1, Index: 0}, MetalIndex: 2}, }, }, {Name: "fs_shadow", Stage: gpu.ShaderStageFragment, Bindings: []gpu.ShaderBinding{}, }, }, }
Shadow is shadow.wgsl.
var Tonemap = &gpu.Shader{ Name: "tonemap", WGSL: `// The tone mapping of viewport3d (SPEC §28.5): the frame was drawn as linear radiance into a float target (the PBR materials // make values above 1), and this pass turns it into the colors of the display: the exposure, a curve (the Khronos PBR Neutral // tone mapper, which leaves the colors below 0.8 as they are, or the ACES fit of Stephen Hill), and the sRGB transfer function. // The input is premultiplied by the alpha; the output is too. struct Tone { // params: x the exposure, y the curve (0 none, 1 PBR Neutral, 2 ACES). params: vec4<f32>, } @group(0) @binding(0) var<uniform> tone: Tone; @group(0) @binding(1) var hdr: texture_2d<f32>; @vertex fn vs_full(@builtin(vertex_index) i: u32) -> @builtin(position) vec4<f32> { let x = f32((i << 1u) & 2u); let y = f32(i & 2u); return vec4<f32>(x * 2.0 - 1.0, 1.0 - y * 2.0, 0.0, 1.0); } // neutral is the Khronos PBR Neutral tone mapper (github.com/KhronosGroup/ToneMapping): the colors under about 0.76 keep // their value, and the brighter ones are compressed towards white and desaturated. fn neutral(color: vec3<f32>) -> vec3<f32> { let start_compression = 0.8 - 0.04; let desaturation = 0.15; let x = min(color.r, min(color.g, color.b)); var offset = 0.04; if (x < 0.08) { offset = x - 6.25 * x * x; } var c = color - vec3<f32>(offset); let peak = max(c.r, max(c.g, c.b)); if (peak < start_compression) { return c; } let d = 1.0 - start_compression; let new_peak = 1.0 - d * d / (peak + d - start_compression); c = c * (new_peak / peak); let g = 1.0 - 1.0 / (desaturation * (peak - new_peak) + 1.0); return mix(c, vec3<f32>(new_peak), g); } fn rrt_and_odt_fit(v: vec3<f32>) -> vec3<f32> { let a = v * (v + vec3<f32>(0.0245786)) - vec3<f32>(0.000090537); let b = v * (0.983729 * v + vec3<f32>(0.4329510)) + vec3<f32>(0.238081); return a / b; } // aces is the fit of the ACES reference rendering transform and output transform by Stephen Hill (BakingLab). fn aces(color: vec3<f32>) -> vec3<f32> { let input_mat = mat3x3<f32>(vec3<f32>(0.59719, 0.07600, 0.02840), vec3<f32>(0.35458, 0.90834, 0.13383), vec3<f32>(0.04823, 0.01566, 0.83777)); let output_mat = mat3x3<f32>(vec3<f32>(1.60475, -0.10208, -0.00327), vec3<f32>(-0.53108, 1.10813, -0.07276), vec3<f32>(-0.07367, -0.00605, 1.07602)); return clamp(output_mat * rrt_and_odt_fit(input_mat * color), vec3<f32>(0.0), vec3<f32>(1.0)); } fn linear_to_srgb(c: vec3<f32>) -> vec3<f32> { let lo = c * 12.92; let hi = 1.055 * pow(c, vec3<f32>(1.0 / 2.4)) - vec3<f32>(0.055); return select(hi, lo, c <= vec3<f32>(0.0031308)); } @fragment fn fs_tonemap(@builtin(position) pos: vec4<f32>) -> @location(0) vec4<f32> { let px = textureLoad(hdr, vec2<i32>(pos.xy), 0); var rgb = vec3<f32>(0.0); if (px.a > 0.0) { rgb = max(px.rgb / px.a, vec3<f32>(0.0)); } rgb = rgb * tone.params.x; let mode = u32(tone.params.y); if (mode == 1u) { rgb = neutral(rgb); } else if (mode == 2u) { // The fit is made for an exposure that is 1/0.6 of the one of the other curves. rgb = aces(rgb / 0.6); } rgb = linear_to_srgb(clamp(rgb, vec3<f32>(0.0), vec3<f32>(1.0))); return vec4<f32>(rgb * px.a, px.a); } `, SPIRV: spirvTonemap, HLSL: `struct Tone { float4 params; }; cbuffer tone : register(b0, space0) { Tone tone; } Texture2D<float4> hdr : register(t1, space0); struct FragmentInput_fs_tonemap { float4 pos_1 : SV_Position; }; float3 neutral(float3 color) { float offset = 0.04; float3 c = (float3)0; float x = min(color.x, min(color.y, color.z)); if ((x < 0.08)) { offset = (x - ((6.25 * x) * x)); } float _e16 = offset; c = (color - (_e16).xxx); float _e21 = c.x; float _e23 = c.y; float _e25 = c.z; float peak = max(_e21, max(_e23, _e25)); if ((peak < 0.76)) { float3 _e29 = c; return _e29; } float d = (1.0 - 0.76); float new_peak = (1.0 - ((d * d) / ((peak + d) - 0.76))); float3 _e38 = c; c = (_e38 * (new_peak / peak)); float g = (1.0 - (1.0 / ((0.15 * (peak - new_peak)) + 1.0))); float3 _e49 = c; return lerp(_e49, (new_peak).xxx, g); } float3 rrt_and_odt_fit(float3 v) { float3 a = ((v * (v + (0.0245786).xxx)) - (9.0537e-5).xxx); float3 b = ((v * ((0.983729 * v) + (0.432951).xxx)) + (0.238081).xxx); return (a / b); } float3 aces(float3 color_1) { float3x3 input_mat = float3x3(float3(0.59719, 0.076, 0.0284), float3(0.35458, 0.90834, 0.13383), float3(0.04823, 0.01566, 0.83777)); float3x3 output_mat = float3x3(float3(1.60475, -0.10208, -0.00327), float3(-0.53108, 1.10813, -0.07276), float3(-0.07367, -0.00605, 1.07602)); const float3 _e28 = rrt_and_odt_fit(mul(color_1, input_mat)); return clamp(mul(_e28, output_mat), (0.0).xxx, (1.0).xxx); } float3 linear_to_srgb(float3 c_1) { float3 lo = (c_1 * 12.92); float3 hi = ((1.055 * pow(c_1, (0.41666666).xxx)) - (0.055).xxx); return ((c_1 <= (0.0031308).xxx) ? lo : hi); } float4 vs_full(uint i : SV_VertexID) : SV_Position { float x_1 = float(((i << 1u) & 2u)); float y = float((i & 2u)); return float4(((x_1 * 2.0) - 1.0), (1.0 - (y * 2.0)), 0.0, 1.0); } int2 naga_f2i32(float2 value) { return int2(clamp(value, -2147483600.0, 2147483500.0)); } uint naga_f2u32(float value) { return uint(clamp(value, 0.0, 4294967000.0)); } float4 fs_tonemap(FragmentInput_fs_tonemap fragmentinput_fs_tonemap) : SV_Target0 { float4 pos = fragmentinput_fs_tonemap.pos_1; float3 rgb = (0.0).xxx; float4 px = hdr.Load(int3(naga_f2i32(pos.xy), int(0))); if ((px.w > 0.0)) { rgb = max((px.xyz / (px.w).xxx), (0.0).xxx); } float3 _e19 = rgb; float _e23 = tone.params.x; rgb = (_e19 * _e23); float _e28 = tone.params.y; uint mode = naga_f2u32(_e28); if ((mode == 1u)) { float3 _e32 = rgb; const float3 _e33 = neutral(_e32); rgb = _e33; } else { if ((mode == 2u)) { float3 _e36 = rgb; const float3 _e40 = aces((_e36 / (0.6).xxx)); rgb = _e40; } } float3 _e41 = rgb; const float3 _e47 = linear_to_srgb(clamp(_e41, (0.0).xxx, (1.0).xxx)); rgb = _e47; float3 _e48 = rgb; return float4((_e48 * px.w), px.w); } `, MSL: `// language: metal2.4 #include <metal_stdlib> #include <simd/simd.h> using metal::uint; struct Tone { metal::float4 params; }; metal::float3 neutral( metal::float3 color ) { float offset = 0.04; metal::float3 c = {}; float x = metal::min(color.x, metal::min(color.y, color.z)); if (x < 0.08) { offset = x - ((6.25 * x) * x); } float _e16 = offset; c = color - metal::float3(_e16); float _e21 = c.x; float _e23 = c.y; float _e25 = c.z; float peak = metal::max(_e21, metal::max(_e23, _e25)); if (peak < 0.76) { metal::float3 _e29 = c; return _e29; } float d = 1.0 - 0.76; float new_peak = 1.0 - ((d * d) / ((peak + d) - 0.76)); metal::float3 _e38 = c; c = _e38 * (new_peak / peak); float g = 1.0 - (1.0 / ((0.15 * (peak - new_peak)) + 1.0)); metal::float3 _e49 = c; return metal::mix(_e49, metal::float3(new_peak), g); } metal::float3 rrt_and_odt_fit( metal::float3 v ) { metal::float3 a = (v * (v + metal::float3(0.0245786))) - metal::float3(0.000090537); metal::float3 b = (v * ((0.983729 * v) + metal::float3(0.432951))) + metal::float3(0.238081); return a / b; } metal::float3 aces( metal::float3 color_1 ) { metal::float3x3 input_mat = metal::float3x3(metal::float3(0.59719, 0.076, 0.0284), metal::float3(0.35458, 0.90834, 0.13383), metal::float3(0.04823, 0.01566, 0.83777)); metal::float3x3 output_mat = metal::float3x3(metal::float3(1.60475, -0.10208, -0.00327), metal::float3(-0.53108, 1.10813, -0.07276), metal::float3(-0.07367, -0.00605, 1.07602)); metal::float3 _e28 = rrt_and_odt_fit(input_mat * color_1); return metal::clamp(output_mat * _e28, metal::float3(0.0), metal::float3(1.0)); } metal::float3 linear_to_srgb( metal::float3 c_1 ) { metal::float3 lo = c_1 * 12.92; metal::float3 hi = (1.055 * metal::pow(c_1, metal::float3(0.41666666))) - metal::float3(0.055); return metal::select(hi, lo, c_1 <= metal::float3(0.0031308)); } struct vs_fullInput { }; struct vs_fullOutput { metal::float4 member [[position]]; }; vertex vs_fullOutput vs_full( uint i [[vertex_id]] ) { float x_1 = static_cast<float>((i << 1u) & 2u); float y = static_cast<float>(i & 2u); return vs_fullOutput { metal::float4((x_1 * 2.0) - 1.0, 1.0 - (y * 2.0), 0.0, 1.0) }; } metal::int2 naga_f2i32(metal::float2 value) { return static_cast<metal::int2>(metal::clamp(value, -2147483600.0, 2147483500.0)); } uint naga_f2u32(float value) { return static_cast<uint>(metal::clamp(value, 0.0, 4294967000.0)); } struct fs_tonemapInput { }; struct fs_tonemapOutput { metal::float4 member_1 [[color(0)]]; }; fragment fs_tonemapOutput fs_tonemap( metal::float4 pos [[position]] , constant Tone& tone [[buffer(0)]] , metal::texture2d<float, metal::access::sample> hdr [[texture(0)]] ) { metal::float3 rgb = metal::float3(0.0); metal::float4 px = hdr.read(metal::uint2(naga_f2i32(pos.xy)), 0); if (px.w > 0.0) { rgb = metal::max(px.xyz / metal::float3(px.w), metal::float3(0.0)); } metal::float3 _e19 = rgb; float _e23 = tone.params.x; rgb = _e19 * _e23; float _e28 = tone.params.y; uint mode = naga_f2u32(_e28); if (mode == 1u) { metal::float3 _e32 = rgb; metal::float3 _e33 = neutral(_e32); rgb = _e33; } else { if (mode == 2u) { metal::float3 _e36 = rgb; metal::float3 _e40 = aces(_e36 / metal::float3(0.6)); rgb = _e40; } } metal::float3 _e41 = rgb; metal::float3 _e47 = linear_to_srgb(metal::clamp(_e41, metal::float3(0.0), metal::float3(1.0))); rgb = _e47; metal::float3 _e48 = rgb; return fs_tonemapOutput { metal::float4(_e48 * px.w, px.w) }; } `, Entries: []gpu.EntryPoint{ {Name: "vs_full", Stage: gpu.ShaderStageVertex, ReadsVertexIndex: true, Bindings: []gpu.ShaderBinding{}, }, {Name: "fs_tonemap", Stage: gpu.ShaderStageFragment, Bindings: []gpu.ShaderBinding{ {Name: "tone", Group: 0, Binding: 0, Kind: gpu.BindingUniformBuffer, Size: 16, Register: gpu.ShaderRegister{Class: "b", Space: 0, Index: 0}, MetalIndex: 0}, {Name: "hdr", Group: 0, Binding: 1, Kind: gpu.BindingSampledTexture, Sample: gpu.SampleTypeUnfilterableFloat, Register: gpu.ShaderRegister{Class: "t", Space: 0, Index: 1}, MetalIndex: 0}, }, }, }, }
Tonemap is tonemap.wgsl.
Functions ¶
This section is empty.
Types ¶
This section is empty.