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

dependencies/pixi-gl-core/GLShader.js

  1. var compileProgram = require('./shader/compileProgram'),
  2. extractAttributes = require('./shader/extractAttributes'),
  3. extractUniforms = require('./shader/extractUniforms'),
  4. setPrecision = require('./shader/setPrecision'),
  5. generateUniformAccessObject = require('./shader/generateUniformAccessObject');
  6. /**
  7. * Helper class to create a webGL Shader
  8. *
  9. * @class
  10. * @memberof PIXI.glCore
  11. * @param gl {WebGLRenderingContext}
  12. * @param vertexSrc {string|string[]} The vertex shader source as an array of strings.
  13. * @param fragmentSrc {string|string[]} The fragment shader source as an array of strings.
  14. * @param precision {string} The float precision of the shader. Options are 'lowp', 'mediump' or 'highp'.
  15. * @param attributeLocations {object} A key value pair showing which location eact attribute should sit eg {position:0, uvs:1}
  16. */
  17. var Shader = function(gl, vertexSrc, fragmentSrc, precision, attributeLocations)
  18. {
  19. /**
  20. * The current WebGL rendering context
  21. *
  22. * @member {WebGLRenderingContext}
  23. */
  24. this.gl = gl;
  25. if(precision)
  26. {
  27. vertexSrc = setPrecision(vertexSrc, precision);
  28. fragmentSrc = setPrecision(fragmentSrc, precision);
  29. }
  30. /**
  31. * The shader program
  32. *
  33. * @member {WebGLProgram}
  34. */
  35. // First compile the program..
  36. this.program = compileProgram(gl, vertexSrc, fragmentSrc, attributeLocations);
  37. /**
  38. * The attributes of the shader as an object containing the following properties
  39. * {
  40. * type,
  41. * size,
  42. * location,
  43. * pointer
  44. * }
  45. * @member {Object}
  46. */
  47. // next extract the attributes
  48. this.attributes = extractAttributes(gl, this.program);
  49. this.uniformData = extractUniforms(gl, this.program);
  50. /**
  51. * The uniforms of the shader as an object containing the following properties
  52. * {
  53. * gl,
  54. * data
  55. * }
  56. * @member {Object}
  57. */
  58. this.uniforms = generateUniformAccessObject( gl, this.uniformData );
  59. };
  60. /**
  61. * Uses this shader
  62. *
  63. * @return {PIXI.glCore.GLShader} Returns itself.
  64. */
  65. Shader.prototype.bind = function()
  66. {
  67. this.gl.useProgram(this.program);
  68. return this;
  69. };
  70. /**
  71. * Destroys this shader
  72. * TODO
  73. */
  74. Shader.prototype.destroy = function()
  75. {
  76. this.attributes = null;
  77. this.uniformData = null;
  78. this.uniforms = null;
  79. var gl = this.gl;
  80. gl.deleteProgram(this.program);
  81. };
  82. module.exports = Shader;