shaders

package
v1.11.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 7, 2026 License: BSD-3-Clause Imports: 2 Imported by: 1 module, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

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);
}
`,
	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}},
			},
		},
		{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}},
			},
		},
	},
}

Cube is cube.wgsl.

Functions

This section is empty.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL