Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
753 changes: 371 additions & 382 deletions intro_flutter_gpu/codelab_rebuild.yaml

Large diffs are not rendered by default.

16 changes: 11 additions & 5 deletions intro_flutter_gpu/step_01/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ class TrianglePainter extends CustomPainter {

@override
void paint(Canvas canvas, Size size) {
// Create a texture to render to
final texture = gpu.gpuContext.createTexture(
gpu.StorageMode.devicePrivate,
size.width.ceil(),
Expand All @@ -42,13 +43,16 @@ class TrianglePainter extends CustomPainter {
throw Exception('Failed to create texture');
}

// Create a render target for the texture
final renderTarget = gpu.RenderTarget.singleColor(
gpu.ColorAttachment(texture: texture),
);

// Create a command buffer and render pass
final commandBuffer = gpu.gpuContext.createCommandBuffer();
final renderPass = commandBuffer.createRenderPass(renderTarget);

// Load our shaders
final vert = shaderLibrary['SimpleVertex'];
if (vert == null) {
throw Exception('Failed to load SimpleVertex vertex shader');
Expand All @@ -59,22 +63,22 @@ class TrianglePainter extends CustomPainter {
throw Exception('Failed to load SimpleFragment fragment shader');
}

// Create the rendering pipeline
final pipeline = gpu.gpuContext.createRenderPipeline(vert, frag);

// Define our triangle vertices
const floatsPerVertex = 2;
final vertices = Float32List.fromList([
-0.5, -0.5, // First vertex
0.5, -0.5, // Second vertex
0.0, 0.5, // Third vertex
]);
final vertices = Float32List.fromList([-0.5, -0.5, 0.5, -0.5, 0.0, 0.5]);

// Create a GPU buffer for our vertices
final verticesDeviceBuffer = gpu.gpuContext.createDeviceBufferWithCopy(
ByteData.sublistView(vertices),
);
if (verticesDeviceBuffer == null) {
throw Exception('Failed to create vertices device buffer');
}

// Bind the pipeline and vertex buffer
renderPass.bindPipeline(pipeline);

final verticesView = gpu.BufferView(
Expand All @@ -87,8 +91,10 @@ class TrianglePainter extends CustomPainter {
vertices.length ~/ floatsPerVertex,
);

// Draw the triangle
renderPass.draw();

// Submit commands to GPU and render to screen
commandBuffer.submit();
final image = texture.asImage();
canvas.drawImage(image, Offset.zero, Paint());
Expand Down
2 changes: 1 addition & 1 deletion intro_flutter_gpu/step_01/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ publish_to: 'none'
version: 0.1.0

environment:
sdk: ^3.7.0-0
sdk: ^3.8.0-0

dependencies:
flutter:
Expand Down
4 changes: 2 additions & 2 deletions intro_flutter_gpu/step_02/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,9 @@ class TrianglePainter extends CustomPainter {

final pipeline = gpu.gpuContext.createRenderPipeline(vert, frag);

const floatsPerVertex = 5;
const floatsPerVertex = 5; // Now 2 for position + 3 for color
final vertices = Float32List.fromList([
// Format: x, y, r, g, b,
// Format: x, y, r, g, b
-0.5, -0.5, 1.0, 0.0, 0.0,
0.5, -0.5, 0.0, 1.0, 0.0,
0.0, 0.5, 0.0, 0.0, 1.0,
Expand Down
2 changes: 1 addition & 1 deletion intro_flutter_gpu/step_02/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ publish_to: 'none'
version: 0.1.0

environment:
sdk: ^3.7.0-0
sdk: ^3.8.0-0

dependencies:
flutter:
Expand Down
20 changes: 10 additions & 10 deletions intro_flutter_gpu/step_03/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -63,16 +63,16 @@ class TrianglePainter extends CustomPainter {

const floatsPerVertex = 5;
final vertices = Float32List.fromList([
// Format: x, y, r, g, b,

// Traingle #1
-0.5, -0.5, 1.0, 0.0, 0.0, // bottom left
0.5, -0.5, 0.0, 1.0, 0.0, // bottom right
-0.5, 0.5, 0.0, 0.0, 1.0, // top left
// Traingle #2
0.5, -0.5, 0.0, 1.0, 0.0, // bottom right
0.5, 0.5, 1.0, 1.0, 0.0, // top right
-0.5, 0.5, 0.0, 0.0, 1.0, // top left
// Format: x, y, r, g, b

// Triangle #1
-0.5, -0.5, 1.0, 0.0, 0.0,
0.5, -0.5, 0.0, 1.0, 0.0,
-0.5, 0.5, 0.0, 0.0, 1.0,
// Triangle #2
0.5, -0.5, 0.0, 1.0, 0.0,
0.5, 0.5, 1.0, 1.0, 0.0,
-0.5, 0.5, 0.0, 0.0, 1.0,
]);

final verticesDeviceBuffer = gpu.gpuContext.createDeviceBufferWithCopy(
Expand Down
2 changes: 1 addition & 1 deletion intro_flutter_gpu/step_03/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ publish_to: 'none'
version: 0.1.0

environment:
sdk: ^3.7.0-0
sdk: ^3.8.0-0

dependencies:
flutter:
Expand Down
20 changes: 10 additions & 10 deletions intro_flutter_gpu/step_04/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -63,16 +63,16 @@ class TrianglePainter extends CustomPainter {

const floatsPerVertex = 5;
final vertices = Float32List.fromList([
// Format: x, y, r, g, b,

// Traingle #1
-0.5, -0.5, 0.0, 0.0, 1.0, // bottom left
0.5, -0.5, 1.0, 1.0, 0.0, // bottom right
-0.5, 0.5, 1.0, 0.0, 0.0, // top left
// Traingle #2
0.5, -0.5, 1.0, 1.0, 0.0, // bottom right
0.5, 0.5, 0.0, 1.0, 0.0, // top right
-0.5, 0.5, 1.0, 0.0, 0.0, // top left
// Format: x, y, r, g, b

// Triangle #1
-0.5, -0.5, 0.0, 0.0, 1.0,
0.5, -0.5, 1.0, 1.0, 0.0,
-0.5, 0.5, 1.0, 0.0, 0.0,
// Triangle #2
0.5, -0.5, 1.0, 1.0, 0.0,
0.5, 0.5, 0.0, 1.0, 0.0,
-0.5, 0.5, 1.0, 0.0, 0.0,
]);

final verticesDeviceBuffer = gpu.gpuContext.createDeviceBufferWithCopy(
Expand Down
2 changes: 1 addition & 1 deletion intro_flutter_gpu/step_04/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ publish_to: 'none'
version: 0.1.0

environment:
sdk: ^3.7.0-0
sdk: ^3.8.0-0

dependencies:
flutter:
Expand Down
22 changes: 11 additions & 11 deletions intro_flutter_gpu/step_05/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -61,18 +61,18 @@ class TrianglePainter extends CustomPainter {

final pipeline = gpu.gpuContext.createRenderPipeline(vert, frag);

const floatsPerVertex = 4;
const floatsPerVertex = 4; // Now 2 for position + 2 for UV
final vertices = Float32List.fromList([
// Format: x, y, u, v,

// Traingle #1
-0.5, -0.5, 0.0, 0.0, // bottom left
0.5, -0.5, 1.0, 0.0, // bottom right
-0.5, 0.5, 0.0, 1.0, // top left
// Traingle #2
0.5, -0.5, 1.0, 0.0, // bottom right
0.5, 0.5, 1.0, 1.0, // top right
-0.5, 0.5, 0.0, 1.0, // top left
// Format: x, y, u, v

// Triangle #1
-0.5, -0.5, 0.0, 0.0,
0.5, -0.5, 1.0, 0.0,
-0.5, 0.5, 0.0, 1.0,
// Triangle #2
0.5, -0.5, 1.0, 0.0,
0.5, 0.5, 1.0, 1.0,
-0.5, 0.5, 0.0, 1.0,
]);

final verticesDeviceBuffer = gpu.gpuContext.createDeviceBufferWithCopy(
Expand Down
2 changes: 1 addition & 1 deletion intro_flutter_gpu/step_05/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ publish_to: 'none'
version: 0.1.0

environment:
sdk: ^3.7.0-0
sdk: ^3.8.0-0

dependencies:
flutter:
Expand Down
12 changes: 6 additions & 6 deletions intro_flutter_gpu/step_05/shaders/simple.frag
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@
in vec2 vertex_uv;
out vec4 frag_color;

const vec4 top_left_color = vec4(1, 0, 0, 1);
const vec4 top_right_color = vec4(0, 1, 0, 1);
const vec4 bottom_left_color = vec4(0, 0, 1, 1);
const vec4 bottom_right_color = vec4(1, 1, 0, 1);
const vec4 red = vec4(1, 0, 0, 1);
const vec4 green = vec4(0, 1, 0, 1);
const vec4 blue = vec4(0, 0, 1, 1);
const vec4 yellow = vec4(1, 1, 0, 1);

void main() {
frag_color =
mix(mix(bottom_left_color, bottom_right_color, vertex_uv.x),
mix(top_left_color, top_right_color, vertex_uv.x), vertex_uv.y);
mix(mix(blue, yellow, vertex_uv.x),
mix(red, green, vertex_uv.x), vertex_uv.y);
}
22 changes: 11 additions & 11 deletions intro_flutter_gpu/step_06/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -61,18 +61,18 @@ class TrianglePainter extends CustomPainter {

final pipeline = gpu.gpuContext.createRenderPipeline(vert, frag);

const floatsPerVertex = 4;
const floatsPerVertex = 4; // 2 for position + 2 for UV
final vertices = Float32List.fromList([
// Format: x, y, u, v,

// Traingle #1
-0.8, -0.8, -1.0, -1.0, // bottom left
0.8, -0.8, 1.0, -1.0, // bottom right
-0.8, 0.8, -1.0, 1.0, // top left
// Traingle #2
0.8, -0.8, 1.0, -1.0, // bottom right
0.8, 0.8, 1.0, 1.0, // top right
-0.8, 0.8, -1.0, 1.0, // top left
// Format: x, y, u, v

// Triangle #1
-0.8, -0.8, -1.0, -1.0,
0.8, -0.8, 1.0, -1.0,
-0.8, 0.8, -1.0, 1.0,
// Triangle #2
0.8, -0.8, 1.0, -1.0,
0.8, 0.8, 1.0, 1.0,
-0.8, 0.8, -1.0, 1.0,
]);

final verticesDeviceBuffer = gpu.gpuContext.createDeviceBufferWithCopy(
Expand Down
2 changes: 1 addition & 1 deletion intro_flutter_gpu/step_06/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ publish_to: 'none'
version: 0.1.0

environment:
sdk: ^3.7.0-0
sdk: ^3.8.0-0

dependencies:
flutter:
Expand Down
15 changes: 7 additions & 8 deletions intro_flutter_gpu/step_06/shaders/simple.frag
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,13 @@ out vec4 frag_color;

// Adapted from https://www.youtube.com/shorts/h5PuIm6fRr8
float mandelbrot(vec2 uv) {
const float MAX_ITER = 128;
vec2 c = 1.2 * uv - vec2(0.7, 0.0);
const float MAX_ITER = 128.0;
vec2 c = 1.2 * uv - vec2(0.7, 0.0);
vec2 z = vec2(0.0);

for (float iter = 0.0; iter < MAX_ITER; iter++) {
z = vec2(z.x * z.x - z.y * z.y, 2.0 * z.x * z.y) + c;

if (length(z) > 4.0) {
return iter / MAX_ITER;
}
Expand All @@ -23,11 +24,9 @@ float mandelbrot(vec2 uv) {
return 0.0;
}


void main() {
float m = mandelbrot(vertex_uv);
vec3 col = vec3(m);
col = pow(col, vec3(0.4545, 0.3, 0.1));
frag_color = vec4(col, 1.0);

float m = mandelbrot(vertex_uv);
vec3 col = vec3(m);
col = pow(col, vec3(0.4545, 0.3, 0.1)); // Color grading
frag_color = vec4(col, 1.0);
}
21 changes: 10 additions & 11 deletions intro_flutter_gpu/step_07/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,12 @@ class _MainAppState extends State<MainApp> with SingleTickerProviderStateMixin {
home: Scaffold(
body: SizedBox.expand(
child: AnimatedBuilder(
animation: _animation,
builder: (context, child) {
return CustomPaint(
painter: TrianglePainter(angle: _animation.value),
);
},
animation: _animation,
),
),
),
Expand Down Expand Up @@ -100,16 +100,13 @@ class TrianglePainter extends CustomPainter {

const floatsPerVertex = 4;
final vertices = Float32List.fromList([
// Format: x, y, u, v,

// Traingle #1
-0.8, -0.8, -1.0, -1.0, // bottom left
0.8, -0.8, 1.0, -1.0, // bottom right
-0.8, 0.8, -1.0, 1.0, // top left
// Traingle #2
0.8, -0.8, 1.0, -1.0, // bottom right
0.8, 0.8, 1.0, 1.0, // top right
-0.8, 0.8, -1.0, 1.0, // top left
// Format: x, y, u, v
-0.8, -0.8, -1.0, -1.0,
0.8, -0.8, 1.0, -1.0,
-0.8, 0.8, -1.0, 1.0,
0.8, -0.8, 1.0, -1.0,
0.8, 0.8, 1.0, 1.0,
-0.8, 0.8, -1.0, 1.0,
]);

final verticesDeviceBuffer = gpu.gpuContext.createDeviceBufferWithCopy(
Expand All @@ -119,8 +116,10 @@ class TrianglePainter extends CustomPainter {
throw Exception('Failed to create vertices device buffer');
}

// Create model matrix for rotation
final model = vm.Matrix4.rotationY(angle);

// Create uniform buffer with transformation matrix
final vertUniforms = [model];

final vertUniformsDeviceBuffer = gpu.gpuContext.createDeviceBufferWithCopy(
Expand Down
2 changes: 1 addition & 1 deletion intro_flutter_gpu/step_07/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ publish_to: 'none'
version: 0.1.0

environment:
sdk: ^3.7.0-0
sdk: ^3.8.0-0

dependencies:
flutter:
Expand Down
15 changes: 7 additions & 8 deletions intro_flutter_gpu/step_07/shaders/simple.frag
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,13 @@ out vec4 frag_color;

// Adapted from https://www.youtube.com/shorts/h5PuIm6fRr8
float mandelbrot(vec2 uv) {
const float MAX_ITER = 128;
vec2 c = 1.2 * uv - vec2(0.7, 0.0);
const float MAX_ITER = 128.0;
vec2 c = 1.2 * uv - vec2(0.7, 0.0);
vec2 z = vec2(0.0);

for (float iter = 0.0; iter < MAX_ITER; iter++) {
z = vec2(z.x * z.x - z.y * z.y, 2.0 * z.x * z.y) + c;

if (length(z) > 4.0) {
return iter / MAX_ITER;
}
Expand All @@ -23,11 +24,9 @@ float mandelbrot(vec2 uv) {
return 0.0;
}


void main() {
float m = mandelbrot(vertex_uv);
vec3 col = vec3(m);
col = pow(col, vec3(0.4545, 0.3, 0.1));
frag_color = vec4(col, 1.0);

float m = mandelbrot(vertex_uv);
vec3 col = vec3(m);
col = pow(col, vec3(0.4545, 0.3, 0.1));
frag_color = vec4(col, 1.0);
}
Loading