@@ -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,22 @@ 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 ;
65- final vertices = Float32List .fromList ([
66- - 0.5 , - 0.5 , // First vertex
67- 0.5 , - 0.5 , // Second vertex
68- 0.0 , 0.5 , // Third vertex
69- ]);
71+ final vertices = Float32List .fromList ([- 0.5 , - 0.5 , 0.5 , - 0.5 , 0.0 , 0.5 ]);
7072
73+ // Create a GPU buffer for our vertices
7174 final verticesDeviceBuffer = gpu.gpuContext.createDeviceBufferWithCopy (
7275 ByteData .sublistView (vertices),
7376 );
7477 if (verticesDeviceBuffer == null ) {
7578 throw Exception ('Failed to create vertices device buffer' );
7679 }
7780
81+ // Bind the pipeline and vertex buffer
7882 renderPass.bindPipeline (pipeline);
7983
8084 final verticesView = gpu.BufferView (
@@ -87,8 +91,10 @@ class TrianglePainter extends CustomPainter {
8791 vertices.length ~ / floatsPerVertex,
8892 );
8993
94+ // Draw the triangle
9095 renderPass.draw ();
9196
97+ // Submit commands to GPU and render to screen
9298 commandBuffer.submit ();
9399 final image = texture.asImage ();
94100 canvas.drawImage (image, Offset .zero, Paint ());
0 commit comments