Source: prepare/webgl/WebGLPrepare.js

prepare/webgl/WebGLPrepare.js

  1. import * as core from '../../core';
  2. import BasePrepare from '../BasePrepare';
  3. /**
  4. * The prepare manager provides functionality to upload content to the GPU.
  5. *
  6. * An instance of this class is automatically created by default, and can be found at renderer.plugins.prepare
  7. *
  8. * @class
  9. * @extends PIXI.prepare.BasePrepare
  10. * @memberof PIXI.prepare
  11. */
  12. export default class WebGLPrepare extends BasePrepare
  13. {
  14. /**
  15. * @param {PIXI.WebGLRenderer} renderer - A reference to the current renderer
  16. */
  17. constructor(renderer)
  18. {
  19. super(renderer);
  20. this.uploadHookHelper = this.renderer;
  21. // Add textures and graphics to upload
  22. this.registerFindHook(findGraphics);
  23. this.registerUploadHook(uploadBaseTextures);
  24. this.registerUploadHook(uploadGraphics);
  25. }
  26. }
  27. /**
  28. * Built-in hook to upload PIXI.Texture objects to the GPU.
  29. *
  30. * @private
  31. * @param {PIXI.WebGLRenderer} renderer - instance of the webgl renderer
  32. * @param {PIXI.DisplayObject} item - Item to check
  33. * @return {boolean} If item was uploaded.
  34. */
  35. function uploadBaseTextures(renderer, item)
  36. {
  37. if (item instanceof core.BaseTexture)
  38. {
  39. // if the texture already has a GL texture, then the texture has been prepared or rendered
  40. // before now. If the texture changed, then the changer should be calling texture.update() which
  41. // reuploads the texture without need for preparing it again
  42. if (!item._glTextures[renderer.CONTEXT_UID])
  43. {
  44. renderer.textureManager.updateTexture(item);
  45. }
  46. return true;
  47. }
  48. return false;
  49. }
  50. /**
  51. * Built-in hook to upload PIXI.Graphics to the GPU.
  52. *
  53. * @private
  54. * @param {PIXI.WebGLRenderer} renderer - instance of the webgl renderer
  55. * @param {PIXI.DisplayObject} item - Item to check
  56. * @return {boolean} If item was uploaded.
  57. */
  58. function uploadGraphics(renderer, item)
  59. {
  60. if (item instanceof core.Graphics)
  61. {
  62. // if the item is not dirty and already has webgl data, then it got prepared or rendered
  63. // before now and we shouldn't waste time updating it again
  64. if (item.dirty || item.clearDirty || !item._webGL[renderer.plugins.graphics.CONTEXT_UID])
  65. {
  66. renderer.plugins.graphics.updateGraphics(item);
  67. }
  68. return true;
  69. }
  70. return false;
  71. }
  72. /**
  73. * Built-in hook to find graphics.
  74. *
  75. * @private
  76. * @param {PIXI.DisplayObject} item - Display object to check
  77. * @param {Array<*>} queue - Collection of items to upload
  78. * @return {boolean} if a PIXI.Graphics object was found.
  79. */
  80. function findGraphics(item, queue)
  81. {
  82. if (item instanceof core.Graphics)
  83. {
  84. queue.push(item);
  85. return true;
  86. }
  87. return false;
  88. }
  89. core.WebGLRenderer.registerPlugin('prepare', WebGLPrepare);