saker200

Untitled

Sep 7th, 2022
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  const adapter = await navigator.gpu.requestAdapter();
  2.  
  3.     if(!adapter){
  4.       throw Error("Couldn't request WebGPU Adapter")
  5.     }
  6.     const device = await adapter?.requestDevice();
  7.  
  8.     if(!device){
  9.       throw Error("Couldn't request WebGPU logical device")
  10.     }
  11.  
  12.     return new Promise(resolve => {
  13.       const sizeArray = new Int32Array([width, height]);
  14.       const gpuWidthHeightBuffer = device?.createBuffer({
  15.         mappedAtCreation: true,
  16.         size: sizeArray.byteLength,
  17.         usage: GPUBufferUsage.STORAGE,
  18.       });
  19.  
  20.       new Int32Array((gpuWidthHeightBuffer as GPUBuffer).getMappedRange()).set(
  21.         sizeArray
  22.       );
  23.       gpuWidthHeightBuffer?.unmap();
  24.  
  25.       const gpuInputBuffer = device?.createBuffer({
  26.         mappedAtCreation: true,
  27.         size: array.byteLength,
  28.         usage: GPUBufferUsage.STORAGE,
  29.       });
  30.  
  31.       // "possibly null" -> ? setzen
  32.       // "possibly undefined" -> as ...
  33.       new Uint8Array((gpuInputBuffer as GPUBuffer).getMappedRange()).set(array);
  34.       gpuInputBuffer?.unmap();
  35.  
  36.       const gpuResultBuffer = device?.createBuffer({
  37.         size: array.byteLength,
  38.         usage: GPUBufferUsage.STORAGE | GPUBufferUsage.COPY_SRC
  39.       });
  40.  
  41.       const gpuReadBuffer = device?.createBuffer({
  42.         size: array.byteLength,
  43.         usage: GPUBufferUsage.COPY_DST | GPUBufferUsage.MAP_READ
  44.       });
  45.  
  46.       const bindGroupLayout = device?.createBindGroupLayout({
  47.         entries: [
  48.           {
  49.             binding: 0,
  50.             visibility: GPUShaderStage.COMPUTE,
  51.             buffer: {
  52.               type: "read-only-storage"
  53.             }
  54.           } as GPUBindGroupLayoutEntry,
  55.           {
  56.             binding: 1,
  57.             visibility: GPUShaderStage.COMPUTE,
  58.             buffer: {
  59.               type: "read-only-storage"
  60.             }
  61.           } as GPUBindGroupLayoutEntry,
  62.           {
  63.             binding: 2,
  64.             visibility: GPUShaderStage.COMPUTE,
  65.             buffer: {
  66.               type: "storage"
  67.             },
  68.           } as GPUBindGroupLayoutEntry,
  69.         ]
  70.       });
  71.      
  72.  
  73.       const bindGroup = device?.createBindGroup({
  74.         layout: bindGroupLayout as GPUBindGroupLayout,
  75.         entries: [
  76.           {
  77.             binding: 0,
  78.             resource: {
  79.               buffer: gpuWidthHeightBuffer as GPUBuffer
  80.             }
  81.           },
  82.           {
  83.             binding: 1,
  84.             resource: {
  85.               buffer: gpuInputBuffer as GPUBuffer
  86.             }
  87.           },
  88.           {
  89.             binding: 2,
  90.             resource: {
  91.               buffer: gpuResultBuffer as GPUBuffer
  92.             }
  93.           }
  94.         ]
  95.       });
  96.  
  97.       const module = device?.createShaderModule({code: shader })
  98.  
  99.       const computePipeline = device?.createComputePipeline({
  100.         layout: device.createPipelineLayout({
  101.           bindGroupLayouts: [bindGroupLayout] as Iterable<GPUBindGroupLayout>
  102.         }),
  103.         compute: {
  104.           module,
  105.           entryPoint: "main"
  106.         }
  107.       });
  108.  
  109.       const commandEncoder = device?.createCommandEncoder();
  110.       const passEncoder = commandEncoder?.beginComputePass();
  111.       passEncoder?.setPipeline(computePipeline as GPUComputePipeline);
  112.       passEncoder?.setBindGroup(0, bindGroup as GPUBindGroup);
  113.       passEncoder?.dispatchWorkgroups(width, height);
  114.       passEncoder?.end();
  115.  
  116.       commandEncoder?.copyBufferToBuffer(
  117.         gpuResultBuffer as GPUBuffer,
  118.         0,
  119.         gpuReadBuffer as GPUBuffer,
  120.         0,
  121.         array.byteLength
  122.       );
  123.       device?.queue.submit([commandEncoder?.finish() as GPUCommandBuffer]);
  124.  
  125.       gpuReadBuffer?.mapAsync(GPUMapMode.READ).then(() => {
  126.         resolve(new Uint8Array(gpuReadBuffer.getMappedRange()));
  127.       });
  128.     });
  129.   }
Advertisement
Add Comment
Please, Sign In to add comment