|
8 | 8 | <div style="padding: 1rem;"> |
9 | 9 | <p>This demo is based off the tutorial linked below.</p> |
10 | 10 | <a href="https://webgpufundamentals.org/webgpu/lessons/webgpu-fundamentals.html#run-computations-on-the-gpu">Run computations on the GPU</a> |
| 11 | + <p> |
| 12 | + This basic example demonstrates a very basiccompute shader that simple multiplies the input data by 2 and returns the result. |
| 13 | + </p> |
11 | 14 | </div> |
12 | 15 | <div style="padding: 1rem;"> |
13 | | - <button class="btn btn-primary" disabled="@_running" @onclick="Run">Click me</button> |
| 16 | + <button class="btn btn-primary" disabled="@_running" @onclick="Run">Run</button> |
14 | 17 | </div> |
15 | 18 | <div style="padding: 1rem;"> |
16 | 19 | <pre> |
|
77 | 80 | } |
78 | 81 | }); |
79 | 82 |
|
80 | | - using var input = new Float32Array(new float[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }); |
81 | | - var inputByteLength = input.ByteLength; |
| 83 | + // input data |
| 84 | + var input = new float[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; |
| 85 | + Log($@"Input: {string.Join(", ", input)}"); |
| 86 | + |
| 87 | + |
| 88 | + using var inputFloatArray = new Float32Array(input); |
| 89 | + var inputByteLength = inputFloatArray.ByteLength; |
82 | 90 |
|
83 | 91 | // create a buffer on the GPU to hold our computation |
84 | 92 | // input and output |
|
88 | 96 | Size = (ulong)inputByteLength, |
89 | 97 | Usage = GPUBufferUsage.Storage | GPUBufferUsage.CopySrc | GPUBufferUsage.CopyDst, |
90 | 98 | }); |
| 99 | + |
91 | 100 | // Copy our input data to that buffer |
92 | | - device.Queue.WriteBuffer(workBuffer, 0, input); |
| 101 | + device.Queue.WriteBuffer(workBuffer, 0, inputFloatArray); |
93 | 102 |
|
94 | 103 | using var resultBuffer = device.CreateBuffer(new GPUBufferDescriptor |
95 | 104 | { |
|
135 | 144 |
|
136 | 145 | // Read the results |
137 | 146 | await resultBuffer.MapAsync(GPUMapMode.Read); |
138 | | - using var result = new Float32Array(resultBuffer.GetMappedRange()); |
139 | | - |
140 | | - float[] resultF = result.ToArray(); |
| 147 | + using var outputFloatArray = new Float32Array(resultBuffer.GetMappedRange()); |
141 | 148 |
|
142 | | - Log($@"Input: {string.Join(", ", input.ToArray())}"); |
143 | | - Log($@"Result: {string.Join(", ", resultF)}"); |
| 149 | + float[] output = outputFloatArray.ToArray(); |
| 150 | + Log($@"Result: {string.Join(", ", output)}"); |
144 | 151 |
|
145 | 152 | resultBuffer.Unmap(); |
146 | 153 | } |
147 | 154 | finally |
148 | 155 | { |
149 | 156 | _running = false; |
| 157 | + StateHasChanged(); |
150 | 158 | } |
151 | 159 | } |
152 | 160 | } |
0 commit comments