mirror of
https://github.com/cocos/cocos4.git
synced 2026-12-01 07:42:38 +08:00
Merge branch '3d' into create-mesh
This commit is contained in:
@@ -26,8 +26,8 @@ import { Asset } from '../../assets/asset';
|
||||
import { ccclass, property } from '../../core/data/class-decorator';
|
||||
import { GFXBindingType } from '../../gfx/define';
|
||||
import { Effect } from '../../renderer/core/effect';
|
||||
import { Pass } from '../../renderer/core/pass';
|
||||
import { EffectAsset } from './effect-asset';
|
||||
import { Pass, IPassOverrides } from '../../renderer/core/pass';
|
||||
import { EffectAsset, IPassInfo, ITechniqueInfo } from './effect-asset';
|
||||
|
||||
@ccclass('cc.Material')
|
||||
export class Material extends Asset {
|
||||
@@ -133,6 +133,18 @@ export class Material extends Asset {
|
||||
this.update();
|
||||
}
|
||||
|
||||
public rebuildWithOverrides (overrides: IPassOverrides, idx?: number) {
|
||||
if (!this._passes || !this._effectAsset) { return; }
|
||||
const passInfos = Effect.getPassInfos(this._effectAsset, this._techIdx);
|
||||
if (idx === undefined) {
|
||||
for (let i = 0; i < this._passes.length; i++) {
|
||||
this._passes[i].rebuildWithOverrides(passInfos[i], overrides);
|
||||
}
|
||||
} else {
|
||||
this._passes[idx].rebuildWithOverrides(passInfos[idx], overrides);
|
||||
}
|
||||
}
|
||||
|
||||
public update (asset: EffectAsset | string | null = this._effectAsset, keepProps: boolean = true) {
|
||||
// get effect asset
|
||||
if (typeof asset === 'string') {
|
||||
|
||||
@@ -116,6 +116,7 @@ export default class ModelComponent extends RenderableComponent {
|
||||
|
||||
set mesh(val) {
|
||||
this._mesh = val;
|
||||
this._updateModels();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -184,7 +185,7 @@ export default class ModelComponent extends RenderableComponent {
|
||||
}
|
||||
|
||||
_updateModels() {
|
||||
if (!this.enabled) return;
|
||||
if (!this.node._scene) return;
|
||||
let meshCount = this._mesh ? this._mesh.subMeshCount : 0;
|
||||
|
||||
for (let i = 0; i < meshCount; ++i) {
|
||||
|
||||
@@ -89,7 +89,7 @@ export default class ParticleSystemRenderer extends RenderableComponent {
|
||||
this.frameTile = cc.v2(1, 1);
|
||||
this.attrs = new Array(5);
|
||||
|
||||
this.device = cc.game._renderContext;
|
||||
this.device = cc.game._gfxDevice;
|
||||
}
|
||||
|
||||
onInit() {
|
||||
|
||||
@@ -187,7 +187,7 @@ export default class SkinningModelComponent extends ModelComponent {
|
||||
return;
|
||||
}
|
||||
|
||||
if (cc.game._renderContext.allowFloatTexture()) {
|
||||
if (cc.game._gfxDevice.allowFloatTexture()) {
|
||||
this._jointsTexture = utils.createJointsTexture(this._skeleton);
|
||||
this._jointMatricesData = new Float32Array(this._jointsTexture._width * this._jointsTexture._height * 4);
|
||||
} else {
|
||||
|
||||
@@ -82,7 +82,7 @@ export default class SkyboxComponent extends Component {
|
||||
onLoad() {
|
||||
this._model.setNode(this.node);
|
||||
|
||||
let ia = renderer.createIA(cc.game._renderContext, box(2, 2, 2));
|
||||
let ia = renderer.createIA(cc.game._gfxDevice, box(2, 2, 2));
|
||||
this._model.setInputAssembler(ia);
|
||||
|
||||
if (!this._material) {
|
||||
|
||||
@@ -0,0 +1,69 @@
|
||||
import { renderer } from '../../renderer';
|
||||
import find from '../../scene-graph/find';
|
||||
import { Mesh, RenderingMesh } from '../assets/mesh';
|
||||
import gfx from '../../renderer/gfx/index';
|
||||
import { GFXPrimitiveMode } from '../../gfx/define';
|
||||
|
||||
/**
|
||||
*
|
||||
* @param {import("../assets/skeleton").default} skinning
|
||||
*/
|
||||
function createJointsTexture(skinning) {
|
||||
const jointCount = skinning.joints.length;
|
||||
|
||||
// Set jointsTexture.
|
||||
// A squared texture with side length N(N > 1) multiples of 2 can store
|
||||
// 2 ^ (2 * N - 2) matrices.
|
||||
// We support most 1024 joints.
|
||||
let size;
|
||||
if (jointCount > 1024) {
|
||||
throw "To many joints(more than 1024).";
|
||||
} else if (jointCount > 256) {
|
||||
size = 64;
|
||||
} else if (jointCount > 64) {
|
||||
size = 32;
|
||||
} else if (jointCount > 16) {
|
||||
size = 16;
|
||||
} else if (jointCount > 4) {
|
||||
size = 8;
|
||||
} else {
|
||||
size = 4;
|
||||
}
|
||||
|
||||
return new gfx.Texture2D(cc.game._gfxDevice, {
|
||||
width: size,
|
||||
height: size,
|
||||
format: gfx.TEXTURE_FMT_RGBA32F,
|
||||
minFilter: gfx.FILTER_NEAREST,
|
||||
magFilter: gfx.FILTER_NEAREST,
|
||||
wrapS: gfx.WRAP_CLAMP,
|
||||
wrapT: gfx.WRAP_CLAMP,
|
||||
mipmap: false
|
||||
});
|
||||
}
|
||||
|
||||
function createMesh(context, data) {
|
||||
let ia = renderer.createIA(context, data);
|
||||
let meshAsset = new Mesh();
|
||||
const primitiveMode = data.primitiveMode === undefined ? GFXPrimitiveMode.TRIANGLE_LIST : data.primitiveMode;
|
||||
meshAsset._renderingMesh = new RenderingMesh([{inputAssembler: ia, primitiveMode}], [], []);
|
||||
meshAsset._minPosition = data.minPos;
|
||||
meshAsset._maxPosition = data.maxPos;
|
||||
|
||||
return meshAsset;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {Uint8Array} buffer
|
||||
*/
|
||||
function toPPM(buffer, w, h) {
|
||||
return `P3 ${w} ${h} 255\n${buffer.filter((e, i) => i % 4 < 3).toString()}\n`;
|
||||
}
|
||||
|
||||
export default {
|
||||
createJointsTexture,
|
||||
createMesh,
|
||||
|
||||
find,
|
||||
toPPM
|
||||
};
|
||||
@@ -1,5 +1,5 @@
|
||||
import { GFXBuffer, IGFXBufferInfo, GFXBufferSource } from '../buffer';
|
||||
import { GFXMemoryUsageBit, GFXStatus, GFXBufferUsageBit } from '../define';
|
||||
import { GFXBuffer, GFXBufferSource, IGFXBufferInfo } from '../buffer';
|
||||
import { GFXBufferUsageBit, GFXMemoryUsageBit, GFXStatus } from '../define';
|
||||
import { GFXDevice } from '../device';
|
||||
import { WebGLGFXDevice } from './webgl-device';
|
||||
import { WebGLGPUBuffer } from './webgl-gpu-objects';
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import { GFXBindingLayout } from '../binding-layout';
|
||||
import { GFXBuffer, GFXBufferSource } from '../buffer';
|
||||
import { GFXCommandBuffer, IGFXCommandBufferInfo } from '../command-buffer';
|
||||
import { GFXBufferTextureCopy, GFXStatus, GFXTextureLayout, IGFXColor, IGFXRect, GFXBufferUsageBit } from '../define';
|
||||
import { GFXBufferTextureCopy, GFXBufferUsageBit, GFXStatus, GFXTextureLayout, IGFXColor, IGFXRect, GFXCommandBufferType } from '../define';
|
||||
import { GFXDevice } from '../device';
|
||||
import { GFXFramebuffer } from '../framebuffer';
|
||||
import { GFXInputAssembler } from '../input-assembler';
|
||||
@@ -29,6 +29,7 @@ import { WebGLGFXTexture } from './webgl-texture';
|
||||
export class WebGLGFXCommandBuffer extends GFXCommandBuffer {
|
||||
|
||||
public cmdPackage: WebGLCmdPackage = new WebGLCmdPackage();
|
||||
private _isInRenderPass: boolean = false;
|
||||
private _curGPUPipelineState: WebGLGPUPipelineState | null = null;
|
||||
private _curGPUBindingLayout: WebGLGPUBindingLayout | null = null;
|
||||
private _curGPUInputAssembler: WebGLGPUInputAssembler | null = null;
|
||||
@@ -68,6 +69,8 @@ export class WebGLGFXCommandBuffer extends GFXCommandBuffer {
|
||||
if (this._isStateInvalied) {
|
||||
this.bindStates();
|
||||
}
|
||||
|
||||
this._isInRenderPass = false;
|
||||
}
|
||||
|
||||
public beginRenderPass (
|
||||
@@ -88,9 +91,12 @@ export class WebGLGFXCommandBuffer extends GFXCommandBuffer {
|
||||
|
||||
this.cmdPackage.cmds.push(WebGLCmd.BEGIN_RENDER_PASS);
|
||||
}
|
||||
|
||||
this._isInRenderPass = true;
|
||||
}
|
||||
|
||||
public endRenderPass () {
|
||||
this._isInRenderPass = false;
|
||||
}
|
||||
|
||||
public bindPipelineState (pipelineState: GFXPipelineState) {
|
||||
@@ -119,46 +125,55 @@ export class WebGLGFXCommandBuffer extends GFXCommandBuffer {
|
||||
|
||||
public draw (inputAssembler: GFXInputAssembler) {
|
||||
|
||||
if (this._isStateInvalied) {
|
||||
this.bindStates();
|
||||
}
|
||||
if (this._type === GFXCommandBufferType.PRIMARY && this._isInRenderPass ||
|
||||
this._type === GFXCommandBufferType.SECONDARY) {
|
||||
if (this._isStateInvalied) {
|
||||
this.bindStates();
|
||||
}
|
||||
|
||||
const cmd = ( this._allocator as WebGLGFXCommandAllocator).
|
||||
drawCmdPool.alloc(WebGLCmdDraw);
|
||||
if (cmd) {
|
||||
(inputAssembler as WebGLGFXInputAssembler).extractCmdDraw(cmd);
|
||||
this.cmdPackage.drawCmds.push(cmd);
|
||||
const cmd = ( this._allocator as WebGLGFXCommandAllocator).
|
||||
drawCmdPool.alloc(WebGLCmdDraw);
|
||||
if (cmd) {
|
||||
(inputAssembler as WebGLGFXInputAssembler).extractCmdDraw(cmd);
|
||||
this.cmdPackage.drawCmds.push(cmd);
|
||||
|
||||
this.cmdPackage.cmds.push(WebGLCmd.DRAW);
|
||||
this.cmdPackage.cmds.push(WebGLCmd.DRAW);
|
||||
}
|
||||
} else {
|
||||
console.error('Command \'draw\' must be recorded inside a render pass.');
|
||||
}
|
||||
}
|
||||
|
||||
public updateBuffer (buffer: GFXBuffer, data: GFXBufferSource, offset?: number, size?: number) {
|
||||
const gpuBuffer = (buffer as WebGLGFXBuffer).gpuBuffer;
|
||||
if (gpuBuffer) {
|
||||
const cmd = (this._allocator as WebGLGFXCommandAllocator).
|
||||
updateBufferCmdPool.alloc(WebGLCmdUpdateBuffer);
|
||||
if (cmd) {
|
||||
if (!this._isInRenderPass) {
|
||||
const gpuBuffer = (buffer as WebGLGFXBuffer).gpuBuffer;
|
||||
if (gpuBuffer) {
|
||||
const cmd = (this._allocator as WebGLGFXCommandAllocator).
|
||||
updateBufferCmdPool.alloc(WebGLCmdUpdateBuffer);
|
||||
if (cmd) {
|
||||
|
||||
let buffSize;
|
||||
if (size !== undefined ) {
|
||||
buffSize = size;
|
||||
} else if (buffer.usage & GFXBufferUsageBit.INDIRECT) {
|
||||
buffSize = 0;
|
||||
} else {
|
||||
buffSize = (data as ArrayBuffer).byteLength;
|
||||
let buffSize;
|
||||
if (size !== undefined ) {
|
||||
buffSize = size;
|
||||
} else if (buffer.usage & GFXBufferUsageBit.INDIRECT) {
|
||||
buffSize = 0;
|
||||
} else {
|
||||
buffSize = (data as ArrayBuffer).byteLength;
|
||||
}
|
||||
|
||||
const buff = data as ArrayBuffer;
|
||||
|
||||
cmd.gpuBuffer = gpuBuffer;
|
||||
cmd.buffer = buff.slice(0);
|
||||
cmd.offset = (offset !== undefined ? offset : 0);
|
||||
cmd.size = buffSize;
|
||||
this.cmdPackage.updateBufferCmds.push(cmd);
|
||||
|
||||
this.cmdPackage.cmds.push(WebGLCmd.UPDATE_BUFFER);
|
||||
}
|
||||
|
||||
const buff = data as ArrayBuffer;
|
||||
|
||||
cmd.gpuBuffer = gpuBuffer;
|
||||
cmd.buffer = buff.slice(0);
|
||||
cmd.offset = (offset !== undefined ? offset : 0);
|
||||
cmd.size = buffSize;
|
||||
this.cmdPackage.updateBufferCmds.push(cmd);
|
||||
|
||||
this.cmdPackage.cmds.push(WebGLCmd.UPDATE_BUFFER);
|
||||
}
|
||||
} else {
|
||||
console.error('Command \'updateBuffer\' must be recorded outside a render pass.');
|
||||
}
|
||||
}
|
||||
|
||||
@@ -167,20 +182,25 @@ export class WebGLGFXCommandBuffer extends GFXCommandBuffer {
|
||||
dstTex: GFXTexture,
|
||||
dstLayout: GFXTextureLayout,
|
||||
regions: GFXBufferTextureCopy[]) {
|
||||
const gpuBuffer = ( srcBuff as WebGLGFXBuffer).gpuBuffer;
|
||||
const gpuTexture = ( dstTex as WebGLGFXTexture).gpuTexture;
|
||||
if (gpuBuffer && gpuTexture) {
|
||||
const cmd = (this._allocator as WebGLGFXCommandAllocator).
|
||||
copyBufferToTextureCmdPool.alloc(WebGLCmdCopyBufferToTexture);
|
||||
if (cmd) {
|
||||
cmd.gpuBuffer = gpuBuffer;
|
||||
cmd.gpuTexture = gpuTexture;
|
||||
cmd.dstLayout = dstLayout;
|
||||
cmd.regions = regions;
|
||||
this.cmdPackage.copyBufferToTextureCmds.push(cmd);
|
||||
|
||||
this.cmdPackage.cmds.push(WebGLCmd.COPY_BUFFER_TO_TEXTURE);
|
||||
if (!this._isInRenderPass) {
|
||||
const gpuBuffer = ( srcBuff as WebGLGFXBuffer).gpuBuffer;
|
||||
const gpuTexture = ( dstTex as WebGLGFXTexture).gpuTexture;
|
||||
if (gpuBuffer && gpuTexture) {
|
||||
const cmd = (this._allocator as WebGLGFXCommandAllocator).
|
||||
copyBufferToTextureCmdPool.alloc(WebGLCmdCopyBufferToTexture);
|
||||
if (cmd) {
|
||||
cmd.gpuBuffer = gpuBuffer;
|
||||
cmd.gpuTexture = gpuTexture;
|
||||
cmd.dstLayout = dstLayout;
|
||||
cmd.regions = regions;
|
||||
this.cmdPackage.copyBufferToTextureCmds.push(cmd);
|
||||
|
||||
this.cmdPackage.cmds.push(WebGLCmd.COPY_BUFFER_TO_TEXTURE);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
console.error('Command \'copyBufferToTexture\' must be recorded outside a render pass.');
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,8 +1,20 @@
|
||||
import { GFXBindingLayout, GFXBindingUnit, IGFXBindingLayoutInfo } from '../binding-layout';
|
||||
import { GFXBuffer, IGFXBufferInfo, GFXBufferSource } from '../buffer';
|
||||
import { GFXBuffer, GFXBufferSource, IGFXBufferInfo } from '../buffer';
|
||||
import { GFXCommandAllocator, IGFXCommandAllocatorInfo } from '../command-allocator';
|
||||
import { GFXCommandBuffer, IGFXCommandBufferInfo } from '../command-buffer';
|
||||
import { GFXBindingType, GFXBufferTextureCopy, GFXBufferUsageBit, GFXFilter, GFXFormat, GFXFormatInfos, GFXFormatSize, GFXQueueType, GFXTextureFlagBit, GFXTextureType, GFXTextureViewType } from '../define';
|
||||
import {
|
||||
GFXBindingType,
|
||||
GFXBufferTextureCopy,
|
||||
GFXBufferUsageBit,
|
||||
GFXFilter,
|
||||
GFXFormat,
|
||||
GFXFormatInfos,
|
||||
GFXFormatSize,
|
||||
GFXQueueType,
|
||||
GFXTextureFlagBit,
|
||||
GFXTextureType,
|
||||
GFXTextureViewType,
|
||||
} from '../define';
|
||||
import { GFXDevice, IGFXDeviceInfo } from '../device';
|
||||
import { GFXFramebuffer, IGFXFramebufferInfo } from '../framebuffer';
|
||||
import { GFXInputAssembler, IGFXInputAssemblerInfo } from '../input-assembler';
|
||||
@@ -19,9 +31,36 @@ import { WebGLGFXBindingLayout } from './webgl-binding-layout';
|
||||
import { WebGLGFXBuffer } from './webgl-buffer';
|
||||
import { WebGLGFXCommandAllocator } from './webgl-command-allocator';
|
||||
import { WebGLGFXCommandBuffer } from './webgl-command-buffer';
|
||||
import { WebGLCmdFuncCopyBufferToTexture, WebGLCmdFuncCreateBuffer, WebGLCmdFuncCreateFramebuffer, WebGLCmdFuncCreateInputAssember, WebGLCmdFuncCreateShader, WebGLCmdFuncCreateTexture, WebGLCmdFuncDestroyBuffer, WebGLCmdFuncDestroyFramebuffer, WebGLCmdFuncDestroyShader, WebGLCmdFuncDestroyTexture, WebGLCmdFuncUpdateBuffer } from './webgl-commands';
|
||||
import {
|
||||
WebGLCmdFuncCopyBufferToTexture,
|
||||
WebGLCmdFuncCreateBuffer,
|
||||
WebGLCmdFuncCreateFramebuffer,
|
||||
WebGLCmdFuncCreateInputAssember,
|
||||
WebGLCmdFuncCreateShader,
|
||||
WebGLCmdFuncCreateTexture,
|
||||
WebGLCmdFuncDestroyBuffer,
|
||||
WebGLCmdFuncDestroyFramebuffer,
|
||||
WebGLCmdFuncDestroyShader,
|
||||
WebGLCmdFuncDestroyTexture,
|
||||
WebGLCmdFuncUpdateBuffer,
|
||||
} from './webgl-commands';
|
||||
import { WebGLGFXFramebuffer } from './webgl-framebuffer';
|
||||
import { WebGLGPUBinding, WebGLGPUBindingLayout as WebGLGPUBindingLayout, WebGLGPUBuffer, WebGLGPUFramebuffer, WebGLGPUInputAssembler, WebGLGPUObjectType, WebGLGPUPipelineLayout, WebGLGPUPipelineState, WebGLGPURenderPass, WebGLGPUSampler, WebGLGPUShader, WebGLGPUShaderStage, WebGLGPUTexture, WebGLGPUTextureView } from './webgl-gpu-objects';
|
||||
import {
|
||||
WebGLGPUBinding,
|
||||
WebGLGPUBindingLayout,
|
||||
WebGLGPUBuffer,
|
||||
WebGLGPUFramebuffer,
|
||||
WebGLGPUInputAssembler,
|
||||
WebGLGPUObjectType,
|
||||
WebGLGPUPipelineLayout,
|
||||
WebGLGPUPipelineState,
|
||||
WebGLGPURenderPass,
|
||||
WebGLGPUSampler,
|
||||
WebGLGPUShader,
|
||||
WebGLGPUShaderStage,
|
||||
WebGLGPUTexture,
|
||||
WebGLGPUTextureView,
|
||||
} from './webgl-gpu-objects';
|
||||
import { WebGLGFXInputAssembler } from './webgl-input-assembler';
|
||||
import { WebGLGFXPipelineLayout } from './webgl-pipeline-layout';
|
||||
import { WebGLGFXPipelineState } from './webgl-pipeline-state';
|
||||
@@ -160,7 +199,7 @@ export class WebGLGFXDevice extends GFXDevice {
|
||||
|
||||
private _webGLRC: WebGLRenderingContext | null = null;
|
||||
private _isAntialias: boolean = true;
|
||||
private _isPremultipliedAlpha: boolean = false;
|
||||
private _isPremultipliedAlpha: boolean = true;
|
||||
|
||||
private _extensions: string[] | null = null;
|
||||
private _EXT_texture_filter_anisotropic: EXT_texture_filter_anisotropic | null = null;
|
||||
@@ -475,7 +514,11 @@ export class WebGLGFXDevice extends GFXDevice {
|
||||
}
|
||||
}
|
||||
|
||||
public copyFramebufferToBuffer (srcFramebuffer: GFXFramebuffer, dstBuffer: ArrayBuffer, regions: GFXBufferTextureCopy[]) {
|
||||
public copyFramebufferToBuffer (
|
||||
srcFramebuffer: GFXFramebuffer,
|
||||
dstBuffer: ArrayBuffer,
|
||||
regions: GFXBufferTextureCopy[]) {
|
||||
|
||||
const gl = this._webGLRC as WebGLRenderingContext;
|
||||
const gpuFramebuffer = (srcFramebuffer as WebGLGFXFramebuffer).gpuFramebuffer;
|
||||
|
||||
@@ -497,7 +540,8 @@ export class WebGLGFXDevice extends GFXDevice {
|
||||
const memSize = GFXFormatSize(GFXFormat.RGBA8, w, h, 1);
|
||||
const data = view.subarray(buffOffset, buffOffset + memSize);
|
||||
|
||||
gl.readPixels(region.texOffset.x, region.texOffset.y, w, h, WebGLRenderingContext.RGBA, WebGLRenderingContext.UNSIGNED_BYTE, data);
|
||||
gl.readPixels(region.texOffset.x, region.texOffset.y, w, h,
|
||||
WebGLRenderingContext.RGBA, WebGLRenderingContext.UNSIGNED_BYTE, data);
|
||||
}
|
||||
|
||||
if (this.stateCache.glFramebuffer !== curFBO) {
|
||||
@@ -955,37 +999,37 @@ export class WebGLGFXDevice extends GFXDevice {
|
||||
|
||||
private initStates (gl: WebGLRenderingContext) {
|
||||
|
||||
gl.activeTexture(WebGLRenderingContext.TEXTURE0);
|
||||
gl.pixelStorei(WebGLRenderingContext.PACK_ALIGNMENT, 1);
|
||||
gl.pixelStorei(WebGLRenderingContext.UNPACK_ALIGNMENT, 1);
|
||||
gl.activeTexture(gl.TEXTURE0);
|
||||
gl.pixelStorei(gl.PACK_ALIGNMENT, 1);
|
||||
gl.pixelStorei(gl.UNPACK_ALIGNMENT, 1);
|
||||
|
||||
gl.bindFramebuffer(WebGLRenderingContext.FRAMEBUFFER, null);
|
||||
gl.bindFramebuffer(gl.FRAMEBUFFER, null);
|
||||
|
||||
// rasteriazer state
|
||||
gl.enable(WebGLRenderingContext.CULL_FACE);
|
||||
gl.cullFace(WebGLRenderingContext.BACK);
|
||||
gl.frontFace(WebGLRenderingContext.CCW);
|
||||
gl.enable(gl.CULL_FACE);
|
||||
gl.cullFace(gl.BACK);
|
||||
gl.frontFace(gl.CCW);
|
||||
gl.polygonOffset(0.0, 0.0);
|
||||
|
||||
// depth stencil state
|
||||
gl.enable(WebGLRenderingContext.DEPTH_TEST);
|
||||
gl.enable(gl.DEPTH_TEST);
|
||||
gl.depthMask(true);
|
||||
gl.depthFunc(WebGLRenderingContext.LESS);
|
||||
gl.depthFunc(gl.LESS);
|
||||
|
||||
gl.stencilFuncSeparate(WebGLRenderingContext.FRONT, WebGLRenderingContext.ALWAYS, 1, 0xffffffff);
|
||||
gl.stencilOpSeparate(WebGLRenderingContext.FRONT, WebGLRenderingContext.KEEP, WebGLRenderingContext.KEEP, WebGLRenderingContext.KEEP);
|
||||
gl.stencilMaskSeparate(WebGLRenderingContext.FRONT, 0xffffffff);
|
||||
gl.stencilFuncSeparate(WebGLRenderingContext.BACK, WebGLRenderingContext.ALWAYS, 1, 0xffffffff);
|
||||
gl.stencilOpSeparate(WebGLRenderingContext.BACK, WebGLRenderingContext.KEEP, WebGLRenderingContext.KEEP, WebGLRenderingContext.KEEP);
|
||||
gl.stencilMaskSeparate(WebGLRenderingContext.BACK, 0xffffffff);
|
||||
gl.stencilFuncSeparate(gl.FRONT, gl.ALWAYS, 1, 0xffffffff);
|
||||
gl.stencilOpSeparate(gl.FRONT, gl.KEEP, gl.KEEP, gl.KEEP);
|
||||
gl.stencilMaskSeparate(gl.FRONT, 0xffffffff);
|
||||
gl.stencilFuncSeparate(gl.BACK, gl.ALWAYS, 1, 0xffffffff);
|
||||
gl.stencilOpSeparate(gl.BACK, gl.KEEP, gl.KEEP, gl.KEEP);
|
||||
gl.stencilMaskSeparate(gl.BACK, 0xffffffff);
|
||||
|
||||
gl.disable(WebGLRenderingContext.STENCIL_TEST);
|
||||
gl.disable(gl.STENCIL_TEST);
|
||||
|
||||
// blend state
|
||||
gl.disable(WebGLRenderingContext.SAMPLE_ALPHA_TO_COVERAGE);
|
||||
gl.disable(WebGLRenderingContext.BLEND);
|
||||
gl.blendEquationSeparate(WebGLRenderingContext.FUNC_ADD, WebGLRenderingContext.FUNC_ADD);
|
||||
gl.blendFuncSeparate(WebGLRenderingContext.ONE, WebGLRenderingContext.ZERO, WebGLRenderingContext.ONE, WebGLRenderingContext.ZERO);
|
||||
gl.disable(gl.SAMPLE_ALPHA_TO_COVERAGE);
|
||||
gl.disable(gl.BLEND);
|
||||
gl.blendEquationSeparate(gl.FUNC_ADD, gl.FUNC_ADD);
|
||||
gl.blendFuncSeparate(gl.ONE, gl.ZERO, gl.ONE, gl.ZERO);
|
||||
gl.colorMask(true, true, true, true);
|
||||
gl.blendColor(0.0, 0.0, 0.0, 0.0);
|
||||
}
|
||||
|
||||
@@ -1,9 +1,24 @@
|
||||
import { GFXBindingType, GFXBufferUsage, GFXBufferUsageBit, GFXFormat, GFXMemoryUsage, GFXMemoryUsageBit, GFXShaderType, GFXTextureFlagBit, GFXTextureFlags, GFXTextureType, GFXTextureUsage, GFXTextureUsageBit, GFXTextureViewType, GFXType } from '../define';
|
||||
import { GFXBufferSource } from '../buffer';
|
||||
import {
|
||||
GFXBindingType,
|
||||
GFXBufferUsage,
|
||||
GFXBufferUsageBit,
|
||||
GFXFormat,
|
||||
GFXMemoryUsage,
|
||||
GFXMemoryUsageBit,
|
||||
GFXShaderType,
|
||||
GFXTextureFlagBit,
|
||||
GFXTextureFlags,
|
||||
GFXTextureType,
|
||||
GFXTextureUsage,
|
||||
GFXTextureUsageBit,
|
||||
GFXTextureViewType,
|
||||
GFXType,
|
||||
} from '../define';
|
||||
import { IGFXInputAttribute } from '../input-assembler';
|
||||
import { GFXBlendState, GFXDepthStencilState, GFXRasterizerState } from '../pipeline-state';
|
||||
import { GFXColorAttachment, GFXDepthStencilAttachment } from '../render-pass';
|
||||
import { GFXUniformBlock, GFXUniformSampler, IGFXShaderMacro } from '../shader';
|
||||
import { GFXBufferSource } from '../buffer';
|
||||
|
||||
export enum WebGLGPUObjectType {
|
||||
UNKNOWN,
|
||||
|
||||
@@ -52,12 +52,10 @@ export class ForwardStage extends RenderStage {
|
||||
[camera.clearColor], camera.clearDepth, camera.clearStencil);
|
||||
|
||||
for (const item of queue.opaques) {
|
||||
cmdBuff.updateBuffer(item.model.localUBO, item.model.localUniforms.view);
|
||||
cmdBuff.execute([item.cmdBuff]);
|
||||
}
|
||||
|
||||
for (const item of queue.transparents) {
|
||||
cmdBuff.updateBuffer(item.model.localUBO, item.model.localUniforms.view);
|
||||
cmdBuff.execute([item.cmdBuff]);
|
||||
}
|
||||
|
||||
|
||||
@@ -363,7 +363,7 @@ export abstract class RenderPipeline {
|
||||
continue;
|
||||
}
|
||||
|
||||
model.updateRenderData();
|
||||
model.updateUBOs();
|
||||
|
||||
// add model pass to render queue
|
||||
this._queue.add(model, camera);
|
||||
|
||||
@@ -54,6 +54,10 @@ export class RenderQueue {
|
||||
|
||||
for (let i = 0; i < model.passes.length; ++i) {
|
||||
const pass = model.passes[i];
|
||||
|
||||
// update pass
|
||||
pass.update();
|
||||
|
||||
const cmdBuff = model.commandBuffers[i];
|
||||
|
||||
model.node.getWorldPosition();
|
||||
|
||||
@@ -85,7 +85,7 @@ export class TestModelStage extends RenderStage {
|
||||
this._scene._scene = this._scene;
|
||||
const modelCom = this._scene.addComponent('cc.ModelComponent');
|
||||
modelCom.material = this._material;
|
||||
modelCom.mesh = cc.utils.createMesh(cc.game._renderContext, cc.primitives.box({length: 1, width: 1, height: 1}));
|
||||
modelCom.mesh = cc.utils.createMesh(cc.game._gfxDevice, cc.primitives.box({length: 1, width: 1, height: 1}));
|
||||
this._model = modelCom._models[0];
|
||||
this._scene.setRotationFromEuler(45, 45, 45);
|
||||
this._scene._rot = this._scene._lrot;
|
||||
|
||||
@@ -64,9 +64,13 @@ const getDefines = (name: string, prog: IShaderInfo) => {
|
||||
};
|
||||
|
||||
export class Effect {
|
||||
public static getPassInfos (effect: EffectAsset, techIdx: number) {
|
||||
return effect.techniques[techIdx].passes;
|
||||
}
|
||||
|
||||
public static parseEffect (effect: EffectAsset, info?: IEffectInfo) {
|
||||
// techniques
|
||||
const { techIdx, defines } = info || {} as IEffectInfo;
|
||||
const { techIdx, defines } = info || {} as IEffectInfo;
|
||||
const tech = effect.techniques[techIdx || 0];
|
||||
const passNum = tech.passes.length;
|
||||
const passes: Pass[] = new Array(passNum);
|
||||
|
||||
@@ -8,7 +8,7 @@ import { GFXBindingType, GFXBufferUsageBit, GFXMemoryUsageBit, GFXPrimitiveMode,
|
||||
import { GFXDevice } from '../../gfx/device';
|
||||
import { GFXPipelineLayout } from '../../gfx/pipeline-layout';
|
||||
import { GFXBlendState, GFXBlendTarget, GFXDepthStencilState,
|
||||
GFXInputState, GFXPipelineState, GFXRasterizerState } from '../../gfx/pipeline-state';
|
||||
GFXInputState, GFXPipelineState, GFXRasterizerState, IGFXPipelineStateInfo } from '../../gfx/pipeline-state';
|
||||
import { GFXRenderPass } from '../../gfx/render-pass';
|
||||
import { GFXSampler } from '../../gfx/sampler';
|
||||
import { GFXShader } from '../../gfx/shader';
|
||||
@@ -24,6 +24,13 @@ export interface IPassInfoFull extends IPassInfo {
|
||||
globals: GFXBuffer;
|
||||
}
|
||||
|
||||
export interface IPassOverrides {
|
||||
bs: GFXBlendState;
|
||||
dss: GFXDepthStencilState;
|
||||
rs: GFXRasterizerState;
|
||||
primitive: GFXPrimitiveMode;
|
||||
}
|
||||
|
||||
const _type2fn = {
|
||||
[GFXType.INT]: (a: Float32Array, v: any) => a[0] = v,
|
||||
[GFXType.INT2]: (a: Float32Array, v: any) => vec2.array(a, v),
|
||||
@@ -114,7 +121,7 @@ export class Pass {
|
||||
if (!this._pipelineLayout) { console.error('create pipeline layout failed'); return; }
|
||||
this._shader = info.shader;
|
||||
this._renderPass = info.renderPass;
|
||||
this.createPipelineState(info);
|
||||
this._createPipelineState(info);
|
||||
|
||||
for (const u of info.blocks) {
|
||||
if (builtinRE.test(u.name)) {
|
||||
@@ -199,9 +206,18 @@ export class Pass {
|
||||
(this._bindingLayout as GFXBindingLayout).bindSampler(binding, value);
|
||||
}
|
||||
|
||||
public setStates (info: IPassInfo) {
|
||||
public rebuildWithOverrides (info: Partial<IPassInfo>, overrides: IPassOverrides) {
|
||||
if (this._pipelineState) { this._pipelineState.destroy(); }
|
||||
this.createPipelineState(info);
|
||||
const ors = Object.assign({}, overrides);
|
||||
this._createPipelineState(info, (states) => {
|
||||
Object.assign(states.rs, ors.rs);
|
||||
Object.assign(states.dss, ors.dss);
|
||||
for (let i = 0; i < ors.bs.targets.length; i++) {
|
||||
Object.assign(states.bs.targets[i], ors.bs.targets[i]);
|
||||
}
|
||||
delete ors.bs.targets;
|
||||
Object.assign(states.bs, ors.bs);
|
||||
});
|
||||
}
|
||||
|
||||
public destroy () {
|
||||
@@ -224,7 +240,7 @@ export class Pass {
|
||||
(this._bindingLayout as GFXBindingLayout).update();
|
||||
}
|
||||
|
||||
protected createPipelineState (info: IPassInfo) {
|
||||
protected _createPipelineState (info: Partial<IPassInfo>, override?: (states: IGFXPipelineStateInfo) => any) {
|
||||
if (info.primitive) { this._primitive = info.primitive; }
|
||||
if (info.stage) { this._stage = info.stage; }
|
||||
|
||||
@@ -252,6 +268,7 @@ export class Pass {
|
||||
rs: Object.assign(new GFXRasterizerState(), info.rasterizerState),
|
||||
shader: this._shader,
|
||||
};
|
||||
if (override) { override(stateInfo); }
|
||||
if (this._pipelineState) {
|
||||
this._pipelineState.initialize(stateInfo);
|
||||
} else {
|
||||
|
||||
@@ -39,7 +39,7 @@ export class Model {
|
||||
private _modelBounds: aabb;
|
||||
private _material: Material | null;
|
||||
private _cmdBuffers: GFXCommandBuffer[];
|
||||
private _localUniforms: UBOLocal | null;
|
||||
private _uboLocal: UBOLocal;
|
||||
private _localUBO: GFXBuffer | null;
|
||||
/**
|
||||
* Setup a default empty model
|
||||
@@ -62,12 +62,11 @@ export class Model {
|
||||
this._castShadow = false;
|
||||
this._material = null;
|
||||
this._cmdBuffers = new Array<GFXCommandBuffer>();
|
||||
this._localUniforms = null;
|
||||
this._uboLocal = new UBOLocal();
|
||||
this._localUBO = null;
|
||||
}
|
||||
|
||||
public initialize () {
|
||||
this._localUniforms = new UBOLocal();
|
||||
this._localUBO = cc.director.root.device.createBuffer({
|
||||
usage: GFXBufferUsageBit.UNIFORM | GFXBufferUsageBit.TRANSFER_DST,
|
||||
memUsage: GFXMemoryUsageBit.HOST,
|
||||
@@ -75,7 +74,7 @@ export class Model {
|
||||
stride: UBOLocal.SIZE,
|
||||
});
|
||||
if (this._localUBO) {
|
||||
this._localUBO.update(this._localUniforms.view);
|
||||
this._localUBO.update(this._uboLocal.view);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -102,22 +101,20 @@ export class Model {
|
||||
this._node._rot, this._node._scale, this._worldBounds);
|
||||
}
|
||||
|
||||
public updateRenderData () {
|
||||
public updateUBOs () {
|
||||
mat4.array(_temp_floatx16, this._node._mat);
|
||||
this._node._mat.invert(_temp_mat4);
|
||||
if (this._localUniforms) {
|
||||
this._localUniforms.view.set(_temp_floatx16, UBOLocal.MAT_WORLD_OFFSET);
|
||||
if (this._uboLocal) {
|
||||
this._uboLocal.view.set(_temp_floatx16, UBOLocal.MAT_WORLD_OFFSET);
|
||||
}
|
||||
mat4.array(_temp_floatx16, _temp_mat4);
|
||||
if (this._localUniforms) {
|
||||
this._localUniforms.view.set(_temp_floatx16, UBOLocal.MAT_WORLD_IT_OFFSET);
|
||||
if (this._uboLocal) {
|
||||
this._uboLocal.view.set(_temp_floatx16, UBOLocal.MAT_WORLD_IT_OFFSET);
|
||||
}
|
||||
|
||||
/*
|
||||
if (this._localUBO) {
|
||||
this._localUBO.update(this._localUniforms.view);
|
||||
this._localUBO.update(this._uboLocal.view);
|
||||
}
|
||||
*/
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -211,23 +208,27 @@ export class Model {
|
||||
|
||||
public recordCommandBuffer (index: number) {
|
||||
const pass = (this._material as Material).passes[index];
|
||||
const cmdBufferInfo = {
|
||||
allocator: cc.director.root.device.commandAllocator,
|
||||
type: GFXCommandBufferType.SECONDARY,
|
||||
};
|
||||
if (this._cmdBuffers[index] == null) {
|
||||
const cmdBufferInfo = {
|
||||
allocator: cc.director.root.device.commandAllocator,
|
||||
type: GFXCommandBufferType.SECONDARY,
|
||||
};
|
||||
this._cmdBuffers[index] = cc.director.root.device.createCommandBuffer(cmdBufferInfo);
|
||||
}
|
||||
if (this._localUBO) {
|
||||
pass.bindingLayout.bindBuffer(UBOLocal.BLOCK.binding, this._localUBO);
|
||||
}
|
||||
|
||||
const localUBO = this._localUBO as GFXBuffer;
|
||||
const subMesh = this._subMeshObject as IRenderingSubmesh;
|
||||
|
||||
pass.bindingLayout.bindBuffer(UBOLocal.BLOCK.binding, localUBO);
|
||||
pass.update();
|
||||
this._cmdBuffers[index].begin();
|
||||
this._cmdBuffers[index].bindPipelineState(pass.pipelineState);
|
||||
this._cmdBuffers[index].bindBindingLayout(pass.bindingLayout);
|
||||
this._cmdBuffers[index].bindInputAssembler((this._subMeshObject as IRenderingSubmesh).inputAssembler);
|
||||
this._cmdBuffers[index].draw((this._subMeshObject as IRenderingSubmesh).inputAssembler);
|
||||
this._cmdBuffers[index].end();
|
||||
|
||||
const cmdBuff = this._cmdBuffers[index];
|
||||
cmdBuff.begin();
|
||||
cmdBuff.bindPipelineState(pass.pipelineState);
|
||||
cmdBuff.bindBindingLayout(pass.bindingLayout);
|
||||
cmdBuff.bindInputAssembler(subMesh.inputAssembler);
|
||||
cmdBuff.draw(subMesh.inputAssembler);
|
||||
cmdBuff.end();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -246,8 +247,8 @@ export class Model {
|
||||
return this._cmdBuffers;
|
||||
}
|
||||
|
||||
get localUniforms (): UBOLocal {
|
||||
return this._localUniforms as UBOLocal;
|
||||
get uboLocal (): UBOLocal {
|
||||
return this._uboLocal;
|
||||
}
|
||||
|
||||
get localUBO (): GFXBuffer {
|
||||
|
||||
@@ -2,11 +2,11 @@ import { intersect, ray, triangle } from '../../3d/geom-utils';
|
||||
import { RecyclePool } from '../../3d/memop';
|
||||
import { _createSceneFun, Root } from '../../core/root';
|
||||
import { mat4, vec3 } from '../../core/vmath';
|
||||
import { GFXPrimitiveMode } from '../../gfx/define';
|
||||
import { Layers } from '../../scene-graph/layers';
|
||||
import { Camera, ICameraInfo } from './camera';
|
||||
import { Light } from './light';
|
||||
import { Model } from './model';
|
||||
import { GFXPrimitiveMode } from '../../gfx/define';
|
||||
|
||||
export interface IRenderSceneInfo {
|
||||
name: string;
|
||||
|
||||
@@ -37,7 +37,7 @@ export function createIA(device, data) {
|
||||
vfmt.push({ name: GFXAttributeName.ATTR_COLOR, format: GFXFormat.RGB32F });
|
||||
}
|
||||
|
||||
let vb = cc.director.root.device.createBuffer({
|
||||
let vb = device.createBuffer({
|
||||
usage: GFXBufferUsageBit.VERTEX,
|
||||
memUsage: GFXMemoryUsageBit.HOST | GFXMemoryUsageBit.DEVICE,
|
||||
size: verts.length * 4,
|
||||
@@ -46,7 +46,7 @@ export function createIA(device, data) {
|
||||
|
||||
vb.update(new Float32Array(verts));
|
||||
|
||||
let ib = cc.director.root.device.createBuffer({
|
||||
let ib = device.createBuffer({
|
||||
usage: GFXBufferUsageBit.INDEX,
|
||||
memUsage: GFXMemoryUsageBit.HOST | GFXMemoryUsageBit.DEVICE,
|
||||
size: data.indices.length * 2,
|
||||
@@ -55,7 +55,7 @@ export function createIA(device, data) {
|
||||
|
||||
ib.update(new Uint16Array(data.indices));
|
||||
|
||||
return cc.director.root.device.createInputAssembler({
|
||||
return device.createInputAssembler({
|
||||
attributes: vfmt,
|
||||
vertexBuffers: [vb],
|
||||
indexBuffer: ib,
|
||||
|
||||
Reference in New Issue
Block a user