From 3bd7299a58d3116f57e6f979cd517f7733bf6664 Mon Sep 17 00:00:00 2001 From: Alexander Rose <alexander.rose@weirdbyte.de> Date: Sun, 3 Feb 2019 10:53:05 -0800 Subject: [PATCH] cleaned up buffers --- src/mol-gl/renderable/schema.ts | 6 +++--- src/mol-gl/webgl/buffer.ts | 33 ++++++++++++++++----------------- 2 files changed, 19 insertions(+), 20 deletions(-) diff --git a/src/mol-gl/renderable/schema.ts b/src/mol-gl/renderable/schema.ts index 51bfcd7c5..09ebcb9f5 100644 --- a/src/mol-gl/renderable/schema.ts +++ b/src/mol-gl/renderable/schema.ts @@ -5,7 +5,7 @@ */ import { ValueCell } from 'mol-util'; -import { ArrayKind, BufferItemSize, ElementsKind, AttributeValues } from '../webgl/buffer'; +import { ArrayKind, AttributeItemSize, ElementsKind, AttributeValues } from '../webgl/buffer'; import { UniformKind, UniformValues } from '../webgl/uniform'; import { DefineKind, DefineValues } from '../shader-code'; import { Vec2, Vec3, Vec4, Mat3, Mat4 } from 'mol-math/linear-algebra'; @@ -102,8 +102,8 @@ export function getValueVersions<T extends RenderableValues>(values: T) { // -export type AttributeSpec<K extends ArrayKind> = { type: 'attribute', kind: K, itemSize: BufferItemSize, divisor: number } -export function AttributeSpec<K extends ArrayKind>(kind: K, itemSize: BufferItemSize, divisor: number): AttributeSpec<K> { +export type AttributeSpec<K extends ArrayKind> = { type: 'attribute', kind: K, itemSize: AttributeItemSize, divisor: number } +export function AttributeSpec<K extends ArrayKind>(kind: K, itemSize: AttributeItemSize, divisor: number): AttributeSpec<K> { return { type: 'attribute', kind, itemSize, divisor } } diff --git a/src/mol-gl/webgl/buffer.ts b/src/mol-gl/webgl/buffer.ts index 80cd13401..18aebb998 100644 --- a/src/mol-gl/webgl/buffer.ts +++ b/src/mol-gl/webgl/buffer.ts @@ -1,5 +1,5 @@ /** - * Copyright (c) 2018 mol* contributors, licensed under MIT, See LICENSE file for more info. + * Copyright (c) 2018-2019 mol* contributors, licensed under MIT, See LICENSE file for more info. * * @author Alexander Rose <alexander.rose@weirdbyte.de> */ @@ -14,7 +14,7 @@ const getNextBufferId = idFactory() export type UsageHint = 'static' | 'dynamic' | 'stream' export type DataType = 'uint8' | 'int8' | 'uint16' | 'int16' | 'uint32' | 'int32' | 'float32' -export type BufferType = 'attribute' | 'elements' +export type BufferType = 'attribute' | 'elements' | 'uniform' export type DataTypeArrayType = { 'uint8': Uint8Array @@ -28,8 +28,6 @@ export type DataTypeArrayType = { export type ArrayType = ValueOf<DataTypeArrayType> export type ArrayKind = keyof DataTypeArrayType -export type BufferItemSize = 1 | 2 | 3 | 4 | 16 - export function getUsageHint(ctx: WebGLContext, usageHint: UsageHint) { const { gl } = ctx switch (usageHint) { @@ -90,8 +88,6 @@ export interface Buffer { readonly _dataType: number readonly _bpe: number - readonly itemSize: number - readonly itemCount: number readonly length: number updateData: (array: ArrayType) => void @@ -99,7 +95,7 @@ export interface Buffer { destroy: () => void } -export function createBuffer(ctx: WebGLContext, array: ArrayType, itemSize: BufferItemSize, usageHint: UsageHint, bufferType: BufferType): Buffer { +export function createBuffer(ctx: WebGLContext, array: ArrayType, usageHint: UsageHint, bufferType: BufferType): Buffer { const { gl } = ctx const _buffer = gl.createBuffer() if (_buffer === null) { @@ -111,11 +107,10 @@ export function createBuffer(ctx: WebGLContext, array: ArrayType, itemSize: Buff const _dataType = dataTypeFromArray(ctx, array) const _bpe = array.BYTES_PER_ELEMENT const _length = array.length - const _itemCount = Math.floor(_length / itemSize) function updateData(array: ArrayType) { gl.bindBuffer(_bufferType, _buffer); - (gl as WebGLRenderingContext).bufferData(_bufferType, array, _usageHint) // TODO remove cast when webgl2 types are fixed + gl.bufferData(_bufferType, array, _usageHint) } updateData(array) @@ -131,14 +126,12 @@ export function createBuffer(ctx: WebGLContext, array: ArrayType, itemSize: Buff _dataType, _bpe, - get itemSize () { return itemSize }, - get itemCount () { return _itemCount }, - get length () { return _length }, + length: _length, updateData, updateSubData: (array: ArrayType, offset: number, count: number) => { gl.bindBuffer(_bufferType, _buffer); - (gl as WebGLRenderingContext).bufferSubData(_bufferType, offset * _bpe, array.subarray(offset, offset + count)) // TODO remove cast when webgl2 types are fixed + gl.bufferSubData(_bufferType, offset * _bpe, array.subarray(offset, offset + count)) }, destroy: () => { @@ -150,8 +143,12 @@ export function createBuffer(ctx: WebGLContext, array: ArrayType, itemSize: Buff } } +// + +export type AttributeItemSize = 1 | 2 | 3 | 4 | 16 + export type AttributeDefs = { - [k: string]: { kind: ArrayKind, itemSize: BufferItemSize, divisor: number } + [k: string]: { kind: ArrayKind, itemSize: AttributeItemSize, divisor: number } } export type AttributeValues = { [k: string]: ValueCell<ArrayType> } export type AttributeBuffers = { [k: string]: AttributeBuffer } @@ -160,11 +157,11 @@ export interface AttributeBuffer extends Buffer { bind: (location: number) => void } -export function createAttributeBuffer<T extends ArrayType, S extends BufferItemSize>(ctx: WebGLContext, array: ArrayType, itemSize: S, divisor: number, usageHint: UsageHint = 'dynamic'): AttributeBuffer { +export function createAttributeBuffer<T extends ArrayType, S extends AttributeItemSize>(ctx: WebGLContext, array: T, itemSize: S, divisor: number, usageHint: UsageHint = 'dynamic'): AttributeBuffer { const { gl } = ctx const { instancedArrays } = ctx.extensions - const buffer = createBuffer(ctx, array, itemSize, usageHint, 'attribute') + const buffer = createBuffer(ctx, array, usageHint, 'attribute') const { _buffer, _bufferType, _dataType, _bpe } = buffer return { @@ -197,6 +194,8 @@ export function createAttributeBuffers(ctx: WebGLContext, schema: RenderableSche return buffers as AttributeBuffers } +// + export type ElementsType = Uint16Array | Uint32Array export type ElementsKind = 'uint16' | 'uint32' @@ -206,7 +205,7 @@ export interface ElementsBuffer extends Buffer { export function createElementsBuffer(ctx: WebGLContext, array: ElementsType, usageHint: UsageHint = 'static'): ElementsBuffer { const { gl } = ctx - const buffer = createBuffer(ctx, array, 1, usageHint, 'elements') + const buffer = createBuffer(ctx, array, usageHint, 'elements') const { _buffer } = buffer return { -- GitLab