Select Git revision
texture.ts 10.46 KiB
/**
* Copyright (c) 2018-2019 mol* contributors, licensed under MIT, See LICENSE file for more info.
*
* @author Alexander Rose <alexander.rose@weirdbyte.de>
*/
import { WebGLContext } from './context'
import { TextureImage, TextureVolume } from '../renderable/util';
import { ValueCell } from 'mol-util';
import { RenderableSchema } from '../renderable/schema';
import { idFactory } from 'mol-util/id-factory';
import { Framebuffer } from './framebuffer';
import { isWebGL2 } from './compat';
import { ValueOf } from 'mol-util/type-helpers';
const getNextTextureId = idFactory()
export type TextureKindValue = {
'image-uint8': TextureImage<Uint8Array>
'image-float32': TextureImage<Float32Array>
'volume-uint8': TextureVolume<Uint8Array>
'volume-float32': TextureVolume<Float32Array>
'texture': Texture
}
export type TextureValueType = ValueOf<TextureKindValue>
export type TextureKind = keyof TextureKindValue
export type TextureType = 'ubyte' | 'float'
export type TextureFormat = 'alpha' | 'rgb' | 'rgba'
/** Numbers are shortcuts for color attachment */
export type TextureAttachment = 'depth' | 'stencil' | 'color0' | 'color1' | 'color2' | 'color3' | 'color4' | 'color5' | 'color6' | 'color7' | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7
export type TextureFilter = 'nearest' | 'linear'
export function getTarget(ctx: WebGLContext, kind: TextureKind): number {
const { gl } = ctx
switch (kind) {
case 'image-uint8': return gl.TEXTURE_2D
case 'image-float32': return gl.TEXTURE_2D
}
if (isWebGL2(gl)) {
switch (kind) {
case 'volume-uint8': return gl.TEXTURE_3D
case 'volume-float32': return gl.TEXTURE_3D
}
}
throw new Error(`unknown texture kind '${kind}'`)
}
export function getFormat(ctx: WebGLContext, format: TextureFormat): number {
const { gl } = ctx
switch (format) {
case 'alpha': return gl.ALPHA
case 'rgb': return gl.RGB
case 'rgba': return gl.RGBA
}
}
export function getInternalFormat(ctx: WebGLContext, format: TextureFormat, type: TextureType): number {
const { gl, isWebGL2 } = ctx
if (isWebGL2) {
switch (format) {
case 'alpha':
switch (type) {
case 'ubyte': return gl.ALPHA
case 'float': throw new Error('invalid format/type combination alpha/float')
}
case 'rgb':
switch (type) {
case 'ubyte': return gl.RGB
case 'float': return (gl as WebGL2RenderingContext).RGB32F
}