The surface buffer: the source
Open the live demo · Read the guide · View on GitHub
Regions the guide quotes: show (line 33), consumed (line 47).
1/// The surface buffer: the world normal and view-axis depth the scene pass
2/// writes for every screen-space effect to share, put on the screen raw.
3///
4/// Quoted by `surface_buffer.md` and shown whole in the Source tab.
5library;
7import 'package:flutter3d/flutter3d.dart';
8import 'package:flutter3d_showcase/pages/post/post_stage.dart';
9import 'package:flutter3d_showcase/src/demo/demo.dart';
11/// The `surface-buffer` page: the post stage's surface buffer put on the
12/// screen. Its check is that showing it costs multisampling and that nothing
13/// else here declines it.
14final class SurfaceBufferDemo extends ShowcaseDemo {
15 /// Whether the buffer is shown in place of the lit picture.
16 bool show = true;
18 late final bool _deviceCanMsaa;
21 void configureView(DemoContext context) => PostStage.frame(context);
24 Scene build(DemoContext context) {
25 _deviceCanMsaa = context.device.features.has(
26 DeviceFeature.offscreenMultisample,
27 );
28 return PostStage.build(context).scene;
29 }
32 RenderSettings settings(DemoContext context) => RenderSettings(
33 showSurfaceBuffer: show,
34 );
37 List<DemoControl> controls(DemoContext context) => <DemoControl>[
38 ToggleControl(
39 'Show the buffer',
40 value: () => show,
41 onChanged: (bool v) => show = v,
42 ),
43 ];
46 void verify(Scene scene, FrameResult frame) {
47 // Asking to see the buffer is asking the scene pass to write it, which on
48 // a device that can otherwise multisample costs the frame that
49 // multisampling for exactly the reason `msaa` explains.
50 final String? declined = frame.antiAliasing.msaaDeclined;
51 if (!_deviceCanMsaa) {
52 if (declined == null) {
53 throw StateError('this device cannot multisample and did not say so');
54 }
55 } else if (show) {
56 if (declined == null) {
57 throw StateError('showing the buffer should have cost multisampling');
58 }
59 } else if (declined != null) {
60 throw StateError('nothing here reads the buffer, and it still declined');
61 }
62 }
63}