Skip to content

Commit

Permalink
kram - switch to adjoint for normal
Browse files Browse the repository at this point in the history
This simplifies having to deal with non-uniform scale, etc.  And it's correct over invT.  Can remove invScale2, and only pass 1 tfm.
Small change to xcconfig to get it to work.
  • Loading branch information
alecazam committed Dec 13, 2024
1 parent 56153e4 commit 1ac83d8
Show file tree
Hide file tree
Showing 8 changed files with 26 additions and 21 deletions.
4 changes: 2 additions & 2 deletions build2/kram.xcconfig
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@
// Also turn on -ftime-trace to review build times in kram-profile.

KRAM_FLAGS_X64 =
KRAM_FLAGS_X64[arch=x86_64] = -mf16c -mfma
KRAM_FLAGS_X64[sdk=*][arch=x86_64] = -mf16c -mfma

KRAM_FLAGS_RELEASE =
KRAM_FLAGS_RELEASE[config=Release] = -DNDEBUG=1
KRAM_FLAGS_RELEASE[sdk=*][config=Release] = -DNDEBUG=1

KRAM_FLAGS = -ftime-trace
KRAM_FLAGS = $(KRAM_FLAGS) -DUSE_SIMDLIB=1 -DUSE_SIMDLIBMODULE=1
Expand Down
2 changes: 1 addition & 1 deletion kramv/KramRenderer.mm
Original file line number Diff line number Diff line change
Expand Up @@ -1492,7 +1492,7 @@ - (void)_updateGameState

// This is per object
uniforms.modelMatrix = _data->_modelMatrix;
uniforms.modelMatrixInvScale2 = _data->_modelMatrixInvScale2;
// uniforms.modelMatrixInvScale2 = _data->_modelMatrixInvScale2;

//_rotation += .01;
}
Expand Down
4 changes: 2 additions & 2 deletions kramv/KramViewerBase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3052,8 +3052,8 @@ void Data::updateTransforms()
inverse(_viewMatrix).columns[3].xyz; // this is all ortho

// obj specific
_modelMatrixInvScale2 = inverseScaleSquared(_modelMatrix);
_showSettings->isInverted = _modelMatrixInvScale2.w < 0.0f;
float4 modelMatrixInvScale2 = inverseScaleSquared(_modelMatrix);
_showSettings->isInverted = modelMatrixInvScale2.w < 0.0f;
}

float4x4 Data::computeImageTransform(float panX, float panY, float zoom)
Expand Down
2 changes: 1 addition & 1 deletion kramv/KramViewerBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -521,7 +521,7 @@ struct Data {

// object specific
float4x4 _modelMatrix;
float4 _modelMatrixInvScale2;
//float4 _modelMatrixInvScale2;
float4x4 _modelMatrix2D;
float4x4 _modelMatrix3D;

Expand Down
2 changes: 1 addition & 1 deletion kramv/Shaders/KramShaders.h
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ typedef NS_ENUM(int32_t, ShaderLightingMode) {
struct Uniforms {
SIMD_NAMESPACE::float4x4 projectionViewMatrix;
SIMD_NAMESPACE::float4x4 modelMatrix;
SIMD_NAMESPACE::float4 modelMatrixInvScale2; // to supply inverse, w is determinant
//SIMD_NAMESPACE::float4 modelMatrixInvScale2; // to supply inverse, w is determinant
SIMD_NAMESPACE::float3 cameraPosition; // world-space
float uvPreview;
float uvToShapeRatio;
Expand Down
22 changes: 13 additions & 9 deletions kramv/Shaders/KramShaders.metal
Original file line number Diff line number Diff line change
Expand Up @@ -519,24 +519,28 @@ inline float3x3 toFloat3x3(float4x4 m)
return float3x3(m[0].xyz, m[1].xyz, m[2].xyz);
}

// This works even with scale of 0 and is correct over using invT.
// The normal will be normalize anyways. Also saves sending down another tfm.
inline float3x3 adjoint(float3x3 m)
{
return float3x3(cross(m[1], m[2]),
cross(m[2], m[0]),
cross(m[0], m[1]));
}


// this is for vertex shader if tangent supplied
void transformBasis(thread float3& normal, thread float3& tangent,
float4x4 modelToWorldTfm, float3 invScale2, bool useTangent)
float4x4 modelToWorldTfm, bool useTangent)
{

float3x3 m = toFloat3x3(modelToWorldTfm);

// note this is RinvT * n = (Rt)t = R, this is for simple inverse, inv scale handled below
// but uniform scale already handled by normalize
normal = m * normal;
normal *= invScale2;
normal = adjoint(m) * normal;
normal = normalize(normal);

// question here of whether tangent is transformed by m or mInvT
// most apps assume m, but after averaging it can be just as off the surface as the normal
if (useTangent) {
tangent = m * tangent;
tangent *= invScale2;
tangent = normalize(tangent);
}

Expand Down Expand Up @@ -622,7 +626,7 @@ ColorInOut DrawImageFunc(

if (needsWorldBasis) {
float3 t = tangent.xyz;
transformBasis(normal, t, uniforms.modelMatrix, uniforms.modelMatrixInvScale2.xyz, uniforms.useTangent);
transformBasis(normal, t, uniforms.modelMatrix, uniforms.useTangent);
tangent.xyz = t;

out.normal = toHalf(normal);
Expand Down
3 changes: 0 additions & 3 deletions libkram/vectormath/double234.h
Original file line number Diff line number Diff line change
Expand Up @@ -106,9 +106,6 @@ SIMD_CALL double4 zeroext(double3 x)

#if SIMD_NEON

// TODO: expose double2 ops on Neon.
// think I have to, so that 4 can call 2x2 with hi/lo

SIMD_CALL double reduce_min(double2 x)
{
return vminvq_f64(x);
Expand Down
8 changes: 6 additions & 2 deletions libkram/vectormath/float234.h
Original file line number Diff line number Diff line change
Expand Up @@ -929,9 +929,13 @@ SIMD_CALL quatf operator-(quatf q)
SIMD_CALL float3 operator*(quatf q, float3 v)
{
// see https://fgiesen.wordpress.com/2019/02/09/rotating-a-single-vector-using-a-quaternion/
//float4 qv = q.v;
//float3 t = 2.0f * cross(qv.xyz, v);
//return v + qv.w * t + cross(qv.xyz, t);

// simplified form of above
float4 qv = q.v;
float3 t = 2.0f * cross(qv.xyz, v);
return v + qv.w * t + cross(qv.xyz, t);
return v + 2.0 * cross(qv.xyz, cross(qv.xyz, v) + qv.w * v);
}

SIMD_CALL bool equal(quatf x, quatf y)
Expand Down

0 comments on commit 1ac83d8

Please sign in to comment.