WGLVBuffer.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. /* *
  2. *
  3. * Copyright (c) 2019-2020 Highsoft AS
  4. *
  5. * Boost module: stripped-down renderer for higher performance
  6. *
  7. * License: highcharts.com/license
  8. *
  9. * !!!!!!! SOURCE GETS TRANSPILED BY TYPESCRIPT. EDIT TS FILE ONLY. !!!!!!!
  10. *
  11. * */
  12. 'use strict';
  13. /* eslint-disable valid-jsdoc */
  14. /**
  15. * Vertex Buffer abstraction.
  16. * A vertex buffer is a set of vertices which are passed to the GPU
  17. * in a single call.
  18. *
  19. * @private
  20. * @function GLVertexBuffer
  21. *
  22. * @param {WebGLContext} gl
  23. * the context in which to create the buffer
  24. *
  25. * @param {GLShader} shader
  26. * the shader to use
  27. *
  28. * @return {*}
  29. */
  30. function GLVertexBuffer(gl, shader, dataComponents
  31. /* , type */
  32. ) {
  33. var buffer = false, vertAttribute = false, components = dataComponents || 2, preAllocated = false, iterator = 0,
  34. // farray = false,
  35. data;
  36. // type = type || 'float';
  37. /**
  38. * @private
  39. */
  40. function destroy() {
  41. if (buffer) {
  42. gl.deleteBuffer(buffer);
  43. buffer = false;
  44. vertAttribute = false;
  45. }
  46. iterator = 0;
  47. components = dataComponents || 2;
  48. data = [];
  49. }
  50. /**
  51. * Build the buffer
  52. * @private
  53. * @param dataIn {Array<float>} - a 0 padded array of indices
  54. * @param attrib {String} - the name of the Attribute to bind the buffer to
  55. * @param dataComponents {Integer} - the number of components per. indice
  56. */
  57. function build(dataIn, attrib, dataComponents) {
  58. var farray;
  59. data = dataIn || [];
  60. if ((!data || data.length === 0) && !preAllocated) {
  61. // console.error('trying to render empty vbuffer');
  62. destroy();
  63. return false;
  64. }
  65. components = dataComponents || components;
  66. if (buffer) {
  67. gl.deleteBuffer(buffer);
  68. }
  69. if (!preAllocated) {
  70. farray = new Float32Array(data);
  71. }
  72. buffer = gl.createBuffer();
  73. gl.bindBuffer(gl.ARRAY_BUFFER, buffer);
  74. gl.bufferData(gl.ARRAY_BUFFER, preAllocated || farray, gl.STATIC_DRAW);
  75. // gl.bindAttribLocation(shader.program(), 0, 'aVertexPosition');
  76. vertAttribute = gl.getAttribLocation(shader.program(), attrib);
  77. gl.enableVertexAttribArray(vertAttribute);
  78. // Trigger cleanup
  79. farray = false;
  80. return true;
  81. }
  82. /**
  83. * Bind the buffer
  84. * @private
  85. */
  86. function bind() {
  87. if (!buffer) {
  88. return false;
  89. }
  90. // gl.bindAttribLocation(shader.program(), 0, 'aVertexPosition');
  91. // gl.enableVertexAttribArray(vertAttribute);
  92. // gl.bindBuffer(gl.ARRAY_BUFFER, buffer);
  93. gl.vertexAttribPointer(vertAttribute, components, gl.FLOAT, false, 0, 0);
  94. // gl.enableVertexAttribArray(vertAttribute);
  95. }
  96. /**
  97. * Render the buffer
  98. * @private
  99. * @param from {Integer} - the start indice
  100. * @param to {Integer} - the end indice
  101. * @param drawMode {String} - the draw mode
  102. */
  103. function render(from, to, drawMode) {
  104. var length = preAllocated ? preAllocated.length : data.length;
  105. if (!buffer) {
  106. return false;
  107. }
  108. if (!length) {
  109. return false;
  110. }
  111. if (!from || from > length || from < 0) {
  112. from = 0;
  113. }
  114. if (!to || to > length) {
  115. to = length;
  116. }
  117. drawMode = drawMode || 'points';
  118. gl.drawArrays(gl[drawMode.toUpperCase()], from / components, (to - from) / components);
  119. return true;
  120. }
  121. /**
  122. * @private
  123. */
  124. function push(x, y, a, b) {
  125. if (preAllocated) { // && iterator <= preAllocated.length - 4) {
  126. preAllocated[++iterator] = x;
  127. preAllocated[++iterator] = y;
  128. preAllocated[++iterator] = a;
  129. preAllocated[++iterator] = b;
  130. }
  131. }
  132. /**
  133. * Note about pre-allocated buffers:
  134. * - This is slower for charts with many series
  135. * @private
  136. */
  137. function allocate(size) {
  138. size *= 4;
  139. iterator = -1;
  140. preAllocated = new Float32Array(size);
  141. }
  142. // /////////////////////////////////////////////////////////////////////////
  143. return {
  144. destroy: destroy,
  145. bind: bind,
  146. data: data,
  147. build: build,
  148. render: render,
  149. allocate: allocate,
  150. push: push
  151. };
  152. }
  153. export default GLVertexBuffer;