mirror of
https://github.com/cocos/cocos4.git
synced 2026-12-02 00:22:43 +08:00
75 lines
3.1 KiB
TypeScript
75 lines
3.1 KiB
TypeScript
import { GFXFormat, GFXFormatInfos, GFXFormatType, IGFXFormatInfo } from '../../gfx/define';
|
|
|
|
const isLittleEndian = cc.sys.isLittleEndian;
|
|
const _typeMap = {
|
|
[GFXFormatType.UNORM]: 'Uint',
|
|
[GFXFormatType.SNORM]: 'Int',
|
|
[GFXFormatType.UINT]: 'Uint',
|
|
[GFXFormatType.INT]: 'Int',
|
|
[GFXFormatType.UFLOAT]: 'Float',
|
|
[GFXFormatType.FLOAT]: 'Float',
|
|
default: 'Uint',
|
|
};
|
|
function _getDataViewType (info: IGFXFormatInfo) {
|
|
const type = _typeMap[info.type] || _typeMap.default;
|
|
const bytes = info.size / info.count * 8;
|
|
return type + bytes;
|
|
}
|
|
|
|
|
|
// default params bahaves just like on an plain, compact Float32Array
|
|
export function writeBuffer (target: DataView, data: number[], format: GFXFormat = GFXFormat.R32F, offset: number = 0, stride: number = 0) {
|
|
const info = GFXFormatInfos[format];
|
|
if (!stride) { stride = info.size; }
|
|
const writer = 'set' + _getDataViewType(info);
|
|
const componentBytesLength = info.size / info.count;
|
|
const nSeg = Math.floor(data.length / info.count);
|
|
|
|
for (let iSeg = 0; iSeg < nSeg; ++iSeg) {
|
|
const x = offset + stride * iSeg;
|
|
for (let iComponent = 0; iComponent < info.count; ++iComponent) {
|
|
const y = x + componentBytesLength * iComponent;
|
|
target[writer](y, data[info.count * iSeg + iComponent], isLittleEndian);
|
|
}
|
|
}
|
|
}
|
|
export function readBuffer (
|
|
target: DataView, format: GFXFormat = GFXFormat.R32F, offset: number = 0,
|
|
length: number = target.byteLength - offset, stride: number = 0, out: number[] = []) {
|
|
const info = GFXFormatInfos[format];
|
|
if (!stride) { stride = info.size; }
|
|
const reader = 'get' + _getDataViewType(info);
|
|
const componentBytesLength = info.size / info.count;
|
|
const nSeg = Math.floor(length / stride);
|
|
|
|
for (let iSeg = 0; iSeg < nSeg; ++iSeg) {
|
|
const x = offset + stride * iSeg;
|
|
for (let iComponent = 0; iComponent < info.count; ++iComponent) {
|
|
const y = x + componentBytesLength * iComponent;
|
|
out[info.count * iSeg + iComponent] = target[reader](y, isLittleEndian);
|
|
}
|
|
}
|
|
return out;
|
|
}
|
|
export function mapBuffer (
|
|
target: DataView, callback: (cur: number, idx: number, view: DataView) => number, format: GFXFormat = GFXFormat.R32F,
|
|
offset: number = 0, length: number = target.byteLength - offset, stride: number = 0, out?: DataView) {
|
|
if (!out) { out = new DataView(new ArrayBuffer(target.byteLength)); }
|
|
const info = GFXFormatInfos[format];
|
|
if (!stride) { stride = info.size; }
|
|
const writer = 'set' + _getDataViewType(info);
|
|
const reader = 'get' + _getDataViewType(info);
|
|
const componentBytesLength = info.size / info.count;
|
|
const nSeg = Math.floor(length / stride);
|
|
|
|
for (let iSeg = 0; iSeg < nSeg; ++iSeg) {
|
|
const x = offset + stride * iSeg;
|
|
for (let iComponent = 0; iComponent < info.count; ++iComponent) {
|
|
const y = x + componentBytesLength * iComponent;
|
|
const cur = target[reader](y, isLittleEndian);
|
|
// iComponent is usually more useful than y
|
|
out[writer](y, callback(cur, iComponent, target), isLittleEndian);
|
|
}
|
|
}
|
|
return out;
|
|
} |