webgpuDrawContext.js 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. import * as WebGPUConstants from "./webgpuConstants.js";
  2. /** @internal */
  3. export class WebGPUDrawContext {
  4. isDirty(materialContextUpdateId) {
  5. return this._isDirty || this._materialContextUpdateId !== materialContextUpdateId;
  6. }
  7. resetIsDirty(materialContextUpdateId) {
  8. this._isDirty = false;
  9. this._materialContextUpdateId = materialContextUpdateId;
  10. }
  11. get useInstancing() {
  12. return this._useInstancing;
  13. }
  14. set useInstancing(use) {
  15. if (this._useInstancing === use) {
  16. return;
  17. }
  18. if (!use) {
  19. if (this.indirectDrawBuffer) {
  20. this._bufferManager.releaseBuffer(this.indirectDrawBuffer);
  21. }
  22. this.indirectDrawBuffer = undefined;
  23. this._indirectDrawData = undefined;
  24. }
  25. else {
  26. this.indirectDrawBuffer = this._bufferManager.createRawBuffer(20, WebGPUConstants.BufferUsage.CopyDst | WebGPUConstants.BufferUsage.Indirect | WebGPUConstants.BufferUsage.Storage, undefined, "IndirectDrawBuffer");
  27. this._indirectDrawData = new Uint32Array(5);
  28. this._indirectDrawData[3] = 0;
  29. this._indirectDrawData[4] = 0;
  30. }
  31. this._useInstancing = use;
  32. this._currentInstanceCount = -1;
  33. }
  34. constructor(bufferManager) {
  35. this._bufferManager = bufferManager;
  36. this.uniqueId = WebGPUDrawContext._Counter++;
  37. this._useInstancing = false;
  38. this._currentInstanceCount = 0;
  39. this.reset();
  40. }
  41. reset() {
  42. this.buffers = {};
  43. this._isDirty = true;
  44. this._materialContextUpdateId = 0;
  45. this.fastBundle = undefined;
  46. this.bindGroups = undefined;
  47. }
  48. setBuffer(name, buffer) {
  49. this._isDirty || (this._isDirty = buffer?.uniqueId !== this.buffers[name]?.uniqueId);
  50. this.buffers[name] = buffer;
  51. }
  52. setIndirectData(indexOrVertexCount, instanceCount, firstIndexOrVertex) {
  53. if (instanceCount === this._currentInstanceCount || !this.indirectDrawBuffer || !this._indirectDrawData) {
  54. // The current buffer is already up to date so do nothing
  55. // Note that we only check for instanceCount and not indexOrVertexCount nor firstIndexOrVertex because those values
  56. // are supposed to not change during the lifetime of a draw context
  57. return;
  58. }
  59. this._currentInstanceCount = instanceCount;
  60. this._indirectDrawData[0] = indexOrVertexCount;
  61. this._indirectDrawData[1] = instanceCount;
  62. this._indirectDrawData[2] = firstIndexOrVertex;
  63. this._bufferManager.setRawData(this.indirectDrawBuffer, 0, this._indirectDrawData, 0, 20);
  64. }
  65. dispose() {
  66. if (this.indirectDrawBuffer) {
  67. this._bufferManager.releaseBuffer(this.indirectDrawBuffer);
  68. this.indirectDrawBuffer = undefined;
  69. this._indirectDrawData = undefined;
  70. }
  71. this.fastBundle = undefined;
  72. this.bindGroups = undefined;
  73. this.buffers = undefined;
  74. }
  75. }
  76. WebGPUDrawContext._Counter = 0;
  77. //# sourceMappingURL=webgpuDrawContext.js.map