Merge pull request #11 from YunHsiao/gfx

API fitting
This commit is contained in:
Weiliang Wang
2019-01-08 14:24:44 +08:00
committed by GitHub
9 changed files with 404 additions and 174 deletions
+8 -14
View File
@@ -91,25 +91,19 @@ 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.setUniform(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);
pass.setTextureView(handle, val._texView);
const binding = pass.getBindingFromName(name);
pass.bindTextureView(binding, val._texView);
}
public copy(mat: Material) {
@@ -152,13 +146,13 @@ 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.setUniform(handle, props[p]);
}
let textures = this._textures[i];
if (!textures) { textures = this._textures[i] = {}; }
for (const t of Object.keys(textures)) {
const handle = pass.getHandleFromName(t);
pass.setTextureView(handle, textures[t]._texView);
const binding = pass.getBindingFromName(t);
pass.bindTextureView(binding, textures[t]._texView);
}
});
} else {
+43
View File
@@ -0,0 +1,43 @@
/****************************************************************************
Copyright (c) 2013-2016 Chukong Technologies Inc.
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
http://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 { RawAsset } from "./raw-asset";
export class Asset extends RawAsset {
protected _native: string;
protected loaded: boolean;
public _nativeAsset: any;
public nativeUrl: string;
static preventDeferredLoadDependents: boolean;
static preventPreloadNativeObject: boolean;
static deserialize(data: string): object;
public createNode: (err: string, node: object) => void | null;
public toString(): string;
public serialize(): string;
protected _setRawAsset(filename: string, inLibrary: boolean): void;
}
+112 -69
View File
@@ -44,92 +44,73 @@ import { RawAsset } from './raw-asset';
* - _nativeAsset 的 getset 方法<br/>
* - cc.Object._serialize<br/>
* - cc.Object._deserialize<br/>
*
* @class Asset
* @extends RawAsset
*/
@ccclass('cc.Asset')
export class Asset extends RawAsset {
/**
* !#en Indicates whether its dependent raw assets can support deferred load
* if the owner scene (or prefab) is marked as `asyncLoadAssets`.
* !#zh 当场景或 Prefab 被标记为 `asyncLoadAssets`,禁止延迟加载该资源所依赖的其它 RawAsset。
*
* @property {Boolean} preventDeferredLoadDependents
* @default false
* @static
*/
public static preventDeferredLoadDependents = false;
/**
* !#en Indicates whether its native object should be preloaded from native url.
* !#zh 禁止预加载原生对象。
*
* @property {Boolean} preventPreloadNativeObject
* @default false
* @static
*/
public static preventPreloadNativeObject = false;
/**
* 应 AssetDB 要求提供这个方法
*/
protected static deserialize(data: string): Asset {
return cc.deserialize(data);
constructor () {
super();
/**
* !#en
* Whether the asset is loaded or not
* !#zh
* 该资源是否已经成功加载
*
* @property loaded
* @type {Boolean}
*/
this.loaded = true;
}
/**
* !#en
* Create a new node using this asset in the scene.<br/>
* If this type of asset dont have its corresponding node type, this method should be null.
* !#zh
* 使用该资源在场景中创建一个新节点。<br/>
* 如果这类资源没有相应的节点类型,该方法应该是空的。
*
* @param {Function} callback
* @param {String} callback.error - null or the error info
* @param {Object} callback.node - the created node or null
*/
public createNode: null | ((callback: (err: string, node: object) => void) => void) = null;
protected loaded = true;
/**
* Serializable url for native asset.
*/
@property
protected _native = '';
/**
* !#en
* Returns the url of this asset's native object, if none it will returns an empty string.
* !#zh
* 返回该资源对应的目标平台资源的 URL,如果没有将返回一个空字符串。
* @property nativeUrl
* @type {String}
* @readOnly
*/
@property({
visible: false,
visible: false
})
get nativeUrl(): Readonly<string> {
get nativeUrl () {
if (this._native) {
const name = this._native;
var name = this._native;
if (name.charCodeAt(0) === 47) { // '/'
// remove library tag
// not imported in library, just created on-the-fly
return name.slice(1);
}
if (cc.AssetLibrary) {
const base = cc.AssetLibrary.getLibUrlNoExt(this._uuid, true);
var base = cc.AssetLibrary.getLibUrlNoExt(this._uuid, true);
if (name.charCodeAt(0) === 46) { // '.'
// imported in dir where json exist
return base + name;
} else {
}
else {
// imported in an independent dir
return base + '/' + name;
}
} else {
}
else {
cc.errorID(6400);
}
}
return '';
}
/**
* Serializable url for native asset.
* @property {String} _native
* @default ""
* @private
*/
@property
_native = ""
/**
* The underlying native asset of this asset if one is available.
* This property can be used to access additional details or functionality releated to the asset.
@@ -140,42 +121,104 @@ export class Asset extends RawAsset {
* @type {any}
*/
@property
get _nativeAsset() { return null; }
set _nativeAsset(obj) {}
get _nativeAsset () {
}
set _nativeAsset (obj) {
}
/**
* 应 AssetDB 要求提供这个方法
*
* @method deserialize
* @param {String} data
* @return {Asset}
* @static
* @private
*/
static deserialize (data) {
return cc.deserialize(data);
}
/**
* !#en Indicates whether its dependent raw assets can support deferred load if the owner scene (or prefab) is marked as `asyncLoadAssets`.
* !#zh 当场景或 Prefab 被标记为 `asyncLoadAssets`,禁止延迟加载该资源所依赖的其它 RawAsset。
*
* @property {Boolean} preventDeferredLoadDependents
* @default false
* @static
*/
static preventDeferredLoadDependents = false;
/**
* !#en Indicates whether its native object should be preloaded from native url.
* !#zh 禁止预加载原生对象。
*
* @property {Boolean} preventPreloadNativeObject
* @default false
* @static
*/
static preventPreloadNativeObject = false;
/**
* Returns the string representation of the object.
*
* The `Asset` object overrides the `toString()` method of the `Object` object.
* JavaScript calls the toString() method automatically when an asset is to be represented
* as a text value or when a texture is referred to in a string concatenation.
* JavaScript calls the toString() method automatically when an asset is to be represented as a text value or when a texture is referred to in a string concatenation.
*
* For assets of the native type, it will return `this.nativeUrl`.
* Otherwise, an empty string is returned.
* This method may be overwritten by subclasses.
*
* @method toString
* @return {String}
*/
public toString(): string {
toString () {
return this.nativeUrl;
}
/**
* 应 AssetDB 要求提供这个方法
*
* @method serialize
* @return {String}
* @private
*/
serialize () {
return Editor.serialize(this);
}
/**
* Set native file name for this asset.
* @seealso nativeUrl
*
* @method _setRawAsset
* @param {String} filename
* @param {Boolean} [inLibrary=true]
* @private
*/
protected _setRawAsset(filename: string, inLibrary: boolean = true) {
_setRawAsset (filename, inLibrary) {
if (inLibrary !== false) {
this._native = filename;
} else {
this._native = filename || undefined;
}
else {
this._native = '/' + filename; // simply use '/' to tag location where is not in the library
}
}
/**
* 应 AssetDB 要求提供这个方法
*/
protected serialize(): string {
return Editor.serialize(this);
}
}
/**
* !#en
* Create a new node using this asset in the scene.<br/>
* If this type of asset dont have its corresponding node type, this method should be null.
* !#zh
* 使用该资源在场景中创建一个新节点。<br/>
* 如果这类资源没有相应的节点类型,该方法应该是空的。
*
* @method createNode
* @param {Function} callback
* @param {String} callback.error - null or the error info
* @param {Object} callback.node - the created node or null
*/
Asset.prototype.createNode = null;
cc.Asset = Asset;
+32
View File
@@ -0,0 +1,32 @@
/****************************************************************************
Copyright (c) 2013-2016 Chukong Technologies Inc.
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
http://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 { CCObject } from '../core/data/object';
export class RawAsset extends CCObject {
_uuid: string;
static isRawAssetType(ctor: Function): boolean;
}
@@ -24,34 +24,45 @@
THE SOFTWARE.
****************************************************************************/
import { ccclass } from '../core/data/class-decorator';
import { CCObject } from '../core/data/object';
import { isChildClassOf } from '../core/utils/js';
function enumerable(value: boolean) {
return (target: any, propertyKey: string) => {
const descriptor = Object.getOwnPropertyDescriptor(target, propertyKey) || {};
if (descriptor.enumerable !== value) {
descriptor.enumerable = value;
Object.defineProperty(target, propertyKey, descriptor);
}
};
}
import { CCObject } from '../core/data/object';
import { ccclass } from '../core/data/class-decorator';
/**
* !#en
* The base class for registering asset types.
* !#zh
* 注册用的资源基类。
*
* @class RawAsset
* @extends Object
*/
@ccclass('cc.RawAsset')
export class RawAsset extends CCObject {
protected static isRawAssetType(ctor: Function): boolean {
return isChildClassOf(ctor, RawAsset) && !isChildClassOf(ctor, cc.Asset);
constructor () {
super();
/**
* @property _uuid
* @type {String}
* @private
*/
Object.defineProperty(this, '_uuid', {
value: '',
writable: true,
// enumerable is false by default, to avoid uuid being assigned to empty string during destroy
});
}
@enumerable(false)
protected _uuid = '';
/**
* @method isRawAssetType
* @param {Function} ctor
* @returns {Boolean}
* @static
* @private
*/
static isRawAssetType (ctor) {
return isChildClassOf(ctor, cc.RawAsset) && !isChildClassOf(ctor, cc.Asset);
}
}
cc.RawAsset = RawAsset;
+47
View File
@@ -0,0 +1,47 @@
/****************************************************************************
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
http://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.
****************************************************************************/
export class CCObject {
protected _name: string;
protected _objFlags: number;
static _deferredDestroy(): void;
public name: string;
public isValid: boolean;
public isRealValid: boolean;
public destroy(): boolean;
public realDestroyInEditor(): void;
public _clearDeferredDestroyTimer(): void;
protected _destruct(): void;
protected _destroyImmediate(): void;
protected _serialize: (exporting: boolean) => object | null;
protected _deserialize: (data: object, ctx: object) => object | null;
protected _onPreDestroy: () => void | null;
protected Flags: object;
}
@@ -38,8 +38,8 @@ const DontDestroy = 1 << 6;
const Destroying = 1 << 7;
const Deactivating = 1 << 8;
const LockedInEditor = 1 << 9;
// const HideInGame = 1 << 9;
const HideInHierarchy = 1 << 10;
//var HideInGame = 1 << 9;
var HideInHierarchy = 1 << 10;
const IsOnEnableCalled = 1 << 11;
const IsEditorOnEnableCalled = 1 << 12;
@@ -54,7 +54,7 @@ const IsAnchorLocked = 1 << 19;
const IsSizeLocked = 1 << 20;
const IsPositionLocked = 1 << 21;
// const Hide = HideInGame | HideInEditor;
//var Hide = HideInGame | HideInEditor;
// should not clone or serialize these flags
const PersistentMask = ~(ToDestroy | Dirty | Destroying | DontDestroy | Deactivating |
IsPreloadStarted | IsOnLoadStarted | IsOnLoadCalled | IsStartCalled |
@@ -62,34 +62,37 @@ const PersistentMask = ~(ToDestroy | Dirty | Destroying | DontDestroy | Deactiva
IsRotationLocked | IsScaleLocked | IsAnchorLocked | IsSizeLocked | IsPositionLocked
/*RegisteredInEditor*/);
const objectsToDestroy: CCObject[] = [];
let deferredDestroyTimer = null;
var objectsToDestroy = [];
var deferredDestroyTimer = null;
function compileDestruct(obj: object, ctor) {
const shouldSkipId = obj instanceof cc._BaseNode || obj instanceof cc.Component;
const idToSkip = shouldSkipId ? '_id' : null;
function compileDestruct (obj, ctor) {
var shouldSkipId = obj instanceof cc._BaseNode || obj instanceof cc.Component;
var idToSkip = shouldSkipId ? '_id' : null;
const propsToReset = {};
for (const key of Object.keys(obj)) {
if (key === idToSkip) {
continue;
}
switch (typeof obj[key]) {
case 'string':
propsToReset[key] = '';
break;
case 'object':
case 'function':
propsToReset[key] = null;
break;
var key, propsToReset = {};
for (key in obj) {
if (obj.hasOwnProperty(key)) {
if (key === idToSkip) {
continue;
}
switch (typeof obj[key]) {
case 'string':
propsToReset[key] = '';
break;
case 'object':
case 'function':
propsToReset[key] = null;
break;
}
}
}
// Overwrite propsToReset according to Class
if (CCClass._isCCClass(ctor)) {
const attrs = cc.Class.Attr.getClassAttrs(ctor);
const propList = ctor.__props__;
for (const key of propList) {
const attrKey = key + cc.Class.Attr.DELIMETER + 'default';
var attrs = cc.Class.Attr.getClassAttrs(ctor);
var propList = ctor.__props__;
for (var i = 0; i < propList.length; i++) {
key = propList[i];
var attrKey = key + cc.Class.Attr.DELIMETER + 'default';
if (attrKey in attrs) {
if (shouldSkipId && key === '_id') {
continue;
@@ -112,24 +115,26 @@ function compileDestruct(obj: object, ctor) {
if (CC_SUPPORT_JIT) {
// compile code
let func = '';
for (const key of Object.keys(propsToReset)) {
let statement: string;
var func = '';
for (key in propsToReset) {
var statement;
if (CCClass.IDENTIFIER_RE.test(key)) {
statement = 'o.' + key + '=';
} else {
}
else {
statement = 'o[' + CCClass.escapeForJS(key) + ']=';
}
let val = propsToReset[key];
var val = propsToReset[key];
if (val === '') {
val = '""';
}
func += (statement + val + ';\n');
}
return Function('o', func);
} else {
return (o: object) => {
for (const key of Object.keys(propsToReset)) {
}
else {
return function (o) {
for (var key in propsToReset) {
o[key] = propsToReset[key];
}
};
@@ -144,10 +149,26 @@ function compileDestruct(obj: object, ctor) {
* @private
*/
class CCObject {
protected static _deferredDestroy() {
const deleteCount = objectsToDestroy.length;
for (let i = 0; i < deleteCount; ++i) {
const obj = objectsToDestroy[i];
constructor (name = '') {
/**
* @property {String} _name
* @default ""
* @private
*/
this._name = name;
/**
* @property {Number} _objFlags
* @default 0
* @private
*/
this._objFlags = 0;
}
static _deferredDestroy () {
var deleteCount = objectsToDestroy.length;
for (var i = 0; i < deleteCount; ++i) {
var obj = objectsToDestroy[i];
if (!(obj._objFlags & Destroyed)) {
obj._destroyImmediate();
}
@@ -156,7 +177,8 @@ class CCObject {
// but we only destroy the objects which called destory in this frame.
if (deleteCount === objectsToDestroy.length) {
objectsToDestroy.length = 0;
} else {
}
else {
objectsToDestroy.splice(0, deleteCount);
}
@@ -165,23 +187,20 @@ class CCObject {
}
}
protected _name = '';
protected _objFlags = 0;
constructor(name = '') {
this._name = name;
}
// MEMBER
/**
* !#en The name of the object.
* !#zh 该对象的名称。
* @property {String} name
* @default ""
* @example
* obj.name = "New Obj";
*/
get name() {
get name () {
return this._name;
}
set name(value) {
set name (value) {
this._name = value;
}
@@ -195,19 +214,20 @@ class CCObject {
*
* !#zh
* 表示该对象是否可用(被 destroy 后将不可用)。<br>
* 当一个对象的 `destroy` 调用以后,会在这一帧结束后才真正销毁。因此从下一帧开始 `isValid` 就会返回 false
* 而当前帧内 `isValid` 仍然会是 true。如果希望判断当前帧是否调用过 `destroy`
* 请使用 `cc.isValid(obj, true)`,不过这往往是特殊的业务需求引起的,通常情况下不需要这样。
* 当一个对象的 `destroy` 调用以后,会在这一帧结束后才真正销毁。因此从下一帧开始 `isValid` 就会返回 false,而当前帧内 `isValid` 仍然会是 true。如果希望判断当前帧是否调用过 `destroy`,请使用 `cc.isValid(obj, true)`,不过这往往是特殊的业务需求引起的,通常情况下不需要这样。
*
* @property {Boolean} isValid
* @default true
* @readOnly
* @example
* const node = new cc.Node();
* var node = new cc.Node();
* cc.log(node.isValid); // true
* node.destroy();
* cc.log(node.isValid); // true, still valid in this frame
* // after a frame...
* cc.log(node.isValid); // false, destroyed in the end of last frame
*/
get isValid(): Readonly<boolean> {
get isValid () {
return !(this._objFlags & Destroyed);
}
@@ -221,8 +241,12 @@ class CCObject {
* 销毁该对象,并释放所有它对其它对象的引用。<br/>
* 实际销毁操作会延迟到当前帧渲染前执行。从下一帧开始,该对象将不再可用。
* 您可以在访问对象之前使用 cc.isValid(obj) 来检查对象是否已被销毁。
* @method destroy
* @return {Boolean} whether it is the first time the destroy being called
* @example
* obj.destroy();
*/
public destroy() {
destroy () {
if (this._objFlags & Destroyed) {
cc.warnID(5000);
return false;
@@ -246,7 +270,7 @@ class CCObject {
* NOTE: this method will not clear the getter or setter functions which defined in the instance of CCObject.
* You can override the _destruct method if you need, for example:
* _destruct: function () {
* for (const key in this) {
* for (var key in this) {
* if (this.hasOwnProperty(key)) {
* switch (typeof this[key]) {
* case 'string':
@@ -260,10 +284,12 @@ class CCObject {
* }
* }
*
* @method _destruct
* @private
*/
protected _destruct() {
const ctor = this.constructor;
let destruct = ctor.__destruct__;
_destruct () {
var ctor = this.constructor;
var destruct = ctor.__destruct__;
if (!destruct) {
destruct = compileDestruct(this, ctor);
js.value(ctor, '__destruct__', destruct, true);
@@ -271,7 +297,7 @@ class CCObject {
destruct(this);
}
protected _destroyImmediate() {
_destroyImmediate () {
if (this._objFlags & Destroyed) {
cc.errorID(5000);
return;
@@ -289,7 +315,7 @@ class CCObject {
}
}
const prototype = CCObject.prototype;
let prototype = CCObject.prototype;
if (CC_EDITOR || CC_TEST) {
js.get(prototype, 'isRealValid', function () {
return !(this._objFlags & RealDestroyed);
@@ -483,26 +509,27 @@ js.value(CCObject, 'Flags', {
* @param {Boolean} [strictMode=false] - If true, Object called destroy() in this frame will also treated as invalid.
* @return {Boolean} whether is valid
* @example
* const node = new cc.Node();
* var node = new cc.Node();
* cc.log(cc.isValid(node)); // true
* node.destroy();
* cc.log(cc.isValid(node)); // true, still valid in this frame
* // after a frame...
* cc.log(cc.isValid(node)); // false, destroyed in the end of last frame
*/
cc.isValid = (value, strictMode) => {
cc.isValid = function (value, strictMode) {
if (typeof value === 'object') {
return !!value && !(value._objFlags & (strictMode ? (Destroyed | ToDestroy) : Destroyed));
} else {
}
else {
return typeof value !== 'undefined';
}
};
if (CC_EDITOR || CC_TEST) {
js.value(CCObject, '_willDestroy', (obj) => {
js.value(CCObject, '_willDestroy', function (obj) {
return !(obj._objFlags & Destroyed) && (obj._objFlags & ToDestroy) > 0;
});
js.value(CCObject, '_cancelDestroy', (obj) => {
js.value(CCObject, '_cancelDestroy', function (obj) {
obj._objFlags &= ~ToDestroy;
js.array.fastRemove(objectsToDestroy, obj);
});
@@ -74,12 +74,14 @@ export class TestMaterialStage extends RenderStage {
// material
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);
const handle2 = pass.getHandleFromName('u_color');
pass.setBlockMember(handle2, cc.color('#ffffff'));
const texBinding = pass.getBindingFromName('u_sampler');
pass.bindTextureView(texBinding, <GFXTextureView>this._texView);
const colorHandle = pass.getHandleFromName('u_color');
pass.setUniform(colorHandle, cc.color('#ffffff'));
pass.update();
this._material.inited = true;
}
const cmdBuff = <GFXCommandBuffer>this._cmdBuff;
this._renderArea.width = view.width;
this._renderArea.height = view.height;
+38 -7
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,31 @@ export class Pass {
return this._handleMap[name];
}
public setBlockMember(handle: number, value: any) {
public getBindingFromName(name: string) {
return parseHandle(this.getHandleFromName(name))[0];
}
public setUniform(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) {
const [ binding ] = parseHandle(handle);
if (this._bindingLayout) {
this._bindingLayout.bindTextureView(binding, value);
this._bindingLayout.update();
public bindTextureView(binding: number, value: GFXTextureView) {
const bl = this._bindingLayout;
if (bl && bl.getBindingUnit(binding).texView !== value) {
bl.bindTextureView(binding, value);
this._layoutDirty = true;
}
}
public bindSampler(binding: number, value: GFXSampler) {
const bl = this._bindingLayout;
if (bl && bl.getBindingUnit(binding).sampler !== value) {
bl.bindSampler(binding, value);
this._layoutDirty = true;
}
}
@@ -181,6 +197,21 @@ 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);
block.dirty = false;
}
}
if (this._bindingLayout && this._layoutDirty) {
this._bindingLayout.update();
this._layoutDirty = false;
}
}
protected createPipelineState(info: PassInfoBase) {
if (info.primitive) { this._primitive = info.primitive; }
if (info.stage) { this._stage = info.stage; }