WebGPUUniformsGroup.js 5.2 KB

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