flutter3d
Showcase Changelog 62 packages API reference

Impeller: the source

since 0.1.0 Backends

Regions the guide quotes: fallback (line 41), settings (line 53), read (line 57).

1/// The desktop and mobile path: `flutter_gpu`, through Impeller, the first
2/// backend `openDevice` tries on a native build.
3///
4/// Quoted by `backend_impeller.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-impeller` page: the lit ball drawn through `flutter_gpu`. Its
12/// check also holds the device to its word on wireframe: declined exactly when
13/// it does not support it.
14final class BackendImpellerDemo 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.72, 0.5, 0.32, 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 // `flutter3d_app`'s native `openDevice` never names a backend. It asks
42 // `flutter3d_hardware`'s registry to open one, having first made sure
43 // Impeller and the software rasteriser have both registered themselves:
44 //
45 // final registry = DeviceRegistry();
46 // registerGpuBackend(registry);
47 // registerCpuBackend(registry);
48 // return registry.open(width: width, height: height, ...);
49 //
50 // Impeller is tried first; the software device is what a native build
51 // falls back to if flutter_gpu will not start.
53 @override
54 RenderSettings settings(DemoContext context) =>
55 const RenderSettings(wireframe: true);
57 String _report(GraphicsDevice device) =>
58 'wireframe: ${device.features.has(DeviceFeature.wireframe)}\n'
59 'blend constant: ${device.features.has(DeviceFeature.blendConstant)}\n'
60 'max colour attachments: ${device.limits.maxColorAttachments}\n'
61 'max anisotropy: ${device.limits.maxSamplerAnisotropy}';
63 @override
64 Widget? customBody(
65 BuildContext buildContext,
66 DemoContext context,
67 ) => Container(
68 color: const Color(0xFF14161A),
69 padding: const EdgeInsets.all(24),
70 alignment: Alignment.topLeft,
71 child: DefaultTextStyle(
72 style: const TextStyle(color: Color(0xFFE8E8EC), fontSize: 16),
73 child: Text(
74 'On Metal and Vulkan, Impeller answers true to wireframe and can '
75 'open more than one colour attachment; on its OpenGL ES path a '
76 'second attachment aborts the process outright, which is why '
77 'nothing above this asks for one without asking maxColorAttachments '
78 'first. It answers false to a blend constant on every platform, '
79 'because flutter_gpu\'s own RenderPass has no setter for one.\n\n'
80 'What the device actually open right now answers:\n\n'
81 '${_report(context.device)}',
82 ),
83 ),
84 );
86 @override
87 void verify(Scene scene, FrameResult frame) {
88 if (frame.drawCalls < 1) {
89 throw StateError('the ball was not drawn');
90 }
91 // Wireframe is the one capability that differs across all three
92 // backends. This page asks for it unconditionally; whether the frame
93 // actually got it has to match what the device that opened said it
94 // could do, whichever backend that turned out to be.
95 // Declined and supported at once, or neither, is the disagreement.
96 if (frame.wireframeDeclined ==
97 _device.features.has(DeviceFeature.wireframe)) {
98 throw StateError(
99 'wireframeDeclined disagreed with supportsWireframe for the open '
100 'device',
101 );
102 }
103 }