WebGPUUniformsGroup.js 5.5 KB

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