Bindings.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. import DataMap from './DataMap.js';
  2. import { AttributeType } from './Constants.js';
  3. class Bindings extends DataMap {
  4. constructor( backend, nodes, textures, attributes, pipelines, info ) {
  5. super();
  6. this.backend = backend;
  7. this.textures = textures;
  8. this.pipelines = pipelines;
  9. this.attributes = attributes;
  10. this.nodes = nodes;
  11. this.info = info;
  12. this.pipelines.bindings = this; // assign bindings to pipelines
  13. }
  14. getForRender( renderObject ) {
  15. const bindings = renderObject.getBindings();
  16. for ( const bindGroup of bindings ) {
  17. const groupData = this.get( bindGroup );
  18. if ( groupData.bindGroup === undefined ) {
  19. // each object defines an array of bindings (ubos, textures, samplers etc.)
  20. this._init( bindGroup );
  21. this.backend.createBindings( bindGroup, bindings );
  22. groupData.bindGroup = bindGroup;
  23. }
  24. }
  25. return bindings;
  26. }
  27. getForCompute( computeNode ) {
  28. const bindings = this.nodes.getForCompute( computeNode ).bindings;
  29. for ( const bindGroup of bindings ) {
  30. const groupData = this.get( bindGroup );
  31. if ( groupData.bindGroup === undefined ) {
  32. this._init( bindGroup );
  33. this.backend.createBindings( bindGroup, bindings );
  34. groupData.bindGroup = bindGroup;
  35. }
  36. }
  37. return bindings;
  38. }
  39. updateForCompute( computeNode ) {
  40. this._updateBindings( computeNode, this.getForCompute( computeNode ) );
  41. }
  42. updateForRender( renderObject ) {
  43. this._updateBindings( renderObject, this.getForRender( renderObject ) );
  44. }
  45. _updateBindings( object, bindings ) {
  46. for ( const bindGroup of bindings ) {
  47. this._update( object, bindGroup, bindings );
  48. }
  49. }
  50. _init( bindGroup ) {
  51. for ( const binding of bindGroup.bindings ) {
  52. if ( binding.isSampledTexture ) {
  53. this.textures.updateTexture( binding.texture );
  54. } else if ( binding.isStorageBuffer ) {
  55. const attribute = binding.attribute;
  56. this.attributes.update( attribute, AttributeType.STORAGE );
  57. }
  58. }
  59. }
  60. _update( object, bindGroup, bindings ) {
  61. const { backend } = this;
  62. let needsBindingsUpdate = false;
  63. // iterate over all bindings and check if buffer updates or a new binding group is required
  64. for ( const binding of bindGroup.bindings ) {
  65. if ( binding.isNodeUniformsGroup ) {
  66. const updated = this.nodes.updateGroup( binding );
  67. if ( ! updated ) continue;
  68. }
  69. if ( binding.isUniformBuffer ) {
  70. const updated = binding.update();
  71. if ( updated ) {
  72. backend.updateBinding( binding );
  73. }
  74. } else if ( binding.isSampler ) {
  75. binding.update();
  76. } else if ( binding.isSampledTexture ) {
  77. const texture = binding.texture;
  78. if ( binding.needsBindingsUpdate ) needsBindingsUpdate = true;
  79. const updated = binding.update();
  80. if ( updated ) {
  81. this.textures.updateTexture( binding.texture );
  82. }
  83. const textureData = backend.get( binding.texture );
  84. if ( backend.isWebGPUBackend === true && textureData.texture === undefined && textureData.externalTexture === undefined ) {
  85. // TODO: Remove this once we found why updated === false isn't bound to a texture in the WebGPU backend
  86. console.error( 'Bindings._update: binding should be available:', binding, updated, binding.texture, binding.textureNode.value );
  87. this.textures.updateTexture( binding.texture );
  88. needsBindingsUpdate = true;
  89. }
  90. if ( texture.isStorageTexture === true ) {
  91. const textureData = this.get( texture );
  92. if ( binding.store === true ) {
  93. textureData.needsMipmap = true;
  94. } else if ( texture.generateMipmaps === true && this.textures.needsMipmaps( texture ) && textureData.needsMipmap === true ) {
  95. this.backend.generateMipmaps( texture );
  96. textureData.needsMipmap = false;
  97. }
  98. }
  99. }
  100. }
  101. if ( needsBindingsUpdate === true ) {
  102. const pipeline = this.pipelines.getForRender( object );
  103. this.backend.updateBindings( bindGroup, bindings, pipeline );
  104. }
  105. }
  106. }
  107. export default Bindings;