copyBuffersToTexture using ArrayBufferViews (#5945)

This commit is contained in:
Yun Hsiao Wu
2019-12-23 16:10:47 +08:00
committed by pandamicro
parent 25636a620b
commit 3513319c1e
15 changed files with 46 additions and 48 deletions
@@ -391,7 +391,7 @@ export class BatchedSkinningModelComponent extends SkinningModelComponent {
protected cookTextures (target: Texture2D, prop: string, passIdx: number) {
const texImages: TexImageSource[] = [];
const texImageRegions: GFXBufferTextureCopy[] = [];
const texBuffers: ArrayBuffer[] = [];
const texBuffers: ArrayBufferView[] = [];
const texBufferRegions: GFXBufferTextureCopy[] = [];
for (const unit of this.units) {
if (!unit.material) { continue; }
@@ -407,7 +407,7 @@ export class BatchedSkinningModelComponent extends SkinningModelComponent {
texImages.push(data);
texImageRegions.push(region);
} else {
texBuffers.push((data as ArrayBufferView).buffer);
texBuffers.push(data);
texBufferRegions.push(region);
}
}
+3 -3
View File
@@ -73,8 +73,8 @@ export class ImageAsset extends Asset {
* 此图像资源的图像数据。
*/
get data () {
let data:any = (this._nativeData as IMemoryImageSource)._data;
return (ArrayBuffer.isView(data) || data instanceof ArrayBuffer) ? data : this._nativeData;
const data = (this._nativeData as IMemoryImageSource)._data;
return ArrayBuffer.isView(data) ? data : this._nativeData as (HTMLCanvasElement | HTMLImageElement);
}
/**
@@ -119,7 +119,7 @@ export class ImageAsset extends Asset {
get _texture () {
if (!this._tex) {
let tex = new cc.Texture2D();
const tex = new cc.Texture2D();
tex.name = this._url;
tex.image = this;
this._tex = tex;
+3 -9
View File
@@ -111,7 +111,7 @@ export class SimpleTexture extends TextureBase {
* @param level Mipmap 层级。
* @param arrayIndex 数组索引。
*/
public uploadData (source: HTMLCanvasElement | HTMLImageElement | ArrayBuffer, level: number = 0, arrayIndex: number = 0) {
public uploadData (source: HTMLCanvasElement | HTMLImageElement | ArrayBufferView, level: number = 0, arrayIndex: number = 0) {
if (!this._gfxTexture || this._gfxTexture.mipLevel <= level) {
return;
}
@@ -136,7 +136,7 @@ export class SimpleTexture extends TextureBase {
}
}
if (source instanceof ArrayBuffer) {
if (ArrayBuffer.isView(source)) {
gfxDevice.copyBuffersToTexture([source], this._gfxTexture, _regions);
} else {
gfxDevice.copyTexImagesToTexture([source], this._gfxTexture, _regions);
@@ -149,13 +149,7 @@ export class SimpleTexture extends TextureBase {
if (!data) {
return;
}
let source: HTMLCanvasElement | HTMLImageElement | ArrayBuffer;
if (ArrayBuffer.isView(data)) {
source = (data as ArrayBufferView).buffer;
} else {
source = (data as HTMLCanvasElement | HTMLImageElement);
}
this.uploadData(source, level, arrayIndex);
this.uploadData(data, level, arrayIndex);
this._checkTextureLoaded();
};
if (image.loaded) {
+3 -3
View File
@@ -320,11 +320,11 @@ export abstract class GFXDevice {
return this._memoryStatus;
}
get reverseCW(): boolean {
get reverseCW (): boolean {
return this._reverseCW;
}
set reverseCW(val: boolean) {
set reverseCW (val: boolean) {
this._reverseCW = val;
}
@@ -506,7 +506,7 @@ export abstract class GFXDevice {
* @param texture GFX纹理。
* @param regions GFX缓冲纹理拷贝区域信息。
*/
public abstract copyBuffersToTexture (buffers: ArrayBuffer[], texture: GFXTexture, regions: GFXBufferTextureCopy[]);
public abstract copyBuffersToTexture (buffers: ArrayBufferView[], texture: GFXTexture, regions: GFXBufferTextureCopy[]);
/**
* @zh
+4 -4
View File
@@ -17,7 +17,7 @@ export class WebGLGFXBuffer extends GFXBuffer {
}
private _gpuBuffer: WebGLGPUBuffer | null = null;
private _uniformBuffer: ArrayBuffer | null = null;
private _uniformBuffer: Uint8Array | null = null;
private _indirectBuffer: IGFXIndirectBuffer | null = null;
constructor (device: GFXDevice) {
@@ -37,7 +37,7 @@ export class WebGLGFXBuffer extends GFXBuffer {
this._indirectBuffer = { drawInfos: [] };
} else {
if ((this._usage & GFXBufferUsageBit.UNIFORM) && this._size > 0) {
this._uniformBuffer = new ArrayBuffer(this._size);
this._uniformBuffer = new Uint8Array(this._size);
}
}
@@ -88,7 +88,7 @@ export class WebGLGFXBuffer extends GFXBuffer {
this._count = this._size / this._stride;
if (this._uniformBuffer) {
this._uniformBuffer = new ArrayBuffer(this._size);
this._uniformBuffer = new Uint8Array(this._size);
}
if (this._bufferView && oldSize !== size) {
@@ -96,7 +96,7 @@ export class WebGLGFXBuffer extends GFXBuffer {
this._bufferView = new Uint8Array(this._size);
this._bufferView.set(oldView);
if (this._gpuBuffer) {
this._gpuBuffer.buffer = this._bufferView.buffer;
this._gpuBuffer.buffer = this._bufferView;
}
}
+5 -5
View File
@@ -630,7 +630,7 @@ export function WebGLCmdFuncCreateBuffer (device: WebGLGFXDevice, gpuBuffer: Web
gpuBuffer.glTarget = gl.NONE;
if (gpuBuffer.buffer) {
gpuBuffer.vf32 = new Float32Array(gpuBuffer.buffer as ArrayBuffer);
gpuBuffer.vf32 = new Float32Array(gpuBuffer.buffer.buffer);
}
} else if (gpuBuffer.usage & GFXBufferUsageBit.INDIRECT) {
gpuBuffer.glTarget = gl.NONE;
@@ -698,7 +698,7 @@ export function WebGLCmdFuncResizeBuffer (device: WebGLGFXDevice, gpuBuffer: Web
} else if (gpuBuffer.usage & GFXBufferUsageBit.UNIFORM) {
// console.error("WebGL 1.0 doesn't support uniform buffer.");
if (gpuBuffer.buffer) {
gpuBuffer.vf32 = new Float32Array(gpuBuffer.buffer as ArrayBuffer);
gpuBuffer.vf32 = new Float32Array(gpuBuffer.buffer.buffer);
}
} else if ((gpuBuffer.usage & GFXBufferUsageBit.INDIRECT) ||
(gpuBuffer.usage & GFXBufferUsageBit.TRANSFER_DST) ||
@@ -2640,7 +2640,7 @@ export function WebGLCmdFuncCopyTexImagesToTexture (
export function WebGLCmdFuncCopyBuffersToTexture (
device: WebGLGFXDevice,
buffers: ArrayBuffer[],
buffers: ArrayBufferView[],
gpuTexture: WebGLGPUTexture,
regions: GFXBufferTextureCopy[]) {
@@ -2668,7 +2668,7 @@ export function WebGLCmdFuncCopyBuffersToTexture (
// console.debug('Copying buffer to texture 2D: ' + w + ' x ' + h);
for (m = region.texSubres.baseMipLevel; m < region.texSubres.levelCount; ++m) {
const pixels = (fmtInfo.type === GFXFormatType.FLOAT && !isCompressed ? new Float32Array(buffers[n++]) : new Uint8Array(buffers[n++]));
const pixels = buffers[n++];
if (!isCompressed) {
gl.texSubImage2D(gl.TEXTURE_2D, m,
region.texOffset.x, region.texOffset.y, w, h,
@@ -2703,7 +2703,7 @@ export function WebGLCmdFuncCopyBuffersToTexture (
const mcount = region.texSubres.baseMipLevel + region.texSubres.levelCount;
for (m = region.texSubres.baseMipLevel; m < mcount; ++m) {
const pixels = (fmtInfo.type === GFXFormatType.FLOAT && !isCompressed ? new Float32Array(buffers[n++]) : new Uint8Array(buffers[n++]));
const pixels = buffers[n++];
if (!isCompressed) {
gl.texSubImage2D(gl.TEXTURE_CUBE_MAP_POSITIVE_X + f, m,
region.texOffset.x, region.texOffset.y, w, h,
+1 -1
View File
@@ -620,7 +620,7 @@ export class WebGLGFXDevice extends GFXDevice {
queue.clear();
}
public copyBuffersToTexture (buffers: ArrayBuffer[], texture: GFXTexture, regions: GFXBufferTextureCopy[]) {
public copyBuffersToTexture (buffers: ArrayBufferView[], texture: GFXTexture, regions: GFXBufferTextureCopy[]) {
WebGLCmdFuncCopyBuffersToTexture(
this,
buffers,
+1 -1
View File
@@ -39,7 +39,7 @@ export class WebGLGPUBuffer {
public glTarget: GLenum = 0;
public glBuffer: WebGLBuffer | null = null;
public buffer: ArrayBuffer | null = null;
public buffer: ArrayBufferView | null = null;
public vf32: Float32Array | null = null;
public indirects: IGFXDrawInfo[] = [];
}
+1 -1
View File
@@ -85,7 +85,7 @@ export class WebGL2GFXBuffer extends GFXBuffer {
this._bufferView = new Uint8Array(this._size);
this._bufferView.set(oldView);
if (this._gpuBuffer) {
this._gpuBuffer.buffer = this._bufferView.buffer;
this._gpuBuffer.buffer = this._bufferView;
}
}
+3 -3
View File
@@ -2645,7 +2645,7 @@ export function WebGL2CmdFuncCopyTexImagesToTexture (
export function WebGL2CmdFuncCopyBuffersToTexture (
device: WebGL2GFXDevice,
buffers: ArrayBuffer[],
buffers: ArrayBufferView[],
gpuTexture: WebGL2GPUTexture,
regions: GFXBufferTextureCopy[]) {
@@ -2670,7 +2670,7 @@ export function WebGL2CmdFuncCopyBuffersToTexture (
w = region.texExtent.width;
h = region.texExtent.height;
for (m = region.texSubres.baseMipLevel; m < region.texSubres.levelCount; ++m) {
const pixels = (fmtInfo.type === GFXFormatType.FLOAT && !isCompressed ? new Float32Array(buffers[n++]) : new Uint8Array(buffers[n++]));
const pixels = buffers[n++];
if (!isCompressed) {
gl.texSubImage2D(gl.TEXTURE_2D, m,
region.texOffset.x, region.texOffset.y, w, h,
@@ -2702,7 +2702,7 @@ export function WebGL2CmdFuncCopyBuffersToTexture (
const mcount = region.texSubres.baseMipLevel + region.texSubres.levelCount;
for (m = region.texSubres.baseMipLevel; m < mcount; ++m) {
const pixels = (fmtInfo.type === GFXFormatType.FLOAT && !isCompressed ? new Float32Array(buffers[n++]) : new Uint8Array(buffers[n++]));
const pixels = buffers[n++];
if (!isCompressed) {
gl.texSubImage2D(gl.TEXTURE_CUBE_MAP_POSITIVE_X + f, m,
+1 -1
View File
@@ -529,7 +529,7 @@ export class WebGL2GFXDevice extends GFXDevice {
queue.clear();
}
public copyBuffersToTexture (buffers: ArrayBuffer[], texture: GFXTexture, regions: GFXBufferTextureCopy[]) {
public copyBuffersToTexture (buffers: ArrayBufferView[], texture: GFXTexture, regions: GFXBufferTextureCopy[]) {
WebGL2CmdFuncCopyBuffersToTexture(
this,
buffers,
+1 -1
View File
@@ -41,7 +41,7 @@ export class WebGL2GPUBuffer {
public glTarget: GLenum = 0;
public glBuffer: WebGLBuffer | null = null;
public buffer: ArrayBuffer | null = null;
public buffer: ArrayBufferView | null = null;
public vf32: Float32Array | null = null;
public indirects: IGFXDrawInfo[] = [];
}
+1 -1
View File
@@ -49,7 +49,7 @@ import { Root } from '../../root';
import { murmurhash2_32_gc } from '../../utils/murmurhash2_gc';
import { customizeType, getBindingFromHandle, getBindingTypeFromHandle,
getOffsetFromHandle, getTypeFromHandle, type2default, type2reader, type2writer } from './pass-utils';
import { IProgramInfo, programLib, IShaderResources } from './program-lib';
import { IProgramInfo, IShaderResources, programLib } from './program-lib';
import { samplerLib } from './sampler-lib';
export interface IDefineMap { [name: string]: number | boolean | string; }
+16 -12
View File
@@ -2,7 +2,7 @@
* @hidden
*/
import { GFXBufferTextureCopy, GFXFormat, GFXFormatInfos, GFXTextureType, GFXTextureUsageBit, GFXTextureViewType } from '../../gfx/define';
import { GFXBufferTextureCopy, GFXFormat, GFXFormatInfos, GFXFormatType, GFXTextureType, GFXTextureUsageBit, GFXTextureViewType } from '../../gfx/define';
import { GFXDevice } from '../../gfx/device';
import { GFXTexture } from '../../gfx/texture';
import { GFXTextureView } from '../../gfx/texture-view';
@@ -44,15 +44,17 @@ export interface ITextureBufferPoolInfo {
export class TextureBufferPool {
private _device: GFXDevice;
private _format: GFXFormat = GFXFormat.UNKNOWN;
private _formatSize: number = 0;
private _format = GFXFormat.UNKNOWN;
private _formatSize = 0;
private _chunks: ITextureBuffer[] = [];
private _chunkCount = 0;
private _handles: ITextureBufferHandle[] = [];
private _region0: GFXBufferTextureCopy = new GFXBufferTextureCopy();
private _region1: GFXBufferTextureCopy = new GFXBufferTextureCopy();
private _region2: GFXBufferTextureCopy = new GFXBufferTextureCopy();
private _region0 = new GFXBufferTextureCopy();
private _region1 = new GFXBufferTextureCopy();
private _region2 = new GFXBufferTextureCopy();
private _roundUpFn: ((targetSize: number, formatSize: number) => number) | null = null;
private _bufferViewCtor: Uint8ArrayConstructor | Float32ArrayConstructor = Uint8Array;
private _channels = 4;
private _inOrderFree = false;
public constructor (device: GFXDevice) {
@@ -60,9 +62,11 @@ export class TextureBufferPool {
}
public initialize (info: ITextureBufferPoolInfo): boolean {
const formatInfo = GFXFormatInfos[info.format];
this._format = info.format;
this._formatSize = GFXFormatInfos[this._format].size;
this._formatSize = formatInfo.size;
this._channels = formatInfo.count;
this._bufferViewCtor = formatInfo.type === GFXFormatType.FLOAT ? Float32Array : Uint8Array;
this._chunks = new Array(info.maxChunks);
this._roundUpFn = info.roundUpFn || null;
this._inOrderFree = info.inOrderFree || false;
@@ -197,7 +201,7 @@ export class TextureBufferPool {
public update (handle: ITextureBufferHandle, buffer: ArrayBuffer) {
const buffers: ArrayBuffer[] = [];
const buffers: ArrayBufferView[] = [];
const regions: GFXBufferTextureCopy[] = [];
const start = handle.start / this._formatSize;
@@ -213,7 +217,7 @@ export class TextureBufferPool {
this._region0.texExtent.width = copySize;
this._region0.texExtent.height = 1;
buffers.push(buffer.slice(begin * this._formatSize, (begin + copySize) * this._formatSize));
buffers.push(new this._bufferViewCtor(buffer, begin * this._formatSize, copySize * this._channels));
regions.push(this._region0);
offsetX = 0;
@@ -236,7 +240,7 @@ export class TextureBufferPool {
this._region1.texExtent.height = 1;
}
buffers.push(buffer.slice(begin * this._formatSize, (begin + copySize) * this._formatSize));
buffers.push(new this._bufferViewCtor(buffer, begin * this._formatSize, copySize * this._channels));
regions.push(this._region1);
offsetX = 0;
@@ -251,7 +255,7 @@ export class TextureBufferPool {
this._region2.texExtent.width = remainSize;
this._region2.texExtent.height = 1;
buffers.push(buffer.slice(begin * this._formatSize, (begin + remainSize) * this._formatSize));
buffers.push(new this._bufferViewCtor(buffer, begin * this._formatSize, remainSize * this._channels));
regions.push(this._region2);
}
+1 -1
View File
@@ -559,7 +559,7 @@ export class TerrainBlock {
weightIndex += 1;
}
}
this._weightMap.uploadData(weightData.buffer);
this._weightMap.uploadData(weightData);
}
public _updateLightmap(tex: Texture2D|null, uoff: number, voff: number, uscale: number, vscale: number) {