webgpuMaterialContext.js 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. /* eslint-disable babylonjs/available */
  2. /* eslint-disable jsdoc/require-jsdoc */
  3. import { ExternalTexture } from "../../Materials/Textures/externalTexture.js";
  4. import { WebGPUCacheSampler } from "./webgpuCacheSampler.js";
  5. /** @internal */
  6. export class WebGPUMaterialContext {
  7. get forceBindGroupCreation() {
  8. // If there is at least one external texture to bind, we must recreate the bind groups each time
  9. // because we need to retrieve a new texture each frame (by calling device.importExternalTexture)
  10. return this._numExternalTextures > 0;
  11. }
  12. get hasFloatOrDepthTextures() {
  13. return this._numFloatOrDepthTextures > 0;
  14. }
  15. constructor() {
  16. this.uniqueId = WebGPUMaterialContext._Counter++;
  17. this.updateId = 0;
  18. this.textureState = 0;
  19. this.reset();
  20. }
  21. reset() {
  22. this.samplers = {};
  23. this.textures = {};
  24. this.isDirty = true;
  25. this._numFloatOrDepthTextures = 0;
  26. this._numExternalTextures = 0;
  27. }
  28. setSampler(name, sampler) {
  29. let samplerCache = this.samplers[name];
  30. let currentHashCode = -1;
  31. if (!samplerCache) {
  32. this.samplers[name] = samplerCache = { sampler, hashCode: 0 };
  33. }
  34. else {
  35. currentHashCode = samplerCache.hashCode;
  36. }
  37. samplerCache.sampler = sampler;
  38. samplerCache.hashCode = sampler ? WebGPUCacheSampler.GetSamplerHashCode(sampler) : 0;
  39. const isDirty = currentHashCode !== samplerCache.hashCode;
  40. if (isDirty) {
  41. this.updateId++;
  42. }
  43. this.isDirty || (this.isDirty = isDirty);
  44. }
  45. setTexture(name, texture) {
  46. let textureCache = this.textures[name];
  47. let currentTextureId = -1;
  48. if (!textureCache) {
  49. this.textures[name] = textureCache = { texture, isFloatOrDepthTexture: false, isExternalTexture: false };
  50. }
  51. else {
  52. currentTextureId = textureCache.texture?.uniqueId ?? -1;
  53. }
  54. if (textureCache.isExternalTexture) {
  55. this._numExternalTextures--;
  56. }
  57. if (textureCache.isFloatOrDepthTexture) {
  58. this._numFloatOrDepthTextures--;
  59. }
  60. if (texture) {
  61. textureCache.isFloatOrDepthTexture =
  62. texture.type === 1 ||
  63. (texture.format >= 13 && texture.format <= 18);
  64. textureCache.isExternalTexture = ExternalTexture.IsExternalTexture(texture);
  65. if (textureCache.isFloatOrDepthTexture) {
  66. this._numFloatOrDepthTextures++;
  67. }
  68. if (textureCache.isExternalTexture) {
  69. this._numExternalTextures++;
  70. }
  71. }
  72. else {
  73. textureCache.isFloatOrDepthTexture = false;
  74. textureCache.isExternalTexture = false;
  75. }
  76. textureCache.texture = texture;
  77. const isDirty = currentTextureId !== (texture?.uniqueId ?? -1);
  78. if (isDirty) {
  79. this.updateId++;
  80. }
  81. this.isDirty || (this.isDirty = isDirty);
  82. }
  83. }
  84. WebGPUMaterialContext._Counter = 0;
  85. //# sourceMappingURL=webgpuMaterialContext.js.map