Skip to content
Snippets Groups Projects
Commit 3bd7299a authored by Alexander Rose's avatar Alexander Rose
Browse files

cleaned up buffers

parent ce86231f
No related branches found
No related tags found
No related merge requests found
...@@ -5,7 +5,7 @@ ...@@ -5,7 +5,7 @@
*/ */
import { ValueCell } from 'mol-util'; 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 { UniformKind, UniformValues } from '../webgl/uniform';
import { DefineKind, DefineValues } from '../shader-code'; import { DefineKind, DefineValues } from '../shader-code';
import { Vec2, Vec3, Vec4, Mat3, Mat4 } from 'mol-math/linear-algebra'; import { Vec2, Vec3, Vec4, Mat3, Mat4 } from 'mol-math/linear-algebra';
...@@ -102,8 +102,8 @@ export function getValueVersions<T extends RenderableValues>(values: T) { ...@@ -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 type AttributeSpec<K extends ArrayKind> = { type: 'attribute', kind: K, itemSize: AttributeItemSize, divisor: number }
export function AttributeSpec<K extends ArrayKind>(kind: K, itemSize: BufferItemSize, divisor: number): AttributeSpec<K> { export function AttributeSpec<K extends ArrayKind>(kind: K, itemSize: AttributeItemSize, divisor: number): AttributeSpec<K> {
return { type: 'attribute', kind, itemSize, divisor } return { type: 'attribute', kind, itemSize, divisor }
} }
......
/** /**
* 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> * @author Alexander Rose <alexander.rose@weirdbyte.de>
*/ */
...@@ -14,7 +14,7 @@ const getNextBufferId = idFactory() ...@@ -14,7 +14,7 @@ const getNextBufferId = idFactory()
export type UsageHint = 'static' | 'dynamic' | 'stream' export type UsageHint = 'static' | 'dynamic' | 'stream'
export type DataType = 'uint8' | 'int8' | 'uint16' | 'int16' | 'uint32' | 'int32' | 'float32' export type DataType = 'uint8' | 'int8' | 'uint16' | 'int16' | 'uint32' | 'int32' | 'float32'
export type BufferType = 'attribute' | 'elements' export type BufferType = 'attribute' | 'elements' | 'uniform'
export type DataTypeArrayType = { export type DataTypeArrayType = {
'uint8': Uint8Array 'uint8': Uint8Array
...@@ -28,8 +28,6 @@ export type DataTypeArrayType = { ...@@ -28,8 +28,6 @@ export type DataTypeArrayType = {
export type ArrayType = ValueOf<DataTypeArrayType> export type ArrayType = ValueOf<DataTypeArrayType>
export type ArrayKind = keyof DataTypeArrayType export type ArrayKind = keyof DataTypeArrayType
export type BufferItemSize = 1 | 2 | 3 | 4 | 16
export function getUsageHint(ctx: WebGLContext, usageHint: UsageHint) { export function getUsageHint(ctx: WebGLContext, usageHint: UsageHint) {
const { gl } = ctx const { gl } = ctx
switch (usageHint) { switch (usageHint) {
...@@ -90,8 +88,6 @@ export interface Buffer { ...@@ -90,8 +88,6 @@ export interface Buffer {
readonly _dataType: number readonly _dataType: number
readonly _bpe: number readonly _bpe: number
readonly itemSize: number
readonly itemCount: number
readonly length: number readonly length: number
updateData: (array: ArrayType) => void updateData: (array: ArrayType) => void
...@@ -99,7 +95,7 @@ export interface Buffer { ...@@ -99,7 +95,7 @@ export interface Buffer {
destroy: () => void 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 { gl } = ctx
const _buffer = gl.createBuffer() const _buffer = gl.createBuffer()
if (_buffer === null) { if (_buffer === null) {
...@@ -111,11 +107,10 @@ export function createBuffer(ctx: WebGLContext, array: ArrayType, itemSize: Buff ...@@ -111,11 +107,10 @@ export function createBuffer(ctx: WebGLContext, array: ArrayType, itemSize: Buff
const _dataType = dataTypeFromArray(ctx, array) const _dataType = dataTypeFromArray(ctx, array)
const _bpe = array.BYTES_PER_ELEMENT const _bpe = array.BYTES_PER_ELEMENT
const _length = array.length const _length = array.length
const _itemCount = Math.floor(_length / itemSize)
function updateData(array: ArrayType) { function updateData(array: ArrayType) {
gl.bindBuffer(_bufferType, _buffer); 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) updateData(array)
...@@ -131,14 +126,12 @@ export function createBuffer(ctx: WebGLContext, array: ArrayType, itemSize: Buff ...@@ -131,14 +126,12 @@ export function createBuffer(ctx: WebGLContext, array: ArrayType, itemSize: Buff
_dataType, _dataType,
_bpe, _bpe,
get itemSize () { return itemSize }, length: _length,
get itemCount () { return _itemCount },
get length () { return _length },
updateData, updateData,
updateSubData: (array: ArrayType, offset: number, count: number) => { updateSubData: (array: ArrayType, offset: number, count: number) => {
gl.bindBuffer(_bufferType, _buffer); 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: () => { destroy: () => {
...@@ -150,8 +143,12 @@ export function createBuffer(ctx: WebGLContext, array: ArrayType, itemSize: Buff ...@@ -150,8 +143,12 @@ export function createBuffer(ctx: WebGLContext, array: ArrayType, itemSize: Buff
} }
} }
//
export type AttributeItemSize = 1 | 2 | 3 | 4 | 16
export type AttributeDefs = { 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 AttributeValues = { [k: string]: ValueCell<ArrayType> }
export type AttributeBuffers = { [k: string]: AttributeBuffer } export type AttributeBuffers = { [k: string]: AttributeBuffer }
...@@ -160,11 +157,11 @@ export interface AttributeBuffer extends Buffer { ...@@ -160,11 +157,11 @@ export interface AttributeBuffer extends Buffer {
bind: (location: number) => void 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 { gl } = ctx
const { instancedArrays } = ctx.extensions 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 const { _buffer, _bufferType, _dataType, _bpe } = buffer
return { return {
...@@ -197,6 +194,8 @@ export function createAttributeBuffers(ctx: WebGLContext, schema: RenderableSche ...@@ -197,6 +194,8 @@ export function createAttributeBuffers(ctx: WebGLContext, schema: RenderableSche
return buffers as AttributeBuffers return buffers as AttributeBuffers
} }
//
export type ElementsType = Uint16Array | Uint32Array export type ElementsType = Uint16Array | Uint32Array
export type ElementsKind = 'uint16' | 'uint32' export type ElementsKind = 'uint16' | 'uint32'
...@@ -206,7 +205,7 @@ export interface ElementsBuffer extends Buffer { ...@@ -206,7 +205,7 @@ export interface ElementsBuffer extends Buffer {
export function createElementsBuffer(ctx: WebGLContext, array: ElementsType, usageHint: UsageHint = 'static'): ElementsBuffer { export function createElementsBuffer(ctx: WebGLContext, array: ElementsType, usageHint: UsageHint = 'static'): ElementsBuffer {
const { gl } = ctx const { gl } = ctx
const buffer = createBuffer(ctx, array, 1, usageHint, 'elements') const buffer = createBuffer(ctx, array, usageHint, 'elements')
const { _buffer } = buffer const { _buffer } = buffer
return { return {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment