我试图在GPU上进行一些计算。在我们的一位客户获得更多积分之前,事情看起来很顺利。
我设法缩小我的问题。下面是我写的最简单的代码示例,以突出我所面临的困难:
import { StorageBufferAttribute, WebGPURenderer } from 'three/webgpu'; // Version 0.177.0 import { Fn, If, Return, atomicAdd, atomicStore, instanceIndex, storage, } from 'three/tsl'; (async () => { const renderer = new WebGPURenderer({}); const count = storage(new StorageBufferAttribute(1, 1), 'uint', 1).setPBO(true).toAtomic(); for (const total of [10_000_000, 100_000_000, 1_000_000_000]) { // Reset count await renderer.computeAsync( Fn(() => { atomicStore(count.element(0), 0); })().compute(1), ); await renderer.computeAsync( Fn(() => { If(instanceIndex.greaterThanEqual(total), () => { Return(); }); atomicAdd(count.element(0), 1); })().compute(total), ); const countBuffer = new Int32Array( await renderer.getArrayBufferAsync( count.value, ), ); if (total !== countBuffer[0]) { console.log(`Count mismatch: expected ${total}, got ${countBuffer[0]}`); } } })();
此代码示例使用以下内容记录两次:
Count mismatch: expected 100000000, got 99999996 Count mismatch: expected 1000000000, got 999999968
我希望GPU上的计算返回预期值。任何建议仍然并行运行该代码并获得正确的值?
提前感谢,