API fitting

This commit is contained in:
Yun Hsiao Wu
2019-01-08 10:24:13 +08:00
parent 633780c968
commit abb02c2ef0
3 changed files with 32 additions and 18 deletions
+4 -10
View File
@@ -91,21 +91,15 @@ export class Material extends Asset {
}
public setProperty(name: string, val: any, passIdx = 0) {
if (passIdx >= this._passes.length) {
console.warn('illegal pass index.');
return;
}
console.assert(passIdx < this._passes.length, 'illegal pass index.');
this._props[passIdx][name] = val;
const pass = this._passes[passIdx];
const handle = pass.getHandleFromName(name);
pass.setBlockMember(handle, val);
pass.setUniformMember(handle, val);
}
public setTexture(name: string, val: TextureBase, passIdx = 0) {
if (passIdx >= this._passes.length) {
console.warn('illegal pass index.');
return;
}
console.assert(passIdx < this._passes.length, 'illegal pass index.');
this._textures[passIdx][name] = val;
const pass = this._passes[passIdx];
const handle = pass.getHandleFromName(name);
@@ -152,7 +146,7 @@ export class Material extends Asset {
if (!props) { props = this._props[i] = {}; }
for (const p of Object.keys(props)) {
const handle = pass.getHandleFromName(p);
pass.setBlockMember(handle, props[p]);
pass.setUniformMember(handle, props[p]);
}
let textures = this._textures[i];
if (!textures) { textures = this._textures[i] = {}; }
@@ -75,11 +75,13 @@ export class TestMaterialStage extends RenderStage {
this._material.effectName = 'test'; // parsed-effect file is embedded in cocos/3d/builtin/effects.js
const pass = this._material.passes[0];
const handle1 = pass.getHandleFromName('u_sampler');
pass.setTextureView(handle1, this._texView);
pass.setTextureView(handle1, <GFXTextureView>this._texView);
const handle2 = pass.getHandleFromName('u_color');
pass.setBlockMember(handle2, cc.color('#ffffff'));
pass.setUniformMember(handle2, cc.color('#ffffff'));
pass.update();
this._material.inited = true;
}
const cmdBuff = <GFXCommandBuffer>this._cmdBuff;
this._renderArea.width = view.width;
this._renderArea.height = view.height;
+24 -6
View File
@@ -12,6 +12,7 @@ import { GFXBlendState, GFXBlendTarget, GFXDepthStencilState,
import { GFXRenderPass } from '../../gfx/render-pass';
import { GFXSampler } from '../../gfx/sampler';
import { GFXShader } from '../../gfx/shader';
import { GFXTextureView } from '../../gfx/texture-view';
import { RenderPassStage } from '../../pipeline/render-pipeline';
import { PropertyMap } from './effect';
@@ -56,6 +57,7 @@ const parseHandle = (handle: number) => [(handle & bindingMask) >> 16, handle &
interface Block {
buffer: ArrayBuffer;
views: Float32Array[];
dirty: boolean;
}
export class Pass {
@@ -72,6 +74,7 @@ export class Pass {
protected _handleMap: Record<string, number> = {};
protected _typeMap: Record<number, GFXType> = {};
protected _blocks: Block[] = [];
protected _layoutDirty: boolean = false;
// external references
protected _device: GFXDevice;
protected _shader: GFXShader | null = null;
@@ -117,6 +120,7 @@ export class Pass {
const block: Block = this._blocks[u.binding] = {
buffer: new ArrayBuffer(u.size),
views: [],
dirty: false,
};
u.members.reduce((acc, cur, idx) => {
// create property-specific buffer views
@@ -152,19 +156,20 @@ export class Pass {
return this._handleMap[name];
}
public setBlockMember(handle: number, value: any) {
public setUniformMember(handle: number, value: any) {
const [ binding, idx ] = parseHandle(handle);
const block = this._blocks[binding];
const type = this._typeMap[handle];
_type2fn[type](block.views[idx], value);
this._buffers[binding].update(block.buffer);
block.dirty = true;
}
public setTextureView(handle: number, value: any) {
public setTextureView(handle: number, value: GFXTextureView) {
const [ binding ] = parseHandle(handle);
if (this._bindingLayout) {
this._bindingLayout.bindTextureView(binding, value);
this._bindingLayout.update();
const bl = this._bindingLayout;
if (bl && bl.getBindingUnit(binding).texView !== value) {
bl.bindTextureView(binding, value);
this._layoutDirty = true;
}
}
@@ -181,6 +186,19 @@ export class Pass {
if (this._pipelineState) { this._pipelineState.destroy(); this._pipelineState = null; }
}
public update() {
const len = this._blocks.length;
for (let i = 0; i < len; i++) {
const block = this._blocks[i];
if (block.dirty) {
this._buffers[i].update(block.buffer);
}
}
if (this._bindingLayout && this._layoutDirty) {
this._bindingLayout.update();
}
}
protected createPipelineState(info: PassInfoBase) {
if (info.primitive) { this._primitive = info.primitive; }
if (info.stage) { this._stage = info.stage; }