Add shader compiler for WebGPU

Use existing Vulkan compiler to generate SPIR-V and convert it into WGSL with tint compiler
https://github.com/google/dawn/releases/tag/v20260219.200501
This commit is contained in:
Wojtek Figat
2026-02-24 17:55:26 +01:00
parent 9158e1c270
commit 9be8589437
17 changed files with 337 additions and 50 deletions

View File

@@ -0,0 +1,57 @@
// Copyright (c) Wojciech Figat. All rights reserved.
using System.IO;
using Flax.Build;
using Flax.Build.NativeCpp;
/// <summary>
/// WebGPU shaders compiler module.
/// </summary>
public class ShaderCompilerWebGPU : ShaderCompiler
{
public static bool Use(BuildOptions options)
{
// Requires prebuilt tint executable in platform ThirdParty binaries folder
// https://github.com/google/dawn/releases
// License: Source/ThirdParty/tint-license.txt (BSD 3-Clause)
switch (options.Platform.Target)
{
case TargetPlatform.Windows:
return options.Architecture == TargetArchitecture.x64;
case TargetPlatform.Linux: // TODO: add Linux binary with tint
case TargetPlatform.Mac:
return options.Architecture == TargetArchitecture.ARM64;
default:
return false;
}
}
/// <inheritdoc />
public override void Setup(BuildOptions options)
{
base.Setup(options);
options.PublicDefinitions.Add("COMPILE_WITH_WEBGPU_SHADER_COMPILER");
options.PublicDependencies.Add("ShaderCompilerVulkan");
// Deploy tint executable as a dependency for the shader compilation from SPIR-V into WGSL
// Tint compiler from: https://github.com/google/dawn/releases
// License: Source/ThirdParty/tint-license.txt (BSD 3-Clause)
var depsRoot = options.DepsFolder;
switch (options.Platform.Target)
{
case TargetPlatform.Windows:
if (options.Architecture == TargetArchitecture.x64)
options.DependencyFiles.Add(Path.Combine(depsRoot, "tint.exe"));
break;
case TargetPlatform.Linux:
if (options.Architecture == TargetArchitecture.x64)
options.DependencyFiles.Add(Path.Combine(depsRoot, "tint"));
break;
case TargetPlatform.Mac:
if (options.Architecture == TargetArchitecture.ARM64)
options.DependencyFiles.Add(Path.Combine(depsRoot, "tint"));
break;
}
}
}