Skip to content

flutter_gpu review on tizen #103

@JSUYA

Description

@JSUYA

When testing flutter_gpu on flutter-tizen 3.32.4-tizen.1.0.0, I confirmed that it did not work properly. (TV, RPI4)
https://medium.com/flutter/getting-started-with-flutter-gpu-f33d497b7c11
(When testing on an android device with the same sample code, the triangle was drawn normally.)
There is also a my_renderer.shaderbundle file in the app installation path.

I think this feature should work without any problem since Tizen supports Impeller gles backend, but I haven't been able to find out what is causing the problem.

If you have any good ideas on this issue, please feel free to share them.


Command

$ flutter-tizen config --enable-native-assets
$ flutter-tizen run --debug --enable-experiment=native-assets

Code samples

main.dart
import 'dart:async';
import 'dart:typed_data';
import 'package:flutter/material.dart';
import 'package:flutter_gpu/gpu.dart' as gpu;
import 'package:vector_math/vector_math.dart';
import 'shaders.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter GPU Triangle Example',
      home: Scaffold(
        body: CustomPaint(
          painter: TrianglePainter(),
          size: const Size(400, 400),
        ),
      ),
    );
  }
}

class TrianglePainter extends CustomPainter {
  @override
  void paint(Canvas canvas, Size size) {
    print('Default color format: ${gpu.gpuContext.defaultColorFormat}');

    final texture = gpu.gpuContext.createTexture(
      gpu.StorageMode.devicePrivate,
      size.width.toInt(),
      size.height.toInt(),
    );

    final vert = shaderLibrary['SimpleVertex']!;
    final frag = shaderLibrary['SimpleFragment']!;
    final pipeline = gpu.gpuContext.createRenderPipeline(vert, frag);

    final gpu.DeviceBuffer vertexBuffer = gpu.gpuContext.createDeviceBuffer(
      gpu.StorageMode.hostVisible,
      4 * 6 * 3,
    );
    vertexBuffer.overwrite(
      Float32List.fromList(<double>[
        -0.5, -0.5, 1.0, 0.0, 0.0, 1.0, //
        0, 0.5, 0.0, 1.0, 0.0, 1.0, //
        0.5, -0.5, 0.0, 0.0, 1.0, 1.0, //
      ]).buffer.asByteData(),
    );

    final commandBuffer = gpu.gpuContext.createCommandBuffer();
    final renderTarget = gpu.RenderTarget.singleColor(
      gpu.ColorAttachment(
        texture: texture,
        clearValue: Vector4(102 / 255, 255 / 255, 255 / 255, 1),
      ),
    );

    final renderPass = commandBuffer.createRenderPass(renderTarget);
    renderPass.bindPipeline(pipeline);
    renderPass.bindVertexBuffer(
      gpu.BufferView(
        vertexBuffer,
        offsetInBytes: 0,
        lengthInBytes: vertexBuffer.sizeInBytes,
      ),
      3,
    );

    renderPass.draw();

    final Completer complete = Completer();

    commandBuffer.submit(
      completionCallback: (success) {
        complete.complete();
      },
    );
    complete.future;

    final image = texture.asImage();

    print(image.toString());

    canvas.drawImage(image, Offset(50, 50), Paint());

    canvas.drawRect(
      Rect.fromLTRB(400, 400, 450, 450),
      Paint()..color = Color.fromARGB(255, 255, 0, 0),
    );
  }

  @override
  bool shouldRepaint(covariant CustomPainter oldDelegate) => true;
}
shader.dart
import 'package:flutter_gpu/gpu.dart' as gpu;

const String _kShaderBundlePath =
    'build/shaderbundles/my_renderer.shaderbundle';
// NOTE: If you're building a library, the path must be prefixed
//       with a package name. For example:
//      'packages/my_cool_renderer/build/shaderbundles/my_renderer.shaderbundle'

gpu.ShaderLibrary? _shaderLibrary;
gpu.ShaderLibrary get shaderLibrary {
  if (_shaderLibrary != null) {
    return _shaderLibrary!;
  }
  _shaderLibrary = gpu.ShaderLibrary.fromAsset(_kShaderBundlePath);
  if (_shaderLibrary != null) {
    return _shaderLibrary!;
  }

  throw Exception("Failed to load shader bundle! ($_kShaderBundlePath)");
}
shaders
out vec4 frag_color;

void main() {
  frag_color = vec4(0, 1, 0, 1);
}
in vec2 position;

void main() {
  gl_Position = vec4(position, 0.0, 1.0);
}

my_renderer.shaderbundle.json

{
    "SimpleVertex": {
        "type": "vertex",
        "file": "shaders/simple.vert"
    },
    "SimpleFragment": {
        "type": "fragment",
        "file": "shaders/simple.frag"
    }
}
pubspec.yaml
name: test_11
description: "A new Flutter project."
publish_to: 'none'

version: 1.0.0+1

environment:
  sdk: ^3.8.0

dependencies:
  flutter:
    sdk: flutter
  cupertino_icons: ^1.0.8
  flutter_gpu:
    sdk: flutter
  flutter_gpu_shaders: ^0.3.0
  native_assets_cli: ^0.13.0
  vector_math: ^2.1.4

dev_dependencies:
  

  flutter_lints: ^5.0.0

flutter:
  uses-material-design: true
  assets:
    - build/shaderbundles/

native_assets:
  hook_script: hook/build.dart

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions