Skip to content

Commit 3846017

Browse files
Settle on the encoding of a keyframe
Also a TODO
1 parent 7e5da61 commit 3846017

File tree

6 files changed

+108
-23
lines changed

6 files changed

+108
-23
lines changed

include/nbl/builtin/glsl/format/constants.glsl

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,18 @@
1616
#define nbl_glsl_RGB19E7_COMPONENT_BITOFFSETS ivec4(0,nbl_glsl_RGB19E7_MANTISSA_BITS,(2*nbl_glsl_RGB19E7_MANTISSA_BITS)&31,(3*nbl_glsl_RGB19E7_MANTISSA_BITS)&31)
1717
#define nbl_glsl_RGB19E7_G_COMPONENT_SPLIT (32-nbl_glsl_RGB19E7_MANTISSA_BITS)
1818

19+
//rgb18e7s3, out custom 3 channel, shared exponent signed floating point format
20+
#define nbl_glsl_RGB18E7S3_MANTISSA_BITS 18
21+
#define nbl_glsl_RGB18E7S3_EXPONENT_BITS nbl_glsl_RGB19E7_EXPONENT_BITS
22+
#define nbl_glsl_RGB18E7S3_EXP_BIAS nbl_glsl_RGB19E7_EXP_BIAS
23+
#define nbl_glsl_MAX_RGB18E7S3_EXP (nbl_glsl_RGB18E7S3_EXP_BIAS+1)
24+
25+
#define nbl_glsl_MAX_RGB18E7S3_MANTISSA_VALUES (0x1<<nbl_glsl_RGB18E7S3_MANTISSA_BITS)
26+
#define nbl_glsl_MAX_RGB18E7S3_MANTISSA (nbl_glsl_MAX_RGB18E7S3_MANTISSA_VALUES-1)
27+
#define nbl_glsl_MAX_RGB18E7S3 float(nbl_glsl_MAX_RGB18E7S3_MANTISSA)/float(nbl_glsl_MAX_RGB18E7S3_MANTISSA_VALUES)*exp2(float(nbl_glsl_MAX_RGB18E7S3_EXP))
28+
29+
#define nbl_glsl_RGB18E7S3_COMPONENT_INDICES nbl_glsl_RGB19E7_COMPONENT_INDICES
30+
#define nbl_glsl_RGB18E7S3_COMPONENT_BITOFFSETS ivec4(0,nbl_glsl_RGB18E7S3_MANTISSA_BITS,(2*nbl_glsl_RGB18E7S3_MANTISSA_BITS)&31,(3*nbl_glsl_RGB18E7S3_MANTISSA_BITS)&31)
31+
#define nbl_glsl_RGB18E7S3_G_COMPONENT_SPLIT (32-nbl_glsl_RGB18E7S3_MANTISSA_BITS)
32+
1933
#endif

include/nbl/builtin/glsl/format/decode.glsl

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,28 @@ vec3 nbl_glsl_decodeRGB19E7(in uvec2 x)
2222
return v*scale;
2323
}
2424

25+
vec3 nbl_glsl_decodeRGB18E7S3(in uvec2 x)
26+
{
27+
int exp = int(bitfieldExtract(x[nbl_glsl_RGB18E7S3_COMPONENT_INDICES[3]], nbl_glsl_RGB18E7S3_COMPONENT_BITOFFSETS[3], nbl_glsl_RGB18E7S3_EXPONENT_BITS) - nbl_glsl_RGB18E7S3_EXP_BIAS - nbl_glsl_RGB18E7S3_MANTISSA_BITS);
28+
float scale = exp2(float(exp));
29+
30+
vec3 v;
31+
v.x = float(bitfieldExtract(x[nbl_glsl_RGB18E7S3_COMPONENT_INDICES[0]], nbl_glsl_RGB18E7S3_COMPONENT_BITOFFSETS[0], nbl_glsl_RGB18E7S3_MANTISSA_BITS));
32+
v.y = float(bitfieldInsert(
33+
bitfieldExtract(x[nbl_glsl_RGB18E7S3_COMPONENT_INDICES[1]], nbl_glsl_RGB18E7S3_COMPONENT_BITOFFSETS[1], nbl_glsl_RGB18E7S3_G_COMPONENT_SPLIT),
34+
bitfieldExtract(x[nbl_glsl_RGB18E7S3_COMPONENT_INDICES[2]], 0, nbl_glsl_RGB18E7S3_COMPONENT_BITOFFSETS[2]),
35+
nbl_glsl_RGB18E7S3_G_COMPONENT_SPLIT,
36+
nbl_glsl_RGB18E7S3_COMPONENT_BITOFFSETS[2]
37+
));
38+
v.z = float(bitfieldExtract(x[nbl_glsl_RGB18E7S3_COMPONENT_INDICES[2]], nbl_glsl_RGB18E7S3_COMPONENT_BITOFFSETS[2], nbl_glsl_RGB18E7S3_MANTISSA_BITS));
39+
40+
uvec3 signs = x.yyy<<uvec3(2u,1u,0u);
41+
signs &= 0x80000000u;
42+
v = uintBitsToFloat(floatBitsToUint(v)^signs);
43+
44+
return v*scale;
45+
}
46+
2547
vec4 nbl_glsl_decodeRGB10A2(in uint x)
2648
{
2749
const uvec3 rgbMask = uvec3(0x3ffu);

include/nbl/builtin/glsl/format/encode.glsl

Lines changed: 42 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,28 @@
33

44
#include <nbl/builtin/glsl/format/constants.glsl>
55

6+
void nbl_glsl_impl_sharedExponentEncodeCommon(in vec3 clamped, in int newExpBias, in int newMaxExp, in int mantissaBits, out int shared_exp)
7+
{
8+
maxrgb = max(max(clamped.r,clamped.g),clamped.b);
9+
// TODO: optimize this
10+
const int f32_exp = ((floatBitsToInt(maxrgb)>>23) & 0xff) - 126;
11+
12+
shared_exp = clamp(f32_exp,-newExpBias,newMaxExp+1);
13+
14+
float scale = exp2(mantissaBits-shared_exp);
15+
const uint maxm = uint(maxrgb*scale + 0.5);
16+
const bool need = maxm==(0x1u<<mantissaBits);
17+
scale = need ? 0.5*scale:scale;
18+
shared_exp = need ? (shared_exp+1):shared_exp;
19+
const uvec3 mantissas = uvec3(clamped*scale + vec3(0.5));
20+
}
21+
622
uvec2 nbl_glsl_encodeRGB19E7(in vec3 col)
723
{
824
const vec3 clamped = clamp(col,vec3(0.0),vec3(nbl_glsl_MAX_RGB19E7));
9-
const float maxrgb = max(max(clamped.r,clamped.g),clamped.b);
1025

11-
const int f32_exp = ((floatBitsToInt(maxrgb)>>23) & 0xff) - 127;
12-
int shared_exp = clamp(f32_exp,-nbl_glsl_RGB19E7_EXP_BIAS-1,nbl_glsl_MAX_RGB19E7_EXP) + 1;
13-
14-
float scale = exp2(nbl_glsl_RGB19E7_MANTISSA_BITS - shared_exp);
15-
const uint maxm = uint(maxrgb*scale + 0.5);
16-
const bool need = (maxm == nbl_glsl_MAX_RGB19E7_MANTISSA_VALUES);
17-
scale = need ? 0.5*scale : scale;
18-
shared_exp = need ? (shared_exp+1) : shared_exp;
19-
const uvec3 mantissas = uvec3(clamped*scale + 0.5);
26+
int shared_exp;
27+
const uvec3 mantissas = nbl_glsl_impl_sharedExponentEncodeCommon(clamped,nbl_glsl_RGB19E7_EXP_BIAS,nbl_glsl_MAX_RGB19E7_EXP,nbl_glsl_RGB19E7_MANTISSA_BITS,shared_exp);
2028

2129
uvec2 encoded;
2230
encoded.x = bitfieldInsert(mantissas.x,mantissas.y,nbl_glsl_RGB19E7_COMPONENT_BITOFFSETS[1],nbl_glsl_RGB19E7_G_COMPONENT_SPLIT);
@@ -30,6 +38,30 @@ uvec2 nbl_glsl_encodeRGB19E7(in vec3 col)
3038
return encoded;
3139
}
3240

41+
uvec2 nbl_glsl_encodeRGB18E7S3(in vec3 col)
42+
{
43+
const vec3 absCol = abs(col);
44+
const vec3 clamped = min(absCol,vec3(nbl_glsl_MAX_RGB18E7S3));
45+
46+
int shared_exp;
47+
const uvec3 mantissas = nbl_glsl_impl_sharedExponentEncodeCommon(clamped,nbl_glsl_RGB18E7S3_EXP_BIAS,nbl_glsl_MAX_RGB18E7S3_EXP,nbl_glsl_RGB18E7S3_MANTISSA_BITS,shared_exp);
48+
49+
uvec3 signs = floatBitsToUint(col)&0x80000000u;
50+
signs.xy >>= uvec2(2u,1u);
51+
52+
uvec2 encoded;
53+
encoded.x = bitfieldInsert(mantissas.x,mantissas.y,nbl_glsl_RGB18E7S3_COMPONENT_BITOFFSETS[1],nbl_glsl_RGB18E7S3_G_COMPONENT_SPLIT);
54+
encoded.y = bitfieldInsert(
55+
mantissas.y>>nbl_glsl_RGB18E7S3_G_COMPONENT_SPLIT,
56+
mantissas.z,
57+
nbl_glsl_RGB18E7S3_COMPONENT_BITOFFSETS[2],
58+
nbl_glsl_RGB18E7S3_MANTISSA_BITS)
59+
| uint((shared_exp+nbl_glsl_RGB18E7S3_EXP_BIAS)<<nbl_glsl_RGB18E7S3_COMPONENT_BITOFFSETS[3])
60+
| signs.x | signs.y | signs.z;
61+
62+
return encoded;
63+
}
64+
3365
uint nbl_glsl_encodeRGB10A2(in vec4 col)
3466
{
3567
const uvec3 rgbMask = uvec3(0x3ffu);

include/nbl/builtin/glsl/scene/animations.glsl

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,38 @@
22
#define _NBL_GLSL_SCENE_ANIMATIONS_INCLUDED_
33

44

5-
65
#include <nbl/builtin/glsl/math/keyframe.glsl>
76

87

8+
struct nbl_glsl_scene_Animation_t
9+
{
10+
uint keyframeOffset; // same offset for timestamps and keyframes
11+
uint keyframesCount_interpolationMode; // 2 bits for interpolation mode
12+
};
913

10-
struct nbl_glsl_scene_Animations_t
14+
struct nbl_glsl_scene_AnimationBlend_t
1115
{
12-
mat4x3 poseBind;
13-
uint keyframesCount;
14-
uint timestampsOffset;
15-
// The difference between the two is because some animation formats allow for staggered keyframes
16-
// i.e. not every keyframe is a tuple of (scale,rotation,translation)
17-
uint interpolatedKeyframesOffset;
18-
uint nonInterpolatedKeyframesOffset;
16+
uint nodeTargetID;
17+
uint desiredTimestamp;
18+
float weight;
19+
uint animationID; // or do we stick whole animation inside?
1920
};
2021

22+
/*
23+
24+
Design Idea
25+
26+
IAnimationLibrary
27+
{
28+
29+
Packs several ranges of nbl_glsl_scene_Keyframe_t and uint-ms-timestamps into a single buffer (timestamps and keyframes go into separate buffers)
30+
31+
The packed ranges are named and stored as `map<const char*,nbl_glsl_scene_Animation_t*>`
32+
33+
You can retrieve animations by name or ID
34+
35+
}
2136
37+
*/
2238

2339
#endif

include/nbl/builtin/glsl/scene/keyframe.glsl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,23 +9,23 @@
99

1010
struct nbl_glsl_scene_Keyframe_t
1111
{
12-
uint data[9];
12+
uvec2 data[3];
1313
};
1414

1515

1616
vec3 nbl_glsl_scene_Keyframe_t_getScale(in nbl_glsl_scene_Keyframe_t keyframe)
1717
{
18-
return uintBitsToFloat(uvec3(keyframe.data[0],keyframe.data[1],keyframe.data[2]));
18+
return nbl_glsl_decodeRGB18E7S3(keyframe.data[3]);
1919
}
2020

2121
nbl_glsl_quaternion_t nbl_glsl_scene_Keyframe_t_getRotation(in nbl_glsl_scene_Keyframe_t keyframe)
2222
{
23-
return nbl_glsl_quaternion_t_constructFromTruncated(uintBitsToFloat(uvec3(keyframe.data[3],keyframe.data[4],keyframe.data[5])));
23+
return {nbl_glsl_decode8888Quaternion(keyframe.data[1][1])};
2424
}
2525

2626
vec3 nbl_glsl_scene_Keyframe_t_getTranslation(in nbl_glsl_scene_Keyframe_t keyframe)
2727
{
28-
return uintBitsToFloat(uvec3(keyframe.data[6],keyframe.data[7],keyframe.data[8]));
28+
return uintBitsToFloat(uvec3(keyframe.data[0].xy, keyframe.data[1][0]));
2929
}
3030

3131

include/nbl/core/math/floatutil.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -427,6 +427,7 @@ class Float16Compressor
427427
}
428428
};
429429

430+
// TODO: @Crisspl lets make the `rgb18e7s3` format and refactor some of this shared exponent magic
430431
inline uint64_t rgb32f_to_rgb19e7(const float _rgb[3])
431432
{
432433
union rgb19e7 {

0 commit comments

Comments
 (0)