|
@@ -4,13 +4,13 @@
|
|
* @author Alexander Rose <alexander.rose@weirdbyte.de>
|
|
* @author Alexander Rose <alexander.rose@weirdbyte.de>
|
|
*/
|
|
*/
|
|
|
|
|
|
-import { UniformDefs, UniformValues } from './uniform';
|
|
|
|
-import { AttributeDefs, AttributeValues, createAttributeBuffers, createElementsBuffer, ElementsKind, ElementsBuffer } from './buffer';
|
|
|
|
-import { TextureDefs, TextureValues, createTextures } from './texture';
|
|
|
|
|
|
+import { UniformValues } from './uniform';
|
|
|
|
+import { AttributeValues, createAttributeBuffers, createElementsBuffer, ElementsBuffer } from './buffer';
|
|
|
|
+import { TextureValues, createTextures } from './texture';
|
|
import { Context } from './context';
|
|
import { Context } from './context';
|
|
-import { ShaderCode } from '../shader-code';
|
|
|
|
|
|
+import { ShaderCode, addShaderDefines, DefineValues } from '../shader-code';
|
|
import { Program } from './program';
|
|
import { Program } from './program';
|
|
-import { ValueCell } from 'mol-util';
|
|
|
|
|
|
+import { RenderableSchema, RenderableValues } from '../renderable/schema';
|
|
|
|
|
|
export type DrawMode = 'points' | 'lines' | 'line-strip' | 'line-loop' | 'triangles' | 'triangle-strip' | 'triangle-fan'
|
|
export type DrawMode = 'points' | 'lines' | 'line-strip' | 'line-loop' | 'triangles' | 'triangle-strip' | 'triangle-fan'
|
|
|
|
|
|
@@ -27,29 +27,21 @@ export function getDrawMode(ctx: Context, drawMode: DrawMode) {
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
-export type RenderItemProps = {
|
|
|
|
- shaderCode: ShaderCode
|
|
|
|
-
|
|
|
|
- uniformDefs: UniformDefs
|
|
|
|
- attributeDefs: AttributeDefs
|
|
|
|
- textureDefs: TextureDefs
|
|
|
|
-
|
|
|
|
- elementsKind?: ElementsKind
|
|
|
|
- drawMode: DrawMode
|
|
|
|
-}
|
|
|
|
-
|
|
|
|
-export type RenderItemState = {
|
|
|
|
- uniformValues: UniformValues
|
|
|
|
- attributeValues: AttributeValues
|
|
|
|
- textureValues: TextureValues
|
|
|
|
-
|
|
|
|
- elements?: Uint32Array
|
|
|
|
- drawCount: ValueCell<number>
|
|
|
|
- instanceCount: ValueCell<number>
|
|
|
|
|
|
+function splitValues(schema: RenderableSchema, values: RenderableValues) {
|
|
|
|
+ const attributeValues: AttributeValues = {}
|
|
|
|
+ const defineValues: DefineValues = {}
|
|
|
|
+ const textureValues: TextureValues = {}
|
|
|
|
+ const uniformValues: UniformValues = {}
|
|
|
|
+ Object.keys(values).forEach(k => {
|
|
|
|
+ if (schema[k].type === 'attribute') attributeValues[k] = values[k]
|
|
|
|
+ if (schema[k].type === 'define') defineValues[k] = values[k]
|
|
|
|
+ if (schema[k].type === 'texture') textureValues[k] = values[k]
|
|
|
|
+ if (schema[k].type === 'uniform') uniformValues[k] = values[k]
|
|
|
|
+ })
|
|
|
|
+ return { attributeValues, defineValues, textureValues, uniformValues }
|
|
}
|
|
}
|
|
|
|
|
|
export interface RenderItem {
|
|
export interface RenderItem {
|
|
- readonly hash: string
|
|
|
|
readonly programId: number
|
|
readonly programId: number
|
|
readonly program: Program
|
|
readonly program: Program
|
|
|
|
|
|
@@ -58,19 +50,23 @@ export interface RenderItem {
|
|
destroy: () => void
|
|
destroy: () => void
|
|
}
|
|
}
|
|
|
|
|
|
-export function createRenderItem(ctx: Context, props: RenderItemProps, state: RenderItemState): RenderItem {
|
|
|
|
|
|
+export function createRenderItem(ctx: Context, drawMode: DrawMode, shaderCode: ShaderCode, schema: RenderableSchema, values: RenderableValues): RenderItem {
|
|
const { programCache } = ctx
|
|
const { programCache } = ctx
|
|
const { angleInstancedArrays, oesVertexArrayObject } = ctx.extensions
|
|
const { angleInstancedArrays, oesVertexArrayObject } = ctx.extensions
|
|
- const { shaderCode, uniformDefs, attributeDefs, textureDefs, elementsKind } = props
|
|
|
|
- const { attributeValues, textureValues, uniformValues, elements } = state
|
|
|
|
|
|
|
|
- const hash = JSON.stringify(props)
|
|
|
|
- const drawMode = getDrawMode(ctx, props.drawMode)
|
|
|
|
- const programRef = programCache.get(ctx, { shaderCode, uniformDefs, attributeDefs, textureDefs })
|
|
|
|
|
|
+ const { attributeValues, defineValues, textureValues, uniformValues } = splitValues(schema, values)
|
|
|
|
+
|
|
|
|
+ const glDrawMode = getDrawMode(ctx, drawMode)
|
|
|
|
+ const programRef = programCache.get(ctx, {
|
|
|
|
+ shaderCode: addShaderDefines(defineValues, shaderCode),
|
|
|
|
+ schema
|
|
|
|
+ })
|
|
const program = programRef.value
|
|
const program = programRef.value
|
|
|
|
|
|
- const textures = createTextures(ctx, textureDefs, textureValues)
|
|
|
|
- const attributeBuffers = createAttributeBuffers(ctx, attributeDefs, attributeValues)
|
|
|
|
|
|
+ const textures = createTextures(ctx, schema, textureValues)
|
|
|
|
+ const attributeBuffers = createAttributeBuffers(ctx, schema, attributeValues)
|
|
|
|
+
|
|
|
|
+ const elements = values.elements
|
|
|
|
|
|
let vertexArray: WebGLVertexArrayObjectOES
|
|
let vertexArray: WebGLVertexArrayObjectOES
|
|
if (oesVertexArrayObject) {
|
|
if (oesVertexArrayObject) {
|
|
@@ -81,8 +77,8 @@ export function createRenderItem(ctx: Context, props: RenderItemProps, state: Re
|
|
}
|
|
}
|
|
|
|
|
|
let elementsBuffer: ElementsBuffer
|
|
let elementsBuffer: ElementsBuffer
|
|
- if (elements && elementsKind) {
|
|
|
|
- elementsBuffer = createElementsBuffer(ctx, elements)
|
|
|
|
|
|
+ if (elements && elements.ref.value) {
|
|
|
|
+ elementsBuffer = createElementsBuffer(ctx, elements.ref.value)
|
|
}
|
|
}
|
|
|
|
|
|
// needs to come after elements buffer creation to include it in the vao
|
|
// needs to come after elements buffer creation to include it in the vao
|
|
@@ -90,13 +86,12 @@ export function createRenderItem(ctx: Context, props: RenderItemProps, state: Re
|
|
oesVertexArrayObject.bindVertexArrayOES(null!)
|
|
oesVertexArrayObject.bindVertexArrayOES(null!)
|
|
}
|
|
}
|
|
|
|
|
|
- let drawCount = state.drawCount.ref
|
|
|
|
- let instanceCount = state.instanceCount.ref
|
|
|
|
|
|
+ let drawCount = values.drawCount.ref
|
|
|
|
+ let instanceCount = values.instanceCount.ref
|
|
|
|
|
|
let destroyed = false
|
|
let destroyed = false
|
|
|
|
|
|
return {
|
|
return {
|
|
- hash,
|
|
|
|
programId: program.id,
|
|
programId: program.id,
|
|
program,
|
|
program,
|
|
|
|
|
|
@@ -110,22 +105,21 @@ export function createRenderItem(ctx: Context, props: RenderItemProps, state: Re
|
|
}
|
|
}
|
|
program.bindTextures(textures)
|
|
program.bindTextures(textures)
|
|
if (elementsBuffer) {
|
|
if (elementsBuffer) {
|
|
- angleInstancedArrays.drawElementsInstancedANGLE(drawMode, drawCount.value, elementsBuffer._dataType, 0, instanceCount.value);
|
|
|
|
|
|
+ angleInstancedArrays.drawElementsInstancedANGLE(glDrawMode, drawCount.value, elementsBuffer._dataType, 0, instanceCount.value);
|
|
} else {
|
|
} else {
|
|
- angleInstancedArrays.drawArraysInstancedANGLE(drawMode, 0, drawCount.value, instanceCount.value)
|
|
|
|
|
|
+ angleInstancedArrays.drawArraysInstancedANGLE(glDrawMode, 0, drawCount.value, instanceCount.value)
|
|
}
|
|
}
|
|
},
|
|
},
|
|
update: () => {
|
|
update: () => {
|
|
- if (state.drawCount.ref.version !== drawCount.version) {
|
|
|
|
|
|
+ if (values.drawCount.ref.version !== drawCount.version) {
|
|
console.log('drawCount version changed')
|
|
console.log('drawCount version changed')
|
|
- drawCount = state.drawCount.ref
|
|
|
|
|
|
+ drawCount = values.drawCount.ref
|
|
}
|
|
}
|
|
- if (state.instanceCount.ref.version !== instanceCount.version) {
|
|
|
|
|
|
+ if (values.instanceCount.ref.version !== instanceCount.version) {
|
|
console.log('instanceCount version changed')
|
|
console.log('instanceCount version changed')
|
|
- instanceCount = state.instanceCount.ref
|
|
|
|
|
|
+ instanceCount = values.instanceCount.ref
|
|
}
|
|
}
|
|
|
|
|
|
- // const { attributeValues } = state
|
|
|
|
// Object.keys(attributeValues).forEach(k => {
|
|
// Object.keys(attributeValues).forEach(k => {
|
|
// const value = attributeValues[k]
|
|
// const value = attributeValues[k]
|
|
// if (value === undefined) return
|
|
// if (value === undefined) return
|
|
@@ -142,7 +136,7 @@ export function createRenderItem(ctx: Context, props: RenderItemProps, state: Re
|
|
programRef.free()
|
|
programRef.free()
|
|
Object.keys(textures).forEach(k => textures[k].destroy())
|
|
Object.keys(textures).forEach(k => textures[k].destroy())
|
|
Object.keys(attributeBuffers).forEach(k => attributeBuffers[k].destroy())
|
|
Object.keys(attributeBuffers).forEach(k => attributeBuffers[k].destroy())
|
|
- if (elements && elementsKind) {
|
|
|
|
|
|
+ if (elements) {
|
|
elementsBuffer.destroy()
|
|
elementsBuffer.destroy()
|
|
}
|
|
}
|
|
if (oesVertexArrayObject) {
|
|
if (oesVertexArrayObject) {
|