WebGPUUniformsGroup.js 4.8 KB

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