context.ts 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. /**
  2. * Copyright (c) 2018 mol* contributors, licensed under MIT, See LICENSE file for more info.
  3. *
  4. * @author Alexander Rose <alexander.rose@weirdbyte.de>
  5. */
  6. import { createProgramCache, ProgramCache } from './program'
  7. import { createShaderCache, ShaderCache } from './shader'
  8. function unbindResources (gl: WebGLRenderingContext) {
  9. // bind null to all texture units
  10. const maxTextureImageUnits = gl.getParameter(gl.MAX_TEXTURE_IMAGE_UNITS)
  11. for (let i = 0; i < maxTextureImageUnits; ++i) {
  12. gl.activeTexture(gl.TEXTURE0 + i)
  13. gl.bindTexture(gl.TEXTURE_2D, null)
  14. gl.bindTexture(gl.TEXTURE_CUBE_MAP, null)
  15. }
  16. // assign the smallest possible buffer to all attributes
  17. const buf = gl.createBuffer();
  18. gl.bindBuffer(gl.ARRAY_BUFFER, buf);
  19. const maxVertexAttribs = gl.getParameter(gl.MAX_VERTEX_ATTRIBS);
  20. for (let i = 0; i < maxVertexAttribs; ++i) {
  21. gl.vertexAttribPointer(i, 1, gl.FLOAT, false, 0, 0);
  22. }
  23. // bind null to all buffers
  24. gl.bindBuffer(gl.ARRAY_BUFFER, null)
  25. gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, null)
  26. gl.bindRenderbuffer(gl.RENDERBUFFER, null)
  27. gl.bindFramebuffer(gl.FRAMEBUFFER, null)
  28. }
  29. type Extensions = {
  30. angleInstancedArrays: ANGLE_instanced_arrays
  31. standardDerivatives: OES_standard_derivatives
  32. oesElementIndexUint: OES_element_index_uint | null
  33. oesVertexArrayObject: OES_vertex_array_object | null
  34. }
  35. export interface Context {
  36. gl: WebGLRenderingContext
  37. extensions: Extensions
  38. shaderCache: ShaderCache
  39. programCache: ProgramCache
  40. bufferCount: number
  41. textureCount: number
  42. vaoCount: number
  43. destroy: () => void
  44. }
  45. export function createContext(gl: WebGLRenderingContext): Context {
  46. const angleInstancedArrays = gl.getExtension('ANGLE_instanced_arrays')
  47. if (angleInstancedArrays === null) {
  48. throw new Error('Could not get "ANGLE_instanced_arrays" extension')
  49. }
  50. const standardDerivatives = gl.getExtension('OES_standard_derivatives')
  51. if (standardDerivatives === null) {
  52. throw new Error('Could not get "OES_standard_derivatives" extension')
  53. }
  54. const oesElementIndexUint = gl.getExtension('OES_element_index_uint')
  55. if (oesElementIndexUint === null) {
  56. console.warn('Could not get "OES_element_index_uint" extension')
  57. }
  58. const oesVertexArrayObject = gl.getExtension('OES_vertex_array_object')
  59. if (oesVertexArrayObject === null) {
  60. console.log('Could not get "OES_vertex_array_object" extension')
  61. }
  62. const shaderCache = createShaderCache()
  63. const programCache = createProgramCache()
  64. return {
  65. gl,
  66. extensions: { angleInstancedArrays, standardDerivatives, oesElementIndexUint, oesVertexArrayObject },
  67. shaderCache,
  68. programCache,
  69. bufferCount: 0,
  70. textureCount: 0,
  71. vaoCount: 0,
  72. destroy: () => {
  73. unbindResources(gl)
  74. programCache.dispose()
  75. shaderCache.dispose()
  76. // TODO destroy buffers and textures
  77. }
  78. }
  79. }