WebGPUUniformsGroup.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  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.type = GPUBindingType.UniformBuffer;
  10. this.visibility = GPUShaderStage.VERTEX;
  11. this.usage = GPUBufferUsage.UNIFORM | GPUBufferUsage.COPY_DST;
  12. this.array = null; // set by the renderer
  13. this.bufferGPU = null; // set by the renderer
  14. Object.defineProperty( this, 'isUniformsGroup', { value: true } );
  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 byteLength = 0;
  33. for ( const uniform of this.uniforms ) {
  34. byteLength += uniform.byteLength;
  35. }
  36. return byteLength;
  37. }
  38. update() {
  39. let updated = false;
  40. let offset = 0;
  41. for ( const uniform of this.uniforms ) {
  42. if ( this.updateByType( uniform, offset ) === true ) {
  43. updated = true;
  44. }
  45. offset += uniform.itemSize;
  46. }
  47. return updated;
  48. }
  49. updateByType( uniform, offset ) {
  50. if ( uniform.isFloatUniform ) return this.updateNumber( uniform, offset );
  51. if ( uniform.isVector2Uniform ) return this.updateVector2( uniform, offset );
  52. if ( uniform.isVector3Uniform ) return this.updateVector3( uniform, offset );
  53. if ( uniform.isVector4Uniform ) return this.updateVector4( uniform, offset );
  54. if ( uniform.isColorUniform ) return this.updateColor( uniform, offset );
  55. if ( uniform.isMatrix3Uniform ) return this.updateMatrix3( uniform, offset );
  56. if ( uniform.isMatrix4Uniform ) return this.updateMatrix4( uniform, offset );
  57. console.error( 'THREE.WebGPUUniformsGroup: Unsupported uniform type.', uniform );
  58. }
  59. updateNumber( uniform, offset ) {
  60. let updated = false;
  61. const a = this.array;
  62. const v = uniform.value;
  63. if ( a[ offset ] !== v ) {
  64. a[ offset ] = v;
  65. updated = true;
  66. }
  67. return updated;
  68. }
  69. updateVector2( uniform, offset ) {
  70. let updated = false;
  71. const a = this.array;
  72. const v = uniform.value;
  73. if ( a[ offset + 0 ] !== v.x || a[ offset + 1 ] !== v.y ) {
  74. a[ offset + 0 ] = v.x;
  75. a[ offset + 1 ] = v.y;
  76. updated = true;
  77. }
  78. return updated;
  79. }
  80. updateVector3( uniform, offset ) {
  81. let updated = false;
  82. const a = this.array;
  83. const v = uniform.value;
  84. if ( a[ offset + 0 ] !== v.x || a[ offset + 1 ] !== v.y || a[ offset + 2 ] !== v.z ) {
  85. a[ offset + 0 ] = v.x;
  86. a[ offset + 1 ] = v.y;
  87. a[ offset + 2 ] = v.z;
  88. updated = true;
  89. }
  90. return updated;
  91. }
  92. updateVector4( uniform, offset ) {
  93. let updated = false;
  94. const a = this.array;
  95. const v = uniform.value;
  96. if ( a[ offset + 0 ] !== v.x || a[ offset + 1 ] !== v.y || a[ offset + 2 ] !== v.z || a[ offset + 4 ] !== v.w ) {
  97. a[ offset + 0 ] = v.x;
  98. a[ offset + 1 ] = v.y;
  99. a[ offset + 2 ] = v.z;
  100. a[ offset + 3 ] = v.z;
  101. updated = true;
  102. }
  103. return updated;
  104. }
  105. updateColor( uniform, offset ) {
  106. let updated = false;
  107. const a = this.array;
  108. const c = uniform.value;
  109. if ( a[ offset + 0 ] !== c.r || a[ offset + 1 ] !== c.g || a[ offset + 2 ] !== c.b ) {
  110. a[ offset + 0 ] = c.r;
  111. a[ offset + 1 ] = c.g;
  112. a[ offset + 2 ] = c.b;
  113. updated = true;
  114. }
  115. return updated;
  116. }
  117. updateMatrix3( uniform, offset ) {
  118. let updated = false;
  119. const a = this.array;
  120. const e = uniform.value.elements;
  121. if ( a[ offset + 0 ] !== e[ 0 ] || a[ offset + 1 ] !== e[ 1 ] || a[ offset + 2 ] !== e[ 2 ] ||
  122. a[ offset + 4 ] !== e[ 3 ] || a[ offset + 5 ] !== e[ 4 ] || a[ offset + 6 ] !== e[ 5 ] ||
  123. a[ offset + 8 ] !== e[ 6 ] || a[ offset + 9 ] !== e[ 7 ] || a[ offset + 10 ] !== e[ 8 ] ) {
  124. a[ offset + 0 ] = e[ 0 ];
  125. a[ offset + 1 ] = e[ 1 ];
  126. a[ offset + 2 ] = e[ 2 ];
  127. a[ offset + 4 ] = e[ 3 ];
  128. a[ offset + 5 ] = e[ 4 ];
  129. a[ offset + 6 ] = e[ 5 ];
  130. a[ offset + 8 ] = e[ 6 ];
  131. a[ offset + 9 ] = e[ 7 ];
  132. a[ offset + 10 ] = e[ 8 ];
  133. updated = true;
  134. }
  135. return updated;
  136. }
  137. updateMatrix4( uniform, offset ) {
  138. let updated = false;
  139. const a = this.array;
  140. const e = uniform.value.elements;
  141. if ( arraysEqual( a, e, offset ) === false ) {
  142. a.set( e, offset );
  143. updated = true;
  144. }
  145. return updated;
  146. }
  147. }
  148. function arraysEqual( a, b, offset ) {
  149. for ( let i = 0, l = b.length; i < l; i ++ ) {
  150. if ( a[ offset + i ] !== b[ i ] ) return false;
  151. }
  152. return true;
  153. }
  154. export default WebGPUUniformsGroup;