UniformsGroup.js 6.1 KB

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