├── .gitignore ├── .metadata ├── .pubignore ├── .vscode └── settings.json ├── CHANGELOG.md ├── LICENSE ├── README.md ├── analysis_options.yaml ├── android ├── .gitignore ├── build.gradle ├── settings.gradle └── src │ └── main │ ├── AndroidManifest.xml │ └── kotlin │ └── com │ └── futouapp │ └── flutter_webgpu │ └── flutter_webgpu │ └── FlutterWebgpuPlugin.kt ├── config.yaml ├── example ├── .gitignore ├── .metadata ├── README.md ├── analysis_options.yaml ├── android │ ├── .gitignore │ ├── app │ │ ├── build.gradle │ │ └── src │ │ │ ├── debug │ │ │ └── AndroidManifest.xml │ │ │ ├── main │ │ │ ├── AndroidManifest.xml │ │ │ ├── kotlin │ │ │ │ └── com │ │ │ │ │ └── futouapp │ │ │ │ │ └── flutter_webgpu │ │ │ │ │ ├── flutter_webgpu_example │ │ │ │ │ └── MainActivity.kt │ │ │ │ │ └── macos │ │ │ │ │ └── flutter_webgpu_example │ │ │ │ │ └── MainActivity.kt │ │ │ └── res │ │ │ │ ├── drawable-v21 │ │ │ │ └── launch_background.xml │ │ │ │ ├── drawable │ │ │ │ └── launch_background.xml │ │ │ │ ├── mipmap-hdpi │ │ │ │ └── ic_launcher.png │ │ │ │ ├── mipmap-mdpi │ │ │ │ └── ic_launcher.png │ │ │ │ ├── mipmap-xhdpi │ │ │ │ └── ic_launcher.png │ │ │ │ ├── mipmap-xxhdpi │ │ │ │ └── ic_launcher.png │ │ │ │ ├── mipmap-xxxhdpi │ │ │ │ └── ic_launcher.png │ │ │ │ ├── values-night │ │ │ │ └── styles.xml │ │ │ │ └── values │ │ │ │ └── styles.xml │ │ │ └── profile │ │ │ └── AndroidManifest.xml │ ├── build.gradle │ ├── gradle.properties │ ├── gradle │ │ └── wrapper │ │ │ └── gradle-wrapper.properties │ └── settings.gradle ├── ios │ ├── .gitignore │ ├── Flutter │ │ ├── AppFrameworkInfo.plist │ │ ├── Debug.xcconfig │ │ └── Release.xcconfig │ ├── Podfile │ ├── Podfile.lock │ ├── Runner.xcodeproj │ │ ├── project.pbxproj │ │ ├── project.xcworkspace │ │ │ ├── contents.xcworkspacedata │ │ │ └── xcshareddata │ │ │ │ ├── IDEWorkspaceChecks.plist │ │ │ │ └── WorkspaceSettings.xcsettings │ │ └── xcshareddata │ │ │ └── xcschemes │ │ │ └── Runner.xcscheme │ ├── Runner.xcworkspace │ │ ├── contents.xcworkspacedata │ │ └── xcshareddata │ │ │ ├── IDEWorkspaceChecks.plist │ │ │ └── WorkspaceSettings.xcsettings │ └── Runner │ │ ├── AppDelegate.swift │ │ ├── Assets.xcassets │ │ ├── AppIcon.appiconset │ │ │ ├── Contents.json │ │ │ ├── Icon-App-1024x1024@1x.png │ │ │ ├── Icon-App-20x20@1x.png │ │ │ ├── Icon-App-20x20@2x.png │ │ │ ├── Icon-App-20x20@3x.png │ │ │ ├── Icon-App-29x29@1x.png │ │ │ ├── Icon-App-29x29@2x.png │ │ │ ├── Icon-App-29x29@3x.png │ │ │ ├── Icon-App-40x40@1x.png │ │ │ ├── Icon-App-40x40@2x.png │ │ │ ├── Icon-App-40x40@3x.png │ │ │ ├── Icon-App-60x60@2x.png │ │ │ ├── Icon-App-60x60@3x.png │ │ │ ├── Icon-App-76x76@1x.png │ │ │ ├── Icon-App-76x76@2x.png │ │ │ └── Icon-App-83.5x83.5@2x.png │ │ └── LaunchImage.imageset │ │ │ ├── Contents.json │ │ │ ├── LaunchImage.png │ │ │ ├── LaunchImage@2x.png │ │ │ ├── LaunchImage@3x.png │ │ │ └── README.md │ │ ├── Base.lproj │ │ ├── LaunchScreen.storyboard │ │ └── Main.storyboard │ │ ├── Info.plist │ │ └── Runner-Bridging-Header.h ├── lib │ ├── Example.dart │ ├── ExampleCapture.dart │ ├── ExampleCompute.dart │ ├── ExampleTriangle.dart │ ├── TextureCube.dart │ ├── boids.dart │ ├── helloTriangle.dart │ ├── helloTriangle2.dart │ ├── helloTriangleMSAA.dart │ ├── main.dart │ └── rotateCube.dart ├── macos │ ├── .gitignore │ ├── Flutter │ │ ├── Flutter-Debug.xcconfig │ │ ├── Flutter-Release.xcconfig │ │ └── GeneratedPluginRegistrant.swift │ ├── Podfile │ ├── Podfile.lock │ ├── Runner.xcodeproj │ │ ├── project.pbxproj │ │ ├── project.xcworkspace │ │ │ └── xcshareddata │ │ │ │ └── IDEWorkspaceChecks.plist │ │ └── xcshareddata │ │ │ └── xcschemes │ │ │ └── Runner.xcscheme │ ├── Runner.xcworkspace │ │ ├── contents.xcworkspacedata │ │ └── xcshareddata │ │ │ └── IDEWorkspaceChecks.plist │ └── Runner │ │ ├── AppDelegate.swift │ │ ├── Assets.xcassets │ │ └── AppIcon.appiconset │ │ │ ├── Contents.json │ │ │ ├── app_icon_1024.png │ │ │ ├── app_icon_128.png │ │ │ ├── app_icon_16.png │ │ │ ├── app_icon_256.png │ │ │ ├── app_icon_32.png │ │ │ ├── app_icon_512.png │ │ │ └── app_icon_64.png │ │ ├── Base.lproj │ │ └── MainMenu.xib │ │ ├── Configs │ │ ├── AppInfo.xcconfig │ │ ├── Debug.xcconfig │ │ ├── Release.xcconfig │ │ └── Warnings.xcconfig │ │ ├── DebugProfile.entitlements │ │ ├── Info.plist │ │ ├── MainFlutterWindow.swift │ │ └── Release.entitlements ├── pubspec.lock ├── pubspec.yaml └── test │ └── widget_test.dart ├── ios ├── .gitignore ├── Assets │ └── .gitkeep ├── Classes │ ├── FlutterWebgpuPlugin.h │ ├── FlutterWebgpuPlugin.m │ ├── SwiftFlutterWebgpuPlugin.swift │ ├── ffi │ │ ├── webgpu-headers │ │ │ ├── LICENSE │ │ │ ├── README.md │ │ │ └── webgpu.h │ │ └── wgpu.h │ └── flutter_webgpu-Bridging-Header.h └── flutter_webgpu.podspec ├── lib ├── WebGPU_Binding.dart ├── flutter_webgpu.dart ├── shader_helper.dart └── webgpu │ ├── GPUAdapter.dart │ ├── GPUBindGroup.dart │ ├── GPUBindGroupLayout.dart │ ├── GPUBindGroupLayoutEntry.dart │ ├── GPUBlendComponent.dart │ ├── GPUBuffer.dart │ ├── GPUColor.dart │ ├── GPUCommandBuffer.dart │ ├── GPUCommandEncoder.dart │ ├── GPUComputePassEncoder.dart │ ├── GPUComputePipeline.dart │ ├── GPUDevice.dart │ ├── GPUExtent3D.dart │ ├── GPUImageCopyBuffer.dart │ ├── GPUImageCopyTexture.dart │ ├── GPUObjectBase.dart │ ├── GPUOrigin3D.dart │ ├── GPUPipelineBase.dart │ ├── GPUPipelineDescriptorBase.dart │ ├── GPUPipelineLayout.dart │ ├── GPUProgrammablePassEncoder.dart │ ├── GPUQueue.dart │ ├── GPURenderEncoderBase.dart │ ├── GPURenderPassDescriptor.dart │ ├── GPURenderPassEncoder.dart │ ├── GPURenderPipeline.dart │ ├── GPUSampler.dart │ ├── GPUShaderModule.dart │ ├── GPUState.dart │ ├── GPUTexture.dart │ ├── GPUTextureView.dart │ ├── GPUVertexBufferLayout.dart │ ├── constant.dart │ ├── gpu.dart │ ├── index.dart │ └── wgpu.dart ├── macos ├── Classes │ ├── FlutterWebgpuPlugin.swift │ ├── ffi │ └── flutter_webgpu-Bridging-Header.h ├── flutter_webgpu.podspec ├── libwgpu_native.a └── libwgpu_native.dylib ├── pubspec.yaml └── test └── flutter_webgpu_test.dart /.gitignore: -------------------------------------------------------------------------------- 1 | # Miscellaneous 2 | *.class 3 | *.log 4 | *.pyc 5 | *.swp 6 | .DS_Store 7 | .atom/ 8 | .buildlog/ 9 | .history 10 | .svn/ 11 | 12 | # IntelliJ related 13 | *.iml 14 | *.ipr 15 | *.iws 16 | .idea/ 17 | 18 | # The .vscode folder contains launch configuration and tasks you configure in 19 | # VS Code which you may wish to be included in version control, so this line 20 | # is commented out by default. 21 | #.vscode/ 22 | 23 | # Flutter/Dart/Pub related 24 | # Libraries should not include pubspec.lock, per https://dart.dev/guides/libraries/private-files#pubspeclock. 25 | /pubspec.lock 26 | **/doc/api/ 27 | .dart_tool/ 28 | .packages 29 | build/ 30 | .history -------------------------------------------------------------------------------- /.metadata: -------------------------------------------------------------------------------- 1 | # This file tracks properties of this Flutter project. 2 | # Used by Flutter tool to assess capabilities and perform upgrades etc. 3 | # 4 | # This file should be version controlled and should not be manually edited. 5 | 6 | version: 7 | revision: 77d935af4db863f6abd0b9c31c7e6df2a13de57b 8 | channel: stable 9 | 10 | project_type: plugin 11 | -------------------------------------------------------------------------------- /.pubignore: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wasabia/flutter_webgpu/687c08960e8c1ddc7a32d7886682bd4657015e2b/.pubignore -------------------------------------------------------------------------------- /.vscode/settings.json: -------------------------------------------------------------------------------- 1 | { 2 | "files.associations": { 3 | "*.cjson": "jsonc", 4 | "*.wxss": "css", 5 | "*.wxs": "javascript", 6 | "*.html.erb": "erb", 7 | "webgpu.h": "c" 8 | } 9 | } -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | ## 0.0.1 2 | 3 | * TODO: Describe initial release. 4 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | TODO: Add your license here. 2 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # flutter_webgpu 2 | cross-platform call WebGPU API by Dart through dart:ffi. 3D programming in the cross-platform. Provides WebGPU with Texture Widget on Flutter. 3 | 4 | webgpu in flutter, base on [gfx-rs/wgpu](https://github.com/gfx-rs/wgpu) 5 | 6 | in progress... 7 | 8 | 9 | will be used by [three_dart](https://github.com/wasabia/three_dart) 10 | 11 | 12 | # Contributing 13 | 14 | Pull Request please! 15 | 16 | 17 | ### Development 18 | 19 | update webgpu-headers submodule 20 | directory: ./native/wgpu-native/ffi/webgpu-headers 21 | 22 | ``` 23 | git submodule update --init 24 | ``` 25 | 26 | 27 | generate header binding 28 | will update code flutter_webgpu/lib/WebGPU_Binding.dart 29 | directory: ./ 30 | 31 | ``` 32 | // flutter_webgpu 33 | dart run ffigen --config config.yaml 34 | ``` 35 | 36 | 37 | generate the wgpu static library 38 | directory: ./native/wgpu-native 39 | 40 | ``` 41 | make lib-native 42 | ``` 43 | 44 | 45 | 46 | -------------------------------------------------------------------------------- /analysis_options.yaml: -------------------------------------------------------------------------------- 1 | include: package:flutter_lints/flutter.yaml 2 | 3 | # Additional information about this file can be found at 4 | # https://dart.dev/guides/language/analysis-options 5 | -------------------------------------------------------------------------------- /android/.gitignore: -------------------------------------------------------------------------------- 1 | *.iml 2 | .gradle 3 | /local.properties 4 | /.idea/workspace.xml 5 | /.idea/libraries 6 | .DS_Store 7 | /build 8 | /captures 9 | -------------------------------------------------------------------------------- /android/build.gradle: -------------------------------------------------------------------------------- 1 | group 'com.futouapp.flutter_webgpu.flutter_webgpu' 2 | version '1.0-SNAPSHOT' 3 | 4 | buildscript { 5 | ext.kotlin_version = '1.3.50' 6 | repositories { 7 | google() 8 | mavenCentral() 9 | } 10 | 11 | dependencies { 12 | classpath 'com.android.tools.build:gradle:4.1.0' 13 | classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" 14 | } 15 | } 16 | 17 | rootProject.allprojects { 18 | repositories { 19 | google() 20 | mavenCentral() 21 | } 22 | } 23 | 24 | apply plugin: 'com.android.library' 25 | apply plugin: 'kotlin-android' 26 | 27 | android { 28 | compileSdkVersion 30 29 | 30 | compileOptions { 31 | sourceCompatibility JavaVersion.VERSION_1_8 32 | targetCompatibility JavaVersion.VERSION_1_8 33 | } 34 | 35 | kotlinOptions { 36 | jvmTarget = '1.8' 37 | } 38 | 39 | sourceSets { 40 | main.java.srcDirs += 'src/main/kotlin' 41 | } 42 | 43 | defaultConfig { 44 | minSdkVersion 16 45 | } 46 | } 47 | 48 | dependencies { 49 | implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version" 50 | } 51 | -------------------------------------------------------------------------------- /android/settings.gradle: -------------------------------------------------------------------------------- 1 | rootProject.name = 'flutter_webgpu' 2 | -------------------------------------------------------------------------------- /android/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 3 | 4 | -------------------------------------------------------------------------------- /android/src/main/kotlin/com/futouapp/flutter_webgpu/flutter_webgpu/FlutterWebgpuPlugin.kt: -------------------------------------------------------------------------------- 1 | package com.futouapp.flutter_webgpu.flutter_webgpu 2 | 3 | import androidx.annotation.NonNull 4 | 5 | import io.flutter.embedding.engine.plugins.FlutterPlugin 6 | import io.flutter.plugin.common.MethodCall 7 | import io.flutter.plugin.common.MethodChannel 8 | import io.flutter.plugin.common.MethodChannel.MethodCallHandler 9 | import io.flutter.plugin.common.MethodChannel.Result 10 | 11 | /** FlutterWebgpuPlugin */ 12 | class FlutterWebgpuPlugin: FlutterPlugin, MethodCallHandler { 13 | /// The MethodChannel that will the communication between Flutter and native Android 14 | /// 15 | /// This local reference serves to register the plugin with the Flutter Engine and unregister it 16 | /// when the Flutter Engine is detached from the Activity 17 | private lateinit var channel : MethodChannel 18 | 19 | override fun onAttachedToEngine(@NonNull flutterPluginBinding: FlutterPlugin.FlutterPluginBinding) { 20 | channel = MethodChannel(flutterPluginBinding.binaryMessenger, "flutter_webgpu") 21 | channel.setMethodCallHandler(this) 22 | } 23 | 24 | override fun onMethodCall(@NonNull call: MethodCall, @NonNull result: Result) { 25 | if (call.method == "getPlatformVersion") { 26 | result.success("Android ${android.os.Build.VERSION.RELEASE}") 27 | } else { 28 | result.notImplemented() 29 | } 30 | } 31 | 32 | override fun onDetachedFromEngine(@NonNull binding: FlutterPlugin.FlutterPluginBinding) { 33 | channel.setMethodCallHandler(null) 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /config.yaml: -------------------------------------------------------------------------------- 1 | name: 'WebGPUBinding' 2 | description: 'Bindings to WebGPU' 3 | output: 'lib/WebGPU_Binding.dart' 4 | compiler-opts: 5 | - '-I/usr/lib/llvm-9/include/' 6 | compiler-opts-automatic: 7 | macos: 8 | include-c-standard-library: true 9 | headers: 10 | entry-points: 11 | - 'native/wgpu-native/ffi/**.h' 12 | - 'native/wgpu-native/ffi/webgpu-headers/webgpu.h' 13 | # include-directives: 14 | # - 'native/wgpu-native/ffi/webgpu-headers/webgpu.h' 15 | -------------------------------------------------------------------------------- /example/.gitignore: -------------------------------------------------------------------------------- 1 | # Miscellaneous 2 | *.class 3 | *.log 4 | *.pyc 5 | *.swp 6 | .DS_Store 7 | .atom/ 8 | .buildlog/ 9 | .history 10 | .svn/ 11 | 12 | # IntelliJ related 13 | *.iml 14 | *.ipr 15 | *.iws 16 | .idea/ 17 | 18 | # The .vscode folder contains launch configuration and tasks you configure in 19 | # VS Code which you may wish to be included in version control, so this line 20 | # is commented out by default. 21 | #.vscode/ 22 | 23 | # Flutter/Dart/Pub related 24 | **/doc/api/ 25 | **/ios/Flutter/.last_build_id 26 | .dart_tool/ 27 | .flutter-plugins 28 | .flutter-plugins-dependencies 29 | .packages 30 | .pub-cache/ 31 | .pub/ 32 | /build/ 33 | 34 | # Web related 35 | lib/generated_plugin_registrant.dart 36 | 37 | # Symbolication related 38 | app.*.symbols 39 | 40 | # Obfuscation related 41 | app.*.map.json 42 | 43 | # Android Studio will place build artifacts here 44 | /android/app/debug 45 | /android/app/profile 46 | /android/app/release 47 | -------------------------------------------------------------------------------- /example/.metadata: -------------------------------------------------------------------------------- 1 | # This file tracks properties of this Flutter project. 2 | # Used by Flutter tool to assess capabilities and perform upgrades etc. 3 | # 4 | # This file should be version controlled and should not be manually edited. 5 | 6 | version: 7 | revision: 77d935af4db863f6abd0b9c31c7e6df2a13de57b 8 | channel: stable 9 | 10 | project_type: app 11 | -------------------------------------------------------------------------------- /example/README.md: -------------------------------------------------------------------------------- 1 | # flutter_webgpu_example 2 | 3 | Demonstrates how to use the flutter_webgpu plugin. 4 | 5 | ## Getting Started 6 | 7 | This project is a starting point for a Flutter application. 8 | 9 | A few resources to get you started if this is your first Flutter project: 10 | 11 | - [Lab: Write your first Flutter app](https://flutter.dev/docs/get-started/codelab) 12 | - [Cookbook: Useful Flutter samples](https://flutter.dev/docs/cookbook) 13 | 14 | For help getting started with Flutter, view our 15 | [online documentation](https://flutter.dev/docs), which offers tutorials, 16 | samples, guidance on mobile development, and a full API reference. 17 | -------------------------------------------------------------------------------- /example/analysis_options.yaml: -------------------------------------------------------------------------------- 1 | # This file configures the analyzer, which statically analyzes Dart code to 2 | # check for errors, warnings, and lints. 3 | # 4 | # The issues identified by the analyzer are surfaced in the UI of Dart-enabled 5 | # IDEs (https://dart.dev/tools#ides-and-editors). The analyzer can also be 6 | # invoked from the command line by running `flutter analyze`. 7 | 8 | # The following line activates a set of recommended lints for Flutter apps, 9 | # packages, and plugins designed to encourage good coding practices. 10 | include: package:flutter_lints/flutter.yaml 11 | 12 | linter: 13 | # The lint rules applied to this project can be customized in the 14 | # section below to disable rules from the `package:flutter_lints/flutter.yaml` 15 | # included above or to enable additional rules. A list of all available lints 16 | # and their documentation is published at 17 | # https://dart-lang.github.io/linter/lints/index.html. 18 | # 19 | # Instead of disabling a lint rule for the entire project in the 20 | # section below, it can also be suppressed for a single line of code 21 | # or a specific dart file by using the `// ignore: name_of_lint` and 22 | # `// ignore_for_file: name_of_lint` syntax on the line or in the file 23 | # producing the lint. 24 | rules: 25 | # avoid_print: false # Uncomment to disable the `avoid_print` rule 26 | # prefer_single_quotes: true # Uncomment to enable the `prefer_single_quotes` rule 27 | 28 | # Additional information about this file can be found at 29 | # https://dart.dev/guides/language/analysis-options 30 | -------------------------------------------------------------------------------- /example/android/.gitignore: -------------------------------------------------------------------------------- 1 | gradle-wrapper.jar 2 | /.gradle 3 | /captures/ 4 | /gradlew 5 | /gradlew.bat 6 | /local.properties 7 | GeneratedPluginRegistrant.java 8 | 9 | # Remember to never publicly share your keystore. 10 | # See https://flutter.dev/docs/deployment/android#reference-the-keystore-from-the-app 11 | key.properties 12 | **/*.keystore 13 | **/*.jks 14 | -------------------------------------------------------------------------------- /example/android/app/build.gradle: -------------------------------------------------------------------------------- 1 | def localProperties = new Properties() 2 | def localPropertiesFile = rootProject.file('local.properties') 3 | if (localPropertiesFile.exists()) { 4 | localPropertiesFile.withReader('UTF-8') { reader -> 5 | localProperties.load(reader) 6 | } 7 | } 8 | 9 | def flutterRoot = localProperties.getProperty('flutter.sdk') 10 | if (flutterRoot == null) { 11 | throw new GradleException("Flutter SDK not found. Define location with flutter.sdk in the local.properties file.") 12 | } 13 | 14 | def flutterVersionCode = localProperties.getProperty('flutter.versionCode') 15 | if (flutterVersionCode == null) { 16 | flutterVersionCode = '1' 17 | } 18 | 19 | def flutterVersionName = localProperties.getProperty('flutter.versionName') 20 | if (flutterVersionName == null) { 21 | flutterVersionName = '1.0' 22 | } 23 | 24 | apply plugin: 'com.android.application' 25 | apply plugin: 'kotlin-android' 26 | apply from: "$flutterRoot/packages/flutter_tools/gradle/flutter.gradle" 27 | 28 | android { 29 | compileSdkVersion flutter.compileSdkVersion 30 | 31 | compileOptions { 32 | sourceCompatibility JavaVersion.VERSION_1_8 33 | targetCompatibility JavaVersion.VERSION_1_8 34 | } 35 | 36 | kotlinOptions { 37 | jvmTarget = '1.8' 38 | } 39 | 40 | sourceSets { 41 | main.java.srcDirs += 'src/main/kotlin' 42 | } 43 | 44 | defaultConfig { 45 | // TODO: Specify your own unique Application ID (https://developer.android.com/studio/build/application-id.html). 46 | applicationId "com.futouapp.flutter_webgpu.flutter_webgpu_example" 47 | minSdkVersion flutter.minSdkVersion 48 | targetSdkVersion flutter.targetSdkVersion 49 | versionCode flutterVersionCode.toInteger() 50 | versionName flutterVersionName 51 | } 52 | 53 | buildTypes { 54 | release { 55 | // TODO: Add your own signing config for the release build. 56 | // Signing with the debug keys for now, so `flutter run --release` works. 57 | signingConfig signingConfigs.debug 58 | } 59 | } 60 | } 61 | 62 | flutter { 63 | source '../..' 64 | } 65 | 66 | dependencies { 67 | implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version" 68 | } 69 | -------------------------------------------------------------------------------- /example/android/app/src/debug/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 3 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /example/android/app/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 3 | 7 | 15 | 19 | 23 | 24 | 25 | 26 | 27 | 28 | 30 | 33 | 34 | 35 | -------------------------------------------------------------------------------- /example/android/app/src/main/kotlin/com/futouapp/flutter_webgpu/flutter_webgpu_example/MainActivity.kt: -------------------------------------------------------------------------------- 1 | package com.futouapp.flutter_webgpu.flutter_webgpu_example 2 | 3 | import io.flutter.embedding.android.FlutterActivity 4 | 5 | class MainActivity: FlutterActivity() { 6 | } 7 | -------------------------------------------------------------------------------- /example/android/app/src/main/kotlin/com/futouapp/flutter_webgpu/macos/flutter_webgpu_example/MainActivity.kt: -------------------------------------------------------------------------------- 1 | package com.futouapp.flutter_webgpu.macos.flutter_webgpu_example 2 | 3 | import io.flutter.embedding.android.FlutterActivity 4 | 5 | class MainActivity: FlutterActivity() { 6 | } 7 | -------------------------------------------------------------------------------- /example/android/app/src/main/res/drawable-v21/launch_background.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 12 | 13 | -------------------------------------------------------------------------------- /example/android/app/src/main/res/drawable/launch_background.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 12 | 13 | -------------------------------------------------------------------------------- /example/android/app/src/main/res/mipmap-hdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wasabia/flutter_webgpu/687c08960e8c1ddc7a32d7886682bd4657015e2b/example/android/app/src/main/res/mipmap-hdpi/ic_launcher.png -------------------------------------------------------------------------------- /example/android/app/src/main/res/mipmap-mdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wasabia/flutter_webgpu/687c08960e8c1ddc7a32d7886682bd4657015e2b/example/android/app/src/main/res/mipmap-mdpi/ic_launcher.png -------------------------------------------------------------------------------- /example/android/app/src/main/res/mipmap-xhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wasabia/flutter_webgpu/687c08960e8c1ddc7a32d7886682bd4657015e2b/example/android/app/src/main/res/mipmap-xhdpi/ic_launcher.png -------------------------------------------------------------------------------- /example/android/app/src/main/res/mipmap-xxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wasabia/flutter_webgpu/687c08960e8c1ddc7a32d7886682bd4657015e2b/example/android/app/src/main/res/mipmap-xxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /example/android/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wasabia/flutter_webgpu/687c08960e8c1ddc7a32d7886682bd4657015e2b/example/android/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /example/android/app/src/main/res/values-night/styles.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 9 | 15 | 18 | 19 | -------------------------------------------------------------------------------- /example/android/app/src/main/res/values/styles.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 9 | 15 | 18 | 19 | -------------------------------------------------------------------------------- /example/android/app/src/profile/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 3 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /example/android/build.gradle: -------------------------------------------------------------------------------- 1 | buildscript { 2 | ext.kotlin_version = '1.3.50' 3 | repositories { 4 | google() 5 | mavenCentral() 6 | } 7 | 8 | dependencies { 9 | classpath 'com.android.tools.build:gradle:4.1.0' 10 | classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" 11 | } 12 | } 13 | 14 | allprojects { 15 | repositories { 16 | google() 17 | mavenCentral() 18 | } 19 | } 20 | 21 | rootProject.buildDir = '../build' 22 | subprojects { 23 | project.buildDir = "${rootProject.buildDir}/${project.name}" 24 | } 25 | subprojects { 26 | project.evaluationDependsOn(':app') 27 | } 28 | 29 | task clean(type: Delete) { 30 | delete rootProject.buildDir 31 | } 32 | -------------------------------------------------------------------------------- /example/android/gradle.properties: -------------------------------------------------------------------------------- 1 | org.gradle.jvmargs=-Xmx1536M 2 | android.useAndroidX=true 3 | android.enableJetifier=true 4 | -------------------------------------------------------------------------------- /example/android/gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | #Fri Jun 23 08:50:38 CEST 2017 2 | distributionBase=GRADLE_USER_HOME 3 | distributionPath=wrapper/dists 4 | zipStoreBase=GRADLE_USER_HOME 5 | zipStorePath=wrapper/dists 6 | distributionUrl=https\://services.gradle.org/distributions/gradle-6.7-all.zip 7 | -------------------------------------------------------------------------------- /example/android/settings.gradle: -------------------------------------------------------------------------------- 1 | include ':app' 2 | 3 | def localPropertiesFile = new File(rootProject.projectDir, "local.properties") 4 | def properties = new Properties() 5 | 6 | assert localPropertiesFile.exists() 7 | localPropertiesFile.withReader("UTF-8") { reader -> properties.load(reader) } 8 | 9 | def flutterSdkPath = properties.getProperty("flutter.sdk") 10 | assert flutterSdkPath != null, "flutter.sdk not set in local.properties" 11 | apply from: "$flutterSdkPath/packages/flutter_tools/gradle/app_plugin_loader.gradle" 12 | -------------------------------------------------------------------------------- /example/ios/.gitignore: -------------------------------------------------------------------------------- 1 | **/dgph 2 | *.mode1v3 3 | *.mode2v3 4 | *.moved-aside 5 | *.pbxuser 6 | *.perspectivev3 7 | **/*sync/ 8 | .sconsign.dblite 9 | .tags* 10 | **/.vagrant/ 11 | **/DerivedData/ 12 | Icon? 13 | **/Pods/ 14 | **/.symlinks/ 15 | profile 16 | xcuserdata 17 | **/.generated/ 18 | Flutter/App.framework 19 | Flutter/Flutter.framework 20 | Flutter/Flutter.podspec 21 | Flutter/Generated.xcconfig 22 | Flutter/ephemeral/ 23 | Flutter/app.flx 24 | Flutter/app.zip 25 | Flutter/flutter_assets/ 26 | Flutter/flutter_export_environment.sh 27 | ServiceDefinitions.json 28 | Runner/GeneratedPluginRegistrant.* 29 | 30 | # Exceptions to above rules. 31 | !default.mode1v3 32 | !default.mode2v3 33 | !default.pbxuser 34 | !default.perspectivev3 35 | -------------------------------------------------------------------------------- /example/ios/Flutter/AppFrameworkInfo.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | CFBundleDevelopmentRegion 6 | en 7 | CFBundleExecutable 8 | App 9 | CFBundleIdentifier 10 | io.flutter.flutter.app 11 | CFBundleInfoDictionaryVersion 12 | 6.0 13 | CFBundleName 14 | App 15 | CFBundlePackageType 16 | FMWK 17 | CFBundleShortVersionString 18 | 1.0 19 | CFBundleSignature 20 | ???? 21 | CFBundleVersion 22 | 1.0 23 | MinimumOSVersion 24 | 9.0 25 | 26 | 27 | -------------------------------------------------------------------------------- /example/ios/Flutter/Debug.xcconfig: -------------------------------------------------------------------------------- 1 | #include? "Pods/Target Support Files/Pods-Runner/Pods-Runner.debug.xcconfig" 2 | #include "Generated.xcconfig" 3 | -------------------------------------------------------------------------------- /example/ios/Flutter/Release.xcconfig: -------------------------------------------------------------------------------- 1 | #include? "Pods/Target Support Files/Pods-Runner/Pods-Runner.release.xcconfig" 2 | #include "Generated.xcconfig" 3 | -------------------------------------------------------------------------------- /example/ios/Podfile: -------------------------------------------------------------------------------- 1 | # Uncomment this line to define a global platform for your project 2 | # platform :ios, '9.0' 3 | 4 | # CocoaPods analytics sends network stats synchronously affecting flutter build latency. 5 | ENV['COCOAPODS_DISABLE_STATS'] = 'true' 6 | 7 | project 'Runner', { 8 | 'Debug' => :debug, 9 | 'Profile' => :release, 10 | 'Release' => :release, 11 | } 12 | 13 | def flutter_root 14 | generated_xcode_build_settings_path = File.expand_path(File.join('..', 'Flutter', 'Generated.xcconfig'), __FILE__) 15 | unless File.exist?(generated_xcode_build_settings_path) 16 | raise "#{generated_xcode_build_settings_path} must exist. If you're running pod install manually, make sure flutter pub get is executed first" 17 | end 18 | 19 | File.foreach(generated_xcode_build_settings_path) do |line| 20 | matches = line.match(/FLUTTER_ROOT\=(.*)/) 21 | return matches[1].strip if matches 22 | end 23 | raise "FLUTTER_ROOT not found in #{generated_xcode_build_settings_path}. Try deleting Generated.xcconfig, then run flutter pub get" 24 | end 25 | 26 | require File.expand_path(File.join('packages', 'flutter_tools', 'bin', 'podhelper'), flutter_root) 27 | 28 | flutter_ios_podfile_setup 29 | 30 | target 'Runner' do 31 | use_frameworks! 32 | use_modular_headers! 33 | 34 | flutter_install_all_ios_pods File.dirname(File.realpath(__FILE__)) 35 | end 36 | 37 | post_install do |installer| 38 | installer.pods_project.targets.each do |target| 39 | flutter_additional_ios_build_settings(target) 40 | end 41 | end 42 | -------------------------------------------------------------------------------- /example/ios/Podfile.lock: -------------------------------------------------------------------------------- 1 | PODS: 2 | - Flutter (1.0.0) 3 | - flutter_webgpu (0.0.1): 4 | - Flutter 5 | 6 | DEPENDENCIES: 7 | - Flutter (from `Flutter`) 8 | - flutter_webgpu (from `.symlinks/plugins/flutter_webgpu/ios`) 9 | 10 | EXTERNAL SOURCES: 11 | Flutter: 12 | :path: Flutter 13 | flutter_webgpu: 14 | :path: ".symlinks/plugins/flutter_webgpu/ios" 15 | 16 | SPEC CHECKSUMS: 17 | Flutter: 50d75fe2f02b26cc09d224853bb45737f8b3214a 18 | flutter_webgpu: 61bb3dfdca36237e3b6e4b6dc1b769bfa9ba5429 19 | 20 | PODFILE CHECKSUM: aafe91acc616949ddb318b77800a7f51bffa2a4c 21 | 22 | COCOAPODS: 1.11.2 23 | -------------------------------------------------------------------------------- /example/ios/Runner.xcodeproj/project.xcworkspace/contents.xcworkspacedata: -------------------------------------------------------------------------------- 1 | 2 | 4 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /example/ios/Runner.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | IDEDidComputeMac32BitWarning 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /example/ios/Runner.xcodeproj/project.xcworkspace/xcshareddata/WorkspaceSettings.xcsettings: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | PreviewsEnabled 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /example/ios/Runner.xcodeproj/xcshareddata/xcschemes/Runner.xcscheme: -------------------------------------------------------------------------------- 1 | 2 | 5 | 8 | 9 | 15 | 21 | 22 | 23 | 24 | 25 | 30 | 31 | 37 | 38 | 39 | 40 | 41 | 42 | 52 | 54 | 60 | 61 | 62 | 63 | 69 | 71 | 77 | 78 | 79 | 80 | 82 | 83 | 86 | 87 | 88 | -------------------------------------------------------------------------------- /example/ios/Runner.xcworkspace/contents.xcworkspacedata: -------------------------------------------------------------------------------- 1 | 2 | 4 | 6 | 7 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /example/ios/Runner.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | IDEDidComputeMac32BitWarning 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /example/ios/Runner.xcworkspace/xcshareddata/WorkspaceSettings.xcsettings: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | PreviewsEnabled 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /example/ios/Runner/AppDelegate.swift: -------------------------------------------------------------------------------- 1 | import UIKit 2 | import Flutter 3 | 4 | @UIApplicationMain 5 | @objc class AppDelegate: FlutterAppDelegate { 6 | override func application( 7 | _ application: UIApplication, 8 | didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]? 9 | ) -> Bool { 10 | GeneratedPluginRegistrant.register(with: self) 11 | return super.application(application, didFinishLaunchingWithOptions: launchOptions) 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /example/ios/Runner/Assets.xcassets/AppIcon.appiconset/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "images" : [ 3 | { 4 | "size" : "20x20", 5 | "idiom" : "iphone", 6 | "filename" : "Icon-App-20x20@2x.png", 7 | "scale" : "2x" 8 | }, 9 | { 10 | "size" : "20x20", 11 | "idiom" : "iphone", 12 | "filename" : "Icon-App-20x20@3x.png", 13 | "scale" : "3x" 14 | }, 15 | { 16 | "size" : "29x29", 17 | "idiom" : "iphone", 18 | "filename" : "Icon-App-29x29@1x.png", 19 | "scale" : "1x" 20 | }, 21 | { 22 | "size" : "29x29", 23 | "idiom" : "iphone", 24 | "filename" : "Icon-App-29x29@2x.png", 25 | "scale" : "2x" 26 | }, 27 | { 28 | "size" : "29x29", 29 | "idiom" : "iphone", 30 | "filename" : "Icon-App-29x29@3x.png", 31 | "scale" : "3x" 32 | }, 33 | { 34 | "size" : "40x40", 35 | "idiom" : "iphone", 36 | "filename" : "Icon-App-40x40@2x.png", 37 | "scale" : "2x" 38 | }, 39 | { 40 | "size" : "40x40", 41 | "idiom" : "iphone", 42 | "filename" : "Icon-App-40x40@3x.png", 43 | "scale" : "3x" 44 | }, 45 | { 46 | "size" : "60x60", 47 | "idiom" : "iphone", 48 | "filename" : "Icon-App-60x60@2x.png", 49 | "scale" : "2x" 50 | }, 51 | { 52 | "size" : "60x60", 53 | "idiom" : "iphone", 54 | "filename" : "Icon-App-60x60@3x.png", 55 | "scale" : "3x" 56 | }, 57 | { 58 | "size" : "20x20", 59 | "idiom" : "ipad", 60 | "filename" : "Icon-App-20x20@1x.png", 61 | "scale" : "1x" 62 | }, 63 | { 64 | "size" : "20x20", 65 | "idiom" : "ipad", 66 | "filename" : "Icon-App-20x20@2x.png", 67 | "scale" : "2x" 68 | }, 69 | { 70 | "size" : "29x29", 71 | "idiom" : "ipad", 72 | "filename" : "Icon-App-29x29@1x.png", 73 | "scale" : "1x" 74 | }, 75 | { 76 | "size" : "29x29", 77 | "idiom" : "ipad", 78 | "filename" : "Icon-App-29x29@2x.png", 79 | "scale" : "2x" 80 | }, 81 | { 82 | "size" : "40x40", 83 | "idiom" : "ipad", 84 | "filename" : "Icon-App-40x40@1x.png", 85 | "scale" : "1x" 86 | }, 87 | { 88 | "size" : "40x40", 89 | "idiom" : "ipad", 90 | "filename" : "Icon-App-40x40@2x.png", 91 | "scale" : "2x" 92 | }, 93 | { 94 | "size" : "76x76", 95 | "idiom" : "ipad", 96 | "filename" : "Icon-App-76x76@1x.png", 97 | "scale" : "1x" 98 | }, 99 | { 100 | "size" : "76x76", 101 | "idiom" : "ipad", 102 | "filename" : "Icon-App-76x76@2x.png", 103 | "scale" : "2x" 104 | }, 105 | { 106 | "size" : "83.5x83.5", 107 | "idiom" : "ipad", 108 | "filename" : "Icon-App-83.5x83.5@2x.png", 109 | "scale" : "2x" 110 | }, 111 | { 112 | "size" : "1024x1024", 113 | "idiom" : "ios-marketing", 114 | "filename" : "Icon-App-1024x1024@1x.png", 115 | "scale" : "1x" 116 | } 117 | ], 118 | "info" : { 119 | "version" : 1, 120 | "author" : "xcode" 121 | } 122 | } 123 | -------------------------------------------------------------------------------- /example/ios/Runner/Assets.xcassets/AppIcon.appiconset/Icon-App-1024x1024@1x.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wasabia/flutter_webgpu/687c08960e8c1ddc7a32d7886682bd4657015e2b/example/ios/Runner/Assets.xcassets/AppIcon.appiconset/Icon-App-1024x1024@1x.png -------------------------------------------------------------------------------- /example/ios/Runner/Assets.xcassets/AppIcon.appiconset/Icon-App-20x20@1x.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wasabia/flutter_webgpu/687c08960e8c1ddc7a32d7886682bd4657015e2b/example/ios/Runner/Assets.xcassets/AppIcon.appiconset/Icon-App-20x20@1x.png -------------------------------------------------------------------------------- /example/ios/Runner/Assets.xcassets/AppIcon.appiconset/Icon-App-20x20@2x.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wasabia/flutter_webgpu/687c08960e8c1ddc7a32d7886682bd4657015e2b/example/ios/Runner/Assets.xcassets/AppIcon.appiconset/Icon-App-20x20@2x.png -------------------------------------------------------------------------------- /example/ios/Runner/Assets.xcassets/AppIcon.appiconset/Icon-App-20x20@3x.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wasabia/flutter_webgpu/687c08960e8c1ddc7a32d7886682bd4657015e2b/example/ios/Runner/Assets.xcassets/AppIcon.appiconset/Icon-App-20x20@3x.png -------------------------------------------------------------------------------- /example/ios/Runner/Assets.xcassets/AppIcon.appiconset/Icon-App-29x29@1x.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wasabia/flutter_webgpu/687c08960e8c1ddc7a32d7886682bd4657015e2b/example/ios/Runner/Assets.xcassets/AppIcon.appiconset/Icon-App-29x29@1x.png -------------------------------------------------------------------------------- /example/ios/Runner/Assets.xcassets/AppIcon.appiconset/Icon-App-29x29@2x.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wasabia/flutter_webgpu/687c08960e8c1ddc7a32d7886682bd4657015e2b/example/ios/Runner/Assets.xcassets/AppIcon.appiconset/Icon-App-29x29@2x.png -------------------------------------------------------------------------------- /example/ios/Runner/Assets.xcassets/AppIcon.appiconset/Icon-App-29x29@3x.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wasabia/flutter_webgpu/687c08960e8c1ddc7a32d7886682bd4657015e2b/example/ios/Runner/Assets.xcassets/AppIcon.appiconset/Icon-App-29x29@3x.png -------------------------------------------------------------------------------- /example/ios/Runner/Assets.xcassets/AppIcon.appiconset/Icon-App-40x40@1x.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wasabia/flutter_webgpu/687c08960e8c1ddc7a32d7886682bd4657015e2b/example/ios/Runner/Assets.xcassets/AppIcon.appiconset/Icon-App-40x40@1x.png -------------------------------------------------------------------------------- /example/ios/Runner/Assets.xcassets/AppIcon.appiconset/Icon-App-40x40@2x.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wasabia/flutter_webgpu/687c08960e8c1ddc7a32d7886682bd4657015e2b/example/ios/Runner/Assets.xcassets/AppIcon.appiconset/Icon-App-40x40@2x.png -------------------------------------------------------------------------------- /example/ios/Runner/Assets.xcassets/AppIcon.appiconset/Icon-App-40x40@3x.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wasabia/flutter_webgpu/687c08960e8c1ddc7a32d7886682bd4657015e2b/example/ios/Runner/Assets.xcassets/AppIcon.appiconset/Icon-App-40x40@3x.png -------------------------------------------------------------------------------- /example/ios/Runner/Assets.xcassets/AppIcon.appiconset/Icon-App-60x60@2x.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wasabia/flutter_webgpu/687c08960e8c1ddc7a32d7886682bd4657015e2b/example/ios/Runner/Assets.xcassets/AppIcon.appiconset/Icon-App-60x60@2x.png -------------------------------------------------------------------------------- /example/ios/Runner/Assets.xcassets/AppIcon.appiconset/Icon-App-60x60@3x.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wasabia/flutter_webgpu/687c08960e8c1ddc7a32d7886682bd4657015e2b/example/ios/Runner/Assets.xcassets/AppIcon.appiconset/Icon-App-60x60@3x.png -------------------------------------------------------------------------------- /example/ios/Runner/Assets.xcassets/AppIcon.appiconset/Icon-App-76x76@1x.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wasabia/flutter_webgpu/687c08960e8c1ddc7a32d7886682bd4657015e2b/example/ios/Runner/Assets.xcassets/AppIcon.appiconset/Icon-App-76x76@1x.png -------------------------------------------------------------------------------- /example/ios/Runner/Assets.xcassets/AppIcon.appiconset/Icon-App-76x76@2x.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wasabia/flutter_webgpu/687c08960e8c1ddc7a32d7886682bd4657015e2b/example/ios/Runner/Assets.xcassets/AppIcon.appiconset/Icon-App-76x76@2x.png -------------------------------------------------------------------------------- /example/ios/Runner/Assets.xcassets/AppIcon.appiconset/Icon-App-83.5x83.5@2x.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wasabia/flutter_webgpu/687c08960e8c1ddc7a32d7886682bd4657015e2b/example/ios/Runner/Assets.xcassets/AppIcon.appiconset/Icon-App-83.5x83.5@2x.png -------------------------------------------------------------------------------- /example/ios/Runner/Assets.xcassets/LaunchImage.imageset/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "images" : [ 3 | { 4 | "idiom" : "universal", 5 | "filename" : "LaunchImage.png", 6 | "scale" : "1x" 7 | }, 8 | { 9 | "idiom" : "universal", 10 | "filename" : "LaunchImage@2x.png", 11 | "scale" : "2x" 12 | }, 13 | { 14 | "idiom" : "universal", 15 | "filename" : "LaunchImage@3x.png", 16 | "scale" : "3x" 17 | } 18 | ], 19 | "info" : { 20 | "version" : 1, 21 | "author" : "xcode" 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /example/ios/Runner/Assets.xcassets/LaunchImage.imageset/LaunchImage.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wasabia/flutter_webgpu/687c08960e8c1ddc7a32d7886682bd4657015e2b/example/ios/Runner/Assets.xcassets/LaunchImage.imageset/LaunchImage.png -------------------------------------------------------------------------------- /example/ios/Runner/Assets.xcassets/LaunchImage.imageset/LaunchImage@2x.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wasabia/flutter_webgpu/687c08960e8c1ddc7a32d7886682bd4657015e2b/example/ios/Runner/Assets.xcassets/LaunchImage.imageset/LaunchImage@2x.png -------------------------------------------------------------------------------- /example/ios/Runner/Assets.xcassets/LaunchImage.imageset/LaunchImage@3x.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wasabia/flutter_webgpu/687c08960e8c1ddc7a32d7886682bd4657015e2b/example/ios/Runner/Assets.xcassets/LaunchImage.imageset/LaunchImage@3x.png -------------------------------------------------------------------------------- /example/ios/Runner/Assets.xcassets/LaunchImage.imageset/README.md: -------------------------------------------------------------------------------- 1 | # Launch Screen Assets 2 | 3 | You can customize the launch screen with your own desired assets by replacing the image files in this directory. 4 | 5 | You can also do it by opening your Flutter project's Xcode project with `open ios/Runner.xcworkspace`, selecting `Runner/Assets.xcassets` in the Project Navigator and dropping in the desired images. -------------------------------------------------------------------------------- /example/ios/Runner/Base.lproj/LaunchScreen.storyboard: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | -------------------------------------------------------------------------------- /example/ios/Runner/Base.lproj/Main.storyboard: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | -------------------------------------------------------------------------------- /example/ios/Runner/Info.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | CFBundleDevelopmentRegion 6 | $(DEVELOPMENT_LANGUAGE) 7 | CFBundleDisplayName 8 | Flutter Webgpu 9 | CFBundleExecutable 10 | $(EXECUTABLE_NAME) 11 | CFBundleIdentifier 12 | $(PRODUCT_BUNDLE_IDENTIFIER) 13 | CFBundleInfoDictionaryVersion 14 | 6.0 15 | CFBundleName 16 | flutter_webgpu_example 17 | CFBundlePackageType 18 | APPL 19 | CFBundleShortVersionString 20 | $(FLUTTER_BUILD_NAME) 21 | CFBundleSignature 22 | ???? 23 | CFBundleVersion 24 | $(FLUTTER_BUILD_NUMBER) 25 | LSRequiresIPhoneOS 26 | 27 | UILaunchStoryboardName 28 | LaunchScreen 29 | UIMainStoryboardFile 30 | Main 31 | UISupportedInterfaceOrientations 32 | 33 | UIInterfaceOrientationPortrait 34 | UIInterfaceOrientationLandscapeLeft 35 | UIInterfaceOrientationLandscapeRight 36 | 37 | UISupportedInterfaceOrientations~ipad 38 | 39 | UIInterfaceOrientationPortrait 40 | UIInterfaceOrientationPortraitUpsideDown 41 | UIInterfaceOrientationLandscapeLeft 42 | UIInterfaceOrientationLandscapeRight 43 | 44 | UIViewControllerBasedStatusBarAppearance 45 | 46 | 47 | 48 | -------------------------------------------------------------------------------- /example/ios/Runner/Runner-Bridging-Header.h: -------------------------------------------------------------------------------- 1 | #import "GeneratedPluginRegistrant.h" 2 | -------------------------------------------------------------------------------- /example/lib/Example.dart: -------------------------------------------------------------------------------- 1 | import 'dart:ffi'; 2 | import 'dart:io'; 3 | import 'dart:typed_data'; 4 | 5 | import 'package:ffi/ffi.dart' as ffi; 6 | import 'package:flutter_webgpu/flutter_webgpu.dart'; 7 | 8 | void wgpuRequestAdapterCallback(int status, WGPUAdapter received, 9 | Pointer message, Pointer userdata) { 10 | print( 11 | "wgpuRequestAdapterCallback status: ${status} received: ${received} message: ${message} userdata: ${userdata} "); 12 | 13 | Pointer _adapter = userdata.cast(); 14 | _adapter.value = received; 15 | 16 | print("wgpuRequestAdapterCallback userdata: ${userdata} "); 17 | } 18 | 19 | typedef WgpuRequestAdapterCallback = Void Function( 20 | Int32, WGPUAdapter, Pointer, Pointer); 21 | 22 | void wgpuRequestDeviceCallback(int status, WGPUDevice received, 23 | Pointer message, Pointer userdata) { 24 | print( 25 | "wgpuRequestDeviceCallback status: ${status} received: ${received} message: ${message} userdata: ${userdata} "); 26 | Pointer _device = userdata.cast(); 27 | _device.value = received; 28 | print("wgpuRequestDeviceCallback userdata: ${userdata} "); 29 | } 30 | 31 | typedef WgpuRequestDeviceCallback = Void Function( 32 | Int32, WGPUDevice, Pointer, Pointer); 33 | 34 | void readBufferMap(int status, Pointer userdata) { 35 | print(" readBufferMap callback ....... "); 36 | } 37 | 38 | typedef ReadBufferMap = Void Function(Int32, Pointer); 39 | 40 | class Example { 41 | static render(int width, int height) { 42 | var _webGPU = Wgpu.binding; 43 | 44 | Pointer options = 45 | ffi.calloc(); 46 | WGPURequestAdapterOptions wrao = options.ref; 47 | wrao.nextInChain = nullptr; 48 | wrao.compatibleSurface = nullptr; 49 | 50 | Pointer adapter = ffi.calloc(); 51 | 52 | var adapterCallback = Pointer.fromFunction( 53 | wgpuRequestAdapterCallback); 54 | 55 | _webGPU.wgpuInstanceRequestAdapter( 56 | nullptr, options, adapterCallback, adapter.cast()); 57 | 58 | Pointer chain = ffi.calloc(); 59 | WGPUChainedStruct _chain = chain.ref; 60 | _chain.next = nullptr; 61 | _chain.sType = WGPUNativeSType.WGPUSType_DeviceExtras; 62 | 63 | Pointer requiredLimits = 64 | ffi.calloc(); 65 | WGPURequiredLimits _requiredLimits = requiredLimits.ref; 66 | _requiredLimits.nextInChain = nullptr; 67 | 68 | Pointer limits = ffi.calloc(); 69 | WGPULimits _limits = limits.ref; 70 | _limits.maxBindGroups = 1; 71 | 72 | _requiredLimits.limits = _limits; 73 | 74 | Pointer deviceExtras = ffi.calloc(); 75 | WGPUDeviceExtras _deviceExtras = deviceExtras.ref; 76 | _deviceExtras.chain = _chain; 77 | _deviceExtras.tracePath = nullptr; 78 | 79 | Pointer descriptor = 80 | ffi.calloc(); 81 | WGPUDeviceDescriptor _descriptor = descriptor.ref; 82 | _descriptor.nextInChain = deviceExtras.cast(); 83 | _descriptor.label = "WGPUDeviceDescriptor".toNativeUtf8().cast(); 84 | _descriptor.requiredLimits = requiredLimits; 85 | 86 | var deviceCallback = Pointer.fromFunction( 87 | wgpuRequestDeviceCallback); 88 | 89 | Pointer device = ffi.calloc(); 90 | 91 | print("adapter.value: ${adapter.value} adapter: ${adapter} "); 92 | 93 | _webGPU.wgpuAdapterRequestDevice( 94 | adapter.value, descriptor, deviceCallback, device.cast()); 95 | 96 | int bytes_per_pixel = Uint32List.bytesPerElement; 97 | int unpadded_bytes_per_row = width * bytes_per_pixel; 98 | int align = 256; 99 | int padded_bytes_per_row_padding = 100 | (align - unpadded_bytes_per_row % align) % align; 101 | int padded_bytes_per_row = 102 | unpadded_bytes_per_row + padded_bytes_per_row_padding; 103 | 104 | int bufferSize = padded_bytes_per_row * height; 105 | 106 | print("bufferSize: ${bufferSize} "); 107 | 108 | Pointer desc = ffi.calloc(); 109 | WGPUBufferDescriptor _desc = desc.ref; 110 | _desc.nextInChain = nullptr; 111 | _desc.label = "Output Buffer".toNativeUtf8().cast(); 112 | _desc.usage = WGPUBufferUsage.WGPUBufferUsage_MapRead | 113 | WGPUBufferUsage.WGPUBufferUsage_CopyDst; 114 | _desc.size = bufferSize; 115 | _desc.mappedAtCreation = false; 116 | 117 | WGPUBuffer outputBuffer = 118 | _webGPU.wgpuDeviceCreateBuffer(device.value, desc); 119 | 120 | Pointer _textureExtent = ffi.calloc(); 121 | WGPUExtent3D textureExtent = _textureExtent.ref; 122 | textureExtent.width = width; 123 | textureExtent.height = height; 124 | textureExtent.depthOrArrayLayers = 1; 125 | 126 | Pointer desc2 = ffi.calloc(); 127 | WGPUTextureDescriptor _desc2 = desc2.ref; 128 | _desc2.nextInChain = nullptr; 129 | _desc2.label = "WGPUTextureDescriptor".toNativeUtf8().cast(); 130 | _desc2.size = textureExtent; 131 | _desc2.mipLevelCount = 1; 132 | _desc2.sampleCount = 1; 133 | _desc2.dimension = WGPUTextureDimension.WGPUTextureDimension_2D; 134 | _desc2.format = WGPUTextureFormat.WGPUTextureFormat_RGBA8UnormSrgb; 135 | _desc2.usage = WGPUTextureUsage.WGPUTextureUsage_RenderAttachment | 136 | WGPUTextureUsage.WGPUTextureUsage_CopySrc; 137 | 138 | WGPUTexture texture = _webGPU.wgpuDeviceCreateTexture(device.value, desc2); 139 | 140 | Pointer desc3 = 141 | ffi.calloc(); 142 | WGPUCommandEncoderDescriptor _desc3 = desc3.ref; 143 | _desc3.label = nullptr; 144 | WGPUCommandEncoder encoder = 145 | _webGPU.wgpuDeviceCreateCommandEncoder(device.value, desc3); 146 | 147 | Pointer desc4 = 148 | ffi.calloc(); 149 | WGPUTextureViewDescriptor _desc4 = desc4.ref; 150 | _desc4.nextInChain = nullptr; 151 | _desc4.label = nullptr; 152 | _desc4.format = WGPUTextureFormat.WGPUTextureFormat_Undefined; 153 | _desc4.dimension = 154 | WGPUTextureViewDimension.WGPUTextureViewDimension_Undefined; 155 | _desc4.aspect = WGPUTextureAspect.WGPUTextureAspect_All; 156 | _desc4.arrayLayerCount = 0; 157 | _desc4.baseArrayLayer = 0; 158 | _desc4.baseMipLevel = 0; 159 | _desc4.mipLevelCount = 0; 160 | 161 | WGPUTextureView outputAttachment = 162 | _webGPU.wgpuTextureCreateView(texture, desc4); 163 | 164 | Pointer color = ffi.calloc(); 165 | WGPUColor _color = color.ref; 166 | _color.r = 1.0; 167 | _color.g = 0.0; 168 | _color.b = 0.0; 169 | _color.a = 1.0; 170 | 171 | Pointer attach = 172 | ffi.calloc(); 173 | WGPURenderPassColorAttachment _attach = attach.ref; 174 | _attach.view = outputAttachment; 175 | _attach.resolveTarget = nullptr; 176 | _attach.loadOp = WGPULoadOp.WGPULoadOp_Clear; 177 | _attach.storeOp = WGPUStoreOp.WGPUStoreOp_Store; 178 | _attach.clearValue = _color; 179 | 180 | Pointer desc5 = 181 | ffi.calloc(); 182 | WGPURenderPassDescriptor _desc5 = desc5.ref; 183 | _desc5.colorAttachments = attach; 184 | _desc5.colorAttachmentCount = 1; 185 | _desc5.depthStencilAttachment = nullptr; 186 | 187 | WGPURenderPassEncoder renderPass = 188 | _webGPU.wgpuCommandEncoderBeginRenderPass(encoder, desc5); 189 | _webGPU.wgpuRenderPassEncoderEnd(renderPass); 190 | 191 | Pointer origin = ffi.calloc(); 192 | WGPUOrigin3D _origin = origin.ref; 193 | _origin.x = 0; 194 | _origin.y = 0; 195 | _origin.z = 0; 196 | 197 | Pointer copyTexture = 198 | ffi.calloc(); 199 | WGPUImageCopyTexture _copyTexture = copyTexture.ref; 200 | _copyTexture.texture = texture; 201 | _copyTexture.mipLevel = 0; 202 | _copyTexture.origin = _origin; 203 | 204 | Pointer layout = ffi.calloc(); 205 | WGPUTextureDataLayout _layout = layout.ref; 206 | _layout.offset = 0; 207 | _layout.bytesPerRow = padded_bytes_per_row; 208 | _layout.rowsPerImage = 0; 209 | 210 | Pointer copyBuffer = ffi.calloc(); 211 | WGPUImageCopyBuffer _copyBuffer = copyBuffer.ref; 212 | _copyBuffer.buffer = outputBuffer; 213 | _copyBuffer.layout = _layout; 214 | 215 | _webGPU.wgpuCommandEncoderCopyTextureToBuffer( 216 | encoder, copyTexture, copyBuffer, _textureExtent); 217 | 218 | WGPUQueue queue = _webGPU.wgpuDeviceGetQueue(device.value); 219 | 220 | Pointer desc6 = 221 | ffi.calloc(); 222 | WGPUCommandBufferDescriptor _desc6 = desc6.ref; 223 | _desc6.label = nullptr; 224 | 225 | WGPUCommandBuffer cmdBuffer = 226 | _webGPU.wgpuCommandEncoderFinish(encoder, desc6); 227 | Pointer cmdBufferPointer = 228 | ffi.calloc(); 229 | cmdBufferPointer.value = cmdBuffer; 230 | _webGPU.wgpuQueueSubmit(queue, 1, cmdBufferPointer); 231 | 232 | var readBufferMapCallback = 233 | Pointer.fromFunction(readBufferMap); 234 | 235 | _webGPU.wgpuBufferMapAsync(outputBuffer, WGPUMapMode.WGPUMapMode_Read, 0, 236 | bufferSize, readBufferMapCallback, nullptr); 237 | 238 | _webGPU.wgpuDevicePoll(device.value, true, nullptr); 239 | 240 | print(" wgpuBufferGetMappedRange "); 241 | var data = _webGPU.wgpuBufferGetMappedRange(outputBuffer, 0, bufferSize); 242 | print(" data ${data} "); 243 | print(data); 244 | 245 | Pointer pixles = data.cast(); 246 | 247 | _webGPU.wgpuBufferUnmap(outputBuffer); 248 | 249 | return pixles.asTypedList(width * height * 4); 250 | } 251 | } 252 | -------------------------------------------------------------------------------- /example/lib/ExampleCapture.dart: -------------------------------------------------------------------------------- 1 | import 'dart:ffi'; 2 | import 'dart:io'; 3 | import 'dart:typed_data'; 4 | 5 | import 'package:ffi/ffi.dart' as ffi; 6 | import 'package:flutter_webgpu/flutter_webgpu.dart'; 7 | 8 | class ExampleCapture { 9 | static render(int width, int height) { 10 | var adapter = requestAdapter(GPURequestAdapterOptions()); 11 | var device = adapter.requestDevice(GPUDeviceDescriptor()); 12 | 13 | int bytes_per_pixel = Uint32List.bytesPerElement; 14 | int unpadded_bytes_per_row = width * bytes_per_pixel; 15 | int align = 256; 16 | int padded_bytes_per_row_padding = 17 | (align - unpadded_bytes_per_row % align) % align; 18 | int padded_bytes_per_row = 19 | unpadded_bytes_per_row + padded_bytes_per_row_padding; 20 | 21 | int bufferSize = padded_bytes_per_row * height; 22 | 23 | var bufferDesc = GPUBufferDescriptor( 24 | size: bufferSize, 25 | usage: GPUBufferUsage.MapRead | GPUBufferUsage.CopyDst); 26 | var outputBuffer = device.createBuffer(bufferDesc); 27 | 28 | var textureExtent = GPUExtent3D(width: width, height: height); 29 | 30 | var textureDesc = GPUTextureDescriptor( 31 | size: textureExtent, 32 | format: GPUTextureFormat.RGBA8UnormSrgb, 33 | usage: GPUTextureUsage.RenderAttachment | 34 | GPUTextureUsage.CopyDst | 35 | GPUTextureUsage.CopySrc); 36 | var texture = device.createTexture(textureDesc); 37 | 38 | var encoder = device.createCommandEncoder(GPUCommandEncoderDescriptor()); 39 | 40 | var outputAttachment = texture.createView( 41 | GPUTextureViewDescriptor(format: GPUTextureFormat.Undefined)); 42 | 43 | var color = GPUColor(r: 1.0, g: 0.0, b: 0.0, a: 1.0); 44 | 45 | var attach = GPURenderPassColorAttachment( 46 | view: outputAttachment, 47 | clearColor: color, 48 | loadOp: GPULoadOp.Clear, 49 | storeOp: GPUStoreOp.Store); 50 | 51 | var renderPass = encoder 52 | .beginRenderPass(GPURenderPassDescriptor(colorAttachments: attach)); 53 | renderPass.end(); 54 | 55 | print(" end pass .... "); 56 | 57 | var copyTexture = 58 | GPUImageCopyTexture(texture: texture, origin: GPUOrigin3D()); 59 | var copyBuffer = GPUImageCopyBuffer( 60 | buffer: outputBuffer, bytesPerRow: padded_bytes_per_row); 61 | 62 | encoder.copyTextureToBuffer(copyTexture, copyBuffer, textureExtent); 63 | 64 | var queue = device.queue; 65 | 66 | var commandBuffer = encoder.finish(GPUCommandBufferDescriptor()); 67 | 68 | queue.submit(commandBuffer); 69 | 70 | outputBuffer.mapAsync(mode: WGPUMapMode_Read, size: bufferSize); 71 | 72 | device.poll(true); 73 | 74 | var data = outputBuffer.getMappedRange(offset: 0, size: bufferSize); 75 | outputBuffer.unmap(); 76 | 77 | print(" data ${data} "); 78 | 79 | Pointer pixles = data.cast(); 80 | 81 | return pixles.asTypedList(width * height * 4); 82 | } 83 | } 84 | -------------------------------------------------------------------------------- /example/lib/ExampleCompute.dart: -------------------------------------------------------------------------------- 1 | import 'dart:ffi'; 2 | import 'dart:io'; 3 | import 'dart:typed_data'; 4 | 5 | import 'package:ffi/ffi.dart' as ffi; 6 | import 'package:flutter_webgpu/flutter_webgpu.dart'; 7 | 8 | class ExampleCompute { 9 | static render(int width, int height) { 10 | Uint32List numbers = Uint32List.fromList([1, 2, 3, 4]); 11 | int numbersSize = numbers.length * Uint32List.bytesPerElement; 12 | int numbersLength = numbersSize ~/ Uint32List.bytesPerElement; 13 | 14 | var adapter = requestAdapter(GPURequestAdapterOptions()); 15 | var device = adapter.requestDevice(GPUDeviceDescriptor()); 16 | 17 | device.setUncapturedErrorCallback(); 18 | 19 | print("device.value: ${device.device.value} device: ${device.device} "); 20 | print( 21 | "adapter.value: ${adapter.adapter.value} adapter: ${adapter.adapter} "); 22 | 23 | String shaderStr = """ 24 | @group(0) 25 | @binding(0) 26 | var v_indices: array; // this is used as both input and output for convenience 27 | 28 | // The Collatz Conjecture states that for any integer n: 29 | // If n is even, n = n/2 30 | // If n is odd, n = 3n+1 31 | // And repeat this process for each new n, you will always eventually reach 1. 32 | // Though the conjecture has not been proven, no counterexample has ever been found. 33 | // This function returns how many times this recurrence needs to be applied to reach 1. 34 | fn collatz_iterations(n_base: u32) -> u32{ 35 | var n: u32 = n_base; 36 | var i: u32 = 0u; 37 | loop { 38 | if (n <= 1u) { 39 | break; 40 | } 41 | if (n % 2u == 0u) { 42 | n = n / 2u; 43 | } 44 | else { 45 | // Overflow? (i.e. 3*n + 1 > 0xffffffffu?) 46 | if (n >= 1431655765u) { // 0x55555555u 47 | return 4294967295u; // 0xffffffffu 48 | } 49 | 50 | n = 3u * n + 1u; 51 | } 52 | i = i + 1u; 53 | } 54 | return i; 55 | } 56 | 57 | @compute 58 | @workgroup_size(1) 59 | fn main(@builtin(global_invocation_id) global_id: vec3) { 60 | v_indices[global_id.x] = collatz_iterations(v_indices[global_id.x]); 61 | } 62 | """; 63 | 64 | GPUShaderModuleDescriptor shaderSource = 65 | GPUShaderModuleDescriptor(code: shaderStr); 66 | var shader = device.createShaderModule(shaderSource); 67 | 68 | var bufferDesc0 = GPUBufferDescriptor( 69 | size: numbersSize, 70 | usage: GPUBufferUsage.MapRead | GPUBufferUsage.CopyDst); 71 | var stagingBuffer = device.createBuffer(bufferDesc0); 72 | 73 | print("numbersSize: ${numbersSize} "); 74 | 75 | var bufferDesc1 = GPUBufferDescriptor( 76 | size: numbersSize, 77 | label: 'StorageBuffer', 78 | usage: GPUBufferUsage.Storage | 79 | GPUBufferUsage.CopyDst | 80 | GPUBufferUsage.CopySrc); 81 | var storageBuffer = device.createBuffer(bufferDesc1); 82 | 83 | 84 | var _bindGroupLayoutDesc = GPUBindGroupLayoutDescriptor(entries: [ 85 | GPUBindGroupLayoutEntry( 86 | binding: 0, 87 | visibility: GPUShaderStage.Compute, 88 | buffer: GPUBufferBindingLayout(type: GPUBufferBindingType.Storage)) 89 | ]); 90 | 91 | var bindGroupLayout = device.createBindGroupLayout(_bindGroupLayoutDesc); 92 | var pipelineLayout = device.createPipelineLayout( 93 | GPUPipelineLayoutDescriptor( 94 | bindGroupLayouts: bindGroupLayout, bindGroupLayoutCount: 1)); 95 | 96 | var computePipeline = device.createComputePipeline( 97 | GPUComputePipelineDescriptor( 98 | layout: pipelineLayout, 99 | compute: GPUProgrammableStage(module: shader, entryPoint: 'main'))); 100 | 101 | var encoder = device.createCommandEncoder(GPUCommandEncoderDescriptor()); 102 | 103 | var computePass = encoder.beginComputePass(GPUComputePassDescriptor()); 104 | 105 | computePass.setPipeline(computePipeline); 106 | 107 | var bindGroup = device.createBindGroup(GPUBindGroupDescriptor( 108 | label: "Bind Group", 109 | layout: bindGroupLayout, 110 | entries: [GPUBindGroupEntry(binding: 0, buffer: storageBuffer, size: numbersSize)], 111 | entryCount: 1)); 112 | 113 | computePass.setBindGroup(0, bindGroup, 0, null); 114 | // computePass.dispatch(numbersLength, 1, 1); 115 | computePass.end(); 116 | 117 | encoder.copyBufferToBuffer(storageBuffer, 0, stagingBuffer, 0, numbersSize); 118 | 119 | var queue = device.queue; 120 | 121 | var commandBuffer = encoder.finish(GPUCommandBufferDescriptor()); 122 | 123 | queue.writeBuffer(storageBuffer, 0, numbers, numbersSize); 124 | queue.submit(commandBuffer); 125 | 126 | stagingBuffer.mapAsync(mode: WGPUMapMode_Read, size: numbersSize); 127 | 128 | device.poll(true); 129 | 130 | print(" wgpuBufferGetMappedRange "); 131 | var data = stagingBuffer.getMappedRange(offset: 0, size: numbersSize); 132 | print(" data ${data} "); 133 | 134 | Pointer pixles = data.cast(); 135 | 136 | print(" compute result: "); 137 | print(pixles.asTypedList(numbers.length)); 138 | 139 | stagingBuffer.unmap(); 140 | 141 | return null; 142 | } 143 | } 144 | -------------------------------------------------------------------------------- /example/lib/ExampleTriangle.dart: -------------------------------------------------------------------------------- 1 | import 'dart:ffi'; 2 | import 'dart:io'; 3 | import 'dart:typed_data'; 4 | 5 | import 'package:ffi/ffi.dart' as ffi; 6 | import 'package:flutter_webgpu/flutter_webgpu.dart'; 7 | 8 | class ExampleTriangle { 9 | static render(int width, int height) { 10 | Uint32List numbers = Uint32List.fromList([1, 2, 3, 4]); 11 | int numbersSize = numbers.length * Uint8List.bytesPerElement; 12 | int numbersLength = numbersSize ~/ Uint8List.bytesPerElement; 13 | 14 | int bytes_per_pixel = Uint32List.bytesPerElement; 15 | int unpadded_bytes_per_row = width * bytes_per_pixel; 16 | int align = 256; 17 | int padded_bytes_per_row_padding = 18 | (align - unpadded_bytes_per_row % align) % align; 19 | int padded_bytes_per_row = 20 | unpadded_bytes_per_row + padded_bytes_per_row_padding; 21 | 22 | int bufferSize = padded_bytes_per_row * height; 23 | 24 | var adapter = requestAdapter(GPURequestAdapterOptions()); 25 | var device = adapter.requestDevice(GPUDeviceDescriptor()); 26 | 27 | device.setUncapturedErrorCallback(); 28 | 29 | print("device.value: ${device.device.value} device: ${device.device} "); 30 | print( 31 | "adapter.value: ${adapter.adapter.value} adapter: ${adapter.adapter} "); 32 | 33 | String shaderStr = """ 34 | @vertex 35 | fn vs_main(@builtin(vertex_index) in_vertex_index: u32) -> @builtin(position) vec4 { 36 | let x = f32(i32(in_vertex_index) - 1); 37 | let y = f32(i32(in_vertex_index & 1u) * 2 - 1); 38 | return vec4(x, y, 0.0, 1.0); 39 | } 40 | 41 | @fragment 42 | fn fs_main() -> @location(0) vec4 { 43 | return vec4(1.0, 0.0, 0.0, 1.0); 44 | } 45 | 46 | """; 47 | 48 | GPUShaderModuleDescriptor shaderSource = 49 | GPUShaderModuleDescriptor(code: shaderStr); 50 | var shader = device.createShaderModule(shaderSource); 51 | 52 | var pipelineLayout = device.createPipelineLayout( 53 | GPUPipelineLayoutDescriptor( 54 | bindGroupLayouts: null, bindGroupLayoutCount: 0)); 55 | 56 | var _fragment = GPUFragmentState( 57 | module: shader, 58 | targets: GPUColorTargetState( 59 | format: GPUTextureFormat.RGBA8Unorm, 60 | writeMask: WGPUColorWriteMask_All, 61 | blend: GPUBlendState( 62 | color: GPUBlendComponent( 63 | srcFactor: WGPUBlendFactor_One, 64 | dstFactor: WGPUBlendFactor_Zero, 65 | operation: WGPUBlendOperation_Add), 66 | alpha: GPUBlendComponent( 67 | srcFactor: WGPUBlendFactor_One, 68 | dstFactor: WGPUBlendFactor_Zero, 69 | operation: WGPUBlendOperation_Add), 70 | )), 71 | entryPoint: 'fs_main'); 72 | 73 | var _depthStencilState = GPUDepthStencilState( 74 | format: GPUTextureFormat.RGBA8Unorm, 75 | depthWriteEnabled: false, 76 | depthCompare: GPUCompareFunction.Never, 77 | stencilFront: GPUStencilFaceState(compare: GPUCompareFunction.Always), 78 | stencilBack: GPUStencilFaceState(compare: GPUCompareFunction.Always), 79 | ); 80 | 81 | var pipeline = device.createRenderPipeline(GPURenderPipelineDescriptor( 82 | layout: pipelineLayout, 83 | vertex: GPUVertexState(module: shader, entryPoint: "vs_main"), 84 | primitive: GPUPrimitiveState(), 85 | multisample: GPUMultisampleState(), 86 | // depthStencil: _depthStencilState, 87 | fragment: _fragment)); 88 | 89 | var _textureExtent = GPUExtent3D(width: width, height: height); 90 | var textureDesc = GPUTextureDescriptor( 91 | format: GPUTextureFormat.RGBA8Unorm, 92 | size: _textureExtent, 93 | usage: GPUTextureUsage.RenderAttachment | GPUTextureUsage.CopySrc); 94 | 95 | var texture = device.createTexture(textureDesc); 96 | GPUTextureView nextTexture = texture.createView(GPUTextureViewDescriptor( 97 | format: GPUTextureFormat.Undefined, mipLevelCount: 0, baseMipLevel: 0)); 98 | var encoder = device.createCommandEncoder(GPUCommandEncoderDescriptor()); 99 | 100 | var color = GPUColor(r: 1.0, g: 0.5, b: 0.3, a: 0.9); 101 | var attach = GPURenderPassColorAttachment( 102 | view: nextTexture, 103 | clearColor: color, 104 | loadOp: GPULoadOp.Clear, 105 | storeOp: GPUStoreOp.Store); 106 | 107 | var renderPass = encoder.beginRenderPass(GPURenderPassDescriptor( 108 | colorAttachments: attach, 109 | // depthStencilAttachment: GPURenderPassDepthStencilAttachment( 110 | // view: nextTexture, 111 | // ) 112 | )); 113 | 114 | renderPass.setPipeline(pipeline); 115 | 116 | renderPass.draw(3, 1, 0, 0); 117 | 118 | renderPass.end(); 119 | 120 | var copyTexture = 121 | GPUImageCopyTexture(texture: texture, origin: GPUOrigin3D()); 122 | 123 | var bufferDesc = GPUBufferDescriptor( 124 | size: bufferSize, 125 | usage: GPUBufferUsage.MapRead | GPUBufferUsage.CopyDst); 126 | var outputBuffer = device.createBuffer(bufferDesc); 127 | var copyBuffer = GPUImageCopyBuffer( 128 | buffer: outputBuffer, bytesPerRow: padded_bytes_per_row); 129 | 130 | encoder.copyTextureToBuffer(copyTexture, copyBuffer, _textureExtent); 131 | 132 | var queue = device.queue; 133 | 134 | var commandBuffer = encoder.finish(GPUCommandBufferDescriptor()); 135 | queue.submit(commandBuffer); 136 | 137 | outputBuffer.mapAsync(mode: WGPUMapMode_Read, size: bufferSize); 138 | 139 | device.poll(true); 140 | 141 | print(" wgpuBufferGetMappedRange bufferSize ${bufferSize} "); 142 | var data = outputBuffer.getMappedRange(offset: 0, size: bufferSize); 143 | print(" data ${data} "); 144 | print(data); 145 | 146 | Pointer pixles = data.cast(); 147 | 148 | outputBuffer.unmap(); 149 | 150 | return pixles.asTypedList(bufferSize); 151 | } 152 | } 153 | -------------------------------------------------------------------------------- /example/lib/TextureCube.dart: -------------------------------------------------------------------------------- 1 | import 'dart:ffi' as ffi; 2 | import 'dart:io'; 3 | import 'dart:typed_data'; 4 | 5 | import 'package:ffi/ffi.dart'; 6 | import 'package:flutter/material.dart'; 7 | import 'package:flutter_webgpu/flutter_webgpu.dart'; 8 | 9 | class TextureCube { 10 | static render(int width, int height) { 11 | int bytes_per_pixel = Uint32List.bytesPerElement; 12 | int unpadded_bytes_per_row = width * bytes_per_pixel; 13 | int align = 256; 14 | int padded_bytes_per_row_padding = 15 | (align - unpadded_bytes_per_row % align) % align; 16 | int padded_bytes_per_row = 17 | unpadded_bytes_per_row + padded_bytes_per_row_padding; 18 | 19 | int bufferSize = padded_bytes_per_row * height; 20 | 21 | var adapter = requestAdapter(GPURequestAdapterOptions()); 22 | var device = adapter.requestDevice(GPUDeviceDescriptor()); 23 | 24 | device.setUncapturedErrorCallback(); 25 | 26 | print("device.value: ${device.device.value} device: ${device.device} "); 27 | print( 28 | "adapter.value: ${adapter.adapter.value} adapter: ${adapter.adapter} "); 29 | 30 | // prettier-ignore 31 | var cubeVertexArray = Float32List.fromList([ 32 | // float4 position, float4 color, float2 uv, 33 | 1, -1, 1, 1, 1, 0, 1, 1, 1, 1, 34 | -1, -1, 1, 1, 0, 0, 1, 1, 0, 1, 35 | -1, -1, -1, 1, 0, 0, 0, 1, 0, 0, 36 | 1, -1, -1, 1, 1, 0, 0, 1, 1, 0, 37 | 1, -1, 1, 1, 1, 0, 1, 1, 1, 1, 38 | -1, -1, -1, 1, 0, 0, 0, 1, 0, 0, 39 | 40 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 41 | 1, -1, 1, 1, 1, 0, 1, 1, 0, 1, 42 | 1, -1, -1, 1, 1, 0, 0, 1, 0, 0, 43 | 1, 1, -1, 1, 1, 1, 0, 1, 1, 0, 44 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 45 | 1, -1, -1, 1, 1, 0, 0, 1, 0, 0, 46 | 47 | -1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 48 | 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 49 | 1, 1, -1, 1, 1, 1, 0, 1, 0, 0, 50 | -1, 1, -1, 1, 0, 1, 0, 1, 1, 0, 51 | -1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 52 | 1, 1, -1, 1, 1, 1, 0, 1, 0, 0, 53 | 54 | -1, -1, 1, 1, 0, 0, 1, 1, 1, 1, 55 | -1, 1, 1, 1, 0, 1, 1, 1, 0, 1, 56 | -1, 1, -1, 1, 0, 1, 0, 1, 0, 0, 57 | -1, -1, -1, 1, 0, 0, 0, 1, 1, 0, 58 | -1, -1, 1, 1, 0, 0, 1, 1, 1, 1, 59 | -1, 1, -1, 1, 0, 1, 0, 1, 0, 0, 60 | 61 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 62 | -1, 1, 1, 1, 0, 1, 1, 1, 0, 1, 63 | -1, -1, 1, 1, 0, 0, 1, 1, 0, 0, 64 | -1, -1, 1, 1, 0, 0, 1, 1, 0, 0, 65 | 1, -1, 1, 1, 1, 0, 1, 1, 1, 0, 66 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 67 | 68 | 1, -1, -1, 1, 1, 0, 0, 1, 1, 1, 69 | -1, -1, -1, 1, 0, 0, 0, 1, 0, 1, 70 | -1, 1, -1, 1, 0, 1, 0, 1, 0, 0, 71 | 1, 1, -1, 1, 1, 1, 0, 1, 1, 0, 72 | 1, -1, -1, 1, 1, 0, 0, 1, 1, 1, 73 | -1, 1, -1, 1, 0, 1, 0, 1, 0, 0, 74 | ]); 75 | 76 | // Create a vertex buffer from the cube data. 77 | var verticesBuffer = device.createBuffer(GPUBufferDescriptor( 78 | size: cubeVertexArray.lengthInBytes, 79 | usage: GPUBufferUsage.Vertex, 80 | mappedAtCreation: true, 81 | )); 82 | 83 | var pointer = 84 | verticesBuffer.getMappedRange(size: cubeVertexArray.lengthInBytes); 85 | Float32List _list = 86 | (pointer.cast()).asTypedList(cubeVertexArray.length); 87 | _list.setAll(0, cubeVertexArray); 88 | verticesBuffer.unmap(); 89 | 90 | String vert1 = """ 91 | struct Uniforms { 92 | modelViewProjectionMatrix : mat4x4, 93 | }; 94 | @binding(0) @group(0) var uniforms : Uniforms; 95 | 96 | struct VertexOutput { 97 | @builtin(position) Position : vec4, 98 | @location(0) fragUV : vec2, 99 | @location(1) fragPosition: vec4, 100 | }; 101 | 102 | @vertex 103 | fn main(@location(0) position : vec4, 104 | @location(1) uv : vec2) -> VertexOutput { 105 | var output : VertexOutput; 106 | output.Position = uniforms.modelViewProjectionMatrix * position; 107 | output.fragUV = uv; 108 | output.fragPosition = 0.5 * (position + vec4(1.0, 1.0, 1.0, 1.0)); 109 | return output; 110 | } 111 | """; 112 | 113 | String frag1 = """ 114 | @binding(1) @group(0) var mySampler: sampler; 115 | @binding(2) @group(0) var myTexture: texture_2d; 116 | 117 | @fragment 118 | fn main(@location(0) fragUV: vec2, 119 | @location(1) fragPosition: vec4) -> @location(0) vec4 { 120 | return textureSample(myTexture, mySampler, fragUV) * fragPosition; 121 | } 122 | """; 123 | 124 | int cubeVertexSize = 4 * 10; // Byte size of one cube vertex. 125 | int cubePositionOffset = 0; 126 | int cubeColorOffset = 4 * 4; // Byte offset of cube vertex color attribute. 127 | int cubeUVOffset = 4 * 8; 128 | int cubeVertexCount = 36; 129 | 130 | var vertModule = device.createShaderModule(GPUShaderModuleDescriptor( 131 | code: vert1, 132 | )); 133 | 134 | var fragModule = device.createShaderModule(GPUShaderModuleDescriptor( 135 | code: frag1, 136 | )); 137 | 138 | var vertex3 = 139 | GPUVertexState(module: vertModule, entryPoint: 'main', buffers: [ 140 | GPUVertexBufferLayout(arrayStride: cubeVertexSize, attributes: [ 141 | GPUVertexAttribute( 142 | format: GPUVertexFormat.Float32x4, 143 | offset: cubePositionOffset, 144 | shaderLocation: 0), 145 | GPUVertexAttribute( 146 | format: GPUVertexFormat.Float32x2, 147 | offset: cubeUVOffset, 148 | shaderLocation: 1) 149 | ]) 150 | ]); 151 | 152 | var uniformGroupLayout = 153 | device.createBindGroupLayout(GPUBindGroupLayoutDescriptor(entries: [ 154 | GPUBindGroupLayoutEntry( 155 | binding: 0, 156 | visibility: GPUShaderStage.Vertex, 157 | buffer: GPUBufferBindingLayout(type: GPUBufferBindingType.Uniform)), 158 | GPUBindGroupLayoutEntry( 159 | binding: 1, 160 | visibility: GPUShaderStage.Fragment, 161 | buffer: GPUBufferBindingLayout(type: GPUBufferBindingType.Storage)), 162 | GPUBindGroupLayoutEntry( 163 | binding: 2, 164 | visibility: GPUShaderStage.Fragment, 165 | buffer: GPUBufferBindingLayout(type: GPUBufferBindingType.Uniform)) 166 | ])); 167 | 168 | var layout = device.createPipelineLayout(GPUPipelineLayoutDescriptor( 169 | bindGroupLayouts: uniformGroupLayout, bindGroupLayoutCount: 1)); 170 | 171 | var desc = GPURenderPipelineDescriptor( 172 | // layout: layout, 173 | vertex: vertex3, 174 | fragment: GPUFragmentState( 175 | module: fragModule, 176 | entryPoint: 'main', 177 | targets: GPUColorTargetState( 178 | format: GPUTextureFormat.RGBA8Unorm, 179 | ), 180 | ), 181 | primitive: GPUPrimitiveState( 182 | cullMode: GPUCullMode.Back, frontFace: GPUFrontFace.CCW), 183 | multisample: GPUMultisampleState(), 184 | // depthStencil: GPUDepthStencilState( 185 | // depthWriteEnabled: true, 186 | // depthCompare: GPUCompareFunction.Greater, 187 | // format: GPUTextureFormat.Depth24Plus, 188 | // stencilFront: 189 | // GPUStencilFaceState(compare: GPUCompareFunction.Always), 190 | // stencilBack: 191 | // GPUStencilFaceState(compare: GPUCompareFunction.Always), 192 | // depthBias: 2, 193 | // depthBiasSlopeScale: 2.0, 194 | // depthBiasClamp: 0.0) 195 | ); 196 | 197 | var pipeline = device.createRenderPipeline(desc); 198 | 199 | var presentationSize = GPUExtent3D(width: width, height: height); 200 | 201 | var depthTexture = device.createTexture(GPUTextureDescriptor( 202 | size: presentationSize, 203 | format: GPUTextureFormat.Depth24Plus, 204 | usage: GPUTextureUsage.RenderAttachment)); 205 | 206 | var uniformBufferSize = 4 * 16; // 4x4 matrix 207 | var uniformBuffer = device.createBuffer(GPUBufferDescriptor( 208 | size: uniformBufferSize, 209 | usage: GPUBufferUsage.Uniform | GPUBufferUsage.CopyDst, 210 | )); 211 | 212 | GPUTexture cubeTexture = device.createTexture(GPUTextureDescriptor( 213 | size: presentationSize, 214 | format: GPUTextureFormat.RGBA8Unorm, 215 | usage: GPUTextureUsage.TextureBinding | 216 | GPUTextureUsage.CopyDst | 217 | GPUTextureUsage.RenderAttachment)); 218 | 219 | var pixels = Uint8List(width * height * 4); 220 | 221 | for(int i = 0; i < pixels.length; i ++ ) { 222 | pixels[i] = 255; 223 | } 224 | 225 | 226 | device.queue.writeTexture( 227 | GPUImageCopyTexture(texture: cubeTexture), 228 | pixels, 229 | GPUTextureDataLayout(bytesPerRow: width * 4, offset: 0), 230 | presentationSize); 231 | 232 | var sampler = device.createSampler(GPUSamplerDescriptor( 233 | magFilter: GPUFilterMode.Linear, minFilter: GPUFilterMode.Linear)); 234 | 235 | var bindGroupLayout = pipeline.getBindGroupLayout(0); 236 | 237 | var uniformBindGroup = device.createBindGroup(GPUBindGroupDescriptor( 238 | layout: bindGroupLayout, 239 | entries: [ 240 | GPUBindGroupEntry( 241 | binding: 0, 242 | buffer: uniformBuffer, 243 | ), 244 | GPUBindGroupEntry( 245 | binding: 1, 246 | sampler: sampler, 247 | ), 248 | GPUBindGroupEntry( 249 | binding: 2, 250 | textureView: cubeTexture.createView(), 251 | ), 252 | ], 253 | )); 254 | 255 | Float32List transformationMatrix = Float32List.fromList( 256 | [1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0, 0, 1, 0, 0, 0, 0, 1]); 257 | 258 | Matrix4 _mat = Matrix4.rotationX(0.36); 259 | _mat.rotateY(0.2); 260 | _mat.scale(0.5, 0.5, 0.5); 261 | 262 | transformationMatrix = Float32List.fromList(_mat.storage); 263 | 264 | // var mp = calloc(transformationMatrix.length); 265 | // Float32List _list2 = (mp as Pointer).asTypedList(transformationMatrix.length); 266 | // _list2.setAll(0, transformationMatrix); 267 | 268 | device.queue.writeBuffer(uniformBuffer, 0, transformationMatrix, 269 | transformationMatrix.lengthInBytes); 270 | 271 | var commandEncoder = device.createCommandEncoder(); 272 | 273 | var _textureExtent = GPUExtent3D(width: width, height: height); 274 | var textureDesc = GPUTextureDescriptor( 275 | format: GPUTextureFormat.RGBA8Unorm, 276 | size: _textureExtent, 277 | usage: GPUTextureUsage.RenderAttachment | GPUTextureUsage.CopySrc); 278 | 279 | var texture = device.createTexture(textureDesc); 280 | GPUTextureView textureView = texture.createView(GPUTextureViewDescriptor()); 281 | 282 | var texture0 = device.createTexture(GPUTextureDescriptor( 283 | format: GPUTextureFormat.RGBA8Unorm, 284 | size: _textureExtent, 285 | usage: GPUTextureUsage.RenderAttachment | GPUTextureUsage.CopySrc, 286 | sampleCount: 1)); 287 | GPUTextureView textureView0 = 288 | texture0.createView(GPUTextureViewDescriptor()); 289 | 290 | var renderPassDescriptor = GPURenderPassDescriptor( 291 | colorAttachments: GPURenderPassColorAttachment( 292 | // view: textureView0, 293 | // resolveTarget: textureView, 294 | view: textureView, 295 | clearColor: GPUColor(r: 0.5, g: 0.5, b: 0.0, a: 0.5), 296 | storeOp: GPUStoreOp.Store, 297 | loadOp: GPULoadOp.Clear), 298 | // depthStencilAttachment: GPURenderPassDepthStencilAttachment( 299 | // view: depthTexture.createView(GPUTextureViewDescriptor()), 300 | // // depthLoadOp: GPULoadOp.Clear, 301 | // // depthStoreOp: GPUStoreOp.Store, 302 | // // depthClearValue: 0.0, 303 | // // stencilLoadOp: GPULoadOp.Clear, 304 | // // stencilStoreOp: GPUStoreOp.Store, 305 | // // stencilClearValue: 0, 306 | // ), 307 | ); 308 | 309 | var passEncoder = commandEncoder.beginRenderPass(renderPassDescriptor); 310 | passEncoder.setPipeline(pipeline); 311 | passEncoder.setBindGroup(0, uniformBindGroup); 312 | passEncoder.setVertexBuffer(0, verticesBuffer); 313 | passEncoder.draw(cubeVertexCount, 1, 0, 0); 314 | passEncoder.end(); 315 | 316 | var copyTexture = 317 | GPUImageCopyTexture(texture: texture, origin: GPUOrigin3D()); 318 | 319 | var bufferDesc = GPUBufferDescriptor( 320 | size: bufferSize, 321 | usage: GPUBufferUsage.MapRead | GPUBufferUsage.CopyDst); 322 | var outputBuffer = device.createBuffer(bufferDesc); 323 | var copyBuffer = GPUImageCopyBuffer( 324 | buffer: outputBuffer, bytesPerRow: padded_bytes_per_row); 325 | 326 | commandEncoder.copyTextureToBuffer(copyTexture, copyBuffer, _textureExtent); 327 | 328 | var commandBuffer = commandEncoder.finish(GPUCommandBufferDescriptor()); 329 | device.queue.submit(commandBuffer); 330 | 331 | outputBuffer.mapAsync(mode: WGPUMapMode_Read, size: bufferSize); 332 | 333 | device.poll(true); 334 | 335 | var data = outputBuffer.getMappedRange(offset: 0); 336 | ffi.Pointer pixles = data.cast(); 337 | outputBuffer.unmap(); 338 | 339 | return pixles.asTypedList(bufferSize); 340 | } 341 | } 342 | -------------------------------------------------------------------------------- /example/lib/helloTriangle.dart: -------------------------------------------------------------------------------- 1 | import 'dart:ffi'; 2 | import 'dart:io'; 3 | import 'dart:typed_data'; 4 | 5 | import 'package:ffi/ffi.dart' as ffi; 6 | import 'package:flutter_webgpu/flutter_webgpu.dart'; 7 | 8 | class HelloTriangle { 9 | static render(int width, int height) { 10 | int bytes_per_pixel = Uint32List.bytesPerElement; 11 | int unpadded_bytes_per_row = width * bytes_per_pixel; 12 | int align = 256; 13 | int padded_bytes_per_row_padding = 14 | (align - unpadded_bytes_per_row % align) % align; 15 | int padded_bytes_per_row = 16 | unpadded_bytes_per_row + padded_bytes_per_row_padding; 17 | 18 | int bufferSize = padded_bytes_per_row * height; 19 | 20 | var adapter = requestAdapter(GPURequestAdapterOptions()); 21 | var device = adapter.requestDevice(GPUDeviceDescriptor()); 22 | 23 | device.setUncapturedErrorCallback(); 24 | 25 | print("device.value: ${device.device.value} device: ${device.device} "); 26 | print( 27 | "adapter.value: ${adapter.adapter.value} adapter: ${adapter.adapter} "); 28 | 29 | var pipeline = device.createRenderPipeline(GPURenderPipelineDescriptor( 30 | layout: device.createPipelineLayout(GPUPipelineLayoutDescriptor( 31 | bindGroupLayouts: null, bindGroupLayoutCount: 0)), 32 | vertex: GPUVertexState( 33 | module: device.createShaderModule(GPUShaderModuleDescriptor( 34 | code: """ 35 | @vertex 36 | fn main(@builtin(vertex_index) VertexIndex : u32) 37 | -> @builtin(position) vec4 { 38 | var pos = array, 3>( 39 | vec2(0.0, 0.5), 40 | vec2(-0.5, -0.5), 41 | vec2(0.5, -0.5)); 42 | 43 | return vec4(pos[VertexIndex], 0.0, 1.0); 44 | } 45 | """, 46 | )), 47 | entryPoint: 'main', 48 | ), 49 | fragment: GPUFragmentState( 50 | module: device.createShaderModule(GPUShaderModuleDescriptor( 51 | code: """ 52 | @fragment 53 | fn main() -> @location(0) vec4 { 54 | return vec4(1.0, 0.0, 0.0, 1.0); 55 | } 56 | """, 57 | )), 58 | entryPoint: 'main', 59 | targets: GPUColorTargetState( 60 | format: GPUTextureFormat.RGBA8Unorm, 61 | ), 62 | ), 63 | primitive: GPUPrimitiveState(), 64 | multisample: GPUMultisampleState(), 65 | )); 66 | 67 | print(" HelloTriangle pipeline: ${pipeline.pipeline} "); 68 | 69 | var commandEncoder = device.createCommandEncoder(); 70 | 71 | var _textureExtent = GPUExtent3D(width: width, height: height); 72 | var textureDesc = GPUTextureDescriptor( 73 | format: GPUTextureFormat.RGBA8Unorm, 74 | size: _textureExtent, 75 | usage: GPUTextureUsage.RenderAttachment | GPUTextureUsage.CopySrc); 76 | 77 | var texture = device.createTexture(textureDesc); 78 | GPUTextureView textureView = texture.createView(GPUTextureViewDescriptor()); 79 | 80 | var renderPassDescriptor = GPURenderPassDescriptor( 81 | colorAttachments: GPURenderPassColorAttachment( 82 | view: textureView, 83 | clearColor: GPUColor(r: 0.0, g: 0.0, b: 0.0, a: 1.0), 84 | loadOp: GPULoadOp.Clear, 85 | storeOp: GPUStoreOp.Store)); 86 | 87 | var passEncoder = commandEncoder.beginRenderPass(renderPassDescriptor); 88 | passEncoder.setPipeline(pipeline); 89 | passEncoder.draw(3, 1, 0, 0); 90 | passEncoder.end(); 91 | 92 | var copyTexture = 93 | GPUImageCopyTexture(texture: texture, origin: GPUOrigin3D()); 94 | 95 | var bufferDesc = GPUBufferDescriptor( 96 | size: bufferSize, 97 | usage: GPUBufferUsage.MapRead | GPUBufferUsage.CopyDst); 98 | var outputBuffer = device.createBuffer(bufferDesc); 99 | var copyBuffer = GPUImageCopyBuffer( 100 | buffer: outputBuffer, bytesPerRow: padded_bytes_per_row); 101 | 102 | commandEncoder.copyTextureToBuffer(copyTexture, copyBuffer, _textureExtent); 103 | 104 | var commandBuffer = commandEncoder.finish(GPUCommandBufferDescriptor()); 105 | device.queue.submit(commandBuffer); 106 | 107 | outputBuffer.mapAsync(mode: WGPUMapMode_Read, size: bufferSize); 108 | 109 | device.poll(true); 110 | 111 | var data = outputBuffer.getMappedRange(offset: 0, size: bufferSize); 112 | Pointer pixles = data.cast(); 113 | outputBuffer.unmap(); 114 | 115 | return pixles.asTypedList(bufferSize); 116 | } 117 | } 118 | -------------------------------------------------------------------------------- /example/lib/helloTriangle2.dart: -------------------------------------------------------------------------------- 1 | import 'dart:ffi'; 2 | import 'dart:io'; 3 | import 'dart:typed_data'; 4 | 5 | import 'package:ffi/ffi.dart' as ffi; 6 | import 'package:flutter_webgpu/flutter_webgpu.dart'; 7 | 8 | class HelloTriangle2 { 9 | static render(int width, int height) { 10 | int bytes_per_pixel = Uint32List.bytesPerElement; 11 | int unpadded_bytes_per_row = width * bytes_per_pixel; 12 | int align = 256; 13 | int padded_bytes_per_row_padding = 14 | (align - unpadded_bytes_per_row % align) % align; 15 | int padded_bytes_per_row = 16 | unpadded_bytes_per_row + padded_bytes_per_row_padding; 17 | 18 | int bufferSize = padded_bytes_per_row * height; 19 | 20 | var adapter = requestAdapter(GPURequestAdapterOptions()); 21 | var device = adapter.requestDevice(GPUDeviceDescriptor()); 22 | 23 | device.setUncapturedErrorCallback(); 24 | 25 | print("device.value: ${device.device.value} device: ${device.device} "); 26 | print( 27 | "adapter.value: ${adapter.adapter.value} adapter: ${adapter.adapter} "); 28 | 29 | 30 | String vert = """ 31 | struct PositionColorInput { 32 | @location(0) in_position_2d: vec2, 33 | @location(1) in_color_rgba: vec4, 34 | }; 35 | 36 | struct PositionColorOutput { 37 | @builtin(position) coords_output: vec4, 38 | @location(0) color_output: vec4, 39 | }; 40 | 41 | 42 | struct Uniforms { 43 | uniform_color2: vec4, 44 | uniform_color: vec4, 45 | }; 46 | 47 | @binding(0) @group(0) var uniforms: Uniforms; 48 | @binding(1) @group(0) var uniforms2: Uniforms; 49 | 50 | @vertex 51 | fn main_vs(input: PositionColorInput) 52 | -> PositionColorOutput { 53 | var output: PositionColorOutput; 54 | output.color_output = input.in_color_rgba; 55 | output.coords_output = vec4(input.in_position_2d, 0.0, 1.0); 56 | return output; 57 | } 58 | 59 | @fragment 60 | fn main_fs(output: PositionColorOutput) -> @location(0) vec4 { 61 | // return output.color_output; 62 | return uniforms2.uniform_color2; 63 | } 64 | """; 65 | 66 | 67 | var vbodata = new Float32List.fromList([ 68 | // 坐标 xy // 颜色 RGBA 69 | 0.0, 0.5, 1.0, 0.0, 0.0, 1.0, // ← 顶点 1 70 | -0.5, -0.5, 0.0, 1.0, 0.0, 1.0, // ← 顶点 2 71 | 0.5, -0.5, 0.0, 0.0, 1.0, 1.0 // ← 顶点 3 72 | ]); 73 | var verticesBuffer = device.createBuffer(GPUBufferDescriptor( 74 | size: vbodata.lengthInBytes, 75 | usage: GPUBufferUsage.Vertex, 76 | mappedAtCreation: true, 77 | )); 78 | 79 | var pointer = 80 | verticesBuffer.getMappedRange(size: vbodata.lengthInBytes); 81 | Float32List _list = 82 | (pointer.cast()).asTypedList(vbodata.length); 83 | _list.setAll(0, vbodata); 84 | verticesBuffer.unmap(); 85 | 86 | 87 | var shaderModule = device.createShaderModule(GPUShaderModuleDescriptor( 88 | code: vert, 89 | )); 90 | 91 | 92 | var bindGroupLayout = device.createBindGroupLayout( 93 | GPUBindGroupLayoutDescriptor( 94 | entries: [ 95 | GPUBindGroupLayoutEntry( 96 | binding: 0, 97 | visibility: GPUShaderStage.Fragment, 98 | buffer: GPUBufferBindingLayout( 99 | type: GPUBufferBindingType.Uniform 100 | ) 101 | ), 102 | GPUBindGroupLayoutEntry( 103 | binding: 1, 104 | visibility: GPUShaderStage.Fragment, 105 | buffer: GPUBufferBindingLayout( 106 | type: GPUBufferBindingType.Uniform 107 | ) 108 | ) 109 | ] 110 | ) 111 | ); 112 | 113 | 114 | 115 | 116 | var pipelineLayout = device.createPipelineLayout(GPUPipelineLayoutDescriptor( 117 | bindGroupLayouts: bindGroupLayout, bindGroupLayoutCount: 1)); 118 | 119 | 120 | 121 | var pipeline = device.createRenderPipeline(GPURenderPipelineDescriptor( 122 | layout: pipelineLayout, 123 | vertex: GPUVertexState( 124 | module: shaderModule, 125 | entryPoint: 'main_vs', 126 | buffers: [ 127 | GPUVertexBufferLayout(arrayStride: (2 + 4) * Float32List.bytesPerElement, attributes: [ 128 | GPUVertexAttribute( 129 | format: GPUVertexFormat.Float32x2, 130 | offset: 0, 131 | shaderLocation: 0), 132 | GPUVertexAttribute( 133 | format: GPUVertexFormat.Float32x4, 134 | offset: 2 * Float32List.bytesPerElement, 135 | shaderLocation: 1) 136 | ]) 137 | ] 138 | ), 139 | fragment: GPUFragmentState( 140 | module: shaderModule, 141 | entryPoint: 'main_fs', 142 | targets: GPUColorTargetState( 143 | format: GPUTextureFormat.RGBA8Unorm, 144 | ), 145 | ), 146 | primitive: GPUPrimitiveState(), 147 | multisample: GPUMultisampleState(), 148 | )); 149 | 150 | 151 | Float32List uniforms = Float32List.fromList([ 152 | 1.0, 1.0, 0.0, 1.0, 153 | 0.5, 0.5, 0.0, 1.0 154 | ]); 155 | var uniformsBuffer = device.createBuffer(GPUBufferDescriptor( 156 | size: uniforms.lengthInBytes, 157 | usage: GPUBufferUsage.Uniform, 158 | mappedAtCreation: true, 159 | )); 160 | 161 | var pointer2 = 162 | uniformsBuffer.getMappedRange(size: uniforms.lengthInBytes); 163 | (pointer2.cast()).asTypedList(uniforms.length).setAll(0, uniforms); 164 | 165 | uniformsBuffer.unmap(); 166 | 167 | 168 | Float32List uniforms2 = Float32List.fromList([ 169 | 0.0, 1.0, 1.0, 1.0, 170 | 0.0, 0.5, 0.5, 1.0 171 | ]); 172 | var uniformsBuffer2 = device.createBuffer(GPUBufferDescriptor( 173 | size: uniforms2.lengthInBytes, 174 | usage: GPUBufferUsage.Uniform, 175 | mappedAtCreation: true, 176 | )); 177 | 178 | var pointer22 = 179 | uniformsBuffer2.getMappedRange(size: uniforms2.lengthInBytes); 180 | (pointer22.cast()).asTypedList(uniforms2.length).setAll(0, uniforms2); 181 | 182 | uniformsBuffer2.unmap(); 183 | 184 | 185 | 186 | 187 | var bindGroup = device.createBindGroup( 188 | GPUBindGroupDescriptor( 189 | layout: bindGroupLayout, 190 | entries: [ 191 | GPUBindGroupEntry(binding: 0, buffer: uniformsBuffer), 192 | GPUBindGroupEntry(binding: 1, buffer: uniformsBuffer2) 193 | ], 194 | entryCount: 2 195 | ) 196 | ); 197 | 198 | 199 | 200 | var commandEncoder = device.createCommandEncoder(); 201 | 202 | var _textureExtent = GPUExtent3D(width: width, height: height); 203 | var textureDesc = GPUTextureDescriptor( 204 | format: GPUTextureFormat.RGBA8Unorm, 205 | size: _textureExtent, 206 | usage: GPUTextureUsage.RenderAttachment | GPUTextureUsage.CopySrc); 207 | 208 | var texture = device.createTexture(textureDesc); 209 | GPUTextureView textureView = texture.createView(GPUTextureViewDescriptor()); 210 | 211 | var renderPassDescriptor = GPURenderPassDescriptor( 212 | colorAttachments: GPURenderPassColorAttachment( 213 | view: textureView, 214 | clearColor: GPUColor(r: 0.0, g: 0.0, b: 0.0, a: 1.0), 215 | loadOp: GPULoadOp.Clear, 216 | storeOp: GPUStoreOp.Store)); 217 | 218 | 219 | 220 | var passEncoder = commandEncoder.beginRenderPass(renderPassDescriptor); 221 | passEncoder.setPipeline(pipeline); 222 | passEncoder.setBindGroup(0, bindGroup); 223 | passEncoder.setVertexBuffer(0, verticesBuffer); 224 | passEncoder.draw(3, 1, 0, 0); 225 | passEncoder.end(); 226 | 227 | 228 | var commandBuffer = commandEncoder.finish(GPUCommandBufferDescriptor()); 229 | device.queue.submit(commandBuffer); 230 | 231 | // device.poll(true); 232 | 233 | 234 | var commandEncoder2 = device.createCommandEncoder(); 235 | var copyTexture = 236 | GPUImageCopyTexture(texture: texture, origin: GPUOrigin3D()); 237 | 238 | var bufferDesc = GPUBufferDescriptor( 239 | size: bufferSize, 240 | usage: GPUBufferUsage.MapRead | GPUBufferUsage.CopyDst); 241 | var outputBuffer = device.createBuffer(bufferDesc); 242 | var copyBuffer = GPUImageCopyBuffer( 243 | buffer: outputBuffer, bytesPerRow: padded_bytes_per_row); 244 | 245 | commandEncoder2.copyTextureToBuffer(copyTexture, copyBuffer, _textureExtent); 246 | 247 | 248 | 249 | var commandBuffer2 = commandEncoder2.finish(GPUCommandBufferDescriptor()); 250 | device.queue.submit(commandBuffer2); 251 | 252 | outputBuffer.mapAsync(mode: WGPUMapMode_Read, size: bufferSize); 253 | 254 | device.poll(true); 255 | 256 | var data = outputBuffer.getMappedRange(offset: 0, size: bufferSize); 257 | Pointer pixles = data.cast(); 258 | outputBuffer.unmap(); 259 | 260 | return pixles.asTypedList(bufferSize); 261 | } 262 | } 263 | -------------------------------------------------------------------------------- /example/lib/helloTriangleMSAA.dart: -------------------------------------------------------------------------------- 1 | import 'dart:ffi'; 2 | import 'dart:io'; 3 | import 'dart:typed_data'; 4 | 5 | import 'package:ffi/ffi.dart' as ffi; 6 | import 'package:flutter_webgpu/flutter_webgpu.dart'; 7 | 8 | class HelloTriangleMSAA { 9 | static render(int width, int height) { 10 | int bytes_per_pixel = Uint32List.bytesPerElement; 11 | int unpadded_bytes_per_row = width * bytes_per_pixel; 12 | int align = 256; 13 | int padded_bytes_per_row_padding = 14 | (align - unpadded_bytes_per_row % align) % align; 15 | int padded_bytes_per_row = 16 | unpadded_bytes_per_row + padded_bytes_per_row_padding; 17 | 18 | int bufferSize = padded_bytes_per_row * height; 19 | 20 | var adapter = requestAdapter(GPURequestAdapterOptions()); 21 | var device = adapter.requestDevice(GPUDeviceDescriptor()); 22 | 23 | device.setUncapturedErrorCallback(); 24 | 25 | print("device.value: ${device.device.value} device: ${device.device} "); 26 | print( 27 | "adapter.value: ${adapter.adapter.value} adapter: ${adapter.adapter} "); 28 | 29 | var pipeline = device.createRenderPipeline(GPURenderPipelineDescriptor( 30 | layout: device.createPipelineLayout(GPUPipelineLayoutDescriptor( 31 | bindGroupLayouts: null, bindGroupLayoutCount: 0)), 32 | vertex: GPUVertexState( 33 | module: device.createShaderModule(GPUShaderModuleDescriptor( 34 | code: """ 35 | @vertex 36 | fn main(@builtin(vertex_index) VertexIndex : u32) 37 | -> @builtin(position) vec4 { 38 | var pos = array, 3>( 39 | vec2(0.0, 0.5), 40 | vec2(-0.5, -0.5), 41 | vec2(0.5, -0.5)); 42 | 43 | return vec4(pos[VertexIndex], 0.0, 1.0); 44 | } 45 | """, 46 | )), 47 | entryPoint: 'main', 48 | ), 49 | fragment: GPUFragmentState( 50 | module: device.createShaderModule(GPUShaderModuleDescriptor( 51 | code: """ 52 | @fragment 53 | fn main() -> @location(0) vec4 { 54 | return vec4(1.0, 0.0, 0.0, 1.0); 55 | } 56 | """, 57 | )), 58 | entryPoint: 'main', 59 | targets: GPUColorTargetState( 60 | format: GPUTextureFormat.RGBA8Unorm, 61 | ), 62 | ), 63 | primitive: GPUPrimitiveState(), 64 | multisample: GPUMultisampleState(count: 4), 65 | )); 66 | 67 | print(" HelloTriangle pipeline: ${pipeline.pipeline} "); 68 | 69 | var commandEncoder = device.createCommandEncoder(); 70 | 71 | var _textureExtent = GPUExtent3D(width: width, height: height); 72 | var textureDesc = GPUTextureDescriptor( 73 | format: GPUTextureFormat.RGBA8Unorm, 74 | size: _textureExtent, 75 | usage: GPUTextureUsage.RenderAttachment | GPUTextureUsage.CopySrc); 76 | 77 | var texture = device.createTexture(textureDesc); 78 | GPUTextureView textureView = texture.createView(GPUTextureViewDescriptor()); 79 | 80 | var texture0 = device.createTexture(GPUTextureDescriptor( 81 | format: GPUTextureFormat.RGBA8Unorm, 82 | size: _textureExtent, 83 | usage: GPUTextureUsage.RenderAttachment | GPUTextureUsage.CopySrc, 84 | sampleCount: 4)); 85 | GPUTextureView textureView0 = 86 | texture0.createView(GPUTextureViewDescriptor()); 87 | 88 | var renderPassDescriptor = GPURenderPassDescriptor( 89 | colorAttachments: GPURenderPassColorAttachment( 90 | view: textureView0, 91 | resolveTarget: textureView, 92 | clearColor: GPUColor(r: 0.0, g: 0.0, b: 0.0, a: 1.0), 93 | loadOp: GPULoadOp.Load, 94 | storeOp: GPUStoreOp.Store)); 95 | 96 | var passEncoder = commandEncoder.beginRenderPass(renderPassDescriptor); 97 | passEncoder.setPipeline(pipeline); 98 | passEncoder.draw(3, 1, 0, 0); 99 | passEncoder.end(); 100 | 101 | var copyTexture = 102 | GPUImageCopyTexture(texture: texture, origin: GPUOrigin3D()); 103 | 104 | var bufferDesc = GPUBufferDescriptor( 105 | size: bufferSize, 106 | usage: GPUBufferUsage.MapRead | GPUBufferUsage.CopyDst); 107 | var outputBuffer = device.createBuffer(bufferDesc); 108 | var copyBuffer = GPUImageCopyBuffer( 109 | buffer: outputBuffer, bytesPerRow: padded_bytes_per_row); 110 | 111 | commandEncoder.copyTextureToBuffer(copyTexture, copyBuffer, _textureExtent); 112 | 113 | var commandBuffer = commandEncoder.finish(GPUCommandBufferDescriptor()); 114 | device.queue.submit(commandBuffer); 115 | 116 | outputBuffer.mapAsync(mode: WGPUMapMode_Read, size: bufferSize); 117 | 118 | device.poll(true); 119 | 120 | var data = outputBuffer.getMappedRange(offset: 0, size: bufferSize); 121 | Pointer pixles = data.cast(); 122 | outputBuffer.unmap(); 123 | 124 | return pixles.asTypedList(bufferSize); 125 | } 126 | } 127 | -------------------------------------------------------------------------------- /example/lib/main.dart: -------------------------------------------------------------------------------- 1 | import 'dart:typed_data'; 2 | import 'dart:ui' as ui; 3 | 4 | import 'package:flutter/material.dart'; 5 | import 'dart:async'; 6 | 7 | import 'package:flutter/services.dart'; 8 | import 'package:flutter_webgpu/flutter_webgpu.dart'; 9 | import 'package:flutter_webgpu_example/Example.dart'; 10 | import 'package:flutter_webgpu_example/ExampleCapture.dart'; 11 | import 'package:flutter_webgpu_example/ExampleCompute.dart'; 12 | import 'package:flutter_webgpu_example/ExampleTriangle.dart'; 13 | import 'package:flutter_webgpu_example/rotateCube.dart'; 14 | import 'package:flutter_webgpu_example/TextureCube.dart'; 15 | 16 | import 'helloTriangle2.dart'; 17 | import 'boids.dart'; 18 | import 'helloTriangle.dart'; 19 | import 'helloTriangleMSAA.dart'; 20 | 21 | void main() { 22 | runApp(const MyApp()); 23 | } 24 | 25 | class MyApp extends StatefulWidget { 26 | const MyApp({Key? key}) : super(key: key); 27 | 28 | @override 29 | State createState() => _MyAppState(); 30 | } 31 | 32 | class _MyAppState extends State { 33 | String _platformVersion = 'Unknown'; 34 | 35 | Uint8List? pixels; 36 | ui.Image? img; 37 | 38 | int width = 512; 39 | int height = 512; 40 | 41 | @override 42 | void initState() { 43 | super.initState(); 44 | initPlatformState(); 45 | 46 | init(); 47 | } 48 | 49 | init() { 50 | // pixels = Example.render(width, height); 51 | // pixels = ExampleCapture.render(width, height); 52 | // pixels = ExampleCompute.render(width, height); 53 | // pixels = ExampleTriangle.render(width, height); 54 | // pixels = HelloTriangle.render(width, height); 55 | // pixels = HelloTriangleMSAA.render(width, height); 56 | // pixels = RotateCube.render(width, height); 57 | 58 | // pixels = HelloTriangle2.render(width, height); 59 | 60 | // crash TODO 61 | // pixels = TextureCube.render(width, height); 62 | pixels = Boids.render(width, height); 63 | 64 | if (pixels != null) { 65 | ui.decodeImageFromPixels(pixels!, width, height, ui.PixelFormat.rgba8888, 66 | (image) { 67 | setState(() { 68 | img = image; 69 | }); 70 | }); 71 | } 72 | } 73 | 74 | // Platform messages are asynchronous, so we initialize in an async method. 75 | Future initPlatformState() async { 76 | String platformVersion; 77 | // Platform messages may fail, so we use a try/catch PlatformException. 78 | // We also handle the message potentially returning null. 79 | try { 80 | platformVersion = 81 | await FlutterWebgpu.platformVersion ?? 'Unknown platform version'; 82 | } on PlatformException { 83 | platformVersion = 'Failed to get platform version.'; 84 | } 85 | 86 | // If the widget was removed from the tree while the asynchronous platform 87 | // message was in flight, we want to discard the reply rather than calling 88 | // setState to update our non-existent appearance. 89 | if (!mounted) return; 90 | 91 | setState(() { 92 | _platformVersion = platformVersion; 93 | }); 94 | } 95 | 96 | @override 97 | Widget build(BuildContext context) { 98 | return MaterialApp( 99 | home: Scaffold( 100 | floatingActionButton: FloatingActionButton( 101 | child: Text("render"), 102 | onPressed: () { 103 | init(); 104 | }, 105 | ), 106 | appBar: AppBar( 107 | title: const Text('Plugin example app'), 108 | ), 109 | body: Column( 110 | children: [ 111 | Container( 112 | child: Text('Running on: $_platformVersion\n'), 113 | ), 114 | Container( 115 | color: Colors.black, 116 | width: width.toDouble(), 117 | height: height.toDouble(), 118 | child: RawImage(image: img), 119 | ) 120 | ], 121 | ), 122 | ), 123 | ); 124 | } 125 | } 126 | -------------------------------------------------------------------------------- /example/lib/rotateCube.dart: -------------------------------------------------------------------------------- 1 | import 'dart:ffi'; 2 | import 'dart:io'; 3 | import 'dart:typed_data'; 4 | 5 | import 'package:ffi/ffi.dart' as ffi; 6 | import 'package:ffi/ffi.dart'; 7 | import 'package:flutter/material.dart'; 8 | import 'package:flutter_webgpu/flutter_webgpu.dart'; 9 | 10 | class RotateCube { 11 | static render(int width, int height) { 12 | int bytes_per_pixel = Uint32List.bytesPerElement; 13 | int unpadded_bytes_per_row = width * bytes_per_pixel; 14 | int align = 256; 15 | int padded_bytes_per_row_padding = 16 | (align - unpadded_bytes_per_row % align) % align; 17 | int padded_bytes_per_row = 18 | unpadded_bytes_per_row + padded_bytes_per_row_padding; 19 | 20 | int bufferSize = padded_bytes_per_row * height; 21 | 22 | var adapter = requestAdapter(GPURequestAdapterOptions()); 23 | var device = adapter.requestDevice(GPUDeviceDescriptor()); 24 | 25 | device.setUncapturedErrorCallback(); 26 | 27 | print("device.value: ${device.device.value} device: ${device.device} "); 28 | print( 29 | "adapter.value: ${adapter.adapter.value} adapter: ${adapter.adapter} "); 30 | 31 | // prettier-ignore 32 | var cubeVertexArray = Float32List.fromList([ 33 | // float4 position, float4 color, float2 uv, 34 | 1, -1, 1, 1, 1, 0, 1, 1, 1, 1, 35 | -1, -1, 1, 1, 0, 0, 1, 1, 0, 1, 36 | -1, -1, -1, 1, 0, 0, 0, 1, 0, 0, 37 | 1, -1, -1, 1, 1, 0, 0, 1, 1, 0, 38 | 1, -1, 1, 1, 1, 0, 1, 1, 1, 1, 39 | -1, -1, -1, 1, 0, 0, 0, 1, 0, 0, 40 | 41 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 42 | 1, -1, 1, 1, 1, 0, 1, 1, 0, 1, 43 | 1, -1, -1, 1, 1, 0, 0, 1, 0, 0, 44 | 1, 1, -1, 1, 1, 1, 0, 1, 1, 0, 45 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 46 | 1, -1, -1, 1, 1, 0, 0, 1, 0, 0, 47 | 48 | -1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 49 | 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 50 | 1, 1, -1, 1, 1, 1, 0, 1, 0, 0, 51 | -1, 1, -1, 1, 0, 1, 0, 1, 1, 0, 52 | -1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 53 | 1, 1, -1, 1, 1, 1, 0, 1, 0, 0, 54 | 55 | -1, -1, 1, 1, 0, 0, 1, 1, 1, 1, 56 | -1, 1, 1, 1, 0, 1, 1, 1, 0, 1, 57 | -1, 1, -1, 1, 0, 1, 0, 1, 0, 0, 58 | -1, -1, -1, 1, 0, 0, 0, 1, 1, 0, 59 | -1, -1, 1, 1, 0, 0, 1, 1, 1, 1, 60 | -1, 1, -1, 1, 0, 1, 0, 1, 0, 0, 61 | 62 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 63 | -1, 1, 1, 1, 0, 1, 1, 1, 0, 1, 64 | -1, -1, 1, 1, 0, 0, 1, 1, 0, 0, 65 | -1, -1, 1, 1, 0, 0, 1, 1, 0, 0, 66 | 1, -1, 1, 1, 1, 0, 1, 1, 1, 0, 67 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 68 | 69 | 1, -1, -1, 1, 1, 0, 0, 1, 1, 1, 70 | -1, -1, -1, 1, 0, 0, 0, 1, 0, 1, 71 | -1, 1, -1, 1, 0, 1, 0, 1, 0, 0, 72 | 1, 1, -1, 1, 1, 1, 0, 1, 1, 0, 73 | 1, -1, -1, 1, 1, 0, 0, 1, 1, 1, 74 | -1, 1, -1, 1, 0, 1, 0, 1, 0, 0, 75 | ]); 76 | 77 | // Create a vertex buffer from the cube data. 78 | var verticesBuffer = device.createBuffer(GPUBufferDescriptor( 79 | size: cubeVertexArray.lengthInBytes, 80 | usage: GPUBufferUsage.Vertex, 81 | mappedAtCreation: true, 82 | )); 83 | 84 | var pointer = 85 | verticesBuffer.getMappedRange(size: cubeVertexArray.lengthInBytes); 86 | Float32List _list = 87 | (pointer.cast()).asTypedList(cubeVertexArray.length); 88 | _list.setAll(0, cubeVertexArray); 89 | verticesBuffer.unmap(); 90 | 91 | String vert0 = """ 92 | struct Uniforms { 93 | modelViewProjectionMatrix : mat4x4, 94 | } 95 | @binding(0) @group(0) var uniforms : Uniforms; 96 | 97 | struct VertexOutput { 98 | @builtin(position) Position : vec4, 99 | @location(0) fragUV : vec2, 100 | @location(1) fragPosition: vec4, 101 | } 102 | 103 | @vertex 104 | fn vs_main( 105 | @location(0) position : vec4, 106 | @location(1) uv : vec2 107 | ) -> VertexOutput { 108 | var output : VertexOutput; 109 | output.Position = uniforms.modelViewProjectionMatrix * position; 110 | output.fragUV = uv; 111 | output.fragPosition = 0.5 * (position + vec4(1.0, 1.0, 1.0, 1.0)); 112 | return output; 113 | } 114 | 115 | 116 | 117 | @fragment 118 | fn fs_main( 119 | @location(0) fragUV: vec2, 120 | @location(1) fragPosition: vec4 121 | ) -> @location(0) vec4 { 122 | return fragPosition; 123 | } 124 | """; 125 | 126 | 127 | 128 | int cubeVertexSize = 4 * 10; // Byte size of one cube vertex. 129 | int cubePositionOffset = 0; 130 | int cubeColorOffset = 4 * 4; // Byte offset of cube vertex color attribute. 131 | int cubeUVOffset = 4 * 8; 132 | int cubeVertexCount = 36; 133 | 134 | var uniformGroupLayout = 135 | device.createBindGroupLayout(GPUBindGroupLayoutDescriptor(entries: [ 136 | GPUBindGroupLayoutEntry( 137 | binding: 0, 138 | visibility: GPUShaderStage.Vertex, 139 | buffer: GPUBufferBindingLayout(type: GPUBufferBindingType.Uniform)) 140 | ])); 141 | 142 | 143 | var vertModule = device.createShaderModule(GPUShaderModuleDescriptor( 144 | code: vert0, 145 | )); 146 | 147 | var vertex3 = 148 | GPUVertexState(module: vertModule, entryPoint: 'vs_main', buffers: [ 149 | GPUVertexBufferLayout(arrayStride: cubeVertexSize, attributes: [ 150 | GPUVertexAttribute( 151 | format: GPUVertexFormat.Float32x4, 152 | offset: cubePositionOffset, 153 | shaderLocation: 0), 154 | GPUVertexAttribute( 155 | format: GPUVertexFormat.Float32x2, 156 | offset: cubeUVOffset, 157 | shaderLocation: 1) 158 | ]) 159 | ]); 160 | 161 | var layout = device.createPipelineLayout(GPUPipelineLayoutDescriptor( 162 | bindGroupLayouts: uniformGroupLayout, bindGroupLayoutCount: 1)); 163 | 164 | var desc = GPURenderPipelineDescriptor( 165 | // layout: layout, 166 | vertex: vertex3, 167 | fragment: GPUFragmentState( 168 | module: vertModule, 169 | entryPoint: 'fs_main', 170 | targets: GPUColorTargetState( 171 | format: GPUTextureFormat.RGBA8Unorm, 172 | ), 173 | ), 174 | primitive: GPUPrimitiveState(), 175 | multisample: GPUMultisampleState(count: 1), 176 | // depthStencil: GPUDepthStencilState( 177 | // depthWriteEnabled: true, 178 | // depthCompare: GPUCompareFunction.Greater, 179 | // format: GPUTextureFormat.Depth32Float, 180 | // stencilFront: GPUStencilFaceState( 181 | // compare: GPUCompareFunction.Always 182 | // ), 183 | // stencilBack: GPUStencilFaceState(compare: GPUCompareFunction.Always), 184 | // // depthBias: 2, 185 | // // depthBiasSlopeScale: 2.0, 186 | // // depthBiasClamp: 0.0 187 | // ) 188 | ); 189 | 190 | 191 | 192 | var pipeline = device.createRenderPipeline(desc); 193 | 194 | var _layout = pipeline.getBindGroupLayout(0); 195 | 196 | var presentationSize = GPUExtent3D(width: width, height: height); 197 | 198 | var depthTexture = device.createTexture(GPUTextureDescriptor( 199 | size: presentationSize, 200 | format: GPUTextureFormat.Depth32Float, 201 | usage: GPUTextureUsage.RenderAttachment)); 202 | 203 | var uniformBufferSize = 4 * 16; // 4x4 matrix 204 | var uniformBuffer = device.createBuffer(GPUBufferDescriptor( 205 | size: uniformBufferSize, 206 | usage: GPUBufferUsage.Uniform | GPUBufferUsage.CopyDst, 207 | )); 208 | 209 | var uniformBindGroup = device.createBindGroup(GPUBindGroupDescriptor( 210 | layout: _layout, 211 | entries: [ 212 | GPUBindGroupEntry( 213 | binding: 0, 214 | buffer: uniformBuffer, 215 | ), 216 | ], 217 | )); 218 | 219 | Float32List transformationMatrix = Float32List.fromList( 220 | [1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0, 0, 1, 0, 0, 0, 0, 1]); 221 | 222 | Matrix4 _mat = Matrix4.rotationX(0.36); 223 | _mat.rotateY(0.2); 224 | _mat.scale(0.5, 0.5, 0.5); 225 | 226 | transformationMatrix = Float32List.fromList(_mat.storage); 227 | 228 | // var mp = calloc(transformationMatrix.length); 229 | // Float32List _list2 = (mp as Pointer).asTypedList(transformationMatrix.length); 230 | // _list2.setAll(0, transformationMatrix); 231 | 232 | device.queue.writeBuffer(uniformBuffer, 0, transformationMatrix, 233 | transformationMatrix.lengthInBytes); 234 | 235 | var commandEncoder = device.createCommandEncoder(); 236 | 237 | var _textureExtent = GPUExtent3D(width: width, height: height); 238 | var textureDesc = GPUTextureDescriptor( 239 | format: GPUTextureFormat.RGBA8Unorm, 240 | size: _textureExtent, 241 | usage: GPUTextureUsage.RenderAttachment | GPUTextureUsage.CopySrc); 242 | 243 | var texture = device.createTexture(textureDesc); 244 | GPUTextureView textureView = texture.createView(GPUTextureViewDescriptor()); 245 | 246 | var texture0 = device.createTexture(GPUTextureDescriptor( 247 | format: GPUTextureFormat.RGBA8Unorm, 248 | size: _textureExtent, 249 | usage: GPUTextureUsage.RenderAttachment | GPUTextureUsage.CopySrc, 250 | sampleCount: 1)); 251 | GPUTextureView textureView0 = 252 | texture0.createView(GPUTextureViewDescriptor()); 253 | 254 | var renderPassDescriptor = GPURenderPassDescriptor( 255 | colorAttachments: GPURenderPassColorAttachment( 256 | // view: textureView0, 257 | // resolveTarget: textureView, 258 | view: textureView, 259 | clearColor: GPUColor(r: 0.5, g: 0.5, b: 0.0, a: 0.5), 260 | storeOp: GPUStoreOp.Store, 261 | loadOp: GPULoadOp.Clear), 262 | // depthStencilAttachment: GPURenderPassDepthStencilAttachment( 263 | // view: depthTexture.createView(GPUTextureViewDescriptor()), 264 | // depthLoadOp: GPULoadOp.Clear, 265 | // depthStoreOp: GPUStoreOp.Store, 266 | // depthClearValue: 0.0, 267 | // stencilLoadOp: GPULoadOp.Clear, 268 | // stencilStoreOp: GPUStoreOp.Store, 269 | // stencilClearValue: 0, 270 | // ), 271 | ); 272 | 273 | 274 | 275 | var passEncoder = commandEncoder.beginRenderPass(renderPassDescriptor); 276 | passEncoder.setPipeline(pipeline); 277 | passEncoder.setBindGroup(0, uniformBindGroup); 278 | passEncoder.setVertexBuffer(0, verticesBuffer); 279 | passEncoder.draw(cubeVertexCount, 1, 0, 0); 280 | 281 | 282 | passEncoder.end(); 283 | 284 | var copyTexture = 285 | GPUImageCopyTexture(texture: texture, origin: GPUOrigin3D()); 286 | 287 | var bufferDesc = GPUBufferDescriptor( 288 | size: bufferSize, 289 | usage: GPUBufferUsage.MapRead | GPUBufferUsage.CopyDst); 290 | var outputBuffer = device.createBuffer(bufferDesc); 291 | var copyBuffer = GPUImageCopyBuffer( 292 | buffer: outputBuffer, bytesPerRow: padded_bytes_per_row); 293 | 294 | commandEncoder.copyTextureToBuffer(copyTexture, copyBuffer, _textureExtent); 295 | 296 | var commandBuffer = commandEncoder.finish(GPUCommandBufferDescriptor()); 297 | device.queue.submit(commandBuffer); 298 | 299 | outputBuffer.mapAsync(mode: WGPUMapMode_Read, size: bufferSize); 300 | 301 | device.poll(true); 302 | 303 | var data = outputBuffer.getMappedRange(offset: 0); 304 | Pointer pixles = data.cast(); 305 | outputBuffer.unmap(); 306 | 307 | return pixles.asTypedList(bufferSize); 308 | } 309 | } 310 | -------------------------------------------------------------------------------- /example/macos/.gitignore: -------------------------------------------------------------------------------- 1 | # Flutter-related 2 | **/Flutter/ephemeral/ 3 | **/Pods/ 4 | 5 | # Xcode-related 6 | **/dgph 7 | **/xcuserdata/ 8 | -------------------------------------------------------------------------------- /example/macos/Flutter/Flutter-Debug.xcconfig: -------------------------------------------------------------------------------- 1 | #include? "Pods/Target Support Files/Pods-Runner/Pods-Runner.debug.xcconfig" 2 | #include "ephemeral/Flutter-Generated.xcconfig" 3 | -------------------------------------------------------------------------------- /example/macos/Flutter/Flutter-Release.xcconfig: -------------------------------------------------------------------------------- 1 | #include? "Pods/Target Support Files/Pods-Runner/Pods-Runner.release.xcconfig" 2 | #include "ephemeral/Flutter-Generated.xcconfig" 3 | -------------------------------------------------------------------------------- /example/macos/Flutter/GeneratedPluginRegistrant.swift: -------------------------------------------------------------------------------- 1 | // 2 | // Generated file. Do not edit. 3 | // 4 | 5 | import FlutterMacOS 6 | import Foundation 7 | 8 | import flutter_webgpu 9 | 10 | func RegisterGeneratedPlugins(registry: FlutterPluginRegistry) { 11 | FlutterWebgpuPlugin.register(with: registry.registrar(forPlugin: "FlutterWebgpuPlugin")) 12 | } 13 | -------------------------------------------------------------------------------- /example/macos/Podfile: -------------------------------------------------------------------------------- 1 | platform :osx, '10.11' 2 | 3 | # CocoaPods analytics sends network stats synchronously affecting flutter build latency. 4 | ENV['COCOAPODS_DISABLE_STATS'] = 'true' 5 | 6 | project 'Runner', { 7 | 'Debug' => :debug, 8 | 'Profile' => :release, 9 | 'Release' => :release, 10 | } 11 | 12 | def flutter_root 13 | generated_xcode_build_settings_path = File.expand_path(File.join('..', 'Flutter', 'ephemeral', 'Flutter-Generated.xcconfig'), __FILE__) 14 | unless File.exist?(generated_xcode_build_settings_path) 15 | raise "#{generated_xcode_build_settings_path} must exist. If you're running pod install manually, make sure \"flutter pub get\" is executed first" 16 | end 17 | 18 | File.foreach(generated_xcode_build_settings_path) do |line| 19 | matches = line.match(/FLUTTER_ROOT\=(.*)/) 20 | return matches[1].strip if matches 21 | end 22 | raise "FLUTTER_ROOT not found in #{generated_xcode_build_settings_path}. Try deleting Flutter-Generated.xcconfig, then run \"flutter pub get\"" 23 | end 24 | 25 | require File.expand_path(File.join('packages', 'flutter_tools', 'bin', 'podhelper'), flutter_root) 26 | 27 | flutter_macos_podfile_setup 28 | 29 | target 'Runner' do 30 | use_frameworks! 31 | use_modular_headers! 32 | 33 | flutter_install_all_macos_pods File.dirname(File.realpath(__FILE__)) 34 | end 35 | 36 | post_install do |installer| 37 | installer.pods_project.targets.each do |target| 38 | flutter_additional_macos_build_settings(target) 39 | end 40 | end 41 | -------------------------------------------------------------------------------- /example/macos/Podfile.lock: -------------------------------------------------------------------------------- 1 | PODS: 2 | - flutter_webgpu (0.0.1): 3 | - FlutterMacOS 4 | - FlutterMacOS (1.0.0) 5 | 6 | DEPENDENCIES: 7 | - flutter_webgpu (from `Flutter/ephemeral/.symlinks/plugins/flutter_webgpu/macos`) 8 | - FlutterMacOS (from `Flutter/ephemeral`) 9 | 10 | EXTERNAL SOURCES: 11 | flutter_webgpu: 12 | :path: Flutter/ephemeral/.symlinks/plugins/flutter_webgpu/macos 13 | FlutterMacOS: 14 | :path: Flutter/ephemeral 15 | 16 | SPEC CHECKSUMS: 17 | flutter_webgpu: aad4ca5e7dded292a49e75f2755c5b72a2922214 18 | FlutterMacOS: ae6af50a8ea7d6103d888583d46bd8328a7e9811 19 | 20 | PODFILE CHECKSUM: 6eac6b3292e5142cfc23bdeb71848a40ec51c14c 21 | 22 | COCOAPODS: 1.11.3 23 | -------------------------------------------------------------------------------- /example/macos/Runner.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | IDEDidComputeMac32BitWarning 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /example/macos/Runner.xcodeproj/xcshareddata/xcschemes/Runner.xcscheme: -------------------------------------------------------------------------------- 1 | 2 | 5 | 8 | 9 | 15 | 21 | 22 | 23 | 24 | 25 | 30 | 31 | 37 | 38 | 39 | 40 | 41 | 42 | 52 | 54 | 60 | 61 | 62 | 63 | 69 | 71 | 77 | 78 | 79 | 80 | 82 | 83 | 86 | 87 | 88 | -------------------------------------------------------------------------------- /example/macos/Runner.xcworkspace/contents.xcworkspacedata: -------------------------------------------------------------------------------- 1 | 2 | 4 | 6 | 7 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /example/macos/Runner.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | IDEDidComputeMac32BitWarning 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /example/macos/Runner/AppDelegate.swift: -------------------------------------------------------------------------------- 1 | import Cocoa 2 | import FlutterMacOS 3 | 4 | @NSApplicationMain 5 | class AppDelegate: FlutterAppDelegate { 6 | override func applicationShouldTerminateAfterLastWindowClosed(_ sender: NSApplication) -> Bool { 7 | return true 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /example/macos/Runner/Assets.xcassets/AppIcon.appiconset/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "images" : [ 3 | { 4 | "size" : "16x16", 5 | "idiom" : "mac", 6 | "filename" : "app_icon_16.png", 7 | "scale" : "1x" 8 | }, 9 | { 10 | "size" : "16x16", 11 | "idiom" : "mac", 12 | "filename" : "app_icon_32.png", 13 | "scale" : "2x" 14 | }, 15 | { 16 | "size" : "32x32", 17 | "idiom" : "mac", 18 | "filename" : "app_icon_32.png", 19 | "scale" : "1x" 20 | }, 21 | { 22 | "size" : "32x32", 23 | "idiom" : "mac", 24 | "filename" : "app_icon_64.png", 25 | "scale" : "2x" 26 | }, 27 | { 28 | "size" : "128x128", 29 | "idiom" : "mac", 30 | "filename" : "app_icon_128.png", 31 | "scale" : "1x" 32 | }, 33 | { 34 | "size" : "128x128", 35 | "idiom" : "mac", 36 | "filename" : "app_icon_256.png", 37 | "scale" : "2x" 38 | }, 39 | { 40 | "size" : "256x256", 41 | "idiom" : "mac", 42 | "filename" : "app_icon_256.png", 43 | "scale" : "1x" 44 | }, 45 | { 46 | "size" : "256x256", 47 | "idiom" : "mac", 48 | "filename" : "app_icon_512.png", 49 | "scale" : "2x" 50 | }, 51 | { 52 | "size" : "512x512", 53 | "idiom" : "mac", 54 | "filename" : "app_icon_512.png", 55 | "scale" : "1x" 56 | }, 57 | { 58 | "size" : "512x512", 59 | "idiom" : "mac", 60 | "filename" : "app_icon_1024.png", 61 | "scale" : "2x" 62 | } 63 | ], 64 | "info" : { 65 | "version" : 1, 66 | "author" : "xcode" 67 | } 68 | } 69 | -------------------------------------------------------------------------------- /example/macos/Runner/Assets.xcassets/AppIcon.appiconset/app_icon_1024.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wasabia/flutter_webgpu/687c08960e8c1ddc7a32d7886682bd4657015e2b/example/macos/Runner/Assets.xcassets/AppIcon.appiconset/app_icon_1024.png -------------------------------------------------------------------------------- /example/macos/Runner/Assets.xcassets/AppIcon.appiconset/app_icon_128.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wasabia/flutter_webgpu/687c08960e8c1ddc7a32d7886682bd4657015e2b/example/macos/Runner/Assets.xcassets/AppIcon.appiconset/app_icon_128.png -------------------------------------------------------------------------------- /example/macos/Runner/Assets.xcassets/AppIcon.appiconset/app_icon_16.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wasabia/flutter_webgpu/687c08960e8c1ddc7a32d7886682bd4657015e2b/example/macos/Runner/Assets.xcassets/AppIcon.appiconset/app_icon_16.png -------------------------------------------------------------------------------- /example/macos/Runner/Assets.xcassets/AppIcon.appiconset/app_icon_256.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wasabia/flutter_webgpu/687c08960e8c1ddc7a32d7886682bd4657015e2b/example/macos/Runner/Assets.xcassets/AppIcon.appiconset/app_icon_256.png -------------------------------------------------------------------------------- /example/macos/Runner/Assets.xcassets/AppIcon.appiconset/app_icon_32.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wasabia/flutter_webgpu/687c08960e8c1ddc7a32d7886682bd4657015e2b/example/macos/Runner/Assets.xcassets/AppIcon.appiconset/app_icon_32.png -------------------------------------------------------------------------------- /example/macos/Runner/Assets.xcassets/AppIcon.appiconset/app_icon_512.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wasabia/flutter_webgpu/687c08960e8c1ddc7a32d7886682bd4657015e2b/example/macos/Runner/Assets.xcassets/AppIcon.appiconset/app_icon_512.png -------------------------------------------------------------------------------- /example/macos/Runner/Assets.xcassets/AppIcon.appiconset/app_icon_64.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wasabia/flutter_webgpu/687c08960e8c1ddc7a32d7886682bd4657015e2b/example/macos/Runner/Assets.xcassets/AppIcon.appiconset/app_icon_64.png -------------------------------------------------------------------------------- /example/macos/Runner/Configs/AppInfo.xcconfig: -------------------------------------------------------------------------------- 1 | // Application-level settings for the Runner target. 2 | // 3 | // This may be replaced with something auto-generated from metadata (e.g., pubspec.yaml) in the 4 | // future. If not, the values below would default to using the project name when this becomes a 5 | // 'flutter create' template. 6 | 7 | // The application's name. By default this is also the title of the Flutter window. 8 | PRODUCT_NAME = example 9 | 10 | // The application's bundle identifier 11 | PRODUCT_BUNDLE_IDENTIFIER = com.futouapp.flutterwebgpu.macos.example 12 | 13 | // The copyright displayed in application information 14 | PRODUCT_COPYRIGHT = Copyright © 2022 com.futouapp.flutterwebgpu.macos. All rights reserved. 15 | -------------------------------------------------------------------------------- /example/macos/Runner/Configs/Debug.xcconfig: -------------------------------------------------------------------------------- 1 | #include "../../Flutter/Flutter-Debug.xcconfig" 2 | #include "Warnings.xcconfig" 3 | -------------------------------------------------------------------------------- /example/macos/Runner/Configs/Release.xcconfig: -------------------------------------------------------------------------------- 1 | #include "../../Flutter/Flutter-Release.xcconfig" 2 | #include "Warnings.xcconfig" 3 | -------------------------------------------------------------------------------- /example/macos/Runner/Configs/Warnings.xcconfig: -------------------------------------------------------------------------------- 1 | WARNING_CFLAGS = -Wall -Wconditional-uninitialized -Wnullable-to-nonnull-conversion -Wmissing-method-return-type -Woverlength-strings 2 | GCC_WARN_UNDECLARED_SELECTOR = YES 3 | CLANG_UNDEFINED_BEHAVIOR_SANITIZER_NULLABILITY = YES 4 | CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE 5 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES 6 | CLANG_WARN_PRAGMA_PACK = YES 7 | CLANG_WARN_STRICT_PROTOTYPES = YES 8 | CLANG_WARN_COMMA = YES 9 | GCC_WARN_STRICT_SELECTOR_MATCH = YES 10 | CLANG_WARN_OBJC_REPEATED_USE_OF_WEAK = YES 11 | CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES 12 | GCC_WARN_SHADOW = YES 13 | CLANG_WARN_UNREACHABLE_CODE = YES 14 | -------------------------------------------------------------------------------- /example/macos/Runner/DebugProfile.entitlements: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | com.apple.security.app-sandbox 6 | 7 | com.apple.security.cs.allow-jit 8 | 9 | com.apple.security.network.server 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /example/macos/Runner/Info.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | CFBundleDevelopmentRegion 6 | $(DEVELOPMENT_LANGUAGE) 7 | CFBundleExecutable 8 | $(EXECUTABLE_NAME) 9 | CFBundleIconFile 10 | 11 | CFBundleIdentifier 12 | $(PRODUCT_BUNDLE_IDENTIFIER) 13 | CFBundleInfoDictionaryVersion 14 | 6.0 15 | CFBundleName 16 | $(PRODUCT_NAME) 17 | CFBundlePackageType 18 | APPL 19 | CFBundleShortVersionString 20 | $(FLUTTER_BUILD_NAME) 21 | CFBundleVersion 22 | $(FLUTTER_BUILD_NUMBER) 23 | LSMinimumSystemVersion 24 | $(MACOSX_DEPLOYMENT_TARGET) 25 | NSHumanReadableCopyright 26 | $(PRODUCT_COPYRIGHT) 27 | NSMainNibFile 28 | MainMenu 29 | NSPrincipalClass 30 | NSApplication 31 | 32 | 33 | -------------------------------------------------------------------------------- /example/macos/Runner/MainFlutterWindow.swift: -------------------------------------------------------------------------------- 1 | import Cocoa 2 | import FlutterMacOS 3 | 4 | class MainFlutterWindow: NSWindow { 5 | override func awakeFromNib() { 6 | let flutterViewController = FlutterViewController.init() 7 | let windowFrame = self.frame 8 | self.contentViewController = flutterViewController 9 | self.setFrame(windowFrame, display: true) 10 | 11 | RegisterGeneratedPlugins(registry: flutterViewController) 12 | 13 | super.awakeFromNib() 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /example/macos/Runner/Release.entitlements: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | com.apple.security.app-sandbox 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /example/pubspec.lock: -------------------------------------------------------------------------------- 1 | # Generated by pub 2 | # See https://dart.dev/tools/pub/glossary#lockfile 3 | packages: 4 | args: 5 | dependency: transitive 6 | description: 7 | name: args 8 | url: "https://pub.dartlang.org" 9 | source: hosted 10 | version: "2.3.1" 11 | async: 12 | dependency: transitive 13 | description: 14 | name: async 15 | url: "https://pub.dartlang.org" 16 | source: hosted 17 | version: "2.9.0" 18 | boolean_selector: 19 | dependency: transitive 20 | description: 21 | name: boolean_selector 22 | url: "https://pub.dartlang.org" 23 | source: hosted 24 | version: "2.1.0" 25 | characters: 26 | dependency: transitive 27 | description: 28 | name: characters 29 | url: "https://pub.dartlang.org" 30 | source: hosted 31 | version: "1.2.1" 32 | cli_util: 33 | dependency: transitive 34 | description: 35 | name: cli_util 36 | url: "https://pub.dartlang.org" 37 | source: hosted 38 | version: "0.3.5" 39 | clock: 40 | dependency: transitive 41 | description: 42 | name: clock 43 | url: "https://pub.dartlang.org" 44 | source: hosted 45 | version: "1.1.1" 46 | collection: 47 | dependency: transitive 48 | description: 49 | name: collection 50 | url: "https://pub.dartlang.org" 51 | source: hosted 52 | version: "1.16.0" 53 | cupertino_icons: 54 | dependency: "direct main" 55 | description: 56 | name: cupertino_icons 57 | url: "https://pub.dartlang.org" 58 | source: hosted 59 | version: "1.0.5" 60 | fake_async: 61 | dependency: transitive 62 | description: 63 | name: fake_async 64 | url: "https://pub.dartlang.org" 65 | source: hosted 66 | version: "1.3.1" 67 | ffi: 68 | dependency: "direct main" 69 | description: 70 | name: ffi 71 | url: "https://pub.dartlang.org" 72 | source: hosted 73 | version: "2.0.1" 74 | ffigen: 75 | dependency: transitive 76 | description: 77 | name: ffigen 78 | url: "https://pub.dartlang.org" 79 | source: hosted 80 | version: "6.1.2" 81 | file: 82 | dependency: transitive 83 | description: 84 | name: file 85 | url: "https://pub.dartlang.org" 86 | source: hosted 87 | version: "6.1.4" 88 | flutter: 89 | dependency: "direct main" 90 | description: flutter 91 | source: sdk 92 | version: "0.0.0" 93 | flutter_lints: 94 | dependency: "direct dev" 95 | description: 96 | name: flutter_lints 97 | url: "https://pub.dartlang.org" 98 | source: hosted 99 | version: "1.0.4" 100 | flutter_test: 101 | dependency: "direct dev" 102 | description: flutter 103 | source: sdk 104 | version: "0.0.0" 105 | flutter_webgpu: 106 | dependency: "direct main" 107 | description: 108 | path: ".." 109 | relative: true 110 | source: path 111 | version: "0.0.1" 112 | glob: 113 | dependency: transitive 114 | description: 115 | name: glob 116 | url: "https://pub.dartlang.org" 117 | source: hosted 118 | version: "2.1.0" 119 | lints: 120 | dependency: transitive 121 | description: 122 | name: lints 123 | url: "https://pub.dartlang.org" 124 | source: hosted 125 | version: "1.0.1" 126 | logging: 127 | dependency: transitive 128 | description: 129 | name: logging 130 | url: "https://pub.dartlang.org" 131 | source: hosted 132 | version: "1.1.0" 133 | matcher: 134 | dependency: transitive 135 | description: 136 | name: matcher 137 | url: "https://pub.dartlang.org" 138 | source: hosted 139 | version: "0.12.12" 140 | material_color_utilities: 141 | dependency: transitive 142 | description: 143 | name: material_color_utilities 144 | url: "https://pub.dartlang.org" 145 | source: hosted 146 | version: "0.1.5" 147 | meta: 148 | dependency: transitive 149 | description: 150 | name: meta 151 | url: "https://pub.dartlang.org" 152 | source: hosted 153 | version: "1.8.0" 154 | path: 155 | dependency: transitive 156 | description: 157 | name: path 158 | url: "https://pub.dartlang.org" 159 | source: hosted 160 | version: "1.8.2" 161 | quiver: 162 | dependency: transitive 163 | description: 164 | name: quiver 165 | url: "https://pub.dartlang.org" 166 | source: hosted 167 | version: "3.1.0" 168 | sky_engine: 169 | dependency: transitive 170 | description: flutter 171 | source: sdk 172 | version: "0.0.99" 173 | source_span: 174 | dependency: transitive 175 | description: 176 | name: source_span 177 | url: "https://pub.dartlang.org" 178 | source: hosted 179 | version: "1.9.0" 180 | stack_trace: 181 | dependency: transitive 182 | description: 183 | name: stack_trace 184 | url: "https://pub.dartlang.org" 185 | source: hosted 186 | version: "1.10.0" 187 | stream_channel: 188 | dependency: transitive 189 | description: 190 | name: stream_channel 191 | url: "https://pub.dartlang.org" 192 | source: hosted 193 | version: "2.1.0" 194 | string_scanner: 195 | dependency: transitive 196 | description: 197 | name: string_scanner 198 | url: "https://pub.dartlang.org" 199 | source: hosted 200 | version: "1.1.1" 201 | term_glyph: 202 | dependency: transitive 203 | description: 204 | name: term_glyph 205 | url: "https://pub.dartlang.org" 206 | source: hosted 207 | version: "1.2.1" 208 | test_api: 209 | dependency: transitive 210 | description: 211 | name: test_api 212 | url: "https://pub.dartlang.org" 213 | source: hosted 214 | version: "0.4.12" 215 | vector_math: 216 | dependency: transitive 217 | description: 218 | name: vector_math 219 | url: "https://pub.dartlang.org" 220 | source: hosted 221 | version: "2.1.2" 222 | yaml: 223 | dependency: transitive 224 | description: 225 | name: yaml 226 | url: "https://pub.dartlang.org" 227 | source: hosted 228 | version: "3.1.1" 229 | sdks: 230 | dart: ">=2.17.0 <3.0.0" 231 | flutter: ">=2.5.0" 232 | -------------------------------------------------------------------------------- /example/pubspec.yaml: -------------------------------------------------------------------------------- 1 | name: flutter_webgpu_example 2 | description: Demonstrates how to use the flutter_webgpu plugin. 3 | 4 | # The following line prevents the package from being accidentally published to 5 | # pub.dev using `flutter pub publish`. This is preferred for private packages. 6 | publish_to: 'none' # Remove this line if you wish to publish to pub.dev 7 | 8 | environment: 9 | sdk: ">=2.15.1 <3.0.0" 10 | 11 | # Dependencies specify other packages that your package needs in order to work. 12 | # To automatically upgrade your package dependencies to the latest versions 13 | # consider running `flutter pub upgrade --major-versions`. Alternatively, 14 | # dependencies can be manually updated by changing the version numbers below to 15 | # the latest version available on pub.dev. To see which dependencies have newer 16 | # versions available, run `flutter pub outdated`. 17 | dependencies: 18 | flutter: 19 | sdk: flutter 20 | 21 | flutter_webgpu: 22 | # When depending on this package from a real application you should use: 23 | # flutter_webgpu: ^x.y.z 24 | # See https://dart.dev/tools/pub/dependencies#version-constraints 25 | # The example app is bundled with the plugin so we use a path dependency on 26 | # the parent directory to use the current plugin's version. 27 | path: ../ 28 | 29 | # The following adds the Cupertino Icons font to your application. 30 | # Use with the CupertinoIcons class for iOS style icons. 31 | cupertino_icons: ^1.0.2 32 | ffi: 33 | 34 | dev_dependencies: 35 | flutter_test: 36 | sdk: flutter 37 | 38 | # The "flutter_lints" package below contains a set of recommended lints to 39 | # encourage good coding practices. The lint set provided by the package is 40 | # activated in the `analysis_options.yaml` file located at the root of your 41 | # package. See that file for information about deactivating specific lint 42 | # rules and activating additional ones. 43 | flutter_lints: ^1.0.0 44 | 45 | # For information on the generic Dart part of this file, see the 46 | # following page: https://dart.dev/tools/pub/pubspec 47 | 48 | # The following section is specific to Flutter. 49 | flutter: 50 | 51 | # The following line ensures that the Material Icons font is 52 | # included with your application, so that you can use the icons in 53 | # the material Icons class. 54 | uses-material-design: true 55 | 56 | # To add assets to your application, add an assets section, like this: 57 | # assets: 58 | # - images/a_dot_burr.jpeg 59 | # - images/a_dot_ham.jpeg 60 | 61 | # An image asset can refer to one or more resolution-specific "variants", see 62 | # https://flutter.dev/assets-and-images/#resolution-aware. 63 | 64 | # For details regarding adding assets from package dependencies, see 65 | # https://flutter.dev/assets-and-images/#from-packages 66 | 67 | # To add custom fonts to your application, add a fonts section here, 68 | # in this "flutter" section. Each entry in this list should have a 69 | # "family" key with the font family name, and a "fonts" key with a 70 | # list giving the asset and other descriptors for the font. For 71 | # example: 72 | # fonts: 73 | # - family: Schyler 74 | # fonts: 75 | # - asset: fonts/Schyler-Regular.ttf 76 | # - asset: fonts/Schyler-Italic.ttf 77 | # style: italic 78 | # - family: Trajan Pro 79 | # fonts: 80 | # - asset: fonts/TrajanPro.ttf 81 | # - asset: fonts/TrajanPro_Bold.ttf 82 | # weight: 700 83 | # 84 | # For details regarding fonts from package dependencies, 85 | # see https://flutter.dev/custom-fonts/#from-packages 86 | -------------------------------------------------------------------------------- /example/test/widget_test.dart: -------------------------------------------------------------------------------- 1 | // This is a basic Flutter widget test. 2 | // 3 | // To perform an interaction with a widget in your test, use the WidgetTester 4 | // utility that Flutter provides. For example, you can send tap and scroll 5 | // gestures. You can also use WidgetTester to find child widgets in the widget 6 | // tree, read text, and verify that the values of widget properties are correct. 7 | 8 | import 'package:flutter/material.dart'; 9 | import 'package:flutter_test/flutter_test.dart'; 10 | 11 | import 'package:flutter_webgpu_example/main.dart'; 12 | 13 | void main() { 14 | testWidgets('Verify Platform version', (WidgetTester tester) async { 15 | // Build our app and trigger a frame. 16 | await tester.pumpWidget(const MyApp()); 17 | 18 | // Verify that platform version is retrieved. 19 | expect( 20 | find.byWidgetPredicate( 21 | (Widget widget) => 22 | widget is Text && widget.data!.startsWith('Running on:'), 23 | ), 24 | findsOneWidget, 25 | ); 26 | }); 27 | } 28 | -------------------------------------------------------------------------------- /ios/.gitignore: -------------------------------------------------------------------------------- 1 | .idea/ 2 | .vagrant/ 3 | .sconsign.dblite 4 | .svn/ 5 | 6 | .DS_Store 7 | *.swp 8 | profile 9 | 10 | DerivedData/ 11 | build/ 12 | GeneratedPluginRegistrant.h 13 | GeneratedPluginRegistrant.m 14 | 15 | .generated/ 16 | 17 | *.pbxuser 18 | *.mode1v3 19 | *.mode2v3 20 | *.perspectivev3 21 | 22 | !default.pbxuser 23 | !default.mode1v3 24 | !default.mode2v3 25 | !default.perspectivev3 26 | 27 | xcuserdata 28 | 29 | *.moved-aside 30 | 31 | *.pyc 32 | *sync/ 33 | Icon? 34 | .tags* 35 | 36 | /Flutter/Generated.xcconfig 37 | /Flutter/ephemeral/ 38 | /Flutter/flutter_export_environment.sh -------------------------------------------------------------------------------- /ios/Assets/.gitkeep: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wasabia/flutter_webgpu/687c08960e8c1ddc7a32d7886682bd4657015e2b/ios/Assets/.gitkeep -------------------------------------------------------------------------------- /ios/Classes/FlutterWebgpuPlugin.h: -------------------------------------------------------------------------------- 1 | #import 2 | 3 | //#import "ffi/wgpu.h" 4 | 5 | @interface FlutterWebgpuPlugin : NSObject 6 | @end 7 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /ios/Classes/FlutterWebgpuPlugin.m: -------------------------------------------------------------------------------- 1 | #import "FlutterWebgpuPlugin.h" 2 | #if __has_include() 3 | #import 4 | #else 5 | // Support project import fallback if the generated compatibility header 6 | // is not copied when this plugin is created as a library. 7 | // https://forums.swift.org/t/swift-static-libraries-dont-copy-generated-objective-c-header/19816 8 | #import "flutter_webgpu-Swift.h" 9 | #endif 10 | 11 | @implementation FlutterWebgpuPlugin 12 | + (void)registerWithRegistrar:(NSObject*)registrar { 13 | [SwiftFlutterWebgpuPlugin registerWithRegistrar:registrar]; 14 | } 15 | @end 16 | -------------------------------------------------------------------------------- /ios/Classes/SwiftFlutterWebgpuPlugin.swift: -------------------------------------------------------------------------------- 1 | import Flutter 2 | import UIKit 3 | 4 | public class SwiftFlutterWebgpuPlugin: NSObject, FlutterPlugin { 5 | public static func register(with registrar: FlutterPluginRegistrar) { 6 | let channel = FlutterMethodChannel(name: "flutter_webgpu", binaryMessenger: registrar.messenger()) 7 | let instance = SwiftFlutterWebgpuPlugin() 8 | registrar.addMethodCallDelegate(instance, channel: channel) 9 | } 10 | 11 | public func handle(_ call: FlutterMethodCall, result: @escaping FlutterResult) { 12 | result("iOS " + UIDevice.current.systemVersion) 13 | } 14 | 15 | // https://medium.com/flutter-community/using-ffi-on-flutter-plugins-to-run-native-rust-code-d64c0f14f9c2 16 | // Note: XCode will not bundle the library unless it detects explicit usage within the workspace. Since our Dart code calling it is out of the scope of XCode, we need to write a dummy Swift function that makes some fake usage. 17 | public func dummyMethodToEnforceBundling() { 18 | // This will never be executed 19 | wgpuGetVersion(); 20 | } 21 | 22 | } 23 | -------------------------------------------------------------------------------- /ios/Classes/ffi/webgpu-headers/LICENSE: -------------------------------------------------------------------------------- 1 | BSD 3-Clause License 2 | 3 | Copyright (c) 2019, "WebGPU native" developers 4 | All rights reserved. 5 | 6 | Redistribution and use in source and binary forms, with or without 7 | modification, are permitted provided that the following conditions are met: 8 | 9 | 1. Redistributions of source code must retain the above copyright notice, this 10 | list of conditions and the following disclaimer. 11 | 12 | 2. Redistributions in binary form must reproduce the above copyright notice, 13 | this list of conditions and the following disclaimer in the documentation 14 | and/or other materials provided with the distribution. 15 | 16 | 3. Neither the name of the copyright holder nor the names of its 17 | contributors may be used to endorse or promote products derived from 18 | this software without specific prior written permission. 19 | 20 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 21 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 23 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 24 | FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 | DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 26 | SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 27 | CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 28 | OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 29 | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 | -------------------------------------------------------------------------------- /ios/Classes/ffi/webgpu-headers/README.md: -------------------------------------------------------------------------------- 1 | # webgpu-headers -------------------------------------------------------------------------------- /ios/Classes/ffi/wgpu.h: -------------------------------------------------------------------------------- 1 | #ifndef WGPU_H_ 2 | #define WGPU_H_ 3 | 4 | #include "webgpu.h" 5 | 6 | typedef enum WGPUNativeSType { 7 | // Start at 6 to prevent collisions with webgpu STypes 8 | WGPUSType_DeviceExtras = 0x60000001, 9 | WGPUSType_AdapterExtras = 0x60000002, 10 | WGPUNativeSType_Force32 = 0x7FFFFFFF 11 | } WGPUNativeSType; 12 | 13 | typedef enum WGPUNativeFeature { 14 | WGPUNativeFeature_TEXTURE_ADAPTER_SPECIFIC_FORMAT_FEATURES = 0x10000000 15 | } WGPUNativeFeature; 16 | 17 | typedef enum WGPULogLevel { 18 | WGPULogLevel_Off = 0x00000000, 19 | WGPULogLevel_Error = 0x00000001, 20 | WGPULogLevel_Warn = 0x00000002, 21 | WGPULogLevel_Info = 0x00000003, 22 | WGPULogLevel_Debug = 0x00000004, 23 | WGPULogLevel_Trace = 0x00000005, 24 | WGPULogLevel_Force32 = 0x7FFFFFFF 25 | } WGPULogLevel; 26 | 27 | typedef struct WGPUAdapterExtras { 28 | WGPUChainedStruct chain; 29 | WGPUBackendType backend; 30 | } WGPUAdapterExtras; 31 | 32 | typedef struct WGPUDeviceExtras { 33 | WGPUChainedStruct chain; 34 | WGPUNativeFeature nativeFeatures; 35 | const char* label; 36 | const char* tracePath; 37 | } WGPUDeviceExtras; 38 | 39 | typedef void (*WGPULogCallback)(WGPULogLevel level, const char *msg); 40 | 41 | #ifdef __cplusplus 42 | extern "C" { 43 | #endif 44 | 45 | void wgpuDevicePoll(WGPUDevice device, bool force_wait); 46 | 47 | void wgpuSetLogCallback(WGPULogCallback callback); 48 | 49 | void wgpuSetLogLevel(WGPULogLevel level); 50 | 51 | uint32_t wgpuGetVersion(void); 52 | 53 | void wgpuRenderPassEncoderSetPushConstants(WGPURenderPassEncoder encoder, WGPUShaderStageFlags stages, uint32_t offset, uint32_t sizeBytes, void* const data); 54 | 55 | void wgpuBufferDrop(WGPUBuffer buffer); 56 | void wgpuCommandEncoderDrop(WGPUCommandEncoder commandEncoder); 57 | void wgpuDeviceDrop(WGPUDevice device); 58 | void wgpuQuerySetDrop(WGPUQuerySet querySet); 59 | void wgpuRenderPipelineDrop(WGPURenderPipeline renderPipeline); 60 | void wgpuTextureDrop(WGPUTexture texture); 61 | void wgpuTextureViewDrop(WGPUTextureView textureView); 62 | void wgpuSamplerDrop(WGPUSampler sampler); 63 | void wgpuBindGroupLayoutDrop(WGPUBindGroupLayout bindGroupLayout); 64 | void wgpuPipelineLayoutDrop(WGPUPipelineLayout pipelineLayout); 65 | void wgpuBindGroupDrop(WGPUBindGroup bindGroup); 66 | void wgpuShaderModuleDrop(WGPUShaderModule shaderModule); 67 | void wgpuCommandBufferDrop(WGPUCommandBuffer commandBuffer); 68 | void wgpuRenderBundleDrop(WGPURenderBundle renderBundle); 69 | void wgpuComputePipelineDrop(WGPUComputePipeline computePipeline); 70 | 71 | #ifdef __cplusplus 72 | } // extern "C" 73 | #endif 74 | 75 | #endif 76 | -------------------------------------------------------------------------------- /ios/Classes/flutter_webgpu-Bridging-Header.h: -------------------------------------------------------------------------------- 1 | // 2 | // flutter_webgpu-Bridging-Header.h 3 | // flutter_webgpu 4 | // 5 | // Created by 赵磊 on 2022/2/3. 6 | // 7 | 8 | #ifndef flutter_webgpu_Bridging_Header_h 9 | #define flutter_webgpu_Bridging_Header_h 10 | 11 | #include "webgpu.h" 12 | 13 | #endif /* flutter_webgpu_Bridging_Header_h */ 14 | -------------------------------------------------------------------------------- /ios/flutter_webgpu.podspec: -------------------------------------------------------------------------------- 1 | # 2 | # To learn more about a Podspec see http://guides.cocoapods.org/syntax/podspec.html. 3 | # Run `pod lib lint flutter_webgpu.podspec` to validate before publishing. 4 | # 5 | Pod::Spec.new do |s| 6 | s.name = 'flutter_webgpu' 7 | s.version = '0.0.1' 8 | s.summary = 'A new flutter plugin project.' 9 | s.description = <<-DESC 10 | A new flutter plugin project. 11 | DESC 12 | s.homepage = 'http://example.com' 13 | s.license = { :file => '../LICENSE' } 14 | s.author = { 'Your Company' => 'email@example.com' } 15 | s.source = { :path => '.' } 16 | s.source_files = 'Classes/**/*' 17 | s.dependency 'Flutter' 18 | s.platform = :ios, '9.0' 19 | 20 | # Flutter.framework does not contain a i386 slice. 21 | s.pod_target_xcconfig = { 'DEFINES_MODULE' => 'YES', 'EXCLUDED_ARCHS[sdk=iphonesimulator*]' => 'i386' } 22 | s.swift_version = '5.0' 23 | 24 | 25 | # s.public_header_files = 'Classes/ffi/**/*.h' 26 | s.static_framework = true 27 | s.vendored_libraries = "**/*.a" 28 | 29 | end 30 | -------------------------------------------------------------------------------- /lib/flutter_webgpu.dart: -------------------------------------------------------------------------------- 1 | import 'dart:async'; 2 | import 'package:flutter/services.dart'; 3 | 4 | export './WebGPU_Binding.dart'; 5 | export 'package:flutter_webgpu/webgpu/index.dart'; 6 | 7 | class FlutterWebgpu { 8 | static const MethodChannel _channel = MethodChannel('flutter_webgpu'); 9 | 10 | static Future get platformVersion async { 11 | final String? version = await _channel.invokeMethod('getPlatformVersion'); 12 | return version; 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /lib/shader_helper.dart: -------------------------------------------------------------------------------- 1 | class ShaderHelper { 2 | static loadWgsl(String shader) { 3 | // WGPUShaderModuleWGSLDescriptor *wgslDescriptor = 4 | // malloc(sizeof(WGPUShaderModuleWGSLDescriptor)); 5 | // wgslDescriptor->chain.next = NULL; 6 | // wgslDescriptor->chain.sType = WGPUSType_ShaderModuleWGSLDescriptor; 7 | // wgslDescriptor->code = (const char *)bytes; 8 | // return (WGPUShaderModuleDescriptor){ 9 | // .nextInChain = (const WGPUChainedStruct *)wgslDescriptor, 10 | // .label = name, 11 | // }; 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /lib/webgpu/GPUAdapter.dart: -------------------------------------------------------------------------------- 1 | part of webgpu; 2 | 3 | void wgpuRequestDeviceCallback(int status, WGPUDevice received, 4 | Pointer message, Pointer userdata) { 5 | print( 6 | "wgpuRequestDeviceCallback status: ${status} received: ${received} message: ${message} userdata: ${userdata} "); 7 | Pointer _device = userdata.cast(); 8 | _device.value = received; 9 | print("wgpuRequestDeviceCallback userdata: ${userdata} "); 10 | } 11 | 12 | typedef WgpuRequestDeviceCallback = Void Function( 13 | Int32, WGPUDevice, Pointer, Pointer); 14 | 15 | class GPUAdapter { 16 | late Pointer adapter; 17 | GPUAdapter(this.adapter) {} 18 | 19 | GPUDevice requestDevice(GPUDeviceDescriptor descriptor) { 20 | var deviceCallback = Pointer.fromFunction( 21 | wgpuRequestDeviceCallback); 22 | 23 | Pointer device = ffi.calloc(); 24 | 25 | Wgpu.binding.wgpuAdapterRequestDevice( 26 | adapter.value, descriptor.pointer, deviceCallback, device.cast()); 27 | 28 | return GPUDevice(device); 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /lib/webgpu/GPUBindGroup.dart: -------------------------------------------------------------------------------- 1 | part of webgpu; 2 | 3 | class GPUBindGroup extends GPUObjectBase { 4 | late WGPUBindGroup bindGroup; 5 | 6 | GPUBindGroup(this.bindGroup) {} 7 | } 8 | 9 | class GPUBindGroupDescriptor { 10 | late Pointer pointer; 11 | 12 | GPUBindGroupDescriptor( 13 | {String? label, 14 | required GPUBindGroupLayout layout, 15 | List? entries, 16 | int entryCount = 0}) { 17 | pointer = ffi.calloc(); 18 | var ref = pointer.ref; 19 | ref.nextInChain = nullptr; 20 | ref.label = (label ?? "GPUBindGroupDescriptor").toNativeUtf8().cast(); 21 | 22 | ref.layout = layout.bindGroupLayout; 23 | 24 | if (entries == null) { 25 | ref.entries = nullptr; 26 | ref.entryCount = 0; 27 | } else { 28 | print(" entries entryCount: ${entries.length} "); 29 | 30 | var entryPointers = ffi.calloc(entries.length); 31 | entries.asMap().forEach((index, entry) { 32 | var pointer = entryPointers[index]; 33 | pointer.binding = entry.pointer.ref.binding; 34 | pointer.offset = entry.pointer.ref.offset; 35 | pointer.buffer = entry.pointer.ref.buffer; 36 | pointer.size = entry.pointer.ref.size; 37 | pointer.sampler = entry.pointer.ref.sampler; 38 | pointer.textureView = entry.pointer.ref.textureView; 39 | }); 40 | 41 | ref.entries = entryPointers; 42 | ref.entryCount = entries.length; 43 | } 44 | } 45 | } 46 | 47 | class GPUBindGroupEntry { 48 | late Pointer pointer; 49 | 50 | GPUBindGroupEntry( 51 | {GPUBuffer? buffer, 52 | int? size, 53 | required int binding, 54 | GPUSampler? sampler, 55 | GPUTextureView? textureView, 56 | int offset = 0}) { 57 | pointer = ffi.calloc(); 58 | var state = pointer.ref; 59 | 60 | state.nextInChain = nullptr; 61 | state.binding = binding; 62 | if (buffer != null) state.buffer = buffer.buffer; 63 | state.offset = offset; 64 | state.size = size ?? 0; 65 | if (sampler != null) state.sampler = sampler.sampler; 66 | if (textureView != null) state.textureView = textureView.textureView; 67 | } 68 | } 69 | -------------------------------------------------------------------------------- /lib/webgpu/GPUBindGroupLayout.dart: -------------------------------------------------------------------------------- 1 | part of webgpu; 2 | 3 | class GPUBindGroupLayout extends GPUObjectBase { 4 | late Pointer pointer; 5 | late WGPUBindGroupLayout bindGroupLayout; 6 | 7 | GPUBindGroupLayout(this.bindGroupLayout) { 8 | pointer = ffi.calloc(1); 9 | pointer[0] = bindGroupLayout; 10 | } 11 | 12 | GPUBindGroupLayout.pointer(this.pointer) {} 13 | } 14 | 15 | class GPUBindGroupLayoutDescriptor extends GPUObjectDescriptorBase { 16 | late Pointer pointer; 17 | 18 | GPUBindGroupLayoutDescriptor( 19 | {List? entries}) { 20 | pointer = ffi.calloc(); 21 | var state = pointer.ref; 22 | state.label = "Bind Group Layout".toNativeUtf8().cast(); 23 | 24 | if (entries == null) { 25 | state.entries = nullptr; 26 | } else { 27 | // state.entries = entries[0].pointer; 28 | var entryPointers = ffi.calloc(entries.length); 29 | entries.asMap().forEach((index, entry) { 30 | var pointer = entryPointers[index]; 31 | pointer.binding = entry.pointer.ref.binding; 32 | pointer.visibility = entry.pointer.ref.visibility; 33 | pointer.buffer = entry.pointer.ref.buffer; 34 | pointer.sampler = entry.pointer.ref.sampler; 35 | pointer.texture = entry.pointer.ref.texture; 36 | pointer.storageTexture = entry.pointer.ref.storageTexture; 37 | }); 38 | 39 | state.entries = entryPointers; 40 | state.entryCount = entries.length; 41 | } 42 | } 43 | } 44 | 45 | class GPUBufferBindingLayout { 46 | late Pointer pointer; 47 | 48 | GPUBufferBindingLayout({required int type, bool hasDynamicOffset = false, int? minBindingSize}) { 49 | pointer = ffi.calloc(); 50 | var state = pointer.ref; 51 | state.type = type; 52 | state.hasDynamicOffset = hasDynamicOffset; 53 | state.minBindingSize = minBindingSize ?? 0; 54 | } 55 | } 56 | 57 | class GPUSamplerBindingLayout { 58 | late Pointer pointer; 59 | 60 | GPUSamplerBindingLayout({required int type}) { 61 | pointer = ffi.calloc(); 62 | var state = pointer.ref; 63 | state.type = type; 64 | } 65 | } 66 | 67 | class GPUTextureBindingLayout { 68 | late Pointer pointer; 69 | 70 | GPUTextureBindingLayout({required int sampleType}) { 71 | pointer = ffi.calloc(); 72 | var state = pointer.ref; 73 | state.sampleType = sampleType; 74 | } 75 | } 76 | 77 | class GPUStorageTextureBindingLayout { 78 | late Pointer pointer; 79 | 80 | GPUStorageTextureBindingLayout({required int access}) { 81 | pointer = ffi.calloc(); 82 | var state = pointer.ref; 83 | state.access = access; 84 | } 85 | } 86 | -------------------------------------------------------------------------------- /lib/webgpu/GPUBindGroupLayoutEntry.dart: -------------------------------------------------------------------------------- 1 | part of webgpu; 2 | 3 | class GPUBindGroupLayoutEntry { 4 | late Pointer pointer; 5 | 6 | GPUBindGroupLayoutEntry( 7 | {required int binding, 8 | required int visibility, 9 | required GPUBufferBindingLayout buffer}) { 10 | pointer = ffi.calloc(); 11 | var state = pointer.ref; 12 | 13 | state.nextInChain = nullptr; 14 | state.binding = binding; 15 | state.visibility = visibility; 16 | state.buffer = buffer.pointer.ref; 17 | state.sampler = 18 | GPUSamplerBindingLayout(type: WGPUSamplerBindingType_Undefined) 19 | .pointer 20 | .ref; 21 | state.texture = 22 | GPUTextureBindingLayout(sampleType: WGPUTextureSampleType_Undefined) 23 | .pointer 24 | .ref; 25 | state.storageTexture = GPUStorageTextureBindingLayout( 26 | access: WGPUStorageTextureAccess_Undefined) 27 | .pointer 28 | .ref; 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /lib/webgpu/GPUBlendComponent.dart: -------------------------------------------------------------------------------- 1 | part of webgpu; 2 | 3 | class GPUBlendComponent { 4 | late Pointer pointer; 5 | 6 | GPUBlendComponent({int? srcFactor, int? dstFactor, int? operation}) { 7 | pointer = ffi.calloc(); 8 | var state = pointer.ref; 9 | state.srcFactor = srcFactor ?? GPUBlendFactor.One; 10 | state.dstFactor = dstFactor ?? GPUBlendFactor.Zero; 11 | state.operation = operation ?? GPUBlendOperation.Add; 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /lib/webgpu/GPUBuffer.dart: -------------------------------------------------------------------------------- 1 | part of webgpu; 2 | 3 | void readBufferMap(int status, Pointer userdata) { 4 | print(" readBufferMap callback status: ${status} userdata: ${userdata}....... "); 5 | 6 | } 7 | 8 | typedef ReadBufferMap = Void Function(Int32, Pointer); 9 | 10 | class GPUBuffer extends GPUObjectBase { 11 | late WGPUBuffer buffer; 12 | late Pointer pointer; 13 | 14 | GPUBuffer(this.buffer) { 15 | pointer = ffi.calloc(); 16 | pointer.value = buffer; 17 | } 18 | 19 | mapAsync({required int mode, int offset = 0, required int size}) { 20 | var readBufferMapCallback = 21 | Pointer.fromFunction(readBufferMap); 22 | Wgpu.binding.wgpuBufferMapAsync(buffer, WGPUMapMode.WGPUMapMode_Read, 23 | offset, size, readBufferMapCallback, nullptr); 24 | } 25 | 26 | Pointer getMappedRange({int offset = 0, int? size}) { 27 | return Wgpu.binding.wgpuBufferGetMappedRange(buffer, offset, size ?? 0); 28 | } 29 | 30 | unmap() { 31 | Wgpu.binding.wgpuBufferUnmap(buffer); 32 | } 33 | } 34 | 35 | class GPUBufferDescriptor extends GPUObjectDescriptorBase { 36 | late Pointer pointer; 37 | 38 | GPUBufferDescriptor( 39 | {required int size, required int usage, String? label, bool mappedAtCreation = false}) { 40 | pointer = ffi.calloc(); 41 | WGPUBufferDescriptor _descriptor = pointer.ref; 42 | _descriptor.nextInChain = nullptr; 43 | if(label != null) _descriptor.label = label.toNativeUtf8().cast(); 44 | _descriptor.usage = usage; 45 | _descriptor.size = size; 46 | _descriptor.mappedAtCreation = mappedAtCreation; 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /lib/webgpu/GPUColor.dart: -------------------------------------------------------------------------------- 1 | part of webgpu; 2 | 3 | class GPUColor { 4 | late Pointer pointer; 5 | 6 | GPUColor( 7 | {required double r, 8 | required double g, 9 | required double b, 10 | required double a}) { 11 | pointer = ffi.calloc(); 12 | WGPUColor _color = pointer.ref; 13 | _color.r = r; 14 | _color.g = g; 15 | _color.b = b; 16 | _color.a = a; 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /lib/webgpu/GPUCommandBuffer.dart: -------------------------------------------------------------------------------- 1 | part of webgpu; 2 | 3 | class GPUCommandBuffer extends GPUObjectBase { 4 | late Pointer pointer; 5 | late WGPUCommandBuffer buffer; 6 | 7 | GPUCommandBuffer(this.buffer) { 8 | pointer = ffi.calloc(); 9 | pointer.value = buffer; 10 | } 11 | } 12 | 13 | class GPUCommandBufferDescriptor extends GPUObjectDescriptorBase { 14 | late Pointer pointer; 15 | 16 | GPUCommandBufferDescriptor() { 17 | pointer = ffi.calloc(); 18 | WGPUCommandBufferDescriptor descriptor = pointer.ref; 19 | descriptor.label = nullptr; 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /lib/webgpu/GPUCommandEncoder.dart: -------------------------------------------------------------------------------- 1 | part of webgpu; 2 | 3 | class GPUCommandEncoder extends GPUObjectBase { 4 | late WGPUCommandEncoder encoder; 5 | 6 | GPUCommandEncoder(this.encoder) {} 7 | 8 | GPURenderPassEncoder beginRenderPass(GPURenderPassDescriptor descriptor) { 9 | WGPURenderPassEncoder renderPass = Wgpu.binding 10 | .wgpuCommandEncoderBeginRenderPass(encoder, descriptor.pointer); 11 | 12 | return GPURenderPassEncoder(renderPass); 13 | } 14 | 15 | GPUComputePassEncoder beginComputePass( 16 | [GPUComputePassDescriptor? descriptor]) { 17 | WGPUComputePassEncoder computePass = Wgpu.binding 18 | .wgpuCommandEncoderBeginComputePass( 19 | encoder, descriptor?.pointer ?? nullptr); 20 | 21 | return GPUComputePassEncoder(computePass); 22 | } 23 | 24 | void copyTextureToBuffer(GPUImageCopyTexture source, 25 | GPUImageCopyBuffer destination, GPUExtent3D copySize) { 26 | Wgpu.binding.wgpuCommandEncoderCopyTextureToBuffer( 27 | encoder, source.pointer, destination.pointer, copySize.pointer); 28 | } 29 | 30 | void copyBufferToBuffer(GPUBuffer source, int sourceOffset, 31 | GPUBuffer destination, int destinationOffset, int size) { 32 | Wgpu.binding.wgpuCommandEncoderCopyBufferToBuffer(encoder, source.buffer, 33 | sourceOffset, destination.buffer, destinationOffset, size); 34 | } 35 | 36 | GPUCommandBuffer finish([GPUCommandBufferDescriptor? descriptor]) { 37 | WGPUCommandBuffer buffer = Wgpu.binding 38 | .wgpuCommandEncoderFinish(encoder, descriptor?.pointer ?? nullptr); 39 | 40 | return GPUCommandBuffer(buffer); 41 | } 42 | } 43 | 44 | class GPUCommandEncoderDescriptor extends GPUObjectDescriptorBase { 45 | late Pointer pointer; 46 | 47 | GPUCommandEncoderDescriptor() { 48 | pointer = ffi.calloc(); 49 | WGPUCommandEncoderDescriptor descriptor = pointer.ref; 50 | descriptor.label = nullptr; 51 | } 52 | } 53 | 54 | class GPURenderPassColorAttachment { 55 | late Pointer pointer; 56 | 57 | GPURenderPassColorAttachment.pointer( 58 | Pointer pointer) { 59 | this.pointer = pointer; 60 | } 61 | 62 | // view 本应该是required的但是为了配合three.js的后赋值 暂时改为optional 63 | GPURenderPassColorAttachment( 64 | {GPUTextureView? view, 65 | GPUTextureView? resolveTarget, 66 | GPUColor? clearColor, 67 | required int loadOp, 68 | required int storeOp}) { 69 | pointer = ffi.calloc(); 70 | WGPURenderPassColorAttachment _attach = pointer.ref; 71 | if (view != null) _attach.view = view.textureView; 72 | _attach.resolveTarget = resolveTarget?.textureView ?? nullptr; 73 | _attach.loadOp = loadOp; 74 | _attach.storeOp = storeOp; 75 | if (clearColor != null) { 76 | _attach.clearValue = clearColor.pointer.ref; 77 | } 78 | } 79 | 80 | set view(GPUTextureView value) { 81 | var ref = pointer.ref; 82 | ref.view = value.textureView; 83 | } 84 | 85 | set clearColor(GPUColor color) { 86 | pointer.ref.clearValue = color.pointer.ref; 87 | } 88 | 89 | set resolveTarget(GPUTextureView? view) { 90 | pointer.ref.resolveTarget = view?.textureView ?? nullptr; 91 | } 92 | } 93 | 94 | class GPURenderPassDepthStencilAttachment { 95 | late Pointer pointer; 96 | 97 | WGPURenderPassDepthStencilAttachment get ref => pointer.ref; 98 | 99 | GPURenderPassDepthStencilAttachment.pointer(this.pointer) {} 100 | 101 | GPURenderPassDepthStencilAttachment( 102 | {GPUTextureView? view, 103 | double? depthClearValue, 104 | int? depthLoadOp, 105 | int? depthStoreOp, 106 | int? stencilStoreOp, 107 | int? stencilLoadOp, 108 | int? stencilClearValue}) { 109 | pointer = ffi.calloc(); 110 | 111 | ref.depthReadOnly = false; 112 | ref.stencilReadOnly = false; 113 | 114 | if (view != null) ref.view = view.textureView; 115 | if (depthStoreOp != null) ref.depthStoreOp = depthStoreOp; 116 | if (stencilStoreOp != null) ref.stencilStoreOp = stencilStoreOp; 117 | if (depthLoadOp != null) ref.depthLoadOp = depthLoadOp; 118 | if (stencilLoadOp != null) ref.stencilLoadOp = stencilLoadOp; 119 | if (depthClearValue != null) ref.depthClearValue = depthClearValue; 120 | if (stencilClearValue != null) ref.stencilClearValue = stencilClearValue; 121 | } 122 | 123 | set view(GPUTextureView value) { 124 | print("depthStencilAttachment view TODO "); 125 | ref.view = value.textureView; 126 | } 127 | 128 | set depthLoadValue(int value) { 129 | ref.depthLoadOp = value; 130 | } 131 | 132 | set stencilLoadValue(int value) { 133 | ref.stencilLoadOp = value; 134 | } 135 | 136 | set clearDepth(double value) { 137 | ref.depthClearValue = value; 138 | } 139 | 140 | set clearStencil(int value) { 141 | ref.stencilClearValue = value; 142 | } 143 | } 144 | -------------------------------------------------------------------------------- /lib/webgpu/GPUComputePassEncoder.dart: -------------------------------------------------------------------------------- 1 | part of webgpu; 2 | 3 | class GPUComputePassEncoder extends GPUObjectBase 4 | implements GPUProgrammablePassEncoder { 5 | late WGPUComputePassEncoder computePass; 6 | 7 | GPUComputePassEncoder(this.computePass) {} 8 | 9 | void setPipeline(GPUComputePipeline pipeline) { 10 | Wgpu.binding.wgpuComputePassEncoderSetPipeline( 11 | computePass, pipeline.computePipeline); 12 | } 13 | 14 | @override 15 | void setBindGroup(int v0, GPUBindGroup bindGroup, [int v1 = 0, v2]) { 16 | Wgpu.binding.wgpuComputePassEncoderSetBindGroup( 17 | computePass, v0, bindGroup.bindGroup, v1, v2 ?? nullptr); 18 | } 19 | 20 | // TODO 21 | void dispatchWorkgroups(int workgroupCountX, 22 | [int workgroupCountY = 1, int workgroupCountZ = 1]) { 23 | Wgpu.binding.wgpuComputePassEncoderDispatch( 24 | computePass, workgroupCountX, workgroupCountY, workgroupCountZ); 25 | } 26 | 27 | void end() { 28 | Wgpu.binding.wgpuComputePassEncoderEnd(computePass); 29 | } 30 | 31 | } 32 | 33 | class GPUComputePassDescriptor extends GPUObjectDescriptorBase { 34 | late Pointer pointer; 35 | 36 | GPUComputePassDescriptor() { 37 | pointer = ffi.calloc(); 38 | var ref = pointer.ref; 39 | 40 | ref.label = "Compute Pass".toNativeUtf8().cast(); 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /lib/webgpu/GPUComputePipeline.dart: -------------------------------------------------------------------------------- 1 | part of webgpu; 2 | 3 | class GPUComputePipeline extends GPUObjectBase implements GPUPipelineBase { 4 | late WGPUComputePipeline computePipeline; 5 | 6 | GPUComputePipeline(this.computePipeline) {} 7 | 8 | @override 9 | GPUBindGroupLayout getBindGroupLayout(int index) { 10 | WGPUBindGroupLayout layout = Wgpu.binding 11 | .wgpuComputePipelineGetBindGroupLayout(computePipeline, index); 12 | return GPUBindGroupLayout(layout); 13 | } 14 | } 15 | 16 | class GPUComputePipelineDescriptor extends GPUPipelineDescriptorBase { 17 | late Pointer pointer; 18 | 19 | GPUComputePipelineDescriptor( 20 | {required GPUProgrammableStage compute, GPUPipelineLayout? layout}) { 21 | pointer = ffi.calloc(); 22 | var ref = pointer.ref; 23 | ref.label = "GPUComputePipelineDescriptor".toNativeUtf8().cast(); 24 | ref.nextInChain = nullptr; 25 | ref.layout = layout?.pipelineLayout ?? nullptr; 26 | ref.compute = compute.pointer.ref; 27 | } 28 | } 29 | 30 | class GPUProgrammableStage { 31 | late Pointer pointer; 32 | 33 | GPUProgrammableStage( 34 | {required GPUShaderModule module, required String entryPoint}) { 35 | pointer = ffi.calloc(); 36 | var ref = pointer.ref; 37 | ref.nextInChain = nullptr; 38 | ref.module = module.shader; 39 | ref.entryPoint = entryPoint.toNativeUtf8().cast(); 40 | ref.constantCount = 0; 41 | ref.constants = nullptr; 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /lib/webgpu/GPUDevice.dart: -------------------------------------------------------------------------------- 1 | part of webgpu; 2 | 3 | class GPUDevice extends GPUObjectBase { 4 | late Pointer device; 5 | 6 | get webGPU => Wgpu.binding; 7 | 8 | GPUQueue? _queue; 9 | 10 | GPUQueue get queue { 11 | _queue ??= GPUQueue(webGPU.wgpuDeviceGetQueue(device.value)); 12 | 13 | return _queue!; 14 | } 15 | 16 | GPUDevice(this.device) {} 17 | 18 | GPUCommandEncoder createCommandEncoder( 19 | [GPUCommandEncoderDescriptor? descriptor]) { 20 | WGPUCommandEncoder encoder = webGPU.wgpuDeviceCreateCommandEncoder( 21 | device.value, (descriptor ?? GPUCommandEncoderDescriptor()).pointer); 22 | 23 | return GPUCommandEncoder(encoder); 24 | } 25 | 26 | GPUBuffer createBuffer(GPUBufferDescriptor descriptor) { 27 | WGPUBuffer outputBuffer = 28 | webGPU.wgpuDeviceCreateBuffer(device.value, descriptor.pointer); 29 | 30 | return GPUBuffer(outputBuffer); 31 | } 32 | 33 | GPUTexture createTexture(GPUTextureDescriptor descriptor) { 34 | WGPUTexture texture = 35 | webGPU.wgpuDeviceCreateTexture(device.value, descriptor.pointer); 36 | 37 | return GPUTexture(texture); 38 | } 39 | 40 | GPUSampler createSampler([GPUSamplerDescriptor? descriptor]) { 41 | WGPUSampler sampler = webGPU.wgpuDeviceCreateSampler( 42 | device.value, descriptor?.pointer ?? nullptr); 43 | return GPUSampler(sampler); 44 | } 45 | 46 | GPUShaderModule createShaderModule(GPUShaderModuleDescriptor descriptor) { 47 | WGPUShaderModule shader = 48 | webGPU.wgpuDeviceCreateShaderModule(device.value, descriptor.pointer); 49 | return GPUShaderModule(shader); 50 | } 51 | 52 | GPUPipelineLayout createPipelineLayout( 53 | GPUPipelineLayoutDescriptor descriptor) { 54 | WGPUPipelineLayout pipelineLayout = 55 | webGPU.wgpuDeviceCreatePipelineLayout(device.value, descriptor.pointer); 56 | 57 | return GPUPipelineLayout(pipelineLayout); 58 | } 59 | 60 | GPURenderPipeline createRenderPipeline( 61 | GPURenderPipelineDescriptor descriptor) { 62 | WGPURenderPipeline pipeline = 63 | webGPU.wgpuDeviceCreateRenderPipeline(device.value, descriptor.pointer); 64 | 65 | return GPURenderPipeline(pipeline); 66 | } 67 | 68 | GPUComputePipeline createComputePipeline( 69 | GPUComputePipelineDescriptor descriptor) { 70 | WGPUComputePipeline computePipeline = webGPU 71 | .wgpuDeviceCreateComputePipeline(device.value, descriptor.pointer); 72 | return GPUComputePipeline(computePipeline); 73 | } 74 | 75 | GPUBindGroupLayout createBindGroupLayout( 76 | GPUBindGroupLayoutDescriptor descriptor) { 77 | WGPUBindGroupLayout bindGroupLayout = webGPU 78 | .wgpuDeviceCreateBindGroupLayout(device.value, descriptor.pointer); 79 | return GPUBindGroupLayout(bindGroupLayout); 80 | } 81 | 82 | GPUBindGroup createBindGroup(GPUBindGroupDescriptor descriptor) { 83 | WGPUBindGroup bindGroup = 84 | webGPU.wgpuDeviceCreateBindGroup(device.value, descriptor.pointer); 85 | 86 | return GPUBindGroup(bindGroup); 87 | } 88 | 89 | void poll([bool forceWait = true]) { 90 | webGPU.wgpuDevicePoll(device.value, forceWait, nullptr); 91 | } 92 | 93 | void setUncapturedErrorCallback() { 94 | var _callback = Pointer.fromFunction( 95 | handleUncapturedError); 96 | webGPU.wgpuDeviceSetUncapturedErrorCallback( 97 | device.value, _callback, nullptr); 98 | } 99 | 100 | void setDeviceLostCallback() { 101 | var _callback = 102 | Pointer.fromFunction(handleDeviceLost); 103 | webGPU.wgpuDeviceSetDeviceLostCallback(device.value, _callback, nullptr); 104 | } 105 | } 106 | 107 | void handleDeviceLost( 108 | int reason, Pointer message, Pointer userdata) { 109 | print("---------------- handleDeviceLost reason: ${reason} "); 110 | print(userdata); 111 | print(message.cast().toDartString()); 112 | } 113 | 114 | void handleUncapturedError( 115 | int type, Pointer message, Pointer userdata) { 116 | print("----------------- ErrorCallback type: ${type} "); 117 | print(userdata); 118 | print(message.cast().toDartString()); 119 | } 120 | 121 | typedef HandleDeviceLostNative = Void Function( 122 | Int32, Pointer, Pointer); 123 | typedef HandleUncapturedErrorNative = Void Function( 124 | Int32, Pointer, Pointer); 125 | 126 | class GPUDeviceDescriptor { 127 | late Pointer pointer; 128 | GPUDeviceDescriptor({int maxBindGroups = 1}) { 129 | pointer = ffi.calloc(); 130 | var ref = pointer.ref; 131 | 132 | Pointer chain = ffi.calloc(); 133 | WGPUChainedStruct _chain = chain.ref; 134 | _chain.next = nullptr; 135 | _chain.sType = WGPUNativeSType.WGPUSType_DeviceExtras; 136 | 137 | Pointer requiredLimits = 138 | ffi.calloc(); 139 | WGPURequiredLimits _requiredLimits = requiredLimits.ref; 140 | _requiredLimits.nextInChain = nullptr; 141 | 142 | Pointer limits = ffi.calloc(); 143 | WGPULimits _limits = limits.ref; 144 | _limits.maxBindGroups = maxBindGroups; 145 | 146 | _requiredLimits.limits = _limits; 147 | 148 | Pointer deviceExtras = ffi.calloc(); 149 | WGPUDeviceExtras _deviceExtras = deviceExtras.ref; 150 | _deviceExtras.chain = _chain; 151 | _deviceExtras.tracePath = nullptr; 152 | 153 | ref.nextInChain = deviceExtras.cast(); 154 | ref.label = "WGPUDeviceDescriptor".toNativeUtf8().cast(); 155 | ref.requiredLimits = requiredLimits; 156 | } 157 | } 158 | -------------------------------------------------------------------------------- /lib/webgpu/GPUExtent3D.dart: -------------------------------------------------------------------------------- 1 | part of webgpu; 2 | 3 | class GPUExtent3D { 4 | late Pointer pointer; 5 | 6 | late int width; 7 | late int height; 8 | 9 | GPUExtent3D({this.width = 1, this.height = 1, int depthOrArrayLayers = 1}) { 10 | pointer = ffi.calloc(); 11 | WGPUExtent3D _extent = pointer.ref; 12 | _extent.width = width; 13 | _extent.height = height; 14 | _extent.depthOrArrayLayers = depthOrArrayLayers; 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /lib/webgpu/GPUImageCopyBuffer.dart: -------------------------------------------------------------------------------- 1 | part of webgpu; 2 | 3 | class GPUImageCopyBuffer { 4 | @override 5 | late Pointer pointer; 6 | 7 | GPUImageCopyBuffer({required GPUBuffer buffer, required int bytesPerRow}) { 8 | pointer = ffi.calloc(); 9 | WGPUImageCopyBuffer _copyBuffer = pointer.ref; 10 | _copyBuffer.buffer = buffer.buffer; 11 | 12 | Pointer layout = ffi.calloc(); 13 | WGPUTextureDataLayout _layout = layout.ref; 14 | _layout.offset = 0; 15 | _layout.bytesPerRow = bytesPerRow; 16 | _layout.rowsPerImage = 0; 17 | _copyBuffer.layout = _layout; 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /lib/webgpu/GPUImageCopyTexture.dart: -------------------------------------------------------------------------------- 1 | part of webgpu; 2 | 3 | class GPUImageCopyTexture { 4 | late Pointer pointer; 5 | 6 | GPUImageCopyTexture( 7 | {required GPUTexture texture, 8 | int mipLevel = 0, 9 | GPUOrigin3D? origin, 10 | int aspect = GPUTextureAspect.All}) { 11 | pointer = ffi.calloc(); 12 | WGPUImageCopyTexture _copyTexture = pointer.ref; 13 | _copyTexture.texture = texture.texture; 14 | _copyTexture.mipLevel = mipLevel; 15 | _copyTexture.origin = (origin ?? GPUOrigin3D()).pointer.ref; 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /lib/webgpu/GPUObjectBase.dart: -------------------------------------------------------------------------------- 1 | part of webgpu; 2 | 3 | class GPUObjectBase { 4 | String? label; 5 | } 6 | 7 | class GPUObjectDescriptorBase { 8 | String? label; 9 | } 10 | -------------------------------------------------------------------------------- /lib/webgpu/GPUOrigin3D.dart: -------------------------------------------------------------------------------- 1 | part of webgpu; 2 | 3 | class GPUOrigin3D { 4 | late Pointer pointer; 5 | 6 | GPUOrigin3D({int x = 0, int y = 0, int z = 0}) { 7 | pointer = ffi.calloc(); 8 | WGPUOrigin3D _origin = pointer.ref; 9 | _origin.x = x; 10 | _origin.y = y; 11 | _origin.z = z; 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /lib/webgpu/GPUPipelineBase.dart: -------------------------------------------------------------------------------- 1 | part of webgpu; 2 | 3 | abstract class GPUPipelineBase { 4 | GPUBindGroupLayout getBindGroupLayout(int index); 5 | } 6 | -------------------------------------------------------------------------------- /lib/webgpu/GPUPipelineDescriptorBase.dart: -------------------------------------------------------------------------------- 1 | part of webgpu; 2 | 3 | class GPUPipelineDescriptorBase extends GPUObjectDescriptorBase { 4 | late GPUPipelineLayout layout; 5 | } 6 | -------------------------------------------------------------------------------- /lib/webgpu/GPUPipelineLayout.dart: -------------------------------------------------------------------------------- 1 | part of webgpu; 2 | 3 | class GPUPipelineLayout extends GPUObjectBase { 4 | late WGPUPipelineLayout pipelineLayout; 5 | 6 | GPUPipelineLayout(this.pipelineLayout) {} 7 | } 8 | 9 | class GPUPipelineLayoutDescriptor extends GPUObjectDescriptorBase { 10 | late Pointer pointer; 11 | 12 | GPUPipelineLayoutDescriptor( 13 | {GPUBindGroupLayout? bindGroupLayouts, int bindGroupLayoutCount = 0}) { 14 | pointer = ffi.calloc(); 15 | var descriptor = pointer.ref; 16 | 17 | descriptor.bindGroupLayouts = 18 | bindGroupLayouts == null ? nullptr : bindGroupLayouts.pointer; 19 | descriptor.bindGroupLayoutCount = bindGroupLayoutCount; 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /lib/webgpu/GPUProgrammablePassEncoder.dart: -------------------------------------------------------------------------------- 1 | part of webgpu; 2 | 3 | abstract class GPUProgrammablePassEncoder { 4 | void setBindGroup(int v0, GPUBindGroup bindGroup, [int v1 = 0, v2]) {} 5 | } 6 | -------------------------------------------------------------------------------- /lib/webgpu/GPUQueue.dart: -------------------------------------------------------------------------------- 1 | part of webgpu; 2 | 3 | class GPUQueue extends GPUObjectBase { 4 | late WGPUQueue queue; 5 | 6 | GPUQueue(this.queue) {} 7 | 8 | // TODO 9 | // commandBuffers can be sequence 10 | // https://gpuweb.github.io/gpuweb/#dom-gpuqueue-submit 11 | void submit(GPUCommandBuffer commandBuffers) { 12 | Wgpu.binding.wgpuQueueSubmit(queue, 1, commandBuffers.pointer); 13 | } 14 | 15 | void writeBuffer( 16 | GPUBuffer buffer, 17 | int bufferOffset, 18 | data, 19 | int size, 20 | ) { 21 | // TODO 对应类型转对应类型的指针 22 | // print("GPUQueue writeBuffer confirm convert to right pointer ${data.runtimeType} ..... "); 23 | // print(data); 24 | 25 | var ptr; 26 | 27 | if (data is Uint32List) { 28 | ptr = ffi.calloc(data.length); 29 | (ptr as Pointer) 30 | .asTypedList(data.length) 31 | .setAll(0, List.from(data.map((e) => e.toInt()))); 32 | } else if (data is Float32List) { 33 | ptr = ffi.calloc(data.length); 34 | (ptr as Pointer) 35 | .asTypedList(data.length) 36 | .setAll(0, List.from(data.map((e) => e.toDouble()))); 37 | } else { 38 | throw ("GPUQueue ${data.runtimeType} not support...... "); 39 | } 40 | 41 | 42 | // size lengthInBytes 43 | Wgpu.binding.wgpuQueueWriteBuffer( 44 | queue, buffer.buffer, bufferOffset, ptr.cast(), size); 45 | } 46 | 47 | void writeTexture(GPUImageCopyTexture destination, data, 48 | GPUTextureDataLayout dataLayout, GPUExtent3D size) { 49 | // TODO 对应类型转对应类型的指针 50 | print("confirm convert to right pointer ..... "); 51 | final ptr = ffi.calloc(data.length); 52 | ptr 53 | .asTypedList(data.length) 54 | .setAll(0, List.from(data.map((e) => e.toInt()))); 55 | 56 | int dataSize = data.length; 57 | 58 | Wgpu.binding.wgpuQueueWriteTexture(queue, destination.pointer, ptr.cast(), 59 | dataSize, dataLayout.pointer, size.pointer); 60 | } 61 | } 62 | 63 | typedef GPUImageDataLayout = GPUTextureDataLayout; 64 | 65 | class GPUTextureDataLayout { 66 | late Pointer pointer; 67 | 68 | GPUTextureDataLayout({int offset = 0, int? bytesPerRow}) { 69 | pointer = ffi.calloc(); 70 | var ref = pointer.ref; 71 | ref.offset = offset; 72 | if (bytesPerRow != null) ref.bytesPerRow = bytesPerRow; 73 | } 74 | } 75 | -------------------------------------------------------------------------------- /lib/webgpu/GPURenderEncoderBase.dart: -------------------------------------------------------------------------------- 1 | part of webgpu; 2 | 3 | abstract class GPURenderEncoderBase { 4 | void setPipeline(GPURenderPipeline pipeline); 5 | 6 | void setIndexBuffer(GPUBuffer buffer, int indexFormat, 7 | [int offset = 0, int? size]); 8 | void setVertexBuffer(int slot, GPUBuffer buffer, [int offset = 0, int? size]); 9 | 10 | void draw(int vertexCount, 11 | [int instanceCount = 1, int firstVertex = 0, int firstInstance = 0]); 12 | void drawIndexed(int indexCount, 13 | [int instanceCount = 1, 14 | int firstIndex = 0, 15 | int baseVertex = 0, 16 | int firstInstance = 0]); 17 | 18 | void drawIndirect(GPUBuffer indirectBuffer, int indirectOffset); 19 | void drawIndexedIndirect(GPUBuffer indirectBuffer, int indirectOffset); 20 | } 21 | -------------------------------------------------------------------------------- /lib/webgpu/GPURenderPassDescriptor.dart: -------------------------------------------------------------------------------- 1 | part of webgpu; 2 | 3 | class GPURenderPassDescriptor extends GPUObjectDescriptorBase { 4 | late Pointer pointer; 5 | 6 | GPURenderPassDescriptor({ 7 | GPURenderPassColorAttachment? colorAttachments, 8 | GPURenderPassDepthStencilAttachment? depthStencilAttachment, 9 | // GPUQuerySet occlusionQuerySet, 10 | // GPURenderPassTimestampWrites timestampWrites = [] 11 | }) { 12 | pointer = ffi.calloc(); 13 | WGPURenderPassDescriptor descriptor = pointer.ref; 14 | descriptor.colorAttachments = colorAttachments?.pointer ?? nullptr; 15 | descriptor.colorAttachmentCount = 1; 16 | descriptor.depthStencilAttachment = 17 | depthStencilAttachment?.pointer ?? nullptr; 18 | } 19 | 20 | get colorAttachments => 21 | GPURenderPassColorAttachment.pointer(pointer.ref.colorAttachments); 22 | get depthStencilAttachment { 23 | print("depthStencilAttachment TODO "); 24 | return GPURenderPassDepthStencilAttachment.pointer( 25 | pointer.ref.depthStencilAttachment); 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /lib/webgpu/GPURenderPassEncoder.dart: -------------------------------------------------------------------------------- 1 | part of webgpu; 2 | 3 | class GPURenderPassEncoder extends GPUObjectBase 4 | implements GPUProgrammablePassEncoder, GPURenderEncoderBase { 5 | late WGPURenderPassEncoder encoder; 6 | 7 | GPURenderPassEncoder(this.encoder) {} 8 | 9 | @override 10 | void setPipeline(GPURenderPipeline pipeline) { 11 | Wgpu.binding.wgpuRenderPassEncoderSetPipeline(encoder, pipeline.pipeline); 12 | } 13 | 14 | @override 15 | void setVertexBuffer(int slot, GPUBuffer buffer, 16 | [int offset = 0, int? size]) { 17 | Wgpu.binding.wgpuRenderPassEncoderSetVertexBuffer( 18 | encoder, slot, buffer.buffer, offset, size ?? 0); 19 | } 20 | 21 | @override 22 | void setBindGroup(int v0, GPUBindGroup bindGroup, [int v1 = 0, v2]) { 23 | Wgpu.binding.wgpuRenderPassEncoderSetBindGroup( 24 | encoder, v0, bindGroup.bindGroup, v1, v2 ?? nullptr); 25 | } 26 | 27 | @override 28 | void draw(int vertexCount, 29 | [int instanceCount = 1, int firstVertex = 0, int firstInstance = 0]) { 30 | Wgpu.binding.wgpuRenderPassEncoderDraw( 31 | encoder, vertexCount, instanceCount, firstVertex, firstInstance); 32 | } 33 | 34 | end() { 35 | Wgpu.binding.wgpuRenderPassEncoderEnd(encoder); 36 | } 37 | 38 | setViewport(double x, double y, double width, double height, double minDepth, 39 | double maxDepth) { 40 | Wgpu.binding.wgpuRenderPassEncoderSetViewport( 41 | encoder, x, y, width, height, minDepth, maxDepth); 42 | } 43 | 44 | setScissorRect(int x, int y, int width, int height) { 45 | Wgpu.binding 46 | .wgpuRenderPassEncoderSetScissorRect(encoder, x, y, width, height); 47 | } 48 | 49 | @override 50 | setIndexBuffer(GPUBuffer buffer, int indexFormat, 51 | [int offset = 0, int? size]) { 52 | Wgpu.binding.wgpuRenderPassEncoderSetIndexBuffer( 53 | encoder, buffer.buffer, indexFormat, offset, size ?? 0); 54 | } 55 | 56 | drawIndexed(int indexCount, 57 | [int instanceCount = 1, 58 | int firstIndex = 0, 59 | int baseVertex = 0, 60 | int firstInstance = 0]) { 61 | Wgpu.binding.wgpuRenderPassEncoderDrawIndexed(encoder, indexCount, 62 | instanceCount, firstIndex, baseVertex, firstInstance); 63 | } 64 | 65 | @override 66 | void drawIndexedIndirect(GPUBuffer indirectBuffer, int indirectOffset) { 67 | // TODO: implement drawIndexedIndirect 68 | throw (" TODO: implement drawIndexedIndirect "); 69 | } 70 | 71 | @override 72 | void drawIndirect(GPUBuffer indirectBuffer, int indirectOffset) { 73 | // TODO: implement drawIndirect 74 | throw (" TODO: implement drawIndirect "); 75 | } 76 | } 77 | -------------------------------------------------------------------------------- /lib/webgpu/GPURenderPipeline.dart: -------------------------------------------------------------------------------- 1 | part of webgpu; 2 | 3 | class GPURenderPipeline extends GPUObjectBase implements GPUPipelineBase { 4 | late WGPURenderPipeline pipeline; 5 | 6 | GPURenderPipeline(this.pipeline) {} 7 | 8 | @override 9 | GPUBindGroupLayout getBindGroupLayout(int index) { 10 | WGPUBindGroupLayout layout = 11 | Wgpu.binding.wgpuRenderPipelineGetBindGroupLayout(pipeline, index); 12 | return GPUBindGroupLayout(layout); 13 | } 14 | } 15 | 16 | class GPURenderPipelineDescriptor extends GPUPipelineDescriptorBase { 17 | late Pointer pointer; 18 | 19 | GPURenderPipelineDescriptor( 20 | {GPUPipelineLayout? layout, 21 | required GPUVertexState vertex, 22 | required GPUPrimitiveState primitive, 23 | required GPUMultisampleState multisample, 24 | required GPUFragmentState fragment, 25 | GPUDepthStencilState? depthStencil}) { 26 | pointer = ffi.calloc(); 27 | var descriptor = pointer.ref; 28 | descriptor.label = "label".toNativeUtf8().cast(); 29 | descriptor.layout = layout?.pipelineLayout ?? nullptr; 30 | descriptor.depthStencil = depthStencil?.pointer ?? nullptr; 31 | 32 | descriptor.vertex = vertex.pointer.ref; 33 | descriptor.primitive = primitive.pointer.ref; 34 | descriptor.multisample = multisample.pointer.ref; 35 | descriptor.fragment = fragment.pointer; 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /lib/webgpu/GPUSampler.dart: -------------------------------------------------------------------------------- 1 | part of webgpu; 2 | 3 | class GPUSampler extends GPUObjectBase { 4 | late WGPUSampler sampler; 5 | 6 | GPUSampler(this.sampler) {} 7 | } 8 | 9 | class GPUSamplerDescriptor extends GPUObjectDescriptorBase { 10 | late Pointer pointer; 11 | 12 | GPUSamplerDescriptor( 13 | {int addressModeU = GPUAddressMode.ClampToEdge, 14 | int addressModeV = GPUAddressMode.ClampToEdge, 15 | int addressModeW = GPUAddressMode.ClampToEdge, 16 | double lodMinClamp = 0.0, 17 | double lodMaxClamp = 32.0, 18 | int magFilter = GPUFilterMode.Nearest, 19 | int minFilter = GPUFilterMode.Nearest, 20 | int mipmapFilter = GPUFilterMode.Nearest, 21 | int maxAnisotropy = 1}) { 22 | pointer = ffi.calloc(); 23 | var ref = pointer.ref; 24 | ref.nextInChain = nullptr; 25 | ref.label = "WGPUSamplerDescriptor".toNativeUtf8().cast(); 26 | ref.addressModeU = addressModeU; 27 | ref.addressModeV = addressModeV; 28 | ref.addressModeW = addressModeW; 29 | ref.lodMinClamp = lodMinClamp; 30 | ref.lodMaxClamp = lodMaxClamp; 31 | ref.magFilter = magFilter; 32 | ref.minFilter = minFilter; 33 | ref.mipmapFilter = mipmapFilter; 34 | ref.maxAnisotropy = maxAnisotropy; 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /lib/webgpu/GPUShaderModule.dart: -------------------------------------------------------------------------------- 1 | part of webgpu; 2 | 3 | class GPUShaderModule extends GPUObjectBase { 4 | late WGPUShaderModule shader; 5 | 6 | GPUShaderModule(this.shader) {} 7 | } 8 | 9 | class GPUShaderModuleDescriptor extends GPUObjectDescriptorBase { 10 | late Pointer pointer; 11 | 12 | GPUShaderModuleDescriptor({required String code}) { 13 | var wgslPointer = ffi.calloc(); 14 | var wgslRef = wgslPointer.ref; 15 | wgslRef.chain.next = nullptr; 16 | wgslRef.chain.sType = WGPUSType_ShaderModuleWGSLDescriptor; 17 | wgslRef.code = code.toNativeUtf8().cast(); 18 | 19 | pointer = ffi.calloc(); 20 | var ref = pointer.ref; 21 | ref.nextInChain = wgslPointer.cast(); 22 | ref.label = "shaderModule".toNativeUtf8().cast(); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /lib/webgpu/GPUState.dart: -------------------------------------------------------------------------------- 1 | part of webgpu; 2 | 3 | class GPUVertexState { 4 | late Pointer pointer; 5 | 6 | GPUVertexState( 7 | {required GPUShaderModule module, 8 | required String entryPoint, 9 | List? buffers}) { 10 | pointer = ffi.calloc(); 11 | var state = pointer.ref; 12 | 13 | state.module = module.shader; 14 | state.entryPoint = entryPoint.toNativeUtf8().cast(); 15 | state.bufferCount = buffers?.length ?? 0; 16 | 17 | if (buffers == null) { 18 | state.buffers = nullptr; 19 | } else if (buffers.length == 1) { 20 | state.buffers = buffers[0].pointer; 21 | } else { 22 | var buffersPointer = ffi.calloc(buffers.length); 23 | 24 | buffers.asMap().forEach((key, value) { 25 | var _p = buffersPointer[key]; 26 | _p = value.pointer.ref; 27 | 28 | _p.arrayStride = value.pointer.ref.arrayStride; 29 | _p.stepMode = value.pointer.ref.stepMode; 30 | _p.attributeCount = value.pointer.ref.attributeCount; 31 | _p.attributes = value.pointer.ref.attributes; 32 | }); 33 | 34 | state.buffers = buffersPointer; 35 | } 36 | } 37 | } 38 | 39 | class GPUPrimitiveState { 40 | late Pointer pointer; 41 | 42 | GPUPrimitiveState( 43 | {int? topology, int? stripIndexFormat, int? frontFace, int? cullMode}) { 44 | pointer = ffi.calloc(); 45 | var state = pointer.ref; 46 | state.topology = topology ?? GPUPrimitiveTopology.TriangleList; 47 | state.stripIndexFormat = stripIndexFormat ?? GPUIndexFormat.Undefined; 48 | state.frontFace = frontFace ?? GPUFrontFace.CCW; 49 | state.cullMode = cullMode ?? GPUCullMode.None; 50 | } 51 | } 52 | 53 | class GPUMultisampleState { 54 | late Pointer pointer; 55 | 56 | GPUMultisampleState( 57 | {int count = 1, int mask = ~0, bool alphaToCoverageEnabled = false}) { 58 | pointer = ffi.calloc(); 59 | var state = pointer.ref; 60 | state.count = count; 61 | state.mask = mask; 62 | state.alphaToCoverageEnabled = alphaToCoverageEnabled; 63 | } 64 | } 65 | 66 | class GPUFragmentState { 67 | late Pointer pointer; 68 | 69 | GPUFragmentState( 70 | {required GPUShaderModule module, 71 | required GPUColorTargetState targets, 72 | required String entryPoint}) { 73 | pointer = ffi.calloc(); 74 | var state = pointer.ref; 75 | 76 | state.module = module.shader; 77 | state.entryPoint = entryPoint.toNativeUtf8().cast(); 78 | state.targetCount = 1; 79 | state.targets = targets.pointer; 80 | } 81 | } 82 | 83 | class GPUColorTargetState { 84 | late Pointer pointer; 85 | 86 | GPUColorTargetState( 87 | {GPUBlendState? blend, 88 | int writeMask = GPUColorWriteFlags.All, 89 | required int format}) { 90 | pointer = ffi.calloc(); 91 | var state = pointer.ref; 92 | 93 | state.format = format; 94 | 95 | state.blend = blend?.pointer ?? nullptr; 96 | state.writeMask = writeMask; 97 | } 98 | } 99 | 100 | class GPUBlendState { 101 | late Pointer pointer; 102 | 103 | GPUBlendState({ 104 | required GPUBlendComponent color, 105 | required GPUBlendComponent alpha, 106 | }) { 107 | pointer = ffi.calloc(); 108 | var state = pointer.ref; 109 | state.color = color.pointer.ref; 110 | state.alpha = alpha.pointer.ref; 111 | } 112 | } 113 | 114 | class GPUDepthStencilState { 115 | late Pointer pointer; 116 | 117 | GPUDepthStencilState( 118 | {required int format, 119 | bool depthWriteEnabled = false, 120 | int? depthCompare, 121 | required GPUStencilFaceState stencilFront, 122 | GPUStencilFaceState? stencilBack, 123 | int? stencilReadMask, 124 | int? stencilWriteMask, 125 | int? depthBias, 126 | double? depthBiasSlopeScale, 127 | double? depthBiasClamp}) { 128 | pointer = ffi.calloc(); 129 | var ref = pointer.ref; 130 | ref.format = format; 131 | ref.depthWriteEnabled = depthWriteEnabled; 132 | ref.depthCompare = depthCompare ?? GPUCompareFunction.Always; 133 | ref.stencilFront = stencilFront.pointer.ref; 134 | ref.stencilBack = 135 | (stencilBack ?? GPUStencilFaceState(compare: GPUCompareFunction.Always)) 136 | .pointer 137 | .ref; 138 | ref.stencilReadMask = stencilReadMask ?? 0xFFFFFFFF; 139 | ref.stencilWriteMask = stencilWriteMask ?? 0xFFFFFFFF; 140 | ref.depthBias = depthBias ?? 0; 141 | ref.depthBiasSlopeScale = depthBiasSlopeScale ?? 0; 142 | ref.depthBiasClamp = depthBiasClamp ?? 0; 143 | } 144 | } 145 | 146 | class GPUStencilFaceState { 147 | late Pointer pointer; 148 | 149 | GPUStencilFaceState( 150 | {int? compare, int? failOp, int? depthFailOp, int? passOp}) { 151 | pointer = ffi.calloc(); 152 | var ref = pointer.ref; 153 | 154 | ref.compare = compare ?? GPUCompareFunction.Always; 155 | 156 | ref.failOp = failOp ?? GPUStencilOperation.Keep; 157 | ref.depthFailOp = depthFailOp ?? GPUStencilOperation.Keep; 158 | ref.passOp = passOp ?? GPUStencilOperation.Keep; 159 | } 160 | } 161 | -------------------------------------------------------------------------------- /lib/webgpu/GPUTexture.dart: -------------------------------------------------------------------------------- 1 | part of webgpu; 2 | 3 | class GPUTexture extends GPUObjectBase { 4 | late WGPUTexture texture; 5 | 6 | GPUTexture(this.texture) {} 7 | 8 | GPUTextureView createView([GPUTextureViewDescriptor? descriptor]) { 9 | WGPUTextureView textureView = Wgpu.binding.wgpuTextureCreateView( 10 | texture, (descriptor ?? GPUTextureViewDescriptor()).pointer); 11 | 12 | return GPUTextureView(textureView); 13 | } 14 | 15 | destroy() { 16 | Wgpu.binding.wgpuTextureDestroy(texture); 17 | } 18 | } 19 | 20 | class GPUTextureDescriptor extends GPUObjectDescriptorBase { 21 | late Pointer pointer; 22 | 23 | GPUTextureDescriptor( 24 | {required GPUExtent3D size, 25 | int mipLevelCount = 1, 26 | int sampleCount = 1, 27 | int? dimension, 28 | required int format, 29 | required int usage}) { 30 | pointer = ffi.calloc(); 31 | WGPUTextureDescriptor _descriptor = pointer.ref; 32 | _descriptor.nextInChain = nullptr; 33 | _descriptor.label = "WGPUTextureDescriptor".toNativeUtf8().cast(); 34 | _descriptor.size = size.pointer.ref; 35 | _descriptor.mipLevelCount = mipLevelCount; 36 | _descriptor.sampleCount = sampleCount; 37 | _descriptor.dimension = dimension ?? GPUTextureDimension.TwoD; 38 | _descriptor.format = format; 39 | _descriptor.usage = usage; 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /lib/webgpu/GPUTextureView.dart: -------------------------------------------------------------------------------- 1 | part of webgpu; 2 | 3 | class GPUTextureView extends GPUObjectBase { 4 | late WGPUTextureView textureView; 5 | 6 | GPUTextureView(this.textureView) {} 7 | } 8 | 9 | class GPUTextureViewDescriptor extends GPUObjectDescriptorBase { 10 | late Pointer pointer; 11 | 12 | GPUTextureViewDescriptor( 13 | {int? format, 14 | int baseMipLevel = 0, 15 | int mipLevelCount = 0, 16 | int? arrayLayerCount, 17 | int? aspect, 18 | int? baseArrayLayer, 19 | int? dimension}) { 20 | pointer = ffi.calloc(); 21 | WGPUTextureViewDescriptor _descriptor = pointer.ref; 22 | _descriptor.nextInChain = nullptr; 23 | _descriptor.label = nullptr; 24 | _descriptor.format = format ?? GPUTextureFormat.Undefined; 25 | _descriptor.dimension = dimension ?? GPUTextureViewDimension.Undefined; 26 | _descriptor.aspect = aspect ?? WGPUTextureAspect.WGPUTextureAspect_All; 27 | _descriptor.arrayLayerCount = arrayLayerCount ?? 0; 28 | _descriptor.baseArrayLayer = baseArrayLayer ?? 0; 29 | _descriptor.baseMipLevel = baseMipLevel; 30 | _descriptor.mipLevelCount = mipLevelCount; 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /lib/webgpu/GPUVertexBufferLayout.dart: -------------------------------------------------------------------------------- 1 | part of webgpu; 2 | 3 | class GPUVertexBufferLayout { 4 | late Pointer pointer; 5 | 6 | GPUVertexBufferLayout( 7 | {required int arrayStride, 8 | int stepMode = GPUVertexStepMode.Vertex, 9 | required List attributes}) { 10 | pointer = ffi.calloc(); 11 | var ref = pointer.ref; 12 | ref.arrayStride = arrayStride; 13 | ref.attributeCount = attributes.length; 14 | ref.stepMode = stepMode; 15 | 16 | if (attributes.length == 1) { 17 | ref.attributes = attributes[0].pointer; 18 | } else { 19 | var attributesPointer = 20 | ffi.calloc(attributes.length); 21 | 22 | attributes.asMap().forEach((key, value) { 23 | var _p = attributesPointer[key]; 24 | _p.format = value.pointer.ref.format; 25 | _p.offset = value.pointer.ref.offset; 26 | _p.shaderLocation = value.pointer.ref.shaderLocation; 27 | }); 28 | 29 | ref.attributes = attributesPointer; 30 | } 31 | } 32 | } 33 | 34 | class GPUVertexAttribute { 35 | late Pointer pointer; 36 | GPUVertexAttribute( 37 | {required int format, required int offset, required int shaderLocation}) { 38 | pointer = ffi.calloc(); 39 | var ref = pointer.ref; 40 | 41 | ref.format = format; 42 | ref.offset = offset; 43 | ref.shaderLocation = shaderLocation; 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /lib/webgpu/gpu.dart: -------------------------------------------------------------------------------- 1 | part of webgpu; 2 | 3 | void wgpuRequestAdapterCallback(int status, WGPUAdapter received, 4 | Pointer message, Pointer userdata) { 5 | Pointer _adapter = userdata.cast(); 6 | _adapter.value = received; 7 | 8 | print( 9 | "wgpuRequestAdapterCallback success adapter: ${_adapter} ${_adapter.value}"); 10 | } 11 | 12 | typedef WgpuRequestAdapterCallback = Void Function( 13 | Int32, WGPUAdapter, Pointer, Pointer); 14 | 15 | GPUAdapter requestAdapter(GPURequestAdapterOptions options) { 16 | Pointer adapter = ffi.calloc(); 17 | var adapterCallback = Pointer.fromFunction( 18 | wgpuRequestAdapterCallback); 19 | 20 | Wgpu.binding.wgpuInstanceRequestAdapter( 21 | nullptr, options.pointer, adapterCallback, adapter.cast()); 22 | 23 | return GPUAdapter(adapter); 24 | } 25 | 26 | class GPURequestAdapterOptions { 27 | late Pointer pointer; 28 | 29 | GPURequestAdapterOptions( 30 | {int? powerPreference, bool forceFallbackAdapter = false}) { 31 | pointer = ffi.calloc(); 32 | var ref = pointer.ref; 33 | ref.nextInChain = nullptr; 34 | ref.compatibleSurface = nullptr; 35 | ref.powerPreference = powerPreference ?? GPUPowerPreference.Undefined; 36 | ref.forceFallbackAdapter = forceFallbackAdapter; 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /lib/webgpu/index.dart: -------------------------------------------------------------------------------- 1 | library webgpu; 2 | 3 | import 'dart:io'; 4 | import 'dart:typed_data'; 5 | 6 | import 'package:flutter/material.dart'; 7 | import 'package:flutter_webgpu/WebGPU_Binding.dart' hide bool; 8 | import 'package:ffi/ffi.dart' as ffi; 9 | import 'dart:ffi'; 10 | 11 | part 'gpu.dart'; 12 | part 'GPUAdapter.dart'; 13 | part 'GPUDevice.dart'; 14 | part 'wgpu.dart'; 15 | part 'GPUCommandEncoder.dart'; 16 | part 'GPUTexture.dart'; 17 | part 'GPUBuffer.dart'; 18 | part 'GPUExtent3D.dart'; 19 | part 'GPUTextureView.dart'; 20 | part 'GPURenderPassEncoder.dart'; 21 | part 'GPUColor.dart'; 22 | part 'GPUImageCopyTexture.dart'; 23 | part 'GPUImageCopyBuffer.dart'; 24 | part 'GPUOrigin3D.dart'; 25 | part 'GPUQueue.dart'; 26 | part 'GPUCommandBuffer.dart'; 27 | part 'constant.dart'; 28 | part 'GPUShaderModule.dart'; 29 | part 'GPUPipelineLayout.dart'; 30 | part 'GPURenderPipeline.dart'; 31 | part 'GPUState.dart'; 32 | part 'GPUBlendComponent.dart'; 33 | part 'GPUBindGroupLayout.dart'; 34 | part 'GPUBindGroup.dart'; 35 | part 'GPUComputePipeline.dart'; 36 | part 'GPUComputePassEncoder.dart'; 37 | part 'GPUObjectBase.dart'; 38 | part 'GPUSampler.dart'; 39 | part 'GPUProgrammablePassEncoder.dart'; 40 | part 'GPUVertexBufferLayout.dart'; 41 | part 'GPUBindGroupLayoutEntry.dart'; 42 | part 'GPUPipelineBase.dart'; 43 | part 'GPUPipelineDescriptorBase.dart'; 44 | part 'GPURenderPassDescriptor.dart'; 45 | part 'GPURenderEncoderBase.dart'; 46 | -------------------------------------------------------------------------------- /lib/webgpu/wgpu.dart: -------------------------------------------------------------------------------- 1 | part of webgpu; 2 | 3 | class Wgpu { 4 | late WebGPUBinding _webGPU; 5 | WebGPUBinding get webGPU => _webGPU; 6 | 7 | static Wgpu? _instance; 8 | static Wgpu get instance { 9 | _instance ??= Wgpu(); 10 | return _instance!; 11 | } 12 | 13 | static WebGPUBinding get binding => instance.webGPU; 14 | 15 | Wgpu() { 16 | final DynamicLibrary dynamicLibrary = Platform.isAndroid 17 | ? DynamicLibrary.open("libwgpu_native.so") 18 | : DynamicLibrary.process(); 19 | 20 | _webGPU = WebGPUBinding(dynamicLibrary); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /macos/Classes/FlutterWebgpuPlugin.swift: -------------------------------------------------------------------------------- 1 | import Cocoa 2 | import FlutterMacOS 3 | 4 | public class FlutterWebgpuPlugin: NSObject, FlutterPlugin { 5 | public static func register(with registrar: FlutterPluginRegistrar) { 6 | let channel = FlutterMethodChannel(name: "flutter_webgpu", binaryMessenger: registrar.messenger) 7 | let instance = FlutterWebgpuPlugin() 8 | registrar.addMethodCallDelegate(instance, channel: channel) 9 | } 10 | 11 | public func handle(_ call: FlutterMethodCall, result: @escaping FlutterResult) { 12 | switch call.method { 13 | case "getPlatformVersion": 14 | result("macOS " + ProcessInfo.processInfo.operatingSystemVersionString) 15 | default: 16 | result(FlutterMethodNotImplemented) 17 | } 18 | } 19 | 20 | // https://medium.com/flutter-community/using-ffi-on-flutter-plugins-to-run-native-rust-code-d64c0f14f9c2 21 | // Note: XCode will not bundle the library unless it detects explicit usage within the workspace. Since our Dart code calling it is out of the scope of XCode, we need to write a dummy Swift function that makes some fake usage. 22 | public func dummyMethodToEnforceBundling() { 23 | // This will never be executed 24 | wgpuGetVersion(); 25 | } 26 | 27 | 28 | } 29 | -------------------------------------------------------------------------------- /macos/Classes/ffi: -------------------------------------------------------------------------------- 1 | ../../native/wgpu-native/ffi/ -------------------------------------------------------------------------------- /macos/Classes/flutter_webgpu-Bridging-Header.h: -------------------------------------------------------------------------------- 1 | // 2 | // flutter_webgpu-Bridging-Header.h 3 | // flutter_webgpu 4 | // 5 | // Created by 赵磊 on 2022/2/3. 6 | // 7 | 8 | #ifndef flutter_webgpu_Bridging_Header_h 9 | #define flutter_webgpu_Bridging_Header_h 10 | 11 | #endif /* flutter_webgpu_Bridging_Header_h */ 12 | -------------------------------------------------------------------------------- /macos/flutter_webgpu.podspec: -------------------------------------------------------------------------------- 1 | # 2 | # To learn more about a Podspec see http://guides.cocoapods.org/syntax/podspec.html. 3 | # Run `pod lib lint flutter_webgpu.podspec` to validate before publishing. 4 | # 5 | Pod::Spec.new do |s| 6 | s.name = 'flutter_webgpu' 7 | s.version = '0.0.1' 8 | s.summary = 'A new flutter plugin project.' 9 | s.description = <<-DESC 10 | A new flutter plugin project. 11 | DESC 12 | s.homepage = 'http://example.com' 13 | s.license = { :file => '../LICENSE' } 14 | s.author = { 'Your Company' => 'email@example.com' } 15 | s.source = { :path => '.' } 16 | s.source_files = 'Classes/**/*' 17 | s.dependency 'FlutterMacOS' 18 | 19 | s.platform = :osx, '10.11' 20 | s.pod_target_xcconfig = { 'DEFINES_MODULE' => 'YES' } 21 | s.swift_version = '5.0' 22 | 23 | 24 | # s.public_header_files = 'Classes/ffi/**/*.h' 25 | s.static_framework = true 26 | s.vendored_libraries = "**/*.a" 27 | 28 | end 29 | -------------------------------------------------------------------------------- /macos/libwgpu_native.a: -------------------------------------------------------------------------------- 1 | ../native/wgpu-native/target/debug/libwgpu_native.a -------------------------------------------------------------------------------- /macos/libwgpu_native.dylib: -------------------------------------------------------------------------------- 1 | ../native/wgpu-native/target/debug/libwgpu_native.dylib -------------------------------------------------------------------------------- /pubspec.yaml: -------------------------------------------------------------------------------- 1 | name: flutter_webgpu 2 | description: cross-platform call WebGPU API by Dart through dart:ffi. 3D programming in the cross-platform. Provides WebGPU with Texture Widget on Flutter. 3 | version: 0.0.1 4 | homepage: https://github.com/wasabia/flutter_webgpu 5 | 6 | environment: 7 | sdk: ">=2.15.1 <3.0.0" 8 | flutter: ">=2.5.0" 9 | 10 | dependencies: 11 | ffigen: ^6.0.1 12 | flutter: 13 | sdk: flutter 14 | ffi: ^2.0.1 15 | 16 | dev_dependencies: 17 | flutter_test: 18 | sdk: flutter 19 | flutter_lints: ^1.0.0 20 | 21 | 22 | # For information on the generic Dart part of this file, see the 23 | # following page: https://dart.dev/tools/pub/pubspec 24 | 25 | # The following section is specific to Flutter. 26 | flutter: 27 | # This section identifies this Flutter project as a plugin project. 28 | # The 'pluginClass' and Android 'package' identifiers should not ordinarily 29 | # be modified. They are used by the tooling to maintain consistency when 30 | # adding or updating assets for this project. 31 | plugin: 32 | platforms: 33 | android: 34 | package: com.futouapp.flutter_webgpu.flutter_webgpu 35 | pluginClass: FlutterWebgpuPlugin 36 | ios: 37 | pluginClass: FlutterWebgpuPlugin 38 | macos: 39 | pluginClass: FlutterWebgpuPlugin 40 | 41 | # To add assets to your plugin package, add an assets section, like this: 42 | # assets: 43 | # - images/a_dot_burr.jpeg 44 | # - images/a_dot_ham.jpeg 45 | # 46 | # For details regarding assets in packages, see 47 | # https://flutter.dev/assets-and-images/#from-packages 48 | # 49 | # An image asset can refer to one or more resolution-specific "variants", see 50 | # https://flutter.dev/assets-and-images/#resolution-aware. 51 | 52 | # To add custom fonts to your plugin package, add a fonts section here, 53 | # in this "flutter" section. Each entry in this list should have a 54 | # "family" key with the font family name, and a "fonts" key with a 55 | # list giving the asset and other descriptors for the font. For 56 | # example: 57 | # fonts: 58 | # - family: Schyler 59 | # fonts: 60 | # - asset: fonts/Schyler-Regular.ttf 61 | # - asset: fonts/Schyler-Italic.ttf 62 | # style: italic 63 | # - family: Trajan Pro 64 | # fonts: 65 | # - asset: fonts/TrajanPro.ttf 66 | # - asset: fonts/TrajanPro_Bold.ttf 67 | # weight: 700 68 | # 69 | # For details regarding fonts in packages, see 70 | # https://flutter.dev/custom-fonts/#from-packages 71 | -------------------------------------------------------------------------------- /test/flutter_webgpu_test.dart: -------------------------------------------------------------------------------- 1 | import 'package:flutter/services.dart'; 2 | import 'package:flutter_test/flutter_test.dart'; 3 | import 'package:flutter_webgpu/flutter_webgpu.dart'; 4 | 5 | void main() { 6 | const MethodChannel channel = MethodChannel('flutter_webgpu'); 7 | 8 | TestWidgetsFlutterBinding.ensureInitialized(); 9 | 10 | setUp(() { 11 | channel.setMockMethodCallHandler((MethodCall methodCall) async { 12 | return '42'; 13 | }); 14 | }); 15 | 16 | tearDown(() { 17 | channel.setMockMethodCallHandler(null); 18 | }); 19 | 20 | test('getPlatformVersion', () async { 21 | expect(await FlutterWebgpu.platformVersion, '42'); 22 | }); 23 | } 24 | --------------------------------------------------------------------------------