TRELLIS.2 透過 C# 和 WebGPU 在瀏覽器內實現文字轉 3D
Text to 3D, 100% in the browser: TRELLIS.2 on WebGPU from C#
作者展示以 C# 和 WebGPU 在瀏覽器內執行 TRELLIS.2 的文字轉 3D 流程,模型在本機 GPU 生成並輸出帶貼圖的 GLB。Chrome 中以 RTX 4070 測得,權重已快取時,Prompt-to-GLB 為 180 s;Photo-to-GLB 首次 148 s,下一次 134 s。
Type "a wooden treasure chest with iron bands", wait about three minutes, and download a textured 3D model. Nothing leaves your machine. The whole pipeline runs on your GPU, in a browser tab, over WebGPU.
Try it: lostbeard.github.io/SpawnDev.ILGPU.ML/text-to-3d (desktop GPU recommended; the ~10 GB weight download is opt-in and cached)
To our knowledge this is the first fully client-side TRELLIS.2 on the web. It ships with SpawnDev.ILGPU.ML 5.3.12, a .NET library that runs neural networks on GPU kernels written in C#.
The pipeline
- SD Turbo draws the object from your prompt (or skip it and drop in a photo)
- RMBG-1.4 cuts the object out
- TRELLIS.2-4B (Microsoft, MIT) builds it: DINOv3 image features, then three 1.3B-parameter flow transformers for sparse structure, shape and texture, then sparse VAE decoders and a dual-grid mesh
- Mesh clustering and simplification, UV atlas, texture bake
- A PBR GLB (base colour + metallic-roughness) you can drop into Blender, Unity or three.js
Measured on an RTX 4070 in Chrome, weights cached:
| time | |
|---|---|
| Photo -> GLB, fresh page | 148 s |
| Photo -> GLB, next generation | 134 s |
| Prompt -> GLB | 180 s |
The first working version took 712 s that same morning. Here's where the time went.
1. GPU kernels in C#, not hand-written WGSL
Every kernel is a C# method that SpawnDev.ILGPU compiles to WGSL (and to CUDA, OpenCL or CPU code on the desktop, from the same source). The two kernels that matter for a transformer:
- A register-tiled GEMM: 128x128 output tiles, 256 threads, an 8x8 block of accumulators per thread. A single kernel covers bf16 and f16 weights and gathered rows, with a separate branch-free inner loop for each.
- Flash attention: 8 lanes share each query, 4 queries per thread, with subgroup shuffles for the reductions. Devices without subgroups use a shared-memory variant.
Running the diffusion sampler with these kernels lands at roughly 8 TFLOPS on GEMM and 5.6 TFLOPS on attention in WebGPU on a 4070.
2. 10 GB of weights vs. a browser quota
Browsers give each site a storage quota, and it's often smaller than the model. RangeStreamModelSource.PlanCacheAsync picks the subset of checkpoint files that fills the quota best and caches those in OPFS. Everything else streams on each run with HTTP range requests, four 16 MB requests in flight, straight from the fetch into GPU buffers without passing through the .NET heap.
var ranged = new RangeStreamModelSource(hubModelSource);
await ranged.PlanCacheAsync(Trellis2Weights.HubWeightFiles(), freeQuotaBytes - 300_000_000L);
using var gen = new Trellis2ImageTo3D(accelerator, Trellis2Weights.FromModelSource(ranged));
byte[] glb = await gen.GenerateGlbAsync(rgbaPixels, width, height);
3. Shader compiles were serial
WebGPU compiles a pipeline at its first use, one at a time. The big GEMM and attention kernels each took seconds to compile, and they all queued up at the start of the run. WebGPUAccelerator.PrecompileAsync (new in SpawnDev.ILGPU 5.3.8) builds them with createComputePipelineAsync in the background while the weights load. Four shaders: 1571 ms serially, 511 ms in parallel.
4. A 3-million-triangle mesh
The dual-grid mesh comes out at about 3M triangles. Feeding all of that to the edge-collapse simplifier is slow in a browser, so MeshClusterer first runs adaptive octree quadric clustering on the GPU, bringing it down to about 0.5M before the edge collapse produces the final 150K faces.
5. Blazor WebAssembly AOT
The non-GPU stages (simplification, UV atlas packing) are plain C#. Under the .NET interpreter they're slow. AOT brought mesh simplification from 41.3 s to 2.6 s and the UV atlas from 16.8 s to 0.8 s. GitHub Pages refuses files over 100 MB, and the AOT dotnet.native.wasm is bigger than that, so the deploy keeps only gzip copies and a small loadBootResource hook inflates them with DecompressionStream.
Try it, read it, use it
- Demo: /text-to-3d and /image-to-3d
- How it works, stage by stage: Docs/trellis2.md
- Source (MIT): github.com/LostBeard/SpawnDev.ILGPU.ML
- NuGet:
dotnet add package SpawnDev.ILGPU.ML
Model licences: TRELLIS.2 is MIT; DINOv3, SD Turbo and RMBG-1.4 carry their own licences. No weights ship in the package; the demo downloads them from Hugging Face on first use.
If you run it, I'd love to hear your GPU and your timings in the comments.
來源:DEV Community · dev.to