@fjandin/react-shader 0.0.20 → 0.0.21

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1 +1 @@
1
- {"version":3,"file":"useWebGPU.d.ts","sourceRoot":"","sources":["../../src/hooks/useWebGPU.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,SAAS,EAAE,eAAe,EAAoB,SAAS,EAAE,MAAM,UAAU,CAAA;AAEvF,UAAU,gBAAgB;IACxB,QAAQ,EAAE,MAAM,CAAA;IAChB,QAAQ,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,eAAe,CAAC,CAAA;IAC1C,cAAc,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,SAAS,CAAC,CAAA;IAC1C,OAAO,CAAC,EAAE,CAAC,KAAK,EAAE,KAAK,KAAK,IAAI,CAAA;IAChC,SAAS,CAAC,EAAE,MAAM,CAAA;IAClB,OAAO,CAAC,EAAE,CAAC,IAAI,EAAE,SAAS,KAAK,IAAI,CAAA;IACnC,OAAO,CAAC,EAAE,CAAC,IAAI,EAAE,SAAS,KAAK,IAAI,CAAA;IACnC,WAAW,CAAC,EAAE,CAAC,IAAI,EAAE,SAAS,KAAK,IAAI,CAAA;IACvC,WAAW,CAAC,EAAE,CAAC,IAAI,EAAE,SAAS,KAAK,IAAI,CAAA;IACvC,SAAS,CAAC,EAAE,CAAC,IAAI,EAAE,SAAS,KAAK,IAAI,CAAA;IACrC,YAAY,CAAC,EAAE,CAAC,IAAI,EAAE,SAAS,EAAE,UAAU,EAAE,MAAM,KAAK,IAAI,CAAA;CAC7D;AAgYD,wBAAgB,SAAS,CAAC,OAAO,EAAE,gBAAgB;;;EA8SlD"}
1
+ {"version":3,"file":"useWebGPU.d.ts","sourceRoot":"","sources":["../../src/hooks/useWebGPU.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,SAAS,EAAE,eAAe,EAAoB,SAAS,EAAE,MAAM,UAAU,CAAA;AAEvF,UAAU,gBAAgB;IACxB,QAAQ,EAAE,MAAM,CAAA;IAChB,QAAQ,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,eAAe,CAAC,CAAA;IAC1C,cAAc,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,SAAS,CAAC,CAAA;IAC1C,OAAO,CAAC,EAAE,CAAC,KAAK,EAAE,KAAK,KAAK,IAAI,CAAA;IAChC,SAAS,CAAC,EAAE,MAAM,CAAA;IAClB,OAAO,CAAC,EAAE,CAAC,IAAI,EAAE,SAAS,KAAK,IAAI,CAAA;IACnC,OAAO,CAAC,EAAE,CAAC,IAAI,EAAE,SAAS,KAAK,IAAI,CAAA;IACnC,WAAW,CAAC,EAAE,CAAC,IAAI,EAAE,SAAS,KAAK,IAAI,CAAA;IACvC,WAAW,CAAC,EAAE,CAAC,IAAI,EAAE,SAAS,KAAK,IAAI,CAAA;IACvC,SAAS,CAAC,EAAE,CAAC,IAAI,EAAE,SAAS,KAAK,IAAI,CAAA;IACrC,YAAY,CAAC,EAAE,CAAC,IAAI,EAAE,SAAS,EAAE,UAAU,EAAE,MAAM,KAAK,IAAI,CAAA;CAC7D;AAkXD,wBAAgB,SAAS,CAAC,OAAO,EAAE,gBAAgB;;;EAiTlD"}
package/dist/index.cjs CHANGED
@@ -394,10 +394,11 @@ function createStorageBuffer(device, name, binding, data) {
394
394
  packingArray[off + 3] = data[i][3];
395
395
  }
396
396
  device.queue.writeBuffer(buffer, 0, packingArray);
397
- return { name, binding, buffer, currentLength: length, dataLength: data.length, packingArray };
397
+ return { name, binding, buffer, currentLength: length, packingArray };
398
398
  }
399
399
  function packAndUploadStorageBuffer(device, entry, data) {
400
400
  const arr = entry.packingArray;
401
+ arr.fill(0);
401
402
  for (let i = 0;i < data.length; i++) {
402
403
  const off = i * 4;
403
404
  arr[off] = data[i][0];
@@ -405,8 +406,7 @@ function packAndUploadStorageBuffer(device, entry, data) {
405
406
  arr[off + 2] = data[i][2];
406
407
  arr[off + 3] = data[i][3];
407
408
  }
408
- const uploadLength = Math.max(entry.dataLength, 1);
409
- device.queue.writeBuffer(entry.buffer, 0, arr, 0, uploadLength * 4);
409
+ device.queue.writeBuffer(entry.buffer, 0, arr);
410
410
  }
411
411
  function rebuildBindGroup(state) {
412
412
  const entries = [{ binding: 0, resource: { buffer: state.uniformBuffer } }];
@@ -523,18 +523,7 @@ async function initializeWebGPU(canvas, fragmentSource, customUniforms, storageB
523
523
  uniformBindGroup,
524
524
  uniformLayout,
525
525
  bindGroupLayout,
526
- storageBuffers,
527
- renderPassDescriptor: {
528
- colorAttachments: [
529
- {
530
- view: undefined,
531
- clearValue: { r: 0, g: 0, b: 0, a: 1 },
532
- loadOp: "clear",
533
- storeOp: "store"
534
- }
535
- ]
536
- },
537
- submitArray: [null]
526
+ storageBuffers
538
527
  };
539
528
  }
540
529
  function cleanupWebGPU(state) {
@@ -552,7 +541,6 @@ function useWebGPU(options) {
552
541
  const lastFrameTimeRef = import_react2.useRef(0);
553
542
  const mouseRef = import_react2.useRef([0, 0]);
554
543
  const mouseNormalizedRef = import_react2.useRef([0, 0]);
555
- const resolutionRef = import_react2.useRef([0, 0]);
556
544
  const mouseLeftDownRef = import_react2.useRef(false);
557
545
  const canvasRectRef = import_react2.useRef(null);
558
546
  const onErrorRef = import_react2.useRef(options.onError);
@@ -625,18 +613,16 @@ function useWebGPU(options) {
625
613
  const allValues = allValuesRef.current;
626
614
  allValues.iTime = elapsedTimeRef.current;
627
615
  allValues.iMouseLeftDown = mouseLeftDownRef.current ? 1 : 0;
628
- resolutionRef.current[0] = canvas.width;
629
- resolutionRef.current[1] = canvas.height;
630
- allValues.iResolution = resolutionRef.current;
616
+ allValues.iResolution = [canvas.width, canvas.height];
631
617
  allValues.iMouse = mouseRef.current;
632
618
  allValues.iMouseNormalized = mouseNormalizedRef.current;
633
- const customs = uniformsRef.current;
634
- if (customs) {
635
- for (const name in customs) {
636
- allValues[name] = customs[name];
619
+ if (uniformsRef.current) {
620
+ for (const [name, value] of Object.entries(uniformsRef.current)) {
621
+ allValues[name] = value;
637
622
  }
638
623
  }
639
624
  const uniformData = uniformDataRef.current;
625
+ uniformData.fill(0);
640
626
  for (const field of uniformLayout.fields) {
641
627
  const value = allValues[field.name];
642
628
  if (value === undefined) {
@@ -651,20 +637,18 @@ function useWebGPU(options) {
651
637
  if (!data)
652
638
  continue;
653
639
  const requiredLength = Math.max(data.length, 1);
654
- if (requiredLength > entry.currentLength || requiredLength < entry.currentLength / 2) {
655
- const allocLength = Math.max(Math.ceil(requiredLength * 1.5), 1);
640
+ if (requiredLength !== entry.currentLength) {
656
641
  entry.buffer.destroy();
657
- const byteSize = allocLength * 16;
642
+ const byteSize = requiredLength * 16;
658
643
  entry.buffer = device.createBuffer({
659
644
  label: `storage: ${entry.name}`,
660
645
  size: byteSize,
661
646
  usage: GPUBufferUsage.STORAGE | GPUBufferUsage.COPY_DST
662
647
  });
663
- entry.packingArray = new Float32Array(allocLength * 4);
664
- entry.currentLength = allocLength;
648
+ entry.packingArray = new Float32Array(requiredLength * 4);
649
+ entry.currentLength = requiredLength;
665
650
  needsBindGroupRebuild = true;
666
651
  }
667
- entry.dataLength = data.length;
668
652
  packAndUploadStorageBuffer(device, entry, data);
669
653
  }
670
654
  if (needsBindGroupRebuild) {
@@ -672,14 +656,21 @@ function useWebGPU(options) {
672
656
  }
673
657
  const commandEncoder = device.createCommandEncoder();
674
658
  const textureView = context.getCurrentTexture().createView();
675
- state.renderPassDescriptor.colorAttachments[0].view = textureView;
676
- const renderPass = commandEncoder.beginRenderPass(state.renderPassDescriptor);
659
+ const renderPass = commandEncoder.beginRenderPass({
660
+ colorAttachments: [
661
+ {
662
+ view: textureView,
663
+ clearValue: { r: 0, g: 0, b: 0, a: 1 },
664
+ loadOp: "clear",
665
+ storeOp: "store"
666
+ }
667
+ ]
668
+ });
677
669
  renderPass.setPipeline(pipeline);
678
670
  renderPass.setBindGroup(0, state.uniformBindGroup);
679
671
  renderPass.draw(3);
680
672
  renderPass.end();
681
- state.submitArray[0] = commandEncoder.finish();
682
- device.queue.submit(state.submitArray);
673
+ device.queue.submit([commandEncoder.finish()]);
683
674
  animationFrameRef.current = requestAnimationFrame(render);
684
675
  }, []);
685
676
  import_react2.useEffect(() => {
package/dist/index.js CHANGED
@@ -348,10 +348,11 @@ function createStorageBuffer(device, name, binding, data) {
348
348
  packingArray[off + 3] = data[i][3];
349
349
  }
350
350
  device.queue.writeBuffer(buffer, 0, packingArray);
351
- return { name, binding, buffer, currentLength: length, dataLength: data.length, packingArray };
351
+ return { name, binding, buffer, currentLength: length, packingArray };
352
352
  }
353
353
  function packAndUploadStorageBuffer(device, entry, data) {
354
354
  const arr = entry.packingArray;
355
+ arr.fill(0);
355
356
  for (let i = 0;i < data.length; i++) {
356
357
  const off = i * 4;
357
358
  arr[off] = data[i][0];
@@ -359,8 +360,7 @@ function packAndUploadStorageBuffer(device, entry, data) {
359
360
  arr[off + 2] = data[i][2];
360
361
  arr[off + 3] = data[i][3];
361
362
  }
362
- const uploadLength = Math.max(entry.dataLength, 1);
363
- device.queue.writeBuffer(entry.buffer, 0, arr, 0, uploadLength * 4);
363
+ device.queue.writeBuffer(entry.buffer, 0, arr);
364
364
  }
365
365
  function rebuildBindGroup(state) {
366
366
  const entries = [{ binding: 0, resource: { buffer: state.uniformBuffer } }];
@@ -477,18 +477,7 @@ async function initializeWebGPU(canvas, fragmentSource, customUniforms, storageB
477
477
  uniformBindGroup,
478
478
  uniformLayout,
479
479
  bindGroupLayout,
480
- storageBuffers,
481
- renderPassDescriptor: {
482
- colorAttachments: [
483
- {
484
- view: undefined,
485
- clearValue: { r: 0, g: 0, b: 0, a: 1 },
486
- loadOp: "clear",
487
- storeOp: "store"
488
- }
489
- ]
490
- },
491
- submitArray: [null]
480
+ storageBuffers
492
481
  };
493
482
  }
494
483
  function cleanupWebGPU(state) {
@@ -506,7 +495,6 @@ function useWebGPU(options) {
506
495
  const lastFrameTimeRef = useRef2(0);
507
496
  const mouseRef = useRef2([0, 0]);
508
497
  const mouseNormalizedRef = useRef2([0, 0]);
509
- const resolutionRef = useRef2([0, 0]);
510
498
  const mouseLeftDownRef = useRef2(false);
511
499
  const canvasRectRef = useRef2(null);
512
500
  const onErrorRef = useRef2(options.onError);
@@ -579,18 +567,16 @@ function useWebGPU(options) {
579
567
  const allValues = allValuesRef.current;
580
568
  allValues.iTime = elapsedTimeRef.current;
581
569
  allValues.iMouseLeftDown = mouseLeftDownRef.current ? 1 : 0;
582
- resolutionRef.current[0] = canvas.width;
583
- resolutionRef.current[1] = canvas.height;
584
- allValues.iResolution = resolutionRef.current;
570
+ allValues.iResolution = [canvas.width, canvas.height];
585
571
  allValues.iMouse = mouseRef.current;
586
572
  allValues.iMouseNormalized = mouseNormalizedRef.current;
587
- const customs = uniformsRef.current;
588
- if (customs) {
589
- for (const name in customs) {
590
- allValues[name] = customs[name];
573
+ if (uniformsRef.current) {
574
+ for (const [name, value] of Object.entries(uniformsRef.current)) {
575
+ allValues[name] = value;
591
576
  }
592
577
  }
593
578
  const uniformData = uniformDataRef.current;
579
+ uniformData.fill(0);
594
580
  for (const field of uniformLayout.fields) {
595
581
  const value = allValues[field.name];
596
582
  if (value === undefined) {
@@ -605,20 +591,18 @@ function useWebGPU(options) {
605
591
  if (!data)
606
592
  continue;
607
593
  const requiredLength = Math.max(data.length, 1);
608
- if (requiredLength > entry.currentLength || requiredLength < entry.currentLength / 2) {
609
- const allocLength = Math.max(Math.ceil(requiredLength * 1.5), 1);
594
+ if (requiredLength !== entry.currentLength) {
610
595
  entry.buffer.destroy();
611
- const byteSize = allocLength * 16;
596
+ const byteSize = requiredLength * 16;
612
597
  entry.buffer = device.createBuffer({
613
598
  label: `storage: ${entry.name}`,
614
599
  size: byteSize,
615
600
  usage: GPUBufferUsage.STORAGE | GPUBufferUsage.COPY_DST
616
601
  });
617
- entry.packingArray = new Float32Array(allocLength * 4);
618
- entry.currentLength = allocLength;
602
+ entry.packingArray = new Float32Array(requiredLength * 4);
603
+ entry.currentLength = requiredLength;
619
604
  needsBindGroupRebuild = true;
620
605
  }
621
- entry.dataLength = data.length;
622
606
  packAndUploadStorageBuffer(device, entry, data);
623
607
  }
624
608
  if (needsBindGroupRebuild) {
@@ -626,14 +610,21 @@ function useWebGPU(options) {
626
610
  }
627
611
  const commandEncoder = device.createCommandEncoder();
628
612
  const textureView = context.getCurrentTexture().createView();
629
- state.renderPassDescriptor.colorAttachments[0].view = textureView;
630
- const renderPass = commandEncoder.beginRenderPass(state.renderPassDescriptor);
613
+ const renderPass = commandEncoder.beginRenderPass({
614
+ colorAttachments: [
615
+ {
616
+ view: textureView,
617
+ clearValue: { r: 0, g: 0, b: 0, a: 1 },
618
+ loadOp: "clear",
619
+ storeOp: "store"
620
+ }
621
+ ]
622
+ });
631
623
  renderPass.setPipeline(pipeline);
632
624
  renderPass.setBindGroup(0, state.uniformBindGroup);
633
625
  renderPass.draw(3);
634
626
  renderPass.end();
635
- state.submitArray[0] = commandEncoder.finish();
636
- device.queue.submit(state.submitArray);
627
+ device.queue.submit([commandEncoder.finish()]);
637
628
  animationFrameRef.current = requestAnimationFrame(render);
638
629
  }, []);
639
630
  useEffect(() => {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@fjandin/react-shader",
3
- "version": "0.0.20",
3
+ "version": "0.0.21",
4
4
  "description": "React component for rendering WebGL shaders",
5
5
  "type": "module",
6
6
  "main": "./dist/index.cjs",