WebGPUUniformsGroup.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. import WebGPUBinding from './WebGPUBinding.js';
  2. class WebGPUUniformsGroup extends WebGPUBinding {
  3. constructor() {
  4. super();
  5. // the order of uniforms in this array must match the order of uniforms in the shader
  6. this.uniforms = [];
  7. this.onBeforeUpdate = function () {};
  8. this.type = 'uniform-buffer';
  9. this.visibility = GPUShaderStage.VERTEX;
  10. this.usage = GPUBufferUsage.UNIFORM | GPUBufferUsage.COPY_DST;
  11. this.array = null; // set by the renderer
  12. this.bufferGPU = null; // set by the renderer
  13. Object.defineProperty( this, 'isUniformsGroup', { value: true } );
  14. }
  15. addUniform( uniform ) {
  16. this.uniforms.push( uniform );
  17. return this;
  18. }
  19. removeUniform( uniform ) {
  20. const index = this.uniforms.indexOf( uniform );
  21. if ( index !== - 1 ) {
  22. this.uniforms.splice( index, 1 );
  23. }
  24. return this;
  25. }
  26. setOnBeforeUpdate( callback ) {
  27. this.onBeforeUpdate = callback;
  28. return this;
  29. }
  30. getByteLength() {
  31. let byteLength = 0;
  32. for ( const uniform of this.uniforms ) {
  33. byteLength += uniform.byteLength;
  34. }
  35. return byteLength;
  36. }
  37. update() {
  38. let updated = false;
  39. let offset = 0;
  40. for ( const uniform of this.uniforms ) {
  41. if ( this.updateByType( uniform, offset ) === true ) {
  42. updated = true;
  43. }
  44. offset += uniform.itemSize;
  45. }
  46. return updated;
  47. }
  48. updateByType( uniform, offset ) {
  49. if ( uniform.isFloatUniform ) return this.updateNumber( uniform, offset );
  50. if ( uniform.isVector2Uniform ) return this.updateVector2( uniform, offset );
  51. if ( uniform.isVector3Uniform ) return this.updateVector3( uniform, offset );
  52. if ( uniform.isVector4Uniform ) return this.updateVector4( uniform, offset );
  53. if ( uniform.isColorUniform ) return this.updateColor( uniform, offset );
  54. if ( uniform.isMatrix3Uniform ) return this.updateMatrix3( uniform, offset );
  55. if ( uniform.isMatrix4Uniform ) return this.updateMatrix4( uniform, offset );
  56. console.error( 'THREE.WebGPUUniformsGroup: Unsupported uniform type.', uniform );
  57. }
  58. updateNumber( uniform, offset ) {
  59. let updated = false;
  60. const a = this.array;
  61. const v = uniform.value;
  62. if ( a[ offset ] !== v ) {
  63. a[ offset ] = v;
  64. updated = true;
  65. }
  66. return updated;
  67. }
  68. updateVector2( uniform, offset ) {
  69. let updated = false;
  70. const a = this.array;
  71. const v = uniform.value;
  72. if ( a[ offset + 0 ] !== v.x || a[ offset + 1 ] !== v.y ) {
  73. a[ offset + 0 ] = v.x;
  74. a[ offset + 1 ] = v.y;
  75. updated = true;
  76. }
  77. return updated;
  78. }
  79. updateVector3( uniform, offset ) {
  80. let updated = false;
  81. const a = this.array;
  82. const v = uniform.value;
  83. if ( a[ offset + 0 ] !== v.x || a[ offset + 1 ] !== v.y || a[ offset + 2 ] !== v.z ) {
  84. a[ offset + 0 ] = v.x;
  85. a[ offset + 1 ] = v.y;
  86. a[ offset + 2 ] = v.z;
  87. updated = true;
  88. }
  89. return updated;
  90. }
  91. updateVector4( uniform, offset ) {
  92. let updated = false;
  93. const a = this.array;
  94. const v = uniform.value;
  95. if ( a[ offset + 0 ] !== v.x || a[ offset + 1 ] !== v.y || a[ offset + 2 ] !== v.z || a[ offset + 4 ] !== v.w ) {
  96. a[ offset + 0 ] = v.x;
  97. a[ offset + 1 ] = v.y;
  98. a[ offset + 2 ] = v.z;
  99. a[ offset + 3 ] = v.z;
  100. updated = true;
  101. }
  102. return updated;
  103. }
  104. updateColor( uniform, offset ) {
  105. let updated = false;
  106. const a = this.array;
  107. const c = uniform.value;
  108. if ( a[ offset + 0 ] !== c.r || a[ offset + 1 ] !== c.g || a[ offset + 2 ] !== c.b ) {
  109. a[ offset + 0 ] = c.r;
  110. a[ offset + 1 ] = c.g;
  111. a[ offset + 2 ] = c.b;
  112. updated = true;
  113. }
  114. return updated;
  115. }
  116. updateMatrix3( uniform, offset ) {
  117. let updated = false;
  118. const a = this.array;
  119. const e = uniform.value.elements;
  120. if ( a[ offset + 0 ] !== e[ 0 ] || a[ offset + 1 ] !== e[ 1 ] || a[ offset + 2 ] !== e[ 2 ] ||
  121. a[ offset + 4 ] !== e[ 3 ] || a[ offset + 5 ] !== e[ 4 ] || a[ offset + 6 ] !== e[ 5 ] ||
  122. a[ offset + 8 ] !== e[ 6 ] || a[ offset + 9 ] !== e[ 7 ] || a[ offset + 10 ] !== e[ 8 ] ) {
  123. a[ offset + 0 ] = e[ 0 ];
  124. a[ offset + 1 ] = e[ 1 ];
  125. a[ offset + 2 ] = e[ 2 ];
  126. a[ offset + 4 ] = e[ 3 ];
  127. a[ offset + 5 ] = e[ 4 ];
  128. a[ offset + 6 ] = e[ 5 ];
  129. a[ offset + 8 ] = e[ 6 ];
  130. a[ offset + 9 ] = e[ 7 ];
  131. a[ offset + 10 ] = e[ 8 ];
  132. updated = true;
  133. }
  134. return updated;
  135. }
  136. updateMatrix4( uniform, offset ) {
  137. let updated = false;
  138. const a = this.array;
  139. const e = uniform.value.elements;
  140. if ( arraysEqual( a, e, offset ) === false ) {
  141. a.set( e, offset );
  142. updated = true;
  143. }
  144. return updated;
  145. }
  146. }
  147. function arraysEqual( a, b, offset ) {
  148. for ( let i = 0, l = b.length; i < l; i ++ ) {
  149. if ( a[ offset + i ] !== b[ i ] ) return false;
  150. }
  151. return true;
  152. }
  153. export default WebGPUUniformsGroup;