resource
On this page

resource()

Function in Layout

Declare a bound resource that is not a struct: a texture or a sampler.

import { resource } from 'typeshade'

The signature, the description and the examples come from the compiler's own source at commit 26de7be8.

Syntax

function resource<T extends ShaderType>(
name: string,
type: T,
at: { group: number; binding: number; space?: AddressSpace },
): Resource<T>

Parameters

name string

the emitted binding name, which is also the name a host binds by.

type T extends ShaderType

the resource’s type, such as texture2dfT, texture2dArrayfT or samplerT.

at { group: number; binding: number; space?: AddressSpace }

the group and binding slot, and optionally the address space.

Return value

Resource<T>

the Resource handle, .binding and .node.

Description

The binding declaration and the access node come from this one call.

.node keeps the specific key of the type you passed: Node<'texture_2d<f32>'>, Node<'sampler'>, Node<'texture_2d_array<u32>'>. That is what makes the texture calls type-checked at the authoring line: textureSample takes a sampled 2D texture and a sampler in that order, so swapping them is a tsc error, an integer texture is rejected where filtering has no meaning, and an array texture requires its layer argument.

.binding goes in module({ bindings }), or pass the handle in uses. The address space defaults to 'uniform', the texture and sampler convention.

Examples

Example

import { resource, textureSample, texture2dfT, samplerT } from 'typeshade'
const tex = resource('tex', texture2dfT, { group: 0, binding: 1 })
const smp = resource('tex_sampler', samplerT, { group: 0, binding: 2 })
const c = textureSample(tex.node, smp.node, uv)

In the guide

See also

Source

src/core/sot.ts, line 848, at commit 26de7be8

Edit this page Report a problem