import macro from 'vtk.js/Sources/macros'; import vtkDataArray from 'vtk.js/Sources/Common/Core/DataArray'; import vtkWebGPUTexture from 'vtk.js/Sources/Rendering/WebGPU/Texture';
const { VtkDataTypes } = vtkDataArray;
function vtkWebGPUTextureManager(publicAPI, model) { model.classHierarchy.push('vtkWebGPUTextureManager');
function _fillRequest(req) { if (req.imageData) { req.dataArray = req.imageData.getPointData().getScalars(); req.time = req.dataArray.getMTime(); req.nativeArray = req.dataArray.getData(); const dims = req.imageData.getDimensions(); req.width = dims[0]; req.height = dims[1]; req.depth = dims[2]; const numComp = req.dataArray.getNumberOfComponents(); switch (numComp) { case 1: req.format = 'r'; break; case 2: req.format = 'rg'; break; case 3: case 4: default: req.format = 'rgba'; break; }
const dataType = req.dataArray.getDataType(); switch (dataType) { case VtkDataTypes.UNSIGNED_CHAR: req.format += '8unorm'; break; case VtkDataTypes.FLOAT: case VtkDataTypes.UNSIGNED_INT: case VtkDataTypes.INT: case VtkDataTypes.DOUBLE: case VtkDataTypes.UNSIGNED_SHORT: case VtkDataTypes.SHORT: default: req.format += '16float'; break; } }
if (req.image) { req.width = req.image.width; req.height = req.image.height; req.depth = 1; req.format = 'rgba8unorm'; }
if (req.jsImageData) { req.width = req.jsImageData.width; req.height = req.jsImageData.height; req.depth = 1; req.format = 'rgba8unorm'; req.flip = true; req.nativeArray = req.jsImageData.data; }
if (req.canvas) { req.width = req.canvas.width; req.height = req.canvas.height; req.depth = 1; req.format = 'rgba8unorm'; req.flip = true; req.usage = GPUTextureUsage.TEXTURE_BINDING | GPUTextureUsage.COPY_DST | GPUTextureUsage.RENDER_ATTACHMENT; } }
function _createTexture(req) { const newTex = vtkWebGPUTexture.newInstance();
newTex.create(model.device, { width: req.width, height: req.height, depth: req.depth, format: req.format, usage: req.usage, mipLevel: req.mipLevel, });
if (req.nativeArray || req.image || req.canvas) { newTex.writeImageData(req); } return newTex; }
publicAPI.getTexture = (req) => { if (req.hash) { return model.device.getCachedObject(req.hash, _createTexture, req); }
return _createTexture(req); };
publicAPI.getTextureForImageData = (imgData) => { const treq = { time: imgData.getMTime() }; treq.imageData = imgData; _fillRequest(treq); treq.hash = treq.time + treq.format + treq.mipLevel; return model.device.getTextureManager().getTexture(treq); };
publicAPI.getTextureForVTKTexture = (srcTexture) => { const treq = { time: srcTexture.getMTime() }; if (srcTexture.getInputData()) { treq.imageData = srcTexture.getInputData(); } else if (srcTexture.getImage()) { treq.image = srcTexture.getImage(); } else if (srcTexture.getJsImageData()) { treq.jsImageData = srcTexture.getJsImageData(); } else if (srcTexture.getCanvas()) { treq.canvas = srcTexture.getCanvas(); } _fillRequest(treq); treq.mipLevel = srcTexture.getMipLevel(); treq.hash = treq.time + treq.format + treq.mipLevel; return model.device.getTextureManager().getTexture(treq); }; }
const DEFAULT_VALUES = { handle: null, device: null, };
export function extend(publicAPI, model, initialValues = {}) { Object.assign(model, DEFAULT_VALUES, initialValues);
macro.obj(publicAPI, model);
macro.setGet(publicAPI, model, ['device']);
vtkWebGPUTextureManager(publicAPI, model); }
export const newInstance = macro.newInstance(extend);
export default { newInstance, extend };
|