2
0

UniformsGroup.js 5.3 KB

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