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