File tree Expand file tree Collapse file tree
packages/flame_3d/lib/src Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ import 'package:flutter_gpu/gpu.dart' as gpu;
2+
3+ // TODO(luan): for now, we need to support both old (returns T?) and
4+ // newer (returns T!) versions of some Flutter GPU context methods.
5+ class GpuContextWrapper {
6+ static final Map <gpu.GpuContext , GpuContextWrapper > _instances = {};
7+
8+ final gpu.GpuContext _gpuContext;
9+
10+ factory GpuContextWrapper (gpu.GpuContext gpuContext) {
11+ return _instances.putIfAbsent (
12+ gpuContext,
13+ () => GpuContextWrapper ._(gpuContext),
14+ );
15+ }
16+
17+ GpuContextWrapper ._(this ._gpuContext);
18+
19+ gpu.Texture createTexture (
20+ gpu.StorageMode storageMode,
21+ int width,
22+ int height, {
23+ gpu.PixelFormat format = gpu.PixelFormat .r8g8b8a8UNormInt,
24+ }) {
25+ return unwrap (
26+ _gpuContext.createTexture (
27+ storageMode,
28+ width,
29+ height,
30+ format: format,
31+ ),
32+ );
33+ }
34+
35+ gpu.DeviceBuffer createDeviceBuffer (
36+ gpu.StorageMode storageMode,
37+ int sizeInBytes,
38+ ) {
39+ return unwrap (_gpuContext.createDeviceBuffer (storageMode, sizeInBytes));
40+ }
41+
42+ static T unwrap <T >(T ? t) {
43+ return t! ;
44+ }
45+ }
Original file line number Diff line number Diff line change @@ -3,6 +3,7 @@ import 'dart:ui';
33
44import 'package:flame_3d/game.dart' ;
55import 'package:flame_3d/resources.dart' ;
6+ import 'package:flame_3d/src/graphics/gpu_context_wrapper.dart' ;
67import 'package:flame_3d/src/graphics/joints_info.dart' ;
78import 'package:flutter_gpu/gpu.dart' as gpu;
89
@@ -166,13 +167,14 @@ class GraphicsDevice {
166167 if (_previousSize != size) {
167168 _previousSize = size;
168169
169- final colorTexture = _gpuContext.createTexture (
170+ final gpuContext = GpuContextWrapper (_gpuContext);
171+ final colorTexture = gpuContext.createTexture (
170172 gpu.StorageMode .devicePrivate,
171173 size.width.toInt (),
172174 size.height.toInt (),
173175 );
174176
175- final depthTexture = _gpuContext .createTexture (
177+ final depthTexture = gpuContext .createTexture (
176178 gpu.StorageMode .deviceTransient,
177179 size.width.toInt (),
178180 size.height.toInt (),
Original file line number Diff line number Diff line change @@ -3,6 +3,7 @@ import 'dart:typed_data';
33
44import 'package:flame_3d/game.dart' ;
55import 'package:flame_3d/resources.dart' ;
6+ import 'package:flame_3d/src/graphics/gpu_context_wrapper.dart' ;
67import 'package:flutter_gpu/gpu.dart' as gpu;
78
89enum PrimitiveType {
@@ -73,7 +74,7 @@ class Surface extends Resource<gpu.DeviceBuffer?> {
7374 gpu.DeviceBuffer ? createResource () {
7475 final sizeInBytes = _vertices.lengthInBytes + _indices.lengthInBytes;
7576 resourceSizeInByes = sizeInBytes;
76- return gpu.gpuContext.createDeviceBuffer (
77+ return GpuContextWrapper ( gpu.gpuContext) .createDeviceBuffer (
7778 gpu.StorageMode .hostVisible,
7879 sizeInBytes,
7980 )
Original file line number Diff line number Diff line change @@ -2,6 +2,7 @@ import 'dart:typed_data';
22import 'dart:ui' ;
33
44import 'package:flame_3d/resources.dart' ;
5+ import 'package:flame_3d/src/graphics/gpu_context_wrapper.dart' ;
56import 'package:flutter_gpu/gpu.dart' as gpu;
67
78/// {@template texture}
@@ -23,7 +24,7 @@ class Texture extends Resource<gpu.Texture> {
2324
2425 @override
2526 gpu.Texture createResource () {
26- return gpu.gpuContext.createTexture (
27+ return GpuContextWrapper ( gpu.gpuContext) .createTexture (
2728 gpu.StorageMode .hostVisible,
2829 width,
2930 height,
You can’t perform that action at this time.
0 commit comments