Source: dependencies/pixi-gl-core/GLBuffer.js

dependencies/pixi-gl-core/GLBuffer.js

  1. var EMPTY_ARRAY_BUFFER = new ArrayBuffer(0);
  2. /**
  3. * Helper class to create a webGL buffer
  4. *
  5. * @class
  6. * @memberof PIXI.glCore
  7. * @param gl {WebGLRenderingContext} The current WebGL rendering context
  8. * @param type {gl.ARRAY_BUFFER | gl.ELEMENT_ARRAY_BUFFER} @mat
  9. * @param data {ArrayBuffer| SharedArrayBuffer|ArrayBufferView} an array of data
  10. * @param drawType {gl.STATIC_DRAW|gl.DYNAMIC_DRAW|gl.STREAM_DRAW}
  11. */
  12. var Buffer = function(gl, type, data, drawType)
  13. {
  14. /**
  15. * The current WebGL rendering context
  16. *
  17. * @member {WebGLRenderingContext}
  18. */
  19. this.gl = gl;
  20. /**
  21. * The WebGL buffer, created upon instantiation
  22. *
  23. * @member {WebGLBuffer}
  24. */
  25. this.buffer = gl.createBuffer();
  26. /**
  27. * The type of the buffer
  28. *
  29. * @member {gl.ARRAY_BUFFER|gl.ELEMENT_ARRAY_BUFFER}
  30. */
  31. this.type = type || gl.ARRAY_BUFFER;
  32. /**
  33. * The draw type of the buffer
  34. *
  35. * @member {gl.STATIC_DRAW|gl.DYNAMIC_DRAW|gl.STREAM_DRAW}
  36. */
  37. this.drawType = drawType || gl.STATIC_DRAW;
  38. /**
  39. * The data in the buffer, as a typed array
  40. *
  41. * @member {ArrayBuffer| SharedArrayBuffer|ArrayBufferView}
  42. */
  43. this.data = EMPTY_ARRAY_BUFFER;
  44. if(data)
  45. {
  46. this.upload(data);
  47. }
  48. this._updateID = 0;
  49. };
  50. /**
  51. * Uploads the buffer to the GPU
  52. * @param data {ArrayBuffer| SharedArrayBuffer|ArrayBufferView} an array of data to upload
  53. * @param offset {Number} if only a subset of the data should be uploaded, this is the amount of data to subtract
  54. * @param dontBind {Boolean} whether to bind the buffer before uploading it
  55. */
  56. Buffer.prototype.upload = function(data, offset, dontBind)
  57. {
  58. // todo - needed?
  59. if(!dontBind) this.bind();
  60. var gl = this.gl;
  61. data = data || this.data;
  62. offset = offset || 0;
  63. if(this.data.byteLength >= data.byteLength)
  64. {
  65. gl.bufferSubData(this.type, offset, data);
  66. }
  67. else
  68. {
  69. gl.bufferData(this.type, data, this.drawType);
  70. }
  71. this.data = data;
  72. };
  73. /**
  74. * Binds the buffer
  75. *
  76. */
  77. Buffer.prototype.bind = function()
  78. {
  79. var gl = this.gl;
  80. gl.bindBuffer(this.type, this.buffer);
  81. };
  82. Buffer.createVertexBuffer = function(gl, data, drawType)
  83. {
  84. return new Buffer(gl, gl.ARRAY_BUFFER, data, drawType);
  85. };
  86. Buffer.createIndexBuffer = function(gl, data, drawType)
  87. {
  88. return new Buffer(gl, gl.ELEMENT_ARRAY_BUFFER, data, drawType);
  89. };
  90. Buffer.create = function(gl, type, data, drawType)
  91. {
  92. return new Buffer(gl, type, data, drawType);
  93. };
  94. /**
  95. * Destroys the buffer
  96. *
  97. */
  98. Buffer.prototype.destroy = function(){
  99. this.gl.deleteBuffer(this.buffer);
  100. };
  101. module.exports = Buffer;