texture.ts 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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 { Context } from './context'
  7. import { TextureImage } from '../renderable/util';
  8. import { ValueCell } from 'mol-util';
  9. import { RenderableSchema } from '../renderable/schema';
  10. import { idFactory } from 'mol-util/id-factory';
  11. import { Framebuffer } from './framebuffer';
  12. const getNextTextureId = idFactory()
  13. export type TextureFormat = 'alpha' | 'rgb' | 'rgba'
  14. export type TextureType = 'ubyte' | 'uint'
  15. export type TextureAttachment = 'depth' | 'stencil' | 'color0'
  16. export type TextureFilter = 'nearest' | 'linear'
  17. export function getFormat(ctx: Context, format: TextureFormat) {
  18. const { gl } = ctx
  19. switch (format) {
  20. case 'alpha': return gl.ALPHA
  21. case 'rgb': return gl.RGB
  22. case 'rgba': return gl.RGBA
  23. }
  24. }
  25. export function getType(ctx: Context, type: TextureType) {
  26. const { gl } = ctx
  27. switch (type) {
  28. case 'ubyte': return gl.UNSIGNED_BYTE
  29. case 'uint': return gl.UNSIGNED_INT
  30. }
  31. }
  32. export function getFilter(ctx: Context, type: TextureFilter) {
  33. const { gl } = ctx
  34. switch (type) {
  35. case 'nearest': return gl.NEAREST
  36. case 'linear': return gl.LINEAR
  37. }
  38. }
  39. export function getAttachment(ctx: Context, attachment: TextureAttachment) {
  40. const { gl } = ctx
  41. switch (attachment) {
  42. case 'depth': return gl.DEPTH_ATTACHMENT
  43. case 'stencil': return gl.STENCIL_ATTACHMENT
  44. case 'color0': return gl.COLOR_ATTACHMENT0
  45. }
  46. }
  47. export interface Texture {
  48. readonly id: number
  49. readonly format: number
  50. readonly type: number
  51. readonly width: number
  52. readonly height: number
  53. load: (image: TextureImage) => void
  54. bind: (id: TextureId) => void
  55. unbind: (id: TextureId) => void
  56. attachFramebuffer: (framebuffer: Framebuffer, attachment: TextureAttachment) => void
  57. destroy: () => void
  58. }
  59. export type TextureId = 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15
  60. export type TextureValues = { [k: string]: ValueCell<TextureImage> }
  61. export type Textures = { [k: string]: Texture }
  62. export function createTexture(ctx: Context, _format: TextureFormat, _type: TextureType, _filter: TextureFilter): Texture {
  63. const id = getNextTextureId()
  64. const { gl } = ctx
  65. const texture = gl.createTexture()
  66. if (texture === null) {
  67. throw new Error('Could not create WebGL texture')
  68. }
  69. const filter = getFilter(ctx, _filter)
  70. const format = getFormat(ctx, _format)
  71. const type = getType(ctx, _type)
  72. let _width = 0
  73. let _height = 0
  74. let destroyed = false
  75. ctx.textureCount += 1
  76. return {
  77. id,
  78. format,
  79. type,
  80. get width () { return _width },
  81. get height () { return _height },
  82. load: (image: TextureImage) => {
  83. const { array, width, height } = image
  84. gl.bindTexture(gl.TEXTURE_2D, texture)
  85. // unpack alignment of 1 since we use textures only for data
  86. gl.pixelStorei(gl.UNPACK_ALIGNMENT, 1);
  87. // gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, true);
  88. gl.texImage2D(gl.TEXTURE_2D, 0, format, width, height, 0, format, type, array)
  89. _width = width
  90. _height = height
  91. gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, filter)
  92. gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, filter)
  93. // clamp-to-edge needed for non-power-of-two textures
  94. gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
  95. gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);
  96. gl.bindTexture(gl.TEXTURE_2D, null)
  97. },
  98. bind: (id: TextureId) => {
  99. gl.activeTexture(gl.TEXTURE0 + id)
  100. gl.bindTexture(gl.TEXTURE_2D, texture)
  101. },
  102. unbind: (id: TextureId) => {
  103. gl.activeTexture(gl.TEXTURE0 + id)
  104. gl.bindTexture(gl.TEXTURE_2D, null)
  105. },
  106. attachFramebuffer: (framebuffer: Framebuffer, attachment: TextureAttachment) => {
  107. framebuffer.bind()
  108. gl.framebufferTexture2D(gl.FRAMEBUFFER, getAttachment(ctx, attachment), gl.TEXTURE_2D, texture, 0)
  109. },
  110. destroy: () => {
  111. if (destroyed) return
  112. gl.deleteTexture(texture)
  113. destroyed = true
  114. ctx.textureCount -= 1
  115. }
  116. }
  117. }
  118. export function createTextures(ctx: Context, schema: RenderableSchema, values: TextureValues) {
  119. const textures: Textures = {}
  120. Object.keys(schema).forEach((k, i) => {
  121. const spec = schema[k]
  122. if (spec.type === 'texture') {
  123. const texture = createTexture(ctx, spec.format, spec.dataType, spec.filter)
  124. texture.load(values[k].ref.value)
  125. textures[k] = texture
  126. }
  127. })
  128. return textures
  129. }