WebGPUUniformsGroup.js 4.9 KB

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