software-mansion / software-mansion/TypeGPU
feat: @typegpu/gl
Nobody has claimed this yet.
- Dominant language
- TypeScript
- Stars
- 3.2k
- Forks
- 122
- Avg merge
- 3d 5h
- Merged PRs (30d)
- 34
Description
Motivation
While WebGPU is the future of graphics programming on the web, there are many areas where WebGL2 is more widely available, or has better platform support. By implementing a WebGL/GLSL fallback for TypeGPU, we could:
- Extend
@typegpu/threeto work on the WebGL backend, in addition to the currently supported WebGPU backend. - Giving WebGL devs access to a powerful syntax for defining type-safe shader functions in TypeScript, with a fallback to GLSL-implemented functions (just like in core TypeGPU).
- Aiding in incremental migration for established WebGL projects towards WebGPU, without requiring a major rewrite. This would complement and work with the new
byeglproject.
Roadmap
- 11th of August - Experimental release as
@typegpu/gl@0.12.0(full-screen shaders, simple uniforms, basic GLSL generation) - TBD - Full standard library (
std.*) coverage
API
The @typegpu/gl package would export:
initWithGLfor initializing a WebGL implementation of TgpuRoot (equivalent oftgpu.init)initWithGLFallbackwhich would first try a WebGPU backend, then fallback to the WebGL implementationglOptions, which passed intotgpu.resolvewould generate GLSL for more fine-grained use.
initWithGL / initWithGLFallback
import tgpu, { d, std } from 'typegpu';
import { initWithGLFallback } from '@typegpu/gl';
const pos = tgpu.const(d.arrayOf(d.vec2f, 3), [
d.vec2f(0.0, 0.5),
d.vec2f(-0.5, -0.5),
d.vec2f(0.5, -0.5),
]);
// Render pipeline
const root = await initWithGLFallback();
const pipeline = root.createRenderPipeline({
vertex: ({ $vertexIndex: vid }) => {
'use gpu';
return {
$position: d.vec4f(pos.$[vid], 0, 1),
};
},
fragment: () => {
'use gpu';
return d.vec4f(1, 0, 0, 1);
},
});
// Setting up the canvas and drawing to it
const context = root.configureContext({
canvas: document.querySelector('canvas') as HTMLCanvasElement,
alphaMode: 'premultiplied',
});
pipeline.withColorAttachment({ view: context }).draw(3);
GLSL struct alignment
To generate TypeGPU structs that follow GLSL rules, we could export glslStruct:
import { glslStruct } from '@typegpu/gl';
const Boid = glslStruct({
pos: d.vec3f,
vel: d.vec3f,
});
We could also align all structs automatically, but that seems more magical. To be prototyped and discussed.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
The issue names the entry points initWithGL, initWithGLFallback, and glOptions, with tgpu.init as the existing API to compare against. Start by mapping those entry points to TypeGPU's current initialization and pipeline behavior, then validate the roadmap scope: full-screen shaders, simple uniforms, and basic GLSL generation. Done means an experimental @typegpu/gl release meeting that scope; no files or tests are named.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- typescript
- Domain
- computer-graphics, web-dev
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 30/100