mirror of
https://github.com/cocos/cocos4.git
synced 2026-12-01 07:42:38 +08:00
opt encode32 (#4313)
This commit is contained in:
committed by
Weiliang Wang
parent
1fb943aac8
commit
ce456e3d37
Vendored
+9
-9
File diff suppressed because one or more lines are too long
@@ -66,7 +66,8 @@ export class SkinningModel extends Model {
|
||||
const mat4TextureKind = storageKind === JointStorageKind.textureRGBA32F ? Mat4TextureKind.rgba32f : Mat4TextureKind.rgba8888;
|
||||
const mat4Texture = new MatrixTexture(
|
||||
__DEFER_BINDPOSE_COMPUTATION__ ? skeleton.joints.length * 2 : skeleton.joints.length,
|
||||
mat4TextureKind);
|
||||
mat4TextureKind,
|
||||
this._device);
|
||||
|
||||
if (__DEFER_BINDPOSE_COMPUTATION__) {
|
||||
for (let iJoint = 0; iJoint < skeleton.bindposes.length; ++iJoint) {
|
||||
@@ -182,7 +183,7 @@ export function selectStorageKind (device: GFXDevice): JointStorageKind {
|
||||
} else if (device.gfxAPI === GFXAPI.WEBGL2) {
|
||||
return JointStorageKind.uniform; // BUG Now
|
||||
} else if (device.hasFeature(GFXFeature.TEXTURE_FLOAT)) {
|
||||
return JointStorageKind.textureRGBA32F;
|
||||
return JointStorageKind.textureRGBA8;
|
||||
} else {
|
||||
return JointStorageKind.textureRGBA8;
|
||||
}
|
||||
@@ -265,7 +266,7 @@ class MatrixTexture {
|
||||
* @param kind The texture format.
|
||||
* @throws Throws error if matrix count is too large.
|
||||
*/
|
||||
constructor (capacity: number, kind: Mat4TextureKind) {
|
||||
constructor (capacity: number, kind: Mat4TextureKind, device: GFXDevice) {
|
||||
this._kind = kind;
|
||||
const attribute = MatrixTexture.Mat4TextureAttributes[kind];
|
||||
let textureExtent = -1;
|
||||
@@ -295,22 +296,22 @@ class MatrixTexture {
|
||||
} else {
|
||||
const base = iMatrix * 16 * 4;
|
||||
const o = this._nativeData as Uint8Array;
|
||||
encode32(matrix.m00, o, base + 0 * 4);
|
||||
encode32(matrix.m01, o, base + 1 * 4);
|
||||
encode32(matrix.m02, o, base + 2 * 4);
|
||||
encode32(matrix.m03, o, base + 3 * 4);
|
||||
encode32(matrix.m04, o, base + 4 * 4);
|
||||
encode32(matrix.m05, o, base + 5 * 4);
|
||||
encode32(matrix.m06, o, base + 6 * 4);
|
||||
encode32(matrix.m07, o, base + 7 * 4);
|
||||
encode32(matrix.m08, o, base + 8 * 4);
|
||||
encode32(matrix.m09, o, base + 9 * 4);
|
||||
encode32(matrix.m10, o, base + 10 * 4);
|
||||
encode32(matrix.m11, o, base + 11 * 4);
|
||||
encode32(matrix.m12, o, base + 12 * 4);
|
||||
encode32(matrix.m13, o, base + 13 * 4);
|
||||
encode32(matrix.m14, o, base + 14 * 4);
|
||||
encode32(matrix.m15, o, base + 15 * 4);
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -319,33 +320,68 @@ class MatrixTexture {
|
||||
}
|
||||
}
|
||||
|
||||
const encode32 = (() => {
|
||||
const EXP2_XX_1 = exp2(-1.0);
|
||||
const EXP2_XX_2 = exp2(23.0 - 8.0);
|
||||
const EXP2_XX_3 = exp2(8.0);
|
||||
const EXP2_XX_4 = exp2(23.0);
|
||||
const EXP2_XX_5 = exp2(-15.0);
|
||||
const encodeIEEE754Single = (() => {
|
||||
const SIGNIFICAND_BITS_COUNT_WITHOUT_SIGN_BIT = 23;
|
||||
const EXPONENT_BITS_COUNT = 8;
|
||||
const BIAS = 127;
|
||||
|
||||
function decode32 (r: number, g: number, b: number, a: number) {
|
||||
const Sign = 1.0 - step(128.0, r) * 2.0;
|
||||
const Exponent = 2.0 * mod(r, 128.0) + step(128.0, g) - 127.0;
|
||||
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);
|
||||
}
|
||||
|
||||
const numberToFloat32 = (() => {
|
||||
const f32storage = new Float32Array(1);
|
||||
return (x: number) => {
|
||||
f32storage[0] = x;
|
||||
return f32storage[0];
|
||||
};
|
||||
})();
|
||||
|
||||
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.
|
||||
if (Exponent === -127) {
|
||||
const Exponent = ExponentAfterBIAS - BIAS; // Undo bias
|
||||
if (Exponent === -BIAS) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
const Mantissa = mod(g, 128.0) * 65536.0 + b * 256.0 + a + (0x800000);
|
||||
|
||||
// Original
|
||||
// const Result = Sign * exp2(Exponent) * (Mantissa * exp2(-23.0 ));
|
||||
// return Result;
|
||||
const Mantissa = LOWER_7BITS_G * SHIFT_LEFT_16 + b * SHIFT_LEFT_8 + a + ONE_SHIFT_LEFT_27;
|
||||
|
||||
// From Arjan's answer.
|
||||
return Sign * exp2(Exponent - 23.0) * Mantissa;
|
||||
// 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;
|
||||
}
|
||||
|
||||
return (f: number, output: Uint8Array, offset: number) => {
|
||||
interface IColor { r: number; g: number; b: number; a: number; }
|
||||
|
||||
function encode1 (f: number, out: IColor) {
|
||||
f = numberToFloat32(f);
|
||||
|
||||
// https://stackoverflow.com/questions/7059962/how-do-i-convert-a-vec4-rgba-value-to-a-float
|
||||
@@ -355,10 +391,10 @@ const encode32 = (() => {
|
||||
|
||||
// From Arjan's answer.
|
||||
if (F === 0) {
|
||||
output[offset + 0] = 0;
|
||||
output[offset + 1] = 0;
|
||||
output[offset + 2] = 0;
|
||||
output[offset + 3] = 0;
|
||||
out.r = 0;
|
||||
out.g = 0;
|
||||
out.b = 0;
|
||||
out.a = 0;
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -367,57 +403,68 @@ const encode32 = (() => {
|
||||
|
||||
// Original.
|
||||
// const Mantissa = (exp2(- Exponent) * F);
|
||||
// Exponent = Math.floor(Math.log2(F) + 127.0) + Math.floor(Math.log2(Mantissa));
|
||||
// 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 += 127;
|
||||
Exponent += BIAS;
|
||||
|
||||
const r = 128.0 * Sign + Math.floor(Exponent * EXP2_XX_1);
|
||||
const g = 128.0 * mod(Exponent, 2.0) + mod(Math.floor(Mantissa * 128.0), 128.0);
|
||||
const b = Math.floor(mod(Math.floor(Mantissa * EXP2_XX_2), EXP2_XX_3));
|
||||
const a = Math.floor(EXP2_XX_4 * mod(Mantissa, EXP2_XX_5));
|
||||
// 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);
|
||||
|
||||
// Verification.
|
||||
// const x = decode32(r, g, b, a);
|
||||
// if (Math.abs(x - f) > 0.00001) {
|
||||
// f = x;
|
||||
// }
|
||||
// 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);
|
||||
|
||||
output[offset + 0] = r;
|
||||
output[offset + 1] = g;
|
||||
output[offset + 2] = b;
|
||||
output[offset + 3] = a;
|
||||
};
|
||||
// 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);
|
||||
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];
|
||||
};
|
||||
} 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];
|
||||
};
|
||||
}
|
||||
})();
|
||||
|
||||
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;
|
||||
};
|
||||
})();
|
||||
})();
|
||||
|
||||
const numberToFloat32 = (() => {
|
||||
const f32storage = new Float32Array(1);
|
||||
return (x: number) => {
|
||||
f32storage[0] = x;
|
||||
return f32storage[0];
|
||||
};
|
||||
})();
|
||||
|
||||
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 int (x: number) {
|
||||
return Math.trunc(x);
|
||||
}
|
||||
|
||||
function fract (x: number) {
|
||||
return x - Math.floor(x);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user