Skip to content

Commit

Permalink
The use of Quternions/Transforms, rebased
Browse files Browse the repository at this point in the history
  • Loading branch information
lhog committed Feb 9, 2025
1 parent 1d05355 commit 8371edb
Show file tree
Hide file tree
Showing 36 changed files with 903 additions and 603 deletions.
44 changes: 43 additions & 1 deletion cont/base/springcontent/shaders/GLSL/ModelVertProgGL4.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,17 @@ layout(std140, binding = 1) uniform UniformParamsBuffer {
vec4 teamColor[255]; //all team colors
};

/*
struct Transform {
vec4 quat;
vec4 trSc;
};
layout(std140, binding = 0) readonly buffer TransformBuffer {
Transform transforms[];
};
*/

layout(std140, binding = 0) readonly buffer MatrixBuffer {
mat4 mat[];
};
Expand Down Expand Up @@ -132,6 +143,37 @@ uint GetUnpackedValue(uint packedValue, uint byteNum) {
return (packedValue >> (8u * byteNum)) & 0xFFu;
}

vec4 MultiplyQuat(vec4 a, vec4 b)
{
return vec4(a.w * b.w - dot(a.w, b.w), a.w * b.xyz + b.w * a.xyz + cross(a.xyz, b.xyz));
}

vec3 RotateByQuaternion(vec4 q, vec3 v) {
return 2.0f * dot(q.xyz, v) * q.xyz + (q.w * q.w - dot(q.xyz, q.xyz)) * v + 2.0 * q.w * cross(q.xyz, v);
}

vec4 RotateByQuaternion(vec4 q, vec4 v) {
return vec4(RotateByQuaternion(q, v.xyz), v.w);
}

vec3 ApplyTransform(Transform tra, vec3 v) {
return RotateByQuaternion(tra.quat, v * tra.trSc.w) + tra.trSc.xyz;
}

vec4 ApplyTransform(Transform tra, vec4 v) {
return vec4(ApplyTransform(tra, v.xyz), v.w);
}

Transform ApplyTransform(Transform parentTra, Transform childTra) {
return Transform(
MultiplyQuat(parentTra.quat, childTra.quat),
vec4(
parentTra.trSc.xyz + RotateByQuaternion(parentTra.quat, parentTra.trSc.w * childTra.trSc.xyz),
parentTra.trSc.w * childTra.trSc.w
)
);
}

void GetModelSpaceVertex(out vec4 msPosition, out vec3 msNormal)
{
bool staticModel = (matrixMode > 0);
Expand Down Expand Up @@ -237,4 +279,4 @@ void main(void)
TransformPlayerCam(worldPos);
break;
};
}
}
19 changes: 13 additions & 6 deletions rts/Game/Game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -739,8 +739,11 @@ void CGame::PreLoadRendering()
geometricObjects = new CGeometricObjects();

// load components that need to exist before PostLoadSimulation
matrixUploader.Init();
modelsUniformsUploader.Init();
modelUniformsStorage.Init();
//transformsMemStorage.Init(); // Add?

transformsUploader.Init();
modelUniformsUploader.Init();
worldDrawer.InitPre();
}

Expand Down Expand Up @@ -1000,8 +1003,9 @@ void CGame::KillRendering()
icon::iconHandler.Kill();
spring::SafeDelete(geometricObjects);
worldDrawer.Kill();
matrixUploader.Kill();
modelsUniformsUploader.Kill();

transformsUploader.Kill();
modelUniformsUploader.Kill();
}

void CGame::KillInterface()
Expand Down Expand Up @@ -1044,6 +1048,9 @@ void CGame::KillSimulation()
unitHandler.Kill();
projectileHandler.Kill();

modelUniformsStorage.Kill();
//transformsMemStorage.Kill(); //Add?

LOG("[Game::%s][3]", __func__);
IPathManager::FreeInstance(pathManager);
IMapDamage::FreeMapDamage(mapDamage);
Expand Down Expand Up @@ -1473,8 +1480,8 @@ bool CGame::UpdateUnsynced(const spring_time currentTime)
shadowHandler.Update();
{
worldDrawer.Update(newSimFrame);
matrixUploader.Update();
modelsUniformsUploader.Update();
transformsUploader.Update();
modelUniformsUploader.Update();
}

mouse->UpdateCursorCameraDir(); // make sure mouse->dir is in sync with camera
Expand Down
2 changes: 1 addition & 1 deletion rts/Lua/LuaOpenGL.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1424,7 +1424,7 @@ static void GLObjectPieceMultMatrix(lua_State* L, const CSolidObject* obj)
if (lmp == nullptr)
return;

glMultMatrixf(lmp->GetModelSpaceMatrix());
glMultMatrixf(lmp->GetModelSpaceTransform().ToMatrix());
}

static bool GLObjectDrawWithLuaMat(lua_State* L, CSolidObject* obj, LuaObjType objType)
Expand Down
2 changes: 1 addition & 1 deletion rts/Lua/LuaShaders.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1012,7 +1012,7 @@ namespace {
if (o == nullptr)
luaL_error(L, "gl.%s() Invalid %s id (%d)", func, &spring::TypeToCStr<T>()[1], id);

ModelUniformData& uni = modelsUniformsStorage.GetObjUniformsArray(o);
ModelUniformData& uni = modelUniformsStorage.GetObjUniformsArray(o);

std::array<float, ModelUniformData::MAX_MODEL_UD_UNIFORMS> floatArray = {0};
int size = LuaUtils::ParseFloatArray(L, 2, floatArray.data(), ModelUniformData::MAX_MODEL_UD_UNIFORMS);
Expand Down
2 changes: 1 addition & 1 deletion rts/Lua/LuaSyncedRead.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8421,7 +8421,7 @@ static int GetSolidObjectPieceMatrix(lua_State* L, const CSolidObject* o)
if (lmp == nullptr)
return 0;

const CMatrix44f& mat = lmp->GetModelSpaceMatrix();
const CMatrix44f& mat = lmp->GetModelSpaceTransform().ToMatrix();

for (float mi: mat.m) {
lua_pushnumber(L, mi);
Expand Down
18 changes: 9 additions & 9 deletions rts/Lua/LuaVBOImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1023,10 +1023,10 @@ SInstanceData LuaVBOImpl::InstanceDataFromGetData(int id, int attrID, uint8_t de
uint32_t teamID = defTeamID;

const TObj* obj = LuaUtils::SolIdToObject<TObj>(id, __func__);
const uint32_t matOffset = static_cast<uint32_t>(matrixUploader.GetElemOffset(obj));
const uint32_t uniIndex = static_cast<uint32_t>(modelsUniformsStorage.GetObjOffset(obj)); //doesn't need to exist for defs and model. Don't check for validity
const uint32_t traOffset = static_cast<uint32_t>(transformsUploader.GetElemOffset(obj));
const uint32_t uniIndex = static_cast<uint32_t>(modelUniformsStorage.GetObjOffset(obj)); //doesn't need to exist for defs and model. Don't check for validity

if (matOffset == ~0u) {
if (traOffset == ~0u) {
LuaUtils::SolLuaError("[LuaVBOImpl::%s] Invalid data supplied. See infolog for details", __func__);
}

Expand All @@ -1040,14 +1040,14 @@ SInstanceData LuaVBOImpl::InstanceDataFromGetData(int id, int attrID, uint8_t de
size_t bposeIndex = 0;
if constexpr (std::is_same<TObj, S3DModel>::value) {
numPieces = static_cast<uint16_t>(obj->numPieces);
bposeIndex = matrixUploader.GetElemOffset(obj);
bposeIndex = transformsUploader.GetElemOffset(obj);
}
else {
numPieces = static_cast<uint16_t>(obj->model->numPieces);
bposeIndex = matrixUploader.GetElemOffset(obj->model);
bposeIndex = transformsUploader.GetElemOffset(obj->model);
}

return SInstanceData(matOffset, teamID, drawFlags, numPieces, uniIndex, bposeIndex);
return SInstanceData(traOffset, teamID, drawFlags, numPieces, uniIndex, bposeIndex);
}

template<typename TObj>
Expand Down Expand Up @@ -1215,7 +1215,7 @@ size_t LuaVBOImpl::ModelsVBO()
* Data Layout:
* ```
* SInstanceData:
* , matOffset{ matOffset_ } // updated during the following draw frames
* , traOffset{ matOffset_ } // updated during the following draw frames
* , uniOffset{ uniOffset_ } // updated during the following draw frames
* , info{ teamIndex, drawFlags, 0, 0 } // not updated during the following draw frames
* , aux1 { 0u }
Expand Down Expand Up @@ -1254,7 +1254,7 @@ size_t LuaVBOImpl::InstanceDataFromUnitDefIDs(const sol::stack_table& ids, int a
* Data Layout
* ```
* SInstanceData:
* , matOffset{ matOffset_ } // updated during the following draw frames
* , traOffset{ matOffset_ } // updated during the following draw frames
* , uniOffset{ uniOffset_ } // updated during the following draw frames
* , info{ teamIndex, drawFlags, 0, 0 } // not updated during the following draw frames
* , aux1 { 0u }
Expand Down Expand Up @@ -1294,7 +1294,7 @@ size_t LuaVBOImpl::InstanceDataFromFeatureDefIDs(const sol::stack_table& ids, in
*
* ```
* SInstanceData:
* , matOffset{ matOffset_ } // updated during the following draw frames
* , traOffset{ matOffset_ } // updated during the following draw frames
* , uniOffset{ uniOffset_ } // updated during the following draw frames
* , info{ teamIndex, drawFlags, 0, 0 } // not updated during the following draw frames
* , aux1 { 0u }
Expand Down
4 changes: 2 additions & 2 deletions rts/Rendering/Common/ModelDrawer.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

namespace GL { struct GeometryBuffer; }
template<typename T> class ScopedModelDrawerImpl;
class ScopedMatricesMemAlloc;
class ScopedTransformMemAlloc;


static constexpr const char* ModelDrawerNames[ModelDrawerTypes::MODEL_DRAWER_CNT] = {
Expand Down Expand Up @@ -110,7 +110,7 @@ class CModelDrawerBase : public CModelDrawerConcept {
static bool CanDrawDeferred() { return modelDrawerState->CanDrawDeferred(); }
static bool SetTeamColor(int team, const float alpha = 1.0f) { return modelDrawerState->SetTeamColor(team, alpha); }
static void SetNanoColor(const float4& color) { modelDrawerState->SetNanoColor(color); }
static const ScopedMatricesMemAlloc& GetMatricesMemAlloc(const ObjType* o) { return const_cast<const TDrawerData*>(modelDrawerData)->GetObjectMatricesMemAlloc(o); }
static const ScopedTransformMemAlloc& GetTransformMemAlloc(const ObjType* o) { return const_cast<const TDrawerData*>(modelDrawerData)->GetObjectTransformMemAlloc(o); }
public:
virtual void Update() const = 0;
// Draw*
Expand Down
41 changes: 21 additions & 20 deletions rts/Rendering/Common/ModelDrawerData.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,18 +69,18 @@ class CModelDrawerDataBase : public CModelDrawerDataConcept

void ClearPreviousDrawFlags() { for (auto object : unsortedObjects) object->previousDrawFlag = 0; }

const ScopedMatricesMemAlloc& GetObjectMatricesMemAlloc(const T* o) const {
const auto it = matricesMemAllocs.find(const_cast<T*>(o));
return (it != matricesMemAllocs.end()) ? it->second : ScopedMatricesMemAlloc::Dummy();
const auto& GetObjectTransformMemAlloc(const T* o) const {
const auto it = scTransMemAllocMap.find(const_cast<T*>(o));
return (it != scTransMemAllocMap.end()) ? it->second : ScopedTransformMemAlloc::Dummy();
}
ScopedMatricesMemAlloc& GetObjectMatricesMemAlloc(const T* o) { return matricesMemAllocs[const_cast<T*>(o)]; }
auto& GetObjectTransformMemAlloc(const T* o) { return scTransMemAllocMap[const_cast<T*>(o)]; }
private:
static constexpr int MMA_SIZE0 = 2 << 16;
protected:
std::array<ModelRenderContainer<T>, MODELTYPE_CNT> modelRenderers;

std::vector<T*> unsortedObjects;
std::unordered_map<T*, ScopedMatricesMemAlloc> matricesMemAllocs;
std::unordered_map<T*, ScopedTransformMemAlloc> scTransMemAllocMap;

bool& mtModelDrawer;
};
Expand All @@ -100,15 +100,15 @@ inline CModelDrawerDataBase<T>::CModelDrawerDataBase(const std::string& ecName,
: CModelDrawerDataConcept(ecName, ecOrder)
, mtModelDrawer(mtModelDrawer_)
{
matricesMemAllocs.reserve(MMA_SIZE0);
scTransMemAllocMap.reserve(MMA_SIZE0);
for (auto& mr : modelRenderers) { mr.Clear(); }
}

template<typename T>
inline CModelDrawerDataBase<T>::~CModelDrawerDataBase()
{
unsortedObjects.clear();
matricesMemAllocs.clear();
scTransMemAllocMap.clear();
}

template<typename T>
Expand All @@ -126,9 +126,9 @@ inline void CModelDrawerDataBase<T>::AddObject(const T* co, bool add)
unsortedObjects.emplace_back(o);

const uint32_t numMatrices = (o->model ? o->model->numPieces : 0) + 1u;
matricesMemAllocs.emplace(o, ScopedMatricesMemAlloc(numMatrices));
scTransMemAllocMap.emplace(o, ScopedTransformMemAlloc(numMatrices));

modelsUniformsStorage.GetObjOffset(co);
modelUniformsStorage.AddObject(co);
}

template<typename T>
Expand All @@ -141,8 +141,8 @@ inline void CModelDrawerDataBase<T>::DelObject(const T* co, bool del)
}

if (del && spring::VectorErase(unsortedObjects, o)) {
matricesMemAllocs.erase(o);
modelsUniformsStorage.GetObjOffset(co);
scTransMemAllocMap.erase(o);
modelUniformsStorage.DelObject(co);
}
}

Expand All @@ -157,15 +157,13 @@ inline void CModelDrawerDataBase<T>::UpdateObject(const T* co, bool init)
template<typename T>
inline void CModelDrawerDataBase<T>::UpdateObjectSMMA(const T* o)
{
ScopedMatricesMemAlloc& smma = GetObjectMatricesMemAlloc(o);
ScopedTransformMemAlloc& smma = GetObjectTransformMemAlloc(o);

const auto tmNew = o->GetTransformMatrix();
const auto& tmOld = const_cast<const ScopedMatricesMemAlloc&>(smma)[0];
const auto tmNew = Transform::FromMatrix(o->GetTransformMatrix());

// from one point it doesn't worth the comparison, cause units usually move
// but having not updated smma[0] allows for longer solid no-update areas in ModelsUniformsUploader::UpdateDerived()
if (tmNew != tmOld)
smma[0] = tmNew;
// but having not updated smma[0] allows for longer solid no-update areas in ModelUniformsUploader::UpdateDerived()
smma.UpdateIfChanged(0, tmNew);

for (int i = 0; i < o->localModel.pieces.size(); ++i) {
const LocalModelPiece& lmp = o->localModel.pieces[i];
Expand All @@ -175,22 +173,25 @@ inline void CModelDrawerDataBase<T>::UpdateObjectSMMA(const T* o)
continue;

if unlikely(!lmp.GetScriptVisible()) {
smma[i + 1] = CMatrix44f::Zero();
//smma[i + 1] = CMatrix44f::Zero();
smma.UpdateForced(i + 1, Transform::Zero());
continue;
}

smma[i + 1] = lmp.GetModelSpaceMatrix();
// UpdateIfChanged is not needed, wasCustomDirty takes that role
smma.UpdateForced(i + 1, lmp.GetModelSpaceTransform());
}
}

template<typename T>
inline void CModelDrawerDataBase<T>::UpdateObjectUniforms(const T* o)
{
auto& uni = modelsUniformsStorage.GetObjUniformsArray(o);
auto& uni = modelUniformsStorage.GetObjUniformsArray(o);
uni.drawFlag = o->drawFlag;

if (gu->spectatingFullView || o->IsInLosForAllyTeam(gu->myAllyTeam)) {
uni.id = o->id;
// TODO remove drawPos, replace with pos
uni.drawPos = float4{ o->drawPos, o->heading * math::PI / SPRING_MAX_HEADING };
uni.speed = o->speed;
uni.maxHealth = o->maxHealth;
Expand Down
2 changes: 1 addition & 1 deletion rts/Rendering/DebugColVolDrawer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ static void DrawObjectDebugPieces(const CSolidObject* o, const float4& defColor)
float4{ (1.0f - (hitDeltaTime / 150.0f)), 0.0f, 0.0f, 1.0f } :
defColor;

const CMatrix44f mp = mo * lmp->GetModelSpaceMatrix();
const CMatrix44f mp = mo * lmp->GetModelSpaceTransform().ToMatrix();
// factors in the volume offsets
DrawCollisionVolume(lmpVol, mp, curColor);
}
Expand Down
Loading

0 comments on commit 8371edb

Please sign in to comment.