MSL syntax
Teams coming from the Metal Shading Language. Inspired by the Metal Shading Language.
Why choose it
MSL attribute names (position, vertex_id, thread_position_in_grid) and the argument-index style resources buffer(n), texture(n) and sampler(n).
Address-space style resources are constant and device; workgroup size is exposed as the built-in threads_per_threadgroup rather than a method attribute.
The same shader
using YotsubaShader.Language;
using YotsubaShader.MslSyntax;
using YotsubaShader.MslSyntax.Resources;
public struct VertexOutput
{
[position]
public Vector4 Position;
}
public class Triangle
{
[device, buffer(0)]
public float[] Output;
[ShaderStage(Stage.Vertex)]
public VertexOutput Vertex([vertex_id] uint id)
{
VertexOutput output = new VertexOutput();
output.Position = new Vector4(0, 0, 0, 1);
return output;
}
[ShaderStage(Stage.Fragment)]
public Vector4 Fragment(VertexOutput input)
{
return new Vector4(1, 0.4f, 0.7f, 1);
}
// Workgroup size is set by the host
[ShaderStage(Stage.Compute)]
public void Compute([thread_position_in_grid] UInt3 id)
{
}
}
Common built-ins
using YotsubaShader.Language;
using YotsubaShader.MslSyntax;
public struct VertexOutput
{
// Clip-space position (vertex output)
[position]
public Vector4 Position;
}
public class BuiltIns
{
[ShaderStage(Stage.Vertex)]
public VertexOutput Vertex(
// Vertex index
[vertex_id] uint vertex,
// Instance index
[instance_id] uint instance)
{
VertexOutput output = new VertexOutput();
output.Position = new Vector4(vertex, instance, 0, 1);
return output;
}
[ShaderStage(Stage.Fragment)]
public Vector4 Fragment(
// Fragment coordinate
[position] Vector4 coord)
{
return new Vector4(coord.X, coord.Y, 0, 1);
}
[ShaderStage(Stage.Compute)]
// No method attribute: the workgroup size is a built-in value
public void Compute(
// Global invocation id
[thread_position_in_grid] uint id)
{
uint index = id;
}
}
Resources
using YotsubaShader.Language;
using YotsubaShader.MslSyntax;
using YotsubaShader.MslSyntax.Resources;
public struct Material
{
public float Exposure;
}
public class Resources
{
// Uniform buffer, with its binding slot
[constant, buffer(0)]
public Material Material;
// Read/write storage buffer
[device, buffer(1)]
public float[] Output;
// Read-only storage buffer
[device(readOnly: true), buffer(2)]
public float[] Input;
}
Other syntaxes: HLSL syntax · GLSL syntax · SPIR-V syntax · WGSL syntax
