get rid of all the encodings (#4355)

This commit is contained in:
Yun Hsiao Wu
2019-04-30 14:59:21 +08:00
committed by Weiliang Wang
parent c45f1a8bf7
commit 5a290fbc6d
3 changed files with 13 additions and 189 deletions
@@ -26,7 +26,7 @@
import { ccclass, executeInEditMode, executionOrder, menu, property } from '../../core/data/class-decorator';
import { Mat4 } from '../../core/value-types';
import { mat4 } from '../../core/vmath';
import { GFXDevice, GFXAPI } from '../../gfx/device';
import { GFXDevice } from '../../gfx/device';
import { __DEFER_BINDPOSE_COMPUTATION__, JointStorageKind, selectStorageKind, SkinningModel } from '../../renderer/models/skinning-model';
import { Node } from '../../scene-graph/node';
import { Mesh } from '../assets';
@@ -217,8 +217,7 @@ export class SkinningModelComponent extends ModelComponent {
CC_BINDPOSE_COMPUTATION_DEFERED: __DEFER_BINDPOSE_COMPUTATION__,
CC_USE_SKINNING: true,
CC_USE_JOINTS_TEXTURE: kind === JointStorageKind.textureRGBA32F || kind === JointStorageKind.textureRGBA8,
CC_USE_JOINTS_TEXTURE_RGBA8888: kind === JointStorageKind.textureRGBA8,
CC_SUPPORT_TEXEL_FETCH: device.gfxAPI === GFXAPI.WEBGL2,
CC_JOINTS_TEXTURE_RGBA8: kind === JointStorageKind.textureRGBA8,
});
}
super._onMaterialModified(index, mat);
+1 -1
View File
@@ -170,7 +170,7 @@ localBindingsDesc.set(UBOForwardLight.BLOCK.name, {
blockInfo: UBOForwardLight.BLOCK,
});
export const JointUniformCapacity = 24;
export const JointUniformCapacity = 30;
export class UBOSkinning {
public static MAT_JOINT_OFFSET: number = 0;
+10 -185
View File
@@ -15,6 +15,10 @@ import { Model } from '../scene/model';
import { RenderScene } from '../scene/render-scene';
const textureSizeBuffer = new Float32Array(4);
const skinningVectors = JointUniformCapacity * 3; // both Mat3x4 and DQ
const vertexVectorLeeway = 30; // the minimum number of free vectors guaranteed in vertex shader when using uniform joint storage
export const __DEFER_BINDPOSE_COMPUTATION__ = true;
export enum JointStorageKind {
textureRGBA8,
@@ -38,9 +42,6 @@ function isTextureStorage (storage: JointStorage): storage is ITextureStorage {
return storage.kind === JointStorageKind.textureRGBA8 || storage.kind === JointStorageKind.textureRGBA32F;
}
export const __FORCE_USE_UNIFORM_STORAGE__ = false;
export const __DEFER_BINDPOSE_COMPUTATION__ = true;
export class SkinningModel extends Model {
private _binded: null | {
jointStorage: JointStorage,
@@ -59,7 +60,7 @@ export class SkinningModel extends Model {
public bindSkeleton (skeleton: Skeleton) {
this._destroyJointStorage();
const storageKind = this._selectStorageKind();
const storageKind = selectStorageKind(this._device);
if (storageKind === JointStorageKind.textureRGBA32F ||
storageKind === JointStorageKind.textureRGBA8) {
@@ -171,17 +172,11 @@ export class SkinningModel extends Model {
this._binded = null;
}
}
private _selectStorageKind (): JointStorageKind {
return selectStorageKind(this._device);
}
}
export function selectStorageKind (device: GFXDevice): JointStorageKind {
if (__FORCE_USE_UNIFORM_STORAGE__) {
if (device.maxVertexUniformVectors - skinningVectors > vertexVectorLeeway) {
return JointStorageKind.uniform;
} else if (device.gfxAPI === GFXAPI.WEBGL2) {
return JointStorageKind.textureRGBA32F;
} else if (device.hasFeature(GFXFeature.TEXTURE_FLOAT)) {
return JointStorageKind.textureRGBA32F;
} else {
@@ -219,8 +214,8 @@ class MatrixTexture {
private static Mat4TextureAttributes = {
[Mat4TextureKind.rgba8888]: {
localStorage: Uint8Array,
pixelsPerMatrix: 16,
localStorage: Float32Array,
pixelsPerMatrix: 4,
capacityTable: MatrixTexture.createCapacityTable(1024, 16),
pixelFormat: PixelFormat.RGBA8888,
},
@@ -291,181 +286,11 @@ class MatrixTexture {
}
public set (iMatrix: number, matrix: mat4) {
if (this._kind === Mat4TextureKind.rgba32f) {
mat4.array(this._nativeData, matrix, iMatrix * 4 * 4);
} else {
const base = iMatrix * 16 * 4;
const o = this._nativeData as Uint8Array;
encodeIEEE754Single(matrix.m00, o, base + 0 * 4);
encodeIEEE754Single(matrix.m01, o, base + 1 * 4);
encodeIEEE754Single(matrix.m02, o, base + 2 * 4);
encodeIEEE754Single(matrix.m03, o, base + 3 * 4);
encodeIEEE754Single(matrix.m04, o, base + 4 * 4);
encodeIEEE754Single(matrix.m05, o, base + 5 * 4);
encodeIEEE754Single(matrix.m06, o, base + 6 * 4);
encodeIEEE754Single(matrix.m07, o, base + 7 * 4);
encodeIEEE754Single(matrix.m08, o, base + 8 * 4);
encodeIEEE754Single(matrix.m09, o, base + 9 * 4);
encodeIEEE754Single(matrix.m10, o, base + 10 * 4);
encodeIEEE754Single(matrix.m11, o, base + 11 * 4);
encodeIEEE754Single(matrix.m12, o, base + 12 * 4);
encodeIEEE754Single(matrix.m13, o, base + 13 * 4);
encodeIEEE754Single(matrix.m14, o, base + 14 * 4);
encodeIEEE754Single(matrix.m15, o, base + 15 * 4);
}
// use identical memory layout for all texture formats
mat4.array(this._nativeData, matrix, iMatrix * 4 * 4);
}
public commit () {
this._texture.directUpdate(this._nativeData.buffer);
}
}
const encodeIEEE754Single = (() => {
const SIGNIFICAND_BITS_COUNT_WITHOUT_SIGN_BIT = 23;
const EXPONENT_BITS_COUNT = 8;
const BIAS = 127;
const EXP2_XX_2 = exp2(SIGNIFICAND_BITS_COUNT_WITHOUT_SIGN_BIT - 8);
const EXP2_XX_3 = exp2(EXPONENT_BITS_COUNT);
const EXP2_XX_4 = exp2(SIGNIFICAND_BITS_COUNT_WITHOUT_SIGN_BIT);
const EXP2_XX_5 = exp2(-(SIGNIFICAND_BITS_COUNT_WITHOUT_SIGN_BIT - 8));
function step (edge: number, x: number): number {
return x < edge ? 0.0 : 1.0;
}
function mod (x: number, y: number): number {
return x - y * Math.floor(x / y);
}
function exp2 (x: number): number {
return Math.pow(2, x);
}
function decode (r: number, g: number, b: number, a: number) {
// http://class.ece.iastate.edu/arun/Cpre305/ieee754/ie4.html
// http://class.ece.iastate.edu/arun/Cpre305/ieee754/ie5.html
const HIGEST_BIT_R = step(128.0, r);
const LOWER_7BITS_R = mod(r, 128.0);
const HIGEST_BIT_G = step(128.0, g);
const LOWER_7BITS_G = mod(g, 128.0);
const SHIFT_LEFT_1 = 2.0;
const SHIFT_LEFT_8 = 256.0;
const SHIFT_LEFT_16 = 65536.0;
const ONE_SHIFT_LEFT_27 = 0x800000;
const Sign = 1.0 - HIGEST_BIT_R * 2.0; // 1 or -1
const ExponentAfterBIAS = LOWER_7BITS_R * SHIFT_LEFT_1 + HIGEST_BIT_G;
// From Arjan's answer.
const Exponent = ExponentAfterBIAS - BIAS; // Undo bias
if (Exponent === -BIAS) {
return 0;
}
const Mantissa = LOWER_7BITS_G * SHIFT_LEFT_16 + b * SHIFT_LEFT_8 + a + ONE_SHIFT_LEFT_27;
// From Arjan's answer.
// Equal to Sign * exp2(Exponent) * (Mantissa * exp2(-SIGNIFICAND_BITS_COUNT_WITHOUT_SIGN_BIT));
// Mantissa * exp2(-SIGNIFICAND_BITS_COUNT_WITHOUT_SIGN_BIT means: xxxxx => 1.xxxxx
return Sign * exp2(Exponent - SIGNIFICAND_BITS_COUNT_WITHOUT_SIGN_BIT) * Mantissa;
}
interface IColor { r: number; g: number; b: number; a: number; }
function encode1 (f: number, out: IColor) {
f = Math.fround(f);
// https://stackoverflow.com/questions/7059962/how-do-i-convert-a-vec4-rgba-value-to-a-float
// http://www.shaderific.com/glsl-functions/
const e = 5.0;
const F = Math.abs(f);
// From Arjan's answer.
if (F === 0) {
out.r = 0;
out.g = 0;
out.b = 0;
out.a = 0;
return;
}
const Sign = step(0.0, -f);
let Exponent = Math.floor(Math.log2(F));
// Original.
// const Mantissa = (exp2(- Exponent) * F);
// Exponent = Math.floor(Math.log2(F) + BIAS) + Math.floor(Math.log2(Mantissa));
// From Arjan's answer.
const Mantissa = F / exp2(Exponent);
if (Mantissa < 1) {
Exponent -= 1;
}
Exponent += BIAS;
// The Highest bit of r is sign, 1 for negative, 0 for positive.
// The lower 7 bits of r store the higher 7 bits of exponent.
out.r = 128.0 * Sign + Math.floor(Exponent * 0.5);
// The Highest bit of g stores the lowest bit of exponent.
// The lower 7 bits of g stores the higher 7 bits of mantissa.
out.g = 128.0 * mod(Exponent, 2.0) + mod(Math.floor(Mantissa * 128.0), 128.0);
// b stores the middle 8 bits of mantissa.
out.b = Math.floor(mod(Math.floor(Mantissa * EXP2_XX_2), EXP2_XX_3));
// a stores the lower 8 bits of mantissa.
out.a = Math.floor(EXP2_XX_4 * mod(Mantissa, EXP2_XX_5));
}
const encode2 = (() => {
const f32storage = new Float32Array(1);
const uints = new Uint8Array(f32storage.buffer);
function isNegativeZero (f: number) {
return f === 0 && 1 / f === -Infinity;
}
if (cc.sys.isLittleEndian) {
return (f: number, out: IColor) => {
f32storage[0] = f;
out.r = uints[3];
out.g = uints[2];
out.b = uints[1];
out.a = uints[0];
if (isNegativeZero(f)) {
out.r = 0;
}
};
} else {
return (f: number, out: IColor) => {
f32storage[0] = f;
out.r = uints[0];
out.g = uints[1];
out.b = uints[2];
out.a = uints[3];
if (isNegativeZero(f)) {
out.r = 0;
}
};
}
})();
return (() => {
const tmpColor: IColor = { r: 0, g: 0, b: 0, a: 0 };
return (f: number, output: Uint8Array, offset: number) => {
encode2(f, tmpColor);
// Verification.
// const { r, g, b, a } = tmpColor;
// const x = decode32(r, g, b, a);
// if (Math.abs(x - f) > 0.00001) {
// f = x;
// }
output[offset + 0] = tmpColor.r;
output[offset + 1] = tmpColor.g;
output[offset + 2] = tmpColor.b;
output[offset + 3] = tmpColor.a;
};
})();
})();