mirror of
https://github.com/cocos/cocos4.git
synced 2026-12-02 08:42:43 +08:00
b162e21a93
* Update licenses * Update license for audio * Update license for core/3d * Update license for animation, assets, asset-manager, components, data * update license for event, geometry, gfx * update license for core modules math, memop, pipeline, platform, primitive, renderer, scene-graph, utils, value-types * Update license for core * Update license for particle and particle-2d * Update license for particle part2 * Update license for physics and physics-2d * Update license for all modules in cocos * Update license for exports * Fix API doc language declaration
150 lines
6.5 KiB
TypeScript
150 lines
6.5 KiB
TypeScript
/*
|
|
Copyright (c) 2020 Xiamen Yaji Software Co., Ltd.
|
|
|
|
https://www.cocos.com/
|
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
of this software and associated engine source code (the "Software"), a limited,
|
|
worldwide, royalty-free, non-assignable, revocable and non-exclusive license
|
|
to use Cocos Creator solely to develop games on your target platforms. You shall
|
|
not use Cocos Creator software for developing other software or tools that's
|
|
used for developing games. You are not granted to publish, distribute,
|
|
sublicense, and/or sell copies of Cocos Creator.
|
|
|
|
The software or tools in this License Agreement are licensed, not sold.
|
|
Xiamen Yaji Software Co., Ltd. reserves all rights not expressly granted to you.
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
THE SOFTWARE.
|
|
*/
|
|
|
|
import { Buffer, BufferSource, DrawInfo } from '../buffer';
|
|
import { CommandBuffer } from '../command-buffer';
|
|
import { BufferUsageBit } from '../define';
|
|
import { BufferTextureCopy, Color, Rect } from '../define-class';
|
|
import { Framebuffer } from '../framebuffer';
|
|
import { InputAssembler } from '../input-assembler';
|
|
import { Texture } from '../texture';
|
|
import { WebGLBuffer } from './webgl-buffer';
|
|
import { WebGLCommandBuffer } from './webgl-command-buffer';
|
|
import {
|
|
WebGLCmdFuncBeginRenderPass, WebGLCmdFuncBindStates, WebGLCmdFuncCopyBuffersToTexture,
|
|
WebGLCmdFuncDraw, WebGLCmdFuncExecuteCmds, WebGLCmdFuncUpdateBuffer } from './webgl-commands';
|
|
import { WebGLDevice } from './webgl-device';
|
|
import { WebGLFramebuffer } from './webgl-framebuffer';
|
|
import { WebGLTexture } from './webgl-texture';
|
|
import { RenderPass } from '../render-pass';
|
|
import { WebGLRenderPass } from './webgl-render-pass';
|
|
|
|
const _dynamicOffsets: number[] = [];
|
|
|
|
export class WebGLPrimaryCommandBuffer extends WebGLCommandBuffer {
|
|
|
|
public beginRenderPass (
|
|
renderPass: RenderPass,
|
|
framebuffer: Framebuffer,
|
|
renderArea: Rect,
|
|
clearColors: Color[],
|
|
clearDepth: number,
|
|
clearStencil: number) {
|
|
|
|
WebGLCmdFuncBeginRenderPass(
|
|
this._device as WebGLDevice,
|
|
(renderPass as WebGLRenderPass).gpuRenderPass,
|
|
(framebuffer as WebGLFramebuffer).gpuFramebuffer,
|
|
renderArea, clearColors, clearDepth, clearStencil);
|
|
this._isInRenderPass = true;
|
|
}
|
|
|
|
public draw (inputAssembler: InputAssembler) {
|
|
if (this._isInRenderPass) {
|
|
if (this._isStateInvalied) {
|
|
this.bindStates();
|
|
}
|
|
|
|
WebGLCmdFuncDraw(this._device as WebGLDevice, inputAssembler as unknown as DrawInfo);
|
|
|
|
++this._numDrawCalls;
|
|
this._numInstances += inputAssembler.instanceCount;
|
|
const indexCount = inputAssembler.indexCount || inputAssembler.vertexCount;
|
|
if (this._curGPUPipelineState) {
|
|
const glPrimitive = this._curGPUPipelineState.glPrimitive;
|
|
switch (glPrimitive) {
|
|
case 0x0004: { // WebGLRenderingContext.TRIANGLES
|
|
this._numTris += indexCount / 3 * Math.max(inputAssembler.instanceCount, 1);
|
|
break;
|
|
}
|
|
case 0x0005: // WebGLRenderingContext.TRIANGLE_STRIP
|
|
case 0x0006: { // WebGLRenderingContext.TRIANGLE_FAN
|
|
this._numTris += (indexCount - 2) * Math.max(inputAssembler.instanceCount, 1);
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
} else {
|
|
console.error('Command \'draw\' must be recorded inside a render pass.');
|
|
}
|
|
}
|
|
|
|
public updateBuffer (buffer: Buffer, data: BufferSource, offset?: number, size?: number) {
|
|
if (!this._isInRenderPass) {
|
|
const gpuBuffer = (buffer as WebGLBuffer).gpuBuffer;
|
|
if (gpuBuffer) {
|
|
if (offset === undefined) { offset = 0; }
|
|
|
|
let buffSize: number;
|
|
if (size !== undefined) {
|
|
buffSize = size;
|
|
} else if (buffer.usage & BufferUsageBit.INDIRECT) {
|
|
buffSize = 0;
|
|
} else {
|
|
buffSize = (data as ArrayBuffer).byteLength;
|
|
}
|
|
|
|
WebGLCmdFuncUpdateBuffer(this._device as WebGLDevice, gpuBuffer, data as ArrayBuffer, offset, buffSize);
|
|
}
|
|
} else {
|
|
console.error('Command \'updateBuffer\' must be recorded outside a render pass.');
|
|
}
|
|
}
|
|
|
|
public copyBuffersToTexture (buffers: ArrayBufferView[], texture: Texture, regions: BufferTextureCopy[]) {
|
|
if (!this._isInRenderPass) {
|
|
const gpuTexture = (texture as WebGLTexture).gpuTexture;
|
|
if (gpuTexture) {
|
|
WebGLCmdFuncCopyBuffersToTexture(this._device as WebGLDevice, buffers, gpuTexture, regions);
|
|
}
|
|
} else {
|
|
console.error('Command \'copyBufferToTexture\' must be recorded outside a render pass.');
|
|
}
|
|
}
|
|
|
|
public execute (cmdBuffs: CommandBuffer[], count: number) {
|
|
for (let i = 0; i < count; ++i) {
|
|
// actually they are secondary buffers, the cast here is only for type checking
|
|
const webGLCmdBuff = cmdBuffs[i] as WebGLPrimaryCommandBuffer;
|
|
WebGLCmdFuncExecuteCmds(this._device as WebGLDevice, webGLCmdBuff.cmdPackage);
|
|
this._numDrawCalls += webGLCmdBuff._numDrawCalls;
|
|
this._numInstances += webGLCmdBuff._numInstances;
|
|
this._numTris += webGLCmdBuff._numTris;
|
|
}
|
|
}
|
|
|
|
protected bindStates () {
|
|
_dynamicOffsets.length = 0;
|
|
for (let i = 0; i < this._curDynamicOffsets.length; i++) {
|
|
Array.prototype.push.apply(_dynamicOffsets, this._curDynamicOffsets[i]);
|
|
}
|
|
WebGLCmdFuncBindStates(this._device as WebGLDevice,
|
|
this._curGPUPipelineState, this._curGPUInputAssembler, this._curGPUDescriptorSets, _dynamicOffsets,
|
|
this._curViewport, this._curScissor, this._curLineWidth, this._curDepthBias, this._curBlendConstants,
|
|
this._curDepthBounds, this._curStencilWriteMask, this._curStencilCompareMask);
|
|
this._isStateInvalied = false;
|
|
}
|
|
}
|