flutter3d
Showcase Changelog 62 packages API reference

WebGPU: the source

since 0.6.0 Backends

Regions the guide quotes: flag (line 41), read (line 51).

1/// The fourth backend, tried before WebGL2 only when a web build asks for
2/// it by name at compile time.
3///
4/// Quoted by `backend_webgpu.md` and shown whole in the Source tab.
5library;
6
7import 'package:flutter/widgets.dart';
8import 'package:flutter3d/flutter3d.dart';
9import 'package:flutter3d_showcase/src/demo/demo.dart';
11/// The `backend-webgpu` page: the lit ball drawn through WebGPU. Its check
12/// fails if the device offers fewer colour attachments than the page says a
13/// modern backend does.
14final class BackendWebgpuDemo extends ShowcaseDemo {
15 late final GraphicsDevice _device;
17 @override
18 Scene build(DemoContext context) {
19 _device = context.device;
20 final RenderMaterial stone = RenderMaterial(
21 name: 'stone',
22 baseColor: LinearColor.fromSrgb(0.55, 0.4, 0.65, 1.0),
23 roughness: 0.6,
24 );
25 final MeshNode ball = MeshNode(
26 DeviceMesh.upload(
27 context.device,
28 SphereShape(segments: 24, rings: 12).build(),
29 ),
30 stone,
31 name: 'ball',
32 );
33 return Scene()
34 ..add(ball)
35 ..add(
36 LightNode(name: 'sun', intensity: 3.0 * LightNode.legacyUnit)
37 ..setLocalForward(Vector3(-0.4, -1.0, -0.3)),
38 );
39 }
41 // Trying WebGPU is a build-time choice, not a runtime one, because
42 // finding out whether a browser hands out an adapter means asking, and
43 // asking means the WebGPU device, its encoder and its WGSL are all
44 // reachable code the moment a build opts in:
45 //
46 // flutter build web --dart-define=FLUTTER3D_WEBGPU=true
47 //
48 // An ordinary build never registers it, so a build that did not ask never
49 // ships the bytes either.
51 String _report(GraphicsDevice device) =>
52 'wireframe: ${device.features.has(DeviceFeature.wireframe)}\n'
53 'blend constant: ${device.features.has(DeviceFeature.blendConstant)}\n'
54 'max colour attachments: ${device.limits.maxColorAttachments}';
56 @override
57 Widget? customBody(
58 BuildContext buildContext,
59 DemoContext context,
60 ) => Container(
61 color: const Color(0xFF14161A),
62 padding: const EdgeInsets.all(24),
63 alignment: Alignment.topLeft,
64 child: DefaultTextStyle(
65 style: const TextStyle(color: Color(0xFFE8E8EC), fontSize: 16),
66 child: Text(
67 'WebGPU has a blend factor for a constant colour but no way to split '
68 'it between colour and alpha the way this engine\'s blend states '
69 'ask, and no polygon fill mode at all, so it declines a blend '
70 'constant and wireframe by name rather than quietly doing nothing '
71 'with either.\n\n'
72 'What the device actually open right now answers:\n\n'
73 '${_report(context.device)}',
74 ),
75 ),
76 );
78 @override
79 void verify(Scene scene, FrameResult frame) {
80 if (frame.drawCalls < 1) {
81 throw StateError('the ball was not drawn');
82 }
83 // A modern API opens several colour attachments in one pass; the one
84 // capability this page names, maxColorAttachments, has to actually
85 // answer more than the bare minimum on the device that opened.
86 if (_device.limits.maxColorAttachments < 2) {
87 throw StateError(
88 'the open device answers fewer colour attachments than this page '
89 'claims a modern backend offers',
90 );
91 }
92 }