|
@@ -10,7 +10,7 @@ import { RuntimeContext, Task } from 'mol-task';
|
|
|
import { PositionData, DensityData } from './common';
|
|
|
import { OrderedSet } from 'mol-data/int';
|
|
|
import { createRenderable, createGaussianDensityRenderObject } from 'mol-gl/render-object';
|
|
|
-import { createContext } from 'mol-gl/webgl/context';
|
|
|
+import { createContext, Context } from 'mol-gl/webgl/context';
|
|
|
import { GaussianDensityValues } from 'mol-gl/renderable/gaussian-density';
|
|
|
import { RenderableState } from 'mol-gl/renderable';
|
|
|
import { ValueCell } from 'mol-util';
|
|
@@ -19,7 +19,9 @@ import { createRenderTarget } from 'mol-gl/webgl/render-target';
|
|
|
export const DefaultGaussianDensityProps = {
|
|
|
resolution: 1,
|
|
|
radiusOffset: 0,
|
|
|
- smoothness: 1.5
|
|
|
+ smoothness: 1.5,
|
|
|
+ readSlices: false,
|
|
|
+ useGpu: true,
|
|
|
}
|
|
|
export type GaussianDensityProps = typeof DefaultGaussianDensityProps
|
|
|
|
|
@@ -33,16 +35,19 @@ function getDelta(box: Box3D, resolution: number) {
|
|
|
|
|
|
export function computeGaussianDensity(position: PositionData, box: Box3D, radius: (index: number) => number, props: GaussianDensityProps) {
|
|
|
return Task.create('Gaussian Density', async ctx => {
|
|
|
- const foo = await GaussianDensityGPU(ctx, position, box, radius, props)
|
|
|
- console.log('FOOBAR', foo)
|
|
|
return await GaussianDensity(ctx, position, box, radius, props)
|
|
|
});
|
|
|
}
|
|
|
|
|
|
export async function GaussianDensity(ctx: RuntimeContext, position: PositionData, box: Box3D, radius: (index: number) => number, props: GaussianDensityProps): Promise<DensityData> {
|
|
|
- const foo = await GaussianDensityGPU(ctx, position, box, radius, props)
|
|
|
- console.log('FOOBAR', foo)
|
|
|
+ if (props.useGpu) {
|
|
|
+ return await GaussianDensityGPU(ctx, position, box, radius, props)
|
|
|
+ } else {
|
|
|
+ return await GaussianDensityCPU(ctx, position, box, radius, props)
|
|
|
+ }
|
|
|
+}
|
|
|
|
|
|
+export async function GaussianDensityCPU(ctx: RuntimeContext, position: PositionData, box: Box3D, radius: (index: number) => number, props: GaussianDensityProps): Promise<DensityData> {
|
|
|
const { resolution, radiusOffset, smoothness } = props
|
|
|
|
|
|
const { indices, x, y, z } = position
|
|
@@ -52,7 +57,7 @@ export async function GaussianDensity(ctx: RuntimeContext, position: PositionDat
|
|
|
const p = Vec3.zero()
|
|
|
|
|
|
const pad = (radiusOffset + 3) * 3 // TODO calculate max radius
|
|
|
- const expandedBox = Box3D.expand(Box3D.empty(), box, Vec3.create(pad, pad, pad));
|
|
|
+ const expandedBox = Box3D.expand(Box3D.empty(), box, Vec3.create(pad, pad, pad))
|
|
|
const extent = Vec3.sub(Vec3.zero(), expandedBox.max, expandedBox.min)
|
|
|
const min = expandedBox.min
|
|
|
|
|
@@ -83,8 +88,9 @@ export async function GaussianDensity(ctx: RuntimeContext, position: PositionDat
|
|
|
|
|
|
const gridPad = 1 / Math.max(...delta)
|
|
|
|
|
|
+ console.time('gaussian density cpu')
|
|
|
for (let i = 0; i < n; ++i) {
|
|
|
- const j = OrderedSet.getAt(indices, i);
|
|
|
+ const j = OrderedSet.getAt(indices, i)
|
|
|
|
|
|
Vec3.set(v, x[j], y[j], z[j])
|
|
|
|
|
@@ -121,9 +127,10 @@ export async function GaussianDensity(ctx: RuntimeContext, position: PositionDat
|
|
|
}
|
|
|
|
|
|
if (i % updateChunk === 0 && ctx.shouldUpdate) {
|
|
|
- await ctx.update({ message: 'filling density grid', current: i, max: n });
|
|
|
+ await ctx.update({ message: 'filling density grid', current: i, max: n })
|
|
|
}
|
|
|
}
|
|
|
+ console.timeEnd('gaussian density cpu')
|
|
|
|
|
|
const transform = Mat4.identity()
|
|
|
Mat4.fromScaling(transform, Vec3.inverse(Vec3.zero(), delta))
|
|
@@ -133,7 +140,7 @@ export async function GaussianDensity(ctx: RuntimeContext, position: PositionDat
|
|
|
}
|
|
|
|
|
|
export async function GaussianDensityGPU(ctx: RuntimeContext, position: PositionData, box: Box3D, radius: (index: number) => number, props: GaussianDensityProps) { // }: Promise<DensityData> {
|
|
|
- const { resolution, radiusOffset } = props
|
|
|
+ const { resolution, radiusOffset, smoothness, readSlices } = props
|
|
|
|
|
|
const { indices, x, y, z } = position
|
|
|
const n = OrderedSet.size(indices)
|
|
@@ -149,6 +156,11 @@ export async function GaussianDensityGPU(ctx: RuntimeContext, position: Position
|
|
|
const dim = Vec3.zero()
|
|
|
Vec3.ceil(dim, Vec3.mul(dim, extent, delta))
|
|
|
|
|
|
+ const _r2 = (radiusOffset + 1.4 * 2)
|
|
|
+ const _radius2 = Vec3.create(_r2, _r2, _r2)
|
|
|
+ Vec3.mul(_radius2, _radius2, delta)
|
|
|
+ const updateChunk = Math.ceil(10000 / (_radius2[0] * _radius2[1] * _radius2[2]))
|
|
|
+
|
|
|
for (let i = 0; i < n; ++i) {
|
|
|
const j = OrderedSet.getAt(indices, i);
|
|
|
|
|
@@ -158,7 +170,7 @@ export async function GaussianDensityGPU(ctx: RuntimeContext, position: Position
|
|
|
radii[i] = radius(j) + radiusOffset
|
|
|
|
|
|
if (i % 10000 === 0 && ctx.shouldUpdate) {
|
|
|
- await ctx.update({ message: 'preparing density data', current: i, max: n });
|
|
|
+ await ctx.update({ message: 'preparing density data', current: i, max: n })
|
|
|
}
|
|
|
}
|
|
|
|
|
@@ -178,47 +190,52 @@ export async function GaussianDensityGPU(ctx: RuntimeContext, position: Position
|
|
|
uBboxMax: ValueCell.create(expandedBox.max),
|
|
|
uBboxSize: ValueCell.create(extent),
|
|
|
uGridDim: ValueCell.create(dim),
|
|
|
+ uAlpha: ValueCell.create(smoothness),
|
|
|
}
|
|
|
const state: RenderableState = {
|
|
|
visible: true,
|
|
|
depthMask: false
|
|
|
}
|
|
|
|
|
|
- const canvas = document.createElement('canvas')
|
|
|
- const gl = canvas.getContext('webgl', {
|
|
|
- alpha: false,
|
|
|
- antialias: true,
|
|
|
- depth: true,
|
|
|
- preserveDrawingBuffer: true
|
|
|
- })
|
|
|
- if (!gl) throw new Error('Could not create a WebGL rendering context')
|
|
|
- const webgl = createContext(gl)
|
|
|
+ // TODO do in OffscreenCanvas (https://www.chromestatus.com/feature/5681560598609920)
|
|
|
+ const webgl = getWebGLContext()
|
|
|
|
|
|
const renderObject = createGaussianDensityRenderObject(values, state)
|
|
|
const renderable = createRenderable(webgl, renderObject)
|
|
|
|
|
|
//
|
|
|
|
|
|
- // get actual max texture size
|
|
|
- const maxTexSize = 4096; // gl. .limits.maxTextureSize;
|
|
|
- let fboTexDimX = 0
|
|
|
- let fboTexDimY = dim[1]
|
|
|
- let fboTexRows = 1
|
|
|
- let fboTexCols = dim[0]
|
|
|
- if(maxTexSize < dim[0] * dim[2]) {
|
|
|
- fboTexCols = Math.floor(maxTexSize / dim[0])
|
|
|
- fboTexRows = Math.ceil(dim[2] / fboTexCols)
|
|
|
- fboTexDimX = fboTexCols * dim[0]
|
|
|
- fboTexDimY *= fboTexRows
|
|
|
- } else {
|
|
|
- fboTexDimX = dim[0] * dim[2]
|
|
|
- }
|
|
|
+ // TODO fallback to lower resolution when texture size is not large enough
|
|
|
+ const maxTexSize = webgl.maxTextureSize
|
|
|
+ let fboTexDimX = 0
|
|
|
+ let fboTexDimY = dim[1]
|
|
|
+ let fboTexRows = 1
|
|
|
+ let fboTexCols = dim[0]
|
|
|
+ if (maxTexSize < dim[0] * dim[2]) {
|
|
|
+ fboTexCols = Math.floor(maxTexSize / dim[0])
|
|
|
+ fboTexRows = Math.ceil(dim[2] / fboTexCols)
|
|
|
+ fboTexDimX = fboTexCols * dim[0]
|
|
|
+ fboTexDimY *= fboTexRows
|
|
|
+ } else {
|
|
|
+ fboTexDimX = dim[0] * dim[2]
|
|
|
+ }
|
|
|
+
|
|
|
+ //
|
|
|
+
|
|
|
+ const space = Tensor.Space(dim, [2, 1, 0], Float32Array)
|
|
|
+ const data = space.create()
|
|
|
+ const field = Tensor.create(space, data)
|
|
|
+
|
|
|
+ const idData = space.create()
|
|
|
+ const idField = Tensor.create(space, idData)
|
|
|
|
|
|
//
|
|
|
|
|
|
+ const { gl } = webgl
|
|
|
+
|
|
|
const program = renderable.getProgram('draw')
|
|
|
const renderTarget = createRenderTarget(webgl, fboTexDimX, fboTexDimY)
|
|
|
-
|
|
|
+
|
|
|
program.use()
|
|
|
renderTarget.bind()
|
|
|
|
|
@@ -235,29 +252,65 @@ export async function GaussianDensityGPU(ctx: RuntimeContext, position: Position
|
|
|
gl.blendEquation(gl.FUNC_ADD)
|
|
|
gl.enable(gl.BLEND)
|
|
|
|
|
|
- gl.finish();
|
|
|
- let currCol = 0;
|
|
|
- let currY = 0;
|
|
|
- let currX = 0;
|
|
|
- for(let i = 0; i < dim[2]; ++i) {
|
|
|
- if (currCol >= fboTexCols) {
|
|
|
- currCol -= fboTexCols
|
|
|
- currY += dim[1]
|
|
|
- currX = 0
|
|
|
+ const slice = new Uint8Array(dim[0] * dim[1] * 4)
|
|
|
+
|
|
|
+ console.time('gpu gaussian density slices')
|
|
|
+ let currCol = 0
|
|
|
+ let currY = 0
|
|
|
+ let currX = 0
|
|
|
+ let j = 0
|
|
|
+ for (let i = 0; i < dim[2]; ++i) {
|
|
|
+ if (currCol >= fboTexCols) {
|
|
|
+ currCol -= fboTexCols
|
|
|
+ currY += dim[1]
|
|
|
+ currX = 0
|
|
|
}
|
|
|
gl.viewport(currX, currY, dim[0], dim[1])
|
|
|
ValueCell.update(values.uCurrentSlice, i)
|
|
|
ValueCell.update(values.uCurrentX, currX)
|
|
|
ValueCell.update(values.uCurrentY, currY)
|
|
|
renderable.render('draw')
|
|
|
- ++currCol
|
|
|
- currX += dim[0]
|
|
|
- }
|
|
|
- gl.finish();
|
|
|
-
|
|
|
- const imageData = renderTarget.getImageData()
|
|
|
- console.log(imageData)
|
|
|
- debugTexture(imageData, 0.4)
|
|
|
+ if (readSlices) {
|
|
|
+ renderTarget.readBuffer(currX, currY, dim[0], dim[1], slice)
|
|
|
+ for (let iy = 0; iy < dim[1]; ++iy) {
|
|
|
+ for (let ix = 0; ix < dim[0]; ++ix) {
|
|
|
+ data[j] = slice[4 * (iy * dim[0] + ix)] / 255
|
|
|
+ ++j
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ ++currCol
|
|
|
+ currX += dim[0]
|
|
|
+
|
|
|
+ if (i % updateChunk === 0 && ctx.shouldUpdate) {
|
|
|
+ await ctx.update({ message: 'filling density grid', current: i, max: n })
|
|
|
+ }
|
|
|
+ }
|
|
|
+ console.timeEnd('gpu gaussian density slices')
|
|
|
+
|
|
|
+ //
|
|
|
+
|
|
|
+ if (!readSlices) {
|
|
|
+ console.time('gpu gaussian density full')
|
|
|
+ renderTarget.getBuffer()
|
|
|
+ let idx = 0
|
|
|
+ let tmpCol = 0
|
|
|
+ let tmpRow = 0
|
|
|
+ for (let iz = 0; iz < dim[2]; ++iz) {
|
|
|
+ if (tmpCol >= fboTexCols ) {
|
|
|
+ tmpCol = 0
|
|
|
+ tmpRow += dim[1]
|
|
|
+ }
|
|
|
+ for (let iy = 0; iy < dim[1]; ++iy) {
|
|
|
+ for (let ix = 0; ix < dim[0]; ++ix) {
|
|
|
+ data[idx] = renderTarget.image.array[4 * (tmpCol * dim[0] + (iy + tmpRow) * fboTexDimX + ix)] / 255
|
|
|
+ idx++
|
|
|
+ }
|
|
|
+ }
|
|
|
+ tmpCol++
|
|
|
+ }
|
|
|
+ console.timeEnd('gpu gaussian density full')
|
|
|
+ }
|
|
|
|
|
|
//
|
|
|
|
|
@@ -265,25 +318,20 @@ export async function GaussianDensityGPU(ctx: RuntimeContext, position: Position
|
|
|
Mat4.fromScaling(transform, Vec3.inverse(Vec3.zero(), delta))
|
|
|
Mat4.setTranslation(transform, expandedBox.min)
|
|
|
|
|
|
- return { field: imageData, idField: undefined, transform }
|
|
|
+ return { field, idField, transform }
|
|
|
}
|
|
|
|
|
|
-function debugTexture(imageData: ImageData, scale: number) {
|
|
|
- const canvas = document.createElement('canvas')
|
|
|
- canvas.width = imageData.width
|
|
|
- canvas.height = imageData.height
|
|
|
- const ctx = canvas.getContext('2d')
|
|
|
- if (!ctx) throw new Error('Could not create canvas 2d context')
|
|
|
- ctx.putImageData(imageData, 0, 0)
|
|
|
- canvas.toBlob(function(imgBlob){
|
|
|
- var objectURL = window.URL.createObjectURL(imgBlob)
|
|
|
- var img = document.createElement('img')
|
|
|
- img.src = objectURL
|
|
|
- img.style.width = imageData.width * scale + 'px'
|
|
|
- img.style.height = imageData.height * scale + 'px'
|
|
|
- img.style.position = 'absolute'
|
|
|
- img.style.top = '0px'
|
|
|
- img.style.left = '0px'
|
|
|
- document.body.appendChild(img)
|
|
|
- }, 'image/png')
|
|
|
+let webglContext: Context
|
|
|
+function getWebGLContext() {
|
|
|
+ if (webglContext) return webglContext
|
|
|
+ const canvas = document.createElement('canvas')
|
|
|
+ const gl = canvas.getContext('webgl', {
|
|
|
+ alpha: false,
|
|
|
+ antialias: true,
|
|
|
+ depth: true,
|
|
|
+ preserveDrawingBuffer: true
|
|
|
+ })
|
|
|
+ if (!gl) throw new Error('Could not create a WebGL rendering context')
|
|
|
+ webglContext = createContext(gl)
|
|
|
+ return webglContext
|
|
|
}
|