Chromium Experimental Barycentric Coordinates

The chromium_experimental_barycentric_coord is an experimental extension that allows the use of the barycentric_coord builtin in fragment shaders in WGSL.

Status

This extension is experimental and the syntax is being discussed. No official WebGPU specification has been written yet.

Availability

The usage is restricted to fragment shaders.

BackendTypeRequirements
SPIR-Vvec3fVK_KHR_fragment_shader_barycentric. Available as the BaryCoordKHR builtin
HLSLvec3fRequires D3D12, HLSL 6.1. Available as the SV_Barycentrics semantic
GLSLvec3fGLSL_EXT_fragment_shader_barycentric (Requires GLSL 4.50 and later, ESSL 3.2 and later). Available as the gl_BaryCoordEXT builtin
Metalf32, vec2f, or vec3fRequires Metal 2.2 on MacOS or Metal 2.3 on iOS. Available as [[barycentric_coord]], can test with supportsShaderBarycentricCoordinates

Vulkan support unfortunately looks pretty low with 10.76% of devices on GPUInfo.org reporting support. The relevant GL extension doesn't even appear on the site.

Due to the limited availability, this will need a enable statement to be used. For this experimental extension it would be enable chromium_experimental_barycentric_coord.

Additionally, anywhere that barycentrics are supported they support perspective corrected and non-perspective correct values are available. Most backends support multiple interpolation modes, but only centroid is available everywhere.

BackendSupported Interpolation Modes
SPIR-VBaryCoordKHR, BaryCoordNoPerspKHR, both can be decorated with the Centroid or Sample interpolation qualifiers
HLSLfloat3 b : SV_Barycentrics (default, perspective correct), noperspective float3 b : SV_Barycentrics. Interpolation modes like linear, centroid, and sample are supported
GLSLgl_BaryCoordEXT, gl_BaryCoordNoPerspEXT, both can be decorated with the centroid or sample interpolation qualifiers
Metal[[barycentric_coord, center_perspective]] (default, can be omitted), [[barycentric_coord, center_no_perspective]]

Specification

This extension adds a new builtin_value_name entry for barycentric_coord.

An entry is added to the Built-in input and output values table:

CategoryValue
Namebarycentric_coord
Stagefragment
Directioninput
Typevec3
Extensionchromium_experimental_barycentric_coord
  • The value contains the relative weights for each vertex in the primitive.
  • Fragments in the corners of triangle primitives will have weights of (1,0,0), (0,1,0), and (0,0,1)
  • Fragments at the endpoints of line primitives will have weights of (1,0,0), and (0,1,0). The z component will always be 0.
  • Support for point primitives is unclear (Metal mentions that they will have yz components of (0, 0), HLSL and GLSL make no mention of points.)
  • The value of the three components should add up to approximately 1, but strictly adding up to 1 is not guaranteed.

Example usage

@fragment fn fs_main(@builtin(barycentric_coord) bary_coord: vec3f) -> @builtin(color) vec4f {
    return vec4f(bary_coord, 1.0);
}

References