
Security News
Axios Supply Chain Attack Reaches OpenAI macOS Signing Pipeline, Forces Certificate Rotation
OpenAI rotated macOS signing certificates after a malicious Axios package reached its CI pipeline in a broader software supply chain attack.
wgpu-polyfill
Advanced tools
WebGPU polyfill for headless testing in Bun using wgpu-native. Enables GPU computing and rendering without a browser.
[!IMPORTANT] This polyfill is Bun-only. It uses Bun's native FFI to interface with wgpu-native and is not compatible with Node.js or browsers.
A WebGPU implementation for Bun using wgpu-native, enabling headless GPU computing and rendering.
bun add wgpu-polyfill
import { installPolyfill } from "wgpu-polyfill";
// Install the polyfill
installPolyfill();
// Use standard WebGPU API
const adapter = await navigator.gpu.requestAdapter();
const device = await adapter.requestDevice();
// Create a compute shader
const shader = device.createShaderModule({
code: `
@group(0) @binding(0) var<storage, read_write> data: array<f32>;
@compute @workgroup_size(64)
fn main(@builtin(global_invocation_id) gid: vec3u) {
data[gid.x] = data[gid.x] * 2.0;
}
`,
});
// ... rest of your WebGPU code
// Install polyfill to navigator.gpu
function installPolyfill(): GPU;
// Get GPU instance without installing to navigator
function getGPU(): GPU;
// Uninstall and cleanup
function uninstallPolyfill(): void;
// Clean up temporary buffers
function clearAllBuffers(): void;
// Constants
const GPUBufferUsage: { MAP_READ, MAP_WRITE, COPY_SRC, COPY_DST, INDEX, VERTEX, UNIFORM, STORAGE, INDIRECT, QUERY_RESOLVE };
const GPUTextureUsage: { COPY_SRC, COPY_DST, TEXTURE_BINDING, STORAGE_BINDING, RENDER_ATTACHMENT };
const GPUShaderStage: { VERTEX, FRAGMENT, COMPUTE };
const GPUMapMode: { READ, WRITE };
const GPUColorWrite: { RED, GREEN, BLUE, ALPHA, ALL };
import { installPolyfill, GPUBufferUsage } from "wgpu-polyfill";
installPolyfill();
const adapter = await navigator.gpu.requestAdapter();
const device = await adapter.requestDevice();
// Create buffers
const data = new Float32Array([1, 2, 3, 4]);
const gpuBuffer = device.createBuffer({
size: data.byteLength,
usage: GPUBufferUsage.STORAGE | GPUBufferUsage.COPY_SRC,
mappedAtCreation: true,
});
new Float32Array(gpuBuffer.getMappedRange()).set(data);
gpuBuffer.unmap();
// Create shader and pipeline
const shader = device.createShaderModule({
code: `
@group(0) @binding(0) var<storage, read_write> data: array<f32>;
@compute @workgroup_size(4)
fn main(@builtin(global_invocation_id) gid: vec3u) {
data[gid.x] *= 2.0;
}
`,
});
const pipeline = device.createComputePipeline({
layout: "auto",
compute: { module: shader, entryPoint: "main" },
});
const bindGroup = device.createBindGroup({
layout: pipeline.getBindGroupLayout(0),
entries: [{ binding: 0, resource: { buffer: gpuBuffer } }],
});
// Execute
const encoder = device.createCommandEncoder();
const pass = encoder.beginComputePass();
pass.setPipeline(pipeline);
pass.setBindGroup(0, bindGroup);
pass.dispatchWorkgroups(1);
pass.end();
device.queue.submit([encoder.finish()]);
import { installPolyfill, GPUTextureUsage, GPUBufferUsage } from "wgpu-polyfill";
installPolyfill();
const adapter = await navigator.gpu.requestAdapter();
const device = await adapter.requestDevice();
// Create render target
const texture = device.createTexture({
size: [256, 256],
format: "rgba8unorm",
usage: GPUTextureUsage.RENDER_ATTACHMENT | GPUTextureUsage.COPY_SRC,
});
// Create shader
const shader = device.createShaderModule({
code: `
@vertex
fn vs(@builtin(vertex_index) i: u32) -> @builtin(position) vec4f {
var pos = array<vec2f, 3>(
vec2f(0.0, 0.5),
vec2f(-0.5, -0.5),
vec2f(0.5, -0.5)
);
return vec4f(pos[i], 0.0, 1.0);
}
@fragment
fn fs() -> @location(0) vec4f {
return vec4f(1.0, 0.0, 0.0, 1.0);
}
`,
});
// Create pipeline
const pipeline = device.createRenderPipeline({
layout: "auto",
vertex: { module: shader, entryPoint: "vs" },
fragment: {
module: shader,
entryPoint: "fs",
targets: [{ format: "rgba8unorm" }],
},
});
// Render
const encoder = device.createCommandEncoder();
const pass = encoder.beginRenderPass({
colorAttachments: [{
view: texture.createView(),
loadOp: "clear",
storeOp: "store",
clearValue: { r: 0, g: 0, b: 0, a: 1 },
}],
});
pass.setPipeline(pipeline);
pass.draw(3);
pass.end();
device.queue.submit([encoder.finish()]);
// Create MSAA texture (4x samples)
const msaaTexture = device.createTexture({
size: [256, 256],
format: "rgba8unorm",
usage: GPUTextureUsage.RENDER_ATTACHMENT,
sampleCount: 4,
});
// Create resolve target
const resolveTexture = device.createTexture({
size: [256, 256],
format: "rgba8unorm",
usage: GPUTextureUsage.RENDER_ATTACHMENT | GPUTextureUsage.COPY_SRC,
});
// Create pipeline with multisample state
const pipeline = device.createRenderPipeline({
layout: "auto",
vertex: { module: shader, entryPoint: "vs" },
fragment: {
module: shader,
entryPoint: "fs",
targets: [{ format: "rgba8unorm" }],
},
multisample: { count: 4 },
});
// Render with resolve
const pass = encoder.beginRenderPass({
colorAttachments: [{
view: msaaTexture.createView(),
resolveTarget: resolveTexture.createView(),
loadOp: "clear",
storeOp: "store",
clearValue: { r: 0, g: 0, b: 0, a: 1 },
}],
});
device.pushErrorScope("validation");
// Do potentially invalid operations
const badBuffer = device.createBuffer({
size: 0, // Invalid!
usage: GPUBufferUsage.STORAGE,
});
const error = await device.popErrorScope();
if (error) {
console.error(`Validation error: ${error.message}`);
}
The polyfill bundles wgpu-native binaries for:
MIT
FAQs
WebGPU polyfill for headless testing in Bun using wgpu-native. Enables GPU computing and rendering without a browser.
The npm package wgpu-polyfill receives a total of 5 weekly downloads. As such, wgpu-polyfill popularity was classified as not popular.
We found that wgpu-polyfill demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Security News
OpenAI rotated macOS signing certificates after a malicious Axios package reached its CI pipeline in a broader software supply chain attack.

Security News
Open source is under attack because of how much value it creates. It has been the foundation of every major software innovation for the last three decades. This is not the time to walk away from it.

Security News
Socket CEO Feross Aboukhadijeh breaks down how North Korea hijacked Axios and what it means for the future of software supply chain security.