MIT License

项目介绍

software-mansion/TypeGPU 是一个托管在 GitHub 上的开源项目,主要使用 TypeScript 编写,采用 MIT License 许可证,目前获得 3.3k 个 Star、144 次 Fork。

仓库全名
software-mansion/TypeGPU
开源许可证
MIT License
Star 数
3.3k
Fork 数
144
未关闭 Issue
341

README 摘要

Website — Documentation TypeGPU is a modular and open-ended toolkit for WebGPU, with advanced type inference and the ability to write shaders in TypeScript. Table of contents: :gear: TypeGPU as a foundation :jigsaw: TypeGPU as a piece of the puzzle :book: TypeGPU for libraries Documentation What's next? Projects using TypeGPU Repository structure :gear: TypeGPU as a foundation We provide an abstraction that solves…

完整内容见 GitHub 原始 README 或本页 。

常见问题

TypeGPU 用什么语言开发?

主要使用 TypeScript 编写,采用 MIT License 许可证,目前有 3.3k 个 Star。

TypeGPU 可以免费商用吗?

仓库采用 MIT License 许可证,可免费使用与修改,商业使用请遵守许可证条款(例如保留版权声明与许可声明)。

如何获取 TypeGPU 的源码?

可以直接克隆仓库:git clone https://github.com/software-mansion/TypeGPU.git;安装与运行步骤见仓库 README。

TypeGPU 最近还在维护吗?

仓库最近一次更新为 21 小时前,当前有 341 个未关闭 Issue;Star 数为 3.3k,可作为社区活跃度的参考。

关于

GitHub 原始描述(英文)

A modular and open-ended toolkit for WebGPU, with advanced type inference and the ability to write shaders in TypeScript

描述取自 GitHub 官方 API,保持仓库作者书写的原文。

简要分析

热度分
3604
价值分
2011
活跃状态
活跃

语言占比

Astro
CSS
HTML
JavaScript
MDX
TypeScript

README

Ad Ad Ad

TypeGPU is a modular and open-ended toolkit for WebGPU, with advanced type inference and the ability to write shaders in TypeScript.

const neighborhood = (a: number, r: number) => {
  'use gpu';
  return d.vec2f(a - r, a + r);
};

//
// #1) Can be called in JS
//
const range = neighborhood(1.1, 0.5);
//    ^? d.v2f

//
// #2) Used to generate WGSL
//
const main = () => {
  'use gpu';
  return neighborhood(1.1, 0.5);
};

const wgsl = tgpu.resolve([main]);
//    ^? string

//
// #3) Executed on the GPU (generates WGSL underneath)
//
root.createGuardedComputePipeline(main).dispatchThreads();

npm version bundle size license



Table of contents:

:gear: TypeGPU as a foundation

We provide an abstraction that solves the most common WebGPU hurdles, yet does not restrict you in capability. You can granularly eject into vanilla WebGPU at any point. This means that, when building your app with TypeGPU, lock-in is not a concern!

The low-level nature of TypeGPU and it’s mirroring of WGSL (WebGPU Shading Language) syntax in TypeScript means that learning TypeGPU helps to learn WebGPU itself, with fewer frustrations.

The Getting Started and Fundamentals guides are a great starting point for new projects!

:jigsaw: TypeGPU as a piece of the puzzle

Our type-safe APIs can be used together, or in isolation. This makes partial application into existing apps just a few lines of code away, no matter the complexity of your app!

We wrote a comprehensive resource on ways TypeGPU can improve your existing codebase.

Pick and choose which parts of TypeGPU you’d like to incorporate into your existing app!

:book: TypeGPU for libraries

When creating a type-safe WebGPU library, one can expect to encounter at least one of the following problems:

  • Serializing/deserializing data.
  • Dynamically generating parts of the WGSL shader.
  • Complex type inference.

If implemented from scratch, interoperability with other libraries (ones that have a different focus, solve different problems) can be near impossible without going down to untyped WebGPU land, or copying data back to JS. Moreover, to keep up with demand from users, they can be tempted to go out of scope of their initial use-case, even though another library already solves that problem.

TypeGPU can be used as an interoperability layer between use-case specific libraries!

Let’s imagine @xyz/gen is a library for procedural generation using WebGPU compute shaders, and @abc/plot is a library for plots and visualization using WebGPU.

import { tgpu } from 'typegpu';
import gen from '@xyz/gen';
import plot from '@abc/plot';

// common root for allocating resources
const root = await tgpu.init();

const terrainBuffer = await gen.generateHeightMap(root, { ... });
//    ^? TgpuBuffer<WgslArray<WgslArray<F32>>> & StorageFlag

// ERROR: Argument of type 'TgpuBuffer<WgslArray<WgslArray<F32>>>' is
// not assignable to parameter of type 'TgpuBuffer<WgslArray<F32>>>'
plot.array1d(root, terrainBuffer);

// SUCCESS!
plot.array2d(root, terrainBuffer);

We can pass typed values around without the need to copy anything back to CPU-accessible memory! Let’s see an example of how we can construct a type-safe API:

import type { StorageFlag, TgpuBuffer, TgpuRoot } from 'typegpu';
import { d } from 'typegpu';

// We can define schemas, or functions that return schemas...
const HeightMap = (width: number, height: number) =>
  d.arrayOf(d.arrayOf(d.f32, height), width);

// ...then infer types from them
type HeightMap = ReturnType<typeof HeightMap>;

export async function generateHeightMap(
  root: TgpuRoot,
  opts: { width: number; height: number },
): Promise<TgpuBuffer<HeightMap> & StorageFlag> {
  const buffer = root
    .createBuffer(HeightMap(opts.width, opts.height))
    .$usage('storage');

  const rawBuffer = root.unwrap(buffer); // => GPUBuffer

  // Here we can do anything we would usually do with a
  // WebGPU buffer, like populating it in a compute shader.
  // `rawBuffer` is the WebGPU resource that is backing the
  // typed `buffer` object, meaning any changes to it will
  // be visible in both.

  return buffer;
}

Planning to create a WebGPU library? Reach out to us! We’d love to work with you to enrich the ecosystem with type-safe WebGPU utilities!

Documentation

We created a set of guides and tutorials to get you up and running fast. Check out our Official Docs!

What’s next?

Projects using TypeGPU

Libraries:

  • Redraw - a 2D graphics library built on top of WebGPU, with support for vector feathering and advanced vector-based effects.
  • Vivarium - Modern, intuitive, WebGPU-powered toolkit for creating your own cellular automata
  • wayfare - A modular game engine built on top of TypeGPU & Koota
  • typegpu-shader-canvas - a high-level library that sets up a canvas for you, so you can focus on delivering pixels with TypeScript functions
  • lilgpu - Lil wrapper to toy with WebGPU, powered by TypeGPU
  • fisheye.js - A JavaScript library for correcting fisheye, or barrel distortion, in images in the browser
  • @pmndrs/glyph - A typography engine for web graphics, with font baking, Unicode shaping, paragraph layout and batched text rendering

Apps:

  • Screen Studio - macOS screen recorder with automatic zoom and smooth animations for product demos, tutorials, and social media videos.
  • Shaders v3 - A visual design platform for creating production-ready WebGPU effects
  • ComfyUI - An open-source tool for creating generative AI
  • Lumen Apeiron by deluksic & Komediruzecki
  • Bone Tide (Web) (App Store) (Google Play) - A horde survival game by Konrad Reczko
  • GFX Computer - A WebMCP-enabled video editor, powered by TypeGPU

Demos:

Repository structure

Packages:

Tooling:

Internals:

Apps:

TypeGPU is created by Software Mansion

swm

Since 2012 Software Mansion is a software agency with experience in building web and mobile apps. We are Core React Native Contributors and experts in dealing with all kinds of React Native issues. We can help you build your next dream product – Hire us.

Made by @software-mansion and community 💛

评论

暂无评论