Capabilities & extensions
On this page

Rendered from AUTHORING.md at commit af54ddad5. The package is imported here by its 0.2.0 name, typeshade.

Capabilities & extensions

After this page you can declare the GPU features a module needs, read what each one costs on each target, check a booted device against them, and tell which module shapes fail closed instead of emitting.

A capability is a neutral id for a GPU feature a module’s emit depends on, such as rendering into a float texture or blending into one.

Declaring a capability

Most capabilities declare themselves. Writing builtin<"clip_distances", T>, builtin<"primitive_index">, a subgroup built-in value or a blend_src option derives the capability the use needs, and so does a storage binding, a compute entry or a texture of a kind one target lacks. An extension no use derives is turned on by a string directive beside "use typeshade", one per line, with WGSL’s own name:

"use typeshade";
"enable subgroups";
@compute([64])
export function k(gid: builtin<"global_invocation_id">) {
}

The vocabulary is fixed and target-neutral, so a raw EXT_* or OVR_* string never appears in a source. Each id folds into the capability gate that runs before any writer touches the module, so a backend that cannot spell the feature throws UnsupportedFeatureError (SD0030) naming the capability, and no source the driver would reject is ever produced.

Four capabilities have no spelling in a source: floatRenderTarget, float32Blend, float32Filterable and multiview. Three are activated by the host and cost the shader no token, and the fourth is a GLSL #extension. A host that needs one lists it in the compiled module’s enables:

import { compile, type ModuleDecl } from 'typeshade'
const m: ModuleDecl = { ...compile(source).module, enables: ['floatRenderTarget'] }

What each id needs per target

Two different costs hide behind one id. A host feature is something the host must activate before it creates a pipeline: gl.getExtension('EXT_color_buffer_float') on WebGL2, a requiredFeatures entry on WebGPU. A source directive is a token the emitted shader itself must carry, #extension … : require on GLSL ES 3.00 and enable …; on WGSL, which the backend writes for you, deduped and sorted, right after the #version line on GLSL and ahead of the declarations on WGSL.

A capability can need either half, both halves, or neither.

enables idWebGL2 and GLSL ES 3.00WebGPU and WGSL
floatRenderTargethost feature EXT_color_buffer_floatcore, nothing to request
float32Blendhost feature EXT_float_blendhost feature float32-blendable
float32Filterablehost feature OES_texture_float_linearhost feature float32-filterable
multiviewdirective GL_OVR_multiview2 and host feature OVR_multiview2unsupported, fails closed
f16not authorable yet (Rule 4.7), fails closed (Rule 10.3)directive f16 and host feature shader-f16
subgroupsone invocation per subgroup (Rule 11.21), nothing to requestdirective subgroups and host feature subgroups
clipDistancesnot supported yet, fails closed (Rule 10.3)directive clip_distances and host feature clip-distances
primitiveIndexnot supported yet, fails closed (Rule 10.3)directive primitive_index and host feature primitive-index
dualSourceBlendingnot supported yet, fails closed (Rule 10.3)directive dual_source_blending and host feature dual-source-blending
bgra8unormStoragenot supported yet, fails closed (Rule 10.3)host feature bgra8unorm-storage, derived
packed4x8Dotnot supported yet, fails closed (Rule 10.3)core, derived; a language feature to check

The bottom six rows are the ones nothing declares by hand. Writing builtin<"clip_distances", T>, builtin<"primitive_index">, builtin<"subgroup_id"> or a blend_src option derives the capability, because WGSL refuses each of those without the matching enable; a "bgra8unorm" storage texture, a call into the packed 4x8 family and a call to a subgroup built-in function (subgroupAdd(x), change 0117) derive theirs from the binding and from the call. f16, which no use can derive, is spelled as the string directive above, and so is subgroups for a file that reads no subgroup built-in value and calls no subgroup function.

A capability with a host half and no source half costs zero emitted bytes: declaring it moves no byte of the shader. The 32 in float32Blend and float32Filterable is load-bearing, because both underlying features are 32-bit float only, while EXT_color_buffer_float covers 16-bit and 32-bit targets, which is why floatRenderTarget carries no bit width.

capabilityMatrix reports the same information as data, derived from the backends themselves, so a tool or a page can print it without transcribing it:

import { capabilityMatrix, glslEs300Backend, wgslBackend } from 'typeshade'
capabilityMatrix([wgslBackend, glslEs300Backend])
// → [{ capability: 'storageBuffer', support: { wgsl: 'native', 'glsl-es300': 'unsupported' },
// declarable: false },
// …,
// { capability: 'f16', support: { wgsl: 'directive', 'glsl-es300': 'unsupported' },
// declarable: true }]

The result has one row per capability, eighteen in a fixed order, including the nine a module’s shape derives and never declares (storageBuffer through textureGather, then bgra8unormStorage and packed4x8Dot), which come back with declarable: false.

Two notes before you trust a row.

  • Support is not the same as reachability. f16 and multiview are supported on the target the table says and neither is authorable today, because there is no f16 scalar type and no way to spell layout(num_views = N) in; or read gl_ViewID_OVR. A module declaring multiview emits the directive and renders single-view. The other four are reachable: clipDistances, primitiveIndex and dualSourceBlending are what their attributes need, and subgroups is reached by builtin<"subgroup_invocation_id"> or builtin<"subgroup_size"> on a compute or fragment entry, by builtin<"subgroup_id"> or builtin<"num_subgroups"> on a compute entry, and by a call to one of the 21 subgroup built-in functions (subgroupAdd and the rest, change 0117). On GLSL ES 3.00 it costs nothing: a subgroup there is one invocation (Rule 11.21), so a fold is the lane’s own value and subgroup_size is 1u. Whether a given adapter HAS one of the four is what reflect().requiredFeatures is for, and a device only HAS an optional feature if requestDevice was asked for it — the compile gate does exactly that, deriving the list from the corpus, which is how examples/gallery/clip-planes/clip-planes.shade.ts compiles on its Tint. Its software adapter offers clip-distances and subgroups but not primitive-index, so that one row’s host string is unconfirmed here.
  • An unsupported cell is a hard stop by design: the emit throws. To ask before you emit, diagnose(m, { backend }) reports the same missing capability as an SD0030 diagnostic and never throws.

Derived and implied capabilities

Some capabilities are derived, which means they are read off the module’s shape and never declared. A storage binding implies storageBuffer, a compute entry implies compute, a multisampled texture load implies msaaTextureLoad, a storage-texture binding implies storageTexture, a 1D texture implies texture1d, a cube-array texture implies textureCubeArray, a textureGather call implies textureGather, and a call to one of the eight packed 4x8 integer builtins implies packed4x8Dot. bgra8unormStorage is derived from a binding’s FORMAT rather than its kind: bgra8unorm is the one storage format that is not core, and a device refuses the bind group layout unless it requested bgra8unorm-storage (measured — and Tint compiles the module either way, so nothing but this capability carries the requirement to the host). A module’s enables is typed to exclude every derived id, so naming one is a type error.

A capability is not the only thing a host may have to check. A WGSL language feature is a property of the browser’s shading-language implementation rather than of the device, so it is not requested at requestDevice at all. reflect().requiredLanguageFeatures lists the ones a module’s source uses, for navigator.gpu.wgslLanguageFeatures to answer. The WGSL writer emits requires readonly_and_readwrite_storage_textures; for a storage texture bound read or read_write, and no directive for the packed 4x8 family, which compiles without one.

One capability can also imply another. float32Blend pulls in floatRenderTarget, because blending into a float target needs that target to be renderable as a colour attachment first, and with only EXT_float_blend active the framebuffer comes back incomplete. reflect().requiredFeatures reports the closure, so a module that declares one gets both:

const m: ModuleDecl = { ...compile(source).module, enables: ['float32Blend'] }
reflect(m).requiredFeatures // ['float32Blend', 'floatRenderTarget']

The list is always present, and empty for a module that needs nothing.

Verifying at boot

Declaring a capability activates nothing. Device features are fixed when the device is created, well before a module is emitted: WebGPU’s requiredFeatures are settled at requestDevice and a feature missing there can never be added later, and a WebGL2 context has whatever extensions were fetched on it. Asking at pipeline-creation time is too late.

So the author’s job is to verify that the booted device covers what the module needs, and to fail loudly when it does not. reflect().requiredFeatures gives neutral ids, because reflection takes a module and knows no target, so translate them through hostFeaturesFor, which returns the concrete strings one backend’s host needs. It skips every capability with no host half, so there are no holes to hand a driver:

import { hostFeaturesFor, reflect, glslEs300Backend, wgslBackend } from 'typeshade'
// WebGL2: verify the already-booted context has each extension.
for (const ext of hostFeaturesFor(glslEs300Backend, reflect(m).requiredFeatures)) {
if (!gl.getExtension(ext)) throw new Error(`WebGL2 lacks ${ext}`)
}
// WebGPU: feed the same lookup into requestDevice, at boot.
const device = await adapter.requestDevice({
requiredFeatures: hostFeaturesFor(wgslBackend, reflect(m).requiredFeatures) as GPUFeatureName[],
})

typeshade/runtime does the WebGPU half for you: a runtime that requests its own device asks for the features its programs need. For kernel-call owners, pass instance requirements to createDevice({ limits, features }). A supplied device must already enable them. Constructor options keep independent libraries from depending on which default-owner call runs first.

Choosing between two modules

A capability states a hard requirement, so it is the wrong tool for a feature you can live without. A fallback is two modules and one decision, made at boot where the device is already known:

const caps = reflect(fancy).requiredFeatures
const ok = hostFeaturesFor(glslEs300Backend, caps).every((e) => gl.getExtension(e))
const m = ok ? fancy : plain // two modules, one decision, made once

Each module still declares what it needs, so the one you did not pick would have failed closed if you had picked it on a device that cannot run it.

Portable compute kernels

A "use typeshade" compute entry runs on both backends. On WebGPU it is a compute entry point. On WebGL2, which has no compute stage, it runs as a pass program: a full-screen draw per pass into a texture, which the compiler writes and the manifest carries (change 0054). The program runtime chooses the tier, so the host code is the same on both.

"use typeshade";
declare const field: storage<array<f32>>;
declare const outColor: storage<array<u32>, "read_write">;
@compute([64])
export function evalField(gid: builtin<"global_invocation_id">): void {
const i = gid.x;
if (i >= field.length) {
return;
}
outColor[i] = pack4x8unorm(vec4(field[i], 0., 0., 1.));
}
import { compile, packModule } from 'typeshade'
const program = packModule(compile(source).module)
program.gl?.computes // { evalField: … }, the pass program WebGL2 runs

program.gl.computes holds each compute entry’s pass program, or the reason it has none. typeshade/runtime runs it with rt.load(program).compute('evalField') on whichever tier the device is (docs/use-typeshade-surface.md §69). Barriers, workgroup memory and atomics run in the WebGL2 tier’s passes too, in an order WGSL allows (changes/0054-webgl2-compute.md). An entry with no pass program, one that reads a 1D, cube-array or multisampled texture for instance, is refused by the runtime with its reason. The IR builder also has a narrower portable tier, a gather-only kernel the GLSL writer lowers to one fragment shader, which docs/ir-builder.md describes.

Edit this page Report a problem