mirror of
https://github.com/cocos/cocos4.git
synced 2026-11-19 14:52:40 +08:00
fix(webgl): sync buffer size with actual GL BUFFER_SIZE on resize (#389)
* fix(webgl): sync buffer size with actual GL BUFFER_SIZE on resize * fix(webgl): move buffer update size check to CmdFuncUpdateBuffer * fix(webgl): add buffer resize failure warning and fallback strategy * fix(2d): guard MeshBuffer upload and IA draw when buffer resize fails * fix(gfx): update EmptyBuffer size on resize for unit tests --------- Co-authored-by: vanitynie <vanitynie@qq.com>
This commit is contained in:
@@ -395,24 +395,59 @@ export class MeshBuffer {
|
||||
}
|
||||
const byteCount = this.byteOffset;
|
||||
const indexCount = this.indexOffset;
|
||||
const indexBytes = indexCount * 2;
|
||||
let allUploaded = true;
|
||||
for (let i = 0; i < submitCount; ++i) {
|
||||
const iaRef = this._iaPool[i];
|
||||
|
||||
const verticesData = new Float32Array(this.vData.buffer, 0, byteCount >> 2);
|
||||
let verticesData = new Float32Array(this.vData.buffer, 0, byteCount >> 2);
|
||||
const indicesData = new Uint16Array(this.iData.buffer, 0, indexCount);
|
||||
|
||||
let requiredVertexBytes = byteCount;
|
||||
const vertexBuffer = iaRef.vertexBuffers[0];
|
||||
if (byteCount > vertexBuffer.size) {
|
||||
vertexBuffer.resize(byteCount);
|
||||
if (byteCount > vertexBuffer.size && indexCount > 0) {
|
||||
let maxVertex = 0;
|
||||
for (let j = 0; j < indexCount; ++j) {
|
||||
if (indicesData[j] > maxVertex) {
|
||||
maxVertex = indicesData[j];
|
||||
}
|
||||
}
|
||||
const actualByteCount = (maxVertex + 1) * this._vertexFormatBytes;
|
||||
if (actualByteCount < byteCount) {
|
||||
requiredVertexBytes = actualByteCount;
|
||||
if (actualByteCount > vertexBuffer.size) {
|
||||
vertexBuffer.resize(actualByteCount);
|
||||
}
|
||||
verticesData = new Float32Array(this.vData.buffer, 0, actualByteCount >> 2);
|
||||
}
|
||||
}
|
||||
}
|
||||
vertexBuffer.update(verticesData);
|
||||
|
||||
if (indexCount * 2 > iaRef.indexBuffer.size) {
|
||||
iaRef.indexBuffer.resize(indexCount * 2);
|
||||
const indexBuffer = iaRef.indexBuffer;
|
||||
if (indexBytes > indexBuffer.size) {
|
||||
indexBuffer.resize(indexBytes);
|
||||
}
|
||||
iaRef.indexBuffer.update(indicesData);
|
||||
|
||||
if (vertexBuffer.size < requiredVertexBytes || indexBuffer.size < indexBytes) {
|
||||
allUploaded = false;
|
||||
if (iOS14) {
|
||||
iaRef.ia.indexCount = 0;
|
||||
} else {
|
||||
for (let k = 0; k < this._nextFreeIAHandle; ++k) {
|
||||
this._iaPool[k].ia.indexCount = 0;
|
||||
}
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
vertexBuffer.update(verticesData);
|
||||
indexBuffer.update(indicesData);
|
||||
}
|
||||
if (allUploaded) {
|
||||
this.dirty = false;
|
||||
}
|
||||
this.dirty = false;
|
||||
}
|
||||
|
||||
private createNewIA (device: Device): IIARef {
|
||||
|
||||
@@ -47,6 +47,9 @@ export class EmptyBuffer extends Buffer {
|
||||
}
|
||||
}
|
||||
public destroy (): void {}
|
||||
public resize (size: number): void {}
|
||||
public resize (size: number): void {
|
||||
this._size = size;
|
||||
this._count = this._size / this._stride;
|
||||
}
|
||||
public update (buffer: Readonly<BufferSource>, size?: number): void {}
|
||||
}
|
||||
|
||||
@@ -33,7 +33,7 @@ import {
|
||||
} from './webgl-commands';
|
||||
import { IWebGLGPUBuffer, IWebGLGPUBufferView, WebGLIndirectDrawInfos } from './webgl-gpu-objects';
|
||||
import { WebGLDeviceManager } from './webgl-define';
|
||||
import { warnID } from '../../core/platform/debug';
|
||||
import { warn, warnID } from '../../core/platform/debug';
|
||||
|
||||
/** @mangle */
|
||||
export class WebGLBuffer extends Buffer {
|
||||
@@ -48,6 +48,7 @@ export class WebGLBuffer extends Buffer {
|
||||
private _gpuBuffer: IWebGLGPUBuffer | null = null;
|
||||
private _gpuBufferView: IWebGLGPUBufferView | null = null;
|
||||
private _uniformBuffer: Uint8Array | null = null;
|
||||
private _lastFailedSize = 0;
|
||||
|
||||
constructor () {
|
||||
super();
|
||||
@@ -125,9 +126,6 @@ export class WebGLBuffer extends Buffer {
|
||||
const oldSize = this._size;
|
||||
if (oldSize === size) { return; }
|
||||
|
||||
this._size = size;
|
||||
this._count = this._size / this._stride;
|
||||
|
||||
if (this._uniformBuffer) {
|
||||
this._uniformBuffer = new Uint8Array(size);
|
||||
}
|
||||
@@ -140,8 +138,18 @@ export class WebGLBuffer extends Buffer {
|
||||
this._gpuBuffer.size = size;
|
||||
if (size > 0) {
|
||||
WebGLCmdFuncResizeBuffer(WebGLDeviceManager.instance, this._gpuBuffer);
|
||||
WebGLDeviceManager.instance.memoryStatus.bufferSize -= oldSize;
|
||||
WebGLDeviceManager.instance.memoryStatus.bufferSize += size;
|
||||
}
|
||||
this._size = this._gpuBuffer.size;
|
||||
this._count = this._size / this._stride;
|
||||
WebGLDeviceManager.instance.memoryStatus.bufferSize -= oldSize;
|
||||
WebGLDeviceManager.instance.memoryStatus.bufferSize += this._size;
|
||||
if (this._size !== size) {
|
||||
if (this._lastFailedSize !== size) {
|
||||
this._lastFailedSize = size;
|
||||
warn(`WebGLBuffer[${this.objectID}] (usage: ${this._usage}) resize failed to reach requested size: requested ${size} bytes, actual ${this._size} bytes.`);
|
||||
}
|
||||
} else {
|
||||
this._lastFailedSize = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -574,6 +574,8 @@ export function WebGLCmdFuncResizeBuffer (device: WebGLDevice, gpuBuffer: IWebGL
|
||||
} else {
|
||||
gl.bufferData(WebGLConstants.ARRAY_BUFFER, gpuBuffer.size, glUsage);
|
||||
}
|
||||
gpuBuffer.size = gl.getBufferParameter(WebGLConstants.ARRAY_BUFFER, WebGLConstants.BUFFER_SIZE) as number;
|
||||
|
||||
gl.bindBuffer(WebGLConstants.ARRAY_BUFFER, null);
|
||||
stateCache.glArrayBuffer = null;
|
||||
} else if (gpuBuffer.usage & BufferUsageBit.INDEX) {
|
||||
@@ -594,6 +596,8 @@ export function WebGLCmdFuncResizeBuffer (device: WebGLDevice, gpuBuffer: IWebGL
|
||||
} else {
|
||||
gl.bufferData(WebGLConstants.ELEMENT_ARRAY_BUFFER, gpuBuffer.size, glUsage);
|
||||
}
|
||||
gpuBuffer.size = gl.getBufferParameter(WebGLConstants.ELEMENT_ARRAY_BUFFER, WebGLConstants.BUFFER_SIZE) as number;
|
||||
|
||||
gl.bindBuffer(WebGLConstants.ELEMENT_ARRAY_BUFFER, null);
|
||||
stateCache.glElementArrayBuffer = null;
|
||||
} else if (gpuBuffer.usage & BufferUsageBit.UNIFORM) {
|
||||
@@ -630,6 +634,7 @@ export function WebGLCmdFuncUpdateBuffer (
|
||||
gpuBuffer.indirects.setDrawInfo(offset + i, drawInfos[i]);
|
||||
}
|
||||
} else {
|
||||
if (size > gpuBuffer.size) { return; }
|
||||
const buff = buffer as ArrayBuffer;
|
||||
const { gl, stateCache } = device;
|
||||
|
||||
|
||||
@@ -22,7 +22,7 @@
|
||||
THE SOFTWARE.
|
||||
*/
|
||||
|
||||
import { warnID } from '../../core/platform/debug';
|
||||
import { warn, warnID } from '../../core/platform/debug';
|
||||
import { Buffer } from '../base/buffer';
|
||||
import { BufferUsageBit, BufferSource, BufferInfo, BufferViewInfo } from '../base/define';
|
||||
import {
|
||||
@@ -45,6 +45,7 @@ export class WebGL2Buffer extends Buffer {
|
||||
}
|
||||
|
||||
private _gpuBuffer: IWebGL2GPUBuffer | null = null;
|
||||
private _lastFailedSize = 0;
|
||||
|
||||
public initialize (info: Readonly<BufferInfo> | Readonly<BufferViewInfo>): void {
|
||||
if ('buffer' in info) { // buffer view
|
||||
@@ -114,15 +115,22 @@ export class WebGL2Buffer extends Buffer {
|
||||
const oldSize = this._size;
|
||||
if (oldSize === size) { return; }
|
||||
|
||||
this._size = size;
|
||||
this._count = this._size / this._stride;
|
||||
|
||||
if (this._gpuBuffer) {
|
||||
this._gpuBuffer.size = size;
|
||||
if (size > 0) {
|
||||
WebGL2CmdFuncResizeBuffer(WebGL2DeviceManager.instance, this._gpuBuffer);
|
||||
WebGL2DeviceManager.instance.memoryStatus.bufferSize -= oldSize;
|
||||
WebGL2DeviceManager.instance.memoryStatus.bufferSize += size;
|
||||
}
|
||||
this._size = this._gpuBuffer.size;
|
||||
this._count = this._size / this._stride;
|
||||
WebGL2DeviceManager.instance.memoryStatus.bufferSize -= oldSize;
|
||||
WebGL2DeviceManager.instance.memoryStatus.bufferSize += this._size;
|
||||
if (this._size !== size) {
|
||||
if (this._lastFailedSize !== size) {
|
||||
this._lastFailedSize = size;
|
||||
warn(`WebGL2Buffer[${this.objectID}] (usage: ${this._usage}) resize failed to reach requested size: requested ${size} bytes, actual ${this._size} bytes.`);
|
||||
}
|
||||
} else {
|
||||
this._lastFailedSize = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -770,6 +770,8 @@ export function WebGL2CmdFuncResizeBuffer (device: WebGL2Device, gpuBuffer: IWeb
|
||||
} else {
|
||||
gl.bufferData(WebGLConstants.ARRAY_BUFFER, gpuBuffer.size, glUsage);
|
||||
}
|
||||
gpuBuffer.size = gl.getBufferParameter(WebGLConstants.ARRAY_BUFFER, WebGLConstants.BUFFER_SIZE) as number;
|
||||
|
||||
gl.bindBuffer(WebGLConstants.ARRAY_BUFFER, null);
|
||||
cache.glArrayBuffer = null;
|
||||
} else if (gpuBuffer.usage & BufferUsageBit.INDEX) {
|
||||
@@ -790,6 +792,8 @@ export function WebGL2CmdFuncResizeBuffer (device: WebGL2Device, gpuBuffer: IWeb
|
||||
} else {
|
||||
gl.bufferData(WebGLConstants.ELEMENT_ARRAY_BUFFER, gpuBuffer.size, glUsage);
|
||||
}
|
||||
gpuBuffer.size = gl.getBufferParameter(WebGLConstants.ELEMENT_ARRAY_BUFFER, WebGLConstants.BUFFER_SIZE) as number;
|
||||
|
||||
gl.bindBuffer(WebGLConstants.ELEMENT_ARRAY_BUFFER, null);
|
||||
cache.glElementArrayBuffer = null;
|
||||
} else if (gpuBuffer.usage & BufferUsageBit.UNIFORM) {
|
||||
@@ -798,6 +802,8 @@ export function WebGL2CmdFuncResizeBuffer (device: WebGL2Device, gpuBuffer: IWeb
|
||||
}
|
||||
|
||||
gl.bufferData(WebGLConstants.UNIFORM_BUFFER, gpuBuffer.size, glUsage);
|
||||
gpuBuffer.size = gl.getBufferParameter(WebGLConstants.UNIFORM_BUFFER, WebGLConstants.BUFFER_SIZE) as number;
|
||||
|
||||
gl.bindBuffer(WebGLConstants.UNIFORM_BUFFER, null);
|
||||
cache.glUniformBuffer = null;
|
||||
} else if ((gpuBuffer.usage & BufferUsageBit.INDIRECT)
|
||||
@@ -824,6 +830,7 @@ export function WebGL2CmdFuncUpdateBuffer (
|
||||
gpuBuffer.indirects.setDrawInfo(offset + i, drawInfos[i]);
|
||||
}
|
||||
} else {
|
||||
if (size > gpuBuffer.size) { return; }
|
||||
const buff = buffer as ArrayBuffer;
|
||||
const { gl } = device;
|
||||
const cache = device.getStateCache();
|
||||
|
||||
@@ -141,12 +141,27 @@ export class InstancedBuffer {
|
||||
|
||||
private _appendInstance (instance: IInstancedItem, data: Uint8Array, shader: Shader, descriptorSet: DescriptorSet): void {
|
||||
if (instance.count >= instance.capacity) { // resize buffers
|
||||
instance.capacity = Math.min(instance.capacity << 1, MAX_CAPACITY);
|
||||
const requiredCapacity = instance.count + 1;
|
||||
const targetCapacity = Math.min(Math.max(instance.capacity << 1, requiredCapacity), MAX_CAPACITY);
|
||||
const targetSize = instance.stride * targetCapacity;
|
||||
instance.vb.resize(targetSize);
|
||||
if (instance.vb.size < targetSize) {
|
||||
const requiredSize = instance.stride * requiredCapacity;
|
||||
if (instance.vb.size < requiredSize) {
|
||||
instance.vb.resize(requiredSize);
|
||||
}
|
||||
}
|
||||
instance.capacity = Math.floor(instance.vb.size / instance.stride);
|
||||
if (instance.count >= instance.capacity) {
|
||||
instance.count = instance.capacity;
|
||||
return;
|
||||
}
|
||||
const newSize = instance.stride * instance.capacity;
|
||||
const oldData = instance.data;
|
||||
instance.data = new Uint8Array(newSize);
|
||||
instance.data.set(oldData);
|
||||
instance.vb.resize(newSize);
|
||||
if (newSize > instance.data.byteLength) {
|
||||
const oldData = instance.data;
|
||||
instance.data = new Uint8Array(newSize);
|
||||
instance.data.set(oldData.subarray(0, instance.count * instance.stride));
|
||||
}
|
||||
}
|
||||
instance.shader = shader;
|
||||
instance.descriptorSet = descriptorSet;
|
||||
|
||||
Reference in New Issue
Block a user