Symbols in this topic
Device
Device is the core wrapper around a raw GPUDevice. Use it when you need explicit low-level resource creation (Buffer, Texture, Shader), queue access, readback, and structured WebGPU error scopes.
Import
TypeScript
1
import { Device } from "vgpu/core";Signature
TypeScript
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
import type { Buffer, BufferOptions, Queue, Shader, ShaderInput, Texture, TextureOptions, VGPUError } from "vgpu/core";
interface DeviceOptions {
readonly isCompatibilityMode?: boolean;
}
declare class Device {
readonly gpu: GPUDevice;
readonly adapterInfo: GPUAdapterInfo | null;
readonly queue: Queue;
readonly isCompatibilityMode: boolean;
constructor(gpu: GPUDevice, adapterInfo?: GPUAdapterInfo | null, opts?: DeviceOptions);
get limits(): GPUSupportedLimits;
get features(): GPUSupportedFeatures;
createShader(input: ShaderInput): Shader;
createTexture(opts: TextureOptions): Texture;
createBuffer(opts: BufferOptions): Buffer;
pushErrorScope(filter: GPUErrorFilter): void;
popErrorScope(): Promise<VGPUError | null>;
destroy(): void;
dispose(): void;
}Parameters
Constructor
| Param | Type | Required | Default | Notes |
|---|---|---|---|---|
| gpu | GPUDevice | ✔ | — | Raw WebGPU device. Device does not request adapters itself. |
| adapterInfo | GPUAdapterInfo | null | ✖ | null | Stored as device.adapterInfo; pass adapter metadata when an adapter provides it. |
| opts | DeviceOptions | ✖ | {} | Core-only options for wrapper behavior. |
| opts.isCompatibilityMode | boolean | ✖ | false | Stored as device.isCompatibilityMode; adapters set it when they requested WebGPU featureLevel: "compatibility". |
createShader(input)
| Param | Type | Required | Default | Notes |
|---|---|---|---|---|
| input | ShaderInput | ✔ | — | A WGSL string or a resolved shader object with .wgsl. Strings are compiled with @vgpu/wgsl before creating the native shader module. |
createTexture(opts)
| Param | Type | Required | Default | Notes |
|---|---|---|---|---|
| opts | TextureOptions | ✔ | — | Descriptor-first texture options; see TextureOptions rows in Texture. |
createBuffer(opts)
| Param | Type | Required | Default | Notes |
|---|---|---|---|---|
| opts | BufferOptions | ✔ | — | Descriptor-first buffer options; see BufferOptions rows in Buffer. |
Error scopes and teardown
| Param | Type | Required | Default | Notes |
|---|---|---|---|---|
| filter | GPUErrorFilter | ✔ | — | Passed to gpu.pushErrorScope(filter) and also starts a vgpu structured-error scope. |
Returns:
new Device(...)returns a wrapper with.gpu,.queue,.limits,.features, and resource factory methods.createShader(input)returnsShader.createTexture(opts)returnsTexture.createBuffer(opts)returnsBuffer.pushErrorScope(filter),destroy(), anddispose()returnvoid.popErrorScope()returnsPromise<VGPUError | null>; the first captured vgpu error wins, otherwise a nativeGPUErroris converted toVGPU-CORE-VALIDATION, otherwisenull.
Throws:
VGPU-CORE-INVALID-USAGEwhencreateBuffer({ size })receives a non-finite size,size <= 0, or an emptyusagearray — pass a positive byte size and at least one buffer usage.VGPU-CORE-VALIDATIONcan be returned frompopErrorScope()when the native WebGPU scope reports aGPUError— inspect.messageand fix the invalid WebGPU descriptor or command.- Native WebGPU errors may be thrown by
gpu.createShaderModule,gpu.createTexture,gpu.pushErrorScope,gpu.popErrorScope, orgpu.destroy; use error scopes around native validation-sensitive calls.
Examples
TypeScript
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
import { createMockAdapter } from "vgpu/mock";
const device = await createMockAdapter().requestDevice();
const buffer = device.createBuffer({
label: "positions",
size: 16,
usage: ["vertex", "copy_dst", "copy_src"],
});
buffer.write(new Float32Array([0, 1, 2, 3]));
const bytes = await buffer.read(16);
console.log(bytes.byteLength);
device.destroy();TypeScript
1
2
3
4
5
6
7
8
9
10
import { createMockAdapter } from "vgpu/mock";
const device = await createMockAdapter().requestDevice();
device.pushErrorScope("validation");
const badBuffer = device.createBuffer({ size: 0, usage: ["copy_dst"] });
const error = await device.popErrorScope();
console.log(badBuffer.options.size, error?.code); // "VGPU-CORE-INVALID-USAGE"
device.dispose();TypeScript
1
2
3
4
5
6
7
8
9
10
11
12
import { createMockAdapter } from "vgpu/mock";
const device = await createMockAdapter().requestDevice();
if (device.features.has("timestamp-query")) {
console.log("timestamp queries are available");
}
console.log(device.limits.maxTextureDimension2D);
console.log(device.isCompatibilityMode);
device.destroy();Notes
Deviceis intentionally low-level: it does not infer buffer/texture usage from shaders or pipeline state. Provide explicit descriptors.- Prefer
device.createBuffer(...)anddevice.createTexture(...)over raw.gpucreation when you want vgpu wrappers, readback, lifecycle callbacks, or structured core errors. destroy()is idempotent anddispose()is an alias. Do not calldevice.gpu.destroy()directly unless you intentionally bypass vgpu lifecycle.createBufferthrows immediately without an error scope, but captures into the current vgpu error scope when one is active.isCompatibilityModeis only a signal set by the adapter; keep compatibility-specific texture views and WGSL bindings in lockstep yourself.- See also:
Buffer,Texture,Queue,VGPUError,VGPUAdapter.