Skip to content

Commit b1efc28

Browse files
committed
Cleanup intro_flutter_gpu
1 parent 6cfdbdb commit b1efc28

File tree

29 files changed

+522
-500
lines changed

29 files changed

+522
-500
lines changed

intro_flutter_gpu/codelab_rebuild.yaml

Lines changed: 385 additions & 378 deletions
Large diffs are not rendered by default.

intro_flutter_gpu/step_01/lib/main.dart

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ class TrianglePainter extends CustomPainter {
3333

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

46+
// Create a render target for the texture
4547
final renderTarget = gpu.RenderTarget.singleColor(
4648
gpu.ColorAttachment(texture: texture),
4749
);
4850

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

55+
// Load our shaders
5256
final vert = shaderLibrary['SimpleVertex'];
5357
if (vert == null) {
5458
throw Exception('Failed to load SimpleVertex vertex shader');
@@ -59,22 +63,26 @@ class TrianglePainter extends CustomPainter {
5963
throw Exception('Failed to load SimpleFragment fragment shader');
6064
}
6165

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

69+
// Define our triangle vertices
6470
const floatsPerVertex = 2;
6571
final vertices = Float32List.fromList([
6672
-0.5, -0.5, // First vertex
67-
0.5, -0.5, // Second vertex
68-
0.0, 0.5, // Third vertex
73+
0.5, -0.5, // Second vertex
74+
0.0, 0.5, // Third vertex
6975
]);
7076

77+
// Create a GPU buffer for our vertices
7178
final verticesDeviceBuffer = gpu.gpuContext.createDeviceBufferWithCopy(
7279
ByteData.sublistView(vertices),
7380
);
7481
if (verticesDeviceBuffer == null) {
7582
throw Exception('Failed to create vertices device buffer');
7683
}
7784

85+
// Bind the pipeline and vertex buffer
7886
renderPass.bindPipeline(pipeline);
7987

8088
final verticesView = gpu.BufferView(
@@ -87,8 +95,10 @@ class TrianglePainter extends CustomPainter {
8795
vertices.length ~/ floatsPerVertex,
8896
);
8997

98+
// Draw the triangle
9099
renderPass.draw();
91100

101+
// Submit commands to GPU and render to screen
92102
commandBuffer.submit();
93103
final image = texture.asImage();
94104
canvas.drawImage(image, Offset.zero, Paint());

intro_flutter_gpu/step_01/pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ publish_to: 'none'
44
version: 0.1.0
55

66
environment:
7-
sdk: ^3.7.0-0
7+
sdk: ^3.8.0-0
88

99
dependencies:
1010
flutter:

intro_flutter_gpu/step_02/lib/main.dart

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -61,12 +61,12 @@ class TrianglePainter extends CustomPainter {
6161

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

64-
const floatsPerVertex = 5;
64+
const floatsPerVertex = 5; // Now 2 for position + 3 for color
6565
final vertices = Float32List.fromList([
66-
// Format: x, y, r, g, b,
67-
-0.5, -0.5, 1.0, 0.0, 0.0,
68-
0.5, -0.5, 0.0, 1.0, 0.0,
69-
0.0, 0.5, 0.0, 0.0, 1.0,
66+
// Format: x, y, r, g, b
67+
-0.5, -0.5, 1.0, 0.0, 0.0, // bottom left - red
68+
0.5, -0.5, 0.0, 1.0, 0.0, // bottom right - green
69+
0.0, 0.5, 0.0, 0.0, 1.0, // top - blue
7070
]);
7171

7272
final verticesDeviceBuffer = gpu.gpuContext.createDeviceBufferWithCopy(

intro_flutter_gpu/step_02/pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ publish_to: 'none'
44
version: 0.1.0
55

66
environment:
7-
sdk: ^3.7.0-0
7+
sdk: ^3.8.0-0
88

99
dependencies:
1010
flutter:

intro_flutter_gpu/step_03/lib/main.dart

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -63,16 +63,17 @@ class TrianglePainter extends CustomPainter {
6363

6464
const floatsPerVertex = 5;
6565
final vertices = Float32List.fromList([
66-
// Format: x, y, r, g, b,
67-
68-
// Traingle #1
69-
-0.5, -0.5, 1.0, 0.0, 0.0, // bottom left
70-
0.5, -0.5, 0.0, 1.0, 0.0, // bottom right
71-
-0.5, 0.5, 0.0, 0.0, 1.0, // top left
72-
// Traingle #2
73-
0.5, -0.5, 0.0, 1.0, 0.0, // bottom right
74-
0.5, 0.5, 1.0, 1.0, 0.0, // top right
75-
-0.5, 0.5, 0.0, 0.0, 1.0, // top left
66+
// Format: x, y, r, g, b
67+
68+
// Triangle #1
69+
-0.5, -0.5, 1.0, 0.0, 0.0, // bottom left - red
70+
0.5, -0.5, 0.0, 1.0, 0.0, // bottom right - green
71+
-0.5, 0.5, 0.0, 0.0, 1.0, // top left - blue
72+
73+
// Triangle #2
74+
0.5, -0.5, 0.0, 1.0, 0.0, // bottom right - green
75+
0.5, 0.5, 1.0, 1.0, 0.0, // top right - yellow
76+
-0.5, 0.5, 0.0, 0.0, 1.0, // top left - blue
7677
]);
7778

7879
final verticesDeviceBuffer = gpu.gpuContext.createDeviceBufferWithCopy(

intro_flutter_gpu/step_03/pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ publish_to: 'none'
44
version: 0.1.0
55

66
environment:
7-
sdk: ^3.7.0-0
7+
sdk: ^3.8.0-0
88

99
dependencies:
1010
flutter:

intro_flutter_gpu/step_04/lib/main.dart

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -63,16 +63,17 @@ class TrianglePainter extends CustomPainter {
6363

6464
const floatsPerVertex = 5;
6565
final vertices = Float32List.fromList([
66-
// Format: x, y, r, g, b,
67-
68-
// Traingle #1
69-
-0.5, -0.5, 0.0, 0.0, 1.0, // bottom left
70-
0.5, -0.5, 1.0, 1.0, 0.0, // bottom right
71-
-0.5, 0.5, 1.0, 0.0, 0.0, // top left
72-
// Traingle #2
73-
0.5, -0.5, 1.0, 1.0, 0.0, // bottom right
74-
0.5, 0.5, 0.0, 1.0, 0.0, // top right
75-
-0.5, 0.5, 1.0, 0.0, 0.0, // top left
66+
// Format: x, y, r, g, b
67+
68+
// Triangle #1
69+
-0.5, -0.5, 0.0, 0.0, 1.0, // bottom left - blue
70+
0.5, -0.5, 1.0, 1.0, 0.0, // bottom right - yellow
71+
-0.5, 0.5, 1.0, 0.0, 0.0, // top left - red
72+
73+
// Triangle #2
74+
0.5, -0.5, 1.0, 1.0, 0.0, // bottom right - yellow
75+
0.5, 0.5, 0.0, 1.0, 0.0, // top right - green
76+
-0.5, 0.5, 1.0, 0.0, 0.0, // top left - red
7677
]);
7778

7879
final verticesDeviceBuffer = gpu.gpuContext.createDeviceBufferWithCopy(

intro_flutter_gpu/step_04/pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ publish_to: 'none'
44
version: 0.1.0
55

66
environment:
7-
sdk: ^3.7.0-0
7+
sdk: ^3.8.0-0
88

99
dependencies:
1010
flutter:

intro_flutter_gpu/step_05/lib/main.dart

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -61,18 +61,19 @@ class TrianglePainter extends CustomPainter {
6161

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

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

68-
// Traingle #1
68+
// Triangle #1
6969
-0.5, -0.5, 0.0, 0.0, // bottom left
70-
0.5, -0.5, 1.0, 0.0, // bottom right
71-
-0.5, 0.5, 0.0, 1.0, // top left
72-
// Traingle #2
73-
0.5, -0.5, 1.0, 0.0, // bottom right
74-
0.5, 0.5, 1.0, 1.0, // top right
75-
-0.5, 0.5, 0.0, 1.0, // top left
70+
0.5, -0.5, 1.0, 0.0, // bottom right
71+
-0.5, 0.5, 0.0, 1.0, // top left
72+
73+
// Triangle #2
74+
0.5, -0.5, 1.0, 0.0, // bottom right
75+
0.5, 0.5, 1.0, 1.0, // top right
76+
-0.5, 0.5, 0.0, 1.0, // top left
7677
]);
7778

7879
final verticesDeviceBuffer = gpu.gpuContext.createDeviceBufferWithCopy(

0 commit comments

Comments
 (0)