mirror of
https://github.com/cocos/cocos4.git
synced 2026-12-02 00:22:43 +08:00
filter shader bindings wrt. macros (#5772)
* filter shader bindings wrt. macros * fix default joint texture size * empty scene opt
This commit is contained in:
File diff suppressed because one or more lines are too long
@@ -66,10 +66,13 @@ export class AnimationBlendState {
|
||||
}
|
||||
|
||||
public apply () {
|
||||
for (const targetState of this._blendTargets) {
|
||||
const targets = this._blendTargets;
|
||||
for (let i = 0; i < targets.length; i++) {
|
||||
const targetState = targets[i];
|
||||
const target = targetState.target;
|
||||
const propertyStates = targetState.properties;
|
||||
for (const p of propertyStates) {
|
||||
for (let j = 0; j < propertyStates.length; j++) {
|
||||
const p = propertyStates[j];
|
||||
if (p.weight !== 0) {
|
||||
if (target instanceof Node) {
|
||||
if (p.name === 'position') {
|
||||
@@ -88,7 +91,9 @@ export class AnimationBlendState {
|
||||
}
|
||||
|
||||
public clear () {
|
||||
for (const targetState of this._blendTargets) {
|
||||
const targets = this._blendTargets;
|
||||
for (let i = 0; i < targets.length; i++) {
|
||||
const targetState = targets[i];
|
||||
const propertyStates = targetState.properties;
|
||||
for (const p of propertyStates) {
|
||||
p.weight = 0;
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
|
||||
import { ccclass } from '../data/class-decorator';
|
||||
import { GFXBufferTextureCopy, GFXTextureFlagBit, GFXTextureUsageBit } from '../gfx/define';
|
||||
import { GFXDevice } from '../gfx/device';
|
||||
import { GFXAPI, GFXDevice } from '../gfx/device';
|
||||
import { GFXTexture, IGFXTextureInfo } from '../gfx/texture';
|
||||
import { GFXTextureView, IGFXTextureViewInfo } from '../gfx/texture-view';
|
||||
import { error } from '../platform/debug';
|
||||
@@ -46,6 +46,11 @@ function getMipLevel (width: number, height: number) {
|
||||
}
|
||||
|
||||
function isPOT (n: number) { return n && (n & (n - 1)) === 0; }
|
||||
function canGenerateMipmap (device: GFXDevice, w: number, h: number) {
|
||||
const needCheckPOT = device.gfxAPI === GFXAPI.WEBGL;
|
||||
if (needCheckPOT) { return isPOT(w) && isPOT(h); }
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 简单贴图基类。
|
||||
@@ -213,7 +218,7 @@ export class SimpleTexture extends TextureBase {
|
||||
|
||||
protected _createTexture (device: GFXDevice) {
|
||||
let flags = GFXTextureFlagBit.NONE;
|
||||
if (this._mipFilter !== Filter.NONE && isPOT(this._width) && isPOT(this._height)) {
|
||||
if (this._mipFilter !== Filter.NONE && canGenerateMipmap(device, this._width, this._height)) {
|
||||
this._mipmapLevel = getMipLevel(this._width, this._height);
|
||||
flags = GFXTextureFlagBit.GEN_MIPMAP;
|
||||
}
|
||||
|
||||
@@ -424,7 +424,7 @@ export class WebGLCmdBeginRenderPass extends WebGLCmdObject {
|
||||
|
||||
public clear () {
|
||||
this.gpuFramebuffer = null;
|
||||
this.clearColors = [];
|
||||
this.clearColors.length = 0;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -529,7 +529,7 @@ export class WebGLCmdCopyBufferToTexture extends WebGLCmdObject {
|
||||
this.gpuBuffer = null;
|
||||
this.gpuTexture = null;
|
||||
this.dstLayout = null;
|
||||
this.regions = [];
|
||||
this.regions.length = 0;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1138,7 +1138,8 @@ export function WebGLCmdFuncDestroyFramebuffer (device: WebGLGFXDevice, gpuFrame
|
||||
export function WebGLCmdFuncCreateShader (device: WebGLGFXDevice, gpuShader: WebGLGPUShader) {
|
||||
const gl = device.gl;
|
||||
|
||||
for (const gpuStage of gpuShader.gpuStages) {
|
||||
for (let k = 0; k < gpuShader.gpuStages.length; k++) {
|
||||
const gpuStage = gpuShader.gpuStages[k];
|
||||
|
||||
let glShaderType: GLenum = 0;
|
||||
let shaderTypeStr = '';
|
||||
@@ -1187,7 +1188,8 @@ export function WebGLCmdFuncCreateShader (device: WebGLGFXDevice, gpuShader: Web
|
||||
gpuShader.glProgram = glProgram;
|
||||
|
||||
// link program
|
||||
for (const gpuStage of gpuShader.gpuStages) {
|
||||
for (let k = 0; k < gpuShader.gpuStages.length; k++) {
|
||||
const gpuStage = gpuShader.gpuStages[k];
|
||||
gl.attachShader(gpuShader.glProgram, gpuStage.glShader!);
|
||||
}
|
||||
|
||||
@@ -1198,7 +1200,8 @@ export function WebGLCmdFuncCreateShader (device: WebGLGFXDevice, gpuShader: Web
|
||||
console.error('Failed to link shader \'' + gpuShader.name + '\'.');
|
||||
console.error(gl.getProgramInfoLog(gpuShader.glProgram));
|
||||
|
||||
for (const gpuStage of gpuShader.gpuStages) {
|
||||
for (let k = 0; k < gpuShader.gpuStages.length; k++) {
|
||||
const gpuStage = gpuShader.gpuStages[k];
|
||||
if (gpuStage.glShader) {
|
||||
gl.deleteShader(gpuStage.glShader);
|
||||
gpuStage.glShader = null;
|
||||
@@ -1288,7 +1291,8 @@ export function WebGLCmdFuncCreateShader (device: WebGLGFXDevice, gpuShader: Web
|
||||
/*
|
||||
glBlock.buffer = new ArrayBuffer(glBlock.size);
|
||||
|
||||
for (const glUniform of glBlock.glUniforms) {
|
||||
for (let k = 0; k < glBlock.glUniforms.length; k++) {
|
||||
const glUniform = glBlock.glUniforms[k];
|
||||
switch (glUniform.glType) {
|
||||
case gl.BOOL:
|
||||
case gl.BOOL_VEC2:
|
||||
@@ -1355,9 +1359,11 @@ export function WebGLCmdFuncCreateShader (device: WebGLGFXDevice, gpuShader: Web
|
||||
// let stride = WebGLGetTypeSize(info.type);
|
||||
|
||||
// build uniform block mapping
|
||||
for (const glBlock of gpuShader.glBlocks) {
|
||||
for (let j = 0; j < gpuShader.glBlocks.length; j++) {
|
||||
const glBlock = gpuShader.glBlocks[j];
|
||||
|
||||
for (const glUniform of glBlock.glUniforms) {
|
||||
for (let k = 0; k < glBlock.glUniforms.length; k++) {
|
||||
const glUniform = glBlock.glUniforms[k];
|
||||
if (glUniform.name === varName) {
|
||||
// let varSize = stride * info.size;
|
||||
|
||||
@@ -1370,7 +1376,8 @@ export function WebGLCmdFuncCreateShader (device: WebGLGFXDevice, gpuShader: Web
|
||||
} // for
|
||||
} else {
|
||||
|
||||
for (const glSampler of gpuShader.glSamplers) {
|
||||
for (let j = 0; j < gpuShader.glSamplers.length; j++) {
|
||||
const glSampler = gpuShader.glSamplers[j];
|
||||
if (glSampler.name === varName) {
|
||||
// let varSize = stride * uniformInfo.size;
|
||||
|
||||
@@ -1397,7 +1404,8 @@ export function WebGLCmdFuncCreateShader (device: WebGLGFXDevice, gpuShader: Web
|
||||
device.stateCache.glProgram = gpuShader.glProgram;
|
||||
}
|
||||
|
||||
for (const glSampler of glActiveSamplers) {
|
||||
for (let i = 0; i < glActiveSamplers.length; i++) {
|
||||
const glSampler = glActiveSamplers[i];
|
||||
gl.uniform1iv(glSampler.glLoc, glSampler.units);
|
||||
}
|
||||
}
|
||||
@@ -1405,7 +1413,8 @@ export function WebGLCmdFuncCreateShader (device: WebGLGFXDevice, gpuShader: Web
|
||||
|
||||
export function WebGLCmdFuncDestroyShader (device: WebGLGFXDevice, gpuShader: WebGLGPUShader) {
|
||||
|
||||
for (const gpuStage of gpuShader.gpuStages) {
|
||||
for (let i = 0; i < gpuShader.gpuStages.length; i++) {
|
||||
const gpuStage = gpuShader.gpuStages[i];
|
||||
if (gpuStage.glShader) {
|
||||
device.gl.deleteShader(gpuStage.glShader);
|
||||
gpuStage.glShader = null;
|
||||
@@ -1454,8 +1463,11 @@ export function WebGLCmdFuncCreateInputAssember (device: WebGLGFXDevice, gpuInpu
|
||||
}
|
||||
|
||||
export function WebGLCmdFuncDestroyInputAssembler (device: WebGLGFXDevice, gpuInputAssembler: IWebGLGPUInputAssembler) {
|
||||
for (const vao of gpuInputAssembler.glVAOs) {
|
||||
device.OES_vertex_array_object!.deleteVertexArrayOES(vao[1]);
|
||||
const it = gpuInputAssembler.glVAOs.values();
|
||||
let res = it.next();
|
||||
while (!res.done) {
|
||||
device.OES_vertex_array_object!.deleteVertexArrayOES(res.value);
|
||||
res = it.next();
|
||||
}
|
||||
gpuInputAssembler.glVAOs.clear();
|
||||
}
|
||||
@@ -2558,7 +2570,8 @@ export function WebGLCmdFuncCopyTexImagesToTexture (
|
||||
|
||||
switch (gpuTexture.glTarget) {
|
||||
case gl.TEXTURE_2D: {
|
||||
for (const region of regions) {
|
||||
for (let i = 0; i < regions.length; i++) {
|
||||
const region = regions[i];
|
||||
// console.debug('Copying image to texture 2D: ' + region.texExtent.width + ' x ' + region.texExtent.height);
|
||||
for (m = region.texSubres.baseMipLevel; m < region.texSubres.levelCount; ++m) {
|
||||
gl.texSubImage2D(gl.TEXTURE_2D, m,
|
||||
@@ -2569,7 +2582,8 @@ export function WebGLCmdFuncCopyTexImagesToTexture (
|
||||
break;
|
||||
}
|
||||
case gl.TEXTURE_CUBE_MAP: {
|
||||
for (const region of regions) {
|
||||
for (let i = 0; i < regions.length; i++) {
|
||||
const region = regions[i];
|
||||
// console.debug('Copying image to texture cube: ' + region.texExtent.width + ' x ' + region.texExtent.height);
|
||||
const fcount = region.texSubres.baseArrayLayer + region.texSubres.layerCount;
|
||||
for (f = region.texSubres.baseArrayLayer; f < fcount; ++f) {
|
||||
@@ -2617,7 +2631,8 @@ export function WebGLCmdFuncCopyBuffersToTexture (
|
||||
const isCompressed = fmtInfo.isCompressed;
|
||||
switch (gpuTexture.glTarget) {
|
||||
case gl.TEXTURE_2D: {
|
||||
for (const region of regions) {
|
||||
for (let i = 0; i < regions.length; i++) {
|
||||
const region = regions[i];
|
||||
w = region.texExtent.width;
|
||||
h = region.texExtent.height;
|
||||
// console.debug('Copying buffer to texture 2D: ' + w + ' x ' + h);
|
||||
@@ -2647,7 +2662,8 @@ export function WebGLCmdFuncCopyBuffersToTexture (
|
||||
break;
|
||||
}
|
||||
case gl.TEXTURE_CUBE_MAP: {
|
||||
for (const region of regions) {
|
||||
for (let i = 0; i < regions.length; i++) {
|
||||
const region = regions[i];
|
||||
n = 0;
|
||||
const fcount = region.texSubres.baseArrayLayer + region.texSubres.layerCount;
|
||||
for (f = region.texSubres.baseArrayLayer; f < fcount; ++f) {
|
||||
|
||||
@@ -575,7 +575,7 @@ export class WebGL2CmdBeginRenderPass extends WebGL2CmdObject {
|
||||
|
||||
public clear () {
|
||||
this.gpuFramebuffer = null;
|
||||
this.clearColors = [];
|
||||
this.clearColors.length = 0;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -679,7 +679,7 @@ export class WebGL2CmdCopyBufferToTexture extends WebGL2CmdObject {
|
||||
this.gpuBuffer = null;
|
||||
this.gpuTexture = null;
|
||||
this.dstLayout = null;
|
||||
this.regions = [];
|
||||
this.regions.length = 0;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -928,18 +928,11 @@ export function WebGL2CmdFuncUpdateBuffer (device: WebGL2GFXDevice, gpuBuffer: W
|
||||
cache.glUniformBuffer = gpuBuffer.glBuffer;
|
||||
}
|
||||
|
||||
let buf: Float32Array;
|
||||
if (buffer instanceof Float32Array) {
|
||||
buf = buffer;
|
||||
} else {
|
||||
buf = new Float32Array(buff, 0, size / 4);
|
||||
}
|
||||
|
||||
if (size === buf.byteLength) {
|
||||
gl.bufferSubData(gpuBuffer.glTarget, offset, buf);
|
||||
if (size === buff.byteLength) {
|
||||
gl.bufferSubData(gpuBuffer.glTarget, offset, buff);
|
||||
// if (gl.getBufferParameter(gl.UNIFORM_BUFFER, gl.BUFFER_SIZE) !== buff.length * 4) { debugger; }
|
||||
} else {
|
||||
gl.bufferSubData(gpuBuffer.glTarget, offset, new Float32Array(buf.buffer, 0, size / 4));
|
||||
gl.bufferSubData(gpuBuffer.glTarget, offset, new Float32Array(buff, 0, size / 4));
|
||||
}
|
||||
break;
|
||||
}
|
||||
@@ -1387,7 +1380,8 @@ export function WebGL2CmdFuncDestroyFramebuffer (device: WebGL2GFXDevice, gpuFra
|
||||
export function WebGL2CmdFuncCreateShader (device: WebGL2GFXDevice, gpuShader: WebGL2GPUShader) {
|
||||
const gl = device.gl;
|
||||
|
||||
for (const gpuStage of gpuShader.gpuStages) {
|
||||
for (let k = 0; k < gpuShader.gpuStages.length; k++) {
|
||||
const gpuStage = gpuShader.gpuStages[k];
|
||||
|
||||
let glShaderType: GLenum = 0;
|
||||
let shaderTypeStr = '';
|
||||
@@ -1436,7 +1430,8 @@ export function WebGL2CmdFuncCreateShader (device: WebGL2GFXDevice, gpuShader: W
|
||||
gpuShader.glProgram = glProgram;
|
||||
|
||||
// link program
|
||||
for (const gpuStage of gpuShader.gpuStages) {
|
||||
for (let k = 0; k < gpuShader.gpuStages.length; k++) {
|
||||
const gpuStage = gpuShader.gpuStages[k];
|
||||
gl.attachShader(gpuShader.glProgram, gpuStage.glShader!);
|
||||
}
|
||||
|
||||
@@ -1447,7 +1442,8 @@ export function WebGL2CmdFuncCreateShader (device: WebGL2GFXDevice, gpuShader: W
|
||||
console.error('Failed to link shader \'' + gpuShader.name + '\'.');
|
||||
console.error(gl.getProgramInfoLog(gpuShader.glProgram));
|
||||
|
||||
for (const gpuStage of gpuShader.gpuStages) {
|
||||
for (let k = 0; k < gpuShader.gpuStages.length; k++) {
|
||||
const gpuStage = gpuShader.gpuStages[k];
|
||||
if (gpuStage.glShader) {
|
||||
gl.deleteShader(gpuStage.glShader);
|
||||
gpuStage.glShader = null;
|
||||
@@ -1518,7 +1514,8 @@ export function WebGL2CmdFuncCreateShader (device: WebGL2GFXDevice, gpuShader: W
|
||||
|
||||
// blockIdx = gl.getUniformBlockIndex(gpuShader.glProgram, blockName);
|
||||
blockBinding = -1;
|
||||
for (const block of gpuShader.blocks) {
|
||||
for (let k = 0; k < gpuShader.blocks.length; k++) {
|
||||
const block = gpuShader.blocks[k];
|
||||
if (block.name === blockName) {
|
||||
blockBinding = block.binding;
|
||||
break;
|
||||
@@ -1625,7 +1622,8 @@ export function WebGL2CmdFuncCreateShader (device: WebGL2GFXDevice, gpuShader: W
|
||||
(uniformInfo.type === gl.SAMPLER_CUBE);
|
||||
|
||||
if (isSampler) {
|
||||
for (const glSampler of gpuShader.glSamplers) {
|
||||
for (let k = 0; k < gpuShader.glSamplers.length; k++) {
|
||||
const glSampler = gpuShader.glSamplers[k];
|
||||
if (glSampler.name === varName) {
|
||||
// let varSize = stride * uniformInfo.size;
|
||||
|
||||
@@ -1652,7 +1650,8 @@ export function WebGL2CmdFuncCreateShader (device: WebGL2GFXDevice, gpuShader: W
|
||||
device.stateCache.glProgram = gpuShader.glProgram;
|
||||
}
|
||||
|
||||
for (const glSampler of glActiveSamplers) {
|
||||
for (let k = 0; k < glActiveSamplers.length; k++) {
|
||||
const glSampler = glActiveSamplers[k];
|
||||
gl.uniform1iv(glSampler.glLoc, glSampler.units);
|
||||
}
|
||||
}
|
||||
@@ -1660,7 +1659,8 @@ export function WebGL2CmdFuncCreateShader (device: WebGL2GFXDevice, gpuShader: W
|
||||
|
||||
export function WebGL2CmdFuncDestroyShader (device: WebGL2GFXDevice, gpuShader: WebGL2GPUShader) {
|
||||
|
||||
for (const gpuStage of gpuShader.gpuStages) {
|
||||
for (let k = 0; k < gpuShader.gpuStages.length; k++) {
|
||||
const gpuStage = gpuShader.gpuStages[k];
|
||||
if (gpuStage.glShader) {
|
||||
device.gl.deleteShader(gpuStage.glShader);
|
||||
gpuStage.glShader = null;
|
||||
@@ -1710,8 +1710,11 @@ export function WebGL2CmdFuncCreateInputAssember (device: WebGL2GFXDevice, gpuIn
|
||||
}
|
||||
|
||||
export function WebGL2CmdFuncDestroyInputAssembler (device: WebGL2GFXDevice, gpuInputAssembler: IWebGL2GPUInputAssembler) {
|
||||
for (const vao of gpuInputAssembler.glVAOs) {
|
||||
device.gl.deleteVertexArray(vao[1]);
|
||||
const it = gpuInputAssembler.glVAOs.values();
|
||||
let res = it.next();
|
||||
while (!res.done) {
|
||||
device.gl.deleteVertexArray(res.value);
|
||||
res = it.next();
|
||||
}
|
||||
gpuInputAssembler.glVAOs.clear();
|
||||
}
|
||||
@@ -1906,7 +1909,7 @@ export function WebGL2CmdFuncExecuteCmds (device: WebGL2GFXDevice, cmdPackage: W
|
||||
break;
|
||||
}
|
||||
case WebGL2Cmd.END_RENDER_PASS: {
|
||||
// WebGL 1.0 doesn't support store operation of attachments.
|
||||
// WebGL 2.0 doesn't support store operation of attachments.
|
||||
// GFXStoreOp.Store is the default GL behaviour.
|
||||
break;
|
||||
}
|
||||
@@ -2160,13 +2163,16 @@ export function WebGL2CmdFuncExecuteCmds (device: WebGL2GFXDevice, cmdPackage: W
|
||||
} // bind pso
|
||||
|
||||
if (cmd2.gpuBindingLayout && gpuShader) {
|
||||
for (const gpuBinding of cmd2.gpuBindingLayout.gpuBindings) {
|
||||
const gpuBindings = cmd2.gpuBindingLayout.gpuBindings;
|
||||
for (let j = 0; j < gpuBindings.length; j++) {
|
||||
const gpuBinding = gpuBindings[j];
|
||||
|
||||
switch (gpuBinding.type) {
|
||||
case GFXBindingType.UNIFORM_BUFFER: {
|
||||
|
||||
if (gpuBinding.gpuBuffer) {
|
||||
for (const glBlock of gpuShader.glBlocks) {
|
||||
for (let k = 0; k < gpuShader.glBlocks.length; k++) {
|
||||
const glBlock = gpuShader.glBlocks[k];
|
||||
if (glBlock.binding === gpuBinding.binding) {
|
||||
if (cache.glBindUBOs[glBlock.binding] !== gpuBinding.gpuBuffer.glBuffer) {
|
||||
gl.bindBufferBase(gl.UNIFORM_BUFFER, glBlock.binding, gpuBinding.gpuBuffer.glBuffer);
|
||||
@@ -2187,7 +2193,8 @@ export function WebGL2CmdFuncExecuteCmds (device: WebGL2GFXDevice, cmdPackage: W
|
||||
|
||||
let glSampler: WebGL2GPUUniformSampler | null = null;
|
||||
|
||||
for (const sampler of gpuShader.glSamplers) {
|
||||
for (let k = 0; k < gpuShader.glSamplers.length; k++) {
|
||||
const sampler = gpuShader.glSamplers[k];
|
||||
if (sampler.binding === gpuBinding.binding) {
|
||||
glSampler = sampler;
|
||||
break;
|
||||
@@ -2195,7 +2202,8 @@ export function WebGL2CmdFuncExecuteCmds (device: WebGL2GFXDevice, cmdPackage: W
|
||||
}
|
||||
|
||||
if (glSampler) {
|
||||
for (const texUnit of glSampler.units) {
|
||||
for (let k = 0; k < glSampler.units.length; k++) {
|
||||
const texUnit = glSampler.units[k];
|
||||
|
||||
const glTexUnit = cache.glTexUnits[texUnit];
|
||||
|
||||
@@ -2250,10 +2258,12 @@ export function WebGL2CmdFuncExecuteCmds (device: WebGL2GFXDevice, cmdPackage: W
|
||||
gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, null);
|
||||
|
||||
let glAttrib: WebGL2Attrib | null;
|
||||
for (const glInput of gpuShader.glInputs) {
|
||||
for (let j = 0; j < gpuShader.glInputs.length; j++) {
|
||||
const glInput = gpuShader.glInputs[j];
|
||||
glAttrib = null;
|
||||
|
||||
for (const attrib of gpuInputAssembler.glAttribs) {
|
||||
for (let k = 0; k < gpuInputAssembler.glAttribs.length; k++) {
|
||||
const attrib = gpuInputAssembler.glAttribs[k];
|
||||
if (attrib.name === glInput.name) {
|
||||
glAttrib = attrib;
|
||||
break;
|
||||
@@ -2297,10 +2307,12 @@ export function WebGL2CmdFuncExecuteCmds (device: WebGL2GFXDevice, cmdPackage: W
|
||||
cache.glCurrentAttribLocs[a] = false;
|
||||
}
|
||||
|
||||
for (const glInput of gpuShader.glInputs) {
|
||||
for (let j = 0; j < gpuShader.glInputs.length; j++) {
|
||||
const glInput = gpuShader.glInputs[j];
|
||||
let glAttrib: WebGL2Attrib | null = null;
|
||||
|
||||
for (const attrib of gpuInputAssembler.glAttribs) {
|
||||
for (let k = 0; k < gpuInputAssembler.glAttribs.length; k++) {
|
||||
const attrib = gpuInputAssembler.glAttribs[k];
|
||||
if (attrib.name === glInput.name) {
|
||||
glAttrib = attrib;
|
||||
break;
|
||||
@@ -2346,7 +2358,8 @@ export function WebGL2CmdFuncExecuteCmds (device: WebGL2GFXDevice, cmdPackage: W
|
||||
}
|
||||
|
||||
if (gpuPipelineState) {
|
||||
for (const dynamicState of gpuPipelineState.dynamicStates) {
|
||||
for (let k = 0; k < gpuPipelineState.dynamicStates.length; k++) {
|
||||
const dynamicState = gpuPipelineState.dynamicStates[k];
|
||||
switch (dynamicState) {
|
||||
case GFXDynamicState.VIEWPORT: {
|
||||
if (cmd2.viewport) {
|
||||
@@ -2517,7 +2530,9 @@ export function WebGL2CmdFuncExecuteCmds (device: WebGL2GFXDevice, cmdPackage: W
|
||||
}
|
||||
} else {
|
||||
if (gpuInputAssembler.gpuIndirectBuffer) {
|
||||
for (const drawInfo of gpuInputAssembler.gpuIndirectBuffer.indirects) {
|
||||
const indirects = gpuInputAssembler.gpuIndirectBuffer.indirects;
|
||||
for (let k = 0; k < indirects.length; k++) {
|
||||
const drawInfo = indirects[k];
|
||||
const gpuBuffer = gpuInputAssembler.gpuIndexBuffer;
|
||||
if (gpuBuffer && drawInfo.indexCount > -1) {
|
||||
const offset = drawInfo.firstIndex * gpuBuffer.stride;
|
||||
@@ -2574,7 +2589,8 @@ export function WebGL2CmdFuncCopyTexImagesToTexture (
|
||||
let f = 0;
|
||||
switch (gpuTexture.glTarget) {
|
||||
case gl.TEXTURE_2D: {
|
||||
for (const region of regions) {
|
||||
for (let k = 0; k < regions.length; k++) {
|
||||
const region = regions[k];
|
||||
for (m = region.texSubres.baseMipLevel; m < region.texSubres.levelCount; ++m) {
|
||||
gl.texSubImage2D(gl.TEXTURE_2D, m,
|
||||
region.texOffset.x, region.texOffset.y,
|
||||
@@ -2584,7 +2600,8 @@ export function WebGL2CmdFuncCopyTexImagesToTexture (
|
||||
break;
|
||||
}
|
||||
case gl.TEXTURE_CUBE_MAP: {
|
||||
for (const region of regions) {
|
||||
for (let k = 0; k < regions.length; k++) {
|
||||
const region = regions[k];
|
||||
const fcount = region.texSubres.baseArrayLayer + region.texSubres.layerCount;
|
||||
for (f = region.texSubres.baseArrayLayer; f < fcount; ++f) {
|
||||
const mcount = region.texSubres.baseMipLevel + region.texSubres.levelCount;
|
||||
@@ -2629,7 +2646,8 @@ export function WebGL2CmdFuncCopyBuffersToTexture (
|
||||
const isCompressed = fmtInfo.isCompressed;
|
||||
switch (gpuTexture.glTarget) {
|
||||
case gl.TEXTURE_2D: {
|
||||
for (const region of regions) {
|
||||
for (let k = 0; k < regions.length; k++) {
|
||||
const region = regions[k];
|
||||
w = region.texExtent.width;
|
||||
h = region.texExtent.height;
|
||||
for (m = region.texSubres.baseMipLevel; m < region.texSubres.levelCount; ++m) {
|
||||
@@ -2655,7 +2673,8 @@ export function WebGL2CmdFuncCopyBuffersToTexture (
|
||||
break;
|
||||
}
|
||||
case gl.TEXTURE_CUBE_MAP: {
|
||||
for (const region of regions) {
|
||||
for (let k = 0; k < regions.length; k++) {
|
||||
const region = regions[k];
|
||||
n = 0;
|
||||
const fcount = region.texSubres.baseArrayLayer + region.texSubres.layerCount;
|
||||
for (f = region.texSubres.baseArrayLayer; f < fcount; ++f) {
|
||||
|
||||
@@ -22,12 +22,6 @@ export class CachedArray<T> {
|
||||
*/
|
||||
public length: number = 0;
|
||||
|
||||
/**
|
||||
* @zh
|
||||
* 指向缓存数组的引用
|
||||
*/
|
||||
private cache: Array<T|null>;
|
||||
|
||||
/**
|
||||
* @zh
|
||||
* 比较函数
|
||||
@@ -41,7 +35,6 @@ export class CachedArray<T> {
|
||||
*/
|
||||
constructor (length: number, compareFn?: (a: T, b: T) => number) {
|
||||
this.array = new Array(length);
|
||||
this.cache = this.array;
|
||||
this.length = 0;
|
||||
|
||||
if (compareFn !== undefined) {
|
||||
@@ -83,7 +76,6 @@ export class CachedArray<T> {
|
||||
* 清空数组所有元素
|
||||
*/
|
||||
public clear () {
|
||||
this.cache.fill(null);
|
||||
this.length = 0;
|
||||
}
|
||||
|
||||
|
||||
@@ -285,8 +285,10 @@ export class ForwardPipeline extends RenderPipeline {
|
||||
*/
|
||||
protected sceneCulling (view: RenderView) {
|
||||
super.sceneCulling(view);
|
||||
this._validLights.splice(0);
|
||||
for (const light of view.camera.scene!.sphereLights) {
|
||||
this._validLights.length = 0;
|
||||
const sphereLights = view.camera.scene!.sphereLights;
|
||||
for (let i = 0; i < sphereLights.length; i++) {
|
||||
const light = sphereLights[i];
|
||||
if (light.enabled) {
|
||||
light.update();
|
||||
sphere.set(_sphere, light.position.x, light.position.y, light.position.z, light.range);
|
||||
@@ -295,7 +297,9 @@ export class ForwardPipeline extends RenderPipeline {
|
||||
}
|
||||
}
|
||||
}
|
||||
for (const light of view.camera.scene!.spotLights) {
|
||||
const spotLights = view.camera.scene!.spotLights;
|
||||
for (let i = 0; i < spotLights.length; i++) {
|
||||
const light = spotLights[i];
|
||||
if (light.enabled) {
|
||||
light.update();
|
||||
sphere.set(_sphere, light.position.x, light.position.y, light.position.z, light.range);
|
||||
@@ -305,8 +309,8 @@ export class ForwardPipeline extends RenderPipeline {
|
||||
}
|
||||
}
|
||||
|
||||
this._lightIndexOffset.splice(0);
|
||||
this._lightIndices.splice(0);
|
||||
this._lightIndexOffset.length = 0;
|
||||
this._lightIndices.length = 0;
|
||||
for (let i = 0; i < this._renderObjects.length; i++) {
|
||||
this._lightIndexOffset[i] = this._lightIndices.length;
|
||||
if (this._renderObjects[i].model.localBindings.get(UBOForwardLight.BLOCK.name)) {
|
||||
|
||||
@@ -848,9 +848,9 @@ export abstract class RenderPipeline {
|
||||
}
|
||||
}
|
||||
|
||||
this._shadingTexViews.splice(0);
|
||||
this._shadingTextures.splice(0);
|
||||
this._shadingFBOs.splice(0);
|
||||
this._shadingTexViews.length = 0;
|
||||
this._shadingTextures.length = 0;
|
||||
this._shadingFBOs.length = 0;
|
||||
|
||||
if (this._depthStencilTexView) {
|
||||
this._depthStencilTexView.destroy();
|
||||
@@ -1181,13 +1181,13 @@ export abstract class RenderPipeline {
|
||||
Vec4.toArray(fv, _v4Zero, UBOGlobal.MAIN_LIT_COLOR_OFFSET);
|
||||
}
|
||||
|
||||
_vec4Array.set(ambient.skyColor);
|
||||
const skyColor = ambient.skyColor;
|
||||
if (this._isHDR) {
|
||||
_vec4Array[3] = ambient.skyIllum * this._fpScale;
|
||||
skyColor[3] = ambient.skyIllum * this._fpScale;
|
||||
} else {
|
||||
_vec4Array[3] = ambient.skyIllum * exposure;
|
||||
skyColor[3] = ambient.skyIllum * exposure;
|
||||
}
|
||||
this._uboGlobal.view.set(_vec4Array, UBOGlobal.AMBIENT_SKY_OFFSET);
|
||||
this._uboGlobal.view.set(skyColor, UBOGlobal.AMBIENT_SKY_OFFSET);
|
||||
|
||||
this._uboGlobal.view.set(ambient.groundAlbedo, UBOGlobal.AMBIENT_GROUND_OFFSET);
|
||||
|
||||
@@ -1205,7 +1205,7 @@ export abstract class RenderPipeline {
|
||||
const camera = view.camera;
|
||||
const scene = camera.scene!;
|
||||
|
||||
this._renderObjects.splice(0);
|
||||
this._renderObjects.length = 0;
|
||||
|
||||
const mainLight = scene.mainLight;
|
||||
if (mainLight && mainLight.enabled) {
|
||||
@@ -1221,7 +1221,9 @@ export abstract class RenderPipeline {
|
||||
this.addVisibleModel(scene.skybox, camera);
|
||||
}
|
||||
|
||||
for (const model of scene.models) {
|
||||
const models = scene.models;
|
||||
for (let i = 0; i < models.length; i++) {
|
||||
const model = models[i];
|
||||
|
||||
model._resetUBOUpdateFlag();
|
||||
|
||||
|
||||
@@ -542,34 +542,10 @@ export class Pass {
|
||||
if (saveOverrides) { Object.assign(this._defines, defineOverrides); }
|
||||
else { Object.assign(defineOverrides, this._defines); defines = defineOverrides; }
|
||||
}
|
||||
const shader = programLib.getGFXShader(this._device, this._programName, defines, pipeline);
|
||||
if (!shader) { console.warn(`create shader ${this._programName} failed`); return null; }
|
||||
if (saveOverrides) { this._shader = shader; }
|
||||
|
||||
const { blocks, samplers } = this._shaderInfo;
|
||||
this._bindings.length = 0;
|
||||
let lastBinding = -1;
|
||||
blocks:
|
||||
for (let i = 0; i < blocks.length; i++) {
|
||||
const block = blocks[i];
|
||||
if (block.binding === lastBinding) { continue; }
|
||||
for (let j = 0; j < block.defines.length; j++) {
|
||||
if (!defines[block.defines[j]]) { continue blocks; }
|
||||
}
|
||||
lastBinding = block.binding;
|
||||
this._bindings.push(block);
|
||||
}
|
||||
samplers:
|
||||
for (let i = 0; i < samplers.length; i++) {
|
||||
const sampler = samplers[i];
|
||||
if (sampler.binding === lastBinding) { continue; }
|
||||
for (let j = 0; j < sampler.defines.length; j++) {
|
||||
if (!defines[sampler.defines[j]]) { continue samplers; }
|
||||
}
|
||||
lastBinding = sampler.binding;
|
||||
this._bindings.push(sampler);
|
||||
}
|
||||
return shader;
|
||||
const res = programLib.getGFXShader(this._device, this._programName, defines, pipeline);
|
||||
if (!res.shader) { console.warn(`create shader ${this._programName} failed`); return null; }
|
||||
if (saveOverrides) { this._shader = res.shader; this._bindings = res.bindings; }
|
||||
return res;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -581,10 +557,12 @@ export class Pass {
|
||||
console.warn(`pass resources not complete, create PSO failed`);
|
||||
return null;
|
||||
}
|
||||
let shader = this._shader!;
|
||||
if (defineOverrides) { shader = this.tryCompile(defineOverrides, false)!; }
|
||||
let shader = this._shader!; _blInfo.bindings = this._bindings;
|
||||
if (defineOverrides) {
|
||||
const res = this.tryCompile(defineOverrides, false);
|
||||
if (res) { shader = res.shader; _blInfo.bindings = res.bindings; }
|
||||
}
|
||||
// bind resources
|
||||
_blInfo.bindings = this._bindings;
|
||||
const bindingLayout = this._device.createBindingLayout(_blInfo);
|
||||
for (const b in this._buffers) {
|
||||
bindingLayout.bindBuffer(parseInt(b), this._buffers[b]);
|
||||
|
||||
@@ -138,13 +138,44 @@ function genHandles (tmpl: IProgramInfo) {
|
||||
return handleMap;
|
||||
}
|
||||
|
||||
function dependencyCheck (dependencies: string[], defines: IDefineMap) {
|
||||
for (let i = 0; i < dependencies.length; i++) {
|
||||
if (!defines[dependencies[i]]) { return false; }
|
||||
}
|
||||
return true;
|
||||
}
|
||||
function getShaderBindings (
|
||||
tmpl: IProgramInfo, defines: IDefineMap, outBlocks: IBlockInfoRT[], outSamplers: ISamplerInfoRT[], bindings: IGFXBinding[]) {
|
||||
const { blocks, samplers } = tmpl;
|
||||
let lastBinding = -1;
|
||||
for (let i = 0; i < blocks.length; i++) {
|
||||
const block = blocks[i];
|
||||
if (block.binding === lastBinding || !dependencyCheck(block.defines, defines)) { continue; }
|
||||
lastBinding = block.binding;
|
||||
outBlocks.push(block);
|
||||
bindings.push(block);
|
||||
}
|
||||
for (let i = 0; i < samplers.length; i++) {
|
||||
const sampler = samplers[i];
|
||||
if (sampler.binding === lastBinding || !dependencyCheck(sampler.defines, defines)) { continue; }
|
||||
lastBinding = sampler.binding;
|
||||
outSamplers.push(sampler);
|
||||
bindings.push(sampler);
|
||||
}
|
||||
}
|
||||
|
||||
interface IShaderResources {
|
||||
shader: GFXShader;
|
||||
bindings: IGFXBinding[];
|
||||
}
|
||||
|
||||
/**
|
||||
* @zh
|
||||
* 维护 shader 资源实例的全局管理器。
|
||||
*/
|
||||
class ProgramLib {
|
||||
protected _templates: Record<string, IProgramInfo>;
|
||||
protected _cache: Record<string, GFXShader>;
|
||||
protected _cache: Record<string, IShaderResources>;
|
||||
|
||||
constructor () {
|
||||
this._templates = {};
|
||||
@@ -232,10 +263,11 @@ class ProgramLib {
|
||||
if (typeof val === 'boolean') { val = val ? '1' : '0'; }
|
||||
return new RegExp(cur + val);
|
||||
});
|
||||
const keys = Object.keys(this._cache).filter((k) => regexes.every((re) => re.test(this._cache[k].name)));
|
||||
const keys = Object.keys(this._cache).filter((k) => regexes.every((re) => re.test(this._cache[k].shader.name)));
|
||||
for (const k of keys) {
|
||||
console.log(`destroyed shader ${this._cache[k].name}`);
|
||||
this._cache[k].destroy();
|
||||
const prog = this._cache[k].shader;
|
||||
console.log(`destroyed shader ${prog.name}`);
|
||||
prog.destroy();
|
||||
delete this._cache[k];
|
||||
}
|
||||
}
|
||||
@@ -251,19 +283,15 @@ class ProgramLib {
|
||||
public getGFXShader (device: GFXDevice, name: string, defines: IDefineMap, pipeline: RenderPipeline) {
|
||||
Object.assign(defines, pipeline.macros);
|
||||
const key = this.getKey(name, defines);
|
||||
let program = this._cache[key];
|
||||
if (program !== undefined) {
|
||||
return program;
|
||||
}
|
||||
const res = this._cache[key];
|
||||
if (res) { return res; }
|
||||
|
||||
// get template
|
||||
const tmpl = this._templates[name];
|
||||
if (!tmpl.globalsInited) { insertBuiltinBindings(tmpl, pipeline.globalBindings, 'globals'); tmpl.globalsInited = true; }
|
||||
|
||||
const macroArray = prepareDefines(defines, tmpl.defines);
|
||||
const customDef = macroArray.reduce((acc, cur) => {
|
||||
return `${acc}#define ${cur.name} ${cur.value}\n`;
|
||||
}, '');
|
||||
const customDef = macroArray.reduce((acc, cur) => `${acc}#define ${cur.name} ${cur.value}\n`, '');
|
||||
|
||||
let vert: string = '';
|
||||
let frag: string = '';
|
||||
@@ -275,17 +303,20 @@ class ProgramLib {
|
||||
frag = `#version 100\n${customDef}\n${tmpl.glsl1.frag}`;
|
||||
}
|
||||
|
||||
program = device.createShader({
|
||||
const blocks: IBlockInfoRT[] = [];
|
||||
const samplers: ISamplerInfoRT[] = [];
|
||||
const bindings: IGFXBinding[] = [];
|
||||
getShaderBindings(tmpl, defines, blocks, samplers, bindings);
|
||||
|
||||
const shader = device.createShader({
|
||||
name: getShaderInstanceName(name, macroArray),
|
||||
blocks: tmpl.blocks,
|
||||
samplers: tmpl.samplers,
|
||||
blocks, samplers,
|
||||
stages: [
|
||||
{ type: GFXShaderType.VERTEX, source: vert },
|
||||
{ type: GFXShaderType.FRAGMENT, source: frag },
|
||||
],
|
||||
});
|
||||
this._cache[key] = program;
|
||||
return program;
|
||||
return this._cache[key] = { shader, bindings };
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -43,8 +43,7 @@ export enum SamplerInfoIndex {
|
||||
minLOD,
|
||||
maxLOD,
|
||||
mipLODBias,
|
||||
borderColor,
|
||||
total = borderColor + 4,
|
||||
total,
|
||||
}
|
||||
|
||||
const defaultInfo = [
|
||||
@@ -57,10 +56,11 @@ const defaultInfo = [
|
||||
8,
|
||||
GFXComparisonFunc.NEVER,
|
||||
0, 0, 0,
|
||||
0, 0, 0, 0,
|
||||
];
|
||||
const defaultHash = genSamplerHash(defaultInfo);
|
||||
|
||||
const borderColor = { r: 0, g: 0, b: 0, a: 0 };
|
||||
|
||||
const gfxInfo: IGFXSamplerInfo = {};
|
||||
|
||||
export function genSamplerHash (info: Array<number | undefined>): number {
|
||||
@@ -115,7 +115,7 @@ class SamplerLib {
|
||||
gfxInfo.minLOD = ((hash >> 20) & 15);
|
||||
gfxInfo.maxLOD = ((hash >> 24) & 15);
|
||||
gfxInfo.mipLODBias = ((hash >> 28) & 15);
|
||||
gfxInfo.borderColor = { r: 0, g: 0, b: 0, a: 0 };
|
||||
gfxInfo.borderColor = borderColor;
|
||||
|
||||
const sampler = this._cache[hash] = device.createSampler(gfxInfo);
|
||||
return sampler;
|
||||
|
||||
@@ -7,7 +7,7 @@ import { GFXDevice } from '../../gfx/device';
|
||||
import { GFXTexture } from '../../gfx/texture';
|
||||
import { GFXTextureView } from '../../gfx/texture-view';
|
||||
|
||||
function nearestPO2 (num: number): number {
|
||||
export function nearestPOT (num: number): number {
|
||||
--num;
|
||||
num |= num >> 16;
|
||||
num |= num >> 8;
|
||||
@@ -145,7 +145,7 @@ export class TextureBufferPool {
|
||||
|
||||
// create a new one
|
||||
const targetSize = Math.sqrt(size / this._formatSize);
|
||||
const texWidth = this._roundUpFn && this._roundUpFn(targetSize, this._formatSize) || Math.max(1024, nearestPO2(targetSize));
|
||||
const texWidth = this._roundUpFn && this._roundUpFn(targetSize, this._formatSize) || Math.max(1024, nearestPOT(targetSize));
|
||||
const texSize = texWidth * texWidth * this._formatSize;
|
||||
|
||||
console.info('TextureBufferPool: Allocate chunk ' + this._chunkCount + ', size: ' + texSize);
|
||||
|
||||
@@ -38,7 +38,7 @@ import { GFXDevice, GFXFeature } from '../../gfx/device';
|
||||
import { Mat4, Quat, Vec3 } from '../../math';
|
||||
import { UBOSkinningAnimation } from '../../pipeline/define';
|
||||
import { genSamplerHash } from '../core/sampler-lib';
|
||||
import { ITextureBufferHandle, TextureBufferPool } from '../core/texture-buffer-pool';
|
||||
import { ITextureBufferHandle, nearestPOT, TextureBufferPool } from '../core/texture-buffer-pool';
|
||||
|
||||
// change here and cc-skinning.chunk to use other skinning algorithms
|
||||
const uploadJointData = uploadJointDataLBS;
|
||||
@@ -175,7 +175,7 @@ export class JointsTexturePool {
|
||||
this._identityBuffer.readyToBeDeleted = true;
|
||||
this.releaseHandle(this._identityBuffer, false);
|
||||
}
|
||||
this._maxIdentityJoints *= 2;
|
||||
this._maxIdentityJoints = nearestPOT(len);
|
||||
this._identityBuffer = this._allocIdentityBuffer();
|
||||
}
|
||||
if (!this._identityBuffer) { return null; }
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import { Quat, Vec3 } from '../../math';
|
||||
import { TransformBit } from '../../scene-graph/node-enum';
|
||||
import { INode } from '../../utils/interfaces';
|
||||
import { Light, LightType } from './light';
|
||||
import { RenderScene } from './render-scene';
|
||||
@@ -33,10 +34,11 @@ export class DirectionalLight extends Light {
|
||||
constructor () {
|
||||
super();
|
||||
this._type = LightType.DIRECTIONAL;
|
||||
this._node.hasChangedFlags = TransformBit.ROTATION;
|
||||
}
|
||||
|
||||
public update () {
|
||||
if (this._node) {
|
||||
if (this._node && this._node.hasChangedFlags) {
|
||||
this._dir = Vec3.transformQuat(_v3, _forward, this._node.getWorldRotation(_qt));
|
||||
Vec3.normalize(this._dir, this._dir);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user