Source: Palette/Shader.js

  1. /* global WebGLShader */
  2. /**
  3. * @classdesc Abstraction of shader references to allow easy manipulation.
  4. * @class Palette.Shader
  5. * @description Initialise and compile a valid Source Object into a Shader.
  6. * @param {WebGLRenderingContext} gl - The context the shader will belong to and be compiled by.
  7. * @param {string} name - The name the shader object will be referred to by.
  8. * @param {number} type - The class of the shader contained, either Palette.Shader.VS or Palette.Shader.FS.
  9. * @param {string} source - The source code to compile the shader from.
  10. * @param {WebGLRenderingContext} gl - The context the shaders of this program will belong to and be compiled by.
  11. * @param {object} [attrs] - The array which contains attribute names and default values, as an array of 3-tuples.
  12. * @author FelixMcFelix (Kyle S.)
  13. */
  14. Palette.Shader = function(gl, name, type, source, attrs){
  15. /**
  16. * The shader's name.
  17. * @name Palette.Shader#name
  18. * @type String
  19. * @protected
  20. * @readonly
  21. */
  22. this.name = name;
  23. /**
  24. * The type of shader, either Palette.Shader.VS or Palette.Shader.FS for objects.
  25. * @name Palette.Shader#type
  26. * @type int
  27. * @protected
  28. * @readonly
  29. */
  30. this.type = type;
  31. /**
  32. * The shader's attached context.
  33. * @name Palette.Shader#context
  34. * @type WebGLRenderingContext
  35. * @protected
  36. * @readonly
  37. */
  38. this.context = gl;
  39. /**
  40. * The shader's attribute array.
  41. * @name Palette.Shader#attrs
  42. * @type Array[]
  43. * @protected
  44. * @readonly
  45. */
  46. this.attrs = attrs;
  47. /**
  48. * The reference to the compiled shader in the WebGLRenderingContext.
  49. * @name Palette.Shader#shader
  50. * @type WebGLShader
  51. * @protected
  52. * @readonly
  53. */
  54. this.shader = null;
  55. /**
  56. * Has the shader attempted compilation yet?
  57. * @name Palette.Shader#compiled
  58. * @type Boolean
  59. * @private
  60. * @readonly
  61. */
  62. this.compiled = false;
  63. this.bakeShader(source);
  64. };
  65. Palette.Shader.prototype = {
  66. /**
  67. * Compile shader code from a source string. Once compiled, you cannot recompile.
  68. * @method Palette.Shader#bakeShader
  69. * @protected
  70. * @param {string} source - The source code to compile and attach to this shader object.
  71. * @return {boolean} True if successful, false if unsuccessful.
  72. */
  73. bakeShader: function(source){
  74. if (this.compiled){return null;}
  75. this.compiled = true;
  76. switch(this.type){
  77. case Palette.Shader.VS:
  78. this.shader = this.context.createShader(this.context.VERTEX_SHADER);
  79. break;
  80. case Palette.Shader.FS:
  81. this.shader = this.context.createShader(this.context.FRAGMENT_SHADER);
  82. break;
  83. default:
  84. return false;
  85. }
  86. this.context.shaderSource(this.shader, source);
  87. this.context.compileShader(this.shader);
  88. if (!this.context.getShaderParameter(this.shader, this.context.COMPILE_STATUS)) {
  89. alert("An error occurred compiling the shaders: " + this.context.getShaderInfoLog(this.shader));
  90. return false;
  91. }
  92. return true;
  93. }
  94. };
  95. Palette.Shader.VS = 0;
  96. Palette.Shader.FS = 1;
  97. Palette.Shader.LIST = 2;
  98. Palette.Shader.prototype.constructor = Palette.Shader;