Documentation
¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
View Source
var Cube = &gpu.Shader{ Name: "cube", WGSL: `// The cube of examples/gpu-triangle: one directional light and Blinn-Phong shading. The matrices come from the // program (a column-major uniform), the vertices are a position and a normal. struct Scene { mvp: mat4x4<f32>, model: mat4x4<f32>, // light is the direction towards the light, in world space; eye is the camera's position. light: vec4<f32>, eye: vec4<f32>, color: vec4<f32>, } @group(0) @binding(0) var<uniform> scene: Scene; struct VertexIn { @location(0) position: vec3<f32>, @location(1) normal: vec3<f32>, } struct VertexOut { @builtin(position) clip: vec4<f32>, @location(0) world: vec3<f32>, @location(1) normal: vec3<f32>, @location(2) tint: vec3<f32>, } @vertex fn vs_main(v: VertexIn) -> VertexOut { var out: VertexOut; out.clip = scene.mvp * vec4<f32>(v.position, 1.0); out.world = (scene.model * vec4<f32>(v.position, 1.0)).xyz; out.normal = (scene.model * vec4<f32>(v.normal, 0.0)).xyz; // Each face leans towards its own axis color, so that the faces are told apart. out.tint = abs(v.normal); return out; } // to_srgb encodes a linear color: an RGBA8Unorm target holds sRGB-encoded values. fn to_srgb(c: vec3<f32>) -> vec3<f32> { let low = c * 12.92; let high = 1.055 * pow(c, vec3<f32>(1.0 / 2.4)) - vec3<f32>(0.055); return select(high, low, c <= vec3<f32>(0.0031308)); } @fragment fn fs_main(f: VertexOut) -> @location(0) vec4<f32> { let n = normalize(f.normal); let l = normalize(scene.light.xyz); let v = normalize(scene.eye.xyz - f.world); let h = normalize(l + v); let lit = max(dot(n, l), 0.0); let specular = pow(max(dot(n, h), 0.0), 64.0) * step(0.0001, lit); let base = mix(scene.color.rgb, f.tint, 0.5); let rgb = base * (0.10 + 0.90 * lit) + vec3<f32>(0.5 * specular); return vec4<f32>(to_srgb(rgb), 1.0); } `, SPIRV: spirvCube, HLSL: `struct Scene { row_major float4x4 mvp; row_major float4x4 model; float4 light; float4 eye; float4 color; }; struct VertexIn { float3 position : LOC0; float3 normal : LOC1; }; struct VertexOut { float4 clip_ : SV_Position; float3 world : LOC0; float3 normal : LOC1; float3 tint : LOC2; }; cbuffer scene : register(b0, space0) { Scene scene; } struct VertexOutput_vs_main { float3 world : LOC0; float3 normal : LOC1; float3 tint : LOC2; float4 clip_ : SV_Position; }; struct FragmentInput_fs_main { float3 world_1 : LOC0; float3 normal_1 : LOC1; float3 tint_1 : LOC2; float4 clip_1 : SV_Position; }; float3 to_srgb(float3 c) { float3 low = (c * 12.92); float3 high = ((1.055 * pow(c, (0.41666666).xxx)) - (0.055).xxx); return ((c <= (0.0031308).xxx) ? low : high); } VertexOutput_vs_main vs_main(VertexIn v) { VertexOut out_ = (VertexOut)0; float4x4 _e5 = scene.mvp; out_.clip_ = mul(float4(v.position, 1.0), _e5); float4x4 _e13 = scene.model; out_.world = mul(float4(v.position, 1.0), _e13).xyz; float4x4 _e22 = scene.model; out_.normal = mul(float4(v.normal, 0.0), _e22).xyz; out_.tint = abs(v.normal); VertexOut _e31 = out_; VertexOut vertexout = _e31; VertexOutput_vs_main vertexout_1 = { vertexout.world, vertexout.normal, vertexout.tint, vertexout.clip_ }; return vertexout_1; } float4 fs_main(FragmentInput_fs_main fragmentinput_fs_main) : SV_Target0 { VertexOut f = { fragmentinput_fs_main.clip_1, fragmentinput_fs_main.world_1, fragmentinput_fs_main.normal_1, fragmentinput_fs_main.tint_1 }; float3 n = normalize(f.normal); float4 _e5 = scene.light; float3 l = normalize(_e5.xyz); float4 _e10 = scene.eye; float3 v_1 = normalize((_e10.xyz - f.world)); float3 h = normalize((l + v_1)); float lit_ = max(dot(n, l), 0.0); float specular = (pow(max(dot(n, h), 0.0), 64.0) * step(0.0001, lit_)); float4 _e30 = scene.color; float3 base = lerp(_e30.xyz, f.tint, 0.5); float3 rgb = ((base * (0.1 + (0.9 * lit_))) + ((0.5 * specular)).xxx); const float3 _e44 = to_srgb(rgb); return float4(_e44, 1.0); } `, MSL: `// language: metal2.4 #include <metal_stdlib> #include <simd/simd.h> using metal::uint; struct Scene { metal::float4x4 mvp; metal::float4x4 model; metal::float4 light; metal::float4 eye; metal::float4 color; }; struct VertexIn { metal::float3 position; metal::float3 normal; }; struct VertexOut { metal::float4 clip; metal::float3 world; metal::float3 normal; metal::float3 tint; }; metal::float3 to_srgb( metal::float3 c ) { metal::float3 low = c * 12.92; metal::float3 high = (1.055 * metal::pow(c, metal::float3(0.41666666))) - metal::float3(0.055); return metal::select(high, low, c <= metal::float3(0.0031308)); } struct vs_mainInput { metal::float3 position [[attribute(0)]]; metal::float3 normal [[attribute(1)]]; }; struct vs_mainOutput { metal::float4 clip [[position]]; metal::float3 world [[user(loc0), center_perspective]]; metal::float3 normal [[user(loc1), center_perspective]]; metal::float3 tint [[user(loc2), center_perspective]]; }; vertex vs_mainOutput vs_main( vs_mainInput varyings [[stage_in]] , constant Scene& scene [[buffer(0)]] ) { const VertexIn v = { varyings.position, varyings.normal }; VertexOut out = {}; metal::float4x4 _e5 = scene.mvp; out.clip = _e5 * metal::float4(v.position, 1.0); metal::float4x4 _e13 = scene.model; out.world = (_e13 * metal::float4(v.position, 1.0)).xyz; metal::float4x4 _e22 = scene.model; out.normal = (_e22 * metal::float4(v.normal, 0.0)).xyz; out.tint = metal::abs(v.normal); VertexOut _e31 = out; const auto _tmp = _e31; return vs_mainOutput { _tmp.clip, _tmp.world, _tmp.normal, _tmp.tint }; } struct fs_mainInput { metal::float3 world [[user(loc0), center_perspective]]; metal::float3 normal [[user(loc1), center_perspective]]; metal::float3 tint [[user(loc2), center_perspective]]; }; struct fs_mainOutput { metal::float4 member_1 [[color(0)]]; }; fragment fs_mainOutput fs_main( fs_mainInput varyings_1 [[stage_in]] , metal::float4 clip [[position]] , constant Scene& scene [[buffer(0)]] ) { const VertexOut f = { clip, varyings_1.world, varyings_1.normal, varyings_1.tint }; metal::float3 n = metal::normalize(f.normal); metal::float4 _e5 = scene.light; metal::float3 l = metal::normalize(_e5.xyz); metal::float4 _e10 = scene.eye; metal::float3 v_1 = metal::normalize(_e10.xyz - f.world); metal::float3 h = metal::normalize(l + v_1); float lit = metal::max(metal::dot(n, l), 0.0); float specular = metal::pow(metal::max(metal::dot(n, h), 0.0), 64.0) * metal::step(0.0001, lit); metal::float4 _e30 = scene.color; metal::float3 base = metal::mix(_e30.xyz, f.tint, 0.5); metal::float3 rgb = (base * (0.1 + (0.9 * lit))) + metal::float3(0.5 * specular); metal::float3 _e44 = to_srgb(rgb); return fs_mainOutput { metal::float4(_e44, 1.0) }; } `, Entries: []gpu.EntryPoint{ {Name: "vs_main", Stage: gpu.ShaderStageVertex, Inputs: []gpu.VertexInput{ {Name: "position", Location: 0, Type: gpu.ShaderTypeFloat32x3}, {Name: "normal", Location: 1, Type: gpu.ShaderTypeFloat32x3}, }, Bindings: []gpu.ShaderBinding{ {Name: "scene", Group: 0, Binding: 0, Kind: gpu.BindingUniformBuffer, Size: 176, Register: gpu.ShaderRegister{Class: "b", Space: 0, Index: 0}, MetalIndex: 0}, }, }, {Name: "fs_main", Stage: gpu.ShaderStageFragment, Bindings: []gpu.ShaderBinding{ {Name: "scene", Group: 0, Binding: 0, Kind: gpu.BindingUniformBuffer, Size: 176, Register: gpu.ShaderRegister{Class: "b", Space: 0, Index: 0}, MetalIndex: 0}, }, }, }, }
Cube is cube.wgsl.
Functions ¶
This section is empty.
Types ¶
This section is empty.
Click to show internal directories.
Click to hide internal directories.